ntp_demo.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #define QL_NTP_LOG_LEVEL QL_LOG_LEVEL_INFO
  24. #define QL_NTP_LOG(msg, ...) QL_LOG(QL_NTP_LOG_LEVEL, "ql_NTP_DEMO", msg, ##__VA_ARGS__)
  25. #define QL_NTP_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_NTP_DEMO", msg, ##__VA_ARGS__)
  26. static ql_task_t ntp_task = NULL;
  27. static ntp_client_id ntp_cli_id = 0;
  28. static ql_sem_t ntp_semp;
  29. static void ntp_result_cb(ntp_client_id cli_id, int result, struct tm *sync_time, void *arg)
  30. {
  31. ql_rtc_time_t time;
  32. if(ntp_cli_id != cli_id)
  33. return;
  34. if(result == QL_NTP_SUCCESS){
  35. char time_str[256] = {0};
  36. snprintf(time_str, 256, "%04d/%02d/%02d,%02d:%02d:%02d",sync_time->tm_year + 1900, sync_time->tm_mon + 1, sync_time->tm_mday,
  37. sync_time->tm_hour, sync_time->tm_min, sync_time->tm_sec);
  38. QL_NTP_LOG("ntp sync time:%s", time_str);
  39. time.tm_year = sync_time->tm_year+ 1900;
  40. time.tm_mon = sync_time->tm_mon + 1;
  41. time.tm_mday = sync_time->tm_mday;
  42. time.tm_wday = sync_time->tm_wday;
  43. time.tm_hour = sync_time->tm_hour;
  44. time.tm_min = sync_time->tm_min;
  45. time.tm_sec = sync_time->tm_sec;
  46. if(ql_rtc_set_time(&time) != QL_RTC_SUCCESS)
  47. {
  48. QL_NTP_LOG("ntp set RTC time failed");
  49. }
  50. }else{
  51. QL_NTP_LOG("ntp sync failed :%d", result);
  52. }
  53. ql_rtos_semaphore_release(ntp_semp);
  54. }
  55. static void ntp_app_thread(void * arg)
  56. {
  57. int ret = 0;
  58. int i = 0, run_num = 1;
  59. int profile_idx = 1;
  60. ql_data_call_info_s info;
  61. char ip4_addr_str[16] = {0};
  62. uint8_t nSim = 0;
  63. ql_rtos_task_sleep_s(10);
  64. QL_NTP_LOG("==========ntp demo start ==========");
  65. QL_NTP_LOG("wait for network register done");
  66. ql_rtos_semaphore_create(&ntp_semp, 0);
  67. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10){
  68. i++;
  69. ql_rtos_task_sleep_s(1);
  70. }
  71. if(ret == 0){
  72. i = 0;
  73. QL_NTP_LOG("====network registered!!!!====");
  74. }else{
  75. QL_NTP_LOG("====network register failure!!!!!====");
  76. goto exit;
  77. }
  78. ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  79. QL_NTP_LOG("===start data call====");
  80. ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  81. QL_NTP_LOG("===data call result:%d", ret);
  82. if(ret != 0){
  83. QL_NTP_LOG("====data call failure!!!!=====");
  84. }
  85. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  86. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  87. if(ret != 0){
  88. QL_NTP_LOG("ql_get_data_call_info ret: %d", ret);
  89. ql_stop_data_call(nSim, profile_idx);
  90. goto exit;
  91. }
  92. QL_NTP_LOG("info->profile_idx: %d", info.profile_idx);
  93. QL_NTP_LOG("info->ip_version: %d", info.ip_version);
  94. QL_NTP_LOG("info->v4.state: %d", info.v4.state);
  95. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  96. QL_NTP_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  97. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  98. QL_NTP_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  99. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  100. QL_NTP_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  101. while(run_num <= 100){
  102. ql_ntp_sync_option sync_option;
  103. int error_num = 0;
  104. QL_NTP_LOG("==============ntp_test[%d]================\n",run_num);
  105. memset(&sync_option, 0x00, sizeof(ql_ntp_sync_option));
  106. sync_option.pdp_cid = profile_idx;
  107. sync_option.sim_id = 0;
  108. sync_option.retry_cnt = 3;
  109. sync_option.retry_interval_tm = 60;
  110. ntp_cli_id = ql_ntp_sync("ntp.aliyun.com", &sync_option, ntp_result_cb, NULL, &error_num);
  111. if(ntp_cli_id != 0){
  112. ql_rtos_semaphore_wait(ntp_semp, QL_WAIT_FOREVER);
  113. }else{
  114. QL_NTP_LOG("ntp failed");
  115. }
  116. QL_NTP_LOG("==============ntp_test_end[%d]================\n",run_num);
  117. run_num++;
  118. ql_rtos_task_sleep_s(1);
  119. }
  120. exit:
  121. ql_rtos_semaphore_delete(ntp_semp);
  122. ql_rtos_task_delete(ntp_task);
  123. return;
  124. }
  125. int ql_ntp_app_init(void)
  126. {
  127. QlOSStatus err = QL_OSI_SUCCESS;
  128. err = ql_rtos_task_create(&ntp_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QntpApp", ntp_app_thread, NULL, 5);
  129. if(err != QL_OSI_SUCCESS)
  130. {
  131. QL_NTP_LOG("ntp_app init failed");
  132. }
  133. return err;
  134. }