ql_sim_demo.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_sim.h"
  18. #include "ql_sim_demo.h"
  19. #include "ql_pin_cfg.h"
  20. #define QL_SIM_LOG_LEVEL QL_LOG_LEVEL_INFO
  21. #define QL_SIM_LOG(msg, ...) QL_LOG(QL_SIM_LOG_LEVEL, "ql_sim_demo", msg, ##__VA_ARGS__)
  22. #define QL_SIM_TASK_STACK_SIZE 2048
  23. #define QL_SIM_TASK_PRIO APP_PRIORITY_NORMAL
  24. #define QL_SIM_TASK_EVENT_CNT 5
  25. static void user_sim_hotplug_callback(uint8_t physical_sim_id, ql_sim_hotplug_status_e status)
  26. {
  27. if (QL_SIM_HOTPLUG_STATUS_IN == status)
  28. {
  29. QL_SIM_LOG("sim=%d,status=%d:plug in !!!", physical_sim_id, status);
  30. }
  31. else
  32. {
  33. QL_SIM_LOG("sim=%d,status=%d:plug out !!!", physical_sim_id, status);
  34. }
  35. }
  36. #ifdef QL_APP_FEATURE_DSSS
  37. void ql_sim_dsss_demo(void)
  38. {
  39. ql_sim_errcode_e ret = QL_SIM_SUCCESS;
  40. char siminfo[64] = {0};
  41. ql_sim_status_e card_status = 0;
  42. uint8_t operate_id = ql_sim_get_operate_id();
  43. QL_SIM_LOG("ql_sim_get_operate_id:%d", operate_id);
  44. ret = ql_sim_get_card_status(0, &card_status);
  45. QL_SIM_LOG("ql_sim_get_card_status ret:0x%x, card_status: %d", ret, card_status);
  46. if (QL_SIM_STATUS_READY == card_status)
  47. {
  48. /* get imsi from current SIM, simID should be 0 in DSSS solution */
  49. ret = ql_sim_get_imsi(0, siminfo, 64);
  50. QL_SIM_LOG("ql_sim_get_imsi ret:0x%x, IMSI: %s", ret, siminfo);
  51. }
  52. for (size_t i = 0; i < 10; i++)
  53. {
  54. operate_id ^= 1;
  55. ret = ql_sim_set_operate_id(operate_id);
  56. QL_SIM_LOG("ql_sim_set_operate_id:%d ret:0x%x", operate_id, ret);
  57. /* should wait for PIN ready */
  58. ql_rtos_task_sleep_ms(5000);
  59. ret = ql_sim_get_card_status(0, &card_status);
  60. QL_SIM_LOG("ql_sim_get_card_status ret:0x%x, card_status: %d", ret, card_status);
  61. if (QL_SIM_STATUS_READY == card_status)
  62. {
  63. /* get imsi from current SIM, simID should be 0 in DSSS solution */
  64. ret = ql_sim_get_imsi(0, siminfo, 64);
  65. QL_SIM_LOG("ql_sim_get_imsi ret:0x%x, IMSI: %s", ret, siminfo);
  66. }
  67. }
  68. }
  69. #endif // QL_APP_FEATURE_DSSS
  70. static void ql_sim_demo_thread(void *param)
  71. {
  72. QlOSStatus err = QL_OSI_SUCCESS;
  73. ql_sim_errcode_e ret = QL_SIM_SUCCESS;
  74. char siminfo[64] = {0};
  75. uint8_t sim_id = 0;
  76. uint8_t physical_sim_id = 0;
  77. for (int n = 0; n < 5; n++)
  78. {
  79. QL_SIM_LOG("hello sim demo %d", n);
  80. ql_rtos_task_sleep_ms(2000);
  81. }
  82. #ifdef QL_APP_FEATURE_DSSS
  83. ql_sim_dsss_demo();
  84. #endif // QL_APP_FEATURE_DSSS
  85. ret = ql_sim_get_imsi(sim_id, siminfo, 64);
  86. QL_SIM_LOG("ret:0x%x, IMSI: %s", ret, siminfo);
  87. ret = ql_sim_get_iccid(sim_id, siminfo, 64);
  88. QL_SIM_LOG("ret:0x%x, ICCID: %s", ret, siminfo);
  89. ret = ql_sim_get_phonenumber(sim_id, siminfo, 64);
  90. QL_SIM_LOG("ret:0x%x, phonenumber: %s", ret, siminfo);
  91. #if 0
  92. ql_sim_status_e card_status = 0;
  93. ql_sim_verify_pin_info_s pin = {0};
  94. ql_sim_unblock_pin_info_s pin_un = {0};
  95. ql_sim_change_pin_info_s pin_change = {0};
  96. /*
  97. !!!be careful to enable sim pin!!!
  98. */
  99. //enable pin verify and effective after reboot
  100. memset(&pin, 0, sizeof(ql_sim_verify_pin_info_s));
  101. memcpy(pin.pin_value,"1234",5);
  102. ret = ql_sim_enable_pin(sim_id, &pin);
  103. QL_SIM_LOG("ql_sim_enable_pin ret:0x%x", ret);
  104. ret = ql_sim_get_card_status(sim_id, &card_status);
  105. QL_SIM_LOG("ret:0x%x, card_status: %d", ret, card_status);
  106. if(QL_SIM_STATUS_SIMPIN == card_status)
  107. {
  108. memset(&pin, 0, sizeof(ql_sim_verify_pin_info_s));
  109. memcpy(pin.pin_value, "1234", 5);
  110. ret = ql_sim_verify_pin(sim_id, &pin);
  111. QL_SIM_LOG("ql_sim_verify_pin ret:0x%x", ret);
  112. ret = ql_sim_get_card_status(sim_id, &card_status);
  113. QL_SIM_LOG("ret:0x%x, card_status: %d", ret, card_status);
  114. }
  115. else if(QL_SIM_STATUS_SIMPUK == card_status)
  116. {
  117. memset(&pin_un, 0, sizeof(ql_sim_unblock_pin_info_s));
  118. memcpy(pin_un.puk_value, "26601934", 9);
  119. memcpy(pin_un.new_pin_value, "1234", 5);
  120. ret = ql_sim_unblock_pin(sim_id, &pin_un);
  121. QL_SIM_LOG("ql_sim_unblock_pin ret:0x%x", ret);
  122. ret = ql_sim_get_card_status(sim_id, &card_status);
  123. QL_SIM_LOG("ret:0x%x, card_status: %d", ret, card_status);
  124. }
  125. memset(&pin_change, 0, sizeof(ql_sim_change_pin_info_s));
  126. memcpy(pin_change.old_pin_value, "1234", 5);
  127. memcpy(pin_change.new_pin_value, "4321", 5);
  128. ret = ql_sim_change_pin(sim_id, &pin_change);
  129. QL_SIM_LOG("ql_sim_change_pin ret:0x%x", ret);
  130. memset(&pin_change, 0, sizeof(ql_sim_change_pin_info_s));
  131. memcpy(pin_change.old_pin_value, "4321", 5);
  132. memcpy(pin_change.new_pin_value, "1234", 5);
  133. ret = ql_sim_change_pin(sim_id, &pin_change);
  134. QL_SIM_LOG("ql_sim_change_pin ret:0x%x", ret);
  135. memset(&pin,0,sizeof(ql_sim_verify_pin_info_s));
  136. memcpy(pin.pin_value,"1234",5);
  137. ret = ql_sim_disable_pin(sim_id, &pin);
  138. QL_SIM_LOG("ql_sim_disable_pin ret:0x%x", ret);
  139. #endif
  140. //sim hot plug
  141. ql_pin_set_func(QL_PIN_SIM0_HOTPLUG,QL_PIN_SIM0_HOTPLUG_FUNC_GPIO);
  142. ql_sim_hotplug_register_cb(user_sim_hotplug_callback);
  143. ql_sim_hotplug_gpio_s gpio_cfg={QL_GPIO_SIM0_HOTPLUG,LVL_LOW};
  144. ret = ql_sim_set_hotplug( physical_sim_id,
  145. QL_SIM_HOTPLUG_ENABLE,
  146. &gpio_cfg);
  147. QL_SIM_LOG("ql_sim1_set_hotplug ret:0x%x", ret);
  148. err = ql_rtos_task_delete(NULL);
  149. if(err != QL_OSI_SUCCESS)
  150. {
  151. QL_SIM_LOG("task deleted failed");
  152. }
  153. }
  154. void ql_sim_app_init(void)
  155. {
  156. QlOSStatus err = QL_OSI_SUCCESS;
  157. ql_task_t sim_task = NULL;
  158. err = ql_rtos_task_create(&sim_task, QL_SIM_TASK_STACK_SIZE, QL_SIM_TASK_PRIO, "QSIMDEMO", ql_sim_demo_thread, NULL, QL_SIM_TASK_EVENT_CNT);
  159. if (err != QL_OSI_SUCCESS)
  160. {
  161. QL_SIM_LOG("task created failed");
  162. }
  163. }