lbs_wifi_demo.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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_wifi_scan.h"
  21. #include "ql_api_datacall.h"
  22. #include "ql_lbs_client.h"
  23. #define QL_LBS_WIFI_LOG_LEVEL QL_LOG_LEVEL_INFO
  24. #define QL_LBS_WIFI_LOG(msg, ...) QL_LOG(QL_LBS_WIFI_LOG_LEVEL, "ql_LBS_WIFI_DEMO", msg, ##__VA_ARGS__)
  25. #define QL_LBS_WIFI_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_LBS_WIFI_DEMO", msg, ##__VA_ARGS__)
  26. typedef enum{
  27. QLBS_WIFI_EVENT_TIMER_START = 1001,
  28. QLBS_WIFI_EVENT_TIMER_STOP,
  29. QLBS_WIFI_EVENT_WIFISCAN_START,
  30. QLBS_WIFI_EVENT_WIFISCAN_STOP,
  31. QLBS_WIFI_EVENT_LBS_CFG,
  32. QLBS_WIFI_EVENT_LBS_START,
  33. QLBS_WIFI_EVENT_END,
  34. }qlbs_wifi_event_code_e;
  35. typedef enum{
  36. QLBS_WIFI_STATE_INIT = 0,
  37. }qlbs_wifi_state_code_e;
  38. typedef struct
  39. {
  40. lbs_client_hndl lbs_client;
  41. ql_task_t task;
  42. ql_mutex_t lock;
  43. ql_queue_t queue;
  44. ql_timer_t timer;
  45. int timeout;
  46. int pdp_cid;
  47. int req_timeout;
  48. char host[256];
  49. lbs_basic_info_t basic_info;
  50. lbs_auth_info_t auth_info;
  51. int wifi_num;
  52. lbs_wifi_mac_info_t *wifi_info;
  53. lbs_cell_info_t *cell_info;
  54. int loc_method;
  55. }qlbs_wifi_ctx_t;
  56. #define QLBS_WIFI_MAX_MSG_CNT 10
  57. static ql_task_t lbs_wifi_task = NULL;
  58. qlbs_wifi_ctx_t *lbs_wifi_client = NULL;
  59. /* wifi scan parameter */
  60. #define QLBS_WIFI_SCAN_AP_CNT QL_WIFI_SCAN_DEFAULT_AP_CNT
  61. #define QLBS_WIFI_SCAN_ROUND QL_WIFI_SCAN_DEFAULT_ROUND+1
  62. #define QLBS_WIFI_SCAN_CHANNEL QL_WIFISCAN_CHANNEL_ALL
  63. #define QLBS_WIFI_SCAN_TIME QL_WIFI_SCAN_DEFAULT_TIME
  64. #define QLBS_WIFI_SCAN_SERVER "www.queclocator.com"
  65. /*token application*/
  66. #define QLBS_WIFI_SCAN_TOKEN "1111111122222222" /*Need to apply for a valid token*/
  67. #define QLBS_WIFI_SCAN_USER_NAME "quectel"
  68. #define QLBS_WIFI_SCAN_USER_PWD "123456"
  69. #define QLBS_WIFI_SCAN_IMEI "861687000001091"
  70. static void ql_lbs_wifi_timeout_cb(void *param)
  71. {
  72. qlbs_wifi_ctx_t *client_ptr = (qlbs_wifi_ctx_t *)param;
  73. ql_event_t send_event = {0};
  74. QL_LBS_WIFI_LOG("enter");
  75. if(client_ptr == NULL || client_ptr->queue == NULL)
  76. return;
  77. send_event.id = QLBS_WIFI_EVENT_END;
  78. send_event.param1 = 0;
  79. ql_rtos_queue_release(client_ptr->queue, sizeof(ql_event_t), (uint8 *)&send_event, QL_WAIT_FOREVER);
  80. }
  81. void ql_lbs_wifi_timer_start(void *param)
  82. {
  83. qlbs_wifi_ctx_t *client_ptr = NULL;
  84. client_ptr = (qlbs_wifi_ctx_t *)param;
  85. QL_LBS_WIFI_LOG("enter");
  86. if(NULL == client_ptr)
  87. return;
  88. if(client_ptr->timer == NULL || ql_rtos_timer_is_running(client_ptr->timer) == false)
  89. {
  90. if (client_ptr->timer == NULL)
  91. {
  92. ql_rtos_timer_create(&client_ptr->timer, client_ptr->task, ql_lbs_wifi_timeout_cb, client_ptr);
  93. }
  94. ql_rtos_timer_start(client_ptr->timer, client_ptr->timeout * 1000, 0);
  95. }
  96. }
  97. void ql_lbs_wifi_timer_stop(void *param)
  98. {
  99. qlbs_wifi_ctx_t *client_ptr = NULL;
  100. client_ptr = (qlbs_wifi_ctx_t *)param;
  101. QL_LBS_WIFI_LOG("enter");
  102. if(NULL == client_ptr)
  103. return;
  104. if(client_ptr->timer != NULL)
  105. {
  106. if( ql_rtos_timer_is_running(client_ptr->timer) == true )
  107. ql_rtos_timer_stop(client_ptr->timer);
  108. ql_rtos_timer_delete(client_ptr->timer);
  109. client_ptr->timer = NULL;
  110. }
  111. }
  112. //all memory of msg_buf and it's sub item will be released by core after the call of callback.
  113. void ql_lbs_wifi_scan_app_callback(ql_wifiscan_ind_msg_s *msg_buf)
  114. {
  115. int i = 0;
  116. ql_event_t send_event = {0};
  117. qlbs_wifi_ctx_t *client_ptr = lbs_wifi_client;
  118. send_event.param1 = (uint32)client_ptr;
  119. if ((NULL == msg_buf)||(NULL == client_ptr))
  120. {
  121. ql_wifiscan_close();
  122. return;
  123. }
  124. QL_LBS_WIFI_LOG("id=0x%X, err=%d, buf=%p", msg_buf->msg_id, msg_buf->msg_err_code, msg_buf->msg_data);
  125. if (QUEC_WIFISCAN_EVENT_DO == msg_buf->msg_id)
  126. {
  127. if ((QL_WIFISCAN_SUCCESS == msg_buf->msg_err_code) && (NULL != msg_buf->msg_data))
  128. {
  129. ql_wifiscan_result_s *scan_result = msg_buf->msg_data;
  130. QL_LBS_WIFI_LOG("ap_cnt=%d", scan_result->ap_cnt);
  131. ql_rtos_mutex_lock(client_ptr->lock, 1000);
  132. client_ptr->wifi_num = scan_result->ap_cnt;
  133. if(client_ptr->wifi_info != NULL)
  134. free(client_ptr->wifi_info);
  135. client_ptr->wifi_info = (lbs_wifi_mac_info_t *)malloc(scan_result->ap_cnt * sizeof(lbs_wifi_mac_info_t));
  136. if(client_ptr->wifi_info == NULL)
  137. {
  138. send_event.id = QLBS_WIFI_EVENT_END;
  139. ql_rtos_queue_release(client_ptr->queue, sizeof(ql_event_t), (uint8 *)&send_event, QL_WAIT_FOREVER);
  140. QL_LBS_WIFI_LOG("malloc faild");
  141. return;
  142. }
  143. for(i = 0; i < scan_result->ap_cnt; i++)
  144. {
  145. sprintf(client_ptr->wifi_info[i].wifi_mac, "%02X:%02X:%02lX:%02lX:%02lX:%02lX",
  146. ((scan_result->ap_infos[i].bssid_high >> 8) & 0xFF),
  147. (scan_result->ap_infos[i].bssid_high & 0xFF),
  148. ((scan_result->ap_infos[i].bssid_low >> 24) & 0xFF),
  149. ((scan_result->ap_infos[i].bssid_low >> 16) & 0xFF),
  150. ((scan_result->ap_infos[i].bssid_low >> 8) & 0xFF),
  151. (scan_result->ap_infos[i].bssid_low & 0xFF));
  152. client_ptr->wifi_info[i].wifi_rssi = (int)scan_result->ap_infos[i].rssival;
  153. strncpy(client_ptr->wifi_info[i].wifi_ssid, "test", strlen("test")+1);
  154. }
  155. ql_rtos_mutex_unlock(client_ptr->lock);
  156. send_event.id = QLBS_WIFI_EVENT_LBS_START;
  157. }
  158. else
  159. {
  160. send_event.id = QLBS_WIFI_EVENT_END;
  161. }
  162. ql_rtos_queue_release(client_ptr->queue, sizeof(ql_event_t), (uint8 *)&send_event, QL_WAIT_FOREVER);
  163. }
  164. }
  165. void ql_lbs_wifi_scan_async_start(void *param)
  166. {
  167. qlbs_wifi_ctx_t *client_ptr = NULL;
  168. ql_event_t send_event = {0};
  169. ql_event_t *recv_event_ptr = (ql_event_t *)param;
  170. if(recv_event_ptr->param1 == 0)
  171. return;
  172. client_ptr = (qlbs_wifi_ctx_t *)recv_event_ptr->param1;
  173. send_event.param1 = (uint32)client_ptr;
  174. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_option_set(QLBS_WIFI_SCAN_AP_CNT,
  175. QLBS_WIFI_SCAN_ROUND,
  176. QLBS_WIFI_SCAN_CHANNEL,
  177. QLBS_WIFI_SCAN_TIME)
  178. )
  179. {
  180. QL_LBS_WIFI_LOG("option set err");
  181. goto exit;
  182. }
  183. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_register_cb(ql_lbs_wifi_scan_app_callback))
  184. {
  185. QL_LBS_WIFI_LOG("register cb err");
  186. goto exit;
  187. }
  188. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_open())
  189. {
  190. QL_LBS_WIFI_LOG("device open err");
  191. goto exit;
  192. }
  193. if (QL_WIFISCAN_SUCCESS != ql_wifiscan_async())
  194. {
  195. QL_LBS_WIFI_LOG("to do a async scan err");
  196. goto exit;
  197. }
  198. return;
  199. exit:
  200. send_event.id = QLBS_WIFI_EVENT_END;
  201. ql_rtos_queue_release(client_ptr->queue, sizeof(ql_event_t), (uint8 *)&send_event, QL_WAIT_FOREVER);
  202. }
  203. int ql_lbs_cell_info_get(void *client)
  204. {
  205. int ret = -1;
  206. uint8_t nSim = 0;
  207. ql_nw_cell_info_s *cell_info = NULL;
  208. ql_nw_seclection_info_s select_info;
  209. qlbs_wifi_ctx_t *client_ptr = (qlbs_wifi_ctx_t *)client;
  210. if(NULL == client_ptr){
  211. goto exit;
  212. }
  213. cell_info = (ql_nw_cell_info_s *)malloc(sizeof(ql_nw_cell_info_s));
  214. if(cell_info == NULL){
  215. goto exit;
  216. }
  217. if(client_ptr->cell_info == NULL){
  218. client_ptr->cell_info = (lbs_cell_info_t *)malloc(sizeof(lbs_cell_info_t));
  219. if(client_ptr->cell_info == NULL){
  220. goto exit;
  221. }
  222. }
  223. memset( cell_info, 0, sizeof(ql_nw_cell_info_s));
  224. ret = ql_nw_get_cell_info(nSim, cell_info);
  225. if(ret!= QL_NW_SUCCESS)
  226. {
  227. QL_LBS_WIFI_LOG("===============lbs get cell info fail===============\n");
  228. goto exit;
  229. }
  230. ret = ql_nw_get_selection(nSim, &select_info);
  231. if(ret != 0){
  232. QL_LBS_WIFI_LOG("ql_nw_get_selection ret: %d", ret);
  233. goto exit;
  234. }
  235. QL_LBS_WIFI_LOG("nw_act_type=%d",select_info.act);
  236. if(select_info.act==QL_NW_ACCESS_TECH_GSM)
  237. {
  238. client_ptr->cell_info->radio=1;
  239. client_ptr->cell_info->mcc = cell_info->gsm_info[0].mcc;
  240. client_ptr->cell_info->mnc = cell_info->gsm_info[0].mnc;
  241. client_ptr->cell_info->cell_id = cell_info->gsm_info[0].cid;
  242. client_ptr->cell_info->lac_id = cell_info->gsm_info[0].lac;
  243. client_ptr->cell_info->bsic = cell_info->gsm_info[0].bsic;
  244. client_ptr->cell_info->uarfcndl = cell_info->gsm_info[0].arfcn;
  245. client_ptr->cell_info->bcch = cell_info->gsm_info[0].arfcn;
  246. client_ptr->cell_info->signal=cell_info->gsm_info[0].rssi;
  247. }
  248. else if(select_info.act==QL_NW_ACCESS_TECH_E_UTRAN)
  249. {
  250. client_ptr->cell_info->radio=3;
  251. client_ptr->cell_info->mcc = cell_info->lte_info[0].mcc;
  252. client_ptr->cell_info->mnc = cell_info->lte_info[0].mnc;
  253. client_ptr->cell_info->cell_id = cell_info->lte_info[0].cid;
  254. client_ptr->cell_info->lac_id = cell_info->lte_info[0].tac;
  255. client_ptr->cell_info->tac = cell_info->lte_info[0].tac;
  256. client_ptr->cell_info->pci = cell_info->lte_info[0].pci;
  257. client_ptr->cell_info->earfcn = cell_info->lte_info[0].earfcn;
  258. client_ptr->cell_info->bcch = cell_info->lte_info[0].earfcn;
  259. client_ptr->cell_info->signal=cell_info->lte_info[0].rssi;
  260. }
  261. else
  262. {
  263. ret =-1;
  264. goto exit;
  265. }
  266. QL_LBS_WIFI_LOG("cell infoinfo: radio=%d,mcc=%d,mnc=%d,lac_id=%x,cell_id=%x,signal=%d,tac =%x,bcch=%d,bsic=%d,uarfcndl=%d,psc=%d,rsrq=%d,pci=%d,earfcn=%d",\
  267. client_ptr->cell_info->radio,client_ptr->cell_info->mcc,client_ptr->cell_info->mnc,client_ptr->cell_info->lac_id,client_ptr->cell_info->cell_id,client_ptr->cell_info->signal,client_ptr->cell_info->tac,\
  268. client_ptr->cell_info->bcch,client_ptr->cell_info->bsic,client_ptr->cell_info->uarfcndl,client_ptr->cell_info->psc,client_ptr->cell_info->rsrq,client_ptr->cell_info->pci,client_ptr->cell_info->earfcn);
  269. exit:
  270. if(NULL != cell_info) free(cell_info);
  271. return ret;
  272. }
  273. void ql_lbs_wifi_lbs_cfg_func(void *client, int method)
  274. {
  275. qlbs_wifi_ctx_t *client_ptr = NULL;
  276. client_ptr = (qlbs_wifi_ctx_t *)client;
  277. if(NULL == client_ptr) return;
  278. client_ptr->req_timeout = 60;
  279. snprintf(client_ptr->host, 256, "%s", QLBS_WIFI_SCAN_SERVER);
  280. client_ptr->basic_info.type = 0x01;
  281. client_ptr->basic_info.encrypt = 0x01;
  282. client_ptr->basic_info.key_index = 0x01;
  283. client_ptr->basic_info.pos_format = 0x01;
  284. client_ptr->basic_info.loc_method = method;
  285. snprintf(client_ptr->auth_info.user_name, 64, "%s", QLBS_WIFI_SCAN_USER_NAME);
  286. snprintf(client_ptr->auth_info.user_pwd, 64, "%s", QLBS_WIFI_SCAN_USER_PWD);
  287. snprintf(client_ptr->auth_info.token, 128, "%s", QLBS_WIFI_SCAN_TOKEN);
  288. snprintf(client_ptr->auth_info.imei, 64, "%s", QLBS_WIFI_SCAN_IMEI);
  289. client_ptr->auth_info.rand = 2346;
  290. }
  291. static void ql_lbs_wifi_result_cb(lbs_response_data_t *response_data)
  292. {
  293. int i = 0;
  294. ql_event_t send_event = {0};
  295. qlbs_wifi_ctx_t *client_ptr = NULL;
  296. QL_LBS_WIFI_LOG("enter");
  297. if (NULL == response_data || response_data->arg == NULL)
  298. {
  299. return;
  300. }
  301. client_ptr = (qlbs_wifi_ctx_t *)response_data->arg;
  302. if(client_ptr->lbs_client != response_data->hndl)
  303. {
  304. return;
  305. }
  306. QL_LBS_WIFI_LOG("lbs result: %08X", response_data->result);
  307. if(response_data->result == QL_LBS_OK && response_data->pos_num > 0)
  308. {
  309. for(i = 0; i < response_data->pos_num; i++)
  310. {
  311. QL_LBS_WIFI_LOG("Location[%d]: %f, %f, %d\n", i, response_data->pos_info[i].longitude,
  312. response_data->pos_info[i].latitude, response_data->pos_info[i].accuracy);
  313. }
  314. }
  315. else
  316. {
  317. QL_LBS_WIFI_LOG("response faild");
  318. }
  319. send_event.id = QLBS_WIFI_EVENT_END;
  320. send_event.param1 = (uint32)client_ptr;
  321. ql_rtos_queue_release(client_ptr->queue, sizeof(ql_event_t), (uint8 *)&send_event, 0);
  322. }
  323. void ql_lbs_wifi_start_func(void *param)
  324. {
  325. int ret = 0;
  326. int loc_method = LBS_CELL_LOC_METHOD;// LBS_CELL_WIFI_LOC_METHOD LBS_WIFI_LOC_METHOD
  327. qlbs_wifi_ctx_t *client_ptr = NULL;
  328. ql_event_t send_event = {0};
  329. ql_event_t *recv_event_ptr = (ql_event_t *)param;
  330. lbs_option_t user_option = {0};
  331. if((NULL == recv_event_ptr)||(recv_event_ptr->param1 == 0))
  332. return;
  333. client_ptr = (qlbs_wifi_ctx_t *)recv_event_ptr->param1;
  334. loc_method = client_ptr->loc_method;
  335. if(NULL == client_ptr) return;
  336. if(client_ptr->wifi_num < 2){
  337. client_ptr->wifi_num = 0;
  338. ql_lbs_wifi_lbs_cfg_func(client_ptr, LBS_CELL_LOC_METHOD);/*wifi is invalid, use CELL*/
  339. }else if(loc_method == LBS_WIFI_LOC_METHOD){
  340. ql_lbs_wifi_lbs_cfg_func(client_ptr, LBS_WIFI_LOC_METHOD);
  341. }else if(loc_method == LBS_CELL_LOC_METHOD){
  342. client_ptr->wifi_num = 0;
  343. ql_lbs_wifi_lbs_cfg_func(client_ptr, LBS_CELL_LOC_METHOD);
  344. }else{
  345. ql_lbs_wifi_lbs_cfg_func(client_ptr, LBS_CELL_WIFI_LOC_METHOD);
  346. }
  347. QL_LBS_WIFI_LOG("wifi_num:%d, loc_method=%d", client_ptr->wifi_num, loc_method);
  348. user_option.pdp_cid = client_ptr->pdp_cid;
  349. user_option.sim_id = 0;
  350. user_option.req_timeout = client_ptr->req_timeout;
  351. user_option.basic_info = &client_ptr->basic_info;
  352. user_option.auth_info = &client_ptr->auth_info;
  353. user_option.wifi_num = client_ptr->wifi_num;
  354. user_option.wifi_info = &client_ptr->wifi_info[0];
  355. user_option.cell_num = 1;
  356. user_option.cell_info = client_ptr->cell_info;
  357. QL_LBS_WIFI_LOG("cell infoinfo: radio=%d,mcc=%d,mnc=%d,lac_id=%x,cell_id=%x,signal=%d,tac =%x,bcch=%d,bsic=%d,uarfcndl=%d,psc=%d,rsrq=%d,pci=%d,earfcn=%d",\
  358. client_ptr->cell_info->radio,client_ptr->cell_info->mcc,client_ptr->cell_info->mnc,client_ptr->cell_info->lac_id,client_ptr->cell_info->cell_id,client_ptr->cell_info->signal,client_ptr->cell_info->tac,\
  359. client_ptr->cell_info->bcch,client_ptr->cell_info->bsic,client_ptr->cell_info->uarfcndl,client_ptr->cell_info->psc,client_ptr->cell_info->rsrq,client_ptr->cell_info->pci,client_ptr->cell_info->earfcn);
  360. ret = ql_lbs_get_position(&client_ptr->lbs_client, client_ptr->host, &user_option, ql_lbs_wifi_result_cb, (void *)client_ptr);
  361. QL_LBS_WIFI_LOG("ret:%08X", ret);
  362. if (QL_LBS_OK != ret)
  363. {
  364. send_event.id = QLBS_WIFI_EVENT_END;
  365. send_event.param1 = (uint32)client_ptr;
  366. ql_rtos_queue_release(client_ptr->queue, sizeof(ql_event_t), (uint8 *)&send_event, 10);
  367. }
  368. }
  369. static void ql_lbs_app_run(void *param)
  370. {
  371. int ret = 0;
  372. int flags_break = 0;
  373. int profile_idx = 1;
  374. int run_cnt = 0;
  375. ql_event_t qlbs_wifi_event_msg = {0};
  376. qlbs_wifi_ctx_t *lbs_client = NULL;
  377. ql_event_t send_event = {0};
  378. lbs_client = (qlbs_wifi_ctx_t *)param;
  379. if (NULL == lbs_client)
  380. {
  381. QL_LBS_WIFI_LOG("ql_rtos_mutex_create failed!!!!");
  382. return;
  383. }
  384. /*
  385. Three positioning methods
  386. (1) WIFI positioning, LBS_WIFI_LOC_METHOD
  387. (2) CELL base station positioning, LBS_CELL_LOC_METHOD
  388. (3) WIFI+CELL hybrid positioning, LBS_CELL_WIFI_LOC_METHOD
  389. Note: For hybrid positioning, you need to obtain cell information first, then turn on QIFISCAN, scan WIFI, and finally turn off WIFISCAN.
  390. */
  391. lbs_client->pdp_cid = profile_idx;
  392. lbs_client->timeout = 120;
  393. lbs_client->task = lbs_wifi_task;
  394. lbs_client->loc_method = LBS_CELL_WIFI_LOC_METHOD;
  395. ret = ql_rtos_timer_create(&lbs_client->timer, lbs_client->task, ql_lbs_wifi_timeout_cb, lbs_client);
  396. if (ret != QL_OSI_SUCCESS)
  397. {
  398. QL_LBS_WIFI_LOG("ql_rtos_mutex_create failed!!!!");
  399. return;
  400. }
  401. ret = ql_rtos_mutex_create(&lbs_client->lock);
  402. if (ret != QL_OSI_SUCCESS)
  403. {
  404. QL_LBS_WIFI_LOG("ql_rtos_mutex_create failed!!!!");
  405. return;
  406. }
  407. ret = ql_rtos_queue_create(&lbs_client->queue, sizeof(ql_event_t), QLBS_WIFI_MAX_MSG_CNT);
  408. if (ret != QL_OSI_SUCCESS)
  409. {
  410. QL_LBS_WIFI_LOG("ql_rtos_queue_create failed!!!!");
  411. return;
  412. }
  413. QL_LBS_WIFI_LOG("==========lbs running==========");
  414. ret = ql_lbs_cell_info_get(lbs_client);
  415. if (ret != QL_OSI_SUCCESS)
  416. {
  417. QL_LBS_WIFI_LOG("ql_lbs_cell_info_get failed!!!!");
  418. return;
  419. }
  420. send_event.param1 = (uint32)lbs_client;
  421. send_event.id = QLBS_WIFI_EVENT_WIFISCAN_START;
  422. ql_rtos_queue_release(lbs_client->queue, sizeof(ql_event_t), (uint8 *)&send_event, 10);
  423. flags_break = 0;
  424. for(;;)
  425. {
  426. memset(&qlbs_wifi_event_msg, 0x00, sizeof(ql_event_t));
  427. ql_rtos_queue_wait(lbs_client->queue, (uint8 *)&qlbs_wifi_event_msg, sizeof(ql_event_t), QL_WAIT_FOREVER);
  428. switch(qlbs_wifi_event_msg.id)
  429. {
  430. case QLBS_WIFI_EVENT_WIFISCAN_START:
  431. {
  432. ql_lbs_wifi_timer_start(lbs_client);
  433. ql_lbs_wifi_scan_async_start(&qlbs_wifi_event_msg);
  434. }
  435. break;
  436. case QLBS_WIFI_EVENT_LBS_START:
  437. {
  438. ql_wifiscan_close();
  439. ql_lbs_wifi_start_func(&qlbs_wifi_event_msg);
  440. }
  441. break;
  442. case QLBS_WIFI_EVENT_END:
  443. {
  444. ql_lbs_wifi_timer_stop(lbs_client);
  445. ql_wifiscan_close();
  446. if(run_cnt++ > 9){
  447. flags_break = 1;
  448. }else{
  449. /*Test: next cycle*/
  450. ql_rtos_task_sleep_s(5);
  451. send_event.id = QLBS_WIFI_EVENT_WIFISCAN_START;
  452. ql_rtos_queue_release(lbs_client->queue, sizeof(ql_event_t), (uint8 *)&send_event, 10);
  453. }
  454. }
  455. break;
  456. default:
  457. break;
  458. }
  459. if(flags_break)
  460. break;
  461. }
  462. }
  463. static void ql_lbs_app_destroy(void *param)
  464. {
  465. qlbs_wifi_ctx_t *lbs_client = NULL;
  466. lbs_client = (qlbs_wifi_ctx_t *)param;
  467. if (NULL == lbs_client)
  468. {
  469. QL_LBS_WIFI_LOG("qlbs_wifi_ctx_t NULL!");
  470. return;
  471. }
  472. if(lbs_client->timer != NULL)
  473. {
  474. if( ql_rtos_timer_is_running(lbs_client->timer) == true )
  475. ql_rtos_timer_stop(lbs_client->timer);
  476. ql_rtos_timer_delete(lbs_client->timer);
  477. lbs_client->timer = NULL;
  478. }
  479. if(lbs_client->wifi_info != NULL)
  480. {
  481. free(lbs_client->wifi_info);
  482. lbs_client->wifi_info = NULL;
  483. }
  484. if(lbs_client->cell_info != NULL)
  485. {
  486. free(lbs_client->cell_info);
  487. lbs_client->cell_info = NULL;
  488. }
  489. if(lbs_client->queue != NULL)
  490. {
  491. ql_rtos_queue_delete(lbs_client->queue);
  492. }
  493. if(lbs_client->lock != NULL)
  494. {
  495. ql_rtos_mutex_delete(lbs_client->lock);
  496. }
  497. free(lbs_client);
  498. lbs_client = NULL;
  499. }
  500. static void lbs_wifi_app_thread(void *arg)
  501. {
  502. int i = 0;
  503. int ret = 0;
  504. uint8_t nSim = 0;
  505. int profile_idx = 1;
  506. ql_data_call_info_s info;
  507. char ip4_addr_str[16] = {0};
  508. ql_rtos_task_sleep_s(10);
  509. QL_LBS_WIFI_LOG("==========lbs wifi demo start ==========");
  510. QL_LBS_WIFI_LOG("wait for network register done");
  511. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10){
  512. i++;
  513. ql_rtos_task_sleep_s(1);
  514. }
  515. if(ret == 0){
  516. i = 0;
  517. QL_LBS_WIFI_LOG("====network registered!!!!====");
  518. }else{
  519. QL_LBS_WIFI_LOG("====network register failure!!!!!====");
  520. goto exit;
  521. }
  522. ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  523. QL_LBS_WIFI_LOG("===start data call====");
  524. ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  525. QL_LBS_WIFI_LOG("===data call result:%d", ret);
  526. if(ret != 0){
  527. QL_LBS_WIFI_LOG("====data call failure!!!!=====");
  528. }
  529. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  530. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  531. if(ret != 0){
  532. QL_LBS_WIFI_LOG("ql_get_data_call_info ret: %d", ret);
  533. ql_stop_data_call(nSim, profile_idx);
  534. goto exit;
  535. }
  536. QL_LBS_WIFI_LOG("info->profile_idx: %d", info.profile_idx);
  537. QL_LBS_WIFI_LOG("info->ip_version: %d", info.ip_version);
  538. QL_LBS_WIFI_LOG("info->v4.state: %d", info.v4.state);
  539. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  540. QL_LBS_WIFI_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  541. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  542. QL_LBS_WIFI_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  543. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  544. QL_LBS_WIFI_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  545. lbs_wifi_client = malloc(sizeof(qlbs_wifi_ctx_t));
  546. if (NULL == lbs_wifi_client)
  547. {
  548. QL_LBS_WIFI_LOG("malloc failed!!!!");
  549. goto exit;
  550. }
  551. memset(lbs_wifi_client, 0x00, sizeof(qlbs_wifi_ctx_t));
  552. ql_lbs_app_run(lbs_wifi_client);
  553. exit:
  554. QL_LBS_WIFI_LOG("==========lbs wifi demo exit ==========");
  555. ql_lbs_app_destroy(lbs_wifi_client);
  556. ql_rtos_task_delete(NULL);
  557. return;
  558. }
  559. int ql_lbs_wifi_app_init(void)
  560. {
  561. QlOSStatus err = QL_OSI_SUCCESS;
  562. err = ql_rtos_task_create(&lbs_wifi_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QlbsWifiApp", lbs_wifi_app_thread, NULL, 5);
  563. if(err != QL_OSI_SUCCESS)
  564. {
  565. QL_LBS_WIFI_LOG("QlbsWifiApp init failed");
  566. }
  567. return err;
  568. }