#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"


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;

}

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,BuffLen*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 && curr_pack < total_pack)
        // {
            gsw_sms_callback(GSW_SMS_RECEIVED_FLG, &gsw_sms_msg);
            LOGD(GSW_SMS,"the gsw_sms_state_e is %d\n",GSW_SMS_RECEIVED_FLG);
        // }
        // 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) > GSW_SMS_RECV_CONT_MAX || 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;
    }

    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';            
            memcpy(smsc, substr, strlen(substr));
            memcpy(sms_center_address, substr, strlen(substr));

            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;
}