@@ -237,34 +237,69 @@ static const char *hc_http_clockdrift (const char *method, const char *uri,
237
237
return JsonBuffer ;
238
238
}
239
239
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 ) {
242
242
243
243
int i ;
244
+ const char * source ;
245
+ const char * quote = "\"" ;
244
246
char buffer [1024 ];
245
247
const char * prefix = "" ;
246
248
247
249
hc_http_attach_ntp ();
248
250
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 );
250
263
264
+ prefix = ",\"clients\":[" ;
251
265
for (i = 0 ; i < HC_NTP_DEPTH ; ++ i ) {
252
266
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 );
258
272
snprintf (buffer , sizeof (buffer ),
259
273
"%s{\"address\":\"%s\",\"timestamp\":%d.%03d,\"delta\":%d}" ,
260
274
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 );
264
297
strcat (JsonBuffer , buffer );
265
298
prefix = "," ;
266
299
}
267
- strcat (JsonBuffer , "]}}" );
300
+ if (prefix [1 ] == 0 ) strcat (JsonBuffer , "]" );
301
+ strcat (JsonBuffer , "}}" );
302
+
268
303
echttp_content_type_json ();
269
304
return JsonBuffer ;
270
305
}
@@ -280,7 +315,7 @@ void hc_http (int argc, const char **argv) {
280
315
echttp_route_uri ("/status" , hc_http_status );
281
316
echttp_route_uri ("/clock/drift" , hc_http_clockdrift );
282
317
echttp_route_uri ("/gps" , hc_http_gps );
283
- echttp_route_uri ("/ntp/clients " , hc_http_ntpclients );
318
+ echttp_route_uri ("/ntp" , hc_http_ntp );
284
319
echttp_static_route ("/ui" , "/usr/local/lib/houseclock/public" );
285
320
echttp_background (& hc_background );
286
321
echttp_loop ();
0 commit comments