X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Flinux.c;h=0dd4bcd191974995f1803fced5e819d29c8cb025;hb=5b112f7a5d8ae68de4b52ac93374c4d68602f1b6;hp=a0b21ef197652cc8e46d99cdceeada50007ef134;hpb=1ed564fd0b44d00c63cb92d1ab775d13a9e5fc13;p=monky diff --git a/src/linux.c b/src/linux.c index a0b21ef..0dd4bcd 100644 --- a/src/linux.c +++ b/src/linux.c @@ -11,7 +11,7 @@ * * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen * Copyright (c) 2007 Toni Spets - * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al. + * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al. * (see AUTHORS) * All rights reserved. * @@ -35,6 +35,7 @@ #include "linux.h" #include "net_stat.h" #include "diskio.h" +#include "bme.c" #include "temphelper.h" #include #include @@ -63,6 +64,7 @@ #endif #include #include +#include /* The following ifdefs were adapted from gkrellm */ #include @@ -83,6 +85,8 @@ #include #endif +#include + struct sysfs { int fd; int arg; @@ -104,7 +108,7 @@ void prepare_update(void) { } -void update_uptime(void) +int update_uptime(void) { #ifdef HAVE_SYSINFO if (!prefer_proc) { @@ -120,11 +124,12 @@ void update_uptime(void) if (!(fp = open_file("/proc/uptime", &rep))) { info.uptime = 0.0; - return; + return 0; } fscanf(fp, "%lf", &info.uptime); fclose(fp); } + return 0; } int check_mount(char *s) @@ -152,7 +157,7 @@ int check_mount(char *s) /* these things are also in sysinfo except Buffers: * (that's why I'm reading them from proc) */ -void update_meminfo(void) +int update_meminfo(void) { FILE *meminfo_fp; static int rep = 0; @@ -164,7 +169,7 @@ void update_meminfo(void) info.buffers = info.cached = info.memfree = info.memeasyfree = 0; if (!(meminfo_fp = open_file("/proc/meminfo", &rep))) { - return; + return 0; } while (!feof(meminfo_fp)) { @@ -194,6 +199,7 @@ void update_meminfo(void) info.bufmem = info.cached + info.buffers; fclose(meminfo_fp); + return 0; } int get_laptop_mode(void) @@ -264,7 +270,7 @@ void update_gateway_info_failure(const char *reason) /* Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT */ #define RT_ENTRY_FORMAT "%63s %lx %lx %x %*d %*d %*d %lx %*d %*d %*d\n" -void update_gateway_info(void) +int update_gateway_info(void) { FILE *fp; struct in_addr ina; @@ -278,7 +284,7 @@ void update_gateway_info(void) if ((fp = fopen("/proc/net/route", "r")) == NULL) { update_gateway_info_failure("fopen()"); - return; + return 0; } /* skip over the table header line, which is always present */ @@ -298,7 +304,7 @@ void update_gateway_info(void) } } fclose(fp); - return; + return 0; } void free_gateway_info(void) @@ -325,7 +331,7 @@ void print_gateway_ip(char *p, int p_max_size) snprintf(p, p_max_size, "%s", gw_info.ip); } -void update_net_stats(void) +int update_net_stats(void) { FILE *net_dev_fp; static int rep = 0; @@ -349,20 +355,20 @@ void update_net_stats(void) /* get delta */ delta = current_update_time - last_update_time; if (delta <= 0.0001) { - return; + return 0; } /* open file and ignore first two lines */ if (!(net_dev_fp = open_file("/proc/net/dev", &rep))) { clear_net_stats(); - return; + return 0; } fgets(buf, 255, net_dev_fp); /* garbage */ fgets(buf, 255, net_dev_fp); /* garbage (field names) */ /* read each interface */ - for (i2 = 0; i2 < 16; i2++) { + for (i2 = 0; i2 < MAX_NET_INTERFACES; i2++) { struct net_stat *ns; char *s, *p; char temp_addr[18]; @@ -391,7 +397,7 @@ void update_net_stats(void) ns->up = 1; memset(&(ns->addr.sa_data), 0, 14); - memset(ns->addrs, 0, 17 * 16 + 1); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */ + memset(ns->addrs, 0, 17 * MAX_NET_INTERFACES + 1); /* Up to 17 chars per ip, max MAX_NET_INTERFACES interfaces. Nasty memory usage... */ last_recv = ns->recv; last_trans = ns->trans; @@ -418,8 +424,8 @@ void update_net_stats(void) /*** ip addr patch ***/ i = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); - conf.ifc_buf = malloc(sizeof(struct ifreq) * 16); - conf.ifc_len = sizeof(struct ifreq) * 16; + conf.ifc_buf = malloc(sizeof(struct ifreq) * MAX_NET_INTERFACES); + conf.ifc_len = sizeof(struct ifreq) * MAX_NET_INTERFACES; memset(conf.ifc_buf, 0, conf.ifc_len); ioctl((long) i, SIOCGIFCONF, &conf); @@ -464,12 +470,6 @@ void update_net_stats(void) curtmp1 = curtmp1 + ns->net_rec[i]; curtmp2 = curtmp2 + ns->net_trans[i]; } - if (curtmp1 == 0) { - curtmp1 = 1; - } - if (curtmp2 == 0) { - curtmp2 = 1; - } ns->recv_speed = curtmp1 / (double) info.net_avg_samples; ns->trans_speed = curtmp2 / (double) info.net_avg_samples; if (info.net_avg_samples > 1) { @@ -543,18 +543,45 @@ void update_net_stats(void) first = 0; fclose(net_dev_fp); + return 0; } int result; -void update_total_processes(void) +int update_total_processes(void) +{ + DIR *dir; + struct dirent *entry; + int ignore1; + char ignore2; + + info.procs = 0; + if (!(dir = opendir("/proc"))) { + return 0; + } + while ((entry = readdir(dir))) { + if (!entry) { + /* Problem reading list of processes */ + closedir(dir); + info.procs = 0; + return 0; + } + if (sscanf(entry->d_name, "%d%c", &ignore1, &ignore2) == 1) { + info.procs++; + } + } + closedir(dir); + return 0; +} + +int update_threads(void) { #ifdef HAVE_SYSINFO if (!prefer_proc) { struct sysinfo s_info; sysinfo(&s_info); - info.procs = s_info.procs; + info.threads = s_info.procs; } else #endif { @@ -562,12 +589,13 @@ void update_total_processes(void) FILE *fp; if (!(fp = open_file("/proc/loadavg", &rep))) { - info.procs = 0; - return; + info.threads = 0; + return 0; } - fscanf(fp, "%*f %*f %*f %*d/%hu", &info.procs); + fscanf(fp, "%*f %*f %*f %*d/%hu", &info.threads); fclose(fp); } + return 0; } #define CPU_SAMPLE_COUNT 15 @@ -641,7 +669,7 @@ void get_cpu_count(void) #define TMPL_LONGSTAT "%*s %llu %llu %llu %llu %llu %llu %llu %llu" #define TMPL_SHORTSTAT "%*s %llu %llu %llu %llu" -inline static void update_stat(void) +int update_stat(void) { FILE *stat_fp; static int rep = 0; @@ -653,14 +681,20 @@ inline static void update_stat(void) const char *stat_template = NULL; unsigned int malloc_cpu_size = 0; extern void* global_cpu; + + static pthread_mutex_t last_stat_update_mutex = PTHREAD_MUTEX_INITIALIZER; static double last_stat_update = 0.0; /* since we use wrappers for this function, the update machinery * can't eliminate double invocations of this function. Check for * them here, otherwise cpu_usage counters are freaking out. */ - if (last_stat_update == current_update_time) - return; + pthread_mutex_lock(&last_stat_update_mutex); + if (last_stat_update == current_update_time) { + pthread_mutex_unlock(&last_stat_update_mutex); + return 0; + } last_stat_update = current_update_time; + pthread_mutex_unlock(&last_stat_update_mutex); /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */ if (!cpu_setup || !info.cpu_usage) { @@ -681,11 +715,11 @@ inline static void update_stat(void) } if (!(stat_fp = open_file("/proc/stat", &rep))) { - info.run_procs = 0; + info.run_threads = 0; if (info.cpu_usage) { memset(info.cpu_usage, 0, info.cpu_count * sizeof(float)); } - return; + return 0; } idx = 0; @@ -695,7 +729,7 @@ inline static void update_stat(void) } if (strncmp(buf, "procs_running ", 14) == 0) { - sscanf(buf, "%*s %hu", &info.run_procs); + sscanf(buf, "%*s %hu", &info.run_threads); } else if (strncmp(buf, "cpu", 3) == 0) { double delta; if (isdigit(buf[3])) { @@ -756,19 +790,22 @@ inline static void update_stat(void) } } fclose(stat_fp); + return 0; } -void update_running_processes(void) +int update_running_processes(void) { update_stat(); + return 0; } -void update_cpu_usage(void) +int update_cpu_usage(void) { update_stat(); + return 0; } -void update_load_average(void) +int update_load_average(void) { #ifdef HAVE_GETLOADAVG if (!prefer_proc) { @@ -786,46 +823,13 @@ void update_load_average(void) if (!(fp = open_file("/proc/loadavg", &rep))) { info.loadavg[0] = info.loadavg[1] = info.loadavg[2] = 0.0; - return; + return 0; } fscanf(fp, "%f %f %f", &info.loadavg[0], &info.loadavg[1], &info.loadavg[2]); fclose(fp); } -} - -#define PROC_I8K "/proc/i8k" -#define I8K_DELIM " " -static char *i8k_procbuf = NULL; -void update_i8k(void) -{ - FILE *fp; - - if (!i8k_procbuf) { - i8k_procbuf = (char *) malloc(128 * sizeof(char)); - } - if ((fp = fopen(PROC_I8K, "r")) == NULL) { - CRIT_ERR(NULL, NULL, "/proc/i8k doesn't exist! use insmod to make sure the kernel " - "driver is loaded..."); - } - - memset(&i8k_procbuf[0], 0, 128); - if (fread(&i8k_procbuf[0], sizeof(char), 128, fp) == 0) { - NORM_ERR("something wrong with /proc/i8k..."); - } - - fclose(fp); - - i8k.version = strtok(&i8k_procbuf[0], I8K_DELIM); - i8k.bios = strtok(NULL, I8K_DELIM); - i8k.serial = strtok(NULL, I8K_DELIM); - i8k.cpu_temp = strtok(NULL, I8K_DELIM); - i8k.left_fan_status = strtok(NULL, I8K_DELIM); - i8k.right_fan_status = strtok(NULL, I8K_DELIM); - i8k.left_fan_rpm = strtok(NULL, I8K_DELIM); - i8k.right_fan_rpm = strtok(NULL, I8K_DELIM); - i8k.ac_status = strtok(NULL, I8K_DELIM); - i8k.buttons_status = strtok(NULL, I8K_DELIM); + return 0; } /***********************************************************/ @@ -1108,61 +1112,6 @@ void free_sysfs_sensor(struct text_object *obj) obj->data.opaque = NULL; } -/* Prior to kernel version 2.6.12, the CPU fan speed was available in - * ADT746X_FAN_OLD, whereas later kernel versions provide this information in - * ADT746X_FAN. */ -#define ADT746X_FAN "/sys/devices/temperatures/sensor1_fan_speed" -#define ADT746X_FAN_OLD "/sys/devices/temperatures/cpu_fan_speed" - -void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size) -{ - static int rep = 0; - char adt746x_fan_state[64]; - FILE *fp; - - if (!p_client_buffer || client_buffer_size <= 0) { - return; - } - - if ((fp = open_file(ADT746X_FAN, &rep)) == NULL - && (fp = open_file(ADT746X_FAN_OLD, &rep)) == NULL) { - sprintf(adt746x_fan_state, "adt746x not found"); - } else { - fgets(adt746x_fan_state, sizeof(adt746x_fan_state), fp); - adt746x_fan_state[strlen(adt746x_fan_state) - 1] = 0; - fclose(fp); - } - - snprintf(p_client_buffer, client_buffer_size, "%s", adt746x_fan_state); -} - -/* Prior to kernel version 2.6.12, the CPU temperature was found in - * ADT746X_CPU_OLD, whereas later kernel versions provide this information in - * ADT746X_CPU. */ -#define ADT746X_CPU "/sys/devices/temperatures/sensor1_temperature" -#define ADT746X_CPU_OLD "/sys/devices/temperatures/cpu_temperature" - -void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size) -{ - static int rep = 0; - char adt746x_cpu_state[64]; - FILE *fp; - - if (!p_client_buffer || client_buffer_size <= 0) { - return; - } - - if ((fp = open_file(ADT746X_CPU, &rep)) == NULL - && (fp = open_file(ADT746X_CPU_OLD, &rep)) == NULL) { - sprintf(adt746x_cpu_state, "adt746x not found"); - } else { - fscanf(fp, "%2s", adt746x_cpu_state); - fclose(fp); - } - - snprintf(p_client_buffer, client_buffer_size, "%s", adt746x_cpu_state); -} - #define CPUFREQ_PREFIX "/sys/devices/system/cpu" #define CPUFREQ_POSTFIX "cpufreq/scaling_cur_freq" @@ -1265,7 +1214,7 @@ char get_freq(char *p_client_buffer, size_t client_buffer_size, * Peter Tarjan (ptarjan@citromail.hu) */ /* return cpu voltage in mV (use divisor=1) or V (use divisor=1000) */ -char get_voltage(char *p_client_buffer, size_t client_buffer_size, +static char get_voltage(char *p_client_buffer, size_t client_buffer_size, const char *p_format, int divisor, unsigned int cpu) { FILE *f; @@ -1334,6 +1283,22 @@ char get_voltage(char *p_client_buffer, size_t client_buffer_size, return 1; } +void print_voltage_mv(struct text_object *obj, char *p, int p_max_size) +{ + static int ok = 1; + if (ok) { + ok = get_voltage(p, p_max_size, "%.0f", 1, obj->data.i); + } +} + +void print_voltage_v(struct text_object *obj, char *p, int p_max_size) +{ + static int ok = 1; + if (ok) { + ok = get_voltage(p, p_max_size, "%'.3f", 1000, obj->data.i); + } +} + #define ACPI_FAN_DIR "/proc/acpi/fan/" void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size) @@ -1368,7 +1333,7 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size) snprintf(p_client_buffer, client_buffer_size, "%s", buf); } -#define SYSFS_AC_ADAPTER_DIR "/sys/class/power_supply/AC" +#define SYSFS_AC_ADAPTER_DIR "/sys/class/power_supply" #define ACPI_AC_ADAPTER_DIR "/proc/acpi/ac_adapter/" /* Linux 2.6.25 onwards ac adapter info is in /sys/class/power_supply/AC/ @@ -1380,22 +1345,33 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size) POWER_SUPPLY_NAME=AC POWER_SUPPLY_TYPE=Mains POWER_SUPPLY_ONLINE=1 + + Update: it seems the folder name is hardware-dependent. We add an aditional adapter + argument, specifying the folder name. + + Update: on some systems it's /sys/class/power_supply/ADP1 instead of /sys/class/power_supply/AC */ -void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size) +void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter) { static int rep = 0; char buf[256]; char buf2[256]; + struct stat sb; FILE *fp; if (!p_client_buffer || client_buffer_size <= 0) { return; } - snprintf(buf2, sizeof(buf2), "%s/uevent", SYSFS_AC_ADAPTER_DIR); - fp = open_file(buf2, &rep); + if(adapter) + snprintf(buf2, sizeof(buf2), "%s/%s/uevent", SYSFS_AC_ADAPTER_DIR, adapter); + else{ + snprintf(buf2, sizeof(buf2), "%s/AC/uevent", SYSFS_AC_ADAPTER_DIR); + if(stat(buf2, &sb) == -1) snprintf(buf2, sizeof(buf2), "%s/ADP1/uevent", SYSFS_AC_ADAPTER_DIR); + } + if(stat(buf2, &sb) == 0) fp = open_file(buf2, &rep); else fp = 0; if (fp) { /* sysfs processing */ while (!feof(fp)) { @@ -1576,20 +1552,52 @@ present voltage: 16608 mV On some systems POWER_SUPPLY_ENERGY_* is replaced by POWER_SUPPLY_CHARGE_* */ +/*on n900 with power kernel: (non power kernel could use "hal-device bme" and do something else) +/sys/class/power_supply/bq27200-0/uevent +PHYSDEVPATH=/class/i2c-adapter/i2c-2/2-0055 +PHYSDEVBUS=i2c +PHYSDEVDRIVER=bq27200-battery +POWER_SUPPLY_NAME=bq27200-0 +POWER_SUPPLY_TYPE=Battery +POWER_SUPPLY_PRESENT=1 << this is always 1, it means the battery is inserted +POWER_SUPPLY_VOLTAGE_NOW=4039 << this keeps updating during charging, both here and in dbus +POWER_SUPPLY_CURRENT_NOW=1960 << this goes negative when charging! +POWER_SUPPLY_CAPACITY=98 << supposed to be the %, it keeps updating while charging, unlike the dbus vals that freeze +POWER_SUPPLY_TEMP=39 << only temperature sensor in n900 :( +*/ + #define SYSFS_BATTERY_BASE_PATH "/sys/class/power_supply" -#define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery" -#define APM_PATH "/proc/apm" -#define MAX_BATTERY_COUNT 4 +#define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery" //not for n900 +#define APM_PATH "/proc/apm" //not for n900 +#define MAX_BATTERY_COUNT 4 //more like 1 static FILE *sysfs_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL }; -static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL }; -static FILE *apm_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL }; +//static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL }; +//static FILE *apm_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL }; static int batteries_initialized = 0; static char batteries[MAX_BATTERY_COUNT][32]; -static int acpi_last_full[MAX_BATTERY_COUNT]; -static int acpi_design_capacity[MAX_BATTERY_COUNT]; +//static int acpi_last_full[MAX_BATTERY_COUNT]; +//static int acpi_design_capacity[MAX_BATTERY_COUNT]; + +//eg 4100 +static int last_battery_volts[MAX_BATTERY_COUNT]; + +//eg -78 +static dbus_int32_t last_cell_radio_dbm; + +//eg 100 +static dbus_int32_t last_cell_radio_percent; + +//eg 'full' 'on' 'off' +static char last_batt_charge_status[16]; + +//eg 100 or -100 +static int last_battery_rate[MAX_BATTERY_COUNT]; + +//eg 35.5 +static float last_battery_temp[MAX_BATTERY_COUNT]; /* e.g. "charging 75%" */ static char last_battery_str[MAX_BATTERY_COUNT][64]; @@ -1619,27 +1627,288 @@ void init_batteries(void) int get_battery_idx(const char *bat) { - int idx; +// int idx; +// +// for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) { +// if (!strlen(batteries[idx]) || !strcmp(batteries[idx], bat)) { +// break; +// } +// } +// +// /* if not found, enter a new entry */ +// if (!strlen(batteries[idx])) { +// snprintf(batteries[idx], 31, "%s", bat); +// } + + return 0; +} - for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) { - if (!strlen(batteries[idx]) || !strcmp(batteries[idx], bat)) { - break; - } +static int dbus_queue = 0; + +void set_dbus_retval(char *buffer, unsigned int n, int item); + +DBusConnection *connection; +DBusError error; +DBusMessage *message; +DBusMessageIter iter; +DBusBusType type; +int message_type; +DBusMessage *reply; + +void dbus_exit_app_view() +{ + //not needed in harmattan - swipe down or away and close (although it doesn't seem to be reliable to swipe) + + + //dbus-send --type=signal --session /com/nokia/hildon_desktop . + +// type = DBUS_BUS_SESSION; +// //message_type = DBUS_MESSAGE_TYPE_SIGNAL; +// dbus_error_init (&error); +// connection = dbus_bus_get (type, &error); +// message = dbus_message_new_signal ("/com/nokia/hildon_desktop", "com.nokia.hildon_desktop", "exit_app_view"); +// // send the message and flush the connection +// if (!dbus_connection_send(connection, message, NULL)) { +// fprintf(stderr, "Out Of Memory!\n"); +// exit(1); +// } +// dbus_connection_flush(connection); +// +// // free the message +// dbus_message_unref(message); + +} + +void get_dbus_stuff(char *buffer,unsigned int intMax_length, int item) +{ + char method[128]; + char path[128]; + char dest[128]; + char *args = ""; + char *args2 = ""; + if (dbus_queue > 10) + { + fprintf (stderr, "too much dbus queuing\n"); + exit(1); +// set_dbus_retval(buffer, intMax_length, item); +// return; + } + dbus_queue++;//prevent a queue from forming on these requests... +//fetch data from dbus, store in here as last_cell_radio_dbm +//return into buffer + + type = DBUS_BUS_SYSTEM; + message_type = DBUS_MESSAGE_TYPE_METHOD_CALL; +// print_reply = TRUE; +// print_reply_literal = FALSE; + int reply_timeout_ms = 5000; + dbus_error_init (&error); + connection = dbus_bus_get (type, &error); + if (connection == NULL) + { + fprintf (stderr, "Failed to open connection to %s message bus: %s\n", + (type == DBUS_BUS_SYSTEM) ? "system" : "session", + error.message); + dbus_error_free (&error); + exit (1); + } + switch(item){ + case DBUS_CELL_DBM: + snprintf(method,127,"Get"); + args = "com.nokia.csd.CSNet.SignalStrength"; + args2 = "SignalDecibels"; + snprintf(path,127,"/com/nokia/csd/csnet"); + snprintf(dest,127,"com.nokia.csd.CSNet"); //service name + message = dbus_message_new_method_call (dest,path,"org.freedesktop.DBus.Properties",method);//dest,path,interface,method + dbus_message_set_auto_start (message, TRUE); + if (!dbus_message_append_args(message, + DBUS_TYPE_STRING, &args, + DBUS_TYPE_STRING, &args2, + DBUS_TYPE_INVALID)) + fprintf (stderr, "OOM appending args\n"); + //dbus_message_iter_init_append(message, &iter); + //if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args)) + // fprintf (stderr, "OOM appending args\n"); + break; + case DBUS_CELL_PERCENT: + snprintf(method,127,"Get"); + args = "com.nokia.csd.CSNet.SignalStrength"; + args2 = "SignalPercent"; + snprintf(path,127,"/com/nokia/csd/csnet"); + snprintf(dest,127,"com.nokia.csd.CSNet"); //service name + message = dbus_message_new_method_call (dest,path,"org.freedesktop.DBus.Properties",method);//dest,path,interface,method + dbus_message_set_auto_start (message, TRUE); + if (!dbus_message_append_args(message, + DBUS_TYPE_STRING, &args, + DBUS_TYPE_STRING, &args2, + DBUS_TYPE_INVALID)) + fprintf (stderr, "OOM appending args\n"); + //dbus_message_iter_init_append(message, &iter); + //if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args)) + // fprintf (stderr, "OOM appending args\n"); + break; + case DBUS_HAL_BATTERY_CHRG_STATUS: + // 'full' 'on' 'off' + snprintf(method,127,"GetProperty"); + args = "maemo.rechargeable.charging_status"; + snprintf(path,127,"/org/freedesktop/Hal/devices/bme"); + snprintf(dest,127,"org.freedesktop.Hal"); + message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method); + dbus_message_set_auto_start (message, TRUE); + dbus_message_iter_init_append(message, &iter); + if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args)) + fprintf (stderr, "OOM appending args\n"); + break; + case DBUS_HAL_BATTERY_PERCENT: + // '96' + snprintf(method,127,"GetProperty"); + args = "battery.charge_level.percentage"; + snprintf(path,127,"/org/freedesktop/Hal/devices/bme"); + snprintf(dest,127,"org.freedesktop.Hal"); + message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method); + dbus_message_set_auto_start (message, TRUE); + dbus_message_iter_init_append(message, &iter); + if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args)) + fprintf (stderr, "OOM appending args\n"); + break; + case DBUS_HAL_BATTERY_VOLTS_CURRENT: + // '3600' - '4200' + snprintf(method,127,"GetProperty"); + args = "battery.voltage.current"; //battery.reporting.current gets battery mA, not charge/discharge rate + snprintf(path,127,"/org/freedesktop/Hal/devices/bme"); + snprintf(dest,127,"org.freedesktop.Hal"); + message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method); + dbus_message_set_auto_start (message, TRUE); + dbus_message_iter_init_append(message, &iter); + if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args)) + fprintf (stderr, "OOM appending args\n"); + break; + default: + fprintf (stderr, "invalid item type in get_dbus_stuff"); + break; + } + if (message == NULL) + { + fprintf (stderr, "Couldn't allocate D-Bus message\n"); + exit (1); + } + if (!dbus_message_set_destination (message, dest)) + { + fprintf (stderr, "Not enough memory\n"); + exit (1); } - /* if not found, enter a new entry */ - if (!strlen(batteries[idx])) { - snprintf(batteries[idx], 31, "%s", bat); + dbus_error_init (&error); + reply = dbus_connection_send_with_reply_and_block (connection, message, reply_timeout_ms, &error); + if (dbus_error_is_set (&error)) + { + fprintf (stderr, "Error %s: %s\n",error.name,error.message); + //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; + } + if (reply) + { + DBusMessageIter iter; + DBusMessageIter subiter; + dbus_message_iter_init (reply, &iter); + int current_fieldnumber = 0; + while (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID) + { + + current_fieldnumber++; + switch(item){ + case DBUS_CELL_DBM: + if (current_fieldnumber == 1) + {//this is a variant + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT){ + fprintf (stderr,"DBUS_CELL_DBM got type '%c'; expected variant!\n", dbus_message_iter_get_arg_type(&iter)); + break; + } + dbus_message_iter_recurse (&iter, &subiter); + dbus_int32_t val = 0; + if (dbus_message_iter_get_arg_type(&subiter) != DBUS_TYPE_INT32){ + fprintf (stderr,"DBUS_CELL_DBM subiter got type '%c'; expected INT32!\n", dbus_message_iter_get_arg_type(&subiter)); + break; + } + dbus_message_iter_get_basic(&subiter, &val); + last_cell_radio_dbm = val; + } + break; + case DBUS_CELL_PERCENT: + if (current_fieldnumber == 1) + {//this is a variant + dbus_message_iter_recurse (&iter, &subiter); + dbus_int32_t val = 0; + dbus_message_iter_get_basic(&subiter, &val); + last_cell_radio_percent = val; + } + break; + case DBUS_HAL_BATTERY_CHRG_STATUS: + if (current_fieldnumber == 1) + { + char *val; + dbus_message_iter_get_basic(&iter, &val); + snprintf(last_batt_charge_status,16,"%s",val); + } + break; + case DBUS_HAL_BATTERY_PERCENT: + if (current_fieldnumber == 1) + { + dbus_uint32_t val; + dbus_message_iter_get_basic(&iter, &val); + last_battery_perct[0] = val; + } + break; + case DBUS_HAL_BATTERY_VOLTS_CURRENT: + if (current_fieldnumber == 1) + { + dbus_uint32_t val; + dbus_message_iter_get_basic(&iter, &val); + last_battery_volts[0] = val; + } + break; + default: + fprintf (stderr, "invalid item type in get_dbus_stuff reply loop"); + break; + } + dbus_message_iter_next (&iter); + } + dbus_message_unref (reply); } + set_dbus_retval(buffer, intMax_length, item); + dbus_message_unref (message); + dbus_connection_unref (connection); + dbus_queue = 0;//reset to zero now that complete +} - return idx; +void set_dbus_retval(char *buffer, unsigned int intMax_length, int item) +{ + switch (item) { + case DBUS_CELL_DBM: + snprintf(buffer, intMax_length, "%d", last_cell_radio_dbm); + break; + case DBUS_CELL_PERCENT: + snprintf(buffer, intMax_length, "%d", last_cell_radio_percent); + break; + case DBUS_HAL_BATTERY_CHRG_STATUS: + snprintf(buffer, intMax_length, "%s", last_batt_charge_status); + break; + case DBUS_HAL_BATTERY_PERCENT: + snprintf(buffer, intMax_length, "%i", last_battery_perct[0]); + break; + case DBUS_HAL_BATTERY_VOLTS_CURRENT: + snprintf(buffer, intMax_length, "%i", last_battery_volts[0]); + break; + default: + fprintf (stderr, "invalid item type in set_dbus_retval"); + break; + } } void set_return_value(char *buffer, unsigned int n, int item, int idx); void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item) { - static int idx, rep = 0, rep1 = 0, rep2 = 0; + static int idx, rep = 0; char acpi_path[128]; char sysfs_path[128]; @@ -1651,7 +1920,7 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item) idx = get_battery_idx(bat); /* don't update battery too often */ - if (current_update_time - last_battery_time[idx] < 29.5) { + if (current_update_time - last_battery_time[idx] < 2) { set_return_value(buffer, n, item, idx); return; } @@ -1661,288 +1930,83 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item) memset(last_battery_str[idx], 0, sizeof(last_battery_str[idx])); memset(last_battery_time_str[idx], 0, sizeof(last_battery_time_str[idx])); - /* first try SYSFS if that fails try ACPI */ + /* first try SYSFS if that fails try DBUS */ - if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) { + if (sysfs_bat_fp[idx] == NULL) { sysfs_bat_fp[idx] = open_file(sysfs_path, &rep); } - if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) { - acpi_bat_fp[idx] = open_file(acpi_path, &rep1); - } - if (sysfs_bat_fp[idx] != NULL) { /* SYSFS */ - int present_rate = -1; - int remaining_capacity = -1; - char charging_state[64]; + int present_rate = -9999; //we will put "current now" into this. negative when charging! + int remaining_capacity = -1; //in % + char charging_state[64];//can't get this without hal bme + int voltage = -1; + int temp = 9999; char present[4]; - + strcpy(present, "no"); strcpy(charging_state, "unknown"); while (!feof(sysfs_bat_fp[idx])) { + //we are looping through the data here char buf[256]; if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL) break; - - /* let's just hope units are ok */ - if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0) - strcpy(present, "yes"); - else if (strncmp (buf, "POWER_SUPPLY_PRESENT=0", 22) == 0) - strcpy(present, "no"); - else if (strncmp (buf, "POWER_SUPPLY_STATUS=", 20) == 0) - sscanf(buf, "POWER_SUPPLY_STATUS=%63s", charging_state); - /* present_rate is not the same as the - current flowing now but it is the same value - which was used in the past. so we continue - the tradition! */ + if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0)//does current row match this? + strcpy(present, "yes");//n900 always yes + else if (strncmp(buf, "POWER_SUPPLY_VOLTAGE_NOW=", 25) == 0)//keep checking possible matches + sscanf(buf, "POWER_SUPPLY_VOLTAGE_NOW=%d", &voltage); else if (strncmp(buf, "POWER_SUPPLY_CURRENT_NOW=", 25) == 0) sscanf(buf, "POWER_SUPPLY_CURRENT_NOW=%d", &present_rate); - else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) - sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity); - else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=", 25) == 0) - sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_last_full[idx]); - else if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) - sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity); - else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=", 25) == 0) - sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_last_full[idx]); + else if (strncmp(buf, "POWER_SUPPLY_CAPACITY=", 22) == 0) + sscanf(buf, "POWER_SUPPLY_CAPACITY=%d", &remaining_capacity); + else if (strncmp(buf, "POWER_SUPPLY_TEMP=", 18) == 0) + sscanf(buf, "POWER_SUPPLY_TEMP=%d", &temp); } - fclose(sysfs_bat_fp[idx]); sysfs_bat_fp[idx] = NULL; - - /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */ - if (remaining_capacity > acpi_last_full[idx]) - acpi_last_full[idx] = remaining_capacity; /* normalize to 100% */ - - /* not present */ - if (strcmp(present, "No") == 0) { - strncpy(last_battery_str[idx], "not present", 64); - } + if (voltage > 10000) voltage = voltage / 1000; //fix for n900 power kernel 47 + last_battery_volts[idx] = voltage; + if (temp < 100) + last_battery_temp[idx] = temp; + else //fix for n900 power kernel 47 + last_battery_temp[idx] = (float) temp / 10; /* charging */ - else if (strcmp(charging_state, "Charging") == 0) { - if (acpi_last_full[idx] != 0 && present_rate > 0) { + if (present_rate <= 0) { /* e.g. charging 75% */ - snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%", - (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 )); - /* e.g. 2h 37m */ - format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1, - (long) (((float)(acpi_last_full[idx] - remaining_capacity) / present_rate) * 3600)); - } else if (acpi_last_full[idx] != 0 && present_rate <= 0) { - snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %d%%", - (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100)); - snprintf(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } else { - strncpy(last_battery_str[idx], "charging", sizeof(last_battery_str[idx])-1); - snprintf(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } - } + snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%", remaining_capacity); + snprintf(last_battery_time_str[idx], sizeof(last_battery_time_str[idx]) - 1, "unknown"); + } /* discharging */ - else if (strncmp(charging_state, "Discharging", 64) == 0) { - if (present_rate > 0) { + else if (present_rate > 0) { /* e.g. discharging 35% */ - snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%", - (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 )); - /* e.g. 1h 12m */ - format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1, - (long) (((float) remaining_capacity / present_rate) * 3600)); - } else if (present_rate == 0) { /* Thanks to Nexox for this one */ - snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "full"); - snprintf(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } else { - snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, - "discharging %d%%", - (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100)); - snprintf(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } + snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%", remaining_capacity); + snprintf(last_battery_time_str[idx], sizeof(last_battery_time_str[idx]) - 1, "unknown"); } /* charged */ - /* thanks to Lukas Zapletal */ - else if (strncmp(charging_state, "Charged", 64) == 0 || strncmp(charging_state, "Full", 64) == 0) { - /* Below happens with the second battery on my X40, - * when the second one is empty and the first one - * being charged. */ - if (remaining_capacity == 0) - strcpy(last_battery_str[idx], "empty"); - else - strcpy(last_battery_str[idx], "charged"); + if (remaining_capacity == 100) + strcpy(last_battery_str[idx], "charged 100%"); + } else { + //if don't have power kernel, use HAL / dbus + int remaining_capacity = -1; //in % + int temp = -1; + remaining_capacity = get_battery_perct(bat); + get_dbus_stuff(buffer,n,DBUS_HAL_BATTERY_VOLTS_CURRENT); + get_dbus_stuff(buffer,n,DBUS_HAL_BATTERY_CHRG_STATUS); + if (strncmp(buffer, "on", 2) == 0) { + snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%", remaining_capacity); } - /* unknown, probably full / AC */ - else { - if (acpi_last_full[idx] != 0 - && remaining_capacity != acpi_last_full[idx]) - snprintf(last_battery_str[idx], 64, "unknown %d%%", - (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100)); - else - strncpy(last_battery_str[idx], "AC", 64); + else if (strncmp(buffer, "off", 3) == 0) { + snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%", remaining_capacity); } - } else if (acpi_bat_fp[idx] != NULL) { - /* ACPI */ - int present_rate = -1; - int remaining_capacity = -1; - char charging_state[64]; - char present[4]; - - /* read last full capacity if it's zero */ - if (acpi_last_full[idx] == 0) { - static int rep3 = 0; - char path[128]; - FILE *fp; - - snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat); - fp = open_file(path, &rep3); - if (fp != NULL) { - while (!feof(fp)) { - char b[256]; - - if (fgets(b, 256, fp) == NULL) { - break; - } - if (sscanf(b, "last full capacity: %d", - &acpi_last_full[idx]) != 0) { - break; - } - } - - fclose(fp); - } - } - - fseek(acpi_bat_fp[idx], 0, SEEK_SET); - - strcpy(charging_state, "unknown"); - - while (!feof(acpi_bat_fp[idx])) { - char buf[256]; - - if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) { - break; - } - - /* let's just hope units are ok */ - if (strncmp(buf, "present:", 8) == 0) { - sscanf(buf, "present: %4s", present); - } else if (strncmp(buf, "charging state:", 15) == 0) { - sscanf(buf, "charging state: %63s", charging_state); - } else if (strncmp(buf, "present rate:", 13) == 0) { - sscanf(buf, "present rate: %d", &present_rate); - } else if (strncmp(buf, "remaining capacity:", 19) == 0) { - sscanf(buf, "remaining capacity: %d", &remaining_capacity); - } - } - /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */ - if (remaining_capacity > acpi_last_full[idx]) { - /* normalize to 100% */ - acpi_last_full[idx] = remaining_capacity; - } - - /* not present */ - if (strcmp(present, "no") == 0) { - strncpy(last_battery_str[idx], "not present", 64); - /* charging */ - } else if (strcmp(charging_state, "charging") == 0) { - if (acpi_last_full[idx] != 0 && present_rate > 0) { - /* e.g. charging 75% */ - snprintf(last_battery_str[idx], - sizeof(last_battery_str[idx]) - 1, "charging %i%%", - (int) ((remaining_capacity * 100) / acpi_last_full[idx])); - /* e.g. 2h 37m */ - format_seconds(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, - (long) (((acpi_last_full[idx] - remaining_capacity) * - 3600) / present_rate)); - } else if (acpi_last_full[idx] != 0 && present_rate <= 0) { - snprintf(last_battery_str[idx], - sizeof(last_battery_str[idx]) - 1, "charging %d%%", - (int) ((remaining_capacity * 100) / acpi_last_full[idx])); - snprintf(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } else { - strncpy(last_battery_str[idx], "charging", - sizeof(last_battery_str[idx]) - 1); - snprintf(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } - /* discharging */ - } else if (strncmp(charging_state, "discharging", 64) == 0) { - if (present_rate > 0) { - /* e.g. discharging 35% */ - snprintf(last_battery_str[idx], - sizeof(last_battery_str[idx]) - 1, "discharging %i%%", - (int) ((remaining_capacity * 100) / acpi_last_full[idx])); - /* e.g. 1h 12m */ - format_seconds(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, - (long) ((remaining_capacity * 3600) / present_rate)); - } else if (present_rate == 0) { /* Thanks to Nexox for this one */ - snprintf(last_battery_str[idx], - sizeof(last_battery_str[idx]) - 1, "full"); - snprintf(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } else { - snprintf(last_battery_str[idx], - sizeof(last_battery_str[idx]) - 1, "discharging %d%%", - (int) ((remaining_capacity * 100) / acpi_last_full[idx])); - snprintf(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } - /* charged */ - } else if (strncmp(charging_state, "charged", 64) == 0) { - /* thanks to Lukas Zapletal */ - /* Below happens with the second battery on my X40, - * when the second one is empty and the first one being charged. */ - if (remaining_capacity == 0) { - strcpy(last_battery_str[idx], "empty"); - } else { - strcpy(last_battery_str[idx], "charged"); - } - /* unknown, probably full / AC */ - } else { - if (strncmp(charging_state, "Full", 64) == 0) { - strncpy(last_battery_str[idx], "full", 64); - } else if (acpi_last_full[idx] != 0 - && remaining_capacity != acpi_last_full[idx]) { - snprintf(last_battery_str[idx], 64, "unknown %d%%", - (int) ((remaining_capacity * 100) / acpi_last_full[idx])); - } else { - strncpy(last_battery_str[idx], "AC", 64); - } - } - fclose(acpi_bat_fp[idx]); - acpi_bat_fp[idx] = NULL; - } else { - /* APM */ - if (apm_bat_fp[idx] == NULL) { - apm_bat_fp[idx] = open_file(APM_PATH, &rep2); - } - - if (apm_bat_fp[idx] != NULL) { - unsigned int ac, status, flag; - int life; - - fscanf(apm_bat_fp[idx], "%*s %*s %*x %x %x %x %d%%", - &ac, &status, &flag, &life); - - if (life == -1) { - /* could check now that there is ac */ - snprintf(last_battery_str[idx], 64, "AC"); - - /* could check that status == 3 here? */ - } else if (ac && life != 100) { - snprintf(last_battery_str[idx], 64, "charging %d%%", life); - } else { - snprintf(last_battery_str[idx], 64, "%d%%", life); - } - - /* it seemed to buffer it so file must be closed (or could use - * syscalls directly but I don't feel like coding it now) */ - fclose(apm_bat_fp[idx]); - apm_bat_fp[idx] = NULL; - } + else if (strncmp(buffer, "full", 4) == 0) {//no, it won't always be 100%. stupid dbus. + snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charged %i%%", remaining_capacity); + } + + struct bme_reply bmeInfo = getBattInfoFromBME(); + last_battery_temp[idx] = ((float)bmeInfo.battery_temperature) - 273.15f; + last_battery_rate[idx] = bmeInfo.battery_current; + //last_battery_temp[idx] = temp; } set_return_value(buffer, n, item, idx); } @@ -1956,7 +2020,17 @@ void set_return_value(char *buffer, unsigned int n, int item, int idx) case BATTERY_TIME: snprintf(buffer, n, "%s", last_battery_time_str[idx]); break; + case BATTERY_VOLTS: + snprintf(buffer, n, "%i", last_battery_volts[idx]); // voltage + break; + case BATTERY_TEMP: + snprintf(buffer, n, "%3.1f", last_battery_temp[idx]); // temperature + break; + case BATTERY_RATE: + snprintf(buffer, n, "%i", last_battery_rate[idx]); // charge/discharge rate + break; default: + fprintf (stderr, "invalid item type in set_return_value"); break; } } @@ -1982,118 +2056,121 @@ void get_battery_short_status(char *buffer, unsigned int n, const char *bat) } else if (0 != strncmp("AC", buffer, 2)) { buffer[0] = 'U'; memmove(buffer + 1, buffer + 11, n - 11); - } + } else fprintf (stderr, "invalid input buffer in get_battery_short_status"); } int get_battery_perct(const char *bat) { static int rep = 0; int idx; - char acpi_path[128]; char sysfs_path[128]; int remaining_capacity = -1; - - snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat); snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat); init_batteries(); - idx = get_battery_idx(bat); /* don't update battery too often */ - if (current_update_time - last_battery_perct_time[idx] < 30) { + if (current_update_time - last_battery_perct_time[idx] < 29.5) { return last_battery_perct[idx]; } last_battery_perct_time[idx] = current_update_time; - /* Only check for SYSFS or ACPI */ - - if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) { - sysfs_bat_fp[idx] = open_file(sysfs_path, &rep); - rep = 0; - } - - if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) { - acpi_bat_fp[idx] = open_file(acpi_path, &rep); - } - + /* try SYSFS first */ + sysfs_bat_fp[idx] = open_file(sysfs_path, &rep); if (sysfs_bat_fp[idx] != NULL) { /* SYSFS */ while (!feof(sysfs_bat_fp[idx])) { char buf[256]; if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL) break; - - if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) { - sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity); - } else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) { - sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]); - } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) { - sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity); - } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) { - sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]); + if (strncmp(buf, "POWER_SUPPLY_CAPACITY=", 22) == 0) { + sscanf(buf, "POWER_SUPPLY_CAPACITY=%d", &remaining_capacity); + +// if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) { +// sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity); +// } else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) { +// sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]); +// } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) { +// sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity); +// } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) { +// sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]); +// } } } - fclose(sysfs_bat_fp[idx]); sysfs_bat_fp[idx] = NULL; + last_battery_perct[idx] = remaining_capacity; - } else if (acpi_bat_fp[idx] != NULL) { - /* ACPI */ - /* read last full capacity if it's zero */ - if (acpi_design_capacity[idx] == 0) { - static int rep2; - char path[128]; - FILE *fp; - - snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat); - fp = open_file(path, &rep2); - if (fp != NULL) { - while (!feof(fp)) { - char b[256]; - - if (fgets(b, 256, fp) == NULL) { - break; - } - if (sscanf(b, "last full capacity: %d", - &acpi_design_capacity[idx]) != 0) { - break; - } - } - fclose(fp); - } - } - - fseek(acpi_bat_fp[idx], 0, SEEK_SET); - - while (!feof(acpi_bat_fp[idx])) { - char buf[256]; - - if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) { - break; - } - - if (buf[0] == 'r') { - sscanf(buf, "remaining capacity: %d", &remaining_capacity); - } - } - } - if (remaining_capacity < 0) { - return 0; } + else + { + char *useless; + useless = malloc(128 * sizeof(char)); + get_dbus_stuff(useless,128,DBUS_HAL_BATTERY_PERCENT); + //i told you it's useless... + //snprintf(last_battery_perct[idx], 127, "%i", useless); + //guess what? dbus battery % doesn't update while charging +// get_dbus_stuff(useless,n,DBUS_HAL_BATTERY_CHRG_STATUS); +// if (strncmp(useless, "full", 4) == 0) +// { +// +// } + } +// else if (acpi_bat_fp[idx] != NULL) { +// /* ACPI */ +// /* read last full capacity if it's zero */ +// if (acpi_design_capacity[idx] == 0) { +// static int rep2; +// char path[128]; +// FILE *fp; +// +// snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat); +// fp = open_file(path, &rep2); +// if (fp != NULL) { +// while (!feof(fp)) { +// char b[256]; +// +// if (fgets(b, 256, fp) == NULL) { +// break; +// } +// if (sscanf(b, "last full capacity: %d", +// &acpi_design_capacity[idx]) != 0) { +// break; +// } +// } +// fclose(fp); +// } +// } +// +// fseek(acpi_bat_fp[idx], 0, SEEK_SET); +// +// while (!feof(acpi_bat_fp[idx])) { +// char buf[256]; +// +// if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) { +// break; +// } +// +// if (buf[0] == 'r') { +// sscanf(buf, "remaining capacity: %d", &remaining_capacity); +// } +// } +// } + /* compute the battery percentage */ - last_battery_perct[idx] = - (int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100); - if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100; + + //(int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100); + //if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100; return last_battery_perct[idx]; } -int get_battery_perct_bar(const char *bar) +int get_battery_perct_bar(const char *bat) { int idx; - get_battery_perct(bar); - idx = get_battery_idx(bar); + get_battery_perct(bat); + idx = get_battery_idx(bat); return (int) (last_battery_perct[idx] * 2.56 - 1); } @@ -2226,7 +2303,7 @@ void get_powerbook_batt_info(char *buffer, size_t n, int i) snprintf(buffer, n, "%s", pb_battery_info[i]); } -void update_top(void) +int update_top(void) { process_find_top(info.cpu, info.memu, info.time #ifdef IOSTATS @@ -2234,32 +2311,41 @@ void update_top(void) #endif ); info.first_process = get_first_process(); + return 0; } -void update_entropy(void) +#define ENTROPY_AVAIL_PATH "/proc/sys/kernel/random/entropy_avail" + +int get_entropy_avail(unsigned int *val) { static int rep = 0; - const char *entropy_avail = "/proc/sys/kernel/random/entropy_avail"; - const char *entropy_poolsize = "/proc/sys/kernel/random/poolsize"; - FILE *fp1, *fp2; + FILE *fp; - info.entropy.entropy_avail = 0; - info.entropy.poolsize = 0; + if (!(fp = open_file(ENTROPY_AVAIL_PATH, &rep))) + return 1; - if ((fp1 = open_file(entropy_avail, &rep)) == NULL) { - return; - } + if (fscanf(fp, "%u", val) != 1) + return 1; - if ((fp2 = open_file(entropy_poolsize, &rep)) == NULL) { - fclose(fp1); - return; - } + fclose(fp); + return 0; +} - fscanf(fp1, "%u", &info.entropy.entropy_avail); - fscanf(fp2, "%u", &info.entropy.poolsize); +#define ENTROPY_POOLSIZE_PATH "/proc/sys/kernel/random/poolsize" - fclose(fp1); - fclose(fp2); +int get_entropy_poolsize(unsigned int *val) +{ + static int rep = 0; + FILE *fp; + + if (!(fp = open_file(ENTROPY_POOLSIZE_PATH, &rep))) + return 1; + + if (fscanf(fp, "%u", val) != 1) + return 1; + + fclose(fp); + return 0; } const char *get_disk_protect_queue(const char *disk) @@ -2282,7 +2368,50 @@ const char *get_disk_protect_queue(const char *disk) return (state > 0) ? "frozen" : "free "; } -void update_diskio(void) +typedef struct DEV_LIST_TYPE +{ + char *dev_name; + int memoized; + struct DEV_LIST_TYPE *next; + +} DEV_LIST, *DEV_LIST_PTR; + +/* Same as sf #2942117 but memoized using a linked list */ +int is_disk(char *dev) +{ + char syspath[PATH_MAX]; + char *slash; + static DEV_LIST_PTR dev_head = NULL; + DEV_LIST_PTR dev_cur, dev_last; + + dev_cur = dev_head; + + while (dev_cur) { + if (strcmp(dev_cur->dev_name, dev) == 0) + return dev_cur->memoized; + dev_last = dev_cur; + dev_cur = dev_cur->next; + } + + dev_cur = (DEV_LIST_PTR)malloc(sizeof(DEV_LIST)); + dev_cur->dev_name = (char *)malloc((strlen(dev)+1)*sizeof(char)); + strcpy(dev_cur->dev_name,dev); + dev_cur->next = NULL; + + while ((slash = strchr(dev, '/'))) + *slash = '!'; + snprintf(syspath, sizeof(syspath), "/sys/block/%s", dev); + dev_cur->memoized = !(access(syspath, F_OK)); + + if (dev_head) + dev_last->next = dev_cur; + else + dev_head = dev_cur; + + return dev_cur->memoized; +} + +int update_diskio(void) { FILE *fp; static int rep = 0; @@ -2298,7 +2427,7 @@ void update_diskio(void) stats.current_write = 0; if (!(fp = open_file("/proc/diskstats", &rep))) { - return; + return 0; } /* read reads and writes from all disks (minor = 0), including cd-roms @@ -2312,8 +2441,11 @@ void update_diskio(void) * XXX: ignore devices which are part of a SW RAID (MD_MAJOR) */ if (col_count == 5 && major != LVM_BLK_MAJOR && major != NBD_MAJOR && major != RAMDISK_MAJOR && major != LOOP_MAJOR) { - total_reads += reads; - total_writes += writes; + /* check needed for kernel >= 2.6.31, see sf #2942117 */ + if (is_disk(devbuf)) { + total_reads += reads; + total_writes += writes; + } } else { col_count = sscanf(buf, "%u %u %s %*u %u %*u %u", &major, &minor, devbuf, &reads, &writes); @@ -2330,4 +2462,5 @@ void update_diskio(void) } update_diskio_values(&stats, total_reads, total_writes); fclose(fp); + return 0; }