36 Commits

Author SHA1 Message Date
6611220851 better blink 2021-03-03 19:16:22 +01:00
fe1bc101a3 version 2021-03-03 19:06:12 +01:00
daf281eb89 enable counter again 2021-03-01 11:58:29 +01:00
fa3df2bb32 fix 2021-03-01 11:43:15 +01:00
f310f604a6 x 2021-03-01 11:40:53 +01:00
e9afbbb379 x 2021-03-01 11:38:14 +01:00
e478f00ceb x 2021-03-01 11:37:53 +01:00
6555208d8c debug 2021-03-01 11:35:52 +01:00
577f1fbd42 debug 2021-03-01 11:34:16 +01:00
7dfac6e471 debug 2021-03-01 11:26:28 +01:00
4abe77f3c1 fix 2021-03-01 11:25:07 +01:00
acd5b2123a fix 2021-03-01 11:24:08 +01:00
3b3d5ee6a6 struct/union 2021-03-01 11:22:14 +01:00
6e73dccd66 changes 2021-03-01 11:16:54 +01:00
991f2724a6 fix 2021-03-01 11:10:02 +01:00
fc101e30f4 sntp change 2021-03-01 11:08:04 +01:00
e6e1bac2c3 check 2021-02-28 22:16:10 +01:00
3e5310557c fix 2021-02-28 22:14:51 +01:00
cee2116f3a rawSEconds 2021-02-28 22:14:25 +01:00
681c5d8695 ntoh 2021-02-28 22:06:17 +01:00
192fa69f68 xmt 2021-02-28 22:01:06 +01:00
2dc99d2b01 second pcap 2021-02-28 21:49:42 +01:00
ef71142ce3 test 2021-02-28 21:29:26 +01:00
d08fab14fa test 2021-02-28 21:28:10 +01:00
f04376ddd9 test 2021-02-28 21:24:45 +01:00
45d5f8af9b fix 2021-02-28 21:00:50 +01:00
0cb99a4854 fix 2021-02-28 20:57:04 +01:00
444e118e54 sntp implementation 2021-02-28 20:55:21 +01:00
145109a280 something broken now 2021-02-28 19:38:33 +01:00
9fcc43b012 retry 2021-02-28 13:31:29 +01:00
b81c9f445f changes 2021-02-28 12:53:43 +01:00
8e84fbe287 changes 2021-02-28 12:53:06 +01:00
7addea50a3 changes 2021-02-28 12:51:58 +01:00
7154b0abc3 changes 2021-02-28 12:40:38 +01:00
897e7039f2 forgotten 2021-02-28 12:37:26 +01:00
1b1180a72f start 2021-02-28 12:35:43 +01:00
10 changed files with 214 additions and 49 deletions

View File

@ -2,10 +2,11 @@
#define _NETWORK_ABSTRACTION_LAYER_IMPL_H_
#include <stdint.h>
#include <stdbool.h>
uint64_t networkSntpQuery();
int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen);
void networkImplInit();
bool isNetworkAvailable();
#endif /* _NETWORK_ABSTRACTION_LAYER_IMPL_H_ */

View File

@ -12,7 +12,7 @@ typedef enum {
LED_BLUE
} signalPin_t;
typedef enum { ON, OFF, TOGGLE, BLINK } signalAction_t;
typedef enum { ON, OFF, TOGGLE, BLINK, BLINK_FAST } signalAction_t;
void showInit();
void show(signalPin_t signalPin, signalAction_t action);

View File

@ -7,10 +7,9 @@
int wizInit();
bool isNetworkAvailable();
bool wizIsNetworkAvailable();
uint8_t* wizGetIPAddress();
bool wizDnsQuery(char *name, uint8_t *ip);
uint64_t wizSntpQuery();
#endif // _WIZHELPER_H_

View File

