455267289fa7f3ea09fea61716e7b43def081954
[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 int              tics = 1;
43 static char*    pidfile = NULL;
44 static char*    logfile = NULL;
45 static bool             daemonize = 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] [-i tics]");
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         int             i;
70         int             collected = 0;
71
72         prevDose = malloc(sizeof(*prevDose) * (tics + 1));
73         if (!prevDose) {
74                 fprintf(stderr, "could not allocate array, exiting...\n");
75                 return;
76         }
77
78         collected = 0;
79
80         while(42) {
81
82                 if (trinketOpen() != ERR_OK) {
83                         fprintf(stderr, "trinketOpen fails\n");
84                         sleep(1);
85                         continue;
86                 }
87
88                 if (trinketGetCumDose(&curDose) != ERR_OK) {
89                         fprintf(stderr, "trinketGetCumDose fails\n");
90                         trinketClose();
91                         sleep(1);
92                         continue;
93                 }
94
95                 if (collected <= tics) {
96                         prevDose[collected++] = curDose;
97                 } else {
98                         for (i=1; i<=tics; i++)
99                                 prevDose[i - 1] = prevDose[i];
100                         prevDose[tics] = curDose;
101                 }
102
103                 if (collected > 1) {
104                         double  counts = curDose - prevDose[0];
105                         double  radlevel = 1e6 * coefficient * counts * 3600.0 /
106                                                                 (double)(period * (collected - 1));
107                         FILE    *fh = stdout;
108                         bool    successOpen = true;
109
110                         if (daemonize) {
111                                 if ((fh = fopen(tfile, "w")) == NULL) {
112                                         fprintf(stderr, "fopen fails\n");
113                                         successOpen = false;
114                                 }
115                         }
116
117                         if (successOpen) {
118                                 fprintf(fh, "total: %lld\n", (long long)curDose);
119                                 fprintf(fh, "counts: %lld\n", (long long)counts);
120                                 fprintf(fh, "tics: %d\n", collected - 1);
121                                 fprintf(fh, "radlevel: %.02f\n", radlevel);
122                         }
123
124                         if (fh && fh != stdout) {
125                                 fflush(fh);
126                                 fclose(fh);
127                                 rename(tfile, ofile);
128                         }
129                 }
130
131                 trinketClose();
132                 sleep(period);
133         }
134 }
135
136
137 extern char *optarg;
138 extern int opterr, optind;
139
140 int
141 main(int argn, char* argv[]) {
142         int i;
143         int     pidfd,
144                 logfd;
145
146         opterr = 0;
147         while((i=getopt(argn,argv,"dD:i:l:p:P:h")) != EOF) {
148                 switch(i) {
149                         case 'd':
150                                 daemonize = true;
151                                 break;
152                         case 'D':
153                                 dbdir = strdup(optarg);
154                                 break;
155                         case 'i':
156                                 tics = atoi(optarg);
157                                 break;
158                         case 'l':
159                                 logfile = strdup(optarg);
160                                 break;
161                         case 'p':
162                                 pidfile = strdup(optarg);
163                                 break;
164                         case 'P':
165                                 period = atoi(optarg);
166                                 break;
167                         case 'h':
168                         default:
169                                 usage(NULL);
170                 }
171         }
172
173         if (opterr || optind != argn)
174                 usage("argument err");
175
176         if (period <= 0)
177                 usage("Collecting period could not be zero nor negative");
178
179         if (tics <= 0)
180                 usage("Number of tics could not be zero nor negative");
181
182         if (daemonize && !dbdir)
183                 usage("trinketd: it is useless to use -d without -D");
184
185         if (dbdir && strlen(dbdir) > MAXPATHLEN - 32)
186                 usage("datadir is too long");
187
188         if (pidfile) {
189                 pidfd = open(pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
190                 if (pidfd < 0)
191                         usage("could not write pidfile");
192         }
193
194         if (logfile) {
195                 logfd = open(logfile, O_CREAT | O_WRONLY | O_APPEND, 0666);
196                 if (logfd < 0)
197                         usage("could not write logfile");
198         }
199
200         if (daemonize) {
201                 if (daemon(0, 0) == -1)
202                         usage("could not daemonize");
203         }
204
205         if (pidfile) {
206                 char pid[64];
207
208                 snprintf(pid, sizeof(pid), "%lld", (long long)getpid());
209                 if (write(pidfd, pid, strlen(pid)) != strlen(pid))
210                         usage("could not write pid");
211                 close(pidfd);
212         }
213
214         if (logfile) {
215                 dup2(logfd, fileno(stderr));
216                 close(logfd);
217         }
218
219         if (hid_init() < 0) {
220                 fprintf(stderr, "hid_init fails\n");
221                 exit(1);
222         }
223
224         if (dbdir) {
225                 snprintf(tfile, MAXPATHLEN, "%s/trinket.tmp", dbdir);
226                 snprintf(ofile, MAXPATHLEN, "%s/trinket.dat", dbdir);
227         }
228
229         main_loop();
230
231         hid_exit();
232         return 0;
233 }