From: Siraaj Khandkar Date: Wed, 25 Mar 2020 03:58:02 +0000 (-0400) Subject: Factor-out common loop X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=55883d9a0ab2ca4956bda685c219557f8bd940f1;p=khatus.git Factor-out common loop --- diff --git a/x5/Makefile b/x5/Makefile index e1c4287..a70c48f 100644 --- a/x5/Makefile +++ b/x5/Makefile @@ -15,10 +15,12 @@ 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 new file mode 100644 index 0000000..e3e2cb3 --- /dev/null +++ b/x5/khatus_lib_sensor.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include + +#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 *)) +{ + 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) + 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 new file mode 100644 index 0000000..992352c --- /dev/null +++ b/x5/khatus_lib_sensor.h @@ -0,0 +1,3 @@ +#define END_OF_MESSAGE '\n' + +void loop(struct timespec *interval, char *fifo, char *buf, int fun(char *)); diff --git a/x5/khatus_sensor_battery.c b/x5/khatus_sensor_battery.c index b589bef..7871245 100644 --- a/x5/khatus_sensor_battery.c +++ b/x5/khatus_sensor_battery.c @@ -10,12 +10,12 @@ #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);} #define MAX_LEN 20 -#define END_OF_MESSAGE '\n' char *argv0; @@ -78,44 +78,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) { FILE *fp; int cap; @@ -147,5 +111,5 @@ 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, &get_capacity); } diff --git a/x5/khatus_sensor_time.c b/x5/khatus_sensor_time.c index 869134c..84e747a 100644 --- a/x5/khatus_sensor_time.c +++ b/x5/khatus_sensor_time.c @@ -10,6 +10,7 @@ #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);} @@ -76,62 +77,30 @@ opt_parse(int argc, char **argv) usage("No filename was provided\n"); } +int +read_time(char *buf) +{ + time_t t; + + t = time(NULL); + strftime(buf, MAX_LEN, opt_fmt, localtime(&t)); + return strlen(buf); +} + int main(int argc, char **argv) { argv0 = argv[0]; - time_t t; struct timespec ti; char buf[MAX_LEN]; - int fd = -1; - - int n = 0; /* written */ - int r = 0; /* remaining */ - int i = 0; /* buffer position */ - opt_parse(argc, argv); - signal(SIGPIPE, SIG_IGN); /* Handling manually */ + signal(SIGPIPE, SIG_IGN); /* Handled in loop */ memset(buf, '\0', MAX_LEN); ti = timespec_of_float(opt_interval); - for (;;) { - debug("openning \"%s\"\n", fifo_name); - fd = open(fifo_name, O_WRONLY); - if (fd < 0) - fatal("Failed to open FIFO file: \"%s\". Error: %s\n", - fifo_name, - strerror(errno)); - debug("openned. fd: %d\n", fd); - t = time(NULL); - strftime(buf, MAX_LEN, opt_fmt, localtime(&t)); - r = strlen(buf); - buf[r] = END_OF_MESSAGE; - for (i = 0; (n = write(fd, buf + i++, 1)) && r; r--) - ; - if (n < 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_name, - errno, - strerror(errno)); - } - if (close(fd) < 0) - fatal("Failed to close %s. Err num: %d, Err msg: %s\n", - fifo_name, - errno, - strerror(errno)); - fd = -1; - debug("closed. fd: %d\n", fd); - snooze(&ti); - } + loop(&ti, fifo_name, buf, read_time); return EXIT_SUCCESS; }