blob: ed9c1b3acf33bf6f902b63c0593145ab311de31e [file] [log] [blame]
b.liu1c74d692024-08-14 17:43:59 +08001#include "lynq_qser_sms.h"
2#include "mbtk_type.h"
3#include "mbtk_pdu_sms.h"
4#include "mbtk_info_api.h"
5#include "lynq_sms.h"
6#include "mbtk_log.h"
7
8
9#include <stdio.h>
10#include <stdlib.h>
11
12
13#define TELEPHONE_NUM_MAX 16
14#define MSM_NUMBER_MAX 1024+1
15#define RES_NUM_MIN 128
16
17
18#define DSC_to_msg(DSC) (DSC == 0 ? "Bit7" : (DSC == 1 ? "Bit8" : "UCS2"))
19
20static mbtk_info_handle_t* sms_info_handle = NULL;
21static char sms_center_address[128] = {0};
22
23typedef struct
24{
25 QSER_SMS_RxMsgHandlerFunc_t handlerPtr;
26 void* contextPtr;
27} lynq_sms_cb_func;
28
29static lynq_sms_cb_func lynq_sms_func_cb_handle;
30
31sms_client_handle_type g_sms_val = -1;
32
33
34void lynq_sms_state_change_cb(const void* data, int data_len)
35{
36 LOGV("sms_state_change_cb()----------start\n");
37 uint8 *ptr = (uint8*)data;
38 //printf("3sms_state_change_cb() : %s\n", ptr);
39
40 if (!strncmp("+CMT:", ptr, 5))//丢弃无用消息
41 return ;
42
43 struct SMS_Struct s = PDUDecoding(ptr);
44 /*
45 printf("服务中心地址: %s\n", s.SCA);
46 printf("发送方地址: %s\n", s.OA);
47 printf("服务中心时间戳: %s\n", s.SCTS);
48 printf("消息内容: %s\n", s.UD);
49 printf("数据编码方案: %s\n", DSC_to_msg(s.DCS));
50 */
51 QSER_SMS_Msg_t* tmp_data = NULL;
52
53 tmp_data = (QSER_SMS_Msg_t*)malloc(sizeof(QSER_SMS_Msg_t));
54 memset(tmp_data,0x00, sizeof(QSER_SMS_Msg_t));
55
56 tmp_data->format = s.DCS;
57 //printf("t数据编码方案: %d\n", tmp_data->format);
58 memcpy(tmp_data->src_addr, s.OA, strlen(s.OA));
59 //printf("t发送方地址: %s\n", tmp_data->src_addr);
60 memcpy(tmp_data->timestamp, s.SCTS, strlen(s.SCTS));
61 //printf("t服务中心时间戳: %s\n", tmp_data->timestamp);
62 tmp_data->sms_data_len = strlen(s.UD);
63 //printf("t消息内容长度: %d\n", tmp_data->sms_data_len);
64 memcpy(tmp_data->sms_data, s.UD, strlen(s.UD));
65 //printf("t消息内容: %s\n", tmp_data->sms_data);
66
67 lynq_sms_func_cb_handle.handlerPtr(tmp_data, NULL);
68
69 free(tmp_data);
70
71}
72
73
74int qser_sms_client_init(sms_client_handle_type *ph_sms)//out
75{
76 //UNUSED(ph_sms);
77
78 if(sms_info_handle == NULL)
79 {
80 sms_info_handle = mbtk_info_handle_get();
81 if(sms_info_handle)
82 {
83 LOGI("creat sms_info_handle is success");
84 *ph_sms = sms_info_handle->client_fd;
85 g_sms_val = sms_info_handle->client_fd;
86 }
87 else
88 {
89 LOGE("creat sms_info_handle is fail");
90 return -1;
91 }
92 }
93
94 return 0;
95}
96
97int qser_sms_client_deinit(sms_client_handle_type h_sms)//in
98{
99 //UNUSED(h_sms);
100 if (h_sms != g_sms_val || g_sms_val == -1)
101 {
102 LOGE("handle NULL\n");
103 return -1;
104 }
105
106 mbtk_sms_state_change_cb_reg(sms_info_handle, NULL);
107
108 if(sms_info_handle)
109 {
110 mbtk_info_handle_free(&sms_info_handle);
111 LOGI("deinit sms_info_handle is succuess");
112 }
113 else
114 {
115 LOGE("deinit sms_info_handle is fail");
116 return -1;
117 }
118
119 return 0;
120}
121
122int qser_sms_send_sms(sms_client_handle_type h_sms, QSER_sms_info_t *pt_sms_info)//in in 发送短信的内容、目的号码
123{
124 //UNUSED(h_sms);
125 //UNUSED(pt_sms_info);
126 if (h_sms != g_sms_val || g_sms_val == -1)
127 {
128 LOGE("handle NULL");
129 return -1;
130 }
131
132 if (pt_sms_info == NULL)
133 {
134 LOGE("QSER_sms_info_t NULL");
135 return -1;
136 }
137
138 if(sms_info_handle == NULL)
139 {
140 LOGE("qser_sms_send_sms sms_info_handle NULL");
141 return -1;
142 }
143
144 uint8_t *phone_num = NULL, *data = NULL;
145
146 char cmgs[MSM_NUMBER_MAX] = {0};
147 char resp[RES_NUM_MIN] = {0};
148 char pdu_data[512] = {0};
149 char phone_lenth[10] = {0};
150 char mem[10] = {0};
151 char *p = pdu_data;
152 int mode = 0;
153 int err = 0;
154 int i = 0;
155
156 data = pt_sms_info->sms_data;
157
158 phone_num = pt_sms_info->src_addr;
159
160 if (pt_sms_info->format == 0)//7
161 mode = 1;
162 else if (pt_sms_info->format == 1)//8
163 return SMS_ERR;
164 else if (pt_sms_info->format == 2)//USC2
165 mode = 0;
166 else
167 return -1;
168
169 //设置存储器
170/*
171 if (pt_sms_info->storage == 0)
172 {
173 strcpy(mem, "SM");
174 }
175 else if (pt_sms_info->storage == 1)
176 {
177 strcpy(mem, "ME");
178 }
179 else
180 {
181 LOGE("qser_sms_deletefromstorage storage is no Support");
182 return -1;
183 }
184
185 memset(resp, 0, sizeof(resp));
186 err = mbtk_sms_cpms_set(sms_info_handle, mem, resp);
187 if(err) {
188 LOGE("Error : %d", err);
189 } else {
190 LOGI("cpms set success. resp:%s", resp);
191 }
192*/
193 if(data == NULL)
194 {
195 LOGE("qser_sms_send_sms data NULL");
196 return -1;
197 }
198
199 if(phone_num == NULL)
200 {
201 LOGE("qser_sms_send_sms phone_num NULL");
202 return -1;
203 }
204
205 if(strlen(data) > 512 || strlen(data) == 0 || strlen(phone_num) == 0)
206 {
207 LOGE("strlen(telephony_num):%d", strlen(phone_num));
208 LOGE("strlen(msg):%d", strlen(data));
209 return -1;
210 }
211
212 if (mode == 0)// PDU
213 {
214#if 1
215 if (sms_center_address[0] == '\0')
216 {
217 memset(sms_center_address, 0, sizeof(sms_center_address));
218 memcpy(sms_center_address, "+8613800280500", strlen("+8613800280500"));
219 }
220
221 //printf("phone_num:%s\n", phone_num);
222 //printf("sms_center_address:%s\n", sms_center_address);
223 //printf("data:%s\n", data);
224
225 char* pdu = NULL;
226 char* smsc = SCAEncoding(sms_center_address);
227 struct PDUS *pdus = PDUEncoding(sms_center_address,phone_num, data, NULL);
228
229 for (i = 0; i < pdus->count; i++)
230 {
231 LOGI("第 %d 条:", i + 1);
232 LOGI("%s\n", pdus->PDU[i]);
233 pdu = pdus->PDU[i];
234 }
235
236 sprintf(p, "%s",smsc);
237 LOGI("pdu_data:%s", pdu_data);
238 sprintf(p+strlen(p), "%s", pdu);
239 LOGI("pdu_data:%s",pdu_data);
240
241 //sprintf(cmgs,"%d,%s",strlen(pdu_data), pdu_data);
242 int t = strlen(pdu_data);
243 sprintf(cmgs,"%d,%s",(t-18)/2, pdu_data);
244 LOGI("cmgs:%s", cmgs);
245#else
246 char *tmp_t = "0891683108200805F011000D91688189914026F3000800044F60597D";
247 sprintf(cmgs,"%d,%s",19,tmp_t);
248 printf("cmgs:%s\n", cmgs);
249#endif
250
251 memset(resp, 0, sizeof(resp));
252
253 err = mbtk_sms_cmgf_set(sms_info_handle, mode);
254 if(err)
255 {
256 LOGE("cmgf set error : %d", err);
257 }
258 else
259 {
260 LOGI("cmgf set success");
261 }
262
263 err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp);
264 if(err)
265 {
266 LOGE("Error : %d", err);
267 return -1;
268 }
269 else
270 {
271 LOGI("cmgs set success . resp:%s", resp);
272 }
273 }
274 else if (mode == 1) // text
275 {
276 err = mbtk_sms_cmgf_set(sms_info_handle, mode);
277 if(err)
278 {
279 LOGE("cmgf set error : %d", err);
280 }
281 else
282 {
283 LOGI("cmgf set success");
284 }
285
286 sprintf(cmgs,"%s,%s", phone_num, data);
287 LOGI("cmgs:%s", cmgs);
288
289 memset(resp, 0, sizeof(resp));
290
291 err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp);
292 if(err)
293 {
294 LOGE("Error : %d", err);
295 return -1;
296 }
297 else
298 {
299 LOGI("cmgs set success . resp:%s", resp);
300 }
301 }
302 else
303 {
304 LOGE("Error : mode");
305 return -1;
306 }
307
308 return 0;
309}
310
311//注册接收新短信
312int qser_sms_addrxmsghandler(QSER_SMS_RxMsgHandlerFunc_t handlerPtr, void* contextPtr)//in sms电话状态回调函数 in 主要是获取 上报 的内容
313{
314 //UNUSED(handlerPtr);
315 //UNUSED(contextPtr);
316
317 if(sms_info_handle == NULL)
318 {
319 LOGE("qser_sms_addrxmsghandler sms_info_handle NULL");
320 return -1;
321 }
322
323 int err = mbtk_sms_cnmi_set(sms_info_handle);
324 if(err)
325 {
326 LOGE("set cnmi fail");
327 return -1;
328 }
329
330 lynq_sms_func_cb_handle.handlerPtr = handlerPtr;
331 lynq_sms_func_cb_handle.contextPtr = contextPtr;
332
333 mbtk_sms_state_change_cb_reg(sms_info_handle, lynq_sms_state_change_cb);
334
335 return 0;
336}
337
338//删除短信
339int qser_sms_deletefromstorage(sms_client_handle_type h_sms, QSER_sms_storage_info_t *pt_sms_storage)//in in 删除短信的信息
340{
341 //UNUSED(h_sms);
342 //UNUSED(pt_sms_storage);
343 char cmgd[128] = {0};
344 int err = 0;
345
346 int t_storage = 0;
347 char mem[10] = {0};
348 char resp[RES_NUM_MIN] = {0};
349
350 if (h_sms != g_sms_val || g_sms_val == -1)
351 {
352 LOGE("handle NULL");
353 return -1;
354 }
355
356 if(pt_sms_storage == NULL)
357 {
358 LOGE("qser_sms_deletefromstorage pt_sms_storage NULL");
359 return -1;
360 }
361
362 if(sms_info_handle == NULL)
363 {
364 LOGE("qser_sms_deletefromstorage sms_info_handle NULL");
365 return -1;
366 }
367/*
368 t_storage = pt_sms_storage->storage; //设置存储器
369
370 if (t_storage == 0)
371 {
372 strncpy(mem, "SM", 2);
373 }
374 else if (t_storage == 1)
375 {
376 strncpy(mem, "ME", 2);
377 }
378 else
379 {
380 LOGE("qser_sms_deletefromstorage storage is no Support");
381 return -1;
382 }
383
384 memset(resp, 0, sizeof(resp));
385 err = mbtk_sms_cpms_set(sms_info_handle, mem, resp);
386 if(err) {
387 LOGE("Error : %d", err);
388 } else {
389 LOGI("cpms set success. resp:%s", resp);
390 }
391*/
392 uint32_t id_x = 0; //若后面其他产品是int类型则用宏控制
393 id_x = pt_sms_storage->storage_idx;//获取idx的值
394
395 if(id_x == -1) //delete all
396 {
397 memcpy(cmgd, "0,4", strlen("0,4"));
398 }
399 else
400 {
401 sprintf(cmgd,"%d",id_x);
402 }
403
404 LOGI("cmgd:%s", cmgd);
405
406 err = mbtk_sms_cmgd_set(sms_info_handle, cmgd);
407 if(err)
408 {
409 LOGE("qser_sms_deletefromstorage Error : %d", err);
410 return -1;
411 }
412 else
413 {
414 LOGI("qser_sms_deletefromstorage set success");
415 }
416
417 return 0;
418}
419
420//获取短信中心号码
421int qser_sms_getsmscenteraddress( sms_client_handle_type h_sms,QSER_sms_service_center_cfg_t *set_sca_cfg)//in out
422{
423 //UNUSED(h_sms);
424 //UNUSED(set_sca_cfg);
425 char sms_center_addree[254] = {0};
426 int len_t;
427 char *p1, *p2 ,*substr;
428
429 if (h_sms != g_sms_val || g_sms_val == -1)
430 {
431 LOGE("handle NULL");
432 return -1;
433 }
434
435 if (set_sca_cfg == NULL)
436 {
437 LOGE("QSER_sms_service_center_cfg_t NULL");
438 return -1;
439 }
440
441 if(sms_info_handle == NULL)
442 {
443 LOGE("qser_sms_getsmscenteraddress sms_info_handle NULL");
444 return -1;
445 }
446
447 int err = mbtk_sms_csca_get(sms_info_handle, sms_center_addree);
448 if(sms_center_addree[0] == '\0')
449 {
450 LOGE("qser_sms_getsmscenteraddress Error : %d", err);
451 return -1;
452 }
453 else
454 {
455 p1 = strchr(sms_center_addree, '\"');
456 p2 = strrchr(sms_center_addree, '\"');
457 if (p1 && p2 && p2 > p1)
458 {
459 len_t = p2 - p1 - 1;
460 char substr_t[len_t + 1];
461 strncpy(substr_t, p1 + 1, len_t);
462 substr_t[len_t] = '\0';
463
464 substr = substr_t;
465
466 memcpy(set_sca_cfg->service_center_addr, substr, strlen(substr));
467 memcpy(sms_center_address, substr, strlen(substr));
468
469 LOGI("qser_sms_getsmscenteraddress success");
470 }
471 else
472 {
473 LOGE("String inside double quotes not found");
474 return -1;
475 }
476 }
477
478 return 0;
479}
480
481//设置短信中心号码
482int qser_sms_setsmscenteraddress( sms_client_handle_type h_sms, QSER_sms_service_center_cfg_t *get_sca_cfg)//in in
483{
484 //UNUSED(h_sms);
485 //UNUSED(get_sca_cfg);
486 char *destNum = NULL;
487
488 if (h_sms != g_sms_val || g_sms_val == -1)
489 {
490 LOGE("handle NULL");
491 return -1;
492 }
493
494 if(sms_info_handle == NULL)
495 {
496 LOGE("qser_sms_setsmscenteraddress sms_info_handle NULL");
497 return -1;
498 }
499
500 if(get_sca_cfg == NULL)
501 {
502 LOGE("qser_sms_setsmscenteraddress get_sca_cfg NULL");
503 return -1;
504 }
505
506 destNum = get_sca_cfg->service_center_addr;
507
508 //printf("1destNum:%s\n", destNum);
509 memset(sms_center_address, 0, sizeof(sms_center_address));
510 memcpy(sms_center_address, destNum, strlen(destNum));
511
512 if (destNum == NULL)
513 {
514 LOGE("qser_sms_setsmscenteraddress destNum NULL");
515 return -1;
516 }
517
518 int err = mbtk_sms_csca_set(sms_info_handle, destNum);
519 if(err)
520 {
521 LOGE("Error : %d", err);
522 return -1;
523 }
524 else
525 {
526 // memset(sms_center_address, 0, sizeof(sms_center_address));
527 // memcpy(sms_center_address, destNum, strlen(destNum));
528 //printf("destNum:%s\n", destNum);
529 LOGI("qser_sms_setsmscenteraddress success");
530 }
531
532 return 0;
533}
534
535
536//显示删除列表
537int qser_sms_deletefromstoragelist( sms_client_handle_type h_sms, char* del_list)//in out
538{
539 //UNUSED(h_sms);
540 //UNUSED(get_sca_cfg);
541
542 if (h_sms != g_sms_val || g_sms_val == -1)
543 {
544 LOGE("handle NULL");
545 return -1;
546 }
547
548 if(sms_info_handle == NULL)
549 {
550 LOGE("qser_sms_deletefromstoragelist sms_info_handle NULL");
551 return -1;
552 }
553
554 int err = mbtk_sms_cmgd_get(sms_info_handle, del_list);
555 if(err > 0 && err != 300)//MBTK_INFO_ERR_SUCCESS
556 {
557 LOGE("Error : %d", err);
558 return -1;
559 }
560
561 return 0;
562}
563