-
Notifications
You must be signed in to change notification settings - Fork 0
/
houseclock.c
209 lines (175 loc) · 5.42 KB
/
houseclock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* houseclock - A simple GPS Time Server with Web console
*
* Copyright 2019, Pascal Martin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* hc_main.c - Main loop of the houseclock program.
*
* SYNOPSYS:
*
* int hc_debug_enabled (void)
*
* Return true if debug mode option (-debug) was enabled. Mostly used
* in the definition of DEBUG.
*/
#include <stdlib.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include "houseclock.h"
#include "hc_db.h"
#include "hc_clock.h"
#include "hc_nmea.h"
#include "hc_ntp.h"
#include "hc_broadcast.h"
#include "hc_http.h"
static int HcDebug = 0;
static int HcTest = 0;
int hc_debug_enabled (void) {
return HcDebug;
}
int hc_test_mode (void) {
return HcTest;
}
static void hc_help (const char *argv0) {
int i = 1;
const char *help;
printf ("%s [-h] [-debug] [-test]%s%s%s\n",
argv0, hc_ntp_help(0), hc_nmea_help(0), hc_http_help(0));
printf ("\nGeneral options:\n");
printf (" -h: print this help.\n");
printf (" -debug prints a lot of debug traces.\n");
printf (" -test prints time drift compare to GPS.\n");
printf (" -db=N Size of the internal database, in MB\n");
printf ("\nNTP options:\n");
help = hc_ntp_help(i=1);
while (help) {
printf (" %s\n", help);
help = hc_ntp_help(++i);
}
printf ("\nGPS options:\n");
help = hc_nmea_help(i=1);
while (help) {
printf (" %s\n", help);
help = hc_nmea_help(++i);
}
printf ("\nHTTP options:\n");
help = hc_http_help(i=1);
while (help) {
printf (" %s\n", help);
help = hc_http_help(++i);
}
exit (0);
}
int main (int argc, const char **argv) {
int maxfd = 1;
int count;
int ntpsocket;
int gpstty = -1;
time_t last_period = 0;
struct timeval now;
const char *dbsizestr = "0";
// These strange statements are to make sure that fds 0 to 2 are
// reserved, since this application might output some errors.
// 3 descriptors are wasted if 0, 1 and 2 are already open. No big deal.
//
open ("/dev/null", O_RDONLY);
dup(open ("/dev/null", O_WRONLY));
int i;
for (i = 1; i < argc; ++i) {
if (strcmp("-h", argv[i]) == 0) {
hc_help(argv[0]);
}
echttp_option_match ("-db=", argv[i], &dbsizestr);
if (echttp_option_present ("-debug", argv[i])) HcDebug = 1;
if (echttp_option_present ("-test", argv[i])) HcTest = 1;
}
// Start the web interface.
hc_db_create (atoi(dbsizestr)*1024*1024);
pid_t httpid = fork();
if (httpid == 0) {
nice (19); // The HTTP server is low priority.
hc_http (argc, argv);
}
if (httpid < 0) {
fprintf (stderr, "[%s %d] Cannot fork: %s\n",
__FILE__, __LINE__, strerror (errno));
exit (1);
}
nice (-20); // The NTP server is high priority.
hc_clock_initialize (argc, argv);
hc_nmea_initialize (argc, argv);
ntpsocket = hc_ntp_initialize (argc, argv);
if (!HcTest) {
if (ntpsocket < 0) return 1;
}
if (maxfd <= ntpsocket) maxfd = ntpsocket + 1;
putenv("TZ=UTC"); // Always use UTC time.
for (;;) {
struct timeval timeout;
fd_set readset;
FD_ZERO(&readset);
if (ntpsocket > 0) {
FD_SET(ntpsocket, &readset);
}
gpstty = hc_nmea_listen();
if (gpstty >= 0) {
FD_SET(gpstty, &readset);
if (maxfd <= gpstty) maxfd = gpstty + 1;
}
gettimeofday(&now, NULL);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
count = select(maxfd+1, &readset, NULL, NULL, &timeout);
gettimeofday(&now, NULL);
if (count >= 0) {
if (gpstty > 0) {
if (FD_ISSET(gpstty, &readset)) {
gpstty = hc_nmea_process (&now);
}
}
if (ntpsocket) {
if (FD_ISSET(ntpsocket, &readset)) {
hc_ntp_process (&now);
}
}
}
if (now.tv_sec > last_period) {
if (ntpsocket > 0) {
hc_ntp_periodic (&now);
}
if (gpstty < 0) {
hc_nmea_initialize (argc, argv);
} else {
hc_nmea_periodic (&now);
}
last_period = now.tv_sec;
int wstatus;
if (waitpid (httpid, &wstatus, WNOHANG) == httpid) {
fprintf (stderr, "[%s %d] the HTTP server died, exit now\n",
__FILE__, __LINE__);
exit(1);
}
}
}
}