本文将讲解NTP的代码实现和调试过程的一些记录。
首先,进行NTP报文结构体的构建。
//宏定义
#define NTP_TIMESTAMP_DELTA 2208988800ull //number of seconds between 1900 and 1970,1900-1970的时间差
#define SEC_TIME_ZONE + (8*60*60) //Beijing,GMT+8, 时区差
typedef struct
{
uint8_t li_vn_mode; // Eight bits. li, vn, and mode.
// li. Two bits. Leap indicator.
// vn. Three bits. Version number of the protocol.
// mode. Three bits. Client will pick mode 3 for client.
uint8_t stratum; // Eight bits. Stratum level of the local clock.
uint8_t poll; // Eight bits. Maximum interval between successive messages.
uint8_t precision; // Eight bits. Precision of the local clock.
uint32_t rootDelay; // 32 bits. Total round trip delay time.
uint32_t rootDispersion; // 32 bits. Ma