ql_pbk_demo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_pbk_demo.h"
  19. #define QL_PBK_LOG_LEVEL QL_LOG_LEVEL_INFO
  20. #define QL_PBK_LOG(msg, ...) QL_LOG(QL_PBK_LOG_LEVEL, "ql_pbk_demo", msg, ##__VA_ARGS__)
  21. #define QL_PBK_TASK_STACK_SIZE 4096
  22. #define QL_PBK_TASK_PRIO APP_PRIORITY_NORMAL
  23. #define QL_PBK_TASK_EVENT_CNT 5
  24. ql_task_t pbk_task = NULL;
  25. ql_sem_t pbk_init_sem = NULL;
  26. void user_pbk_event_callback(uint8_t nSim, int event_id, void *ctx)
  27. {
  28. switch (event_id)
  29. {
  30. case QL_PBK_INIT_OK_IND: {
  31. QL_PBK_LOG("QL_PBK_INIT_OK_IND sim:%d", nSim);
  32. if (0 == nSim)
  33. {
  34. ql_rtos_semaphore_release(pbk_init_sem);
  35. }
  36. break;
  37. }
  38. default:
  39. break;
  40. }
  41. }
  42. static void ql_pbk_demo_thread(void *param)
  43. {
  44. QlOSStatus err = QL_OSI_SUCCESS;
  45. ql_sim_errcode_e ret = QL_SIM_SUCCESS;
  46. uint8_t sim_id = 0;
  47. int start_index = 1;
  48. int end_index = 20;
  49. uint8_t *username = NULL;
  50. uint8_t username_len = 0;
  51. ql_sim_pbk_item_info_s item = {0};
  52. ql_sim_pbk_itemset_info_s *itemset = NULL;
  53. itemset = (ql_sim_pbk_itemset_info_s *)malloc(sizeof(ql_sim_pbk_itemset_info_s));
  54. if (itemset == NULL)
  55. {
  56. QL_PBK_LOG("malloc itemset fail");
  57. goto exit;
  58. }
  59. ql_pbk_callback_register(user_pbk_event_callback);
  60. ql_sim_set_pbk_encoding(QL_SIM_PBK_GSM);
  61. //wait pbk ok
  62. if (ql_rtos_semaphore_wait(pbk_init_sem, QL_WAIT_FOREVER))
  63. {
  64. QL_PBK_LOG("Waiting for PBK init timeout");
  65. }
  66. //add
  67. item.index = 1;
  68. strcpy((char *)item.username, "quectel");
  69. strcpy((char *)item.phonenum, "+0123456789");
  70. item.username_len = strlen((const char *)item.username);
  71. ret = ql_sim_write_pbk_item(sim_id, QL_SIM_PBK_STORAGE_SM, &item);
  72. QL_PBK_LOG("add item ret:0x%x", ret);
  73. //read
  74. start_index = 1;
  75. end_index = QL_PBK_ITEM_COUNT_MAX * 2; //You can actually read QL_PBK_ITEM_COUNT_MAX items
  76. memset((void *)itemset, 0, sizeof(ql_sim_pbk_itemset_info_s));
  77. ret = ql_sim_read_pbk_item(sim_id, QL_SIM_PBK_STORAGE_SM, start_index, end_index, username, username_len, itemset);
  78. QL_PBK_LOG("read item ret:0x%x", ret);
  79. if (QL_SIM_SUCCESS == ret)
  80. {
  81. QL_PBK_LOG("read item count:%d", itemset->item_count);
  82. for (size_t i = 0; i < itemset->item_count; i++)
  83. {
  84. QL_PBK_LOG("read item index:%-3d phonenum:%-24.24s username:%-32.32s namelen:%d",
  85. itemset->item[i].index,
  86. itemset->item[i].phonenum,
  87. itemset->item[i].username,
  88. itemset->item[i].username_len);
  89. }
  90. }
  91. //search
  92. start_index = 0;
  93. username = (uint8_t *)"quec";
  94. username_len = strlen((char const *)username);
  95. memset((void *)itemset, 0, sizeof(ql_sim_pbk_itemset_info_s));
  96. ret = ql_sim_read_pbk_item(sim_id, QL_SIM_PBK_STORAGE_SM, start_index, end_index, username, username_len, itemset);
  97. QL_PBK_LOG("search item ret:0x%x", ret);
  98. if (QL_SIM_SUCCESS == ret)
  99. {
  100. QL_PBK_LOG("search item count:%d", itemset->item_count);
  101. for (size_t i = 0; i < itemset->item_count; i++)
  102. {
  103. QL_PBK_LOG("search item index:%-3d phonenum:%-24.24s username:%-32.32s namelen:%d",
  104. itemset->item[i].index,
  105. itemset->item[i].phonenum,
  106. itemset->item[i].username,
  107. itemset->item[i].username_len);
  108. }
  109. }
  110. //delete
  111. item.index = 1;
  112. memset(item.phonenum, 0, sizeof(item.phonenum) / sizeof(item.phonenum[0]));
  113. ret = ql_sim_write_pbk_item(sim_id, QL_SIM_PBK_STORAGE_SM, &item);
  114. QL_PBK_LOG("delete item ret:0x%x", ret);
  115. free(itemset);
  116. exit:
  117. err = ql_rtos_task_delete(NULL);
  118. if (err != QL_OSI_SUCCESS)
  119. {
  120. QL_PBK_LOG("task deleted failed");
  121. }
  122. }
  123. void ql_pbk_app_init(void)
  124. {
  125. QlOSStatus err = QL_OSI_SUCCESS;
  126. err = ql_rtos_semaphore_create(&pbk_init_sem, 0);
  127. if (err != QL_OSI_SUCCESS)
  128. {
  129. QL_PBK_LOG("pbk_init_sem created failed, ret = 0x%x", err);
  130. }
  131. err = ql_rtos_task_create(&pbk_task, QL_PBK_TASK_STACK_SIZE, QL_PBK_TASK_PRIO, "QPBKDEMO", ql_pbk_demo_thread, NULL, QL_PBK_TASK_EVENT_CNT);
  132. if (err != QL_OSI_SUCCESS)
  133. {
  134. QL_PBK_LOG("task created failed");
  135. }
  136. }