| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include "azure_c_shared_utility/xlogging.h" |
| 5 |
|
| 6 |
/*Codes_SRS_SNTP_LWIP_30_001: [ The ntp_lwip shall implement the methods defined in sntp.h. ]*/ |
| 7 |
#include "sntp.h" |
| 8 |
#include "azure_c_shared_utility/threadapi.h" |
| 9 |
#include "azure_c_shared_utility/agenttime.h" |
| 10 |
#include "sntp_os.h" |
| 11 |
|
| 12 |
|
| 13 |
/*Codes_SRS_SNTP_LWIP_30_002: [ The serverName parameter shall be an NTP server URL which shall not be validated. ]*/ |
| 14 |
/*Codes_SRS_SNTP_LWIP_30_003: [ The SNTP_SetServerName shall set the NTP server to be used by ntp_lwip and return 0 to indicate success.]*/ |
| 15 |
// |
| 16 |
// SNTP_SetServerName must be called before SNTP_Init. |
| 17 |
// The character array pointed to by serverName parameter must persist |
| 18 |
// between calls to SNTP_SetServerName and SNTP_Deinit because the |
| 19 |
// char* is stored and no copy of the string is made. |
| 20 |
// |
| 21 |
// SNTP_SetServerName is a wrapper for the lwIP call sntp_setservername |
| 22 |
// and defers parameter validation to the lwIP library. |
| 23 |
// |
| 24 |
// Future implementations of this adapter may allow multiple calls to |
| 25 |
// SNTP_SetServerName in order to support multiple servers. |
| 26 |
// |
| 27 |
int SNTP_SetServerName(const char* serverName) |
| 28 |
{ |
| 29 |
// Future implementations could easily allow multiple calls to SNTP_SetServerName |
| 30 |
// by incrementing the index supplied to sntp_setservername |
| 31 |
sntp_setservername(0, (char*)serverName); |
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
/*Codes_SRS_SNTP_LWIP_30_004: [ SNTP_Init shall initialize the SNTP client, contact the NTP server to set system time, then return 0 to indicate success (lwIP has no failure path). ]*/ |
| 36 |
int SNTP_Init() |
| 37 |
{ |
| 38 |
LogInfo("Initializing SNTP"); |
| 39 |
sntp_setoperatingmode(SNTP_OPMODE_POLL); |
| 40 |
sntp_init(); |
| 41 |
time_t ts = 0; |
| 42 |
// Before 1980 is uninitialized |
| 43 |
while (ts < 10 * 365 * 24 * 3600) |
| 44 |
{ |
| 45 |
ThreadAPI_Sleep(1000); |
| 46 |
ts = get_time(NULL); |
| 47 |
|
| 48 |
} |
| 49 |
LogInfo("SNTP initialization complete"); |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
/*Codes_SRS_SNTP_LWIP_30_005: [ SNTP_Denit shall deinitialize the SNTP client. ]*/ |
| 54 |
void SNTP_Deinit() |
| 55 |
{ |
| 56 |
sntp_stop(); |
| 57 |
} |