/* $Id: plugin_i2c_sensors.c,v 1.1 2004/01/10 17:36:56 reinelt Exp $ * * I2C sensors plugin * * Copyright 2003,2004 Xavier Vello * * This file is part of LCD4Linux. * * LCD4Linux is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * LCD4Linux is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * * $Log: plugin_i2c_sensors.c,v $ * Revision 1.1 2004/01/10 17:36:56 reinelt * * I2C Sensors plugin from Xavier added * * */ /* * exported functions: * * int plugin_init_i2c_sensors (void) * adds function i2c_sensors() to retrieve informations from * the i2c sensors via the sysfs interface of 2.6.x kernels * * -- WARNING -- * This plugin is only for kernel series 2.5/2.6 and higher ! * It uses the new sysfs pseudo-filesystem that you can mount with: * mount -t sysfs none /sys * * -- WARNING #2 -- * This plugin should detect where your sensors are at startup. * If you can't get any token to work, ensure you don't get * an error message with "lcd4linux -F". * * If so, try to force the path to your sensors in the conf like this : * i2c_sensors.path /sys/bus/i2c/devices/0-6000/ * * - replace 0-6000 with the appropriate dir * - DON'T forget the trailing slash or it won't work ! * */ #include #include #include #include #include #include #include "debug.h" #include "plugin.h" #include "cfg.h" static char *path=NULL; static void my_i2c_sensors (RESULT *result, RESULT *arg) { int fd=-2; double value; char buffer[32]; char *key=R2S(arg); char *file; // construct absolute path to the file to read // Fixme: MR: free the path again?? file = strdup(path); file = realloc(file, strlen(path)+strlen(key)+1); file = strcat(file, key); // read of file to buffer fd = open(file, O_RDONLY); read (fd, &buffer, 31); close(fd); if (!buffer) { SetResult(&result, R_STRING, "??"); return; } // now the formating stuff, depending on the file wanted if (!strncmp(key, "temp_", 5) || !strncmp(key, "curr_", 5) || !strncmp(key, "in_", 3) || !strncmp(key, "vid", 3)) { value = strtod(buffer, NULL); value /= 1000.0; if (value) { SetResult(&result, R_NUMBER, &value); return; } } else if (!strncmp(key, "fan_", 4) || !strncmp(key, "pwn_", 4) || !strncmp(key, "vrm", 5)) { value = strtod(buffer, NULL); if (value) { SetResult(&result, R_NUMBER, &buffer); return; } } else { SetResult(&result, R_STRING, &buffer); return; } // fallback is there's a problem SetResult(&result, R_STRING, "??"); } void my_i2c_sensors_path(void) { struct dirent *dir; struct dirent *file; char *base="/sys/bus/i2c/devices/"; char dname[64]; DIR *fd1; DIR *fd2; int done; fd1 = opendir(base); while((dir = readdir(fd1))) { // Skip '.' and '..' if (strcmp(dir->d_name, "." )==0 || strcmp(dir->d_name, "..")==0) { continue; } // dname is the absolute path strcpy(dname, base); strcat(dname, dir->d_name); strcat(dname, "/"); fd2 = opendir(dname); done = 0; while((file = readdir(fd2))) { if (!strcmp(file->d_name, "temp_input1")) { // FIXME : do all sensors have a temp_input1 ? path = realloc(path, strlen(dname)); strcpy(path, dname); // we've got it ;) done=1; break; } } closedir(fd2); if (done) break; } closedir(fd1); // fallback is path undefined if (*path != '/') { error("[i2c_sensors] No i2c sensors found via the i2c interface !"); error("[i2c_sensors] Try to specify the path to the sensors !"); } } int plugin_init_i2c_sensors (void) { // char *path_cfg=cfg_get(NULL, "i", ""); // printf("%s\n", path_cfg); // if (strncmp(path_cfg, "/", 1)) { // printf("%s\n", "Calling my_i2c_sensors_path()"); my_i2c_sensors_path(); // } else { // path = realloc(path, strlen(path_cfg)+1); // strcpy(path, path_cfg); // } AddFunction ("i2c_sensors", 1, my_i2c_sensors); printf("%s\n", path); return 0; } ]reinelt21-1068/+1162 2000-08-09[lcd4linux @ 2000-08-09 14:14:11 by reinelt]reinelt2-11/+41 2000-08-09[lcd4linux @ 2000-08-09 11:03:07 by reinelt]reinelt1-13/+28 2000-08-09[lcd4linux @ 2000-08-09 09:50:29 by reinelt]reinelt13-127/+204 2000-07-31[lcd4linux @ 2000-07-31 10:43:44 by reinelt]reinelt8-91/+235 2000-07-31[lcd4linux @ 2000-07-31 06:46:35 by reinelt]reinelt4-5/+15 2000-06-04[lcd4linux @ 2000-06-04 21:43:50 by herp]herp1-2/+9 2000-05-21[lcd4linux @ 2000-05-21 06:20:35 by reinelt]reinelt7-35/+148 2000-05-03[lcd4linux @ 2000-05-03 17:14:51 by herp]herp1-1/+1 2000-05-03[lcd4linux @ 2000-05-03 09:37:32 by herp]herp3-0/+658 2000-05-02[lcd4linux @ 2000-05-02 23:07:48 by herp]herp4-15/+24 2000-05-02[lcd4linux @ 2000-05-02 06:05:00 by reinelt]reinelt5-7/+312 2000-04-30[lcd4linux @ 2000-04-30 06:40:42 by reinelt]reinelt2-17/+37 2000-04-28[lcd4linux @ 2000-04-28 05:19:55 by reinelt]reinelt5-12/+554 2000-04-20[lcd4linux @ 2000-04-20 05:48:42 by reinelt]reinelt2-0/+18 2000-04-19[lcd4linux @ 2000-04-19 04:44:20 by reinelt]reinelt1-2/+2 2000-04-17[lcd4linux @ 2000-04-17 05:14:27 by reinelt]reinelt3-5/+119 2000-04-15[lcd4linux @ 2000-04-15 16:56:52 by reinelt]reinelt7-58/+197 2000-04-15[lcd4linux @ 2000-04-15 11:56:35 by reinelt]reinelt4-10/+60 2000-04-15[lcd4linux @ 2000-04-15 11:13:54 by reinelt]reinelt10-28/+144 2000-04-13[lcd4linux @ 2000-04-13 06:09:52 by reinelt]reinelt5-54/+126 2000-04-12[lcd4linux @ 2000-04-12 08:05:45 by reinelt]reinelt6-24/+535 2000-04-10[lcd4linux @ 2000-04-10 04:40:53 by reinelt]reinelt5-31/+65 2000-04-07[lcd4linux @ 2000-04-07 05:42:20 by reinelt]reinelt6-17/+266 2000-04-05[lcd4linux @ 2000-04-05 05:58:36 by reinelt]reinelt4-17/+65