struct/union

This commit is contained in:
2021-03-01 11:22:14 +01:00
parent 6e73dccd66
commit 3b3d5ee6a6

View File

@ -24,26 +24,29 @@ extern const uint8_t SNTP_SOCK;
const uint8_t SEND_LI_VN_MODE = 0xe3; // LI: unknown (3), VN: 4, Mode: Client (3)
typedef struct __attribute__((__packed__)) {
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;
typedef union __attribute__((__packed__)) {
struct s {
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;
};
uint8_t b[sizeof(struct s)];
} ntpMsg_t;
typedef struct {
@ -89,10 +92,10 @@ void networkSntpEngine(void *handle) {
uint8_t sockState = getSn_SR(SNTP_SOCK);
if (sockState == SOCK_UDP) {
memset(&(localHandle->ntpMsg), 0, sizeof(localHandle->ntpMsg));
localHandle->ntpMsg.li_vn_mode = SEND_LI_VN_MODE;
localHandle->ntpMsg.xmt_l = 1;
sendto(SNTP_SOCK, (uint8_t*)(&(localHandle->ntpMsg)),
sizeof(localHandle->ntpMsg), localHandle->ntpAddr, NTP_PORT);
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);
@ -118,8 +121,8 @@ void networkSntpEngine(void *handle) {
uint8_t srcAddr[4];
uint16_t srcPort;
recvfrom(SNTP_SOCK, (uint8_t*)(&(localHandle->ntpMsg)),
sizeof(localHandle->ntpMsg), srcAddr, &srcPort);
recvfrom(SNTP_SOCK, &(localHandle->ntpMsg.b),
sizeof(localHandle->ntpMsg.b), srcAddr, &srcPort);
/*
uint8_t buf[90];
@ -148,7 +151,7 @@ void networkSntpEngine(void *handle) {
srcAddr[0], srcAddr[1], srcAddr[2], srcAddr[3],
srcPort);
coloredMsg(LOG_BLUE, "nes, received in the %d. cycles", localHandle->retryCount);
localHandle->seconds = ((uint64_t)localHandle->ntpMsg.xmt_h) - UNIX_NTP_EPOCH_DIFF;
localHandle->seconds = ((uint64_t)localHandle->ntpMsg.s.xmt_h) - UNIX_NTP_EPOCH_DIFF;
coloredMsg(LOG_BLUE, "nes, seconds: %llu", localHandle->seconds);
localHandle->sntpState = SNTP_STATE_DONE;
} else {