IMC plugin update
[platform/core/telephony/tel-plugin-imc.git] / src / imc_sim.c
index 955818c..86596ae 100644 (file)
@@ -41,6 +41,8 @@
 #define ENABLE_FLAG 1
 #define DISABLE_FLAG 2
 
+#define VAL_MINUS_ONE (-1)
+
 #define IMC_SIM_ACCESS_READ_BINARY             176
 #define IMC_SIM_ACCESS_READ_RECORD             178
 #define IMC_SIM_ACCESS_GET_RESPONSE            192
@@ -108,24 +110,27 @@ typedef enum {
 typedef struct {
        guint smsp_count;                                       /**< SMSP record count */
        guint smsp_rec_len;                                     /**< SMSP record length */
+       guint mb_rec_len;                                       /**< MBDN record length */
 } ImcSimPrivateInfo;
 
 typedef struct {
-       gboolean b_valid;                                       /**< Valid or not */
-       guint rec_length;                                       /**< Length of one record in file */
-       guint rec_count;                                        /**< Number of records in file */
-       guint data_size;                                        /**< File size */
+       gboolean b_valid;                                               /**< Valid or not */
+       guint rec_length;                                               /**< Length of one record in file */
+       guint rec_count;                                                /**< Number of records in file */
+       guint data_size;                                                /**< File size */
        guint current_index;                                    /**< Current index to read */
-       ImcSimFileType file_type;                               /**< File type and structure */
+       guint mb_count;                                         /**< Current mbdn read index */
+       int mb_index[TEL_SIM_MSP_CNT_MAX * TEL_SIM_MAILBOX_TYPE_MAX];   /**< List of mbdn index to read */
+       int mbdn_index;                                         /**< MBDN index to set mb_data */
+       TelSimMailBoxNumber *mb_data;                           /**< Set MailBox data */
+       ImcSimFileType file_type;                                       /**< File type and structure */
        ImcSimCurrSecOp sec_op;                                 /**< Current index to read */
-       TelSimMailboxList mbi_list;                             /**< Mailbox List */
-       TelSimMailBoxNumber mb_list[TEL_SIM_MSP_CNT_MAX*5];     /**< Mailbox number */
        TelSimFileId file_id;                                   /**< Current file id */
-       TelSimResult file_result;                               /**< File access result */
+       TelSimResult file_result;                                       /**< File access result */
        TelSimFileResult files;                                 /**< File read data */
        TcoreCommand req_command;                               /**< Request command Id */
        TelSimImsiInfo imsi;                                    /**< Stored locally as of now,
-                                                                         Need to store in secure storage*/
+                                                               Need to store in secure storage */
 } ImcSimMetaInfo;
 
 /* Utility Function Declaration */
@@ -133,17 +138,23 @@ static TelSimResult __imc_sim_decode_status_word(unsigned short status_word1, un
 static void __imc_sim_update_sim_status(CoreObject *co, TelSimCardStatus sim_status);
 static void __imc_sim_notify_sms_state(CoreObject *co, gboolean sms_ready);
 static TelReturn __imc_sim_start_to_cache(CoreObject *co);
-static gboolean __imc_sim_get_sim_type(CoreObject *co, TcoreObjectResponseCallback cb, void *cb_data);
-static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_cb_data, TelSimResult sim_result, gboolean decode_ret);
-static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp_cb_data, TelSimResult sim_result);
-static TelReturn __imc_sim_update_file(CoreObject *co, ImcRespCbData *resp_cb_data, int cmd, TelSimFileId ef,
-                                               int p1, int p2, int p3, char *encoded_data);
+static TelReturn __imc_sim_get_sim_type(CoreObject *co,
+       TcoreObjectResponseCallback cb, void *cb_data);
+static void __imc_sim_next_from_read_binary(CoreObject *co,
+       ImcRespCbData *resp_cb_data, TelSimResult sim_result, gboolean decode_ret);
+static void __imc_sim_next_from_get_response(CoreObject *co,
+       ImcRespCbData *resp_cb_data, TelSimResult sim_result);
+static void __sim_set_mailbox_info(CoreObject *co, ImcRespCbData *resp_cb_data);
+static TelReturn __imc_sim_update_file(CoreObject *co,
+       ImcRespCbData *resp_cb_data, int cmd, TelSimFileId ef,
+       int p1, int p2, int p3, char *encoded_data);
 static void __imc_sim_read_record(CoreObject *co, ImcRespCbData *resp_cb_data);
 static void __imc_sim_read_binary(CoreObject *co, ImcRespCbData *resp_cb_data);
 static TelReturn __imc_sim_get_response (CoreObject *co, ImcRespCbData *resp_cb_data);
 static TelReturn __imc_sim_get_retry_count(CoreObject *co, ImcRespCbData *resp_cb_data);
 static TelSimLockType __imc_sim_lock_type(int lock_type);
-static char *__imc_sim_get_fac_from_lock_type(TelSimLockType lock_type, ImcSimCurrSecOp *sec_op, int flag);
+static char *__imc_sim_get_fac_from_lock_type(TelSimLockType lock_type,
+       ImcSimCurrSecOp *sec_op, int flag);
 static int __imc_sim_get_lock_type(ImcSimCurrSecOp sec_op);
 
 /* Internal Response Functions*/
