2022-09-16 11:03:46 +08:00
|
|
|
#include "battery.h"
|
2023-11-06 19:08:17 +08:00
|
|
|
#include "common/io/io.h"
|
|
|
|
|
#include "detection/temps/temps_linux.h"
|
2023-06-19 16:07:23 +08:00
|
|
|
#include "util/stringUtils.h"
|
2022-09-16 11:03:46 +08:00
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
2023-11-06 19:08:17 +08:00
|
|
|
static void parseBattery(FFstrbuf* dir, const char* id, FFBatteryOptions* options, FFlist* results)
|
2022-09-16 11:03:46 +08:00
|
|
|
{
|
|
|
|
|
uint32_t dirLength = dir->length;
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY testBatteryBuffer = ffStrbufCreate();
|
2022-09-16 11:03:46 +08:00
|
|
|
|
|
|
|
|
//type must exist and be "Battery"
|
|
|
|
|
ffStrbufAppendS(dir, "/type");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &testBatteryBuffer);
|
2023-12-23 04:55:14 +09:00
|
|
|
ffStrbufTrimRightSpace(&testBatteryBuffer);
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Battery") != 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//scope may not exist or must not be "Device"
|
|
|
|
|
ffStrbufAppendS(dir, "/scope");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &testBatteryBuffer);
|
2023-12-23 04:55:14 +09:00
|
|
|
ffStrbufTrimRightSpace(&testBatteryBuffer);
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Device") == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-08-30 11:17:51 +08:00
|
|
|
FFBatteryResult* result = ffListAdd(results);
|
2022-09-16 11:03:46 +08:00
|
|
|
|
|
|
|
|
//capacity must exist and be not empty
|
|
|
|
|
ffStrbufAppendS(dir, "/capacity");
|
2022-10-18 22:55:28 +08:00
|
|
|
bool available = ffReadFileBuffer(dir->chars, &testBatteryBuffer);
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
2022-10-18 22:55:28 +08:00
|
|
|
if(available)
|
|
|
|
|
result->capacity = ffStrbufToDouble(&testBatteryBuffer);
|
2023-04-15 15:29:54 +08:00
|
|
|
|
2022-10-18 22:55:28 +08:00
|
|
|
if(!available)
|
2022-09-16 11:03:46 +08:00
|
|
|
{
|
2022-10-18 22:55:28 +08:00
|
|
|
result->capacity = 0.0/0.0;
|
2022-09-16 11:03:46 +08:00
|
|
|
--results->length;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//At this point, we have a battery. Try to get as much values as possible.
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->manufacturer);
|
|
|
|
|
ffStrbufAppendS(dir, "/manufacturer");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->manufacturer);
|
2023-12-23 04:55:14 +09:00
|
|
|
ffStrbufTrimRightSpace(&result->manufacturer);
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->modelName);
|
|
|
|
|
ffStrbufAppendS(dir, "/model_name");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->modelName);
|
2023-12-23 04:55:14 +09:00
|
|
|
ffStrbufTrimRightSpace(&result->modelName);
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->technology);
|
|
|
|
|
ffStrbufAppendS(dir, "/technology");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->technology);
|
2023-12-23 04:55:14 +09:00
|
|
|
ffStrbufTrimRightSpace(&result->technology);
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->status);
|
|
|
|
|
ffStrbufAppendS(dir, "/status");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->status);
|
2023-12-23 04:55:14 +09:00
|
|
|
ffStrbufTrimRightSpace(&result->status);
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
2022-09-25 15:23:00 +08:00
|
|
|
|
2023-11-06 19:08:17 +08:00
|
|
|
ffStrbufAppendS(dir, "/cycle_count");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &testBatteryBuffer);
|
2023-12-23 04:55:14 +09:00
|
|
|
ffStrbufTrimRightSpace(&testBatteryBuffer);
|
2023-11-06 19:08:17 +08:00
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
2023-12-25 11:52:30 +09:00
|
|
|
if (dir->length)
|
|
|
|
|
{
|
|
|
|
|
int64_t cycleCount = 0;
|
|
|
|
|
cycleCount = (int64_t) strtoll(testBatteryBuffer.chars, NULL, 10);
|
|
|
|
|
result->cycleCount = cycleCount < 0 || cycleCount > UINT32_MAX ? 0 : (uint32_t) cycleCount;
|
|
|
|
|
}
|
2023-11-06 19:08:17 +08:00
|
|
|
|
2022-09-25 15:23:00 +08:00
|
|
|
result->temperature = FF_BATTERY_TEMP_UNSET;
|
2023-11-06 19:08:17 +08:00
|
|
|
if (options->temp)
|
|
|
|
|
{
|
|
|
|
|
const FFlist* tempsResult = ffDetectTemps();
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFTempValue, value, *tempsResult)
|
|
|
|
|
{
|
2023-12-26 16:43:44 +08:00
|
|
|
if (ffStrbufEqualS(&value->deviceName, id))
|
2023-11-06 19:08:17 +08:00
|
|
|
{
|
|
|
|
|
result->temperature = value->value;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-16 11:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
|
2023-03-07 16:13:53 +08:00
|
|
|
{
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY baseDir = ffStrbufCreateA(64);
|
2024-01-04 14:18:06 +08:00
|
|
|
ffStrbufAppend(&baseDir, "/sys/class/power_supply/");
|
2022-09-16 11:03:46 +08:00
|
|
|
|
|
|
|
|
uint32_t baseDirLength = baseDir.length;
|
|
|
|
|
|
2023-11-06 19:08:17 +08:00
|
|
|
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(baseDir.chars);
|
2022-09-16 11:03:46 +08:00
|
|
|
if(dirp == NULL)
|
2024-01-04 14:18:06 +08:00
|
|
|
return "opendir(\"/sys/class/power_supply/\") == NULL";
|
2022-09-16 11:03:46 +08:00
|
|
|
|
|
|
|
|
struct dirent* entry;
|
|
|
|
|
while((entry = readdir(dirp)) != NULL)
|
|
|
|
|
{
|
2023-06-19 16:07:23 +08:00
|
|
|
if(ffStrEquals(entry->d_name, ".") || ffStrEquals(entry->d_name, ".."))
|
2023-01-09 10:45:51 +08:00
|
|
|
continue;
|
|
|
|
|
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufAppendS(&baseDir, entry->d_name);
|
2023-11-06 19:08:17 +08:00
|
|
|
parseBattery(&baseDir, entry->d_name, options, results);
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufSubstrBefore(&baseDir, baseDirLength);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
if(results->length == 0)
|
2024-01-04 14:18:06 +08:00
|
|
|
return "\"/sys/class/power_supply/\" doesn't contain any battery folder";
|
2022-09-16 11:03:46 +08:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|