ping_demo.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "ql_api_osi.h"
  17. #include "ql_api_nw.h"
  18. #include "ql_log.h"
  19. #include "ql_api_datacall.h"
  20. #include "ql_ping_app.h"
  21. #define QL_PING_LOG_LEVEL QL_LOG_LEVEL_INFO
  22. #define QL_PING_LOG(msg, ...) QL_LOG(QL_PING_LOG_LEVEL, "ql_PING_DEMO", msg, ##__VA_ARGS__)
  23. #define QL_PING_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_PING_DEMO", msg, ##__VA_ARGS__)
  24. static ql_task_t ping_task = NULL;
  25. static ping_session_id ping_sess_id = 0;
  26. static ql_sem_t ping_semp;
  27. static void ping_event_cb(ping_session_id session_id, uint16_t event_id, int evt_code, void *evt_param)
  28. {
  29. if(session_id != ping_sess_id)
  30. return;
  31. switch(event_id){
  32. case QL_PING_STATS:{
  33. if(evt_code == QL_PING_OK){
  34. ql_ping_stats_type *stats = (ql_ping_stats_type *)evt_param;
  35. QL_PING_LOG("from \"%s\" 's reply: bystes=%d, rtt=%d, ttl=%d", stats->resolved_ip_addr, stats->ping_size, stats->ping_rtt, stats->ping_ttl);
  36. }
  37. }
  38. break;
  39. case QL_PING_SUMMARY:{
  40. if(evt_code == QL_PING_OK){
  41. ql_ping_summary_type *summary = (ql_ping_summary_type *)evt_param;
  42. QL_PING_LOG("statistics info: send=%dpackets, recv=%dpackets, lost=%dpackets, max_rtt=%d,min_rtt=%d,avg_rtt=%d",
  43. summary->num_pkts_sent, summary->num_pkts_recvd, summary->num_pkts_lost,
  44. summary->max_rtt, summary->min_rtt,summary->avg_rtt);
  45. }else{
  46. QL_PING_LOG("ping timeout!!!!!");
  47. }
  48. ping_sess_id = 0;
  49. ql_rtos_semaphore_release(ping_semp);
  50. }
  51. break;
  52. default:
  53. break;
  54. }
  55. }
  56. static void ping_app_thread(void * arg)
  57. {
  58. int ret = 0;
  59. int i = 0, run_num = 1;
  60. int profile_idx = 1;
  61. ql_data_call_info_s info;
  62. char ip4_addr_str[16] = {0};
  63. uint8_t nSim = 0;
  64. ql_rtos_task_sleep_s(10);
  65. QL_PING_LOG("==========ping demo start ==========");
  66. QL_PING_LOG("wait for network register done");
  67. ql_rtos_semaphore_create(&ping_semp, 0);
  68. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10){
  69. i++;
  70. ql_rtos_task_sleep_s(1);
  71. }
  72. if(ret == 0){
  73. i = 0;
  74. QL_PING_LOG("====network registered!!!!====");
  75. }else{
  76. QL_PING_LOG("====network register failure!!!!!====");
  77. goto exit;
  78. }
  79. ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  80. QL_PING_LOG("===start data call====");
  81. ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  82. QL_PING_LOG("===data call result:%d", ret);
  83. if(ret != 0){
  84. QL_PING_LOG("====data call failure!!!!=====");
  85. }
  86. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  87. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  88. if(ret != 0){
  89. QL_PING_LOG("ql_get_data_call_info ret: %d", ret);
  90. ql_stop_data_call(nSim, profile_idx);
  91. goto exit;
  92. }
  93. QL_PING_LOG("info->profile_idx: %d", info.profile_idx);
  94. QL_PING_LOG("info->ip_version: %d", info.ip_version);
  95. QL_PING_LOG("info->v4.state: %d", info.v4.state);
  96. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  97. QL_PING_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  98. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  99. QL_PING_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  100. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  101. QL_PING_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  102. while(run_num < 100){
  103. int case_id = run_num%5;
  104. ql_ping_config_type ping_option;
  105. memset(&ping_option, 0x00, sizeof(ql_ping_config_type));
  106. QL_PING_LOG("==============ping_test[%d]================\n",run_num+1);
  107. if(case_id == 1){
  108. ping_option.num_data_bytes = 56;
  109. ping_option.num_pings = 10;
  110. ping_option.ping_response_time_out = 10;
  111. ping_option.ttl = 255;
  112. ping_sess_id = ql_ping_start(profile_idx, 0, "www.baidu.com", &ping_option, ping_event_cb);
  113. }else if(case_id == 2){
  114. ping_option.num_data_bytes = 48;
  115. ping_option.num_pings = 15;
  116. ping_option.ping_response_time_out = 10;
  117. ping_option.ttl = 255;
  118. ping_sess_id = ql_ping_start(profile_idx, 0, "www.163.com", &ping_option, ping_event_cb);
  119. }else if(case_id == 3){
  120. ping_option.num_data_bytes = 56;
  121. ping_option.num_pings = 4;
  122. ping_option.ping_response_time_out = 10;
  123. ping_option.ttl = 255;
  124. ping_sess_id = ql_ping_start(profile_idx, 0, "www.hao123.com", &ping_option, ping_event_cb);
  125. }else if(case_id == 4){
  126. ping_option.num_data_bytes = 56;
  127. ping_option.num_pings = 8;
  128. ping_option.ping_response_time_out = 10;
  129. ping_option.ttl = 255;
  130. ping_sess_id = ql_ping_start(profile_idx, 0, "www.sina.com.cn", &ping_option, ping_event_cb);
  131. }else{
  132. ping_option.num_data_bytes = 48;
  133. ping_option.num_pings = 12;
  134. ping_option.ping_response_time_out = 10;
  135. ping_option.ttl = 255;
  136. ping_sess_id = ql_ping_start(profile_idx, 0, "www.jianshu.com", &ping_option, ping_event_cb);
  137. }
  138. if(ping_sess_id != 0){
  139. ql_rtos_semaphore_wait(ping_semp, QL_WAIT_FOREVER);
  140. }else{
  141. QL_PING_LOG("ping failed");
  142. }
  143. QL_PING_LOG("==============ping_test_end[%d]================\n",run_num+1);
  144. run_num++;
  145. ql_rtos_task_sleep_s(1);
  146. }
  147. exit:
  148. ql_rtos_semaphore_delete(ping_semp);
  149. ql_rtos_task_delete(ping_task);
  150. return;
  151. }
  152. int ql_ping_app_init(void)
  153. {
  154. QlOSStatus err = QL_OSI_SUCCESS;
  155. err = ql_rtos_task_create(&ping_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QpingApp", ping_app_thread, NULL, 5);
  156. if(err != QL_OSI_SUCCESS)
  157. {
  158. QL_PING_LOG("ping_app init failed");
  159. }
  160. return err;
  161. }