Leave TODO to define a max after which to stop reading
[khatus.git] / x5 / khatus_sensor_battery.c
index b589bef..e4625c6 100644 (file)
 #include <unistd.h>
 
 #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);}
 
 #define MAX_LEN 20
-#define END_OF_MESSAGE '\n'
 
 char *argv0;
 
-char path[PATH_MAX];
-
 double  opt_interval = 1.0;
 char   *opt_battery  = "BAT0";
 char   *opt_fifo     = NULL;
@@ -78,44 +76,8 @@ opt_parse(int argc, char **argv)
                usage("No filename was provided\n");
 }
 
-void
-loop(struct timespec *ti, char *fifo, char *buf, int fun(char *))
-{
-       int fd = -1;
-       int w  = -1;  /* written */
-       int r  = -1;  /* remaining */
-       int i  = -1;  /* buffer position */
-
-       for (;;) {
-               debug("openning \"%s\"\n", fifo);
-               fd = open(fifo, O_WRONLY);
-               if (fd < 0)
-                       fatal("Failed to open FIFO file: \"%s\". Error: %s\n",
-                           fifo,
-                           strerror(errno));
-               debug("openned. fd: %d\n", fd);
-               r = fun(buf);
-               buf[r] = END_OF_MESSAGE;
-               for (i = 0; (w = write(fd, buf + i++, 1)) && r; r--)
-                       ;
-               if (w < 0)
-                       fatal("Failed to write to %s. Err num: %d, Err msg: %s\n",
-                           fifo,
-                           errno,
-                           strerror(errno));
-               if (close(fd) < 0)
-                       fatal("Failed to close %s. Err num: %d, Err msg: %s\n",
-                           fifo,
-                           errno,
-                           strerror(errno));
-               fd = -1;
-               debug("closed. fd: %d\n", fd);
-               snooze(ti);
-       }
-}
-
 int
-read_capacity(char *buf)
+get_capacity(char *buf, char *path)
 {
        FILE *fp;
        int cap;
@@ -140,6 +102,7 @@ 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);
 
@@ -147,5 +110,11 @@ main(int argc, char **argv)
 
        memset(path, '\0', PATH_MAX);
        snprintf(path, PATH_MAX, path_fmt, opt_battery);
-       loop(&ti, opt_fifo, buf, &read_capacity);
+       loop(
+           &ti,
+           opt_fifo,
+           buf,
+           (SENSOR_FUN_T)    get_capacity,
+           (SENSOR_PARAMS_T) path
+       );
 }
This page took 0.036496 seconds and 4 git commands to generate.