b.liu | 4e243dc | 2023-11-27 11:20:00 +0800 | [diff] [blame] | 1 | #include "lynq_qser_sms.h" |
| 2 | #include "mbtk_type.h" |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 3 | #include "mbtk_pdu_sms.h" |
| 4 | #include "mbtk_info_api.h" |
| 5 | #include "lynq_sms.h" |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 6 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | |
| 12 | #define TELEPHONE_NUM_MAX 16 |
| 13 | #define MSM_NUMBER_MAX 1024+1 |
| 14 | #define RES_NUM_MIN 128 |
| 15 | |
| 16 | |
| 17 | #define DSC_to_msg(DSC) (DSC == 0 ? "Bit7" : (DSC == 1 ? "Bit8" : "UCS2")) |
| 18 | |
| 19 | static mbtk_info_handle_t* sms_info_handle = NULL; |
| 20 | static char sms_center_address[128] = {0}; |
| 21 | |
| 22 | |
| 23 | void lynq_sms_state_change_cb(const void* data, int data_len) |
| 24 | { |
| 25 | LOGV("sms_state_change_cb()----------start\n"); |
| 26 | uint8 *ptr = (uint8*)data; |
| 27 | printf("3sms_state_change_cb() : %s\n", ptr); |
| 28 | |
| 29 | struct SMS_Struct s = PDUDecoding(ptr); |
| 30 | printf("服务中心地址: %s\n", s.SCA); |
| 31 | printf("发送方地址: %s\n", s.OA); |
| 32 | printf("服务中心时间戳: %s\n", s.SCTS); |
| 33 | printf("消息内容: %s\n", s.UD); |
| 34 | printf("数据编码方案: %s\n", DSC_to_msg(s.DCS)); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | int qser_sms_client_init(sms_client_handle_type *ph_sms)//out |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 39 | { |
| 40 | UNUSED(ph_sms); |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 41 | int ret = SMS_OK; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 42 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 43 | if(sms_info_handle == NULL) |
| 44 | { |
| 45 | sms_info_handle = mbtk_info_handle_get(); |
| 46 | if(sms_info_handle) |
| 47 | { |
| 48 | printf("creat sms_info_handle is success\n"); |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | printf("creat sms_info_handle is fail\n"); |
| 53 | ret = SMS_UNKNONW; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 58 | } |
| 59 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 60 | int qser_sms_client_deinit(sms_client_handle_type h_sms)//in |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 61 | { |
| 62 | UNUSED(h_sms); |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 63 | int ret = SMS_OK; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 64 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 65 | if(sms_info_handle) |
| 66 | { |
| 67 | ret = mbtk_info_handle_free(&sms_info_handle); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | printf("deinit sms_info_handle is fail\n"); |
| 72 | ret = SMS_UNKNONW; |
| 73 | } |
| 74 | |
| 75 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 76 | } |
| 77 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 78 | int qser_sms_send_sms(sms_client_handle_type h_sms, QSER_sms_info_t *pt_sms_info)//in in 发送短信的内容、目的号码 |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 79 | { |
| 80 | UNUSED(h_sms); |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 81 | //UNUSED(pt_sms_info); |
| 82 | int ret = SMS_OK; |
| 83 | uint8_t *phone_num = NULL, *data = NULL; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 84 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 85 | if(sms_info_handle == NULL) |
| 86 | { |
| 87 | printf("qser_sms_send_sms sms_info_handle NULL\n"); |
| 88 | return SMS_ERR; |
| 89 | } |
| 90 | |
| 91 | //QSER_sms_info_t get_sms_info; |
| 92 | |
| 93 | //get_sms_info.format = pt_sms_info->format;//格式,7/8/UCS2,分别对应英文/彩铃图片/中文 |
| 94 | //get_sms_info.mode = pt_sms_info->mode;//指定存储位置 |
| 95 | //get_sms_info.sms_data = pt_sms_info->sms_data;//数据 |
| 96 | //get_sms_info.sms_data_len = pt_sms_info->sms_data_len;//数据长度 |
| 97 | //get_sms_info.src_addr = pt_sms_info->src_addr;//电话号码 |
| 98 | //get_sms_info.storage = pt_sms_info->storage;//存储位置 |
| 99 | //get_sms_info.storage_index = pt_sms_info->storage_index;//索引 |
| 100 | //get_sms_info.timestamp = pt_sms_info->timestamp;//时间戳 |
| 101 | //get_sms_info.type = pt_sms_info->type;//起始/结束/广播 |
| 102 | //get_sms_info.user_data_head = pt_sms_info->user_data_head;//长短信用户头部信息 |
| 103 | //get_sms_info.user_data_head_valid = pt_sms_info->user_data_head_valid;//是否是长短信 |
| 104 | |
| 105 | char cmgs[MSM_NUMBER_MAX] = {0}; |
| 106 | char resp[RES_NUM_MIN] = {0}; |
| 107 | char pdu_data[512] = {0}; |
| 108 | char phone_lenth[10] = {0}; |
| 109 | char *p = pdu_data; |
| 110 | int mode = 0; |
| 111 | int err = 0; |
| 112 | int i = 0; |
| 113 | |
| 114 | data = pt_sms_info->sms_data; |
| 115 | |
| 116 | phone_num = pt_sms_info->src_addr; |
| 117 | |
| 118 | if (pt_sms_info->format == 0)//7 |
| 119 | mode = 1; |
| 120 | else if (pt_sms_info->format == 1)//8 |
| 121 | return SMS_ERR; |
| 122 | else if (pt_sms_info->format == 2)//USC2 |
| 123 | mode = 0; |
| 124 | else |
| 125 | return SMS_ERR; |
| 126 | |
| 127 | if(data == NULL) |
| 128 | { |
| 129 | printf("qser_sms_send_sms data NULL\n"); |
| 130 | return SMS_ERR; |
| 131 | } |
| 132 | |
| 133 | if(phone_num == NULL) |
| 134 | { |
| 135 | printf("qser_sms_send_sms phone_num NULL\n"); |
| 136 | return SMS_ERR; |
| 137 | } |
| 138 | |
| 139 | if((pt_sms_info->sms_data_len > 512) || (pt_sms_info->sms_data_len == 0)) |
| 140 | { |
| 141 | if(strlen(data) > 512 || strlen(data) == 0 || strlen(phone_num) == 0) |
| 142 | { |
| 143 | printf("strlen(telephony_num):%d\n", strlen(phone_num)); |
| 144 | printf("strlen(msg):%d\n", strlen(data)); |
| 145 | return SMS_ERR; |
| 146 | } |
| 147 | printf("strlen(msg_len):%d\n", pt_sms_info->sms_data_len); |
| 148 | return SMS_UNKNONW;//可能data_len没有赋值 |
| 149 | } |
| 150 | |
| 151 | |
| 152 | if (mode == 0)// PDU |
| 153 | { |
| 154 | #if 1 |
| 155 | if (sms_center_address[0] == '\0') |
| 156 | { |
| 157 | memset(sms_center_address, 0, sizeof(sms_center_address)); |
| 158 | memcpy(sms_center_address, "+8613800280500", strlen("+8613800280500")); |
| 159 | } |
| 160 | |
| 161 | printf("phone_num:%s\n", phone_num); |
| 162 | printf("sms_center_address:%s\n", sms_center_address); |
| 163 | printf("data:%s\n", data); |
| 164 | |
| 165 | char* pdu = NULL; |
| 166 | char* smsc = SCAEncoding(sms_center_address); |
| 167 | struct PDUS *pdus = PDUEncoding(sms_center_address,phone_num, data, NULL); |
| 168 | |
| 169 | for (i = 0; i < pdus->count; i++) |
| 170 | { |
| 171 | printf("第 %d 条:\n", i + 1); |
| 172 | printf("%s\n", pdus->PDU[i]); |
| 173 | pdu = pdus->PDU[i]; |
| 174 | } |
| 175 | |
| 176 | sprintf(p, "%s",smsc); |
| 177 | printf("pdu_data:%s\n", pdu_data); |
| 178 | sprintf(p+strlen(p), "%s", pdu); |
| 179 | printf("pdu_data:%s\n",pdu_data); |
| 180 | |
| 181 | //sprintf(cmgs,"%d,%s",strlen(pdu_data), pdu_data); |
| 182 | int t = strlen(pdu_data); |
| 183 | sprintf(cmgs,"%d,%s",(t-18)/2, pdu_data); |
| 184 | printf("cmgs:%s\n", cmgs); |
| 185 | #else |
| 186 | char *tmp_t = "0891683108200805F011000D91688189914026F3000800044F60597D"; |
| 187 | sprintf(cmgs,"%d,%s",19,tmp_t); |
| 188 | printf("cmgs:%s\n", cmgs); |
| 189 | #endif |
| 190 | |
| 191 | memset(resp, 0, sizeof(resp)); |
| 192 | |
| 193 | err = mbtk_sms_cmgf_set(sms_info_handle, mode); |
| 194 | if(err) |
| 195 | { |
| 196 | printf("cmgf set error : %d\n", err); |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | printf("cmgf set success\n"); |
| 201 | } |
| 202 | |
| 203 | err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp); |
| 204 | if(err) |
| 205 | { |
| 206 | printf("Error : %d\n", err); |
| 207 | ret = SMS_ERR; |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | printf("cmgs set success . resp:%s\n", resp); |
| 212 | } |
| 213 | } |
| 214 | else if (mode == 1) // text |
| 215 | { |
| 216 | err = mbtk_sms_cmgf_set(sms_info_handle, mode); |
| 217 | if(err) |
| 218 | { |
| 219 | printf("cmgf set error : %d\n", err); |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | printf("cmgf set success\n"); |
| 224 | } |
| 225 | |
| 226 | sprintf(cmgs,"%s,%s", phone_num, data); |
| 227 | printf("cmgs:%s\n", cmgs); |
| 228 | |
| 229 | memset(resp, 0, sizeof(resp)); |
| 230 | |
| 231 | err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp); |
| 232 | if(err) |
| 233 | { |
| 234 | printf("Error : %d\n", err); |
| 235 | ret = SMS_ERR; |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | printf("cmgs set success . resp:%s\n", resp); |
| 240 | } |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | printf("Error : mode\n"); |
| 245 | ret = SMS_ERR; |
| 246 | } |
| 247 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 248 | } |
| 249 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 250 | //注册接收新短信 |
| 251 | int qser_sms_addrxmsghandler(QSER_SMS_RxMsgHandlerFunc_t handlerPtr, void* contextPtr)//in sms电话状态回调函数 in 主要是获取 上报 的内容 |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 252 | { |
| 253 | UNUSED(handlerPtr); |
| 254 | UNUSED(contextPtr); |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 255 | int ret = SMS_OK; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 256 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 257 | if(sms_info_handle == NULL) |
| 258 | { |
| 259 | printf("qser_sms_addrxmsghandler sms_info_handle NULL\n"); |
| 260 | return SMS_ERR; |
| 261 | } |
| 262 | |
| 263 | int err = mbtk_sms_cnmi_set(sms_info_handle); |
| 264 | if(err) |
| 265 | { |
| 266 | printf("set cnmi fail\n"); |
| 267 | ret = SMS_ERR; |
| 268 | } |
| 269 | |
| 270 | mbtk_sms_state_change_cb_reg(sms_info_handle, lynq_sms_state_change_cb); |
| 271 | |
| 272 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 273 | } |
| 274 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 275 | //删除短信 |
| 276 | int qser_sms_deletefromstorage(sms_client_handle_type h_sms, QSER_sms_storage_info_t *pt_sms_storage)//in in 删除短信的信息 |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 277 | { |
| 278 | UNUSED(h_sms); |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 279 | //UNUSED(pt_sms_storage); |
| 280 | char cmgd[128] = {0}; |
| 281 | int err = 0; |
| 282 | int id_x = -1; |
| 283 | int ret = SMS_OK; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 284 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 285 | if(sms_info_handle == NULL) |
| 286 | { |
| 287 | printf("qser_sms_deletefromstorage sms_info_handle NULL\n"); |
| 288 | return SMS_ERR; |
| 289 | } |
| 290 | |
| 291 | if(pt_sms_storage == NULL) |
| 292 | { |
| 293 | printf("qser_sms_deletefromstorage pt_sms_storage NULL\n"); |
| 294 | return SMS_ERR; |
| 295 | } |
| 296 | |
| 297 | id_x = pt_sms_storage->storage_idx;//获取idx的值 |
| 298 | |
| 299 | if(id_x == -1) //delete all |
| 300 | { |
| 301 | memcpy(cmgd, ",4", strlen(",4")); |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | sprintf(cmgd,"%d",id_x); |
| 306 | } |
| 307 | |
| 308 | printf("cmgd:%s\n", cmgd); |
| 309 | |
| 310 | err = mbtk_sms_cmgd_set(sms_info_handle, cmgd); |
| 311 | if(err) |
| 312 | { |
| 313 | printf("qser_sms_deletefromstorage Error : %d\n", err); |
| 314 | ret = SMS_ERR; |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | printf("qser_sms_deletefromstorage set success\n"); |
| 319 | } |
| 320 | |
| 321 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 322 | } |
| 323 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 324 | //获取短信中心号码 |
| 325 | int qser_sms_getsmscenteraddress( sms_client_handle_type h_sms,QSER_sms_service_center_cfg_t *set_sca_cfg)//in out |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 326 | { |
| 327 | UNUSED(h_sms); |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 328 | //UNUSED(set_sca_cfg); |
| 329 | char sms_center_addree[254] = {0}; |
| 330 | int len_t; |
| 331 | char *p1, *p2 ,*substr; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 332 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 333 | int ret = SMS_OK; |
| 334 | |
| 335 | if(sms_info_handle == NULL) |
| 336 | { |
| 337 | printf("qser_sms_getsmscenteraddress sms_info_handle NULL\n"); |
| 338 | return SMS_ERR; |
| 339 | } |
| 340 | |
| 341 | int err = mbtk_sms_csca_get(sms_info_handle, sms_center_addree); |
| 342 | if(sms_center_addree[0] == '\0') |
| 343 | { |
| 344 | printf("qser_sms_getsmscenteraddress Error : %d\n", err); |
| 345 | return SMS_ERR; |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | p1 = strchr(sms_center_addree, '\"'); // 查找第一个双引号 |
| 350 | p2 = strrchr(sms_center_addree, '\"'); // 查找最后一个双引号 |
| 351 | if (p1 && p2 && p2 > p1) // 如果找到了两个双引号 |
| 352 | { |
| 353 | len_t = p2 - p1 - 1; // 计算双引号内的字符串长度 |
| 354 | char substr_t[len_t + 1]; // 定义一个新的字符串 |
| 355 | strncpy(substr_t, p1 + 1, len_t); // 复制双引号内的字符串到新的字符串中 |
| 356 | substr_t[len_t] = '\0'; // 在新的字符串末尾添加结束符 |
| 357 | //printf("双引号内的字符串为:%s\n", substr_t); // 输出结果 |
| 358 | substr = substr_t; |
| 359 | |
| 360 | memcpy(set_sca_cfg->service_center_addr, substr, strlen(substr)); |
| 361 | memcpy(sms_center_address, substr, strlen(substr)); |
| 362 | |
| 363 | printf("qser_sms_getsmscenteraddress success\n"); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | printf("未找到双引号内的字符串\n"); // 输出结果 |
| 368 | ret = SMS_ERR; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 373 | } |
| 374 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 375 | //设置短信中心号码 |
| 376 | int qser_sms_setsmscenteraddress( sms_client_handle_type h_sms, QSER_sms_service_center_cfg_t *get_sca_cfg)//in in |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 377 | { |
| 378 | UNUSED(h_sms); |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 379 | //UNUSED(get_sca_cfg); |
| 380 | char *destNum = NULL; |
| 381 | int ret = SMS_OK; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 382 | |
r.xiao | dac8256 | 2023-12-07 18:22:32 -0800 | [diff] [blame^] | 383 | if(sms_info_handle == NULL) |
| 384 | { |
| 385 | printf("qser_sms_setsmscenteraddress sms_info_handle NULL\n"); |
| 386 | return SMS_ERR; |
| 387 | } |
| 388 | |
| 389 | if(get_sca_cfg == NULL) |
| 390 | { |
| 391 | printf("qser_sms_setsmscenteraddress get_sca_cfg NULL\n"); |
| 392 | return SMS_ERR; |
| 393 | } |
| 394 | |
| 395 | destNum = get_sca_cfg->service_center_addr; |
| 396 | |
| 397 | printf("1destNum:%s\n", destNum); |
| 398 | memset(sms_center_address, 0, sizeof(sms_center_address)); |
| 399 | memcpy(sms_center_address, destNum, strlen(destNum)); |
| 400 | |
| 401 | if (destNum == NULL) |
| 402 | { |
| 403 | printf("qser_sms_setsmscenteraddress destNum NULL\n"); |
| 404 | return SMS_ERR; |
| 405 | } |
| 406 | |
| 407 | int err = mbtk_sms_csca_set(sms_info_handle, destNum); |
| 408 | if(err) |
| 409 | { |
| 410 | printf("Error : %d\n", err); |
| 411 | ret = SMS_ERR; |
| 412 | } |
| 413 | else |
| 414 | { |
| 415 | // memset(sms_center_address, 0, sizeof(sms_center_address)); |
| 416 | // memcpy(sms_center_address, destNum, strlen(destNum)); |
| 417 | printf("destNum:%s\n", destNum); |
| 418 | printf("qser_sms_setsmscenteraddress success\n"); |
| 419 | } |
| 420 | |
| 421 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 422 | } |
| 423 | |