fix +CMGR result parsing error
[platform/core/telephony/tel-plugin-imc.git] / src / s_sms.c
index 874f08e..813fa02 100644 (file)
@@ -38,6 +38,8 @@
 #include <at.h>
 #include <plugin.h>
 
+#include <util.h>
+
 #include "common/TelErr.h"
 #include "s_common.h"
 #include "s_sms.h"
@@ -56,7 +58,6 @@
 /*=============================================================
                                                        Device Ready
 ==============================================================*/
-#define AT_SMS_DEVICE_READY                    12              /* AT device ready */
 #define SMS_DEVICE_READY                               1               /* Telephony device ready */
 #define SMS_DEVICE_NOT_READY                   0               /* Telephony device not ready */
 
@@ -89,7 +90,9 @@
 #define AT_SW1_LEN_RESP 0x9F
 
 #define AT_MAX_RECORD_LEN 256
-#define AT_EF_SMS_RECORD_LEN 176
+ /* SCA 12 bytes long and TDPU is 164 bytes long */
+#define PDU_LEN_MAX 176
+#define HEX_PDU_LEN_MAX                        ((PDU_LEN_MAX * 2) + 1)
 
 /*=============================================================
                                                        String Preprocessor
@@ -153,7 +156,7 @@ static void util_sms_free_memory(void *sms_ptr)
        dbg("Exit");
 }
 
+
 static int util_sms_decode_smsParameters(unsigned char *incoming, unsigned int length, struct telephony_sms_Params *params)
 {
        int alpha_id_len = 0;
@@ -221,7 +224,7 @@ static int util_sms_decode_smsParameters(unsigned char *incoming, unsigned int l
                        dbg("Dest Addr = %s", params->tpDestAddr.diallingNum);
                }
        } else {
-               params->tpDestAddr.dialNumLen = 0;                      
+               params->tpDestAddr.dialNumLen = 0;
        }
 
        if ((params->paramIndicator & SMSPValidSvcAddr) == 0) {
@@ -311,63 +314,14 @@ static int util_sms_decode_smsParameters(unsigned char *incoming, unsigned int l
        return TRUE;
 }
 
-
 /*=============================================================
                                                        Notifications
 ==============================================================*/
