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); |
| 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); |
| 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 | |
| 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))dlsym(dlHandle_mbtk,"smsPduDecode"); |
| 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 | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 287 | static void generate_sms_id(char* dest, const char* num, const char* date) { |
| 288 | snprintf(dest, 64, "%.30s_%.30s", num, date); |
| 289 | } |
| 290 | |
| 291 | // 查找或创建短信重组器 |
| 292 | static SmsReassembler* get_sms_reassembler(const char* num, const char* date, int total_segments) |
| 293 | { |
| 294 | char msg_id[64]; |
| 295 | generate_sms_id(msg_id, num, date); |
| 296 | |
| 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 | // 清理超时重组器 |
| 307 | for (int j = 0; j < g_sms_reassemblers[i].total_segments; j++) |
| 308 | { |
| 309 | if (g_sms_reassemblers[i].segments[j]) |
| 310 | { |
| 311 | free(g_sms_reassemblers[i].segments[j]); |
| 312 | } |
| 313 | } |
| 314 | free(g_sms_reassemblers[i].segments); |
| 315 | memset(&g_sms_reassemblers[i], 0, sizeof(SmsReassembler)); |
| 316 | } |
| 317 | else if (strcmp(g_sms_reassemblers[i].msg_id, msg_id) == 0) |
| 318 | { |
| 319 | g_sms_reassemblers[i].last_received = current_time; |
| 320 | return &g_sms_reassemblers[i]; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // 创建新的重组器 |
| 326 | for (int i = 0; i < MAX_SMS_REASSEMBLERS; i++) |
| 327 | { |
| 328 | if (g_sms_reassemblers[i].msg_id[0] == '\0') |
| 329 | { |
| 330 | strncpy(g_sms_reassemblers[i].msg_id, msg_id, sizeof(g_sms_reassemblers[i].msg_id) - 1); |
| 331 | g_sms_reassemblers[i].total_segments = total_segments; |
| 332 | g_sms_reassemblers[i].received_count = 0; |
| 333 | g_sms_reassemblers[i].last_received = current_time; |
| 334 | |
| 335 | // 分配分片数组内存 |
| 336 | g_sms_reassemblers[i].segments = calloc(total_segments, sizeof(char*)); |
| 337 | if (!g_sms_reassemblers[i].segments) |
| 338 | { |
| 339 | LOGE(GSW_SMS, "Memory allocation failed for segments array"); |
| 340 | memset(&g_sms_reassemblers[i], 0, sizeof(SmsReassembler)); |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | return &g_sms_reassemblers[i]; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | LOGE(GSW_SMS, "No free slots for new SMS reassembler"); |
| 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | // 重组短信 |
| 353 | static char* reassemble_sms(SmsReassembler* reassembler) |
| 354 | { |
| 355 | size_t total_len = 0; |
| 356 | for (int i = 0; i < reassembler->total_segments; i++) |
| 357 | { |
| 358 | if (reassembler->segments[i]) |
| 359 | { |
| 360 | total_len += strlen(reassembler->segments[i]); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | char* full_msg = malloc(total_len + 1); |
| 365 | if (!full_msg) |
| 366 | { |
| 367 | LOGE(GSW_SMS, "Memory allocation failed for full message"); |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | full_msg[0] = '\0'; |
| 372 | for (int i = 0; i < reassembler->total_segments; i++) |
| 373 | { |
| 374 | if (reassembler->segments[i]) |
| 375 | { |
| 376 | strcat(full_msg, reassembler->segments[i]); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return full_msg; |
| 381 | } |
| 382 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 383 | void ArrayToStr(unsigned char *Buff, unsigned int BuffLen, char *OutputStr) |
| 384 | { |
| 385 | int i = 0; |
| 386 | char TempBuff[MSG_MAX_LEN * 2 +1] = {0}; |
| 387 | char strBuff[MSG_MAX_LEN * 2 +1] = {0}; |
| 388 | |
| 389 | for(i = 0; i<BuffLen;i++) |
| 390 | { |
| 391 | sprintf(TempBuff,"%02x",(unsigned char)Buff[i]); |
lichengzhang | dab5ee7 | 2025-07-21 18:03:00 +0800 | [diff] [blame] | 392 | strncat(strBuff,TempBuff,2); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 393 | } |
| 394 | strncpy(OutputStr, strBuff, BuffLen*2); |
| 395 | return; |
| 396 | } |
| 397 | |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 398 | void gsw_sms_state_change_cb(const void* data, int data_len) |
| 399 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 400 | LOGD(GSW_SMS,"gsw_sms_state_change_cb -------start\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 401 | |
| 402 | uint8 *ptr = (uint8*)data; |
| 403 | |
| 404 | if (!strncmp("+CMT:", (const char *)ptr, 5))//丢弃无用消息 |
| 405 | return ; |
| 406 | |
| 407 | gsw_sms_msg_type_t gsw_sms_msg; |
| 408 | gsw_sms_date_t gsw_sms_date; |
| 409 | |
| 410 | memset(&gsw_sms_msg, 0, sizeof(gsw_sms_msg_type_t)); |
| 411 | memset(&gsw_sms_date, 0, sizeof(gsw_sms_date_t)); |
| 412 | |
| 413 | char smsc[MDAPI_MAX_PDU_SIZE] = {0}; |
| 414 | char received_pdu[MDAPI_MAX_PDU_SIZE] = {0}; |
| 415 | char msg[MDAPI_MAX_PDU_SIZE] = {0}; |
| 416 | char num[MDAPI_MAX_PDU_SIZE] = {0}; |
| 417 | char date[MAX_DATE_SIZE] = {0}; |
| 418 | int charset = 0; |
| 419 | int ret = -1; |
| 420 | int curr_pack = 1; |
| 421 | int total_pack = 1; |
| 422 | if(ptr == NULL) |
| 423 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 424 | LOGE(GSW_SMS,"ptr is null"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 425 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 426 | 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] | 427 | if(data_len > MDAPI_MAX_PDU_SIZE) |
| 428 | { |
| 429 | strncpy(received_pdu, (const char *)ptr, MDAPI_MAX_PDU_SIZE-1); |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | strncpy(received_pdu, (const char *)ptr, data_len); |
| 434 | } |
| 435 | ret = smsPduDecode((const char *)received_pdu, data_len, num, smsc, msg, &charset, &curr_pack, &total_pack,date); |
| 436 | if(ret != 0) |
| 437 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 438 | LOGE(GSW_SMS,"smsPduDecode fail\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 439 | return ; |
| 440 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 441 | LOGE(GSW_SMS,"smsPduDecode ret: %d\n", ret); |
| 442 | LOGE(GSW_SMS,"SMS :%s, %s, %d\n", __FILE__, __FUNCTION__, __LINE__); |
| 443 | 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] | 444 | |
| 445 | gsw_sms_msg.content_encode = charset; |
| 446 | memcpy(gsw_sms_msg.src_num, num, strlen(num)); |
| 447 | gsw_sms_msg.content_len = strlen(msg); |
| 448 | memcpy(gsw_sms_msg.content, msg, strlen(msg)); |
| 449 | |
| 450 | 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) |
| 451 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 452 | LOGE(GSW_SMS,"sscanf failed\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | snprintf((char *)gsw_sms_date.timezone, sizeof(gsw_sms_date.timezone), "%s","+8"); |
| 456 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 457 | LOGE(GSW_SMS,"Year: %s\n", (char*)gsw_sms_date.year); |
| 458 | LOGE(GSW_SMS,"Month: %s\n", (char*)gsw_sms_date.month); |
| 459 | LOGE(GSW_SMS,"Day: %s\n", (char*)gsw_sms_date.day); |
| 460 | LOGE(GSW_SMS,"Hour: %s\n", (char*)gsw_sms_date.hour); |
| 461 | LOGE(GSW_SMS,"Minute: %s\n", (char*)gsw_sms_date.minutes); |
| 462 | LOGE(GSW_SMS,"Second: %s\n", (char*)gsw_sms_date.seconds); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 463 | |
| 464 | gsw_sms_msg.date = gsw_sms_date; |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 465 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 466 | |
| 467 | if(gsw_sms_callback) |
| 468 | { |
lichengzhang | 4880ea5 | 2025-08-05 14:01:13 +0800 | [diff] [blame] | 469 | if(total_pack > 1) |
| 470 | { |
| 471 | SmsReassembler* reassembler = get_sms_reassembler(num, date, total_pack); |
| 472 | if (!reassembler) |
| 473 | { |
| 474 | LOGE(GSW_SMS, "Failed to get SMS reassembler"); |
| 475 | return; |
| 476 | } |
| 477 | int seg_index = curr_pack - 1; |
| 478 | |
| 479 | // 如果该分片尚未接收 |
| 480 | if (!reassembler->segments[seg_index]) |
| 481 | { |
| 482 | reassembler->segments[seg_index] = strdup(msg); |
| 483 | if (!reassembler->segments[seg_index]) |
| 484 | { |
| 485 | LOGE(GSW_SMS, "Memory allocation failed for segment content"); |
| 486 | return; |
| 487 | } |
| 488 | reassembler->received_count++; |
| 489 | } |
| 490 | |
| 491 | // 检查是否所有分片都已接收 |
| 492 | if (reassembler->received_count == reassembler->total_segments) |
| 493 | { |
| 494 | char* full_msg = reassemble_sms(reassembler); |
| 495 | if (full_msg) |
| 496 | { |
| 497 | |
| 498 | gsw_sms_msg.content_encode = charset; |
| 499 | strncpy(gsw_sms_msg.src_num, num, sizeof(gsw_sms_msg.src_num) - 1); |
| 500 | gsw_sms_msg.content_len = strlen(full_msg); |
| 501 | |
| 502 | size_t copy_size = (gsw_sms_msg.content_len < sizeof(gsw_sms_msg.content)) ? |
| 503 | gsw_sms_msg.content_len : sizeof(gsw_sms_msg.content) - 1; |
| 504 | strncpy(gsw_sms_msg.content, full_msg, copy_size); |
| 505 | gsw_sms_msg.content[copy_size] = '\0'; |
| 506 | |
| 507 | snprintf((char *)gsw_sms_date.timezone, sizeof(gsw_sms_date.timezone), "%s", "+8"); |
| 508 | |
| 509 | if (gsw_sms_callback) |
| 510 | gsw_sms_callback(GSW_SMS_FULL_FLG, &gsw_sms_msg); |
| 511 | |
| 512 | free(full_msg); |
| 513 | } |
| 514 | |
| 515 | for (int i = 0; i < reassembler->total_segments; i++) |
| 516 | { |
| 517 | if (reassembler->segments[i]) |
| 518 | { |
| 519 | free(reassembler->segments[i]); |
| 520 | reassembler->segments[i] = NULL; |
| 521 | } |
| 522 | } |
| 523 | free(reassembler->segments); |
| 524 | reassembler->segments = NULL; |
| 525 | reassembler->received_count = 0; |
| 526 | reassembler->total_segments = 0; |
| 527 | memset(reassembler->msg_id, 0, sizeof(reassembler->msg_id)); |
| 528 | } |
| 529 | } |
| 530 | else |
| 531 | gsw_sms_callback(GSW_SMS_FULL_FLG, &gsw_sms_msg); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 532 | } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | |
| 536 | /** |
| 537 | * @brief SDK interface to call back sms messages |
| 538 | * @param [in] handle_ptr |
| 539 | * @retval 0: success |
| 540 | * @retval other: fail |
| 541 | */ |
| 542 | int gsw_sms_reg_callback(GSW_SMS_Callback_fun handle_ptr) |
| 543 | { |
| 544 | if(init_flag == 0) |
| 545 | { |
| 546 | return -1; |
| 547 | } |
| 548 | |
| 549 | if(handle_ptr == NULL) |
| 550 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 551 | 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] | 552 | return -1; |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | gsw_sms_callback = handle_ptr; |
| 557 | } |
| 558 | return GSW_HAL_SUCCESS; |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * @brief sms sdk init |
| 563 | * @param [in] token |
| 564 | * @retval 0: success |
| 565 | * @retval other: fail |
| 566 | */ |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 567 | int gsw_sms_sdk_init(int32_t token) |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 568 | { |
| 569 | handle = token; |
| 570 | int ret = -1; |
| 571 | |
| 572 | if(init_flag == 1) |
| 573 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 574 | LOGE(GSW_SMS,"has init sms sdk,return\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 575 | return GSW_HAL_SUCCESS; |
| 576 | } |
| 577 | |
| 578 | ret = mbtk_sms_import(); |
| 579 | if(ret != 0) |
| 580 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 581 | // if(mbtk_log != NULL) |
| 582 | // { |
| 583 | // LOGE(GSW_SMS,"[gsw_sms_sdk_init]mbtk_sms_import fail\n"); |
| 584 | // } |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 585 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 586 | } |
| 587 | |
lichengzhang | 7704687 | 2025-07-07 11:45:51 +0800 | [diff] [blame] | 588 | //mbtk_log_init("syslog", "MBTK_RIL"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 589 | |
| 590 | sms_info_handle = mbtk_info_handle_get(); |
| 591 | if(sms_info_handle == NULL) |
| 592 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 593 | LOGE(GSW_SMS,"[gsw_sms_sdk_init] mbtk handle init fail\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 594 | dlclose(dlHandle_mbtk); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 595 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | ret = mbtk_sms_cnmi_set(sms_info_handle); |
| 599 | if(ret != 0) |
| 600 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 601 | LOGE(GSW_SMS,"mbtk_sms_cnmi_set fail.[%d]\n", ret); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 602 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | ret = mbtk_sms_state_change_cb_reg(sms_info_handle, gsw_sms_state_change_cb); |
| 606 | if(ret != 0) |
| 607 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 608 | 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] | 609 | if(sms_info_handle) |
| 610 | { |
| 611 | mbtk_info_handle_free(&sms_info_handle); |
| 612 | sms_info_handle = NULL; |
| 613 | } |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 614 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 615 | } |
| 616 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 617 | 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] | 618 | init_flag = 1; |
| 619 | return GSW_HAL_SUCCESS; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * @brief sms sdk deinit |
| 624 | * @param |
| 625 | * @retval 0: success |
| 626 | * @retval other: fail |
| 627 | */ |
| 628 | int gsw_sms_sdk_deinit(void) |
| 629 | { |
| 630 | int ret = -1; |
| 631 | |
| 632 | if(init_flag == 0) |
| 633 | { |
| 634 | return -1; |
| 635 | } |
| 636 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 637 | LOGD(GSW_SMS,"callback clear\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 638 | gsw_sms_callback = NULL; |
| 639 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 640 | LOGD(GSW_SMS,"handle_free\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 641 | if(sms_info_handle != NULL) |
| 642 | { |
| 643 | ret = mbtk_info_handle_free(&sms_info_handle); |
| 644 | if(ret != 0) |
| 645 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 646 | 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] | 647 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 648 | } |
| 649 | sms_info_handle = NULL; |
| 650 | } |
| 651 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 652 | LOGD(GSW_SMS,"dlclose start\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 653 | if(dlHandle_mbtk == NULL) |
| 654 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 655 | LOGE(GSW_SMS,"[gsw_sms_sdk_deinit]dlHandle_sms is null\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 656 | } |
| 657 | else |
| 658 | { |
| 659 | dlclose(dlHandle_mbtk); |
| 660 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 661 | LOGD(GSW_SMS,"dlclose end\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 662 | init_flag = 0; |
| 663 | return GSW_HAL_SUCCESS; |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * @brief send sms fuction * |
| 668 | * @param [in] phone_num dest phone num send sms |
| 669 | * @param [in] char_set encode format for sms 0 7bit 1 binary 2 usc2 |
| 670 | * @param [in] msg sms content |
| 671 | * @param [in] msg_len send sms length,max is 1024 |
| 672 | * @retval 0: success |
| 673 | * @retval other: fail |
| 674 | */ |
| 675 | int gsw_send_sms(char *phone_num, int char_set, char *msg, int msg_len) |
| 676 | { |
| 677 | |
| 678 | if(init_flag == 0) |
| 679 | { |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 680 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | if(phone_num == NULL || msg == NULL) |
| 684 | { |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 685 | return GSW_HAL_ARG_INVALID; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 686 | } |
| 687 | |
lichengzhang | dab5ee7 | 2025-07-21 18:03:00 +0800 | [diff] [blame] | 688 | if(strlen((char *)msg) == 0 || strlen((char *)phone_num) == 0) |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 689 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 690 | LOGE(GSW_SMS,"strlen(telephony_num):%d", strlen((char *)phone_num)); |
| 691 | LOGE(GSW_SMS,"strlen(msg):%d", strlen((char *)msg)); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 692 | return GSW_HAL_ARG_INVALID; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 693 | } |
lichengzhang | dab5ee7 | 2025-07-21 18:03:00 +0800 | [diff] [blame] | 694 | if(strlen((char *)msg) > MSG_MAX_LEN || msg_len > MSG_MAX_LEN || strlen((char *)msg) != msg_len) |
| 695 | { |
| 696 | LOGE(GSW_SMS,"stelen is %d\n",strlen((char *)msg)); |
| 697 | LOGE(GSW_SMS,"the lenth is error\n"); |
| 698 | return GSW_HAL_NORMAL_FAIL; |
| 699 | } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 700 | |
| 701 | int err = -1; |
| 702 | |
| 703 | char smscPDU[30] = {0}; |
| 704 | char **pdu = NULL; |
| 705 | char smsc[4] = {0}; |
| 706 | char cmgs[MSM_NUMBER_MAX] = {0}; |
| 707 | char resp[RES_NUM_MIN] = {0}; |
| 708 | 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 |
| 709 | |
| 710 | kal_int32 msg_num = 0; |
| 711 | kal_int32 pdu_msg_len = 0; |
| 712 | kal_int32 status = MDAPI_RET_ERROR; |
| 713 | kal_int32 index = 0; |
| 714 | |
| 715 | if(char_set == 1) //8bit |
| 716 | { |
| 717 | ArrayToStr((unsigned char *)msg, (unsigned int)msg_len, msg_e_b); |
| 718 | status = _mdapi_sms_get_msg_num(msg_e_b, char_set, &msg_num, &pdu_msg_len); |
| 719 | } |
| 720 | |
| 721 | else //7bit usc2 |
| 722 | { |
| 723 | status = _mdapi_sms_get_msg_num((char *)msg, char_set, &msg_num, &pdu_msg_len); |
| 724 | } |
| 725 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 726 | 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] | 727 | if(status == MDAPI_RET_ERROR) |
| 728 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 729 | LOGE(GSW_SMS,"get message number failed\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 730 | //return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 731 | } |
| 732 | else |
| 733 | { |
| 734 | //allocate memery for **pdu |
| 735 | pdu = (char **)malloc(sizeof(char *) * msg_num); |
| 736 | if(pdu == NULL) |
| 737 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 738 | 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] | 739 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | else |
| 743 | { |
| 744 | for(index = 0; index < msg_num; index++) |
| 745 | { |
| 746 | pdu[index] = (char *)malloc(sizeof(char)*MAX_PDU_SIZE); |
| 747 | if(pdu[index] == NULL) |
| 748 | { |
| 749 | for(int i = 0; i < index; i++) |
| 750 | { |
| 751 | free(pdu[i]); |
| 752 | pdu[i] = NULL; |
| 753 | } |
| 754 | free(pdu); |
| 755 | pdu = NULL; |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 756 | 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] | 757 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 758 | } |
| 759 | else |
| 760 | { |
| 761 | memset(pdu[index], 0, MAX_PDU_SIZE); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 762 | 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] | 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | //allocate memory for **pdu success |
| 769 | if(index == msg_num) |
| 770 | { |
| 771 | if(char_set == 1)//8bit |
| 772 | { |
| 773 | smsPduEncode(smsc, (char *)phone_num, msg_e_b, char_set, smscPDU, pdu); |
| 774 | } |
| 775 | else |
| 776 | { |
| 777 | smsPduEncode(smsc, (char *)phone_num, (char *)msg, char_set, smscPDU, pdu); |
| 778 | } |
| 779 | for(index = 0; index < msg_num; index++) |
| 780 | { |
| 781 | char pdu_data[MAX_PDU_SIZE] = {0}; |
| 782 | char *p = pdu_data; |
| 783 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 784 | LOGE(GSW_SMS,"index:%d\n",index); |
| 785 | 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] | 786 | sprintf(p, "%s",smscPDU); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 787 | LOGE(GSW_SMS,"pdu_data:%s\n", pdu_data); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 788 | int sc = strlen(pdu_data); |
| 789 | sprintf(p+strlen(p), "%s", pdu[index]); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 790 | LOGE(GSW_SMS,"pdu_data:%s\n", pdu_data); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 791 | |
| 792 | int t = strlen(pdu_data); |
| 793 | sprintf(cmgs, "%d,%s", (t-sc)/2, pdu_data); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 794 | LOGE(GSW_SMS,"cmgs:%s\n", cmgs); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 795 | memset(resp, 0, sizeof(resp)); |
| 796 | |
| 797 | err = mbtk_sms_cmgf_set(sms_info_handle, 0); |
| 798 | if(err) |
| 799 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 800 | LOGE(GSW_SMS,"cmgf set error : %d\n", err); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 801 | for(index = 0; index < msg_num; index++){ |
| 802 | free(pdu[index]); |
| 803 | pdu[index] = NULL; |
| 804 | } |
| 805 | free(pdu); |
| 806 | pdu = NULL; |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 807 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 808 | } |
| 809 | else |
| 810 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 811 | LOGD(GSW_SMS,"cmgf set success\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp); |
| 815 | if(err) |
| 816 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 817 | LOGE(GSW_SMS,"cmgs send fail\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 818 | for(index = 0; index < msg_num; index++){ |
| 819 | free(pdu[index]); |
| 820 | pdu[index] = NULL; |
| 821 | } |
| 822 | free(pdu); |
| 823 | pdu = NULL; |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 824 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 825 | } |
| 826 | else |
| 827 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 828 | LOGD(GSW_SMS,"cmgs send success, resp:%s\n", resp); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | for(index = 0; index < msg_num; index++){ |
| 835 | free(pdu[index]); |
| 836 | pdu[index] = NULL; |
| 837 | } |
| 838 | free(pdu); |
| 839 | pdu = NULL; |
| 840 | |
| 841 | return GSW_HAL_SUCCESS; |
| 842 | } |
| 843 | |
| 844 | |
| 845 | /** |
| 846 | * @brief get smsc fuction * |
| 847 | * @param [in] len input buf len for smsc,max is 32 |
| 848 | * @param [out] smsc address for smsc get from this func * |
| 849 | * @retval 0: success |
| 850 | * @retval other: fail |
| 851 | */ |
| 852 | int gsw_get_smsc_address(int len, char *smsc) |
| 853 | { |
| 854 | int ret = -1; |
| 855 | int len_t = 0; |
| 856 | char smsc_temp[SMSC_MAX_LEN] = {0}; |
| 857 | char *p1, *p2 ,*substr; |
| 858 | |
| 859 | if(init_flag == 0) |
| 860 | { |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 861 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | if(smsc == NULL || len <= 0) |
| 865 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 866 | LOGE(GSW_SMS,"smsc is null or len = %d\n",len); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 867 | return GSW_HAL_ARG_INVALID; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | ret = mbtk_sms_csca_get(sms_info_handle, smsc_temp); |
| 871 | if(smsc_temp[0] == '\0') |
| 872 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 873 | LOGE(GSW_SMS,"gsw_get_smsc_address Error : %d\n", ret); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 874 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | else |
| 878 | { |
| 879 | p1 = strchr(smsc_temp, '\"'); |
| 880 | p2 = strrchr(smsc_temp, '\"'); |
| 881 | if (p1 && p2 && p2 > p1) |
| 882 | { |
| 883 | len_t = p2 - p1 - 1; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 884 | substr = malloc(len_t + 1); |
| 885 | if (NULL == substr) |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 886 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 887 | LOGE(GSW_SMS,"substr = NULL, malloc faill!!!\n"); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 888 | return GSW_HAL_NO_MEMORY; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 889 | } |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 890 | strncpy(substr, p1 + 1, len_t); |
| 891 | substr[len_t] = '\0'; |
lichengzhang | 12647e5 | 2025-07-21 17:23:10 +0800 | [diff] [blame] | 892 | strncpy(smsc, substr, len); |
| 893 | strncpy(sms_center_address, substr, len); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 894 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 895 | 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] | 896 | free(substr); |
| 897 | substr = NULL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 898 | } |
| 899 | else |
| 900 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 901 | LOGE(GSW_SMS,"String inside double quotes not found\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 902 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
| 906 | |
| 907 | return GSW_HAL_SUCCESS; |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * @brief set smsc fuction |
| 912 | * @param [out] smsc string value for smsc,max length is 32 * |
| 913 | * @retval 0: success |
| 914 | * @retval other: fail |
| 915 | */ |
| 916 | int gsw_set_smsc_address(const char *smsc) |
| 917 | { |
| 918 | int ret = -1; |
| 919 | |
| 920 | if(init_flag == 0) |
| 921 | { |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 922 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | if(smsc == NULL || strlen((char *)smsc) == 0) |
| 926 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 927 | LOGE(GSW_SMS,"smsc is null or empty\n"); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 928 | return GSW_HAL_ARG_INVALID; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | ret = mbtk_sms_csca_set(sms_info_handle, (char *)smsc); |
| 932 | |
| 933 | if(ret == 0) |
| 934 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 935 | LOGE(GSW_SMS,"set smsc success,smsc = %s\n",smsc); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 936 | } |
| 937 | else |
| 938 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 939 | LOGE(GSW_SMS,"set smsc fail,ret = %d\n", ret); |
xy.he | b41615b | 2025-05-28 16:33:20 +0800 | [diff] [blame] | 940 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | return GSW_HAL_SUCCESS; |
| 944 | } |