wifi_scan_demo.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*================================================================
  2. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  3. Quectel Wireless Solutions 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. /*===========================================================================
  13. * include files
  14. ===========================================================================*/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "ql_log.h"
  19. #include "ql_api_osi.h"
  20. #include "ql_wifi_scan.h"
  21. #include "wifi_scan_demo.h"
  22. /*===========================================================================
  23. *Definition
  24. ===========================================================================*/
  25. #define QL_WIFISACN_DEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  26. #define QL_WIFISACN_DEMO_LOG(msg, ...) QL_LOG(QL_WIFISACN_DEMO_LOG_LEVEL, "wifiscan_demo", msg, ##__VA_ARGS__)
  27. #define QL_WIFISACN_DEMO_LOG_PUSH(msg, ...) QL_LOG_PUSH("wifiscan_demo", msg, ##__VA_ARGS__)
  28. #define QL_WIFISACN_TASK_STACK_SIZE 4096
  29. #define QL_WIFISACN_TASK_PRIO APP_PRIORITY_NORMAL
  30. #define QL_WIFISACN_TASK_EVENT_CNT 10
  31. /*===========================================================================
  32. * Variate
  33. ===========================================================================*/
  34. ql_task_t ql_wifiscan_app_task = NULL;
  35. /*===========================================================================
  36. * Struct
  37. ===========================================================================*/
  38. /*===========================================================================
  39. * Functions
  40. ===========================================================================*/
  41. void ql_wifiscan_ap_info_output(uint16_t ap_cnt, ql_wifi_ap_info_s *p_ap_infos)
  42. {
  43. if (NULL == p_ap_infos)
  44. {
  45. return;
  46. }
  47. for(uint16_t n=0; n<ap_cnt; n++)
  48. {
  49. QL_WIFISACN_DEMO_LOG("ap: mac=%x%lx, rssi=%i dBm, channel: %u, ssid:%s, ssid len=%d",
  50. p_ap_infos->bssid_high, p_ap_infos->bssid_low, p_ap_infos->rssival, p_ap_infos->channel,\
  51. p_ap_infos->ssid, p_ap_infos->ssidlen);
  52. p_ap_infos++;
  53. }
  54. }
  55. //all memory of msg_buf and it's sub item will be released by core after the call of callback.
  56. void ql_wifiscan_app_callback(ql_wifiscan_ind_msg_s *msg_buf)
  57. {
  58. if (NULL == msg_buf)
  59. {
  60. ql_wifiscan_close();
  61. return;
  62. }
  63. QL_WIFISACN_DEMO_LOG("id=0x%X, err=%d, buf=%p", msg_buf->msg_id, msg_buf->msg_err_code, msg_buf->msg_data);
  64. if (QUEC_WIFISCAN_EVENT_DO == msg_buf->msg_id)
  65. {
  66. if ((QL_WIFISCAN_SUCCESS == msg_buf->msg_err_code) && (NULL != msg_buf->msg_data))
  67. {
  68. ql_wifiscan_result_s *scan_result = msg_buf->msg_data;
  69. QL_WIFISACN_DEMO_LOG("ap_cnt=%d", scan_result->ap_cnt);
  70. ql_wifiscan_ap_info_output(scan_result->ap_cnt, scan_result->ap_infos);
  71. }
  72. ql_event_t scan_event = {0};
  73. scan_event.id = QUEC_WIFISCAN_EVENT_ASYNC_IND;
  74. ql_rtos_event_send(ql_wifiscan_app_task, &scan_event);
  75. }
  76. }
  77. //to start a async scan
  78. void ql_wifiscan_async_start(void)
  79. {
  80. #if 1
  81. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_option_set(QL_WIFI_SCAN_DEFAULT_AP_CNT,
  82. QL_WIFI_SCAN_DEFAULT_ROUND,
  83. QL_WIFISCAN_CHANNEL_ALL,
  84. QL_WIFI_SCAN_DEFAULT_TIME)
  85. )
  86. #else
  87. ql_wifiscan_opt_param_s param = {
  88. .expect_ap_cnt = QL_WIFI_SCAN_DEFAULT_AP_CNT,
  89. .round = QL_WIFI_SCAN_DEFAULT_ROUND,
  90. .scan_channel = QL_WIFISCAN_CHANNEL_ALL,
  91. .channel_time = QL_WIFI_SCAN_DEFAULT_TIME,
  92. .prio = QL_DATA_PRIORITY,
  93. .ssid_format = QL_WIFISCAN_SSID_FM_UTF8,
  94. .retry_period = QL_WIFI_SCAN_DEFAULT_RETRY_TIME
  95. };
  96. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_option_set_ex(&param))
  97. #endif
  98. {
  99. QL_WIFISACN_DEMO_LOG("option set err");
  100. return;
  101. }
  102. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_register_cb(ql_wifiscan_app_callback))
  103. {
  104. QL_WIFISACN_DEMO_LOG("register cb err");
  105. return;
  106. }
  107. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_open())
  108. {
  109. QL_WIFISACN_DEMO_LOG("device open err");
  110. return;
  111. }
  112. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_async())
  113. {
  114. ql_wifiscan_close();
  115. QL_WIFISACN_DEMO_LOG("to do a async scan err");
  116. return;
  117. }
  118. }
  119. //complete flow to do one time scan: option->open->scan->close->output
  120. void ql_wifiscan_synchro_complete_flow(void)
  121. {
  122. #if 1
  123. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_option_set(QL_WIFI_SCAN_DEFAULT_AP_CNT,
  124. QL_WIFI_SCAN_DEFAULT_ROUND,
  125. QL_WIFISCAN_CHANNEL_ALL,
  126. QL_WIFI_SCAN_DEFAULT_TIME)
  127. )
  128. #else
  129. ql_wifiscan_opt_param_s param = {
  130. .expect_ap_cnt = QL_WIFI_SCAN_DEFAULT_AP_CNT,
  131. .round = QL_WIFI_SCAN_DEFAULT_ROUND,
  132. .scan_channel = QL_WIFISCAN_CHANNEL_ALL,
  133. .channel_time = QL_WIFI_SCAN_DEFAULT_TIME,
  134. .prio = QL_DATA_PRIORITY,
  135. .ssid_format = QL_WIFISCAN_SSID_FM_UTF8,
  136. .retry_period = QL_WIFI_SCAN_DEFAULT_RETRY_TIME
  137. };
  138. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_option_set_ex(&param))
  139. #endif
  140. {
  141. QL_WIFISACN_DEMO_LOG("option set err");
  142. return;
  143. }
  144. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_register_cb(ql_wifiscan_app_callback))
  145. {
  146. QL_WIFISACN_DEMO_LOG("register cb err");
  147. return;
  148. }
  149. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_open())
  150. {
  151. QL_WIFISACN_DEMO_LOG("device open err");
  152. return;
  153. }
  154. uint16_t ap_cnt = 0;
  155. ql_wifi_ap_info_s *ap_infos = (ql_wifi_ap_info_s *)calloc(1, QL_WIFI_SCAN_DEFAULT_AP_CNT * sizeof(ql_wifi_ap_info_s));
  156. if (NULL == ap_infos)
  157. {
  158. QL_WIFISACN_DEMO_LOG("memory calloc err");
  159. }
  160. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_do(&ap_cnt, ap_infos))
  161. {
  162. free(ap_infos);
  163. ql_wifiscan_close();
  164. QL_WIFISACN_DEMO_LOG("to do a scan err");
  165. return;
  166. }
  167. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_close())
  168. {
  169. free(ap_infos);
  170. QL_WIFISACN_DEMO_LOG("close device err");
  171. return;
  172. }
  173. QL_WIFISACN_DEMO_LOG("ap_cnt=%d", ap_cnt);
  174. ql_wifiscan_ap_info_output(ap_cnt, ap_infos);
  175. free(ap_infos);
  176. }
  177. static void ql_wifiscan_app_thread(void *param)
  178. {
  179. ql_rtos_task_sleep_ms(15000);// sleep some time, then to do demo
  180. ql_wifiscan_synchro_complete_flow();
  181. ql_rtos_task_sleep_ms(5000);
  182. ql_wifiscan_async_start();//start a async scan, and it should trigger one event sent at callback
  183. while(1)
  184. {
  185. ql_event_t event = {0};
  186. if(ql_event_try_wait(&event) != 0)
  187. {
  188. continue;
  189. }
  190. if(event.id == QUEC_WIFISCAN_EVENT_ASYNC_IND)
  191. {
  192. ql_wifiscan_close();//close the wifiscan device fot the async scan
  193. ql_rtos_task_sleep_ms(15000);
  194. // next cycle demo
  195. ql_wifiscan_synchro_complete_flow();
  196. ql_rtos_task_sleep_ms(5000);
  197. ql_wifiscan_async_start();
  198. }
  199. }
  200. }
  201. void ql_wifiscan_app_init(void)
  202. {
  203. QlOSStatus err = QL_OSI_SUCCESS;
  204. err = ql_rtos_task_create(&ql_wifiscan_app_task, QL_WIFISACN_TASK_STACK_SIZE, QL_WIFISACN_TASK_PRIO, "wifiscan DEMO", ql_wifiscan_app_thread, NULL, QL_WIFISACN_TASK_EVENT_CNT);
  205. if(err != QL_OSI_SUCCESS)
  206. {
  207. QL_WIFISACN_DEMO_LOG("demo task created failed");
  208. }
  209. }