aliotsmartcard_demo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #include "ql_api_aliotsmartcard.h"
  22. #define QL_ALIOTSMARTCARD_LOG_LEVEL QL_LOG_LEVEL_INFO
  23. #define QL_ALIOTSMARTCARD_LOG(msg, ...) QL_LOG(QL_ALIOTSMARTCARD_LOG_LEVEL, "ql_smartcard_DEMO", msg, ##__VA_ARGS__)
  24. #define QL_ALIOTSMARTCARD_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_smartcard_DEMO", msg, ##__VA_ARGS__)
  25. static ql_task_t smartcard_task = NULL;
  26. struct ql_smartcard_mnenable
  27. {
  28. bool func_hac;
  29. bool func_sc;
  30. bool func_sw;
  31. bool func_mse;
  32. }asc_mnenable={1,1,0,1};
  33. #define QL_SMARTCARD_PBK_WAIT_TIMEOUT 30000 //unit:ms
  34. #define QL_SMARTCARD_CONFIG_FILE_PATH "UFS:ql_smartcard.json"
  35. #define QL_SMARTCARD_SLOT_SIGN 3 /*3:SIM1 ~ SIM3*/
  36. #define QL_SMARTCARD_PDP_CID 1
  37. static int datacall_satrt(void)
  38. {
  39. int ret = 0;
  40. int i = 0;
  41. ql_data_call_info_s info;
  42. char ip4_addr_str[16] = {0};
  43. uint8_t nSim = 0;
  44. ql_rtos_task_sleep_s(10);
  45. QL_ALIOTSMARTCARD_LOG("========== smartcard demo start ==========");
  46. QL_ALIOTSMARTCARD_LOG("wait for network register done");
  47. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10){
  48. i++;
  49. ql_rtos_task_sleep_s(1);
  50. }
  51. if(ret == 0){
  52. i = 0;
  53. QL_ALIOTSMARTCARD_LOG("====network registered!!!!====");
  54. }else{
  55. QL_ALIOTSMARTCARD_LOG("====network register failure!!!!!====");
  56. goto exit;
  57. }
  58. QL_ALIOTSMARTCARD_LOG("========== smartcard demo start ==========");
  59. aliyun_iot_smartcard_hal_pbk_wait(QL_SMARTCARD_PBK_WAIT_TIMEOUT);
  60. ql_rtos_task_sleep_s(3);
  61. QL_ALIOTSMARTCARD_LOG("========== smartcard demo start ==========");
  62. ql_set_data_call_asyn_mode(nSim, QL_SMARTCARD_PDP_CID, 0);
  63. QL_ALIOTSMARTCARD_LOG("===start data call====");
  64. ret=ql_start_data_call(nSim, QL_SMARTCARD_PDP_CID, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  65. QL_ALIOTSMARTCARD_LOG("===data call result:%d", ret);
  66. if(ret != 0){
  67. QL_ALIOTSMARTCARD_LOG("====data call failure!!!!=====");
  68. }
  69. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  70. ret = ql_get_data_call_info(nSim, QL_SMARTCARD_PDP_CID, &info);
  71. if(ret != 0){
  72. QL_ALIOTSMARTCARD_LOG("ql_get_data_call_info ret: %d", ret);
  73. ql_stop_data_call(nSim, QL_SMARTCARD_PDP_CID);
  74. goto exit;
  75. }
  76. QL_ALIOTSMARTCARD_LOG("info->QL_SMARTCARD_PDP_CID: %d", info.profile_idx);
  77. QL_ALIOTSMARTCARD_LOG("info->ip_version: %d", info.ip_version);
  78. QL_ALIOTSMARTCARD_LOG("info->v4.state: %d", info.v4.state);
  79. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  80. QL_ALIOTSMARTCARD_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  81. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  82. QL_ALIOTSMARTCARD_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  83. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  84. QL_ALIOTSMARTCARD_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  85. return 0;
  86. exit:
  87. return -1;
  88. }
  89. static void aliotsmartcard_app_thread(void * arg)
  90. {
  91. int ret = 0;
  92. int i = 0;
  93. uint8_t func_save = TRUE;/*Parameter update, default save*/
  94. uint8_t update_mnenable_para = FALSE;
  95. uint8_t update_mnpolicy_para = FALSE;
  96. uint8_t update_scan_carrier = FALSE;
  97. uint8_t update_manual_switch = FALSE;
  98. bool old_func_hac = TRUE;
  99. bool old_func_sw = FALSE;
  100. const char *ping_dest_ip = "114.114.114.114";
  101. uint16_t delay_threshold = 60;
  102. uint8_t slot_cnt = QL_SMARTCARD_SLOT_SIGN;
  103. char viccid_buf[32] = {0};
  104. aliyun_iot_smartcard_sim_t sim_info[QL_SMARTCARD_SLOT_SIGN] = {0};
  105. bool mode = FALSE;
  106. /*step1: wait card init done and active*/
  107. ret = datacall_satrt();
  108. if (ret < 0) {
  109. QL_ALIOTSMARTCARD_LOG("smartcard active ret=%d", ret);
  110. goto exit;
  111. }
  112. QL_ALIOTSMARTCARD_LOG("smartcard runn...");
  113. /*step2: init config path*/
  114. ret = aliyun_iot_smartcard_parse_configs(QL_SMARTCARD_CONFIG_FILE_PATH);
  115. if (ret < 0) {
  116. QL_ALIOTSMARTCARD_LOG("smartcard parse_configs failed ret=%d", ret);
  117. goto exit;
  118. }
  119. /*step3: start card thread*/
  120. for (int i = 0; i < 3; i++) {
  121. ret = aliyun_iot_smartcard_init();
  122. if (ret == 0) {
  123. break;
  124. }
  125. aliyun_iot_smartcard_deinit();
  126. QL_ALIOTSMARTCARD_LOG("aliyun_iot_smartcard_init fail %d times, ret = %d!",i+1, ret);
  127. ql_rtos_task_sleep_ms(2000);
  128. QL_ALIOTSMARTCARD_LOG("wait for 2 second and retry");
  129. }
  130. if (3 == i) {
  131. QL_ALIOTSMARTCARD_LOG("aliyun_iot_smartcard_init failed ret=%d", ret);
  132. goto exit;
  133. }
  134. /*step4: API get parameters, exp.*/
  135. ret = aliyun_iot_smartcard_get_enable((bool*)&old_func_hac);
  136. if(0 != ret){
  137. QL_ALIOTSMARTCARD_LOG("smartcard get func_hac ret=%d", ret);
  138. goto exit;
  139. }
  140. ret = aliyun_iot_smartcard_get_switch_flag((bool*)&old_func_sw);
  141. if(0 != ret){
  142. QL_ALIOTSMARTCARD_LOG("smartcard get func_sw ret=%d", ret);
  143. goto exit;
  144. }
  145. /*step5: API set parameters*/
  146. if(TRUE == update_mnenable_para){
  147. ret = aliyun_iot_smartcard_set_enable(asc_mnenable.func_hac);
  148. if(0 != ret){
  149. QL_ALIOTSMARTCARD_LOG("smartcard func_hac ret=%d", ret);
  150. goto exit;
  151. }
  152. aliyun_iot_smartcard_set_scan_flag(asc_mnenable.func_sc);
  153. if(0 != ret){
  154. QL_ALIOTSMARTCARD_LOG("smartcard func_sc ret=%d", ret);
  155. goto exit;
  156. }
  157. aliyun_iot_smartcard_set_switch_flag(asc_mnenable.func_sw);
  158. if(0 != ret){
  159. QL_ALIOTSMARTCARD_LOG("smartcard func_sw ret=%d", ret);
  160. goto exit;
  161. }
  162. aliyun_iot_smartcard_set_manu_switch_flag(asc_mnenable.func_mse);
  163. if(0 != ret){
  164. QL_ALIOTSMARTCARD_LOG("smartcard func_mse ret=%d", ret);
  165. goto exit;
  166. }
  167. /*modify "func_hac" parameters require reinitialization*/
  168. if(asc_mnenable.func_hac != old_func_hac){
  169. aliyun_iot_smartcard_deinit();
  170. ret = aliyun_iot_smartcard_init();
  171. if (ret != 0) {
  172. QL_ALIOTSMARTCARD_LOG("smartcard update param, init fail ret=%d", ret);
  173. goto exit;
  174. }
  175. }
  176. /*modify "func_sw" parameters, Need to modify monitor task at the same time*/
  177. if((TRUE == asc_mnenable.func_sw)&&(FALSE == old_func_sw)){
  178. ret = aliyun_iot_smartcard_start_monitor();/*start monitor task*/
  179. if (ret != 0) {
  180. QL_ALIOTSMARTCARD_LOG("smartcard start monitor fail ret=%d", ret);
  181. goto exit;
  182. }
  183. }
  184. if((TRUE == old_func_sw)&&(FALSE == asc_mnenable.func_sw)){
  185. ret = aliyun_iot_smartcard_stop_monitor();/*stop monitor task*/
  186. if (ret != 0) {
  187. QL_ALIOTSMARTCARD_LOG("smartcard stop monitor fail ret=%d", ret);
  188. goto exit;
  189. }
  190. }
  191. /*Parameter modification, update configuration file*/
  192. if(TRUE == func_save){
  193. aliyun_iot_smartcard_update_configs();
  194. }
  195. }
  196. /*step6: API set mnpolicy, exp.*/
  197. if(TRUE == update_mnpolicy_para){
  198. ret = aliyun_iot_smartcard_set_delay_threshold(delay_threshold);
  199. if(0 != ret){
  200. QL_ALIOTSMARTCARD_LOG("smartcard delay_threshold ret=%d", ret);
  201. goto exit;
  202. }
  203. ret = aliyun_iot_smartcard_set_ping_addr(ping_dest_ip);
  204. if(0 != ret){
  205. QL_ALIOTSMARTCARD_LOG("smartcard ping_dest_ip ret=%d", ret);
  206. goto exit;
  207. }
  208. }
  209. /*step7: API manual switch.*/
  210. if(TRUE == update_manual_switch){
  211. ret = aliyun_iot_smartcard_manual_switch(SWITCH_TO_SIM1);
  212. if(0 != ret){
  213. QL_ALIOTSMARTCARD_LOG("smartcard manual switch ret=%d", ret);
  214. goto exit;
  215. }
  216. }
  217. /*step8: API auto switch.*/
  218. if(TRUE == update_scan_carrier){
  219. ret = aliyun_iot_smartcard_scan_carrier();
  220. if(0 != ret){
  221. QL_ALIOTSMARTCARD_LOG("smartcard scan carrier ret=%d", ret);
  222. goto exit;
  223. }
  224. }
  225. /*step9: API get cardlist info.*/
  226. ret = aliyun_iot_is_smartcard(&mode);
  227. if(0 != ret){
  228. QL_ALIOTSMARTCARD_LOG("not smartcard!");
  229. goto exit;
  230. }
  231. ret = aliyun_iot_smartcard_get_aliiccid(viccid_buf, sizeof(viccid_buf));
  232. if(0 != ret){
  233. QL_ALIOTSMARTCARD_LOG("get aliiccid ret=%d", ret);
  234. goto exit;
  235. }
  236. memset(&sim_info[0], 0, sizeof(aliyun_iot_smartcard_sim_t)*QL_SMARTCARD_SLOT_SIGN);
  237. ret = aliyun_iot_smartcard_get_all_sim_info(&slot_cnt, &sim_info[0]);
  238. if(0 != ret){
  239. QL_ALIOTSMARTCARD_LOG("get sim info ret=%d", ret);
  240. goto exit;
  241. }
  242. slot_cnt = 0;
  243. QL_ALIOTSMARTCARD_LOG("smartcard %d iccid=%s, act=%d, active=%d, car_name=%s, car_type=%d, rssi=%d", slot_cnt,
  244. sim_info[slot_cnt].iccid, sim_info[slot_cnt].act, sim_info[slot_cnt].active,
  245. sim_info[slot_cnt].car_name, sim_info[slot_cnt].car_type, sim_info[slot_cnt].rssi);
  246. slot_cnt = 1;
  247. QL_ALIOTSMARTCARD_LOG("smartcard %d iccid=%s, act=%d, active=%d, car_name=%s, car_type=%d, rssi=%d", slot_cnt,
  248. sim_info[slot_cnt].iccid, sim_info[slot_cnt].act, sim_info[slot_cnt].active,
  249. sim_info[slot_cnt].car_name, sim_info[slot_cnt].car_type, sim_info[slot_cnt].rssi);
  250. slot_cnt = 2;
  251. QL_ALIOTSMARTCARD_LOG("smartcard %d iccid=%s, act=%d, active=%d, car_name=%s, car_type=%d, rssi=%d", slot_cnt,
  252. sim_info[slot_cnt].iccid, sim_info[slot_cnt].act, sim_info[slot_cnt].active,
  253. sim_info[slot_cnt].car_name, sim_info[slot_cnt].car_type, sim_info[slot_cnt].rssi);
  254. while(1){
  255. QL_ALIOTSMARTCARD_LOG("smartcard running");
  256. ql_rtos_task_sleep_s(1);
  257. }
  258. QL_ALIOTSMARTCARD_LOG("smartcard task exit!");
  259. exit:
  260. while(1){
  261. QL_ALIOTSMARTCARD_LOG("smartcard running");
  262. ql_rtos_task_sleep_s(1);
  263. }
  264. QL_ALIOTSMARTCARD_LOG("smartcard task exit!");
  265. smartcard_task = NULL;
  266. ql_rtos_task_delete(NULL);/*self-destruction*/
  267. return;
  268. }
  269. int ql_aliotsmartcard_app_init(void)
  270. {
  271. QlOSStatus err = QL_OSI_SUCCESS;
  272. err = ql_rtos_task_create(&smartcard_task, 4*1024, APP_PRIORITY_NORMAL, "ascApp", aliotsmartcard_app_thread, NULL, 5);
  273. if(err != QL_OSI_SUCCESS)
  274. {
  275. QL_ALIOTSMARTCARD_LOG("ping_app init failed");
  276. }
  277. return err;
  278. }