usbnet_demo.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "ql_api_osi.h"
  16. #include "ql_log.h"
  17. #include "ql_api_nw.h"
  18. #include "ql_api_datacall.h"
  19. #include "ql_api_usbnet.h"
  20. #include "ql_power.h"
  21. #define QL_USBNET_LOG_LEVEL QL_LOG_LEVEL_INFO
  22. #define QL_USBNET_DEMO_LOG(msg, ...) QL_LOG(QL_USBNET_LOG_LEVEL, "usbnet_demo", msg, ##__VA_ARGS__)
  23. #define QUEC_USBNET_STOP_REQ 100|(QL_COMPONENT_NETWORK_USBNET<<16)
  24. ql_sem_t ql_nw_reg_sem = NULL;
  25. ql_task_t usbnet_task = NULL;
  26. ql_timer_t usbnet_stop_timer = NULL;
  27. static void usbnet_timer_callback(void *ctx)
  28. {
  29. ql_event_t event={0};
  30. event.id = QUEC_USBNET_STOP_REQ;
  31. ql_rtos_event_send(usbnet_task, &event); //send event to reconnect
  32. return;
  33. }
  34. static void ql_nw_event_callback(uint8_t sim_id, unsigned int event_type, void *ind_msg_buf)
  35. {
  36. if(QUEC_NW_DATA_REG_STATUS_IND == event_type)
  37. {
  38. ql_nw_common_reg_status_info_s *data_reg_status=(ql_nw_common_reg_status_info_s *)ind_msg_buf;
  39. QL_USBNET_DEMO_LOG("Sim%d data reg status changed, current status is %d", sim_id, data_reg_status->state);
  40. if((QL_NW_REG_STATE_HOME_NETWORK == data_reg_status->state) || (QL_NW_REG_STATE_ROAMING == data_reg_status->state))
  41. {
  42. ql_rtos_semaphore_release(ql_nw_reg_sem);
  43. }
  44. }
  45. }
  46. static void ql_usbnet_event_callback(unsigned int event_type, ql_usbnet_errcode_e errcode, void *ctx)
  47. {
  48. QL_USBNET_DEMO_LOG("event_type: 0x%x, errcode: 0x%x", event_type, errcode);
  49. switch(event_type)
  50. {
  51. case QUEC_USBNET_START_RSP_IND:
  52. {
  53. if(QL_USBNET_SUCCESS == errcode)
  54. {
  55. QL_USBNET_DEMO_LOG("usbnet connect success");
  56. //run the following blocked code, the RNDIS/ECM dial will be disconnected after two minutes
  57. #if 0
  58. int ret = ql_rtos_timer_start(usbnet_stop_timer, 1000 * 120, 0);
  59. if(ret != 0)
  60. {
  61. QL_USBNET_DEMO_LOG("usbnet_stop_timer start failed");
  62. }
  63. #endif
  64. }
  65. else
  66. {
  67. QL_USBNET_DEMO_LOG("usbnet connect fail, err: 0x%x", errcode);
  68. ql_rtos_timer_stop(usbnet_stop_timer);
  69. }
  70. break;
  71. }
  72. case QUEC_USBNET_DEACTIVE_IND:
  73. {
  74. QL_USBNET_DEMO_LOG("PDP deactived");
  75. ql_rtos_timer_stop(usbnet_stop_timer);
  76. ql_event_t event={0};
  77. event.id = QUEC_USBNET_DEACTIVE_IND;
  78. ql_rtos_event_send(usbnet_task, &event); //send event to reconnect
  79. break;
  80. }
  81. case QUEC_USBNET_PORT_CONNECT_IND:
  82. {
  83. QL_USBNET_DEMO_LOG("usb port plug in");
  84. ql_event_t event={0};
  85. event.id = QUEC_USBNET_PORT_CONNECT_IND;
  86. ql_rtos_event_send(usbnet_task, &event); //send event to reconnect
  87. break;
  88. }
  89. case QUEC_USBNET_PORT_DISCONNECT_IND:
  90. {
  91. QL_USBNET_DEMO_LOG("usb port pull out");
  92. ql_rtos_timer_stop(usbnet_stop_timer);
  93. break;
  94. }
  95. }
  96. }
  97. static void usbent_app_thread(void * arg)
  98. {
  99. ql_usbnet_errcode_e ret = 0;
  100. uint8_t sim_id = 0;
  101. int profile_idx = 1; //range 1 to 7
  102. ql_nw_reg_status_info_s nw_info = {0};
  103. ql_event_t event;
  104. ql_usbnet_type_e saved_type = 0;
  105. ql_usbnet_type_e target_type = QL_USBNET_ECM;
  106. ql_usbnet_state_e status = QL_USBNET_STATE_NONE;
  107. uint8_t reset_flag = false;
  108. ret = ql_rtos_timer_create(&usbnet_stop_timer, usbnet_task, usbnet_timer_callback, NULL);
  109. QL_USBNET_DEMO_LOG("create timer ret: 0x%x", ret);
  110. if(ret != 0)
  111. {
  112. goto exit;
  113. }
  114. ret = ql_usbnet_get_type(&saved_type);
  115. QL_USBNET_DEMO_LOG("get usbnet saved type ret: 0x%x, %d", ret, saved_type);
  116. if(0 != ret)
  117. {
  118. goto exit;
  119. }
  120. //run the following blocked code, RNDIS/ECM and Socket can use profile 1 simultaneously
  121. #if 0
  122. uint32_t saved_list = 0;
  123. uint32_t target_list = 0x1; //enable sim0 profile 1 nat function
  124. ret = ql_datacall_get_nat(&saved_list);
  125. QL_USBNET_DEMO_LOG("get datacall saved nat ret: 0x%x, %d", ret, saved_list);
  126. if(0 != ret)
  127. {
  128. goto exit;
  129. }
  130. if(saved_list != target_list)
  131. {
  132. ret = ql_datacall_set_nat(target_list);
  133. QL_USBNET_DEMO_LOG("ql_datacall_set_nat err, ret=0x%x", ret);
  134. if(0 != ret)
  135. {
  136. goto exit;
  137. }
  138. reset_flag = true; //NAT function reboot to take effect
  139. }
  140. /*ql_nat_subnet_config_s config = {"192.168.1.0","255.255.255.0"};
  141. ret = ql_datacall_set_subnet(sim_id, profile_idx, &config);
  142. QL_USBNET_DEMO_LOG("ql_datacall_set_subnet ret=0x%x", ret);
  143. if(0 != ret)
  144. {
  145. goto exit;
  146. }*/
  147. #endif
  148. if(saved_type != target_type)
  149. {
  150. ret = ql_usbnet_set_type(target_type);
  151. QL_USBNET_DEMO_LOG("ql_usbnet_set_type err, ret=0x%x", ret);
  152. if(0 != ret)
  153. {
  154. goto exit;
  155. }
  156. reset_flag = true; //usbnet type reboot to take effect
  157. }
  158. if(reset_flag)
  159. {
  160. ql_power_reset(RESET_NORMAL);
  161. }
  162. ql_rtos_semaphore_create(&ql_nw_reg_sem, 0);
  163. ret = ql_nw_register_cb(ql_nw_event_callback);
  164. QL_USBNET_DEMO_LOG("register network callback ret=0x%x", ret);
  165. if(0 != ret)
  166. {
  167. goto exit;
  168. }
  169. ret = ql_nw_get_reg_status(sim_id, &nw_info);
  170. QL_USBNET_DEMO_LOG("ret: 0x%x, current data reg status=%d", ret, nw_info.data_reg.state);
  171. if((QL_NW_REG_STATE_HOME_NETWORK != nw_info.data_reg.state) && (QL_NW_REG_STATE_ROAMING != nw_info.data_reg.state))
  172. {
  173. ql_rtos_semaphore_wait(ql_nw_reg_sem, QL_WAIT_FOREVER); //wait network registered
  174. }
  175. ret = ql_usbnet_register_cb(ql_usbnet_event_callback, NULL);
  176. QL_USBNET_DEMO_LOG("register usbnet callback ret=0x%x", ret);
  177. if(ret != QL_USBNET_SUCCESS)
  178. {
  179. goto exit;
  180. }
  181. ret = ql_usbnet_get_status(&status);
  182. QL_USBNET_DEMO_LOG("ret: 0x%x, usbnet status: %d", ret, status);
  183. if((QL_USBNET_SUCCESS == ret) && (QL_USBNET_STATE_NONE == status))
  184. {
  185. ret = ql_usbnet_start(sim_id, profile_idx, NULL);
  186. QL_USBNET_DEMO_LOG("ql_usbnet_start ret=0x%x", ret);
  187. }
  188. while(1)
  189. {
  190. if(ql_event_try_wait(&event) != 0)
  191. {
  192. continue;
  193. }
  194. switch (event.id)
  195. {
  196. case QUEC_USBNET_DEACTIVE_IND:
  197. {
  198. ql_rtos_task_sleep_ms(5000); //wait network status update
  199. ret = ql_nw_get_reg_status(sim_id, &nw_info);
  200. if((QL_NW_REG_STATE_HOME_NETWORK != nw_info.data_reg.state) && (QL_NW_REG_STATE_ROAMING != nw_info.data_reg.state))
  201. {
  202. ql_rtos_semaphore_wait(ql_nw_reg_sem, QL_WAIT_FOREVER);
  203. }
  204. ret = ql_usbnet_start(sim_id, profile_idx, NULL);
  205. QL_USBNET_DEMO_LOG("ql_usbnet_start ret=0x%x", ret);
  206. break;
  207. }
  208. case QUEC_USBNET_PORT_CONNECT_IND:
  209. {
  210. ret = ql_usbnet_start(sim_id, profile_idx, NULL);
  211. QL_USBNET_DEMO_LOG("ql_usbnet_start ret=0x%x", ret);
  212. break;
  213. }
  214. case QUEC_USBNET_STOP_REQ:
  215. {
  216. ret = ql_usbnet_get_status(&status);
  217. QL_USBNET_DEMO_LOG("ret: 0x%x, usbnet status: %d", ret, status);
  218. if((QL_USBNET_SUCCESS == ret) && (QL_USBNET_STATE_CONNECT == status))
  219. {
  220. ret = ql_usbnet_stop();
  221. QL_USBNET_DEMO_LOG("ql_usbnet_stop ret=0x%x", ret);
  222. }
  223. break;
  224. }
  225. }
  226. }
  227. exit:
  228. if(ql_nw_reg_sem != NULL)
  229. {
  230. ql_rtos_semaphore_delete(ql_nw_reg_sem);
  231. ql_nw_reg_sem = NULL;
  232. }
  233. if(usbnet_stop_timer != NULL)
  234. {
  235. ql_rtos_timer_delete(usbnet_stop_timer);
  236. usbnet_stop_timer = NULL;
  237. }
  238. if(usbnet_task != NULL)
  239. {
  240. ql_rtos_task_delete(usbnet_task);
  241. usbnet_task = NULL;
  242. }
  243. }
  244. void ql_usbnet_app_init(void)
  245. {
  246. QlOSStatus err = QL_OSI_SUCCESS;
  247. err = ql_rtos_task_create(&usbnet_task, 4*1024, APP_PRIORITY_NORMAL, "QUSBNETDEMO", usbent_app_thread, NULL, 10);
  248. if(err != QL_OSI_SUCCESS)
  249. {
  250. QL_USBNET_DEMO_LOG("task created failed");
  251. return;
  252. }
  253. }