Skip to content

Commit 45c2f3a

Browse files
Report status of the time drift
1 parent 00fbe52 commit 45c2f3a

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

hc_clock.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@
5656

5757
#include "houseclock.h"
5858
#include "hc_clock.h"
59+
#include "hc_db.h"
5960

6061
static int clockPrecision;
6162
static int clockShowDrift = 0;
6263

6364
static int clockSynchronized = 0;
6465

66+
static int *hc_clock_drift_db = 0;
67+
6568
const char *hc_clock_help (int level) {
6669

6770
static const char *clockHelp[] = {
@@ -87,6 +90,14 @@ void hc_clock_initialize (int argc, const char **argv) {
8790
clockPrecision = atoi(precision_option);
8891

8992
clockSynchronized = 0;
93+
94+
i = hc_db_new (HC_CLOCK_DRIFT, sizeof(int), 120);
95+
if (i != 0) {
96+
fprintf (stderr, "cannot create %s: %s\n", HC_CLOCK_DRIFT, strerror(i));
97+
exit (1);
98+
}
99+
hc_clock_drift_db = (int *) hc_db_get (HC_CLOCK_DRIFT);
100+
for (i = 0; i < 120; ++i) hc_clock_drift_db[i] = 0;
90101
}
91102

92103
void hc_clock_synchronize(const struct timeval *gps,
@@ -97,8 +108,10 @@ void hc_clock_synchronize(const struct timeval *gps,
97108

98109
time_t absdrift = (drift < 0)? (0 - drift) : drift;
99110

111+
if (hc_clock_drift_db) hc_clock_drift_db[local->tv_sec%120] = (int)drift;
112+
100113
if (clockShowDrift || hc_test_mode()) {
101-
printf ("%8.3f\n", drift/1000.0);
114+
printf ("[%d]=%8.3f\n", local->tv_sec%120, drift/1000.0);
102115
return;
103116
}
104117

hc_clock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ void hc_clock_synchronize (const struct timeval *gps,
2727
const struct timeval *local, int latency);
2828
int hc_clock_synchronized (void);
2929

30+
/* Live database.
31+
*/
32+
#define HC_CLOCK_DRIFT "ClockDrift"
33+

hc_http.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "houseclock.h"
3838
#include "hc_db.h"
3939
#include "hc_nmea.h"
40+
#include "hc_clock.h"
4041
#include "hc_http.h"
4142

4243
#include "echttp.h"
@@ -51,19 +52,53 @@ static void hc_background (int fd, int mode) {
5152

5253
static const char *hc_http_status (const char *method, const char *uri,
5354
const char *data, int length) {
54-
static char buffer[1024];
55+
static char buffer[8192];
56+
static char *driftbuffer;
57+
static int driftbuffersize;
5558
static hc_nmea_status *status_db = 0;
59+
static int *drift_db = 0;
60+
static int drift_count;
5661
char latitude[20];
5762
char longitude[20];
5863

5964
if (status_db == 0) {
65+
status_db = (hc_nmea_status *) hc_db_get (HC_NMEA_STATUS);
66+
if (status_db == 0) {
67+
echttp_error (503, "Service Temporarily Unavailable");
68+
return "";
69+
}
6070
if (hc_db_get_count (HC_NMEA_STATUS) != 1
6171
|| hc_db_get_size (HC_NMEA_STATUS) != sizeof(hc_nmea_status)) {
6272
fprintf (stderr, "wrong data structure for table %s\n",
6373
HC_NMEA_STATUS);
6474
exit (1);
6575
}
66-
status_db = (hc_nmea_status *) hc_db_get (HC_NMEA_STATUS);
76+
}
77+
78+
if (drift_db == 0) {
79+
drift_db = (int *) hc_db_get (HC_CLOCK_DRIFT);
80+
if (drift_db == 0) {
81+
echttp_error (503, "Service Temporarily Unavailable");
82+
return "";
83+
}
84+
drift_count = hc_db_get_count (HC_CLOCK_DRIFT);
85+
if (hc_db_get_size (HC_CLOCK_DRIFT) != sizeof(int)) {
86+
fprintf (stderr, "wrong data structure for table %s\n",
87+
HC_CLOCK_DRIFT);
88+
exit (1);
89+
}
90+
driftbuffersize = drift_count * 12;
91+
driftbuffer = malloc(driftbuffersize);
92+
}
93+
94+
snprintf (driftbuffer, driftbuffersize, "%d", drift_db[0]);
95+
int room = driftbuffersize - strlen(driftbuffer);
96+
char *p = driftbuffer + (driftbuffersize-room);
97+
int i;
98+
for (i = 1; i < drift_count; ++i) {
99+
snprintf (p, room, ",%d", drift_db[i]);
100+
room -= strlen(p);
101+
p = driftbuffer + (driftbuffersize-room);
67102
}
68103

69104
// This conversion is not made when decoding the NMEA stream to avoid
@@ -77,7 +112,8 @@ static const char *hc_http_status (const char *method, const char *uri,
77112
snprintf (buffer, sizeof(buffer),
78113
"{\"gps\":{\"fix\":%s"
79114
",\"time\":[%c%c,%c%c,%c%c],\"date\":[%d,%c%c,%c%c]"
80-
",\"latitude\":%s,\"longitude\":%s,\"timestamp\":%zd.%03d}}",
115+
",\"latitude\":%s,\"longitude\":%s,\"timestamp\":%zd.%03d}"
116+
",\"clock\":{\"drift\":[%s]}}",
81117
status_db->fix?"true":"false",
82118
status_db->time[0], status_db->time[1],
83119
status_db->time[2], status_db->time[3],
@@ -87,7 +123,8 @@ static const char *hc_http_status (const char *method, const char *uri,
87123
status_db->date[0], status_db->date[1],
88124
latitude, longitude,
89125
(size_t) (status_db->timestamp.tv_sec),
90-
status_db->timestamp.tv_usec/1000);
126+
status_db->timestamp.tv_usec/1000,
127+
driftbuffer);
91128
echttp_content_type_json();
92129
return buffer;
93130
}

0 commit comments

Comments
 (0)