blob: 1d28ce912a586e448cfb5f7da4ca8308fad0fb6c [file] [log] [blame]
#include "lynq_qser_sms.h"
#include "mbtk_type.h"
#include "mbtk_pdu_sms.h"
#include "mbtk_info_api.h"
#include "lynq_sms.h"
#include <stdio.h>
#include <stdlib.h>
#define TELEPHONE_NUM_MAX 16
#define MSM_NUMBER_MAX 1024+1
#define RES_NUM_MIN 128
#define DSC_to_msg(DSC) (DSC == 0 ? "Bit7" : (DSC == 1 ? "Bit8" : "UCS2"))
static mbtk_info_handle_t* sms_info_handle = NULL;
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;
sms_client_handle_type g_sms_val = -1;
void lynq_sms_state_change_cb(const void* data, int data_len)
{
LOGV("sms_state_change_cb()----------start\n");
uint8 *ptr = (uint8*)data;
//printf("3sms_state_change_cb() : %s\n", ptr);
if (!strncmp("+CMT:", ptr, 5))//丢弃无用消息
return ;
struct SMS_Struct s = PDUDecoding(ptr);
/*
printf("服务中心地址: %s\n", s.SCA);
printf("发送方地址: %s\n", s.OA);
printf("服务中心时间戳: %s\n", s.SCTS);
printf("消息内容: %s\n", s.UD);
printf("数据编码方案: %s\n", DSC_to_msg(s.DCS));
*/
QSER_SMS_Msg_t* tmp_data = NULL;
tmp_data = (QSER_SMS_Msg_t*)malloc(sizeof(QSER_SMS_Msg_t));
memset(tmp_data,0x00, sizeof(QSER_SMS_Msg_t));
tmp_data->format = s.DCS;
//printf("t数据编码方案: %d\n", tmp_data->format);
memcpy(tmp_data->src_addr, s.OA, strlen(s.OA));
//printf("t发送方地址: %s\n", tmp_data->src_addr);
memcpy(tmp_data->timestamp, s.SCTS, strlen(s.SCTS));
//printf("t服务中心时间戳: %s\n", tmp_data->timestamp);
tmp_data->sms_data_len = strlen(s.UD);
//printf("t消息内容长度: %d\n", tmp_data->sms_data_len);
memcpy(tmp_data->sms_data, s.UD, strlen(s.UD));
//printf("t消息内容: %s\n", tmp_data->sms_data);
lynq_sms_func_cb_handle.handlerPtr(tmp_data, NULL);
free(tmp_data);
}
int qser_sms_client_init(sms_client_handle_type *ph_sms)//out
{
//UNUSED(ph_sms);
if(sms_info_handle == NULL)
{
sms_info_handle = mbtk_info_handle_get();
if(sms_info_handle)
{
printf("creat sms_info_handle is success\n");
*ph_sms = sms_info_handle->client_fd;
g_sms_val = sms_info_handle->client_fd;
}
else
{
printf("creat sms_info_handle is fail\n");
return -1;
}
}
return 0;
}
int qser_sms_client_deinit(sms_client_handle_type h_sms)//in
{
//UNUSED(h_sms);
if (h_sms != g_sms_val || g_sms_val == -1)
{
printf("handle NULL\n");
return -1;
}
if(sms_info_handle)
{
mbtk_info_handle_free(&sms_info_handle);
printf("deinit sms_info_handle is succuess\n");
}
else
{
printf("deinit sms_info_handle is fail\n");
return -1;
}
return 0;
}
int qser_sms_send_sms(sms_client_handle_type h_sms, QSER_sms_info_t *pt_sms_info)//in in 发送短信的内容、目的号码
{
//UNUSED(h_sms);
//UNUSED(pt_sms_info);
if (h_sms != g_sms_val || g_sms_val == -1)
{
printf("handle NULL\n");
return -1;
}
if (pt_sms_info == NULL)
{
printf("QSER_sms_info_t NULL\n");
return -1;
}
if(sms_info_handle == NULL)
{
printf("qser_sms_send_sms sms_info_handle NULL\n");
return -1;
}
//QSER_sms_info_t get_sms_info;
//get_sms_info.format = pt_sms_info->format;//格式,7/8/UCS2,分别对应英文/彩铃图片/中文
//get_sms_info.mode = pt_sms_info->mode;//指定存储位置
//get_sms_info.sms_data = pt_sms_info->sms_data;//数据
//get_sms_info.sms_data_len = pt_sms_info->sms_data_len;//数据长度
//get_sms_info.src_addr = pt_sms_info->src_addr;//电话号码
//get_sms_info.storage = pt_sms_info->storage;//存储位置
//get_sms_info.storage_index = pt_sms_info->storage_index;//索引
//get_sms_info.timestamp = pt_sms_info->timestamp;//时间戳
//get_sms_info.type = pt_sms_info->type;//起始/结束/广播
//get_sms_info.user_data_head = pt_sms_info->user_data_head;//长短信用户头部信息
//get_sms_info.user_data_head_valid = pt_sms_info->user_data_head_valid;//是否是长短信
uint8_t *phone_num = NULL, *data = NULL;
char cmgs[MSM_NUMBER_MAX] = {0};
char resp[RES_NUM_MIN] = {0};
char pdu_data[512] = {0};
char phone_lenth[10] = {0};
char *p = pdu_data;
int mode = 0;
int err = 0;
int i = 0;
data = pt_sms_info->sms_data;
phone_num = pt_sms_info->src_addr;
if (pt_sms_info->format == 0)//7
mode = 1;
else if (pt_sms_info->format == 1)//8
return SMS_ERR;
else if (pt_sms_info->format == 2)//USC2
mode = 0;
else
return -1;
if(data == NULL)
{
printf("qser_sms_send_sms data NULL\n");
return -1;
}
if(phone_num == NULL)
{
printf("qser_sms_send_sms phone_num NULL\n");
return -1;
}
if(strlen(data) > 512 || strlen(data) == 0 || strlen(phone_num) == 0)
{
printf("strlen(telephony_num):%d\n", strlen(phone_num));
printf("strlen(msg):%d\n", strlen(data));
return -1;
}
if (mode == 0)// PDU
{
#if 1
if (sms_center_address[0] == '\0')
{
memset(sms_center_address, 0, sizeof(sms_center_address));
memcpy(sms_center_address, "+8613800280500", strlen("+8613800280500"));
}
printf("phone_num:%s\n", phone_num);
printf("sms_center_address:%s\n", sms_center_address);
printf("data:%s\n", data);
char* pdu = NULL;
char* smsc = SCAEncoding(sms_center_address);
struct PDUS *pdus = PDUEncoding(sms_center_address,phone_num, data, NULL);
for (i = 0; i < pdus->count; i++)
{
printf("第 %d 条:\n", i + 1);
printf("%s\n", pdus->PDU[i]);
pdu = pdus->PDU[i];
}
sprintf(p, "%s",smsc);
printf("pdu_data:%s\n", pdu_data);
sprintf(p+strlen(p), "%s", pdu);
printf("pdu_data:%s\n",pdu_data);
//sprintf(cmgs,"%d,%s",strlen(pdu_data), pdu_data);
int t = strlen(pdu_data);
sprintf(cmgs,"%d,%s",(t-18)/2, pdu_data);
printf("cmgs:%s\n", cmgs);
#else
char *tmp_t = "0891683108200805F011000D91688189914026F3000800044F60597D";
sprintf(cmgs,"%d,%s",19,tmp_t);
printf("cmgs:%s\n", cmgs);
#endif
memset(resp, 0, sizeof(resp));
err = mbtk_sms_cmgf_set(sms_info_handle, mode);
if(err)
{
printf("cmgf set error : %d\n", err);
}
else
{
printf("cmgf set success\n");
}
err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp);
if(err)
{
printf("Error : %d\n", err);
return -1;
}
else
{
printf("cmgs set success . resp:%s\n", resp);
}
}
else if (mode == 1) // text
{
err = mbtk_sms_cmgf_set(sms_info_handle, mode);
if(err)
{
printf("cmgf set error : %d\n", err);
}
else
{
printf("cmgf set success\n");
}
sprintf(cmgs,"%s,%s", phone_num, data);
printf("cmgs:%s\n", cmgs);
memset(resp, 0, sizeof(resp));
err = mbtk_sms_cmgs_set(sms_info_handle, cmgs, resp);
if(err)
{
printf("Error : %d\n", err);
return -1;
}
else
{
printf("cmgs set success . resp:%s\n", resp);
}
}
else
{
printf("Error : mode\n");
return -1;
}
return 0;
}
//注册接收新短信
int qser_sms_addrxmsghandler(QSER_SMS_RxMsgHandlerFunc_t handlerPtr, void* contextPtr)//in sms电话状态回调函数 in 主要是获取 上报 的内容
{
//UNUSED(handlerPtr);
//UNUSED(contextPtr);
if(sms_info_handle == NULL)
{
printf("qser_sms_addrxmsghandler sms_info_handle NULL\n");
return -1;
}
int err = mbtk_sms_cnmi_set(sms_info_handle);
if(err)
{
printf("set cnmi fail\n");
return -1;
}
lynq_sms_func_cb_handle.handlerPtr = handlerPtr;
lynq_sms_func_cb_handle.contextPtr = contextPtr;
mbtk_sms_state_change_cb_reg(sms_info_handle, lynq_sms_state_change_cb);
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;
int id_x = -1;
if (h_sms != g_sms_val || g_sms_val == -1)
{
printf("handle NULL\n");
return -1;
}
if(pt_sms_storage == NULL)
{
printf("qser_sms_deletefromstorage pt_sms_storage NULL\n");
return -1;
}
if(sms_info_handle == NULL)
{
printf("qser_sms_deletefromstorage sms_info_handle NULL\n");
return -1;
}
id_x = pt_sms_storage->storage_idx;//获取idx的值
if(id_x == -1) //delete all
{
memcpy(cmgd, ",4", strlen(",4"));
}
else
{
sprintf(cmgd,"%d",id_x);
}
printf("cmgd:%s\n", cmgd);
err = mbtk_sms_cmgd_set(sms_info_handle, cmgd);
if(err)
{
printf("qser_sms_deletefromstorage Error : %d\n", err);
return -1;
}
else
{
printf("qser_sms_deletefromstorage set success\n");
}
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 != g_sms_val || g_sms_val == -1)
{
printf("handle NULL\n");
return -1;
}
if (set_sca_cfg == NULL)
{
printf("QSER_sms_service_center_cfg_t NULL\n");
return -1;
}
if(sms_info_handle == NULL)
{
printf("qser_sms_getsmscenteraddress sms_info_handle NULL\n");
return -1;
}
int err = mbtk_sms_csca_get(sms_info_handle, sms_center_addree);
if(sms_center_addree[0] == '\0')
{
printf("qser_sms_getsmscenteraddress Error : %d\n", 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'; // 在新的字符串末尾添加结束符
//printf("双引号内的字符串为:%s\n", substr_t); // 输出结果
substr = substr_t;
memcpy(set_sca_cfg->service_center_addr, substr, strlen(substr));
memcpy(sms_center_address, substr, strlen(substr));
printf("qser_sms_getsmscenteraddress success\n");
}
else
{
printf("未找到双引号内的字符串\n"); // 输出结果
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 != g_sms_val || g_sms_val == -1)
{
printf("handle NULL\n");
return -1;
}
if(sms_info_handle == NULL)
{
printf("qser_sms_setsmscenteraddress sms_info_handle NULL\n");
return -1;
}
if(get_sca_cfg == NULL)
{
printf("qser_sms_setsmscenteraddress get_sca_cfg NULL\n");
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)
{
printf("qser_sms_setsmscenteraddress destNum NULL\n");
return -1;
}
int err = mbtk_sms_csca_set(sms_info_handle, destNum);
if(err)
{
printf("Error : %d\n", 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);
printf("qser_sms_setsmscenteraddress success\n");
}
return 0;
}