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