[Bugfix][T108-GSW][bug-view-2211] add interface to set default attach apn

    Only Configure: No
    Affected branch: unknown
    Affected module: network
    Is it affected on both ZXIC and ASR: only ASR
    Self-test: yes
    Doc Update: no

Change-Id: If792fbaa6b604d5dc6b540df1ee7f4f12c747cee
diff --git a/mbtk/mbtk_rild/src/mbtk_info_server.c b/mbtk/mbtk_rild/src/mbtk_info_server.c
index 1b58410..7b52415 100755
--- a/mbtk/mbtk_rild/src/mbtk_info_server.c
+++ b/mbtk/mbtk_rild/src/mbtk_info_server.c
@@ -3722,6 +3722,84 @@
 }
 #endif
 
+
+int req_apn_init_attach_apn_inner( mbtk_apn_info_t *apn, int *cme_err)
+{
+    ATResponse *response = NULL;
+    char cmd[400] = {0};
+    int index = 0;
+    int err = 0;
+    
+    if(cme_err==NULL)
+    {
+        LOGE("cme_err is NULL");
+        return AT_ERROR_GENERIC;
+    }
+
+    index += sprintf(cmd, "AT*CGDFLT=1,");//save to nvm
+    switch(apn->ip_type) {
+        case MBTK_IP_TYPE_IP: {
+            index += sprintf(cmd + index,"\"IP\",");
+            break;
+        }
+        case MBTK_IP_TYPE_IPV6: {
+            index += sprintf(cmd + index,"\"IPV6\",");
+            break;
+        }
+        case MBTK_IP_TYPE_IPV4V6: {
+            index += sprintf(cmd + index,"\"IPV4V6\",");
+            break;
+        }
+        default: {
+            index += sprintf(cmd + index,"\"PPP\",");
+            break;
+        }
+    }
+    if(strlen((char*)apn->apn) > 0) {
+        index += sprintf(cmd + index,"\"%s\",,,,,,,,,,,,,,,,,,1", apn->apn);
+    }
+    else
+    {
+        index += sprintf(cmd + index,"\"%s\",,,,,,,,,,,,,,,,,,0", "");
+    }
+    
+    err = at_send_command(cmd, &response);
+    if (err < 0 || response->success == 0){
+        *cme_err = at_get_cme_error(response);
+        goto exit;
+    }
+
+    if(!str_empty(apn->user) || !str_empty(apn->pass)) {
+        at_response_free(response);
+        response = NULL;
+        memset(cmd,0,400);
+        int cmd_auth=0;
+        if(strstr((char*)apn->auth,"NONE"))
+            cmd_auth = 0;
+        else if(strstr((char*)apn->auth,"PAP"))
+            cmd_auth = 1;
+        else if(strstr((char*)apn->auth,"CHAP"))
+            cmd_auth = 2;
+#if 0
+        else if(strstr(apn->auth,"PAP AND CHAP"))
+            cmd_auth = 3;
+#endif
+        else
+            goto exit;
+
+        sprintf(cmd, "AT*CGDFAUTH=%d,%d,%s,%s",1,cmd_auth,apn->user,apn->pass);
+        err = at_send_command(cmd, &response);
+        if (err < 0 || response->success == 0){
+            *cme_err = at_get_cme_error(response);
+            goto exit;
+        }
+    }
+exit:
+    at_response_free(response);
+    return err;
+}
+
+
 /*
 AT+CGDCONT=1,"IPV4V6","cmnet"
 OK
@@ -3737,6 +3815,22 @@
     int index = 0;
     int err = 0;
 
+    if(cme_err==NULL)
+    {
+        LOGE("cme_err is NULL");
+        return AT_ERROR_GENERIC;
+    }
+
+    if(apn->cid==1)
+    {
+       err=req_apn_init_attach_apn_inner(apn, cme_err);
+       if(err || *cme_err != MBTK_INFO_ERR_CME_NON)
+        {
+           LOGE("Set initial attach APN fail, err is %d, cme_err is %d",err,*cme_err);
+           goto exit; 
+        }
+    }
+
     index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
     switch(apn->ip_type) {
         case MBTK_IP_TYPE_IP: {
@@ -3762,9 +3856,7 @@
 
     err = at_send_command(cmd, &response);
     if (err < 0 || response->success == 0){
-        if(cme_err) {
-            *cme_err = at_get_cme_error(response);
-        }
+        *cme_err = at_get_cme_error(response);
         goto exit;
     }
 
@@ -7210,6 +7302,7 @@
     // cid : 2 - 7
     int cid = MBTK_APN_CID_MIN;
     mbtk_apn_info_t apn;
+    int cme_err=MBTK_INFO_ERR_CME_NON;
     for(; cid <= MBTK_APN_CID_MAX; cid++) {
         memset(prop_name, 0, 20);
         memset(prop_data, 0, 300);
@@ -7253,8 +7346,8 @@
             if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
                 memcpy(apn.auth, ptr_2, strlen(ptr_2)); // auth
             }
-
-            req_apn_set(&apn, NULL);
+            
+            req_apn_set(&apn, &cme_err);
         }
     }
 }