T108: fix data_call

Change-Id: I48ac56b38bd7bbfaad8c310398bc3a1e7e2613d1
diff --git a/mbtk/mbtk_rild/src/mbtk_data_call.c b/mbtk/mbtk_rild/src/mbtk_data_call.c
new file mode 100644
index 0000000..3da0154
--- /dev/null
+++ b/mbtk/mbtk_rild/src/mbtk_data_call.c
@@ -0,0 +1,788 @@
+#if 1
+#include "mbtk_data_call.h"
+
+#define MBTK_RESULT_FAIL     -1
+#define MBTK_RESULT_SUCCESS  0
+
+#define MBTK_BUFF_SIZE         10
+#define MBTK_BUFF_STATE_INIT   "000000000"
+#define MBTK_BUFF_STATE_INIT_1 "010000000"
+#define MBTK_APN_STATE         "persist.qser.apn.state"
+#define MBTK_APN_AUTH_TYPE     "persist.qser.apn.auth"
+#define MBTK_APN_TYPE          "persist.qser.apn.type"
+
+#if (defined(MBTK_SG_SUPPORT) && defined(MBTK_ALL_CID_SUPPORT))
+//Setting apn parameters starts from channel 1
+#define MBTK_SET_APN_CID_MIN 1
+#else
+//Setting apn parameters starts from channel 2
+#define MBTK_SET_APN_CID_MIN 2
+#endif
+#define MBTK_SET_APN_CID_MAX 7
+
+/*cid 1 - 7*/
+mbtk_qser_apn_info_s apninfo_s[9];
+char apn_set_state[MBTK_BUFF_SIZE] = MBTK_BUFF_STATE_INIT;
+char apn_auth_type[MBTK_BUFF_SIZE] = MBTK_BUFF_STATE_INIT;
+mbtk_apn_auth_proto_enum apn_auth[9];
+int default_apn_cid = -1;
+bool default_pdp_state = false;
+bool apn_init_flag = false;
+
+int mbtk_get_default_apn_cid(void)
+{
+    return default_apn_cid;
+}
+
+void mbtk_set_default_apn_cid(int cid)
+{
+    default_apn_cid = cid;
+}
+
+bool mbtk_get_default_pdp_state(void)
+{
+    return default_pdp_state;
+}
+
+void mbtk_set_default_pdp_state(bool state, int cid)
+{
+    int default_cid = mbtk_get_default_apn_cid();
+#ifndef MBTK_SG_SUPPORT
+    if(default_cid == cid || cid == 1)
+#else
+    if(default_cid == cid)
+#endif
+    {
+        default_pdp_state = state;
+    }
+}
+
+
+int mbtk_check_default_pdp_state(int cid)
+{
+#ifdef MBTK_ALL_CID_SUPPORT
+    int default_cid = mbtk_get_default_apn_cid();
+#ifndef MBTK_SG_SUPPORT
+    if((default_cid == cid || cid == 1) && mbtk_get_default_pdp_state())
+#else
+    if(default_cid == cid && mbtk_get_default_pdp_state())
+#endif
+    {
+        LOGE("default pdp already open.default_cid [%d] cid [%d]", default_cid, cid);
+        return MBTK_RESULT_FAIL;
+    }
+#endif
+    return MBTK_RESULT_SUCCESS;
+}
+
+static int mbtk_get_cid(void)
+{
+    int i = 0;
+    for(i = MBTK_SET_APN_CID_MIN; i <= MBTK_SET_APN_CID_MAX; i++)
+    {
+        if(apn_set_state[i] == '0')
+        {
+            return i;
+        }
+    }
+
+    return MBTK_RESULT_FAIL;
+}
+
+
+int mbtk_check_cid(int cid)
+{
+    if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX)
+    {
+        LOGE("cid range error.");
+        return MBTK_RESULT_FAIL;
+    }
+
+#ifdef MBTK_ALL_CID_SUPPORT
+    if(apn_set_state[cid] != '1' && apn_set_state[cid] != '2')
+    {
+        LOGE("cid [%d] is not exist.", cid);
+        return MBTK_RESULT_FAIL;
+    }
+#endif
+    return MBTK_RESULT_SUCCESS;
+}
+
+int mbtk_qser_route_config(int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6)
+{
+    if(ipv4 == NULL || ipv6 == NULL)
+    {
+        LOGE("ip param is NULL.");
+        return MBTK_RESULT_FAIL;
+    }
+
+#ifdef MBTK_ALL_CID_SUPPORT
+    int default_cid = mbtk_get_default_apn_cid();
+#ifndef MBTK_SG_SUPPORT
+    if(default_cid != cid || cid != 1)
+#else
+    if(default_cid != cid)
+#endif
+#endif
+    {
+        LOGE("cid [%d] is default cid.", cid);
+        return MBTK_RESULT_FAIL;
+    }
+
+    char buf[1024] = {0};
+    char dns[128] = {0};
+    int offset = 0;
+    int fd = -1;
+    sprintf(buf, "route add default dev ccinet%d", cid -1);
+    system(buf);
+
+    memset(buf, 0x0, 1024);
+    memset(dns, 0x0, 128);
+    offset = sprintf(buf, "search lan\n");
+    if(ipv4->valid)
+    {
+        if(inet_ntop(AF_INET, &(ipv4->PrimaryDNS), dns, 32) == NULL) {
+            LOGE("[qser_data] PrimaryDNS error.");
+        } else {
+            LOGE("[qser_data] PrimaryDNS : %s", dns);
+        }
+        offset += sprintf(buf + offset, "nameserver %s\n", dns);
+        memset(dns, 0x0, 128);
+        if(inet_ntop(AF_INET, &(ipv4->SecondaryDNS), dns, 32) == NULL) {
+            LOGE("[qser_data] SecondaryDNS error.");
+        } else {
+            LOGE("[qser_data] SecondaryDNS : %s", dns);
+        }
+        offset += sprintf(buf + offset, "nameserver %s\n", dns);
+    }
+    if(ipv6->valid)
+    {
+        memset(dns, 0x0, 128);
+		if(ipv6_2_str(&(ipv6->PrimaryDNS), dns))
+        {
+			LOGE("[qser_data] PrimaryDNS error.");
+		} else {
+			LOGE("[qser_data] PrimaryDNS : %s", dns);
+		}
+        offset += sprintf(buf + offset, "nameserver %s\n", dns);
+        memset(dns, 0x0, 128);
+		if(ipv6_2_str(&(ipv6->SecondaryDNS), dns))
+        {
+			LOGE("[qser_data] SecondaryDNS error.");
+		} else {
+			LOGE("[qser_data] SecondaryDNS : %s", dns);
+		}
+        offset += sprintf(buf + offset, "nameserver %s\n", dns);
+    }
+
+    if(offset > 0)
+    {
+        fd = open("/tmp/resolv.conf", O_WRONLY | O_TRUNC);
+        if(fd < 0)
+        {
+            LOGE("[qser_data] mbtk_route_config : open fail.");
+            return MBTK_RESULT_FAIL;
+        }
+
+        int ret = write(fd, buf, offset);
+        if(ret < 0)
+        {
+            LOGE("[qser_data] mbtk_route_config : write fail.");
+        }
+
+        close(fd);
+    }
+
+    return MBTK_RESULT_SUCCESS;
+}
+
+
+static int mbtk_apn_check_num1(const uint8 *apn_type, const uint8 *apn_name, int cid)
+{
+    if(apn_type == NULL || apn_name == NULL)
+    {
+        LOGE("paran is NULL.");
+        return MBTK_RESULT_FAIL;
+    }
+
+    if(cid == 1)
+    {
+        LOGE("no need check.");
+        return MBTK_RESULT_SUCCESS;
+    }
+
+    int apn_name_length = strlen((char *)apn_name);
+    //LOGD("qser_apn_info[0].apn_name: %s!", qser_apn_info[0].apn_name);
+    char *p = strstr((char *)apninfo_s[1].apn_name, "mnc");
+    if(p == NULL)
+    {
+        LOGE("not find !");
+    }
+    else
+    {
+        int default_apn_name_length = p - ((char *)apninfo_s[1].apn_name) -1;
+        LOGD("[qser_data] qser_check_apn_type default_apn_name_length = [%d]!", default_apn_name_length);
+        if(default_apn_name_length == apn_name_length)
+        {
+            int i = 0;
+            while(i < default_apn_name_length)
+            {
+                if(isalpha(apninfo_s[1].apn_name[i]) && isalpha(apn_name[i]))
+                {
+                    if(tolower(apninfo_s[1].apn_name[i]) != tolower(apn_name[i]))
+                    {
+                        break;
+                    }
+                }
+                else
+                {
+                    //LOGE("[qser_data] not char!");
+                    if(apninfo_s[1].apn_name[i] != apn_name[i])
+                    {
+                        break;
+                    }
+                }
+                
+                i++;
+            }
+
+            if(i == default_apn_name_length)
+            {
+                LOGE("apn_name is same!cid = [1].");
+                return MBTK_RESULT_FAIL;
+            }
+        }
+    }
+
+    return MBTK_RESULT_SUCCESS;
+}
+
+static int mbtk_apn_check_num2_7(const uint8 *apn_type, const uint8 *apn_name, mbtk_ip_type_enum ip_type,  int cid)
+{
+    if(apn_type == NULL || apn_name == NULL)
+    {
+        LOGE("paran is NULL.");
+        return MBTK_RESULT_FAIL;
+    }
+
+    //check 2 - 7
+    int pdp_type_tmp = 1;
+    for (;pdp_type_tmp <= MBTK_APN_CID_MAX; pdp_type_tmp++)
+    {
+        if(pdp_type_tmp == cid)
+        {
+            continue;
+        }
+        if( (apn_set_state[pdp_type_tmp] == '1') || (apn_set_state[pdp_type_tmp] == '2') )
+        {
+            if(strcmp((char *)apninfo_s[pdp_type_tmp].apn_type, (char *)apn_type) == 0)
+            {
+                LOGE("apn_type is same!idx = [%d]", pdp_type_tmp);
+                return MBTK_RESULT_FAIL;
+            }
+            if (strcmp((char *)apninfo_s[pdp_type_tmp].apn_name, (char *)apn_name) == 0)
+            {
+                if(apninfo_s[pdp_type_tmp].ip_type == MBTK_IP_TYPE_IPV4V6 || ip_type == MBTK_IP_TYPE_IPV4V6)
+                {
+                    LOGE("[qser_data] apn_name is same and pdp_type is IPV4V6!idx = [%d]", pdp_type_tmp);
+                    return MBTK_RESULT_FAIL;
+                }
+
+                if(apninfo_s[pdp_type_tmp].ip_type == ip_type)
+                {
+                    LOGE("pdp_type is same and pdp_type is same!idx = [%d]", pdp_type_tmp);
+                    return MBTK_RESULT_FAIL;
+                }
+            }
+        }
+    }
+
+    return MBTK_RESULT_SUCCESS;
+}
+
+static int get_apn_user_pass_by_cid(mbtk_apn_info_t *apn)
+{
+    char prop_name[20] = {0};
+    char prop_data[300] = {0};
+    char local_ip[2] = {0};
+    char local_apn_name[128] = {0};
+    char local_apn_user[128] = {0};
+    char local_apn_pass[128] = {0};
+    char local_apn_auth[128] = {0};
+    sprintf(prop_name, "%s_%d",MBTK_APN_PROP,apn->cid);
+    if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data))
+    {
+        int i = 0;
+        int data_len = strlen(prop_data);
+        for(; i < data_len; i++)
+        {
+            if(prop_data[i] == ',')
+            {
+                prop_data[i] = ' ';
+            }
+        }
+        //LOGD("prop_data [%s].", prop_data);
+        int ret = sscanf(prop_data, " %s %s %s %s %s ", local_ip, local_apn_name, local_apn_user, local_apn_pass, local_apn_auth);
+        if(ret != 5)
+        {
+            LOGE("get user pass fail!ret [%d].", ret);
+            return MBTK_RESULT_FAIL;
+        }
+        
+        mbtk_ip_type_enum local_ip_type = (mbtk_ip_type_enum)atoi(&local_ip[0]);
+
+        if(apn->ip_type != local_ip_type || strcmp((char *)apn->apn, local_apn_name))
+        {
+            LOGE("APN Changed, not get user/pass/auth.");
+            return MBTK_RESULT_FAIL;
+        }
+
+        if(memcmp(local_apn_user, "NULL", 4))
+        { // Not "NULL"
+            memcpy(apn->user, local_apn_user, strlen(local_apn_user)); // user
+        }
+
+        if(memcmp(local_apn_pass, "NULL", 4))
+        { // Not "NULL"
+            memcpy(apn->pass, local_apn_pass, strlen(local_apn_pass)); // pass
+        }
+
+        return MBTK_RESULT_SUCCESS;
+    }
+    return MBTK_RESULT_FAIL;
+}
+
+
+int mbtk_qser_apn_del(int cid)
+{
+    char buff[32] = {0};
+    int ret = mbtk_check_cid(cid);
+    if(ret < 0)
+    {
+        LOGE("cid is exist");
+        return MBTK_RESULT_FAIL;
+    }
+
+    apn_auth_type[cid] = '0';
+    property_set(MBTK_APN_AUTH_TYPE, (char *)apn_auth_type);
+
+    if(apn_set_state[cid] == '2')
+    {
+        mbtk_set_default_apn_cid(-1);
+    }
+    apn_set_state[cid] = '0';
+    property_set(MBTK_APN_STATE, (char *)apn_set_state);
+
+    sprintf(buff, "%s%d", MBTK_APN_TYPE, cid);
+    property_set(buff, "NULL");
+    
+    memset(&apninfo_s[cid], 0x0, sizeof(mbtk_qser_apn_info_s));
+    return MBTK_RESULT_SUCCESS;
+}
+/*
+AT+CGDCONT?
++CGDCONT: 1,"IPV4V6","cmnet.MNC000.MCC460.GPRS","10.131.67.146 254.128.0.0.0.0.0.0.0.1.0.2.200.2.158.0",0,0,,,,
+
++CGDCONT: 8,"IPV4V6","IMS","254.128.0.0.0.0.0.0.0.1.0.2.200.2.160.160",0,0,0,2,1,1
+
+OK
+*/
+
+int mbtk_qser_req_apn_get(void *data, int *data_len, int *cme_err)
+{
+    ATResponse *response = NULL;
+    int err = at_send_command_multiline("AT+CGDCONT?", "+CGDCONT:", &response);
+
+    if (err < 0 || response->success == 0 || !response->p_intermediates){
+        *cme_err = at_get_cme_error(response);
+        goto exit;
+    }
+
+    ATLine* lines_ptr = response->p_intermediates;
+    char *line = NULL;
+    int tmp_int;
+    char *tmp_str = NULL;
+    char buff[32] = {0};
+    uint8 apn_type[128] = {0};
+    /*
+        <apn_num[1]><cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth[1]><apn_type_len[2]><apn_type_len>...
+                    <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth[1]><apn_type_len[2]><apn_type_len>
+    */
+    uint8* apn_num = (uint8*)data;
+    uint8* data_ptr = (uint8*)((uint8*)data + sizeof(uint8)); // Jump apn_num[1]
+    mbtk_apn_info_t apn;
+    while(lines_ptr)
+    {
+        line = lines_ptr->line;
+        err = at_tok_start(&line);
+        if (err < 0)
+        {
+            goto exit;
+        }
+
+        err = at_tok_nextint(&line, &tmp_int); // cid
+        if (err < 0)
+        {
+            goto exit;
+        }
+        // Only get CID 1-7
+        if(apn_set_state[tmp_int] != '0')
+        {
+            memset(&apn, 0x0, sizeof(mbtk_apn_info_t));
+            apn.cid = tmp_int;
+            *data_ptr++ = (uint8)tmp_int; // cid
+
+            err = at_tok_nextstr(&line, &tmp_str);// ip type
+            if (err < 0)
+            {
+                goto exit;
+            }
+            if(!strcasecmp(tmp_str, "IP")) {
+                *data_ptr++ = (uint8)MBTK_IP_TYPE_IP;
+                apn.ip_type = MBTK_IP_TYPE_IP;
+            } else if(!strcasecmp(tmp_str, "IPV6")) {
+                *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV6;
+                apn.ip_type = MBTK_IP_TYPE_IPV6;
+            } else if(!strcasecmp(tmp_str, "IPV4V6")) {
+                *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV4V6;
+                apn.ip_type = MBTK_IP_TYPE_IPV4V6;
+            } else {
+                *data_ptr++ = (uint8)MBTK_IP_TYPE_PPP;
+                apn.ip_type = MBTK_IP_TYPE_PPP;
+            }
+
+            err = at_tok_nextstr(&line, &tmp_str); // apn
+            if (err < 0)
+            {
+                goto exit;
+            }
+            if(str_empty(tmp_str)) {
+                uint16_2_byte((uint16)0, data_ptr, false);
+                data_ptr += sizeof(uint16);
+            } else {
+                uint16_2_byte((uint16)strlen(tmp_str), data_ptr, false);
+                data_ptr += sizeof(uint16);
+                memcpy(data_ptr, tmp_str, strlen(tmp_str));
+                data_ptr += strlen(tmp_str);
+                memcpy(apn.apn, tmp_str, strlen(tmp_str));
+            }
+
+            if(get_apn_user_pass_by_cid(&apn)) {
+                // user
+                uint16_2_byte((uint16)0, data_ptr, false);
+                data_ptr += sizeof(uint16);
+
+                // pass
+                uint16_2_byte((uint16)0, data_ptr, false);
+                data_ptr += sizeof(uint16);
+
+                // auth
+                *data_ptr++ = (uint8)MBTK_APN_AUTH_PROTO_DEFAULT;
+            } else {
+                // user
+                if(str_empty(apn.user)) {
+                    uint16_2_byte((uint16)0, data_ptr, false);
+                    data_ptr += sizeof(uint16);
+                } else {
+                    uint16_2_byte((uint16)strlen((char *)apn.user), data_ptr, false);
+                    data_ptr += sizeof(uint16);
+                    memcpy(data_ptr, apn.user, strlen((char *)apn.user));
+                    data_ptr += strlen((char *)apn.user);
+                }
+
+                // pass
+                if(str_empty(apn.pass)) {
+                    uint16_2_byte((uint16)0, data_ptr, false);
+                    data_ptr += sizeof(uint16);
+                } else {
+                    uint16_2_byte((uint16)strlen((char *)apn.pass), data_ptr, false);
+                    data_ptr += sizeof(uint16);
+                    memcpy(data_ptr, apn.pass, strlen((char *)apn.pass));
+                    data_ptr += strlen((char *)apn.pass);
+                }
+
+                // auth
+                switch(apn_auth_type[apn.cid])
+                {
+                    case '0':
+                    {
+                        *data_ptr++ = (uint8)MBTK_APN_AUTH_PROTO_DEFAULT;
+                        break;
+                    }
+                    case '1':
+                    {
+                        *data_ptr++ = (uint8)MBTK_APN_AUTH_PROTO_NONE;
+                        break;
+                    }
+                    case '2':
+                    {
+                        *data_ptr++ = (uint8)MBTK_APN_AUTH_PROTO_PAP;
+                        break;
+                    }
+                    case '3':
+                    {
+                        *data_ptr++ = (uint8)MBTK_APN_AUTH_PROTO_CHAP;
+                        break;
+                    }
+                    default:
+                    {
+                        *data_ptr++ = (uint8)MBTK_APN_AUTH_PROTO_DEFAULT;
+                        break;
+                    }
+                }
+            }
+
+            //apn_type
+            memset(buff, 0x0, 32);
+            sprintf(buff, "%s%d", MBTK_APN_TYPE, apn.cid);
+            property_get(buff, (char *)apn_type, "");
+            if(str_empty(apn_type))
+            {
+                uint16_2_byte((uint16)0, data_ptr, false);
+                data_ptr += sizeof(uint16);
+            } else
+            {
+                uint16_2_byte((uint16)strlen((char *)apn_type), data_ptr, false);
+                data_ptr += sizeof(uint16);
+                memcpy(data_ptr, apn_type, strlen((char *)apn_type));
+                data_ptr += strlen((char *)apn_type);
+            }
+
+            if(apn_init_flag)
+            {
+                apn_init_flag = false;
+                mbtk_qser_apn_save(apn, apn_type, false);
+            }
+            (*apn_num)++;
+        }
+
+        lines_ptr = lines_ptr->p_next;
+    }
+
+    *data_len = data_ptr - (uint8*)data;
+
+    goto exit;
+exit:
+    at_response_free(response);
+    return err;
+}
+
+
+int mbtk_qser_apn_save(mbtk_apn_info_t apn, const uint8 *apn_type, bool state_save)
+{
+    if(apn_type == NULL)
+    {
+        LOGE("param is error.");
+        return MBTK_RESULT_FAIL;
+    }
+
+    int ret = -1;
+    char buff[32] = {0};
+    int cid = apn.cid;
+    
+    memset(&apninfo_s[cid], 0x0, sizeof(mbtk_qser_apn_info_s));
+    apninfo_s[cid].cid = apn.cid;
+    apninfo_s[cid].ip_type = apn.ip_type;
+    apninfo_s[cid].auth_proto = apn_auth[cid];
+    if(strlen((char *)apn.apn))
+    {
+        memcpy(apninfo_s[cid].apn_name, apn.apn, strlen((char *)apn.apn));
+    }
+    if(strlen((char *)apn.user))
+    {
+        memcpy(apninfo_s[cid].user_name, apn.user, strlen((char *)apn.user));
+    }
+    if(strlen((char *)apn.pass))
+    {
+        memcpy(apninfo_s[cid].user_pass, apn.pass, strlen((char *)apn.pass));
+    }
+    if(strlen((char *)apn_type))
+    {
+        memcpy(apninfo_s[cid].apn_type, apn_type, strlen((char *)apn_type));
+        if(state_save)
+        {
+            sprintf(buff, "%s%d", MBTK_APN_TYPE, apn.cid);
+            property_set(buff, (char *)apn_type);
+        }
+    }
+
+    if(state_save)
+    {
+        if(strcmp((char *)apn_type, "iot_default") == 0)
+        {
+            apn_set_state[cid] = '2';
+            mbtk_set_default_apn_cid(cid);
+        }
+        else
+        {
+            apn_set_state[cid] = '1';
+        }
+        property_set(MBTK_APN_STATE, (char *)apn_set_state);
+
+        apn_auth_type[cid] = (char)apn_auth[cid] + '0';
+        property_set(MBTK_APN_AUTH_TYPE, (char *)apn_auth_type);
+    }
+    return MBTK_RESULT_SUCCESS;
+}
+
+int mbtk_strdata_to_apn(const uint8 *str, mbtk_apn_info_t *apn, uint8 *apn_type, mbtk_apn_req_type_enum *req_type)
+{
+    if(str == NULL || apn == NULL || apn_type == NULL)
+    {
+        LOGE("param is error.");
+        return MBTK_RESULT_FAIL;
+    }
+
+    // <cid[1]><ip_type[1]><req_type[1]><auth[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><apn_type[2]>
+    int len = 0;
+    int ret = -1;
+    memset(apn, 0x0, sizeof(mbtk_apn_info_t));
+    
+    // cid
+    apn->cid = (int)*str++;
+
+    // ip_type
+    apn->ip_type = (mbtk_ip_type_enum)(*str++);
+
+    //req_type
+    *req_type = (mbtk_apn_req_type_enum)(*str++);
+    if((*req_type) == MBTK_APN_REQ_TYPE_ADD)
+    {
+        apn->cid = mbtk_get_cid();
+        if(apn->cid < 0)
+        {
+            LOGE("no enough cid.");
+            return MBTK_RESULT_FAIL;
+        }
+    }
+    else
+    {
+        ret = mbtk_check_cid(apn->cid);
+        if(ret < 0)
+        {
+            LOGE("cid is error.");
+            return MBTK_RESULT_FAIL;
+        }
+        
+#ifndef MBTK_SG_SUPPORT
+        if(apn->cid == 1)
+        {
+            LOGE("cid 1 no support set.");
+            return MBTK_RESULT_FAIL;
+        }
+#endif
+    }
+
+    // auth
+    apn_auth[apn->cid] = (mbtk_apn_auth_proto_enum)(*str++);
+    if(apn_auth[apn->cid] == MBTK_APN_AUTH_PROTO_NONE || apn_auth[apn->cid] == MBTK_APN_AUTH_PROTO_DEFAULT)
+    {
+        memcpy(apn->auth,"NONE",strlen("NONE")+1);
+    }
+    else if(apn_auth[apn->cid] == MBTK_APN_AUTH_PROTO_PAP)
+    {
+        memcpy(apn->auth,"PAP",strlen("PAP")+1);
+    }
+    else if(apn_auth[apn->cid] == MBTK_APN_AUTH_PROTO_CHAP)
+    {
+        memcpy(apn->auth,"CHAP",strlen("CHAP")+1);
+    }
+    #if 0
+    else if(apn->auth_proto == QSER_APN_AUTH_PROTO_PAP_CHAP)
+    {
+        //NOT SUPPORT
+    }
+    #endif
+    else
+    {
+        LOGE("auth input error!");
+        return MBTK_RESULT_FAIL;
+    }
+    
+    // apn
+    len = byte_2_uint16(str, false);
+    str += sizeof(uint16);
+    if(len > 0)
+    {
+        memcpy(apn->apn, str, len);
+        str += len;
+    }
+
+    // user
+    len = byte_2_uint16(str, false);
+    str += sizeof(uint16);
+    if(len > 0)
+    {
+        memcpy(apn->user, str, len);
+        str += len;
+    }
+
+    // pass
+    len = byte_2_uint16(str, false);
+    str += sizeof(uint16);
+    if(len > 0)
+    {
+        memcpy(apn->pass, str, len);
+        str += len;
+    }
+
+    // auth_type
+    len = byte_2_uint16(str, false);
+    str += sizeof(uint16);
+    if(len > 0)
+    {
+        memcpy(apn_type, str, len);
+        str += len;
+    }
+
+    ret = mbtk_apn_check_num1(apn_type, apn->apn, apn->cid);
+    if(ret < 0)
+    {
+        LOGE("num1 check error!");
+        return MBTK_RESULT_FAIL;
+    }
+
+    ret = mbtk_apn_check_num2_7(apn_type, apn->apn, apn->ip_type, apn->cid);
+    if(ret < 0)
+    {
+        LOGE("num2_7 check error!");
+        return MBTK_RESULT_FAIL;
+    }
+
+    LOGD("APN : %d, %d, %s, %s, %s, %s.", apn->cid,
+                                        apn->ip_type,
+                                        (str_empty(apn->apn) ? "NULL" : apn->apn),
+                                        (str_empty(apn->user) ? "NULL" : apn->user),
+                                        (str_empty(apn->pass) ? "NULL" : apn->pass),
+                                        (str_empty(apn->auth) ? "NULL" : apn->auth),
+                                        (str_empty(apn_type) ? "NULL" : apn_type));
+    return MBTK_RESULT_SUCCESS;
+}
+
+void mbtk_qser_apn_init(void)
+{
+    LOGD("enter mbtk_qser_apn_init.");
+    apn_init_flag = true;
+#ifdef MBTK_SG_SUPPORT
+    property_get(MBTK_APN_STATE, (char *)apn_set_state, MBTK_BUFF_STATE_INIT);
+#else
+    property_get(MBTK_APN_STATE, (char *)apn_set_state, MBTK_BUFF_STATE_INIT_1);
+#endif
+    LOGD("get apn_set_state[%s].", apn_set_state);
+
+    int i = 0;
+    for(; i < MBTK_BUFF_SIZE; i++)
+    {
+        if(apn_set_state[i] == '2')
+        {
+            mbtk_set_default_apn_cid(i);
+            break;
+        }
+    }
+
+    property_get(MBTK_APN_AUTH_TYPE, (char *)apn_auth_type, MBTK_BUFF_STATE_INIT);
+    LOGD("get apn_auth_type[%s].", apn_auth_type);
+
+    mbtk_get_apn_send_pack();
+}
+#endif
diff --git a/mbtk/mbtk_rild/src/mbtk_info_server.c b/mbtk/mbtk_rild/src/mbtk_info_server.c
index c253439..bc43cd4 100755
--- a/mbtk/mbtk_rild/src/mbtk_info_server.c
+++ b/mbtk/mbtk_rild/src/mbtk_info_server.c
@@ -22,6 +22,7 @@
 #include "mbtk_led.h"
 #include "cust_info.h"
 #include "mbtk_device.h"
