home
/
code
/
khatus.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Co-locate all slot functions
[khatus.git]
/
x5
/
khatus_sensor_battery.c
diff --git
a/x5/khatus_sensor_battery.c
b/x5/khatus_sensor_battery.c
index
e4625c6
..
9923b34
100644
(file)
--- a/
x5/khatus_sensor_battery.c
+++ b/
x5/khatus_sensor_battery.c
@@
-1,17
+1,17
@@
+#include <signal.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
-#include <fcntl.h>
#include <limits.h>
#include <limits.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
-#include "khatus_lib_log.h"
-#include "khatus_lib_sensor.h"
-#include "khatus_lib_time.h"
+#include "khlib_log.h"
+#include "khlib_time.h"
#define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);}
#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";
double opt_interval = 1.0;
char *opt_battery = "BAT0";
-char *opt_fifo = NULL;
void
print_usage()
{
printf(
void
print_usage()
{
printf(
- "%s: [OPT ...]
FIFO
\n"
+ "%s: [OPT ...]\n"
"\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",
"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;
{
char c;
- while ((c = getopt(argc, argv, "
f
:i:h")) != -1)
+ while ((c = getopt(argc, argv, "
b
:i:h")) != -1)
switch (c) {
switch (c) {
- case '
f
':
+ case '
b
':
opt_battery = calloc(strlen(optarg), sizeof(char));
strcpy(opt_battery, optarg);
break;
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 '?':
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);
fprintf(stderr,
"Option -%c requires an argument.\n",
optopt);
@@
-70,10
+68,6
@@
opt_parse(int argc, char **argv)
default:
assert(0);
}
default:
assert(0);
}
- opt_fifo = argv[optind];
- debug("opt_fifo: %s\n", opt_fifo);
- if (!opt_fifo)
- usage("No filename was provided\n");
}
int
}
int
@@
-83,17
+77,21
@@
get_capacity(char *buf, char *path)
int cap;
if (!(fp = fopen(path, "r")))
int cap;
if (!(fp = fopen(path, "r")))
- fatal("Failed to open %s. errno: %d, msg: %s\n",
- path, errno, strerror(errno));
+ khlib_fatal(
+ "Failed to open %s. errno: %d, msg: %s\n",
+ path,
+ errno,
+ strerror(errno)
+ );
switch (fscanf(fp, "%d", &cap)) {
switch (fscanf(fp, "%d", &cap)) {
- case -1: fatal("EOF\n");
- case 0: fatal("Read 0\n");
+ case -1:
khlib_
fatal("EOF\n");
+ case 0:
khlib_
fatal("Read 0\n");
case 1: break;
default: assert(0);
}
fclose(fp);
case 1: break;
default: assert(0);
}
fclose(fp);
- return snprintf(buf, 6, "%3d%%
\n
", cap);
+ return snprintf(buf, 6, "%3d%%", cap);
}
int
}
int
@@
-104,17
+102,22
@@
main(int argc, char **argv)
char buf[10];
char path[PATH_MAX];
char *path_fmt = "/sys/class/power_supply/%s/capacity";
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);
opt_parse(argc, argv);
-
+ ti = khlib_timespec_of_float(opt_interval);
+ khlib_debug("opt_battery: \"%s\"\n", opt_battery);
+ khlib_debug("opt_interval: %f\n", opt_interval);
+ khlib_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);
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);
+ khlib_sleep(&ti);
+ }
+ return EXIT_SUCCESS;
}
}
This page took
0.038065 seconds
and
4
git commands to generate.