LYNQ_SMS接口以及添加对应测试demo

Change-Id: I53acce0060e8e487e5e857665d11b0797845ac8e
diff --git a/mbtk/lynq_lib/src/lynq_sms.c b/mbtk/lynq_lib/src/lynq_sms.c
index 5699e52..c7fa721 100755
--- a/mbtk/lynq_lib/src/lynq_sms.c
+++ b/mbtk/lynq_lib/src/lynq_sms.c
@@ -1,57 +1,423 @@
 #include "lynq_qser_sms.h"
 #include "mbtk_type.h"
+#include "mbtk_pdu_sms.h"
+#include "mbtk_info_api.h"
+#include "lynq_sms.h"
 
-int qser_sms_client_init(sms_client_handle_type *ph_sms)
+
+#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};
+
+
+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);
+
+    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));
+}
+
+
+int qser_sms_client_init(sms_client_handle_type *ph_sms)//out
 {
     UNUSED(ph_sms);
+    int ret = SMS_OK;
 
-    return 0;
+    if(sms_info_handle == NULL)
+    {
+        sms_info_handle = mbtk_info_handle_get();
+        if(sms_info_handle)
+        {
+            printf("creat sms_info_handle is success\n");
+        }
+        else
+        {
+            printf("creat sms_info_handle is fail\n");
+            ret = SMS_UNKNONW;
+        }
+    }
+
+    return ret;
 }
 
-int qser_sms_client_deinit(sms_client_handle_type h_sms)
+int qser_sms_client_deinit(sms_client_handle_type h_sms)//in
 {
     UNUSED(h_sms);
+    int ret = SMS_OK;
 
-    return 0;
+    if(sms_info_handle)
+    {
+        ret = mbtk_info_handle_free(&sms_info_handle);
+    }
+    else
+    {
+        printf("deinit sms_info_handle is fail\n");
+        ret = SMS_UNKNONW;
+    }
+
+    return ret;
 }
 
-int qser_sms_send_sms(sms_client_handle_type h_sms, QSER_sms_info_t *pt_sms_info)
+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);
+    //UNUSED(pt_sms_info);
+    int ret = SMS_OK;
+    uint8_t *phone_num = NULL, *data = NULL;
 
-    return 0;
+    if(sms_info_handle == NULL)
+    {
+        printf("qser_sms_send_sms sms_info_handle NULL\n");
+        return SMS_ERR;
+    }
+
+    //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;//是否是长短信
+
+    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 SMS_ERR;
+
+    if(data == NULL)
+    {
+        printf("qser_sms_send_sms data NULL\n");
+        return SMS_ERR;
+    }
+
+    if(phone_num == NULL)
+    {
+        printf("qser_sms_send_sms phone_num NULL\n");
+        return SMS_ERR;
+    }
+
+    if((pt_sms_info->sms_data_len > 512) || (pt_sms_info->sms_data_len == 0))
+    {
+        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 SMS_ERR;
+        }
+        printf("strlen(msg_len):%d\n", pt_sms_info->sms_data_len);
+        return SMS_UNKNONW;//可能data_len没有赋值
+    }
+
+
+    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);
+            ret = SMS_ERR;
+        }
+        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);
+            ret = SMS_ERR;
+        }
+        else
+        {
+            printf("cmgs set success . resp:%s\n", resp);
+        }
+    }
+    else
+    {
+        printf("Error : mode\n");
+        ret = SMS_ERR;
+    }
+    return ret;
 }
 
-int qser_sms_addrxmsghandler(QSER_SMS_RxMsgHandlerFunc_t handlerPtr, void* contextPtr)
+//注册接收新短信
+int qser_sms_addrxmsghandler(QSER_SMS_RxMsgHandlerFunc_t handlerPtr, void* contextPtr)//in sms电话状态回调函数 in 主要是获取 上报 的内容
 {
     UNUSED(handlerPtr);
     UNUSED(contextPtr);
+    int ret = SMS_OK;
 
-    return 0;
+    if(sms_info_handle == NULL)
+    {
+        printf("qser_sms_addrxmsghandler sms_info_handle NULL\n");
+        return SMS_ERR;
+    }
+
+    int err = mbtk_sms_cnmi_set(sms_info_handle);
+    if(err)
+    {
+        printf("set cnmi fail\n");
+        ret = SMS_ERR;
+    }
+
+    mbtk_sms_state_change_cb_reg(sms_info_handle, lynq_sms_state_change_cb);
+
+    return ret;
 }
 
-int qser_sms_deletefromstorage(sms_client_handle_type h_sms, QSER_sms_storage_info_t *pt_sms_storage)
+//删除短信
+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);
+    //UNUSED(pt_sms_storage);
+    char cmgd[128] = {0};
+    int err = 0;
+    int id_x = -1;
+    int ret = SMS_OK;
 
-    return 0;
+    if(sms_info_handle == NULL)
+    {
+        printf("qser_sms_deletefromstorage sms_info_handle NULL\n");
+        return SMS_ERR;
+    }
+
+    if(pt_sms_storage == NULL)
+    {
+        printf("qser_sms_deletefromstorage pt_sms_storage NULL\n");
+        return SMS_ERR;
+    }
+
+    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);
+        ret = SMS_ERR;
+    }
+    else
+    {
+        printf("qser_sms_deletefromstorage set success\n");
+    }
+
+    return ret;
 }
 
-int qser_sms_getsmscenteraddress( sms_client_handle_type h_sms,QSER_sms_service_center_cfg_t *set_sca_cfg)
+//获取短信中心号码
+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);
+    //UNUSED(set_sca_cfg);
+    char sms_center_addree[254] = {0};
+    int len_t;
+    char *p1, *p2 ,*substr;
 
-    return 0;
+    int ret = SMS_OK;
+
+    if(sms_info_handle == NULL)
+    {
+        printf("qser_sms_getsmscenteraddress sms_info_handle NULL\n");
+        return SMS_ERR;
+    }
+
+    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 SMS_ERR;
+    }
+    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"); // 输出结果
+            ret = SMS_ERR;
+        }
+    }
+
+    return ret;
 }
 
-int qser_sms_setsmscenteraddress( sms_client_handle_type h_sms, QSER_sms_service_center_cfg_t *get_sca_cfg)
+//设置短信中心号码
+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);
+    //UNUSED(get_sca_cfg);
+    char *destNum = NULL;
+    int ret = SMS_OK;
 
-    return 0;
+    if(sms_info_handle == NULL)
+    {
+        printf("qser_sms_setsmscenteraddress sms_info_handle NULL\n");
+        return SMS_ERR;
+    }
+
+    if(get_sca_cfg == NULL)
+    {
+        printf("qser_sms_setsmscenteraddress get_sca_cfg NULL\n");
+        return SMS_ERR;
+    }
+
+    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 SMS_ERR;
+    }
+
+    int err = mbtk_sms_csca_set(sms_info_handle, destNum);
+    if(err)
+    {
+        printf("Error : %d\n", err);
+        ret = SMS_ERR;
+    }
+    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 ret;
 }