blob: 1c839a5ee39f6393bf0cd85c010c24770230e3e9 [file] [log] [blame]
#include "lynq_qser_sms.h"
#include "mbtk_type.h"
#include "mbtk_pdu_sms.h"
#include "lynq_sms.h"
#include "mbtk_log.h"
#include "mbtk_ril_api.h"
#include <stdio.h>
#include <stdlib.h>
#define TELEPHONE_NUM_MAX 16
#define MSM_NUMBER_MAX 2048+1
#define RES_NUM_MIN 128
#define HAL_SMS_CONTENT_LEN 1024
#define QSER_RESULT_SUCCESS 0
#define QSER_RESULT_FAIL -1
#define DSC_to_msg(DSC) (DSC == 0 ? "Bit7" : (DSC == 1 ? "Bit8" : "UCS2"))
static mbtk_ril_handle* qser_info_handle;
static const uint qser_h_sms = 0x5F6F7F8F;
static char sms_center_address[128] = {0};
typedef struct
{
QSER_SMS_RxMsgHandlerFunc_t handlerPtr;
void* contextPtr;
} lynq_sms_cb_func;
static lynq_sms_cb_func lynq_sms_func_cb_handle;
void lynq_sms_state_change_cb(const void* data, int data_len)
{
LOGV("sms_state_change_cb()----------start\n");
mbtk_ril_sms_state_info_t *ptr = (mbtk_ril_sms_state_info_t *)data;
//LOGD("3sms_state_change_cb() : %s\n", ptr->pdu);
QSER_SMS_Msg_t tmp_data;
memset(&tmp_data,0x00, sizeof(QSER_SMS_Msg_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[32] = {0};
int charset = 0;
int ret = -1;
int curr_pack = 1;
int total_pack = 1;
if(ptr == NULL)
{
LOGE("ptr is null");
}
LOGE("ptr: %s\n,data_len = %d\n", (char *)ptr->pdu, data_len);
if(data_len > MDAPI_MAX_PDU_SIZE)
{
strncpy(received_pdu, (const char *)ptr->pdu, MDAPI_MAX_PDU_SIZE-1);
}
else
{
strncpy(received_pdu, (const char *)ptr->pdu, data_len);
}
ret = smsPduDecode((const char *)received_pdu, data_len, num, smsc, msg, &charset, &curr_pack, &total_pack,date);
if(ret != 0)
{
LOGE("smsPduDecode fail ret: %d\n",ret);
return ;
}
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);
tmp_data.format = charset;
memcpy(tmp_data.src_addr, num, strlen(num));
tmp_data.sms_data_len = strlen(msg);
memcpy(tmp_data.sms_data, msg, strlen(msg));
if(total_pack > 1 && curr_pack < total_pack)
{
tmp_data.user_data_head_valid = TRUE;
tmp_data.user_data_head.total_segments = total_pack;
tmp_data.user_data_head.seg_number = curr_pack;
lynq_sms_func_cb_handle.handlerPtr(&tmp_data, &curr_pack);
}
else
{
tmp_data.user_data_head_valid = FALSE;
tmp_data.user_data_head.total_segments = total_pack;
tmp_data.user_data_head.seg_number = curr_pack;
lynq_sms_func_cb_handle.handlerPtr(&tmp_data, NULL);
}
}
int qser_sms_client_init(sms_client_handle_type *ph_sms)//out
{
//UNUSED(ph_voice);
if(ph_sms == NULL)
{
LOGE("[qser_sms] ph_sms is NULL.");
return QSER_RESULT_FAIL;
}
if(NULL == qser_info_handle)
{
qser_info_handle = mbtk_ril_open(MBTK_AT_PORT_DEF);
if(NULL == qser_info_handle)
{
LOGE("[qser_sms] qser_info_handle get fail.");
return QSER_RESULT_FAIL;
}
}
else
{
LOGE("[qser_sms] qser_info_handle has init.");
*ph_sms = qser_h_sms;
return QSER_RESULT_FAIL;
}
*ph_sms = qser_h_sms;
LOGE("[qser_sms] qser_info_handle get success.");
return QSER_RESULT_SUCCESS;
}
int qser_sms_client_deinit(sms_client_handle_type h_sms)//in
{
if(h_sms != qser_h_sms)
{
LOGE("[qser_sms] h_sms is error.");
return QSER_RESULT_FAIL;
}
if(qser_info_handle)
{
int ret = mbtk_ril_close(MBTK_AT_PORT_DEF);;
if(ret)
{
LOGE("[qser_sms] mbtk_info_handle_free() fail.");
return QSER_RESULT_FAIL;
}
else
{
qser_info_handle = NULL;
lynq_sms_func_cb_handle.handlerPtr = NULL;
}
}
else
{
LOGE("[qser_sms] handle not inited.");
return QSER_RESULT_FAIL;
}
return QSER_RESULT_SUCCESS;
}
int qser_sms_send_sms(sms_client_handle_type h_sms, QSER_sms_info_t *pt_sms_info)//in in 发送短信的内容、目的号码
{
if(h_sms != qser_h_sms)
{
LOGE("[qser_sms] h_sms is error.");
return QSER_RESULT_FAIL;
}
if (pt_sms_info == NULL)
{
LOGE("QSER_sms_info_t NULL");
return -1;
}
if(qser_info_handle == NULL)
{
LOGE("qser_sms_send_sms qser_info_handle NULL");
return -1;
}
int8 *phone_num = NULL;
int8 *msg = NULL;
int32 msg_len;
char cmgs[MSM_NUMBER_MAX] = {0};
char resp[RES_NUM_MIN] = {0};
char smscPDU[30] = {0};
char **pdu = NULL;
char smsc[4] = {0};
char msg_e_b[HAL_SMS_CONTENT_LEN] = {0};// +1 for end of string.*2:A char array contains two elements of a string for each value
int char_set = 0;
int lsms_flag = 0;
int err = 0;
int i = 0;
msg = (int8 *)pt_sms_info->sms_data;
msg_len = strlen(pt_sms_info->sms_data);
phone_num = (int8 *)pt_sms_info->src_addr;
lsms_flag = pt_sms_info->user_data_head_valid;
if(strcmp((char*)msg,"") == 0 || msg[0] == '\0')
{
LOGE("qser_sms_send_sms msg [%s]",msg);
return -1;
}
if(strcmp((char*)phone_num,"") == 0 || phone_num[0] == '\0')
{
LOGE("qser_sms_send_sms phone_num [%s]",phone_num);
return -1;
}
if (pt_sms_info->format == 0)//7
char_set = 0;
else if (pt_sms_info->format == 1)//8
char_set = 1;
else if (pt_sms_info->format == 2)//UCS2
char_set = 2;
else
{
LOGE("qser_sms_send_sms format error");
return -1;
}
//设置存储器
/*
if (pt_sms_info->storage == 0)
{
strcpy(mem, "SM");
}
else if (pt_sms_info->storage == 1)
{
strcpy(mem, "ME");
}
else
{
LOGE("qser_sms_deletefromstorage storage is no Support");
return -1;
}
memset(resp, 0, sizeof(resp));
err = mbtk_sms_cpms_set(qser_info_handle, mem, resp);
if(err) {
LOGE("Error : %d", err);
} else {
LOGI("cpms set success. resp:%s", resp);
}
*/
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("msg_len = [%d] ,msg_num=[%d]",msg_len, msg_num);
if(status == MDAPI_RET_ERROR)
{
LOGE("get message number failed");
return QSER_RESULT_FAIL;
}
else
{
if (lsms_flag == TRUE || lsms_flag == FALSE)
{
if (lsms_flag == FALSE && msg_num > 1)
{
LOGE("msg_num %d user head need for true",msg_num);
return QSER_RESULT_FAIL;
}
else if (lsms_flag == TRUE && msg_num > 4)
{
LOGE("Only 4 long SMS messages are supported");
return QSER_RESULT_FAIL;
}
}
else
{
LOGE("1:TRUE long sms 0:FALSE short message set user head error:%d",lsms_flag);
return QSER_RESULT_FAIL;
}
//allocate memery for **pdu
pdu = (char **)malloc(sizeof(char *) * msg_num);
if(pdu == NULL)
{
LOGE("allocate memory for pdu failed");
return QSER_RESULT_FAIL;
}
else
{
for(index = 0; index < msg_num; index++)
{
pdu[index] = (char *)malloc(sizeof(char)*MAX_PDU_SIZE);
if(pdu[index] == NULL)
{
for(i = 0; i < index; i++)
{
free(pdu[i]);
pdu[i] = NULL;
}
free(pdu);
pdu = NULL;
LOGE("allocate memory for pdu[%d] failed",index);
return QSER_RESULT_FAIL;
}
else
{
memset(pdu[index], 0, MAX_PDU_SIZE);
LOGE("pdu[%d} init value is: %s ",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("index:%d",index);
LOGE("smscPDU: %s, pdu: %s",smscPDU, pdu[index]);
sprintf(p, "%s",smscPDU);
LOGE("pdu_data:%s\n", pdu_data);
int sc = strlen(pdu_data);
sprintf(p+strlen(p), "%s", pdu[index]);
LOGE("pdu_data:%s", pdu_data);
int t = strlen(pdu_data);
sprintf(cmgs, "%d,%s", (t-sc)/2, pdu_data);
LOGE("cmgs:%s\n", cmgs);
memset(resp, 0, sizeof(resp));
err = mbtk_sms_cmgf_set(qser_info_handle, 0);
if(err)
{
LOGE("cmgf set error : %d", err);
for(index = 0; index < msg_num; index++){
free(pdu[index]);
pdu[index] = NULL;
}
free(pdu);
pdu = NULL;
return QSER_RESULT_FAIL;
}
else
{
LOGD("cmgf set success");
}
err = mbtk_sms_cmgs_set(qser_info_handle, cmgs, resp);
if(err)
{
LOGE("cmgs send fail (%d)",err);
for(index = 0; index < msg_num; index++){
free(pdu[index]);
pdu[index] = NULL;
}
free(pdu);
pdu = NULL;
return QSER_RESULT_FAIL;
}
else
{
LOGD("cmgs send success, resp:%s", resp);
}
}
}
for(index = 0; index < msg_num; index++){
free(pdu[index]);
pdu[index] = NULL;
}
free(pdu);
pdu = NULL;
return 0;
}
//注册接收新短信
int qser_sms_addrxmsghandler(QSER_SMS_RxMsgHandlerFunc_t handlerPtr, void* contextPtr)//in sms电话状态回调函数 in 主要是获取 上报 的内容
{
//UNUSED(handlerPtr);
//UNUSED(contextPtr);
if(qser_info_handle == NULL)
{
LOGE("qser_sms_addrxmsghandler qser_info_handle NULL");
return -1;
}
int err = mbtk_sms_cnmi_set(qser_info_handle);
if(err)
{
LOGE("set cnmi fail (%d)",err);
return -1;
}
lynq_sms_func_cb_handle.handlerPtr = handlerPtr;
lynq_sms_func_cb_handle.contextPtr = contextPtr;
err = mbtk_sms_state_change_cb_reg(lynq_sms_state_change_cb);
if(err)
{
LOGE("mbtk_sms_state_change_cb_reg fail (%d)",err);
return -1;
}
return 0;
}
//删除短信
int qser_sms_deletefromstorage(sms_client_handle_type h_sms, QSER_sms_storage_info_t *pt_sms_storage)//in in 删除短信的信息
{
//UNUSED(h_sms);
//UNUSED(pt_sms_storage);
char cmgd[128] = {0};
int err = 0;
if(h_sms != qser_h_sms)
{
LOGE("[qser_sms] h_sms is error.");
return QSER_RESULT_FAIL;
}
if(pt_sms_storage == NULL)
{
LOGE("qser_sms_deletefromstorage pt_sms_storage NULL");
return -1;
}
if(qser_info_handle == NULL)
{
LOGE("qser_sms_deletefromstorage qser_info_handle NULL");
return -1;
}
/*
t_storage = pt_sms_storage->storage; //设置存储器
if (t_storage == 0)
{
strncpy(mem, "SM", 2);
}
else if (t_storage == 1)
{
strncpy(mem, "ME", 2);
}
else
{
LOGE("qser_sms_deletefromstorage storage is no Support");
return -1;
}
memset(resp, 0, sizeof(resp));
err = mbtk_sms_cpms_set(qser_info_handle, mem, resp);
if(err) {
LOGE("Error : %d", err);
} else {
LOGI("cpms set success. resp:%s", resp);
}
*/
uint32_t id_x = 0; //若后面其他产品是int类型则用宏控制
id_x = pt_sms_storage->storage_idx;//获取idx的值
if(id_x == -1) //delete all
{
memcpy(cmgd, "0,4", strlen("0,4"));
}
else
{
sprintf(cmgd,"%d",id_x);
}
LOGI("cmgd:%s", cmgd);
err = mbtk_sms_cmgd_set(qser_info_handle, cmgd);
if(err)
{
LOGE("qser_sms_deletefromstorage Error : %d", err);
return -1;
}
else
{
LOGI("qser_sms_deletefromstorage set success");
}
return 0;
}
//获取短信中心号码
int qser_sms_getsmscenteraddress( sms_client_handle_type h_sms,QSER_sms_service_center_cfg_t *set_sca_cfg)//in out
{
//UNUSED(h_sms);
//UNUSED(set_sca_cfg);
char sms_center_addree[254] = {0};
int len_t;
char *p1, *p2 ,*substr;
if(h_sms != qser_h_sms)
{
LOGE("[qser_sms] h_sms is error.");
return QSER_RESULT_FAIL;
}
if (set_sca_cfg == NULL)
{
LOGE("QSER_sms_service_center_cfg_t NULL");
return -1;
}
if(qser_info_handle == NULL)
{
LOGE("qser_sms_getsmscenteraddress qser_info_handle NULL");
return -1;
}
int err = mbtk_sms_csca_get(qser_info_handle, sms_center_addree);
if(sms_center_addree[0] == '\0')
{
LOGE("qser_sms_getsmscenteraddress Error : %d", err);
return -1;
}
else
{
p1 = strchr(sms_center_addree, '\"');
p2 = strrchr(sms_center_addree, '\"');
if (p1 && p2 && p2 > p1)
{
len_t = p2 - p1 - 1;
char substr_t[len_t + 1];
strncpy(substr_t, p1 + 1, len_t);
substr_t[len_t] = '\0';
substr = substr_t;
memcpy(set_sca_cfg->service_center_addr, substr, strlen(substr));
memcpy(sms_center_address, substr, strlen(substr));
LOGI("qser_sms_getsmscenteraddress success");
}
else
{
LOGE("String inside double quotes not found");
return -1;
}
}
return 0;
}
//设置短信中心号码
int qser_sms_setsmscenteraddress( sms_client_handle_type h_sms, QSER_sms_service_center_cfg_t *get_sca_cfg)//in in
{
//UNUSED(h_sms);
//UNUSED(get_sca_cfg);
char *destNum = NULL;
if(h_sms != qser_h_sms)
{
LOGE("[qser_sms] h_sms is error.");
return QSER_RESULT_FAIL;
}
if(qser_info_handle == NULL)
{
LOGE("qser_sms_setsmscenteraddress qser_info_handle NULL");
return -1;
}
if(get_sca_cfg == NULL)
{
LOGE("qser_sms_setsmscenteraddress get_sca_cfg NULL");
return -1;
}
destNum = get_sca_cfg->service_center_addr;
//printf("1destNum:%s\n", destNum);
memset(sms_center_address, 0, sizeof(sms_center_address));
memcpy(sms_center_address, destNum, strlen(destNum));
if (destNum == NULL)
{
LOGE("qser_sms_setsmscenteraddress destNum NULL");
return -1;
}
int err = mbtk_sms_csca_set(qser_info_handle, destNum);
if(err)
{
LOGE("Error : %d", err);
return -1;
}
else
{
// memset(sms_center_address, 0, sizeof(sms_center_address));
// memcpy(sms_center_address, destNum, strlen(destNum));
//printf("destNum:%s\n", destNum);
LOGI("qser_sms_setsmscenteraddress success");
}
return 0;
}
//显示删除列表
int qser_sms_deletefromstoragelist( sms_client_handle_type h_sms, char* del_list)//in out
{
//UNUSED(h_sms);
//UNUSED(get_sca_cfg);
if(h_sms != qser_h_sms)
{
LOGE("[qser_sms] h_sms is error.");
return QSER_RESULT_FAIL;
}
if(qser_info_handle == NULL)
{
LOGE("qser_sms_deletefromstoragelist qser_info_handle NULL");
return -1;
}
int err = mbtk_sms_cmgd_get(qser_info_handle, del_list);
if(err > 0 && err != 300)//MBTK_INFO_ERR_SUCCESS
{
LOGE("Error : %d", err);
return -1;
}
return 0;
}