sms_demo.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. 10/10/2020 marvin 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_api_sms.h"
  19. #include "ql_log.h"
  20. ql_task_t sms_task = NULL;
  21. ql_sem_t sms_init_sem = NULL;
  22. ql_sem_t sms_list_sem = NULL;
  23. void user_sms_event_callback(uint8_t nSim, int event_id, void *ctx)
  24. {
  25. switch(event_id)
  26. {
  27. case QL_SMS_INIT_OK_IND:
  28. {
  29. QL_SMS_LOG("QL_SMS_INIT_OK_IND");
  30. ql_rtos_semaphore_release(sms_init_sem);
  31. break;
  32. }
  33. case QL_SMS_NEW_MSG_IND:
  34. {
  35. ql_sms_new_s *msg = (ql_sms_new_s *)ctx;
  36. QL_SMS_LOG("sim=%d, index=%d, storage memory=%d", nSim, msg->index, msg->mem);
  37. break;
  38. }
  39. case QL_SMS_LIST_IND:
  40. {
  41. #if 0
  42. ql_sms_msg_s *msg = (ql_sms_msg_s *)ctx;
  43. QL_SMS_LOG("sim=%d,index=%d, msg = %s",nSim, msg->index, msg->buf);
  44. #endif
  45. break;
  46. }
  47. case QL_SMS_LIST_EX_IND:
  48. {
  49. ql_sms_recv_s *msg = (ql_sms_recv_s *)ctx;
  50. QL_SMS_LOG("index=%d,os=%s,tooa=%u,status=%d,fo=0x%x,dcs=0x%x,scst=%d/%d/%d %d:%d:%d±%d,uid=%u,total=%u,seg=%u,dataLen=%d,data=%s",
  51. msg->index,msg->oa,msg->tooa,msg->status,msg->fo,msg->dcs,
  52. msg->scts.uYear,msg->scts.uMonth,msg->scts.uDay,msg->scts.uHour,msg->scts.uMinute,msg->scts.uSecond,msg->scts.iZone,
  53. msg->uid,msg->msg_total,msg->msg_seg,msg->dataLen,msg->data);
  54. break;
  55. }
  56. case QL_SMS_LIST_END_IND:
  57. {
  58. QL_SMS_LOG("QL_SMS_LIST_END_IND");
  59. ql_rtos_semaphore_release(sms_list_sem);
  60. break;
  61. }
  62. case QL_SMS_MEM_FULL_IND:
  63. {
  64. ql_sms_new_s *msg = (ql_sms_new_s *)ctx;
  65. QL_SMS_LOG("QL_SMS_MEM_FULL_IND sim=%d, memory=%d",nSim,msg->mem);
  66. break;
  67. }
  68. case QL_SMS_REPORT_IND:
  69. {
  70. ql_sms_report_s *msg = (ql_sms_report_s *)ctx;
  71. QL_SMS_LOG("QL_SMS_REPORT_IND sim=%d, fo=%02x, mr=%02x, st=%02x, ra_size=%d, tora=%02x, ra=%s, scst=%d/%d/%d %d:%d:%d,%d, dt=%d/%d/%d %d:%d:%d,%d", \
  72. nSim, msg->fo, msg->mr, msg->st, msg->ra_size, msg->tora, (char *)&msg->ra, \
  73. msg->scts.uYear,msg->scts.uMonth,msg->scts.uDay,msg->scts.uHour,msg->scts.uMinute,msg->scts.uSecond,msg->scts.iZone, \
  74. msg->dt.uYear,msg->dt.uMonth,msg->dt.uDay,msg->dt.uHour,msg->dt.uMinute,msg->dt.uSecond,msg->dt.iZone);
  75. break;
  76. }
  77. default :
  78. break;
  79. }
  80. }
  81. void sms_demo_task(void * param)
  82. {
  83. char addr[20] = {0};
  84. uint8_t nSim = 0;
  85. QL_SMS_LOG("enter");
  86. ql_sms_callback_register(user_sms_event_callback);
  87. //wait sms ok
  88. if(ql_rtos_semaphore_wait(sms_init_sem, QL_WAIT_FOREVER)){
  89. QL_SMS_LOG("Waiting for SMS init timeout");
  90. }
  91. if(QL_SMS_SUCCESS == ql_sms_get_center_address(nSim, addr, sizeof(addr))){
  92. QL_SMS_LOG("ql_sms_get_center_address OK, addr=%s",addr);
  93. }else{
  94. QL_SMS_LOG("ql_sms_get_center_address FAIL");
  95. }
  96. ql_sms_set_code_mode(QL_CS_GSM);
  97. #if 0
  98. //Send English text message
  99. if(QL_SMS_SUCCESS == ql_sms_send_msg(nSim,"+8610086","~!@#$%^&*()_+<>?:{}|", GSM)){
  100. QL_SMS_LOG("ql_sms_send_msg OK");
  101. }else{
  102. QL_SMS_LOG("ql_sms_send_msg FAIL");
  103. }
  104. //Send messages in Chinese and English. (Need use UTF8 encoding to open sms_demo.c for chinese.)
  105. if(QL_SMS_SUCCESS == ql_sms_send_msg(nSim,"+8610086","hello,你好", UCS2)){
  106. QL_SMS_LOG("ql_sms_send_msg OK");
  107. }else{
  108. QL_SMS_LOG("ql_sms_send_msg FAIL");
  109. }
  110. #endif
  111. //Get how many SMS messages can be stored in the SIM card in total and how much storage is used
  112. #if 1
  113. ql_sms_stor_info_s stor_info;
  114. if(QL_SMS_SUCCESS == ql_sms_get_storage_info(nSim,&stor_info)){
  115. QL_SMS_LOG("ql_sms_get_storage_info OK");
  116. QL_SMS_LOG("SM used=%u,SM total=%u,SM unread=%u,ME used=%u,ME total=%u,ME unread=%u, newSmsStorId=%u",
  117. stor_info.usedSlotSM,stor_info.totalSlotSM,stor_info.unReadRecordsSM,
  118. stor_info.usedSlotME,stor_info.totalSlotME,stor_info.unReadRecordsME,
  119. stor_info.newSmsStorId);
  120. }else{
  121. QL_SMS_LOG("ql_sms_get_storage_info FAIL");
  122. }
  123. #endif
  124. //The first parameter specifies that SMS messages are read from SM
  125. ql_sms_set_storage(nSim,SM,SM,SM);
  126. //Read one messages in SIM
  127. #if 1
  128. uint16_t msg_len = 512;
  129. ql_sms_mem_info_t sms_mem = {0};
  130. ql_sms_recv_s *sms_recv = NULL;
  131. char *msg = malloc(msg_len);
  132. if(msg == NULL){
  133. QL_SMS_LOG("malloc ql_sms_msg_s fail");
  134. goto exit;
  135. }
  136. memset(msg ,0 ,msg_len);
  137. ql_sms_get_storage(nSim, &sms_mem);
  138. QL_SMS_LOG("mem1=%d, mem2=%d, mem3=%d", sms_mem.mem1, sms_mem.mem2, sms_mem.mem3);
  139. //Read SMS messages as text
  140. if(QL_SMS_SUCCESS == ql_sms_read_msg(nSim,2, msg, msg_len, TEXT)){
  141. // QL_SMS_LOG("read msg OK, msg=%s", msg);
  142. }else{
  143. QL_SMS_LOG("read sms FAIL");
  144. }
  145. //Read SMS messages as pdu
  146. memset(msg ,0 ,msg_len);
  147. if(QL_SMS_SUCCESS == ql_sms_read_msg(nSim,2, msg, msg_len, PDU)){
  148. // QL_SMS_LOG("read msg OK, msg=%s", msg);
  149. }else{
  150. QL_SMS_LOG("read sms FAIL");
  151. }
  152. if(msg)free(msg);
  153. //Read SMS messages as text
  154. sms_recv = (ql_sms_recv_s *)calloc(1,sizeof(ql_sms_recv_s));
  155. if(sms_recv == NULL)
  156. {
  157. QL_SMS_LOG("calloc FAIL");
  158. goto exit;
  159. }
  160. if(QL_SMS_SUCCESS == ql_sms_read_msg_ex(nSim,2, TEXT,sms_recv)){
  161. QL_SMS_LOG("index=%d,os=%s,tooa=%u,status=%d,fo=0x%x,dcs=0x%x,scst=%d/%d/%d %d:%d:%d±%d,uid=%u,total=%u,seg=%u,dataLen=%d,data=%s",
  162. sms_recv->index,sms_recv->oa,sms_recv->tooa,sms_recv->status,sms_recv->fo,sms_recv->dcs,
  163. sms_recv->scts.uYear,sms_recv->scts.uMonth,sms_recv->scts.uDay,sms_recv->scts.uHour,sms_recv->scts.uMinute,sms_recv->scts.uSecond,sms_recv->scts.iZone,
  164. sms_recv->uid,sms_recv->msg_total,sms_recv->msg_seg,sms_recv->dataLen,sms_recv->data);
  165. }else{
  166. QL_SMS_LOG("read sms FAIL");
  167. }
  168. if(sms_recv)free(sms_recv);
  169. #endif
  170. //Read all message in SIM
  171. #if 1
  172. ql_sms_set_storage(nSim,SM,SM,SM);//set sms storage as SIM.
  173. if(QL_SMS_SUCCESS == ql_sms_read_msg_list(nSim, TEXT)){
  174. if(ql_rtos_semaphore_wait(sms_list_sem, QL_WAIT_FOREVER)){
  175. QL_SMS_LOG("sms_list_sem time out");
  176. }
  177. }else{
  178. QL_SMS_LOG("get msg list FAIL");
  179. }
  180. #endif
  181. //Delete message.
  182. #if 0
  183. if(QL_SMS_SUCCESS == ql_sms_delete_msg_ex(nSim, 0, QL_SMS_DEL_ALL)){
  184. QL_SMS_LOG("delete msg OK");
  185. }else{
  186. QL_SMS_LOG("delete sms FAIL");
  187. }
  188. #endif
  189. goto exit;
  190. exit:
  191. ql_rtos_task_delete(NULL);
  192. }
  193. QlOSStatus ql_sms_app_init(void)
  194. {
  195. QlOSStatus err = QL_OSI_SUCCESS;
  196. err = ql_rtos_task_create(&sms_task, 4096, APP_PRIORITY_NORMAL, "QsmsApp", sms_demo_task, NULL, 2);
  197. if(err != QL_OSI_SUCCESS)
  198. {
  199. QL_SMS_LOG("sms_task created failed, ret = 0x%x", err);
  200. }
  201. err = ql_rtos_semaphore_create(&sms_init_sem, 0);
  202. if(err != QL_OSI_SUCCESS)
  203. {
  204. QL_SMS_LOG("sms_init_sem created failed, ret = 0x%x", err);
  205. }
  206. err = ql_rtos_semaphore_create(&sms_list_sem, 0);
  207. if(err != QL_OSI_SUCCESS)
  208. {
  209. QL_SMS_LOG("sms_init_sem created failed, ret = 0x%x", err);
  210. }
  211. return err;
  212. }