Fix ril open and close for v2.

Change-Id: Iec5f7c4de8a27c206d62c2cbb10b796e463ff9c4
diff --git a/mbtk/libmbtk_lib/ril/v2/mbtk_ril_api.c b/mbtk/libmbtk_lib/ril/v2/mbtk_ril_api.c
index 05f9def..ac774cf 100755
--- a/mbtk/libmbtk_lib/ril/v2/mbtk_ril_api.c
+++ b/mbtk/libmbtk_lib/ril/v2/mbtk_ril_api.c
@@ -1,3 +1,21 @@
+/*
+*    mbtk_ril_api.c
+*
+*    MBTK ril API for v2.
+*
+*/
+/******************************************************************************
+
+                          EDIT HISTORY FOR FILE
+
+  WHEN        WHO       WHAT,WHERE,WHY
+--------    --------    -------------------------------------------------------
+2024/08/15     LiuBin      Initial version
+2024/12/31     LiuBin      Auto open default port.
+                           Add port index for open/close,close port only index is 0.
+
+******************************************************************************/
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -454,115 +472,99 @@
         return NULL;
     }
 
-    if(ril_cli.ril_ready)
-    {
-        if(at_port_check(port)) {
-            LOGD("Port(%d) is opened.", port);
-            ril_cli.ports[port].open_count++;
-            return &(ril_cli.ports[port]);
-        } else {
-            ril_cli.ports[port].port = port;
-            // ril_cli.ports[port].enable = TRUE;
-            ril_cli.ports[port].open_count++;
-            return &(ril_cli.ports[port]);
+    if(!ril_cli.ril_ready) {
+        ril_cli.cli_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
+        if(ril_cli.cli_fd < 0)
+        {
+            LOGE("socket() fail[%d].", errno);
+            return NULL;
         }
-    }
 
-    ril_cli.cli_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
-    if(ril_cli.cli_fd < 0)
-    {
-        LOGE("socket() fail[%d].", errno);
-        return NULL;
-    }
+        // Set O_NONBLOCK
+        int flags = fcntl(ril_cli.cli_fd, F_GETFL, 0);
+        if (flags < 0)
+        {
+            LOGE("Get flags error:%d", errno);
+            goto error;
+        }
+        flags |= O_NONBLOCK;
+        if (fcntl(ril_cli.cli_fd, F_SETFL, flags) < 0)
+        {
+            LOGE("Set flags error:%d", errno);
+            goto error;
+        }
 
-    // Set O_NONBLOCK
-    int flags = fcntl(ril_cli.cli_fd, F_GETFL, 0);
-    if (flags < 0)
-    {
-        LOGE("Get flags error:%d", errno);
-        goto error;
-    }
-    flags |= O_NONBLOCK;
-    if (fcntl(ril_cli.cli_fd, F_SETFL, flags) < 0)
-    {
-        LOGE("Set flags error:%d", errno);
-        goto error;
-    }
+        struct sockaddr_un cli_addr;
+        memset(&cli_addr, 0, sizeof(cli_addr));
+        cli_addr.sun_family = AF_LOCAL;
+        strcpy(cli_addr.sun_path, RIL_SOCK_NAME);
+        if(connect(ril_cli.cli_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
+        {
+            LOGE("connect() fail[%d].", errno);
+            goto error;
+        }
 
-    struct sockaddr_un cli_addr;
-    memset(&cli_addr, 0, sizeof(cli_addr));
-    cli_addr.sun_family = AF_LOCAL;
-    strcpy(cli_addr.sun_path, RIL_SOCK_NAME);
-    if(connect(ril_cli.cli_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
-    {
-        LOGE("connect() fail[%d].", errno);
-        goto error;
-    }
-
-    if(pipe(ril_cli.exit_fd))
-    {
-        LOGE("pipe() fail[%d].", errno);
-        goto error;
-    }
+        if(pipe(ril_cli.exit_fd))
+        {
+            LOGE("pipe() fail[%d].", errno);
+            goto error;
+        }
 
 #if 1
-    pthread_attr_t thread_attr;
-    pthread_attr_init(&thread_attr);
-    if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
-    {
-        LOGE("pthread_attr_setdetachstate() fail.");
-        goto error;
-    }
+        pthread_attr_t thread_attr;
+        pthread_attr_init(&thread_attr);
+        if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
+        {
+            LOGE("pthread_attr_setdetachstate() fail.");
+            goto error;
+        }
 
-    if(pthread_create(&ril_cli.read_thread_id, &thread_attr, ril_read_run, NULL))
-    {
-        LOGE("pthread_create() fail.");
-        goto error;
-    }
-    pthread_attr_destroy(&thread_attr);
+        if(pthread_create(&ril_cli.read_thread_id, &thread_attr, ril_read_run, NULL))
+        {
+            LOGE("pthread_create() fail.");
+            goto error;
+        }
+        pthread_attr_destroy(&thread_attr);
 #else
-    if(pthread_create(&read_thread_id, NULL, ril_read_run, NULL))
-    {
-        LOGE("pthread_create() fail.");
-        goto error;
-    }
+        if(pthread_create(&read_thread_id, NULL, ril_read_run, NULL))
+        {
+            LOGE("pthread_create() fail.");
+            goto error;
+        }
 #endif
 
-    pthread_mutex_init(&ril_cli.send_mutex, NULL);
-    ril_cli.msg_list = list_create(msg_list_free_cb);
-    if(ril_cli.msg_list == NULL) {
-        LOGE("list_create(msg_list) fail.");
-        goto error;
+        pthread_mutex_init(&ril_cli.send_mutex, NULL);
+        ril_cli.msg_list = list_create(msg_list_free_cb);
+        if(ril_cli.msg_list == NULL) {
+            LOGE("list_create(msg_list) fail.");
+            goto error;
+        }
+
+        ril_cli.cli_thread_list = list_create(cli_thread_list_free_cb);
+        if(ril_cli.cli_thread_list == NULL) {
+            LOGE("list_create(cli_thread_list) fail.");
+            goto error;
+        }
+
+        ril_cli.ril_ready = FALSE;
+
+        // Waitting mbtk_rild ready...
+        while(!ril_cli.ril_ready) {
+            usleep(100000);
+        }
+
+        LOGD("RIL server ready...");
     }
 
-    ril_cli.cli_thread_list = list_create(cli_thread_list_free_cb);
-    if(ril_cli.cli_thread_list == NULL) {
-        LOGE("list_create(cli_thread_list) fail.");
-        goto error;
-    }
-
-    ril_cli.ril_ready = FALSE;
-
-    // Waitting mbtk_rild ready...
-    while(!ril_cli.ril_ready) {
-        usleep(100000);
-    }
-
-    LOGD("RIL server ready...");
-
     // Auto open default port.
     ril_cli.ports[MBTK_AT_PORT_DEF].port = MBTK_AT_PORT_DEF;
-    //ril_cli.ports[MBTK_AT_PORT_DEF].enable = TRUE;
     ril_cli.ports[MBTK_AT_PORT_DEF].open_count++;
-
     if(port != MBTK_AT_PORT_DEF) {
         ril_cli.ports[port].port = port;
-        //ril_cli.ports[port].enable = TRUE;
         ril_cli.ports[port].open_count++;
-        return &(ril_cli.ports[port]);
-    } else {
-        return &(ril_cli.ports[MBTK_AT_PORT_DEF]);
     }
+
+    return &(ril_cli.ports[port]);
 error:
     if(ril_cli.cli_fd > 0)
     {
@@ -589,17 +591,18 @@
         return MBTK_RIL_ERR_PORT;
     }
 
-    ril_cli.ports[port].open_count--;
-
-    if(ril_cli.ports[port].open_count > 0) {
-        LOGD("Port(%d) open_count is %d, no close port.", ril_cli.ports[port].open_count);
-        return MBTK_RIL_ERR_SUCCESS;
+    // Not def port,should auto close def port.
+    if(port != MBTK_AT_PORT_DEF) {
+        ril_cli.ports[MBTK_AT_PORT_DEF].open_count--;
+        if(ril_cli.ports[MBTK_AT_PORT_DEF].open_count == 0) {
+            ril_cli.ports[MBTK_AT_PORT_DEF].port = ATPORTTYPE_NON;
+        }
     }
 
-    LOGD("Will close port %d", port);
-    // ril_cli.ports[port].enable = FALSE;
-    ril_cli.ports[port].port = ATPORTTYPE_NON;
-    ril_cli.ports[port].open_count = 0;
+    ril_cli.ports[port].open_count--;
+    if(ril_cli.ports[port].open_count == 0) {
+        ril_cli.ports[port].port = ATPORTTYPE_NON;
+    }
 
     // All port is close ???
     int i = ATPORTTYPE_0;
@@ -610,6 +613,7 @@
     }
 
     if(i == ATPORTTYPE_NUM) { // All port is close.
+        LOGD("Will close port %d", port);
         if(!ril_cli.ril_ready)
         {
             return MBTK_RIL_ERR_NOT_INIT;
@@ -655,7 +659,13 @@
 
         return MBTK_RIL_ERR_SUCCESS;
     } else {
-        return MBTK_RIL_ERR_PORT_NOT_CLOSE;
+        i = ATPORTTYPE_0;
+        for(; i < ATPORTTYPE_NUM; i++) {
+            if(ril_cli.ports[i].open_count > 0) {
+                LOGD("Port[%d] open_count is %d, so can't close.", i, ril_cli.ports[i].open_count);
+            }
+        }
+        return MBTK_RIL_ERR_SUCCESS;
     }
 }
 
diff --git a/mbtk/mbtk_rild_v2/src/main.c b/mbtk/mbtk_rild_v2/src/main.c
index 438c748..0b70641 100755
--- a/mbtk/mbtk_rild_v2/src/main.c
+++ b/mbtk/mbtk_rild_v2/src/main.c
@@ -18,6 +18,7 @@
   WHEN        WHO       WHAT,WHERE,WHY

 --------    --------    -------------------------------------------------------

 2024/08/13     LiuBin      Initial version

+2024/12/31     LiuBin      Add new sms urc process(+CMT)

 

 ******************************************************************************/

 #include <stdio.h>

@@ -686,29 +687,32 @@
 // 0891683108200855F6240D91688189911196F10000221130717445230331D90C

 static void urc_sms_state_change_process(const char *s, const char *sms_pdu)

 {

-    mbtk_ril_sms_state_info_t sms_info;

-    memset(&sms_info, 0, sizeof(mbtk_ril_sms_state_info_t));

-    char* tmp_s = memdup(s,strlen(s) + 1);

-    char *line = tmp_s;

-    char *tmp_str;

-    if (at_tok_start(&line) < 0)

-    {

-        goto CMT_EXIT;

-    }

-    if (at_tok_nextstr(&line, &tmp_str) < 0)

-    {

-        goto CMT_EXIT;

-    }

-    if (at_tok_nextstr(&line, &tmp_str) < 0)

-    {

-        goto CMT_EXIT;

-    }

-    sms_info.pdu_len = (uint16)atoi(tmp_str);

-    memcpy(sms_info.pdu, sms_pdu, strlen(sms_pdu));

+    if(sms_pdu) {

+        LOGD("PDU : %s", sms_pdu);

+        mbtk_ril_sms_state_info_t sms_info;

+        memset(&sms_info, 0, sizeof(mbtk_ril_sms_state_info_t));

+        char* tmp_s = memdup(s,strlen(s) + 1);

+        char *line = tmp_s;

+        char *tmp_str;

+        if (at_tok_start(&line) < 0)

+        {

+            goto CMT_EXIT;

+        }

+        if (at_tok_nextstr(&line, &tmp_str) < 0)

+        {

+            goto CMT_EXIT;

+        }

+        if (at_tok_nextstr(&line, &tmp_str) < 0)

+        {

+            goto CMT_EXIT;

+        }

+        sms_info.pdu_len = (uint16)atoi(tmp_str);

+        memcpy(sms_info.pdu, sms_pdu, strlen(sms_pdu));

 

-    urc_msg_distribute(false, RIL_MSG_ID_IND_SMS_STATE_CHANGE, &sms_info, sizeof(mbtk_ril_sms_state_info_t));

-CMT_EXIT:

-    free(tmp_s);

+        urc_msg_distribute(false, RIL_MSG_ID_IND_SMS_STATE_CHANGE, &sms_info, sizeof(mbtk_ril_sms_state_info_t));

+    CMT_EXIT:

+        free(tmp_s);

+    }

 }

 

 // +CREG: 1, "8010", "000060a5", 0, 2, 0

diff --git a/mbtk/mbtk_rild_v2/src/ril_data_call.c b/mbtk/mbtk_rild_v2/src/ril_data_call.c
index f806659..5ec4d55 100755
--- a/mbtk/mbtk_rild_v2/src/ril_data_call.c
+++ b/mbtk/mbtk_rild_v2/src/ril_data_call.c
@@ -15,6 +15,7 @@
 --------    --------    -------------------------------------------------------
 2024/10/10    LiuBin     Initial version
 2024/11/13    LiuBin     Add auto data call while network changed.
+                         Fix apn change for same name.
 
 ******************************************************************************/
 #include <stdio.h>
@@ -463,6 +464,8 @@
             }
 
             // Is add,the APN can't same.
+            // Is change, not change apn name to other cid.
+#if 0
             if(!is_change) {
                 index = 0;
                 while(index < apns.num) {
@@ -473,6 +476,24 @@
                     index++;
                 }
             }
+#else
+            index = 0;
+            while(index < apns.num) {
+                if(strcmp((char*)apns.apns[index].apn,(char*)apn->apn) == 0) {
+                    if(!is_change) { // Is add,the APN can't same.
+                        LOGW("APN : %s exist, can't add.", apn->apn);
+                        return -1;
+                    } else { // Is change, not change apn name to other cid.
+                        if(index != apn->cid) {
+                            LOGW("APN : %s exist in cid[%d], can't change to cid[%d]", apn->apn,
+                                index, apn->cid);
+                            return -1;
+                        }
+                    }
+                }
+                index++;
+            }
+#endif
         }
     }
     return 0;