-static gboolean on_event_sms_ready_status(CoreObject *o, const void *event_info, void *user_data)
-{
-       struct tnoti_sms_ready_status readyStatusInfo = {0,};
-       char *line = NULL;
-       GSList *tokens = NULL;
-       GSList *lines = NULL;
-       char *pResp = NULL;
-       //CoreObject *o = NULL;
-
-       int rtn = -1 , status = 0;
-
-       dbg(" Func Entrance");
-
-       lines = (GSList *)event_info;
-       if (1 != g_slist_length(lines)) {
-               dbg("unsolicited msg but multiple line");
-               goto OUT;
-       }
-       line = (char *) (lines->data);
-
-       dbg(" Func Entrance");
-
-       if (line != NULL) {
-               dbg("Response OK");
-               dbg("noti line is %s", line);
-               tokens = tcore_at_tok_new(line);
-               pResp = g_slist_nth_data(tokens, 0);
-               if (pResp != NULL)
-                       status = atoi(pResp);
-       } else {
-               dbg("Response NOK");
-       }
-
-       if (status == AT_SMS_DEVICE_READY) {
-               readyStatusInfo.status = SMS_DEVICE_READY;
-               tcore_sms_set_ready_status(o, readyStatusInfo.status);
-               dbg("SMS Ready status = [%s]", readyStatusInfo.status ? "TRUE" : "FALSE");
-               rtn = tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SMS_DEVICE_READY, sizeof(struct tnoti_sms_ready_status), &readyStatusInfo);
-               dbg(" Return value [%d]", rtn);
-       } else {
-               readyStatusInfo.status = SMS_DEVICE_NOT_READY;
-       }
-
-OUT:
-       if(NULL!=tokens)
-               tcore_at_tok_free(tokens);
-       return TRUE;
-}
-
-static gboolean on_event_class2_sms_incom_msg(CoreObject *obj, const void *event_info, void *user_data)
+static gboolean on_event_class2_sms_incom_msg(CoreObject *obj,
+                                                                       const void *event_info, void *user_data)
 {
        //+CMTI: <mem>,<index>
-       
+
        GSList *tokens = NULL , *lines = NULL;
        char *line = NULL, *cmd_str = NULL;
        int index = 0, mem_type = 0;
@@ -402,7 +356,7 @@ static gboolean on_event_class2_sms_incom_msg(CoreObject *obj, const void *event
 
        dbg("index: [%d]", index);
 
-       cmd_str = g_strdup_printf("AT+CMGR=%d", index); 
+       cmd_str = g_strdup_printf("AT+CMGR=%d", index);
        atreq     = tcore_at_request_new((const char *)cmd_str, "+CMGR", TCORE_AT_PDU);
        pending = tcore_pending_new(obj, 0);
 
@@ -427,7 +381,7 @@ static gboolean on_event_class2_sms_incom_msg(CoreObject *obj, const void *event
        tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
        tcore_hal_send_request(hal, pending);
        g_free(cmd_str);
-       
+
        if(tokens)
                tcore_at_tok_free(tokens);
 
@@ -437,7 +391,7 @@ static gboolean on_event_class2_sms_incom_msg(CoreObject *obj, const void *event
 static gboolean on_event_sms_incom_msg(CoreObject *o, const void *event_info, void *user_data)
 {
        //+CMT: [<alpha>],<length><CR><LF><pdu> (PDU mode enabled);
-       
+
        int rtn = -1;
        GSList *tokens = NULL;
        GSList *lines = NULL;
@@ -478,7 +432,7 @@ static gboolean on_event_sms_incom_msg(CoreObject *o, const void *event_info, vo
                pdu_len = atoi((char *)g_slist_nth_data(tokens, 0));
                dbg("pdu_len: [%d]", pdu_len);  /* 1: PDU Length */
        }
-       
+
        line = (char *)g_slist_nth_data(lines, 1); /* Fetch Line 2 */
 
        dbg("Line 2: [%s]", line);
@@ -492,12 +446,12 @@ static gboolean on_event_sms_incom_msg(CoreObject *o, const void *event_info, vo
        bytePDU = (unsigned char *)util_hexStringToBytes(line);
 
        sca_length = bytePDU[0];
-               
+
        dbg("SCA length = %d", sca_length);
-       
+
        gsmMsgInfo.msgInfo.msgLength = pdu_len;
 
-       if (sca_length == 0) { 
+       if (sca_length == 0) {
                memcpy(gsmMsgInfo.msgInfo.tpduData, &bytePDU[1], gsmMsgInfo.msgInfo.msgLength);
        } else {
                memcpy(gsmMsgInfo.msgInfo.sca, &bytePDU[1], sca_length);
@@ -509,11 +463,11 @@ static gboolean on_event_sms_incom_msg(CoreObject *o, const void *event_info, vo
        util_hex_dump("      ", gsmMsgInfo.msgInfo.msgLength,gsmMsgInfo.msgInfo.tpduData);
 
        rtn = tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SMS_INCOM_MSG, sizeof(struct tnoti_sms_umts_msg), &gsmMsgInfo);
-       
+
        if(tokens)
                tcore_at_tok_free(tokens);
 
-       free(bytePDU);
+       g_free(bytePDU);
 
        return TRUE;
 }
@@ -590,7 +544,7 @@ static gboolean on_event_sms_cb_incom_msg(CoreObject *o, const void *event_info,
                } else {
                        dbg("token 0 is null");
                }
-               
+
                pdu = g_slist_nth_data(lines, 1);
                if (pdu != NULL) {
                        cbMsgInfo.cbMsg.length = length;
@@ -600,12 +554,12 @@ static gboolean on_event_sms_cb_incom_msg(CoreObject *o, const void *event_info,
 
                        if ((cbMsgInfo.cbMsg.length >0) && (SMS_CB_SIZE_MAX >= cbMsgInfo.cbMsg.length)) {
                                unsigned char *byte_pdu = NULL;
-                               
+
                                byte_pdu = (unsigned char *)util_hexStringToBytes(pdu);
-                               
+
                                memcpy(cbMsgInfo.cbMsg.msgData, (char*)byte_pdu, cbMsgInfo.cbMsg.length);
                                rtn = tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SMS_CB_INCOM_MSG, sizeof(struct tnoti_sms_cellBroadcast_msg), &cbMsgInfo);
-                               free(byte_pdu);
+                               g_free(byte_pdu);
                        } else {
                                dbg("Invalid Message Length");
                        }
@@ -718,7 +672,7 @@ static void on_response_send_umts_msg(TcorePending *pending, int data_len, const
        memset(&resp_umts, 0x00, sizeof(resp_umts));
        resp_umts.result = SMS_DEVICE_FAILURE;
 
-       if (at_response->success > 0) { // success
+       if (at_response->success > 0) { /* SUCCESS */
                dbg("Response OK");
                if (at_response->lines) { // lines present in at_response
                        gslist_line = (char *)at_response->lines->data;
@@ -763,57 +717,73 @@ 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;
 
-       if (sca_length == 0) { 
+       if (sca_length == 0) {
                memcpy(gsmMsgInfo.msgInfo.tpduData, &bytePDU[1], gsmMsgInfo.msgInfo.msgLength);
        } else {
                memcpy(gsmMsgInfo.msgInfo.sca, bytePDU, sca_length);
@@ -826,10 +796,8 @@ 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);
 
-       free(bytePDU);
-
-       dbg("Exit");
-       return;
+       g_free(bytePDU);
+       tcore_at_tok_free(tokens);
 }
 
 static void on_response_read_msg(TcorePending *pending, int data_len, const void *data, void *user_data)
@@ -932,7 +900,7 @@ static void on_response_read_msg(TcorePending *pending, int data_len, const void
 
                                resp_read_msg.dataInfo.smsData.msgLength = pdu_len;
                                dbg("msgLength: [%d]", resp_read_msg.dataInfo.smsData.msgLength);
-                               
+
                                if(0 == sca_length) {
                                        if ((resp_read_msg.dataInfo.smsData.msgLength > 0)
                                                && (resp_read_msg.dataInfo.smsData.msgLength <= SMS_SMDATA_SIZE_MAX))   {
@@ -944,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)) {
@@ -960,7 +931,7 @@ static void on_response_read_msg(TcorePending *pending, int data_len, const void
                                                resp_read_msg.result = SMS_INVALID_PARAMETER_FORMAT;
                                        }
                                }
-                               free(byte_pdu);
+                               g_free(byte_pdu);
                        }else {
                                dbg("NULL PDU");
                        }
@@ -1116,10 +1087,10 @@ static void on_response_get_stored_msg_cnt(TcorePending *pending, int data_len,
                                tcore_pending_link_user_request(pending_new, ur_dup);
                                tcore_pending_set_send_callback(pending_new, on_confirmation_sms_message_send, NULL);
                                tcore_hal_send_request(tcore_object_get_hal(o), pending_new);
-                               
+
                                //free the consumed token
                                tcore_at_tok_free(tokens);
-                       
+
                                g_free(cmd_str);
 
                                dbg("Exit");
@@ -1149,6 +1120,7 @@ static void on_response_get_sca(TcorePending *pending, int data_len, const void
        UserRequest *user_req = NULL;
 
        GSList *tokens = NULL;
+       const char *sca_tok_addr;
        char *gslist_line = NULL, *sca_addr = NULL, *sca_toa = NULL;
 
        dbg("Entry");
@@ -1164,9 +1136,10 @@ static void on_response_get_sca(TcorePending *pending, int data_len, const void
                        gslist_line = (char *)at_response->lines->data;
 
                        tokens = tcore_at_tok_new(gslist_line);
-                       sca_addr = g_slist_nth_data(tokens, 0);
+                       sca_tok_addr = g_slist_nth_data(tokens, 0);
                        sca_toa = g_slist_nth_data(tokens, 1);
 
+                       sca_addr = tcore_at_tok_extract(sca_tok_addr);
                        if ((NULL != sca_addr)
                                && (NULL != sca_toa)) {
                                dbg("sca_addr: [%s]. sca_toa: [%s]", sca_addr, sca_toa);
@@ -1198,8 +1171,8 @@ static void on_response_get_sca(TcorePending *pending, int data_len, const void
 
        tcore_user_request_send_response(user_req, TRESP_SMS_GET_SCA, sizeof(respGetSca), &respGetSca);
 
-       if(tokens)
-               tcore_at_tok_free(tokens);
+       tcore_at_tok_free(tokens);
+       g_free(sca_addr);
 
        dbg("Exit");
        return;
@@ -1251,7 +1224,7 @@ static void on_response_get_cb_config(TcorePending *p, int data_len, const void
 
        memset(&respGetCbConfig, 0, sizeof(struct tresp_sms_get_cb_config));
        respGetCbConfig.result = SMS_DEVICE_FAILURE;
-       
+
        ur = tcore_pending_ref_user_request(p);
        if (!ur) {
                dbg("no user_request");
@@ -1284,6 +1257,8 @@ static void on_response_get_cb_config(TcorePending *p, int data_len, const void
                                                cb_mid_str = util_removeQuotes(pResp);
                                                cb_tokens = tcore_at_tok_new((const char *) cb_mid_str);
 
+                                               g_free(cb_mid_str);
+
                                                num_cb_tokens = g_slist_length(cb_tokens);
                                                dbg("num_cb_tokens = %d", num_cb_tokens);
 
@@ -1299,8 +1274,12 @@ static void on_response_get_cb_config(TcorePending *p, int data_len, const void
                                                                respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = FALSE;
                                                                respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
                                                        }
+                                               } else {
+                                                       respGetCbConfig.cbConfig.msgIdRangeCount = 0;
+                                                       respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = FALSE;
+                                                       respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
                                                }
-                                               
+
                                                for (i = 0; i < num_cb_tokens; i++) {
                                                        respGetCbConfig.cbConfig.msgIDs[i].net3gpp.selected = TRUE;
                                                        respGetCbConfig.cbConfig.msgIdRangeCount++;
@@ -1308,7 +1287,7 @@ static void on_response_get_cb_config(TcorePending *p, int data_len, const void
                                                        mid_tok = tcore_at_tok_nth(cb_tokens, i);
                                                        first_tok = strtok(mid_tok, delim);
                                                        second_tok = strtok(NULL, delim);
-                                       
+
                                                        if ((first_tok != NULL) && (second_tok != NULL)) { // mids in range (320-478)
                                                                dbg("inside if mid_range");
                                                                respGetCbConfig.cbConfig.msgIDs[i].net3gpp.fromMsgId = atoi(first_tok);
@@ -1332,7 +1311,7 @@ static void on_response_get_cb_config(TcorePending *p, int data_len, const void
                                                respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = FALSE;
                                                respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
                                        }
-                               }                       
+                               }
                        } else {
                                        dbg("line is NULL");
                        }
@@ -1347,7 +1326,7 @@ static void on_response_get_cb_config(TcorePending *p, int data_len, const void
 
        if(tokens)
                tcore_at_tok_free(tokens);
-       
+
        return;
 }
 
@@ -1372,7 +1351,7 @@ static void on_response_set_cb_config(TcorePending *pending, int data_len, const
 
        ur = tcore_pending_ref_user_request(pending);
        respSetCbConfig.result = SMS_SENDSMS_SUCCESS;
-       
+
        if (resp->success > 0) {
                dbg("RESPONSE OK");
        } else {
@@ -1484,7 +1463,7 @@ static void on_response_set_msg_status(TcorePending *pending, int data_len, cons
 
        if(tokens)
                tcore_at_tok_free(tokens);
-       
+
        dbg("Exit");
        return;
 }
@@ -1545,8 +1524,8 @@ static void on_response_get_sms_params(TcorePending *pending, int data_len, cons
                                for (i = 0; i < (int) respGetParams.paramsInfo.tpSvcCntrAddr.dialNumLen; i++)
                                        dbg("SCAddr = %d [%02x]", i, respGetParams.paramsInfo.tpSvcCntrAddr.diallingNum[i]);
 
-                               free(recordData);
-                               free(hexData);
+                               g_free(recordData);
+                               g_free(hexData);
                        } else {
                                dbg("No response");
                        }
@@ -1611,7 +1590,7 @@ static void on_response_set_sms_params(TcorePending *pending, int data_len, cons
 
        if(tokens)
                tcore_at_tok_free(tokens);
-               
+
        dbg("Exit");
        return;
 }
@@ -1684,7 +1663,7 @@ static void on_response_get_paramcnt(TcorePending *p, int data_len, const void *
 
                                        ptr_data = (unsigned char *)recordData;
 
-                                       co_sim = tcore_plugin_ref_core_object(tcore_pending_ref_plugin(p), "sim");
+                                       co_sim = tcore_plugin_ref_core_object(tcore_pending_ref_plugin(p), CORE_OBJECT_TYPE_SIM);
                                        sim_type = tcore_sim_get_type(co_sim);
                                        dbg("sim type is %d",sim_type);
 
@@ -1774,8 +1753,8 @@ static void on_response_get_paramcnt(TcorePending *p, int data_len, const void *
                                                                }
                                                        } else {
                                                                dbg("INVALID FCP received - DEbug!");
-                                                               free(hexData);
-                                                               free(recordData);
+                                                               g_free(hexData);
+                                                               g_free(recordData);
                                                                tcore_at_tok_free(tokens);
                                                                return;
                                                        }
@@ -1792,8 +1771,8 @@ static void on_response_get_paramcnt(TcorePending *p, int data_len, const void *
                                                                dbg("Getting FileID=[0x%x]", file_id);
                                                        } else {
                                                                dbg("INVALID FCP received - DEbug!");
-                                                               free(hexData);
-                                                               free(recordData);
+                                                               g_free(hexData);
+                                                               g_free(recordData);
                                                                tcore_at_tok_free(tokens);
                                                                return;
                                                        }
@@ -1832,18 +1811,18 @@ static void on_response_get_paramcnt(TcorePending *p, int data_len, const void *
                                                                switch (*ptr_data) {
                                                                        case 0x04:
                                                                        case 0x06:
-                                                                               dbg("<IPC_RX> operation state -deactivated");
+                                                                               dbg("[RX] Operation State: DEACTIVATED");
                                                                                ptr_data++;
                                                                                break;
 
                                                                        case 0x05:
                                                                        case 0x07:
-                                                                               dbg("<IPC_RX> operation state -activated");
+                                                                               dbg("[RX] Operation State: ACTIVATED");
                                                                                ptr_data++;
                                                                                break;
 
                                                                        default:
-                                                                               dbg("<IPC_RX> DEBUG! LIFE CYCLE STATUS =[0x%x]",*ptr_data);
+                                                                               dbg("[RX] DEBUG! LIFE CYCLE STATUS: [0x%x]",*ptr_data);
                                                                                ptr_data++;
                                                                                break;
                                                                }
@@ -1872,8 +1851,8 @@ static void on_response_get_paramcnt(TcorePending *p, int data_len, const void *
                                                                }
                                                        } else {
                                                                dbg("INVALID FCP received[0x%x] - DEbug!", *ptr_data);
-                                                               free(hexData);
-                                                               free(recordData);
+                                                               g_free(hexData);
+                                                               g_free(recordData);
                                                                tcore_at_tok_free(tokens);
                                                                return;
                                                        }
@@ -1895,8 +1874,8 @@ static void on_response_get_paramcnt(TcorePending *p, int data_len, const void *
                                                                ptr_data = ptr_data + 2;
                                                        } else {
                                                                dbg("INVALID FCP received - DEbug!");
-                                                               free(hexData);
-                                                               free(recordData);
+                                                               g_free(hexData);
+                                                               g_free(recordData);
                                                                tcore_at_tok_free(tokens);
                                                                return;
                                                        }
@@ -1922,8 +1901,8 @@ static void on_response_get_paramcnt(TcorePending *p, int data_len, const void *
                                                        }
                                                } else {
                                                        dbg("INVALID FCP received - DEbug!");
-                                                       free(hexData);
-                                                       free(recordData);
+                                                       g_free(hexData);
+                                                       g_free(recordData);
                                                        tcore_at_tok_free(tokens);
                                                        return;
                                                }
@@ -2022,8 +2001,8 @@ static void on_response_get_paramcnt(TcorePending *p, int data_len, const void *
                                        smsp_record_len = tcore_plugin_ref_property(plugin, "SMSPRECORDLEN");
                                        memcpy(smsp_record_len, &record_len, sizeof(int));
 
-                                       free(recordData);
-                                       free(hexData);
+                                       g_free(recordData);
+                                       g_free(hexData);
                                } else {
                                        /*2. SIM access fail case*/
                                        dbg("SIM access fail");
@@ -2063,7 +2042,7 @@ static void _response_get_efsms_data(TcorePending *p, int data_len, const void *
        const char *line = NULL;
        int sw1 = 0;
        int sw2 = 0;
-       
+
        TcoreHal *hal = NULL;
        TcoreATRequest *atreq = NULL;
        TcorePending *pending = NULL;
@@ -2074,7 +2053,7 @@ static void _response_get_efsms_data(TcorePending *p, int data_len, const void *
        req_msg_status = tcore_user_request_ref_data(ur, NULL);
 
        resp_msg_status.result = SMS_DEVICE_FAILURE;
-               
+
        hal = tcore_object_get_hal(tcore_pending_ref_core_object(pending));
        dbg("msgStatus: [%x], index [%x]", req_msg_status->msgStatus, req_msg_status->index);
 
@@ -2135,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), AT_EF_SMS_RECORD_LEN, encoded_data);
+                       cmd_str = g_strdup_printf("AT+CRSM=220,28476,%d, 4, %d, \"%s\"", (req_msg_status->index+1), 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) {
@@ -2144,7 +2123,7 @@ static void _response_get_efsms_data(TcorePending *p, int data_len, const void *
 
                                //free memory we own
                                g_free(cmd_str);
-                               free(encoded_data);
+                               g_free(encoded_data);
                                util_sms_free_memory(atreq);
                                util_sms_free_memory(pending);
 
@@ -2162,14 +2141,14 @@ static void _response_get_efsms_data(TcorePending *p, int data_len, const void *
                        tcore_hal_send_request(hal, pending);
 
                        g_free(cmd_str);
-                       free(encoded_data);
+                       g_free(encoded_data);
                }
        }
 
 OUT:
        if(tokens)
                tcore_at_tok_free(tokens);
-       
+
        tcore_user_request_send_response(ur, TRESP_SMS_SET_MSG_STATUS , sizeof(struct tresp_sms_set_msg_status), &msg_status);
 
        dbg("Exit");
@@ -2180,101 +2159,73 @@ OUT:
 /*=============================================================
                                                        Requests
 ==============================================================*/
-static TReturn send_umts_msg(CoreObject *obj, UserRequest *ur)
+static TReturn send_umts_msg(CoreObject *co_sms, UserRequest *ur)
 {
-       gchar *cmd_str = NULL;
-       TcoreHal *hal = NULL;
-       TcoreATRequest *atreq = NULL;
-       TcorePending *pending = NULL;
-       const struct treq_sms_send_umts_msg *sendUmtsMsg = NULL;
-       char buf[2*(SMS_SMSP_ADDRESS_LEN+SMS_SMDATA_SIZE_MAX)+1] = {0};
-       int ScLength = 0;
-       int pdu_len = 0;
+       const struct treq_sms_send_umts_msg *send_msg;
+       const unsigned char *tpdu_byte_data, *sca_byte_data;
+       int tpdu_byte_len, pdu_byte_len;
+       char buf[HEX_PDU_LEN_MAX];
+       char pdu[PDU_LEN_MAX];
+       char *cmd_str;
+       int pdu_hex_len, mms;
+       TReturn ret;
 
-       dbg("Entry");
+       dbg("Enter");
 
-       sendUmtsMsg = tcore_user_request_ref_data(ur, NULL);
-       hal = tcore_object_get_hal(obj);
-       if (NULL == sendUmtsMsg || NULL == hal) {
-               err("NULL input. Unable to proceed");
-               dbg("sendUmtsMsg: [%p], hal: [%p]", sendUmtsMsg, hal);
+       send_msg = tcore_user_request_ref_data(ur, NULL);
 
-               dbg("Exit");
-               return TCORE_RETURN_EINVAL;
-       }
+       tpdu_byte_data = send_msg->msgDataPackage.tpduData;
+       sca_byte_data = send_msg->msgDataPackage.sca;
 
-       if(FALSE == tcore_hal_get_power_state(hal)){
-               dbg("cp not ready/n");
-               return TCORE_RETURN_ENOSYS;
-       }
 
-       dbg("msgLength: [%d]", sendUmtsMsg->msgDataPackage.msgLength);
-       util_hex_dump("    ", (SMS_SMDATA_SIZE_MAX+1), (void *)sendUmtsMsg->msgDataPackage.tpduData);
-       util_hex_dump("    ", SMS_SMSP_ADDRESS_LEN, (void *)sendUmtsMsg->msgDataPackage.sca);
+       /* TPDU length is in byte */
+       tpdu_byte_len = send_msg->msgDataPackage.msgLength;
 
-       ScLength = (int)sendUmtsMsg->msgDataPackage.sca[0];
+       /* Use same Radio Resource Channel */
+       mms = send_msg->more;
 
-       dbg("ScLength: [%d]", ScLength);
+       dbg("TDPU length: [%d]", tpdu_byte_len);
+       dbg("SCA semi-octet length: [%d]", sca_byte_data[0]);
 
-       if ((sendUmtsMsg->msgDataPackage.msgLength > 0)
-               && (sendUmtsMsg->msgDataPackage.msgLength <= SMS_SMDATA_SIZE_MAX)
-               && (ScLength <= SMS_SCADDRESS_LEN_MAX)) {
-               if (ScLength == 0) { // ScAddress not specified
-                       buf[0] = '0';
-                       buf[1] = '0';
-                       pdu_len = 2;
-               } else {
-                       dbg("Specifying SCA in TPDU is currently not supported");
+       /* Prepare PDU for hex encoding */
+       pdu_byte_len = tcore_util_pdu_encode(sca_byte_data, tpdu_byte_data,
+                                               tpdu_byte_len, pdu);
 
-                       buf[0] = '0';
-                       buf[1] = '0';
-                       pdu_len = 2;
-               }
+       pdu_hex_len = (int) tcore_util_encode_hex((unsigned char *) pdu,
+                                               pdu_byte_len, buf);
 
-               util_byte_to_hex((const char *)sendUmtsMsg->msgDataPackage.tpduData, (char *)&buf[pdu_len], sendUmtsMsg->msgDataPackage.msgLength);
+       dbg("PDU hexadecimal length: [%d]", pdu_hex_len);
 
-               pdu_len = pdu_len + 2*sendUmtsMsg->msgDataPackage.msgLength;
+       if (mms > 0) {
+               cmd_str = g_strdup_printf("AT+CMMS=%d", mms);
 
-               buf[pdu_len] = '\0'; //Ensure termination
-
-               dbg("pdu_len: [%d]", pdu_len);
-               util_hex_dump("    ", sizeof(buf), (void *)buf);
-
-               //AT+CMGS=<length><CR>PDU is given<ctrl-Z/ESC>
-               cmd_str = g_strdup_printf("AT+CMGS=%d%s%s\x1A", sendUmtsMsg->msgDataPackage.msgLength,"\r",buf);
-               atreq = tcore_at_request_new((const char *)cmd_str, "+CMGS", TCORE_AT_SINGLELINE);
-               pending = tcore_pending_new(obj, 0);
-
-               if (NULL == cmd_str || NULL == atreq || NULL == pending) {
-                       err("Out of memory. Unable to proceed");
-                       dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
-
-                       //free memory we own
-                       g_free(cmd_str);
-                       util_sms_free_memory(atreq);
-                       util_sms_free_memory(pending);
-
-                       dbg("Exit");
-                       return TCORE_RETURN_ENOMEM;
+               ret = tcore_prepare_and_send_at_request(co_sms, cmd_str, NULL,
+                                       TCORE_AT_NO_RESULT, NULL, NULL, NULL,
+                                       on_confirmation_sms_message_send,
+                                       NULL);
+               if (ret != TCORE_RETURN_SUCCESS) {
+                       err("Failed to prepare and send AT request");
+                       goto error;
                }
 
-               util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
+               g_free(cmd_str);
+       }
 
-               tcore_pending_set_request_data(pending, 0, atreq);
-               tcore_pending_set_response_callback(pending, on_response_send_umts_msg, NULL);
-               tcore_pending_link_user_request(pending, ur);
-               tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
-               tcore_hal_send_request(hal, pending);
+       cmd_str = g_strdup_printf("AT+CMGS=%d\r%s\x1A", tpdu_byte_len, buf);
 
-               g_free(cmd_str);
+       ret = tcore_prepare_and_send_at_request(co_sms, cmd_str, "+CMGS:",
+                               TCORE_AT_SINGLELINE, ur,
+                               on_response_send_umts_msg, NULL,
+                               on_confirmation_sms_message_send, NULL);
+       if (ret != TCORE_RETURN_SUCCESS)
+               err("Failed to prepare and send AT request");
 
-               dbg("Exit");
-               return TCORE_RETURN_SUCCESS;
-       }
+error:
+       g_free(cmd_str);
 
-       err("Invalid Data len");
        dbg("Exit");
-       return TCORE_RETURN_SMS_INVALID_DATA_LEN;
+
+       return ret;
 }
 
 static TReturn read_msg(CoreObject *obj, UserRequest *ur)
@@ -2303,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);
 
@@ -2394,7 +2345,7 @@ static TReturn save_msg(CoreObject *obj, UserRequest *ur)
 
                buf[0] = ScLength;
                dbg("ScLength = %d", ScLength);
-               
+
                if(ScLength == 0) {
                        buf[0] = 0;
                } else {
@@ -2410,7 +2361,7 @@ static TReturn save_msg(CoreObject *obj, UserRequest *ur)
                util_hex_dump("    ", sizeof(buf), (void *)buf);
 
                memset (hex_pdu, 0x00, pdu_len * 2 + 1);
-               
+
                util_byte_to_hex((const char *)buf, (char *)hex_pdu, pdu_len);
 
                //AT+CMGW=<length>[,<stat>]<CR>PDU is given<ctrl-Z/ESC>
@@ -2514,7 +2465,7 @@ static TReturn delete_msg(CoreObject *obj, UserRequest *ur)
        return TCORE_RETURN_SUCCESS;
 }
 
-static TReturn get_storedMsgCnt(CoreObject *obj, UserRequest *ur)
+static TReturn get_stored_msg_cnt(CoreObject *obj, UserRequest *ur)
 {
        gchar *cmd_str = NULL;
        TcoreHal *hal = NULL;
@@ -2573,7 +2524,7 @@ static TReturn get_sca(CoreObject *obj, UserRequest *ur)
        TcoreHal *hal = NULL;
        TcoreATRequest *atreq = NULL;
        TcorePending *pending = NULL;
-       
+
        dbg("Entry");
 
        hal = tcore_object_get_hal(obj);
@@ -2780,13 +2731,13 @@ static TReturn set_cb_config(CoreObject *obj, UserRequest *ur)
                                mids_GString = g_string_new("AT+CSCB=1");
                                break;
                        }
-                       
+
                        appendMsgId = setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId;
 
                        for( ctr2 = 0; (ctr2 <= ((setCbConfig->msgIDs[ctr1].net3gpp.toMsgId) - (setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId))); ctr2++ ) {
                                dbg( "%x", appendMsgId);
                                mids_GString = g_string_append(mids_GString, g_strdup_printf("%d", appendMsgId));
-                               
+
                                if (ctr2 == ((setCbConfig->msgIDs[ctr1].net3gpp.toMsgId) - (setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId))) {
                                        mids_GString = g_string_append(mids_GString, "\""); //Mids string termination
                                } else {
@@ -2914,7 +2865,7 @@ static TReturn set_mem_status(CoreObject *obj, UserRequest *ur)
 static TReturn set_delivery_report(CoreObject *obj, UserRequest *ur)
 {
        struct tresp_sms_set_delivery_report respSetDeliveryReport = {0,};
-       
+
        respSetDeliveryReport.result = SMS_SUCCESS;
 
        dbg("Entry");
@@ -2947,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), AT_EF_SMS_RECORD_LEN);
+       cmd_str = g_strdup_printf("AT+CRSM=178,28476,%d,4,%d", (msg_status->index+1), 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) {
@@ -3046,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;
@@ -3068,14 +3020,18 @@ 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);
-       
+
        _tcore_util_sms_encode_smsParameters(&(setSmsParams->params), temp_data, SMSPRecordLen);
 
        util_byte_to_hex((const char *)temp_data, (char *)encoded_data,SMSPRecordLen);
-       
+
        encoded_data_len = ((SMSPRecordLen) * 2);
 
        hal = tcore_object_get_hal(obj);
@@ -3176,7 +3132,7 @@ static struct tcore_sms_operations sms_ops = {
        .read_msg = read_msg,
        .save_msg = save_msg,
        .delete_msg = delete_msg,
-       .get_storedMsgCnt = get_storedMsgCnt,
+       .get_stored_msg_cnt = get_stored_msg_cnt,
        .get_sca = get_sca,
        .set_sca = set_sca,
        .get_cb_config = get_cb_config,
@@ -3191,72 +3147,39 @@ static struct tcore_sms_operations sms_ops = {
        .get_paramcnt = get_paramcnt,
 };
 
-gboolean s_sms_init(TcorePlugin *plugin, TcoreHal *hal)
+gboolean s_sms_init(TcorePlugin *cp, CoreObject *co_sms)
 {
-       CoreObject *obj = NULL;
-       struct property_sms_info *data = NULL;
-       GQueue *work_queue = NULL;
-       int *smsp_record_len = NULL;
-
+       int *smsp_record_len;
        dbg("Entry");
-       dbg("plugin: [%p]", plugin);
-       dbg("hal: [%p]", hal);
 
-       obj = tcore_sms_new(plugin, "umts_sms", &sms_ops, hal);
+       /* Override SMS Operations */
+       tcore_sms_override_ops(co_sms, &sms_ops);
 
-       data = calloc(sizeof(struct property_sms_info), 1);
+       /* Registering for SMS notifications */
+       tcore_object_override_callback(co_sms, "+CMTI:", on_event_class2_sms_incom_msg, NULL);
+       tcore_object_override_callback(co_sms, "\e+CMT:", on_event_sms_incom_msg, NULL);
 
-       if (NULL == obj || NULL == data) {
-               err("Unable to initialize. Exiting");
-               s_sms_exit(plugin);
+       tcore_object_override_callback(co_sms, "\e+CDS", on_event_sms_incom_msg, NULL);
+       tcore_object_override_callback(co_sms, "+XSMSMMSTAT", on_event_sms_memory_status, NULL);
+       tcore_object_override_callback(co_sms, "+CMS", on_event_sms_memory_status, NULL);
 
-               dbg("Exit");
-               return FALSE;
-       }
+       tcore_object_override_callback(co_sms, "+CBMI:", on_event_sms_cb_incom_msg, NULL);
+       tcore_object_override_callback(co_sms, "\e+CBM:", on_event_sms_cb_incom_msg, NULL);
 
-       work_queue = g_queue_new();
-       tcore_object_link_user_data(obj, work_queue);
-
-       //Registering for SMS notifications
-       tcore_object_add_callback(obj, "\e+CMTI", on_event_class2_sms_incom_msg, NULL);
-       tcore_object_add_callback(obj, "\e+CMT", on_event_sms_incom_msg, NULL);
-
-       tcore_object_add_callback(obj, "\e+CDS", on_event_sms_incom_msg, NULL);
-       tcore_object_add_callback(obj, "+XSMSMMSTAT", on_event_sms_memory_status, NULL);
-       tcore_object_add_callback(obj, "+CMS", on_event_sms_memory_status, NULL);
-
-       tcore_object_add_callback(obj, "\e+CBMI", on_event_sms_cb_incom_msg, NULL);
-       tcore_object_add_callback(obj, "\e+CBM", on_event_sms_cb_incom_msg, NULL);
-       tcore_object_add_callback(obj, "+XSIM", on_event_sms_ready_status, NULL);
-
-       tcore_plugin_link_property(plugin, "SMS", data);
-
-       //storing smsp record length
-       smsp_record_len = calloc(sizeof(int), 1);
-       tcore_plugin_link_property(plugin, "SMSPRECORDLEN", smsp_record_len);
+       /* Storing SMSP record length */
+       smsp_record_len = g_new0(int, 1);
+       tcore_plugin_link_property(cp, "SMSPRECORDLEN", smsp_record_len);
 
        dbg("Exit");
        return TRUE;
 }
 
-void s_sms_exit(TcorePlugin *plugin)
+void s_sms_exit(TcorePlugin *cp, CoreObject *co_sms)
 {
-       CoreObject *obj = NULL;
-       struct property_sms_info *data = NULL;
+       int *smsp_record_len;
 
-       dbg("Entry");
-       dbg("plugin: [%p]", plugin);
-
-       obj = tcore_plugin_ref_core_object(plugin, "umts_sms");
-       if (NULL == obj) {
-               err("NULL core object. Nothing to do.");
-               return;
-       }
-       tcore_sms_free(obj);
-
-       data = tcore_plugin_ref_property(plugin, "SMS");
-       util_sms_free_memory(data);
+       smsp_record_len = tcore_plugin_ref_property(cp, "SMSPRECORDLEN");
+       g_free(smsp_record_len);
 
        dbg("Exit");
-       return;
 }