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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*******************************************************************************
* Copyright (C) 2010, Linaro Limited.
*
* This file is part of PowerDebug.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Amit Arora <amit.arora@linaro.org> (IBM Corporation)
* - initial API and implementation
*******************************************************************************/
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <sys/types.h>
#include <stdbool.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include "powerdebug.h"
#include "display.h"
#include "sensor.h"
#include "tree.h"
#include "utils.h"
#define SYSFS_SENSOR "/sys/class/hwmon"
static struct tree *sensor_tree;
struct temp_info {
char name[NAME_MAX];
int temp;
};
struct fan_info {
char name[NAME_MAX];
int rpms;
};
struct sensor_info {
char name[NAME_MAX];
struct temp_info *temperatures;
struct fan_info *fans;
short nrtemps;
short nrfans;
};
static int sensor_dump_cb(struct tree *tree, void *data)
{
int i;
struct sensor_info *sensor = tree->private;
if (!strlen(sensor->name))
return 0;
printf("%s\n", sensor->name);
for (i = 0; i < sensor->nrtemps; i++)
printf(" %s %.1f °C/V\n", sensor->temperatures[i].name,
(float)sensor->temperatures[i].temp / 1000);
for (i = 0; i < sensor->nrfans; i++)
printf(" %s %d rpm\n", sensor->fans[i].name,
sensor->fans[i].rpms);
return 0;
}
int sensor_dump(void)
{
printf("\nSensor Information:\n");
printf("*******************\n\n");
return tree_for_each(sensor_tree, sensor_dump_cb, NULL);
}
static struct sensor_info *sensor_alloc(void)
{
struct sensor_info *sensor;
sensor = malloc(sizeof(*sensor));
if (sensor)
memset(sensor, 0, sizeof(*sensor));
return sensor;
}
static int read_sensor_cb(struct tree *tree, void *data)
{
DIR *dir;
int value;
struct dirent dirent, *direntp;
struct sensor_info *sensor = tree->private;
int nrtemps = 0;
int nrfans = 0;
dir = opendir(tree->path);
if (!dir)
return -1;
file_read_value(tree->path, "name", "%s", sensor->name);
while (!readdir_r(dir, &dirent, &direntp)) {
if (!direntp)
break;
if (direntp->d_type != DT_REG)
continue;
if (!strncmp(direntp->d_name, "temp", 4)) {
if (file_read_value(tree->path, direntp->d_name, "%d",
&value))
continue;
sensor->temperatures =
realloc(sensor->temperatures,
sizeof(struct temp_info) * (nrtemps + 1));
if (!sensor->temperatures)
continue;
strcpy(sensor->temperatures[nrtemps].name,
direntp->d_name);
sensor->temperatures[nrtemps].temp = value;
nrtemps++;
}
if (!strncmp(direntp->d_name, "fan", 3)) {
if (file_read_value(tree->path, direntp->d_name, "%d",
&value))
continue;
sensor->fans =
realloc(sensor->fans,
sizeof(struct temp_info) * (nrfans + 1));
if (!sensor->fans)
continue;
strcpy(sensor->fans[nrfans].name,
direntp->d_name);
sensor->fans[nrfans].rpms = value;
nrfans++;
}
}
sensor->nrtemps = nrtemps;
sensor->nrfans = nrfans;
closedir(dir);
return 0;
}
static int fill_sensor_cb(struct tree *t, void *data)
{
struct sensor_info *sensor;
sensor = sensor_alloc();
if (!sensor)
return -1;
t->private = sensor;
if (!t->parent)
return 0;
return read_sensor_cb(t, data);
}
static int fill_sensor_tree(void)
{
return tree_for_each(sensor_tree, fill_sensor_cb, NULL);
}
static int sensor_filter_cb(const char *name)
{
/* let's ignore some directories in order to avoid to be
* pulled inside the sysfs circular symlinks mess/hell
* (choose the word which fit better)
*/
if (!strcmp(name, "subsystem"))
return 1;
if (!strcmp(name, "driver"))
return 1;
if (!strcmp(name, "hwmon"))
return 1;
if (!strcmp(name, "power"))
return 1;
return 0;
}
static int sensor_display_cb(struct tree *t, void *data)
{
struct sensor_info *sensor = t->private;
int *line = data;
char buf[1024];
int i;
if (!strlen(sensor->name))
return 0;
sprintf(buf, "%s", sensor->name);
display_print_line(SENSOR, *line, buf, 1, t);
(*line)++;
for (i = 0; i < sensor->nrtemps; i++) {
sprintf(buf, " %-35s%.1f", sensor->temperatures[i].name,
(float)sensor->temperatures[i].temp / 1000);
display_print_line(SENSOR, *line, buf, 0, t);
(*line)++;
}
for (i = 0; i < sensor->nrfans; i++) {
sprintf(buf, " %-35s%d rpm", sensor->fans[i].name,
sensor->fans[i].rpms);
display_print_line(SENSOR, *line, buf, 0, t);
(*line)++;
}
return 0;
}
static int sensor_print_header(void)
{
char *buf;
int ret;
if (asprintf(&buf, "%-36s%s", "Name", "Value") < 0)
return -1;
ret = display_column_name(buf);
free(buf);
return ret;
}
static int sensor_display(bool refresh)
{
int ret, line = 0;
display_reset_cursor(SENSOR);
sensor_print_header();
ret = tree_for_each(sensor_tree, sensor_display_cb, &line);
display_refresh_pad(SENSOR);
return ret;
}
static struct display_ops sensor_ops = {
.display = sensor_display,
};
int sensor_init(void)
{
sensor_tree = tree_load(SYSFS_SENSOR, sensor_filter_cb, false);
if (!sensor_tree)
return -1;
if (fill_sensor_tree())
return -1;
return display_register(SENSOR, &sensor_ops);
}
|