luat_sms.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef LUAT_SMS_H
  22. #define LUAT_SMS_H
  23. #include "luat_base.h"
  24. #ifndef bool
  25. #define bool uint8_t
  26. #endif
  27. /**
  28. * @defgroup LUAT_SMS SMS接口
  29. * @{
  30. */
  31. #define LUAT_MSG_MAX_ADDR_LEN 80
  32. #define LUAT_SMS_MAX_TXT_SIZE 640
  33. #define LUAT_SMS_MAX_PDU_SIZE 180
  34. #define LUAT_SMS_MAX_LENGTH_OF_ADDRESS_VALUE 40
  35. #define LUAT_SMS_MAX_ADDR_STR_MAX_LEN ((LUAT_SMS_MAX_LENGTH_OF_ADDRESS_VALUE + 1) * 4)
  36. #define LUAT_SMS_SHORT_MSG_PDU_SIZE (140)
  37. #define LUAT_SMS_LONG_MSG_PDU_SIZE (134)
  38. #define LUAT_SMS_CODE_7BIT 0
  39. #define LUAT_SMS_CODE_8BIT 4
  40. #define LUAT_SMS_CODE_UCS2 8
  41. typedef void (*luat_sms_handle_recv_cb)(uint8_t event, void* param);
  42. typedef void (*luat_sms_handle_send_cb)(int ret);
  43. typedef enum
  44. {
  45. SMS_SEND_OK = 0,
  46. SMS_ME_FAILURE = 300,
  47. SMS_SERVICE_OF_ME_RESV,
  48. SMS_OPERATION_NOT_ALLOWED,
  49. SMS_OPERATION_NOT_SUPPORTED,
  50. SMS_INVALID_PDU_MODE_PARAMETER,
  51. SMS_INVALID_TEXT_MODE_PARAMETER,
  52. SMS_USIM_NOT_INSERTED = 310,
  53. SMS_USIM_PIN_REQUIRED,
  54. SMS_PHSIM_PIN_REQUIRED,
  55. SMS_USIM_FAILURE,
  56. SMS_USIM_BUSY,
  57. SMS_USIM_WRONG,
  58. SMS_USIM_PUK_REQUIRED,
  59. SMS_USIM_PIN2_REQUIRED,
  60. SMS_USIM_PUK2_REQUIRED,
  61. SMS_MEMORY_FAILURE = 320,
  62. SMS_INVALID_MEM_INDEX,
  63. SMS_MEM_FULL,
  64. SMS_SMSC_ADDR_UNKNOWN = 330,
  65. SMS_NO_NETWORK_SERVICE,
  66. SMS_NETWORK_TIMEOUT,
  67. SMS_NO_CNMA_ACK_EXPECTED = 340,
  68. SMS_UNKNOWN_ERROR = 500,
  69. SMS_INVALID_DATA = 550,
  70. SMS_UNSUPPORT_TEXT_WITH_CHINESE = 555,
  71. SMS_MAX_ERROR = 0xFFFF
  72. }luat_sms_send_ret_code_e;
  73. typedef struct
  74. {
  75. luat_sms_handle_recv_cb cb;
  76. luat_sms_handle_send_cb send_cb;
  77. }luat_sms_cb_cfg_t;
  78. typedef struct
  79. {
  80. uint8_t year;
  81. uint8_t month;
  82. uint8_t day;
  83. uint8_t hour;
  84. uint8_t minute;
  85. uint8_t second;
  86. uint8_t tz; /* time zone */
  87. uint8_t tz_sign; /* '+'/'-' */
  88. }luat_sms_recv_msg_time_t;
  89. typedef struct
  90. {
  91. uint8_t type;
  92. uint8_t msg_class;
  93. uint8_t alpha_bet;
  94. uint8_t indication;
  95. uint8_t dcs;
  96. }luat_sms_recv_msg_dcs_t;
  97. //接受的短信信息结构体
  98. typedef struct
  99. {
  100. uint16_t pdu_length;//PDU 长度
  101. uint16_t sms_length;//TEXT 的长度
  102. luat_sms_recv_msg_time_t time;//时间
  103. luat_sms_recv_msg_dcs_t dcs_info;//Data Coding Scheme
  104. char pdu_data[LUAT_SMS_MAX_TXT_SIZE + 1];//PDU 数据
  105. uint8_t sms_buffer[LUAT_SMS_MAX_TXT_SIZE + 1];//TEXT 数据
  106. uint8_t sc_address[LUAT_MSG_MAX_ADDR_LEN + 1];//中心地址
  107. uint8_t phone_address[LUAT_MSG_MAX_ADDR_LEN + 1];//来电号码
  108. uint8_t refNum;
  109. uint8_t maxNum;
  110. uint8_t seqNum;
  111. }luat_sms_recv_msg_t;
  112. typedef struct
  113. {
  114. size_t phone_len; // 电话号码长度
  115. size_t payload_len; // 待组包的短信数据长度
  116. const char *phone; // 电话号码
  117. uint8_t pdu_buf[320]; // 组包后的PDU短信
  118. uint8_t payload_buf[200]; // 待组包的短信数据
  119. uint8_t auto_phone; // 是否自动处理电话号码
  120. uint8_t maxNum; // 短信最大条数, 长短信用
  121. uint8_t seqNum; // 当前短信序号
  122. uint8_t refNum;
  123. }luat_sms_pdu_packet_t;
  124. /**
  125. * @brief 初始化短信
  126. */
  127. void luat_sms_init(void);
  128. /**
  129. * @brief 发送短信
  130. * @param p_input 短信的内容(当 is_pdu = false 时, 只支持英文,数字以及常用符号)
  131. * @param p_des 接收短信的手机号
  132. * @param is_pdu 是否是PDU格式的短信(当 false 时, 有效参数为 p_input & pdes, 当 true 时, 有效参数为 p_input & pudLen)
  133. * @param input_pdu_len PDU格式短信的长度,注意和p_input长度没有关系
  134. * @return 0成功,-1失败
  135. */
  136. int luat_sms_send_msg(uint8_t *p_input, char *p_des, bool is_pdu, int input_pdu_len);
  137. /**
  138. * @brief 发送PDU短信
  139. * @param pdu_data PDU格式的短信
  140. * @param pdu_len PDU格式短信的长度
  141. * @return 0成功,-1失败
  142. */
  143. int luat_sms_send_msg_v2(uint8_t *pdu_data, size_t pdu_len);
  144. /**
  145. * @brief 接受短信回调
  146. * @param callback_fun 回调函数
  147. */
  148. void luat_sms_recv_msg_register_handler(luat_sms_handle_recv_cb callback_fun);
  149. /**
  150. * @brief 发送短信回调
  151. * @param callback_fun 回调函数
  152. */
  153. void luat_sms_send_msg_register_handler(luat_sms_handle_send_cb callback_fun);
  154. /**
  155. * @brief 打包pdu数据
  156. * @param packet
  157. */
  158. int luat_sms_pdu_packet(luat_sms_pdu_packet_t *packet);
  159. uint8_t luat_sms_gsm_to_ascii(uint8_t *gsm_data, uint8_t length);
  160. uint16_t luat_sms_decode_7bit_data(uint8_t *src, uint16_t src_len, uint8_t *dst, uint16_t dst_len, uint16_t shift_bits);
  161. void luat_sms_pdu_message_unpack(luat_sms_recv_msg_t *msg_info, uint8_t *pdu_data, int pdu_len);
  162. /**@}*/
  163. #endif