Skip to content

Commit a579b7d

Browse files
Return the pool of servers as part of ntp status
1 parent 05e8a49 commit a579b7d

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

hc_clock.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777

7878
static int clockShowDrift = 0;
7979

80+
#define HC_CLOCK_DRIFT_DEPTH 120
8081
static hc_clock_status *hc_clock_status_db = 0;
8182
static int *hc_clock_drift_db = 0;
8283

@@ -111,13 +112,13 @@ void hc_clock_initialize (int argc, const char **argv) {
111112
}
112113
precision = atoi(precision_option);
113114

114-
i = hc_db_new (HC_CLOCK_DRIFT, sizeof(int), 120);
115+
i = hc_db_new (HC_CLOCK_DRIFT, sizeof(int), HC_CLOCK_DRIFT_DEPTH);
115116
if (i != 0) {
116117
fprintf (stderr, "cannot create %s: %s\n", HC_CLOCK_DRIFT, strerror(i));
117118
exit (1);
118119
}
119120
hc_clock_drift_db = (int *) hc_db_get (HC_CLOCK_DRIFT);
120-
for (i = 0; i < 120; ++i) hc_clock_drift_db[i] = 0;
121+
for (i = 0; i < HC_CLOCK_DRIFT_DEPTH; ++i) hc_clock_drift_db[i] = 0;
121122

122123
i = hc_db_new (HC_CLOCK_STATUS, sizeof(hc_clock_status), 1);
123124
if (i != 0) {
@@ -198,12 +199,13 @@ void hc_clock_synchronize(const struct timeval *source,
198199

199200
time_t absdrift = (drift < 0)? (0 - drift) : drift;
200201

201-
hc_clock_drift_db[source->tv_sec%120] = (int)drift;
202+
hc_clock_drift_db[source->tv_sec%HC_CLOCK_DRIFT_DEPTH] = (int)drift;
202203
hc_clock_status_db->drift = (int)drift;
203204
hc_clock_status_db->timestamp = *local;
204205

205206
if (clockShowDrift || hc_test_mode()) {
206-
printf ("[%d] %8.3f\n", local->tv_sec%120, drift/1000.0);
207+
printf ("[%d] %8.3f\n",
208+
local->tv_sec%HC_CLOCK_DRIFT_DEPTH, drift/1000.0);
207209
if (hc_test_mode()) {
208210
if (absdrift < hc_clock_status_db->precision) {
209211
hc_clock_status_db->synchronized = 1;

hc_http.c

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,34 +237,69 @@ static const char *hc_http_clockdrift (const char *method, const char *uri,
237237
return JsonBuffer;
238238
}
239239

240-
static const char *hc_http_ntpclients (const char *method, const char *uri,
241-
const char *data, int length) {
240+
static const char *hc_http_ntp (const char *method, const char *uri,
241+
const char *data, int length) {
242242

243243
int i;
244+
const char *source;
245+
const char *quote = "\"";
244246
char buffer[1024];
245247
const char *prefix = "";
246248

247249
hc_http_attach_ntp();
248250

249-
strncpy (JsonBuffer, "{\"ntp\":{\"clients\":[", sizeof(JsonBuffer));
251+
if (ntp_db->stratum == 1) {
252+
source = "GPS";
253+
} else if (ntp_db->source >= 0) {
254+
source = ntp_db->pool[ntp_db->source].name;
255+
} else {
256+
source = "null";
257+
quote = "";
258+
}
259+
260+
snprintf (JsonBuffer, sizeof(JsonBuffer),
261+
"{\"ntp\":{\"source\":%s%s%s,\"stratum\":%d",
262+
quote, source, quote, ntp_db->stratum);
250263

264+
prefix = ",\"clients\":[";
251265
for (i = 0; i < HC_NTP_DEPTH; ++i) {
252266
int delta;
253-
if (ntp_db->clients[i].local.tv_sec == 0) continue;
254-
delta = ((ntp_db->clients[i].local.tv_sec
255-
- ntp_db->clients[i].origin.tv_sec) * 1000)
256-
+ ((ntp_db->clients[i].local.tv_usec
257-
- ntp_db->clients[i].origin.tv_usec) / 1000);
267+
struct hc_ntp_client *client = ntp_db->clients + i;
268+
269+
if (client->local.tv_sec == 0) continue;
270+
delta = ((client->local.tv_sec - client->origin.tv_sec) * 1000)
271+
+ ((client->local.tv_usec - client->origin.tv_usec) / 1000);
258272
snprintf (buffer, sizeof(buffer),
259273
"%s{\"address\":\"%s\",\"timestamp\":%d.%03d,\"delta\":%d}",
260274
prefix,
261-
hc_broadcast_format(&(ntp_db->clients[i].address)),
262-
ntp_db->clients[i].local.tv_sec,
263-
ntp_db->clients[i].local.tv_usec / 1000, delta);
275+
hc_broadcast_format(&(client->address)),
276+
client->local.tv_sec, client->local.tv_usec / 1000, delta);
277+
strcat (JsonBuffer, buffer);
278+
prefix = ",";
279+
}
280+
if (prefix[1] == 0) strcat(JsonBuffer, "]");
281+
282+
prefix = ",\"servers\":[";
283+
for (i = 0; i < HC_NTP_POOL; ++i) {
284+
int delta;
285+
struct hc_ntp_server *server = ntp_db->pool + i;
286+
287+
if (server->local.tv_sec == 0) continue;
288+
delta = ((server->local.tv_sec - server->origin.tv_sec) * 1000)
289+
+ ((server->local.tv_usec - server->origin.tv_usec) / 1000);
290+
snprintf (buffer, sizeof(buffer),
291+
"%s{\"address\":\"%s\",\"timestamp\":%d.%03d,"
292+
"\"delta\":%d,\"stratum\":%d}",
293+
prefix,
294+
server->name,
295+
server->local.tv_sec,
296+
server->local.tv_usec / 1000, delta, server->stratum);
264297
strcat (JsonBuffer, buffer);
265298
prefix = ",";
266299
}
267-
strcat (JsonBuffer, "]}}");
300+
if (prefix[1] == 0) strcat(JsonBuffer, "]");
301+
strcat (JsonBuffer, "}}");
302+
268303
echttp_content_type_json();
269304
return JsonBuffer;
270305
}
@@ -280,7 +315,7 @@ void hc_http (int argc, const char **argv) {
280315
echttp_route_uri ("/status", hc_http_status);
281316
echttp_route_uri ("/clock/drift", hc_http_clockdrift);
282317
echttp_route_uri ("/gps", hc_http_gps);
283-
echttp_route_uri ("/ntp/clients", hc_http_ntpclients);
318+
echttp_route_uri ("/ntp", hc_http_ntp);
284319
echttp_static_route ("/ui", "/usr/local/lib/houseclock/public");
285320
echttp_background (&hc_background);
286321
echttp_loop();

0 commit comments

Comments
 (0)