hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 1 | #include <stdbool.h> |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
| 5 | #include <dlfcn.h> |
| 6 | #include <time.h> |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 7 | #include "gsw_nw_interface.h" |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 8 | #include "gsw_log_interface.h" |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 9 | typedef unsigned short uint16_t; |
| 10 | typedef unsigned int uint32_t; |
| 11 | typedef unsigned char uint8_t; |
| 12 | typedef unsigned short uint16; |
| 13 | typedef uint32_t sms_client_handle_type; |
| 14 | typedef int kal_int32; |
| 15 | |
| 16 | |
| 17 | //qser include |
| 18 | |
| 19 | |
| 20 | |
| 21 | //mbtk include |
| 22 | typedef void (*mbtk_info_callback_func)(const void* data, int data_len); |
| 23 | typedef unsigned char uint8; |
| 24 | |
| 25 | typedef struct |
| 26 | { |
| 27 | int client_fd; |
| 28 | pthread_t read_thread_id; |
| 29 | int exit_fd[2]; |
| 30 | bool is_waitting; |
| 31 | pthread_cond_t cond; |
| 32 | pthread_mutex_t mutex; |
| 33 | |
| 34 | pthread_mutex_t send_mutex; |
| 35 | |
| 36 | // Temp response data. |
| 37 | uint16 info_err; |
| 38 | uint16 data_len; |
| 39 | void *data; |
| 40 | |
| 41 | //mbtk wyq for server_ready_status add start |
| 42 | char server_ready_status; |
| 43 | //mbtk wyq for server_ready_status add end |
| 44 | |
| 45 | mbtk_info_callback_func net_state_cb; |
| 46 | mbtk_info_callback_func call_state_cb; |
| 47 | mbtk_info_callback_func sms_state_cb; |
| 48 | mbtk_info_callback_func radio_state_cb; |
| 49 | mbtk_info_callback_func sim_state_cb; |
| 50 | mbtk_info_callback_func pdp_state_cb; |
| 51 | //add signal by xr |
| 52 | mbtk_info_callback_func signal_state_cb; |
| 53 | } mbtk_info_handle_t; |
| 54 | |
| 55 | // PDU include |
| 56 | enum EnumDCS { |
| 57 | BIT7 = 0, // GSM 字符集 |
| 58 | BIT8 = 1, // ASCII字符集 |
| 59 | UCS2 = 2 // Unicode 字符集 |
| 60 | }; |
| 61 | |
| 62 | struct SMS_Struct { |
| 63 | char *SCA; // 服务中心地址 |
| 64 | char *OA; // 发送方地址 |
| 65 | char *SCTS; // 服务中心时间戳 |
| 66 | struct UDHS *UDH; // 用户数据头 |
| 67 | char *UD; // 用户数据 |
| 68 | |
| 69 | bool RP; // 应答路径 |
| 70 | bool UDHI; // 用户数据头标识 |
| 71 | bool SRI; // 状态报告指示 |
| 72 | bool MMS; // 更多信息发送 |
| 73 | int MTI; // 信息类型指示 |
| 74 | |
| 75 | char PID; // PID 协议标识 |
| 76 | |
| 77 | enum EnumDCS DCS; // 数据编码方案 |
| 78 | bool TC; // 文本压缩指示 0: 未压缩 1:压缩 |
| 79 | int MC; // 消息类型 -1: 无 1:移动设备特定类型 2:SIM特定类型 3:终端设备特定类型 |
| 80 | |
| 81 | }; |
| 82 | |
| 83 | struct PDUS { |
| 84 | unsigned int count; |
| 85 | char **PDU; |
| 86 | }; |
| 87 | |
| 88 | enum MDAPI_RET_e { |
| 89 | MDAPI_RET_SUCCESS = 0, |
| 90 | MDAPI_RET_ERROR = 1, |
| 91 | MDAPI_RET_TIMEOUT = 2, |
| 92 | MDAPI_RET_NOT_SUPPORT = 3, |
| 93 | }; |
| 94 | |
| 95 | #define MDAPI_TIME_STR_SIZE 22 |
| 96 | #define MDAPI_PHONE_NUMBER_SIZE 32 |
| 97 | #define MDAPI_MAX_PDU_SIZE 512 |
| 98 | #define MSM_NUMBER_MAX 2048+1 |
| 99 | #define RES_NUM_MIN 128 |
| 100 | #define MIN_MSM_PARAM_NUM 4 |
| 101 | #define MIN_IMS_MSM_PARAM_NUM 6 |
| 102 | #define MIN_WRITMSM_PARAM_NUM 5 |
| 103 | #define MSG_MAX_LEN 1024 |
| 104 | #define TELEPHONNUM_LEN 64 |
| 105 | #define STORAGSMS_MAX_SIZE 128 |
lichengzhang | 46a546c | 2025-06-17 16:24:55 +0800 | [diff] [blame] | 106 | #define SMSC_MAX_LEN 100 |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 107 | #define SMS_NUM_MAX 255 |
| 108 | #define MAX_OUT_SIZE 512 |
| 109 | #define MAX_PDU_SIZE 512 |
| 110 | #define MAX_DATE_SIZE 32 |
| 111 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 112 | GSW_SMS_Callback_fun gsw_sms_callback = NULL; |
| 113 | |
| 114 | #define lib_mbtk_path "/lib/libmbtk_lib.so" |
| 115 | #define lib_qser_sms_path "/lib/liblynq-qser-sms.so" |
| 116 | |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 117 | // 短信分片管理器结构体 |
| 118 | typedef struct { |
| 119 | char msg_id[64]; // 短信唯一标识(号码+日期) |
| 120 | int total_segments; // 总分片数 |
| 121 | int received_count; // 已接收分片数 |
| 122 | char** segments; // 分片内容数组 |
| 123 | time_t last_received; // 最后接收时间 |
| 124 | } SmsReassembler; |
| 125 | |
| 126 | #define MAX_SMS_REASSEMBLERS 10 // 最大同时处理的短信数量 |
| 127 | #define MAX_SEGMENTS 255 // 最大分片数 |
| 128 | #define SMS_TIMEOUT 300 // 5分钟超时(秒) |
| 129 | static SmsReassembler g_sms_reassemblers[MAX_SMS_REASSEMBLERS] = {0}; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 130 | |
| 131 | static mbtk_info_handle_t* (*mbtk_info_handle_get)(void); |
| 132 | int (*mbtk_sms_state_change_cb_reg)(mbtk_info_handle_t* handle, mbtk_info_callback_func cb); |
| 133 | static int (*mbtk_info_handle_free)(mbtk_info_handle_t** handle); |
| 134 | struct SMS_Struct (*PDUDecoding)(const char *data); |
| 135 | char *(*SCADecoding)(const char *data, int *EndIndex); |
| 136 | char *(*SCAEncoding)(char *SCA); |
| 137 | struct PDUS *(*PDUEncoding)(char *SCA, char *DA, char *UDC, struct UDHS *udhs); |
| 138 | int (*mbtk_sms_cmgf_set)(mbtk_info_handle_t* handle, int mode); |
| 139 | int (*mbtk_sms_cmgs_set)(mbtk_info_handle_t* handle, char * cmgs, char *resp); |
| 140 | int (*mbtk_sms_csca_set)(mbtk_info_handle_t* handle, char * csca); |
| 141 | int (*mbtk_sms_csca_get)(mbtk_info_handle_t* handle, char *buf); |
| 142 | int (*mbtk_sms_cnmi_set)(mbtk_info_handle_t* handle); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 143 | // static void (*mbtk_log)(int level, const char *format, ...); |
| 144 | // static void (*mbtk_log_init)(char *path, char *tag); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 145 | int (*smsPduEncode)(const char *smsc, const char *da_num, const char *msg, int charset, char *smsc_pdu, char **pdu); |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 146 | int (*smsPduDecode)(const char *pdu_str, int pdu_len, char *da_num, char *smsc, char *msg, int *charset, int *curr_pack, int *total_pack, char *date, int *ref_num); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 147 | kal_int32 (*_mdapi_sms_get_msg_num)(const char *msg, int charset, kal_int32 *msg_num, kal_int32 *msg_len); |
| 148 | |
l.yang | 6a42e4d | 2025-05-28 01:04:20 -0700 | [diff] [blame] | 149 | #define GSW_SMS "[HAL][GSW_SMS]" |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 150 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 151 | static mbtk_info_handle_t* sms_info_handle = NULL; |
| 152 | static sms_client_handle_type handle; |
| 153 | static char sms_center_address[SMSC_MAX_LEN] = {0}; |
| 154 | static void *dlHandle_mbtk; |
| 155 | static int init_flag = 0; |
| 156 | |
| 157 | static int mbtk_sms_import() |
| 158 | { |
| 159 | dlHandle_mbtk = dlopen(lib_mbtk_path, RTLD_NOW); |
| 160 | if (dlHandle_mbtk == NULL) |
| 161 | { |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 162 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 163 | } |
| 164 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 165 | // mbtk_log_init = (void (*)(char *path, char *tag))dlsym(dlHandle_mbtk, "mbtk_log_init"); |
| 166 | // if (mbtk_log_init == NULL) |
| 167 | // { |
| 168 | // return GSW_HAL_NORMAL_FAIL; |
| 169 | // } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 170 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 171 | // mbtk_log = (void (*)(int level, const char *format, ...))dlsym(dlHandle_mbtk, "mbtk_log"); |
| 172 | // if (mbtk_log == NULL) |
| 173 | // { |
| 174 | // return GSW_HAL_NORMAL_FAIL; |
| 175 | // } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 176 | |
| 177 | mbtk_info_handle_get = (mbtk_info_handle_t* (*)(void))dlsym(dlHandle_mbtk, "mbtk_info_handle_get"); |
| 178 | if (mbtk_info_handle_get == NULL) |
| 179 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 180 | LOGE(GSW_SMS,"mbtk_info_handle_get dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 181 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | mbtk_sms_state_change_cb_reg = (int (*)(mbtk_info_handle_t* handle, mbtk_info_callback_func cb))dlsym(dlHandle_mbtk, "mbtk_sms_state_change_cb_reg"); |
| 185 | if (mbtk_sms_state_change_cb_reg == NULL) |
| 186 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 187 | LOGE(GSW_SMS,"mbtk_sms_state_change_cb_reg dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 188 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | mbtk_info_handle_free = (int (*)(mbtk_info_handle_t** handle))dlsym(dlHandle_mbtk, "mbtk_info_handle_free"); |
| 192 | if (mbtk_info_handle_free == NULL) |
| 193 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 194 | LOGE(GSW_SMS,"mbtk_info_handle_free dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 195 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | PDUDecoding = (struct SMS_Struct (*)(const char *data))dlsym(dlHandle_mbtk, "PDUDecoding"); |
| 199 | if (PDUDecoding == NULL) |
| 200 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 201 | LOGE(GSW_SMS,"PDUDecoding dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 202 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | SCADecoding = (char *(*)(const char *data, int *EndIndex))dlsym(dlHandle_mbtk, "SCADecoding"); |
| 206 | if (SCADecoding == NULL) |
| 207 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 208 | LOGE(GSW_SMS,"SCADecoding dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 209 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | SCAEncoding = (char *(*)(char *SCA))dlsym(dlHandle_mbtk, "SCAEncoding"); |
| 213 | if (SCAEncoding == NULL) |
| 214 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 215 | LOGE(GSW_SMS,"SCAEncoding dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 216 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | PDUEncoding = (struct PDUS *(*)(char *SCA, char *DA, char *UDC, struct UDHS *udhs))dlsym(dlHandle_mbtk, "PDUEncoding"); |
| 220 | if (PDUEncoding == NULL) |
| 221 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 222 | LOGE(GSW_SMS,"PDUEncoding dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 223 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | mbtk_sms_cmgf_set = (int (*)(mbtk_info_handle_t* handle, int mode))dlsym(dlHandle_mbtk, "mbtk_sms_cmgf_set"); |
| 227 | if (mbtk_sms_cmgf_set == NULL) |
| 228 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 229 | LOGE(GSW_SMS,"mbtk_sms_cmgf_set dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 230 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | mbtk_sms_cmgs_set = (int (*)(mbtk_info_handle_t* handle, char * cmgs, char *resp))dlsym(dlHandle_mbtk, "mbtk_sms_cmgs_set"); |
| 234 | if (mbtk_sms_cmgs_set == NULL) |
| 235 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 236 | LOGE(GSW_SMS,"mbtk_sms_cmgs_set dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 237 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | mbtk_sms_csca_set = (int (*)(mbtk_info_handle_t* handle, char * csca))dlsym(dlHandle_mbtk, "mbtk_sms_csca_set"); |
| 241 | if (mbtk_sms_csca_set == NULL) |
| 242 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 243 | LOGE(GSW_SMS,"mbtk_sms_csca_set dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 244 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | mbtk_sms_csca_get = (int (*)(mbtk_info_handle_t* handle, char *buf))dlsym(dlHandle_mbtk, "mbtk_sms_csca_get"); |
| 248 | if (mbtk_sms_csca_get == NULL) |
| 249 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 250 | LOGE(GSW_SMS,"mbtk_sms_csca_get dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 251 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | mbtk_sms_cnmi_set = (int (*)(mbtk_info_handle_t* handle))dlsym(dlHandle_mbtk, "mbtk_sms_cnmi_set"); |
| 255 | if (mbtk_sms_cnmi_set == NULL) |
| 256 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 257 | LOGE(GSW_SMS,"mbtk_sms_cnmi_set dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 258 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | smsPduEncode = (int (*)(const char *smsc, const char *da_num, const char *msg, int charset, char *smsc_pdu, char **pdu))dlsym(dlHandle_mbtk, "smsPduEncode"); |
| 262 | if (smsPduEncode == NULL) |
| 263 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 264 | LOGE(GSW_SMS,"smsPduEncode dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 265 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 269 | smsPduDecode = (int (*)(const char *pdu_str, int pdu_len, char *da_num, char *smsc, char *msg, int *charset, int *curr_pack, int *total_pack, char *date, int *ref_num))dlsym(dlHandle_mbtk,"smsPduDecode"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 270 | if (smsPduDecode == NULL) |
| 271 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 272 | LOGE(GSW_SMS,"smsPduDecode dlsym fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 273 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | _mdapi_sms_get_msg_num = (kal_int32 (*)(const char *msg, int charset, kal_int32 *msg_num, kal_int32 *msg_len))dlsym(dlHandle_mbtk,"_mdapi_sms_get_msg_num"); |
| 277 | if (_mdapi_sms_get_msg_num == NULL) |
| 278 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 279 | LOGE(GSW_SMS,"_mdapi_sms_get_msg_num dlsym fail"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 280 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | return GSW_HAL_SUCCESS; |
| 284 | |
| 285 | } |
| 286 | |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 287 | static void generate_sms_id(char* dest, const char* num, int ref_num) { |
| 288 | snprintf(dest, 64, "%.30s_%d", num, ref_num); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // 查找或创建短信重组器 |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 292 | static SmsReassembler* get_sms_reassembler(const char* num, int ref_num, int total_segments) |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 293 | { |
| 294 | char msg_id[64]; |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 295 | generate_sms_id(msg_id, num, ref_num); |
| 296 | LOGE(GSW_SMS,"msg_id:%s\n",msg_id); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 297 | time_t current_time = time(NULL); |
| 298 | |
| 299 | for (int i = 0; i < MAX_SMS_REASSEMBLERS; i++) |
| 300 | { |
| 301 | if (g_sms_reassemblers[i].msg_id[0] != '\0') |
| 302 | { |
| 303 | // 检查超时 |
| 304 | if (current_time - g_sms_reassemblers[i].last_received > SMS_TIMEOUT) |
| 305 | { |
| 306 | // 清理超时重组器 |
lichengzhang | 5a25c2a | 2025-08-20 13:29:54 +0800 | [diff] [blame] | 307 | LOGE(GSW_SMS,"start clean reassembler\n"); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 308 | for (int j = 0; j < g_sms_reassemblers[i].total_segments; j++) |
| 309 | { |
| 310 | if (g_sms_reassemblers[i].segments[j]) |
| 311 | { |
| 312 | free(g_sms_reassemblers[i].segments[j]); |
| 313 | } |
| 314 | } |
| 315 | free(g_sms_reassemblers[i].segments); |
| 316 | memset(&g_sms_reassemblers[i], 0, sizeof(SmsReassembler)); |
| 317 | } |
| 318 | else if (strcmp(g_sms_reassemblers[i].msg_id, msg_id) == 0) |
| 319 | { |
lichengzhang | 5a25c2a | 2025-08-20 13:29:54 +0800 | [diff] [blame] | 320 | LOGE(GSW_SMS,"start clean reassembler2\n"); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 321 | g_sms_reassemblers[i].last_received = current_time; |
| 322 | return &g_sms_reassemblers[i]; |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // 创建新的重组器 |
| 328 | for (int i = 0; i < MAX_SMS_REASSEMBLERS; i++) |
| 329 | { |
| 330 | if (g_sms_reassemblers[i].msg_id[0] == '\0') |
| 331 | { |
| 332 | strncpy(g_sms_reassemblers[i].msg_id, msg_id, sizeof(g_sms_reassemblers[i].msg_id) - 1); |
| 333 | g_sms_reassemblers[i].total_segments = total_segments; |
| 334 | g_sms_reassemblers[i].received_count = 0; |
| 335 | g_sms_reassemblers[i].last_received = current_time; |
lichengzhang | 5a25c2a | 2025-08-20 13:29:54 +0800 | [diff] [blame] | 336 | LOGE(GSW_SMS,"start create reassembler\n"); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 337 | // 分配分片数组内存 |
| 338 | g_sms_reassemblers[i].segments = calloc(total_segments, sizeof(char*)); |
| 339 | if (!g_sms_reassemblers[i].segments) |
| 340 | { |
| 341 | LOGE(GSW_SMS, "Memory allocation failed for segments array"); |
| 342 | memset(&g_sms_reassemblers[i], 0, sizeof(SmsReassembler)); |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | return &g_sms_reassemblers[i]; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | LOGE(GSW_SMS, "No free slots for new SMS reassembler"); |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | // 重组短信 |
| 355 | static char* reassemble_sms(SmsReassembler* reassembler) |
| 356 | { |
lichengzhang | f964e9e | 2025-08-19 18:59:47 +0800 | [diff] [blame] | 357 | LOGE(GSW_SMS, "start real reassemble_sms\n"); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 358 | size_t total_len = 0; |
| 359 | for (int i = 0; i < reassembler->total_segments; i++) |
| 360 | { |
| 361 | if (reassembler->segments[i]) |
| 362 | { |
| 363 | total_len += strlen(reassembler->segments[i]); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | char* full_msg = malloc(total_len + 1); |
| 368 | if (!full_msg) |
| 369 | { |
| 370 | LOGE(GSW_SMS, "Memory allocation failed for full message"); |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | full_msg[0] = '\0'; |
| 375 | for (int i = 0; i < reassembler->total_segments; i++) |
| 376 | { |
| 377 | if (reassembler->segments[i]) |
| 378 | { |
| 379 | strcat(full_msg, reassembler->segments[i]); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return full_msg; |
| 384 | } |
| 385 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 386 | void ArrayToStr(unsigned char *Buff, unsigned int BuffLen, char *OutputStr) |
| 387 | { |
| 388 | int i = 0; |
| 389 | char TempBuff[MSG_MAX_LEN * 2 +1] = {0}; |
| 390 | char strBuff[MSG_MAX_LEN * 2 +1] = {0}; |
| 391 | |
| 392 | for(i = 0; i<BuffLen;i++) |
| 393 | { |
| 394 | sprintf(TempBuff,"%02x",(unsigned char)Buff[i]); |
lichengzhang | dab5ee7 | 2025-07-21 18:03:00 +0800 | [diff] [blame] | 395 | strncat(strBuff,TempBuff,2); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 396 | } |
| 397 | strncpy(OutputStr, strBuff, BuffLen*2); |
| 398 | return; |
| 399 | } |
| 400 | |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 401 | void gsw_sms_state_change_cb(const void* data, int data_len) |
| 402 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 403 | LOGD(GSW_SMS,"gsw_sms_state_change_cb -------start\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 404 | |
| 405 | uint8 *ptr = (uint8*)data; |
| 406 | |
| 407 | if (!strncmp("+CMT:", (const char *)ptr, 5))//丢弃无用消息 |
| 408 | return ; |
| 409 | |
| 410 | gsw_sms_msg_type_t gsw_sms_msg; |
| 411 | gsw_sms_date_t gsw_sms_date; |
| 412 | |
| 413 | memset(&gsw_sms_msg, 0, sizeof(gsw_sms_msg_type_t)); |
| 414 | memset(&gsw_sms_date, 0, sizeof(gsw_sms_date_t)); |
| 415 | |
| 416 | char smsc[MDAPI_MAX_PDU_SIZE] = {0}; |
| 417 | char received_pdu[MDAPI_MAX_PDU_SIZE] = {0}; |
| 418 | char msg[MDAPI_MAX_PDU_SIZE] = {0}; |
| 419 | char num[MDAPI_MAX_PDU_SIZE] = {0}; |
| 420 | char date[MAX_DATE_SIZE] = {0}; |
| 421 | int charset = 0; |
| 422 | int ret = -1; |
| 423 | int curr_pack = 1; |
| 424 | int total_pack = 1; |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 425 | int ref_num = 0; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 426 | if(ptr == NULL) |
| 427 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 428 | LOGE(GSW_SMS,"ptr is null"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 429 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 430 | LOGE(GSW_SMS,"ptr: %s\n,data_len = %d\n", (char *)ptr, data_len); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 431 | if(data_len > MDAPI_MAX_PDU_SIZE) |
| 432 | { |
| 433 | strncpy(received_pdu, (const char *)ptr, MDAPI_MAX_PDU_SIZE-1); |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | strncpy(received_pdu, (const char *)ptr, data_len); |
| 438 | } |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 439 | ret = smsPduDecode((const char *)received_pdu, data_len, num, smsc, msg, &charset, &curr_pack, &total_pack, date,&ref_num); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 440 | if(ret != 0) |
| 441 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 442 | LOGE(GSW_SMS,"smsPduDecode fail\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 443 | return ; |
| 444 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 445 | LOGE(GSW_SMS,"smsPduDecode ret: %d\n", ret); |
| 446 | LOGE(GSW_SMS,"SMS :%s, %s, %d\n", __FILE__, __FUNCTION__, __LINE__); |
| 447 | LOGE(GSW_SMS,"[EVENT][MT_SMS]PDU decode:smsc: %s\n, phone number: %s\ncharset: %d\n msg_len: %d\n message content: %s\n curr_pack: %d\n total_pack: %d\n date: %s", smsc, num, charset, strlen(msg), msg, curr_pack, total_pack, date); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 448 | |
| 449 | gsw_sms_msg.content_encode = charset; |
| 450 | memcpy(gsw_sms_msg.src_num, num, strlen(num)); |
| 451 | gsw_sms_msg.content_len = strlen(msg); |
| 452 | memcpy(gsw_sms_msg.content, msg, strlen(msg)); |
| 453 | |
| 454 | if(sscanf(date, "%4[^-]-%2[^-]-%2[^ ] %2[^:]:%2[^:]:%2s", (char*)gsw_sms_date.year, (char*)gsw_sms_date.month, (char*)gsw_sms_date.day, (char*)gsw_sms_date.hour, (char*)gsw_sms_date.minutes, (char*)gsw_sms_date.seconds)<=0) |
| 455 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 456 | LOGE(GSW_SMS,"sscanf failed\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | snprintf((char *)gsw_sms_date.timezone, sizeof(gsw_sms_date.timezone), "%s","+8"); |
| 460 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 461 | LOGE(GSW_SMS,"Year: %s\n", (char*)gsw_sms_date.year); |
| 462 | LOGE(GSW_SMS,"Month: %s\n", (char*)gsw_sms_date.month); |
| 463 | LOGE(GSW_SMS,"Day: %s\n", (char*)gsw_sms_date.day); |
| 464 | LOGE(GSW_SMS,"Hour: %s\n", (char*)gsw_sms_date.hour); |
| 465 | LOGE(GSW_SMS,"Minute: %s\n", (char*)gsw_sms_date.minutes); |
| 466 | LOGE(GSW_SMS,"Second: %s\n", (char*)gsw_sms_date.seconds); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 467 | |
| 468 | gsw_sms_msg.date = gsw_sms_date; |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 469 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 470 | |
| 471 | if(gsw_sms_callback) |
| 472 | { |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 473 | if(total_pack > 1) |
| 474 | { |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 475 | SmsReassembler* reassembler = get_sms_reassembler(num, ref_num, total_pack); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 476 | if (!reassembler) |
| 477 | { |
| 478 | LOGE(GSW_SMS, "Failed to get SMS reassembler"); |
| 479 | return; |
| 480 | } |
| 481 | int seg_index = curr_pack - 1; |
| 482 | |
| 483 | // 如果该分片尚未接收 |
| 484 | if (!reassembler->segments[seg_index]) |
| 485 | { |
| 486 | reassembler->segments[seg_index] = strdup(msg); |
| 487 | if (!reassembler->segments[seg_index]) |
| 488 | { |
| 489 | LOGE(GSW_SMS, "Memory allocation failed for segment content"); |
| 490 | return; |
| 491 | } |
| 492 | reassembler->received_count++; |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 493 | LOGE(GSW_SMS, "seg_index:%d\n",reassembler->received_count); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 494 | } |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 495 | LOGE(GSW_SMS, "seg_index:%d\n",seg_index); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 496 | // 检查是否所有分片都已接收 |
| 497 | if (reassembler->received_count == reassembler->total_segments) |
| 498 | { |
| 499 | char* full_msg = reassemble_sms(reassembler); |
| 500 | if (full_msg) |
| 501 | { |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 502 | LOGE(GSW_SMS, "total_segments:%d\n",reassembler->received_count); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 503 | gsw_sms_msg.content_encode = charset; |
| 504 | strncpy(gsw_sms_msg.src_num, num, sizeof(gsw_sms_msg.src_num) - 1); |
| 505 | gsw_sms_msg.content_len = strlen(full_msg); |
| 506 | |
| 507 | size_t copy_size = (gsw_sms_msg.content_len < sizeof(gsw_sms_msg.content)) ? |
| 508 | gsw_sms_msg.content_len : sizeof(gsw_sms_msg.content) - 1; |
| 509 | strncpy(gsw_sms_msg.content, full_msg, copy_size); |
| 510 | gsw_sms_msg.content[copy_size] = '\0'; |
lichengzhang | fd15224 | 2025-09-02 20:14:24 +0800 | [diff] [blame] | 511 | LOGE(GSW_SMS, "gsw_sms_msg:%s\n",gsw_sms_msg.content); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 512 | snprintf((char *)gsw_sms_date.timezone, sizeof(gsw_sms_date.timezone), "%s", "+8"); |
| 513 | |
| 514 | if (gsw_sms_callback) |
lichengzhang | f964e9e | 2025-08-19 18:59:47 +0800 | [diff] [blame] | 515 | gsw_sms_callback(GSW_SMS_RECEIVED_FLG, &gsw_sms_msg); |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 516 | |
| 517 | free(full_msg); |
| 518 | } |
| 519 | |
| 520 | for (int i = 0; i < reassembler->total_segments; i++) |
| 521 | { |
| 522 | if (reassembler->segments[i]) |
| 523 | { |
| 524 | free(reassembler->segments[i]); |
| 525 | reassembler->segments[i] = NULL; |
| 526 | } |
| 527 | } |
| 528 | free(reassembler->segments); |
| 529 | reassembler->segments = NULL; |
| 530 | reassembler->received_count = 0; |
| 531 | reassembler->total_segments = 0; |
| 532 | memset(reassembler->msg_id, 0, sizeof(reassembler->msg_id)); |
| 533 | } |
| 534 | } |
| 535 | else |
lichengzhang | f964e9e | 2025-08-19 18:59:47 +0800 | [diff] [blame] | 536 | gsw_sms_callback(GSW_SMS_RECEIVED_FLG, &gsw_sms_msg); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 537 | } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
| 541 | /** |
| 542 | * @brief SDK interface to call back sms messages |
| 543 | * @param [in] handle_ptr |
| 544 | * @retval 0: success |
| 545 | * @retval other: fail |
| 546 | */ |
| 547 | int gsw_sms_reg_callback(GSW_SMS_Callback_fun handle_ptr) |
| 548 | { |
| 549 | if(init_flag == 0) |
| 550 | { |
| 551 | return -1; |
| 552 | } |
| 553 | |
| 554 | if(handle_ptr == NULL) |
| 555 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 556 | LOGE(GSW_SMS,"gsw_sms_reg_callback fail,handle_ptr is null\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 557 | return -1; |
| 558 | } |
| 559 | else |
| 560 | { |
| 561 | gsw_sms_callback = handle_ptr; |
| 562 | } |
| 563 | return GSW_HAL_SUCCESS; |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * @brief sms sdk init |
| 568 | * @param [in] token |
| 569 | * @retval 0: success |
| 570 | * @retval other: fail |
| 571 | */ |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 572 | int gsw_sms_sdk_init(int32_t token) |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 573 | { |
| 574 | handle = token; |
| 575 | int ret = -1; |
| 576 | |
| 577 | if(init_flag == 1) |
| 578 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 579 | LOGE(GSW_SMS,"has init sms sdk,return\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 580 | return GSW_HAL_SUCCESS; |
| 581 | } |
| 582 | |
| 583 | ret = mbtk_sms_import(); |
| 584 | if(ret != 0) |
| 585 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 586 | // if(mbtk_log != NULL) |
| 587 | // { |
| 588 | // LOGE(GSW_SMS,"[gsw_sms_sdk_init]mbtk_sms_import fail\n"); |
| 589 | // } |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 590 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 591 | } |
| 592 | |
lichengzhang | 7704687 | 2025-07-07 11:45:51 +0800 | [diff] [blame] | 593 | //mbtk_log_init("syslog", "MBTK_RIL"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 594 | |
| 595 | sms_info_handle = mbtk_info_handle_get(); |
| 596 | if(sms_info_handle == NULL) |
| 597 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 598 | LOGE(GSW_SMS,"[gsw_sms_sdk_init] mbtk handle init fail\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 599 | dlclose(dlHandle_mbtk); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 600 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | ret = mbtk_sms_cnmi_set(sms_info_handle); |
| 604 | if(ret != 0) |
| 605 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 606 | LOGE(GSW_SMS,"mbtk_sms_cnmi_set fail.[%d]\n", ret); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 607 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | ret = mbtk_sms_state_change_cb_reg(sms_info_handle, gsw_sms_state_change_cb); |
| 611 | if(ret != 0) |
| 612 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 613 | LOGE(GSW_SMS,"mbtk_sms_state_change_cb_reg fail.[%d]\n", ret); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 614 | if(sms_info_handle) |
| 615 | { |
| 616 | mbtk_info_handle_free(&sms_info_handle); |
| 617 | sms_info_handle = NULL; |
| 618 | } |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 619 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 620 | } |
| 621 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 622 | LOGD(GSW_SMS,"[gsw_sms_sdk_init]gsw_sms_sdk_init success\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 623 | init_flag = 1; |
| 624 | return GSW_HAL_SUCCESS; |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * @brief sms sdk deinit |
| 629 | * @param |
| 630 | * @retval 0: success |
| 631 | * @retval other: fail |
| 632 | */ |
| 633 | int gsw_sms_sdk_deinit(void) |
| 634 | { |
| 635 | int ret = -1; |
| 636 | |
| 637 | if(init_flag == 0) |
| 638 | { |
| 639 | return -1; |
| 640 | } |
| 641 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 642 | LOGD(GSW_SMS,"callback clear\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 643 | gsw_sms_callback = NULL; |
| 644 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 645 | LOGD(GSW_SMS,"handle_free\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 646 | if(sms_info_handle != NULL) |
| 647 | { |
| 648 | ret = mbtk_info_handle_free(&sms_info_handle); |
| 649 | if(ret != 0) |
| 650 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 651 | LOGE(GSW_SMS,"[gsw_sms_sdk_deinit]mbtk_info_handle_free fail\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 652 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 653 | } |
| 654 | sms_info_handle = NULL; |
| 655 | } |
| 656 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 657 | LOGD(GSW_SMS,"dlclose start\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 658 | if(dlHandle_mbtk == NULL) |
| 659 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 660 | LOGE(GSW_SMS,"[gsw_sms_sdk_deinit]dlHandle_sms is null\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 661 | } |
| 662 | else |
| 663 | { |
| 664 | dlclose(dlHandle_mbtk); |
| 665 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 666 | LOGD(GSW_SMS,"dlclose end\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 667 | init_flag = 0; |
| 668 | return GSW_HAL_SUCCESS; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * @brief send sms fuction * |
| 673 | * @param [in] phone_num dest phone num send sms |
| 674 | * @param [in] char_set encode format for sms 0 7bit 1 binary 2 usc2 |
| 675 | * @param [in] msg sms content |
| 676 | * @param [in] msg_len send sms length,max is 1024 |
| 677 | * @retval 0: success |
| 678 | * @retval other: fail |
| 679 | */ |
| 680 | int gsw_send_sms(char *phone_num, int char_set, char *msg, int msg_len) |
| 681 | { |
| 682 | |
| 683 | if(init_flag == 0) |
| 684 | { |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 685 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | if(phone_num == NULL || msg == NULL) |
| 689 | { |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 690 | return GSW_HAL_ARG_INVALID; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 691 | } |
| 692 | |
lichengzhang | dab5ee7 | 2025-07-21 18:03:00 +0800 | [diff] [blame] | 693 | if(strlen((char *)msg) == 0 || strlen((char *)phone_num) == 0) |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 694 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 695 | LOGE(GSW_SMS,"strlen(telephony_num):%d", strlen((char *)phone_num)); |
| 696 | LOGE(GSW_SMS,"strlen(msg):%d", strlen((char *)msg)); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 697 | return GSW_HAL_ARG_INVALID; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 698 | } |
lichengzhang | dab5ee7 | 2025-07-21 18:03:00 +0800 | [diff] [blame] | 699 | if(strlen((char *)msg) > MSG_MAX_LEN || msg_len > MSG_MAX_LEN || strlen((char *)msg) != msg_len) |
| 700 | { |
| 701 | LOGE(GSW_SMS,"stelen is %d\n",strlen((char *)msg)); |
| 702 | LOGE(GSW_SMS,"the lenth is error\n"); |
| 703 | return GSW_HAL_NORMAL_FAIL; |
| 704 | } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 705 | |
| 706 | int err = -1; |
| 707 | |
| 708 | char smscPDU[30] = {0}; |
| 709 | char **pdu = NULL; |
| 710 | char smsc[4] = {0}; |
| 711 | char cmgs[MSM_NUMBER_MAX] = {0}; |
| 712 | char resp[RES_NUM_MIN] = {0}; |
| 713 | char msg_e_b[GSW_SMS_RECV_CONT_MAX+1] = {0};// +1 for end of string.*2:A char array contains two elements of a string for each value |
| 714 | |
| 715 | kal_int32 msg_num = 0; |
| 716 | kal_int32 pdu_msg_len = 0; |
| 717 | kal_int32 status = MDAPI_RET_ERROR; |
| 718 | kal_int32 index = 0; |
| 719 | |
| 720 | if(char_set == 1) //8bit |
| 721 | { |
| 722 | ArrayToStr((unsigned char *)msg, (unsigned int)msg_len, msg_e_b); |
| 723 | status = _mdapi_sms_get_msg_num(msg_e_b, char_set, &msg_num, &pdu_msg_len); |
| 724 | } |
| 725 | |
| 726 | else //7bit usc2 |
| 727 | { |
| 728 | status = _mdapi_sms_get_msg_num((char *)msg, char_set, &msg_num, &pdu_msg_len); |
| 729 | } |
| 730 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 731 | LOGE(GSW_SMS,"%s, %s, %d, msg_len = [%d] ,msg_num=[%d]\n", __FILE__, __FUNCTION__, __LINE__, msg_len, msg_num); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 732 | if(status == MDAPI_RET_ERROR) |
| 733 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 734 | LOGE(GSW_SMS,"get message number failed\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 735 | //return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 736 | } |
| 737 | else |
| 738 | { |
| 739 | //allocate memery for **pdu |
| 740 | pdu = (char **)malloc(sizeof(char *) * msg_num); |
| 741 | if(pdu == NULL) |
| 742 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 743 | LOGE(GSW_SMS,"%s, %s, %d, allocate memory for pdu failed\n", __FILE__, __FUNCTION__, __LINE__); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 744 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | else |
| 748 | { |
| 749 | for(index = 0; index < msg_num; index++) |
| 750 | { |
| 751 | pdu[index] = (char *)malloc(sizeof(char)*MAX_PDU_SIZE); |
| 752 | if(pdu[index] == NULL) |
| 753 | { |
| 754 | for(int i = 0; i < index; i++) |
| 755 | { |
| 756 | free(pdu[i]); |
| 757 | pdu[i] = NULL; |
| 758 | } |
| 759 | free(pdu); |
| 760 | pdu = NULL; |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 761 | LOGE(GSW_SMS,"%s, %s, %d, allocate memory for pdu[%d] failed\n", __FILE__, __FUNCTION__, __LINE__,index); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 762 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 763 | } |
| 764 | else |
| 765 | { |
| 766 | memset(pdu[index], 0, MAX_PDU_SIZE); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 767 | LOGE(GSW_SMS,"%s, %s, %d, pdu[%d} init value is: %s \n", __FILE__, __FUNCTION__, __LINE__, index, pdu[index]); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | //allocate memory for **pdu success |
| 774 | if(index == msg_num) |
| 775 | { |
| 776 | if(char_set == 1)//8bit |
| 777 | { |
| 778 | smsPduEncode(smsc, (char *)phone_num, msg_e_b, char_set, smscPDU, pdu); |
| 779 | } |
| 780 | else |
| 781 | { |
| 782 | smsPduEncode(smsc, (char *)phone_num, (char *)msg, char_set, smscPDU, pdu); |
| 783 | } |
| 784 | for(index = 0; index < msg_num; index++) |
| 785 | { |
| 786 | char pdu_data[MAX_PDU_SIZE] = {0}; |
| 787 | char *p = pdu_data; |
| 788 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 789 | LOGE(GSW_SMS,"index:%d\n",index); |
| 790 | LOGE(GSW_SMS,"%s, %s, %d, smscPDU: %s, pdu: %s",__FILE__, __FUNCTION__, __LINE__, smscPDU, pdu[index]); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 791 | sprintf(p, "%s",smscPDU); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 792 | LOGE(GSW_SMS,"pdu_data:%s\n", pdu_data); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 793 | int sc = strlen(pdu_data); |
| 794 | sprintf(p+strlen(p), "%s", pdu[index]); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 795 | LOGE(GSW_SMS,"pdu_data:%s\n", pdu_data); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 796 | |
| 797 | int t = strlen(pdu_data); |
| 798 | sprintf(cmgs, "%d,%s", (t-sc)/2, pdu_data); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 799 | LOGE(GSW_SMS,"cmgs:%s\n", cmgs); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 800 | memset(resp, 0, sizeof(resp)); |
| 801 | |
| 802 | err = mbtk_sms_cmgf_set(sms_info_handle, 0); |
| 803 | if(err) |
| 804 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 805 | LOGE(GSW_SMS,"cmgf set error : %d\n", err); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 806 | for(index = 0; index < msg_num; index++){ |
| 807 | free(pdu[index]); |
| 808 | pdu[index] = NULL; |
| 809 | } |
| 810 | free(pdu); |
| 811 | pdu = NULL; |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 812 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 813 | } |
| 814 | else |
| 815 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 816 | LOGD(GSW_SMS,"cmgf set success\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp); |
| 820 | if(err) |
| 821 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 822 | LOGE(GSW_SMS,"cmgs send fail\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 823 | for(index = 0; index < msg_num; index++){ |
| 824 | free(pdu[index]); |
| 825 | pdu[index] = NULL; |
| 826 | } |
| 827 | free(pdu); |
| 828 | pdu = NULL; |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 829 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 830 | } |
| 831 | else |
| 832 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 833 | LOGD(GSW_SMS,"cmgs send success, resp:%s\n", resp); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | for(index = 0; index < msg_num; index++){ |
| 840 | free(pdu[index]); |
| 841 | pdu[index] = NULL; |
| 842 | } |
| 843 | free(pdu); |
| 844 | pdu = NULL; |
| 845 | |
| 846 | return GSW_HAL_SUCCESS; |
| 847 | } |
| 848 | |
| 849 | |
| 850 | /** |
| 851 | * @brief get smsc fuction * |
| 852 | * @param [in] len input buf len for smsc,max is 32 |
| 853 | * @param [out] smsc address for smsc get from this func * |
| 854 | * @retval 0: success |
| 855 | * @retval other: fail |
| 856 | */ |
| 857 | int gsw_get_smsc_address(int len, char *smsc) |
| 858 | { |
| 859 | int ret = -1; |
| 860 | int len_t = 0; |
| 861 | char smsc_temp[SMSC_MAX_LEN] = {0}; |
| 862 | char *p1, *p2 ,*substr; |
| 863 | |
| 864 | if(init_flag == 0) |
| 865 | { |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 866 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | if(smsc == NULL || len <= 0) |
| 870 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 871 | LOGE(GSW_SMS,"smsc is null or len = %d\n",len); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 872 | return GSW_HAL_ARG_INVALID; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | ret = mbtk_sms_csca_get(sms_info_handle, smsc_temp); |
| 876 | if(smsc_temp[0] == '\0') |
| 877 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 878 | LOGE(GSW_SMS,"gsw_get_smsc_address Error : %d\n", ret); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 879 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | else |
| 883 | { |
| 884 | p1 = strchr(smsc_temp, '\"'); |
| 885 | p2 = strrchr(smsc_temp, '\"'); |
| 886 | if (p1 && p2 && p2 > p1) |
| 887 | { |
| 888 | len_t = p2 - p1 - 1; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 889 | substr = malloc(len_t + 1); |
| 890 | if (NULL == substr) |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 891 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 892 | LOGE(GSW_SMS,"substr = NULL, malloc faill!!!\n"); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 893 | return GSW_HAL_NO_MEMORY; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 894 | } |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 895 | strncpy(substr, p1 + 1, len_t); |
| 896 | substr[len_t] = '\0'; |
lichengzhang | 12647e5 | 2025-07-21 17:23:10 +0800 | [diff] [blame] | 897 | strncpy(smsc, substr, len); |
| 898 | strncpy(sms_center_address, substr, len); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 899 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 900 | LOGE(GSW_SMS,"qser_sms_getsmscenteraddress success, smsc = %s\n", sms_center_address); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 901 | free(substr); |
| 902 | substr = NULL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 903 | } |
| 904 | else |
| 905 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 906 | LOGE(GSW_SMS,"String inside double quotes not found\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 907 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
| 911 | |
| 912 | return GSW_HAL_SUCCESS; |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * @brief set smsc fuction |
| 917 | * @param [out] smsc string value for smsc,max length is 32 * |
| 918 | * @retval 0: success |
| 919 | * @retval other: fail |
| 920 | */ |
| 921 | int gsw_set_smsc_address(const char *smsc) |
| 922 | { |
| 923 | int ret = -1; |
| 924 | |
| 925 | if(init_flag == 0) |
| 926 | { |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 927 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | if(smsc == NULL || strlen((char *)smsc) == 0) |
| 931 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 932 | LOGE(GSW_SMS,"smsc is null or empty\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 933 | return GSW_HAL_ARG_INVALID; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | ret = mbtk_sms_csca_set(sms_info_handle, (char *)smsc); |
| 937 | |
| 938 | if(ret == 0) |
| 939 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 940 | LOGE(GSW_SMS,"set smsc success,smsc = %s\n",smsc); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 941 | } |
| 942 | else |
| 943 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 944 | LOGE(GSW_SMS,"set smsc fail,ret = %d\n", ret); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 945 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | return GSW_HAL_SUCCESS; |
| 949 | } |