@@ -154,6 +165,90 @@ static void __on_response_imc_sim_get_response(TcorePending *p, guint data_len,
 static void __on_response_imc_sim_get_retry_count(TcorePending *p, guint data_len, const void *data, void *user_data);
 static void __on_response_imc_sim_update_file(TcorePending *p, guint data_len, const void *data, void *user_data);
 
+static TelSimResult
+__imc_sim_convert_cme_error_tel_sim_result(const TcoreAtResponse *at_resp)
+{
+
+       TelSimResult result = TEL_SIM_RESULT_FAILURE;
+       const gchar *line;
+       GSList *tokens = NULL;
+
+       dbg("Entry");
+
+       if (!at_resp || !at_resp->lines) {
+               err("Invalid response data");
+               return result;
+       }
+
+       line = (const gchar *)at_resp->lines->data;
+       tokens = tcore_at_tok_new(line);
+       if (g_slist_length(tokens) > 0) {
+               gchar *resp_str;
+               gint cme_err;
+
+               resp_str = g_slist_nth_data(tokens, 0);
+               if (!resp_str) {
+                       err("Invalid CME Error data");
+                       tcore_at_tok_free(tokens);
+                       return result;
+               }
+               cme_err = atoi(resp_str);
+               dbg("CME error[%d]", cme_err);
+
+               switch (cme_err) {
+               case 3:
+                       result = TEL_SIM_RESULT_OPERATION_NOT_PERMITTED;
+               break;
+
+               case 4:
+                       result = TEL_SIM_RESULT_OPERATION_NOT_SUPPORTED;
+               break;
+
+               case 10:
+               case 13:
+               case 14:
+               case 15:
+                       result = TEL_SIM_RESULT_CARD_ERROR;
+               break;
+
+               case 5:
+               case 6:
+               case 11:
+               case 17:
+               case 46:
+               case 47:
+                       result = TEL_SIM_RESULT_PIN_REQUIRED;
+               break;
+
+               case 7:
+               case 12:
+               case 18:
+               case 45:
+                       result = TEL_SIM_RESULT_PUK_REQUIRED;
+               break;
+
+               case 16:
+                       result =  TEL_SIM_RESULT_INCORRECT_PASSWORD;
+               break;
+
+               case 20:
+               case 23:
+                       result = TEL_SIM_RESULT_MEMORY_FAILURE;
+               break;
+
+               case 50:
+                       result =  TEL_SIM_RESULT_INVALID_PARAMETER;
+               break;
+
+               default:
+                       result = TEL_SIM_RESULT_FAILURE;
+               }
+       }
+       tcore_at_tok_free(tokens);
+
+       return result;
+}
+
 /* GET SMSP info for SMS module */
 gboolean imc_sim_get_smsp_info(TcorePlugin *plugin, int *rec_count, int *rec_len)
 {
@@ -348,7 +443,7 @@ TelReturn __imc_sim_start_to_cache(CoreObject *co)
        IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_ECC, ret);
        IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_MSISDN, ret);
        IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_SMSP, ret);
-
+       IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_MBDN, ret);
        return ret;
 }
 