+#include "mbtk_data_call.h"
 
 typedef struct {
     uint32 band_gsm;
@@ -5026,6 +5027,69 @@
                 }
                 break;
             }
+            case MBTK_INFO_ID_NET_QSER_APN_REQ:
+            {
+                if(pack->data_len == 0 || pack->data == NULL)
+                {
+                    uint8 buff[SOCK_MSG_LEN_MAX];
+                    memset(buff, 0, SOCK_MSG_LEN_MAX);
+                    int data_len = 0;
+                    if(mbtk_qser_req_apn_get(buff, &data_len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
+                    {
+                        if(cme_err != MBTK_INFO_ERR_CME_NON) {
+                            err = MBTK_INFO_ERR_CME + cme_err;
+                        } else {
+                            err = MBTK_INFO_ERR_UNKNOWN;
+                        }
+                        LOGE("Get APN fail.");
+                    }
+                    else
+                    {
+                        pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
+                    }
+                }
+                else
+                {
+                    const uint8* ptr = pack->data;
+                    mbtk_apn_info_t apn;
+                    mbtk_apn_req_type_enum req_type = MBTK_APN_REQ_TYPE_SET;
+                    uint8 apn_type[MBTK_QSER_APN_NAME_SIZE] = {0};
+                    int ret = mbtk_strdata_to_apn(pack->data, &apn, apn_type, &req_type);
+                    if(ret < 0)
+                    {
+                        LOGE("mbtk_strdata_to_apn fail. ret = [%d]", ret);
+                        err = MBTK_INFO_ERR_REQ_PARAMETER;
+                    }
+                    else
+                    {
+                        if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
+                        {
+                            if(cme_err != MBTK_INFO_ERR_CME_NON) {
+                                err = MBTK_INFO_ERR_CME + cme_err;
+                            } else {
+                                err = MBTK_INFO_ERR_UNKNOWN;
+                            }
+                            LOGE("Set APN fail.");
+                        }
+                        else
+                        {
+                            // Save apn.
+                            apn_prop_set(&apn);
+                            mbtk_qser_apn_save(apn, apn_type, true);
+                            if(req_type == MBTK_APN_REQ_TYPE_ADD)
+                            {
+                                pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, (void *)&apn.cid, 1);
+                            }
+                            else
+                            {
+                                pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, NULL, 0);
+                            }
+                        }
+                    }
+                }
+
+                break;
+            }
             case MBTK_INFO_ID_NET_APN_DEL_REQ:
             {
                 if(pack->data_len == 0 || pack->data == NULL)
@@ -5036,18 +5100,31 @@
                 else
                 {
                     int profile = pack->data[0];
-                    if(req_apn_del(profile, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
+                    if(cid_active[profile] == 1)
                     {
-                        if(cme_err != MBTK_INFO_ERR_CME_NON) {
-                            err = MBTK_INFO_ERR_CME + cme_err;
-                        } else {
-                            err = MBTK_INFO_ERR_UNKNOWN;
-                        }
-                        LOG("Delete apn fail.");
+                        LOGD("cid pdp already open.");
+                        err = MBTK_INFO_ERR_CID_EXIST;
+                    }
+                    if(mbtk_qser_apn_del(profile) < 0)
+                    {
+                        LOGD("del error.");
+                        err = MBTK_INFO_ERR_REQ_PARAMETER;
                     }
                     else
                     {
-                        pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_DEL_RSP, NULL, 0);
+                        if(req_apn_del(profile, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
+                        {
+                            if(cme_err != MBTK_INFO_ERR_CME_NON) {
+                                err = MBTK_INFO_ERR_CME + cme_err;
+                            } else {
+                                err = MBTK_INFO_ERR_UNKNOWN;
+                            }
+                            LOG("Delete apn fail.");
+                        }
+                        else
+                        {
+                            pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_DEL_RSP, NULL, 0);
+                        }
                     }
                 }
                 break;
@@ -5069,10 +5146,17 @@
                     int cid = pack->data[1];
                     int reconn = 0;
 
+#if 0
                     if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
                         err = MBTK_INFO_ERR_CID;
                         break;
                     }
+#endif
+                    if(mbtk_check_cid(cid) < 0)
+                    {
+                        err = MBTK_INFO_ERR_CID;
+                        break;
+                    }
 
                     LOG("cid_active[%d] = %d", cid, cid_active[cid]);
                     memset(&cgact_wait, 0, sizeof(info_cgact_wait_t));
@@ -5096,6 +5180,12 @@
                                 break;
                             }
 
+                            if(mbtk_check_default_pdp_state(cid))
+                            {
+                                err = MBTK_INFO_ERR_UNSUPPORTED;
+                                break;
+                            }
+
                             data_call_reconn:
                             //mbtk wyq for data_call_ex add end
                             cgact_wait.waitting = true;
@@ -5240,6 +5330,8 @@
                                     }
 
                                     cid_active[cid] = 1;
