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