2 * collectd - src/statefs_battery.c
3 * Copyright (C) 2016 rinigus
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * rinigus <http://github.com/rinigus>
30 Battery stats are collected from StateFS Battery namespace. Reported
39 timefull and timelow seconds
43 https://git.merproject.org/mer-core/statefs-providers/blob/master/src/power_udev/provider_power_udev.cpp
53 #define STATEFS_ROOT "/run/state/namespaces/Battery/"
54 #define BUFFER_SIZE 512
56 static int submitted_this_run = 0;
58 static void battery_submit(const char *type, gauge_t value,
59 const char *type_instance) {
61 value_list_t vl = VALUE_LIST_INIT;
63 values[0].gauge = value;
67 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
68 sstrncpy(vl.plugin, "battery", sizeof(vl.plugin));
69 /* statefs supports 1 battery at present */
70 sstrncpy(vl.plugin_instance, "0", sizeof(vl.plugin_instance));
71 sstrncpy(vl.type, type, sizeof(vl.type));
72 if (type_instance != NULL)
73 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
74 plugin_dispatch_values(&vl);
79 static _Bool getvalue(const char *fname, gauge_t *value) {
81 char buffer[BUFFER_SIZE];
83 if ((fh = fopen(fname, "r")) == NULL) {
84 WARNING("battery plugin: cannot open StateFS file %s", fname);
88 if (fgets(buffer, STATIC_ARRAY_SIZE(buffer), fh) == NULL) {
90 return (0); // empty file
93 (*value) = atof(buffer);
100 /* cannot be static, is referred to from battery.c */
101 int battery_read_statefs(void) {
104 submitted_this_run = 0;
106 if (getvalue(STATEFS_ROOT "ChargePercentage", &value))
107 battery_submit("charge", value, NULL);
108 // Use capacity as a charge estimate if ChargePercentage is not available
109 else if (getvalue(STATEFS_ROOT "Capacity", &value))
110 battery_submit("charge", value, NULL);
112 if (getvalue(STATEFS_ROOT "Current", &value))
113 battery_submit("current", value * 1e-6, NULL); // from uA to A
115 if (getvalue(STATEFS_ROOT "Energy", &value))
116 battery_submit("energy_wh", value * 1e-6, NULL); // from uWh to Wh
118 if (getvalue(STATEFS_ROOT "Power", &value))
119 battery_submit("power", value * 1e-6, NULL); // from uW to W
121 if (getvalue(STATEFS_ROOT "Temperature", &value))
122 battery_submit("temperature", value * 0.1, NULL); // from 10xC to C
124 if (getvalue(STATEFS_ROOT "TimeUntilFull", &value))
125 battery_submit("duration", value, "full");
127 if (getvalue(STATEFS_ROOT "TimeUntilLow", &value))
128 battery_submit("duration", value, "low");
130 if (getvalue(STATEFS_ROOT "Voltage", &value))
131 battery_submit("voltage", value * 1e-6, NULL); // from uV to V
133 if (submitted_this_run == 0) {
134 ERROR("battery plugin: statefs backend: none of the statistics are "