wifi_demo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. /*===========================================================================
  13. * include files
  14. ===========================================================================*/
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "ql_api_osi.h"
  19. #include "ql_log.h"
  20. #include "ql_gpio.h"
  21. #include "wifi_demo_macro.h"
  22. #include "ql_api_datacall.h"
  23. #include "ql_api_nw.h"
  24. #include "ql_api_wifi.h"
  25. #include "sockets.h"
  26. #include "lwip/ip_addr.h"
  27. #include "lwip/ip6_addr.h"
  28. #include "lwip/inet.h"
  29. #include "lwip/tcp.h"
  30. #ifdef QL_WIFI_FC41D
  31. #include "ql_wifi_app.h"
  32. #endif
  33. /*========================================================================
  34. * Macro Definition
  35. *========================================================================*/
  36. #define QL_WIFI_DEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  37. #define QL_WIFI_DEMO_LOG(msg, ...) QL_LOG(QL_WIFI_DEMO_LOG_LEVEL, "phy_demo", msg, ##__VA_ARGS__)
  38. #define QL_WIFI_DEMO_MSG_QUEUE_CNT 10
  39. #define QL_WIFI_DEMO_DISCONNECT_RETRY_TIME (20 * 1000)
  40. #define QL_WIFI_DEMO_RETRY_CNT_MAX 3
  41. #define QL_WIFI_DEMO_SIM_ID 0
  42. #define QL_WIFI_DEMO_PROFILE_IDX 1
  43. #define QL_WIFI_DEMO_SOCKET_BUF_SIZE 128
  44. #define QL_WIFI_DEMO_TCP_CLIENTS_CNT 1
  45. /*========================================================================
  46. * Enumeration Definition
  47. *========================================================================*/
  48. typedef enum
  49. {
  50. WIFI_DEMO_RETRY_CONNECT_START = 0,
  51. WIFI_DEMO_RETRY_CONNECT_END,
  52. } wifi_demo_retry_state_e;
  53. typedef enum
  54. {
  55. WIFI_DEMO_EVENT_INIT = 0,
  56. WIFI_DEMO_EVENT_TRY_CONNECT,
  57. WIFI_DEMO_EVENT_CONNECTED,
  58. WIFI_DEMO_EVENT_DISCONNECT,
  59. WIFI_DEMO_EVENT_APP_TCP_CONNECT,
  60. WIFI_DEMO_EVENT_APP_TCP_DISCONNECT,
  61. WIFI_DEMO_EVENT_GET_STATUS,
  62. } wifi_demo_event_e;
  63. /*========================================================================
  64. * Type Definition
  65. *=========================================================================*/
  66. #ifdef QL_WIFI_FC41D
  67. typedef struct
  68. {
  69. int id;
  70. int fd;
  71. ql_task_t task;
  72. uint8_t close_flag;
  73. } wifi_demo_tcp_client_s;
  74. #endif
  75. typedef struct
  76. {
  77. ql_task_t task;
  78. #ifdef QL_WIFI_FC41D
  79. uint8_t sim_id;
  80. int profile_idx;
  81. ql_wifi_state_e state;
  82. ql_queue_t msg_queue;
  83. ql_timer_t retry_timer;
  84. wifi_demo_retry_state_e retry_state;
  85. int retry_cnt;
  86. // tcp client
  87. wifi_demo_tcp_client_s tcp_client[QL_WIFI_DEMO_TCP_CLIENTS_CNT];
  88. int tcp_client_cnt;
  89. #endif
  90. } wifi_demo_manager_s;
  91. typedef struct
  92. {
  93. uint32_t id;
  94. uint32_t param1;
  95. uint32_t param2;
  96. uint32_t param3;
  97. } wifi_demo_event_s;
  98. /*========================================================================
  99. * Global Variable
  100. *========================================================================*/
  101. wifi_demo_manager_s wifi_demo_manager = {0};
  102. #ifdef QL_WIFI_FC41D
  103. ql_mutex_t wifi_demo_mutex = NULL;
  104. static char send_buf[QL_WIFI_DEMO_SOCKET_BUF_SIZE] = {0};
  105. static int send_len = 0;
  106. static char recv_buf[QL_WIFI_DEMO_SOCKET_BUF_SIZE] = {0};
  107. static int recv_len = 0;
  108. #endif
  109. /*========================================================================
  110. * function Definition
  111. *========================================================================*/
  112. #ifdef QL_WIFI_FC41D
  113. void wifi_demo_send_event(ql_task_t task, uint32_t id, uint32_t param1, uint32_t param2, uint32_t param3)
  114. {
  115. wifi_demo_manager_s *manager = &wifi_demo_manager;
  116. ql_queue_t *msg_queue = NULL;
  117. if (task == manager->task)
  118. {
  119. msg_queue = manager->msg_queue;
  120. }
  121. else
  122. {
  123. return;
  124. }
  125. QlOSStatus err = QL_OSI_SUCCESS;
  126. wifi_demo_event_s event = {.id = id, .param1 = param1, .param2 = param2, .param3 = param3};
  127. err = ql_rtos_queue_release(msg_queue, sizeof(wifi_demo_event_s), (uint8 *)&event, QL_NO_WAIT);
  128. if (err)
  129. {
  130. QL_WIFI_DEMO_LOG("release failed");
  131. }
  132. return;
  133. }
  134. static void wifi_demo_retry_timer_callback(void *ctx)
  135. {
  136. wifi_demo_manager_s *manager = &wifi_demo_manager;
  137. QL_WIFI_DEMO_LOG("timer retry");
  138. wifi_demo_send_event(manager->task, WIFI_DEMO_EVENT_TRY_CONNECT, 0, 0, 0);
  139. return;
  140. }
  141. static void wifi_demo_connect_status(void* ctx)
  142. {
  143. uint32_t value = *((uint32_t*)ctx);
  144. wifi_demo_manager_s *manager = &wifi_demo_manager;
  145. QL_WIFI_DEMO_LOG("wifi connect %d", value);
  146. wifi_demo_send_event(manager->task, WIFI_DEMO_EVENT_GET_STATUS, value, 0, 0);
  147. return;
  148. }
  149. void wifi_demo_tcp_client_thread(void *arg)
  150. {
  151. wifi_demo_manager_s *manager = &wifi_demo_manager;
  152. wifi_demo_tcp_client_s *ctx = (wifi_demo_tcp_client_s *)arg;
  153. ctx->close_flag = 0;
  154. int ret = 0;
  155. uint8_t sim_id = manager->sim_id;
  156. int profile_idx = manager->profile_idx;
  157. ql_data_call_info_s info = {0};
  158. // tcp socket
  159. int i = 0;
  160. int flags = 0;
  161. int connected = 0;
  162. int socket_fd = -1;
  163. fd_set read_fds;
  164. fd_set write_fds;
  165. fd_set exp_fds;
  166. int fd_changed = 0;
  167. int closing = false;
  168. char ip4_addr_str[16] = {0};
  169. struct sockaddr_in local4, server_ipv4;
  170. uint8_t destip[] = {220, 180, 239, 212};
  171. uint16_t destport = 8305;
  172. ip4_addr_t int_destip;
  173. IP4_ADDR(&int_destip, destip[0], destip[1], destip[2], destip[3]);
  174. ret = ql_get_data_call_info(sim_id, profile_idx, &info);
  175. if (ret != QL_DATACALL_SUCCESS)
  176. {
  177. QL_WIFI_DEMO_LOG("datacall get err");
  178. goto exit;
  179. }
  180. QL_WIFI_DEMO_LOG("info->profile_idx: %d", info.profile_idx);
  181. QL_WIFI_DEMO_LOG("info->ip_version: %d", info.ip_version);
  182. QL_WIFI_DEMO_LOG("info->v4.state: %d", info.v4.state);
  183. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  184. QL_WIFI_DEMO_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  185. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  186. QL_WIFI_DEMO_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  187. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  188. QL_WIFI_DEMO_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  189. memset(&local4, 0x00, sizeof(struct sockaddr_in));
  190. local4.sin_family = AF_INET;
  191. local4.sin_port = 0;
  192. inet_aton(ip4addr_ntoa(&info.v4.addr.ip), &local4.sin_addr);
  193. FD_ZERO(&read_fds);
  194. FD_ZERO(&write_fds);
  195. FD_ZERO(&exp_fds);
  196. QL_WIFI_DEMO_LOG("socket start!");
  197. socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  198. if(socket_fd < 0)
  199. {
  200. goto exit;
  201. }
  202. ret = bind(socket_fd,(struct sockaddr *)&local4,sizeof(struct sockaddr));
  203. if(ret < 0)
  204. {
  205. close(socket_fd);
  206. socket_fd = -1;
  207. goto exit;
  208. }
  209. flags |= O_NONBLOCK;
  210. fcntl(socket_fd, F_SETFL,flags);
  211. memset(&server_ipv4, 0x00, sizeof(struct sockaddr_in));
  212. server_ipv4.sin_family = AF_INET;
  213. server_ipv4.sin_port = htons(destport);
  214. memcpy(&(server_ipv4.sin_addr),&int_destip,sizeof(int_destip));
  215. ret = connect(socket_fd, (struct sockaddr *)&server_ipv4, sizeof(server_ipv4));
  216. if(ret == 0)
  217. {
  218. connected = 1;
  219. }
  220. else
  221. {
  222. if(lwip_get_error(socket_fd) != EINPROGRESS)
  223. {
  224. close(socket_fd);
  225. socket_fd = -1;
  226. goto exit;
  227. }
  228. }
  229. if (connected == 1)
  230. {
  231. QL_WIFI_DEMO_LOG("tcp connected");
  232. ql_rtos_mutex_try_lock(wifi_demo_mutex);
  233. ctx->fd = socket_fd;
  234. ql_rtos_mutex_unlock(wifi_demo_mutex);
  235. char con[128] = {0};
  236. int len = 0;
  237. FD_SET(socket_fd, &read_fds);
  238. len = snprintf(con, 128, "tcp connected");
  239. if (write(socket_fd, con, len) != len)
  240. {
  241. FD_SET(socket_fd, &write_fds);
  242. }
  243. }
  244. else
  245. {
  246. FD_SET(socket_fd, &write_fds);
  247. }
  248. FD_SET(socket_fd, &exp_fds);
  249. while (1)
  250. {
  251. fd_changed = select(socket_fd + 1, &read_fds, &write_fds, &exp_fds, NULL);
  252. if (fd_changed > 0)
  253. {
  254. if (FD_ISSET(socket_fd, &write_fds))
  255. {
  256. FD_CLR(socket_fd, &write_fds);
  257. if (connected == 0)
  258. {
  259. int value = 0;
  260. char con[128] = {0};
  261. int len = sizeof(value);
  262. getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &value, &len);
  263. if (value == 0 || value == EISCONN)
  264. {
  265. QL_WIFI_DEMO_LOG("tcp connected");
  266. ql_rtos_mutex_try_lock(wifi_demo_mutex);
  267. ctx->fd = socket_fd;
  268. ql_rtos_mutex_unlock(wifi_demo_mutex);
  269. connected = 1;
  270. FD_SET(socket_fd, &read_fds);
  271. len = snprintf(con, 128, "tcp connected");
  272. write(socket_fd, con, len);
  273. }
  274. else
  275. {
  276. QL_WIFI_DEMO_LOG("tcp connect failed");
  277. close(socket_fd);
  278. socket_fd = -1;
  279. break;
  280. }
  281. }
  282. else
  283. {
  284. memset(send_buf, 0x00, 128);
  285. send_len = snprintf(send_buf, 128, "%d%s%d\r\n", i, "startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend", i);
  286. write(socket_fd, send_buf, send_len);
  287. i++;
  288. }
  289. }
  290. else if (FD_ISSET(socket_fd, &read_fds))
  291. {
  292. FD_CLR(socket_fd, &read_fds);
  293. memset(recv_buf, 0x00, 128);
  294. recv_len = read(socket_fd, recv_buf, 128);
  295. if (recv_len > 0)
  296. {
  297. QL_WIFI_DEMO_LOG("recv: %s", recv_buf);
  298. memset(send_buf, 0x00, 128);
  299. send_len = snprintf(send_buf, 128, "%d%s%d\r\n", i, "startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend", i);
  300. write(socket_fd, send_buf, send_len);
  301. i++;
  302. FD_SET(socket_fd, &read_fds);
  303. }
  304. else if (recv_len == 0)
  305. {
  306. if (closing == false)
  307. {
  308. QL_WIFI_DEMO_LOG("shut down");
  309. shutdown(socket_fd, SHUT_WR);
  310. closing = true;
  311. FD_SET(socket_fd, &read_fds);
  312. }
  313. else
  314. {
  315. close(socket_fd);
  316. socket_fd = -1;
  317. break;
  318. }
  319. }
  320. else
  321. {
  322. if (lwip_get_error(socket_fd) == EAGAIN)
  323. {
  324. FD_SET(socket_fd, &read_fds);
  325. }
  326. else
  327. {
  328. close(socket_fd);
  329. socket_fd = -1;
  330. break;
  331. }
  332. }
  333. }
  334. else if (FD_ISSET(socket_fd, &exp_fds))
  335. {
  336. FD_CLR(socket_fd, &exp_fds);
  337. close(socket_fd);
  338. socket_fd = -1;
  339. break;
  340. }
  341. }
  342. }
  343. exit:
  344. QL_WIFI_DEMO_LOG("wifi tcp client %d end,%p", ctx->id, ctx->task);
  345. close(socket_fd);
  346. ql_rtos_mutex_try_lock(wifi_demo_mutex);
  347. memset(ctx, 0, sizeof(wifi_demo_tcp_client_s));
  348. ctx->id = -1;
  349. ctx->fd = -1;
  350. manager->tcp_client_cnt--;
  351. ql_rtos_mutex_unlock(wifi_demo_mutex);
  352. ql_rtos_task_delete(NULL);
  353. }
  354. static int wifi_demo_tcp_client_create()
  355. {
  356. wifi_demo_manager_s *manager = &wifi_demo_manager;
  357. wifi_demo_tcp_client_s *ctx = NULL;
  358. // User defined.Only support one client here.
  359. if (manager->tcp_client_cnt >= 1)
  360. {
  361. QL_WIFI_DEMO_LOG("only support one client");
  362. return -1;
  363. }
  364. // Find NULL task
  365. int i = 0;
  366. for (i = 0; i < QL_WIFI_DEMO_TCP_CLIENTS_CNT; i++)
  367. {
  368. ctx = &(manager->tcp_client[i]);
  369. QL_WIFI_DEMO_LOG("ctx->task %p", ctx->task);
  370. if (ctx->task == NULL)
  371. {
  372. break;
  373. }
  374. }
  375. if (i >= QL_WIFI_DEMO_TCP_CLIENTS_CNT)
  376. {
  377. QL_WIFI_DEMO_LOG("client full");
  378. return -1;
  379. }
  380. if (QL_OSI_SUCCESS != ql_rtos_task_create(&(ctx->task), 4 * 1024, APP_PRIORITY_NORMAL, "wifi_tcp_client", wifi_demo_tcp_client_thread, ctx, 5))
  381. {
  382. QL_WIFI_DEMO_LOG("task created failed");
  383. return -1;
  384. }
  385. ctx->id = i;
  386. ctx->fd = -1;
  387. manager->tcp_client_cnt++;
  388. return 0;
  389. }
  390. #endif
  391. static void wifi_demo_thread(void *arg)
  392. {
  393. wifi_demo_manager_s *manager = &wifi_demo_manager;
  394. #ifdef QL_WIFI_FC41D
  395. QL_WIFI_DEMO_LOG("wifi demo task start");
  396. int ret = 0;
  397. uint8_t sim_id = QL_WIFI_DEMO_SIM_ID;
  398. int profile_idx = QL_WIFI_DEMO_PROFILE_IDX; // range 1 to 7
  399. char ssid_str[] = {"enp0s11u"};
  400. char password_str[] = {"l123654789"};
  401. // Initialize client ctx
  402. manager->tcp_client_cnt = 0;
  403. for (int i = 0; i < QL_WIFI_DEMO_TCP_CLIENTS_CNT; i++)
  404. {
  405. manager->tcp_client[i].id = -1;
  406. manager->tcp_client[i].fd = -1;
  407. manager->tcp_client[i].task = NULL;
  408. }
  409. // Create mutex
  410. ret = ql_rtos_mutex_create(&wifi_demo_mutex);
  411. if (ret != QL_OSI_SUCCESS)
  412. {
  413. QL_WIFI_DEMO_LOG("create mutex ret: %x", ret);
  414. goto exit;
  415. }
  416. // Create timer
  417. ret = ql_rtos_timer_create(&(manager->retry_timer), NULL, wifi_demo_retry_timer_callback, NULL);
  418. if (ret != QL_OSI_SUCCESS)
  419. {
  420. QL_WIFI_DEMO_LOG("create timer ret: %x", ret);
  421. goto exit;
  422. }
  423. // Create message queue
  424. ret = ql_rtos_queue_create(&(manager->msg_queue), sizeof(wifi_demo_event_s), QL_WIFI_DEMO_MSG_QUEUE_CNT);
  425. if (ret != QL_OSI_SUCCESS)
  426. {
  427. QL_WIFI_DEMO_LOG("msg queue ret: %x", ret);
  428. goto exit;
  429. }
  430. // wifi init
  431. ret = ql_wifi_app_init(NULL);
  432. if (ret != QL_WIFI_APP_SUCCESS)
  433. {
  434. QL_WIFI_DEMO_LOG("ql_wifi_app_init err %x", ret);
  435. goto exit;
  436. }
  437. QL_WIFI_DEMO_LOG("ql_wifi_app_init done");
  438. // enable dhcp
  439. char dhcp = 1;
  440. ret = ql_wifi_opt_set(QL_WIFI_OPT_DHCP, &dhcp, sizeof(char));
  441. if (ret != QL_WIFI_SUCCESS)
  442. {
  443. QL_WIFI_DEMO_LOG("dhcp set err %x", ret);
  444. goto exit;
  445. }
  446. QL_WIFI_DEMO_LOG("QL_WIFI_OPT_DHCP done");
  447. // specify wifi cid
  448. ql_wifi_cid_s ctx =
  449. {
  450. .sim_id = sim_id,
  451. .cid = profile_idx,
  452. .enable = 1,
  453. };
  454. ret = ql_wifi_opt_set(QL_WIFI_OPT_CID, &ctx, sizeof(ql_wifi_cid_s));
  455. if (ret != QL_WIFI_SUCCESS)
  456. {
  457. QL_WIFI_DEMO_LOG("cid set err %x", ret);
  458. goto exit;
  459. }
  460. QL_WIFI_DEMO_LOG("QL_WIFI_OPT_CID done");
  461. manager->sim_id = sim_id;
  462. manager->profile_idx = profile_idx;
  463. // ssid & password
  464. ql_wifi_app_user_ap_info_s *ap_user_info = (ql_wifi_app_user_ap_info_s *)calloc(1,sizeof(ql_wifi_app_user_ap_info_s));
  465. if(!ap_user_info)
  466. {
  467. QL_WIFI_DEMO_LOG("calloc faild!");
  468. goto exit;
  469. }
  470. int ssid_len = strlen(ssid_str);
  471. int password_len = strlen(password_str);
  472. if(ssid_len >= QL_WIFI_SSID_MAX_LEN || password_len >= QL_WIFI_PASSWORD_MAX_LEN)
  473. {
  474. goto exit;
  475. }
  476. memcpy(ap_user_info->ssid,ssid_str,ssid_len);
  477. memcpy(ap_user_info->password,password_str,password_len);
  478. ret = ql_wifi_app_set_user_ap_info(ap_user_info);
  479. if(ret != QL_WIFI_APP_SUCCESS)
  480. {
  481. QL_WIFI_DEMO_LOG("get ssid/password faild!");
  482. free(ap_user_info);
  483. goto exit;
  484. }
  485. free(ap_user_info);
  486. wifi_demo_send_event(manager->task, WIFI_DEMO_EVENT_INIT, 0, 0, 0);
  487. while (1)
  488. {
  489. wifi_demo_event_s event = {0};
  490. if (QL_OSI_SUCCESS != ql_rtos_queue_wait(manager->msg_queue, (uint8 *)&event, sizeof(wifi_demo_event_s), QL_WAIT_FOREVER))
  491. {
  492. QL_WIFI_DEMO_LOG("queue wait error");
  493. goto exit;
  494. }
  495. QL_WIFI_DEMO_LOG("wifi_demo_thread %x", event.id);
  496. switch (event.id)
  497. {
  498. case WIFI_DEMO_EVENT_INIT: {
  499. // wifi software reset,better add hardware reset
  500. ret = ql_wifi_app_reset(NULL);
  501. if (ret != QL_WIFI_APP_SUCCESS)
  502. {
  503. QL_WIFI_DEMO_LOG("reset err %x", ret);
  504. goto exit;
  505. }
  506. ql_rtos_task_sleep_ms(10);
  507. QL_WIFI_DEMO_LOG("wifi init");
  508. wifi_demo_send_event(manager->task, WIFI_DEMO_EVENT_TRY_CONNECT, 0, 0, 0);
  509. break;
  510. }
  511. case WIFI_DEMO_EVENT_TRY_CONNECT: {
  512. manager->retry_cnt++;
  513. if (manager->retry_cnt >= QL_WIFI_DEMO_RETRY_CNT_MAX)
  514. {
  515. manager->retry_cnt = 0;
  516. wifi_demo_send_event(manager->task, WIFI_DEMO_EVENT_INIT, 0, 0, 0);
  517. break;
  518. }
  519. // start timer
  520. ret = ql_rtos_timer_start(manager->retry_timer, QL_WIFI_DEMO_DISCONNECT_RETRY_TIME, 0);
  521. QL_WIFI_DEMO_LOG("timer start %x", ret);
  522. if (ret != QL_OSI_SUCCESS)
  523. {
  524. QL_WIFI_DEMO_LOG("manager->retry_timer start failed");
  525. wifi_demo_send_event(manager->task, WIFI_DEMO_EVENT_TRY_CONNECT, 0, 0, 0);
  526. }
  527. QL_WIFI_DEMO_LOG("wifi connect start");
  528. // start to connect wifi
  529. ret = ql_wifi_app_sta_disable(NULL);
  530. if (ret != QL_WIFI_APP_SUCCESS)
  531. {
  532. QL_WIFI_DEMO_LOG("sta disable err %x", ret);
  533. goto exit;
  534. }
  535. ql_rtos_task_sleep_ms(10);
  536. ret = ql_wifi_app_sta_enable(wifi_demo_connect_status);
  537. if (ret != QL_WIFI_APP_SUCCESS)
  538. {
  539. QL_WIFI_DEMO_LOG("sta enable err %x", ret);
  540. goto exit;
  541. }
  542. QL_WIFI_DEMO_LOG("ql_wifi_app_sta_enable done");
  543. // Set retry_state end.When we get disconnect status,
  544. // we check this state for ignoring to send WIFI_DEMO_EVENT_TRY_CONNECT again.
  545. manager->retry_state = WIFI_DEMO_RETRY_CONNECT_END;
  546. break;
  547. }
  548. case WIFI_DEMO_EVENT_CONNECTED: {
  549. ql_data_call_info_s info = {0};
  550. ret = ql_get_data_call_info(sim_id, profile_idx, &info);
  551. QL_WIFI_DEMO_LOG("ql_get_data_call_info ret: 0x%x", ret);
  552. QL_WIFI_DEMO_LOG("info.profile_idx: %d, info.ip_version: %d", info.profile_idx, info.ip_version);
  553. QL_WIFI_DEMO_LOG("info->v4.state: %d, info.v6.state: %d", info.v4.state, info.v6.state);
  554. if (info.v4.state)
  555. {
  556. QL_WIFI_DEMO_LOG("info.v4.addr.ip: %s", ip4addr_ntoa(&(info.v4.addr.ip)));
  557. QL_WIFI_DEMO_LOG("info.v4.addr.pri_dns: %s", ip4addr_ntoa(&(info.v4.addr.pri_dns)));
  558. QL_WIFI_DEMO_LOG("info.v4.addr.sec_dns: %s", ip4addr_ntoa(&(info.v4.addr.sec_dns)));
  559. }
  560. wifi_demo_send_event(manager->task, WIFI_DEMO_EVENT_APP_TCP_CONNECT, 0, 0, 0);
  561. break;
  562. }
  563. case WIFI_DEMO_EVENT_APP_TCP_CONNECT: {
  564. ql_rtos_mutex_try_lock(wifi_demo_mutex);
  565. wifi_demo_tcp_client_create();
  566. ql_rtos_mutex_unlock(wifi_demo_mutex);
  567. break;
  568. }
  569. case WIFI_DEMO_EVENT_GET_STATUS: {
  570. uint32_t value = event.param1;
  571. if (value == QL_WIFI_STATE_CONNECTED)
  572. {
  573. manager->retry_state = WIFI_DEMO_RETRY_CONNECT_END;
  574. ql_rtos_timer_stop(manager->retry_timer);
  575. //From others to QL_WIFI_STATE_CONNECTED
  576. if(manager->state != QL_WIFI_STATE_CONNECTED)
  577. {
  578. wifi_demo_send_event(manager->task, WIFI_DEMO_EVENT_CONNECTED, 0, 0, 0);
  579. }
  580. }
  581. else if (value == QL_WIFI_STATE_DISCONNECT)
  582. {
  583. if (manager->retry_state == WIFI_DEMO_RETRY_CONNECT_END)
  584. {
  585. manager->retry_state = WIFI_DEMO_RETRY_CONNECT_START;
  586. ql_rtos_timer_stop(manager->retry_timer);
  587. int ret = 0;
  588. ret = ql_rtos_timer_start(manager->retry_timer, QL_WIFI_DEMO_DISCONNECT_RETRY_TIME, 0);
  589. QL_WIFI_DEMO_LOG("timer start %x", ret);
  590. if (ret != QL_OSI_SUCCESS)
  591. {
  592. QL_WIFI_DEMO_LOG("manager->retry_timer start failed");
  593. wifi_demo_send_event(manager->task, WIFI_DEMO_EVENT_TRY_CONNECT, 0, 0, 0);
  594. }
  595. }
  596. }
  597. manager->state = value;
  598. manager->retry_cnt = 0;
  599. break;
  600. }
  601. default: {
  602. break;
  603. }
  604. }
  605. }
  606. exit:
  607. if (manager->retry_timer)
  608. {
  609. ql_rtos_timer_delete(manager->retry_timer);
  610. }
  611. if (manager->msg_queue)
  612. {
  613. ql_rtos_queue_delete(manager->msg_queue);
  614. manager->msg_queue = NULL;
  615. }
  616. if(wifi_demo_mutex)
  617. {
  618. ql_rtos_mutex_delete(wifi_demo_mutex);
  619. wifi_demo_mutex = NULL;
  620. }
  621. #endif
  622. QL_WIFI_DEMO_LOG("wifi demo task end");
  623. ql_rtos_task_delete(NULL);
  624. }
  625. void ql_wifi_demo_init(void)
  626. {
  627. QlOSStatus err = QL_OSI_SUCCESS;
  628. wifi_demo_manager_s *manager = &wifi_demo_manager;
  629. err = ql_rtos_task_create(&(manager->task), 4 * 1024, APP_PRIORITY_NORMAL, "wifi_demo", wifi_demo_thread, NULL, 5);
  630. if (err != QL_OSI_SUCCESS)
  631. {
  632. QL_WIFI_DEMO_LOG("task created failed");
  633. return;
  634. }
  635. }