Code sync from tizen_2.4
[platform/core/telephony/tel-plugin-dbus_tapi.git] / src / dtapi_sim.c
1 /*
2  * tel-plugin-dbus-tapi
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include <glib.h>
26
27 #include <tcore.h>
28 #include <server.h>
29 #include <plugin.h>
30 #include <user_request.h>
31 #include <co_sim.h>
32
33 #include "generated-code.h"
34 #include "dtapi_common.h"
35
36 /*
37  * Error message to application (DBUS)
38  */
39 #define DBUS_SIM_STATUS_ERROR                           "SIM STATUS ERROR"
40 #define DBUS_SIM_NOT_FOUND                              "SIM NOT FOUND"
41 #define DBUS_SIM_PERM_BLOCKED                           "SIM PERM BLOCKED"
42 #define DBUS_SIM_CARD_ERROR                             "SIM CARD ERROR"
43 #define DBUS_SIM_NOT_INITIALIZED                        "SIM NOT INITIALIZED"
44 #define DBUS_SIM_INIT_COMPLETED                 "SIM INIT COMPLETED"
45 #define DBUS_SIM_LOCKED                         "SIM LOCKED"
46 #define DBUS_SIM_NOT_READY                              "SIM NOT READY"
47 #define DBUS_SIM_RESPONSE_DATA_ERROR            "SIM RESPONSE DATA ERROR"
48 #define DBUS_SIM_SERVICE_IS_DISABLED                    "SIM SERVICE IS DISABLED"
49 #define DBUS_SIM_SERVICE_NOT_SUPPORTED_FOR_NVSIM        "SERVICE NOT SUPPORTED FOR NVSIM"
50
51 #define DBUS_SIM_GET_COSIM(invocation, co_sim, server) do { \
52         co_sim = __get_sim_co_by_cp_name(server, GET_CP_NAME(invocation)); \
53         if (!co_sim) { \
54                 err("[%s] SIM Core object is NULL", GET_CP_NAME(invocation)); \
55                 FAIL_RESPONSE(invocation, DEFAULT_MSG_REQ_FAILED); \
56                 return TRUE; \
57         } \
58 } while (0)
59
60 #define DBUS_SIM_CHECK_SIM_STATUS(invocation, op_type, co_sim) do { \
61         if (__check_sim_state(op_type, tcore_sim_get_status(co_sim)) == FALSE) { \
62                 err("[%s] Invalid SIM status", GET_CP_NAME(invocation)); \
63                 __return_fail_response(invocation, tcore_sim_get_status(co_sim)); \
64                 return TRUE; \
65         } \
66 } while (0)
67
68 #define DBUS_SIM_CHECK_SIM_TYPE(co_sim, request) do { \
69         if (tcore_sim_get_type(co_sim) == SIM_TYPE_NVSIM) { \
70                 err("[%s] is not supported for NVSIM", request); \
71                 FAIL_RESPONSE(invocation, DBUS_SIM_SERVICE_NOT_SUPPORTED_FOR_NVSIM); \
72                 return TRUE; \
73         } \
74 } while (0)
75
76 #define DBUS_SIM_CHECK_SIM_SERVICE_TABLE(invocation, op_type, co_sim) do { \
77         gboolean b_cphs = FALSE; \
78         b_cphs = tcore_sim_get_cphs_status(co_sim); \
79         if (b_cphs && op_type != GET_MSISDN) { \
80                 dbg("[%s] CPHS SIM... Do not check SST", GET_CP_NAME(invocation)); \
81         } else { \
82                 struct tel_sim_service_table* svct = tcore_sim_get_service_table(co_sim); \
83                 if (svct != NULL) { \
84                         if (__check_sim_service_table(op_type, svct) == FALSE) { \
85                                 err("[%s] 'Service' is disabled in SST", GET_CP_NAME(invocation)); \
86                                 FAIL_RESPONSE(invocation, DBUS_SIM_SERVICE_IS_DISABLED); \
87                                 free(svct); \
88                                 return TRUE; \
89                         } else { \
90                                 dbg("[%s] Request to modem", GET_CP_NAME(invocation)); \
91                                 free(svct); \
92                         } \
93                 } \
94         } \
95 } while (0)
96
97 #define DBUS_SIM_CHECK_DISPATCH_RET(ret, invocation) do { \
98         if (ret != TCORE_RETURN_SUCCESS) { \
99                 if (ret == TCORE_RETURN_SIM_DISABLED_IN_SST) { \
100                         err("[%s] 'Service' is disabled in SST", GET_CP_NAME(invocation)); \
101                         FAIL_RESPONSE(invocation, DBUS_SIM_SERVICE_IS_DISABLED); \
102                 } else { \
103                         err("[%s] Dispatch request failed: [0x%x]", GET_CP_NAME(invocation), ret); \
104                         FAIL_RESPONSE(invocation, DEFAULT_MSG_REQ_FAILED); \
105                 } \
106         } \
107 } while (0)
108
109 enum dbus_tapi_sim_gdbus_method_name {
110         /* EF Get */
111         GET_INIT_STATUS = 1,
112         GET_CARD_TYPE,
113         GET_IMSI,
114         GET_ECC,
115         GET_ICCID = 5,
116         GET_LANGUAGE,
117         SET_LANGUAGE,
118         GET_CALL_FORWARDING,
119         SET_CALL_FORWARDING,
120         GET_MESSAGE_WAITING = 10,
121         SET_MESSAGE_WAITING,
122         GET_MAILBOX,
123         SET_MAILBOX,
124         GET_CPHS_INFO,
125         GET_SVCT = 15,
126         GET_MSISDN,
127         GET_OPLMWACT,
128         GET_SPN,
129         GET_CPHS_NET_NAME,
130
131         /* Misc */
132         AUTHENTICATION = 20,
133         VERIFY_SEC,
134         VERIFY_PUK,
135         CHANGE_PIN,
136         DISABLE_FACILITY,
137         ENABLE_FACILITY = 25,
138         GET_FACILITY,
139         GET_LOCK_INFO,
140         TRANSFER_APDU,
141         GET_ATR,
142         GET_FIELDS = 30, /* for get various data at once */
143         GET_GID,
144         SET_POWERSTATE,
145         GET_IMPI,
146         GET_IMPU,
147         GET_DOMAIN = 35,
148         GET_PCSCF,
149         GET_APP_LIST,
150         GET_ISIM_SERVICE_TABLE,
151
152         /* Notification */
153         STATUS = 100,
154         REFRESHED,
155 };
156
157 static gboolean __is_sim_status_valid(enum tel_sim_status sim_status)
158 {
159         switch (sim_status) {
160         case SIM_STATUS_INIT_COMPLETED:
161         case SIM_STATUS_INITIALIZING:
162         case SIM_STATUS_PIN_REQUIRED:
163         case SIM_STATUS_PUK_REQUIRED:
164         case SIM_STATUS_LOCK_REQUIRED:
165         case SIM_STATUS_CARD_BLOCKED:
166         case SIM_STATUS_NCK_REQUIRED:
167         case SIM_STATUS_NSCK_REQUIRED:
168         case SIM_STATUS_SPCK_REQUIRED:
169         case SIM_STATUS_CCK_REQUIRED:
170                 return TRUE;
171         default:
172                 return FALSE;
173         }
174 }
175
176 static CoreObject *__get_sim_co_by_cp_name(Server *server, char *cp_name)
177 {
178         TcorePlugin *plugin;
179
180         if (!server) {
181                 err("server is NULL");
182                 return NULL;
183         }
184
185         plugin = tcore_server_find_plugin(server, cp_name);
186         return tcore_plugin_ref_core_object(plugin, CORE_OBJECT_TYPE_SIM);
187 }
188
189 static CoreObject* __get_sim_co_from_ur(Server *server, UserRequest *ur)
190 {
191         CoreObject *co_sim;
192         char *modem_name;
193
194         modem_name = tcore_user_request_get_modem_name(ur);
195         if (!modem_name) {
196                 err("Modem name is NULL");
197                 return NULL;
198         }
199
200         co_sim = __get_sim_co_by_cp_name(server, modem_name);
201         free(modem_name);
202
203         return co_sim;
204 }
205
206 static gboolean __check_sim_state(enum dbus_tapi_sim_gdbus_method_name method,
207         enum tel_sim_status sim_status)
208 {
209         gboolean ret = TRUE;
210
211         if ((int)sim_status < SIM_STATUS_CARD_ERROR) {
212                 err("SIM status is NOT valid");
213                 return FALSE;
214         }
215
216         switch (method) {
217         case GET_CARD_TYPE:
218         case GET_ECC:
219         case GET_ICCID:
220         case GET_LANGUAGE:
221         case GET_CPHS_INFO:
222         case GET_SPN:
223         case AUTHENTICATION:
224         case TRANSFER_APDU:
225         case GET_ATR:
226         /* Regarding Lock facilities */
227         case CHANGE_PIN:
228         case ENABLE_FACILITY:
229         case DISABLE_FACILITY:
230         case GET_FACILITY:
231         case GET_LOCK_INFO:
232         case VERIFY_SEC:
233         case VERIFY_PUK:
234                 switch (sim_status) {
235                 case SIM_STATUS_CARD_ERROR: /* FALLTHROUGH */
236                 case SIM_STATUS_CARD_BLOCKED: /* FALLTHROUGH */
237                 case SIM_STATUS_CARD_NOT_PRESENT: /* FALLTHROUGH */
238                 case SIM_STATUS_CARD_REMOVED: /* FALLTHROUGH */
239                 case SIM_STATUS_UNKNOWN: /* FALLTHROUGH */
240                 case SIM_STATUS_CARD_POWEROFF:
241                         ret = FALSE;
242                 break;
243
244                 default:
245                 break;
246                 }
247         break;
248
249         case GET_IMSI:
250         case GET_SVCT:
251         case GET_MSISDN:
252         case GET_OPLMWACT:
253         case GET_CPHS_NET_NAME:
254         case GET_CALL_FORWARDING:
255         case SET_CALL_FORWARDING:
256         case GET_MESSAGE_WAITING:
257         case SET_MESSAGE_WAITING:
258         case GET_MAILBOX:
259         case SET_MAILBOX:
260         case SET_LANGUAGE:
261         case GET_FIELDS:
262         case GET_IMPI:
263         case GET_IMPU:
264         case GET_GID:
265         case GET_DOMAIN:
266         case GET_PCSCF:
267         case GET_APP_LIST:
268         case GET_ISIM_SERVICE_TABLE:
269                 if (sim_status != SIM_STATUS_INIT_COMPLETED)
270                         ret = FALSE;
271         break;
272
273         case SET_POWERSTATE:
274                 switch (sim_status) {
275                 case SIM_STATUS_INIT_COMPLETED: /* FALLTHROUGH */
276                 case SIM_STATUS_INITIALIZING: /* FALLTHROUGH */
277                 case SIM_STATUS_PIN_REQUIRED: /* FALLTHROUGH */
278                 case SIM_STATUS_CARD_BLOCKED: /* FALLTHROUGH */
279                 case SIM_STATUS_CARD_POWEROFF:
280                 break;
281
282                 default:
283                         ret = FALSE;
284                 break;
285                 }
286         break;
287
288         case GET_INIT_STATUS:
289         case STATUS:
290         case REFRESHED:
291         default:
292                 err("Unhandled/Unknown operation: [%d]", method);
293         break;
294         }
295
296         return ret;
297 }
298
299 static gboolean __check_sim_service_table(enum dbus_tapi_sim_gdbus_method_name method,
300         struct tel_sim_service_table *svct)
301 {
302         enum tel_sim_type sim_type = svct->sim_type;
303
304         gboolean ret = TRUE;
305
306         switch (method) {
307         case GET_MSISDN:
308                 if (!(sim_type == SIM_TYPE_GSM && svct->table.sst.service[SIM_SST_MSISDN])
309                                 && !(sim_type == SIM_TYPE_USIM && svct->table.ust.service[SIM_UST_MSISDN]))
310                         ret = FALSE;
311         break;
312
313         case GET_CALL_FORWARDING:
314         case SET_CALL_FORWARDING:
315                 if (!(sim_type == SIM_TYPE_GSM && svct->table.sst.service[SIM_SST_CFIS])
316                                 && !(sim_type == SIM_TYPE_USIM && svct->table.ust.service[SIM_UST_CFIS]))
317                         ret = FALSE;
318         break;
319
320         case GET_MESSAGE_WAITING:
321         case SET_MESSAGE_WAITING:
322                 if (!(sim_type == SIM_TYPE_GSM && svct->table.sst.service[SIM_SST_MWIS])
323                                 && !(sim_type == SIM_TYPE_USIM && svct->table.ust.service[SIM_UST_MWIS]))
324                         ret = FALSE;
325         break;
326
327         case GET_MAILBOX:
328         case SET_MAILBOX:
329                 if (!(sim_type == SIM_TYPE_GSM && svct->table.sst.service[SIM_SST_MBDN]) &&
330                                 !(sim_type == SIM_TYPE_USIM && svct->table.ust.service[SIM_UST_MBDN]))
331                         ret = FALSE;
332         break;
333
334         default:
335                 err("Unhandled/Unknown operation: [%d]", method);
336         break;
337         }
338
339         return ret;
340 }
341
342 static void __return_fail_response(GDBusMethodInvocation *invocation,
343         enum tel_sim_status sim_status)
344 {
345         dbg("[%s] SIM Status: [%d]", GET_CP_NAME(invocation), sim_status);
346
347         switch (sim_status) {
348         case SIM_STATUS_CARD_NOT_PRESENT:
349         case SIM_STATUS_CARD_REMOVED:
350                 FAIL_RESPONSE(invocation, DBUS_SIM_NOT_FOUND);
351         break;
352
353         case SIM_STATUS_CARD_BLOCKED:
354                 FAIL_RESPONSE(invocation, DBUS_SIM_PERM_BLOCKED);
355         break;
356
357         case SIM_STATUS_CARD_ERROR:
358         case SIM_STATUS_CARD_CRASHED:
359                 FAIL_RESPONSE(invocation, DBUS_SIM_CARD_ERROR);
360         break;
361
362         case SIM_STATUS_INITIALIZING:
363                 FAIL_RESPONSE(invocation, DBUS_SIM_NOT_INITIALIZED);
364         break;
365
366         case SIM_STATUS_INIT_COMPLETED:
367                 FAIL_RESPONSE(invocation, DBUS_SIM_INIT_COMPLETED);
368         break;
369
370         case SIM_STATUS_PIN_REQUIRED:
371         case SIM_STATUS_PUK_REQUIRED:
372         case SIM_STATUS_NCK_REQUIRED:
373         case SIM_STATUS_NSCK_REQUIRED:
374         case SIM_STATUS_SPCK_REQUIRED:
375         case SIM_STATUS_CCK_REQUIRED:
376         case SIM_STATUS_LOCK_REQUIRED:
377                 FAIL_RESPONSE(invocation, DBUS_SIM_LOCKED);
378         break;
379
380         case SIM_STATUS_UNKNOWN:
381                 FAIL_RESPONSE(invocation, DBUS_SIM_NOT_READY);
382         break;
383
384         default:
385                 dbg("Unhandled/Unknown status: [%d]", sim_status);
386                 FAIL_RESPONSE(invocation, DBUS_SIM_STATUS_ERROR);
387         break;
388         }
389 }
390
391 static gboolean on_sim_get_init_status(TelephonySim *sim,
392         GDBusMethodInvocation *invocation, gpointer user_data)
393 {
394         struct custom_data *ctx = user_data;
395         enum tel_sim_status sim_status = SIM_STATUS_UNKNOWN;
396         gboolean sim_changed = FALSE;
397         CoreObject *co_sim = NULL;
398         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
399
400         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
401                 return TRUE;
402
403         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
404
405         sim_status = tcore_sim_get_status(co_sim);
406         sim_changed = tcore_sim_get_identification(co_sim);
407         dbg("[%s] SIM - Status: [%d] Changed: [%s]",
408                 GET_CP_NAME(invocation),
409                 sim_status, (sim_changed ? "YES" : "NO"));
410
411         telephony_sim_complete_get_init_status(sim,
412                 invocation, sim_status, sim_changed);
413
414         return TRUE;
415 }
416
417 static gboolean on_sim_get_card_type(TelephonySim *sim,
418         GDBusMethodInvocation *invocation, gpointer user_data)
419 {
420         struct custom_data *ctx = user_data;
421         enum tel_sim_type sim_type = SIM_TYPE_UNKNOWN;
422         CoreObject *co_sim = NULL;
423         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
424
425         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
426                 return TRUE;
427
428         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
429         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_CARD_TYPE, co_sim);
430
431         sim_type = tcore_sim_get_type(co_sim);
432         dbg("[%s] SIM Card type: [%s]",  GET_CP_NAME(invocation),
433                 (sim_type == SIM_TYPE_GSM ? "GSM" :
434                 (sim_type == SIM_TYPE_USIM ? "USIM" :
435                 (sim_type == SIM_TYPE_RUIM ? "RUIM" :
436                 (sim_type == SIM_TYPE_NVSIM ? "NVSIM" :
437                 (sim_type == SIM_TYPE_ISIM ? "ISIM" :
438                 "UNKNOWN"))))));
439
440         telephony_sim_complete_get_card_type(sim,
441                 invocation, sim_type);
442
443         return TRUE;
444 }
445
446 static gboolean on_sim_get_imsi(TelephonySim *sim,
447         GDBusMethodInvocation *invocation, gpointer user_data)
448 {
449         struct custom_data *ctx = user_data;
450         struct tel_sim_imsi *imsi;
451         CoreObject *co_sim = NULL;
452         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
453
454         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
455                 return TRUE;
456
457         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
458         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_IMSI, co_sim);
459
460         imsi = tcore_sim_get_imsi(co_sim);
461         if (!imsi) {
462                 FAIL_RESPONSE(invocation, DBUS_SIM_RESPONSE_DATA_ERROR);
463         } else {
464                 dbg("[%s] IMSI - PLMN: [%s] MSIN: [%s]", GET_CP_NAME(invocation),
465                         imsi->plmn, imsi->msin);
466
467                 telephony_sim_complete_get_imsi(sim,
468                         invocation, imsi->plmn, imsi->msin);
469
470                 g_free(imsi);
471         }
472
473         return TRUE;
474 }
475
476 static gboolean on_sim_get_ecc(TelephonySim *sim,
477         GDBusMethodInvocation *invocation, gpointer user_data)
478 {
479         struct custom_data *ctx = user_data;
480         CoreObject *co_sim = NULL;
481         struct tel_sim_ecc_list *ecc_list = NULL;
482         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
483
484         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
485                 return TRUE;
486
487         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
488         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_ECC, co_sim);
489         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get ECC");
490
491         ecc_list = tcore_sim_get_ecc_list(co_sim);
492         if (!ecc_list) {
493                 struct custom_data *ctx = user_data;
494
495                 dbg("[%s] ECC list is NULL - Request to Modem.", GET_CP_NAME(invocation));
496
497                 /* Dispatch request */
498                 dtapi_dispatch_request(ctx, sim, invocation,
499                         TREQ_SIM_GET_ECC,
500                         NULL, 0);
501         } else {
502                 GVariant *gv = NULL;
503                 GVariantBuilder b;
504                 int i;
505
506                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
507                 for (i = 0; i < ecc_list->ecc_count; i++) {
508                         dbg("[%s] ECC[%d] - Category: [0x%x] Number: [%s] String: [%s]",
509                                 GET_CP_NAME(invocation), i,
510                                 ecc_list->ecc[i].ecc_category,
511                                 ecc_list->ecc[i].ecc_num,
512                                 ecc_list->ecc[i].ecc_string);
513                         g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
514                         g_variant_builder_add(&b, "{sv}", "category",
515                                 g_variant_new_int32(ecc_list->ecc[i].ecc_category));
516                         g_variant_builder_add(&b, "{sv}", "number",
517                                 g_variant_new_string(ecc_list->ecc[i].ecc_num));
518                         g_variant_builder_add(&b, "{sv}", "name",
519                                 g_variant_new_string(ecc_list->ecc[i].ecc_string));
520                         g_variant_builder_close(&b);
521                 }
522                 gv = g_variant_builder_end(&b);
523                 if (!gv)
524                         err("ecc gv is NULL");
525
526                 telephony_sim_complete_get_ecc(sim,
527                         invocation, gv);
528
529                 g_free(ecc_list);
530         }
531
532         return TRUE;
533 }
534
535 static gboolean on_sim_get_iccid(TelephonySim *sim,
536         GDBusMethodInvocation *invocation, gpointer user_data)
537 {
538         struct custom_data *ctx = user_data;
539         CoreObject *co_sim = NULL;
540         struct tel_sim_iccid* iccid = NULL;
541         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
542
543         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
544                 return TRUE;
545
546         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
547         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_ICCID, co_sim);
548         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get ICCID");
549
550         iccid = tcore_sim_get_iccid(co_sim);
551         if (!iccid) {
552                 /* Dispatch request */
553                 dtapi_dispatch_request(ctx, sim, invocation,
554                         TREQ_SIM_GET_ICCID,
555                         NULL, 0);
556         } else {
557                 dbg("[%s] ICCID: [%s]", GET_CP_NAME(invocation),
558                         iccid->iccid);
559
560                 telephony_sim_complete_get_iccid(sim,
561                         invocation, SIM_ACCESS_SUCCESS, iccid->iccid);
562
563                 g_free(iccid);
564         }
565
566         return TRUE;
567 }
568
569 static gboolean on_sim_get_language(TelephonySim *sim,
570         GDBusMethodInvocation *invocation, gpointer user_data)
571 {
572         struct custom_data *ctx = user_data;
573         CoreObject *co_sim = NULL;
574         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
575
576         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
577                 return TRUE;
578
579         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
580         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_LANGUAGE, co_sim);
581         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get Language");
582
583         /* Dispatch request */
584         dtapi_dispatch_request(ctx, sim, invocation,
585                 TREQ_SIM_GET_LANGUAGE,
586                 NULL, 0);
587
588         return TRUE;
589 }
590
591 static gboolean on_sim_set_language(TelephonySim *sim,
592         GDBusMethodInvocation *invocation, gint language, gpointer user_data)
593 {
594         struct custom_data *ctx = user_data;
595         CoreObject *co_sim = NULL;
596         struct treq_sim_set_language req;
597         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
598
599         if (!check_access_control(p_cynara, invocation, AC_SIM, "w"))
600                 return TRUE;
601
602         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
603         DBUS_SIM_CHECK_SIM_STATUS(invocation, SET_LANGUAGE, co_sim);
604         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Set Language");
605
606         memset(&req, 0x0, sizeof(struct treq_sim_set_language));
607
608         req.language = language;
609
610         dbg("[%s] Language: [0x%02x]", GET_CP_NAME(invocation),
611                 req.language);
612
613         /* Dispatch request */
614         dtapi_dispatch_request(ctx, sim, invocation,
615                 TREQ_SIM_SET_LANGUAGE,
616                 &req, sizeof(struct treq_sim_set_language));
617
618         return TRUE;
619 }
620
621 static gboolean on_sim_get_call_forwarding(TelephonySim *sim,
622         GDBusMethodInvocation *invocation, gpointer user_data)
623 {
624         struct custom_data *ctx = user_data;
625         CoreObject *co_sim = NULL;
626         TReturn ret;
627         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
628
629         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
630                 return TRUE;
631
632         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
633         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_CALL_FORWARDING, co_sim);
634         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get Call Forwarding");
635         DBUS_SIM_CHECK_SIM_SERVICE_TABLE(invocation, GET_CALL_FORWARDING, co_sim);
636
637         /* Dispatch request */
638         ret = dtapi_dispatch_request_ex(ctx, sim, invocation,
639                 TREQ_SIM_GET_CALLFORWARDING,
640                 NULL, 0);
641         DBUS_SIM_CHECK_DISPATCH_RET(ret, invocation);
642
643         return TRUE;
644 }
645
646 static gboolean on_sim_set_call_forwarding(TelephonySim *sim,
647         GDBusMethodInvocation *invocation,
648         gboolean  cphs,
649         gint rec_index,
650         gint msp_num,
651         guchar cfu_status,
652         gint ton, gint npi, const gchar *number,
653         gint cc2_id, gint ext7_id,
654         gboolean cphs_line1, gboolean cphs_line2,
655         gboolean cphs_fax, gboolean cphs_data, gpointer user_data)
656 {
657         struct custom_data *ctx = user_data;
658         CoreObject *co_sim = NULL;
659         struct treq_sim_set_callforwarding req;
660         TReturn ret;
661         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
662
663         if (!check_access_control(p_cynara, invocation, AC_SIM, "w"))
664                 return TRUE;
665
666         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
667         DBUS_SIM_CHECK_SIM_STATUS(invocation, SET_CALL_FORWARDING, co_sim);
668         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Set Call Forwarding");
669         DBUS_SIM_CHECK_SIM_SERVICE_TABLE(invocation, SET_CALL_FORWARDING, co_sim);
670
671         memset(&req, 0x0, sizeof(struct treq_sim_set_callforwarding));
672
673         req.b_cphs = cphs;
674         if (req.b_cphs) {
675                 req.cphs_cf.b_line1 = cphs_line1;
676                 req.cphs_cf.b_line2 = cphs_line2;
677                 req.cphs_cf.b_fax = cphs_fax;
678                 req.cphs_cf.b_data = cphs_data;
679
680                 dbg("[%s] b_line1: [%d] b_line2: [%d] b_fax: [%d] b_data: [%d]",
681                         GET_CP_NAME(invocation),
682                         req.cphs_cf.b_line1, req.cphs_cf.b_line2,
683                         req.cphs_cf.b_fax, req.cphs_cf.b_data);
684         } else {
685                 req.cf.rec_index = rec_index;
686                 req.cf.msp_num = msp_num;
687                 req.cf.cfu_status = cfu_status;
688                 req.cf.ton = ton;
689                 req.cf.npi = npi;
690                 memcpy(&req.cf.cfu_num, number, strlen(number));
691                 req.cf.cc2_id = cc2_id;
692                 req.cf.ext7_id = ext7_id;
693
694                 dbg("[%s] rec_index: [%d] msp_num: [%d] cfu_status: [0x%x] " \
695                         "ton: [%d] npi: [%d] cfu_num: [%s] cc2_id: [%d] ext7_id: [%d]",
696                         GET_CP_NAME(invocation), req.cf.rec_index,
697                         req.cf.msp_num, req.cf.cfu_status, req.cf.ton,
698                         req.cf.npi, req.cf.cfu_num, req.cf.cc2_id, req.cf.ext7_id);
699         }
700
701         /* Dispatch request */
702         ret = dtapi_dispatch_request_ex(ctx, sim, invocation,
703                 TREQ_SIM_SET_CALLFORWARDING,
704                 &req, sizeof(struct treq_sim_set_callforwarding));
705         DBUS_SIM_CHECK_DISPATCH_RET(ret, invocation);
706
707         return TRUE;
708 }
709
710 static gboolean on_sim_get_message_waiting(TelephonySim *sim,
711         GDBusMethodInvocation *invocation, gpointer user_data)
712 {
713         struct custom_data *ctx = user_data;
714         CoreObject *co_sim = NULL;
715         TReturn ret;
716         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
717
718         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
719                 return TRUE;
720
721         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
722         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_MESSAGE_WAITING, co_sim);
723         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get Message Waiting");
724         DBUS_SIM_CHECK_SIM_SERVICE_TABLE(invocation, GET_MESSAGE_WAITING, co_sim);
725
726         /* Dispatch request */
727         ret = dtapi_dispatch_request_ex(ctx, sim, invocation,
728                 TREQ_SIM_GET_MESSAGEWAITING,
729                 NULL, 0);
730         DBUS_SIM_CHECK_DISPATCH_RET(ret, invocation);
731
732         return TRUE;
733 }
734
735 static gboolean on_sim_set_message_waiting(TelephonySim *sim,
736         GDBusMethodInvocation *invocation,
737         gboolean  cphs,
738         gint rec_index,
739         guchar indicator_status,
740         gint voice_cnt, gint fax_cnt,
741         gint email_cnt, gint other_cnt,
742         gint video_cnt,
743         gboolean cphs_voice1, gboolean cphs_voice2,
744         gboolean cphs_fax, gboolean cphs_data, gpointer user_data)
745 {
746         struct custom_data *ctx = user_data;
747         TReturn ret;
748         CoreObject *co_sim = NULL;
749         struct treq_sim_set_messagewaiting req;
750         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
751
752         if (!check_access_control(p_cynara, invocation, AC_SIM, "w"))
753                 return TRUE;
754
755         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
756         DBUS_SIM_CHECK_SIM_STATUS(invocation, SET_MESSAGE_WAITING, co_sim);
757         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Set Message Waiting");
758         DBUS_SIM_CHECK_SIM_SERVICE_TABLE(invocation, SET_MESSAGE_WAITING, co_sim);
759
760         memset(&req, 0x0, sizeof(struct treq_sim_set_messagewaiting));
761
762         req.b_cphs = cphs;
763
764         if (req.b_cphs) {
765                 req.cphs_mw.b_voice1 = cphs_voice1;
766                 req.cphs_mw.b_voice2 = cphs_voice2;
767                 req.cphs_mw.b_fax = cphs_fax;
768                 req.cphs_mw.b_data = cphs_data;
769
770                 dbg("[%s] b_voice1: [%d] b_voice2: [%d] b_fax: [%d] b_data:[%d]",
771                         GET_CP_NAME(invocation),
772                         req.cphs_mw.b_voice1, req.cphs_mw.b_voice2,
773                         req.cphs_mw.b_fax, req.cphs_mw.b_data);
774         } else {
775                 req.mw.rec_index = rec_index;
776                 req.mw.indicator_status = indicator_status;
777                 req.mw.voice_count = voice_cnt;
778                 req.mw.fax_count = fax_cnt;
779                 req.mw.email_count = email_cnt;
780                 req.mw.other_count = other_cnt;
781                 req.mw.video_count = video_cnt;
782
783                 dbg("[%s] rec_index: [%d] indicator_status: [0x%x] voice_count: [%d] " \
784                         "fax_count: [%d] email_count: [%d] other_count: [%d] video_count: [%d]",
785                         GET_CP_NAME(invocation),
786                         req.mw.rec_index, req.mw.indicator_status,
787                         req.mw.voice_count, req.mw.fax_count,
788                         req.mw.email_count, req.mw.other_count,
789                         req.mw.video_count);
790         }
791
792         /* Dispatch request */
793         ret = dtapi_dispatch_request_ex(ctx, sim, invocation,
794                 TREQ_SIM_SET_MESSAGEWAITING,
795                 &req, sizeof(struct treq_sim_set_messagewaiting));
796         DBUS_SIM_CHECK_DISPATCH_RET(ret, invocation);
797
798         return TRUE;
799 }
800
801 static gboolean on_sim_get_mailbox(TelephonySim *sim,
802         GDBusMethodInvocation *invocation, gpointer user_data)
803 {
804         struct custom_data *ctx = user_data;
805         TReturn ret;
806         CoreObject *co_sim = NULL;
807         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
808
809         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
810                 return TRUE;
811
812         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
813         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_MAILBOX, co_sim);
814         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get Mailbox");
815         DBUS_SIM_CHECK_SIM_SERVICE_TABLE(invocation, GET_MAILBOX, co_sim);
816
817         /* Dispatch request */
818         ret = dtapi_dispatch_request_ex(ctx, sim, invocation,
819                 TREQ_SIM_GET_MAILBOX,
820                 NULL, 0);
821         DBUS_SIM_CHECK_DISPATCH_RET(ret, invocation);
822
823         return TRUE;
824 }
825
826 static gboolean on_sim_set_mailbox(TelephonySim *sim,
827         GDBusMethodInvocation *invocation,
828         gboolean cphs,
829         gint mb_type, gint rec_index,
830         gint profile_number,
831         gint alpha_id_max_len, const gchar *alpha_id,
832         gint ton, gint npi, const gchar *number,
833         gint cc_id, gint ext1_id, gpointer user_data)
834 {
835         struct custom_data *ctx = user_data;
836         TReturn ret;
837         CoreObject *co_sim = NULL;
838         struct treq_sim_set_mailbox req;
839         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
840
841         if (!check_access_control(p_cynara, invocation, AC_SIM, "w"))
842                 return TRUE;
843
844         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
845         DBUS_SIM_CHECK_SIM_STATUS(invocation, SET_MAILBOX, co_sim);
846         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Set Mailbox");
847         DBUS_SIM_CHECK_SIM_SERVICE_TABLE(invocation, SET_MAILBOX, co_sim);
848
849         memset(&req, 0x0, sizeof(struct treq_sim_set_mailbox));
850
851         req.b_cphs = cphs;
852
853         req.mb_info.mb_type = mb_type;
854         req.mb_info.rec_index = rec_index;
855         req.mb_info.profile_number = profile_number;
856         req.mb_info.number_info.alpha_id_max_len = alpha_id_max_len;
857         if (strlen(alpha_id))
858                 memcpy(&req.mb_info.number_info.alpha_id, alpha_id, strlen(alpha_id));
859         req.mb_info.number_info.ton = ton;
860         req.mb_info.number_info.npi = npi;
861         if (strlen(number))
862                 memcpy(&req.mb_info.number_info.num, number, strlen(number));
863         req.mb_info.number_info.cc_id = cc_id;
864         req.mb_info.number_info.ext1_id = ext1_id;
865
866         dbg("[%s] b_cphs: [%d] mb_type: [%d] rec_index: [%d] " \
867                 "profile_number: [%d] alpha_id_max_len: [%d] alpha_id: [%s] " \
868                 "ton: [%d] npi: [%d] num: [%s] cc_id: [%d] ext1_id: [%d]",
869                 GET_CP_NAME(invocation),
870                 req.b_cphs,
871                 req.mb_info.mb_type, req.mb_info.rec_index,
872                 req.mb_info.profile_number,
873                 req.mb_info.number_info.alpha_id_max_len,
874                 req.mb_info.number_info.alpha_id,
875                 req.mb_info.number_info.ton, req.mb_info.number_info.npi,
876                 req.mb_info.number_info.num,
877                 req.mb_info.number_info.cc_id, req.mb_info.number_info.ext1_id);
878
879         /* Dispatch request */
880         ret = dtapi_dispatch_request_ex(ctx, sim, invocation,
881                 TREQ_SIM_SET_MAILBOX,
882                 &req, sizeof(struct treq_sim_set_mailbox));
883         DBUS_SIM_CHECK_DISPATCH_RET(ret, invocation);
884
885         return TRUE;
886 }
887
888 static gboolean on_sim_get_cphsinfo(TelephonySim *sim,
889         GDBusMethodInvocation *invocation, gpointer user_data)
890 {
891         struct custom_data *ctx = user_data;
892         CoreObject *co_sim = NULL;
893         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
894
895         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
896                 return TRUE;
897
898         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
899         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_CPHS_INFO, co_sim);
900         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get CPHS Info");
901
902         /* Dispatch request */
903         dtapi_dispatch_request(ctx, sim, invocation,
904                 TREQ_SIM_GET_CPHS_INFO,
905                 NULL, 0);
906
907         return TRUE;
908 }
909
910 static gboolean on_sim_get_service_table(TelephonySim *sim,
911         GDBusMethodInvocation *invocation, gpointer user_data)
912 {
913         struct custom_data *ctx = user_data;
914         CoreObject *co_sim = NULL;
915         struct tel_sim_service_table *svct = NULL;
916         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
917
918         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
919                 return TRUE;
920
921         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
922         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_SVCT, co_sim);
923         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get SVCT");
924
925         svct = tcore_sim_get_service_table(co_sim);
926         if (!svct) {
927                 struct custom_data *ctx = user_data;
928
929                 dbg("[%s] NOT cached - Request to modem",
930                         GET_CP_NAME(invocation));
931
932                 /* Dispatch request */
933                 dtapi_dispatch_request(ctx, sim, invocation,
934                         TREQ_SIM_GET_SERVICE_TABLE,
935                         NULL, 0);
936         } else {
937                 GVariantBuilder builder;
938                 GVariant * inner_gv = NULL;
939                 GVariant *svct_gv = NULL;
940                 int i = 0;
941
942                 dbg("[%s] GET_SERVICE_TABLE", GET_CP_NAME(invocation));
943
944                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
945                 if (svct->sim_type == SIM_TYPE_GSM) {
946                         for (i = 0; i < SIM_SST_SERVICE_CNT_MAX; i++)
947                                 g_variant_builder_add(&builder, "y",
948                                         svct->table.sst.service[i]);
949                 } else if (svct->sim_type == SIM_TYPE_USIM) {
950                         for (i = 0; i < SIM_UST_SERVICE_CNT_MAX; i++)
951                                 g_variant_builder_add(&builder, "y",
952                                         svct->table.ust.service[i]);
953                 } else if (svct->sim_type == SIM_TYPE_RUIM) {
954                         if (SIM_CDMA_SVC_TABLE == svct->table.cst.cdma_svc_table) {
955                                 for (i = 0; i < SIM_CDMA_ST_SERVICE_CNT_MAX; i++)
956                                         g_variant_builder_add(&builder, "iy",
957                                                 svct->table.cst.cdma_svc_table,
958                                                 svct->table.cst.service.cdma_service[i]);
959                         } else if (SIM_CSIM_SVC_TABLE == svct->table.cst.cdma_svc_table) {
960                                 for (i = 0; i < SIM_CSIM_ST_SERVICE_CNT_MAX; i++)
961                                         g_variant_builder_add(&builder, "iy",
962                                                 svct->table.cst.cdma_svc_table,
963                                                 svct->table.cst.service.csim_service[i]);
964                         } else {
965                                 err("Invalid cdma_svc_table: [%d]", svct->table.cst.cdma_svc_table);
966                         }
967                 } else {
968                         err("[%s] Unknown SIM type: [%d]", GET_CP_NAME(invocation), svct->sim_type);
969                 }
970                 inner_gv = g_variant_builder_end(&builder);
971                 svct_gv = g_variant_new("v", inner_gv);
972
973                 telephony_sim_complete_get_service_table(sim,
974                         invocation, SIM_ACCESS_SUCCESS, svct->sim_type, svct_gv);
975
976                 free(svct);
977         }
978
979         return TRUE;
980 }
981
982 static gboolean on_sim_get_msisdn(TelephonySim *sim,
983         GDBusMethodInvocation *invocation, gpointer user_data)
984 {
985         struct custom_data *ctx = user_data;
986         CoreObject *co_sim = NULL;
987         struct tel_sim_msisdn_list *msisdn_list = NULL;
988         gboolean read_from_modem = FALSE;
989         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
990
991         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
992                 return TRUE;
993
994         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
995         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_MSISDN, co_sim);
996         DBUS_SIM_CHECK_SIM_SERVICE_TABLE(invocation, GET_MSISDN, co_sim);
997
998         if (SIM_TYPE_NVSIM == tcore_sim_get_type(co_sim)) {
999                 dbg("NV SIM, don't use cached MSISDN");
1000                 read_from_modem = TRUE;
1001         } else {
1002                 msisdn_list = tcore_sim_get_msisdn_list(co_sim);
1003                 if (msisdn_list)
1004                         read_from_modem = FALSE;
1005                 else
1006                         read_from_modem = TRUE;
1007         }
1008
1009         if (read_from_modem) {
1010                 TReturn ret;
1011
1012                 dbg("[%s] NOT cached - Request to modem", GET_CP_NAME(invocation));
1013
1014                 /* Dispatch request */
1015                 ret = dtapi_dispatch_request_ex(ctx, sim, invocation,
1016                         TREQ_SIM_GET_MSISDN,
1017                         NULL, 0);
1018                 DBUS_SIM_CHECK_DISPATCH_RET(ret, invocation);
1019         } else {
1020                 GVariant *gv = NULL;
1021                 int i;
1022                 GVariantBuilder b;
1023
1024                 dbg("[%s] MSISDN count: [%d]",
1025                         GET_CP_NAME(invocation), msisdn_list->count);
1026
1027                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
1028                 for (i = 0; i < msisdn_list->count; i++) {
1029                         g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
1030                         g_variant_builder_add(&b, "{sv}", "name",
1031                                 g_variant_new_string((const gchar *)msisdn_list->msisdn[i].name));
1032                         if (msisdn_list->msisdn[i].ton == SIM_TON_INTERNATIONAL) {
1033                                 unsigned char *tmp = (unsigned char *)calloc(SIM_MSISDN_NUMBER_LEN_MAX + 1, 1);
1034                                 if (tmp != NULL) {
1035                                         tmp[0] = '+';
1036
1037                                         strncpy((char *)tmp+1,
1038                                                 (const char*)msisdn_list->msisdn[i].num,
1039                                                 SIM_MSISDN_NUMBER_LEN_MAX - 1);
1040                                         tmp[SIM_MSISDN_NUMBER_LEN_MAX] = '\0';
1041
1042                                         g_variant_builder_add(&b, "{sv}", "number",
1043                                                 g_variant_new_string((const gchar *)tmp));
1044
1045                                         free(tmp);
1046                                 } else {
1047                                         warn("Memory allocation failed");
1048                                         g_variant_builder_add(&b, "{sv}", "number",
1049                                                 g_variant_new_string((const gchar *)msisdn_list->msisdn[i].num));
1050                                 }
1051                         } else {
1052                                 g_variant_builder_add(&b, "{sv}", "number",
1053                                         g_variant_new_string((const gchar *)msisdn_list->msisdn[i].num));
1054                         }
1055                         g_variant_builder_close(&b);
1056                 }
1057                 gv = g_variant_builder_end(&b);
1058
1059                 telephony_sim_complete_get_msisdn(sim,
1060                         invocation, SIM_ACCESS_SUCCESS, gv);
1061
1062                 g_free(msisdn_list);
1063         }
1064
1065         return TRUE;
1066 }
1067
1068 static gboolean on_sim_get_oplmnwact(TelephonySim *sim,
1069         GDBusMethodInvocation *invocation, gpointer user_data)
1070 {
1071         struct custom_data *ctx = user_data;
1072         CoreObject *co_sim = NULL;
1073         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1074
1075         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1076                 return TRUE;
1077
1078         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1079         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_OPLMWACT, co_sim);
1080         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get OPLMNWACT");
1081
1082         /* Dispatch request */
1083         dtapi_dispatch_request(ctx, sim, invocation,
1084                 TREQ_SIM_GET_OPLMNWACT,
1085                 NULL, 0);
1086
1087         return TRUE;
1088 }
1089
1090 static gboolean on_sim_get_spn(TelephonySim *sim,
1091         GDBusMethodInvocation *invocation, gpointer user_data)
1092 {
1093         struct custom_data *ctx = user_data;
1094         CoreObject *co_sim = NULL;
1095         struct tel_sim_spn* spn = NULL;
1096         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1097
1098         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1099                 return TRUE;
1100
1101         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1102         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_SPN, co_sim);
1103         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get SPN");
1104
1105         spn = tcore_sim_get_spn(co_sim);
1106         if (!spn) {
1107                 dbg("[%s] NOT cached - Request to modem", GET_CP_NAME(invocation));
1108
1109                 /* Dispatch request */
1110                 dtapi_dispatch_request(ctx, sim, invocation,
1111                         TREQ_SIM_GET_SPN,
1112                         NULL, 0);
1113         } else {
1114                 dbg("[%s] Display condition: [%d] SPN: [%s]",
1115                         GET_CP_NAME(invocation),
1116                         spn->display_condition, (const gchar *)spn->spn);
1117
1118                 telephony_sim_complete_get_spn(sim, invocation, SIM_ACCESS_SUCCESS,
1119                         spn->display_condition, (const gchar *)spn->spn);
1120
1121                 g_free(spn);
1122         }
1123         return TRUE;
1124 }
1125
1126 static gboolean on_sim_get_cphs_netname(TelephonySim *sim,
1127         GDBusMethodInvocation *invocation, gpointer user_data)
1128 {
1129         struct custom_data *ctx = user_data;
1130         CoreObject *co_sim = NULL;
1131         struct tel_sim_cphs_netname *cphs_netname = NULL;
1132         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1133
1134         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1135                 return TRUE;
1136
1137         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1138         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_CPHS_NET_NAME, co_sim);
1139         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get CPHS Net Name");
1140
1141         cphs_netname = tcore_sim_get_cphs_netname(co_sim);
1142         if (!cphs_netname) {
1143                 dbg("[%s] NOT cached - Request to modem", GET_CP_NAME(invocation));
1144
1145                 /* Dispatch request */
1146                 dtapi_dispatch_request(ctx, sim, invocation,
1147                         TREQ_SIM_GET_CPHS_NETNAME,
1148                         NULL, 0);
1149         } else {
1150                 dbg("[%s] Full name: [%s] Short name: [%s]",
1151                         GET_CP_NAME(invocation),
1152                         (const gchar *)cphs_netname->full_name,
1153                         (const gchar *)cphs_netname->short_name);
1154
1155                 telephony_sim_complete_get_cphs_net_name(sim,
1156                         invocation, SIM_ACCESS_SUCCESS,
1157                         (const gchar *)cphs_netname->full_name,
1158                         (const gchar *)cphs_netname->short_name);
1159
1160                 g_free(cphs_netname);
1161         }
1162
1163         return TRUE;
1164 }
1165
1166 static gboolean on_sim_get_gid(TelephonySim *sim,
1167         GDBusMethodInvocation *invocation, gpointer user_data)
1168 {
1169         struct custom_data *ctx = user_data;
1170         CoreObject *co_sim = NULL;
1171         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1172
1173         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1174                 return TRUE;
1175
1176         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1177         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_GID, co_sim);
1178         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get GID");
1179
1180         /* Dispatch request */
1181         dtapi_dispatch_request(ctx, sim, invocation,
1182                 TREQ_SIM_GET_GID,
1183                 NULL, 0);
1184
1185         return TRUE;
1186 }
1187
1188 static gboolean on_sim_authentication(TelephonySim *sim,
1189         GDBusMethodInvocation *invocation, gint auth_type,
1190         GVariant *rand, GVariant *autn, gpointer user_data)
1191 {
1192         struct custom_data *ctx = user_data;
1193         GVariantIter *iter = NULL;
1194         GVariant *rand_gv = NULL;
1195         GVariant *autn_gv = NULL;
1196         guchar rt_i;
1197         int i = 0;
1198         TReturn ret;
1199         CoreObject *co_sim = NULL;
1200         struct treq_sim_req_authentication req;
1201         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1202
1203         if (!check_access_control(p_cynara, invocation, AC_SIM, "x"))
1204                 return TRUE;
1205
1206         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1207         DBUS_SIM_CHECK_SIM_STATUS(invocation, AUTHENTICATION, co_sim);
1208
1209         memset(&req, 0x0, sizeof(struct treq_sim_req_authentication));
1210
1211         req.auth_type = auth_type;
1212
1213         rand_gv = g_variant_get_variant(rand);
1214         g_variant_get(rand_gv, "ay", &iter);
1215         while (g_variant_iter_loop(iter, "y", &rt_i)) {
1216                 req.rand_data[i] = rt_i;
1217                 i++;
1218         }
1219         g_variant_iter_free(iter);
1220         g_variant_unref(rand_gv);
1221
1222         req.rand_length = (unsigned int)i;
1223
1224         i = 0;
1225         autn_gv = g_variant_get_variant(autn);
1226         g_variant_get(autn_gv, "ay", &iter);
1227         while (g_variant_iter_loop(iter, "y", &rt_i)) {
1228                 req.autn_data[i] = rt_i;
1229                 i++;
1230         }
1231         g_variant_iter_free(iter);
1232         g_variant_unref(autn_gv);
1233
1234         req.autn_length = (unsigned int)i;
1235
1236         dbg("[%s] Authentication - type: [0x%02x] RAND len: [%d] AUTN len: [%d]",
1237                 GET_CP_NAME(invocation),
1238                 req.auth_type, req.rand_length, req.autn_length);
1239
1240         /* Dispatch request */
1241         ret = dtapi_dispatch_request_ex(ctx, sim, invocation,
1242                 TREQ_SIM_REQ_AUTHENTICATION,
1243                 &req, sizeof(struct treq_sim_req_authentication));
1244         if (ret != TCORE_RETURN_SUCCESS) {
1245                 GVariantBuilder builder;
1246                 GVariant *ak = NULL;
1247                 GVariant *cp = NULL;
1248                 GVariant *it = NULL;
1249                 GVariant *resp = NULL;
1250                 GVariant *ak_gv = NULL;
1251                 GVariant *cp_gv = NULL;
1252                 GVariant *it_gv = NULL;
1253                 GVariant *resp_gv = NULL;
1254
1255                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
1256                 ak = g_variant_builder_end(&builder);
1257                 ak_gv = g_variant_new("v", ak);
1258
1259                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
1260                 cp = g_variant_builder_end(&builder);
1261                 cp_gv = g_variant_new("v", cp);
1262
1263                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
1264                 it = g_variant_builder_end(&builder);
1265                 it_gv = g_variant_new("v", it);
1266
1267                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
1268                 resp = g_variant_builder_end(&builder);
1269                 resp_gv = g_variant_new("v", resp);
1270
1271                 telephony_sim_complete_authentication(sim,
1272                         invocation, SIM_ACCESS_FAILED,
1273                         0, 0, ak_gv, cp_gv, it_gv, resp_gv);
1274         }
1275
1276         return TRUE;
1277 }
1278
1279 static gboolean on_sim_verify_sec(TelephonySim *sim,
1280         GDBusMethodInvocation *invocation,
1281         gint pin_type, const gchar *pin, gpointer user_data)
1282 {
1283         struct custom_data *ctx = user_data;
1284         CoreObject *co_sim = NULL;
1285         struct treq_sim_verify_pins req;
1286         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1287
1288         if (!check_access_control(p_cynara, invocation, AC_SIM, "x"))
1289                 return TRUE;
1290
1291         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1292         DBUS_SIM_CHECK_SIM_STATUS(invocation, VERIFY_SEC, co_sim);
1293         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Verify Sec");
1294
1295         memset(&req, 0x0, sizeof(struct treq_sim_verify_pins));
1296
1297         req.pin_type = pin_type;
1298         req.pin_length = strlen(pin);
1299         memcpy(req.pin, pin, req.pin_length);
1300
1301         dbg("[%s] PIN - type: [0x%02x] len: [%d]",
1302                 GET_CP_NAME(invocation),
1303                 req.pin_type, req.pin_length);
1304
1305         /* Dispatch request */
1306         dtapi_dispatch_request(ctx, sim, invocation,
1307                 TREQ_SIM_VERIFY_PINS,
1308                 &req, sizeof(struct treq_sim_verify_pins));
1309
1310         return TRUE;
1311 }
1312
1313 static gboolean on_sim_verify_puk(TelephonySim *sim,
1314         GDBusMethodInvocation *invocation,
1315         gint puk_type, const gchar *puk,
1316         const gchar *new_pin, gpointer user_data)
1317 {
1318         struct custom_data *ctx = user_data;
1319         CoreObject *co_sim = NULL;
1320         struct treq_sim_verify_puks req;
1321         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1322
1323         if (!check_access_control(p_cynara, invocation, AC_SIM, "x"))
1324                 return TRUE;
1325
1326         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1327         DBUS_SIM_CHECK_SIM_STATUS(invocation, VERIFY_PUK, co_sim);
1328         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Verify PUK");
1329
1330         memset(&req, 0x0, sizeof(struct treq_sim_verify_puks));
1331
1332         req.puk_type = puk_type;
1333         req.puk_length = strlen(puk);
1334         memcpy(req.puk, puk, req.puk_length);
1335         req.pin_length = strlen(new_pin);
1336         memcpy(req.pin, new_pin, req.pin_length);
1337
1338         dbg("[%s] PUK - type: [0x%02x] len: [%d] PIN len: [%d]",
1339                 GET_CP_NAME(invocation), req.puk_type,
1340                 req.puk_length, req.pin_length);
1341
1342         /* Dispatch request */
1343         dtapi_dispatch_request(ctx, sim, invocation,
1344                 TREQ_SIM_VERIFY_PUKS,
1345                 &req, sizeof(struct treq_sim_verify_puks));
1346
1347         return TRUE;
1348 }
1349
1350 static gboolean on_sim_change_pin(TelephonySim *sim,
1351         GDBusMethodInvocation *invocation,
1352         gint pin_type, const gchar *old_pin, const gchar *new_pin,
1353         gpointer user_data)
1354 {
1355         struct custom_data *ctx = user_data;
1356         CoreObject *co_sim = NULL;
1357         struct treq_sim_change_pins req;
1358         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1359
1360         if (!check_access_control(p_cynara, invocation, AC_SIM, "x"))
1361                 return TRUE;
1362
1363         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1364         DBUS_SIM_CHECK_SIM_STATUS(invocation, CHANGE_PIN, co_sim);
1365         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Change PIN");
1366
1367         memset(&req, 0x0, sizeof(struct treq_sim_change_pins));
1368
1369         req.type = pin_type;
1370         req.old_pin_length = strlen(old_pin);
1371         memcpy(req.old_pin, old_pin, req.old_pin_length);
1372         req.new_pin_length = strlen(new_pin);
1373         memcpy(req.new_pin, new_pin, req.new_pin_length);
1374
1375         dbg("[%s] PIN - type: [0x%02x] Old PIN len: [%d] New PIN len: [%d]",
1376                 GET_CP_NAME(invocation), req.type,
1377                 req.old_pin_length, req.new_pin_length);
1378
1379         /* Dispatch request */
1380         dtapi_dispatch_request(ctx, sim, invocation,
1381                 TREQ_SIM_CHANGE_PINS,
1382                 &req, sizeof(struct treq_sim_change_pins));
1383
1384         return TRUE;
1385 }
1386
1387 static gboolean on_sim_disable_facility(TelephonySim *sim,
1388         GDBusMethodInvocation *invocation,
1389         gint facility_type, const gchar *password, gpointer user_data)
1390 {
1391         struct custom_data *ctx = user_data;
1392         struct treq_sim_disable_facility req;
1393         CoreObject *co_sim = NULL;
1394         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1395
1396         if (!check_access_control(p_cynara, invocation, AC_SIM, "x"))
1397                 return TRUE;
1398
1399         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1400         DBUS_SIM_CHECK_SIM_STATUS(invocation, DISABLE_FACILITY, co_sim);
1401         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Diable Facility");
1402
1403         memset(&req, 0x0, sizeof(struct treq_sim_disable_facility));
1404
1405         dbg("[%s] facility_type: [%d]", GET_CP_NAME(invocation), facility_type);
1406         switch (facility_type) {
1407         case 1:
1408                 req.type = SIM_FACILITY_PS;
1409         break;
1410
1411         case 3:
1412                 req.type = SIM_FACILITY_SC;
1413         break;
1414
1415         case 4:
1416                 req.type = SIM_FACILITY_FD;
1417         break;
1418
1419         case 5:
1420                 req.type = SIM_FACILITY_PN;
1421         break;
1422
1423         case 6:
1424                 req.type = SIM_FACILITY_PU;
1425         break;
1426
1427         case 7:
1428                 req.type = SIM_FACILITY_PP;
1429         break;
1430
1431         case 8:
1432                 req.type = SIM_FACILITY_PC;
1433         break;
1434
1435         default:
1436                 err("Unhandled/Unknown Facility type: [0x%x]", facility_type);
1437
1438                 FAIL_RESPONSE(invocation, DEFAULT_MSG_REQ_FAILED);
1439
1440                 return TRUE;
1441         }
1442
1443         req.password_length = strlen(password);
1444         memcpy(req.password, password, req.password_length);
1445
1446         dbg("[%s] Facility - type: [0x%02x] Passowrd len: [%d]",
1447                 GET_CP_NAME(invocation),
1448                 req.type, req.password_length);
1449
1450         /* Dispatch request */
1451         dtapi_dispatch_request(ctx, sim, invocation,
1452                 TREQ_SIM_DISABLE_FACILITY,
1453                 &req, sizeof(struct treq_sim_disable_facility));
1454
1455         return TRUE;
1456 }
1457
1458 static gboolean on_sim_enable_facility(TelephonySim *sim, GDBusMethodInvocation *invocation,
1459             gint facility_type,
1460             const gchar *password,
1461                 gpointer user_data)
1462 {
1463         struct custom_data *ctx = user_data;
1464         struct treq_sim_enable_facility req;
1465         CoreObject *co_sim = NULL;
1466         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1467
1468         if (!check_access_control(p_cynara, invocation, AC_SIM, "x"))
1469                 return TRUE;
1470
1471         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1472         DBUS_SIM_CHECK_SIM_STATUS(invocation, ENABLE_FACILITY, co_sim);
1473         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Enable Facility");
1474
1475         memset(&req, 0x0, sizeof(struct treq_sim_enable_facility));
1476
1477         dbg("[%s] facility_type: [%d]", GET_CP_NAME(invocation), facility_type);
1478         switch (facility_type) {
1479         case 1:
1480                 req.type = SIM_FACILITY_PS;
1481         break;
1482
1483         case 3:
1484                 req.type = SIM_FACILITY_SC;
1485         break;
1486
1487         case 4:
1488                 req.type = SIM_FACILITY_FD;
1489         break;
1490
1491         case 5:
1492                 req.type = SIM_FACILITY_PN;
1493         break;
1494
1495         case 6:
1496                 req.type = SIM_FACILITY_PU;
1497         break;
1498
1499         case 7:
1500                 req.type = SIM_FACILITY_PP;
1501         break;
1502
1503         case 8:
1504                 req.type = SIM_FACILITY_PC;
1505         break;
1506
1507         default:
1508                 err("Unhandled/Unknown Facility type: [0x%x]", facility_type);
1509
1510                 FAIL_RESPONSE(invocation, DEFAULT_MSG_REQ_FAILED);
1511
1512                 return TRUE;
1513         }
1514
1515         req.password_length = strlen(password);
1516         memcpy(req.password, password, req.password_length);
1517
1518         dbg("[%s] Facility - type: [0x%02x] Passowrd len: [%d]",
1519                 GET_CP_NAME(invocation),
1520                 req.type, req.password_length);
1521
1522         /* Dispatch request */
1523         dtapi_dispatch_request(ctx, sim, invocation,
1524                 TREQ_SIM_ENABLE_FACILITY,
1525                 &req, sizeof(struct treq_sim_enable_facility));
1526
1527         return TRUE;
1528 }
1529
1530 static gboolean on_sim_get_facility(TelephonySim *sim,
1531         GDBusMethodInvocation *invocation,
1532         gint facility_type, gpointer user_data)
1533 {
1534         struct custom_data *ctx = user_data;
1535         struct treq_sim_get_facility_status req;
1536         CoreObject *co_sim = NULL;
1537         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1538
1539         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1540                 return TRUE;
1541
1542         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1543         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_FACILITY, co_sim);
1544         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get Facility");
1545
1546         memset(&req, 0x0, sizeof(struct treq_sim_get_facility_status));
1547
1548         dbg("[%s] facility_type: [%d]", GET_CP_NAME(invocation), facility_type);
1549
1550         switch (facility_type) {
1551         case 1:
1552                 req.type = SIM_FACILITY_PS;
1553         break;
1554
1555         case 3:
1556                 req.type = SIM_FACILITY_SC;
1557         break;
1558
1559         case 4:
1560                 req.type = SIM_FACILITY_FD;
1561         break;
1562
1563         case 5:
1564                 req.type = SIM_FACILITY_PN;
1565         break;
1566
1567         case 6:
1568                 req.type = SIM_FACILITY_PU;
1569         break;
1570
1571         case 7:
1572                 req.type = SIM_FACILITY_PP;
1573         break;
1574
1575         case 8:
1576                 req.type = SIM_FACILITY_PC;
1577         break;
1578
1579         default:
1580                 err("Unhandled/Unknown Facility type: [0x%x]", facility_type);
1581
1582                 FAIL_RESPONSE(invocation, DEFAULT_MSG_REQ_FAILED);
1583
1584                 return TRUE;
1585         }
1586
1587         /* Dispatch request */
1588         dtapi_dispatch_request(ctx, sim, invocation,
1589                 TREQ_SIM_GET_FACILITY_STATUS,
1590                 &req, sizeof(struct treq_sim_get_facility_status));
1591
1592         return TRUE;
1593 }
1594
1595 static gboolean on_sim_get_lock_info(TelephonySim *sim,
1596         GDBusMethodInvocation *invocation,
1597         gint facility_type, gpointer user_data)
1598 {
1599         struct custom_data *ctx = user_data;
1600         CoreObject *co_sim = NULL;
1601         struct treq_sim_get_lock_info req;
1602         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1603
1604         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1605                 return TRUE;
1606
1607         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1608         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_LOCK_INFO, co_sim);
1609         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get Lock Info");
1610
1611         memset(&req, 0x0, sizeof(struct treq_sim_get_lock_info));
1612
1613         dbg("[%s] facility_type: [%d]", GET_CP_NAME(invocation), facility_type);
1614         switch (facility_type) {
1615         case 1:
1616                 req.type = SIM_FACILITY_PS;
1617         break;
1618
1619         case 3:
1620                 req.type = SIM_FACILITY_SC;
1621         break;
1622
1623         case 4:
1624                 req.type = SIM_FACILITY_FD;
1625         break;
1626
1627         case 5:
1628                 req.type = SIM_FACILITY_PN;
1629         break;
1630
1631         case 6:
1632                 req.type = SIM_FACILITY_PU;
1633         break;
1634
1635         case 7:
1636                 req.type = SIM_FACILITY_PP;
1637         break;
1638
1639         case 8:
1640                 req.type = SIM_FACILITY_PC;
1641         break;
1642
1643         default:
1644                 err("Unhandled/Unknown Facility type: [0x%x]", facility_type);
1645
1646                 FAIL_RESPONSE(invocation, DEFAULT_MSG_REQ_FAILED);
1647
1648                 return TRUE;
1649         }
1650
1651         /* Dispatch request */
1652         dtapi_dispatch_request(ctx, sim, invocation,
1653                 TREQ_SIM_GET_LOCK_INFO,
1654                 &req, sizeof(struct treq_sim_get_lock_info));
1655
1656         return TRUE;
1657 }
1658
1659 static gboolean on_sim_transfer_apdu(TelephonySim *sim,
1660         GDBusMethodInvocation *invocation,
1661         GVariant *apdu, gpointer user_data)
1662 {
1663         struct custom_data *ctx = user_data;
1664         struct treq_sim_transmit_apdu req;
1665         GVariantIter *iter = NULL;
1666         GVariant *inner_gv = NULL;
1667         guchar rt_i;
1668         CoreObject *co_sim = NULL;
1669         int i = 0;
1670         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1671
1672         if (!check_access_control(p_cynara, invocation, AC_SIM, "x"))
1673                 return TRUE;
1674
1675         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1676         DBUS_SIM_CHECK_SIM_STATUS(invocation, TRANSFER_APDU, co_sim);
1677         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Transfer APDU");
1678
1679         memset(&req, 0x0, sizeof(struct treq_sim_transmit_apdu));
1680
1681         inner_gv = g_variant_get_variant(apdu);
1682
1683         g_variant_get(inner_gv, "ay", &iter);
1684         while (g_variant_iter_loop(iter, "y", &rt_i)) {
1685                 req.apdu[i] = rt_i;
1686                 i++;
1687         }
1688         req.apdu_length = (unsigned int)i;
1689         g_variant_iter_free(iter);
1690         g_variant_unref(inner_gv);
1691         g_variant_unref(apdu);
1692
1693         tcore_util_hex_dump("[APDU_REQ] ", req.apdu_length, req.apdu);
1694
1695         /* Dispatch request */
1696         dtapi_dispatch_request(ctx, sim, invocation,
1697                 TREQ_SIM_TRANSMIT_APDU,
1698                 &req, sizeof(struct treq_sim_transmit_apdu));
1699
1700         return TRUE;
1701 }
1702
1703 static gboolean on_sim_get_atr(TelephonySim *sim,
1704         GDBusMethodInvocation *invocation, gpointer user_data)
1705 {
1706         struct custom_data *ctx = user_data;
1707         CoreObject *co_sim = NULL;
1708         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1709
1710         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1711                 return TRUE;
1712
1713         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1714         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_ATR, co_sim);
1715         DBUS_SIM_CHECK_SIM_TYPE(co_sim, "Get ATR");
1716
1717         /* Dispatch request */
1718         dtapi_dispatch_request(ctx, sim, invocation,
1719                 TREQ_SIM_GET_ATR,
1720                 NULL, 0);
1721
1722         return TRUE;
1723 }
1724
1725 static gboolean on_sim_get_fields(TelephonySim *sim,
1726         GDBusMethodInvocation *invocation, gpointer user_data)
1727 {
1728         struct custom_data *ctx = user_data;
1729         struct tel_sim_imsi *n_imsi = NULL;
1730         CoreObject *co_sim = NULL;
1731         GVariantBuilder b;
1732         GVariant *gv_fields = NULL;
1733         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1734
1735         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1736                 return TRUE;
1737
1738         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1739
1740         g_variant_builder_init(&b, G_VARIANT_TYPE("a{svv}}"));
1741
1742         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_IMSI, co_sim);
1743
1744         n_imsi = tcore_sim_get_imsi(co_sim);
1745         if (n_imsi != NULL) {
1746                 g_variant_builder_add(&b, "{svv}", "imsi",
1747                         g_variant_new_string("plmn"),
1748                         g_variant_new_string(n_imsi->plmn));
1749                 g_variant_builder_add(&b, "{svv}", "imsi",
1750                         g_variant_new_string("msin"),
1751                         g_variant_new_string(n_imsi->msin));
1752                 free(n_imsi);
1753         }
1754
1755         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_ICCID, co_sim);
1756         g_variant_builder_add(&b, "{svv}", "iccid",
1757                 g_variant_new_string(""), g_variant_new_string(""));
1758
1759         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_MSISDN, co_sim);
1760         g_variant_builder_add(&b, "{svv}", "msisdn",
1761                 g_variant_new_string("name"), g_variant_new_string("number"));
1762
1763         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_SPN, co_sim);
1764         g_variant_builder_add(&b, "{svv}", "spn",
1765                 g_variant_new_uint16(255), g_variant_new_string("network name"));
1766
1767         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_INIT_STATUS, co_sim);
1768         g_variant_builder_add(&b, "{svv}", "init_status",
1769                 g_variant_new_uint16(0), g_variant_new_boolean(TRUE));
1770
1771         gv_fields = g_variant_builder_end(&b);
1772
1773         telephony_sim_complete_get_fields(sim,
1774                 invocation, 0, gv_fields);
1775
1776         return TRUE;
1777 }
1778
1779 static gboolean on_sim_set_power_state(TelephonySim *sim,
1780         GDBusMethodInvocation *invocation, gint state, gpointer user_data)
1781 {
1782         struct custom_data *ctx = user_data;
1783         CoreObject *co_sim = NULL;
1784         struct treq_sim_set_powerstate req;
1785         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1786
1787         if (!check_access_control(p_cynara, invocation, AC_SIM, "w"))
1788                 return TRUE;
1789
1790         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1791         DBUS_SIM_CHECK_SIM_STATUS(invocation, SET_POWERSTATE, co_sim);
1792
1793         memset(&req, 0x0, sizeof(struct treq_sim_set_powerstate));
1794
1795         req.state = state;
1796
1797         dbg("[%s] SIM Power state: [%d]",
1798                 GET_CP_NAME(invocation), req.state);
1799
1800         /* Dispatch request */
1801         dtapi_dispatch_request(ctx, sim, invocation,
1802                 TREQ_SIM_SET_POWERSTATE,
1803                 &req, sizeof(struct treq_sim_set_powerstate));
1804
1805         return TRUE;
1806 }
1807
1808 static gboolean on_sim_get_impi(TelephonySim *sim,
1809         GDBusMethodInvocation *invocation, gpointer user_data)
1810 {
1811         struct custom_data *ctx = user_data;
1812         CoreObject *co_sim = NULL;
1813         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1814
1815         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1816                 return TRUE;
1817
1818         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1819         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_IMPI, co_sim);
1820
1821         /* Dispatch request */
1822         dtapi_dispatch_request(ctx, sim, invocation,
1823                 TREQ_SIM_GET_IMPI,
1824                 NULL, 0);
1825
1826         return TRUE;
1827 }
1828
1829 static gboolean on_sim_get_impu(TelephonySim *sim,
1830         GDBusMethodInvocation *invocation, gpointer user_data)
1831 {
1832         struct custom_data *ctx = user_data;
1833         CoreObject *co_sim = NULL;
1834         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1835
1836         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1837                 return TRUE;
1838
1839         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1840         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_IMPU, co_sim);
1841
1842         /* Dispatch request */
1843         dtapi_dispatch_request(ctx, sim, invocation,
1844                 TREQ_SIM_GET_IMPU,
1845                 NULL, 0);
1846
1847         return TRUE;
1848 }
1849
1850 static gboolean on_sim_get_domain(TelephonySim *sim,
1851         GDBusMethodInvocation *invocation, gpointer user_data)
1852 {
1853         struct custom_data *ctx = user_data;
1854         CoreObject *co_sim = NULL;
1855         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1856
1857         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1858                 return TRUE;
1859
1860         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1861         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_DOMAIN, co_sim);
1862
1863         /* Dispatch request */
1864         dtapi_dispatch_request(ctx, sim, invocation,
1865                 TREQ_SIM_GET_DOMAIN,
1866                 NULL, 0);
1867
1868         return TRUE;
1869 }
1870
1871 static gboolean on_sim_get_pcscf(TelephonySim *sim,
1872         GDBusMethodInvocation *invocation, gpointer user_data)
1873 {
1874         struct custom_data *ctx = user_data;
1875         CoreObject *co_sim = NULL;
1876         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1877
1878         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1879                 return TRUE;
1880
1881         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1882         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_PCSCF, co_sim);
1883
1884         /* Dispatch request */
1885         dtapi_dispatch_request(ctx, sim, invocation,
1886                 TREQ_SIM_GET_PCSCF,
1887                 NULL, 0);
1888
1889         return TRUE;
1890 }
1891
1892 static gboolean on_sim_get_app_list(TelephonySim *sim,
1893         GDBusMethodInvocation *invocation, gpointer user_data)
1894 {
1895         struct custom_data *ctx = user_data;
1896         CoreObject *co_sim = NULL;
1897         unsigned char app_list = 0;
1898         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1899
1900         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1901                 return TRUE;
1902
1903         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1904         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_APP_LIST, co_sim);
1905
1906         app_list = tcore_sim_get_app_list(co_sim);
1907
1908         telephony_sim_complete_get_app_list(sim,
1909                 invocation, app_list);
1910
1911         return TRUE;
1912 }
1913
1914 static gboolean on_sim_get_isim_service_table(TelephonySim *sim,
1915         GDBusMethodInvocation *invocation, gpointer user_data)
1916 {
1917         struct custom_data *ctx = user_data;
1918         struct tel_sim_ist *ist = NULL;
1919         CoreObject *co_sim = NULL;
1920         cynara *p_cynara = (ctx) ? ctx->p_cynara : NULL;
1921
1922         if (!check_access_control(p_cynara, invocation, AC_SIM, "r"))
1923                 return TRUE;
1924
1925         DBUS_SIM_GET_COSIM(invocation, co_sim, ctx->server);
1926         DBUS_SIM_CHECK_SIM_STATUS(invocation, GET_ISIM_SERVICE_TABLE, co_sim);
1927
1928         ist = tcore_sim_get_isim_service_table(co_sim);
1929         if (ist) {
1930                 GVariantBuilder builder;
1931                 GVariant *ist_gv = NULL;
1932                 GVariant *inner_gv = NULL;
1933                 int i;
1934
1935                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
1936                 for (i = 0; i < SIM_IST_SERVICE_CNT_MAX; i++)
1937                         g_variant_builder_add(&builder, "y", ist->service[i]);
1938                 inner_gv = g_variant_builder_end(&builder);
1939                 ist_gv = g_variant_new("v", inner_gv);
1940
1941                 telephony_sim_complete_get_isim_service_table(sim, invocation,
1942                         SIM_ACCESS_SUCCESS, ist_gv);
1943
1944                 g_free(ist);
1945         } else {
1946                 /* Dispatch request */
1947                 dtapi_dispatch_request(ctx, sim, invocation,
1948                         TREQ_SIM_GET_ISIM_SERVICE_TABLE,
1949                         NULL, 0);
1950         }
1951
1952         return TRUE;
1953 }
1954
1955 gboolean dbus_plugin_setup_sim_interface(TelephonyObjectSkeleton *object,
1956         struct custom_data *ctx)
1957 {
1958         TelephonySim *sim;
1959
1960         sim = telephony_sim_skeleton_new();
1961         telephony_object_skeleton_set_sim(object, sim);
1962         g_object_unref(sim);
1963
1964         dbg("sim = %p", sim);
1965
1966         /*
1967          * Register signal handlers for SIM interface
1968          */
1969         g_signal_connect(sim,
1970                 "handle-get-init-status",
1971                 G_CALLBACK(on_sim_get_init_status), ctx);
1972
1973         g_signal_connect(sim,
1974                 "handle-get-card-type",
1975                 G_CALLBACK(on_sim_get_card_type), ctx);
1976
1977         g_signal_connect(sim,
1978                 "handle-get-imsi",
1979                 G_CALLBACK(on_sim_get_imsi), ctx);
1980
1981         g_signal_connect(sim,
1982                 "handle-get-ecc",
1983                 G_CALLBACK(on_sim_get_ecc), ctx);
1984
1985         g_signal_connect(sim,
1986                 "handle-get-iccid",
1987                 G_CALLBACK(on_sim_get_iccid), ctx);
1988
1989         g_signal_connect(sim,
1990                 "handle-get-language",
1991                 G_CALLBACK(on_sim_get_language), ctx);
1992
1993         g_signal_connect(sim,
1994                 "handle-set-language",
1995                 G_CALLBACK(on_sim_set_language), ctx);
1996
1997         g_signal_connect(sim,
1998                 "handle-get-call-forwarding",
1999                 G_CALLBACK(on_sim_get_call_forwarding), ctx);
2000
2001         g_signal_connect(sim,
2002                 "handle-set-call-forwarding",
2003                 G_CALLBACK(on_sim_set_call_forwarding), ctx);
2004
2005         g_signal_connect(sim,
2006                 "handle-get-message-waiting",
2007                 G_CALLBACK(on_sim_get_message_waiting), ctx);
2008
2009         g_signal_connect(sim,
2010                 "handle-set-message-waiting",
2011                 G_CALLBACK(on_sim_set_message_waiting), ctx);
2012
2013         g_signal_connect(sim,
2014                 "handle-get-mailbox",
2015                 G_CALLBACK(on_sim_get_mailbox), ctx);
2016
2017         g_signal_connect(sim,
2018                 "handle-set-mailbox",
2019                 G_CALLBACK(on_sim_set_mailbox), ctx);
2020
2021         g_signal_connect(sim,
2022                 "handle-get-cphsinfo",
2023                 G_CALLBACK(on_sim_get_cphsinfo), ctx);
2024
2025         g_signal_connect(sim,
2026                 "handle-get-service-table",
2027                 G_CALLBACK(on_sim_get_service_table), ctx);
2028
2029         g_signal_connect(sim,
2030                 "handle-get-msisdn",
2031                 G_CALLBACK(on_sim_get_msisdn), ctx);
2032
2033         g_signal_connect(sim,
2034                 "handle-get-oplmnwact",
2035                 G_CALLBACK(on_sim_get_oplmnwact), ctx);
2036
2037         g_signal_connect(sim,
2038                 "handle-get-spn",
2039                 G_CALLBACK(on_sim_get_spn), ctx);
2040
2041         g_signal_connect(sim,
2042                 "handle-get-cphs-net-name",
2043                 G_CALLBACK(on_sim_get_cphs_netname), ctx);
2044
2045         g_signal_connect(sim,
2046                 "handle-get-gid",
2047                 G_CALLBACK(on_sim_get_gid), ctx);
2048
2049         g_signal_connect(sim,
2050                 "handle-authentication",
2051                 G_CALLBACK(on_sim_authentication), ctx);
2052
2053         g_signal_connect(sim,
2054                 "handle-verify-sec",
2055                 G_CALLBACK(on_sim_verify_sec), ctx);
2056
2057         g_signal_connect(sim,
2058                 "handle-verify-puk",
2059                 G_CALLBACK(on_sim_verify_puk), ctx);
2060
2061         g_signal_connect(sim,
2062                 "handle-change-pin",
2063                 G_CALLBACK(on_sim_change_pin), ctx);
2064
2065         g_signal_connect(sim,
2066                 "handle-disable-facility",
2067                 G_CALLBACK(on_sim_disable_facility), ctx);
2068
2069         g_signal_connect(sim,
2070                 "handle-enable-facility",
2071                 G_CALLBACK(on_sim_enable_facility), ctx);
2072
2073         g_signal_connect(sim,
2074                 "handle-get-facility",
2075                 G_CALLBACK(on_sim_get_facility), ctx);
2076
2077         g_signal_connect(sim,
2078                 "handle-get-lock-info",
2079                 G_CALLBACK(on_sim_get_lock_info), ctx);
2080
2081         g_signal_connect(sim,
2082                 "handle-transfer-apdu",
2083                 G_CALLBACK(on_sim_transfer_apdu), ctx);
2084
2085         g_signal_connect(sim,
2086                 "handle-get-atr",
2087                 G_CALLBACK(on_sim_get_atr), ctx);
2088
2089         g_signal_connect(sim,
2090                 "handle-get-fields",
2091                 G_CALLBACK(on_sim_get_fields), ctx);
2092
2093         g_signal_connect(sim,
2094                 "handle-set-powerstate",
2095                 G_CALLBACK(on_sim_set_power_state), ctx);
2096
2097         g_signal_connect(sim,
2098                 "handle-get-impi",
2099                 G_CALLBACK(on_sim_get_impi), ctx);
2100
2101         g_signal_connect(sim,
2102                 "handle-get-impu",
2103                 G_CALLBACK(on_sim_get_impu), ctx);
2104
2105         g_signal_connect(sim,
2106                 "handle-get-domain",
2107                 G_CALLBACK(on_sim_get_domain), ctx);
2108
2109         g_signal_connect(sim,
2110                 "handle-get-pcscf",
2111                 G_CALLBACK(on_sim_get_pcscf), ctx);
2112
2113         g_signal_connect(sim,
2114                 "handle-get-app-list",
2115                 G_CALLBACK(on_sim_get_app_list), ctx);
2116
2117         g_signal_connect(sim,
2118                 "handle-get-isim-service-table",
2119                 G_CALLBACK(on_sim_get_isim_service_table), ctx);
2120
2121         /*
2122          * Initialize DBUS property
2123          */
2124         telephony_sim_set_cf_state(sim, FALSE);
2125
2126         return TRUE;
2127 }
2128
2129 gboolean dbus_plugin_sim_response(struct custom_data *ctx,
2130         UserRequest *ur, struct dbus_request_info *dbus_info,
2131         enum tcore_response_command command, unsigned int data_len, const void *data)
2132 {
2133         char *cpname = dbus_info ? GET_CP_NAME(dbus_info->invocation) : "";
2134
2135         switch (command) {
2136         case TRESP_SIM_GET_ECC: {
2137                 const struct tresp_sim_read *resp_read = data;
2138                 CoreObject *co_sim = NULL;
2139                 GVariant *gv = NULL;
2140                 GVariantBuilder b;
2141                 int i = 0;
2142
2143                 dbg("[%s] SIM_GET_ECC - Result: [%s])", cpname,
2144                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2145
2146                 co_sim = __get_sim_co_from_ur(ctx->server, ur);
2147                 if (!co_sim) {
2148                         err("SIM Core object is NULL");
2149                         return FALSE;
2150                 }
2151
2152                 if (resp_read->result == SIM_ACCESS_SUCCESS)
2153                         tcore_sim_set_ecc_list(co_sim, &resp_read->data.ecc);
2154                 else if (resp_read->result == SIM_ACCESS_FILE_NOT_FOUND)
2155                         tcore_sim_set_ecc_list(co_sim, NULL);
2156
2157                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2158                 for (i = 0; i < resp_read->data.ecc.ecc_count; i++) {
2159                         dbg("[%s] ecc[%d] : ecc_category=[0x%x], ecc_num=[%s], " \
2160                                 "ecc_string=[%s]", cpname, i,
2161                                 resp_read->data.ecc.ecc[i].ecc_category,
2162                                 resp_read->data.ecc.ecc[i].ecc_num,
2163                                 resp_read->data.ecc.ecc[i].ecc_string);
2164
2165                         g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2166                         g_variant_builder_add(&b, "{sv}", "category",
2167                                 g_variant_new_int32(resp_read->data.ecc.ecc[i].ecc_category));
2168                         g_variant_builder_add(&b, "{sv}", "number",
2169                                 g_variant_new_string(resp_read->data.ecc.ecc[i].ecc_num));
2170                         g_variant_builder_add(&b, "{sv}", "name",
2171                                 g_variant_new_string(resp_read->data.ecc.ecc[i].ecc_string));
2172                         g_variant_builder_close(&b);
2173                 }
2174                 gv = g_variant_builder_end(&b);
2175
2176                 telephony_sim_complete_get_ecc(dbus_info->interface_object,
2177                         dbus_info->invocation, gv);
2178         }
2179         break;
2180
2181         case TRESP_SIM_GET_ICCID: {
2182                 const struct tresp_sim_read *resp_read = data;
2183                 CoreObject *co_sim = NULL;
2184
2185                 dbg("[%s] SIM_GET_ICCID - Result: [%s] ICCID: [%s])", cpname,
2186                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"),
2187                          resp_read->data.iccid.iccid);
2188
2189                 co_sim = __get_sim_co_from_ur(ctx->server, ur);
2190                 if (!co_sim) {
2191                         err("SIM Core object is NULL");
2192                         return FALSE;
2193                 }
2194
2195                 if (resp_read->result == SIM_ACCESS_SUCCESS)
2196                         tcore_sim_set_iccid(co_sim, &resp_read->data.iccid);
2197                 else if (resp_read->result == SIM_ACCESS_FILE_NOT_FOUND)
2198                         tcore_sim_set_iccid(co_sim, NULL);
2199
2200                 telephony_sim_complete_get_iccid(dbus_info->interface_object,
2201                         dbus_info->invocation, resp_read->result,
2202                         resp_read->data.iccid.iccid);
2203         }
2204         break;
2205
2206         case TRESP_SIM_GET_LANGUAGE: {
2207                 const struct tresp_sim_read *resp_read = data;
2208
2209                 dbg("[%s] SIM_GET_LANGUAGE - Result: [%s] Language: [0x%2x]", cpname,
2210                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"),
2211                         resp_read->data.language.language[0]);
2212
2213                 telephony_sim_complete_get_language(dbus_info->interface_object,
2214                         dbus_info->invocation, resp_read->result,
2215                         resp_read->data.language.language[0]);
2216         }
2217         break;
2218
2219         case TRESP_SIM_SET_LANGUAGE: {
2220                 const struct tresp_sim_set_data *resp_set_data = data;
2221
2222                 dbg("[%s] SIM_SET_LANGUAGE - Result: [%s]", cpname,
2223                         (resp_set_data->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2224
2225                 telephony_sim_complete_set_language(dbus_info->interface_object,
2226                         dbus_info->invocation, resp_set_data->result);
2227         }
2228         break;
2229
2230         case TRESP_SIM_GET_CALLFORWARDING: {
2231                 const struct tresp_sim_read *resp_read = data;
2232                 GVariant *gv_cf = NULL;
2233                 GVariant *gv_cphs_cf = NULL;
2234                 GVariantBuilder b;
2235
2236                 dbg("[%s] SIM_GET_CALLFORWARDING - Result: [%s] CPHS: [%s]",
2237                         cpname, (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"),
2238                         (resp_read->data.cf.b_cphs ? "Yes" : "No"));
2239
2240                 if (resp_read->data.cf.b_cphs) {
2241                         dbg("[%s] b_line1[%d], b_line2[%d], b_fax[%d], b_data[%d]",
2242                                 cpname, resp_read->data.cf.cphs_cf.b_line1,
2243                                 resp_read->data.cf.cphs_cf.b_line2,
2244                                 resp_read->data.cf.cphs_cf.b_fax,
2245                                 resp_read->data.cf.cphs_cf.b_data);
2246
2247                         g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
2248                         g_variant_builder_add(&b, "{sv}", "b_line1",
2249                                 g_variant_new_boolean(resp_read->data.cf.cphs_cf.b_line1));
2250                         g_variant_builder_add(&b, "{sv}", "b_line2",
2251                                 g_variant_new_boolean(resp_read->data.cf.cphs_cf.b_line2));
2252                         g_variant_builder_add(&b, "{sv}", "b_fax",
2253                                 g_variant_new_boolean(resp_read->data.cf.cphs_cf.b_fax));
2254                         g_variant_builder_add(&b, "{sv}", "b_data",
2255                                 g_variant_new_boolean(resp_read->data.cf.cphs_cf.b_data));
2256                         gv_cphs_cf = g_variant_builder_end(&b);
2257
2258                         g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2259                         gv_cf = g_variant_builder_end(&b);
2260
2261                 } else {
2262                         int i = 0;
2263
2264                         dbg("[%s] Profile count: [%d]", cpname, resp_read->data.cf.cf_list.profile_count);
2265
2266                         g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2267                         for (i = 0; i < resp_read->data.cf.cf_list.profile_count; i++) {
2268                                 dbg("[%s] [%d] : rec_index[0x%x], msp_num[0x%x], " \
2269                                         "cfu_status[0x%x], cfu_num[%s], ton[0x%x], " \
2270                                         "npi[0x%x], cc2_id[0x%x], ext7_id[0x%x]", cpname, i,
2271                                         resp_read->data.cf.cf_list.cf[i].rec_index,
2272                                         resp_read->data.cf.cf_list.cf[i].msp_num,
2273                                         resp_read->data.cf.cf_list.cf[i].cfu_status,
2274                                         resp_read->data.cf.cf_list.cf[i].cfu_num,
2275                                         resp_read->data.cf.cf_list.cf[i].ton,
2276                                         resp_read->data.cf.cf_list.cf[i].npi,
2277                                         resp_read->data.cf.cf_list.cf[i].cc2_id,
2278                                         resp_read->data.cf.cf_list.cf[i].ext7_id);
2279
2280                                 g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2281                                 g_variant_builder_add(&b, "{sv}", "rec_index",
2282                                         g_variant_new_int32(resp_read->data.cf.cf_list.cf[i].rec_index));
2283                                 g_variant_builder_add(&b, "{sv}", "msp_num",
2284                                         g_variant_new_byte(resp_read->data.cf.cf_list.cf[i].msp_num));
2285                                 g_variant_builder_add(&b, "{sv}", "cfu_status",
2286                                         g_variant_new_byte(resp_read->data.cf.cf_list.cf[i].cfu_status));
2287                                 g_variant_builder_add(&b, "{sv}", "cfu_num",
2288                                         g_variant_new_string(resp_read->data.cf.cf_list.cf[i].cfu_num));
2289                                 g_variant_builder_add(&b, "{sv}", "ton",
2290                                         g_variant_new_int32(resp_read->data.cf.cf_list.cf[i].ton));
2291                                 g_variant_builder_add(&b, "{sv}", "npi",
2292                                         g_variant_new_int32(resp_read->data.cf.cf_list.cf[i].npi));
2293                                 g_variant_builder_add(&b, "{sv}", "cc2_id",
2294                                         g_variant_new_byte(resp_read->data.cf.cf_list.cf[i].cc2_id));
2295                                 g_variant_builder_add(&b, "{sv}", "ext7_id",
2296                                         g_variant_new_byte(resp_read->data.cf.cf_list.cf[i].ext7_id));
2297                                 g_variant_builder_close(&b);
2298                         }
2299                         gv_cf = g_variant_builder_end(&b);
2300
2301                         g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
2302                         gv_cphs_cf = g_variant_builder_end(&b);
2303                 }
2304
2305                 telephony_sim_complete_get_call_forwarding(dbus_info->interface_object,
2306                         dbus_info->invocation, resp_read->result,
2307                         resp_read->data.cf.b_cphs, gv_cf, gv_cphs_cf);
2308         }
2309         break;
2310
2311         case TRESP_SIM_SET_CALLFORWARDING: {
2312                 const struct tresp_sim_set_data *resp_set_data = data;
2313
2314                 dbg("[%s] SIM_SET_CALLFORWARDING - Result: [%s]", cpname,
2315                         (resp_set_data->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2316
2317                 telephony_sim_complete_set_call_forwarding(dbus_info->interface_object,
2318                         dbus_info->invocation, resp_set_data->result);
2319         }
2320         break;
2321
2322         case TRESP_SIM_GET_MESSAGEWAITING: {
2323                 const struct tresp_sim_read *resp_read = data;
2324                 GVariant *gv_mw = NULL;
2325                 GVariant *gv_cphs_mw = NULL;
2326                 GVariantBuilder b;
2327
2328                 dbg("[%s] SIM_GET_MESSAGEWAITING - Result: [%s] CPHS: [%s]",
2329                         cpname, (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"),
2330                         (resp_read->data.mw.b_cphs ? "Yes" : "No"));
2331
2332                 if (resp_read->data.mw.b_cphs) {
2333                         dbg("[%s] b_voice1[%d], b_voice2[%d], b_fax[%d], b_data[%d]",
2334                                 cpname, resp_read->data.mw.cphs_mw.b_voice1,
2335                                 resp_read->data.mw.cphs_mw.b_voice2,
2336                                 resp_read->data.mw.cphs_mw.b_fax,
2337                                 resp_read->data.mw.cphs_mw.b_data);
2338
2339                         g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
2340                         g_variant_builder_add(&b, "{sv}", "b_voice1",
2341                                 g_variant_new_boolean(resp_read->data.mw.cphs_mw.b_voice1));
2342                         g_variant_builder_add(&b, "{sv}", "b_voice2",
2343                                 g_variant_new_boolean(resp_read->data.mw.cphs_mw.b_voice2));
2344                         g_variant_builder_add(&b, "{sv}", "b_fax",
2345                                 g_variant_new_boolean(resp_read->data.mw.cphs_mw.b_fax));
2346                         g_variant_builder_add(&b, "{sv}", "b_data",
2347                                 g_variant_new_boolean(resp_read->data.mw.cphs_mw.b_data));
2348                         gv_cphs_mw = g_variant_builder_end(&b);
2349
2350                         g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2351                         gv_mw = g_variant_builder_end(&b);
2352
2353                 } else {
2354                         int i = 0;
2355
2356                         dbg("[%s] Profile count: [%d]", cpname,
2357                                 resp_read->data.mw.mw_list.profile_count);
2358
2359                         g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2360                         for (i = 0; i < resp_read->data.mw.mw_list.profile_count; i++) {
2361                                 dbg("[%s] mw[%d] : rec_index[0x%x], indicator_status[0x%x], " \
2362                                         "voice_count[0x%x], fax_count[0x%x] email_count[0x%x], " \
2363                                         "other_count[0x%x], video_count[0x%x]", cpname, i,
2364                                         resp_read->data.mw.mw_list.mw[i].rec_index,
2365                                         resp_read->data.mw.mw_list.mw[i].indicator_status,
2366                                         resp_read->data.mw.mw_list.mw[i].voice_count,
2367                                         resp_read->data.mw.mw_list.mw[i].fax_count,
2368                                         resp_read->data.mw.mw_list.mw[i].email_count,
2369                                         resp_read->data.mw.mw_list.mw[i].other_count,
2370                                         resp_read->data.mw.mw_list.mw[i].video_count);
2371
2372                                 g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2373                                 g_variant_builder_add(&b, "{sv}", "rec_index",
2374                                         g_variant_new_int32(resp_read->data.mw.mw_list.mw[i].rec_index));
2375                                 g_variant_builder_add(&b, "{sv}", "indicator_status",
2376                                         g_variant_new_byte(resp_read->data.mw.mw_list.mw[i].indicator_status));
2377                                 g_variant_builder_add(&b, "{sv}", "voice_count",
2378                                         g_variant_new_int32(resp_read->data.mw.mw_list.mw[i].voice_count));
2379                                 g_variant_builder_add(&b, "{sv}", "fax_count",
2380                                         g_variant_new_int32(resp_read->data.mw.mw_list.mw[i].fax_count));
2381                                 g_variant_builder_add(&b, "{sv}", "email_count",
2382                                         g_variant_new_int32(resp_read->data.mw.mw_list.mw[i].email_count));
2383                                 g_variant_builder_add(&b, "{sv}", "other_count",
2384                                         g_variant_new_int32(resp_read->data.mw.mw_list.mw[i].other_count));
2385                                 g_variant_builder_add(&b, "{sv}", "video_count",
2386                                         g_variant_new_int32(resp_read->data.mw.mw_list.mw[i].video_count));
2387                                 g_variant_builder_close(&b);
2388                         }
2389                         gv_mw = g_variant_builder_end(&b);
2390                         g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
2391                         gv_cphs_mw = g_variant_builder_end(&b);
2392                 }
2393
2394                 telephony_sim_complete_get_message_waiting(dbus_info->interface_object,
2395                         dbus_info->invocation, resp_read->result,
2396                         resp_read->data.mw.b_cphs, gv_mw, gv_cphs_mw);
2397         }
2398         break;
2399
2400         case TRESP_SIM_SET_MESSAGEWAITING: {
2401                 const struct tresp_sim_set_data *resp_set_data = data;
2402
2403                 dbg("[%s] SIM_SET_MESSAGEWAITING - Result: [%s]",
2404                         cpname, (resp_set_data->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2405
2406                 telephony_sim_complete_set_message_waiting(dbus_info->interface_object,
2407                         dbus_info->invocation, resp_set_data->result);
2408         }
2409         break;
2410
2411         case TRESP_SIM_GET_MAILBOX: {
2412                 const struct tresp_sim_read *resp_read = data;
2413                 GVariant *gv = NULL;
2414                 GVariantBuilder b;
2415                 int i = 0;
2416
2417                 dbg("[%s] SIM_GET_MAILBOX - Result: [%s] CPHS: [%s] Count: [%d])",
2418                         cpname, (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"),
2419                         (resp_read->data.mb.b_cphs ? "Yes" : "No"),
2420                         resp_read->data.mb.count);
2421
2422                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2423                 for (i = 0; i < resp_read->data.mb.count; i++) {
2424                         dbg("[%s] mb[%d] : rec_index[%d], profile_number[%d], mb_type[%d], " \
2425                                 "alpha_id_max_len[%d]alpha_id[%s], ton[%d], npi[%d], num[%s], " \
2426                                 "cc_id[%d], ext1_id[%d]", cpname, i,
2427                                 resp_read->data.mb.mb[i].rec_index, resp_read->data.mb.mb[i].profile_number,
2428                                 resp_read->data.mb.mb[i].mb_type, resp_read->data.mb.mb[i].number_info.alpha_id_max_len,
2429                                 resp_read->data.mb.mb[i].number_info.alpha_id, resp_read->data.mb.mb[i].number_info.ton,
2430                                 resp_read->data.mb.mb[i].number_info.npi, resp_read->data.mb.mb[i].number_info.num,
2431                                 resp_read->data.mb.mb[i].number_info.cc_id, resp_read->data.mb.mb[i].number_info.ext1_id);
2432
2433                         g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2434                         g_variant_builder_add(&b, "{sv}", "rec_index",
2435                         g_variant_new_int32(resp_read->data.mb.mb[i].rec_index));
2436                         g_variant_builder_add(&b, "{sv}", "profile_num",
2437                         g_variant_new_int32(resp_read->data.mb.mb[i].profile_number));
2438                         g_variant_builder_add(&b, "{sv}", "mb_type",
2439                         g_variant_new_int32(resp_read->data.mb.mb[i].mb_type));
2440                         g_variant_builder_add(&b, "{sv}", "alpha_id_max_len",
2441                         g_variant_new_int32(resp_read->data.mb.mb[i].number_info.alpha_id_max_len));
2442                         g_variant_builder_add(&b, "{sv}", "alpha_id",
2443                         g_variant_new_string(resp_read->data.mb.mb[i].number_info.alpha_id));
2444                         g_variant_builder_add(&b, "{sv}", "ton",
2445                         g_variant_new_int32(resp_read->data.mb.mb[i].number_info.ton));
2446                         g_variant_builder_add(&b, "{sv}", "npi",
2447                         g_variant_new_int32(resp_read->data.mb.mb[i].number_info.npi));
2448                         g_variant_builder_add(&b, "{sv}", "num",
2449                         g_variant_new_string(resp_read->data.mb.mb[i].number_info.num));
2450                         g_variant_builder_add(&b, "{sv}", "cc_id",
2451                         g_variant_new_byte(resp_read->data.mb.mb[i].number_info.cc_id));
2452                         g_variant_builder_add(&b, "{sv}", "ext1_id",
2453                         g_variant_new_byte(resp_read->data.mb.mb[i].number_info.ext1_id));
2454                         g_variant_builder_close(&b);
2455                 }
2456
2457                 gv = g_variant_builder_end(&b);
2458
2459                 telephony_sim_complete_get_mailbox(dbus_info->interface_object, dbus_info->invocation,
2460                         resp_read->result, resp_read->data.mb.b_cphs, gv);
2461         }
2462         break;
2463
2464         case TRESP_SIM_SET_MAILBOX: {
2465                 const struct tresp_sim_set_data *resp_set_data = data;
2466
2467                 dbg("[%s] SIM_SET_MAILBOX - Result: [%s]", cpname,
2468                         (resp_set_data->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2469
2470                 telephony_sim_complete_set_mailbox(dbus_info->interface_object,
2471                         dbus_info->invocation, resp_set_data->result);
2472         }
2473         break;
2474
2475         case TRESP_SIM_GET_CPHS_INFO: {
2476                 const struct tresp_sim_read *resp_read = data;
2477
2478                 dbg("[%s] SIM_GET_CPHS_INFO - Result: [%s]", cpname,
2479                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2480
2481                 telephony_sim_complete_get_cphsinfo(dbus_info->interface_object,
2482                         dbus_info->invocation, resp_read->result,
2483                         resp_read->data.cphs.CphsPhase,
2484                         resp_read->data.cphs.CphsServiceTable.bOperatorNameShortForm,
2485                         resp_read->data.cphs.CphsServiceTable.bMailBoxNumbers,
2486                         resp_read->data.cphs.CphsServiceTable.bServiceStringTable,
2487                         resp_read->data.cphs.CphsServiceTable.bCustomerServiceProfile,
2488                         resp_read->data.cphs.CphsServiceTable.bInformationNumbers);
2489         }
2490         break;
2491
2492         case TRESP_SIM_GET_SERVICE_TABLE: {
2493                 const struct tresp_sim_read *resp_read = data;
2494                 CoreObject *co_sim = NULL;
2495                 GVariantBuilder builder;
2496                 GVariant * inner_gv = NULL;
2497                 GVariant *svct_gv = NULL;
2498                 int i = 0;
2499
2500                 dbg("[%s] SIM_GET_SERVICE_TABLE - Result: [%s]", cpname,
2501                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2502
2503                 co_sim = __get_sim_co_from_ur(ctx->server, ur);
2504                 if (!co_sim) {
2505                         err("SIM Core object is NULL");
2506                         return FALSE;
2507                 }
2508
2509                 if (resp_read->result == SIM_ACCESS_SUCCESS)
2510                         tcore_sim_set_service_table(co_sim, &resp_read->data.svct);
2511                 else if (resp_read->result == SIM_ACCESS_FILE_NOT_FOUND)
2512                         tcore_sim_set_service_table(co_sim, NULL);
2513
2514                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
2515                 if (resp_read->data.svct.sim_type == SIM_TYPE_GSM) {
2516                         for (i = 0; i < SIM_SST_SERVICE_CNT_MAX; i++)
2517                                 g_variant_builder_add(&builder, "y",
2518                                         resp_read->data.svct.table.sst.service[i]);
2519                 } else if (resp_read->data.svct.sim_type == SIM_TYPE_USIM) {
2520                         for (i = 0; i < SIM_UST_SERVICE_CNT_MAX; i++)
2521                                 g_variant_builder_add(&builder, "y",
2522                                         resp_read->data.svct.table.ust.service[i]);
2523                 } else if (resp_read->data.svct.sim_type == SIM_TYPE_RUIM) {
2524                         if (SIM_CDMA_SVC_TABLE == resp_read->data.svct.table.cst.cdma_svc_table) {
2525                                 for (i = 0; i < SIM_CDMA_ST_SERVICE_CNT_MAX; i++) {
2526                                         g_variant_builder_add(&builder, "iy",
2527                                                 resp_read->data.svct.table.cst.cdma_svc_table,
2528                                                 resp_read->data.svct.table.cst.service.cdma_service[i]);
2529                                 }
2530                         } else if (SIM_CSIM_SVC_TABLE == resp_read->data.svct.table.cst.cdma_svc_table) {
2531                                 for (i = 0; i < SIM_CSIM_ST_SERVICE_CNT_MAX; i++) {
2532                                         g_variant_builder_add(&builder, "iy",
2533                                                 resp_read->data.svct.table.cst.cdma_svc_table,
2534                                                 resp_read->data.svct.table.cst.service.csim_service[i]);
2535                                 }
2536                         } else {
2537                                 err("Invalid cdma_svc_table:[%d]", resp_read->data.svct.table.cst.cdma_svc_table);
2538                         }
2539                 } else {
2540                         dbg("unknown sim type.");
2541                 }
2542                 inner_gv = g_variant_builder_end(&builder);
2543                 svct_gv = g_variant_new("v", inner_gv);
2544
2545                 telephony_sim_complete_get_service_table(dbus_info->interface_object,
2546                         dbus_info->invocation, resp_read->result,
2547                         resp_read->data.svct.sim_type, svct_gv);
2548         }
2549         break;
2550
2551         case TRESP_SIM_GET_SPN: {
2552                 const struct tresp_sim_read *resp_read = data;
2553                 CoreObject *co_sim = NULL;
2554
2555                 dbg("[%s] SIM_GET_SPN - Result: [%s] Display condition: [%d] SPN: [%s]",
2556                         cpname, (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"),
2557                         resp_read->data.spn.display_condition, (const gchar *)resp_read->data.spn.spn);
2558
2559                 co_sim = __get_sim_co_from_ur(ctx->server, ur);
2560                 if (!co_sim) {
2561                         err("SIM Core object is NULL");
2562                         return FALSE;
2563                 }
2564
2565                 if (resp_read->result == SIM_ACCESS_SUCCESS)
2566                         tcore_sim_set_spn(co_sim, &resp_read->data.spn);
2567                 else if (resp_read->result == SIM_ACCESS_FILE_NOT_FOUND)
2568                         tcore_sim_set_spn(co_sim, NULL);
2569
2570                 telephony_sim_complete_get_spn(dbus_info->interface_object,
2571                         dbus_info->invocation, resp_read->result,
2572                         resp_read->data.spn.display_condition,
2573                         (const gchar *)resp_read->data.spn.spn);
2574         }
2575         break;
2576
2577         case TRESP_SIM_GET_CPHS_NETNAME: {
2578                 const struct tresp_sim_read *resp_read = data;
2579                 CoreObject *co_sim = NULL;
2580
2581                 dbg("[%s] SIM_GET_CPHS_NETNAME - Result: [%s]", cpname,
2582                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2583
2584                 co_sim = __get_sim_co_from_ur(ctx->server, ur);
2585                 if (!co_sim) {
2586                         err("SIM Core object is NULL");
2587                         return FALSE;
2588                 }
2589
2590                 if (resp_read->result == SIM_ACCESS_SUCCESS)
2591                         tcore_sim_set_cphs_netname(co_sim, &resp_read->data.cphs_net);
2592                 else if (resp_read->result == SIM_ACCESS_FILE_NOT_FOUND)
2593                         tcore_sim_set_cphs_netname(co_sim, NULL);
2594
2595                 telephony_sim_complete_get_cphs_net_name(dbus_info->interface_object,
2596                         dbus_info->invocation, resp_read->result,
2597                         (const gchar *)resp_read->data.cphs_net.full_name,
2598                         (const gchar *)resp_read->data.cphs_net.short_name);
2599         }
2600         break;
2601
2602         case TRESP_SIM_GET_GID: {
2603                 const struct tresp_sim_read *resp_read = data;
2604                 GVariantBuilder builder;
2605                 GVariant *inner_gv = NULL;
2606                 GVariant *gid_gv = NULL;
2607                 int i = 0;
2608
2609                 dbg("[%s] SIM_GET_GID - Result: [%s]", cpname,
2610                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2611
2612                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
2613                 for (i = 0; i < resp_read->data.gid.GroupIdentifierLen; i++)
2614                         g_variant_builder_add(&builder, "y", resp_read->data.gid.szGroupIdentifier[i]);
2615                 inner_gv = g_variant_builder_end(&builder);
2616                 gid_gv = g_variant_new("v", inner_gv);
2617
2618                 telephony_sim_complete_get_gid(dbus_info->interface_object,
2619                         dbus_info->invocation, resp_read->result,
2620                         resp_read->data.gid.GroupIdentifierLen, gid_gv);
2621         }
2622         break;
2623
2624         case TRESP_SIM_GET_MSISDN:{
2625                 const struct tresp_sim_read *resp_read = data;
2626                 CoreObject *co_sim = NULL;
2627                 GVariant *gv = NULL;
2628                 GVariantBuilder b;
2629                 int i = 0;
2630
2631                 dbg("[%s] SIM_GET_MSISDN - Result: [%s]", cpname,
2632                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2633
2634                 co_sim = __get_sim_co_from_ur(ctx->server, ur);
2635                 if (!co_sim) {
2636                         err("SIM Core object is NULL");
2637                         return FALSE;
2638                 }
2639
2640                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2641                 if (resp_read->result == SIM_ACCESS_SUCCESS)
2642                         tcore_sim_set_msisdn_list(co_sim, &resp_read->data.msisdn_list);
2643                 else if (resp_read->result == SIM_ACCESS_FILE_NOT_FOUND)
2644                         tcore_sim_set_msisdn_list(co_sim, NULL);
2645
2646                 for (i = 0; i < resp_read->data.msisdn_list.count; i++) {
2647                         g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2648                         g_variant_builder_add(&b, "{sv}", "name",
2649                                 g_variant_new_string((const gchar *)resp_read->data.msisdn_list.msisdn[i].name));
2650                         if (resp_read->data.msisdn_list.msisdn[i].ton == SIM_TON_INTERNATIONAL) {
2651                                 unsigned char *tmp = (unsigned char *)calloc(SIM_MSISDN_NUMBER_LEN_MAX + 1, 1);
2652                                 if (tmp != NULL) {
2653                                         tmp[0] = '+';
2654
2655                                         strncpy((char *)tmp + 1,
2656                                                 (const char*)resp_read->data.msisdn_list.msisdn[i].num,
2657                                                 SIM_MSISDN_NUMBER_LEN_MAX - 1);
2658                                         tmp[SIM_MSISDN_NUMBER_LEN_MAX] = '\0';
2659
2660                                         g_variant_builder_add(&b, "{sv}", "number",
2661                                                 g_variant_new_string((const gchar *)tmp));
2662                                         free(tmp);
2663                                 } else {
2664                                         dbg("Memory allocation failed");
2665
2666                                         g_variant_builder_add(&b, "{sv}", "number",
2667                                                 g_variant_new_string((const gchar *)resp_read->data.msisdn_list.msisdn[i].num));
2668                                 }
2669                         } else {
2670                                 g_variant_builder_add(&b, "{sv}", "number",
2671                                         g_variant_new_string((const gchar *)resp_read->data.msisdn_list.msisdn[i].num));
2672                         }
2673                         g_variant_builder_close(&b);
2674                 }
2675                 gv = g_variant_builder_end(&b);
2676
2677                 telephony_sim_complete_get_msisdn(dbus_info->interface_object,
2678                         dbus_info->invocation, resp_read->result, gv);
2679         }
2680         break;
2681
2682         case TRESP_SIM_GET_OPLMNWACT: {
2683                 const struct tresp_sim_read *resp_read = data;
2684                 GVariant *gv = NULL;
2685                 GVariantBuilder b;
2686                 int i = 0;
2687
2688                 dbg("[%s] SIM_GET_OPLMNWACT - Result: [%s]", cpname,
2689                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2690
2691                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2692                 for (i = 0; i < resp_read->data.opwa.opwa_count; i++) {
2693                         g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2694                         g_variant_builder_add(&b, "{sv}", "plmn",
2695                                 g_variant_new_string((const gchar *)resp_read->data.opwa.opwa[i].plmn));
2696                         g_variant_builder_add(&b, "{sv}", "b_umts",
2697                                 g_variant_new_boolean(resp_read->data.opwa.opwa[i].b_umts));
2698                         g_variant_builder_add(&b, "{sv}", "b_gsm",
2699                                 g_variant_new_boolean(resp_read->data.opwa.opwa[i].b_gsm));
2700                         g_variant_builder_close(&b);
2701                 }
2702                 gv = g_variant_builder_end(&b);
2703
2704                 telephony_sim_complete_get_oplmnwact(dbus_info->interface_object,
2705                         dbus_info->invocation, resp_read->result, gv);
2706         }
2707         break;
2708
2709         case TRESP_SIM_REQ_AUTHENTICATION: {
2710                 const struct tresp_sim_req_authentication *resp_auth = data;
2711                 GVariantBuilder builder;
2712                 GVariant *ak = NULL;
2713                 GVariant *cp = NULL;
2714                 GVariant *it = NULL;
2715                 GVariant *resp = NULL;
2716                 GVariant *ak_gv = NULL;
2717                 GVariant *cp_gv = NULL;
2718                 GVariant *it_gv = NULL;
2719                 GVariant *resp_gv = NULL;
2720                 int i = 0;
2721
2722                 dbg("[%s] SIM_REQ_AUTHENTICATION - Result: [%s]", cpname,
2723                         (resp_auth->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
2724
2725                 tcore_util_hex_dump("[AUTH_KEY] ",
2726                         resp_auth->authentication_key_length, resp_auth->authentication_key);
2727                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
2728                 for (i = 0; i < (int)resp_auth->authentication_key_length; i++)
2729                         g_variant_builder_add(&builder, "y", resp_auth->authentication_key[i]);
2730                 ak = g_variant_builder_end(&builder);
2731                 ak_gv = g_variant_new("v", ak);
2732
2733                 tcore_util_hex_dump("[CIPHER_DATA] ",
2734                         resp_auth->cipher_length, resp_auth->cipher_data);
2735                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
2736                 for (i = 0; i < (int)resp_auth->cipher_length; i++)
2737                         g_variant_builder_add(&builder, "y", resp_auth->cipher_data[i]);
2738                 cp = g_variant_builder_end(&builder);
2739                 cp_gv = g_variant_new("v", cp);
2740
2741                 tcore_util_hex_dump("[INTEGRITY_DATA] ",
2742                         resp_auth->integrity_length, resp_auth->integrity_data);
2743                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
2744                 for (i = 0; i < (int)resp_auth->integrity_length; i++)
2745                         g_variant_builder_add(&builder, "y", resp_auth->integrity_data[i]);
2746                 it = g_variant_builder_end(&builder);
2747                 it_gv = g_variant_new("v", it);
2748
2749                 tcore_util_hex_dump("[RESP_DATA] ",
2750                         resp_auth->resp_length, resp_auth->resp_data);
2751                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
2752                 for (i = 0; i < (int)resp_auth->resp_length; i++)
2753                         g_variant_builder_add(&builder, "y", resp_auth->resp_data[i]);
2754                 resp = g_variant_builder_end(&builder);
2755                 resp_gv = g_variant_new("v", resp);
2756
2757                 telephony_sim_complete_authentication(dbus_info->interface_object,
2758                         dbus_info->invocation, resp_auth->result,
2759                         resp_auth->auth_type, resp_auth->auth_result,
2760                         ak_gv, cp_gv, it_gv, resp_gv);
2761         }
2762         break;
2763
2764         case TRESP_SIM_VERIFY_PINS: {
2765                 const struct tresp_sim_verify_pins *resp_verify_pins = data;
2766
2767                 dbg("[%s] SIM_VERIFY_PINS - Result: [%s] PIN Type: [%d] Re-try count: [%d]",
2768                         cpname, (resp_verify_pins->result == SIM_PIN_OPERATION_SUCCESS ? "Success" : "Fail"),
2769                         resp_verify_pins->pin_type, resp_verify_pins->retry_count);
2770
2771                 telephony_sim_complete_verify_sec(dbus_info->interface_object,
2772                         dbus_info->invocation, resp_verify_pins->result,
2773                         resp_verify_pins->pin_type, resp_verify_pins->retry_count);
2774         }
2775         break;
2776
2777         case TRESP_SIM_VERIFY_PUKS: {
2778                 const struct tresp_sim_verify_puks *resp_verify_puks = data;
2779
2780                 dbg("[%s] SIM_VERIFY_PUKS - Result: [%s] PIN Type: [%d] Re-try count: [%d]",
2781                         cpname, (resp_verify_puks->result == SIM_PIN_OPERATION_SUCCESS ? "Success" : "Fail"),
2782                         resp_verify_puks->pin_type, resp_verify_puks->retry_count);
2783
2784                 telephony_sim_complete_verify_puk(dbus_info->interface_object,
2785                         dbus_info->invocation, resp_verify_puks->result,
2786                         resp_verify_puks->pin_type, resp_verify_puks->retry_count);
2787         }
2788         break;
2789
2790         case TRESP_SIM_CHANGE_PINS: {
2791                 const struct tresp_sim_change_pins *resp_change_pins = data;
2792
2793                 dbg("[%s] SIM_CHANGE_PINS - Result: [%s] PIN Type: [%d] Re-try count: [%d]",
2794                         cpname, (resp_change_pins->result == SIM_PIN_OPERATION_SUCCESS ? "Success" : "Fail"),
2795                         resp_change_pins->pin_type, resp_change_pins->retry_count);
2796
2797                 telephony_sim_complete_change_pin(dbus_info->interface_object,
2798                         dbus_info->invocation, resp_change_pins->result,
2799                         resp_change_pins->pin_type, resp_change_pins->retry_count);
2800         }
2801         break;
2802
2803         case TRESP_SIM_DISABLE_FACILITY: {
2804                 const struct tresp_sim_disable_facility *resp_dis_facility = data;
2805                 gint f_type = 0;
2806
2807                 dbg("[%s] SIM_DISABLE_FACILITY - Result: [%s] Type: [%d] Re-try count: [%d]",
2808                         cpname, (resp_dis_facility->result == SIM_PIN_OPERATION_SUCCESS ? "Success" : "Fail"),
2809                         resp_dis_facility->type, resp_dis_facility->retry_count);
2810
2811                 switch (resp_dis_facility->type) {
2812                 case SIM_FACILITY_PS:
2813                         f_type = 1;
2814                 break;
2815
2816                 case SIM_FACILITY_SC:
2817                         f_type = 3;
2818                 break;
2819
2820                 case SIM_FACILITY_FD:
2821                         f_type = 4;
2822                 break;
2823
2824                 case SIM_FACILITY_PN:
2825                         f_type = 5;
2826                 break;
2827
2828                 case SIM_FACILITY_PU:
2829                         f_type = 6;
2830                 break;
2831
2832                 case SIM_FACILITY_PP:
2833                         f_type = 7;
2834                 break;
2835
2836                 case SIM_FACILITY_PC:
2837                         f_type = 8;
2838                 break;
2839
2840                 default:
2841                         err("Unhandled/Unknown Facility type: [0x%x]", resp_dis_facility->type);
2842                 break;
2843                 }
2844
2845                 telephony_sim_complete_disable_facility(dbus_info->interface_object,
2846                         dbus_info->invocation, resp_dis_facility->result,
2847                         f_type, resp_dis_facility->retry_count);
2848         }
2849         break;
2850
2851         case TRESP_SIM_ENABLE_FACILITY: {
2852                 const struct tresp_sim_enable_facility *resp_en_facility = data;
2853                 gint f_type = 0;
2854
2855                 dbg("[%s] SIM_ENABLE_FACILITY - Result: [%s] Type: [%d] Re-try count: [%d]",
2856                         cpname, (resp_en_facility->result == SIM_PIN_OPERATION_SUCCESS ? "Success" : "Fail"),
2857                         resp_en_facility->type, resp_en_facility->retry_count);
2858
2859                 switch (resp_en_facility->type) {
2860                 case SIM_FACILITY_PS:
2861                         f_type = 1;
2862                 break;
2863
2864                 case SIM_FACILITY_SC:
2865                         f_type = 3;
2866                 break;
2867
2868                 case SIM_FACILITY_FD:
2869                         f_type = 4;
2870                 break;
2871
2872                 case SIM_FACILITY_PN:
2873                         f_type = 5;
2874                 break;
2875
2876                 case SIM_FACILITY_PU:
2877                         f_type = 6;
2878                 break;
2879
2880                 case SIM_FACILITY_PP:
2881                         f_type = 7;
2882                 break;
2883
2884                 case SIM_FACILITY_PC:
2885                         f_type = 8;
2886                 break;
2887
2888                 default:
2889                         err("Unhandled/Unknown Facility type: [0x%x]", resp_en_facility->type);
2890                 break;
2891                 }
2892
2893                 telephony_sim_complete_enable_facility(dbus_info->interface_object,
2894                         dbus_info->invocation, resp_en_facility->result,
2895                         f_type, resp_en_facility->retry_count);
2896         }
2897         break;
2898
2899         case TRESP_SIM_GET_FACILITY_STATUS: {
2900                 const struct tresp_sim_get_facility_status *resp_get_facility = data;
2901                 gint f_type = 0;
2902
2903                 dbg("[%s] SIM_GET_FACILITY_STATUS - Result: [%s] Type: [%d] Enable: [%s]",
2904                         cpname, (resp_get_facility->result == SIM_PIN_OPERATION_SUCCESS ? "Success" : "Fail"),
2905                         resp_get_facility->type,
2906                         (resp_get_facility->b_enable ? "Yes" : "No"));
2907
2908                 switch (resp_get_facility->type) {
2909                 case SIM_FACILITY_PS:
2910                         f_type = 1;
2911                 break;
2912
2913                 case SIM_FACILITY_SC:
2914                         f_type = 3;
2915                 break;
2916
2917                 case SIM_FACILITY_FD:
2918                         f_type = 4;
2919                 break;
2920
2921                 case SIM_FACILITY_PN:
2922                         f_type = 5;
2923                 break;
2924
2925                 case SIM_FACILITY_PU:
2926                         f_type = 6;
2927                 break;
2928
2929                 case SIM_FACILITY_PP:
2930                         f_type = 7;
2931                 break;
2932
2933                 case SIM_FACILITY_PC:
2934                         f_type = 8;
2935                 break;
2936
2937                 default:
2938                         err("Unhandled/Unknown Facility type: [0x%x]", resp_get_facility->type);
2939                 break;
2940                 }
2941
2942                 telephony_sim_complete_get_facility(dbus_info->interface_object,
2943                         dbus_info->invocation, resp_get_facility->result,
2944                         f_type, resp_get_facility->b_enable);
2945         }
2946         break;
2947
2948         case TRESP_SIM_GET_LOCK_INFO: {
2949                 const struct tresp_sim_get_lock_info *resp_lock = data;
2950                 gint f_type = 0;
2951
2952                 dbg("[%s] SIM_GET_LOCK_INFO - Result: [%s] Type: [%d] Re-try count: [%d]",
2953                         cpname, (resp_lock->result == SIM_PIN_OPERATION_SUCCESS ? "Success" : "Fail"),
2954                         resp_lock->type, resp_lock->retry_count);
2955
2956                 switch (resp_lock->type) {
2957                 case SIM_FACILITY_PS:
2958                         f_type = 1;
2959                 break;
2960
2961                 case SIM_FACILITY_SC:
2962                         f_type = 3;
2963                 break;
2964
2965                 case SIM_FACILITY_FD:
2966                         f_type = 4;
2967                 break;
2968
2969                 case SIM_FACILITY_PN:
2970                         f_type = 5;
2971                 break;
2972
2973                 case SIM_FACILITY_PU:
2974                         f_type = 6;
2975                 break;
2976
2977                 case SIM_FACILITY_PP:
2978                         f_type = 7;
2979                 break;
2980
2981                 case SIM_FACILITY_PC:
2982                         f_type = 8;
2983                 break;
2984
2985                 default:
2986                         err("Unhandled/Unknown Lock type: [0x%x]", resp_lock->type);
2987                 break;
2988                 }
2989
2990                 telephony_sim_complete_get_lock_info(dbus_info->interface_object,
2991                         dbus_info->invocation, resp_lock->result,
2992                         f_type, resp_lock->lock_status, resp_lock->retry_count);
2993         }
2994         break;
2995
2996         case TRESP_SIM_TRANSMIT_APDU: {
2997                 const struct tresp_sim_transmit_apdu *resp_apdu = data;
2998                 GVariantBuilder builder;
2999                 GVariant * apdu_gv = NULL;
3000                 GVariant *inner_gv = NULL;
3001                 int i = 0;
3002
3003                 dbg("[%s] SIM_TRANSMIT_APDU - Result: [%s]", cpname,
3004                         (resp_apdu->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
3005
3006                 tcore_util_hex_dump("[APDU_RESP] ",
3007                         resp_apdu->apdu_resp_length, resp_apdu->apdu_resp);
3008
3009                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
3010                 for (i = 0; i < (int)resp_apdu->apdu_resp_length; i++)
3011                         g_variant_builder_add(&builder, "y", resp_apdu->apdu_resp[i]);
3012                 inner_gv = g_variant_builder_end(&builder);
3013                 apdu_gv = g_variant_new("v", inner_gv);
3014
3015                 telephony_sim_complete_transfer_apdu(dbus_info->interface_object,
3016                         dbus_info->invocation, resp_apdu->result, apdu_gv);
3017         }
3018         break;
3019
3020         case TRESP_SIM_GET_ATR:{
3021                 const struct tresp_sim_get_atr *resp_get_atr = data;
3022                 GVariantBuilder builder;
3023                 GVariant * atr_gv = NULL;
3024                 GVariant *inner_gv = NULL;
3025                 int i = 0;
3026
3027                 dbg("[%s] SIM_GET_ATR - Result: [%s]", cpname,
3028                         (resp_get_atr->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
3029
3030                 tcore_util_hex_dump("[ATR_RESP] ",
3031                         resp_get_atr->atr_length, resp_get_atr->atr);
3032
3033                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
3034                 for (i = 0; i < (int)resp_get_atr->atr_length; i++)
3035                         g_variant_builder_add(&builder, "y", resp_get_atr->atr[i]);
3036                 inner_gv = g_variant_builder_end(&builder);
3037                 atr_gv = g_variant_new("v", inner_gv);
3038
3039                 telephony_sim_complete_get_atr(dbus_info->interface_object,
3040                         dbus_info->invocation, resp_get_atr->result, atr_gv);
3041         }
3042         break;
3043
3044         case TRESP_SIM_SET_POWERSTATE: {
3045                 const struct tresp_sim_set_powerstate *resp_power = data;
3046
3047                 info("[%s] SIM_SET_POWERSTATE - Result: [%s]", cpname,
3048                         (resp_power->result == SIM_POWER_SET_SUCCESS ? "Success" : "Fail"));
3049
3050                 telephony_sim_complete_set_powerstate(dbus_info->interface_object,
3051                         dbus_info->invocation, resp_power->result);
3052         }
3053         break;
3054
3055         case TRESP_SIM_GET_IMPI: {
3056                 const struct tresp_sim_read *resp_read = data;
3057
3058                 dbg("[%s] SIM_GET_IMPI - Result: [%s]", cpname,
3059                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
3060
3061                 telephony_sim_complete_get_impi(dbus_info->interface_object,
3062                         dbus_info->invocation, resp_read->result,
3063                         resp_read->data.impi.impi);
3064         }
3065         break;
3066
3067         case TRESP_SIM_GET_IMPU: {
3068                 const struct tresp_sim_read *resp_read = data;
3069                 GVariant *gv = NULL;
3070                 GVariantBuilder b;
3071                 unsigned int i;
3072
3073                 dbg("[%s] SIM_GET_IMPU - Result: [%s]", cpname,
3074                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
3075
3076                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
3077                 for (i = 0; i < resp_read->data.impu_list.count; i++) {
3078                         g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
3079                         g_variant_builder_add(&b, "{sv}", "impu",
3080                                 g_variant_new_string(resp_read->data.impu_list.impu[i].impu));
3081                         g_variant_builder_close(&b);
3082                 }
3083                 gv = g_variant_builder_end(&b);
3084
3085                 telephony_sim_complete_get_impu(dbus_info->interface_object,
3086                         dbus_info->invocation, resp_read->result, gv);
3087         }
3088         break;
3089
3090         case TRESP_SIM_GET_DOMAIN: {
3091                 const struct tresp_sim_read *resp_read = data;
3092
3093                 dbg("[%s] SIM_GET_DOMAIN - Result: [%s]", cpname,
3094                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
3095
3096                 telephony_sim_complete_get_domain(dbus_info->interface_object,
3097                         dbus_info->invocation, resp_read->result,
3098                         resp_read->data.domain.domain);
3099         }
3100         break;
3101
3102         case TRESP_SIM_GET_PCSCF: {
3103                 const struct tresp_sim_read *resp_read = data;
3104                 GVariant *gv = NULL;
3105                 GVariantBuilder b;
3106                 unsigned int i;
3107
3108                 dbg("[%s] SIM_GET_PCSCF - Result: [%s]", cpname,
3109                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
3110
3111                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
3112                 for (i = 0; i < resp_read->data.pcscf_list.count; i++) {
3113                         g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
3114                         g_variant_builder_add(&b, "{sv}", "type",
3115                                 g_variant_new_int32(resp_read->data.pcscf_list.pcscf[i].type));
3116                         g_variant_builder_add(&b, "{sv}", "pcscf",
3117                                 g_variant_new_string(resp_read->data.pcscf_list.pcscf[i].pcscf));
3118                         g_variant_builder_close(&b);
3119                 }
3120                 gv = g_variant_builder_end(&b);
3121
3122                 telephony_sim_complete_get_pcscf(dbus_info->interface_object,
3123                         dbus_info->invocation, resp_read->result, gv);
3124         }
3125         break;
3126
3127         case TRESP_SIM_GET_ISIM_SERVICE_TABLE: {
3128                 const struct tresp_sim_read *resp_read = data;
3129                 GVariantBuilder builder;
3130                 GVariant *ist_gv = NULL;
3131                 GVariant *inner_gv = NULL;
3132                 int i;
3133
3134                 dbg("[%s] SIM_GET_ISIM_SERVICE_TABLE - Result: [%s]", cpname,
3135                         (resp_read->result == SIM_ACCESS_SUCCESS ? "Success" : "Fail"));
3136
3137                 g_variant_builder_init(&builder, G_VARIANT_TYPE("ay"));
3138                 for (i = 0; i < SIM_IST_SERVICE_CNT_MAX; i++)
3139                         g_variant_builder_add(&builder, "y", resp_read->data.ist.service[i]);
3140                 inner_gv = g_variant_builder_end(&builder);
3141                 ist_gv = g_variant_new("v", inner_gv);
3142
3143                 telephony_sim_complete_get_isim_service_table(dbus_info->interface_object,
3144                         dbus_info->invocation, resp_read->result, ist_gv);
3145         }
3146         break;
3147
3148         default:
3149                 err("Unhandled/Unknown Response: [0x%x]", command);
3150         break;
3151         }
3152
3153         return TRUE;
3154 }
3155
3156 gboolean dbus_plugin_sim_notification(struct custom_data *ctx,
3157         CoreObject *source, TelephonyObjectSkeleton *object,
3158         enum tcore_notification_command command, unsigned int data_len, const void *data)
3159 {
3160         TelephonySim *sim;
3161         const char *cp_name;
3162         enum dbus_tapi_sim_slot_id slot_id = SIM_SLOT_PRIMARY;
3163
3164         cp_name = tcore_server_get_cp_name_by_plugin(tcore_object_ref_plugin(source));
3165
3166         sim = telephony_object_peek_sim(TELEPHONY_OBJECT(object));
3167         if (sim == NULL) {
3168                 err("sim object is NULL!!!");
3169                 return FALSE;
3170         }
3171
3172         switch (command) {
3173         case TNOTI_SIM_STATUS: {
3174                 const struct tnoti_sim_status *n_sim_status = data;
3175                 int count = 0;
3176
3177                 info("[%s] SIM_STATUS - [%d]", cp_name,
3178                         n_sim_status->sim_status);
3179
3180 #ifdef ENABLE_KPI_LOGS
3181                 if (n_sim_status->sim_status == SIM_STATUS_INIT_COMPLETED)
3182                         TIME_CHECK("[%s] SIM Initialized", cp_name);
3183 #endif
3184
3185                 telephony_sim_emit_status(sim, n_sim_status->sim_status);
3186
3187                 slot_id = get_sim_slot_id_by_cp_name(cp_name);
3188                 if (slot_id == SIM_SLOT_PRIMARY)
3189                         ctx->sim1_status = n_sim_status->sim_status;
3190                 else if (slot_id == SIM_SLOT_SECONDARY)
3191                         ctx->sim2_status = n_sim_status->sim_status;
3192                 else
3193                         warn("NOT handled Slot ID: [%d]", slot_id);
3194
3195                 if (__is_sim_status_valid(ctx->sim1_status))
3196                         count++;
3197
3198                 if (__is_sim_status_valid(ctx->sim2_status))
3199                         count++;
3200
3201                 if (ctx->valid_sim_count != count) {
3202                         ctx->valid_sim_count = count;
3203
3204                         telephony_manager_emit_sim_inserted(ctx->mgr, count);
3205                 }
3206         }
3207         break;
3208
3209         case TNOTI_SIM_REFRESHED: {
3210                 const struct tnoti_sim_refreshed *n_sim_refreshed = data;
3211
3212                 info("[%s] SIM_REFRESHED - b_full_file_changed: [%s] changed_file_count: [%d]",
3213                         cp_name, (n_sim_refreshed->b_full_file_changed ? "YES" : "NO"),
3214                         n_sim_refreshed->file_list.file_count);
3215
3216                 telephony_sim_emit_refreshed(sim, n_sim_refreshed->cmd_type);
3217         }
3218         break;
3219
3220         case TNOTI_SIM_CALL_FORWARD_STATE: {
3221                 const struct tnoti_sim_call_forward_state *info = data;
3222
3223                 info("[%s] SIM_CALL_FORWARD_STATE - Call Forwarding: [%s]",
3224                         cp_name, info->b_forward ? "ON" : "OFF");
3225
3226                 telephony_sim_set_cf_state(sim, info->b_forward);
3227         }
3228         break;
3229
3230         default:
3231                 err("Unhandled/Unknown Notification: [0x%x]", command);
3232         break;
3233         }
3234
3235         return TRUE;
3236 }
3237