2 * Copyright (C) ARM Limited 2013. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
11 #include "libsensors/sensors.h"
16 #include "SessionData.h"
20 HwmonCounter(HwmonCounter *next, int key, const sensors_chip_name *chip, const sensors_feature *feature);
23 HwmonCounter *getNext() const { return next; }
24 int getKey() const { return key; }
25 bool isEnabled() const { return enabled; }
26 const char *getName() const { return name; }
27 const char *getLabel() const { return label; }
28 const char *getTitle() const { return title; }
29 bool isDuplicate() const { return duplicate; }
30 const char *getDisplay() const { return display; }
31 const char *getUnit() const { return unit; }
32 int getModifier() const { return modifier; }
34 void setEnabled(const bool enabled) {
35 this->enabled = enabled;
36 // canRead will clear enabled if the counter is not readable
44 void init(const sensors_chip_name *chip, const sensors_feature *feature);
46 HwmonCounter *const next;
54 const sensors_chip_name *chip;
55 const sensors_feature *feature;
63 double previous_value;
65 sensors_subfeature_type input;
67 // Intentionally unimplemented
68 HwmonCounter(const HwmonCounter &);
69 HwmonCounter &operator=(const HwmonCounter &);
72 HwmonCounter::HwmonCounter(HwmonCounter *next, int key, const sensors_chip_name *chip, const sensors_feature *feature) : next(next), key(key), polled(false), readable(false), enabled(false), duplicate(false), chip(chip), feature(feature) {
74 int len = sensors_snprintf_chip_name(NULL, 0, chip) + 1;
75 char *chip_name = new char[len];
76 sensors_snprintf_chip_name(chip_name, len, chip);
78 len = snprintf(NULL, 0, "hwmon_%s_%d", chip_name, feature->number) + 1;
80 snprintf(name, len, "hwmon_%s_%d", chip_name, feature->number);
84 label = sensors_get_label(chip, feature);
86 switch (feature->type) {
87 case SENSORS_FEATURE_IN:
89 input = SENSORS_SUBFEATURE_IN_INPUT;
95 case SENSORS_FEATURE_FAN:
97 input = SENSORS_SUBFEATURE_FAN_INPUT;
103 case SENSORS_FEATURE_TEMP:
104 title = "Temperature";
105 input = SENSORS_SUBFEATURE_TEMP_INPUT;
111 case SENSORS_FEATURE_POWER:
113 input = SENSORS_SUBFEATURE_POWER_INPUT;
119 case SENSORS_FEATURE_ENERGY:
121 input = SENSORS_SUBFEATURE_ENERGY_INPUT;
122 display = "accumulate";
127 case SENSORS_FEATURE_CURR:
129 input = SENSORS_SUBFEATURE_CURR_INPUT;
135 case SENSORS_FEATURE_HUMIDITY:
137 input = SENSORS_SUBFEATURE_HUMIDITY_INPUT;
144 logg->logError(__FILE__, __LINE__, "Unsupported hwmon feature %i", feature->type);
148 for (HwmonCounter * counter = next; counter != NULL; counter = counter->getNext()) {
149 if (strcmp(label, counter->getLabel()) == 0 && strcmp(title, counter->getTitle()) == 0) {
151 counter->duplicate = true;
157 HwmonCounter::~HwmonCounter() {
162 double HwmonCounter::read() {
165 const sensors_subfeature *subfeature;
167 // Keep in sync with canRead
168 subfeature = sensors_get_subfeature(chip, feature, input);
170 logg->logError(__FILE__, __LINE__, "No input value for hwmon sensor %s", label);
174 if (sensors_get_value(chip, subfeature->number, &value) != 0) {
175 logg->logError(__FILE__, __LINE__, "Can't get input value for hwmon sensor %s", label);
179 result = (monotonic ? value - previous_value : value);
180 previous_value = value;
185 bool HwmonCounter::canRead() {
188 const sensors_subfeature *subfeature;
191 subfeature = sensors_get_subfeature(chip, feature, input);
195 result = sensors_get_value(chip, subfeature->number, &value) == 0;
207 Hwmon::Hwmon() : counters(NULL) {
208 int err = sensors_init(NULL);
210 logg->logMessage("Failed to initialize libsensors! (%d)", err);
213 sensors_sysfs_no_scaling = 1;
216 const sensors_chip_name *chip;
217 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
219 const sensors_feature *feature;
220 while ((feature = sensors_get_features(chip, &feature_nr))) {
221 counters = new HwmonCounter(counters, getEventKey(), chip, feature);
227 while (counters != NULL) {
228 HwmonCounter * counter = counters;
229 counters = counter->getNext();
235 HwmonCounter *Hwmon::findCounter(const Counter &counter) const {
236 for (HwmonCounter * hwmonCounter = counters; hwmonCounter != NULL; hwmonCounter = hwmonCounter->getNext()) {
237 if (hwmonCounter->canRead() && strcmp(hwmonCounter->getName(), counter.getType()) == 0) {
245 bool Hwmon::claimCounter(const Counter &counter) const {
246 return findCounter(counter) != NULL;
249 bool Hwmon::countersEnabled() const {
250 for (HwmonCounter * counter = counters; counter != NULL; counter = counter->getNext()) {
251 if (counter->isEnabled()) {
258 void Hwmon::resetCounters() {
259 for (HwmonCounter * counter = counters; counter != NULL; counter = counter->getNext()) {
260 counter->setEnabled(false);
264 void Hwmon::setupCounter(Counter &counter) {
265 HwmonCounter *const hwmonCounter = findCounter(counter);
266 if (hwmonCounter == NULL) {
267 counter.setEnabled(false);
270 hwmonCounter->setEnabled(true);
271 counter.setKey(hwmonCounter->getKey());
274 void Hwmon::writeCounters(mxml_node_t *root) const {
275 for (HwmonCounter * counter = counters; counter != NULL; counter = counter->getNext()) {
276 if (!counter->canRead()) {
279 mxml_node_t *node = mxmlNewElement(root, "counter");
280 mxmlElementSetAttr(node, "name", counter->getName());
284 void Hwmon::writeEvents(mxml_node_t *root) const {
285 root = mxmlNewElement(root, "category");
286 mxmlElementSetAttr(root, "name", "hwmon");
289 for (HwmonCounter * counter = counters; counter != NULL; counter = counter->getNext()) {
290 if (!counter->canRead()) {
293 mxml_node_t *node = mxmlNewElement(root, "event");
294 mxmlElementSetAttr(node, "counter", counter->getName());
295 mxmlElementSetAttr(node, "title", counter->getTitle());
296 if (counter->isDuplicate()) {
297 mxmlElementSetAttrf(node, "name", "%s (0x%x)", counter->getLabel(), counter->getKey());
299 mxmlElementSetAttr(node, "name", counter->getLabel());
301 mxmlElementSetAttr(node, "display", counter->getDisplay());
302 mxmlElementSetAttr(node, "units", counter->getUnit());
303 if (counter->getModifier() != 1) {
304 mxmlElementSetAttrf(node, "modifier", "%d", counter->getModifier());
306 if (strcmp(counter->getDisplay(), "average") == 0 || strcmp(counter->getDisplay(), "maximum") == 0) {
307 mxmlElementSetAttr(node, "average_selection", "yes");
309 snprintf(buf, sizeof(buf), "libsensors %s sensor %s (%s)", counter->getTitle(), counter->getLabel(), counter->getName());
310 mxmlElementSetAttr(node, "description", buf);
314 void Hwmon::start() {
315 for (HwmonCounter * counter = counters; counter != NULL; counter = counter->getNext()) {
316 if (!counter->isEnabled()) {
323 void Hwmon::read(Buffer * const buffer) {
324 for (HwmonCounter * counter = counters; counter != NULL; counter = counter->getNext()) {
325 if (!counter->isEnabled()) {
328 buffer->event(counter->getKey(), counter->read());