blob: 1d28ce912a586e448cfb5f7da4ca8308fad0fb6c [file] [log] [blame]
b.liu4e243dc2023-11-27 11:20:00 +08001#include "lynq_qser_sms.h"
2#include "mbtk_type.h"
r.xiaodac82562023-12-07 18:22:32 -08003#include "mbtk_pdu_sms.h"
4#include "mbtk_info_api.h"
5#include "lynq_sms.h"
b.liu5fa9e772023-11-23 18:00:55 +08006
r.xiaodac82562023-12-07 18:22:32 -08007
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
19static mbtk_info_handle_t* sms_info_handle = NULL;
20static char sms_center_address[128] = {0};
21
r.xiaoefee0062023-12-29 19:35:43 -080022typedef struct
23{
24 QSER_SMS_RxMsgHandlerFunc_t handlerPtr;
25 void* contextPtr;
26} lynq_sms_cb_func;
27
28static lynq_sms_cb_func lynq_sms_func_cb_handle;
29
30sms_client_handle_type g_sms_val = -1;
31
r.xiaodac82562023-12-07 18:22:32 -080032
33void lynq_sms_state_change_cb(const void* data, int data_len)
34{
35 LOGV("sms_state_change_cb()----------start\n");
36 uint8 *ptr = (uint8*)data;
r.xiaoefee0062023-12-29 19:35:43 -080037 //printf("3sms_state_change_cb() : %s\n", ptr);
38
39 if (!strncmp("+CMT:", ptr, 5))//丢弃无用消息
40 return ;
r.xiaodac82562023-12-07 18:22:32 -080041
42 struct SMS_Struct s = PDUDecoding(ptr);
r.xiaoefee0062023-12-29 19:35:43 -080043 /*
r.xiaodac82562023-12-07 18:22:32 -080044 printf("服务中心地址: %s\n", s.SCA);
45 printf("发送方地址: %s\n", s.OA);
46 printf("服务中心时间戳: %s\n", s.SCTS);
47 printf("消息内容: %s\n", s.UD);
48 printf("数据编码方案: %s\n", DSC_to_msg(s.DCS));
r.xiaoefee0062023-12-29 19:35:43 -080049 */
50 QSER_SMS_Msg_t* tmp_data = NULL;
51
52 tmp_data = (QSER_SMS_Msg_t*)malloc(sizeof(QSER_SMS_Msg_t));
53 memset(tmp_data,0x00, sizeof(QSER_SMS_Msg_t));
54
55 tmp_data->format = s.DCS;
56 //printf("t数据编码方案: %d\n", tmp_data->format);
57 memcpy(tmp_data->src_addr, s.OA, strlen(s.OA));
58 //printf("t发送方地址: %s\n", tmp_data->src_addr);
59 memcpy(tmp_data->timestamp, s.SCTS, strlen(s.SCTS));
60 //printf("t服务中心时间戳: %s\n", tmp_data->timestamp);
61 tmp_data->sms_data_len = strlen(s.UD);
62 //printf("t消息内容长度: %d\n", tmp_data->sms_data_len);
63 memcpy(tmp_data->sms_data, s.UD, strlen(s.UD));
64 //printf("t消息内容: %s\n", tmp_data->sms_data);
65
66 lynq_sms_func_cb_handle.handlerPtr(tmp_data, NULL);
67
68 free(tmp_data);
69
r.xiaodac82562023-12-07 18:22:32 -080070}
71
72
73int qser_sms_client_init(sms_client_handle_type *ph_sms)//out
b.liu5fa9e772023-11-23 18:00:55 +080074{
r.xiaoefee0062023-12-29 19:35:43 -080075 //UNUSED(ph_sms);
b.liu5fa9e772023-11-23 18:00:55 +080076
r.xiaodac82562023-12-07 18:22:32 -080077 if(sms_info_handle == NULL)
78 {
79 sms_info_handle = mbtk_info_handle_get();
80 if(sms_info_handle)
81 {
82 printf("creat sms_info_handle is success\n");
r.xiaoefee0062023-12-29 19:35:43 -080083 *ph_sms = sms_info_handle->client_fd;
84 g_sms_val = sms_info_handle->client_fd;
r.xiaodac82562023-12-07 18:22:32 -080085 }
86 else
87 {
88 printf("creat sms_info_handle is fail\n");
r.xiaoefee0062023-12-29 19:35:43 -080089 return -1;
r.xiaodac82562023-12-07 18:22:32 -080090 }
91 }
92
r.xiaoefee0062023-12-29 19:35:43 -080093 return 0;
b.liu5fa9e772023-11-23 18:00:55 +080094}
95
r.xiaodac82562023-12-07 18:22:32 -080096int qser_sms_client_deinit(sms_client_handle_type h_sms)//in
b.liu5fa9e772023-11-23 18:00:55 +080097{
r.xiaoefee0062023-12-29 19:35:43 -080098 //UNUSED(h_sms);
99 if (h_sms != g_sms_val || g_sms_val == -1)
100 {
101 printf("handle NULL\n");
102 return -1;
103 }
104
b.liu5fa9e772023-11-23 18:00:55 +0800105
r.xiaodac82562023-12-07 18:22:32 -0800106 if(sms_info_handle)
107 {
r.xiaoefee0062023-12-29 19:35:43 -0800108 mbtk_info_handle_free(&sms_info_handle);
109 printf("deinit sms_info_handle is succuess\n");
r.xiaodac82562023-12-07 18:22:32 -0800110 }
111 else
112 {
113 printf("deinit sms_info_handle is fail\n");
r.xiaoefee0062023-12-29 19:35:43 -0800114 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800115 }
116
r.xiaoefee0062023-12-29 19:35:43 -0800117 return 0;
b.liu5fa9e772023-11-23 18:00:55 +0800118}
119
r.xiaodac82562023-12-07 18:22:32 -0800120int qser_sms_send_sms(sms_client_handle_type h_sms, QSER_sms_info_t *pt_sms_info)//in in 发送短信的内容、目的号码
b.liu5fa9e772023-11-23 18:00:55 +0800121{
r.xiaoefee0062023-12-29 19:35:43 -0800122 //UNUSED(h_sms);
r.xiaodac82562023-12-07 18:22:32 -0800123 //UNUSED(pt_sms_info);
r.xiaoefee0062023-12-29 19:35:43 -0800124 if (h_sms != g_sms_val || g_sms_val == -1)
125 {
126 printf("handle NULL\n");
127 return -1;
128 }
129
130
131 if (pt_sms_info == NULL)
132 {
133 printf("QSER_sms_info_t NULL\n");
134 return -1;
135 }
b.liu5fa9e772023-11-23 18:00:55 +0800136
r.xiaodac82562023-12-07 18:22:32 -0800137 if(sms_info_handle == NULL)
138 {
139 printf("qser_sms_send_sms sms_info_handle NULL\n");
r.xiaoefee0062023-12-29 19:35:43 -0800140 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800141 }
142
143 //QSER_sms_info_t get_sms_info;
144
145 //get_sms_info.format = pt_sms_info->format;//格式,7/8/UCS2,分别对应英文/彩铃图片/中文
146 //get_sms_info.mode = pt_sms_info->mode;//指定存储位置
147 //get_sms_info.sms_data = pt_sms_info->sms_data;//数据
148 //get_sms_info.sms_data_len = pt_sms_info->sms_data_len;//数据长度
149 //get_sms_info.src_addr = pt_sms_info->src_addr;//电话号码
150 //get_sms_info.storage = pt_sms_info->storage;//存储位置
151 //get_sms_info.storage_index = pt_sms_info->storage_index;//索引
152 //get_sms_info.timestamp = pt_sms_info->timestamp;//时间戳
153 //get_sms_info.type = pt_sms_info->type;//起始/结束/广播
154 //get_sms_info.user_data_head = pt_sms_info->user_data_head;//长短信用户头部信息
155 //get_sms_info.user_data_head_valid = pt_sms_info->user_data_head_valid;//是否是长短信
156
r.xiaoefee0062023-12-29 19:35:43 -0800157 uint8_t *phone_num = NULL, *data = NULL;
158
r.xiaodac82562023-12-07 18:22:32 -0800159 char cmgs[MSM_NUMBER_MAX] = {0};
160 char resp[RES_NUM_MIN] = {0};
161 char pdu_data[512] = {0};
162 char phone_lenth[10] = {0};
163 char *p = pdu_data;
164 int mode = 0;
165 int err = 0;
166 int i = 0;
167
168 data = pt_sms_info->sms_data;
169
170 phone_num = pt_sms_info->src_addr;
171
172 if (pt_sms_info->format == 0)//7
173 mode = 1;
174 else if (pt_sms_info->format == 1)//8
175 return SMS_ERR;
176 else if (pt_sms_info->format == 2)//USC2
177 mode = 0;
178 else
r.xiaoefee0062023-12-29 19:35:43 -0800179 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800180
181 if(data == NULL)
182 {
183 printf("qser_sms_send_sms data NULL\n");
r.xiaoefee0062023-12-29 19:35:43 -0800184 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800185 }
186
187 if(phone_num == NULL)
188 {
189 printf("qser_sms_send_sms phone_num NULL\n");
r.xiaoefee0062023-12-29 19:35:43 -0800190 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800191 }
192
r.xiaoefee0062023-12-29 19:35:43 -0800193
194 if(strlen(data) > 512 || strlen(data) == 0 || strlen(phone_num) == 0)
r.xiaodac82562023-12-07 18:22:32 -0800195 {
r.xiaoefee0062023-12-29 19:35:43 -0800196 printf("strlen(telephony_num):%d\n", strlen(phone_num));
197 printf("strlen(msg):%d\n", strlen(data));
198 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800199 }
200
r.xiaodac82562023-12-07 18:22:32 -0800201 if (mode == 0)// PDU
202 {
203#if 1
204 if (sms_center_address[0] == '\0')
205 {
206 memset(sms_center_address, 0, sizeof(sms_center_address));
207 memcpy(sms_center_address, "+8613800280500", strlen("+8613800280500"));
208 }
209
210 printf("phone_num:%s\n", phone_num);
211 printf("sms_center_address:%s\n", sms_center_address);
212 printf("data:%s\n", data);
213
214 char* pdu = NULL;
215 char* smsc = SCAEncoding(sms_center_address);
216 struct PDUS *pdus = PDUEncoding(sms_center_address,phone_num, data, NULL);
217
218 for (i = 0; i < pdus->count; i++)
219 {
220 printf("第 %d 条:\n", i + 1);
221 printf("%s\n", pdus->PDU[i]);
222 pdu = pdus->PDU[i];
223 }
224
225 sprintf(p, "%s",smsc);
226 printf("pdu_data:%s\n", pdu_data);
227 sprintf(p+strlen(p), "%s", pdu);
228 printf("pdu_data:%s\n",pdu_data);
229
230 //sprintf(cmgs,"%d,%s",strlen(pdu_data), pdu_data);
231 int t = strlen(pdu_data);
232 sprintf(cmgs,"%d,%s",(t-18)/2, pdu_data);
233 printf("cmgs:%s\n", cmgs);
234#else
235 char *tmp_t = "0891683108200805F011000D91688189914026F3000800044F60597D";
236 sprintf(cmgs,"%d,%s",19,tmp_t);
237 printf("cmgs:%s\n", cmgs);
238#endif
239
240 memset(resp, 0, sizeof(resp));
241
242 err = mbtk_sms_cmgf_set(sms_info_handle, mode);
243 if(err)
244 {
245 printf("cmgf set error : %d\n", err);
246 }
247 else
248 {
249 printf("cmgf set success\n");
250 }
251
252 err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp);
253 if(err)
254 {
255 printf("Error : %d\n", err);
r.xiaoefee0062023-12-29 19:35:43 -0800256 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800257 }
258 else
259 {
260 printf("cmgs set success . resp:%s\n", resp);
261 }
262 }
263 else if (mode == 1) // text
264 {
265 err = mbtk_sms_cmgf_set(sms_info_handle, mode);
266 if(err)
267 {
268 printf("cmgf set error : %d\n", err);
269 }
270 else
271 {
272 printf("cmgf set success\n");
273 }
274
275 sprintf(cmgs,"%s,%s", phone_num, data);
276 printf("cmgs:%s\n", cmgs);
277
278 memset(resp, 0, sizeof(resp));
279
280 err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp);
281 if(err)
282 {
283 printf("Error : %d\n", err);
r.xiaoefee0062023-12-29 19:35:43 -0800284 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800285 }
286 else
287 {
288 printf("cmgs set success . resp:%s\n", resp);
289 }
290 }
291 else
292 {
293 printf("Error : mode\n");
r.xiaoefee0062023-12-29 19:35:43 -0800294 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800295 }
r.xiaoefee0062023-12-29 19:35:43 -0800296
297 return 0;
b.liu5fa9e772023-11-23 18:00:55 +0800298}
299
r.xiaodac82562023-12-07 18:22:32 -0800300//注册接收新短信
301int qser_sms_addrxmsghandler(QSER_SMS_RxMsgHandlerFunc_t handlerPtr, void* contextPtr)//in sms电话状态回调函数 in 主要是获取 上报 的内容
b.liu5fa9e772023-11-23 18:00:55 +0800302{
r.xiaoefee0062023-12-29 19:35:43 -0800303 //UNUSED(handlerPtr);
304 //UNUSED(contextPtr);
b.liu5fa9e772023-11-23 18:00:55 +0800305
r.xiaodac82562023-12-07 18:22:32 -0800306 if(sms_info_handle == NULL)
307 {
308 printf("qser_sms_addrxmsghandler sms_info_handle NULL\n");
r.xiaoefee0062023-12-29 19:35:43 -0800309 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800310 }
311
312 int err = mbtk_sms_cnmi_set(sms_info_handle);
313 if(err)
314 {
315 printf("set cnmi fail\n");
r.xiaoefee0062023-12-29 19:35:43 -0800316 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800317 }
318
r.xiaoefee0062023-12-29 19:35:43 -0800319 lynq_sms_func_cb_handle.handlerPtr = handlerPtr;
320 lynq_sms_func_cb_handle.contextPtr = contextPtr;
321
r.xiaodac82562023-12-07 18:22:32 -0800322 mbtk_sms_state_change_cb_reg(sms_info_handle, lynq_sms_state_change_cb);
323
r.xiaoefee0062023-12-29 19:35:43 -0800324 return 0;
b.liu5fa9e772023-11-23 18:00:55 +0800325}
326
r.xiaodac82562023-12-07 18:22:32 -0800327//删除短信
328int qser_sms_deletefromstorage(sms_client_handle_type h_sms, QSER_sms_storage_info_t *pt_sms_storage)//in in 删除短信的信息
b.liu5fa9e772023-11-23 18:00:55 +0800329{
r.xiaoefee0062023-12-29 19:35:43 -0800330 //UNUSED(h_sms);
r.xiaodac82562023-12-07 18:22:32 -0800331 //UNUSED(pt_sms_storage);
332 char cmgd[128] = {0};
333 int err = 0;
334 int id_x = -1;
b.liu5fa9e772023-11-23 18:00:55 +0800335
r.xiaoefee0062023-12-29 19:35:43 -0800336 if (h_sms != g_sms_val || g_sms_val == -1)
r.xiaodac82562023-12-07 18:22:32 -0800337 {
r.xiaoefee0062023-12-29 19:35:43 -0800338 printf("handle NULL\n");
339 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800340 }
341
r.xiaoefee0062023-12-29 19:35:43 -0800342
r.xiaodac82562023-12-07 18:22:32 -0800343 if(pt_sms_storage == NULL)
344 {
345 printf("qser_sms_deletefromstorage pt_sms_storage NULL\n");
r.xiaoefee0062023-12-29 19:35:43 -0800346 return -1;
347 }
348
349 if(sms_info_handle == NULL)
350 {
351 printf("qser_sms_deletefromstorage sms_info_handle NULL\n");
352 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800353 }
354
355 id_x = pt_sms_storage->storage_idx;//获取idx的值
356
357 if(id_x == -1) //delete all
358 {
359 memcpy(cmgd, ",4", strlen(",4"));
360 }
361 else
362 {
363 sprintf(cmgd,"%d",id_x);
364 }
365
366 printf("cmgd:%s\n", cmgd);
367
368 err = mbtk_sms_cmgd_set(sms_info_handle, cmgd);
369 if(err)
370 {
371 printf("qser_sms_deletefromstorage Error : %d\n", err);
r.xiaoefee0062023-12-29 19:35:43 -0800372 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800373 }
374 else
375 {
376 printf("qser_sms_deletefromstorage set success\n");
377 }
378
r.xiaoefee0062023-12-29 19:35:43 -0800379 return 0;
b.liu5fa9e772023-11-23 18:00:55 +0800380}
381
r.xiaodac82562023-12-07 18:22:32 -0800382//获取短信中心号码
383int qser_sms_getsmscenteraddress( sms_client_handle_type h_sms,QSER_sms_service_center_cfg_t *set_sca_cfg)//in out
b.liu5fa9e772023-11-23 18:00:55 +0800384{
r.xiaoefee0062023-12-29 19:35:43 -0800385 //UNUSED(h_sms);
r.xiaodac82562023-12-07 18:22:32 -0800386 //UNUSED(set_sca_cfg);
387 char sms_center_addree[254] = {0};
388 int len_t;
389 char *p1, *p2 ,*substr;
b.liu5fa9e772023-11-23 18:00:55 +0800390
r.xiaoefee0062023-12-29 19:35:43 -0800391 if (h_sms != g_sms_val || g_sms_val == -1)
392 {
393 printf("handle NULL\n");
394 return -1;
395 }
396
397
398 if (set_sca_cfg == NULL)
399 {
400 printf("QSER_sms_service_center_cfg_t NULL\n");
401 return -1;
402 }
403
r.xiaodac82562023-12-07 18:22:32 -0800404
405 if(sms_info_handle == NULL)
406 {
407 printf("qser_sms_getsmscenteraddress sms_info_handle NULL\n");
r.xiaoefee0062023-12-29 19:35:43 -0800408 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800409 }
410
411 int err = mbtk_sms_csca_get(sms_info_handle, sms_center_addree);
412 if(sms_center_addree[0] == '\0')
413 {
414 printf("qser_sms_getsmscenteraddress Error : %d\n", err);
r.xiaoefee0062023-12-29 19:35:43 -0800415 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800416 }
417 else
418 {
419 p1 = strchr(sms_center_addree, '\"'); // 查找第一个双引号
420 p2 = strrchr(sms_center_addree, '\"'); // 查找最后一个双引号
421 if (p1 && p2 && p2 > p1) // 如果找到了两个双引号
422 {
423 len_t = p2 - p1 - 1; // 计算双引号内的字符串长度
424 char substr_t[len_t + 1]; // 定义一个新的字符串
425 strncpy(substr_t, p1 + 1, len_t); // 复制双引号内的字符串到新的字符串中
426 substr_t[len_t] = '\0'; // 在新的字符串末尾添加结束符
427 //printf("双引号内的字符串为:%s\n", substr_t); // 输出结果
428 substr = substr_t;
429
430 memcpy(set_sca_cfg->service_center_addr, substr, strlen(substr));
431 memcpy(sms_center_address, substr, strlen(substr));
432
433 printf("qser_sms_getsmscenteraddress success\n");
434 }
435 else
436 {
437 printf("未找到双引号内的字符串\n"); // 输出结果
r.xiaoefee0062023-12-29 19:35:43 -0800438 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800439 }
440 }
441
r.xiaoefee0062023-12-29 19:35:43 -0800442 return 0;
b.liu5fa9e772023-11-23 18:00:55 +0800443}
444
r.xiaodac82562023-12-07 18:22:32 -0800445//设置短信中心号码
446int qser_sms_setsmscenteraddress( sms_client_handle_type h_sms, QSER_sms_service_center_cfg_t *get_sca_cfg)//in in
b.liu5fa9e772023-11-23 18:00:55 +0800447{
r.xiaoefee0062023-12-29 19:35:43 -0800448 //UNUSED(h_sms);
r.xiaodac82562023-12-07 18:22:32 -0800449 //UNUSED(get_sca_cfg);
450 char *destNum = NULL;
r.xiaoefee0062023-12-29 19:35:43 -0800451
452 if (h_sms != g_sms_val || g_sms_val == -1)
453 {
454 printf("handle NULL\n");
455 return -1;
456 }
457
b.liu5fa9e772023-11-23 18:00:55 +0800458
r.xiaodac82562023-12-07 18:22:32 -0800459 if(sms_info_handle == NULL)
460 {
461 printf("qser_sms_setsmscenteraddress sms_info_handle NULL\n");
r.xiaoefee0062023-12-29 19:35:43 -0800462 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800463 }
464
465 if(get_sca_cfg == NULL)
466 {
467 printf("qser_sms_setsmscenteraddress get_sca_cfg NULL\n");
r.xiaoefee0062023-12-29 19:35:43 -0800468 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800469 }
470
471 destNum = get_sca_cfg->service_center_addr;
472
473 printf("1destNum:%s\n", destNum);
474 memset(sms_center_address, 0, sizeof(sms_center_address));
475 memcpy(sms_center_address, destNum, strlen(destNum));
476
477 if (destNum == NULL)
478 {
479 printf("qser_sms_setsmscenteraddress destNum NULL\n");
r.xiaoefee0062023-12-29 19:35:43 -0800480 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800481 }
482
483 int err = mbtk_sms_csca_set(sms_info_handle, destNum);
484 if(err)
485 {
486 printf("Error : %d\n", err);
r.xiaoefee0062023-12-29 19:35:43 -0800487 return -1;
r.xiaodac82562023-12-07 18:22:32 -0800488 }
489 else
490 {
491 // memset(sms_center_address, 0, sizeof(sms_center_address));
492 // memcpy(sms_center_address, destNum, strlen(destNum));
493 printf("destNum:%s\n", destNum);
494 printf("qser_sms_setsmscenteraddress success\n");
495 }
496
r.xiaoefee0062023-12-29 19:35:43 -0800497 return 0;
b.liu5fa9e772023-11-23 18:00:55 +0800498}
499