4 * Copyright (c) 2013 Samsung Electronics Co. Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
28 #include <core_object.h>
39 #include "imc_common.h"
42 #define DISABLE_FLAG 2
44 #define IMC_SIM_ACCESS_READ_BINARY 176
45 #define IMC_SIM_ACCESS_READ_RECORD 178
46 #define IMC_SIM_ACCESS_GET_RESPONSE 192
47 #define IMC_SIM_ACCESS_UPDATE_BINARY 214
48 #define IMC_SIM_ACCESS_UPDATE_RECORD 220
50 #define IMC_SIM_READ_FILE(co, cb, cb_data, fileId, ret) \
52 ImcSimMetaInfo file_meta = {0, }; \
53 ImcRespCbData *resp_cb_data = NULL; \
55 file_meta.file_id = fileId; \
56 file_meta.file_result = TEL_SIM_RESULT_FAILURE; \
58 resp_cb_data = imc_create_resp_cb_data(cb, cb_data, &file_meta, sizeof(ImcSimMetaInfo)); \
60 ret = __imc_sim_get_response(co, resp_cb_data); \
61 dbg("Request reading '%s' - [%s]", #fileId, (ret == TEL_RETURN_SUCCESS ? "SUCCESS" : "FAILURE")); \
65 IMC_SIM_FILE_TYPE_DEDICATED = 0x00, /**< Dedicated */
66 IMC_SIM_FILE_TYPE_TRANSPARENT = 0x01, /**< Transparent -binary type*/
67 IMC_SIM_FILE_TYPE_LINEAR_FIXED = 0x02, /**< Linear fixed - record type*/
68 IMC_SIM_FILE_TYPE_CYCLIC = 0x04, /**< Cyclic - record type*/
69 IMC_SIM_FILE_TYPE_INVALID_TYPE = 0xFF /**< Invalid type */
73 IMC_SIM_CURR_SEC_OP_PIN1_VERIFY,
74 IMC_SIM_CURR_SEC_OP_PIN2_VERIFY,
75 IMC_SIM_CURR_SEC_OP_PUK1_VERIFY,
76 IMC_SIM_CURR_SEC_OP_PUK2_VERIFY,
77 IMC_SIM_CURR_SEC_OP_SIM_VERIFY,
78 IMC_SIM_CURR_SEC_OP_ADM_VERIFY,
79 IMC_SIM_CURR_SEC_OP_PIN1_CHANGE,
80 IMC_SIM_CURR_SEC_OP_PIN2_CHANGE,
81 IMC_SIM_CURR_SEC_OP_PIN1_ENABLE,
82 IMC_SIM_CURR_SEC_OP_PIN1_DISABLE,
83 IMC_SIM_CURR_SEC_OP_PIN2_ENABLE,
84 IMC_SIM_CURR_SEC_OP_PIN2_DISABLE, // 10
85 IMC_SIM_CURR_SEC_OP_SIM_ENABLE,
86 IMC_SIM_CURR_SEC_OP_SIM_DISABLE,
87 IMC_SIM_CURR_SEC_OP_NET_ENABLE,
88 IMC_SIM_CURR_SEC_OP_NET_DISABLE,
89 IMC_SIM_CURR_SEC_OP_NS_ENABLE,
90 IMC_SIM_CURR_SEC_OP_NS_DISABLE,
91 IMC_SIM_CURR_SEC_OP_SP_ENABLE,
92 IMC_SIM_CURR_SEC_OP_SP_DISABLE,
93 IMC_SIM_CURR_SEC_OP_CP_ENABLE,
94 IMC_SIM_CURR_SEC_OP_CP_DISABLE, // 20
95 IMC_SIM_CURR_SEC_OP_FDN_ENABLE,
96 IMC_SIM_CURR_SEC_OP_FDN_DISABLE,
97 IMC_SIM_CURR_SEC_OP_PIN1_STATUS,
98 IMC_SIM_CURR_SEC_OP_PIN2_STATUS,
99 IMC_SIM_CURR_SEC_OP_FDN_STATUS,
100 IMC_SIM_CURR_SEC_OP_NET_STATUS,
101 IMC_SIM_CURR_SEC_OP_NS_STATUS,
102 IMC_SIM_CURR_SEC_OP_SP_STATUS,
103 IMC_SIM_CURR_SEC_OP_CP_STATUS,
104 IMC_SIM_CURR_SEC_OP_SIM_STATUS,
105 IMC_SIM_CURR_SEC_OP_SIM_UNKNOWN = 0xff
109 guint smsp_count; /**< SMSP record count */
110 guint smsp_rec_len; /**< SMSP record length */
114 gboolean b_valid; /**< Valid or not */
115 guint rec_length; /**< Length of one record in file */
116 guint rec_count; /**< Number of records in file */
117 guint data_size; /**< File size */
118 guint current_index; /**< Current index to read */
119 ImcSimFileType file_type; /**< File type and structure */
120 ImcSimCurrSecOp sec_op; /**< Current index to read */
121 TelSimMailboxList mbi_list; /**< Mailbox List */
122 TelSimMailBoxNumber mb_list[TEL_SIM_MSP_CNT_MAX*5]; /**< Mailbox number */
123 TelSimFileId file_id; /**< Current file id */
124 TelSimResult file_result; /**< File access result */
125 TelSimFileResult files; /**< File read data */
126 TcoreCommand req_command; /**< Request command Id */
127 TelSimImsiInfo imsi; /**< Stored locally as of now,
128 Need to store in secure storage*/
131 /* Utility Function Declaration */
132 static TelSimResult __imc_sim_decode_status_word(unsigned short status_word1, unsigned short status_word2);
133 static void __imc_sim_update_sim_status(CoreObject *co, TelSimCardStatus sim_status);
134 static void __imc_sim_notify_sms_state(CoreObject *co, gboolean sms_ready);
135 static TelReturn __imc_sim_start_to_cache(CoreObject *co);
136 static gboolean __imc_sim_get_sim_type(CoreObject *co, TcoreObjectResponseCallback cb, void *cb_data);
137 static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_cb_data, TelSimResult sim_result, gboolean decode_ret);
138 static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp_cb_data, TelSimResult sim_result);
139 static TelReturn __imc_sim_update_file(CoreObject *co, ImcRespCbData *resp_cb_data, int cmd, TelSimFileId ef,
140 int p1, int p2, int p3, char *encoded_data);
141 static void __imc_sim_read_record(CoreObject *co, ImcRespCbData *resp_cb_data);
142 static void __imc_sim_read_binary(CoreObject *co, ImcRespCbData *resp_cb_data);
143 static TelReturn __imc_sim_get_response (CoreObject *co, ImcRespCbData *resp_cb_data);
144 static TelReturn __imc_sim_get_retry_count(CoreObject *co, ImcRespCbData *resp_cb_data);
145 static TelSimLockType __imc_sim_lock_type(int lock_type);
146 static char *__imc_sim_get_fac_from_lock_type(TelSimLockType lock_type, ImcSimCurrSecOp *sec_op, int flag);
147 static int __imc_sim_get_lock_type(ImcSimCurrSecOp sec_op);
149 /* Internal Response Functions*/
150 static void __on_response_imc_sim_get_sim_type_internal(CoreObject *co, gint result, const void *response, void *user_data);
151 static void __on_response_imc_sim_get_sim_type(TcorePending *p, guint data_len, const void *data, void *user_data);
152 static void __on_response_imc_sim_read_data(TcorePending *p, guint data_len, const void *data, void *user_data);
153 static void __on_response_imc_sim_get_response(TcorePending *p, guint data_len, const void *data, void *user_data);
154 static void __on_response_imc_sim_get_retry_count(TcorePending *p, guint data_len, const void *data, void *user_data);
155 static void __on_response_imc_sim_update_file(TcorePending *p, guint data_len, const void *data, void *user_data);
157 /* GET SMSP info for SMS module */
158 gboolean imc_sim_get_smsp_info(TcorePlugin *plugin, int *rec_count, int *rec_len)
160 CoreObject *co = NULL;
161 ImcSimPrivateInfo *priv_info = NULL;
165 co = tcore_plugin_ref_core_object(plugin, CORE_OBJECT_TYPE_SIM);
166 priv_info = tcore_sim_ref_userdata(co);
170 *rec_count = priv_info->smsp_count;
171 *rec_len = priv_info->smsp_rec_len;
173 dbg("smsp_count:[%d], smsp_rec_len:[%d]", priv_info->smsp_count, priv_info->smsp_rec_len);
177 static void __imc_sim_set_identity(CoreObject *co, TelSimImsiInfo *imsi)
179 gchar new_imsi[15 + 1] = {0, };
182 memcpy(&new_imsi, imsi->mcc, strlen(imsi->mcc));
183 memcpy(&new_imsi[strlen(imsi->mcc)], imsi->mnc, strlen(imsi->mnc));
184 memcpy(&new_imsi[strlen(imsi->mcc) + strlen(imsi->mnc)], imsi->msin, strlen(imsi->msin));
186 /* TODO: This is temporary code, we should use secure storage instead of vconf */
187 old_imsi = vconf_get_str("db/telephony/imsi");
189 if (g_strcmp0(old_imsi, new_imsi) != 0) {
191 vconf_set_str("db/telephony/imsi", new_imsi);
192 tcore_sim_set_identification(co, TRUE);
195 tcore_sim_set_identification(co, FALSE);
198 dbg("Old IMSI value is NULL, set IMSI");
199 vconf_set_str("db/telephony/imsi", new_imsi);
200 tcore_sim_set_identification(co, TRUE);
204 /* Utility Functions */
205 static TelSimResult __imc_sim_decode_status_word(unsigned short status_word1, unsigned short status_word2)
207 TelSimResult rst = TEL_SIM_RESULT_FAILURE;
209 if (status_word1 == 0x93 && status_word2 == 0x00) {
210 /*Failed SIM request command*/
211 dbg("error - SIM application toolkit busy [%x][%x]", status_word1, status_word2);
212 } else if (status_word1 == 0x94 && status_word2 == 0x00) {
213 /*Failed SIM request command*/
214 dbg("error - No EF Selected [%x][%x]", status_word1, status_word2);
215 } else if (status_word1 == 0x94 && status_word2 == 0x02) {
216 /*Failed SIM request command*/
217 dbg("error - Out of Range - Invalid address or record number[%x][%x]",
218 status_word1, status_word2);
219 } else if (status_word1 == 0x94 && status_word2 == 0x04) {
220 /*Failed SIM request command*/
221 dbg("error - File ID not found [%x][%x]", status_word1, status_word2);
222 } else if (status_word1 == 0x94 && status_word2 == 0x08) {
223 /*Failed SIM request command*/
224 dbg("error - File is inconsistent with command - Modem not support or USE IPC [%x][%x]",
225 status_word1, status_word2);
226 } else if (status_word1 == 0x98 && status_word2 == 0x02) {
227 /*Failed SIM request command*/
228 dbg("error - CHV not initialized [%x][%x]", status_word1, status_word2);
229 } else if (status_word1 == 0x98 && status_word2 == 0x04) {
230 /*Failed SIM request command*/
231 dbg("error - Access condition not fullfilled [%x][%x]", status_word1, status_word2);
232 dbg("error -Unsuccessful CHV verification - at least one attempt left [%x][%x]",
233 status_word1, status_word2);
234 dbg("error - Unsuccessful Unblock CHV - at least one attempt left [%x][%x]",
235 status_word1, status_word2);
236 dbg("error - Authentication failure [%x][%x]", status_word1, status_word2);
237 } else if (status_word1 == 0x98 && status_word2 == 0x08) {
238 /*Failed SIM request command*/
239 dbg("error - Contradiction with CHV status [%x][%x]", status_word1, status_word2);
240 } else if (status_word1 == 0x98 && status_word2 == 0x10) {
241 /*Failed SIM request command*/
242 dbg("error - Contradiction with invalidation status [%x][%x]",
243 status_word1, status_word2);
244 } else if (status_word1 == 0x98 && status_word2 == 0x40) {
245 /*Failed SIM request command*/
246 dbg("error -Unsuccessful CHV verification - no attempt left [%x][%x]",
247 status_word1, status_word2);
248 dbg("error - Unsuccessful Unblock CHV - no attempt left [%x][%x]",
249 status_word1, status_word2);
250 dbg("error - CHV blocked [%x][%x]", status_word1, status_word2);
251 } else if (status_word1 == 0x67 && status_word2 == 0x00) {
252 dbg("error -Incorrect Parameter 3 [%x][%x]", status_word1, status_word2);
253 } else if (status_word1 == 0x6B && status_word2 == 0x00) {
254 dbg("error -Incorrect Parameter 1 or 2 [%x][%x]", status_word1, status_word2);
255 } else if (status_word1 == 0x6D && status_word2 == 0x00) {
256 dbg("error -Unknown instruction given as command [%x][%x]", status_word1, status_word2);
257 } else if (status_word1 == 0x6E && status_word2 == 0x00) {
258 dbg("error -Unknown instruction given as command [%x][%x]", status_word1, status_word2);
259 } else if (status_word1 == 0x69 && status_word2 == 0x82) {
260 dbg("error -Access denied [%x][%x]", status_word1, status_word2);
261 } else if (status_word1 == 0x6A && status_word2 == 0x87) {
262 dbg("error -Incorrect parameters [%x][%x]", status_word1, status_word2);
263 } else if (status_word1 == 0x6A && status_word2 == 0x82) {
264 dbg("error -File Not found [%x][%x]", status_word1, status_word2);
265 } else if (status_word1 == 0x6A && status_word2 == 0x83) {
266 dbg("error -Record Not found [%x][%x]", status_word1, status_word2);
268 rst = TEL_SIM_RESULT_CARD_ERROR;
269 dbg("error -Unknown state [%x][%x]", status_word1, status_word2);
274 static void __imc_sim_update_sim_status(CoreObject *co, TelSimCardStatus sim_status)
276 TelSimCardStatus curr_sim_status;
279 * Send SIM Init status, if not sent already
281 (void)tcore_sim_get_status(co, &curr_sim_status);
282 if (sim_status != curr_sim_status) {
283 TelSimCardStatusInfo sim_status_noti = {0, };
285 dbg("Change in SIM State - Old State: [0x%02x] --> New State: [0x%02x]",
286 curr_sim_status, sim_status);
288 /* Update SIM Status */
289 tcore_sim_set_status(co, sim_status);
290 sim_status_noti.status = sim_status;
291 tcore_sim_get_identification(co, &sim_status_noti.change_status);
293 /* Send notification: SIM Status */
294 tcore_object_send_notification(co,
295 TCORE_NOTIFICATION_SIM_STATUS,
296 sizeof(sim_status_noti), &sim_status_noti);
300 static void __imc_sim_notify_sms_state(CoreObject *co,
305 gboolean sms_status = FALSE;
309 plugin = tcore_object_ref_plugin(co);
310 co_sms = tcore_plugin_ref_core_object(plugin, CORE_OBJECT_TYPE_SMS);
311 tcore_check_return_assert(co_sms != NULL);
313 (void)tcore_sms_get_ready_status(co_sms, &sms_status);
314 if (sms_status == sms_ready) {
315 dbg("No change in SMS Status: [%s]",
316 (sms_status ? "INITIALIZED" : "UNINITIALIZED"));
318 TelSimCardStatus sim_status;
320 /* Update SMS State */
321 tcore_sms_set_ready_status(co_sms, sms_ready);
323 dbg("SMS Status - Changed [%s] --> [%s]",
324 (sms_status ? "INITIALIZED" : "UNINITIALIZED"),
325 (sms_ready ? "INITIALIZED" : "UNINITIALIZED"));
328 * Send SMS device ready notification, if SIM is initialiazed.
330 (void)tcore_sim_get_status(co, &sim_status);
331 if (sim_status == TEL_SIM_STATUS_SIM_INIT_COMPLETED) {
332 /* Send notification: SMS Device ready */
333 tcore_object_send_notification(co_sms,
334 TCORE_NOTIFICATION_SMS_DEVICE_READY,
335 sizeof(sms_ready), &sms_ready);
340 TelReturn __imc_sim_start_to_cache(CoreObject *co)
343 IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_IMSI, ret);
344 IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_CPHS_CPHS_INFO, ret);
345 IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_ICCID, ret);
346 IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_SPN, ret);
347 IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_SST, ret);
348 IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_ECC, ret);
349 IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_MSISDN, ret);
350 IMC_SIM_READ_FILE(co, NULL, NULL, TEL_SIM_EF_SMSP, ret);
355 static void __on_response_imc_sim_get_sim_type_internal(CoreObject *co,
356 gint result, const void *response, void *user_data)
358 dbg("SIM Response - SIM Type (internal): [+XUICC]");
360 if (result == TEL_SIM_RESULT_SUCCESS) {
361 TelSimCardType *sim_type = (TelSimCardType *)response;
362 dbg("SIM Type: [%d]", *sim_type);
364 /* Update SIM type */
365 tcore_sim_set_type(co, *sim_type);
366 if (*sim_type != TEL_SIM_CARD_TYPE_UNKNOWN) {
369 /* Start Caching SIM files */
370 ret = __imc_sim_start_to_cache(co);
372 /* Send SIM Type notification */
373 tcore_object_send_notification(co,
374 TCORE_NOTIFICATION_SIM_TYPE,
375 sizeof(TelSimCardType), sim_type);
380 static void __on_response_imc_sim_get_sim_type(TcorePending *p,
381 guint data_len, const void *data, void *user_data)
383 const TcoreAtResponse *at_resp = data;
384 CoreObject *co = tcore_pending_ref_core_object(p);
385 ImcRespCbData *resp_cb_data = user_data;
386 TelSimCardType sim_type = TEL_SIM_CARD_TYPE_UNKNOWN;
388 TelSimResult result = TEL_SIM_RESULT_FAILURE;
390 dbg("SIM Response - SIM Type: [+XUICC]");
392 tcore_check_return_assert(co != NULL);
393 tcore_check_return_assert(resp_cb_data != NULL);
395 if (at_resp && at_resp->success) {
396 if (at_resp->lines) {
400 line = (const gchar *)at_resp->lines->data;
407 tokens = tcore_at_tok_new(line);
410 if (g_slist_length(tokens) == 1) {
411 guint state = atoi(g_slist_nth_data(tokens, 0));
413 if (state == 0) /* 0 - 2G SIM */
414 sim_type = TEL_SIM_CARD_TYPE_GSM;
415 else if (state == 1) /* 1 - 3G SIM */
416 sim_type = TEL_SIM_CARD_TYPE_USIM;
418 result = TEL_SIM_RESULT_SUCCESS;
421 err("Invalid message");
424 tcore_at_tok_free(tokens);
428 /* Invoke callback */
429 if (resp_cb_data->cb)
430 resp_cb_data->cb(co, (gint)result, &sim_type, resp_cb_data->cb_data);
432 /* Free callback data */
433 imc_destroy_resp_cb_data(resp_cb_data);
437 * Operation - get_sim_type
440 * AT-Command: AT+XUICC?
442 * Response - sim_type (TelSimCardType)
443 * Success: (Single line) -
447 * +CME ERROR: <error>
449 gboolean __imc_sim_get_sim_type(CoreObject *co,
450 TcoreObjectResponseCallback cb, void *cb_data)
452 ImcRespCbData *resp_cb_data;
455 /* Response callback data */
456 resp_cb_data = imc_create_resp_cb_data(cb, cb_data,
459 /* Send Request to modem */
460 ret = tcore_at_prepare_and_send_request(co,
461 "AT+XUICC?", "+XUICC:",
462 TCORE_AT_COMMAND_TYPE_SINGLELINE,
463 TCORE_PENDING_PRIORITY_DEFAULT,
465 __on_response_imc_sim_get_sim_type, resp_cb_data,
466 on_send_imc_request, NULL,
468 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Get SIM Type");
473 static void __imc_sim_process_sim_status(CoreObject *co, guint sim_state)
475 TelSimCardStatus sim_card_status;
479 sim_card_status = TEL_SIM_STATUS_CARD_NOT_PRESENT;
484 sim_card_status = TEL_SIM_STATUS_SIM_PIN_REQUIRED;
489 sim_card_status = TEL_SIM_STATUS_SIM_INITIALIZING;
490 dbg("PIN DISABLED AT BOOT UP");
494 sim_card_status = TEL_SIM_STATUS_SIM_INITIALIZING;
499 sim_card_status = TEL_SIM_STATUS_SIM_PUK_REQUIRED;
504 sim_card_status = TEL_SIM_STATUS_SIM_PUK_REQUIRED;
505 dbg("CARD PERMANENTLY BLOCKED");
509 sim_card_status = TEL_SIM_STATUS_CARD_ERROR;
510 dbg("SIM CARD ERROR");
514 sim_card_status = TEL_SIM_STATUS_SIM_INIT_COMPLETED;
515 dbg("SIM INIT COMPLETED");
519 sim_card_status = TEL_SIM_STATUS_CARD_ERROR;
520 dbg("SIM CARD ERROR");
524 sim_card_status = TEL_SIM_STATUS_CARD_REMOVED;
529 dbg("SIM SMS Ready");
531 /* Notify SMS status */
532 return __imc_sim_notify_sms_state(co, TRUE);
535 sim_card_status = TEL_SIM_STATUS_UNKNOWN;
536 dbg("SIM STATE UNKNOWN");
540 err("Unknown/Unsupported SIM state: [%d]", sim_state);
544 switch (sim_card_status) {
545 case TEL_SIM_STATUS_SIM_INIT_COMPLETED: {
546 TelSimCardType sim_type;
548 dbg("SIM INIT COMPLETED");
550 (void)tcore_sim_get_type(co, &sim_type);
551 if (sim_type == TEL_SIM_CARD_TYPE_UNKNOWN) {
553 * SIM is initialized for first time, need to
556 (void)__imc_sim_get_sim_type(co,
557 __on_response_imc_sim_get_sim_type_internal, NULL);
564 case TEL_SIM_STATUS_CARD_REMOVED:
565 dbg("SIM CARD REMOVED");
566 tcore_sim_set_type(co, TEL_SIM_CARD_TYPE_UNKNOWN);
569 case TEL_SIM_STATUS_CARD_NOT_PRESENT:
570 dbg("SIM CARD NOT PRESENT");
571 tcore_sim_set_type(co, TEL_SIM_CARD_TYPE_UNKNOWN);
574 case TEL_SIM_STATUS_CARD_ERROR:
575 dbg("SIM CARD ERROR");
576 tcore_sim_set_type(co, TEL_SIM_CARD_TYPE_UNKNOWN);
580 err("SIM Status: [0x%02x]", sim_card_status);
584 /* Update SIM Status */
585 return __imc_sim_update_sim_status(co, sim_card_status);
588 static void __on_response_imc_sim_get_sim_status(TcorePending *p,
589 guint data_len, const void *data, void *user_data)
591 const TcoreAtResponse *at_resp = data;
592 CoreObject *co = tcore_pending_ref_core_object(p);
593 ImcRespCbData *resp_cb_data = (ImcRespCbData *)user_data;
596 dbg("SIM Response - SIM status: [+XSIMSTATE]");
598 tcore_check_return_assert(co != NULL);
599 tcore_check_return_assert(resp_cb_data != NULL);
601 if (at_resp && at_resp->success) {
602 if (at_resp->lines) {
603 const gchar *line = NULL;
605 /* Process +XSIMSTATE response */
606 line = (const gchar *) (at_resp->lines->data);
609 guint sim_state, sms_state;
614 * +XSIMSTATE: <mode>,<SIM state>,<PB Ready>,<SMS Ready>
616 tokens = tcore_at_tok_new(line);
618 if (g_slist_length(tokens) == 4) {
620 sim_state = atoi(g_slist_nth_data(tokens, 1));
622 /* Process SIM Status */
623 __imc_sim_process_sim_status(co, sim_state);
626 sms_state = atoi(g_slist_nth_data(tokens, 3));
628 /* Notify SMS status */
629 __imc_sim_notify_sms_state(co, (sms_state > 0));
632 err("Invalid message");
635 tcore_at_tok_free(tokens);
640 /* Free callback data */
641 imc_destroy_resp_cb_data(resp_cb_data);
645 * Operation - get_sim_status
648 * AT-Command: AT+XSIMSTATE?
650 * Response - sim_status
651 * Success: (Single line) -
652 * +XSIMSTATE: <mode>,<SIM state>,<PB Ready>,<SMS Ready>
655 * +CME ERROR: <error>
657 static gboolean __imc_sim_get_sim_status(CoreObject *co,
658 TcoreObjectResponseCallback cb, void *cb_data)
660 ImcRespCbData *resp_cb_data;
663 /* Response callback data */
664 resp_cb_data = imc_create_resp_cb_data(cb, cb_data,
667 /* Send Request to modem */
668 ret = tcore_at_prepare_and_send_request(co,
669 "AT+XSIMSTATE?", "+XSIMSTATE:",
670 TCORE_AT_COMMAND_TYPE_SINGLELINE,
671 TCORE_PENDING_PRIORITY_DEFAULT,
673 __on_response_imc_sim_get_sim_status, resp_cb_data,
674 on_send_imc_request, NULL,
676 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Get SIM Status");
681 static void __imc_sim_next_from_read_binary(CoreObject *co, ImcRespCbData *resp_cb_data, TelSimResult sim_result, gboolean decode_ret)
683 ImcSimMetaInfo *file_meta = (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
684 TelSimCardType card_type = TEL_SIM_CARD_TYPE_UNKNOWN;
688 dbg("[SIM]EF[0x%x] read sim_result[%d] Decode rt[%d]", file_meta->file_id, sim_result, decode_ret);
689 switch (file_meta->file_id) {
691 case TEL_SIM_EF_USIM_PL:
693 case TEL_SIM_EF_USIM_LI:
694 if (decode_ret == TRUE) {
695 if (resp_cb_data->cb)
696 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
698 tcore_sim_get_type(co, &card_type);
700 /* The ME requests the Extended Language Preference. The ME only requests the Language Preference (EFLP) if at least one of the following conditions holds:
701 - EFELP is not available;
702 - EFELP does not contain an entry corresponding to a language specified in ISO 639[30];
703 - the ME does not support any of the languages in EFELP.
706 /* The ME only requests the Language Preference (EFPL) if at least one of the following conditions holds:
707 - if the EFLI has the value 'FFFF' in its highest priority position
708 - if the ME does not support any of the language codes indicated in EFLI , or if EFLI is not present
710 if (TEL_SIM_CARD_TYPE_GSM == card_type) {
711 if (file_meta->file_id == TEL_SIM_EF_LP) {
712 if (resp_cb_data->cb)
713 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
715 file_meta->file_id = TEL_SIM_EF_LP;
716 __imc_sim_get_response(co, resp_cb_data);
718 } else if (TEL_SIM_CARD_TYPE_USIM) {
719 if (file_meta->file_id == TEL_SIM_EF_LP || file_meta->file_id == TEL_SIM_EF_USIM_LI) {
720 file_meta->file_id = TEL_SIM_EF_ELP;
721 __imc_sim_get_response(co, resp_cb_data);
723 if (resp_cb_data->cb)
724 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
731 tcore_sim_get_type(co, &card_type);
732 if (TEL_SIM_CARD_TYPE_USIM == card_type) {
733 if (file_meta->current_index == file_meta->rec_count) {
734 if (resp_cb_data->cb)
735 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
737 file_meta->current_index++;
738 __imc_sim_read_record(co, resp_cb_data);
740 } else if (TEL_SIM_CARD_TYPE_GSM == card_type) {
741 if (resp_cb_data->cb)
742 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
744 dbg("[SIM DATA]Invalid CardType[%d] Unable to handle", card_type);
748 case TEL_SIM_EF_IMSI:
749 if (resp_cb_data->cb) {
750 resp_cb_data->cb(co, (gint)sim_result, &file_meta->imsi, resp_cb_data->cb_data);
752 file_meta->file_id = TEL_SIM_EF_CPHS_CPHS_INFO;
753 file_meta->file_result = TEL_SIM_RESULT_FAILURE;
754 __imc_sim_get_response(co, resp_cb_data);
756 /* Update SIM INIT status - INIT COMPLETE */
757 __imc_sim_update_sim_status(co, TEL_SIM_STATUS_SIM_INIT_COMPLETED);
760 case TEL_SIM_EF_MSISDN:
761 if (file_meta->current_index == file_meta->rec_count) {
763 dbg("rec_count [%d], msisdn_count[%d]", file_meta->rec_count,
764 file_meta->files.data.msisdn_list.count);
765 if (resp_cb_data->cb) {
766 resp_cb_data->cb(co, (gint)sim_result,
767 &file_meta->files.data.msisdn_list, resp_cb_data->cb_data);
771 for (i = 0; i < file_meta->files.data.msisdn_list.count; i++) {
772 tcore_free(file_meta->files.data.msisdn_list.list[i].alpha_id);
773 tcore_free(file_meta->files.data.msisdn_list.list[i].num);
775 tcore_free(file_meta->files.data.msisdn_list.list);
777 file_meta->current_index++;
778 __imc_sim_read_record(co, resp_cb_data);
783 if (file_meta->current_index == file_meta->rec_count) {
784 if (resp_cb_data->cb)
785 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
788 file_meta->current_index++;
789 __imc_sim_read_record(co, resp_cb_data);
794 if (file_meta->current_index == file_meta->rec_count) {
795 if (resp_cb_data->cb)
796 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
798 file_meta->current_index++;
799 __imc_sim_read_record(co, resp_cb_data);
803 case TEL_SIM_EF_USIM_CFIS:
804 case TEL_SIM_EF_USIM_MWIS:
805 case TEL_SIM_EF_USIM_MBI:
806 case TEL_SIM_EF_MBDN:
807 case TEL_SIM_EF_CPHS_MAILBOX_NUMBERS:
808 case TEL_SIM_EF_CPHS_INFORMATION_NUMBERS:
809 if (file_meta->current_index == file_meta->rec_count) {
810 if (resp_cb_data->cb)
811 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
813 file_meta->current_index++;
814 __imc_sim_read_record(co, resp_cb_data);
818 case TEL_SIM_EF_CPHS_OPERATOR_NAME_STRING:
820 ImcSimMetaInfo *file_meta_new = (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
822 file_meta->files.result = sim_result;
823 if (decode_ret == TRUE && sim_result == TEL_SIM_RESULT_SUCCESS) {
824 file_meta_new->files.data.cphs_net.full_name = file_meta->files.data.cphs_net.full_name;
825 dbg("file_meta_new->files.data.cphs_net.full_name[%s]", file_meta_new->files.data.cphs_net.full_name);
828 file_meta_new->file_id = TEL_SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING;
829 file_meta_new->file_result = TEL_SIM_RESULT_FAILURE;
831 __imc_sim_get_response(co, resp_cb_data);
835 case TEL_SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING:
836 if (resp_cb_data->cb)
837 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data.cphs_net, resp_cb_data->cb_data);
839 tcore_free(file_meta->files.data.cphs_net.full_name);
840 tcore_free(file_meta->files.data.cphs_net.short_name);
841 file_meta->files.data.cphs_net.full_name = NULL;
842 file_meta->files.data.cphs_net.short_name = NULL;
845 case TEL_SIM_EF_ICCID:
846 if (resp_cb_data->cb)
847 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data.iccid, resp_cb_data->cb_data);
852 case TEL_SIM_EF_SPDI:
853 case TEL_SIM_EF_OPLMN_ACT:
854 case TEL_SIM_EF_CPHS_CPHS_INFO:
855 case TEL_SIM_EF_CPHS_CALL_FORWARD_FLAGS:
856 case TEL_SIM_EF_CPHS_VOICE_MSG_WAITING:
857 case TEL_SIM_EF_CPHS_DYNAMICFLAGS:
858 case TEL_SIM_EF_CPHS_DYNAMIC2FLAG:
859 case TEL_SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE:
860 case TEL_SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE_LINE2:
861 if (resp_cb_data->cb)
862 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
866 err("File id not handled [0x%x]", file_meta->file_id);
871 static void __imc_sim_next_from_get_response(CoreObject *co, ImcRespCbData *resp_cb_data, TelSimResult sim_result)
873 ImcSimMetaInfo *file_meta = (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
874 TelSimCardType card_type = TEL_SIM_CARD_TYPE_UNKNOWN;
876 dbg("EF[0x%x] access Result[%d]", file_meta->file_id, sim_result);
878 file_meta->files.result = sim_result;
879 if (file_meta->file_id != TEL_SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING)
880 memset(&file_meta->files.data, 0x00, sizeof(file_meta->files.data));
882 if ((file_meta->file_id != TEL_SIM_EF_ELP && file_meta->file_id != TEL_SIM_EF_LP &&
883 file_meta->file_id != TEL_SIM_EF_USIM_PL && file_meta->file_id != TEL_SIM_EF_CPHS_CPHS_INFO)
884 && (sim_result != TEL_SIM_RESULT_SUCCESS)) {
885 if (resp_cb_data->cb)
886 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
890 switch (file_meta->file_id) {
892 if (sim_result == TEL_SIM_RESULT_SUCCESS) {
893 dbg("[SIM DATA] exist EFELP/PL(0x2F05)");
894 __imc_sim_read_binary(co, resp_cb_data);
896 tcore_sim_get_type(co, &card_type);
897 if (TEL_SIM_CARD_TYPE_GSM == card_type) {
898 ImcSimMetaInfo file_meta_new = {0,};
900 dbg("[SIM DATA]SIM_EF_ELP(2F05) access fail. Request SIM_EF_LP(0x6F05) info");
901 /* The ME requests the Language Preference (EFLP) if EFELP is not available */
902 file_meta_new.file_id = TEL_SIM_EF_LP;
903 file_meta_new.file_result = TEL_SIM_RESULT_FAILURE;
904 file_meta_new.req_command = TCORE_COMMAND_SIM_GET_LANGUAGE;
906 memcpy(resp_cb_data->data, &file_meta_new, sizeof(ImcSimMetaInfo));
908 __imc_sim_get_response(co, resp_cb_data);
909 } else if (TEL_SIM_CARD_TYPE_USIM == card_type) {
910 dbg(" [SIM DATA]fail to get Language information in USIM(EF-LI(6F05),EF-PL(2F05))");
911 if (resp_cb_data->cb)
912 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
919 if (sim_result == TEL_SIM_RESULT_SUCCESS) {
920 dbg("[SIM DATA] exist EFLP/LI(0x6F05)");
921 __imc_sim_read_binary(co, resp_cb_data);
923 tcore_sim_get_type(co, &card_type);
924 dbg("[SIM DATA]SIM_EF_LP/LI(6F05) access fail. Current CardType[%d]", card_type);
925 if (TEL_SIM_CARD_TYPE_GSM == card_type) {
926 if (resp_cb_data->cb)
927 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
930 /* if EFLI is not present, then the language selection shall be as defined in EFPL at the MF level */
931 else if (TEL_SIM_CARD_TYPE_USIM == card_type) {
932 ImcSimMetaInfo file_meta_new = {0,};
934 dbg("[SIM DATA] try USIM EFPL(0x2F05)");
935 file_meta_new.file_id = TEL_SIM_EF_ELP;
936 file_meta_new.file_result = TEL_SIM_RESULT_FAILURE;
937 file_meta_new.req_command = TCORE_COMMAND_SIM_GET_LANGUAGE;
939 memcpy(resp_cb_data->data, &file_meta_new, sizeof(ImcSimMetaInfo));
941 __imc_sim_get_response(co, resp_cb_data);
946 case TEL_SIM_EF_USIM_PL:
947 if (sim_result == TEL_SIM_RESULT_SUCCESS) {
948 dbg("[SIM DATA] exist EFELP/PL(0x2F05)");
949 __imc_sim_read_binary(co, resp_cb_data);
951 /* EFELIand EFPL not present, so set language count as zero and select ECC */
953 " [SIM DATA]SIM_EF_USIM_PL(2A05) access fail. Request SIM_EF_ECC(0x6FB7) info");
954 if (resp_cb_data->cb)
955 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
961 tcore_sim_get_type(co, &card_type);
962 if (TEL_SIM_CARD_TYPE_GSM == card_type) {
963 __imc_sim_read_binary(co, resp_cb_data);
964 } else if (TEL_SIM_CARD_TYPE_USIM == card_type) {
965 if (file_meta->rec_count > TEL_SIM_ECC_LIST_MAX) {
966 file_meta->rec_count = TEL_SIM_ECC_LIST_MAX;
968 file_meta->current_index++;
969 __imc_sim_read_record(co, resp_cb_data);
973 case TEL_SIM_EF_ICCID:
974 case TEL_SIM_EF_IMSI:
977 case TEL_SIM_EF_SPDI:
978 case TEL_SIM_EF_CPHS_CALL_FORWARD_FLAGS:
979 case TEL_SIM_EF_CPHS_VOICE_MSG_WAITING:
980 case TEL_SIM_EF_CPHS_OPERATOR_NAME_STRING:
981 case TEL_SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING:
982 case TEL_SIM_EF_CPHS_DYNAMICFLAGS:
983 case TEL_SIM_EF_CPHS_DYNAMIC2FLAG:
984 case TEL_SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE:
985 case TEL_SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE_LINE2:
986 __imc_sim_read_binary(co, resp_cb_data);
989 case TEL_SIM_EF_CPHS_CPHS_INFO:
990 if (sim_result == TEL_SIM_RESULT_SUCCESS) {
991 tcore_sim_set_cphs_status(co, TRUE);
992 __imc_sim_read_binary(co, resp_cb_data);
994 tcore_sim_set_cphs_status(co, FALSE);
995 if (resp_cb_data->cb)
996 resp_cb_data->cb(co, (gint)sim_result, &file_meta->files.data, resp_cb_data->cb_data);
1001 case TEL_SIM_EF_USIM_CFIS:
1002 if (file_meta->rec_count > TEL_SIM_CALL_FORWARDING_TYPE_MAX) {
1003 file_meta->rec_count = TEL_SIM_CALL_FORWARDING_TYPE_MAX;
1005 file_meta->current_index++;
1006 __imc_sim_read_record(co, resp_cb_data);
1009 case TEL_SIM_EF_MSISDN:
1010 file_meta->files.data.msisdn_list.list =
1011 tcore_malloc0(sizeof(TelSimSubscriberInfo) * file_meta->rec_count);
1013 case TEL_SIM_EF_OPL:
1014 case TEL_SIM_EF_PNN:
1015 case TEL_SIM_EF_USIM_MWIS:
1016 case TEL_SIM_EF_USIM_MBI:
1017 case TEL_SIM_EF_MBDN:
1018 case TEL_SIM_EF_CPHS_MAILBOX_NUMBERS:
1019 case TEL_SIM_EF_CPHS_INFORMATION_NUMBERS:
1020 file_meta->current_index++;
1021 __imc_sim_read_record(co, resp_cb_data);
1024 case TEL_SIM_EF_SMSP:
1026 ImcSimPrivateInfo *priv_info = NULL;
1028 priv_info = tcore_sim_ref_userdata(co);
1030 dbg("SMSP info set to tcore : count:[%d], rec_len:[%d]",file_meta->rec_count, file_meta->rec_length);
1031 priv_info->smsp_count = file_meta->rec_count;
1032 priv_info->smsp_rec_len = file_meta->rec_length;
1037 dbg("error - File id for get file info [0x%x]", file_meta->file_id);
1043 static void __on_response_imc_sim_update_file(TcorePending *p, guint data_len, const void *data, void *user_data)
1045 const TcoreAtResponse *resp = data;
1046 CoreObject *co_sim = NULL;
1047 GSList *tokens = NULL;
1048 TelSimResult sim_result = TEL_SIM_RESULT_CARD_ERROR;
1050 ImcRespCbData *resp_cb_data = (ImcRespCbData *) user_data;
1051 ImcSimMetaInfo *file_meta = (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
1055 co_sim = tcore_pending_ref_core_object(p);
1057 dbg("file_id:[0x%x]", file_meta->file_id);
1059 if (resp->success > 0) {
1064 line = (const char *)resp->lines->data;
1065 tokens = tcore_at_tok_new(line);
1066 if (g_slist_length(tokens) != 2) {
1067 err("Invalid message");
1070 sw1 = atoi(g_slist_nth_data(tokens, 0));
1071 sw2 = atoi(g_slist_nth_data(tokens, 1));
1074 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
1075 sim_result = TEL_SIM_RESULT_SUCCESS;
1077 sim_result = __imc_sim_decode_status_word(sw1, sw2);
1080 err("RESPONSE NOK");
1081 sim_result = TEL_SIM_RESULT_FAILURE;
1085 if (resp_cb_data->cb)
1086 resp_cb_data->cb(co_sim, (gint)sim_result, NULL, resp_cb_data->cb_data);
1088 tcore_at_tok_free(tokens);
1092 static void __on_response_imc_sim_read_data(TcorePending *p, guint data_len,
1093 const void *data, void *user_data)
1095 const TcoreAtResponse *resp = data;
1096 CoreObject *co = NULL;
1097 GSList *tokens = NULL;
1098 TelSimResult sim_result;
1099 gboolean dr = FALSE;
1100 const char *line = NULL;
1106 TelSimCardType card_type = TEL_SIM_CARD_TYPE_UNKNOWN;
1107 ImcRespCbData *resp_cb_data = (ImcRespCbData *) user_data;
1108 ImcSimMetaInfo *file_meta = (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
1112 co = tcore_pending_ref_core_object(p);
1114 if (resp->success > 0) {
1117 line = (const char *)resp->lines->data;
1118 tokens = tcore_at_tok_new(line);
1119 if (g_slist_length(tokens) != 3) {
1120 err("Invalid message");
1121 tcore_at_tok_free(tokens);
1125 sw1 = atoi(g_slist_nth_data(tokens, 0));
1126 sw2 = atoi(g_slist_nth_data(tokens, 1));
1127 res = g_slist_nth_data(tokens, 2);
1129 tmp = tcore_at_tok_extract(res);
1130 tcore_util_hexstring_to_bytes(tmp, &res, (guint *)&res_len);
1131 dbg("Response: [%s] Response length: [%d]", res, res_len);
1133 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
1134 sim_result = TEL_SIM_RESULT_SUCCESS;
1135 file_meta->files.result = sim_result;
1137 dbg("File ID: [0x%x]", file_meta->file_id);
1138 switch (file_meta->file_id) {
1139 case TEL_SIM_EF_IMSI: {
1140 dbg("Data: [%s]", res);
1141 dr = tcore_sim_decode_imsi((unsigned char *)res, res_len, &file_meta->imsi);
1143 err("IMSI decoding failed");
1145 __imc_sim_set_identity(co, &file_meta->imsi);
1148 tcore_sim_set_imsi(co, &file_meta->imsi);
1153 case TEL_SIM_EF_ICCID: {
1154 dr = tcore_sim_decode_iccid((unsigned char *)res, res_len,
1155 file_meta->files.data.iccid);
1159 case TEL_SIM_EF_ELP: /* 2G EF - 2 bytes decoding */
1160 case TEL_SIM_EF_USIM_LI: /* 3G EF - 2 bytes decoding */
1161 case TEL_SIM_EF_USIM_PL: /* 3G EF - same as EFELP, so 2 byte decoding */
1162 case TEL_SIM_EF_LP: /* 1 byte encoding */
1164 tcore_sim_get_type(co, &card_type);
1165 if ((TEL_SIM_CARD_TYPE_GSM == card_type)
1166 && (file_meta->file_id == TEL_SIM_EF_LP)) {
1168 * 2G LP(0x6F05) has 1 byte for each language
1170 dr = tcore_sim_decode_lp((unsigned char *)res, res_len, &file_meta->files.data.language);
1173 * 3G LI(0x6F05)/PL(0x2F05),
1174 * 2G ELP(0x2F05) has 2 bytes for each language
1176 dr = tcore_sim_decode_li((unsigned char *)res, res_len,
1177 file_meta->file_id, &file_meta->files.data.language);
1182 case TEL_SIM_EF_SPN:
1183 dr = tcore_sim_decode_spn((unsigned char *)res, res_len, &file_meta->files.data.spn);
1186 case TEL_SIM_EF_SPDI:
1187 dr = tcore_sim_decode_spdi((unsigned char *)res, res_len, &file_meta->files.data.spdi);
1190 case TEL_SIM_EF_SST:
1192 TelSimServiceTable *svct = NULL;
1194 svct = g_try_new0(TelSimServiceTable, 1);
1195 tcore_sim_get_type(co, &card_type);
1196 svct->sim_type = card_type;
1197 if (TEL_SIM_CARD_TYPE_GSM == card_type) {
1198 dr = tcore_sim_decode_sst((unsigned char *)res, res_len, svct->table.sst_service);
1199 } else if (TEL_SIM_CARD_TYPE_USIM == card_type) {
1200 dr = tcore_sim_decode_ust((unsigned char *)res, res_len, svct->table.ust_service);
1202 err("Not handled card_type[%d]", card_type);
1206 err("SST/UST decoding failed");
1208 tcore_sim_set_service_table(co, svct);
1216 case TEL_SIM_EF_ECC:
1218 tcore_sim_get_type(co, &card_type);
1219 if (TEL_SIM_CARD_TYPE_GSM == card_type) {
1220 dr = tcore_sim_decode_ecc((unsigned char *)res, res_len, &file_meta->files.data.ecc);
1221 } else if (TEL_SIM_CARD_TYPE_USIM == card_type) {
1222 TelSimEcc *ecc = NULL;
1224 ecc = g_try_new0(TelSimEcc, 1);
1225 dbg("Index [%d]", file_meta->current_index);
1227 dr = tcore_sim_decode_uecc((unsigned char *)res, res_len, ecc);
1229 memcpy(&file_meta->files.data.ecc.list[file_meta->files.data.ecc.count], ecc, sizeof(TelSimEcc));
1230 file_meta->files.data.ecc.count++;
1236 dbg("Unknown/Unsupported SIM card Type: [%d]", card_type);
1241 case TEL_SIM_EF_MSISDN:
1243 TelSimSubscriberInfo *msisdn = NULL;
1245 dbg("Index [%d]", file_meta->current_index);
1246 msisdn = tcore_malloc0(sizeof(TelSimSubscriberInfo));
1247 dr = tcore_sim_decode_msisdn((unsigned char *)res, res_len, msisdn);
1249 memcpy(&file_meta->files.data.msisdn_list.list[file_meta->files.data.msisdn_list.count],
1250 msisdn, sizeof(TelSimSubscriberInfo));
1252 file_meta->files.data.msisdn_list.count++;
1256 dbg("Freeing resources");
1261 case TEL_SIM_EF_OPL:
1263 TelSimOpl *opl = NULL;
1265 dbg("decode w/ index [%d]", file_meta->current_index);
1266 opl = g_try_new0(TelSimOpl, 1);
1268 dr = tcore_sim_decode_opl((unsigned char *)res, res_len, opl);
1270 memcpy(&file_meta->files.data.opl.list[file_meta->files.data.opl.opl_count],
1271 opl, sizeof(TelSimOpl));
1273 file_meta->files.data.opl.opl_count++;
1281 case TEL_SIM_EF_PNN:
1283 TelSimPnn *pnn = NULL;
1285 dbg("decode w/ index [%d]", file_meta->current_index);
1286 pnn = g_try_new0(TelSimPnn, 1);
1288 dr = tcore_sim_decode_pnn((unsigned char *)res, res_len, pnn);
1290 memcpy(&file_meta->files.data.pnn.list[file_meta->files.data.pnn.pnn_count],
1291 pnn, sizeof(TelSimPnn));
1293 file_meta->files.data.pnn.pnn_count++;
1301 case TEL_SIM_EF_OPLMN_ACT:
1302 /*dr = tcore_sim_decode_oplmnwact(&file_meta->files.data.opwa,
1303 (unsigned char *)res, res_len);*/
1306 case TEL_SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE:
1307 /*dr = tcore_sim_decode_csp(&po->p_cphs->csp,
1308 p_data->response, p_data->response_len);*/
1311 case TEL_SIM_EF_USIM_MBI: /* linear type */
1313 TelSimMbi *mbi = NULL;
1315 mbi = g_try_new0(TelSimMbi, 1);
1316 dr = tcore_sim_decode_mbi((unsigned char *)res, res_len, mbi);
1318 memcpy(&file_meta->mbi_list.list[file_meta->mbi_list.count],
1319 mbi, sizeof(TelSimMbi));
1320 file_meta->mbi_list.count++;
1322 dbg("mbi count[%d]", file_meta->mbi_list.count);
1330 case TEL_SIM_EF_CPHS_MAILBOX_NUMBERS: /* linear type */
1331 case TEL_SIM_EF_MBDN: /* linear type */
1332 dr = tcore_sim_decode_xdn((unsigned char *)res, res_len,
1333 file_meta->mb_list[file_meta->current_index-1].alpha_id,
1334 file_meta->mb_list[file_meta->current_index-1].number);
1335 file_meta->mb_list[file_meta->current_index-1].alpha_id_len = strlen(file_meta->mb_list[file_meta->current_index-1].alpha_id);
1336 file_meta->mb_list[file_meta->current_index-1].profile_id = file_meta->current_index;
1339 case TEL_SIM_EF_CPHS_VOICE_MSG_WAITING: /* transparent type */
1340 dr = tcore_sim_decode_vmwf((unsigned char *)res, res_len, file_meta->files.data.mw.mw);
1343 case TEL_SIM_EF_USIM_MWIS: { /* linear type */
1344 TelSimMwis *mw = NULL;
1346 mw = g_try_new0(TelSimMwis, 1);
1348 dr = tcore_sim_decode_mwis((unsigned char *)res, res_len, mw);
1350 guint count = file_meta->files.data.mw.profile_count;
1352 memcpy(&file_meta->files.data.mw.mw[count], mw, sizeof(TelSimMwis));
1355 * The Profile Identity shall be between 1 and 4 as defined
1356 * in TS 23.097 for MSP
1358 file_meta->files.data.mw.mw[count].profile_id = count+1;
1360 file_meta->files.data.mw.profile_count++;
1368 case TEL_SIM_EF_CPHS_CALL_FORWARD_FLAGS: /* transparent type */
1369 dr = tcore_sim_decode_cff((unsigned char *)res, res_len, file_meta->files.data.mw.mw);
1372 case TEL_SIM_EF_USIM_CFIS: /* linear type */
1374 TelSimCfis *cf = NULL;
1376 cf = g_try_new0(TelSimCfis, 1);
1377 dr = tcore_sim_decode_cfis((unsigned char *)res, res_len, cf);
1379 memcpy(&file_meta->files.data.cf.cf[file_meta->files.data.cf.profile_count],
1380 cf, sizeof(TelSimCfis));
1381 file_meta->files.data.cf.profile_count++;
1389 case TEL_SIM_EF_CPHS_SERVICE_STRING_TABLE:
1390 dbg("not handled - TEL_SIM_EF_CPHS_SERVICE_STRING_TABLE ");
1393 case TEL_SIM_EF_CPHS_OPERATOR_NAME_STRING:
1394 file_meta->files.data.cphs_net.full_name = tcore_malloc0(TEL_SIM_CPHS_OPERATOR_NAME_LEN_MAX+1);
1395 dr = tcore_sim_decode_ons((unsigned char *)res, res_len,
1396 (unsigned char*)file_meta->files.data.cphs_net.full_name);
1397 dbg("file_meta->files.result[%d],file_meta->files.data.cphs_net.full_name[%s]",
1398 file_meta->files.result, file_meta->files.data.cphs_net.full_name);
1401 case TEL_SIM_EF_CPHS_DYNAMICFLAGS:
1402 /*dr = tcore_sim_decode_dynamic_flag(&po->p_cphs->dflagsinfo,
1403 p_data->response, p_data->response_len);*/
1406 case TEL_SIM_EF_CPHS_DYNAMIC2FLAG:
1407 /*dr = tcore_sim_decode_dynamic2_flag(&po->p_cphs->d2flagsinfo, p_data->response,
1408 p_data->response_len);*/
1411 case TEL_SIM_EF_CPHS_CPHS_INFO:
1412 /*dr = tcore_sim_decode_cphs_info(&file_meta->files.data.cphs,
1413 (unsigned char *)res, res_len);*/
1416 case TEL_SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING:
1417 file_meta->files.data.cphs_net.short_name = tcore_malloc0(TEL_SIM_CPHS_OPERATOR_NAME_SHORT_FORM_LEN_MAX+1);
1418 dr = tcore_sim_decode_short_ons((unsigned char *)res, res_len,
1419 (unsigned char*)file_meta->files.data.cphs_net.short_name);
1420 dbg("file_meta->files.result[%d],file_meta->files.data.cphs_net.short_name[%s]",
1421 file_meta->files.result, file_meta->files.data.cphs_net.short_name);
1424 case TEL_SIM_EF_CPHS_INFORMATION_NUMBERS:
1425 /*dr = tcore_sim_decode_information_number(&po->p_cphs->infn, p_data->response, p_data->response_len);*/
1429 dbg("File Decoding Failed - not handled File[0x%x]", file_meta->file_id);
1434 sim_result = __imc_sim_decode_status_word(sw1, sw2);
1435 file_meta->files.result = sim_result;
1443 tcore_at_tok_free(tokens);
1445 err("RESPONSE NOK");
1446 dbg("Error - File ID: [0x%x]", file_meta->file_id);
1447 sim_result = TEL_SIM_RESULT_FAILURE;
1451 __imc_sim_next_from_read_binary(tcore_pending_ref_core_object(p), resp_cb_data, sim_result, dr);
1456 static void __on_response_imc_sim_get_response(TcorePending *p,
1457 guint data_len, const void *data, void *user_data)
1459 const TcoreAtResponse *resp = data;
1460 CoreObject *co = NULL;
1461 TelSimResult sim_result;
1462 GSList *tokens = NULL;
1463 const char *line = NULL;
1466 ImcRespCbData *resp_cb_data = (ImcRespCbData *)user_data;
1467 ImcSimMetaInfo *file_meta =
1468 (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
1470 dbg("SIM Response - SIM File info: [+CRSM]");
1472 co = tcore_pending_ref_core_object(p);
1474 if (resp->success > 0) {
1477 line = (const char *)resp->lines->data;
1478 tokens = tcore_at_tok_new(line);
1479 if (g_slist_length(tokens) < 2) {
1480 err("Invalid message");
1481 tcore_at_tok_free(tokens);
1485 sw1 = atoi(g_slist_nth_data(tokens, 0));
1486 sw2 = atoi(g_slist_nth_data(tokens, 1));
1488 /*1. SIM access success case*/
1489 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
1490 unsigned char tag_len = 0;
1491 unsigned short record_len = 0;
1492 char num_of_records = 0;
1493 unsigned char file_id_len = 0;
1494 unsigned short file_id = 0;
1495 unsigned short file_size = 0;
1496 unsigned short file_type = 0;
1497 unsigned short arr_file_id = 0;
1498 int arr_file_id_rec_num = 0;
1499 TelSimCardType card_type = TEL_SIM_CARD_TYPE_UNKNOWN;
1501 /* handling only last 3 bits */
1502 unsigned char file_type_tag = 0x07;
1503 unsigned char *ptr_data;
1507 char *record_data = NULL;
1508 guint record_data_len;
1509 hexData = g_slist_nth_data(tokens, 2);
1510 dbg("hexData: %s", hexData);
1511 dbg("hexData: %s", hexData + 1);
1513 tmp = tcore_at_tok_extract(hexData);
1514 tcore_util_hexstring_to_bytes(tmp, &record_data, &record_data_len);
1515 tcore_util_hex_dump(" ", record_data_len, record_data);
1518 ptr_data = (unsigned char *)record_data;
1519 tcore_sim_get_type(co, &card_type);
1520 if (TEL_SIM_CARD_TYPE_USIM == card_type) {
1522 ETSI TS 102 221 v7.9.0
1524 '62' FCP template tag
1525 - Response for an EF
1526 '82' M File Descriptor
1527 '83' M File Identifier
1528 'A5' O Proprietary information
1529 '8A' M Life Cycle Status Integer
1530 '8B', '8C' or 'AB' C1 Security attributes
1532 '81' O Total file size
1533 '88' O Short File Identifier (SFI)
1536 /* rsim.res_len has complete data length received */
1538 /* FCP template tag - File Control Parameters tag*/
1539 if (*ptr_data == 0x62) {
1540 /* parse complete FCP tag*/
1541 /* increment to next byte */
1543 tag_len = *ptr_data++;
1544 dbg("tag_len: %02x", tag_len);
1545 /* FCP file descriptor - file type, accessibility, DF, ADF etc*/
1546 if (*ptr_data == 0x82) {
1547 /* increment to next byte */
1551 /* consider only last 3 bits*/
1552 dbg("file_type_tag: %02x", file_type_tag);
1553 file_type_tag = file_type_tag & (*ptr_data);
1554 dbg("file_type_tag: %02x", file_type_tag);
1556 switch (file_type_tag) {
1557 /* increment to next byte */
1560 dbg("Getting FileType: [Transparent file type]");
1561 file_type = IMC_SIM_FILE_TYPE_TRANSPARENT;
1563 /* increment to next byte */
1565 /* increment to next byte */
1570 dbg("Getting FileType: [Linear fixed file type]");
1571 /* increment to next byte */
1573 /* data coding byte - value 21 */
1576 memcpy(&record_len, ptr_data, 2);
1578 IMC_SWAP_BYTES_16(record_len);
1579 ptr_data = ptr_data + 2;
1580 num_of_records = *ptr_data++;
1581 /* Data lossy conversation from enum (int) to unsigned char */
1582 file_type = IMC_SIM_FILE_TYPE_LINEAR_FIXED;
1586 dbg("Cyclic fixed file type");
1587 /* increment to next byte */
1589 /* data coding byte - value 21 */
1592 memcpy(&record_len, ptr_data, 2);
1594 IMC_SWAP_BYTES_16(record_len);
1595 ptr_data = ptr_data + 2;
1596 num_of_records = *ptr_data++;
1597 file_type = IMC_SIM_FILE_TYPE_CYCLIC;
1601 dbg("not handled file type [0x%x]", *ptr_data);
1605 dbg("INVALID FCP received - DEbug!");
1606 tcore_at_tok_free(tokens);
1607 g_free(record_data);
1611 /*File identifier - 0x84,0x85,0x86 etc are currently ignored and not handled */
1612 if (*ptr_data == 0x83) {
1613 /* increment to next byte */
1615 file_id_len = *ptr_data++;
1616 dbg("file_id_len: %02x", file_id_len);
1618 memcpy(&file_id, ptr_data, file_id_len);
1619 dbg("file_id: %x", file_id);
1622 IMC_SWAP_BYTES_16(file_id);
1623 dbg("file_id: %x", file_id);
1625 ptr_data = ptr_data + 2;
1626 dbg("Getting FileID=[0x%x]", file_id);
1628 dbg("INVALID FCP received - DEbug!");
1629 tcore_at_tok_free(tokens);
1630 g_free(record_data);
1634 /* proprietary information */
1635 if (*ptr_data == 0xA5) {
1636 unsigned short prop_len;
1637 /* increment to next byte */
1641 prop_len = *ptr_data;
1642 dbg("prop_len: %02x", prop_len);
1645 ptr_data = ptr_data + prop_len + 1;
1647 dbg("INVALID FCP received - DEbug!");
1650 /* life cycle status integer [8A][length:0x01][status]*/
1653 00000000 : No information given
1654 00000001 : creation state
1655 00000011 : initialization state
1656 000001-1 : operation state -activated
1657 000001-0 : operation state -deactivated
1658 000011-- : Termination state
1659 b8~b5 !=0, b4~b1=X : Proprietary
1660 Any other value : RFU
1662 if (*ptr_data == 0x8A) {
1663 /* increment to next byte */
1665 /* length - value 1 */
1668 switch (*ptr_data) {
1671 dbg("<RX> operation state -deactivated");
1677 dbg("<RX> operation state -activated");
1682 dbg("<RX> DEBUG! LIFE CYCLE STATUS =[0x%x]", *ptr_data);
1688 /* related to security attributes : currently not handled*/
1689 if (*ptr_data == 0x86 || *ptr_data == 0x8B || *ptr_data == 0x8C || *ptr_data == 0xAB) {
1690 /* increment to next byte */
1692 /* if tag length is 3 */
1693 if (*ptr_data == 0x03) {
1694 /* increment to next byte */
1697 memcpy(&arr_file_id, ptr_data, 2);
1699 IMC_SWAP_BYTES_16(arr_file_id);
1700 ptr_data = ptr_data + 2;
1701 arr_file_id_rec_num = *ptr_data++;
1702 dbg("arr_file_id_rec_num:[%d]", arr_file_id_rec_num);
1704 /* if tag length is not 3 */
1705 /* ignoring bytes */
1706 // ptr_data = ptr_data + 4;
1707 dbg("Useless security attributes, so jump to next tag");
1708 ptr_data = ptr_data + (*ptr_data + 1);
1711 dbg("INVALID FCP received[0x%x] - DEbug!", *ptr_data);
1712 tcore_at_tok_free(tokens);
1713 g_free(record_data);
1717 dbg("Current ptr_data value is [%x]", *ptr_data);
1719 /* file size excluding structural info*/
1720 if (*ptr_data == 0x80) {
1721 /* for EF file size is body of file and for Linear or cyclic it is
1722 * number of recXsizeof(one record)
1724 /* increment to next byte */
1726 /* length is 1 byte - value is 2 bytes or more */
1728 memcpy(&file_size, ptr_data, 2);
1730 IMC_SWAP_BYTES_16(file_size);
1731 ptr_data = ptr_data + 2;
1733 dbg("INVALID FCP received - DEbug!");
1734 tcore_at_tok_free(tokens);
1735 g_free(record_data);
1739 /* total file size including structural info*/
1740 if (*ptr_data == 0x81) {
1742 /* increment to next byte */
1746 dbg("len:[%d]", len);
1748 ptr_data = ptr_data + 3;
1750 dbg("INVALID FCP received - DEbug!");
1751 /* 0x81 is optional tag?? check out! so do not return -1 from here! */
1753 /*short file identifier ignored*/
1754 if (*ptr_data == 0x88) {
1755 dbg("0x88: Do Nothing");
1759 dbg("INVALID FCP received - DEbug!");
1760 tcore_at_tok_free(tokens);
1761 g_free(record_data);
1764 } else if (TEL_SIM_CARD_TYPE_GSM == card_type) {
1765 unsigned char gsm_specific_file_data_len = 0;
1766 /* ignore RFU byte1 and byte2 */
1770 // file_size = p_info->response_len;
1771 memcpy(&file_size, ptr_data, 2);
1773 IMC_SWAP_BYTES_16(file_size);
1774 /* parsed file size */
1775 ptr_data = ptr_data + 2;
1777 memcpy(&file_id, ptr_data, 2);
1778 IMC_SWAP_BYTES_16(file_id);
1779 dbg("FILE id --> [%x]", file_id);
1780 ptr_data = ptr_data + 2;
1781 /* save file type - transparent, linear fixed or cyclic */
1782 file_type_tag = (*(ptr_data + 7));
1784 switch (*ptr_data) {
1787 dbg("RFU file type- not handled - Debug!");
1792 dbg("MF file type - not handled - Debug!");
1797 dbg("DF file type - not handled - Debug!");
1802 dbg("EF file type [%d] ", file_type_tag);
1803 /* increment to next byte */
1806 if (file_type_tag == 0x00 || file_type_tag == 0x01) {
1807 /* increament to next byte as this byte is RFU */
1810 (file_type_tag == 0x00) ? IMC_SIM_FILE_TYPE_TRANSPARENT : IMC_SIM_FILE_TYPE_LINEAR_FIXED;
1812 /* increment to next byte */
1814 /* For a cyclic EF all bits except bit 7 are RFU; b7=1 indicates that */
1815 /* the INCREASE command is allowed on the selected cyclic file. */
1816 file_type = IMC_SIM_FILE_TYPE_CYCLIC;
1818 /* bytes 9 to 11 give SIM file access conditions */
1820 /* byte 10 has one nibble that is RF U and another for INCREASE which is not used currently */
1822 /* byte 11 is invalidate and rehabilate nibbles */
1824 /* byte 12 - file status */
1826 /* byte 13 - GSM specific data */
1827 gsm_specific_file_data_len = *ptr_data;
1828 dbg("gsm_specific_file_data_len:[%d]", gsm_specific_file_data_len);
1830 /* byte 14 - structure of EF - transparent or linear or cyclic , already saved above */
1832 /* byte 15 - length of record for linear and cyclic , for transparent it is set to 0x00. */
1833 record_len = *ptr_data;
1834 dbg("record length[%d], file size[%d]", record_len, file_size);
1835 if (record_len != 0)
1836 num_of_records = (file_size / record_len);
1838 dbg("Number of records [%d]", num_of_records);
1842 dbg("not handled file type");
1846 err("Unknown Card Type - [%d]", card_type);
1849 dbg("req ef[0x%x] resp ef[0x%x] size[%ld] Type[0x%x] NumOfRecords[%ld] RecordLen[%ld]",
1850 file_meta->file_id, file_id, file_size, file_type, num_of_records, record_len);
1852 file_meta->file_type = file_type;
1853 file_meta->data_size = file_size;
1854 file_meta->rec_length = record_len;
1855 file_meta->rec_count = num_of_records;
1856 file_meta->current_index = 0; /* reset for new record type EF */
1857 sim_result = TEL_SIM_RESULT_SUCCESS;
1858 g_free(record_data);
1860 /*2. SIM access fail case*/
1861 err("Failed to get ef[0x%x] (file_meta->file_id) ", file_meta->file_id);
1862 sim_result = __imc_sim_decode_status_word(sw1, sw2);
1865 tcore_at_tok_free(tokens);
1867 err("RESPONSE NOK");
1868 err("Failed to get ef[0x%x] (file_meta->file_id) ", file_meta->file_id);
1869 sim_result = TEL_SIM_RESULT_FAILURE;
1872 dbg("Calling __imc_sim_next_from_get_response");
1873 __imc_sim_next_from_get_response(co, resp_cb_data, sim_result);
1877 static TelReturn __imc_sim_update_file(CoreObject *co, ImcRespCbData *resp_cb_data, int cmd, TelSimFileId ef,
1878 int p1, int p2, int p3, char *encoded_data)
1880 char *cmd_str = NULL;
1881 TelReturn ret = TEL_RETURN_FAILURE;
1882 ImcSimMetaInfo *file_meta = (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
1884 dbg("Entry File-id:[0x%02x]", file_meta->file_id);
1886 cmd_str = g_strdup_printf("AT+CRSM=%d,%d,%d,%d,%d,\"%s\"", cmd, ef, p1, p2, p3, encoded_data);
1888 ret = tcore_at_prepare_and_send_request(co, cmd_str, "+CRSM:",
1889 TCORE_AT_COMMAND_TYPE_SINGLELINE,
1890 TCORE_PENDING_PRIORITY_DEFAULT, NULL,
1891 __on_response_imc_sim_update_file, resp_cb_data,
1892 on_send_imc_request, NULL, 0, NULL, NULL);
1894 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Update SIM File");
1896 tcore_free(encoded_data);
1902 static void __imc_sim_read_record(CoreObject *co, ImcRespCbData *resp_cb_data)
1904 gchar *at_cmd = NULL;
1908 ImcSimMetaInfo *file_meta = (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
1909 TelReturn ret = TEL_RETURN_FAILURE;
1911 dbg("Entry File-id:[0x%02x]", file_meta->file_id);
1913 /* According to TS 102 221, values of p1, p2, p3 can be as below:
1914 * 11.1.5 READ RECORD
1916 * P2: Mode, see table 11.11
1919 * Le: Number of bytes to be read (P3)
1922 p1 = (unsigned char) file_meta->current_index;
1923 p2 = (unsigned char) 0x04; /* 0x4 for absolute mode */
1924 p3 = (unsigned char) file_meta->rec_length;
1926 at_cmd = g_strdup_printf("AT+CRSM=%d, %d, %d, %d, %d",
1927 IMC_SIM_ACCESS_READ_RECORD, file_meta->file_id, p1, p2, p3);
1929 ret = tcore_at_prepare_and_send_request(co, at_cmd, "+CRSM:",
1930 TCORE_AT_COMMAND_TYPE_SINGLELINE,
1931 TCORE_PENDING_PRIORITY_DEFAULT, NULL,
1932 __on_response_imc_sim_read_data, resp_cb_data,
1933 on_send_imc_request, NULL, 0, NULL, NULL);
1935 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Get File Record");
1937 dbg("ret:[%d]", ret);
1943 static void __imc_sim_read_binary(CoreObject *co, ImcRespCbData *resp_cb_data)
1945 gchar *at_cmd = NULL;
1950 ImcSimMetaInfo *file_meta = (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
1951 TelReturn ret = TEL_RETURN_FAILURE;
1953 dbg("Entry File-id:[0x%02x]", file_meta->file_id);
1955 /* According to TS 102 221, values of P1, P2, P3 can be as below:
1956 * 11.1.3 READ BINARY
1957 * P1: See table 11.10
1961 * Le: Number of bytes to be read (P3)
1964 p1 = (unsigned char) (offset & 0xFF00) >> 8;
1965 p2 = (unsigned char) offset & 0x00FF; /* offset low */
1966 p3 = (unsigned char) file_meta->data_size;
1968 at_cmd = g_strdup_printf("AT+CRSM=%d, %d, %d, %d, %d",
1969 IMC_SIM_ACCESS_READ_BINARY, file_meta->file_id, p1, p2, p3);
1971 ret = tcore_at_prepare_and_send_request(co, at_cmd, "+CRSM:",
1972 TCORE_AT_COMMAND_TYPE_SINGLELINE,
1973 TCORE_PENDING_PRIORITY_DEFAULT, NULL,
1974 __on_response_imc_sim_read_data, resp_cb_data,
1975 on_send_imc_request, NULL, 0, NULL, NULL);
1977 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Get File Data");
1979 dbg("ret:[%d]", ret);
1985 static TelReturn __imc_sim_get_response(CoreObject *co, ImcRespCbData *resp_cb_data)
1987 gchar *at_cmd = NULL;
1988 ImcSimMetaInfo *file_meta =
1989 (ImcSimMetaInfo *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
1990 TelReturn ret = TEL_RETURN_FAILURE;
1992 dbg("Entry File-id:[0x%02x]", file_meta->file_id);
1994 at_cmd = g_strdup_printf("AT+CRSM=%d, %d",
1995 IMC_SIM_ACCESS_GET_RESPONSE, file_meta->file_id);
1997 ret = tcore_at_prepare_and_send_request(co,
1999 TCORE_AT_COMMAND_TYPE_SINGLELINE,
2000 TCORE_PENDING_PRIORITY_DEFAULT, NULL,
2001 __on_response_imc_sim_get_response, resp_cb_data,
2002 on_send_imc_request, NULL,
2004 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Get File Info");
2011 static void __on_response_imc_sim_get_retry_count(TcorePending *p, guint data_len,
2012 const void *data, void *user_data)
2014 TelSimResult result = TEL_SIM_RESULT_INCORRECT_PASSWORD;
2015 const TcoreAtResponse *at_resp = data;
2016 ImcRespCbData *resp_cb_data = user_data;
2017 CoreObject *co = tcore_pending_ref_core_object(p);
2018 ImcSimCurrSecOp *sec_op = NULL;
2019 GSList *tokens = NULL;
2020 const char *line = NULL;
2022 int attempts_left = 0;
2023 int time_penalty = 0;
2027 tcore_check_return_assert(co != NULL);
2028 tcore_check_return_assert(resp_cb_data != NULL);
2030 sec_op = (ImcSimCurrSecOp *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
2032 if (at_resp && at_resp->success) {
2033 dbg("Sim Get Retry Count [OK]");
2035 if (at_resp->lines) {
2036 line = (const char *)at_resp->lines->data;
2037 tokens = tcore_at_tok_new(line);
2038 if (g_slist_length(tokens) < 3) {
2039 err("Invalid message");
2043 lock_type = atoi(g_slist_nth_data(tokens, 0));
2044 attempts_left = atoi(g_slist_nth_data(tokens, 1));
2045 time_penalty = atoi(g_slist_nth_data(tokens, 2));
2047 dbg("lock_type = %d, attempts_left = %d, time_penalty = %d",
2048 lock_type, attempts_left, time_penalty);
2051 case IMC_SIM_CURR_SEC_OP_PIN1_VERIFY:
2052 case IMC_SIM_CURR_SEC_OP_PIN2_VERIFY:
2054 TelSimSecPinResult verify_pin = {0, };
2056 if (*sec_op == IMC_SIM_CURR_SEC_OP_PIN1_VERIFY)
2057 verify_pin.pin_type = TEL_SIM_PIN_TYPE_PIN1;
2058 else if (*sec_op == IMC_SIM_CURR_SEC_OP_PIN2_VERIFY)
2059 verify_pin.pin_type = TEL_SIM_PIN_TYPE_PIN2;
2061 verify_pin.retry_count = attempts_left;
2063 if(resp_cb_data->cb)
2064 resp_cb_data->cb(co, (gint)result,
2065 &verify_pin, resp_cb_data->cb_data);
2068 case IMC_SIM_CURR_SEC_OP_PUK1_VERIFY:
2069 case IMC_SIM_CURR_SEC_OP_PUK2_VERIFY:
2071 TelSimSecPukResult verify_puk = {0, };
2073 if (*sec_op == IMC_SIM_CURR_SEC_OP_PUK1_VERIFY)
2074 verify_puk.puk_type = TEL_SIM_PUK_TYPE_PUK1;
2075 else if (*sec_op == IMC_SIM_CURR_SEC_OP_PUK2_VERIFY)
2076 verify_puk.puk_type = TEL_SIM_PUK_TYPE_PUK2;
2078 verify_puk.retry_count = attempts_left;
2080 if(resp_cb_data->cb)
2081 resp_cb_data->cb(co, (gint)result,
2082 &verify_puk, resp_cb_data->cb_data);
2085 case IMC_SIM_CURR_SEC_OP_PIN1_CHANGE:
2086 case IMC_SIM_CURR_SEC_OP_PIN2_CHANGE:
2088 TelSimSecPinResult change_pin = {0, };
2090 if (*sec_op == IMC_SIM_CURR_SEC_OP_PIN1_CHANGE)
2091 change_pin.pin_type = TEL_SIM_PIN_TYPE_PIN1;
2092 else if (*sec_op == IMC_SIM_CURR_SEC_OP_PIN2_CHANGE)
2093 change_pin.pin_type = TEL_SIM_PIN_TYPE_PIN2;
2095 change_pin.retry_count = attempts_left;
2097 if(resp_cb_data->cb)
2098 resp_cb_data->cb(co, (gint)result,
2099 &change_pin, resp_cb_data->cb_data);
2102 case IMC_SIM_CURR_SEC_OP_PIN1_DISABLE:
2103 case IMC_SIM_CURR_SEC_OP_PIN2_DISABLE:
2104 case IMC_SIM_CURR_SEC_OP_FDN_DISABLE:
2105 case IMC_SIM_CURR_SEC_OP_SIM_DISABLE:
2106 case IMC_SIM_CURR_SEC_OP_NET_DISABLE:
2107 case IMC_SIM_CURR_SEC_OP_NS_DISABLE:
2108 case IMC_SIM_CURR_SEC_OP_SP_DISABLE:
2109 case IMC_SIM_CURR_SEC_OP_CP_DISABLE:
2111 TelSimFacilityResult disable_facility = {0, };
2114 lock_type = __imc_sim_get_lock_type(*sec_op);
2115 if (lock_type == -1)
2118 disable_facility.type = lock_type;
2119 disable_facility.retry_count = attempts_left;
2121 if(resp_cb_data->cb)
2122 resp_cb_data->cb(co, (gint)result,
2123 &disable_facility, resp_cb_data->cb_data);
2126 case IMC_SIM_CURR_SEC_OP_PIN1_ENABLE:
2127 case IMC_SIM_CURR_SEC_OP_PIN2_ENABLE:
2128 case IMC_SIM_CURR_SEC_OP_FDN_ENABLE:
2129 case IMC_SIM_CURR_SEC_OP_SIM_ENABLE:
2130 case IMC_SIM_CURR_SEC_OP_NET_ENABLE:
2131 case IMC_SIM_CURR_SEC_OP_NS_ENABLE:
2132 case IMC_SIM_CURR_SEC_OP_SP_ENABLE:
2133 case IMC_SIM_CURR_SEC_OP_CP_ENABLE:
2135 TelSimFacilityResult enable_facility = {0, };
2138 lock_type = __imc_sim_get_lock_type(*sec_op);
2139 if (lock_type == -1)
2142 enable_facility.type = lock_type;
2143 enable_facility.retry_count = attempts_left;
2145 if(resp_cb_data->cb)
2146 resp_cb_data->cb(co, (gint)result,
2147 &enable_facility, resp_cb_data->cb_data);
2151 err("Unhandled sec op [%d]", *sec_op);
2155 tcore_at_tok_free(tokens);
2156 imc_destroy_resp_cb_data(resp_cb_data);
2159 err("Sim Get Retry Count [NOK]");
2161 /*TODO - send response for verify pin, puk etc.,
2162 * when get_retry_count fails
2164 tcore_at_tok_free(tokens);
2165 imc_destroy_resp_cb_data(resp_cb_data);
2168 static TelReturn __imc_sim_get_retry_count(CoreObject *co,
2169 ImcRespCbData *resp_cb_data)
2171 TelReturn ret = TEL_RETURN_FAILURE;
2172 ImcSimCurrSecOp *sec_op = (
2173 ImcSimCurrSecOp *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
2175 gchar *cmd_str = NULL;
2180 case IMC_SIM_CURR_SEC_OP_PIN1_VERIFY:
2181 case IMC_SIM_CURR_SEC_OP_PIN1_CHANGE:
2182 case IMC_SIM_CURR_SEC_OP_PIN1_ENABLE:
2183 case IMC_SIM_CURR_SEC_OP_PIN1_DISABLE:
2186 case IMC_SIM_CURR_SEC_OP_PIN2_VERIFY:
2187 case IMC_SIM_CURR_SEC_OP_PIN2_CHANGE:
2188 case IMC_SIM_CURR_SEC_OP_PIN2_ENABLE:
2189 case IMC_SIM_CURR_SEC_OP_PIN2_DISABLE:
2190 case IMC_SIM_CURR_SEC_OP_FDN_ENABLE:
2191 case IMC_SIM_CURR_SEC_OP_FDN_DISABLE:
2194 case IMC_SIM_CURR_SEC_OP_PUK1_VERIFY:
2197 case IMC_SIM_CURR_SEC_OP_PUK2_VERIFY:
2200 case IMC_SIM_CURR_SEC_OP_NET_ENABLE:
2201 case IMC_SIM_CURR_SEC_OP_NET_DISABLE:
2204 case IMC_SIM_CURR_SEC_OP_NS_ENABLE:
2205 case IMC_SIM_CURR_SEC_OP_NS_DISABLE:
2208 case IMC_SIM_CURR_SEC_OP_SP_ENABLE:
2209 case IMC_SIM_CURR_SEC_OP_SP_DISABLE:
2212 case IMC_SIM_CURR_SEC_OP_CP_ENABLE:
2213 case IMC_SIM_CURR_SEC_OP_CP_DISABLE:
2216 case IMC_SIM_CURR_SEC_OP_ADM_VERIFY:
2222 cmd_str = g_strdup_printf("AT+XPINCNT=%d", lock_type);
2224 ret = tcore_at_prepare_and_send_request(co, cmd_str, NULL,
2225 TCORE_AT_COMMAND_TYPE_SINGLELINE,
2226 TCORE_PENDING_PRIORITY_DEFAULT,
2228 __on_response_imc_sim_get_retry_count,
2230 on_send_imc_request,
2231 NULL, 0, NULL, NULL);
2233 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Get Retry Count");
2238 static TelSimLockType __imc_sim_lock_type(int lock_type)
2242 return TEL_SIM_LOCK_SC;
2244 return TEL_SIM_LOCK_FD;
2246 return TEL_SIM_LOCK_PN;
2248 return TEL_SIM_LOCK_PU;
2250 return TEL_SIM_LOCK_PP;
2252 return TEL_SIM_LOCK_PC ;
2254 return TEL_SIM_LOCK_PS ;
2256 err("Invalid lock_type [%d]", lock_type);
2261 static char *__imc_sim_get_fac_from_lock_type(TelSimLockType lock_type,
2262 ImcSimCurrSecOp *sec_op, int flag)
2266 case TEL_SIM_LOCK_PS :
2268 if (flag == ENABLE_FLAG)
2269 *sec_op = IMC_SIM_CURR_SEC_OP_SIM_ENABLE;
2270 else if (flag == DISABLE_FLAG)
2271 *sec_op = IMC_SIM_CURR_SEC_OP_SIM_DISABLE;
2273 *sec_op = IMC_SIM_CURR_SEC_OP_SIM_STATUS;
2275 case TEL_SIM_LOCK_SC :
2277 if (flag == ENABLE_FLAG)
2278 *sec_op = IMC_SIM_CURR_SEC_OP_PIN1_ENABLE;
2279 else if (flag == DISABLE_FLAG)
2280 *sec_op = IMC_SIM_CURR_SEC_OP_PIN1_DISABLE;
2282 *sec_op = IMC_SIM_CURR_SEC_OP_PIN1_STATUS;
2284 case TEL_SIM_LOCK_FD :
2286 if (flag == ENABLE_FLAG)
2287 *sec_op = IMC_SIM_CURR_SEC_OP_FDN_ENABLE;
2288 else if (flag == DISABLE_FLAG)
2289 *sec_op = IMC_SIM_CURR_SEC_OP_FDN_DISABLE;
2291 *sec_op = IMC_SIM_CURR_SEC_OP_FDN_STATUS;
2293 case TEL_SIM_LOCK_PN :
2295 if (flag == ENABLE_FLAG)
2296 *sec_op = IMC_SIM_CURR_SEC_OP_NET_ENABLE;
2297 else if (flag == DISABLE_FLAG)
2298 *sec_op = IMC_SIM_CURR_SEC_OP_NET_DISABLE;
2300 *sec_op = IMC_SIM_CURR_SEC_OP_NET_STATUS;
2302 case TEL_SIM_LOCK_PU :
2304 if (flag == ENABLE_FLAG)
2305 *sec_op = IMC_SIM_CURR_SEC_OP_NS_ENABLE;
2306 else if (flag == DISABLE_FLAG)
2307 *sec_op = IMC_SIM_CURR_SEC_OP_NS_DISABLE;
2309 *sec_op = IMC_SIM_CURR_SEC_OP_NS_STATUS;
2311 case TEL_SIM_LOCK_PP :
2313 if (flag == ENABLE_FLAG)
2314 *sec_op = IMC_SIM_CURR_SEC_OP_SP_ENABLE;
2315 else if (flag == DISABLE_FLAG)
2316 *sec_op = IMC_SIM_CURR_SEC_OP_SP_DISABLE;
2318 *sec_op = IMC_SIM_CURR_SEC_OP_SP_STATUS;
2320 case TEL_SIM_LOCK_PC :
2322 if (flag == ENABLE_FLAG)
2323 *sec_op = IMC_SIM_CURR_SEC_OP_CP_ENABLE;
2324 else if (flag == DISABLE_FLAG)
2325 *sec_op = IMC_SIM_CURR_SEC_OP_CP_DISABLE;
2327 *sec_op = IMC_SIM_CURR_SEC_OP_CP_STATUS;
2330 err("Unhandled sim lock type [%d]", lock_type);
2335 static int __imc_sim_get_lock_type(ImcSimCurrSecOp sec_op)
2338 case IMC_SIM_CURR_SEC_OP_SIM_DISABLE :
2339 case IMC_SIM_CURR_SEC_OP_SIM_ENABLE :
2340 case IMC_SIM_CURR_SEC_OP_SIM_STATUS :
2341 return TEL_SIM_LOCK_PS;
2342 case IMC_SIM_CURR_SEC_OP_PIN1_DISABLE :
2343 case IMC_SIM_CURR_SEC_OP_PIN1_ENABLE :
2344 case IMC_SIM_CURR_SEC_OP_PIN1_STATUS :
2345 return TEL_SIM_LOCK_SC;
2346 case IMC_SIM_CURR_SEC_OP_FDN_DISABLE :
2347 case IMC_SIM_CURR_SEC_OP_FDN_ENABLE :
2348 case IMC_SIM_CURR_SEC_OP_FDN_STATUS :
2349 return TEL_SIM_LOCK_FD;
2350 case IMC_SIM_CURR_SEC_OP_NET_DISABLE :
2351 case IMC_SIM_CURR_SEC_OP_NET_ENABLE :
2352 case IMC_SIM_CURR_SEC_OP_NET_STATUS :
2353 return TEL_SIM_LOCK_PN;
2354 case IMC_SIM_CURR_SEC_OP_NS_DISABLE :
2355 case IMC_SIM_CURR_SEC_OP_NS_ENABLE :
2356 case IMC_SIM_CURR_SEC_OP_NS_STATUS :
2357 return TEL_SIM_LOCK_PU;
2358 case IMC_SIM_CURR_SEC_OP_SP_DISABLE :
2359 case IMC_SIM_CURR_SEC_OP_SP_ENABLE :
2360 case IMC_SIM_CURR_SEC_OP_SP_STATUS :
2361 return TEL_SIM_LOCK_PP;
2362 case IMC_SIM_CURR_SEC_OP_CP_DISABLE :
2363 case IMC_SIM_CURR_SEC_OP_CP_ENABLE :
2364 case IMC_SIM_CURR_SEC_OP_CP_STATUS :
2365 return TEL_SIM_LOCK_PC ;
2367 err("Invalid sec op [%d]", sec_op);
2374 * Notification: +XSIM: <SIM state>
2376 * Possible values of <SIM state> can be
2378 * 1 PIN verification needed
2379 * 2 PIN verification not needed - Ready
2380 * 3 PIN verified - Ready
2381 * 4 PUK verification needed
2382 * 5 SIM permanently blocked
2384 * 7 ready for attach (+COPS)
2385 * 8 SIM Technical Problem
2387 * 10 SIM Reactivating
2388 * 11 SIM Reactivated
2389 * 12 SIM SMS Caching Completed. (Sent only when SMS caching enabled)
2390 * 99 SIM State Unknown
2392 static gboolean on_notification_imc_sim_status(CoreObject *co,
2393 const void *event_info, void *user_data)
2395 GSList *lines = (GSList *)event_info;
2398 dbg("SIM notification - SIM status: [+XSIM]");
2400 if (g_slist_length(lines) != 1) {
2401 err("+XSIM unsolicited message expected to be "
2402 "Single line but received multiple lines");
2406 line = (const gchar *) (lines->data);
2414 * +XSIM: <SIM state>
2416 tokens = tcore_at_tok_new(line);
2417 if (g_slist_length(tokens) == 1) {
2419 sim_state = atoi(g_slist_nth_data(tokens, 0));
2421 /* Process SIM Status */
2422 __imc_sim_process_sim_status(co, sim_state);
2424 err("Invalid message");
2427 tcore_at_tok_free(tokens);
2434 static TcoreHookReturn on_hook_imc_modem_power(TcorePlugin *source,
2435 TcoreNotification command, guint data_len, void *data, void *user_data)
2437 CoreObject *co = (CoreObject *)user_data;
2439 tcore_check_return_value(co != NULL, TCORE_HOOK_RETURN_CONTINUE);
2441 dbg("Get SIM status");
2442 (void)__imc_sim_get_sim_status(co, NULL, NULL);
2444 return TCORE_HOOK_RETURN_CONTINUE;
2447 /* Response Functions */
2448 static void on_response_imc_sim_req_authentication(TcorePending *p, guint data_len,
2449 const void *data, void *user_data)
2451 const TcoreAtResponse *at_resp = data;
2452 GSList *tokens = NULL;
2453 CoreObject *co = tcore_pending_ref_core_object(p);
2454 TelSimAuthenticationResponse auth_resp = {0, };
2455 TelSimResult sim_result = TEL_SIM_RESULT_FAILURE;
2456 ImcRespCbData *resp_cb_data = user_data;
2457 TelSimAuthenticationType *auth_type = (TelSimAuthenticationType *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
2461 if (NULL == at_resp) {
2462 err("at_resp is NULL");
2463 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2467 auth_resp.auth_type = *auth_type;
2469 if (at_resp->success == TRUE) {
2474 if (at_resp->lines != NULL) {
2475 line = at_resp->lines->data;
2476 dbg("Received data: [%s]", line);
2478 err("at_resp->lines is NULL");
2479 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2483 tokens = tcore_at_tok_new(line);
2484 if (tokens == NULL) {
2485 err("tokens is NULL");
2486 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2490 status = atoi(g_slist_nth_data(tokens, 0));
2493 dbg("Authentications successful");
2494 auth_resp.detailed_result = TEL_SIM_AUTH_NO_ERROR;
2497 err("Synchronize fail");
2498 auth_resp.detailed_result = TEL_SIM_AUTH_SYNCH_FAILURE;
2502 auth_resp.detailed_result = TEL_SIM_AUTH_MAK_CODE_FAILURE;
2505 err("Does not support security context");
2506 auth_resp.detailed_result = TEL_SIM_AUTH_UNSUPPORTED_CONTEXT;
2509 err("Other failure");
2510 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2514 if (auth_resp.auth_type == TEL_SIM_AUTH_GSM) {
2516 char *convert_kc, *convert_sres;
2518 kc = g_slist_nth_data(tokens, 1);
2520 guint convert_kc_len = 0;
2521 kc = tcore_at_tok_extract(kc);
2522 dbg("Kc: [%s]", kc);
2524 tcore_util_hexstring_to_bytes(kc, &convert_kc, &convert_kc_len);
2525 if (convert_kc_len && convert_kc_len <= TEL_SIM_AUTH_MAX_RESP_DATA_LEN) {
2526 auth_resp.authentication_key_length = convert_kc_len;
2527 memcpy(&auth_resp.authentication_key, convert_kc, convert_kc_len);
2530 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2536 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2540 sres = g_slist_nth_data(tokens, 2);
2542 guint convert_sres_len = 0;
2543 sres = tcore_at_tok_extract(sres);
2544 dbg("SRES: [%s]", sres);
2546 tcore_util_hexstring_to_bytes(sres, &convert_sres, &convert_sres_len);
2547 if (convert_sres_len && convert_sres_len <= TEL_SIM_AUTH_MAX_RESP_DATA_LEN) {
2548 auth_resp.resp_length = convert_sres_len;
2549 memcpy(&auth_resp.resp_data, convert_sres, convert_sres_len);
2551 err("Invalid SRES");
2552 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2555 g_free(convert_sres);
2557 err("Invalid SRES");
2558 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2561 } else if (auth_resp.auth_type == TEL_SIM_AUTH_3G_CTX) {
2562 char *res, *ck, *ik, *kc;
2563 char *convert_res, *convert_ck;
2564 char *convert_ik, *convert_kc;
2566 res = g_slist_nth_data(tokens, 1);
2568 guint convert_res_len = 0;
2569 res = tcore_at_tok_extract(res);
2570 dbg("RES/AUTS: [%s]", res);
2572 tcore_util_hexstring_to_bytes(res, &convert_res, &convert_res_len);
2573 if (convert_res_len && convert_res_len <= TEL_SIM_AUTH_MAX_RESP_DATA_LEN) {
2574 auth_resp.resp_length = convert_res_len;
2575 memcpy(auth_resp.resp_data, convert_res, convert_res_len);
2577 err("Invalid RES/AUTS");
2578 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2581 g_free(convert_res);
2583 err("Invalid RES/AUTS");
2584 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2588 ck = g_slist_nth_data(tokens, 2);
2590 guint convert_ck_len = 0;
2591 ck = tcore_at_tok_extract(ck);
2592 dbg("CK: [%s]", ck);
2594 tcore_util_hexstring_to_bytes(ck, &convert_ck, &convert_ck_len);
2595 if (convert_ck_len && convert_ck_len <= TEL_SIM_AUTH_MAX_RESP_DATA_LEN) {
2596 auth_resp.cipher_length = convert_ck_len;
2597 memcpy(&auth_resp.cipher_data, convert_ck, convert_ck_len);
2600 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2606 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2610 ik = g_slist_nth_data(tokens, 3);
2612 guint convert_ik_len = 0;
2613 ik = tcore_at_tok_extract(ik);
2614 dbg("IK: [%s]", ik);
2616 tcore_util_hexstring_to_bytes(ik, &convert_ik, &convert_ik_len);
2617 if (convert_ik_len && convert_ik_len <= TEL_SIM_AUTH_MAX_RESP_DATA_LEN) {
2618 auth_resp.integrity_length = convert_ik_len;
2619 memcpy(&auth_resp.integrity_data, convert_ik, convert_ik_len);
2622 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2628 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2632 kc = g_slist_nth_data(tokens, 4);
2634 guint convert_kc_len = 0;
2635 kc = tcore_at_tok_extract(kc);
2636 dbg("Kc: [%s]", kc);
2638 tcore_util_hexstring_to_bytes(kc, &convert_kc, &convert_kc_len);
2639 if (convert_kc_len && convert_kc_len <= TEL_SIM_AUTH_MAX_RESP_DATA_LEN) {
2640 auth_resp.authentication_key_length = convert_kc_len;
2641 memcpy(&auth_resp.authentication_key, convert_kc, convert_kc_len);
2644 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2650 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2654 err("Not supported");
2655 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2658 sim_result = TEL_SIM_RESULT_SUCCESS;
2660 err("RESPONSE NOK");
2661 auth_resp.detailed_result = TEL_SIM_AUTH_CANNOT_PERFORM;
2665 if(resp_cb_data->cb)
2666 resp_cb_data->cb(co, (gint)sim_result, &auth_resp, resp_cb_data->cb_data);
2668 tcore_at_tok_free(tokens);
2671 static void on_response_imc_sim_verify_pins(TcorePending *p, guint data_len,
2672 const void *data, void *user_data)
2674 const TcoreAtResponse *at_resp = data;
2675 ImcRespCbData *resp_cb_data = user_data;
2676 CoreObject *co = tcore_pending_ref_core_object(p);
2677 TelSimResult result = TEL_SIM_RESULT_FAILURE;
2678 ImcSimCurrSecOp *sec_op = NULL;
2679 TelSimSecPinResult verify_pin_resp = {0, };
2683 tcore_check_return_assert(co != NULL);
2684 tcore_check_return_assert(resp_cb_data != NULL);
2686 sec_op = (ImcSimCurrSecOp *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
2688 if (at_resp && at_resp->success) {
2689 dbg("Sim Verify Pin Response- [OK]");
2691 result = TEL_SIM_RESULT_SUCCESS;
2693 if (*sec_op == IMC_SIM_CURR_SEC_OP_PIN1_VERIFY) {
2694 TelSimCardStatus status;
2696 verify_pin_resp.pin_type = TEL_SIM_PIN_TYPE_PIN1;
2698 tcore_sim_get_status(co, &status);
2699 if (status != TEL_SIM_STATUS_SIM_INIT_COMPLETED) {
2700 /*Update sim status*/
2701 __imc_sim_update_sim_status(co,
2702 TEL_SIM_STATUS_SIM_INITIALIZING);
2704 } else if (*sec_op == IMC_SIM_CURR_SEC_OP_PIN2_VERIFY) {
2705 verify_pin_resp.pin_type = TEL_SIM_PIN_TYPE_PIN2;
2709 if(resp_cb_data->cb)
2710 resp_cb_data->cb(co, (gint)result,
2712 resp_cb_data->cb_data);
2713 imc_destroy_resp_cb_data(resp_cb_data);
2715 err("Sim Verify Pin Response- [NOK]");
2716 /* Get retry count */
2717 __imc_sim_get_retry_count(co, resp_cb_data);
2721 static void on_response_imc_sim_verify_puks(TcorePending *p, guint data_len,
2722 const void *data, void *user_data)
2724 const TcoreAtResponse *at_resp = data;
2725 ImcRespCbData *resp_cb_data = user_data;
2726 CoreObject *co = tcore_pending_ref_core_object(p);
2727 TelSimResult result = TEL_SIM_RESULT_FAILURE;
2728 ImcSimCurrSecOp *sec_op = NULL;
2729 TelSimSecPukResult verify_puk_resp = {0, };
2733 tcore_check_return_assert(co != NULL);
2734 tcore_check_return_assert(resp_cb_data != NULL);
2736 sec_op = (ImcSimCurrSecOp *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
2738 if (at_resp && at_resp->success) {
2739 dbg("Sim Verify Puk Response- [OK]");
2741 result = TEL_SIM_RESULT_SUCCESS;
2743 if (*sec_op == IMC_SIM_CURR_SEC_OP_PUK1_VERIFY) {
2744 verify_puk_resp.puk_type = TEL_SIM_PUK_TYPE_PUK1;
2745 } else if (*sec_op == IMC_SIM_CURR_SEC_OP_PUK2_VERIFY) {
2746 verify_puk_resp.puk_type = TEL_SIM_PUK_TYPE_PUK2;
2749 if(resp_cb_data->cb)
2750 resp_cb_data->cb(co, (gint)result,
2752 resp_cb_data->cb_data);
2753 imc_destroy_resp_cb_data(resp_cb_data);
2755 err("Sim Verify Puk Response- [NOK]");
2756 /* Get retry count */
2757 __imc_sim_get_retry_count(co, resp_cb_data);
2761 static void on_response_imc_sim_change_pins(TcorePending *p, guint data_len,
2762 const void *data, void *user_data)
2764 const TcoreAtResponse *at_resp = data;
2765 ImcRespCbData *resp_cb_data = user_data;
2766 CoreObject *co = tcore_pending_ref_core_object(p);
2767 TelSimResult result = TEL_SIM_RESULT_FAILURE;
2768 ImcSimCurrSecOp *sec_op = NULL;
2769 TelSimSecPinResult change_pin_resp = {0, };
2773 tcore_check_return_assert(co != NULL);
2774 tcore_check_return_assert(resp_cb_data != NULL);
2776 sec_op = (ImcSimCurrSecOp *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
2778 if (at_resp && at_resp->success) {
2779 dbg("Sim Change Pin Response- [OK]");
2781 result = TEL_SIM_RESULT_SUCCESS;
2783 if (*sec_op == IMC_SIM_CURR_SEC_OP_PIN1_CHANGE) {
2784 change_pin_resp.pin_type = TEL_SIM_PIN_TYPE_PIN1;
2785 } else if (*sec_op == IMC_SIM_CURR_SEC_OP_PIN2_CHANGE) {
2786 change_pin_resp.pin_type = TEL_SIM_PIN_TYPE_PIN2;
2789 if(resp_cb_data->cb)
2790 resp_cb_data->cb(co, (gint)result,
2792 resp_cb_data->cb_data);
2793 imc_destroy_resp_cb_data(resp_cb_data);
2795 err("Sim Change Pin Response- [NOK]");
2796 /* Get retry count */
2797 __imc_sim_get_retry_count(co, resp_cb_data);
2801 static void on_response_imc_sim_disable_facility(TcorePending *p, guint data_len,
2802 const void *data, void *user_data)
2804 const TcoreAtResponse *at_resp = data;
2805 ImcRespCbData *resp_cb_data = user_data;
2806 CoreObject *co = tcore_pending_ref_core_object(p);
2807 TelSimResult result = TEL_SIM_RESULT_FAILURE;
2808 ImcSimCurrSecOp *sec_op = NULL;
2809 TelSimFacilityResult disable_facility_resp = {0, };
2813 tcore_check_return_assert(co != NULL);
2814 tcore_check_return_assert(resp_cb_data != NULL);
2816 sec_op = (ImcSimCurrSecOp *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
2818 if (at_resp && at_resp->success) {
2820 dbg("Sim Disable Facility Response- [OK]");
2822 lock_type = __imc_sim_get_lock_type(*sec_op);
2823 if (lock_type == -1) {
2824 result = TEL_SIM_RESULT_INVALID_PARAMETER;
2827 if(resp_cb_data->cb)
2828 resp_cb_data->cb(co, (gint)result,
2830 resp_cb_data->cb_data);
2831 imc_destroy_resp_cb_data(resp_cb_data);
2835 disable_facility_resp.type = lock_type;
2836 result = TEL_SIM_RESULT_SUCCESS;
2839 if(resp_cb_data->cb)
2840 resp_cb_data->cb(co, (gint)result,
2841 &disable_facility_resp,
2842 resp_cb_data->cb_data);
2843 imc_destroy_resp_cb_data(resp_cb_data);
2845 err("Sim Disable Facility Response- [NOK]");
2846 /* Get retry count */
2847 __imc_sim_get_retry_count(co, resp_cb_data);
2851 static void on_response_imc_sim_enable_facility(TcorePending *p, guint data_len,
2852 const void *data, void *user_data)
2854 const TcoreAtResponse *at_resp = data;
2855 ImcRespCbData *resp_cb_data = user_data;
2856 CoreObject *co = tcore_pending_ref_core_object(p);
2857 TelSimResult result = TEL_SIM_RESULT_FAILURE;
2858 ImcSimCurrSecOp *sec_op = NULL;
2859 TelSimFacilityResult enable_facility_resp = {0, };
2863 tcore_check_return_assert(co != NULL);
2864 tcore_check_return_assert(resp_cb_data != NULL);
2866 sec_op = (ImcSimCurrSecOp *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
2868 if (at_resp && at_resp->success) {
2870 dbg("Sim Enable Facility Response- [OK]");
2872 lock_type = __imc_sim_get_lock_type(*sec_op);
2873 if (lock_type == -1) {
2874 result = TEL_SIM_RESULT_INVALID_PARAMETER;
2877 if(resp_cb_data->cb)
2878 resp_cb_data->cb(co, (gint)result,
2880 resp_cb_data->cb_data);
2881 imc_destroy_resp_cb_data(resp_cb_data);
2885 enable_facility_resp.type = lock_type;
2886 result = TEL_SIM_RESULT_SUCCESS;
2889 if(resp_cb_data->cb)
2890 resp_cb_data->cb(co, (gint)result,
2891 &enable_facility_resp,
2892 resp_cb_data->cb_data);
2893 imc_destroy_resp_cb_data(resp_cb_data);
2895 err("Sim Enable Facility Response- [NOK]");
2896 /* Get retry count */
2897 __imc_sim_get_retry_count(co, resp_cb_data);
2901 static void on_response_imc_sim_get_facility(TcorePending *p, guint data_len,
2902 const void *data, void *user_data)
2904 const TcoreAtResponse *at_resp = data;
2905 ImcRespCbData *resp_cb_data = user_data;
2906 CoreObject *co = tcore_pending_ref_core_object(p);
2907 TelSimResult result = TEL_SIM_RESULT_FAILURE;
2908 ImcSimCurrSecOp *sec_op = NULL;
2909 TelSimFacilityInfo get_facility_resp = {0, };
2913 tcore_check_return_assert(co != NULL);
2914 tcore_check_return_assert(resp_cb_data != NULL);
2916 sec_op = (ImcSimCurrSecOp *)IMC_GET_DATA_FROM_RESP_CB_DATA(resp_cb_data);
2918 if (at_resp && at_resp->success) {
2919 GSList *tokens = NULL;
2923 dbg("Sim Get Facility Response- [OK]");
2925 lock_type = __imc_sim_get_lock_type(*sec_op);
2926 if (lock_type == -1) {
2927 result = TEL_SIM_RESULT_INVALID_PARAMETER;
2930 if (at_resp->lines) {
2931 line = (const char *)at_resp->lines->data;
2932 tokens = tcore_at_tok_new(line);
2933 if (g_slist_length(tokens) != 1) {
2934 err("Invalid message");
2935 tcore_at_tok_free(tokens);
2938 get_facility_resp.f_status = atoi(g_slist_nth_data(tokens, 0));
2939 get_facility_resp.type = lock_type;
2940 result = TEL_SIM_RESULT_SUCCESS;
2943 tcore_at_tok_free(tokens);
2945 err("Sim Get Facility Response- [NOK]");
2948 /* Invoke callback */
2949 if(resp_cb_data->cb)
2950 resp_cb_data->cb(co, (gint)result, &get_facility_resp, resp_cb_data->cb_data);
2951 imc_destroy_resp_cb_data(resp_cb_data);
2954 static void on_response_imc_sim_get_lock_info(TcorePending *p, guint data_len,
2955 const void *data, void *user_data)
2957 const TcoreAtResponse *at_resp = data;
2958 ImcRespCbData *resp_cb_data = user_data;
2959 CoreObject *co = tcore_pending_ref_core_object(p);
2960 TelSimResult result = TEL_SIM_RESULT_FAILURE;
2961 TelSimLockInfo get_lock_info_resp = {0, };
2965 tcore_check_return_assert(co != NULL);
2966 tcore_check_return_assert(resp_cb_data != NULL);
2968 if(at_resp && at_resp->success) {
2969 GSList *tokens = NULL;
2972 int attempts_left = 0;
2973 int time_penalty = 0;
2975 dbg("Sim Get Lock Info Response- [OK]");
2977 if (at_resp->lines) {
2978 line = (const char *)at_resp->lines->data;
2979 tokens = tcore_at_tok_new(line);
2980 if (g_slist_length(tokens) < 3) {
2981 err("Invalid message");
2982 tcore_at_tok_free(tokens);
2986 lock_type = atoi(g_slist_nth_data(tokens, 0));
2987 attempts_left = atoi(g_slist_nth_data(tokens, 1));
2988 time_penalty = atoi(g_slist_nth_data(tokens, 2));
2990 dbg("lock_type = %d, attempts_left = %d, time_penalty = %d",
2991 lock_type, attempts_left, time_penalty);
2993 get_lock_info_resp.lock_type = __imc_sim_lock_type(lock_type);
2994 get_lock_info_resp.retry_count = attempts_left;
2995 result = TEL_SIM_RESULT_SUCCESS;
2997 tcore_at_tok_free(tokens);
2999 err("Sim Get Lock Info Response- [NOK]");
3002 /* Invoke callback */
3003 if(resp_cb_data->cb)
3004 resp_cb_data->cb(co, (gint)result, &get_lock_info_resp, resp_cb_data->cb_data);
3005 imc_destroy_resp_cb_data(resp_cb_data);
3008 static void on_response_imc_sim_req_apdu (TcorePending *p, guint data_len, const void *data, void *user_data)
3010 const TcoreAtResponse *resp = data;
3011 CoreObject *co = NULL;
3012 TelSimApduResp apdu_resp = {0,};
3013 TelSimResult sim_result = TEL_SIM_RESULT_FAILURE;
3014 GSList *tokens = NULL;
3016 ImcRespCbData *resp_cb_data = (ImcRespCbData *) user_data;
3020 co = tcore_pending_ref_core_object(p);
3022 if (resp->success > 0) {
3026 char *decoded_data = NULL;
3027 guint decoded_data_len = 0;
3028 line = (const char *)resp->lines->data;
3029 tokens = tcore_at_tok_new(line);
3030 if (g_slist_length(tokens) != 2) {
3031 err("Invalid message");
3035 tmp = tcore_at_tok_extract(g_slist_nth_data(tokens, 1));
3036 tcore_util_hexstring_to_bytes(tmp, &decoded_data, &decoded_data_len);
3038 apdu_resp.apdu_resp_len = decoded_data_len;
3039 memcpy((char *)apdu_resp.apdu_resp, decoded_data, decoded_data_len);
3041 g_free(decoded_data);
3042 sim_result = TEL_SIM_RESULT_SUCCESS;
3045 err("RESPONSE NOK");
3050 if (resp_cb_data->cb)
3051 resp_cb_data->cb(co, (gint)sim_result, &apdu_resp, resp_cb_data->cb_data);
3052 tcore_at_tok_free(tokens);
3056 static void on_response_imc_sim_req_atr(TcorePending *p, guint data_len, const void *data, void *user_data)
3058 const TcoreAtResponse *resp = data;
3059 CoreObject *co = NULL;
3060 TelSimAtr atr_res = {0,};
3061 TelSimResult sim_result = TEL_SIM_RESULT_FAILURE;
3062 GSList *tokens = NULL;
3064 ImcRespCbData *resp_cb_data = (ImcRespCbData *) user_data;
3068 co = tcore_pending_ref_core_object(p);
3070 if (resp->success > 0) {
3074 char *decoded_data = NULL;
3075 guint decoded_data_len = 0;
3076 line = (const char *)resp->lines->data;
3077 tokens = tcore_at_tok_new(line);
3078 if (g_slist_length(tokens) < 1) {
3079 err("Invalid message");
3083 tmp = tcore_at_tok_extract(g_slist_nth_data(tokens, 0));
3084 tcore_util_hexstring_to_bytes(tmp, &decoded_data, &decoded_data_len);
3086 atr_res.atr_len = decoded_data_len;
3087 memcpy((char *)atr_res.atr, decoded_data, decoded_data_len);
3089 g_free(decoded_data);
3090 sim_result = TEL_SIM_RESULT_SUCCESS;
3093 err("RESPONSE NOK");
3098 if (resp_cb_data->cb)
3099 resp_cb_data->cb(co, (gint)sim_result, &atr_res, resp_cb_data->cb_data);
3100 tcore_at_tok_free(tokens);
3104 /* SIM Operations */
3106 * Operation - get_imsi
3109 * AT-Command: AT+CRSM= <command>[,<fileid>[,<P1>,<P2>,<P3>[,<data>[,<pathid>]]]]
3120 * 28423 meaning IMSI file (6F07)
3121 * 28473 meaning ACM file (6F39)
3122 * 28481 meaning PUKT file (6F41)
3123 * 28482 meaning SMS file (6F42)
3126 * Integer type defining the request.
3127 * These parameters are mandatory for every command, except GET RESPONSE and STATUS.
3130 * Information which shall be written to the SIM
3133 * String type, contains the path of an elementary file on the SIM/USIM in hexadecimal format
3141 * +CRSM: <sw1>,<sw2>[,<response>]
3144 * Integer type containing the SIM information
3147 * Response of successful completion of the command previously issued
3150 * +CME ERROR: <error>
3152 static TelReturn imc_sim_get_imsi (CoreObject *co,
3153 TcoreObjectResponseCallback cb, void *cb_data)
3158 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_IMSI, ret);
3163 static TelReturn imc_sim_get_ecc (CoreObject *co,
3164 TcoreObjectResponseCallback cb, void *cb_data)
3169 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_ECC, ret);
3174 static TelReturn imc_sim_get_iccid (CoreObject *co,
3175 TcoreObjectResponseCallback cb, void *cb_data)
3180 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_ICCID, ret);
3185 static TelReturn imc_sim_get_language (CoreObject *co,
3186 TcoreObjectResponseCallback cb, void *cb_data)
3191 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_LP, ret);
3196 static TelReturn imc_sim_set_language (CoreObject *co,
3197 TelSimLanguagePreferenceCode language,
3198 TcoreObjectResponseCallback cb, void *cb_data)
3200 ImcSimMetaInfo file_meta = {0, };
3201 TelSimCardType card_type = TEL_SIM_CARD_TYPE_UNKNOWN;
3202 ImcRespCbData *resp_cb_data = NULL;
3205 char *encoded_data = NULL;
3206 int encoded_data_len = 0;
3213 file_meta.file_id = TEL_SIM_EF_LP;
3214 file_meta.file_result = TEL_SIM_RESULT_FAILURE;
3216 tcore_sim_get_type(co, &card_type);
3218 dbg("language[%d], card_type[%d]", language, card_type);
3220 if (TEL_SIM_CARD_TYPE_GSM == card_type) {
3222 tcore_sim_encode_lp(language, &tmp, &tmp_len);
3223 } else if (TEL_SIM_CARD_TYPE_USIM == card_type) {
3225 tcore_sim_encode_li(language, &tmp, &tmp_len);
3227 err("Invalid card_type:[%d]", card_type);
3228 return TEL_RETURN_OPERATION_NOT_SUPPORTED;
3231 err("Failed to encode Language [%d]", language);
3232 return TEL_RETURN_FAILURE;
3234 dbg("Encoded Language [%s]", tmp);
3236 encoded_data_len = 2 * tmp_len;
3237 encoded_data = (char *)tcore_malloc0(encoded_data_len + 1);
3238 tcore_util_byte_to_hex(tmp, encoded_data, tmp_len);
3243 p3 = encoded_data_len;
3244 dbg("encoded_data - [%s], encoded_data_len - %d", encoded_data, encoded_data_len);
3246 resp_cb_data = imc_create_resp_cb_data(cb, cb_data, &file_meta, sizeof(ImcSimMetaInfo));
3248 return __imc_sim_update_file(co, resp_cb_data, IMC_SIM_ACCESS_UPDATE_BINARY,
3249 TEL_SIM_EF_LP, p1, p2, p3, encoded_data);
3252 static TelReturn imc_sim_get_callforwarding_info (CoreObject *co,
3253 TcoreObjectResponseCallback cb, void *cb_data)
3258 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_USIM_CFIS, ret);
3263 static TelReturn imc_sim_get_messagewaiting_info (CoreObject *co,
3264 TcoreObjectResponseCallback cb, void *cb_data)
3269 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_USIM_MWIS, ret);
3274 static TelReturn imc_sim_set_messagewaiting_info (CoreObject *co,
3275 const TelSimMwis *request, TcoreObjectResponseCallback cb, void *cb_data)
3277 ImcSimMetaInfo file_meta = {0, };
3278 ImcRespCbData *resp_cb_data = NULL;
3279 gchar *encoded_mwis;
3280 guint encoded_mwis_len = 0;
3281 gchar *encoded_data = NULL;
3282 guint encoded_data_len = 0;
3290 * Videomail is not supported.
3292 if (!tcore_sim_encode_mwis(request, TEL_SIM_MAILBOX_TYPE_MAX,
3293 &encoded_mwis, &encoded_mwis_len)) {
3294 err("Failed to encode mwis");
3295 return TEL_RETURN_FAILURE;
3298 encoded_data_len = 2 * encoded_mwis_len;
3299 encoded_data = (char *)tcore_malloc0(encoded_data_len + 1);
3300 tcore_util_byte_to_hex(encoded_mwis, encoded_data, encoded_mwis_len);
3301 tcore_free(encoded_mwis);
3302 dbg("Encoded data: [%s] Encoded data length: [%d]", encoded_data, encoded_data_len);
3306 p3 = TEL_SIM_MAILBOX_TYPE_MAX; /* Indicator Status | Voicemail | Fax | Electronic Mail | Others */
3307 dbg("p1: [%d] p2: [%d] p3: [%d]", p1, p2, p3);
3309 file_meta.file_id = TEL_SIM_EF_USIM_MWIS;
3310 file_meta.file_result = TEL_SIM_RESULT_FAILURE;
3312 resp_cb_data = imc_create_resp_cb_data(cb, cb_data, &file_meta, sizeof(ImcSimMetaInfo));
3314 return __imc_sim_update_file(co, resp_cb_data, IMC_SIM_ACCESS_UPDATE_RECORD,
3315 TEL_SIM_EF_USIM_MWIS, p1, p2, p3, encoded_data);
3318 static TelReturn imc_sim_get_mailbox_info (CoreObject *co,
3319 TcoreObjectResponseCallback cb, void *cb_data)
3324 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_USIM_MBI, ret);
3329 static TelReturn imc_sim_set_mailbox_info (CoreObject *co,
3330 const TelSimMailBoxNumber *request, TcoreObjectResponseCallback cb, void *cb_data)
3332 ImcSimMetaInfo file_meta = {0, };
3333 ImcRespCbData *resp_cb_data = NULL;
3336 char *encoded_data = NULL;
3337 int encoded_data_len = 0;
3344 file_meta.file_id = TEL_SIM_EF_USIM_MBI;
3345 file_meta.file_result = TEL_SIM_RESULT_FAILURE;
3347 /* TBD - Do Encoding.
3348 if (!tcore_sim_encode_mbi(request, sizeof(request), tmp, &tmp_len)) {
3349 err("Failed to encode mwis");
3350 return TEL_RETURN_FAILURE;
3353 encoded_data_len = tmp_len * 2;
3354 encoded_data = (char *)tcore_malloc0(encoded_data_len + 1);
3355 tcore_util_byte_to_hex(tmp, encoded_data, tmp_len);
3356 if (!encoded_data) {
3357 err("Failed to convert byte to hex");
3358 tcore_free(encoded_data);
3359 return TEL_RETURN_FAILURE;
3364 p3 = encoded_data_len;
3365 dbg("encoded_data - [%s], encoded_data_len - %d", encoded_data, encoded_data_len);
3367 resp_cb_data = imc_create_resp_cb_data(cb, cb_data, &file_meta, sizeof(ImcSimMetaInfo));
3369 return __imc_sim_update_file(co, resp_cb_data, IMC_SIM_ACCESS_UPDATE_RECORD,
3370 TEL_SIM_EF_USIM_MBI, p1, p2, p3, encoded_data);
3373 static TelReturn imc_sim_get_msisdn (CoreObject *co,
3374 TcoreObjectResponseCallback cb, void *cb_data)
3379 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_MSISDN, ret);
3384 static TelReturn imc_sim_get_spn (CoreObject *co,
3385 TcoreObjectResponseCallback cb, void *cb_data)
3390 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_SPN, ret);
3395 static TelReturn imc_sim_get_cphs_netname (CoreObject *co,
3396 TcoreObjectResponseCallback cb, void *cb_data)
3401 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_CPHS_OPERATOR_NAME_STRING, ret);
3406 static TelReturn imc_sim_get_sp_display_info (CoreObject *co,
3407 TcoreObjectResponseCallback cb, void *cb_data)
3412 IMC_SIM_READ_FILE(co, cb, cb_data, TEL_SIM_EF_SPDI, ret);
3417 static TelReturn imc_sim_req_authentication (CoreObject *co,
3418 const TelSimAuthenticationData *request,
3419 TcoreObjectResponseCallback cb, void *cb_data)
3421 gchar *cmd_str = NULL;
3422 ImcRespCbData *resp_cb_data = NULL;
3423 TelReturn ret = TEL_RETURN_FAILURE;
3424 TelSimCardType card_type = TEL_SIM_CARD_TYPE_UNKNOWN;
3425 char *convert_rand = NULL;
3426 char *convert_autn = NULL;
3432 tcore_sim_get_type(co, &card_type);
3433 if (TEL_SIM_CARD_TYPE_GSM == card_type || TEL_SIM_CARD_TYPE_USIM == card_type) {
3436 err("Not Supported SIM type:[%d]", card_type);
3437 return TEL_SIM_RESULT_OPERATION_NOT_SUPPORTED;
3440 if (request->rand_data != NULL) {
3441 convert_rand = tcore_malloc0(request->rand_length*2 + 1);
3442 tcore_util_byte_to_hex(request->rand_data, convert_rand, request->rand_length);
3443 dbg("Convert RAND hex to string: [%s]", convert_rand);
3445 err("rand_data is NULL");
3449 switch (request->auth_type) {
3450 case TEL_SIM_AUTH_GSM:
3452 cmd_str = g_strdup_printf("AT+XAUTH=%d,%d,\"%s\"",
3453 session_id, context_type, convert_rand);
3455 case TEL_SIM_AUTH_3G_CTX:
3457 if (request->autn_data != NULL) {
3458 convert_autn = tcore_malloc0(request->autn_length*2 + 1);
3459 tcore_util_byte_to_hex(request->autn_data, convert_autn, request->autn_length);
3460 dbg("Convert AUTN hex to string: [%s]", convert_autn);
3462 err("autn_data is NULL");
3465 cmd_str = g_strdup_printf("AT+XAUTH=%d,%d,\"%s\",\"%s\"",
3466 session_id, context_type, convert_rand, convert_autn);
3469 err("Not supported Authentication type:[%d]", request->auth_type);
3470 ret = TEL_SIM_RESULT_OPERATION_NOT_SUPPORTED;
3474 resp_cb_data = imc_create_resp_cb_data(cb, cb_data, (void *)&request->auth_type, sizeof(TelSimAuthenticationType));
3476 ret = tcore_at_prepare_and_send_request(co, cmd_str, "+XAUTH:",
3477 TCORE_AT_COMMAND_TYPE_SINGLELINE,
3478 TCORE_PENDING_PRIORITY_DEFAULT, NULL,
3479 on_response_imc_sim_req_authentication, resp_cb_data,
3480 on_send_imc_request, NULL, 0, NULL, NULL);
3482 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Sim request authentication");
3485 tcore_free(convert_rand);
3486 tcore_free(convert_autn);
3492 * Operation - verify_pins/verify_puks
3496 * AT-Command: AT+CPIN= <pin> [, <newpin>]
3499 * String type values
3502 * AT-Command: AT+CPIN2= <puk2/oldpin2> [, <newpin2>]andAT+CPIN2=<oldpin2>
3504 * <puk2/pin2>, <newpin2>
3505 * String type values
3511 * +CME ERROR: <error>
3513 static TelReturn imc_sim_verify_pins(CoreObject *co, const TelSimSecPinPw *request,
3514 TcoreObjectResponseCallback cb, void *cb_data)
3516 TelReturn ret = TEL_RETURN_FAILURE;
3517 ImcRespCbData *resp_cb_data = NULL;
3518 ImcSimCurrSecOp sec_op;
3519 gchar *cmd_str = NULL;
3523 if (request->pin_type == TEL_SIM_PIN_TYPE_PIN1) {
3524 sec_op = IMC_SIM_CURR_SEC_OP_PIN1_VERIFY;
3525 cmd_str = g_strdup_printf("AT+CPIN=\"%s\"", request->pw);
3526 } else if (request->pin_type == TEL_SIM_PIN_TYPE_PIN2) {
3527 sec_op = IMC_SIM_CURR_SEC_OP_PIN2_VERIFY;
3528 cmd_str = g_strdup_printf("AT+CPIN2=\"%s\"", request->pw);
3530 err("Invalid pin type [%d]", request->pin_type);
3531 return TEL_RETURN_INVALID_PARAMETER;
3534 resp_cb_data = imc_create_resp_cb_data(cb, cb_data,
3535 &sec_op, sizeof(sec_op));
3537 ret = tcore_at_prepare_and_send_request(co, cmd_str, NULL,
3538 TCORE_AT_COMMAND_TYPE_NO_RESULT,
3539 TCORE_PENDING_PRIORITY_DEFAULT,
3541 on_response_imc_sim_verify_pins,
3543 on_send_imc_request,
3544 NULL, 0, NULL, NULL);
3546 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Sim Verify Pins");
3551 static TelReturn imc_sim_verify_puks(CoreObject *co, const TelSimSecPukPw *request,
3552 TcoreObjectResponseCallback cb, void *cb_data)
3554 TelReturn ret = TEL_RETURN_FAILURE;
3555 ImcRespCbData *resp_cb_data = NULL;
3556 ImcSimCurrSecOp sec_op;
3557 gchar *cmd_str = NULL;
3561 if (request->puk_type == TEL_SIM_PUK_TYPE_PUK1) {
3562 sec_op = IMC_SIM_CURR_SEC_OP_PUK1_VERIFY;
3563 cmd_str = g_strdup_printf("AT+CPIN=\"%s\", \"%s\"",
3564 request->puk_pw, request->new_pin_pw);
3565 } else if (request->puk_type == TEL_SIM_PUK_TYPE_PUK2) {
3566 sec_op = IMC_SIM_CURR_SEC_OP_PUK2_VERIFY;
3567 cmd_str = g_strdup_printf("AT+CPIN2=\"%s\", \"%s\"",
3568 request->puk_pw, request->new_pin_pw);
3570 err("Invalid puk type [%d]", request->puk_type);
3571 return TEL_RETURN_INVALID_PARAMETER;
3574 resp_cb_data = imc_create_resp_cb_data(cb, cb_data,
3575 &sec_op, sizeof(sec_op));
3577 ret = tcore_at_prepare_and_send_request(co, cmd_str, NULL,
3578 TCORE_AT_COMMAND_TYPE_NO_RESULT,
3579 TCORE_PENDING_PRIORITY_DEFAULT,
3581 on_response_imc_sim_verify_puks,
3583 on_send_imc_request,
3584 NULL, 0, NULL, NULL);
3586 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Sim Verify Puks");
3592 * Operation - change_pins
3595 * AT-Command: AT+CPWD= <fac>,<oldpwd>,<newpwd>
3610 * +CME ERROR: <error>
3612 static TelReturn imc_sim_change_pins(CoreObject *co, const TelSimSecChangePinPw *request,
3613 TcoreObjectResponseCallback cb, void *cb_data)
3615 TelReturn ret = TEL_RETURN_FAILURE;
3616 ImcRespCbData *resp_cb_data = NULL;
3617 ImcSimCurrSecOp sec_op;
3618 gchar *cmd_str = NULL;
3619 char *pin1_fac = "SC";
3620 char *pin2_fac = "P2";
3624 if (request->pin_type == TEL_SIM_PIN_TYPE_PIN1) {
3625 sec_op = IMC_SIM_CURR_SEC_OP_PIN1_CHANGE;
3626 cmd_str = g_strdup_printf("AT+CPWD=\"%s\",\"%s\",\"%s\"",
3627 pin1_fac, request->old_pw, request->new_pw);
3628 } else if (request->pin_type == TEL_SIM_PIN_TYPE_PIN2) {
3629 sec_op = IMC_SIM_CURR_SEC_OP_PIN2_CHANGE;
3630 cmd_str = g_strdup_printf("AT+CPWD=\"%s\",\"%s\",\"%s\"",
3631 pin2_fac, request->old_pw, request->new_pw);
3633 err("Invalid pin type [%d]", request->pin_type);
3634 return TEL_RETURN_INVALID_PARAMETER;
3637 resp_cb_data = imc_create_resp_cb_data(cb, cb_data,
3638 &sec_op, sizeof(sec_op));
3640 ret = tcore_at_prepare_and_send_request(co, cmd_str, NULL,
3641 TCORE_AT_COMMAND_TYPE_NO_RESULT,
3642 TCORE_PENDING_PRIORITY_DEFAULT,
3644 on_response_imc_sim_change_pins,
3646 on_send_imc_request,
3647 NULL, 0, NULL, NULL);
3649 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Sim Change Pins");
3655 * Operation - disable_facility/enable_facility/get_facility
3658 * AT-Command: AT+CLCK = <fac>, <mode> [, <passwd> [, <class>]]
3675 * Success: when <mode>=2:
3677 * +CLCK: <status>[,<class1> [<CR><LF>
3678 * +CLCK: <status>,<class2> [...]]
3682 static TelReturn imc_sim_disable_facility(CoreObject *co, const TelSimFacilityPw *request,
3683 TcoreObjectResponseCallback cb, void *cb_data)
3685 TelReturn ret = TEL_RETURN_FAILURE;
3686 ImcRespCbData *resp_cb_data = NULL;
3687 ImcSimCurrSecOp sec_op;
3688 gchar *cmd_str = NULL;
3690 int mode = 0; /*mode = 0 for disable lock*/
3694 fac = __imc_sim_get_fac_from_lock_type(request->lock_type,
3695 &sec_op, DISABLE_FLAG);
3697 return TEL_RETURN_INVALID_PARAMETER;
3699 cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d, \"%s\"",
3700 fac, mode, request->pw);
3702 resp_cb_data = imc_create_resp_cb_data(cb, cb_data,
3703 &sec_op, sizeof(sec_op));
3705 ret = tcore_at_prepare_and_send_request(co, cmd_str, "+CLCK:",
3706 TCORE_AT_COMMAND_TYPE_SINGLELINE,
3707 TCORE_PENDING_PRIORITY_DEFAULT,
3709 on_response_imc_sim_disable_facility,
3711 on_send_imc_request,
3712 NULL, 0, NULL, NULL);
3714 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Sim Disable Facility");
3719 static TelReturn imc_sim_enable_facility(CoreObject *co, const TelSimFacilityPw *request,
3720 TcoreObjectResponseCallback cb, void *cb_data)
3722 TelReturn ret = TEL_RETURN_FAILURE;
3723 ImcRespCbData *resp_cb_data = NULL;
3724 ImcSimCurrSecOp sec_op;
3725 gchar *cmd_str = NULL;
3727 int mode = 1; /*mode = 1 for enable lock*/
3731 fac = __imc_sim_get_fac_from_lock_type(request->lock_type,
3732 &sec_op, ENABLE_FLAG);
3734 return TEL_RETURN_INVALID_PARAMETER;
3736 cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d, \"%s\"",
3737 fac, mode, request->pw);
3739 resp_cb_data = imc_create_resp_cb_data(cb, cb_data,
3740 &sec_op, sizeof(sec_op));
3742 ret = tcore_at_prepare_and_send_request(co, cmd_str, "+CLCK:",
3743 TCORE_AT_COMMAND_TYPE_SINGLELINE,
3744 TCORE_PENDING_PRIORITY_DEFAULT,
3746 on_response_imc_sim_enable_facility,
3748 on_send_imc_request,
3749 NULL, 0, NULL, NULL);
3751 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Sim Disable Facility");
3756 static TelReturn imc_sim_get_facility(CoreObject *co, TelSimLockType lock_type,
3757 TcoreObjectResponseCallback cb, void *cb_data)
3759 TelReturn ret = TEL_RETURN_FAILURE;
3760 ImcRespCbData *resp_cb_data = NULL;
3761 ImcSimCurrSecOp sec_op;
3762 gchar *cmd_str = NULL;
3764 int mode = 2; /*mode = 2 for Get Facility*/
3768 fac = __imc_sim_get_fac_from_lock_type(lock_type,
3771 return TEL_RETURN_INVALID_PARAMETER;
3773 cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d", fac, mode);
3775 resp_cb_data = imc_create_resp_cb_data(cb, cb_data,
3776 &sec_op, sizeof(sec_op));
3778 ret = tcore_at_prepare_and_send_request(co, cmd_str, "+CLCK:",
3779 TCORE_AT_COMMAND_TYPE_SINGLELINE,
3780 TCORE_PENDING_PRIORITY_DEFAULT,
3782 on_response_imc_sim_get_facility,
3784 on_send_imc_request,
3785 NULL, 0, NULL, NULL);
3787 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Sim Get Facility");
3792 static TelReturn imc_sim_get_lock_info(CoreObject *co, TelSimLockType lock_type,
3793 TcoreObjectResponseCallback cb, void *cb_data)
3795 TelReturn ret = TEL_RETURN_FAILURE;
3796 ImcRespCbData *resp_cb_data = NULL;
3797 gchar *cmd_str = NULL;
3802 resp_cb_data = imc_create_resp_cb_data(cb, cb_data, NULL, 0);
3804 switch (lock_type) {
3805 case TEL_SIM_LOCK_PS:
3809 case TEL_SIM_LOCK_SC:
3813 case TEL_SIM_LOCK_FD:
3817 case TEL_SIM_LOCK_PN:
3821 case TEL_SIM_LOCK_PU:
3825 case TEL_SIM_LOCK_PP:
3829 case TEL_SIM_LOCK_PC:
3837 cmd_str = g_strdup_printf("AT+XPINCNT=%d", lockType);
3839 ret = tcore_at_prepare_and_send_request(co, cmd_str, "+XPINCNT:",
3840 TCORE_AT_COMMAND_TYPE_SINGLELINE,
3841 TCORE_PENDING_PRIORITY_DEFAULT,
3843 on_response_imc_sim_get_lock_info,
3845 on_send_imc_request,
3846 NULL, 0, NULL, NULL);
3848 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Sim Get Lock Info");
3853 static TelReturn imc_sim_req_apdu (CoreObject *co, const TelSimApdu *request, TcoreObjectResponseCallback cb, void *cb_data)
3855 gchar *cmd_str = NULL;
3857 ImcRespCbData *resp_cb_data = NULL;
3858 TelReturn ret = TEL_RETURN_FAILURE;
3862 resp_cb_data = imc_create_resp_cb_data(cb, cb_data, NULL, 0);
3864 apdu = (char *)tcore_malloc0((2 * request->apdu_len) + 1);
3865 tcore_util_byte_to_hex((char *)request->apdu, apdu, request->apdu_len);
3867 cmd_str = g_strdup_printf("AT+CSIM=%d,\"%s\"", strlen((const char *)apdu), apdu);
3869 ret = tcore_at_prepare_and_send_request(co, cmd_str, "+CSIM:",
3870 TCORE_AT_COMMAND_TYPE_SINGLELINE,
3871 TCORE_PENDING_PRIORITY_DEFAULT, NULL,
3872 on_response_imc_sim_req_apdu, resp_cb_data,
3873 on_send_imc_request, NULL, 0, NULL, NULL);
3875 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Sim Request APDU");
3884 static TelReturn imc_sim_req_atr (CoreObject *co, TcoreObjectResponseCallback cb, void *cb_data)
3886 gchar *cmd_str = NULL;
3887 ImcRespCbData *resp_cb_data = NULL;
3888 TelReturn ret = TEL_RETURN_FAILURE;
3892 resp_cb_data = imc_create_resp_cb_data(cb, cb_data, NULL, 0);
3894 cmd_str = g_strdup_printf("AT+XGATR");
3896 ret = tcore_at_prepare_and_send_request(co, cmd_str, "+XGATR:",
3897 TCORE_AT_COMMAND_TYPE_SINGLELINE,
3898 TCORE_PENDING_PRIORITY_DEFAULT, NULL,
3899 on_response_imc_sim_req_atr, resp_cb_data,
3900 on_send_imc_request, NULL, 0, NULL, NULL);
3902 IMC_CHECK_REQUEST_RET(ret, resp_cb_data, "Sim Request ATR");
3910 /* SIM Operations */
3911 static TcoreSimOps imc_sim_ops = {
3912 .get_imsi = imc_sim_get_imsi,
3913 .get_ecc = imc_sim_get_ecc,
3914 .get_iccid = imc_sim_get_iccid,
3915 .get_language = imc_sim_get_language,
3916 .set_language = imc_sim_set_language,
3917 .get_callforwarding_info = imc_sim_get_callforwarding_info,
3918 .get_messagewaiting_info = imc_sim_get_messagewaiting_info,
3919 .set_messagewaiting_info = imc_sim_set_messagewaiting_info,
3920 .get_mailbox_info = imc_sim_get_mailbox_info,
3921 .set_mailbox_info = imc_sim_set_mailbox_info,
3922 .get_msisdn = imc_sim_get_msisdn,
3923 .get_spn = imc_sim_get_spn,
3924 .get_cphs_netname = imc_sim_get_cphs_netname,
3925 .get_sp_display_info = imc_sim_get_sp_display_info,
3926 .req_authentication = imc_sim_req_authentication,
3927 .verify_pins = imc_sim_verify_pins,
3928 .verify_puks = imc_sim_verify_puks,
3929 .change_pins = imc_sim_change_pins,
3930 .disable_facility = imc_sim_disable_facility,
3931 .enable_facility = imc_sim_enable_facility,
3932 .get_facility = imc_sim_get_facility,
3933 .get_lock_info = imc_sim_get_lock_info,
3934 .req_apdu = imc_sim_req_apdu,
3935 .req_atr = imc_sim_req_atr
3938 gboolean imc_sim_init(TcorePlugin *p, CoreObject *co)
3940 ImcSimPrivateInfo *priv_info = NULL;
3944 priv_info = g_try_new0(ImcSimPrivateInfo, 1);
3945 if (priv_info == NULL)
3948 tcore_sim_link_userdata(co, priv_info);
3950 /* Set operations */
3951 tcore_sim_set_ops(co, &imc_sim_ops);
3954 tcore_object_add_callback(co, "+XSIM:",
3955 on_notification_imc_sim_status, NULL);
3958 tcore_plugin_add_notification_hook(p,
3959 TCORE_NOTIFICATION_MODEM_POWER,
3960 on_hook_imc_modem_power, co);
3966 void imc_sim_exit(TcorePlugin *plugin, CoreObject *co)
3968 ImcSimPrivateInfo *priv_info = NULL;
3972 priv_info = tcore_sim_ref_userdata(co);