From: Siraaj Khandkar Date: Wed, 25 Mar 2020 20:23:06 +0000 (-0400) Subject: Support arbitrary parameter passing to sensor function X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=fa2a9b445f0242e7dca9e330148485adccc8f659;p=khatus.git Support arbitrary parameter passing to sensor function --- diff --git a/x5/khatus_lib_sensor.c b/x5/khatus_lib_sensor.c index e3e2cb3..7fa9bda 100644 --- a/x5/khatus_lib_sensor.c +++ b/x5/khatus_lib_sensor.c @@ -10,7 +10,12 @@ #include "khatus_lib_time.h" void -loop(struct timespec *ti, char *fifo, char *buf, int fun(char *)) +loop( + struct timespec *ti, + char *fifo, + char *buf, + int fun(char *, void *), + void *params) { int fd = -1; int w = -1; /* written */ @@ -25,7 +30,7 @@ loop(struct timespec *ti, char *fifo, char *buf, int fun(char *)) fifo, strerror(errno)); debug("openned. fd: %d\n", fd); - r = fun(buf); + r = fun(buf, params); buf[r] = END_OF_MESSAGE; for (i = 0; (w = write(fd, buf + i++, 1)) && r; r--) ; diff --git a/x5/khatus_lib_sensor.h b/x5/khatus_lib_sensor.h index 992352c..3699179 100644 --- a/x5/khatus_lib_sensor.h +++ b/x5/khatus_lib_sensor.h @@ -1,3 +1,12 @@ #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, int fun(char *)); +void +loop( + struct timespec *interval, + char *fifo, + char *buf, + SENSOR_FUN_T, + SENSOR_PARAMS_T +); diff --git a/x5/khatus_sensor_battery.c b/x5/khatus_sensor_battery.c index 7871245..e4625c6 100644 --- a/x5/khatus_sensor_battery.c +++ b/x5/khatus_sensor_battery.c @@ -19,8 +19,6 @@ char *argv0; -char path[PATH_MAX]; - double opt_interval = 1.0; char *opt_battery = "BAT0"; char *opt_fifo = NULL; @@ -79,7 +77,7 @@ opt_parse(int argc, char **argv) } int -get_capacity(char *buf) +get_capacity(char *buf, char *path) { FILE *fp; int cap; @@ -104,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); @@ -111,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, &get_capacity); + loop( + &ti, + opt_fifo, + buf, + (SENSOR_FUN_T) get_capacity, + (SENSOR_PARAMS_T) path + ); } diff --git a/x5/khatus_sensor_time.c b/x5/khatus_sensor_time.c index 84e747a..cc47677 100644 --- a/x5/khatus_sensor_time.c +++ b/x5/khatus_sensor_time.c @@ -78,12 +78,12 @@ opt_parse(int argc, char **argv) } int -read_time(char *buf) +get_time(char *buf, char *fmt) { time_t t; t = time(NULL); - strftime(buf, MAX_LEN, opt_fmt, localtime(&t)); + strftime(buf, MAX_LEN, fmt, localtime(&t)); return strlen(buf); } @@ -101,6 +101,12 @@ main(int argc, char **argv) memset(buf, '\0', MAX_LEN); ti = timespec_of_float(opt_interval); - loop(&ti, fifo_name, buf, read_time); + loop( + &ti, + fifo_name, + buf, + (SENSOR_FUN_T) get_time, + (SENSOR_PARAMS_T) opt_fmt + ); return EXIT_SUCCESS; }