Fix V2 for APN

Change-Id: I936f6f3cb99cdfb2c28765e1229ddac29eab6e87
diff --git a/mbtk/mbtk_rild_v2/inc/ril_info.h b/mbtk/mbtk_rild_v2/inc/ril_info.h
index 595d603..e9cb822 100755
--- a/mbtk/mbtk_rild_v2/inc/ril_info.h
+++ b/mbtk/mbtk_rild_v2/inc/ril_info.h
@@ -24,7 +24,12 @@
 #define IND_REGISTER_MAX 10
 #define PACK_PROCESS_QUEUE_MAX 20
 
+#if 0
 #define MBTK_APN_PROP       "persist.mbtk.apn"
+#else
+#define MBTK_APN_PROP       "/etc/mbtk/apn"
+#endif
+
 #define MBTK_DEF_ROUTE_CID  "persist.mbtk.def_route_cid"
 #define MBTK_DEF_DNS_CID  "persist.mbtk.def_dns_cid"
 
@@ -103,14 +108,6 @@
     int data_len;
 } ril_urc_msg_info_t;
 
-typedef struct {
-    mbtk_ril_cid_enum cid_for_def_route;
-    mbtk_ril_cid_enum cid_for_dns;
-
-    int num;
-    mbtk_apn_info_t apns[MBTK_APN_CID_MAX];
-} ril_apn_info_array_t;
-
 typedef struct
 {
     int cid;
diff --git a/mbtk/mbtk_rild_v2/src/main.c b/mbtk/mbtk_rild_v2/src/main.c
index 7f68de3..5565621 100755
--- a/mbtk/mbtk_rild_v2/src/main.c
+++ b/mbtk/mbtk_rild_v2/src/main.c
@@ -240,7 +240,7 @@
 }
 
 void ril_rsp_pack_send(int fd, int ril_id, int msg_index, const void* data, int data_len)

