Pārlūkot izejas kodu

Merge pull request #76 from mysterywolf/master

[ntp] 优化代码 增加注释
xiangxistu 4 gadi atpakaļ
vecāks
revīzija
0e95f5786e
2 mainītis faili ar 33 papildinājumiem un 33 dzēšanām
  1. 32 32
      ntp/ntp.c
  2. 1 1
      ntp/ntp.h

+ 32 - 32
ntp/ntp.c

@@ -61,62 +61,62 @@ extern int closesocket(int s);
 #if RT_VER_NUM <= 0x40003
 #ifndef NETUTILS_NTP_TIMEZONE
 #define NETUTILS_NTP_TIMEZONE                   8
-#endif
-#endif
+#endif /* NETUTILS_NTP_TIMEZONE */
+#endif /* RT_VER_NUM <= 0x40003 */
 
 #ifdef NETUTILS_NTP_HOSTNAME
 #define NTP_HOSTNAME1                  NETUTILS_NTP_HOSTNAME
 #else
 #define NTP_HOSTNAME1                  RT_NULL
-#endif
+#endif /* NETUTILS_NTP_HOSTNAME */
 
 #ifdef NETUTILS_NTP_HOSTNAME2
 #define NTP_HOSTNAME2                  NETUTILS_NTP_HOSTNAME2
 #else
 #define NTP_HOSTNAME2                  RT_NULL
-#endif
+#endif /* NETUTILS_NTP_HOSTNAME2 */
 
 #ifdef NETUTILS_NTP_HOSTNAME3
 #define NTP_HOSTNAME3                  NETUTILS_NTP_HOSTNAME3
 #else
 #define NTP_HOSTNAME3                  RT_NULL
-#endif
+#endif /* NETUTILS_NTP_HOSTNAME3 */
 
 #define NTP_TIMESTAMP_DELTA            2208988800ull
 
-#define LI(packet)   (uint8_t) ((packet.li_vn_mode & 0xC0) >> 6) /* (li   & 11 000 000) >> 6 */
-#define VN(packet)   (uint8_t) ((packet.li_vn_mode & 0x38) >> 3) /* (vn   & 00 111 000) >> 3 */
-#define MODE(packet) (uint8_t) ((packet.li_vn_mode & 0x07) >> 0) /* (mode & 00 000 111) >> 0 */
+#define LI(packet)   (rt_uint8_t) ((packet.li_vn_mode & 0xC0) >> 6) /* (li   & 11 000 000) >> 6 */
+#define VN(packet)   (rt_uint8_t) ((packet.li_vn_mode & 0x38) >> 3) /* (vn   & 00 111 000) >> 3 */
+#define MODE(packet) (rt_uint8_t) ((packet.li_vn_mode & 0x07) >> 0) /* (mode & 00 000 111) >> 0 */
 
 /* Structure that defines the 48 byte NTP packet protocol */
 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 */
+    rt_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 */
+    rt_uint8_t stratum;         /* Eight bits. Stratum level of the local clock */
+    rt_uint8_t poll;            /* Eight bits. Maximum interval between successive messages */
+    rt_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. Max error aloud from primary clock source */
-    uint32_t refId;          /* 32 bits. Reference clock identifier */
+    rt_uint32_t rootDelay;      /* 32 bits. Total round trip delay time */
+    rt_uint32_t rootDispersion; /* 32 bits. Max error aloud from primary clock source */
+    rt_uint32_t refId;          /* 32 bits. Reference clock identifier */
 
-    uint32_t refTm_s;        /* 32 bits. Reference time-stamp seconds */
-    uint32_t refTm_f;        /* 32 bits. Reference time-stamp fraction of a second */
+    rt_uint32_t refTm_s;        /* 32 bits. Reference time-stamp seconds */
+    rt_uint32_t refTm_f;        /* 32 bits. Reference time-stamp fraction of a second */
 
-    uint32_t origTm_s;       /* 32 bits. Originate time-stamp seconds */
-    uint32_t origTm_f;       /* 32 bits. Originate time-stamp fraction of a second */
+    rt_uint32_t origTm_s;       /* 32 bits. Originate time-stamp seconds */
+    rt_uint32_t origTm_f;       /* 32 bits. Originate time-stamp fraction of a second */
 
-    uint32_t rxTm_s;         /* 32 bits. Received time-stamp seconds */
-    uint32_t rxTm_f;         /* 32 bits. Received time-stamp fraction of a second */
+    rt_uint32_t rxTm_s;         /* 32 bits. Received time-stamp seconds */
+    rt_uint32_t rxTm_f;         /* 32 bits. Received time-stamp fraction of a second */
 
-    uint32_t txTm_s;         /* 32 bits and the most important field the client cares about. Transmit time-stamp seconds */
-    uint32_t txTm_f;         /* 32 bits. Transmit time-stamp fraction of a second */
+    rt_uint32_t txTm_s;         /* 32 bits and the most important field the client cares about. Transmit time-stamp seconds */
+    rt_uint32_t txTm_f;         /* 32 bits. Transmit time-stamp fraction of a second */
 
-} ntp_packet;                /* Total: 384 bits or 48 bytes */
+} ntp_packet;                   /* Total: 384 bits or 48 bytes */
 
 static ntp_packet packet = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
@@ -196,9 +196,9 @@ time_t ntp_get_time(const char *host_name)
         const char month[][NTP_INTERNET_MONTH_LEN] =
             {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
         char date[NTP_INTERNET_DATE_LEN] = {0};
-        uint8_t send_data[NTP_INTERNET_BUFF_LEN] = {0};
-        uint8_t index, moth_num = 0;
-        uint16_t check = 0;
+        rt_uint8_t send_data[NTP_INTERNET_BUFF_LEN] = {0};
+        rt_uint8_t index, moth_num = 0;
+        rt_uint16_t check = 0;
 
         /* get build moth value*/
         rt_snprintf(date, NTP_INTERNET_DATE_LEN, "%s", __DATE__);
@@ -219,7 +219,7 @@ time_t ntp_get_time(const char *host_name)
 #if defined(RT_USING_LWIP) && !defined(RT_USING_NETDEV)
             #define netdev netif
             #define netdev_default netif_default
-#endif
+#endif /* defined(RT_USING_LWIP) && !defined(RT_USING_NETDEV) */
             extern struct netdev *netdev_default;
             struct netdev *dev = netdev_default;
 
@@ -247,7 +247,7 @@ time_t ntp_get_time(const char *host_name)
 
         rt_memcpy(((char *)&packet + 4), send_data, NTP_INTERNET_BUFF_LEN);
     }
-#endif /* RT_USING_NETDEV || RT_USING_LWIP */
+#endif /* defined(RT_USING_NETDEV) || defined(RT_USING_LWIP) */
 
 
     /* Create a UDP socket. */

+ 1 - 1
ntp/ntp.h

@@ -27,7 +27,7 @@
 #ifndef __NTP_H__
 #define __NTP_H__
 
-#include <time.h>
+#include <sys/time.h>
 
 #ifdef __cplusplus
 extern "C" {