+                                    mbtk_set_default_pdp_state(true, cid);
+                                    mbtk_qser_route_config(cid, &ipv4, &ipv6);
                                     if(cli_info->fd != DATA_CALL_BOOTCONN_FD)
                                     {
                                         pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
@@ -5280,6 +5372,7 @@
                                     LOGD("Config %s IPv4 0 success.", dev);
                                 }
                                 cid_active[cid] = 0;
+                                mbtk_set_default_pdp_state(false, cid);
                                 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
                                 break;
                             }
@@ -5321,6 +5414,7 @@
                                 }
 #endif
                                 cid_active[cid] = 0;
+                                mbtk_set_default_pdp_state(false, cid);
                                 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
                             }
                             break;
@@ -6253,10 +6347,12 @@
     if(act)
     {
         cid_active[cid] = 1;
+        mbtk_set_default_pdp_state(true, cid);
     }
     else
     {
         cid_active[cid] = 0;
+        mbtk_set_default_pdp_state(false, cid);
     }
     char dev[20] = {0};
     sprintf(dev, "ccinet%d", cid - 1);
@@ -6536,8 +6632,14 @@
                     }
                     else
                     {
-                        free(pack->data);
-                        free(item->cli_info);
+                        if(pack->data != NULL)
+                        {
+                            free(pack->data);
+                        }
+                        if(item->cli_info)
+                        {
+                            free(item->cli_info);
+                        }
                     }
                 }
                 at_process = false;
@@ -6945,6 +7047,29 @@
     }
 }
 
+int mbtk_get_apn_send_pack(void)
+{
+    sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
+    if(info == NULL)
+    {
+        LOG("clinent_info  malloc() fail.");
+        return -1;
+    }
+    memset(info, 0, sizeof(sock_client_info_t));
+    info->fd = DATA_CALL_APN_GET_FD;
+
+    mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_QSER_APN_REQ);
+    if(pack == NULL)
+    {
+        free(info);
+        LOG("Packet malloc() fail.");
+        return -1;
+    }
+
+    send_pack_to_queue(info, pack);
+    return 0;
+}
+
 int mbtk_info_server_start()
 {
     signal(SIGPIPE, SIG_IGN);
@@ -7063,6 +7188,8 @@
 
     pthread_attr_destroy(&thread_attr);
 
+    mbtk_qser_apn_init();
+
     LOG("MBTK Information Server Start...");
 
     return 0;