@@ -368,6 +463,7 @@ static void __on_response_imc_sim_get_sim_type_internal(CoreObject *co,
 
                        /* Start Caching SIM files */
                        ret = __imc_sim_start_to_cache(co);
+                       dbg("ret:[%d]", ret);
 
                        /* Send SIM Type notification */
                        tcore_object_send_notification(co,
@@ -446,7 +542,7 @@ static void __on_response_imc_sim_get_sim_type(TcorePending *p,
  * Failure:
  *     +CME ERROR: <error>
  */
-gboolean __imc_sim_get_sim_type(CoreObject *co,
+TelReturn __imc_sim_get_sim_type(CoreObject *co,
        TcoreObjectResponseCallback cb, void *cb_data)
 {
        ImcRespCbData *resp_cb_data;
@@ -695,7 +791,7 @@ static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_
                        /* 2G */
                        /* The ME requests the Extended Language Preference. The ME only requests the Language Preference (EFLP) if at least one of the following conditions holds:
                         -      EFELP is not available;
-                        -      EFELP does not contain an entry corresponding to a language specified in ISO 639[30];
+                        -      EFELP does not contain an Entry corresponding to a language specified in ISO 639[30];
                         -      the ME does not support any of the languages in EFELP.
                         */
                        /* 3G */
@@ -710,11 +806,13 @@ static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_
                                } else {
                                        file_meta->file_id = TEL_SIM_EF_LP;
                                        __imc_sim_get_response(co, resp_cb_data);
+                                       return;
                                }
                        } else if (TEL_SIM_CARD_TYPE_USIM) {
                                if (file_meta->file_id == TEL_SIM_EF_LP || file_meta->file_id == TEL_SIM_EF_USIM_LI) {
                                        file_meta->file_id = TEL_SIM_EF_ELP;
                                        __imc_sim_get_response(co, resp_cb_data);
+                                       return;
                                } else {
                                        if (resp_cb_data->cb)
                                                resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
@@ -732,6 +830,7 @@ static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_
                        } else {
                                file_meta->current_index++;
                                __imc_sim_read_record(co, resp_cb_data);
+                               return;
                        }
                } else if (TEL_SIM_CARD_TYPE_GSM == card_type) {
                        if (resp_cb_data->cb)
@@ -742,15 +841,17 @@ static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_
        break;
 
        case TEL_SIM_EF_IMSI:
+               /* Update SIM INIT status - INIT COMPLETE */
+               __imc_sim_update_sim_status(co, TEL_SIM_STATUS_SIM_INIT_COMPLETED);
+
                if (resp_cb_data->cb) {
                        resp_cb_data->cb(co, (gint)sim_result, &file_meta->imsi, resp_cb_data->cb_data);
                } else {
                        file_meta->file_id = TEL_SIM_EF_CPHS_CPHS_INFO;
                        file_meta->file_result = TEL_SIM_RESULT_FAILURE;
                        __imc_sim_get_response(co, resp_cb_data);
+                       return;
                }
-               /* Update SIM INIT status - INIT COMPLETE */
-               __imc_sim_update_sim_status(co, TEL_SIM_STATUS_SIM_INIT_COMPLETED);
        break;
 
        case TEL_SIM_EF_MSISDN:
@@ -772,6 +873,7 @@ static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_
                } else {
                        file_meta->current_index++;
                        __imc_sim_read_record(co, resp_cb_data);
+                       return;
                }
        break;
 
@@ -783,6 +885,7 @@ static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_
                } else {
                        file_meta->current_index++;
                        __imc_sim_read_record(co, resp_cb_data);
+                       return;
                }
        break;
 
@@ -793,14 +896,62 @@ static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_
                } else {
                        file_meta->current_index++;
                        __imc_sim_read_record(co, resp_cb_data);
+                       return;
                }
        break;
 
-       case TEL_SIM_EF_USIM_CFIS:
-       case TEL_SIM_EF_USIM_MWIS:
        case TEL_SIM_EF_USIM_MBI:
+               if (file_meta->current_index == file_meta->rec_count) {
+                       if (file_meta->mb_data) {
+                               guint i = 0;
+                               for (i = 0; i < file_meta->files.data.mb.count; i++) {
+                                       if (file_meta->files.data.mb.list[i].mb_type
+                                                       == file_meta->mb_data->mb_type)
+                                               file_meta->mbdn_index = file_meta->mb_index[i];
+                               }
+
+                               if(!file_meta->mbdn_index)
+                                       file_meta->mbdn_index = VAL_MINUS_ONE;
+
+                               __sim_set_mailbox_info(co, resp_cb_data);
+
+                               return;
+                       }
+
+                       /* Init file_meta to read next EF file */
+                       file_meta->rec_count = 0;
+                       file_meta->current_index = 0;
+                       file_meta->file_id = TEL_SIM_EF_MBDN;
+
+                       /* Read MBDN record*/
+                       dbg("Read MBDN record");
+                       __imc_sim_get_response(co, resp_cb_data);
+
+                       return;
+
+               } else {
+                       file_meta->current_index++;
+
+                       __imc_sim_read_record(co, resp_cb_data);
+
+                       return;
+               }
+       break;
+
        case TEL_SIM_EF_MBDN:
        case TEL_SIM_EF_CPHS_MAILBOX_NUMBERS:
+               if (file_meta->mb_count == file_meta->files.data.mb.count) {
+                       if (resp_cb_data->cb)
+                               resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
+               } else {
+                       file_meta->current_index = file_meta->mb_index[file_meta->mb_count];
+                       __imc_sim_read_record(co, resp_cb_data);
+                       return;
+               }
+       break;
+
+       case TEL_SIM_EF_USIM_CFIS:
+       case TEL_SIM_EF_USIM_MWIS:
        case TEL_SIM_EF_CPHS_INFORMATION_NUMBERS:
                if (file_meta->current_index == file_meta->rec_count) {
                        if (resp_cb_data->cb)
@@ -808,6 +959,7 @@ static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_
                } else {
                        file_meta->current_index++;
                        __imc_sim_read_record(co, resp_cb_data);
+                       return;
                }
        break;
 
@@ -825,6 +977,7 @@ static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_
                file_meta_new->file_result = TEL_SIM_RESULT_FAILURE;
 
                __imc_sim_get_response(co, resp_cb_data);
+               return;
        }
        break;
 
@@ -876,6 +1029,9 @@ static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_
                err("File id not handled [0x%x]", file_meta->file_id);
        break;
        }
+
+       /* free resp_cb_data */
+       imc_destroy_resp_cb_data(resp_cb_data);
 }
 
 static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp_cb_data, TelSimResult sim_result)
@@ -886,7 +1042,9 @@ static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp
        dbg("EF[0x%x] access Result[%d]", file_meta->file_id, sim_result);
 
        file_meta->files.result = sim_result;
-       if (file_meta->file_id != TEL_SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING)
+       if (file_meta->file_id != TEL_SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING &&
+               file_meta->file_id != TEL_SIM_EF_USIM_MBI &&
+               file_meta->file_id != TEL_SIM_EF_MBDN)
                memset(&file_meta->files.data, 0x00, sizeof(file_meta->files.data));
 
        if ((file_meta->file_id != TEL_SIM_EF_ELP && file_meta->file_id != TEL_SIM_EF_LP &&
@@ -894,6 +1052,8 @@ static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp
                && (sim_result != TEL_SIM_RESULT_SUCCESS)) {
                if (resp_cb_data->cb)
                        resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
+               /* free resp_cb_data */
+               imc_destroy_resp_cb_data(resp_cb_data);
                return;
        }
 
@@ -920,12 +1080,15 @@ static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp
                                dbg(" [SIM DATA]fail to get Language information in USIM(EF-LI(6F05),EF-PL(2F05))");
                                if (resp_cb_data->cb)
                                        resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
+                               /* free resp_cb_data */
+                               imc_destroy_resp_cb_data(resp_cb_data);
                                return;
                        }
                }
                break;
 
        case TEL_SIM_EF_LP:
+       case TEL_SIM_EF_USIM_LI:
                if (sim_result == TEL_SIM_RESULT_SUCCESS) {
                        dbg("[SIM DATA] exist EFLP/LI(0x6F05)");
                        __imc_sim_read_binary(co, resp_cb_data);
@@ -935,6 +1098,9 @@ static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp
                        if (TEL_SIM_CARD_TYPE_GSM == card_type) {
                                if (resp_cb_data->cb)
                                        resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
+                               /* free resp_cb_data */
+                               imc_destroy_resp_cb_data(resp_cb_data);
+
                                return;
                        }
                        /* if EFLI is not present, then the language selection shall be as defined in EFPL at the MF level      */
@@ -963,6 +1129,9 @@ static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp
                                " [SIM DATA]SIM_EF_USIM_PL(2A05) access fail. Request SIM_EF_ECC(0x6FB7) info");
                        if (resp_cb_data->cb)
                                        resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
+                       /* free resp_cb_data */
+                       imc_destroy_resp_cb_data(resp_cb_data);
+
                        return;
                }
                break;
@@ -1004,6 +1173,9 @@ static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp
                        tcore_sim_set_cphs_status(co, FALSE);
                        if (resp_cb_data->cb)
                                resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
+
+                       /* free resp_cb_data */
+                       imc_destroy_resp_cb_data(resp_cb_data);
                }
                break;
 
@@ -1019,12 +1191,26 @@ static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp
        case TEL_SIM_EF_MSISDN:
                file_meta->files.data.msisdn_list.list =
                        tcore_malloc0(sizeof(TelSimSubscriberInfo) * file_meta->rec_count);
+               file_meta->current_index++;
+               __imc_sim_read_record(co, resp_cb_data);
+               break;
+
+       case TEL_SIM_EF_MBDN:
+       {
+               ImcSimPrivateInfo *priv_info = NULL;
+               priv_info = tcore_sim_ref_userdata(co);
+               if(priv_info)
+                       priv_info->mb_rec_len = file_meta->rec_length;
+               file_meta->mb_count = 0;
+               file_meta->current_index = file_meta->mb_index[file_meta->mb_count];
+               __imc_sim_read_record(co, resp_cb_data);
+       }
+               break;
 
        case TEL_SIM_EF_OPL:
        case TEL_SIM_EF_PNN:
        case TEL_SIM_EF_USIM_MWIS:
        case TEL_SIM_EF_USIM_MBI:
-       case TEL_SIM_EF_MBDN:
        case TEL_SIM_EF_CPHS_MAILBOX_NUMBERS:
        case TEL_SIM_EF_CPHS_INFORMATION_NUMBERS:
                file_meta->current_index++;
@@ -1088,13 +1274,16 @@ static void __on_response_imc_sim_update_file(TcorePending *p, guint data_len, c
                }
        } else {
                err("RESPONSE NOK");
-               sim_result = TEL_SIM_RESULT_FAILURE;
+               sim_result = __imc_sim_convert_cme_error_tel_sim_result(resp);
        }
 OUT:
        /* Send Response */
        if (resp_cb_data->cb)
                resp_cb_data->cb(co_sim, (gint)sim_result, NULL, resp_cb_data->cb_data);
 
+       /* free resp_cb_data */
+       imc_destroy_resp_cb_data(resp_cb_data);
+
        tcore_at_tok_free(tokens);
        dbg("Exit");
 }
