Shift X2 status from legacy to archived
[khatus.git] / x5 / khatus_sensor_battery.c
CommitLineData
521d3106 1#include <signal.h>
d03cabfe
SK
2#include <assert.h>
3#include <ctype.h>
4#include <errno.h>
d03cabfe 5#include <limits.h>
521d3106 6#include <signal.h>
d03cabfe
SK
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <time.h>
11#include <unistd.h>
12
1084633a
SK
13#include "khlib_log.h"
14#include "khlib_time.h"
d03cabfe
SK
15
16#define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);}
17
18#define MAX_LEN 20
d03cabfe
SK
19
20char *argv0;
21
d03cabfe
SK
22double opt_interval = 1.0;
23char *opt_battery = "BAT0";
d03cabfe
SK
24
25void
26print_usage()
27{
28 printf(
521d3106 29 "%s: [OPT ...]\n"
d03cabfe 30 "\n"
d03cabfe
SK
31 "OPT = -i int # interval\n"
32 " | -b string # battery file name from /sys/class/power_supply/\n"
33 " | -h # help message (i.e. what you're reading now :) )\n",
34 argv0);
35}
36
37void
38opt_parse(int argc, char **argv)
39{
40 char c;
41
521d3106 42 while ((c = getopt(argc, argv, "b:i:h")) != -1)
d03cabfe 43 switch (c) {
521d3106 44 case 'b':
d03cabfe
SK
45 opt_battery = calloc(strlen(optarg), sizeof(char));
46 strcpy(opt_battery, optarg);
47 break;
48 case 'i':
49 opt_interval = atof(optarg);
50 break;
51 case 'h':
52 print_usage();
53 exit(EXIT_SUCCESS);
54 case '?':
521d3106 55 if (optopt == 'b' || optopt == 'i')
d03cabfe
SK
56 fprintf(stderr,
57 "Option -%c requires an argument.\n",
58 optopt);
59 else if (isprint(optopt))
60 fprintf (stderr,
61 "Unknown option `-%c'.\n",
62 optopt);
63 else
64 fprintf(stderr,
65 "Unknown option character `\\x%x'.\n",
66 optopt);
67 exit(EXIT_FAILURE);
68 default:
69 assert(0);
70 }
d03cabfe
SK
71}
72
d03cabfe 73int
fa2a9b44 74get_capacity(char *buf, char *path)
d03cabfe
SK
75{
76 FILE *fp;
77 int cap;
78
79 if (!(fp = fopen(path, "r")))
1084633a
SK
80 khlib_fatal(
81 "Failed to open %s. errno: %d, msg: %s\n",
82 path,
83 errno,
84 strerror(errno)
85 );
d03cabfe
SK
86
87 switch (fscanf(fp, "%d", &cap)) {
1084633a
SK
88 case -1: khlib_fatal("EOF\n");
89 case 0: khlib_fatal("Read 0\n");
d03cabfe
SK
90 case 1: break;
91 default: assert(0);
92 }
93 fclose(fp);
521d3106 94 return snprintf(buf, 6, "%3d%%", cap);
d03cabfe
SK
95}
96
97int
98main(int argc, char **argv)
99{
100 argv0 = argv[0];
101
102 char buf[10];
fa2a9b44 103 char path[PATH_MAX];
d03cabfe 104 char *path_fmt = "/sys/class/power_supply/%s/capacity";
521d3106 105 struct timespec ti;
d03cabfe
SK
106
107 opt_parse(argc, argv);
1084633a
SK
108 ti = khlib_timespec_of_float(opt_interval);
109 khlib_debug("opt_battery: \"%s\"\n", opt_battery);
110 khlib_debug("opt_interval: %f\n", opt_interval);
111 khlib_debug("ti: {tv_sec = %ld, tv_nsec = %ld}\n",ti.tv_sec,ti.tv_nsec);
d03cabfe
SK
112 memset(path, '\0', PATH_MAX);
113 snprintf(path, PATH_MAX, path_fmt, opt_battery);
521d3106
SK
114 signal(SIGPIPE, SIG_IGN);
115
116 for (;;) {
117 get_capacity(buf, path);
118 puts(buf);
119 fflush(stdout);
1084633a 120 khlib_sleep(&ti);
521d3106
SK
121 }
122 return EXIT_SUCCESS;
d03cabfe 123}
This page took 0.02761 seconds and 4 git commands to generate.