vsim_adapt_demo.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. 23/07/2022 Joe.tu create
  12. =================================================================*/
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "ql_api_common.h"
  17. #include "ql_api_osi.h"
  18. #include "ql_log.h"
  19. #include "ql_api_vsim_adapt.h"
  20. #define QL_VSIM_ADAPT_LOG_LEVEL QL_LOG_LEVEL_INFO
  21. #define QL_VSIM_ADAPT_DEMO_LOG(msg, ...) QL_LOG(QL_VSIM_ADAPT_LOG_LEVEL, "vsim_adapt_demo", msg, ##__VA_ARGS__)
  22. static ql_task_t vsim_adapt_task = NULL;
  23. static int prv_process_apdu(uint8_t *apdu_req, uint16_t apdu_req_len, uint8_t *apdu_rsp, uint16_t *apdu_rsp_len, uint8_t slot)
  24. {
  25. const char apdu[] = {0xFF, 0xEE, 0xDD, 0xCC, 0xBB,
  26. 0xAA, 0x99, 0x88, 0x77, 0x66,
  27. 0x55, 0x44, 0x33, 0x22, 0x11, 0x00};
  28. *apdu_rsp_len = sizeof(apdu);
  29. memcpy(apdu_rsp, apdu, *apdu_rsp_len);
  30. QL_VSIM_ADAPT_DEMO_LOG("return apdu:%d", *apdu_rsp_len);
  31. return 0x9000; /* return SW1 SW2 */
  32. }
  33. static uint16_t prv_process_reset(uint8_t *atr_data, uint8_t *atr_size, uint8_t nSimID)
  34. {
  35. const char atr[] = {0x00, 0x11, 0x22, 0x33, 0x44,
  36. 0x55, 0x66, 0x77, 0x88, 0x99,
  37. 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
  38. uint8_t size = sizeof(atr);
  39. if (size > *atr_size)
  40. return -1;
  41. memcpy(atr_data, atr, *atr_size);
  42. QL_VSIM_ADAPT_DEMO_LOG("return atr:%d", *atr_size);
  43. return 0; /* return 0 on success */
  44. }
  45. ql_vsim_adapt_handler_s apapt_handler = {.process_apdu = prv_process_apdu,
  46. .reset = prv_process_reset};
  47. /* Note: to implement a local soft sim or others at startup, should register vsim adapt callback before atEngineStart */
  48. static void vsim_adapt_app_thread(void *arg)
  49. {
  50. QlOSStatus err = 0;
  51. uint8_t nSim = 0;
  52. ql_vsim_adapt_errcode_e ret = 0;
  53. ql_vsim_adapt_sim_type_e sim_type = QL_VSIM_ADAPT_SIM_TYPE_TSIM;
  54. ql_rtos_task_sleep_s(10);
  55. QL_VSIM_ADAPT_DEMO_LOG("==========vsim adapt demo start ==========");
  56. sim_type = ql_vsim_adapt_get_sim_type(nSim);
  57. QL_VSIM_ADAPT_DEMO_LOG("get sim_type:%d", sim_type);
  58. if (QL_VSIM_ADAPT_SIM_TYPE_MAX == sim_type)
  59. {
  60. QL_VSIM_ADAPT_DEMO_LOG("get sim_type error");
  61. goto exit;
  62. }
  63. ret = ql_vsim_adapt_set_sim_type(QL_VSIM_ADAPT_SIM_TYPE_SSIM, &apapt_handler, nSim);
  64. if (QL_VSIM_ADAPT_SUCCESS != ret)
  65. {
  66. goto exit;
  67. }
  68. QL_VSIM_ADAPT_DEMO_LOG("==============vsim adapt demo end================");
  69. exit:
  70. err = ql_rtos_task_delete(NULL);
  71. if (err != QL_OSI_SUCCESS)
  72. {
  73. QL_VSIM_ADAPT_DEMO_LOG("task deleted failed");
  74. }
  75. }
  76. int ql_vsim_adapt_init(void)
  77. {
  78. QlOSStatus err = QL_OSI_SUCCESS;
  79. err = ql_rtos_task_create(&vsim_adapt_task, 4 * 1024, APP_PRIORITY_NORMAL, "QVIMADAPT", vsim_adapt_app_thread, NULL, 5);
  80. if (err != QL_OSI_SUCCESS)
  81. {
  82. QL_VSIM_ADAPT_DEMO_LOG("vsim_adapt_app init failed");
  83. }
  84. return err;
  85. }