1 /*-------------------------------------------------------------------------
4 * Copyright (c) 2016, Teodor Sigaev <teodor@sigaev.ru>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
26 *-------------------------------------------------------------------------
36 #include <hidapi/hidapi.h>
37 #include <sys/param.h>
42 static char* dbdir = NULL;
43 static int period = 300; /* in seconds */
45 static char* pidfile = NULL;
46 static bool daemonize = false;
47 static char tfile[MAXPATHLEN],
50 static const double coefficient = (248e-6 / 2637.0);
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] [-p pidfile] [-P period] [-D datadir] [-i tics] [-l logfile");
73 prevDose = malloc(sizeof(*prevDose) * (tics + 1));
75 tlog(TL_CRIT, "could not allocate array, exiting...");
81 if (trinketOpen() != ERR_OK) {
82 tlog(TL_ALARM, "trinketOpen fails");
87 if (trinketGetCumDose(&curDose) != ERR_OK) {
88 tlog(TL_ALARM, "trinketGetCumDose fails");
96 if (collected <= tics) {
97 prevDose[collected++] = curDose;
99 for (i=1; i<=tics; i++)
100 prevDose[i - 1] = prevDose[i];
101 prevDose[tics] = curDose;
105 double counts = curDose - prevDose[0];
106 double radlevel = 1e6 * coefficient * counts * 3600.0 /
107 (double)(period * (collected - 1));
109 bool successOpen = true;
112 if ((fh = fopen(tfile, "w")) == NULL) {
113 tlog(TL_ALARM, "fopen fails: %s", strerror(errno));
119 fprintf(fh, "total: %lld\n", (long long)curDose);
120 fprintf(fh, "counts: %lld\n", (long long)counts);
121 fprintf(fh, "tics: %d\n", collected - 1);
122 fprintf(fh, "radlevel: %.02f\n", radlevel);
125 if (fh && fh != stdout) {
128 rename(tfile, ofile);
138 extern int opterr, optind;
141 main(int argn, char* argv[]) {
144 char *logfile = NULL;
147 while((i=getopt(argn,argv,"dD:i:l:p:P:h")) != EOF) {
153 dbdir = strdup(optarg);
159 logfile = strdup(optarg);
162 pidfile = strdup(optarg);
165 period = atoi(optarg);
173 if (opterr || optind != argn)
174 usage("argument err");
177 usage("Collecting period could not be zero nor negative");
180 usage("Number of tics could not be zero nor negative");
182 if (daemonize && !dbdir)
183 usage("trinketd: it is useless to use -d without -D");
185 if (dbdir && strlen(dbdir) > MAXPATHLEN - 32)
186 usage("datadir is too long");
189 pidfd = open(pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
191 usage("could not write pidfile");
195 opentlog((logfile == NULL) ? TL_OPEN_SYSLOG : TL_OPEN_FILE,
198 if (daemon(0, 0) == -1)
199 tlog(TL_CRIT | TL_EXIT, "could not daemonize");
201 opentlog(TL_OPEN_STDERR | TL_OPEN_FILE, TL_INFO, logfile);
209 snprintf(pid, sizeof(pid), "%lld", (long long)getpid());
210 if (write(pidfd, pid, strlen(pid)) != strlen(pid))
211 tlog(TL_CRIT | TL_EXIT, "could not write pid");
215 tlog(TL_CRIT, "trinketd started, pid: %lld", (long long)getpid());
218 tlog(TL_CRIT | TL_EXIT, "hid_init fails");
221 snprintf(tfile, MAXPATHLEN, "%s/trinket.tmp", dbdir);
222 snprintf(ofile, MAXPATHLEN, "%s/trinket.dat", dbdir);