Fix For PTREL-286 : Multi-User Architecture Change updated for review comments
[platform/core/telephony/tel-plugin-imc.git] / src / s_network.c
old mode 100755 (executable)
new mode 100644 (file)
index 1ac4c48..aab77c9
@@ -36,6 +36,7 @@
 #include <storage.h>
 #include <util.h>
 #include <at.h>
+#include <tzplatform_config.h>
 
 #include "s_common.h"
 #include "s_network.h"
@@ -153,12 +154,15 @@ static void _insert_mcc_mnc_oper_list(TcorePlugin *p, CoreObject *o)
        gpointer key, value;
        GHashTable *result = NULL, *row = NULL;
        struct tcore_network_operator_info *noi = NULL;
+       const char *path = NULL;
        int count = 0;
 
        s = tcore_plugin_ref_server(p);
        strg = tcore_server_find_storage(s, "database");
 
-       handle = tcore_storage_create_handle(strg, "/opt/dbspace/.mcc_mnc_oper_list.db");
+       path = tzplatform_mkpath(TZ_SYS_DB, ".mcc_mnc_oper_list.db");
+
+       handle = tcore_storage_create_handle(strg, path);
        if (!handle) {
                dbg("fail to create database handle");
                return;
@@ -369,6 +373,7 @@ static void on_response_search_network(TcorePending *p, int data_len, const void
        GSList *network_token = NULL;
        int AcT = 0;
        char *temp_plmn_info = NULL;
+       char *alpha_name = NULL;
        char *pResp = NULL;
        int num_network_avail = 0;
 
@@ -404,13 +409,15 @@ static void on_response_search_network(TcorePending *p, int data_len, const void
                                resp.list[i].status = (enum telephony_network_plmn_status) atoi(pResp);
                        }
 
-                       if ((pResp = tcore_at_tok_nth(network_token, 1))) {     /* Long Aplha name */
-                               dbg("Long Aplha name : %s", pResp);
+                       if ((pResp = tcore_at_tok_nth(network_token, 1))) {     /* Long Alpha name */
+                               dbg("Long Alpha name : %s", pResp);
 
                                if (strlen(pResp) > 0)
                                        /* Strip off starting quote & ending quote */
                                        strncpy(resp.list[i].name, pResp + 1, strlen(pResp) - 2);
-                       } else if ((pResp = tcore_at_tok_nth(network_token, 2))) {
+                       }
+
+                       if ((pResp = tcore_at_tok_nth(network_token, 2))) {
                                dbg("Short Aplha name : %s", pResp);
                                /* Short Aplha name */
                                /* Strip off starting quote & ending quote */
@@ -422,15 +429,13 @@ static void on_response_search_network(TcorePending *p, int data_len, const void
                        pResp = tcore_at_tok_nth(network_token, 3);
                        if (pResp != NULL) {
                                dbg("PLMN ID : %s", pResp);
-                               temp_plmn_info = util_removeQuotes(pResp);
+                               temp_plmn_info = tcore_at_tok_extract((const char *)pResp);
+                               strncpy(resp.list[i].plmn, temp_plmn_info, 6);
+                               resp.list[i].plmn[6] = '\0';
                        }
 
-                       memcpy(resp.list[i].plmn, temp_plmn_info, 6);
-                       if (resp.list[i].plmn[5] == '#')
-                               resp.list[i].plmn[5] = '\0';
-
                        /* Parse Access Technology */
-                       if ((pResp = tcore_at_tok_nth(tokens, 4))) {
+                       if ((pResp = tcore_at_tok_nth(network_token, 4))) {
                                if (strlen(pResp) > 0) {
                                        AcT = atoi(pResp);
 
@@ -445,6 +450,8 @@ static void on_response_search_network(TcorePending *p, int data_len, const void
                        resp.list_count++;
 
                        tcore_at_tok_free(network_token);
+                       g_free(alpha_name);
+                       g_free(temp_plmn_info);
                }
        } else {
                dbg("RESPONSE NOK");
@@ -459,8 +466,6 @@ OUT:
 
        /* Free tokens */
        tcore_at_tok_free(tokens);
-
-       g_free(temp_plmn_info);
 }
 
 static void on_response_set_umts_band(TcorePending *p, int data_len, const void *data, void *user_data)
@@ -842,6 +847,7 @@ static void on_response_get_nitz_name(TcorePending *p, int data_len, const void
        int count = 0;
        int net_name_type = 0;
        char *pResp = NULL;
+       char *net_name = NULL;
 
        dbg("Entry on_response_get_nitz_name (+XCOPS)");
        o = tcore_pending_ref_core_object(p);
@@ -855,6 +861,8 @@ static void on_response_get_nitz_name(TcorePending *p, int data_len, const void
                                goto OUT;
                        }
 
+                       memset(&noti, 0, sizeof(struct tnoti_network_identity));
+
                        for (count = 0; count < nol; count++) {
                                // parse each line
                                line = g_slist_nth_data(atResp->lines, count);
@@ -866,32 +874,51 @@ static void on_response_get_nitz_name(TcorePending *p, int data_len, const void
                                        dbg("Net name type  : %d", net_name_type);
 
                                        switch (net_name_type) {
-                                       case 0:     /* plmn_id (mcc, mnc) */
+                                       case 0: /* plmn_id (mcc, mnc) */
                                                if ((pResp = tcore_at_tok_nth(tokens, 1))) {
-                                                       strncpy(noti.plmn, pResp + 1, strlen(pResp) - 2); /* skip quotes (") while copying */
+                                                       if (strlen(pResp) > 0) {
+                                                               net_name = tcore_at_tok_extract((const char *)pResp);
+                                                               strncpy(noti.plmn, net_name, 6);
+                                                               noti.plmn[6] = '\0';
+                                                       }
                                                }
                                                break;
 
-                                       case 5:      /* Short Nitz name*/
+                                       case 5: /* Short NITZ name*/
+                                       case 3: /* Short Network Name (CPHS) */
                                                if ((pResp = tcore_at_tok_nth(tokens, 1))) {
-                                                       strncpy(noti.short_name, pResp + 1, strlen(pResp) - 2); /* skip quotes (") while copying */
+                                                       if (strlen(pResp) > 0) {
+                                                               net_name = tcore_at_tok_extract((const char *)pResp);
+                                                               strncpy(noti.short_name, net_name, 16);
+                                                               noti.short_name[16] = '\0';
+                                                       }
                                                }
                                                break;
 
-                                       case 6:     /* Full Nitz name */
+                                       case 6: /* Full NITZ name */
+                                       case 4: /* Long Network Name (CPHS) */
                                                if ((pResp = tcore_at_tok_nth(tokens, 1))) {
-                                                       strncpy(noti.full_name, pResp + 1, strlen(pResp) - 2); /* skip quotes (") while copying */
+                                                       if (strlen(pResp) > 0) {
+                                                               net_name = tcore_at_tok_extract((const char *)pResp);
+                                                               strncpy(noti.full_name, net_name, 32);
+                                                               noti.full_name[32] = '\0';
+                                                       }
                                                }
                                                break;
 
                                        default:
                                                break;
                                        }
+
+                                       g_free(net_name);
+                                       net_name = NULL;
                                }
-                               if (tokens != NULL)
-                                       tcore_at_tok_free(tokens);
+
+                               tcore_at_tok_free(tokens);
                        }
 
+                       dbg("plmn <%s> short NITZ name<%s> full NITZ name<%s>",
+                               noti.plmn, noti.short_name, noti.full_name);
                        tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_NETWORK_IDENTITY,
                                                                                   sizeof(struct tnoti_network_identity), &noti);
                }
@@ -910,7 +937,6 @@ static void on_response_get_preferred_plmn(TcorePending *p, int data_len, const
        char *line = NULL;
        const TcoreATResponse *atResp = data;
        GSList *tokens = NULL;
-       char temp_plmn_info[17] = {0};
        char *pResp = NULL;
        int plmn_format = 0;
 
@@ -918,7 +944,7 @@ static void on_response_get_preferred_plmn(TcorePending *p, int data_len, const
        int total_lines = 0;
        int GSM_AcT2 = 0, GSM_Compact_AcT2 = 0, UTRAN_AcT2 = 0;
 
-       dbg("Entry on_response_get_preferred_plmn");
+       dbg("Entry");
 
        if (atResp->success > 0) {
                dbg("RESPONSE OK");
@@ -935,10 +961,9 @@ static void on_response_get_preferred_plmn(TcorePending *p, int data_len, const
                                total_lines = MAX_NETWORKS_PREF_PLMN_SUPPORT;
 
 /*
-+COPL: <index1>,<format>,<oper1>[,<GSM_AcT1>,<GSM_Compact_AcT1>,<UTRAN_AcT1>,<E-UTRAN_AcT1>] [<CR><LF>
++CPOL: <index1>,<format>,<oper1>[,<GSM_AcT1>,<GSM_Compact_AcT1>,<UTRAN_AcT1>,<E-UTRAN_AcT1>] [<CR><LF>
 +CPOL: <index2>,<format>,<oper2>[,<GSM_AcT2>,<GSM_Compact_AcT2>,<UTRAN_AcT2>,<E-UTRAN_AcT2>]
 */
-
                        resp.result = TCORE_RETURN_SUCCESS;
 
                        for (i = 0; i < total_lines; i++) {
@@ -951,7 +976,7 @@ static void on_response_get_preferred_plmn(TcorePending *p, int data_len, const
                                /* EF Index */
                                if ((pResp = tcore_at_tok_nth(tokens, 0))) {
                                        dbg("Index : %s", pResp);
-                                       resp.list[i].ef_index = atoi(pResp);
+                                       resp.list[i].index = atoi(pResp);
                                }
                                /* Format */
                                if ((pResp = tcore_at_tok_nth(tokens, 1))) {
@@ -964,21 +989,18 @@ static void on_response_get_preferred_plmn(TcorePending *p, int data_len, const
                                        dbg("plmn ID : %s", pResp);
 
                                        if (strlen(pResp) > 0) {
-                                               strncpy(temp_plmn_info, pResp + 1, (strlen(pResp)) - 2);
+                                               char *oper;
+
+                                               oper = tcore_at_tok_extract((const char *)pResp);
+                                               dbg("operator <%s>", oper);
 
                                                // Get only PLMN ID
                                                if (plmn_format == 2) {
-                                                       // cp_plmn = util_hexStringToBytes(temp_plmn_info);
-
-                                                       if (strncmp((char *) temp_plmn_info, "000000", 6) == 0)
-                                                               continue;
-
-                                                       memcpy(resp.list[i].plmn, temp_plmn_info, 6);
-                                                       if (resp.list[i].plmn[5] == '#')
-                                                               resp.list[i].plmn[5] = '\0';
-
-                                                       // g_free(cp_plmn);
+                                                       strncpy(resp.list[i].plmn, oper, 6);
+                                                       resp.list[i].plmn[6] = '\0';
                                                }
+
+                                               g_free (oper);
                                        }
                                }
 
@@ -1009,7 +1031,12 @@ static void on_response_get_preferred_plmn(TcorePending *p, int data_len, const
                                tcore_at_tok_free(tokens);
                        }
                }
+       } else {
+               dbg("RESPONSE NOT OK");
+               // TODO: CMEE error mapping is required.
+               resp.result = TCORE_RETURN_FAILURE;
        }
+
 OUT:
        ur = tcore_pending_ref_user_request(p);
        if (ur) {
@@ -1024,9 +1051,9 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
        const TcoreATResponse *resp = data;
        UserRequest *ur;
        struct tresp_network_get_serving_network Tresp = {0};
-       char plmn[7] = {0};
        char *long_plmn_name = NULL;
        char *short_plmn_name = NULL;
+       char *plmn_id = NULL;
        CoreObject *o;
        GSList *tokens = NULL;
        const char *line;
@@ -1078,7 +1105,7 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
                                if ((pResp = tcore_at_tok_nth(tokens, 2))) {
                                        dbg("long PLMN  : %s", pResp);
                                        if (strlen(pResp) > 0) {
-                                               long_plmn_name = util_removeQuotes(pResp);
+                                               long_plmn_name = tcore_at_tok_extract((const char *)pResp);
 
                                                // set network name into po
                                                tcore_network_set_network_name(o, TCORE_NETWORK_NAME_TYPE_FULL, long_plmn_name);
@@ -1090,7 +1117,7 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
                                if ((pResp = tcore_at_tok_nth(tokens, 2))) {
                                        dbg("short PLMN  : %s", pResp);
                                        if (strlen(pResp) > 0) {
-                                               short_plmn_name = util_removeQuotes(pResp);
+                                               short_plmn_name = tcore_at_tok_extract((const char *)pResp);
 
                                                // set network name into po
                                                tcore_network_set_network_name(o, TCORE_NETWORK_NAME_TYPE_SHORT, short_plmn_name);
@@ -1102,11 +1129,10 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
                                if ((pResp = tcore_at_tok_nth(tokens, 2))) {
                                        dbg("numeric : %s", pResp);
                                        if (strlen(pResp) > 0) {
-                                               memset(plmn, 0, 7);
+                                               plmn_id = tcore_at_tok_extract((const char *)pResp);
 
-                                               /* Strip off starting quotes & ending quotes */
-                                               strncpy(plmn, pResp + 1, strlen(pResp) - 2);
-                                               tcore_network_set_plmn(o, plmn);
+                                               // set plmn id into po
+                                               tcore_network_set_plmn(o, plmn_id);
                                        }
                                }
                                break;
@@ -1127,7 +1153,8 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
                        tcore_at_tok_free(tokens);
                }
 
-               memcpy(Tresp.plmn, plmn, 7);
+               if(plmn_id)
+                       memcpy(Tresp.plmn, plmn_id, strlen(plmn_id));
                tcore_network_get_access_technology(o, &(Tresp.act));
                tcore_network_get_lac(o, &(Tresp.gsm.lac));
 
@@ -1140,12 +1167,13 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
                        struct tnoti_network_change network_change;
 
                        memset(&network_change, 0, sizeof(struct tnoti_network_change));
-                       memcpy(network_change.plmn, plmn, 7);
+                       if(plmn_id)
+                               memcpy(network_change.plmn, plmn_id, strlen(plmn_id));
                        tcore_network_get_access_technology(o, &(network_change.act));
                        tcore_network_get_lac(o, &(network_change.gsm.lac));
 
                        tcore_server_send_notification(tcore_plugin_ref_server(tcore_pending_ref_plugin(p)), tcore_pending_ref_core_object(p),
-                                                                                  TNOTI_NETWORK_CHANGE, sizeof(struct tnoti_network_change), &network_change);
+                                                                               TNOTI_NETWORK_CHANGE, sizeof(struct tnoti_network_change), &network_change);
                        dbg("dbg.. network_change.plmn  : %s", network_change.plmn);
                        dbg("dbg.. network_change.act  : %d", network_change.act);
                        dbg("dbg.. network_change.gsm.lac  : %d", network_change.gsm.lac);
@@ -1153,14 +1181,16 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
                        if ((AT_COPS_MODE_DEREGISTER != network_mode) &&
                                (AT_COPS_MODE_SET_ONLY != network_mode)) {
                                /*Network identity noti*/
-                               memset(&noti, 0x0, sizeof(struct tnoti_network_change));
+                               memset(&noti, 0x0, sizeof(struct tnoti_network_identity));
                                if (long_plmn_name)
-                                       memcpy(noti.full_name, long_plmn_name, MIN(33, strlen(long_plmn_name)));
+                                       memcpy(noti.full_name, long_plmn_name, MIN(32, strlen(long_plmn_name)));
                                if (short_plmn_name)
-                                       memcpy(noti.short_name, short_plmn_name, MIN(17, strlen(long_plmn_name)));
-                               memcpy(noti.plmn, plmn, 7);
+                                       memcpy(noti.short_name, short_plmn_name, MIN(16, strlen(short_plmn_name)));
+                               if (plmn_id)
+                                       memcpy(noti.plmn, plmn_id, strlen(plmn_id)); // plmn_id length is necessarily <= 6
+
                                tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)),
-                                                                                          o, TNOTI_NETWORK_IDENTITY, sizeof(struct tnoti_network_identity), &noti);
+                                                               o, TNOTI_NETWORK_IDENTITY, sizeof(struct tnoti_network_identity), &noti);
                                dbg("dbg.. noti.short_name  : %s", noti.short_name);
                                dbg("dbg.. noti.full_name  : %s", noti.full_name);
                                dbg("dbg.. noti.plmn  : %s", noti.plmn);
@@ -1169,6 +1199,7 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
 
                g_free(long_plmn_name);
                g_free(short_plmn_name);
+               g_free(plmn_id);
        }
        return;
 }
@@ -1239,23 +1270,32 @@ Note: <Act> is supporting from R7 and above Protocol Stack.
                        goto OUT;
                } else {
                        stat = atoi(pResp);
-                       if ((pResp = g_slist_nth_data(tokens, 1)))
-                               lac = atoi(pResp);
+                       if ((pResp = g_slist_nth_data(tokens, 1))) {
+                               pResp = util_removeQuotes(pResp);
+                               lac = strtol(pResp, NULL, 16);
+                               g_free(pResp);
+                       }
 
-                       if ((pResp = g_slist_nth_data(tokens, 2)))
-                               ci = atoi(pResp);
-                       else
+                       if ((pResp = g_slist_nth_data(tokens, 2))) {
+                               pResp = util_removeQuotes(pResp);
+                               ci = strtol(pResp, NULL, 16);
+                               g_free(pResp);
+                       } else {
                                dbg("No ci in +CGREG");
+                       }
 
                        if ((pResp = g_slist_nth_data(tokens, 3)))
                                AcT = atoi(pResp);
                        else
                                dbg("No AcT in +CGREG");
 
-                       if ((pResp = g_slist_nth_data(tokens, 4)))
-                               rac = atoi(pResp);
-                       else
+                       if ((pResp = g_slist_nth_data(tokens, 4))) {
+                               pResp = util_removeQuotes(pResp);
+                               rac = strtol(pResp, NULL, 16);
+                               g_free(pResp);
+                       } else {
                                dbg("No rac in +CGREG");
+                       }
                }
 
 
@@ -1420,13 +1460,19 @@ Note: <Act> is supporting from R7 and above Protocol Stack.
                        goto OUT;
                } else {
                        stat = atoi(pResp);
-                       if ((pResp = g_slist_nth_data(tokens, 1)))
-                               lac = atoi(pResp);
+                       if ((pResp = g_slist_nth_data(tokens, 1))) {
+                               pResp = util_removeQuotes(pResp);
+                               lac = strtol(pResp, NULL, 16);
+                               g_free(pResp);
+                       }
 
-                       if ((pResp = g_slist_nth_data(tokens, 2)))
-                               ci = atoi(pResp);
-                       else
+                       if ((pResp = g_slist_nth_data(tokens, 2))) {
+                               pResp = util_removeQuotes(pResp);
+                               ci = strtol(pResp, NULL, 16);
+                               g_free(pResp);
+                       } else {
                                dbg("No ci in +CREG");
+                       }
 
                        if ((pResp = g_slist_nth_data(tokens, 3)))
                                AcT = atoi(pResp);
@@ -2066,7 +2112,7 @@ AT+CPOL=
                        req_data->plmn[5] = '\0';
                }
        }
-       cmd_str = g_strdup_printf("AT+CPOL=%d,%d,\"%s\",%d,%d,%d", req_data->ef_index + 1, format, req_data->plmn, gsm_act, gsm_compact_act, utran_act);
+       cmd_str = g_strdup_printf("AT+CPOL=%d,%d,\"%s\",%d,%d,%d", req_data->index + 1, format, req_data->plmn, gsm_act, gsm_compact_act, utran_act);
 
        dbg("cmd_str - %s", cmd_str);
        atreq = tcore_at_request_new(cmd_str, "+CPOL", TCORE_AT_NO_RESULT);
@@ -2106,7 +2152,7 @@ static TReturn get_preferred_plmn(CoreObject *o, UserRequest *ur)
 
        pending = tcore_pending_new(o, 0);
 
-       cmd_str = g_strdup_printf("AT+CPOL?");
+       cmd_str = g_strdup_printf("AT+CPOL=,2;+CPOL?");
        atreq = tcore_at_request_new(cmd_str, "+CPOL", TCORE_AT_MULTILINE);
 
        tcore_pending_set_request_data(pending, 0, atreq);