-{
+{

     ril_msg_pack_info_t* pack = ril_msg_pack_creat(RIL_MSG_TYPE_RSP, ril_id, msg_index, data, data_len);

     if(pack)
     {
diff --git a/mbtk/mbtk_rild_v2/src/ril_data_call.c b/mbtk/mbtk_rild_v2/src/ril_data_call.c
index 4fbeca5..97ff338 100755
--- a/mbtk/mbtk_rild_v2/src/ril_data_call.c
+++ b/mbtk/mbtk_rild_v2/src/ril_data_call.c
@@ -44,7 +44,7 @@
         char buf[100] = {0};
 
         // Delete all default route in the first.
-
+        // route del default
 
         // Add default route.
         memset(buf, 0, sizeof(buf));
diff --git a/mbtk/mbtk_rild_v2/src/ril_net.c b/mbtk/mbtk_rild_v2/src/ril_net.c
index 3592850..6ca5d65 100755
--- a/mbtk/mbtk_rild_v2/src/ril_net.c
+++ b/mbtk/mbtk_rild_v2/src/ril_net.c
@@ -27,13 +27,68 @@
 void ril_rsp_pack_send(int fd, int ril_id, int msg_index, const void* data, int data_len);
 static int req_apn_get(bool get_def_cid, mbtk_apn_info_array_t *apns, int *cme_err);
 static int req_apn_set(mbtk_apn_info_t *apn, int *cme_err);
-static void apn_prop_get(ril_apn_info_array_t *apns);
+static void apn_prop_get(mbtk_apn_info_array_t *apns);
+
+static int apn_file_save(const char *file, char *data, int data_len)
+{
+    if(!file) {
+        return -1;
+    }
+
+    if(str_empty(data) || data_len <= 0) { // Delete file
+        return unlink(file);
+    } else {
+        int fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0644);
+        if(fd < 0) {
+            LOGE("open(%s) fail:%d", file, errno);
+            return -1;
+        }
+
+        if(write(fd, data, data_len) != data_len) {
+            LOGE("write fail:%d", errno);
+            close(fd);
+            return -1;
+        }
+        close(fd);
+        return 0;
+    }
+}
+
+static int apn_file_read(const char *file, char *data, int data_len)
+{
+    if(!file) {
+        LOGE("file is null");
+        return -1;
+    }
+
+    if(data == NULL || data_len <= 100) {
+        LOGE("apn_file_read() arg error.");
+        return -1;
+    } else {
+        int len = -1;
+        int fd = open(file, O_RDONLY, 0644);
+        if(fd < 0) {
+            LOGE("open(%s) fail:%d", file, errno);
+            return -1;
+        }
+
+        memset(data, 0, data_len);
+        if((len = read(fd, data, data_len)) < 0) {
+            LOGE("read fail:%d", errno);
+            close(fd);
+            return -1;
+        }
+        close(fd);
+        return len;
+    }
+}
+
 
 void apn_auto_conf_from_prop()
 {
-    ril_apn_info_array_t apns;
+    mbtk_apn_info_array_t apns;
     int i = 0;
-    memset(&apns, 0, sizeof(ril_apn_info_array_t));
+    memset(&apns, 0, sizeof(mbtk_apn_info_array_t));
     apn_prop_get(&apns);
     while(i < apns.num) {
         int cme_err = MBTK_RIL_ERR_CME_NON;
@@ -169,36 +224,44 @@
     return 0;
 }
 
-static void apn_prop_get(ril_apn_info_array_t *apns)
+static void apn_prop_get(mbtk_apn_info_array_t *apns)
 {
-    char prop_name[20] = {0};
-    char prop_data[300] = {0};
+    char prop_name[128] = {0};
+    char prop_data[1024] = {0};
     int cid;
-    memset(apns, 0, sizeof(ril_apn_info_array_t));
+    memset(apns, 0, sizeof(mbtk_apn_info_array_t));
     bool asr_auto_call_open = !apn_conf_support(MBTK_RIL_CID_DEF);
 
     // If auto data call is open,the default route is CID 1.
     if(asr_auto_call_open) {
-        apns->cid_for_def_route = MBTK_RIL_CID_DEF;
-        apns->cid_for_dns = MBTK_RIL_CID_DEF;
+        //apns->cid_for_def_route = MBTK_RIL_CID_DEF;
+        //apns->cid_for_def_dns = MBTK_RIL_CID_DEF;
         cid = MBTK_RIL_CID_2;
     } else {
-        if(property_get(MBTK_DEF_ROUTE_CID, prop_data, "") > 0 && !str_empty(prop_data)) {
-            apns->cid_for_def_route = (mbtk_ril_cid_enum)atoi(prop_data);
-        }
-        memset(prop_data, 0, sizeof(prop_data));
-        if(property_get(MBTK_DEF_DNS_CID, prop_data, "") > 0 && !str_empty(prop_data)) {
-            apns->cid_for_dns = (mbtk_ril_cid_enum)atoi(prop_data);
-        }
         cid = MBTK_APN_CID_MIN;
     }
+
+    char def_cid[10] = {0};
+    sprintf(def_cid, "%d", MBTK_RIL_CID_DEF);
+    if(property_get(MBTK_DEF_ROUTE_CID, prop_data, def_cid) > 0 && !str_empty(prop_data)) {
+        apns->cid_for_def_route = (mbtk_ril_cid_enum)atoi(prop_data);
+    }
+    memset(prop_data, 0, sizeof(prop_data));
+    if(property_get(MBTK_DEF_DNS_CID, prop_data, def_cid) > 0 && !str_empty(prop_data)) {
+        apns->cid_for_def_dns = (mbtk_ril_cid_enum)atoi(prop_data);
+    }
+
     for(; cid <= MBTK_APN_CID_MAX; cid++) {
-        memset(prop_name, 0, 20);
-        memset(prop_data, 0, 300);
+        memset(prop_name, 0, sizeof(prop_name));
+        memset(prop_data, 0, sizeof(prop_data));
 
         sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
         // ip_type,auth,auto_data_call,apn,user,pass
+#if 0
         if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
+#else
+        if(apn_file_read(prop_name, prop_data, sizeof(prop_data)) > 0 && !str_empty(prop_data)) {
+#endif
             apns->apns[apns->num].cid = (mbtk_ril_cid_enum)cid;
             char *ptr_1 = prop_data;
             apns->apns[apns->num].ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
@@ -240,8 +303,17 @@
             }
 
             ptr_1++; // Jump ',' to pass
+            ptr_2 = strstr(ptr_1, ",");
+            if(!ptr_2) {
+                continue;
+            }
             if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
-                memcpy(apns->apns[apns->num].pass, ptr_1, strlen(ptr_1)); // pass
+                memcpy(apns->apns[apns->num].pass, ptr_1, ptr_2 - ptr_1); // pass
+            }
+
+            ptr_2++; // Jump ',' to type
+            if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
+                memcpy(apns->apns[apns->num].type, ptr_2, strlen(ptr_2)); // type
             }
 
             apns->num++;
@@ -251,13 +323,17 @@
 
 static int apn_prop_get_by_cid(mbtk_ril_cid_enum cid, mbtk_apn_info_t *apn)
 {
-    char prop_name[20] = {0};
-    char prop_data[300] = {0};
+    char prop_name[128] = {0};
+    char prop_data[1024] = {0};
     memset(apn, 0, sizeof(mbtk_apn_info_t));
 
     sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
     // ip_type,auth,auto_data_call,apn,user,pass
+#if 0
     if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
+#else
+    if(apn_file_read(prop_name, prop_data, sizeof(prop_data)) > 0 && !str_empty(prop_data)) {
+#endif
         apn->cid = cid;
         apn->auto_save = (uint8)1;
         char *ptr_1 = prop_data;
@@ -300,38 +376,131 @@
         }
 
         ptr_1++; // Jump ',' to pass
+        ptr_2 = strstr(ptr_1, ",");
+        if(!ptr_2) {
+            return -1;
+        }
         if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
-            memcpy(apn->pass, ptr_1, strlen(ptr_1)); // pass
+            memcpy(apn->user, ptr_1, ptr_2 - ptr_1); // pass
+        }
+
+        ptr_2++; // Jump ',' to type
+        if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
+            memcpy(apn->type, ptr_2, strlen(ptr_2)); // pass
         }
         return 0;
     }
     return -1;
 }
 
