Fix index mismatch issue
[platform/core/telephony/tel-plugin-imc.git] / src / s_sms.c
index c13f280..1129c5d 100644 (file)
@@ -628,7 +628,7 @@ static void on_response_sms_save_msg(TcorePending *p, int data_len, const void *
                        pResp = g_slist_nth_data(tokens, 0);
                        if (pResp) {
                                dbg("0: %s", pResp);
-                               saveMsgInfo.index = (atoi(pResp) - 1); /* IMC index starts from 1 */
+                               saveMsgInfo.index = atoi(pResp); 
                                saveMsgInfo.result = SMS_SENDSMS_SUCCESS;
                        } else {
                                dbg("No Tokens");
@@ -717,52 +717,68 @@ static void on_response_class2_read_msg(TcorePending *pending, int data_len, con
        dbg("lines: [%p]", at_response->lines);
        g_slist_foreach(at_response->lines, print_glib_list_elem, NULL); //for debug log
 
-       if (at_response->success > 0) {
-               dbg("Response OK");
-               if (at_response->lines) {
-                       //fetch first line
-                       gslist_line = (char *)at_response->lines->data;
+       if (at_response->success <= 0) {
+               err("Response NOK");
+               return;
+       }
 
-                       dbg("gslist_line: [%s]", gslist_line);
+       dbg("Response OK");
+       if (at_response->lines == NULL) {
+               err("No lines");
+               return;
+       }
 
-                       tokens = tcore_at_tok_new(gslist_line);
-                       dbg("Number of tokens: [%d]", g_slist_length(tokens));
-                       g_slist_foreach(tokens, print_glib_list_elem, NULL); //for debug log
+       //fetch first line
+       gslist_line = (char *)at_response->lines->data;
+       if (gslist_line == NULL) {
+               err("Error response data");
+               return;
+       }
 
-                       line_token = g_slist_nth_data(tokens, 2); //Third Token: Length
-                       if (line_token != NULL) {
-                               pdu_len = atoi(line_token);
-                               dbg("Length: [%d]", pdu_len);
-                       }
+       dbg("gslist_line: [%s]", gslist_line);
 
-                       //fetch second line
-                       gslist_line = (char *)at_response->lines->next->data;
+       tokens = tcore_at_tok_new(gslist_line);
+       dbg("Number of tokens: [%d]", g_slist_length(tokens));
+       g_slist_foreach(tokens, print_glib_list_elem, NULL); //for debug log
 
-                       dbg("gslist_line: [%s]", gslist_line);
+       line_token = g_slist_nth_data(tokens, 2); //Third Token: Length
+       if (line_token == NULL) {
+               err("Error response data");
+               tcore_at_tok_free(tokens);
+               return;
+       }
 
-                       //free the consumed token
-                       tcore_at_tok_free(tokens);
+       pdu_len = atoi(line_token);
+       dbg("Length: [%d]", pdu_len);
+       tcore_at_tok_free(tokens);
 
-                       tokens = tcore_at_tok_new(gslist_line);
-                       dbg("Number of tokens: [%d]", g_slist_length(tokens));
-                       g_slist_foreach(tokens, print_glib_list_elem, NULL); //for debug log
+       //fetch second line
+       if (at_response->lines->next == NULL) {
+               err("Error response data");
+               return;
+       }
+       gslist_line = (char *)at_response->lines->next->data;
+       if (gslist_line == NULL) {
+               err("Error response data");
+               return;
+       }
 
-                       hex_pdu = g_slist_nth_data(tokens, 0); //Fetch SMS PDU
+       dbg("gslist_line: [%s]", gslist_line);
 
-                       //free the consumed token
-                       tcore_at_tok_free(tokens);
-               } else {
-                       dbg("No lines");
-               }
-       } else {
-               err("Response NOK");
-       }
+       tokens = tcore_at_tok_new(gslist_line);
+       dbg("Number of tokens: [%d]", g_slist_length(tokens));
+       g_slist_foreach(tokens, print_glib_list_elem, NULL); //for debug log
+
+       hex_pdu = g_slist_nth_data(tokens, 0); //Fetch SMS PDU
 
        /* Convert to Bytes */
        bytePDU = (unsigned char *)util_hexStringToBytes(hex_pdu);
+       if (bytePDU == NULL) {
+               tcore_at_tok_free(tokens);
+               return;
+       }
 
        sca_length = bytePDU[0];
-
        dbg("SCA length = %d", sca_length);
 
        gsmMsgInfo.msgInfo.msgLength = pdu_len;
@@ -781,9 +797,7 @@ static void on_response_class2_read_msg(TcorePending *pending, int data_len, con
        rtn = tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(tcore_pending_ref_core_object(pending))), tcore_pending_ref_core_object(pending), TNOTI_SMS_INCOM_MSG, sizeof(struct tnoti_sms_umts_msg), &gsmMsgInfo);
 
        g_free(bytePDU);
-
-       dbg("Exit");
-       return;
+       tcore_at_tok_free(tokens);
 }
 
 static void on_response_read_msg(TcorePending *pending, int data_len, const void *data, void *user_data)
@@ -898,6 +912,9 @@ static void on_response_read_msg(TcorePending *pending, int data_len, const void
                                                dbg("Invalid Message Length");
                                                resp_read_msg.result = SMS_INVALID_PARAMETER_FORMAT;
                                        }
+                               } else if (sca_length > SMS_ENCODED_SCA_LEN_MAX) {
+                                       dbg("Invalid Message Length");
+                                       resp_read_msg.result = SMS_INVALID_PARAMETER_FORMAT;
                                } else {
                                        if ((resp_read_msg.dataInfo.smsData.msgLength > 0)
                                                && (resp_read_msg.dataInfo.smsData.msgLength <= SMS_SMDATA_SIZE_MAX)) {
@@ -2097,7 +2114,7 @@ static void _response_get_efsms_data(TcorePending *p, int data_len, const void *
                        util_byte_to_hex((const char *)&msg_status, (char *)encoded_data, 1);
 
                        //Update EF-SMS with just status byte overwritten, rest 175 bytes are same as received in read information
-                       cmd_str = g_strdup_printf("AT+CRSM=220,28476,%d, 4, %d, \"%s\"", (req_msg_status->index+1), PDU_LEN_MAX, encoded_data);
+                       cmd_str = g_strdup_printf("AT+CRSM=220,28476,%d, 4, %d, \"%s\"", req_msg_status->index, PDU_LEN_MAX, encoded_data);
                        atreq = tcore_at_request_new((const char *)cmd_str, "+CRSM", TCORE_AT_SINGLELINE);
                        pending = tcore_pending_new(tcore_pending_ref_core_object(pending), 0);
                        if (NULL == cmd_str || NULL == atreq || NULL == pending) {
@@ -2237,7 +2254,7 @@ static TReturn read_msg(CoreObject *obj, UserRequest *ur)
        }
        dbg("index: [%d]", readMsg->index);
 
-       cmd_str = g_strdup_printf("AT+CMGR=%d", (readMsg->index + 1)); //IMC index is one ahead of TAPI
+       cmd_str = g_strdup_printf("AT+CMGR=%d", readMsg->index); //IMC index is one ahead of TAPI
        atreq = tcore_at_request_new((const char *)cmd_str, "+CMGR", TCORE_AT_PDU);
        pending = tcore_pending_new(obj, 0);
 
@@ -2416,7 +2433,7 @@ static TReturn delete_msg(CoreObject *obj, UserRequest *ur)
        if (delete_msg->index == -1) {
                cmd_str = g_strdup_printf("AT+CMGD=0,4"); // Delete All Messages
        } else {
-               cmd_str = g_strdup_printf("AT+CMGD=%d,0", delete_msg->index + 1); // Delete specified index
+               cmd_str = g_strdup_printf("AT+CMGD=%d,0", delete_msg->index); // Delete specified index
        }
 
        pending = tcore_pending_new(obj, 0);
@@ -2881,7 +2898,7 @@ static TReturn set_msg_status(CoreObject *obj, UserRequest *ur)
        }
        msg_status = tcore_user_request_ref_data(ur, NULL);
 
-       cmd_str = g_strdup_printf("AT+CRSM=178,28476,%d,4,%d", (msg_status->index+1), PDU_LEN_MAX);
+       cmd_str = g_strdup_printf("AT+CRSM=178,28476,%d,4,%d", msg_status->index, PDU_LEN_MAX);
        atreq = tcore_at_request_new((const char *)cmd_str, "+CRSM", TCORE_AT_SINGLELINE);
        pending = tcore_pending_new(obj, 0);
        if (NULL == cmd_str || NULL == atreq || NULL == pending) {
@@ -2980,6 +2997,7 @@ static TReturn set_sms_params(CoreObject *obj, UserRequest *ur)
        char *encoded_data = NULL;
        unsigned char *temp_data = NULL;
        int SMSPRecordLen = 0;
+       int *smsp_record_len;
 
        TcoreHal *hal = NULL;
        TcoreATRequest *atreq = NULL;
@@ -3002,7 +3020,11 @@ static TReturn set_sms_params(CoreObject *obj, UserRequest *ur)
        }
 
        //EFsmsp file size is 28 +Y bytes (Y is alpha id size)
-       SMSPRecordLen = 28 + setSmsParams->params.alphaIdLen;
+       smsp_record_len = tcore_plugin_ref_property(tcore_object_ref_plugin(obj), "SMSPRECORDLEN");
+       SMSPRecordLen = *smsp_record_len;
+       if (SMSPRecordLen < nDefaultSMSPWithoutAlphaId)
+               return FALSE;
+
        temp_data = calloc(SMSPRecordLen,1);
        encoded_data = calloc(SMSPRecordLen*2 + 1,1);