blob: 70a85ebffced772aa4400e7e6abfee06471cd3db [file] [log] [blame]
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <dlfcn.h>
#include <time.h>
#include "gsw_nw_interface.h"
#include "gsw_log_interface.h"
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16;
typedef uint32_t sms_client_handle_type;
typedef int kal_int32;
//qser include
//mbtk include
typedef void (*mbtk_info_callback_func)(const void* data, int data_len);
typedef unsigned char uint8;
typedef struct
{
int client_fd;
pthread_t read_thread_id;
int exit_fd[2];
bool is_waitting;
pthread_cond_t cond;
pthread_mutex_t mutex;
pthread_mutex_t send_mutex;
// Temp response data.
uint16 info_err;
uint16 data_len;
void *data;
//mbtk wyq for server_ready_status add start
char server_ready_status;
//mbtk wyq for server_ready_status add end
mbtk_info_callback_func net_state_cb;
mbtk_info_callback_func call_state_cb;
mbtk_info_callback_func sms_state_cb;
mbtk_info_callback_func radio_state_cb;
mbtk_info_callback_func sim_state_cb;
mbtk_info_callback_func pdp_state_cb;
//add signal by xr
mbtk_info_callback_func signal_state_cb;
} mbtk_info_handle_t;
// PDU include
enum EnumDCS {
BIT7 = 0, // GSM 字符集
BIT8 = 1, // ASCII字符集
UCS2 = 2 // Unicode 字符集
};
struct SMS_Struct {
char *SCA; // 服务中心地址
char *OA; // 发送方地址
char *SCTS; // 服务中心时间戳
struct UDHS *UDH; // 用户数据头
char *UD; // 用户数据
bool RP; // 应答路径
bool UDHI; // 用户数据头标识
bool SRI; // 状态报告指示
bool MMS; // 更多信息发送
int MTI; // 信息类型指示
char PID; // PID 协议标识
enum EnumDCS DCS; // 数据编码方案
bool TC; // 文本压缩指示 0: 未压缩 1:压缩
int MC; // 消息类型 -1: 无 1:移动设备特定类型 2:SIM特定类型 3:终端设备特定类型
};
struct PDUS {
unsigned int count;
char **PDU;
};
enum MDAPI_RET_e {
MDAPI_RET_SUCCESS = 0,
MDAPI_RET_ERROR = 1,
MDAPI_RET_TIMEOUT = 2,
MDAPI_RET_NOT_SUPPORT = 3,
};
#define MDAPI_TIME_STR_SIZE 22
#define MDAPI_PHONE_NUMBER_SIZE 32
#define MDAPI_MAX_PDU_SIZE 512
#define MSM_NUMBER_MAX 2048+1
#define RES_NUM_MIN 128
#define MIN_MSM_PARAM_NUM 4
#define MIN_IMS_MSM_PARAM_NUM 6
#define MIN_WRITMSM_PARAM_NUM 5
#define MSG_MAX_LEN 1024
#define TELEPHONNUM_LEN 64
#define STORAGSMS_MAX_SIZE 128
#define SMSC_MAX_LEN 100
#define SMS_NUM_MAX 255
#define MAX_OUT_SIZE 512
#define MAX_PDU_SIZE 512
#define MAX_DATE_SIZE 32
GSW_SMS_Callback_fun gsw_sms_callback = NULL;
#define lib_mbtk_path "/lib/libmbtk_lib.so"
#define lib_qser_sms_path "/lib/liblynq-qser-sms.so"
// 短信分片管理器结构体
typedef struct {
char msg_id[64]; // 短信唯一标识(号码+日期)
int total_segments; // 总分片数
int received_count; // 已接收分片数
char** segments; // 分片内容数组
time_t last_received; // 最后接收时间
} SmsReassembler;
#define MAX_SMS_REASSEMBLERS 10 // 最大同时处理的短信数量
#define MAX_SEGMENTS 255 // 最大分片数
#define SMS_TIMEOUT 300 // 5分钟超时(秒)
static SmsReassembler g_sms_reassemblers[MAX_SMS_REASSEMBLERS] = {0};
static mbtk_info_handle_t* (*mbtk_info_handle_get)(void);
int (*mbtk_sms_state_change_cb_reg)(mbtk_info_handle_t* handle, mbtk_info_callback_func cb);
static int (*mbtk_info_handle_free)(mbtk_info_handle_t** handle);
struct SMS_Struct (*PDUDecoding)(const char *data);
char *(*SCADecoding)(const char *data, int *EndIndex);
char *(*SCAEncoding)(char *SCA);
struct PDUS *(*PDUEncoding)(char *SCA, char *DA, char *UDC, struct UDHS *udhs);
int (*mbtk_sms_cmgf_set)(mbtk_info_handle_t* handle, int mode);
int (*mbtk_sms_cmgs_set)(mbtk_info_handle_t* handle, char * cmgs, char *resp);
int (*mbtk_sms_csca_set)(mbtk_info_handle_t* handle, char * csca);
int (*mbtk_sms_csca_get)(mbtk_info_handle_t* handle, char *buf);
int (*mbtk_sms_cnmi_set)(mbtk_info_handle_t* handle);
// static void (*mbtk_log)(int level, const char *format, ...);
// static void (*mbtk_log_init)(char *path, char *tag);
int (*smsPduEncode)(const char *smsc, const char *da_num, const char *msg, int charset, char *smsc_pdu, char **pdu);
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);
kal_int32 (*_mdapi_sms_get_msg_num)(const char *msg, int charset, kal_int32 *msg_num, kal_int32 *msg_len);
#define GSW_SMS "[HAL][GSW_SMS]"
static mbtk_info_handle_t* sms_info_handle = NULL;
static sms_client_handle_type handle;
static char sms_center_address[SMSC_MAX_LEN] = {0};
static void *dlHandle_mbtk;
static int init_flag = 0;
static int mbtk_sms_import()
{
dlHandle_mbtk = dlopen(lib_mbtk_path, RTLD_NOW);
if (dlHandle_mbtk == NULL)
{
return GSW_HAL_NORMAL_FAIL;
}
// mbtk_log_init = (void (*)(char *path, char *tag))dlsym(dlHandle_mbtk, "mbtk_log_init");
// if (mbtk_log_init == NULL)
// {
// return GSW_HAL_NORMAL_FAIL;
// }
// mbtk_log = (void (*)(int level, const char *format, ...))dlsym(dlHandle_mbtk, "mbtk_log");
// if (mbtk_log == NULL)
// {
// return GSW_HAL_NORMAL_FAIL;
// }
mbtk_info_handle_get = (mbtk_info_handle_t* (*)(void))dlsym(dlHandle_mbtk, "mbtk_info_handle_get");
if (mbtk_info_handle_get == NULL)
{
LOGE(GSW_SMS,"mbtk_info_handle_get dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
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");
if (mbtk_sms_state_change_cb_reg == NULL)
{
LOGE(GSW_SMS,"mbtk_sms_state_change_cb_reg dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_info_handle_free = (int (*)(mbtk_info_handle_t** handle))dlsym(dlHandle_mbtk, "mbtk_info_handle_free");
if (mbtk_info_handle_free == NULL)
{
LOGE(GSW_SMS,"mbtk_info_handle_free dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
PDUDecoding = (struct SMS_Struct (*)(const char *data))dlsym(dlHandle_mbtk, "PDUDecoding");
if (PDUDecoding == NULL)
{
LOGE(GSW_SMS,"PDUDecoding dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
SCADecoding = (char *(*)(const char *data, int *EndIndex))dlsym(dlHandle_mbtk, "SCADecoding");
if (SCADecoding == NULL)
{
LOGE(GSW_SMS,"SCADecoding dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
SCAEncoding = (char *(*)(char *SCA))dlsym(dlHandle_mbtk, "SCAEncoding");
if (SCAEncoding == NULL)
{
LOGE(GSW_SMS,"SCAEncoding dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
PDUEncoding = (struct PDUS *(*)(char *SCA, char *DA, char *UDC, struct UDHS *udhs))dlsym(dlHandle_mbtk, "PDUEncoding");
if (PDUEncoding == NULL)
{
LOGE(GSW_SMS,"PDUEncoding dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_sms_cmgf_set = (int (*)(mbtk_info_handle_t* handle, int mode))dlsym(dlHandle_mbtk, "mbtk_sms_cmgf_set");
if (mbtk_sms_cmgf_set == NULL)
{
LOGE(GSW_SMS,"mbtk_sms_cmgf_set dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_sms_cmgs_set = (int (*)(mbtk_info_handle_t* handle, char * cmgs, char *resp))dlsym(dlHandle_mbtk, "mbtk_sms_cmgs_set");
if (mbtk_sms_cmgs_set == NULL)
{
LOGE(GSW_SMS,"mbtk_sms_cmgs_set dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_sms_csca_set = (int (*)(mbtk_info_handle_t* handle, char * csca))dlsym(dlHandle_mbtk, "mbtk_sms_csca_set");
if (mbtk_sms_csca_set == NULL)
{
LOGE(GSW_SMS,"mbtk_sms_csca_set dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_sms_csca_get = (int (*)(mbtk_info_handle_t* handle, char *buf))dlsym(dlHandle_mbtk, "mbtk_sms_csca_get");
if (mbtk_sms_csca_get == NULL)
{
LOGE(GSW_SMS,"mbtk_sms_csca_get dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_sms_cnmi_set = (int (*)(mbtk_info_handle_t* handle))dlsym(dlHandle_mbtk, "mbtk_sms_cnmi_set");
if (mbtk_sms_cnmi_set == NULL)
{
LOGE(GSW_SMS,"mbtk_sms_cnmi_set dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
smsPduEncode = (int (*)(const char *smsc, const char *da_num, const char *msg, int charset, char *smsc_pdu, char **pdu))dlsym(dlHandle_mbtk, "smsPduEncode");
if (smsPduEncode == NULL)
{
LOGE(GSW_SMS,"smsPduEncode dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
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");
if (smsPduDecode == NULL)
{
LOGE(GSW_SMS,"smsPduDecode dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
_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");
if (_mdapi_sms_get_msg_num == NULL)
{
LOGE(GSW_SMS,"_mdapi_sms_get_msg_num dlsym fail");
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
static void generate_sms_id(char* dest, const char* num, const char* date) {
snprintf(dest, 64, "%.30s_%.30s", num, date);
}
// 查找或创建短信重组器
static SmsReassembler* get_sms_reassembler(const char* num, const char* date, int total_segments)
{
char msg_id[64];
generate_sms_id(msg_id, num, date);
time_t current_time = time(NULL);
for (int i = 0; i < MAX_SMS_REASSEMBLERS; i++)
{
if (g_sms_reassemblers[i].msg_id[0] != '\0')
{
// 检查超时
if (current_time - g_sms_reassemblers[i].last_received > SMS_TIMEOUT)
{
// 清理超时重组器
for (int j = 0; j < g_sms_reassemblers[i].total_segments; j++)
{
if (g_sms_reassemblers[i].segments[j])
{
free(g_sms_reassemblers[i].segments[j]);
}
}
free(g_sms_reassemblers[i].segments);
memset(&g_sms_reassemblers[i], 0, sizeof(SmsReassembler));
}
else if (strcmp(g_sms_reassemblers[i].msg_id, msg_id) == 0)
{
g_sms_reassemblers[i].last_received = current_time;
return &g_sms_reassemblers[i];
}
}
}
// 创建新的重组器
for (int i = 0; i < MAX_SMS_REASSEMBLERS; i++)
{
if (g_sms_reassemblers[i].msg_id[0] == '\0')
{
strncpy(g_sms_reassemblers[i].msg_id, msg_id, sizeof(g_sms_reassemblers[i].msg_id) - 1);
g_sms_reassemblers[i].total_segments = total_segments;
g_sms_reassemblers[i].received_count = 0;
g_sms_reassemblers[i].last_received = current_time;
// 分配分片数组内存
g_sms_reassemblers[i].segments = calloc(total_segments, sizeof(char*));
if (!g_sms_reassemblers[i].segments)
{
LOGE(GSW_SMS, "Memory allocation failed for segments array");
memset(&g_sms_reassemblers[i], 0, sizeof(SmsReassembler));
return NULL;
}
return &g_sms_reassemblers[i];
}
}
LOGE(GSW_SMS, "No free slots for new SMS reassembler");
return NULL;
}
// 重组短信
static char* reassemble_sms(SmsReassembler* reassembler)
{
size_t total_len = 0;
for (int i = 0; i < reassembler->total_segments; i++)
{
if (reassembler->segments[i])
{
total_len += strlen(reassembler->segments[i]);
}
}
char* full_msg = malloc(total_len + 1);
if (!full_msg)
{
LOGE(GSW_SMS, "Memory allocation failed for full message");
return NULL;
}
full_msg[0] = '\0';
for (int i = 0; i < reassembler->total_segments; i++)
{
if (reassembler->segments[i])
{
strcat(full_msg, reassembler->segments[i]);
}
}
return full_msg;
}
void ArrayToStr(unsigned char *Buff, unsigned int BuffLen, char *OutputStr)
{
int i = 0;
char TempBuff[MSG_MAX_LEN * 2 +1] = {0};
char strBuff[MSG_MAX_LEN * 2 +1] = {0};
for(i = 0; i<BuffLen;i++)
{
sprintf(TempBuff,"%02x",(unsigned char)Buff[i]);
strncat(strBuff,TempBuff,2);
}
strncpy(OutputStr, strBuff, BuffLen*2);
return;
}
void gsw_sms_state_change_cb(const void* data, int data_len)
{
LOGD(GSW_SMS,"gsw_sms_state_change_cb -------start\n");
uint8 *ptr = (uint8*)data;
if (!strncmp("+CMT:", (const char *)ptr, 5))//丢弃无用消息
return ;
gsw_sms_msg_type_t gsw_sms_msg;
gsw_sms_date_t gsw_sms_date;
memset(&gsw_sms_msg, 0, sizeof(gsw_sms_msg_type_t));
memset(&gsw_sms_date, 0, sizeof(gsw_sms_date_t));
char smsc[MDAPI_MAX_PDU_SIZE] = {0};
char received_pdu[MDAPI_MAX_PDU_SIZE] = {0};
char msg[MDAPI_MAX_PDU_SIZE] = {0};
char num[MDAPI_MAX_PDU_SIZE] = {0};
char date[MAX_DATE_SIZE] = {0};
int charset = 0;
int ret = -1;
int curr_pack = 1;
int total_pack = 1;
if(ptr == NULL)
{
LOGE(GSW_SMS,"ptr is null");
}
LOGE(GSW_SMS,"ptr: %s\n,data_len = %d\n", (char *)ptr, data_len);
if(data_len > MDAPI_MAX_PDU_SIZE)
{
strncpy(received_pdu, (const char *)ptr, MDAPI_MAX_PDU_SIZE-1);
}
else
{
strncpy(received_pdu, (const char *)ptr, data_len);
}
ret = smsPduDecode((const char *)received_pdu, data_len, num, smsc, msg, &charset, &curr_pack, &total_pack,date);
if(ret != 0)
{
LOGE(GSW_SMS,"smsPduDecode fail\n");
return ;
}
LOGE(GSW_SMS,"smsPduDecode ret: %d\n", ret);
LOGE(GSW_SMS,"SMS :%s, %s, %d\n", __FILE__, __FUNCTION__, __LINE__);
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);
gsw_sms_msg.content_encode = charset;
memcpy(gsw_sms_msg.src_num, num, strlen(num));
gsw_sms_msg.content_len = strlen(msg);
memcpy(gsw_sms_msg.content, msg, strlen(msg));
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)
{
LOGE(GSW_SMS,"sscanf failed\n");
}
snprintf((char *)gsw_sms_date.timezone, sizeof(gsw_sms_date.timezone), "%s","+8");
LOGE(GSW_SMS,"Year: %s\n", (char*)gsw_sms_date.year);
LOGE(GSW_SMS,"Month: %s\n", (char*)gsw_sms_date.month);
LOGE(GSW_SMS,"Day: %s\n", (char*)gsw_sms_date.day);
LOGE(GSW_SMS,"Hour: %s\n", (char*)gsw_sms_date.hour);
LOGE(GSW_SMS,"Minute: %s\n", (char*)gsw_sms_date.minutes);
LOGE(GSW_SMS,"Second: %s\n", (char*)gsw_sms_date.seconds);
gsw_sms_msg.date = gsw_sms_date;
if(gsw_sms_callback)
{
if(total_pack > 1)
{
SmsReassembler* reassembler = get_sms_reassembler(num, date, total_pack);
if (!reassembler)
{
LOGE(GSW_SMS, "Failed to get SMS reassembler");
return;
}
int seg_index = curr_pack - 1;
// 如果该分片尚未接收
if (!reassembler->segments[seg_index])
{
reassembler->segments[seg_index] = strdup(msg);
if (!reassembler->segments[seg_index])
{
LOGE(GSW_SMS, "Memory allocation failed for segment content");
return;
}
reassembler->received_count++;
}
// 检查是否所有分片都已接收
if (reassembler->received_count == reassembler->total_segments)
{
char* full_msg = reassemble_sms(reassembler);
if (full_msg)
{
gsw_sms_msg.content_encode = charset;
strncpy(gsw_sms_msg.src_num, num, sizeof(gsw_sms_msg.src_num) - 1);
gsw_sms_msg.content_len = strlen(full_msg);
size_t copy_size = (gsw_sms_msg.content_len < sizeof(gsw_sms_msg.content)) ?
gsw_sms_msg.content_len : sizeof(gsw_sms_msg.content) - 1;
strncpy(gsw_sms_msg.content, full_msg, copy_size);
gsw_sms_msg.content[copy_size] = '\0';
snprintf((char *)gsw_sms_date.timezone, sizeof(gsw_sms_date.timezone), "%s", "+8");
if (gsw_sms_callback)
gsw_sms_callback(GSW_SMS_FULL_FLG, &gsw_sms_msg);
free(full_msg);
}
for (int i = 0; i < reassembler->total_segments; i++)
{
if (reassembler->segments[i])
{
free(reassembler->segments[i]);
reassembler->segments[i] = NULL;
}
}
free(reassembler->segments);
reassembler->segments = NULL;
reassembler->received_count = 0;
reassembler->total_segments = 0;
memset(reassembler->msg_id, 0, sizeof(reassembler->msg_id));
}
}
else
gsw_sms_callback(GSW_SMS_FULL_FLG, &gsw_sms_msg);
}
}
/**
* @brief SDK interface to call back sms messages
* @param [in] handle_ptr
* @retval 0: success
* @retval other: fail
*/
int gsw_sms_reg_callback(GSW_SMS_Callback_fun handle_ptr)
{
if(init_flag == 0)
{
return -1;
}
if(handle_ptr == NULL)
{
LOGE(GSW_SMS,"gsw_sms_reg_callback fail,handle_ptr is null\n");
return -1;
}
else
{
gsw_sms_callback = handle_ptr;
}
return GSW_HAL_SUCCESS;
}
/**
* @brief sms sdk init
* @param [in] token
* @retval 0: success
* @retval other: fail
*/
int gsw_sms_sdk_init(int32_t token)
{
handle = token;
int ret = -1;
if(init_flag == 1)
{
LOGE(GSW_SMS,"has init sms sdk,return\n");
return GSW_HAL_SUCCESS;
}
ret = mbtk_sms_import();
if(ret != 0)
{
// if(mbtk_log != NULL)
// {
// LOGE(GSW_SMS,"[gsw_sms_sdk_init]mbtk_sms_import fail\n");
// }
return GSW_HAL_NORMAL_FAIL;
}
//mbtk_log_init("syslog", "MBTK_RIL");
sms_info_handle = mbtk_info_handle_get();
if(sms_info_handle == NULL)
{
LOGE(GSW_SMS,"[gsw_sms_sdk_init] mbtk handle init fail\n");
dlclose(dlHandle_mbtk);
return GSW_HAL_NORMAL_FAIL;
}
ret = mbtk_sms_cnmi_set(sms_info_handle);
if(ret != 0)
{
LOGE(GSW_SMS,"mbtk_sms_cnmi_set fail.[%d]\n", ret);
return GSW_HAL_NORMAL_FAIL;
}
ret = mbtk_sms_state_change_cb_reg(sms_info_handle, gsw_sms_state_change_cb);
if(ret != 0)
{
LOGE(GSW_SMS,"mbtk_sms_state_change_cb_reg fail.[%d]\n", ret);
if(sms_info_handle)
{
mbtk_info_handle_free(&sms_info_handle);
sms_info_handle = NULL;
}
return GSW_HAL_NORMAL_FAIL;
}
LOGD(GSW_SMS,"[gsw_sms_sdk_init]gsw_sms_sdk_init success\n");
init_flag = 1;
return GSW_HAL_SUCCESS;
}
/**
* @brief sms sdk deinit
* @param
* @retval 0: success
* @retval other: fail
*/
int gsw_sms_sdk_deinit(void)
{
int ret = -1;
if(init_flag == 0)
{
return -1;
}
LOGD(GSW_SMS,"callback clear\n");
gsw_sms_callback = NULL;
LOGD(GSW_SMS,"handle_free\n");
if(sms_info_handle != NULL)
{
ret = mbtk_info_handle_free(&sms_info_handle);
if(ret != 0)
{
LOGE(GSW_SMS,"[gsw_sms_sdk_deinit]mbtk_info_handle_free fail\n");
return GSW_HAL_NORMAL_FAIL;
}
sms_info_handle = NULL;
}
LOGD(GSW_SMS,"dlclose start\n");
if(dlHandle_mbtk == NULL)
{
LOGE(GSW_SMS,"[gsw_sms_sdk_deinit]dlHandle_sms is null\n");
}
else
{
dlclose(dlHandle_mbtk);
}
LOGD(GSW_SMS,"dlclose end\n");
init_flag = 0;
return GSW_HAL_SUCCESS;
}
/**
* @brief send sms fuction *
* @param [in] phone_num dest phone num send sms
* @param [in] char_set encode format for sms 0 7bit 1 binary 2 usc2
* @param [in] msg sms content
* @param [in] msg_len send sms length,max is 1024
* @retval 0: success
* @retval other: fail
*/
int gsw_send_sms(char *phone_num, int char_set, char *msg, int msg_len)
{
if(init_flag == 0)
{
return GSW_HAL_NORMAL_FAIL;
}
if(phone_num == NULL || msg == NULL)
{
return GSW_HAL_ARG_INVALID;
}
if(strlen((char *)msg) == 0 || strlen((char *)phone_num) == 0)
{
LOGE(GSW_SMS,"strlen(telephony_num):%d", strlen((char *)phone_num));
LOGE(GSW_SMS,"strlen(msg):%d", strlen((char *)msg));
return GSW_HAL_ARG_INVALID;
}
if(strlen((char *)msg) > MSG_MAX_LEN || msg_len > MSG_MAX_LEN || strlen((char *)msg) != msg_len)
{
LOGE(GSW_SMS,"stelen is %d\n",strlen((char *)msg));
LOGE(GSW_SMS,"the lenth is error\n");
return GSW_HAL_NORMAL_FAIL;
}
int err = -1;
char smscPDU[30] = {0};
char **pdu = NULL;
char smsc[4] = {0};
char cmgs[MSM_NUMBER_MAX] = {0};
char resp[RES_NUM_MIN] = {0};
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
kal_int32 msg_num = 0;
kal_int32 pdu_msg_len = 0;
kal_int32 status = MDAPI_RET_ERROR;
kal_int32 index = 0;
if(char_set == 1) //8bit
{
ArrayToStr((unsigned char *)msg, (unsigned int)msg_len, msg_e_b);
status = _mdapi_sms_get_msg_num(msg_e_b, char_set, &msg_num, &pdu_msg_len);
}
else //7bit usc2
{
status = _mdapi_sms_get_msg_num((char *)msg, char_set, &msg_num, &pdu_msg_len);
}
LOGE(GSW_SMS,"%s, %s, %d, msg_len = [%d] ,msg_num=[%d]\n", __FILE__, __FUNCTION__, __LINE__, msg_len, msg_num);
if(status == MDAPI_RET_ERROR)
{
LOGE(GSW_SMS,"get message number failed\n");
//return GSW_HAL_NORMAL_FAIL;
}
else
{
//allocate memery for **pdu
pdu = (char **)malloc(sizeof(char *) * msg_num);
if(pdu == NULL)
{
LOGE(GSW_SMS,"%s, %s, %d, allocate memory for pdu failed\n", __FILE__, __FUNCTION__, __LINE__);
return GSW_HAL_NORMAL_FAIL;
}
else
{
for(index = 0; index < msg_num; index++)
{
pdu[index] = (char *)malloc(sizeof(char)*MAX_PDU_SIZE);
if(pdu[index] == NULL)
{
for(int i = 0; i < index; i++)
{
free(pdu[i]);
pdu[i] = NULL;
}
free(pdu);
pdu = NULL;
LOGE(GSW_SMS,"%s, %s, %d, allocate memory for pdu[%d] failed\n", __FILE__, __FUNCTION__, __LINE__,index);
return GSW_HAL_NORMAL_FAIL;
}
else
{
memset(pdu[index], 0, MAX_PDU_SIZE);
LOGE(GSW_SMS,"%s, %s, %d, pdu[%d} init value is: %s \n", __FILE__, __FUNCTION__, __LINE__, index, pdu[index]);
}
}
}
}
//allocate memory for **pdu success
if(index == msg_num)
{
if(char_set == 1)//8bit
{
smsPduEncode(smsc, (char *)phone_num, msg_e_b, char_set, smscPDU, pdu);
}
else
{
smsPduEncode(smsc, (char *)phone_num, (char *)msg, char_set, smscPDU, pdu);
}
for(index = 0; index < msg_num; index++)
{
char pdu_data[MAX_PDU_SIZE] = {0};
char *p = pdu_data;
LOGE(GSW_SMS,"index:%d\n",index);
LOGE(GSW_SMS,"%s, %s, %d, smscPDU: %s, pdu: %s",__FILE__, __FUNCTION__, __LINE__, smscPDU, pdu[index]);
sprintf(p, "%s",smscPDU);
LOGE(GSW_SMS,"pdu_data:%s\n", pdu_data);
int sc = strlen(pdu_data);
sprintf(p+strlen(p), "%s", pdu[index]);
LOGE(GSW_SMS,"pdu_data:%s\n", pdu_data);
int t = strlen(pdu_data);
sprintf(cmgs, "%d,%s", (t-sc)/2, pdu_data);
LOGE(GSW_SMS,"cmgs:%s\n", cmgs);
memset(resp, 0, sizeof(resp));
err = mbtk_sms_cmgf_set(sms_info_handle, 0);
if(err)
{
LOGE(GSW_SMS,"cmgf set error : %d\n", err);
for(index = 0; index < msg_num; index++){
free(pdu[index]);
pdu[index] = NULL;
}
free(pdu);
pdu = NULL;
return GSW_HAL_NORMAL_FAIL;
}
else
{
LOGD(GSW_SMS,"cmgf set success\n");
}
err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp);
if(err)
{
LOGE(GSW_SMS,"cmgs send fail\n");
for(index = 0; index < msg_num; index++){
free(pdu[index]);
pdu[index] = NULL;
}
free(pdu);
pdu = NULL;
return GSW_HAL_NORMAL_FAIL;
}
else
{
LOGD(GSW_SMS,"cmgs send success, resp:%s\n", resp);
}
}
}
for(index = 0; index < msg_num; index++){
free(pdu[index]);
pdu[index] = NULL;
}
free(pdu);
pdu = NULL;
return GSW_HAL_SUCCESS;
}
/**
* @brief get smsc fuction *
* @param [in] len input buf len for smsc,max is 32
* @param [out] smsc address for smsc get from this func *
* @retval 0: success
* @retval other: fail
*/
int gsw_get_smsc_address(int len, char *smsc)
{
int ret = -1;
int len_t = 0;
char smsc_temp[SMSC_MAX_LEN] = {0};
char *p1, *p2 ,*substr;
if(init_flag == 0)
{
return GSW_HAL_NORMAL_FAIL;
}
if(smsc == NULL || len <= 0)
{
LOGE(GSW_SMS,"smsc is null or len = %d\n",len);
return GSW_HAL_ARG_INVALID;
}
ret = mbtk_sms_csca_get(sms_info_handle, smsc_temp);
if(smsc_temp[0] == '\0')
{
LOGE(GSW_SMS,"gsw_get_smsc_address Error : %d\n", ret);
return GSW_HAL_NORMAL_FAIL;
}
else
{
p1 = strchr(smsc_temp, '\"');
p2 = strrchr(smsc_temp, '\"');
if (p1 && p2 && p2 > p1)
{
len_t = p2 - p1 - 1;
substr = malloc(len_t + 1);
if (NULL == substr)
{
LOGE(GSW_SMS,"substr = NULL, malloc faill!!!\n");
return GSW_HAL_NO_MEMORY;
}
strncpy(substr, p1 + 1, len_t);
substr[len_t] = '\0';
strncpy(smsc, substr, len);
strncpy(sms_center_address, substr, len);
LOGE(GSW_SMS,"qser_sms_getsmscenteraddress success, smsc = %s\n", sms_center_address);
free(substr);
substr = NULL;
}
else
{
LOGE(GSW_SMS,"String inside double quotes not found\n");
return GSW_HAL_NORMAL_FAIL;
}
}
return GSW_HAL_SUCCESS;
}
/**
* @brief set smsc fuction
* @param [out] smsc string value for smsc,max length is 32 *
* @retval 0: success
* @retval other: fail
*/
int gsw_set_smsc_address(const char *smsc)
{
int ret = -1;
if(init_flag == 0)
{
return GSW_HAL_NORMAL_FAIL;
}
if(smsc == NULL || strlen((char *)smsc) == 0)
{
LOGE(GSW_SMS,"smsc is null or empty\n");
return GSW_HAL_ARG_INVALID;
}
ret = mbtk_sms_csca_set(sms_info_handle, (char *)smsc);
if(ret == 0)
{
LOGE(GSW_SMS,"set smsc success,smsc = %s\n",smsc);
}
else
{
LOGE(GSW_SMS,"set smsc fail,ret = %d\n", ret);
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}