#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 */
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--)
;
#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
+);
char *argv0;
-char path[PATH_MAX];
-
double opt_interval = 1.0;
char *opt_battery = "BAT0";
char *opt_fifo = NULL;
}
int
-get_capacity(char *buf)
+get_capacity(char *buf, char *path)
{
FILE *fp;
int cap;
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);
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
+ );
}
}
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);
}
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;
}