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);
|
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->modelName);
|
|
|
|
|
ffStrbufAppendS(dir, "/model_name");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->modelName);
|
|
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->technology);
|
|
|
|
|
ffStrbufAppendS(dir, "/technology");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->technology);
|
|
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->status);
|
|
|
|
|
ffStrbufAppendS(dir, "/status");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->status);
|
|
|
|
|
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);
|
|
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
if(dir->length)
|
2023-11-06 19:39:08 +08:00
|
|
|
result->cycleCount = (uint32_t) ffStrbufToUInt(&testBatteryBuffer, 0);
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (ffStrbufEqualS(&value->name, id))
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
2023-06-12 09:20:39 +08:00
|
|
|
if(options->dir.length > 0)
|
2022-09-16 11:03:46 +08:00
|
|
|
{
|
2023-06-12 09:20:39 +08:00
|
|
|
ffStrbufAppend(&baseDir, &options->dir);
|
2022-09-17 00:34:37 +08:00
|
|
|
ffStrbufEnsureEndsWithC(&baseDir, '/');
|
2022-09-16 11:03:46 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&baseDir, "/sys/class/power_supply/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
return "opendir(batteryDir) == NULL";
|
|
|
|
|
|
|
|
|
|
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)
|
2022-09-16 11:03:46 +08:00
|
|
|
return "batteryDir doesn't contain any battery folder";
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|