ntp_demo.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <time.h>
  17. #include "ql_api_osi.h"
  18. #include "ql_api_nw.h"
  19. #include "ql_log.h"
  20. #include "ql_api_datacall.h"
  21. #include "ql_ntp_client.h"
  22. #include "ql_api_rtc.h"
  23. //#include "ntp_info_decode.h"
  24. #define QL_NTP_LOG_LEVEL QL_LOG_LEVEL_INFO
  25. #define QL_NTP_LOG(msg, ...) QL_LOG(QL_NTP_LOG_LEVEL, "ql_NTP_DEMO", msg, ##__VA_ARGS__)
  26. #define QL_NTP_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_NTP_DEMO", msg, ##__VA_ARGS__)
  27. static ql_task_t ntp_task = NULL;
  28. static ntp_client_id ntp_cli_id = 0;
  29. static ql_sem_t ntp_semp;
  30. static void ntp_result_cb(ntp_client_id cli_id, int result, struct tm *sync_time, void *arg)
  31. {
  32. ql_rtc_time_t time;
  33. if(ntp_cli_id != cli_id)
  34. return;
  35. if(result == QL_NTP_SUCCESS)
  36. {
  37. char time_str[256] = {0};
  38. snprintf(time_str, 256, "%04d/%02d/%02d,%02d:%02d:%02d",sync_time->tm_year + 1900, sync_time->tm_mon + 1, sync_time->tm_mday,
  39. sync_time->tm_hour, sync_time->tm_min, sync_time->tm_sec);
  40. QL_NTP_LOG("ntp sync time:%s", time_str);
  41. time.tm_year = sync_time->tm_year+ 1900;
  42. time.tm_mon = sync_time->tm_mon + 1;
  43. time.tm_mday = sync_time->tm_mday;
  44. time.tm_wday = sync_time->tm_wday;
  45. time.tm_hour = sync_time->tm_hour;
  46. time.tm_min = sync_time->tm_min;
  47. time.tm_sec = sync_time->tm_sec;
  48. if(ql_rtc_set_time(&time) != QL_RTC_SUCCESS)
  49. {
  50. QL_NTP_LOG("ntp set RTC time failed");
  51. }
  52. }
  53. else
  54. {
  55. QL_NTP_LOG("ntp sync failed :%d", result);
  56. }
  57. ql_rtos_semaphore_release(ntp_semp);
  58. }
  59. static void ntp_app_thread(void * arg)
  60. {
  61. int ret = 0;
  62. int i = 0, run_num = 1;
  63. int profile_idx = 1;
  64. ql_data_call_info_s info;
  65. char ip4_addr_str[16] = {0};
  66. uint8_t nSim = 0;
  67. ql_rtos_task_sleep_s(10);
  68. QL_NTP_LOG("==========ntp demo start ==========");
  69. QL_NTP_LOG("wait for network register done");
  70. ql_rtos_semaphore_create(&ntp_semp, 0);
  71. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  72. {
  73. i++;
  74. ql_rtos_task_sleep_s(1);
  75. }
  76. if(ret == 0){
  77. i = 0;
  78. QL_NTP_LOG("====network registered!!!!====");
  79. }else{
  80. QL_NTP_LOG("====network register failure!!!!!====");
  81. goto exit;
  82. }
  83. ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  84. QL_NTP_LOG("===start data call====");
  85. ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  86. QL_NTP_LOG("===data call result:%d", ret);
  87. if(ret != 0){
  88. QL_NTP_LOG("====data call failure!!!!=====");
  89. }
  90. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  91. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  92. if(ret != 0){
  93. QL_NTP_LOG("ql_get_data_call_info ret: %d", ret);
  94. ql_stop_data_call(nSim, profile_idx);
  95. goto exit;
  96. }
  97. QL_NTP_LOG("info->profile_idx: %d", info.profile_idx);
  98. QL_NTP_LOG("info->ip_version: %d", info.ip_version);
  99. QL_NTP_LOG("info->v4.state: %d", info.v4.state);
  100. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  101. QL_NTP_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  102. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  103. QL_NTP_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  104. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  105. QL_NTP_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  106. while(run_num <= 100){
  107. ql_ntp_sync_option sync_option;
  108. int error_num = 0;
  109. QL_NTP_LOG("==============ntp_test[%d]================\n",run_num);
  110. memset(&sync_option, 0x00, sizeof(ql_ntp_sync_option));
  111. sync_option.pdp_cid = profile_idx;
  112. sync_option.sim_id = 0;
  113. sync_option.retry_cnt = 3;
  114. sync_option.retry_interval_tm = 60;
  115. ntp_cli_id = ql_ntp_sync("ntp.aliyun.com", &sync_option, ntp_result_cb, NULL, &error_num);
  116. if(ntp_cli_id != 0)
  117. {
  118. ql_rtos_semaphore_wait(ntp_semp, QL_WAIT_FOREVER);
  119. }else
  120. {
  121. QL_NTP_LOG("ntp failed");
  122. }
  123. QL_NTP_LOG("==============ntp_test_end[%d]================\n",run_num);
  124. run_num++;
  125. ql_rtos_task_sleep_s(1);
  126. }
  127. exit:
  128. ql_rtos_semaphore_delete(ntp_semp);
  129. ql_rtos_task_delete(ntp_task);
  130. return;
  131. }
  132. int ql_ntp_app_init(void)
  133. {
  134. QlOSStatus err = QL_OSI_SUCCESS;
  135. err = ql_rtos_task_create(&ntp_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QntpApp", ntp_app_thread, NULL, 5);
  136. if(err != QL_OSI_SUCCESS)
  137. {
  138. QL_NTP_LOG("ntp_app init failed");
  139. }
  140. return err;
  141. }