@@ -1126,21 +1315,24 @@ static void __on_response_imc_sim_read_data(TcorePending *p, guint data_len,
                if (resp->lines) {
                        line = (const char *)resp->lines->data;
                        tokens = tcore_at_tok_new(line);
-                       if (g_slist_length(tokens) != 3) {
+                       if (g_slist_length(tokens) < 2) {
                                err("Invalid message");
                                tcore_at_tok_free(tokens);
+                               /* free resp_cb_data */
+                               imc_destroy_resp_cb_data(resp_cb_data);
                                return;
                        }
                }
                sw1 = atoi(g_slist_nth_data(tokens, 0));
                sw2 = atoi(g_slist_nth_data(tokens, 1));
-               res = g_slist_nth_data(tokens, 2);
-
-               tmp = tcore_at_tok_extract(res);
-               tcore_util_hexstring_to_bytes(tmp, &res, (guint *)&res_len);
-               dbg("Response: [%s] Response length: [%d]", res, res_len);
 
                if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
+                       res = g_slist_nth_data(tokens, 2);
+
+                       tmp = tcore_at_tok_extract(res);
+                       tcore_util_hexstring_to_bytes(tmp, &res, (guint *)&res_len);
+                       dbg("Response: [%s] Response length: [%d]", res, res_len);
+
                        sim_result = TEL_SIM_RESULT_SUCCESS;
                        file_meta->files.result = sim_result;
 
@@ -1191,6 +1383,12 @@ static void __on_response_imc_sim_read_data(TcorePending *p, guint data_len,
 
                        case TEL_SIM_EF_SPN:
                                dr = tcore_sim_decode_spn((unsigned char *)res, res_len, &file_meta->files.data.spn);
+                               if( dr == FALSE ) {
+                                       err("SPN decoding failed");
+                               } else {
+                                       tcore_sim_set_disp_condition(co, file_meta->files.data.spn.display_condition);
+                                       tcore_sim_set_spn(co, file_meta->files.data.spn.spn);
+                               }
                        break;
 
                        case TEL_SIM_EF_SPDI:
@@ -1321,29 +1519,63 @@ static void __on_response_imc_sim_read_data(TcorePending *p, guint data_len,
                        case TEL_SIM_EF_USIM_MBI:                       /* linear type */
                        {
                                TelSimMbi *mbi = NULL;
+                               guint count = 0;
 
                                mbi = g_try_new0(TelSimMbi, 1);
                                dr = tcore_sim_decode_mbi((unsigned char *)res, res_len, mbi);
+
+                               dbg("voice_index [0x%2x],fax_index[0x%2x], email_index[0x%2x]," \
+                                       "other_index[0x%2x], video_index [0x%2x]  ", mbi->voice_index,
+                                       mbi->fax_index, mbi->email_index, mbi->other_index, mbi->video_index);
+
                                if (dr == TRUE) {
-                                       memcpy(&file_meta->mbi_list.list[file_meta->mbi_list.count],
-                                                                               mbi, sizeof(TelSimMbi));
-                                       file_meta->mbi_list.count++;
+                                       count = file_meta->files.data.mb.count;
 
-                                       dbg("mbi count[%d]", file_meta->mbi_list.count);
+                                       if(mbi->voice_index) {
+                                               file_meta->files.data.mb.list[count].mb_type = TEL_SIM_MAILBOX_VOICE;
+                                               file_meta->mb_index[count] = mbi->voice_index;
+                                               count++;
+                                       }
+                                       if(mbi->fax_index) {
+                                               file_meta->files.data.mb.list[count].mb_type = TEL_SIM_MAILBOX_FAX;
+                                               file_meta->mb_index[count] = mbi->fax_index;
+                                               count++;
+                                       }
+                                       if(mbi->email_index) {
+                                               file_meta->files.data.mb.list[count].mb_type = TEL_SIM_MAILBOX_EMAIL;
+                                               file_meta->mb_index[count] = mbi->email_index;
+                                               count++;
+                                       }
+                                       if(mbi->other_index) {
+                                               file_meta->files.data.mb.list[count].mb_type = TEL_SIM_MAILBOX_OTHER;
+                                               file_meta->mb_index[count] = mbi->other_index;
+                                               count++;
+                                       }
+                                       if(mbi->video_index) {
+                                               file_meta->files.data.mb.list[count].mb_type = TEL_SIM_MAILBOX_VIDEO;
+                                               file_meta->mb_index[count] = mbi->video_index;
+                                               count++;
+                                       }
+
+                                       file_meta->files.data.mb.count = count;
                                }
 
                                /* Free memory */
                                g_free(mbi);
+                               dbg("index [%d] mb_type[%d]", count, file_meta->files.data.mb.list[count].mb_type);
                        }
                        break;
 
                        case TEL_SIM_EF_CPHS_MAILBOX_NUMBERS:           /* linear type */
                        case TEL_SIM_EF_MBDN:                           /* linear type */
+                       {
                                dr = tcore_sim_decode_xdn((unsigned char *)res, res_len,
-                                                                               file_meta->mb_list[file_meta->current_index-1].alpha_id,
-                                                                               file_meta->mb_list[file_meta->current_index-1].number);
-                               file_meta->mb_list[file_meta->current_index-1].alpha_id_len = strlen(file_meta->mb_list[file_meta->current_index-1].alpha_id);
-                               file_meta->mb_list[file_meta->current_index-1].profile_id = file_meta->current_index;
+                                                       file_meta->files.data.mb.list[file_meta->mb_count].alpha_id,
+                                                       &file_meta->files.data.mb.list[file_meta->mb_count].alpha_id_len,
+                                                       file_meta->files.data.mb.list[file_meta->mb_count].number);
+                               file_meta->files.data.mb.list[file_meta->mb_count].profile_id = file_meta->mb_count+1;
+                               file_meta->mb_count++;
+                       }
                        break;
 
                        case TEL_SIM_EF_CPHS_VOICE_MSG_WAITING:         /* transparent type */
@@ -1454,7 +1686,7 @@ static void __on_response_imc_sim_read_data(TcorePending *p, guint data_len,
        } else {
                err("RESPONSE NOK");
                dbg("Error - File ID: [0x%x]", file_meta->file_id);
-               sim_result = TEL_SIM_RESULT_FAILURE;
+               sim_result = __imc_sim_convert_cme_error_tel_sim_result(resp);
        }
 
        /* Get File data */
@@ -1489,7 +1721,7 @@ static void __on_response_imc_sim_get_response(TcorePending *p,
                        if (g_slist_length(tokens) < 2) {
                                err("Invalid message");
                                tcore_at_tok_free(tokens);
-                               return;
+                               goto OUT;
                        }
                }
                sw1 = atoi(g_slist_nth_data(tokens, 0));
@@ -1615,7 +1847,7 @@ static void __on_response_imc_sim_get_response(TcorePending *p,
                                                dbg("INVALID FCP received - DEbug!");
                                                tcore_at_tok_free(tokens);
                                                g_free(record_data);
-                                               return;
+                                               goto OUT;
                                        }
 
                                        /*File identifier - 0x84,0x85,0x86 etc are currently ignored and not handled */
@@ -1638,7 +1870,7 @@ static void __on_response_imc_sim_get_response(TcorePending *p,
                                                dbg("INVALID FCP received - DEbug!");
                                                tcore_at_tok_free(tokens);
                                                g_free(record_data);
-                                               return;
+                                               goto OUT;
                                        }
 
                                        /* proprietary information */
@@ -1721,7 +1953,7 @@ static void __on_response_imc_sim_get_response(TcorePending *p,
                                                dbg("INVALID FCP received[0x%x] - DEbug!", *ptr_data);
                                                tcore_at_tok_free(tokens);
                                                g_free(record_data);
-                                               return;
+                                               goto OUT;
                                        }
 
                                        dbg("Current ptr_data value is [%x]", *ptr_data);
@@ -1743,7 +1975,7 @@ static void __on_response_imc_sim_get_response(TcorePending *p,
                                                dbg("INVALID FCP received - DEbug!");
                                                tcore_at_tok_free(tokens);
                                                g_free(record_data);
-                                               return;
+                                               goto OUT;
                                        }
 
                                        /* total file size including structural info*/
@@ -1769,7 +2001,7 @@ static void __on_response_imc_sim_get_response(TcorePending *p,
                                        dbg("INVALID FCP received - DEbug!");
                                        tcore_at_tok_free(tokens);
                                        g_free(record_data);
-                                       return;
+                                       goto OUT;
                                }
                        } else if (TEL_SIM_CARD_TYPE_GSM == card_type) {
                                unsigned char gsm_specific_file_data_len = 0;
@@ -1876,9 +2108,10 @@ static void __on_response_imc_sim_get_response(TcorePending *p,
        } else {
                err("RESPONSE NOK");
                err("Failed to get ef[0x%x] (file_meta->file_id) ", file_meta->file_id);
-               sim_result = TEL_SIM_RESULT_FAILURE;
+               sim_result = __imc_sim_convert_cme_error_tel_sim_result(resp);
        }
 
+OUT:
        dbg("Calling __imc_sim_next_from_get_response");
        __imc_sim_next_from_get_response(co, resp_cb_data, sim_result);
        dbg("Exit");
@@ -2247,7 +2480,7 @@ static TelReturn __imc_sim_get_retry_count(CoreObject *co,
 
 static TelSimLockType __imc_sim_lock_type(int lock_type)
 {
-       switch(lock_type) {
+       switch (lock_type) {
                case 1 :
                        return TEL_SIM_LOCK_SC;
                case 2 :
@@ -2272,7 +2505,7 @@ static char *__imc_sim_get_fac_from_lock_type(TelSimLockType lock_type,
                ImcSimCurrSecOp *sec_op, int flag)
 {
        char *fac = NULL;
-       switch(lock_type) {
+       switch (lock_type) {
                case TEL_SIM_LOCK_PS :
                        fac = "PS";
                        if (flag == ENABLE_FLAG)
@@ -2344,7 +2577,7 @@ static char *__imc_sim_get_fac_from_lock_type(TelSimLockType lock_type,
 
 static int __imc_sim_get_lock_type(ImcSimCurrSecOp sec_op)
 {
-       switch(sec_op) {
+       switch (sec_op) {
                case IMC_SIM_CURR_SEC_OP_SIM_DISABLE :
                case IMC_SIM_CURR_SEC_OP_SIM_ENABLE :
                case IMC_SIM_CURR_SEC_OP_SIM_STATUS :
@@ -2471,7 +2704,7 @@ static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_l
        if (NULL == at_resp) {
                err("at_resp is NULL");
                auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-               goto out;
+               goto OUT;
        }
 
        auth_resp.auth_type = *auth_type;
@@ -2487,14 +2720,14 @@ static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_l
                } else {
                        err("at_resp->lines is NULL");
                        auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-                       goto out;
+                       goto OUT;
                }
 
                tokens = tcore_at_tok_new(line);
                if (tokens == NULL) {
                        err("tokens is NULL");
                        auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-                       goto out;
+                       goto OUT;
                }
 
                status = atoi(g_slist_nth_data(tokens, 0));
@@ -2506,19 +2739,19 @@ static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_l
                case 1:
                        err("Synchronize fail");
                        auth_resp.detailed_result = TEL_SIM_AUTH_SYNCH_FAILURE;
-                       goto out;
+                       goto OUT;
                case 2:
                        err("MAC wrong");
                        auth_resp.detailed_result = TEL_SIM_AUTH_MAK_CODE_FAILURE;
-                       goto out;
+                       goto OUT;
                case 3:
                        err("Does not support security context");
                        auth_resp.detailed_result = TEL_SIM_AUTH_UNSUPPORTED_CONTEXT;
-                       goto out;
+                       goto OUT;
                default:
                        err("Other failure");
                        auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-                       goto out;
+                       goto OUT;
                }
 
                if (auth_resp.auth_type == TEL_SIM_AUTH_GSM) {
@@ -2544,7 +2777,7 @@ static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_l
                        } else {
                                err("Invalid Kc");
                                auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-                               goto out;
+                               goto OUT;
                        }
 
                        sres = g_slist_nth_data(tokens, 2);
@@ -2566,7 +2799,7 @@ static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_l
                        } else {
                                err("Invalid SRES");
                                auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-                               goto out;
+                               goto OUT;
                        }
                } else if (auth_resp.auth_type == TEL_SIM_AUTH_3G_CTX) {
                        char *res, *ck, *ik, *kc;
@@ -2592,7 +2825,7 @@ static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_l
                        } else {
                                err("Invalid RES/AUTS");
                                auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-                               goto out;
+                               goto OUT;
                        }
 
                        ck = g_slist_nth_data(tokens, 2);
@@ -2614,7 +2847,7 @@ static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_l
                        } else {
                                err("Invalid CK");
                                auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-                               goto out;
+                               goto OUT;
                        }
 
                        ik = g_slist_nth_data(tokens, 3);
@@ -2636,7 +2869,7 @@ static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_l
                        } else {
                                err("Invalid IK");
                                auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-                               goto out;
+                               goto OUT;
                        }
 
                        kc = g_slist_nth_data(tokens, 4);
@@ -2658,12 +2891,12 @@ static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_l
                        } else {
                                err("Invalid Kc");
                                auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-                               goto out;
+                               goto OUT;
                        }
                } else {
                        err("Not supported");
                        auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
-                       goto out;
+                       goto OUT;
                }
                sim_result = TEL_SIM_RESULT_SUCCESS;
        } else {
@@ -2671,10 +2904,13 @@ static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_l
                auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
        }
 
-out:
+OUT:
        if(resp_cb_data->cb)
                resp_cb_data->cb(co, (gint)sim_result, &auth_resp, resp_cb_data->cb_data);
 
+       /* free resp_cb_data */
+       imc_destroy_resp_cb_data(resp_cb_data);
+
        tcore_at_tok_free(tokens);
 }
 
@@ -2953,6 +3189,8 @@ static void on_response_imc_sim_get_facility(TcorePending *p, guint data_len,
                tcore_at_tok_free(tokens);
        } else {
                err("Sim Get Facility Response- [NOK]");
+               result = __imc_sim_convert_cme_error_tel_sim_result(at_resp);
+
        }
 EXIT:
        /* Invoke callback */
@@ -3007,6 +3245,7 @@ static void on_response_imc_sim_get_lock_info(TcorePending *p, guint data_len,
                tcore_at_tok_free(tokens);
        } else {
                err("Sim Get Lock Info Response- [NOK]");
+               result = __imc_sim_convert_cme_error_tel_sim_result(at_resp);
        }
 EXIT:
        /* Invoke callback */
@@ -3053,6 +3292,7 @@ static void on_response_imc_sim_req_apdu (TcorePending *p, guint data_len, const
                }
        } else {
                err("RESPONSE NOK");
+               sim_result = __imc_sim_convert_cme_error_tel_sim_result(resp);
        }
 
 OUT:
@@ -3060,6 +3300,7 @@ OUT:
        if (resp_cb_data->cb)
                resp_cb_data->cb(co, (gint)sim_result, &apdu_resp, resp_cb_data->cb_data);
        tcore_at_tok_free(tokens);
+       imc_destroy_resp_cb_data(resp_cb_data);
        dbg("Exit");
 }
 
@@ -3101,6 +3342,8 @@ static void on_response_imc_sim_req_atr(TcorePending *p, guint data_len, const v
                }
        } else {
                err("RESPONSE NOK");
+               sim_result = __imc_sim_convert_cme_error_tel_sim_result(resp);
+
        }
 
 OUT:
@@ -3108,6 +3351,7 @@ OUT:
        if (resp_cb_data->cb)
                resp_cb_data->cb(co, (gint)sim_result, &atr_res, resp_cb_data->cb_data);
        tcore_at_tok_free(tokens);
+       imc_destroy_resp_cb_data(resp_cb_data);
        dbg("Exit");
 }
 