+static int apn_prop_get_by_cid_without_cgdcont(mbtk_ril_cid_enum cid, mbtk_apn_info_t *apn)
+{
+    char prop_name[128] = {0};
+    char prop_data[1024] = {0};
+
+    sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
+    // ip_type,auth,auto_data_call,apn,user,pass
+#if 0
+    if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
+#else
+    if(apn_file_read(prop_name, prop_data, sizeof(prop_data)) > 0 && !str_empty(prop_data)) {
+#endif
+        LOGD("APN : %s", prop_data);
+        char *ptr_1 = prop_data;
+        apn->auto_save = (uint8)1;
+        //apn->ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
+        ptr_1 = strstr(ptr_1, ",");
+        if(!ptr_1) {
+            return -1;
+        }
+        ptr_1++; // Jump ',' to auth
+
+        apn->auth = (mbtk_apn_auth_type_enum)atoi(ptr_1);
+        ptr_1 = strstr(ptr_1, ",");
+        if(!ptr_1) {
+            return -1;
+        }
+        ptr_1++; // Jump ',' to auto_data_call
+
+        apn->auto_boot_call = (uint8)atoi(ptr_1);
+        ptr_1 = strstr(ptr_1, ",");
+        if(!ptr_1) {
+            return -1;
+        }
+        ptr_1++; // Jump ',' to apn
+
+        char *ptr_2 = strstr(ptr_1, ",");
+        if(!ptr_2) {
+            return -1;
+        }
+#if 0
+        if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
+            memcpy(apn->apn, ptr_1, ptr_2 - ptr_1); // apn
+        }
+#endif
+
+        ptr_2++; // Jump ',' to user
+        ptr_1 = strstr(ptr_2, ",");
+        if(!ptr_1) {
+            return -1;
+        }
+        if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
+            memcpy(apn->user, ptr_2, ptr_1 - ptr_2); // user
+        }
+
+        ptr_1++; // Jump ',' to pass
+        ptr_2 = strstr(ptr_1, ",");
+        if(!ptr_2) {
+            return -1;
+        }
+        if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
+            memcpy(apn->user, ptr_1, ptr_2 - ptr_1); // pass
+        }
+
+        ptr_2++; // Jump ',' to type
+        if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
+            memcpy(apn->type, ptr_2, strlen(ptr_2)); // pass
+        }
+        return 0;
+    } else {
+        apn->auto_save = (uint8)0;
+    }
+    return -1;
+}
+
 static int apn_prop_set(mbtk_apn_info_t *apn)
 {
     char prop_name[20] = {0};
-    char prop_data[300] = {0};
+    char prop_data[1024] = {0};
     int ret = -1;
     if(apn->auto_save) {
         sprintf(prop_name, "%s_%d", MBTK_APN_PROP, apn->cid);
         // Delete apn
         if(!str_empty(apn->apn)) {
-            snprintf(prop_data, 300, "%d,%d,%d,%s,%s,%s", apn->ip_type, apn->auth, apn->auto_boot_call,
+            snprintf(prop_data, 1024, "%d,%d,%d,%s,%s,%s,%s", apn->ip_type, apn->auth, apn->auto_boot_call,
                 apn->apn,
                 str_empty(apn->user) ? "NULL" : apn->user,
-                str_empty(apn->pass) ? "NULL" : apn->pass);
+                str_empty(apn->pass) ? "NULL" : apn->pass,
+                str_empty(apn->pass) ? "NULL" : apn->type);
         }
 
+#if 0
         ret = property_set(prop_name, prop_data);
+#else
+        if(str_empty(apn->apn)) { // Delete APN
+            ret = apn_file_save(prop_name, NULL, 0);
+        } else {
+            ret = apn_file_save(prop_name, prop_data, strlen(prop_data));
+        }
+#endif
     }
 
-    if(apn->def_route) {
+    if(!ret && apn->def_route) {
         memset(prop_data, 0, sizeof(prop_data));
         prop_data[0] = '0' + apn->cid;
         ret = property_set(MBTK_DEF_ROUTE_CID, prop_data);
     }
-    if(apn->as_dns) {
+    if(!ret && apn->as_dns) {
         memset(prop_data, 0, sizeof(prop_data));
         prop_data[0] = '0' + apn->cid;
         ret = property_set(MBTK_DEF_DNS_CID, prop_data);
@@ -1615,12 +1784,28 @@
                 memcpy(apns->apns[apns->num].apn, tmp_str, strlen(tmp_str));
             }
 
+            // Get other arg from proc or file.
+            apn_prop_get_by_cid_without_cgdcont(apns->apns[apns->num].cid, &(apns->apns[apns->num]));
+
             apns->num++;
         }
 
         lines_ptr = lines_ptr->p_next;
     }
 
+    char prop_name[128] = {0};
+    char prop_data[1024] = {0};
+    char def_cid[10] = {0};
+    sprintf(def_cid, "%d", MBTK_RIL_CID_DEF);
+
+    if(property_get(MBTK_DEF_ROUTE_CID, prop_data, def_cid) > 0 && !str_empty(prop_data)) {
+        apns->cid_for_def_route = (mbtk_ril_cid_enum)atoi(prop_data);
+    }
+    memset(prop_data, 0, sizeof(prop_data));
+    if(property_get(MBTK_DEF_DNS_CID, prop_data, def_cid) > 0 && !str_empty(prop_data)) {
+        apns->cid_for_def_dns = (mbtk_ril_cid_enum)atoi(prop_data);
+    }
+
     goto exit;
 exit:
     at_response_free(response);