fix sim data length count error
[platform/core/telephony/tel-plugin-imc.git] / src / s_network.c
old mode 100644 (file)
new mode 100755 (executable)
index e4aab10..68da560
@@ -369,6 +369,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 +405,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 +425,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 +446,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");
@@ -457,13 +460,8 @@ OUT:
                tcore_user_request_send_response(ur, TRESP_NETWORK_SEARCH, sizeof(struct tresp_network_search), &resp);
        }
 
-       if (tokens)
-               tcore_at_tok_free(tokens);
-
-       if (temp_plmn_info)
-               free(temp_plmn_info);
-
-       return;
+       /* Free tokens */
+       tcore_at_tok_free(tokens);
 }
 
 static void on_response_set_umts_band(TcorePending *p, int data_len, const void *data, void *user_data)
@@ -845,6 +843,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);
@@ -858,6 +857,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);
@@ -869,32 +870,48 @@ 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*/
                                                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 */
                                                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);
                                }
-                               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);
                }
@@ -913,7 +930,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;
 
@@ -921,7 +937,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");
@@ -938,10 +954,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++) {
@@ -954,7 +969,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))) {
@@ -967,21 +982,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';
-
-                                                       // free(cp_plmn);
+                                                       strncpy(resp.list[i].plmn, oper, 6);
+                                                       resp.list[i].plmn[6] = '\0';
                                                }
+
+                                               g_free (oper);
                                        }
                                }
 
@@ -1012,7 +1024,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) {
@@ -1030,6 +1047,7 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
        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;
@@ -1081,7 +1099,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);
@@ -1093,7 +1111,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);
@@ -1105,11 +1123,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;
@@ -1148,7 +1165,7 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
                        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);
@@ -1158,21 +1175,23 @@ static void on_response_get_serving_network(TcorePending *p, int data_len, const
                                /*Network identity noti*/
                                memset(&noti, 0x0, sizeof(struct tnoti_network_change));
                                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);
                        }
                }
-               if (long_plmn_name)
-                       free(long_plmn_name);
-               if (short_plmn_name)
-                       free(short_plmn_name);
+
+               g_free(long_plmn_name);
+               g_free(short_plmn_name);
+               g_free(plmn_id);
        }
        return;
 }
@@ -1302,7 +1321,7 @@ Note: <Act> is supporting from R7 and above Protocol Stack.
                regist_status.roaming_status = tcore_network_get_roaming_state(o);
 
                tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o,
-                                                                          TNOTI_NETWORK_REGISTRATION_STATUS, sizeof(struct tnoti_network_registration_status), &regist_status);
+                                                                          TNOTI_NETWORK_REGISTRATION_STATUS, sizeof(regist_status), &regist_status);
 #if 0
                if (service_type == NETWORK_SERVICE_TYPE_HSDPA)
                        noti.status = TELEPHONY_HSDPA_ON;
@@ -1660,6 +1679,10 @@ static void on_sim_resp_hook_get_netname(UserRequest *ur, enum tcore_response_co
                if ((resp->data.spn.display_condition & 0x03) == 0x01) {
                        tcore_network_set_network_name_priority(o, TCORE_NETWORK_NAME_PRIORITY_ANY);
                }
+
+               // fallback in case no SPN name is provided
+               if (resp->data.spn.spn[0] == '\0')
+                       tcore_network_set_network_name_priority(o, TCORE_NETWORK_NAME_PRIORITY_NETWORK);
        }
 }
 
@@ -2066,7 +2089,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 +2129,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);