1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2007 Toni Spets
14 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
16 * All rights reserved.
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 #include "temphelper.h"
44 #include <sys/types.h>
45 #include <sys/sysinfo.h>
47 #ifndef HAVE_CLOCK_GETTIME
52 // #include <assert.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <linux/sockios.h>
61 #include <arpa/inet.h>
65 #include <linux/route.h>
69 /* The following ifdefs were adapted from gkrellm */
70 #include <linux/major.h>
72 #if !defined(MD_MAJOR)
76 #if !defined(LVM_BLK_MAJOR)
77 #define LVM_BLK_MAJOR 58
80 #if !defined(NBD_MAJOR)
88 #include <dbus/dbus.h>
98 #define SHORTSTAT_TEMPL "%*s %llu %llu %llu"
99 #define LONGSTAT_TEMPL "%*s %llu %llu %llu "
101 /* This flag tells the linux routines to use the /proc system where possible,
102 * even if other api's are available, e.g. sysinfo() or getloadavg().
103 * the reason for this is to allow for /proc-based distributed monitoring.
104 * using a flag in this manner creates less confusing code. */
105 static int prefer_proc = 0;
107 void prepare_update(void)
111 int update_uptime(void)
115 struct sysinfo s_info;
118 info.uptime = (double) s_info.uptime;
125 if (!(fp = open_file("/proc/uptime", &rep))) {
129 fscanf(fp, "%lf", &info.uptime);
135 int check_mount(char *s)
138 FILE *mtab = fopen("/etc/mtab", "r");
141 char buf1[256], buf2[128];
143 while (fgets(buf1, 256, mtab)) {
144 sscanf(buf1, "%*s %128s", buf2);
145 if (!strcmp(s, buf2)) {
152 NORM_ERR("Could not open mtab");
157 /* these things are also in sysinfo except Buffers:
158 * (that's why I'm reading them from proc) */
160 int update_meminfo(void)
165 /* unsigned int a; */
168 info.mem = info.memmax = info.swap = info.swapfree = info.swapmax = info.bufmem =
169 info.buffers = info.cached = info.memfree = info.memeasyfree = 0;
171 if (!(meminfo_fp = open_file("/proc/meminfo", &rep))) {
175 while (!feof(meminfo_fp)) {
176 if (fgets(buf, 255, meminfo_fp) == NULL) {
180 if (strncmp(buf, "MemTotal:", 9) == 0) {
181 sscanf(buf, "%*s %llu", &info.memmax);
182 } else if (strncmp(buf, "MemFree:", 8) == 0) {
183 sscanf(buf, "%*s %llu", &info.memfree);
184 } else if (strncmp(buf, "SwapTotal:", 10) == 0) {
185 sscanf(buf, "%*s %llu", &info.swapmax);
186 } else if (strncmp(buf, "SwapFree:", 9) == 0) {
187 sscanf(buf, "%*s %llu", &info.swapfree);
188 } else if (strncmp(buf, "Buffers:", 8) == 0) {
189 sscanf(buf, "%*s %llu", &info.buffers);
190 } else if (strncmp(buf, "Cached:", 7) == 0) {
191 sscanf(buf, "%*s %llu", &info.cached);
195 info.mem = info.memmax - info.memfree;
196 info.memeasyfree = info.memfree;
197 info.swap = info.swapmax - info.swapfree;
199 info.bufmem = info.cached + info.buffers;
205 int get_laptop_mode(void)
210 if ((fp = fopen("/proc/sys/vm/laptop_mode", "r")) != NULL)
211 fscanf(fp, "%d\n", &val);
217 * # cat /sys/block/sda/queue/scheduler
218 * noop [anticipatory] cfq
220 char *get_ioscheduler(char *disk)
226 return strndup("n/a", text_buffer_size);
228 snprintf(buf, 127, "/sys/block/%s/queue/scheduler", disk);
229 if ((fp = fopen(buf, "r")) == NULL) {
230 return strndup("n/a", text_buffer_size);
233 fscanf(fp, "%127s", buf);
235 buf[strlen(buf) - 1] = '\0';
237 return strndup(buf + 1, text_buffer_size);
241 return strndup("n/a", text_buffer_size);
250 #define COND_FREE(x) if(x) free(x); x = 0
251 #define SAVE_SET_STRING(x, y) \
252 if (x && strcmp((char *)x, (char *)y)) { \
254 x = strndup("multiple", text_buffer_size); \
256 x = strndup(y, text_buffer_size); \
259 void update_gateway_info_failure(const char *reason)
264 //2 pointers to 1 location causes a crash when we try to free them both
265 gw_info.iface = strndup("failed", text_buffer_size);
266 gw_info.ip = strndup("failed", text_buffer_size);
270 /* Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT */
271 #define RT_ENTRY_FORMAT "%63s %lx %lx %x %*d %*d %*d %lx %*d %*d %*d\n"
273 int update_gateway_info(void)
278 unsigned long dest, gate, mask;
281 COND_FREE(gw_info.iface);
282 COND_FREE(gw_info.ip);
285 if ((fp = fopen("/proc/net/route", "r")) == NULL) {
286 update_gateway_info_failure("fopen()");
290 /* skip over the table header line, which is always present */
291 fscanf(fp, "%*[^\n]\n");
294 if(fscanf(fp, RT_ENTRY_FORMAT,
295 iface, &dest, &gate, &flags, &mask) != 5) {
296 update_gateway_info_failure("fscanf()");
299 if (!(dest || mask) && ((flags & RTF_GATEWAY) || !gate) ) {
301 SAVE_SET_STRING(gw_info.iface, iface)
303 SAVE_SET_STRING(gw_info.ip, inet_ntoa(ina))
310 void free_gateway_info(void)
316 memset(&gw_info, 0, sizeof(gw_info));
319 int gateway_exists(void)
321 return !!gw_info.count;
324 void print_gateway_iface(char *p, int p_max_size)
326 snprintf(p, p_max_size, "%s", gw_info.iface);
329 void print_gateway_ip(char *p, int p_max_size)
331 snprintf(p, p_max_size, "%s", gw_info.ip);
334 int update_net_stats(void)
338 static char first = 1;
340 // FIXME: arbitrary size chosen to keep code simple.
342 unsigned int curtmp1, curtmp2;
349 // wireless info variables
350 int skfd, has_bitrate = 0;
351 struct wireless_info *winfo;
356 delta = current_update_time - last_update_time;
357 if (delta <= 0.0001) {
361 /* open file and ignore first two lines */
362 if (!(net_dev_fp = open_file("/proc/net/dev", &rep))) {
367 fgets(buf, 255, net_dev_fp); /* garbage */
368 fgets(buf, 255, net_dev_fp); /* garbage (field names) */
370 /* read each interface */
371 for (i2 = 0; i2 < MAX_NET_INTERFACES; i2++) {
375 long long r, t, last_recv, last_trans;
377 if (fgets(buf, 255, net_dev_fp) == NULL) {
381 while (isspace((int) *p)) {
387 while (*p && *p != ':') {
396 ns = get_net_stat(s, NULL, NULL);
398 memset(&(ns->addr.sa_data), 0, 14);
400 memset(ns->addrs, 0, 17 * MAX_NET_INTERFACES + 1); /* Up to 17 chars per ip, max MAX_NET_INTERFACES interfaces. Nasty memory usage... */
402 last_recv = ns->recv;
403 last_trans = ns->trans;
405 /* bytes packets errs drop fifo frame compressed multicast|bytes ... */
406 sscanf(p, "%lld %*d %*d %*d %*d %*d %*d %*d %lld",
409 /* if recv or trans is less than last time, an overflow happened */
410 if (r < ns->last_read_recv) {
413 ns->recv += (r - ns->last_read_recv);
415 ns->last_read_recv = r;
417 if (t < ns->last_read_trans) {
420 ns->trans += (t - ns->last_read_trans);
422 ns->last_read_trans = t;
424 /*** ip addr patch ***/
425 i = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
427 conf.ifc_buf = malloc(sizeof(struct ifreq) * MAX_NET_INTERFACES);
428 conf.ifc_len = sizeof(struct ifreq) * MAX_NET_INTERFACES;
429 memset(conf.ifc_buf, 0, conf.ifc_len);
431 ioctl((long) i, SIOCGIFCONF, &conf);
433 for (k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) {
434 struct net_stat *ns2;
436 if (!(((struct ifreq *) conf.ifc_buf) + k))
440 ((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name, NULL, NULL);
441 ns2->addr = ((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.ifru_addr;
442 sprintf(temp_addr, "%u.%u.%u.%u, ",
443 ns2->addr.sa_data[2] & 255,
444 ns2->addr.sa_data[3] & 255,
445 ns2->addr.sa_data[4] & 255,
446 ns2->addr.sa_data[5] & 255);
447 if(NULL == strstr(ns2->addrs, temp_addr))
448 strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17);
455 /*** end ip addr patch ***/
458 /* calculate speeds */
459 ns->net_rec[0] = (ns->recv - last_recv) / delta;
460 ns->net_trans[0] = (ns->trans - last_trans) / delta;
467 #pragma omp parallel for reduction(+:curtmp1, curtmp2) schedule(dynamic,10)
468 #endif /* HAVE_OPENMP */
469 for (i = 0; i < info.net_avg_samples; i++) {
470 curtmp1 = curtmp1 + ns->net_rec[i];
471 curtmp2 = curtmp2 + ns->net_trans[i];
473 ns->recv_speed = curtmp1 / (double) info.net_avg_samples;
474 ns->trans_speed = curtmp2 / (double) info.net_avg_samples;
475 if (info.net_avg_samples > 1) {
477 #pragma omp parallel for schedule(dynamic,10)
478 #endif /* HAVE_OPENMP */
479 for (i = info.net_avg_samples; i > 1; i--) {
480 ns->net_rec[i - 1] = ns->net_rec[i - 2];
481 ns->net_trans[i - 1] = ns->net_trans[i - 2];
486 /* update wireless info */
487 winfo = malloc(sizeof(struct wireless_info));
488 memset(winfo, 0, sizeof(struct wireless_info));
490 skfd = iw_sockets_open();
491 if (iw_get_basic_config(skfd, s, &(winfo->b)) > -1) {
493 // set present winfo variables
494 if (iw_get_stats(skfd, s, &(winfo->stats),
495 &winfo->range, winfo->has_range) >= 0) {
496 winfo->has_stats = 1;
498 if (iw_get_range_info(skfd, s, &(winfo->range)) >= 0) {
499 winfo->has_range = 1;
501 if (iw_get_ext(skfd, s, SIOCGIWAP, &wrq) >= 0) {
502 winfo->has_ap_addr = 1;
503 memcpy(&(winfo->ap_addr), &(wrq.u.ap_addr), sizeof(sockaddr));
507 if (iw_get_ext(skfd, s, SIOCGIWRATE, &wrq) >= 0) {
508 memcpy(&(winfo->bitrate), &(wrq.u.bitrate), sizeof(iwparam));
509 iw_print_bitrate(ns->bitrate, 16, winfo->bitrate.value);
514 if (winfo->has_range && winfo->has_stats
515 && ((winfo->stats.qual.level != 0)
516 || (winfo->stats.qual.updated & IW_QUAL_DBM))) {
517 if (!(winfo->stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
518 ns->link_qual = winfo->stats.qual.qual;
519 ns->link_qual_max = winfo->range.max_qual.qual;
524 if (winfo->has_ap_addr) {
525 iw_sawap_ntop(&winfo->ap_addr, ns->ap);
529 if (winfo->b.has_essid) {
530 if (winfo->b.essid_on) {
531 snprintf(ns->essid, 32, "%s", winfo->b.essid);
533 snprintf(ns->essid, 32, "off/any");
537 snprintf(ns->mode, 16, "%s", iw_operation_mode[winfo->b.mode]);
539 iw_sockets_close(skfd);
551 int update_total_processes(void)
554 struct dirent *entry;
559 if (!(dir = opendir("/proc"))) {
562 while ((entry = readdir(dir))) {
564 /* Problem reading list of processes */
569 if (sscanf(entry->d_name, "%d%c", &ignore1, &ignore2) == 1) {
577 int update_threads(void)
581 struct sysinfo s_info;
584 info.threads = s_info.procs;
591 if (!(fp = open_file("/proc/loadavg", &rep))) {
595 fscanf(fp, "%*f %*f %*f %*d/%hu", &info.threads);
601 #define CPU_SAMPLE_COUNT 15
603 unsigned long long cpu_user;
604 unsigned long long cpu_system;
605 unsigned long long cpu_nice;
606 unsigned long long cpu_idle;
607 unsigned long long cpu_iowait;
608 unsigned long long cpu_irq;
609 unsigned long long cpu_softirq;
610 unsigned long long cpu_steal;
611 unsigned long long cpu_total;
612 unsigned long long cpu_active_total;
613 unsigned long long cpu_last_total;
614 unsigned long long cpu_last_active_total;
615 double cpu_val[CPU_SAMPLE_COUNT];
617 static short cpu_setup = 0;
619 /* Determine if this kernel gives us "extended" statistics information in
621 * Kernels around 2.5 and earlier only reported user, system, nice, and
622 * idle values in proc stat.
623 * Kernels around 2.6 and greater report these PLUS iowait, irq, softirq,
625 void determine_longstat(char *buf)
627 unsigned long long iowait = 0;
629 KFLAG_SETOFF(KFLAG_IS_LONGSTAT);
630 /* scanf will either return -1 or 1 because there is only 1 assignment */
631 if (sscanf(buf, "%*s %*d %*d %*d %*d %llu", &iowait) > 0) {
632 KFLAG_SETON(KFLAG_IS_LONGSTAT);
636 void get_cpu_count(void)
642 if (info.cpu_usage) {
646 if (!(stat_fp = open_file("/proc/stat", &rep))) {
652 while (!feof(stat_fp)) {
653 if (fgets(buf, 255, stat_fp) == NULL) {
657 if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) {
658 if (info.cpu_count == 0) {
659 determine_longstat(buf);
664 info.cpu_usage = malloc((info.cpu_count + 1) * sizeof(float));
669 #define TMPL_LONGSTAT "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
670 #define TMPL_SHORTSTAT "%*s %llu %llu %llu %llu"
672 int update_stat(void)
676 static struct cpu_info *cpu = NULL;
681 const char *stat_template = NULL;
682 unsigned int malloc_cpu_size = 0;
683 extern void* global_cpu;
685 static pthread_mutex_t last_stat_update_mutex = PTHREAD_MUTEX_INITIALIZER;
686 static double last_stat_update = 0.0;
688 /* since we use wrappers for this function, the update machinery
689 * can't eliminate double invocations of this function. Check for
690 * them here, otherwise cpu_usage counters are freaking out. */
691 pthread_mutex_lock(&last_stat_update_mutex);
692 if (last_stat_update == current_update_time) {
693 pthread_mutex_unlock(&last_stat_update_mutex);
696 last_stat_update = current_update_time;
697 pthread_mutex_unlock(&last_stat_update_mutex);
699 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
700 if (!cpu_setup || !info.cpu_usage) {
705 if (!stat_template) {
707 KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGSTAT : TMPL_SHORTSTAT;
711 malloc_cpu_size = (info.cpu_count + 1) * sizeof(struct cpu_info);
712 cpu = malloc(malloc_cpu_size);
713 memset(cpu, 0, malloc_cpu_size);
717 if (!(stat_fp = open_file("/proc/stat", &rep))) {
718 info.run_threads = 0;
719 if (info.cpu_usage) {
720 memset(info.cpu_usage, 0, info.cpu_count * sizeof(float));
726 while (!feof(stat_fp)) {
727 if (fgets(buf, 255, stat_fp) == NULL) {
731 if (strncmp(buf, "procs_running ", 14) == 0) {
732 sscanf(buf, "%*s %hu", &info.run_threads);
733 } else if (strncmp(buf, "cpu", 3) == 0) {
735 if (isdigit(buf[3])) {
736 idx = atoi(&buf[3]) + 1;
740 sscanf(buf, stat_template, &(cpu[idx].cpu_user),
741 &(cpu[idx].cpu_nice), &(cpu[idx].cpu_system),
742 &(cpu[idx].cpu_idle), &(cpu[idx].cpu_iowait),
743 &(cpu[idx].cpu_irq), &(cpu[idx].cpu_softirq),
744 &(cpu[idx].cpu_steal));
746 cpu[idx].cpu_total = cpu[idx].cpu_user + cpu[idx].cpu_nice +
747 cpu[idx].cpu_system + cpu[idx].cpu_idle +
748 cpu[idx].cpu_iowait + cpu[idx].cpu_irq +
749 cpu[idx].cpu_softirq + cpu[idx].cpu_steal;
751 cpu[idx].cpu_active_total = cpu[idx].cpu_total -
752 (cpu[idx].cpu_idle + cpu[idx].cpu_iowait);
754 delta = current_update_time - last_update_time;
756 if (delta <= 0.001) {
760 cpu[idx].cpu_val[0] = (cpu[idx].cpu_active_total -
761 cpu[idx].cpu_last_active_total) /
762 (float) (cpu[idx].cpu_total - cpu[idx].cpu_last_total);
765 #pragma omp parallel for reduction(+:curtmp) schedule(dynamic,10)
766 #endif /* HAVE_OPENMP */
767 for (i = 0; i < info.cpu_avg_samples; i++) {
768 curtmp = curtmp + cpu[idx].cpu_val[i];
770 /* TESTING -- I've removed this, because I don't think it is right.
771 * You shouldn't divide by the cpu count here ...
772 * removing for testing */
774 info.cpu_usage[idx] = curtmp / info.cpu_avg_samples /
777 info.cpu_usage[idx] = curtmp / info.cpu_avg_samples;
779 /* TESTING -- this line replaces the prev. "suspect" if/else */
780 info.cpu_usage[idx] = curtmp / info.cpu_avg_samples;
782 cpu[idx].cpu_last_total = cpu[idx].cpu_total;
783 cpu[idx].cpu_last_active_total = cpu[idx].cpu_active_total;
785 #pragma omp parallel for schedule(dynamic,10)
786 #endif /* HAVE_OPENMP */
787 for (i = info.cpu_avg_samples - 1; i > 0; i--) {
788 cpu[idx].cpu_val[i] = cpu[idx].cpu_val[i - 1];
796 int update_running_processes(void)
802 int update_cpu_usage(void)
808 int update_load_average(void)
810 #ifdef HAVE_GETLOADAVG
815 info.loadavg[0] = (float) v[0];
816 info.loadavg[1] = (float) v[1];
817 info.loadavg[2] = (float) v[2];
824 if (!(fp = open_file("/proc/loadavg", &rep))) {
825 info.loadavg[0] = info.loadavg[1] = info.loadavg[2] = 0.0;
828 fscanf(fp, "%f %f %f", &info.loadavg[0], &info.loadavg[1],
835 /***********************************************************/
836 /***********************************************************/
837 /***********************************************************/
839 static int no_dots(const struct dirent *d)
841 if (d->d_name[0] == '.') {
847 static int get_first_file_in_a_directory(const char *dir, char *s, int *rep)
849 struct dirent **namelist;
852 n = scandir(dir, &namelist, no_dots, alphasort);
855 NORM_ERR("scandir for %s: %s", dir, strerror(errno));
866 strncpy(s, namelist[0]->d_name, 255);
870 #pragma omp parallel for schedule(dynamic,10)
871 #endif /* HAVE_OPENMP */
872 for (i = 0; i < n; i++) {
881 static int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
882 int *divisor, char *devtype)
889 memset(buf, 0, sizeof(buf));
891 /* if device is NULL or *, get first */
892 if (dev == NULL || strcmp(dev, "*") == 0) {
895 if (!get_first_file_in_a_directory(dir, buf, &rep)) {
901 if (strcmp(dir, "/sys/class/hwmon/") == 0) {
903 /* buf holds result from get_first_file_in_a_directory() above,
904 * e.g. "hwmon0" -- append "/device" */
905 strcat(buf, "/device");
907 /* dev holds device number N as a string,
908 * e.g. "0", -- convert to "hwmon0/device" */
909 sprintf(buf, "hwmon%s/device", dev);
914 /* change vol to in, tempf to temp */
915 if (strcmp(type, "vol") == 0) {
917 } else if (strcmp(type, "tempf") == 0) {
922 snprintf(path, 255, "%s%s/%s%d_input", dir, dev, type, n);
924 /* first, attempt to open file in /device */
925 fd = open(path, O_RDONLY);
928 /* if it fails, strip the /device from dev and attempt again */
929 buf[strlen(buf) - 7] = 0;
930 snprintf(path, 255, "%s%s/%s%d_input", dir, dev, type, n);
931 fd = open(path, O_RDONLY);
933 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nplease check your device or remove this "
934 "var from "PACKAGE_NAME, path, strerror(errno));
938 strncpy(devtype, path, 255);
940 if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
941 || strcmp(type, "tempf") == 0) {
946 /* fan does not use *_div as a read divisor */
947 if (strcmp("fan", type) == 0) {
951 /* test if *_div file exist, open it and use it as divisor */
952 if (strcmp(type, "tempf") == 0) {
953 snprintf(path, 255, "%s%s/%s%d_div", dir, "one", "two", n);
955 snprintf(path, 255, "%s%s/%s%d_div", dir, dev, type, n);
958 divfd = open(path, O_RDONLY);
964 divn = read(divfd, divbuf, 63);
965 /* should read until n == 0 but I doubt that kernel will give these
966 * in multiple pieces. :) */
968 NORM_ERR("open_sysfs_sensor(): can't read from sysfs");
971 *divisor = atoi(divbuf);
979 static double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
987 lseek(*fd, 0, SEEK_SET);
993 n = read(*fd, buf, 63);
994 /* should read until n == 0 but I doubt that kernel will give these
995 * in multiple pieces. :) */
997 NORM_ERR("get_sysfs_info(): read from %s failed\n", devtype);
1006 *fd = open(devtype, O_RDONLY);
1008 NORM_ERR("can't open '%s': %s", devtype, strerror(errno));
1011 /* My dirty hack for computing CPU value
1012 * Filedil, from forums.gentoo.org */
1013 /* if (strstr(devtype, "temp1_input") != NULL) {
1014 return -15.096 + 1.4893 * (val / 1000.0);
1017 /* divide voltage and temperature by 1000 */
1018 /* or if any other divisor is given, use that */
1019 if (strcmp(type, "tempf") == 0) {
1021 return ((val / divisor + 40) * 9.0 / 5) - 40;
1022 } else if (divisor) {
1023 return ((val / 1000.0 + 40) * 9.0 / 5) - 40;
1025 return ((val + 40) * 9.0 / 5) - 40;
1029 return val / divisor;
1030 } else if (divisor) {
1031 return val / 1000.0;
1038 #define HWMON_RESET() {\
1043 static void parse_sysfs_sensor(struct text_object *obj, const char *arg, const char *path, const char *type)
1045 char buf1[64], buf2[64];
1046 float factor, offset;
1050 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1051 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1052 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1053 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1056 NORM_ERR("i2c failed to parse arguments");
1057 obj->type = OBJ_text;
1060 DBGP("parsed %s args: '%s' '%s' %d %f %f\n", type, buf1, buf2, n, factor, offset);
1061 sf = malloc(sizeof(struct sysfs));
1062 memset(sf, 0, sizeof(struct sysfs));
1063 sf->fd = open_sysfs_sensor(path, (*buf1) ? buf1 : 0, buf2, n,
1064 &sf->arg, sf->devtype);
1065 strncpy(sf->type, buf2, 63);
1066 sf->factor = factor;
1067 sf->offset = offset;
1068 obj->data.opaque = sf;
1071 #define PARSER_GENERATOR(name, path) \
1072 void parse_##name##_sensor(struct text_object *obj, const char *arg) \
1074 parse_sysfs_sensor(obj, arg, path, #name); \
1077 PARSER_GENERATOR(i2c, "/sys/bus/i2c/devices/")
1078 PARSER_GENERATOR(hwmon, "/sys/class/hwmon/")
1079 PARSER_GENERATOR(platform, "/sys/bus/platform/devices/")
1081 void print_sysfs_sensor(struct text_object *obj, char *p, int p_max_size)
1084 struct sysfs *sf = obj->data.opaque;
1089 r = get_sysfs_info(&sf->fd, sf->arg,
1090 sf->devtype, sf->type);
1092 r = r * sf->factor + sf->offset;
1094 if (!strncmp(sf->type, "temp", 4)) {
1095 temp_print(p, p_max_size, r, TEMP_CELSIUS);
1096 } else if (r >= 100.0 || r == 0) {
1097 snprintf(p, p_max_size, "%d", (int) r);
1099 snprintf(p, p_max_size, "%.1f", r);
1103 void free_sysfs_sensor(struct text_object *obj)
1105 struct sysfs *sf = obj->data.opaque;
1111 free(obj->data.opaque);
1112 obj->data.opaque = NULL;
1115 #define CPUFREQ_PREFIX "/sys/devices/system/cpu"
1116 #define CPUFREQ_POSTFIX "cpufreq/scaling_cur_freq"
1118 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
1119 char get_freq(char *p_client_buffer, size_t client_buffer_size,
1120 const char *p_format, int divisor, unsigned int cpu)
1128 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
1134 char current_freq_file[128];
1136 snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu - 1,
1138 f = fopen(current_freq_file, "r");
1140 /* if there's a cpufreq /sys node, read the current frequency from
1141 * this node and divide by 1000 to get Mhz. */
1142 if (fgets(s, sizeof(s), f)) {
1143 s[strlen(s) - 1] = '\0';
1144 freq = strtod(s, NULL);
1147 snprintf(p_client_buffer, client_buffer_size, p_format,
1148 (freq / 1000) / divisor);
1153 // open the CPU information file
1154 f = open_file("/proc/cpuinfo", &rep);
1156 perror(PACKAGE_NAME": Failed to access '/proc/cpuinfo' at get_freq()");
1161 while (fgets(s, sizeof(s), f) != NULL) {
1163 #if defined(__i386) || defined(__x86_64)
1164 // and search for the cpu mhz
1165 if (strncmp(s, "cpu MHz", 7) == 0 && cpu == 0) {
1167 #if defined(__alpha)
1168 // different on alpha
1169 if (strncmp(s, "cycle frequency [Hz]", 20) == 0 && cpu == 0) {
1171 // this is different on ppc for some reason
1172 if (strncmp(s, "clock", 5) == 0 && cpu == 0) {
1173 #endif // defined(__alpha)
1174 #endif // defined(__i386) || defined(__x86_64)
1176 // copy just the number
1177 strcpy(frequency, strchr(s, ':') + 2);
1178 #if defined(__alpha)
1180 frequency[strlen(frequency) - 6] = '\0';
1181 // kernel reports in Hz
1182 freq = strtod(frequency, NULL) / 1000000;
1185 frequency[strlen(frequency) - 1] = '\0';
1186 freq = strtod(frequency, NULL);
1190 if (strncmp(s, "processor", 9) == 0) {
1197 snprintf(p_client_buffer, client_buffer_size, p_format,
1198 (float) freq / divisor);
1202 #define CPUFREQ_VOLTAGE "cpufreq/scaling_voltages"
1204 /* /sys/devices/system/cpu/cpu0/cpufreq/scaling_voltages looks something
1214 * Peter Tarjan (ptarjan@citromail.hu) */
1216 /* return cpu voltage in mV (use divisor=1) or V (use divisor=1000) */
1217 static char get_voltage(char *p_client_buffer, size_t client_buffer_size,
1218 const char *p_format, int divisor, unsigned int cpu)
1224 char current_freq_file[128];
1227 /* build the voltage file name */
1229 snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu,
1232 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
1237 /* read the current cpu frequency from the /sys node */
1238 f = fopen(current_freq_file, "r");
1240 if (fgets(s, sizeof(s), f)) {
1241 s[strlen(s) - 1] = '\0';
1242 freq = strtod(s, NULL);
1246 fprintf(stderr, PACKAGE_NAME": Failed to access '%s' at ", current_freq_file);
1247 perror("get_voltage()");
1254 snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu,
1257 /* use the current cpu frequency to find the corresponding voltage */
1258 f = fopen(current_freq_file, "r");
1264 if (fgets(line, 255, f) == NULL) {
1267 sscanf(line, "%d %d", &freq_comp, &voltage);
1268 if (freq_comp == freq) {
1274 fprintf(stderr, PACKAGE_NAME": Failed to access '%s' at ", current_freq_file);
1275 perror("get_voltage()");
1281 snprintf(p_client_buffer, client_buffer_size, p_format,
1282 (float) voltage / divisor);
1286 void print_voltage_mv(struct text_object *obj, char *p, int p_max_size)
1290 ok = get_voltage(p, p_max_size, "%.0f", 1, obj->data.i);
1294 void print_voltage_v(struct text_object *obj, char *p, int p_max_size)
1298 ok = get_voltage(p, p_max_size, "%'.3f", 1000, obj->data.i);
1302 #define ACPI_FAN_DIR "/proc/acpi/fan/"
1304 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
1311 if (!p_client_buffer || client_buffer_size <= 0) {
1315 /* yeah, slow... :/ */
1316 if (!get_first_file_in_a_directory(ACPI_FAN_DIR, buf, &rep)) {
1317 snprintf(p_client_buffer, client_buffer_size, "no fans?");
1321 snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_FAN_DIR, buf);
1323 fp = open_file(buf2, &rep);
1325 snprintf(p_client_buffer, client_buffer_size,
1326 "can't open fan's state file");
1329 memset(buf, 0, sizeof(buf));
1330 fscanf(fp, "%*s %99s", buf);
1333 snprintf(p_client_buffer, client_buffer_size, "%s", buf);
1336 #define SYSFS_AC_ADAPTER_DIR "/sys/class/power_supply"
1337 #define ACPI_AC_ADAPTER_DIR "/proc/acpi/ac_adapter/"
1338 /* Linux 2.6.25 onwards ac adapter info is in
1339 /sys/class/power_supply/AC/
1340 On my system I get the following.
1341 /sys/class/power_supply/AC/uevent:
1342 PHYSDEVPATH=/devices/LNXSYSTM:00/device:00/PNP0A08:00/device:01/PNP0C09:00/ACPI0003:00
1345 POWER_SUPPLY_NAME=AC
1346 POWER_SUPPLY_TYPE=Mains
1347 POWER_SUPPLY_ONLINE=1
1349 Update: it seems the folder name is hardware-dependent. We add an aditional adapter
1350 argument, specifying the folder name.
1352 Update: on some systems it's /sys/class/power_supply/ADP1 instead of /sys/class/power_supply/AC
1355 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter)
1364 if (!p_client_buffer || client_buffer_size <= 0) {
1369 snprintf(buf2, sizeof(buf2), "%s/%s/uevent", SYSFS_AC_ADAPTER_DIR, adapter);
1371 snprintf(buf2, sizeof(buf2), "%s/AC/uevent", SYSFS_AC_ADAPTER_DIR);
1372 if(stat(buf2, &sb) == -1) snprintf(buf2, sizeof(buf2), "%s/ADP1/uevent", SYSFS_AC_ADAPTER_DIR);
1374 if(stat(buf2, &sb) == 0) fp = open_file(buf2, &rep); else fp = 0;
1376 /* sysfs processing */
1378 if (fgets(buf, sizeof(buf), fp) == NULL)
1381 if (strncmp(buf, "POWER_SUPPLY_ONLINE=", 20) == 0) {
1383 sscanf(buf, "POWER_SUPPLY_ONLINE=%d", &online);
1384 snprintf(p_client_buffer, client_buffer_size,
1385 "%s-line", (online ? "on" : "off"));
1391 /* yeah, slow... :/ */
1392 if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &rep)) {
1393 snprintf(p_client_buffer, client_buffer_size, "no ac_adapters?");
1397 snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf);
1399 fp = open_file(buf2, &rep);
1401 snprintf(p_client_buffer, client_buffer_size,
1402 "No ac adapter found.... where is it?");
1405 memset(buf, 0, sizeof(buf));
1406 fscanf(fp, "%*s %99s", buf);
1409 snprintf(p_client_buffer, client_buffer_size, "%s", buf);
1414 /proc/acpi/thermal_zone/THRM/cooling_mode
1415 cooling mode: active
1416 /proc/acpi/thermal_zone/THRM/polling_frequency
1418 /proc/acpi/thermal_zone/THRM/state
1420 /proc/acpi/thermal_zone/THRM/temperature
1422 /proc/acpi/thermal_zone/THRM/trip_points
1424 passive: 73 C: tc1=4 tc2=3 tsp=40 devices=0xcdf6e6c0
1427 #define ACPI_THERMAL_DIR "/proc/acpi/thermal_zone/"
1428 #define ACPI_THERMAL_FORMAT "/proc/acpi/thermal_zone/%s/temperature"
1430 int open_acpi_temperature(const char *name)
1436 if (name == NULL || strcmp(name, "*") == 0) {
1439 if (!get_first_file_in_a_directory(ACPI_THERMAL_DIR, buf, &rep)) {
1445 snprintf(path, 255, ACPI_THERMAL_FORMAT, name);
1447 fd = open(path, O_RDONLY);
1449 NORM_ERR("can't open '%s': %s", path, strerror(errno));
1455 static double last_acpi_temp;
1456 static double last_acpi_temp_time;
1458 double get_acpi_temperature(int fd)
1464 /* don't update acpi temperature too often */
1465 if (current_update_time - last_acpi_temp_time < 11.32) {
1466 return last_acpi_temp;
1468 last_acpi_temp_time = current_update_time;
1470 /* seek to beginning */
1471 lseek(fd, 0, SEEK_SET);
1478 n = read(fd, buf, 255);
1480 NORM_ERR("can't read fd %d: %s", fd, strerror(errno));
1483 sscanf(buf, "temperature: %lf", &last_acpi_temp);
1487 return last_acpi_temp;
1491 hipo@lepakko hipo $ cat /proc/acpi/battery/BAT1/info
1493 design capacity: 4400 mAh
1494 last full capacity: 4064 mAh
1495 battery technology: rechargeable
1496 design voltage: 14800 mV
1497 design capacity warning: 300 mAh
1498 design capacity low: 200 mAh
1499 capacity granularity 1: 32 mAh
1500 capacity granularity 2: 32 mAh
1502 serial number: 16922
1508 hipo@lepakko conky $ cat /proc/acpi/battery/BAT1/state
1511 charging state: unknown
1513 remaining capacity: 4064 mAh
1514 present voltage: 16608 mV
1518 2213<@jupet�kellari��> jupet@lagi-unstable:~$ cat /proc/apm
1519 2213<@jupet�kellari��> 1.16 1.2 0x03 0x01 0xff 0x10 -1% -1 ?
1520 2213<@jupet�kellari��> (-1 ollee ei akkua kiinni, koska akku on p�yd�ll�)
1521 2214<@jupet�kellari��> jupet@lagi-unstable:~$ cat /proc/apm
1522 2214<@jupet�kellari��> 1.16 1.2 0x03 0x01 0x03 0x09 98% -1 ?
1524 2238<@jupet�kellari��> 1.16 1.2 0x03 0x00 0x00 0x01 100% -1 ? ilman verkkovirtaa
1525 2239<@jupet�kellari��> 1.16 1.2 0x03 0x01 0x00 0x01 99% -1 ? verkkovirralla
1527 2240<@jupet�kellari��> 1.16 1.2 0x03 0x01 0x03 0x09 100% -1 ? verkkovirralla ja monitori p��ll�
1528 2241<@jupet�kellari��> 1.16 1.2 0x03 0x00 0x00 0x01 99% -1 ? monitori p��ll� mutta ilman verkkovirtaa
1531 /* Kapil Hari Paranjape <kapil@imsc.res.in>
1532 Linux 2.6.24 onwards battery info is in
1533 /sys/class/power_supply/BAT0/
1534 On my system I get the following.
1535 /sys/class/power_supply/BAT0/uevent:
1536 PHYSDEVPATH=/devices/LNXSYSTM:00/device:00/PNP0A03:00/device:01/PNP0C09:00/PNP0C0A:00
1538 PHYSDEVDRIVER=battery
1539 POWER_SUPPLY_NAME=BAT0
1540 POWER_SUPPLY_TYPE=Battery
1541 POWER_SUPPLY_STATUS=Discharging
1542 POWER_SUPPLY_PRESENT=1
1543 POWER_SUPPLY_TECHNOLOGY=Li-ion
1544 POWER_SUPPLY_VOLTAGE_MIN_DESIGN=10800000
1545 POWER_SUPPLY_VOLTAGE_NOW=10780000
1546 POWER_SUPPLY_CURRENT_NOW=13970000
1547 POWER_SUPPLY_ENERGY_FULL_DESIGN=47510000
1548 POWER_SUPPLY_ENERGY_FULL=27370000
1549 POWER_SUPPLY_ENERGY_NOW=11810000
1550 POWER_SUPPLY_MODEL_NAME=IBM-92P1060
1551 POWER_SUPPLY_MANUFACTURER=Panasonic
1552 On some systems POWER_SUPPLY_ENERGY_* is replaced by POWER_SUPPLY_CHARGE_*
1555 /*on n900 with power kernel: (non power kernel could use "hal-device bme" and do something else)
1556 /sys/class/power_supply/bq27200-0/uevent
1557 PHYSDEVPATH=/class/i2c-adapter/i2c-2/2-0055
1559 PHYSDEVDRIVER=bq27200-battery
1560 POWER_SUPPLY_NAME=bq27200-0
1561 POWER_SUPPLY_TYPE=Battery
1562 POWER_SUPPLY_PRESENT=1 << this is always 1, it means the battery is inserted
1563 POWER_SUPPLY_VOLTAGE_NOW=4039 << this keeps updating during charging, both here and in dbus
1564 POWER_SUPPLY_CURRENT_NOW=1960 << this goes negative when charging!
1565 POWER_SUPPLY_CAPACITY=98 << supposed to be the %, it keeps updating while charging, unlike the dbus vals that freeze
1566 POWER_SUPPLY_TEMP=39 << only temperature sensor in n900 :(
1569 #define SYSFS_BATTERY_BASE_PATH "/sys/class/power_supply"
1570 #define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery" //not for n900
1571 #define APM_PATH "/proc/apm" //not for n900
1572 #define MAX_BATTERY_COUNT 4 //more like 1
1574 static FILE *sysfs_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
1575 //static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
1576 //static FILE *apm_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
1578 static int batteries_initialized = 0;
1579 static char batteries[MAX_BATTERY_COUNT][32];
1581 //static int acpi_last_full[MAX_BATTERY_COUNT];
1582 //static int acpi_design_capacity[MAX_BATTERY_COUNT];
1585 static int last_battery_volts[MAX_BATTERY_COUNT];
1588 static dbus_int32_t last_cell_radio_dbm;
1591 static dbus_int32_t last_cell_radio_percent;
1593 //eg 'full' 'on' 'off'
1594 static char last_batt_charge_status[16];
1597 static int last_battery_rate[MAX_BATTERY_COUNT];
1600 static float last_battery_temp[MAX_BATTERY_COUNT];
1602 /* e.g. "charging 75%" */
1603 static char last_battery_str[MAX_BATTERY_COUNT][64];
1605 static char last_battery_time_str[MAX_BATTERY_COUNT][64];
1607 static double last_battery_time[MAX_BATTERY_COUNT];
1609 static int last_battery_perct[MAX_BATTERY_COUNT];
1610 static double last_battery_perct_time[MAX_BATTERY_COUNT];
1612 void init_batteries(void)
1616 if (batteries_initialized) {
1620 #pragma omp parallel for schedule(dynamic,10)
1621 #endif /* HAVE_OPENMP */
1622 for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
1623 batteries[idx][0] = '\0';
1625 batteries_initialized = 1;
1628 int get_battery_idx(const char *bat)
1632 // for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
1633 // if (!strlen(batteries[idx]) || !strcmp(batteries[idx], bat)) {
1638 // /* if not found, enter a new entry */
1639 // if (!strlen(batteries[idx])) {
1640 // snprintf(batteries[idx], 31, "%s", bat);
1646 static int dbus_queue = 0;
1648 void set_dbus_retval(char *buffer, unsigned int n, int item);
1650 DBusConnection *connection;
1652 DBusMessage *message;
1653 DBusMessageIter iter;
1658 void dbus_exit_app_view()
1660 //not needed in harmattan - swipe down or away and close (although it doesn't seem to be reliable to swipe)
1663 //dbus-send --type=signal --session /com/nokia/hildon_desktop .
1665 // type = DBUS_BUS_SESSION;
1666 // //message_type = DBUS_MESSAGE_TYPE_SIGNAL;
1667 // dbus_error_init (&error);
1668 // connection = dbus_bus_get (type, &error);
1669 // message = dbus_message_new_signal ("/com/nokia/hildon_desktop", "com.nokia.hildon_desktop", "exit_app_view");
1670 // // send the message and flush the connection
1671 // if (!dbus_connection_send(connection, message, NULL)) {
1672 // fprintf(stderr, "Out Of Memory!\n");
1675 // dbus_connection_flush(connection);
1677 // // free the message
1678 // dbus_message_unref(message);
1682 void get_dbus_stuff(char *buffer,unsigned int intMax_length, int item)
1689 if (dbus_queue > 10)
1691 fprintf (stderr, "too much dbus queuing\n");
1693 // set_dbus_retval(buffer, intMax_length, item);
1696 dbus_queue++;//prevent a queue from forming on these requests...
1697 //fetch data from dbus, store in here as last_cell_radio_dbm
1698 //return into buffer
1700 type = DBUS_BUS_SYSTEM;
1701 message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
1702 // print_reply = TRUE;
1703 // print_reply_literal = FALSE;
1704 int reply_timeout_ms = 5000;
1705 dbus_error_init (&error);
1706 connection = dbus_bus_get (type, &error);
1707 if (connection == NULL)
1709 fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
1710 (type == DBUS_BUS_SYSTEM) ? "system" : "session",
1712 dbus_error_free (&error);
1717 snprintf(method,127,"Get");
1718 args = "com.nokia.csd.CSNet.SignalStrength";
1719 args2 = "SignalDecibels";
1720 snprintf(path,127,"/com/nokia/csd/csnet");
1721 snprintf(dest,127,"com.nokia.csd.CSNet"); //service name
1722 message = dbus_message_new_method_call (dest,path,"org.freedesktop.DBus.Properties",method);//dest,path,interface,method
1723 dbus_message_set_auto_start (message, TRUE);
1724 if (!dbus_message_append_args(message,
1725 DBUS_TYPE_STRING, &args,
1726 DBUS_TYPE_STRING, &args2,
1728 fprintf (stderr, "OOM appending args\n");
1729 //dbus_message_iter_init_append(message, &iter);
1730 //if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
1731 // fprintf (stderr, "OOM appending args\n");
1733 case DBUS_CELL_PERCENT:
1734 snprintf(method,127,"Get");
1735 args = "com.nokia.csd.CSNet.SignalStrength";
1736 args2 = "SignalPercent";
1737 snprintf(path,127,"/com/nokia/csd/csnet");
1738 snprintf(dest,127,"com.nokia.csd.CSNet"); //service name
1739 message = dbus_message_new_method_call (dest,path,"org.freedesktop.DBus.Properties",method);//dest,path,interface,method
1740 dbus_message_set_auto_start (message, TRUE);
1741 if (!dbus_message_append_args(message,
1742 DBUS_TYPE_STRING, &args,
1743 DBUS_TYPE_STRING, &args2,
1745 fprintf (stderr, "OOM appending args\n");
1746 //dbus_message_iter_init_append(message, &iter);
1747 //if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
1748 // fprintf (stderr, "OOM appending args\n");
1750 case DBUS_HAL_BATTERY_CHRG_STATUS:
1751 // 'full' 'on' 'off'
1752 snprintf(method,127,"GetProperty");
1753 args = "maemo.rechargeable.charging_status";
1754 snprintf(path,127,"/org/freedesktop/Hal/devices/bme");
1755 snprintf(dest,127,"org.freedesktop.Hal");
1756 message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method);
1757 dbus_message_set_auto_start (message, TRUE);
1758 dbus_message_iter_init_append(message, &iter);
1759 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
1760 fprintf (stderr, "OOM appending args\n");
1762 case DBUS_HAL_BATTERY_PERCENT:
1764 snprintf(method,127,"GetProperty");
1765 args = "battery.charge_level.percentage";
1766 snprintf(path,127,"/org/freedesktop/Hal/devices/bme");
1767 snprintf(dest,127,"org.freedesktop.Hal");
1768 message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method);
1769 dbus_message_set_auto_start (message, TRUE);
1770 dbus_message_iter_init_append(message, &iter);
1771 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
1772 fprintf (stderr, "OOM appending args\n");
1774 case DBUS_HAL_BATTERY_VOLTS_CURRENT:
1776 snprintf(method,127,"GetProperty");
1777 args = "battery.voltage.current"; //battery.reporting.current gets battery mA, not charge/discharge rate
1778 snprintf(path,127,"/org/freedesktop/Hal/devices/bme");
1779 snprintf(dest,127,"org.freedesktop.Hal");
1780 message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method);
1781 dbus_message_set_auto_start (message, TRUE);
1782 dbus_message_iter_init_append(message, &iter);
1783 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
1784 fprintf (stderr, "OOM appending args\n");
1787 fprintf (stderr, "invalid item type in get_dbus_stuff");
1790 if (message == NULL)
1792 fprintf (stderr, "Couldn't allocate D-Bus message\n");
1795 if (!dbus_message_set_destination (message, dest))
1797 fprintf (stderr, "Not enough memory\n");
1801 dbus_error_init (&error);
1802 reply = dbus_connection_send_with_reply_and_block (connection, message, reply_timeout_ms, &error);
1803 if (dbus_error_is_set (&error))
1805 fprintf (stderr, "Error %s: %s\n",error.name,error.message);
1806 //exit (1);//if we set timeout to 30s or something i guess it's okay to exit on "no reply" cuz something is fu*ked;
1810 DBusMessageIter iter;
1811 DBusMessageIter subiter;
1812 dbus_message_iter_init (reply, &iter);
1813 int current_fieldnumber = 0;
1814 while (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID)
1817 current_fieldnumber++;
1820 if (current_fieldnumber == 1)
1821 {//this is a variant
1822 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT){
1823 fprintf (stderr,"DBUS_CELL_DBM got type '%c'; expected variant!\n", dbus_message_iter_get_arg_type(&iter));
1826 dbus_message_iter_recurse (&iter, &subiter);
1827 dbus_int32_t val = 0;
1828 if (dbus_message_iter_get_arg_type(&subiter) != DBUS_TYPE_INT32){
1829 fprintf (stderr,"DBUS_CELL_DBM subiter got type '%c'; expected INT32!\n", dbus_message_iter_get_arg_type(&subiter));
1832 dbus_message_iter_get_basic(&subiter, &val);
1833 last_cell_radio_dbm = val;
1836 case DBUS_CELL_PERCENT:
1837 if (current_fieldnumber == 1)
1838 {//this is a variant
1839 dbus_message_iter_recurse (&iter, &subiter);
1840 dbus_int32_t val = 0;
1841 dbus_message_iter_get_basic(&subiter, &val);
1842 last_cell_radio_percent = val;
1845 case DBUS_HAL_BATTERY_CHRG_STATUS:
1846 if (current_fieldnumber == 1)
1849 dbus_message_iter_get_basic(&iter, &val);
1850 snprintf(last_batt_charge_status,16,"%s",val);
1853 case DBUS_HAL_BATTERY_PERCENT:
1854 if (current_fieldnumber == 1)
1857 dbus_message_iter_get_basic(&iter, &val);
1858 last_battery_perct[0] = val;
1861 case DBUS_HAL_BATTERY_VOLTS_CURRENT:
1862 if (current_fieldnumber == 1)
1865 dbus_message_iter_get_basic(&iter, &val);
1866 last_battery_volts[0] = val;
1870 fprintf (stderr, "invalid item type in get_dbus_stuff reply loop");
1873 dbus_message_iter_next (&iter);
1875 dbus_message_unref (reply);
1877 set_dbus_retval(buffer, intMax_length, item);
1878 dbus_message_unref (message);
1879 dbus_connection_unref (connection);
1880 dbus_queue = 0;//reset to zero now that complete
1883 void set_dbus_retval(char *buffer, unsigned int intMax_length, int item)
1887 snprintf(buffer, intMax_length, "%d", last_cell_radio_dbm);
1889 case DBUS_CELL_PERCENT:
1890 snprintf(buffer, intMax_length, "%d", last_cell_radio_percent);
1892 case DBUS_HAL_BATTERY_CHRG_STATUS:
1893 snprintf(buffer, intMax_length, "%s", last_batt_charge_status);
1895 case DBUS_HAL_BATTERY_PERCENT:
1896 snprintf(buffer, intMax_length, "%i", last_battery_perct[0]);
1898 case DBUS_HAL_BATTERY_VOLTS_CURRENT:
1899 snprintf(buffer, intMax_length, "%i", last_battery_volts[0]);
1902 fprintf (stderr, "invalid item type in set_dbus_retval");
1907 void set_return_value(char *buffer, unsigned int n, int item, int idx);
1909 void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
1911 static int idx, rep = 0;
1912 char acpi_path[128];
1913 char sysfs_path[128];
1915 snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
1916 snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
1920 idx = get_battery_idx(bat);
1922 /* don't update battery too often */
1923 if (current_update_time - last_battery_time[idx] < 2) {
1924 set_return_value(buffer, n, item, idx);
1928 last_battery_time[idx] = current_update_time;
1930 memset(last_battery_str[idx], 0, sizeof(last_battery_str[idx]));
1931 memset(last_battery_time_str[idx], 0, sizeof(last_battery_time_str[idx]));
1933 /* first try SYSFS if that fails try DBUS */
1935 if (sysfs_bat_fp[idx] == NULL) {
1936 sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
1939 if (sysfs_bat_fp[idx] != NULL) {
1941 int present_rate = -9999; //we will put "current now" into this. negative when charging!
1942 int remaining_capacity = -1; //in %
1943 char charging_state[64];//can't get this without hal bme
1947 strcpy(present, "no");
1948 strcpy(charging_state, "unknown");
1950 while (!feof(sysfs_bat_fp[idx])) {
1951 //we are looping through the data here
1953 if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
1955 if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0)//does current row match this?
1956 strcpy(present, "yes");//n900 always yes
1957 else if (strncmp(buf, "POWER_SUPPLY_VOLTAGE_NOW=", 25) == 0)//keep checking possible matches
1958 sscanf(buf, "POWER_SUPPLY_VOLTAGE_NOW=%d", &voltage);
1959 else if (strncmp(buf, "POWER_SUPPLY_CURRENT_NOW=", 25) == 0)
1960 sscanf(buf, "POWER_SUPPLY_CURRENT_NOW=%d", &present_rate);
1961 else if (strncmp(buf, "POWER_SUPPLY_CAPACITY=", 22) == 0)
1962 sscanf(buf, "POWER_SUPPLY_CAPACITY=%d", &remaining_capacity);
1963 else if (strncmp(buf, "POWER_SUPPLY_TEMP=", 18) == 0)
1964 sscanf(buf, "POWER_SUPPLY_TEMP=%d", &temp);
1966 fclose(sysfs_bat_fp[idx]);
1967 sysfs_bat_fp[idx] = NULL;
1968 if (voltage > 10000) voltage = voltage / 1000; //fix for n900 power kernel 47
1969 last_battery_volts[idx] = voltage;
1971 last_battery_temp[idx] = temp;
1972 else //fix for n900 power kernel 47
1973 last_battery_temp[idx] = (float) temp / 10;
1975 if (present_rate <= 0) {
1976 /* e.g. charging 75% */
1977 snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%", remaining_capacity);
1978 snprintf(last_battery_time_str[idx], sizeof(last_battery_time_str[idx]) - 1, "unknown");
1981 else if (present_rate > 0) {
1982 /* e.g. discharging 35% */
1983 snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%", remaining_capacity);
1984 snprintf(last_battery_time_str[idx], sizeof(last_battery_time_str[idx]) - 1, "unknown");
1987 if (remaining_capacity == 100)
1988 strcpy(last_battery_str[idx], "charged 100%");
1990 //if don't have power kernel, use HAL / dbus
1991 int remaining_capacity = -1; //in %
1993 remaining_capacity = get_battery_perct(bat);
1994 get_dbus_stuff(buffer,n,DBUS_HAL_BATTERY_VOLTS_CURRENT);
1995 get_dbus_stuff(buffer,n,DBUS_HAL_BATTERY_CHRG_STATUS);
1996 if (strncmp(buffer, "on", 2) == 0) {
1997 snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%", remaining_capacity);
1999 else if (strncmp(buffer, "off", 3) == 0) {
2000 snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%", remaining_capacity);
2002 else if (strncmp(buffer, "full", 4) == 0) {//no, it won't always be 100%. stupid dbus.
2003 snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charged %i%%", remaining_capacity);
2006 struct bme_reply bmeInfo = getBattInfoFromBME();
2007 last_battery_temp[idx] = ((float)bmeInfo.battery_temperature) - 273.15f;
2008 last_battery_rate[idx] = bmeInfo.battery_current;
2009 //last_battery_temp[idx] = temp;
2011 set_return_value(buffer, n, item, idx);
2014 void set_return_value(char *buffer, unsigned int n, int item, int idx)
2017 case BATTERY_STATUS:
2018 snprintf(buffer, n, "%s", last_battery_str[idx]);
2021 snprintf(buffer, n, "%s", last_battery_time_str[idx]);
2024 snprintf(buffer, n, "%i", last_battery_volts[idx]); // voltage
2027 snprintf(buffer, n, "%3.1f", last_battery_temp[idx]); // temperature
2030 snprintf(buffer, n, "%i", last_battery_rate[idx]); // charge/discharge rate
2033 fprintf (stderr, "invalid item type in set_return_value");
2038 void get_battery_short_status(char *buffer, unsigned int n, const char *bat)
2040 get_battery_stuff(buffer, n, bat, BATTERY_STATUS);
2041 if (0 == strncmp("charging", buffer, 8)) {
2043 memmove(buffer + 1, buffer + 8, n - 8);
2044 } else if (0 == strncmp("discharging", buffer, 11)) {
2046 memmove(buffer + 1, buffer + 11, n - 11);
2047 } else if (0 == strncmp("charged", buffer, 7)) {
2049 memmove(buffer + 1, buffer + 7, n - 7);
2050 } else if (0 == strncmp("not present", buffer, 11)) {
2052 memmove(buffer + 1, buffer + 11, n - 11);
2053 } else if (0 == strncmp("empty", buffer, 5)) {
2055 memmove(buffer + 1, buffer + 5, n - 5);
2056 } else if (0 != strncmp("AC", buffer, 2)) {
2058 memmove(buffer + 1, buffer + 11, n - 11);
2059 } else fprintf (stderr, "invalid input buffer in get_battery_short_status");
2062 int get_battery_perct(const char *bat)
2066 char sysfs_path[128];
2067 int remaining_capacity = -1;
2068 snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
2071 idx = get_battery_idx(bat);
2073 /* don't update battery too often */
2074 if (current_update_time - last_battery_perct_time[idx] < 29.5) {
2075 return last_battery_perct[idx];
2077 last_battery_perct_time[idx] = current_update_time;
2079 /* try SYSFS first */
2080 sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
2081 if (sysfs_bat_fp[idx] != NULL) {
2083 while (!feof(sysfs_bat_fp[idx])) {
2085 if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
2087 if (strncmp(buf, "POWER_SUPPLY_CAPACITY=", 22) == 0) {
2088 sscanf(buf, "POWER_SUPPLY_CAPACITY=%d", &remaining_capacity);
2090 // if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) {
2091 // sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
2092 // } else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) {
2093 // sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]);
2094 // } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) {
2095 // sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
2096 // } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) {
2097 // sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]);
2101 fclose(sysfs_bat_fp[idx]);
2102 sysfs_bat_fp[idx] = NULL;
2103 last_battery_perct[idx] = remaining_capacity;
2109 useless = malloc(128 * sizeof(char));
2110 get_dbus_stuff(useless,128,DBUS_HAL_BATTERY_PERCENT);
2111 //i told you it's useless...
2112 //snprintf(last_battery_perct[idx], 127, "%i", useless);
2113 //guess what? dbus battery % doesn't update while charging
2114 // get_dbus_stuff(useless,n,DBUS_HAL_BATTERY_CHRG_STATUS);
2115 // if (strncmp(useless, "full", 4) == 0)
2120 // else if (acpi_bat_fp[idx] != NULL) {
2122 // /* read last full capacity if it's zero */
2123 // if (acpi_design_capacity[idx] == 0) {
2128 // snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
2129 // fp = open_file(path, &rep2);
2130 // if (fp != NULL) {
2131 // while (!feof(fp)) {
2134 // if (fgets(b, 256, fp) == NULL) {
2137 // if (sscanf(b, "last full capacity: %d",
2138 // &acpi_design_capacity[idx]) != 0) {
2146 // fseek(acpi_bat_fp[idx], 0, SEEK_SET);
2148 // while (!feof(acpi_bat_fp[idx])) {
2151 // if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
2155 // if (buf[0] == 'r') {
2156 // sscanf(buf, "remaining capacity: %d", &remaining_capacity);
2161 /* compute the battery percentage */
2163 //(int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100);
2164 //if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100;
2165 return last_battery_perct[idx];
2168 int get_battery_perct_bar(const char *bat)
2172 get_battery_perct(bat);
2173 idx = get_battery_idx(bat);
2174 return (int) (last_battery_perct[idx] * 2.56 - 1);
2177 /* On Apple powerbook and ibook:
2178 $ cat /proc/pmu/battery_0
2185 $ cat /proc/pmu/info
2186 PMU driver version : 2
2187 PMU firmware version : 0c
2192 /* defines as in <linux/pmu.h> */
2193 #define PMU_BATT_PRESENT 0x00000001
2194 #define PMU_BATT_CHARGING 0x00000002
2196 static FILE *pmu_battery_fp;
2197 static FILE *pmu_info_fp;
2198 static char pb_battery_info[3][32];
2199 static double pb_battery_info_update;
2201 #define PMU_PATH "/proc/pmu"
2202 void get_powerbook_batt_info(char *buffer, size_t n, int i)
2205 const char *batt_path = PMU_PATH "/battery_0";
2206 const char *info_path = PMU_PATH "/info";
2208 int charge, max_charge, ac = -1;
2211 /* don't update battery too often */
2212 if (current_update_time - pb_battery_info_update < 29.5) {
2213 snprintf(buffer, n, "%s", pb_battery_info[i]);
2216 pb_battery_info_update = current_update_time;
2218 if (pmu_battery_fp == NULL) {
2219 pmu_battery_fp = open_file(batt_path, &rep);
2220 if (pmu_battery_fp == NULL) {
2225 if (pmu_battery_fp != NULL) {
2226 rewind(pmu_battery_fp);
2227 while (!feof(pmu_battery_fp)) {
2230 if (fgets(buf, sizeof(buf), pmu_battery_fp) == NULL) {
2234 if (buf[0] == 'f') {
2235 sscanf(buf, "flags : %8x", &flags);
2236 } else if (buf[0] == 'c' && buf[1] == 'h') {
2237 sscanf(buf, "charge : %d", &charge);
2238 } else if (buf[0] == 'm') {
2239 sscanf(buf, "max_charge : %d", &max_charge);
2240 } else if (buf[0] == 't') {
2241 sscanf(buf, "time rem. : %ld", &timeval);
2245 if (pmu_info_fp == NULL) {
2246 pmu_info_fp = open_file(info_path, &rep);
2247 if (pmu_info_fp == NULL) {
2252 if (pmu_info_fp != NULL) {
2253 rewind(pmu_info_fp);
2254 while (!feof(pmu_info_fp)) {
2257 if (fgets(buf, sizeof(buf), pmu_info_fp) == NULL) {
2260 if (buf[0] == 'A') {
2261 sscanf(buf, "AC Power : %d", &ac);
2265 /* update status string */
2266 if ((ac && !(flags & PMU_BATT_PRESENT))) {
2267 strncpy(pb_battery_info[PB_BATT_STATUS], "AC", sizeof(pb_battery_info[PB_BATT_STATUS]));
2268 } else if (ac && (flags & PMU_BATT_PRESENT)
2269 && !(flags & PMU_BATT_CHARGING)) {
2270 strncpy(pb_battery_info[PB_BATT_STATUS], "charged", sizeof(pb_battery_info[PB_BATT_STATUS]));
2271 } else if ((flags & PMU_BATT_PRESENT) && (flags & PMU_BATT_CHARGING)) {
2272 strncpy(pb_battery_info[PB_BATT_STATUS], "charging", sizeof(pb_battery_info[PB_BATT_STATUS]));
2274 strncpy(pb_battery_info[PB_BATT_STATUS], "discharging", sizeof(pb_battery_info[PB_BATT_STATUS]));
2277 /* update percentage string */
2278 if (timeval == 0 && ac && (flags & PMU_BATT_PRESENT)
2279 && !(flags & PMU_BATT_CHARGING)) {
2280 snprintf(pb_battery_info[PB_BATT_PERCENT],
2281 sizeof(pb_battery_info[PB_BATT_PERCENT]), "100%%");
2282 } else if (timeval == 0) {
2283 snprintf(pb_battery_info[PB_BATT_PERCENT],
2284 sizeof(pb_battery_info[PB_BATT_PERCENT]), "unknown");
2286 snprintf(pb_battery_info[PB_BATT_PERCENT],
2287 sizeof(pb_battery_info[PB_BATT_PERCENT]), "%d%%",
2288 (charge * 100) / max_charge);
2291 /* update time string */
2292 if (timeval == 0) { /* fully charged or battery not present */
2293 snprintf(pb_battery_info[PB_BATT_TIME],
2294 sizeof(pb_battery_info[PB_BATT_TIME]), "unknown");
2295 } else if (timeval < 60 * 60) { /* don't show secs */
2296 format_seconds_short(pb_battery_info[PB_BATT_TIME],
2297 sizeof(pb_battery_info[PB_BATT_TIME]), timeval);
2299 format_seconds(pb_battery_info[PB_BATT_TIME],
2300 sizeof(pb_battery_info[PB_BATT_TIME]), timeval);
2303 snprintf(buffer, n, "%s", pb_battery_info[i]);
2306 int update_top(void)
2308 process_find_top(info.cpu, info.memu, info.time
2313 info.first_process = get_first_process();
2317 #define ENTROPY_AVAIL_PATH "/proc/sys/kernel/random/entropy_avail"
2319 int get_entropy_avail(unsigned int *val)
2324 if (!(fp = open_file(ENTROPY_AVAIL_PATH, &rep)))
2327 if (fscanf(fp, "%u", val) != 1)
2334 #define ENTROPY_POOLSIZE_PATH "/proc/sys/kernel/random/poolsize"
2336 int get_entropy_poolsize(unsigned int *val)
2341 if (!(fp = open_file(ENTROPY_POOLSIZE_PATH, &rep)))
2344 if (fscanf(fp, "%u", val) != 1)
2351 const char *get_disk_protect_queue(const char *disk)
2357 snprintf(path, 127, "/sys/block/%s/device/unload_heads", disk);
2358 if (access(path, F_OK)) {
2359 snprintf(path, 127, "/sys/block/%s/queue/protect", disk);
2361 if ((fp = fopen(path, "r")) == NULL)
2363 if (fscanf(fp, "%d\n", &state) != 1) {
2368 return (state > 0) ? "frozen" : "free ";
2371 typedef struct DEV_LIST_TYPE
2375 struct DEV_LIST_TYPE *next;
2377 } DEV_LIST, *DEV_LIST_PTR;
2379 /* Same as sf #2942117 but memoized using a linked list */
2380 int is_disk(char *dev)
2382 char syspath[PATH_MAX];
2384 static DEV_LIST_PTR dev_head = NULL;
2385 DEV_LIST_PTR dev_cur, dev_last;
2390 if (strcmp(dev_cur->dev_name, dev) == 0)
2391 return dev_cur->memoized;
2393 dev_cur = dev_cur->next;
2396 dev_cur = (DEV_LIST_PTR)malloc(sizeof(DEV_LIST));
2397 dev_cur->dev_name = (char *)malloc((strlen(dev)+1)*sizeof(char));
2398 strcpy(dev_cur->dev_name,dev);
2399 dev_cur->next = NULL;
2401 while ((slash = strchr(dev, '/')))
2403 snprintf(syspath, sizeof(syspath), "/sys/block/%s", dev);
2404 dev_cur->memoized = !(access(syspath, F_OK));
2407 dev_last->next = dev_cur;
2411 return dev_cur->memoized;
2414 int update_diskio(void)
2418 char buf[512], devbuf[64];
2419 unsigned int major, minor;
2421 struct diskio_stat *cur;
2422 unsigned int reads, writes;
2423 unsigned int total_reads = 0, total_writes = 0;
2426 stats.current_read = 0;
2427 stats.current_write = 0;
2429 if (!(fp = open_file("/proc/diskstats", &rep))) {
2433 /* read reads and writes from all disks (minor = 0), including cd-roms
2434 * and floppies, and sum them up */
2435 while (fgets(buf, 512, fp)) {
2436 col_count = sscanf(buf, "%u %u %s %*u %*u %u %*u %*u %*u %u", &major,
2437 &minor, devbuf, &reads, &writes);
2438 /* ignore subdevices (they have only 3 matching entries in their line)
2439 * and virtual devices (LVM, network block devices, RAM disks, Loopback)
2441 * XXX: ignore devices which are part of a SW RAID (MD_MAJOR) */
2442 if (col_count == 5 && major != LVM_BLK_MAJOR && major != NBD_MAJOR
2443 && major != RAMDISK_MAJOR && major != LOOP_MAJOR) {
2444 /* check needed for kernel >= 2.6.31, see sf #2942117 */
2445 if (is_disk(devbuf)) {
2446 total_reads += reads;
2447 total_writes += writes;
2450 col_count = sscanf(buf, "%u %u %s %*u %u %*u %u",
2451 &major, &minor, devbuf, &reads, &writes);
2452 if (col_count != 5) {
2457 while (cur && strcmp(devbuf, cur->dev))
2461 update_diskio_values(cur, reads, writes);
2463 update_diskio_values(&stats, total_reads, total_writes);