struct/union
This commit is contained in:
@ -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)
|
const uint8_t SEND_LI_VN_MODE = 0xe3; // LI: unknown (3), VN: 4, Mode: Client (3)
|
||||||
typedef struct __attribute__((__packed__)) {
|
typedef union __attribute__((__packed__)) {
|
||||||
uint8_t li_vn_mode;
|
struct s {
|
||||||
uint8_t stratum;
|
uint8_t li_vn_mode;
|
||||||
uint8_t poll;
|
uint8_t stratum;
|
||||||
uint8_t precision;
|
uint8_t poll;
|
||||||
uint32_t rootdelay;
|
uint8_t precision;
|
||||||
uint32_t rootdisp;
|
uint32_t rootdelay;
|
||||||
uint32_t refid;
|
uint32_t rootdisp;
|
||||||
//uint64_t reftime;
|
uint32_t refid;
|
||||||
uint32_t reftime_h;
|
//uint64_t reftime;
|
||||||
uint32_t reftime_l;
|
uint32_t reftime_h;
|
||||||
//uint64_t org;
|
uint32_t reftime_l;
|
||||||
uint32_t org_h;
|
//uint64_t org;
|
||||||
uint32_t org_l;
|
uint32_t org_h;
|
||||||
//uint64_t rec;
|
uint32_t org_l;
|
||||||
uint32_t rec_h;
|
//uint64_t rec;
|
||||||
uint32_t rec_l;
|
uint32_t rec_h;
|
||||||
//uint64_t xmt;
|
uint32_t rec_l;
|
||||||
uint32_t xmt_h;
|
//uint64_t xmt;
|
||||||
uint32_t xmt_l;
|
uint32_t xmt_h;
|
||||||
|
uint32_t xmt_l;
|
||||||
|
};
|
||||||
|
uint8_t b[sizeof(struct s)];
|
||||||
} ntpMsg_t;
|
} ntpMsg_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -89,10 +92,10 @@ void networkSntpEngine(void *handle) {
|
|||||||
uint8_t sockState = getSn_SR(SNTP_SOCK);
|
uint8_t sockState = getSn_SR(SNTP_SOCK);
|
||||||
if (sockState == SOCK_UDP) {
|
if (sockState == SOCK_UDP) {
|
||||||
memset(&(localHandle->ntpMsg), 0, sizeof(localHandle->ntpMsg));
|
memset(&(localHandle->ntpMsg), 0, sizeof(localHandle->ntpMsg));
|
||||||
localHandle->ntpMsg.li_vn_mode = SEND_LI_VN_MODE;
|
localHandle->ntpMsg.s.li_vn_mode = SEND_LI_VN_MODE;
|
||||||
localHandle->ntpMsg.xmt_l = 1;
|
localHandle->ntpMsg.s.xmt_l = 1;
|
||||||
sendto(SNTP_SOCK, (uint8_t*)(&(localHandle->ntpMsg)),
|
sendto(SNTP_SOCK, &(localHandle->ntpMsg.b),
|
||||||
sizeof(localHandle->ntpMsg), localHandle->ntpAddr, NTP_PORT);
|
sizeof(localHandle->ntpMsg.b), localHandle->ntpAddr, NTP_PORT);
|
||||||
coloredMsg(LOG_BLUE, "nes, sent");
|
coloredMsg(LOG_BLUE, "nes, sent");
|
||||||
localHandle->sntpState = SNTP_STATE_RECV;
|
localHandle->sntpState = SNTP_STATE_RECV;
|
||||||
schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
|
schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
|
||||||
@ -118,8 +121,8 @@ void networkSntpEngine(void *handle) {
|
|||||||
uint8_t srcAddr[4];
|
uint8_t srcAddr[4];
|
||||||
uint16_t srcPort;
|
uint16_t srcPort;
|
||||||
|
|
||||||
recvfrom(SNTP_SOCK, (uint8_t*)(&(localHandle->ntpMsg)),
|
recvfrom(SNTP_SOCK, &(localHandle->ntpMsg.b),
|
||||||
sizeof(localHandle->ntpMsg), srcAddr, &srcPort);
|
sizeof(localHandle->ntpMsg.b), srcAddr, &srcPort);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
uint8_t buf[90];
|
uint8_t buf[90];
|
||||||
@ -148,7 +151,7 @@ void networkSntpEngine(void *handle) {
|
|||||||
srcAddr[0], srcAddr[1], srcAddr[2], srcAddr[3],
|
srcAddr[0], srcAddr[1], srcAddr[2], srcAddr[3],
|
||||||
srcPort);
|
srcPort);
|
||||||
coloredMsg(LOG_BLUE, "nes, received in the %d. cycles", localHandle->retryCount);
|
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);
|
coloredMsg(LOG_BLUE, "nes, seconds: %llu", localHandle->seconds);
|
||||||
localHandle->sntpState = SNTP_STATE_DONE;
|
localHandle->sntpState = SNTP_STATE_DONE;
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user