@@ -3199,7 +3443,6 @@ static TelReturn imc_sim_get_language (CoreObject *co,
        dbg("Entry");
 
        IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_LP, ret);
-
        return ret;
 }
 
@@ -3210,8 +3453,6 @@ static TelReturn imc_sim_set_language (CoreObject *co,
        ImcSimMetaInfo file_meta = {0, };
        TelSimCardType card_type = TEL_SIM_CARD_TYPE_UNKNOWN;
        ImcRespCbData *resp_cb_data = NULL;
-       char *tmp = NULL;
-       int tmp_len = 0;
        char *encoded_data = NULL;
        int encoded_data_len = 0;
        int p1 = 0;
@@ -3339,52 +3580,108 @@ static TelReturn imc_sim_get_mailbox_info (CoreObject *co,
        dbg("Entry");
 
        IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_USIM_MBI, ret);
-
        return ret;
 }
 
-static TelReturn imc_sim_set_mailbox_info (CoreObject *co,
-       const TelSimMailBoxNumber *request, TcoreObjectResponseCallback cb, void *cb_data)
+static void __sim_set_mailbox_info(CoreObject *co, ImcRespCbData *resp_cb_data)
 {
-       ImcSimMetaInfo file_meta = {0, };
-       ImcRespCbData *resp_cb_data = NULL;
        char *tmp = NULL;
-       int tmp_len = 0;
        char *encoded_data = NULL;
        int encoded_data_len = 0;
        int p1 = 0;
        int p2 = 0;
        int p3 = 0;
+       TelSimResult sim_result = TEL_SIM_RESULT_FAILURE;
+       ImcSimMetaInfo *file_meta = (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
+       ImcSimPrivateInfo *priv_info = tcore_sim_ref_userdata(co);
 
        dbg("Entry");
 
-       file_meta.file_id = TEL_SIM_EF_USIM_MBI;
-       file_meta.file_result = TEL_SIM_RESULT_FAILURE;
+       if (!file_meta || !file_meta->mb_data || !priv_info) {
+               err("NULL data : file_meta[%p], file_meta->mb_data[%p], priv_info[%p]",
+                       file_meta, file_meta->mb_data, priv_info);
+               goto OUT;
+       }
 
-       /* TBD - Do Encoding.
-       if (!tcore_sim_encode_mbi(request, sizeof(request), tmp, &tmp_len)) {
-               err("Failed to encode mwis");
-               return TEL_RETURN_FAILURE;
-       } */
+       if (file_meta->mbdn_index == VAL_MINUS_ONE) {
+               err("Failed to get MBDN Index");
+               goto OUT;
+       }
+
+       dbg("mbdn_index:[%d], mb_rec_len:[%d]",
+               file_meta->mbdn_index, priv_info->mb_rec_len);
 
-       encoded_data_len = tmp_len * 2;
+       if (!tcore_sim_encode_xdn(file_meta->mb_data->alpha_id,
+                       file_meta->mb_data->number,
+                       priv_info->mb_rec_len, &tmp)) {
+               err("Failed to encode mailbox_info");
+               goto OUT;
+       }
+
+       if (!tmp)
+               goto OUT;
+
+       dbg("Record length: [%d]", priv_info->mb_rec_len);
+
+       encoded_data_len = 2 * priv_info->mb_rec_len;
        encoded_data = (char *)tcore_malloc0(encoded_data_len + 1);
-       tcore_util_byte_to_hex(tmp, encoded_data, tmp_len);
+       tcore_util_byte_to_hex(tmp, encoded_data, priv_info->mb_rec_len);
        if (!encoded_data) {
                err("Failed to convert byte to hex");
-               tcore_free(encoded_data);
-               return TEL_RETURN_FAILURE;
+               goto OUT;
        }
 
-       p1 = 1;
+       p1 = file_meta->mbdn_index;
        p2 = 0x04;
-       p3 = encoded_data_len;
-       dbg("encoded_data - [%s], encoded_data_len - %d", encoded_data, encoded_data_len);
+       p3 = priv_info->mb_rec_len;
+       dbg("encoded_data - [%s], encoded_data_len - %d",
+               encoded_data, priv_info->mb_rec_len);
+
+       __imc_sim_update_file(co, resp_cb_data,
+               IMC_SIM_ACCESS_UPDATE_RECORD,
+               TEL_SIM_EF_MBDN, p1, p2, p3, encoded_data);
+
+       tcore_free(tmp);
+       tcore_free(file_meta->mb_data);
+       file_meta->mb_data = NULL;
+
+       dbg("Exit");
+       return;
+
+OUT:
+       /* Send Response */
+       if (resp_cb_data->cb)
+               resp_cb_data->cb(co, (gint)sim_result, NULL, resp_cb_data->cb_data);
+
+       /* Free Memory */
+       tcore_free(tmp);
+       tcore_free(encoded_data);
+       tcore_free(file_meta->mb_data);
+       file_meta->mb_data = NULL;
+       imc_destroy_resp_cb_data(resp_cb_data);
+
+       dbg("Exit");
+}
+
+static TelReturn imc_sim_set_mailbox_info (CoreObject *co,
+       const TelSimMailBoxNumber *request, TcoreObjectResponseCallback cb, void *cb_data)
+{
+       ImcSimMetaInfo file_meta = {0, };
+       ImcRespCbData *resp_cb_data = NULL;
+       TelReturn ret;
+
+       dbg("Entry");
+
+       file_meta.file_id = TEL_SIM_EF_USIM_MBI;
+       file_meta.file_result = TEL_SIM_RESULT_FAILURE;
+
+       file_meta.mb_data = (TelSimMailBoxNumber *)tcore_malloc0(sizeof(TelSimMailBoxNumber));
+       memcpy(file_meta.mb_data, request, sizeof(TelSimMailBoxNumber));
 
        resp_cb_data = imc_create_resp_cb_data(cb, cb_data, &file_meta, sizeof(ImcSimMetaInfo));
+       ret =  __imc_sim_get_response(co, resp_cb_data);
 
-       return __imc_sim_update_file(co, resp_cb_data, IMC_SIM_ACCESS_UPDATE_RECORD,
-                                       TEL_SIM_EF_USIM_MBI, p1, p2, p3, encoded_data);
+       return ret;
 }
 
 static TelReturn imc_sim_get_msisdn (CoreObject *co,
@@ -3454,14 +3751,9 @@ static TelReturn imc_sim_req_authentication (CoreObject *co,
                return TEL_SIM_RESULT_OPERATION_NOT_SUPPORTED;
        }
 
-       if (request->rand_data != NULL) {
-               convert_rand = tcore_malloc0(request->rand_length*2 + 1);
-               tcore_util_byte_to_hex(request->rand_data, convert_rand, request->rand_length);
-               dbg("Convert RAND hex to string: [%s]", convert_rand);
-       } else {
-               err("rand_data is NULL");
-               goto EXIT;
-       }
+       convert_rand = tcore_malloc0(request->rand_length*2 + 1);
+       tcore_util_byte_to_hex(request->rand_data, convert_rand, request->rand_length);
+       dbg("Convert RAND hex to string: [%s]", convert_rand);
 
        switch (request->auth_type) {
        case TEL_SIM_AUTH_GSM:
@@ -3471,14 +3763,11 @@ static TelReturn imc_sim_req_authentication (CoreObject *co,
        break;
        case TEL_SIM_AUTH_3G_CTX:
                context_type = 1;
-               if (request->autn_data != NULL) {
-                       convert_autn = tcore_malloc0(request->autn_length*2 + 1);
-                       tcore_util_byte_to_hex(request->autn_data, convert_autn, request->autn_length);
-                       dbg("Convert AUTN hex to string: [%s]", convert_autn);
-               } else {
-                       err("autn_data is NULL");
-                       goto EXIT;
-               }
+               convert_autn = tcore_malloc0(request->autn_length*2 + 1);
+               tcore_util_byte_to_hex(request->autn_data, convert_autn, request->autn_length);
+
+               dbg("Convert AUTN hex to string: [%s]", convert_autn);
+
                cmd_str = g_strdup_printf("AT+XAUTH=%d,%d,\"%s\",\"%s\"",
                        session_id, context_type, convert_rand, convert_autn);
        break;