Simplify sensor IO model and fix opts and timeouts
authorSiraaj Khandkar <siraaj@khandkar.net>
Thu, 26 Mar 2020 02:00:24 +0000 (22:00 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Thu, 26 Mar 2020 02:00:24 +0000 (22:00 -0400)
no need to handle FIFO directly after all - if we catch the SIGPIPE,
shell redirection works beautifully.

x5/Makefile
x5/khatus_lib_sensor.c [deleted file]
x5/khatus_lib_sensor.h [deleted file]
x5/khatus_sensor_battery.c
x5/khatus_sensor_time.c

index a70c48f..e1c4287 100644 (file)
@@ -15,12 +15,10 @@ khatus: \
 
 khatus_sensor_battery: \
        khatus_lib_log.o \
-       khatus_lib_sensor.o \
        khatus_lib_time.o
 
 khatus_sensor_time: \
        khatus_lib_log.o \
-       khatus_lib_sensor.o \
        khatus_lib_time.o
 
 khatus_lib_time.o: khatus_lib_log.o
diff --git a/x5/khatus_lib_sensor.c b/x5/khatus_lib_sensor.c
deleted file mode 100644 (file)
index 7fa9bda..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "khatus_lib_log.h"
-#include "khatus_lib_sensor.h"
-#include "khatus_lib_time.h"
-
-void
-loop(
-    struct timespec *ti,
-    char *fifo,
-    char *buf,
-    int fun(char *, void *),
-    void *params)
-{
-       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, params);
-               buf[r] = END_OF_MESSAGE;
-               for (i = 0; (w = write(fd, buf + i++, 1)) && r; r--)
-                       ;
-               if (w < 0)
-                       switch (errno) {
-                       case EPIPE:
-                               error("Broken pipe. Msg buf: %s\n", buf);
-                               break;
-                       default:
-                               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);
-       }
-}
diff --git a/x5/khatus_lib_sensor.h b/x5/khatus_lib_sensor.h
deleted file mode 100644 (file)
index 3699179..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-#define END_OF_MESSAGE '\n'
-#define SENSOR_FUN_T  int (*)(char *, void *)
-#define SENSOR_PARAMS_T  void *
-
-void
-loop(
-    struct timespec *interval,
-    char *fifo,
-    char *buf,
-    SENSOR_FUN_T,
-    SENSOR_PARAMS_T
-);
index e4625c6..e6c7fe8 100644 (file)
@@ -1,8 +1,9 @@
+#include <signal.h>
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <limits.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -10,7 +11,6 @@
 #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);}
@@ -21,15 +21,13 @@ char *argv0;
 
 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",
@@ -41,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;
@@ -54,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);
@@ -70,10 +68,6 @@ 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
@@ -93,7 +87,7 @@ get_capacity(char *buf, char *path)
        default: assert(0);
        }
        fclose(fp);
-       return snprintf(buf, 6, "%3d%%\n", cap);
+       return snprintf(buf, 6, "%3d%%", cap);
 }
 
 int
@@ -104,17 +98,22 @@ main(int argc, char **argv)
        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,
-           (SENSOR_FUN_T)    get_capacity,
-           (SENSOR_PARAMS_T) path
-       );
+       signal(SIGPIPE, SIG_IGN);
+
+       for (;;) {
+               get_capacity(buf, path);
+               puts(buf);
+               fflush(stdout);
+               snooze(&ti);
+       }
+       return EXIT_SUCCESS;
 }
index cc47677..01483b9 100644 (file)
@@ -1,7 +1,6 @@
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -10,7 +9,6 @@
 #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);}
@@ -22,15 +20,13 @@ char *argv0 = NULL;
 
 double opt_interval = 1.0;
 char *opt_fmt = "%a %b %d %H:%M:%S";
-char *fifo_name = 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"
            "    | -f string  # format string\n"
            "    | -h         # help message (i.e. what you're reading now :) )\n",
@@ -71,20 +67,6 @@ opt_parse(int argc, char **argv)
                default:
                        assert(0);
                }
-       fifo_name = argv[optind];
-       debug("fifo_name: %s\n", fifo_name);
-       if (!fifo_name)
-               usage("No filename was provided\n");
-}
-
-int
-get_time(char *buf, char *fmt)
-{
-       time_t t;
-
-       t = time(NULL);
-       strftime(buf, MAX_LEN, fmt, localtime(&t));
-       return strlen(buf);
 }
 
 int
@@ -92,21 +74,24 @@ main(int argc, char **argv)
 {
        argv0 = argv[0];
 
+       time_t t;
        struct timespec ti;
        char buf[MAX_LEN];
 
        opt_parse(argc, argv);
-
-       signal(SIGPIPE, SIG_IGN);  /* Handled in loop */
-
-       memset(buf, '\0', MAX_LEN);
        ti = timespec_of_float(opt_interval);
-       loop(
-           &ti,
-           fifo_name,
-           buf,
-           (SENSOR_FUN_T)    get_time,
-           (SENSOR_PARAMS_T) opt_fmt
-       );
+       debug("opt_fmt: \"%s\"\n", opt_fmt);
+       debug("opt_interval: %f\n", opt_interval);
+       debug("ti: {tv_sec = %ld, tv_nsec = %ld}\n", ti.tv_sec, ti.tv_nsec);
+       memset(buf, '\0', MAX_LEN);
+       signal(SIGPIPE, SIG_IGN);
+
+       for (;;) {
+               t = time(NULL);
+               strftime(buf, MAX_LEN, opt_fmt, localtime(&t));
+               puts(buf);
+               fflush(stdout);
+               snooze(&ti);
+       }
        return EXIT_SUCCESS;
 }
This page took 0.04012 seconds and 4 git commands to generate.