Repository navigation
Expand file tree
/
Copy pathreference_clock.c
More file actions
343 lines (282 loc) · 9.62 KB
/
Copy pathreference_clock.c
File metadata and controls
343 lines (282 loc) · 9.62 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#define _GNU_SOURCE
#include "reference_clock.h"
#include "time_sync.h"
#include <fcntl.h>
#include <math.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
/* ============================================================================
* RFC 5905 Section 4: Reference Clock Support
* ============================================================================
*/
/* ============================================================================
* RFC 5905 Section 4: Reference Clock State
* ============================================================================
*/
/* RFC 5905 Section 4: Reference clock state */
ReferenceClockState g_reference_clock;
/* ============================================================================
* RFC 5905 Section 4: Reference Clock Initialization
* ============================================================================
*/
/**
* reference_clock_init - Initialize reference clock subsystem
*
* RFC 5905 Section 4: Reference clocks are used to provide time
* information to NTP servers. Supported types:
* - GPS receivers (stratum 1)
* - Atomic clocks (stratum 1)
* - PPS (Pulse Per Second) input
*
* Called once during system initialization.
*
* Return: 0 on success, -1 on error
*/
int reference_clock_init(void) {
ReferenceClockState* state = &g_reference_clock;
/* Initialize all fields to zero (CERT C: Initialize all members) */
memset(state, 0, sizeof(*state));
/* Set default type */
state->type = REF_CLOCK_NONE;
/* Set default device */
snprintf(state->device, sizeof(state->device), "/dev/null");
/* Set default stratum (RFC 5905: 16 = unknown) */
state->stratum = 16;
/* Set default reference ID */
state->ref_id[0] = '\0';
/* Set default reference timestamp */
state->ref_ts.sec = 0;
state->ref_ts.frac = 0;
return 0;
}
/* ============================================================================
* RFC 5905 Section 4: Reference Clock Cleanup
* ============================================================================
*/
/**
* reference_clock_cleanup - Cleanup reference clock resources
*
* RFC 5905 Section 4: Clean up reference clock resources.
*
* Called during system shutdown.
*
* Return: 0 on success, -1 on error
*/
int reference_clock_cleanup(void) {
ReferenceClockState* state = &g_reference_clock;
/* Close any open devices */
if (state->type == REF_CLOCK_GPS) {
/* GPS receiver cleanup */
/* Close GPS device if open */
} else if (state->type == REF_CLOCK_ATOMIC) {
/* Atomic clock cleanup */
/* Close atomic clock device if open */
} else if (state->type == REF_CLOCK_PPS) {
/* PPS input cleanup */
/* Close PPS device if open */
}
return 0;
}
/* ============================================================================
* RFC 5905 Section 4: Reference Clock Configuration
* ============================================================================
*/
/**
* reference_clock_configure - Configure reference clock device
*
* RFC 5905 Section 4: Configure reference clock device.
*
* Parameters:
* device - Device path (e.g., /dev/ttyS0 for GPS, /dev/i2c-1 for atomic)
* type - Clock type (GPS, atomic, PPS)
*
* Return: 0 on success, -1 on error
*/
int reference_clock_configure(const char* device, ReferenceClockType type) {
ReferenceClockState* state = &g_reference_clock;
/* Validate device path */
if (device == NULL) { return -1; }
/* Check device path length (CERT C: Buffer size check) */
size_t len = strlen(device);
if (len >= sizeof(state->device)) { len = sizeof(state->device) - 1; }
/* Copy device path */
memcpy(state->device, device, len);
state->device[len] = '\0';
/* Validate clock type */
if (type < REF_CLOCK_NONE || type > REF_CLOCK_PPS) { return -1; }
state->type = type;
/* Enable clock */
return reference_clock_set_enabled(true);
}
/**
* reference_clock_set_enabled - Enable/disable reference clock
*
* Parameters:
* enabled - Enable (true) or disable (false)
*
* Return: 0 on success, -1 on error
*/
int reference_clock_set_enabled(bool enabled) {
ReferenceClockState* state = &g_reference_clock;
state->enabled = enabled;
return 0;
}
/* ============================================================================
* RFC 5905 Section 4: Reference Clock Check
* ============================================================================
*/
/**
* reference_clock_check - Check reference clock status and update time
*
* RFC 5905 Section 4: Check reference clock status and update time.
* This function handles:
* - GPS receivers: Read time from GPS NMEA sentences
* - Atomic clocks: Read time from I2C/SPI interface
* - PPS: Read time from pulse-per-second input
*
* Called periodically to check reference clock status and update time.
*
* Return: 0 on success, -1 on error
*/
int reference_clock_check(void) {
ReferenceClockState* state = &g_reference_clock;
/* Check if clock is enabled */
if (!state->enabled) { return 0; }
/* Check if clock type is valid */
if (state->type == REF_CLOCK_NONE) { return 0; }
/* Get current time */
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
/* Update reference timestamp */
state->ref_ts.sec = (uint32_t)ts.tv_sec;
state->ref_ts.frac = (uint32_t)ts.tv_nsec;
/* Update last update time */
state->last_update = time(NULL);
/* Simulate offset and jitter (replace with actual hardware reading) */
/* In production: Read from GPS/atomic clock device */
state->offset_us = 0;
state->jitter_us = 0;
/* Update system time if offset is within acceptable range */
int64_t offset_us = 0;
if (reference_clock_get_offset(&offset_us) == 0) {
/* Apply offset if within threshold */
if (llabs(offset_us) < 1000000) { /* 1 second threshold */
/* Apply time correction */
int result = apply_time_correction_slew_or_step(offset_us);
if (result != 0) {
syslog(LOG_WARNING, "Failed to apply time correction");
}
}
}
return 0;
}
/* ============================================================================
* RFC 5905 Section 4: Reference Clock Status
* ============================================================================
*/
/**
* reference_clock_get_status - Get reference clock status
*
* RFC 5905 Section 4: Get reference clock status.
*
* Return: 0 = OK, 1 = disabled, 2 = error, -1 = unknown
*/
int reference_clock_get_status(void) {
ReferenceClockState* state = &g_reference_clock;
if (!state->enabled) { return 1; }
if (state->type == REF_CLOCK_NONE) { return 2; }
return 0;
}
/**
* reference_clock_get_offset - Get reference clock offset
*
* RFC 5905 Section 4: Get reference clock offset.
*
* Parameters:
* offset_us - Pointer to store offset in microseconds
*
* Return: 0 on success, -1 on error
*/
int reference_clock_get_offset(int64_t* offset_us) {
ReferenceClockState* state = &g_reference_clock;
if (offset_us == NULL) { return -1; }
if (!state->enabled) { return -1; }
if (state->type == REF_CLOCK_NONE) { return -1; }
/* Get offset from state */
*offset_us = state->offset_us;
return 0;
}
/**
* reference_clock_get_jitter - Get reference clock jitter
*
* RFC 5905 Section 4: Get reference clock jitter.
*
* Parameters:
* jitter_us - Pointer to store jitter in microseconds
*
* Return: 0 on success, -1 on error
*/
int reference_clock_get_jitter(uint64_t* jitter_us) {
ReferenceClockState* state = &g_reference_clock;
if (jitter_us == NULL) { return -1; }
if (!state->enabled) { return -1; }
if (state->type == REF_CLOCK_NONE) { return -1; }
/* Get jitter from state */
*jitter_us = state->jitter_us;
return 0;
}
/* ============================================================================
* RFC 5905 Section 4: Reference Clock State Access
* ============================================================================
*/
/**
* get_reference_clock_state - Get pointer to reference clock state
*
* RFC 5905 Section 4: Get pointer to reference clock state.
*
* Return: Pointer to reference clock state (never NULL)
*/
ReferenceClockState* get_reference_clock_state(void) {
return &g_reference_clock;
}
/* ============================================================================
* RFC 5905 Section 4: Reference Clock Type Utilities
* ============================================================================
*/
/**
* reference_clock_type_to_string - Get string representation of clock type
*
* RFC 5905 Section 4: Get string representation of clock type.
*
* Parameters:
* type - Clock type
*
* Return: String representation (never NULL)
*/
const char* reference_clock_type_to_string(ReferenceClockType type) {
switch (type) {
case REF_CLOCK_NONE: return "none";
case REF_CLOCK_GPS: return "gps";
case REF_CLOCK_ATOMIC: return "atomic";
case REF_CLOCK_PPS: return "pps";
default: return "unknown";
}
}
/**
* reference_clock_is_available - Check if reference clock is available
*
* RFC 5905 Section 4: Check if reference clock is available.
*
* Return: true if available, false otherwise
*/
bool reference_clock_is_available(void) {
ReferenceClockState* state = &g_reference_clock;
return state->enabled && state->type != REF_CLOCK_NONE;
}
/* ============================================================================
* End of reference_clock.c
* ============================================================================
*/