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