something broken now

This commit is contained in:
2021-02-28 19:38:33 +01:00
parent 9fcc43b012
commit 145109a280

View File

@ -13,73 +13,79 @@ static t_configBlock *config;
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
static const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800; static const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
extern const uint8_t SNTP_SOCK; extern const uint8_t SNTP_SOCK;
static uint64_t seconds;
static enum { typedef struct {
SNTP_STATE_IDLE, uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
SNTP_STATE_SEND, uint64_t seconds;
SNTP_STATE_DONE, enum {
SNTP_STATE_ERROR SNTP_STATE_IDLE,
} sntpState = SNTP_STATE_IDLE; SNTP_STATE_SEND,
SNTP_STATE_DONE,
SNTP_STATE_ERROR
} sntpState;
} sntpEngineHandle_t;
sntpEngineHandle_t sntpEngineHandle = { .seconds = 0, .sntpState = SNTP_STATE_IDLE };
void networkSntpEngine(void *handle) { void networkSntpEngine(void *handle) {
sntpEngineHandle_t *localHandle = (sntpEngineHandle_t*) handle;
if (isNetworkAvailable()) { if (isNetworkAvailable()) {
switch (sntpState) { switch (localHandle->sntpState) {
case SNTP_STATE_SEND: case SNTP_STATE_SEND:
coloredMsg(LOG_BLUE, "nes, about to call SNTP"); coloredMsg(LOG_BLUE, "nes, about to call SNTP");
uint8_t ntpServer[4]; uint8_t ntpServer[4];
if (wizDnsQuery(config->ntpServer, ntpServer)) { if (wizDnsQuery(config->ntpServer, ntpServer)) {
SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer); SNTP_init(SNTP_SOCK, ntpServer, 0, localHandle->sntpBuffer);
uint16_t cycles = 0; uint16_t cycles = 0;
uint32_t startTime = HAL_GetTick(); uint32_t startTime = HAL_GetTick();
while (1) { while (1) {
cycles += 1; cycles += 1;
datetime curTime; datetime curTime;
if (1 == SNTP_run(&curTime)) { if (1 == SNTP_run(&curTime)) {
seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF; localHandle->seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF;
coloredMsg(LOG_BLUE, "nes, cycles: %u, curTime: %lu", cycles, seconds); coloredMsg(LOG_BLUE, "nes, cycles: %u, curTime: %lu", cycles, localHandle->seconds);
uint32_t stopTime = HAL_GetTick(); uint32_t stopTime = HAL_GetTick();
coloredMsg(LOG_BLUE, "nes, duration: %u ms", stopTime - startTime); coloredMsg(LOG_BLUE, "nes, duration: %u ms", stopTime - startTime);
sntpState = SNTP_STATE_DONE; localHandle->sntpState = SNTP_STATE_DONE;
break; break;
} }
} }
if (seconds == 0) { if (localHandle->seconds == 0) {
coloredMsg(LOG_BLUE, "nes, time update failed"); coloredMsg(LOG_BLUE, "nes, time update failed");
sntpState = SNTP_STATE_ERROR; localHandle->sntpState = SNTP_STATE_ERROR;
} }
} else { } else {
coloredMsg(LOG_BLUE, "nes, error when querying ntp server name"); coloredMsg(LOG_BLUE, "nes, error when querying ntp server name");
sntpState = SNTP_STATE_ERROR; localHandle->sntpState = SNTP_STATE_ERROR;
} }
default: default:
coloredMsg(LOG_BLUE, "nes, unexpected state"); coloredMsg(LOG_BLUE, "nes, unexpected state");
} }
} else { } else {
coloredMsg(LOG_BLUE, "nes, no network yet, try again"); coloredMsg(LOG_BLUE, "nes, no network yet, try again");
schAdd(networkSntpEngine, NULL, 100, 0); schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
} }
} }
uint64_t networkSntpQuery() { uint64_t networkSntpQuery() {
uint64_t res = 0; uint64_t res = 0;
if (sntpState == SNTP_STATE_IDLE) { if (sntpEngineHandle.sntpState == SNTP_STATE_IDLE) {
coloredMsg(LOG_BLUE, "nsq, start sntp request"); coloredMsg(LOG_BLUE, "nsq, start sntp request");
sntpState = SNTP_STATE_SEND; sntpEngineHandle.sntpState = SNTP_STATE_SEND;
schAdd(networkSntpEngine, NULL, 1, 0); schAdd(networkSntpEngine, (void*) &sntpEngineHandle, 1, 0);
} else if (sntpState == SNTP_STATE_DONE) { } else if (sntpEngineHandle.sntpState == SNTP_STATE_DONE) {
coloredMsg(LOG_BLUE, "nsq, start sntp done"); coloredMsg(LOG_BLUE, "nsq, start sntp done");
res = seconds; res = sntpEngineHandle.seconds;
sntpState = SNTP_STATE_IDLE; sntpEngineHandle.sntpState = SNTP_STATE_IDLE;
} else if (sntpState == SNTP_STATE_ERROR) { } else if (sntpEngineHandle.sntpState == SNTP_STATE_ERROR) {
coloredMsg(LOG_BLUE, "nsq, start sntp failed"); coloredMsg(LOG_BLUE, "nsq, start sntp failed");
sntpState = SNTP_STATE_IDLE; sntpEngineHandle.sntpState = SNTP_STATE_IDLE;
} }
return res; return res;