summaryrefslogtreecommitdiffstats
path: root/src/detection/poweradapter/poweradapter_linux.c
blob: fb555d53840ae1e603a4256c2112f811681ad79c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "poweradapter.h"
#include "common/io.h"
#include "common/strutil.h"

#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>

static void parsePowerAdapter(int dfd, const char* id, FFlist* results) {
    FF_STRBUF_AUTO_DESTROY tmpBuffer = ffStrbufCreate();

    {
        char present = '\0';
        if (ffReadFileDataRelative(dfd, "present", 1, &present) && present == '0') {
            return;
        }
    }

    // type must exist and be "Mains"
    if (ffReadFileBufferRelative(dfd, "type", &tmpBuffer)) {
        ffStrbufTrimRightSpace(&tmpBuffer);
    }

    if (!ffStrbufEqualS(&tmpBuffer, "Mains")) {
        return;
    }

    // scope may not exist or must not be "Device" (?)
    if (ffReadFileBufferRelative(dfd, "scope", &tmpBuffer)) {
        ffStrbufTrimRightSpace(&tmpBuffer);

        if (ffStrbufEqualS(&tmpBuffer, "Device")) {
            return;
        }
    }

    char online = '\0';
    ffReadFileDataRelative(dfd, "online", sizeof(online), &online);

    if (online != '1') {
        return;
    }

    // input_power_limit must exist and be not empty
    if (!ffReadFileBufferRelative(dfd, "input_power_limit", &tmpBuffer) || tmpBuffer.length == 0) {
        return;
    }

    FFPowerAdapterResult* result = FF_LIST_ADD(FFPowerAdapterResult, *results);
    ffStrbufInit(&result->name);
    ffStrbufInit(&result->description);
    result->watts = (int) (ffStrbufToDouble(&tmpBuffer, 0) / 1e6 + 0.5);
    ffStrbufInit(&result->manufacturer);
    ffStrbufInit(&result->modelName);
    ffStrbufInit(&result->serial);

    if (ffReadFileBufferRelative(dfd, "manufacturer", &result->manufacturer)) {
        ffStrbufTrimRightSpace(&result->manufacturer);
    } else if (ffStrEquals(id, "macsmc-ac")) { // asahi
        ffStrbufSetStatic(&result->manufacturer, "Apple Inc.");
    }

    if (ffReadFileBufferRelative(dfd, "model_name", &result->modelName)) {
        ffStrbufTrimRightSpace(&result->modelName);
    }

    if (ffReadFileBufferRelative(dfd, "serial_number", &result->serial)) {
        ffStrbufTrimRightSpace(&result->serial);
    }
}

const char* ffDetectPowerAdapter(FFlist* results) {
    FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/power_supply/");
    if (dirp == NULL) {
        return "opendir(\"/sys/class/power_supply/\") == NULL";
    }

    struct dirent* entry;
    while ((entry = readdir(dirp)) != NULL) {
        if (entry->d_name[0] == '.') {
            continue;
        }

        FF_AUTO_CLOSE_FD int dfd = openat(dirfd(dirp), entry->d_name, O_RDONLY | O_CLOEXEC);
        if (dfd > 0) {
            parsePowerAdapter(dfd, entry->d_name, results);
        }
    }

    return NULL;
}