sms.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /******************************************************************************/
  2. /* */
  3. /* Copyright 2023 by AICXTEK TECHNOLOGIES CO.,LTD. All rights reserved. */
  4. /* */
  5. /******************************************************************************/
  6. /**
  7. * DESCRIPTION
  8. *
  9. * This file is sample code of sms
  10. */
  11. /*********************
  12. * INCLUDES
  13. *********************/
  14. #include "aic_type.h"
  15. #include "aic_srv_tele.h"
  16. /**********************
  17. * STATIC FUNCTIONS
  18. **********************/
  19. static int32_t handleSmsCallback(void *p_param, uint32_t size)
  20. {
  21. int32_t ret = -1;
  22. uint32_t event = TS_EVENT_SMS_MAX;
  23. if(NULL == p_param) {
  24. alog_error("[%s][Error]p_param is NULL.", __func__);
  25. return -1;
  26. }
  27. event = *(uint32_t *)p_param;
  28. alog_info("[%s][Info]event id is 0x%x.", __func__, event);
  29. switch(event) {
  30. case TS_EVENT_SMS_NEW_IND: /* new sms */
  31. {
  32. ts_sms_new_ind_t *p_ind = (ts_sms_new_ind_t *)p_param;
  33. alog_info("[%s][Info]TS_EVENT_SMS_NEW_IND, index:%d, status:%d \n", __func__, p_ind->index, p_ind->status);
  34. /* read sms according to index */
  35. ret = aic_srv_tele_sms_read_in_sim(p_ind->card_id, p_ind->index, handleSmsCallback);
  36. alog_info("[%s][Info](TS_EVENT_SMS_NEW_IND)aic_srv_tele_sms_read_in_sim return %d.", __func__, ret);
  37. break;
  38. }
  39. case TS_EVENT_SMS_READ_IN_SIM_RESP:
  40. {
  41. ts_sms_read_in_sim_resp_t *p_sim = (ts_sms_read_in_sim_resp_t *)p_param;
  42. alog_info("[%s][Info]TS_EVENT_SMS_READ_IN_SIM_RESP, index:%d, textstart:0x%x \n", __func__, p_sim->index, p_sim->text_start);
  43. if (p_sim->base_info.err_code == 0) {
  44. alog_info("[%s][Info]index(%d), sms_class(%d), ymd(%d-%d-%d), hms(%d:%d:%d), number(%s), content(%s)",
  45. __func__,
  46. p_sim->index,
  47. p_sim->sms_class,
  48. p_sim->time_stamp.year,
  49. p_sim->time_stamp.month,
  50. p_sim->time_stamp.day,
  51. p_sim->time_stamp.hour,
  52. p_sim->time_stamp.min,
  53. p_sim->time_stamp.sec,
  54. p_sim->number,
  55. (char *)&p_sim->text_start);
  56. }
  57. }
  58. break;
  59. case TS_EVENT_SMS_GET_SMSC_RESP:
  60. {
  61. ts_sms_get_smsc_resp_t* p_sms_smsc = (ts_sms_get_smsc_resp_t*)(p_param);
  62. alog_info("[%s][Info]TS_EVENT_SMS_GET_SMSC_RESP, p_sms_smsc->smsc is %s. \n", __func__, p_sms_smsc->smsc);
  63. }
  64. break;
  65. case TS_EVENT_SMS_SEND_RESP:
  66. {
  67. ts_sms_send_resp_t* p_sms_send = (ts_sms_send_resp_t*)(p_param);
  68. alog_info("[%s][Info]TS_EVENT_SMS_SEND_RESP, p_sms_send->base_info.err_code = %d.", __func__, p_sms_send->base_info.err_code);
  69. if (p_sms_send->base_info.err_code == 0){/* sms send successfully */
  70. alog_info("[%s][Info]TS_EVENT_SMS_SEND_RESP, p_sms_send->index = %d.", __func__, p_sms_send->index);
  71. }
  72. }
  73. break;
  74. case TS_EVENT_SMS_DEL_IN_SIM_RESP:
  75. {
  76. ts_sms_del_in_sim_resp_t *p_sms_del = (ts_sms_del_in_sim_resp_t *)p_param;
  77. alog_info("[%s][Info]TS_EVENT_SMS_DEL_IN_SIM_RESP, p_sms_del->base_info.err_code = %d.", __func__, p_sms_del->base_info.err_code);
  78. }
  79. break;
  80. case TS_EVENT_SMS_LOAD_FROM_SIM_RESP: {
  81. ts_sms_load_from_sim_resp_t *p_load_sms = (ts_sms_load_from_sim_resp_t *)p_param;
  82. uint16_t index = 0;
  83. alog_info("[%s]TS_EVENT_SMS_LOAD_FROM_SIM_RESP, card_id = %d, err_code = %d, is_full = %d, count = %d.",
  84. __func__,
  85. p_load_sms->base_info.card_id,
  86. p_load_sms->base_info.err_code,
  87. p_load_sms->is_full,
  88. p_load_sms->count);
  89. if (0 == p_load_sms->base_info.err_code) {
  90. /* load sim sms success. */
  91. if (true == p_load_sms->is_full) {
  92. /* If the sms in sim is full, user could do something here.(such as pop up some UI tips or delete some messages.) */
  93. }
  94. /* when user receive this response event, call this api to get sms content */
  95. for (index = 0; index < p_load_sms->count; index++) {
  96. ret = aic_srv_tele_sms_read_in_sim(p_load_sms->base_info.card_id, index + 1, handleSmsCallback);
  97. alog_info("[%s]aic_srv_tele_sms_read_in_sim[index = %d] return %d.", __func__, index, ret);
  98. }
  99. } else {
  100. /* load sim sms failed, and user could pop up some UI tips. */
  101. }
  102. break;
  103. }
  104. default:
  105. break;
  106. }
  107. return 0;
  108. }
  109. static int32_t handleSimCallback(void *p_param, uint32_t size)
  110. {
  111. ts_event_sim_t *p_sim_event = NULL;
  112. int ret_audio = 0;
  113. if(NULL == p_param) {
  114. alog_error("[%s][Err]p_param is null.", __func__);
  115. return -1;
  116. }
  117. p_sim_event = (ts_event_sim_t *)p_param;
  118. switch (p_sim_event->event_id) {
  119. case TS_EVENT_SIM_STATUS_UPDATED: {
  120. ts_sim_status_updated_t *p_sim_status = NULL;
  121. p_sim_status = (ts_sim_status_updated_t *)p_param;
  122. alog_info("[%s][Info]TS_EVENT_SIM_STATUS_UPDATED(sim_exist_status = %d, cur_card_id = %d).\r\n", __func__, p_sim_status->sim_exist_status, p_sim_status->cur_card_id);
  123. if (0 != p_sim_status->sim_exist_status) {
  124. /* load sim sms list */
  125. aic_srv_tele_sms_load_from_sim(p_sim_status->cur_card_id, handleSmsCallback);
  126. }
  127. break;
  128. }
  129. default:
  130. break;
  131. }
  132. return 0;
  133. }
  134. /**********************
  135. * GLOBAL FUNCTIONS
  136. **********************/
  137. void sms_send(char *p_number, char *p_text)
  138. {
  139. int32_t ret = -1;
  140. ts_sms_send_info_t sms_send_info = {0};
  141. if (NULL == p_number) {
  142. alog_error("[%s][Error]p_number is NULL.", __func__);
  143. return;
  144. }
  145. if (NULL == p_text) {
  146. alog_error("[%s][Error]p_text is NULL.", __func__);
  147. return;
  148. }
  149. sms_send_info.p_number = malloc(sizeof(char)*20 + 1);
  150. if (NULL == sms_send_info.p_number) {
  151. alog_error("[%s][Error]sms_send_info.p_number malloc fail.", __func__);
  152. return;
  153. }
  154. memset(sms_send_info.p_number, 0, sizeof(char)*20 + 1);
  155. memcpy(sms_send_info.p_number, p_number, min(20, strlen(p_number)));
  156. sms_send_info.p_smsc = NULL;
  157. sms_send_info.p_text = malloc(sizeof(char)*100 + 1);
  158. if (NULL == sms_send_info.p_text) {
  159. free(sms_send_info.p_number);
  160. sms_send_info.p_number = NULL;
  161. alog_error("[%s][Error]sms_send_info.p_text malloc fail.", __func__);
  162. return;
  163. }
  164. memset(sms_send_info.p_text, 0, sizeof(char)*100 + 1);
  165. memcpy(sms_send_info.p_text, p_text, min(100, strlen(p_text)));
  166. alog_info("[%s][Info]number: %s, text: %s.", __func__, sms_send_info.p_number, sms_send_info.p_text);
  167. ret = aic_srv_tele_sms_send(0, &sms_send_info, handleSmsCallback);
  168. alog_info("[%s][Info]aic_srv_tele_sms_send return %d.", __func__, ret);
  169. if (NULL != sms_send_info.p_number) {
  170. free(sms_send_info.p_number);
  171. sms_send_info.p_number = NULL;
  172. }
  173. if (NULL != sms_send_info.p_text) {
  174. free(sms_send_info.p_text);
  175. sms_send_info.p_text = NULL;
  176. }
  177. }
  178. void sms_init( void )
  179. {
  180. int32_t sms_handle = 0;
  181. int32_t sim_handle = 0;
  182. aic_srv_bus_info_t bus_info = {0};
  183. /* register sms callback, this callback receive global events(eg. sms new ind, status report, etc.)
  184. and response events(eg. send response, get smsc response, etc.)*/
  185. bus_info.p_client_name = "sms_app";
  186. sms_handle = aic_srv_tele_sms_register(&bus_info, handleSmsCallback);
  187. alog_info("[%s][Info]aic_srv_tele_sms_register return handle 0x%x.", __func__, sms_handle);
  188. /* register sim callback, when sim ready, call aic_srv_tele_sms_load_from_sim to load sim sms. */
  189. sim_handle = aic_srv_tele_sim_register(&bus_info, handleSimCallback);
  190. }