X-Git-Url: https://git.xandkar.net/?p=khatus.git;a=blobdiff_plain;f=x5%2Fkhatus_sensor_battery.c;h=e6c7fe8b1f41d8de7aa074db1009e16d39ed8da0;hp=7871245f6853df1925cf1a47481feb689700c67a;hb=16e0239d76a7de132b20a564cc2c14ff2358ecf2;hpb=55883d9a0ab2ca4956bda685c219557f8bd940f1 diff --git a/x5/khatus_sensor_battery.c b/x5/khatus_sensor_battery.c index 7871245..e6c7fe8 100644 --- a/x5/khatus_sensor_battery.c +++ b/x5/khatus_sensor_battery.c @@ -1,8 +1,9 @@ +#include #include #include #include -#include #include +#include #include #include #include @@ -10,7 +11,6 @@ #include #include "khatus_lib_log.h" -#include "khatus_lib_sensor.h" #include "khatus_lib_time.h" #define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);} @@ -19,19 +19,15 @@ char *argv0; -char path[PATH_MAX]; - double opt_interval = 1.0; char *opt_battery = "BAT0"; -char *opt_fifo = NULL; void print_usage() { printf( - "%s: [OPT ...] FIFO\n" + "%s: [OPT ...]\n" "\n" - "FIFO = string # path to fifo file\n" "OPT = -i int # interval\n" " | -b string # battery file name from /sys/class/power_supply/\n" " | -h # help message (i.e. what you're reading now :) )\n", @@ -43,9 +39,9 @@ opt_parse(int argc, char **argv) { char c; - while ((c = getopt(argc, argv, "f:i:h")) != -1) + while ((c = getopt(argc, argv, "b:i:h")) != -1) switch (c) { - case 'f': + case 'b': opt_battery = calloc(strlen(optarg), sizeof(char)); strcpy(opt_battery, optarg); break; @@ -56,7 +52,7 @@ opt_parse(int argc, char **argv) print_usage(); exit(EXIT_SUCCESS); case '?': - if (optopt == 'f' || optopt == 'i') + if (optopt == 'b' || optopt == 'i') fprintf(stderr, "Option -%c requires an argument.\n", optopt); @@ -72,14 +68,10 @@ opt_parse(int argc, char **argv) default: assert(0); } - opt_fifo = argv[optind]; - debug("opt_fifo: %s\n", opt_fifo); - if (!opt_fifo) - usage("No filename was provided\n"); } int -get_capacity(char *buf) +get_capacity(char *buf, char *path) { FILE *fp; int cap; @@ -95,7 +87,7 @@ get_capacity(char *buf) default: assert(0); } fclose(fp); - return snprintf(buf, 6, "%3d%%\n", cap); + return snprintf(buf, 6, "%3d%%", cap); } int @@ -104,12 +96,24 @@ main(int argc, char **argv) argv0 = argv[0]; char buf[10]; + char path[PATH_MAX]; char *path_fmt = "/sys/class/power_supply/%s/capacity"; - struct timespec ti = timespec_of_float(opt_interval); + struct timespec ti; opt_parse(argc, argv); - + ti = timespec_of_float(opt_interval); + debug("opt_battery: \"%s\"\n", opt_battery); + debug("opt_interval: %f\n", opt_interval); + debug("ti: {tv_sec = %ld, tv_nsec = %ld}\n", ti.tv_sec, ti.tv_nsec); memset(path, '\0', PATH_MAX); snprintf(path, PATH_MAX, path_fmt, opt_battery); - loop(&ti, opt_fifo, buf, &get_capacity); + signal(SIGPIPE, SIG_IGN); + + for (;;) { + get_capacity(buf, path); + puts(buf); + fflush(stdout); + snooze(&ti); + } + return EXIT_SUCCESS; }