@ -48,7 +48,7 @@ void counterMinuteTick(void *handle) {
minuteBuffer->s.totalRunningHours = deviceStats->totalRunningHours;
minuteBuffer->s.totalPowercycles = deviceStats->totalPowercycles;
minuteBuffer->s.totalWatchdogResets = deviceStats->totalWatchdogResets;
minuteBuffer->s.version = strtol(VERSION, NULL, 16);
minuteBuffer->s.version = strtoll(VERSION, NULL, 16);
memset(minuteBuffer->s.deviceId, 0, sizeof(minuteBuffer->s.deviceId));

View File

@ -3,6 +3,7 @@
#include <iwdg.h>
#include <logger.h>
#include <sinkStruct.h>
#include <show.h>
#include <config.h>
#include <networkAbstractionLayer_impl.h>
@ -14,12 +15,13 @@ static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false };
static t_configBlock *config;
static void networkSecondsHandler(void *handle) {
static bool tryAgain = false;
seconds.seconds += 1;
if (! seconds.valid) {
if (! seconds.valid && isNetworkAvailable()) {
coloredMsg(LOG_YELLOW, "nsh, initially querying time");
uint64_t tmpSeconds = networkSntpQuery();
if (tmpSeconds != 0) {
@ -27,6 +29,7 @@ static void networkSecondsHandler(void *handle) {
seconds.seconds = tmpSeconds;
seconds.missedUpdates = 0;
seconds.valid = true;
show(LED_GREEN, ON);
} else {
coloredMsg(LOG_YELLOW, "nsh, failed");
seconds.missedUpdates += 1;
@ -38,6 +41,7 @@ static void networkSecondsHandler(void *handle) {
coloredMsg(LOG_YELLOW, "nsh, success, network time is %lu", tmpSeconds);
seconds.missedUpdates = 0;
tryAgain = false;
show(LED_GREEN, ON);
if (seconds.seconds != tmpSeconds) {
coloredMsg(LOG_YELLOW, "nsh, local time updated");
seconds.seconds = tmpSeconds;

View File

@ -1,16 +1,198 @@
#include <stdint.h>
#include <string.h>
#include <networkAbstractionLayer_impl.h>
#include <logger.h>
#include <PontCoopScheduler.h>
#include <wizHelper.h>
#include <wizchip_conf.h>
#include <socket.h>
#include <config.h>
#include <sntp.h>
static t_configBlock *config;
uint64_t networkSntpQuery() {
return wizSntpQuery();
static const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
static const uint16_t NTP_PORT = 123;
static const uint16_t MAX_SNTP_RETRIES = 100;
extern const uint8_t SNTP_SOCK;
const uint8_t SEND_LI_VN_MODE = 0xe3; // LI: unknown (3), VN: 4, Mode: Client (3)
typedef struct s_ntpStruct {
uint8_t li_vn_mode;
uint8_t stratum;
uint8_t poll;
uint8_t precision;
uint32_t rootdelay;
uint32_t rootdisp;
uint32_t refid;
//uint64_t reftime;
uint32_t reftime_h;
uint32_t reftime_l;
//uint64_t org;
uint32_t org_h;
uint32_t org_l;
//uint64_t rec;
uint32_t rec_h;
uint32_t rec_l;
//uint64_t xmt;
uint32_t xmt_h;
uint32_t xmt_l;
} t_ntpStruct;
typedef union __attribute__((__packed__)) {
t_ntpStruct s;
uint8_t b[sizeof(t_ntpStruct)];
} ntpMsg_t;
typedef struct {
ntpMsg_t ntpMsg;
uint8_t ntpAddr[4];
uint16_t retryCount;
uint64_t seconds;
enum {
SNTP_STATE_IDLE,
SNTP_STATE_START,
SNTP_STATE_SEND,
SNTP_STATE_RECV,
SNTP_STATE_DONE,
SNTP_STATE_ERROR
} sntpState;
} sntpEngineHandle_t;
sntpEngineHandle_t sntpEngineHandle = {
.seconds = 0,
.retryCount = 0,
.sntpState = SNTP_STATE_IDLE
};
void networkSntpEngine(void *handle) {
sntpEngineHandle_t *localHandle = (sntpEngineHandle_t*) handle;
if (isNetworkAvailable()) {
switch (localHandle->sntpState) {
case SNTP_STATE_START:
coloredMsg(LOG_BLUE, "nes, resolve ntp server");
if (! wizDnsQuery(config->ntpServer, localHandle->ntpAddr)) {
coloredMsg(LOG_BLUE, "nes, failed to resolve ntp server");
localHandle->sntpState = SNTP_STATE_ERROR;
} else {
localHandle->sntpState = SNTP_STATE_SEND;
schAdd(networkSntpEngine, (void*) localHandle, 10, 0);
}
break;
case SNTP_STATE_SEND:
coloredMsg(LOG_BLUE, "nes, about to call SNTP");
localHandle->retryCount = 0;
socket(SNTP_SOCK, Sn_MR_UDP, NTP_PORT, 0);
uint8_t sockState = getSn_SR(SNTP_SOCK);
if (sockState == SOCK_UDP) {
memset(&(localHandle->ntpMsg), 0, sizeof(localHandle->ntpMsg));
localHandle->ntpMsg.s.li_vn_mode = SEND_LI_VN_MODE;
localHandle->ntpMsg.s.xmt_l = 1;
sendto(SNTP_SOCK, localHandle->ntpMsg.b,
sizeof(localHandle->ntpMsg.b), localHandle->ntpAddr, NTP_PORT);
coloredMsg(LOG_BLUE, "nes, sent");
localHandle->sntpState = SNTP_STATE_RECV;
schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
} else {
coloredMsg(LOG_BLUE, "nes, socket in unexpected state %d", sockState);
localHandle->sntpState = SNTP_STATE_ERROR;
}
break;
case SNTP_STATE_RECV:
coloredMsg(LOG_BLUE, "nes, check receive");
localHandle->retryCount += 1;
uint16_t recvLen = getSn_RX_RSR(SNTP_SOCK);
if (recvLen == 0) {
if (localHandle->retryCount > MAX_SNTP_RETRIES) {
coloredMsg(LOG_BLUE, "nes, max retry count reached, failed");
localHandle->sntpState = SNTP_STATE_ERROR;
} else {
coloredMsg(LOG_BLUE, "nes, nothing received yet, try again");
schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
}
} else if (recvLen >= sizeof(localHandle->ntpMsg)) {
memset(&(localHandle->ntpMsg), 0, sizeof(localHandle->ntpMsg));
uint8_t srcAddr[4];
uint16_t srcPort;
recvfrom(SNTP_SOCK, localHandle->ntpMsg.b,
sizeof(localHandle->ntpMsg.b), srcAddr, &srcPort);
uint8_t *buf = localHandle->ntpMsg.b;
uint8_t x = 0;
coloredMsg(LOG_BLUE, "nes, %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
buf[x+0], buf[x+1], buf[x+2], buf[x+3], buf[x+4], buf[x+5], buf[x+6], buf[x+7],
buf[x+8], buf[x+9], buf[x+10], buf[x+11], buf[x+12], buf[x+13], buf[x+14], buf[x+15]);
x += 16;
coloredMsg(LOG_BLUE, "nes, %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
buf[x+0], buf[x+1], buf[x+2], buf[x+3], buf[x+4], buf[x+5], buf[x+6], buf[x+7],
buf[x+8], buf[x+9], buf[x+10], buf[x+11], buf[x+12], buf[x+13], buf[x+14], buf[x+15]);
x += 16;
coloredMsg(LOG_BLUE, "nes, %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
buf[x+0], buf[x+1], buf[x+2], buf[x+3], buf[x+4], buf[x+5], buf[x+6], buf[x+7],
buf[x+8], buf[x+9], buf[x+10], buf[x+11], buf[x+12], buf[x+13], buf[x+14], buf[x+15]);
x += 16;
coloredMsg(LOG_BLUE, "nes, %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
buf[x+0], buf[x+1], buf[x+2], buf[x+3], buf[x+4], buf[x+5], buf[x+6], buf[x+7],
buf[x+8], buf[x+9], buf[x+10], buf[x+11], buf[x+12], buf[x+13], buf[x+14], buf[x+15]);
close(SNTP_SOCK);
coloredMsg(LOG_BLUE, "nes, msg received from %d.%d.%d.%d:%d",
srcAddr[0], srcAddr[1], srcAddr[2], srcAddr[3],
srcPort);
coloredMsg(LOG_BLUE, "nes, received in the %d. cycles", localHandle->retryCount);
uint32_t xmt_h =
(localHandle->ntpMsg.s.xmt_h & 0x0000000ff) << 24 |
(localHandle->ntpMsg.s.xmt_h & 0x00000ff00) << 8 |
(localHandle->ntpMsg.s.xmt_h & 0x000ff0000) >> 8 |
(localHandle->ntpMsg.s.xmt_h & 0x0ff000000) >> 24;
localHandle->seconds = ((uint64_t)xmt_h) - UNIX_NTP_EPOCH_DIFF;
coloredMsg(LOG_BLUE, "nes, xmt: %08x", xmt_h);
coloredMsg(LOG_BLUE, "nes, seconds: %lu", localHandle->seconds);
localHandle->sntpState = SNTP_STATE_DONE;
} else {
coloredMsg(LOG_BLUE, "nes, invalid number of octets received: %d", recvLen);
localHandle->sntpState = SNTP_STATE_ERROR;
}
break;
default:
coloredMsg(LOG_BLUE, "nes, unexpected state");
}
} else {
coloredMsg(LOG_BLUE, "nes, no network yet, try again");
schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
}
}
uint64_t networkSntpQuery() {
uint64_t res = 0;
if (sntpEngineHandle.sntpState == SNTP_STATE_IDLE) {
coloredMsg(LOG_BLUE, "nsq, start sntp request");
sntpEngineHandle.sntpState = SNTP_STATE_START;
schAdd(networkSntpEngine, (void*) &sntpEngineHandle, 1, 0);
} else if (sntpEngineHandle.sntpState == SNTP_STATE_DONE) {
coloredMsg(LOG_BLUE, "nsq, start sntp done");
res = sntpEngineHandle.seconds;
coloredMsg(LOG_BLUE, "nsq, time is %lu", res);
sntpEngineHandle.sntpState = SNTP_STATE_IDLE;
} else if (sntpEngineHandle.sntpState == SNTP_STATE_ERROR) {
coloredMsg(LOG_BLUE, "nsq, start sntp failed");
sntpEngineHandle.sntpState = SNTP_STATE_IDLE;
}
return res;
}
extern uint8_t SINK_SOCK;
@ -38,6 +220,11 @@ int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufL
return 1;
}
bool isNetworkAvailable() {
return wizIsNetworkAvailable();
}
void networkImplInit() {
config = getConfig();
wizInit();
}

View File

@ -1,6 +1,7 @@
#include <main.h>
#include <show.h>
#include <PontCoopScheduler.h>
#include <show.h>
#include <stdint.h>
#include <stdlib.h>
@ -24,9 +25,20 @@ showElement_t showElement[] = {
};
static void showHandler(void *handle) {
const uint8_t MAX_CNT = 3;
static uint8_t cnt = 0;
cnt += 1;
if (cnt > MAX_CNT) {
cnt = 0;
}
uint8_t idx = 0;
while (showElement[idx].port) {
if (showElement[idx].currentState == BLINK) {
if (cnt == MAX_CNT) {
HAL_GPIO_TogglePin(showElement[idx].port, showElement[idx].pin);
}
} else if (showElement[idx].currentState == BLINK_FAST) {
HAL_GPIO_TogglePin(showElement[idx].port, showElement[idx].pin);
}
idx++;
@ -43,5 +55,5 @@ void show(signalPin_t signalPin, signalAction_t action) {
}
void showInit() {
schAdd(showHandler, NULL, 0, 250);
schAdd(showHandler, NULL, 0, 100);
}

View File

@ -26,18 +26,14 @@ static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE];
static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
extern const uint8_t DHCP_SOCK;
extern const uint8_t DNS_SOCK;
extern const uint8_t SNTP_SOCK;
static bool networkAvailable = false;
bool isNetworkAvailable() {
bool wizIsNetworkAvailable() {
return networkAvailable;
}
@ -90,7 +86,7 @@ static void wizDHCPAssign() {
coloredMsg(LOG_BLUE, "wizda, set netinfo again");
networkAvailable = true;
show(LED_GREEN, ON);
show(LED_GREEN, BLINK_FAST);
coloredMsg(LOG_BLUE, "wizda, network is available");
}
@ -133,40 +129,6 @@ bool wizDnsQuery(char *name, uint8_t *ip) {
}
uint64_t wizSntpQuery() {
uint64_t seconds = 0;
if (networkAvailable) {
coloredMsg(LOG_BLUE, "wizsq, about to call SNTP");
uint8_t ntpServer[4];
if (wizDnsQuery(config->ntpServer, ntpServer)) {
SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer);
uint16_t cycles = 0;
uint32_t startTime = HAL_GetTick();
while (1) {
cycles += 1;
datetime curTime;
if (1 == SNTP_run(&curTime)) {
seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF;
coloredMsg(LOG_BLUE, "wizsq, cycles: %u, curTime: %lu", cycles, seconds);
uint32_t stopTime = HAL_GetTick();
coloredMsg(LOG_BLUE, "wizsq, duration: %u ms", stopTime - startTime);
break;
}
}
if (seconds == 0) {
coloredMsg(LOG_BLUE, "wizsq, time update failed");
}
} else {
coloredMsg(LOG_BLUE, "wizsq, error when querying ntp server name");
}
}
return seconds;
}
static void wizPhyLinkHandler(void *handle) {
// this handler is anyhow called with a 1s period, so we reuse it for the DNS timer
DNS_time_handler();

BIN
docs/ntp.pcap Normal file

Binary file not shown.

BIN
docs/ntp2.pcap Normal file

Binary file not shown.