| 1 |
/* |
| 2 |
* Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style license that can be |
| 4 |
* found in the LICENSE file. |
| 5 |
*/ |
| 6 |
|
| 7 |
// Sensors related functions. Original code was refactored and moved |
| 8 |
// from psutil/_psutil_osx.c in 2023. This is the GIT blame before the move: |
| 9 |
// https://github.com/giampaolo/psutil/blame/efd7ed3/psutil/_psutil_osx.c |
| 10 |
// Original battery code: |
| 11 |
// https://github.com/giampaolo/psutil/commit/e0df5da |
| 12 |
|
| 13 |
|
| 14 |
#include <Python.h> |
| 15 |
#include <CoreFoundation/CoreFoundation.h> |
| 16 |
#include <IOKit/ps/IOPowerSources.h> |
| 17 |
#include <IOKit/ps/IOPSKeys.h> |
| 18 |
|
| 19 |
#include "../../arch/all/init.h" |
| 20 |
|
| 21 |
|
| 22 |
PyObject * |
| 23 |
psutil_sensors_battery(PyObject *self, PyObject *args) { |
| 24 |
PyObject *py_tuple = NULL; |
| 25 |
CFTypeRef power_info = NULL; |
| 26 |
CFArrayRef power_sources_list = NULL; |
| 27 |
CFDictionaryRef power_sources_information = NULL; |
| 28 |
CFNumberRef capacity_ref = NULL; |
| 29 |
CFNumberRef time_to_empty_ref = NULL; |
| 30 |
CFStringRef ps_state_ref = NULL; |
| 31 |
uint32_t capacity; /* units are percent */ |
| 32 |
int time_to_empty; /* units are minutes */ |
| 33 |
int is_power_plugged; |
| 34 |
|
| 35 |
power_info = IOPSCopyPowerSourcesInfo(); |
| 36 |
|
| 37 |
if (!power_info) { |
| 38 |
PyErr_SetString(PyExc_RuntimeError, |
| 39 |
"IOPSCopyPowerSourcesInfo() syscall failed"); |
| 40 |
goto error; |
| 41 |
} |
| 42 |
|
| 43 |
power_sources_list = IOPSCopyPowerSourcesList(power_info); |
| 44 |
if (!power_sources_list) { |
| 45 |
PyErr_SetString(PyExc_RuntimeError, |
| 46 |
"IOPSCopyPowerSourcesList() syscall failed"); |
| 47 |
goto error; |
| 48 |
} |
| 49 |
|
| 50 |
/* Should only get one source. But in practice, check for > 0 sources */ |
| 51 |
if (!CFArrayGetCount(power_sources_list)) { |
| 52 |
PyErr_SetString(PyExc_NotImplementedError, "no battery"); |
| 53 |
goto error; |
| 54 |
} |
| 55 |
|
| 56 |
power_sources_information = IOPSGetPowerSourceDescription( |
| 57 |
power_info, CFArrayGetValueAtIndex(power_sources_list, 0)); |
| 58 |
if (!power_sources_information) { |
| 59 |
PyErr_SetString( |
| 60 |
PyExc_RuntimeError, "Failed to get power source description" |
| 61 |
); |
| 62 |
goto error; |
| 63 |
} |
| 64 |
|
| 65 |
capacity_ref = (CFNumberRef)CFDictionaryGetValue( |
| 66 |
power_sources_information, CFSTR(kIOPSCurrentCapacityKey)); |
| 67 |
if (!capacity_ref || !CFNumberGetValue(capacity_ref, kCFNumberSInt32Type, &capacity)) { |
| 68 |
PyErr_SetString(PyExc_RuntimeError, |
| 69 |
"No battery capacity information in power sources info"); |
| 70 |
goto error; |
| 71 |
} |
| 72 |
|
| 73 |
ps_state_ref = (CFStringRef)CFDictionaryGetValue( |
| 74 |
power_sources_information, CFSTR(kIOPSPowerSourceStateKey)); |
| 75 |
if (!ps_state_ref) { |
| 76 |
PyErr_SetString(PyExc_RuntimeError, "power source state info missing"); |
| 77 |
goto error; |
| 78 |
} |
| 79 |
is_power_plugged = CFStringCompare( |
| 80 |
ps_state_ref, CFSTR(kIOPSACPowerValue), 0) == kCFCompareEqualTo; |
| 81 |
|
| 82 |
time_to_empty_ref = (CFNumberRef)CFDictionaryGetValue( |
| 83 |
power_sources_information, CFSTR(kIOPSTimeToEmptyKey)); |
| 84 |
if (!time_to_empty_ref || !CFNumberGetValue(time_to_empty_ref, |
| 85 |
kCFNumberIntType, &time_to_empty)) { |
| 86 |
/* This value is recommended for non-Apple power sources, so it's not |
| 87 |
* an error if it doesn't exist. We'll return -1 for "unknown" */ |
| 88 |
/* A value of -1 indicates "Still Calculating the Time" also for |
| 89 |
* apple power source */ |
| 90 |
time_to_empty = -1; |
| 91 |
} |
| 92 |
|
| 93 |
py_tuple = Py_BuildValue("Iii", |
| 94 |
capacity, time_to_empty, is_power_plugged); |
| 95 |
if (!py_tuple) { |
| 96 |
goto error; |
| 97 |
} |
| 98 |
|
| 99 |
CFRelease(power_info); |
| 100 |
CFRelease(power_sources_list); |
| 101 |
/* Caller should NOT release power_sources_information */ |
| 102 |
|
| 103 |
return py_tuple; |
| 104 |
|
| 105 |
error: |
| 106 |
if (power_info) |
| 107 |
CFRelease(power_info); |
| 108 |
if (power_sources_list) |
| 109 |
CFRelease(power_sources_list); |
| 110 |
Py_XDECREF(py_tuple); |
| 111 |
return NULL; |
| 112 |
} |