munin plugin
[trinked.git] / main.c
1 /*-------------------------------------------------------------------------
2  * main.c
3  *
4  * Copyright (c) 2016, Teodor Sigaev <teodor@sigaev.ru>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *        notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *        notice, this list of conditions and the following disclaimer in the
13  *        documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *-------------------------------------------------------------------------
27  */
28
29 #include <fcntl.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <hidapi/hidapi.h>
36 #include <sys/param.h>
37
38 #include <trinket.h>
39
40 static char*    dbdir = NULL;
41 static int              period = (300); /* in seconds */
42 static char*    pidfile = NULL;
43 static char*    logfile = NULL;
44 static bool             daemonize = false;
45 static bool             workaround = false;
46 static char             tfile[MAXPATHLEN],
47                                 ofile[MAXPATHLEN];
48
49 static const double coefficient = (248e-6 / 2637.0);
50
51 static void
52 usage(const char *errmsg) {
53         puts("trinketd - collecting info from AtomPro");
54         puts("Copyright (c) 2016, Teodor Sigaev <teodor@sigaev.ru>");
55         puts("trinketd [-d] [-l logfile] [-p pidfile] [-P period] [-D datadir] [-w]");
56
57         if (errmsg) {
58                 puts("");
59                 puts(errmsg);
60         }
61
62         exit(1);
63 }
64
65 static void
66 main_loop() {
67         double  prevDose,
68                         curDose;
69
70 restart:
71         prevDose = -1;
72
73         if (trinketOpen() != ERR_OK) {
74                 fprintf(stderr, "trinketOpen fails\n");
75                 sleep(1);
76                 goto restart;
77         }
78
79         while(42) {
80                 if (workaround)
81                         trinketPing();
82
83                 if (trinketGetCumDose(&curDose) != ERR_OK) {
84                         fprintf(stderr, "trinketGetCumDose fails\n");
85                         trinketClose();
86                         sleep(1);
87                         goto restart;
88                 }
89
90                 if (prevDose >= 0.0) {
91                         double  counts = curDose - prevDose;
92                         double  radlevel = 1e6 * coefficient * counts * 3600.0 / (double)period;
93                         FILE    *fh = stdout;
94                         bool    successOpen = true;
95
96                         if (daemonize) {
97                                 if ((fh = fopen(tfile, "w")) == NULL) {
98                                         fprintf(stderr, "fopen fails\n");
99                                         successOpen = false;
100                                 }
101                         }
102
103                         if (successOpen) {
104                                 fprintf(fh, "total: %lld\n", (long long)curDose);
105                                 fprintf(fh, "counts: %lld\n", (long long)counts);
106                                 fprintf(fh, "radlevel: %.02f\n", radlevel);
107                         }
108
109                         if (fh && fh != stdout) {
110                                 fflush(fh);
111                                 fclose(fh);
112                                 rename(tfile, ofile);
113                         }
114                 }
115
116                 prevDose = curDose;
117                 sleep(period);
118         }
119
120         trinketClose();
121 }
122
123
124 extern char *optarg;
125 extern int opterr, optind;
126
127 int
128 main(int argn, char* argv[]) {
129         int i;
130         int     pidfd,
131                 logfd;
132
133         opterr = 0;
134         while((i=getopt(argn,argv,"dD:l:p:P:wh")) != EOF) {
135                 switch(i) {
136                         case 'd':
137                                 daemonize = true;
138                                 break;
139                         case 'D':
140                                 dbdir = strdup(optarg);
141                                 break;
142                         case 'l':
143                                 logfile = strdup(optarg);
144                                 break;
145                         case 'p':
146                                 pidfile = strdup(optarg);
147                                 break;
148                         case 'P':
149                                 period = atoi(optarg);
150                                 break;
151                         case 'w':
152                                 workaround = true;
153                                 break;
154                         case 'h':
155                         default:
156                                 usage(NULL);
157                 }
158         }
159
160         if (opterr || optind != argn)
161                 usage("argument err");
162
163         if (period <= 0)
164                 usage("Collecting period could not be zero nor negative");
165
166         if (daemonize && !dbdir)
167                 usage("trinketd: it is useless to use -d without -D");
168
169         if (dbdir && strlen(dbdir) > MAXPATHLEN - 32)
170                 usage("datadir is too long");
171
172         if (pidfile) {
173                 pidfd = open(pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
174                 if (pidfd < 0)
175                         usage("could not write pidfile");
176         }
177
178         if (logfile) {
179                 logfd = open(logfile, O_CREAT | O_WRONLY | O_APPEND, 0666);
180                 if (logfd < 0)
181                         usage("could not write logfile");
182         }
183
184         if (daemonize) {
185                 if (daemon(0, 0) == -1)
186                         usage("could not daemonize");
187         }
188
189         if (pidfile) {
190                 char pid[64];
191
192                 snprintf(pid, sizeof(pid), "%lld", (long long)getpid());
193                 if (write(pidfd, pid, strlen(pid)) != strlen(pid))
194                         usage("could not write pid");
195                 close(pidfd);
196         }
197
198         if (logfile) {
199                 dup2(logfd, fileno(stderr));
200                 close(logfd);
201         }
202
203         if (hid_init() < 0) {
204                 fprintf(stderr, "hid_init fails\n");
205                 exit(1);
206         }
207
208         if (dbdir) {
209                 snprintf(tfile, MAXPATHLEN, "%s/trinket.tmp", dbdir);
210                 snprintf(ofile, MAXPATHLEN, "%s/trinket.dat", dbdir);
211         }
212
213         main_loop();
214
215         hid_exit();
216         return 0;
217 }