|
|
@@ -21,6 +21,7 @@
|
|
|
* 2018-02-10 armink the first version
|
|
|
* 2020-07-21 Chenxuan C++ support
|
|
|
* 2021-05-09 Meco Man remove timezone function
|
|
|
+ * 2021-07-22 Meco Man implement workqueue for NTP sync
|
|
|
*/
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
@@ -66,19 +67,19 @@ extern int closesocket(int s);
|
|
|
#ifdef NETUTILS_NTP_HOSTNAME
|
|
|
#define NTP_HOSTNAME1 NETUTILS_NTP_HOSTNAME
|
|
|
#else
|
|
|
-#define NTP_HOSTNAME1 NULL
|
|
|
+#define NTP_HOSTNAME1 RT_NULL
|
|
|
#endif
|
|
|
|
|
|
#ifdef NETUTILS_NTP_HOSTNAME2
|
|
|
#define NTP_HOSTNAME2 NETUTILS_NTP_HOSTNAME2
|
|
|
#else
|
|
|
-#define NTP_HOSTNAME2 NULL
|
|
|
+#define NTP_HOSTNAME2 RT_NULL
|
|
|
#endif
|
|
|
|
|
|
#ifdef NETUTILS_NTP_HOSTNAME3
|
|
|
#define NTP_HOSTNAME3 NETUTILS_NTP_HOSTNAME3
|
|
|
#else
|
|
|
-#define NTP_HOSTNAME3 NULL
|
|
|
+#define NTP_HOSTNAME3 RT_NULL
|
|
|
#endif
|
|
|
|
|
|
#define NTP_TIMESTAMP_DELTA 2208988800ull
|
|
|
@@ -127,7 +128,7 @@ static int sendto_ntp_server(int sockfd, const char *host_name, struct sockaddr_
|
|
|
int portno = 123;
|
|
|
|
|
|
server = gethostbyname(host_name);
|
|
|
- if (server == NULL)
|
|
|
+ if (server == RT_NULL)
|
|
|
{
|
|
|
LOG_D("No such host(%s)", host_name);
|
|
|
return -RT_ERROR;
|
|
|
@@ -152,7 +153,7 @@ static int sendto_ntp_server(int sockfd, const char *host_name, struct sockaddr_
|
|
|
/**
|
|
|
* Get the UTC time from NTP server
|
|
|
*
|
|
|
- * @param host_name NTP server host name, NULL: will using default host name
|
|
|
+ * @param host_name NTP server host name, RT_NULL: will using default host name
|
|
|
*
|
|
|
* @note this function is not reentrant
|
|
|
*
|
|
|
@@ -270,7 +271,7 @@ time_t ntp_get_time(const char *host_name)
|
|
|
/* use the static default NTP server */
|
|
|
for (i = 0; i < NTP_SERVER_NUM; i++)
|
|
|
{
|
|
|
- if (host_name_buf[i] == NULL || strlen(host_name_buf[i]) == 0)
|
|
|
+ if (host_name_buf[i] == RT_NULL || strlen(host_name_buf[i]) == 0)
|
|
|
continue;
|
|
|
|
|
|
if (sendto_ntp_server(sockfd, host_name_buf[i], &serv_addr[server_num]) >= 0)
|
|
|
@@ -336,7 +337,7 @@ __exit:
|
|
|
/**
|
|
|
* Get the local time from NTP server
|
|
|
*
|
|
|
- * @param host_name NTP server host name, NULL: will using default host name
|
|
|
+ * @param host_name NTP server host name, RT_NULL: will using default host name
|
|
|
*
|
|
|
* @return >0: success, current local time, offset timezone by NETUTILS_NTP_TIMEZONE
|
|
|
* =0: get failed
|
|
|
@@ -358,7 +359,7 @@ time_t ntp_get_local_time(const char *host_name)
|
|
|
/**
|
|
|
* Sync current local time to RTC by NTP
|
|
|
*
|
|
|
- * @param host_name NTP server host name, NULL: will using default host name
|
|
|
+ * @param host_name NTP server host name, RT_NULL: will using default host name
|
|
|
*
|
|
|
* @return >0: success
|
|
|
* =0: sync failed
|
|
|
@@ -394,9 +395,6 @@ time_t ntp_sync_to_rtc(const char *host_name)
|
|
|
|
|
|
#if RT_VER_NUM > 0x40003
|
|
|
#ifdef NTP_USING_AUTO_SYNC
|
|
|
-#ifndef NTP_AUTO_SYNC_THREAD_STACK_SIZE
|
|
|
-#define NTP_AUTO_SYNC_THREAD_STACK_SIZE (1500)
|
|
|
-#endif
|
|
|
/* NTP first sync delay time for network connect, unit: second */
|
|
|
#ifndef NTP_AUTO_SYNC_FIRST_DELAY
|
|
|
#define NTP_AUTO_SYNC_FIRST_DELAY (30)
|
|
|
@@ -406,6 +404,27 @@ time_t ntp_sync_to_rtc(const char *host_name)
|
|
|
#define NTP_AUTO_SYNC_PERIOD (1L*60L*60L)
|
|
|
#endif
|
|
|
|
|
|
+#ifdef RT_USING_SYSTEM_WORKQUEUE
|
|
|
+static struct rt_work ntp_sync_work;
|
|
|
+
|
|
|
+static void ntp_sync_work_func(struct rt_work *work, void *work_data)
|
|
|
+{
|
|
|
+ ntp_sync_to_rtc(RT_NULL);
|
|
|
+ rt_work_submit(&ntp_sync_work, rt_tick_from_millisecond(NTP_AUTO_SYNC_PERIOD * 1000));
|
|
|
+}
|
|
|
+
|
|
|
+static int ntp_auto_sync_init(void)
|
|
|
+{
|
|
|
+ rt_work_init(&ntp_sync_work, ntp_sync_work_func, RT_NULL);
|
|
|
+ rt_work_submit(&ntp_sync_work, rt_tick_from_millisecond(NTP_AUTO_SYNC_FIRST_DELAY * 1000));
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+#else
|
|
|
+#ifndef NTP_AUTO_SYNC_THREAD_STACK_SIZE
|
|
|
+#define NTP_AUTO_SYNC_THREAD_STACK_SIZE (1500)
|
|
|
+#endif
|
|
|
+
|
|
|
static void ntp_sync_thread_enrty(void *param)
|
|
|
{
|
|
|
/* first sync delay for network connect */
|
|
|
@@ -413,7 +432,7 @@ static void ntp_sync_thread_enrty(void *param)
|
|
|
|
|
|
while (1)
|
|
|
{
|
|
|
- ntp_sync_to_rtc(NULL);
|
|
|
+ ntp_sync_to_rtc(RT_NULL);
|
|
|
rt_thread_delay(NTP_AUTO_SYNC_PERIOD * RT_TICK_PER_SECOND);
|
|
|
}
|
|
|
}
|
|
|
@@ -443,15 +462,16 @@ static int ntp_auto_sync_init(void)
|
|
|
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
+#endif /* RT_USING_SYSTEM_WORKQUEUE */
|
|
|
INIT_COMPONENT_EXPORT(ntp_auto_sync_init);
|
|
|
-#endif /*NTP_USING_AUTO_SYNC*/
|
|
|
-#endif /*RT_VER_NUM > 0x40003*/
|
|
|
+#endif /* NTP_USING_AUTO_SYNC */
|
|
|
+#endif /* RT_VER_NUM > 0x40003 */
|
|
|
|
|
|
#ifdef FINSH_USING_MSH
|
|
|
#include <finsh.h>
|
|
|
static void cmd_ntp_sync(int argc, char **argv)
|
|
|
{
|
|
|
- char *host_name = NULL;
|
|
|
+ char *host_name = RT_NULL;
|
|
|
|
|
|
if (argc > 1)
|
|
|
{
|