Compare commits
34 Commits
new_sntp_i
...
new_sntp_s
Author | SHA1 | Date | |
---|---|---|---|
daf281eb89
|
|||
fa3df2bb32
|
|||
f310f604a6
|
|||
e9afbbb379
|
|||
e478f00ceb
|
|||
6555208d8c
|
|||
577f1fbd42
|
|||
7dfac6e471
|
|||
4abe77f3c1
|
|||
acd5b2123a
|
|||
3b3d5ee6a6
|
|||
6e73dccd66
|
|||
991f2724a6
|
|||
fc101e30f4
|
|||
e6e1bac2c3
|
|||
3e5310557c
|
|||
cee2116f3a
|
|||
681c5d8695
|
|||
192fa69f68
|
|||
2dc99d2b01
|
|||
ef71142ce3
|
|||
d08fab14fa
|
|||
f04376ddd9
|
|||
45d5f8af9b
|
|||
0cb99a4854
|
|||
444e118e54
|
|||
145109a280 | |||
9fcc43b012
|
|||
b81c9f445f
|
|||
8e84fbe287
|
|||
7addea50a3
|
|||
7154b0abc3
|
|||
897e7039f2
|
|||
1b1180a72f
|
@ -98,7 +98,7 @@ int main(void)
|
|||||||
MX_SPI2_Init();
|
MX_SPI2_Init();
|
||||||
MX_TIM1_Init();
|
MX_TIM1_Init();
|
||||||
MX_USART1_UART_Init();
|
MX_USART1_UART_Init();
|
||||||
// MX_IWDG_Init();
|
MX_IWDG_Init();
|
||||||
/* USER CODE BEGIN 2 */
|
/* USER CODE BEGIN 2 */
|
||||||
my_setup_2();
|
my_setup_2();
|
||||||
|
|
||||||
|
@ -2,12 +2,11 @@
|
|||||||
#define _NETWORK_ABSTRACTION_LAYER_IMPL_H_
|
#define _NETWORK_ABSTRACTION_LAYER_IMPL_H_
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
uint64_t networkSntpQuery();
|
uint64_t networkSntpQuery();
|
||||||
|
|
||||||
|
|
||||||
int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen);
|
int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen);
|
||||||
void networkImplInit();
|
void networkImplInit();
|
||||||
|
bool isNetworkAvailable();
|
||||||
|
|
||||||
#endif /* _NETWORK_ABSTRACTION_LAYER_IMPL_H_ */
|
#endif /* _NETWORK_ABSTRACTION_LAYER_IMPL_H_ */
|
||||||
|
@ -7,10 +7,9 @@
|
|||||||
|
|
||||||
|
|
||||||
int wizInit();
|
int wizInit();
|
||||||
bool isNetworkAvailable();
|
bool wizIsNetworkAvailable();
|
||||||
uint8_t* wizGetIPAddress();
|
uint8_t* wizGetIPAddress();
|
||||||
bool wizDnsQuery(char *name, uint8_t *ip);
|
bool wizDnsQuery(char *name, uint8_t *ip);
|
||||||
uint64_t wizSntpQuery();
|
|
||||||
|
|
||||||
|
|
||||||
#endif // _WIZHELPER_H_
|
#endif // _WIZHELPER_H_
|
||||||
|
@ -14,12 +14,13 @@ static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false };
|
|||||||
static t_configBlock *config;
|
static t_configBlock *config;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void networkSecondsHandler(void *handle) {
|
static void networkSecondsHandler(void *handle) {
|
||||||
static bool tryAgain = false;
|
static bool tryAgain = false;
|
||||||
|
|
||||||
seconds.seconds += 1;
|
seconds.seconds += 1;
|
||||||
|
|
||||||
if (! seconds.valid) {
|
if (! seconds.valid && isNetworkAvailable()) {
|
||||||
coloredMsg(LOG_YELLOW, "nsh, initially querying time");
|
coloredMsg(LOG_YELLOW, "nsh, initially querying time");
|
||||||
uint64_t tmpSeconds = networkSntpQuery();
|
uint64_t tmpSeconds = networkSntpQuery();
|
||||||
if (tmpSeconds != 0) {
|
if (tmpSeconds != 0) {
|
||||||
|
@ -4,134 +4,195 @@
|
|||||||
#include <networkAbstractionLayer_impl.h>
|
#include <networkAbstractionLayer_impl.h>
|
||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
#include <PontCoopScheduler.h>
|
#include <PontCoopScheduler.h>
|
||||||
|
|
||||||
#include <wizHelper.h>
|
#include <wizHelper.h>
|
||||||
#include <wizchip_conf.h>
|
#include <wizchip_conf.h>
|
||||||
#include <socket.h>
|
#include <socket.h>
|
||||||
#include <wizchip_conf.h>
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
#include <sntp.h>
|
||||||
|
|
||||||
|
|
||||||
static t_configBlock *config;
|
static t_configBlock *config;
|
||||||
|
|
||||||
|
|
||||||
const uint16_t MAX_RECV_RETRY_COUNT = 100;
|
|
||||||
const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
|
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)
|
const uint8_t SEND_LI_VN_MODE = 0xe3; // LI: unknown (3), VN: 4, Mode: Client (3)
|
||||||
typedef struct {
|
typedef struct s_ntpStruct {
|
||||||
uint8_t li_vn_mode;
|
uint8_t li_vn_mode;
|
||||||
uint8_t stratum;
|
uint8_t stratum;
|
||||||
uint8_t poll;
|
uint8_t poll;
|
||||||
uint8_t precision;
|
uint8_t precision;
|
||||||
uint32_t rootdelay;
|
uint32_t rootdelay;
|
||||||
uint32_t rootdisp;
|
uint32_t rootdisp;
|
||||||
uint32_t refid;
|
uint32_t refid;
|
||||||
uint64_t reftime;
|
//uint64_t reftime;
|
||||||
uint64_t org;
|
uint32_t reftime_h;
|
||||||
uint64_t rec;
|
uint32_t reftime_l;
|
||||||
uint64_t xmt;
|
//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;
|
} ntpMsg_t;
|
||||||
|
|
||||||
extern uint8_t SNTP_SOCK;
|
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;
|
||||||
|
|
||||||
const uint16_t NTP_PORT = 123;
|
sntpEngineHandle_t sntpEngineHandle = {
|
||||||
|
.seconds = 0,
|
||||||
enum {
|
.retryCount = 0,
|
||||||
SNTP_STATE_IDLE,
|
.sntpState = SNTP_STATE_IDLE
|
||||||
SNTP_STATE_START,
|
};
|
||||||
SNTP_STATE_SEND,
|
|
||||||
SNTP_STATE_RECV,
|
|
||||||
SNTP_STATE_ERROR,
|
|
||||||
SNTP_STATE_DONE
|
|
||||||
} sntpState = SNTP_STATE_IDLE;
|
|
||||||
uint64_t seconds;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
void networkSntpEngine(void *handle) {
|
||||||
static void networkSntpEngine(void *handle) {
|
sntpEngineHandle_t *localHandle = (sntpEngineHandle_t*) handle;
|
||||||
static uint16_t retryCount = 0;
|
if (isNetworkAvailable()) {
|
||||||
|
switch (localHandle->sntpState) {
|
||||||
coloredMsg(LOG_BLUE, "nes, %u", sntpState);
|
case SNTP_STATE_START:
|
||||||
|
coloredMsg(LOG_BLUE, "nes, resolve ntp server");
|
||||||
switch (sntpState) {
|
if (! wizDnsQuery(config->ntpServer, localHandle->ntpAddr)) {
|
||||||
case SNTP_STATE_START:
|
coloredMsg(LOG_BLUE, "nes, failed to resolve ntp server");
|
||||||
coloredMsg(LOG_BLUE, "nes, about to send");
|
localHandle->sntpState = SNTP_STATE_ERROR;
|
||||||
uint8_t ntpAddr[4];
|
} else {
|
||||||
if (! wizDnsQuery(config->ntpServer, ntpAddr)) {
|
localHandle->sntpState = SNTP_STATE_SEND;
|
||||||
coloredMsg(LOG_BLUE, "nes, failed to resolve ntp server");
|
schAdd(networkSntpEngine, (void*) localHandle, 10, 0);
|
||||||
sntpState = SNTP_STATE_ERROR;
|
}
|
||||||
} else {
|
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);
|
socket(SNTP_SOCK, Sn_MR_UDP, NTP_PORT, 0);
|
||||||
retryCount = 0;
|
|
||||||
uint8_t sockState = getSn_SR(SNTP_SOCK);
|
uint8_t sockState = getSn_SR(SNTP_SOCK);
|
||||||
if (sockState == SOCK_UDP) {
|
if (sockState == SOCK_UDP) {
|
||||||
ntpMsg_t ntpMsg;
|
memset(&(localHandle->ntpMsg), 0, sizeof(localHandle->ntpMsg));
|
||||||
memset(&ntpMsg, 0, sizeof(ntpMsg));
|
localHandle->ntpMsg.s.li_vn_mode = SEND_LI_VN_MODE;
|
||||||
ntpMsg.li_vn_mode = SEND_LI_VN_MODE;
|
localHandle->ntpMsg.s.xmt_l = 1;
|
||||||
sendto(SNTP_SOCK, (uint8_t*)&ntpMsg, sizeof(ntpMsg), ntpAddr, NTP_PORT);
|
sendto(SNTP_SOCK, localHandle->ntpMsg.b,
|
||||||
|
sizeof(localHandle->ntpMsg.b), localHandle->ntpAddr, NTP_PORT);
|
||||||
coloredMsg(LOG_BLUE, "nes, sent");
|
coloredMsg(LOG_BLUE, "nes, sent");
|
||||||
sntpState = SNTP_STATE_RECV;
|
localHandle->sntpState = SNTP_STATE_RECV;
|
||||||
schAdd(networkSntpEngine, NULL, 1000, 0);
|
schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
|
||||||
} else {
|
} else {
|
||||||
coloredMsg(LOG_BLUE, "nes, socket in unexpected state: %d", sockState);
|
coloredMsg(LOG_BLUE, "nes, socket in unexpected state %d", sockState);
|
||||||
sntpState = SNTP_STATE_ERROR;
|
localHandle->sntpState = SNTP_STATE_ERROR;
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
break;
|
case SNTP_STATE_RECV:
|
||||||
case SNTP_STATE_RECV:
|
coloredMsg(LOG_BLUE, "nes, check receive");
|
||||||
coloredMsg(LOG_BLUE, "nes, check receive");
|
localHandle->retryCount += 1;
|
||||||
uint16_t recvLen = getSn_RX_RSR(SNTP_SOCK);
|
uint16_t recvLen = getSn_RX_RSR(SNTP_SOCK);
|
||||||
if (recvLen == 0) {
|
if (recvLen == 0) {
|
||||||
retryCount += 1;
|
if (localHandle->retryCount > MAX_SNTP_RETRIES) {
|
||||||
if (retryCount > MAX_RECV_RETRY_COUNT) {
|
coloredMsg(LOG_BLUE, "nes, max retry count reached, failed");
|
||||||
coloredMsg(LOG_BLUE, "nes max retry count reached, failed");
|
localHandle->sntpState = SNTP_STATE_ERROR;
|
||||||
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 {
|
} else {
|
||||||
coloredMsg(LOG_BLUE, "nes, nothing received yet, try again");
|
coloredMsg(LOG_BLUE, "nes, invalid number of octets received: %d", recvLen);
|
||||||
schAdd(networkSntpEngine, NULL, 100, 0);
|
localHandle->sntpState = SNTP_STATE_ERROR;
|
||||||
}
|
}
|
||||||
} else if (recvLen == sizeof(ntpMsg_t)) {
|
break;
|
||||||
ntpMsg_t ntpMsg;
|
default:
|
||||||
memset(&ntpMsg, 0, sizeof(ntpMsg_t));
|
coloredMsg(LOG_BLUE, "nes, unexpected state");
|
||||||
uint8_t srcAddr[4];
|
}
|
||||||
uint16_t srcPort;
|
} else {
|
||||||
recvfrom(SNTP_SOCK, (uint8_t*)&ntpMsg, sizeof(ntpMsg_t), srcAddr, &srcPort);
|
coloredMsg(LOG_BLUE, "nes, no network yet, try again");
|
||||||
close(SNTP_SOCK);
|
schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
|
||||||
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", retryCount);
|
|
||||||
seconds = ntpMsg.rec - UNIX_NTP_EPOCH_DIFF;
|
|
||||||
coloredMsg(LOG_BLUE, "nes, seconds: %lu", seconds);
|
|
||||||
sntpState = SNTP_STATE_DONE;
|
|
||||||
} else {
|
|
||||||
coloredMsg(LOG_BLUE, "nes, invalid number of octets received: %d", recvLen);
|
|
||||||
sntpState = SNTP_STATE_ERROR;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
coloredMsg(LOG_BLUE, "nes, unexpected state");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
uint64_t networkSntpQuery() {
|
uint64_t networkSntpQuery() {
|
||||||
/*
|
|
||||||
uint64_t res = 0;
|
uint64_t res = 0;
|
||||||
if (sntpState == SNTP_STATE_IDLE) {
|
|
||||||
sntpState = SNTP_STATE_START;
|
|
||||||
schAdd(networkSntpEngine, NULL, 1, 0);
|
|
||||||
} else if (sntpState == SNTP_STATE_ERROR) {
|
|
||||||
sntpState = SNTP_STATE_IDLE;
|
|
||||||
} else if (sntpState == SNTP_STATE_DONE) {
|
|
||||||
sntpState = SNTP_STATE_IDLE;
|
|
||||||
res = seconds;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
*/
|
|
||||||
return 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;
|
extern uint8_t SINK_SOCK;
|
||||||
|
|
||||||
@ -159,6 +220,10 @@ int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufL
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isNetworkAvailable() {
|
||||||
|
return wizIsNetworkAvailable();
|
||||||
|
}
|
||||||
|
|
||||||
void networkImplInit() {
|
void networkImplInit() {
|
||||||
config = getConfig();
|
config = getConfig();
|
||||||
wizInit();
|
wizInit();
|
||||||
|
@ -33,7 +33,7 @@ extern const uint8_t DNS_SOCK;
|
|||||||
|
|
||||||
static bool networkAvailable = false;
|
static bool networkAvailable = false;
|
||||||
|
|
||||||
bool isNetworkAvailable() {
|
bool wizIsNetworkAvailable() {
|
||||||
return networkAvailable;
|
return networkAvailable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
docs/ntp.pcap
Normal file
BIN
docs/ntp.pcap
Normal file
Binary file not shown.
BIN
docs/ntp2.pcap
Normal file
BIN
docs/ntp2.pcap
Normal file
Binary file not shown.
Reference in New Issue
Block a user