37
37
#include "houseclock.h"
38
38
#include "hc_db.h"
39
39
#include "hc_nmea.h"
40
+ #include "hc_clock.h"
40
41
#include "hc_http.h"
41
42
42
43
#include "echttp.h"
@@ -51,19 +52,53 @@ static void hc_background (int fd, int mode) {
51
52
52
53
static const char * hc_http_status (const char * method , const char * uri ,
53
54
const char * data , int length ) {
54
- static char buffer [1024 ];
55
+ static char buffer [8192 ];
56
+ static char * driftbuffer ;
57
+ static int driftbuffersize ;
55
58
static hc_nmea_status * status_db = 0 ;
59
+ static int * drift_db = 0 ;
60
+ static int drift_count ;
56
61
char latitude [20 ];
57
62
char longitude [20 ];
58
63
59
64
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
+ }
60
70
if (hc_db_get_count (HC_NMEA_STATUS ) != 1
61
71
|| hc_db_get_size (HC_NMEA_STATUS ) != sizeof (hc_nmea_status )) {
62
72
fprintf (stderr , "wrong data structure for table %s\n" ,
63
73
HC_NMEA_STATUS );
64
74
exit (1 );
65
75
}
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 );
67
102
}
68
103
69
104
// 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,
77
112
snprintf (buffer , sizeof (buffer ),
78
113
"{\"gps\":{\"fix\":%s"
79
114
",\"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]}}" ,
81
117
status_db -> fix ?"true" :"false" ,
82
118
status_db -> time [0 ], status_db -> time [1 ],
83
119
status_db -> time [2 ], status_db -> time [3 ],
@@ -87,7 +123,8 @@ static const char *hc_http_status (const char *method, const char *uri,
87
123
status_db -> date [0 ], status_db -> date [1 ],
88
124
latitude , longitude ,
89
125
(size_t ) (status_db -> timestamp .tv_sec ),
90
- status_db -> timestamp .tv_usec /1000 );
126
+ status_db -> timestamp .tv_usec /1000 ,
127
+ driftbuffer );
91
128
echttp_content_type_json ();
92
129
return buffer ;
93
130
}
0 commit comments