Add dbus conf file & Fix daemon crash issue
[platform/core/telephony/tel-plugin-packetservice.git] / src / ps_context.c
1 /*
2  * tel-plugin-packetservice
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongHoo Park <donghoo.park@samsung.com>
7  *              Arun Shukla <arun.shukla@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24 #include "ps_common.h"
25 #include "generated-code.h"
26
27 #include <tcore.h>
28 #include <plugin.h>
29 #include <server.h>
30 #include <storage.h>
31 #include <core_object.h>
32 #include <co_ps.h>
33 #include <co_context.h>
34
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37
38 #include <iniparser.h>
39
40 #include <libxml/xmlmemory.h>
41 #include <libxml/parser.h>
42 #include <libxml/tree.h>
43
44 #define PROP_DEFAULT            FALSE
45 #define PROP_DEFAULT_STR        NULL
46 #define DATABASE_PATH_0         "/opt/dbspace/.dnet.db"
47 #define DATABASE_PATH_1         "/opt/dbspace/.dnet2.db"
48 #define DELAY_TO_SIGNAL_EMIT 1
49
50 typedef struct {
51         const char *mccmnc;
52         const char *apn;
53         enum co_context_type pdp_type;
54         enum co_context_role role;
55         const char *p_cscf_ipv4addr;
56         const char *p_cscf_ipv6addr;
57 } OperatorTable;
58
59 #define FREE_AND_ASSIGN(ptr, value) do { \
60         if (ptr) \
61                 g_free(ptr);\
62         ptr = g_strdup(value); \
63 } while (0)
64
65 static Storage *strg_db;
66
67 /*FIXME*/
68 OperatorTable attach_apn_preference[] = {
69         {"45005", "", CONTEXT_TYPE_IPV4V6, CONTEXT_ROLE_IMS, "220.103.220.10", "2001:2d8:00e0:0220::10"},
70         {"45006", "", CONTEXT_TYPE_IPV4V6, CONTEXT_ROLE_IMS, "", ""},
71         {"45008", "", CONTEXT_TYPE_IPV4V6, CONTEXT_ROLE_IMS, "", ""},
72 };
73
74 static void __ps_context_emit_dedicated_bearer_info_signal(ps_context_t *context);
75 static void __ps_context_emit_property_changed_signal(ps_context_t *context);
76 static void _ps_context_setup_interface(PacketServiceContext *context, ps_context_t *context_data);
77
78 static gboolean __ps_context_create_storage_handle(gpointer plugin);
79 static gchar *__ps_context_create_path(char *profile_name, int profile_id, int svc_ctg_id, gchar *cp_name);
80 static gboolean __ps_context_profile_is_attach_apn(CoreObject *co_context, const gchar *mccmnc);
81 static gboolean __ps_context_create_co_context(gpointer context, GHashTable *property, gchar *cp_name);
82 static gboolean __ps_context_update_profile(ps_context_t *context, GHashTable *property);
83 static gboolean __ps_context_update_database(ps_context_t *context);
84 static gboolean __ps_context_update_default_internet_to_db(ps_context_t *context, gboolean enabled);
85 static gboolean __ps_context_remove_database(ps_context_t *context);
86 static int __ps_context_insert_network_id_to_database(gchar *mccmnc, gchar *cp_name);
87 static int __ps_context_load_network_id_from_database(gchar *mccmnc, gchar *cp_name);
88 static gchar *__ps_context_load_network_name_from_database(int network_id, gchar *cp_name);
89 static int __ps_context_load_profile_id_from_database(gchar *cp_name);
90 static int __ps_context_load_num_of_pdn_from_database(gchar *mccmnc, gchar *cp_name);
91 static gboolean __ps_context_insert_profile_tuple(dictionary *dic, int index, gchar *cp_name);
92 static int __ps_context_insert_profile_to_database(GHashTable *property, int network_id, gchar *cp_name);
93 static int __ps_context_get_network_id(gchar *mccmnc, gchar *cp_name);
94 GVariant *__ps_context_get_profile_properties(gpointer context, GVariantBuilder *properties);
95 static gboolean __ps_context_set_default_connection_enable(gpointer object, gboolean enabled);
96
97 void __remove_context_handler(gpointer data)
98 {
99         ps_context_t *context = data;
100
101         dbg("Entered");
102
103         if (!context) {
104                 dbg("Context is Null");
105                 return;
106         }
107
108         /*Need to UNexport and Unref the master Object */
109         g_object_unref(context->if_obj);
110
111         ps_dbg_ex_co(_ps_service_ref_co_network(_ps_context_ref_service(context)), "context removed for the path [%s]", context->path);
112
113         g_free(context->path);
114         g_free(context->mccmnc);
115         g_free(context);
116
117         dbg("Exiting");
118         return;
119 }
120
121 static void __ps_context_emit_dedicated_bearer_info_signal(ps_context_t *context)
122 {
123         GVariant *gv = NULL;
124         CoreObject *co_network = NULL, *co_context = NULL;
125         struct dedicated_bearer_info bearer_info;
126         GVariantBuilder properties;
127         guint i = 0;
128         char *num_dedicated_bearer, *primary_context_id;
129         char *secondary_context_id, *qci, *gbr_dl, *gbr_ul, *max_br_dl, *max_br_ul;
130
131         g_return_if_fail(context != NULL);
132
133         co_network = _ps_service_ref_co_network(_ps_context_ref_service(context));
134         co_context = _ps_context_ref_co_context(context);
135         tcore_context_get_bearer_info(co_context, &bearer_info);
136
137         if (bearer_info.num_dedicated_bearer == 0) {
138                 ps_warn_ex_co(co_network, "No dedicated bearer information");
139                 return;
140         }
141
142         g_variant_builder_init(&properties, G_VARIANT_TYPE("a{ss}"));
143
144         primary_context_id = INT2STRING(tcore_context_get_id(co_context));
145         g_variant_builder_add(&properties, "{ss}", "primary_context_id", primary_context_id);
146         g_free(primary_context_id);
147
148         num_dedicated_bearer = CHAR2STRING(bearer_info.num_dedicated_bearer);
149         g_variant_builder_add(&properties, "{ss}", "num_dedicated_bearer", num_dedicated_bearer);
150         g_free(num_dedicated_bearer);
151
152         secondary_context_id = INT2STRING(bearer_info.secondary_context_id);
153         g_variant_builder_add(&properties, "{ss}", "secondary_context_id", secondary_context_id);
154         g_free(secondary_context_id);
155
156         for (i = 0; i < bearer_info.num_dedicated_bearer ; i++) {
157                 char *buf;
158                 buf = g_strdup_printf("%s_%d", "qci", i);
159                 qci = CHAR2STRING(bearer_info.qos[i].qci);
160                 g_variant_builder_add(&properties, "{ss}", buf, qci);
161                 g_free(buf);
162                 g_free(qci);
163
164                 buf = g_strdup_printf("%s_%d", "gbr_dl", i);
165                 gbr_dl = INT2STRING(bearer_info.qos[i].gbr_dl);
166                 g_variant_builder_add(&properties, "{ss}", buf, gbr_dl);
167                 g_free(buf);
168                 g_free(gbr_dl);
169
170                 buf = g_strdup_printf("%s_%d", "gbr_ul", i);
171                 gbr_ul = INT2STRING(bearer_info.qos[i].gbr_ul);
172                 g_variant_builder_add(&properties, "{ss}", buf, gbr_ul);
173                 g_free(buf);
174                 g_free(gbr_ul);
175
176                 buf = g_strdup_printf("%s_%d", "max_br_dl", i);
177                 max_br_dl = INT2STRING(bearer_info.qos[i].max_br_dl);
178                 g_variant_builder_add(&properties, "{ss}", buf, max_br_dl);
179                 g_free(buf);
180                 g_free(max_br_dl);
181
182                 buf = g_strdup_printf("%s_%d", "max_br_ul", i);
183                 max_br_ul = INT2STRING(bearer_info.qos[i].max_br_ul);
184                 g_variant_builder_add(&properties, "{ss}", buf, max_br_ul);
185                 g_free(buf);
186                 g_free(max_br_ul);
187         }
188         gv = g_variant_builder_end(&properties);
189
190         packet_service_context_emit_dedicated_bearer_info(context->if_obj, gv);
191         ps_warn_ex_co(co_network, "context (%p) emit the dedicated bearer infomation signal", context);
192 }
193
194 static void __ps_context_emit_property_changed_signal(ps_context_t *context)
195 {
196         GVariant *gv = NULL;
197         GVariantBuilder property;
198         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(context));
199
200         ps_dbg_ex_co(co_network, "get context Properties");
201
202         gv = _ps_context_get_properties(context, &property);
203         packet_service_context_emit_property_changed(context->if_obj, gv);
204
205         ps_dbg_ex_co(co_network, "context (%p) emit the context property changed signal", context);
206         return;
207 }
208
209 /*      Funtion : _ps_context_remove_context
210  *      Description : removes and unregister the interface for the context
211  */
212 gboolean _ps_context_remove_context(gpointer context)
213 {
214         ps_context_t *pscontext = context;
215         ps_service_t *service = _ps_context_ref_service(pscontext);
216
217         g_return_val_if_fail(pscontext != NULL, FALSE);
218         g_return_val_if_fail(service != NULL, FALSE);
219         g_return_val_if_fail(pscontext->path != NULL, FALSE);
220
221         ps_dbg_ex_co(_ps_service_ref_co_network(service),
222                 "remove context (%s)", pscontext->path);
223
224         /*Removing the context from the static list */
225         _ps_service_reset_connection_timer(context);
226         _ps_context_set_connected(context, FALSE);
227
228         /* Unexport object */
229         g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(pscontext->if_obj));
230
231         /* remove context from the list (modem, service) */
232         tcore_ps_remove_context(service->co_ps,
233                 (CoreObject *)_ps_context_ref_co_context(context));
234         tcore_context_free(pscontext->co_context);
235
236         /* free allocated resources for context. */
237         g_free(pscontext->mccmnc);
238         g_free(pscontext->path);
239         g_free(pscontext);
240
241         dbg("Exiting");
242         return TRUE;
243 }
244
245 static gboolean __ps_context_create_storage_handle(gpointer plugin)
246 {
247         TcorePlugin *p = plugin;
248
249         strg_db = tcore_server_find_storage(tcore_plugin_ref_server(p), "database");
250         dbg("Storage: (%p)", strg_db);
251
252         return TRUE;
253 }
254
255 static gchar *__ps_context_create_path(char *profile_name, int profile_id, int svc_ctg_id, gchar *cp_name)
256 {
257         gchar *path = NULL, *in_path = NULL;
258         int str_len = 0, context_index = 0;
259
260         if (!profile_name) {
261                 dbg("profile_name is null");
262                 return NULL;
263         }
264
265         str_len = strlen(profile_name);
266         in_path = g_strdup_printf("/%s%s", cp_name, "/context/");
267
268         for (context_index = 0; context_index < str_len; context_index++) {
269                 gchar *buf = NULL, *tmp = NULL;
270                 buf = g_strdup_printf("%02x", profile_name[context_index]);
271                 tmp = g_strconcat(in_path, buf, NULL);
272
273                 g_free(buf);
274                 g_free(in_path);
275
276                 in_path = g_strdup(tmp);
277                 g_free(tmp);
278         }
279
280         dbg("converted name(%s) path(%s)", profile_name, in_path);
281
282         path = g_strdup_printf("%s_%d_%d", in_path, profile_id, svc_ctg_id);
283         dbg("path (%s)", path);
284
285         g_free(in_path);
286         return path;
287 }
288
289 static gboolean __ps_context_profile_is_attach_apn(CoreObject *co_context, const gchar *mccmnc)
290 {
291         gboolean attach_apn = FALSE, default_conn = FALSE;
292         int role = 0;
293
294         if (co_context == NULL)
295                 goto EXIT;
296
297         default_conn = tcore_context_get_default_profile(co_context);
298         role = tcore_context_get_role(co_context);
299
300         /*
301          * TODO: Read CSC Configuration routine.
302          * By default, default Ineternet profile will be used for PS attach.
303          * CSC will define APN for PS attach according to operator requirement.
304          */
305          if (default_conn) {
306                 int i, count;
307                 gboolean matched = FALSE;
308
309                 count = sizeof(attach_apn_preference) / sizeof(attach_apn_preference[0]);
310                 for (i = 0; i < count ; i++) {
311                         if (g_strcmp0(attach_apn_preference[i].mccmnc, mccmnc) == 0) {
312                                 if (attach_apn_preference[i].role == role) {
313                                         dbg("index[%d], attach_apn_preference[%d].role: %d, role: %d, apn: %s",
314                                                 i, i, attach_apn_preference[i].role, role, attach_apn_preference[i].apn);
315                                         attach_apn = TRUE;
316                                 }
317                                 matched = TRUE;
318                                 break;
319                         }
320                 }
321                 if (!matched) {
322                         if (role == CONTEXT_ROLE_INTERNET)
323                                 attach_apn = TRUE;
324                  }
325          }
326 EXIT:
327         dbg("role(%d), attach_apn (%d)", role, attach_apn);
328         return attach_apn;
329 }
330
331 static gboolean __ps_context_create_co_context(gpointer object, GHashTable *property, gchar *cp_name)
332 {
333         GHashTableIter iter;
334         gpointer key, value;
335         ps_context_t *context = NULL;
336         CoreObject *co_context = NULL;
337
338         gchar *path = NULL;
339         int profile_id = 0;
340         gchar *profile_name = NULL;
341         gchar *apn = NULL;
342         gchar *auth_id = NULL, *auth_pwd = NULL, *home_url = NULL, *proxy_addr = NULL;
343         int auth_type = 0, svc_ctg_id = 0, pdp_type = 0;
344         gboolean hidden = FALSE, editable = FALSE, default_conn = FALSE, user_defined = FALSE, is_roaming_apn = FALSE, profile_enable = TRUE;
345
346         g_hash_table_iter_init(&iter, (GHashTable *) property);
347         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
348                 if (g_str_equal(key, "3") == TRUE) { /*Profile ID*/
349                         if (value) {
350                                 profile_id = atoi((const char *) value);
351                                 dbg("profile id (%d)", profile_id);
352                         }
353                 } else if (g_str_equal(key, "4") == TRUE) {
354                         g_free(profile_name);
355                         profile_name = g_strdup((const char *) value);
356                         dbg("profile name (%s)", profile_name);
357                 } else if (g_str_equal(key, "5") == TRUE) {
358                         g_free(apn);
359                         apn = g_strdup((const char *) value);
360                         dbg("APN (%s)", apn);
361                 } else if (g_str_equal(key, "6") == TRUE) {
362                         if (value) {
363                                 auth_type = atoi((const char *) value);
364                                 dbg("auth type (%d)", auth_type);
365                         }
366                 } else if (g_str_equal(key, "7") == TRUE) {
367                         g_free(auth_id);
368                         auth_id = g_strdup((const char *) value);
369                         dbg("auth id (%s)", auth_id);
370                 } else if (g_str_equal(key, "8") == TRUE) {
371                         g_free(auth_pwd);
372                         auth_pwd = g_strdup((const char *) value);
373                         dbg("auth pwd (%s)", auth_pwd);
374                 } else if (g_str_equal(key, "9") == TRUE) {
375                         if (!value || g_strcmp0((const gchar *) value, "") == 0) {
376                                 g_free(proxy_addr);
377                                 proxy_addr = g_strdup((const char *) value);
378                         } else {
379                                 gboolean b_regex = FALSE;
380                                 b_regex = g_regex_match_simple("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+:[0-9]*", (const gchar *) value, 0, 0);
381
382                                 if (b_regex) {
383                                         int port_num = 0;
384                                         gchar **tmp_proxy = NULL;
385
386                                         tmp_proxy = g_strsplit_set((const gchar *) value, ".:", -1);
387                                         port_num = atoi(tmp_proxy[4]);
388
389                                         if (port_num <= 0) {
390                                                 g_free(proxy_addr);
391                                                 proxy_addr = g_strdup_printf("%d.%d.%d.%d",
392                                                         atoi(tmp_proxy[0]), atoi(tmp_proxy[1]), atoi(tmp_proxy[2]), atoi(tmp_proxy[3]));
393                                         } else {
394                                                 g_free(proxy_addr);
395                                                 proxy_addr = g_strdup_printf("%d.%d.%d.%d:%d",
396                                                         atoi(tmp_proxy[0]), atoi(tmp_proxy[1]), atoi(tmp_proxy[2]), atoi(tmp_proxy[3]), port_num);
397                                         }
398                                         g_strfreev(tmp_proxy);
399                                 } else {
400                                         g_free(proxy_addr);
401                                         proxy_addr = g_strdup((const char *) value);
402                                 } /* not in regular experssion */
403                         }
404
405                         dbg("proxy addr (%s)", proxy_addr);
406                 } else if (g_str_equal(key, "10") == TRUE) {
407                         g_free(home_url);
408                         home_url = g_strdup((const char *) value);
409                         dbg("home url (%s)", home_url);
410                 } else if (g_str_equal(key, "11") == TRUE) {
411                         if (value) {
412                                 pdp_type = atoi((const char *) value);
413                                 dbg("pdp type (%d)", pdp_type);
414                         }
415                 } else if (g_str_equal(key, "19") == TRUE) {
416                         if (value) {
417                                 svc_ctg_id = atoi((const char *) value);
418                                 dbg("context category type (%d)", svc_ctg_id);
419                         }
420                 } else if (g_str_equal(key, "20") == TRUE) {
421                         if (value) {
422                                 hidden = atoi((const char *) value);
423                                 dbg("hidden profile (%d)", hidden);
424                         }
425                 } else if (g_str_equal(key, "21") == TRUE) {
426                         if (value) {
427                                 editable = atoi((const char *) value);
428                                 dbg("editable profile (%d)", editable);
429                         }
430                 } else if (g_str_equal(key, "22") == TRUE) {
431                         if (value) {
432                                 default_conn = atoi((const char*) value);
433                                 dbg("default connection profile (%d)", default_conn);
434                         }
435                 } else if (g_str_equal(key, "23") == TRUE) {
436                         if (value) {
437                                 user_defined = atoi((const char*) value);
438                                 dbg("user defined profile (%d)", user_defined);
439                         }
440                 } else if (g_str_equal(key, "24") == TRUE) {
441                         if (value) {
442                                 is_roaming_apn = atoi((const char*) value);
443                                 dbg("roaming APN profile (%d)", is_roaming_apn);
444                         }
445                 } else if (g_str_equal(key, "25") == TRUE) {
446                         if (value) {
447                                 profile_enable = atoi((const char*) value);
448                                 dbg("profile enable (%d)", profile_enable);
449                         }
450                 }
451         }
452
453         path = __ps_context_create_path(profile_name, profile_id, svc_ctg_id, cp_name);
454
455         context = (ps_context_t *)object;
456         co_context = tcore_context_new(context->plg, path, NULL);
457 #ifdef TIZEN_PS_IPV4_ONLY
458         dbg("pdp type has been changed (%d)", CONTEXT_TYPE_IP);
459         tcore_context_set_type(co_context, CONTEXT_TYPE_IP);
460 #else
461         tcore_context_set_type(co_context, pdp_type);
462 #endif
463         tcore_context_set_state(co_context, CONTEXT_STATE_DEACTIVATED);
464         tcore_context_set_role(co_context, svc_ctg_id);
465         tcore_context_set_apn(co_context, apn);
466         tcore_context_set_auth(co_context, auth_type);
467         tcore_context_set_username(co_context, auth_id);
468         tcore_context_set_password(co_context, auth_pwd);
469         tcore_context_set_proxy(co_context, proxy_addr);
470         tcore_context_set_mmsurl(co_context, home_url);
471         tcore_context_set_profile_name(co_context, profile_name);
472         tcore_context_set_default_profile(co_context, default_conn);
473         tcore_context_set_attach_apn(co_context, __ps_context_profile_is_attach_apn(co_context, context->mccmnc));
474         tcore_context_set_roaming_apn(co_context, is_roaming_apn);
475
476         context->profile_id = profile_id;
477         context->hidden = hidden;
478         context->editable = editable;
479         context->is_default = default_conn;
480         _ps_context_set_profile_enable(context, profile_enable);
481         context->path = path;
482         context->co_context = co_context;
483
484         g_free(profile_name);
485         g_free(apn);
486         g_free(auth_id);
487         g_free(auth_pwd);
488         g_free(home_url);
489         g_free(proxy_addr);
490
491         return TRUE;
492 }
493
494 static gpointer __ps_context_create_context(GDBusConnection *conn, TcorePlugin *p,
495                 gchar *mccmnc, GHashTable *property, gchar *cp_name)
496 {
497         PacketServiceContext *context;
498         GError *error = NULL;
499         ps_context_t *new_context;
500         gchar *path = NULL;
501
502         dbg("Entered");
503
504         /*Initializing the master list for internal referencing*/
505         new_context = g_try_malloc0(sizeof(ps_context_t));
506         if (NULL == new_context) {
507                 err("Unable to allocate memory for context");
508                 goto FAILURE;
509         }
510         dbg("creating the skeleton object");
511         context = packet_service_context_skeleton_new();
512         if (NULL == context) {
513                 g_free(new_context);
514                 goto FAILURE;
515         }
516
517         dbg("Assigning the memory location for the internal data");
518         new_context->conn = conn;
519         new_context->plg = p;
520         new_context->if_obj = context;
521         new_context->mccmnc = g_strdup(mccmnc);
522
523         __ps_context_create_co_context(new_context, property , cp_name);
524         _ps_context_set_alwayson_enable(new_context, TRUE);
525         path = _ps_context_ref_path(new_context);
526         _ps_context_setup_interface(context, new_context);
527
528         dbg("registering the interface object");
529
530         dbg("exporting the interface object to the dbus connection");
531         /*exporting the interface object to the path mention for master*/
532         g_dbus_interface_skeleton_export((G_DBUS_INTERFACE_SKELETON(context)),
533                         conn,
534                         path,
535                         &error);
536
537         g_assert_no_error(error);
538
539         dbg("Successfully new object created for the interface for path [%s]", path);
540         return new_context;
541
542 FAILURE:
543         /*To do : handle failure */
544         dbg("Unable to allocate memory for the new object");
545         return NULL;
546 }
547
548 static gboolean __ps_context_update_profile(ps_context_t *context, GHashTable *property)
549 {
550         CoreObject *co_context = NULL;
551         GHashTableIter iter;
552         gpointer key, value;
553
554         co_context = context->co_context;
555         if (!co_context)
556                 return FALSE;
557
558         g_hash_table_iter_init(&iter, property);
559         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
560                 if (g_str_equal(key, "apn") == TRUE) {
561                         tcore_context_set_apn(co_context, (const char *) value);
562                 } else if (g_str_equal(key, "profile_name") == TRUE) {
563                         tcore_context_set_profile_name(co_context, (const char *) value);
564                 } else if (g_str_equal(key, "auth_type") == TRUE) {
565                         int i_tmp = 0;
566                         if (value) {
567                                 i_tmp = atoi((const char *) value);
568                                 tcore_context_set_auth(co_context, i_tmp);
569                         }
570                 } else if (g_str_equal(key, "auth_id") == TRUE) {
571                         tcore_context_set_username(co_context, (const char *) value);
572                 } else if (g_str_equal(key, "auth_pwd") == TRUE) {
573                         tcore_context_set_password(co_context, (const char *) value);
574                 } else if (g_str_equal(key, "pdp_protocol") == TRUE) {
575                         int i_tmp = 0;
576                         if (value) {
577                                 i_tmp = atoi((const char *) value);
578                                 tcore_context_set_type(co_context, i_tmp);
579                         }
580                 } else if (g_str_equal(key, "proxy_addr") == TRUE) {
581                         tcore_context_set_proxy(co_context, (const char *) value);
582                 } else if (g_str_equal(key, "home_url") == TRUE) {
583                         tcore_context_set_mmsurl(co_context, (const char *) value);
584                 } else if (g_str_equal(key, "profile_enable") == TRUE) {
585                         if (value) {
586                                 gboolean tmp = TRUE;
587                                 if (g_str_equal(value, "FALSE") == TRUE)
588                                         tmp = FALSE;
589                                 _ps_context_set_profile_enable(context, tmp);
590                         }
591                 }
592         }
593
594         return __ps_context_update_database(context);
595 }
596
597 static gboolean __ps_context_update_default_internet_to_db(ps_context_t *context, gboolean enabled)
598 {
599         gpointer handle;
600         GHashTable *in_param;
601         char szQuery[3000];
602         gboolean rv = FALSE;
603         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(context));
604         char *cp_name = _ps_modem_ref_cp_name(_ps_service_ref_modem(_ps_context_ref_service(context)));
605
606         g_return_val_if_fail(context != NULL, FALSE);
607
608         /* Initialize Storage */
609         if (g_str_has_suffix(cp_name, "1"))
610                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
611         else
612                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
613         if (handle == NULL) {
614                 ps_err_ex_co(co_network, "Failed to get Storage handle");
615                 return FALSE;
616         }
617
618         /* Initialize parameters */
619         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
620         g_hash_table_insert(in_param, "1",
621                         g_strdup_printf("%d", enabled));                                /* Profile enabled/disabled */
622         g_hash_table_insert(in_param, "2",
623                         g_strdup_printf("%d", context->profile_id));    /* Profile ID */
624
625         /* SQL query */
626         memset(szQuery, 0x0, sizeof(szQuery));
627         snprintf(szQuery, sizeof(szQuery), "%s",
628                 " update pdp_profile set \
629                  default_internet_con = ? \
630                  where profile_id = ?");
631
632         rv = tcore_storage_update_query_database(strg_db, handle, szQuery, in_param);
633         ps_dbg_ex_co(co_network, "Update Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
634
635         /* Free resources */
636         g_hash_table_destroy(in_param);
637
638         /* De-initialize Storage */
639         tcore_storage_remove_handle(strg_db, handle);
640
641         return rv;
642 }
643
644 static gboolean __ps_context_update_database(ps_context_t *context)
645 {
646         gpointer handle;
647         GHashTable *in_param;
648         char szQuery[3000];
649         gboolean rv = FALSE;
650         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(context));
651         char *cp_name = _ps_modem_ref_cp_name(_ps_service_ref_modem(_ps_context_ref_service(context)));
652
653         g_return_val_if_fail(context != NULL, FALSE);
654
655         /* Initialize Storage */
656         if (g_str_has_suffix(cp_name, "1"))
657                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
658         else
659                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
660         if (handle == NULL) {
661                 ps_err_ex_co(co_network, "Failed to get Storage handle");
662                 return rv;
663         }
664
665         /* Initialize parameters */
666         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
667         g_hash_table_insert(in_param, "1",
668                         tcore_context_get_apn(context->co_context));                                            /* APN */
669         g_hash_table_insert(in_param, "2",
670                         g_strdup_printf("%d", tcore_context_get_auth(context->co_context)));    /* Auth Type */
671         g_hash_table_insert(in_param, "3",
672                         tcore_context_get_username(context->co_context));                                       /* Username */
673         g_hash_table_insert(in_param, "4",
674                         tcore_context_get_password(context->co_context));                                       /* Password */
675         g_hash_table_insert(in_param, "5",
676                         tcore_context_get_proxy(context->co_context));                                          /* Proxy */
677         g_hash_table_insert(in_param, "6",
678                         tcore_context_get_mmsurl(context->co_context));                                         /* MMS URL */
679         g_hash_table_insert(in_param, "7",
680                         tcore_context_get_profile_name(context->co_context));                                           /* Profile Name */
681         g_hash_table_insert(in_param, "8",
682                         g_strdup_printf("%d", tcore_context_get_type(context->co_context))); /* PDP protocol */
683         g_hash_table_insert(in_param, "9",
684                         g_strdup_printf("%d", _ps_context_get_profile_enable(context))); /* Profile Enable */
685         g_hash_table_insert(in_param, "10",
686                         g_strdup_printf("%d", context->profile_id));                                            /* Profile ID */
687
688         /* SQL query */
689         memset(szQuery, 0x0, sizeof(szQuery));
690         snprintf(szQuery, sizeof(szQuery), "%s",
691                 " update pdp_profile set \
692                  apn = ?, auth_type = ?, auth_id = ?, auth_pwd = ?, \
693                  proxy_ip_addr = ?, home_url = ?, profile_name = ?, \
694                  pdp_protocol = ?, profile_enable = ? where profile_id = ?");
695
696         rv = tcore_storage_update_query_database(strg_db, handle, szQuery, in_param);
697         ps_dbg_ex_co(co_network, "Update Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
698
699         /* Free resources */
700         g_hash_table_destroy(in_param);
701
702         /* De-initialize Storage */
703         tcore_storage_remove_handle(strg_db, handle);
704
705         return rv;
706 }
707
708 static gboolean __ps_context_remove_database(ps_context_t *context)
709 {
710         gpointer handle;
711         GHashTable *in_param;
712         char szQuery[1000];
713         gboolean rv = FALSE;
714         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(context));
715         char *cp_name = _ps_modem_ref_cp_name(_ps_service_ref_modem(_ps_context_ref_service(context)));
716
717         g_return_val_if_fail(context != NULL, FALSE);
718
719         /* Initialize Storage */
720         if (g_str_has_suffix(cp_name, "1"))
721                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
722         else
723                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
724         if (handle == NULL) {
725                 ps_err_ex_co(co_network, "Failed to get Storage handle");
726                 return rv;
727         }
728
729         /* Initialize parameters */
730         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
731         g_hash_table_insert(in_param, "1",
732                         g_strdup_printf("%d", context->profile_id));    /* Profile ID */
733
734         /* SQL query */
735         memset(szQuery, 0x0, sizeof(szQuery));
736         snprintf(szQuery, sizeof(szQuery), "%s",
737                 " delete from pdp_profile where profile_id = ? ");
738
739         rv = tcore_storage_remove_query_database(strg_db, handle, szQuery, in_param);
740         ps_dbg_ex_co(co_network, "Remove from Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
741
742         /* Free resources */
743         g_hash_table_destroy(in_param);
744
745         /* De-initialize Storage */
746         tcore_storage_remove_handle(strg_db, handle);
747
748         return rv;
749 }
750
751 static int __ps_context_insert_network_id_to_database(gchar *mccmnc, gchar *cp_name)
752 {
753         gpointer handle;
754         GHashTable *in_param, *out_param;
755         char szQuery[5000];
756         gboolean rv = FALSE;
757
758         GHashTableIter iter;
759         gpointer key, value;
760         int network_id = 0;
761
762         /* Initialize Storage */
763         if (g_str_has_suffix(cp_name, "1"))
764                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
765         else
766                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
767         if (handle == NULL) {
768                 err("Failed to get Storage handle");
769                 return rv;
770         }
771
772         /*
773          * Check the maximum Network ID that exists in database,
774          * if NONE exists, then 'Network ID' would be equal to 1
775          * else if there exists a valid maximum entry; 'Network ID' would be incremented value.
776          */
777         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
778                                 (GDestroyNotify) g_hash_table_destroy);
779
780         /* SQL query */
781         memset(szQuery, 0x0, sizeof(szQuery));
782         snprintf(szQuery, sizeof(szQuery), "%s",
783                 "select max(network_info_id) as network_id from network_info");
784
785         rv = tcore_storage_read_query_database(strg_db, handle, szQuery, NULL, out_param, 1);
786         dbg("Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
787
788         g_hash_table_iter_init(&iter, out_param);
789         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
790                 GHashTableIter iter2;
791                 gpointer key2, value2;
792
793                 if (value) {
794                         g_hash_table_iter_init(&iter2, (GHashTable *)value);
795                         while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
796                                 dbg("key2(%s) value2(%s)", key2, value2);
797                                 if (g_str_equal(key2, "0") == TRUE) {
798                                         if (!value2 || g_strcmp0((const char *)value2, "") == 0)
799                                                 network_id = 0;
800                                         else
801                                                 network_id = atoi((const char *)value2);
802
803                                         /* TODO - Check this logic */
804                                         break;
805                                 }
806                         }
807                 }
808         }
809
810         /* Free Resources */
811         g_hash_table_destroy(out_param);
812
813         /* Increment Network ID */
814         network_id++;
815
816         /* SQL query */
817         memset(szQuery, 0x0, sizeof(szQuery));
818         snprintf(szQuery, sizeof(szQuery), "%s",
819                 " insert into network_info(network_info_id, network_name, mccmnc) values(?, ?, ?) ");
820
821         /* Initialize parameters */
822         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
823         g_hash_table_insert(in_param, "1", g_strdup_printf("%d", network_id));  /* Network ID */
824         g_hash_table_insert(in_param, "2", g_strdup_printf("PLMN_%s", mccmnc));
825         g_hash_table_insert(in_param, "3", g_strdup(mccmnc));
826
827         rv = tcore_storage_insert_query_database(strg_db, handle, szQuery, in_param);
828         if (rv == FALSE) {
829                 err("Failed to insert query to Storage");
830                 network_id = 0;
831         }
832
833         /* Free resources */
834         g_hash_table_destroy(in_param);
835
836         /* De-initialize Storage */
837         tcore_storage_remove_handle(strg_db, handle);
838
839         return network_id;
840 }
841
842 static int __ps_context_insert_profile_to_database(GHashTable *property, int network_id, gchar *cp_name)
843 {
844         gpointer handle;
845         GHashTable *in_param;
846         char szQuery[5000];
847         gboolean rv = FALSE;
848
849         GHashTableIter iter;
850         gpointer key, value;
851
852         int profile_id = 0;
853         gchar *profile_name = NULL, *apn = NULL, *auth_type = NULL;
854         gchar *auth_id = NULL, *auth_pwd = NULL, *proxy_addr = NULL;
855         gchar *home_url = NULL, *svc_id = NULL, *pdp_protocol = NULL;
856
857         /* Initialize Storage */
858         if (g_str_has_suffix(cp_name, "1"))
859                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
860         else
861                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
862         if (handle == NULL) {
863                 err("Failed to get Storage handle");
864                 return 0;
865         }
866
867         profile_id = __ps_context_load_profile_id_from_database(cp_name);
868         if (profile_id < 0) {
869                 dbg("Failed to get last Profile ID");
870                 profile_id = 0;
871
872                 goto EXIT;
873         }
874
875         /* Increment Profile ID */
876         profile_id++;
877
878         g_hash_table_iter_init(&iter, property);
879         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
880                 if (g_str_equal(key, "profile_name") == TRUE) {                 /* Profile Name */
881                         if (value != NULL) {
882                                 FREE_AND_ASSIGN(profile_name, value);
883                         } else {
884                                 if (profile_name)
885                                         g_free(profile_name);
886
887                                 profile_name = __ps_context_load_network_name_from_database(network_id, cp_name);
888                         }
889                 } else if (g_str_equal(key, "apn") == TRUE) {                   /* APN */
890                         FREE_AND_ASSIGN(apn, value);
891                 } else if (g_str_equal(key, "auth_type") == TRUE) {     /* Auth Type */
892                         FREE_AND_ASSIGN(auth_type, value);
893                 } else if (g_str_equal(key, "auth_id") == TRUE) {               /* Auth ID */
894                         FREE_AND_ASSIGN(auth_id, value);
895                 } else if (g_str_equal(key, "auth_pwd") == TRUE) {      /* Auth Password */
896                         FREE_AND_ASSIGN(auth_pwd, value);
897                 } else if (g_str_equal(key, "proxy_addr") == TRUE) {    /* Proxy Address */
898                         FREE_AND_ASSIGN(proxy_addr, value);
899                 } else if (g_str_equal(key, "home_url") == TRUE) {      /* Home URL */
900                         FREE_AND_ASSIGN(home_url, value);
901                 } else if (g_str_equal(key, "svc_ctg_id") == TRUE) {    /* Service ID */
902                         FREE_AND_ASSIGN(svc_id, value);
903                 } else if (g_str_equal(key, "pdp_protocol") == TRUE) {  /* PDP protocol */
904                         FREE_AND_ASSIGN(pdp_protocol, value);
905                 }
906         }
907
908         /* Set default PDP protocol */
909         if (pdp_protocol == NULL) {
910                 dbg("default pdp_protocol = IPv4");
911                 pdp_protocol = g_strdup_printf("%d", CONTEXT_TYPE_IP);
912         }
913
914         /* Initialize parameters */
915         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
916
917         g_hash_table_insert(in_param, "1",
918                         g_strdup_printf("%d", profile_id));                     /* Profile ID */
919         g_hash_table_insert(in_param, "2", profile_name);       /* Profile Name */
920         g_hash_table_insert(in_param, "3", apn);                        /* APN */
921         g_hash_table_insert(in_param, "4", auth_type);          /* Auth Type */
922         g_hash_table_insert(in_param, "5", auth_id);            /* Auth ID */
923         g_hash_table_insert(in_param, "6", auth_pwd);           /* Auth Password */
924         g_hash_table_insert(in_param, "7", pdp_protocol);               /* PDP Protocol */
925         g_hash_table_insert(in_param, "8", proxy_addr);         /* Proxy Address */
926         g_hash_table_insert(in_param, "9", home_url);           /* Home URL */
927         g_hash_table_insert(in_param, "10",
928                         g_strdup_printf("%d", network_id));                     /* Network ID */
929         g_hash_table_insert(in_param, "11", svc_id);            /* Service ID */
930
931         dbg("Profile ID: [%d] Profile name: [%s] APN :[%s] Auth Type [%s] Auth ID: [%s] "
932                 "Auth Password: [%s] PDP Protocol: [%s] Proxy Address: [%s] Home URL: [%s] Service ID: [%s]",
933                 profile_id, profile_name, apn, auth_type, auth_id, auth_pwd, pdp_protocol, proxy_addr, home_url, svc_id);
934
935         /* SQL Query */
936         memset(szQuery, 0x0, sizeof(szQuery));
937         snprintf(szQuery, sizeof(szQuery), "%s",
938                 " insert into pdp_profile(\
939                  profile_id, profile_name, apn, auth_type, auth_id, auth_pwd, \
940                  pdp_protocol, proxy_ip_addr, home_url, linger_time, \
941                  network_info_id, svc_category_id, hidden, editable, \
942                  default_internet_con, user_defined, is_roaming_apn, profile_enable) values(\
943                  ?, ?, ?, ?, ?, ?, \
944                  ?, ?, ?, 300, \
945                  ?, ?, 0, 1, 0, 1, 0, 1)");
946
947         rv = tcore_storage_insert_query_database(strg_db, handle, szQuery, in_param);
948         if (rv == FALSE) {
949                 err("Failed to insert to Storage");
950                 profile_id = 0;
951         }
952
953         /* Free resources */
954         g_hash_table_destroy(in_param);
955
956 EXIT:
957         /* De-initialize Storage */
958         tcore_storage_remove_handle(strg_db, handle);
959
960         return profile_id;
961 }
962
963 static int __ps_context_load_network_id_from_database(gchar *mccmnc, gchar *cp_name)
964 {
965         gpointer handle;
966         GHashTable *in_param, *out_param;
967         char szQuery[5000];
968         gboolean rv = FALSE;
969
970         GHashTableIter iter;
971         gpointer key, value;
972
973         int network_id = -1;
974
975         /* Initialize Storage */
976         if (g_str_has_suffix(cp_name, "1"))
977                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
978         else
979                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
980         if (handle == NULL) {
981                 err("Failed to get Storage handle");
982                 return network_id;
983         }
984
985         /* Initialize parameters */
986         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
987         g_hash_table_insert(in_param, "1", g_strdup(mccmnc));
988
989         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
990                         (GDestroyNotify) g_hash_table_destroy);
991
992         /* SQL Query */
993         memset(szQuery, 0x0, sizeof(szQuery));
994         snprintf(szQuery, sizeof(szQuery), "%s",
995                 "select network_info_id from network_info where mccmnc = ? ");
996
997         rv = tcore_storage_read_query_database(strg_db, handle, szQuery, in_param, out_param, 1);
998         dbg("Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
999
1000         g_hash_table_iter_init(&iter, out_param);
1001         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1002                 GHashTableIter iter2;
1003                 gpointer key2, value2;
1004
1005                 if (value) {
1006                         g_hash_table_iter_init(&iter2, (GHashTable *)value);
1007                         while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
1008                                 if (g_str_equal(key2, "0") == TRUE) {
1009                                         if (!value2 || (g_strcmp0((const char *)value2, "") == 0))
1010                                                 network_id = 0;
1011                                         else
1012                                                 network_id = atoi((const char *)value2);
1013
1014                                         /* TODO - Check this out */
1015                                         break;
1016                                 }
1017                         }
1018                 }
1019         }
1020
1021         /* Free resources */
1022         g_hash_table_destroy(in_param);
1023         g_hash_table_destroy(out_param);
1024
1025         /* De-initialize Storage */
1026         tcore_storage_remove_handle(strg_db, handle);
1027
1028         return network_id;
1029 }
1030
1031 static gchar *__ps_context_load_network_name_from_database(int network_id, gchar *cp_name)
1032 {
1033         gpointer handle;
1034         GHashTable *in_param, *out_param;
1035         char szQuery[5000];
1036         gboolean rv = FALSE;
1037
1038         GHashTableIter iter;
1039         gpointer key, value;
1040
1041         gchar *network_name = NULL;
1042
1043         /* Initialize Storage */
1044         if (g_str_has_suffix(cp_name, "1"))
1045                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
1046         else
1047                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
1048         if (handle == NULL) {
1049                 err("Failed to get Storage handle");
1050                 return NULL;
1051         }
1052
1053         /* Initialize parameters */
1054         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1055         g_hash_table_insert(in_param, "1", g_strdup_printf("%d", network_id));
1056
1057         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
1058                                                 (GDestroyNotify)g_hash_table_destroy);
1059
1060         /* SQL query */
1061         memset(szQuery, 0x0, sizeof(szQuery));
1062         snprintf(szQuery, sizeof(szQuery), "%s",
1063                 "select network_name from network_info where network_info_id = ? ");
1064
1065         rv = tcore_storage_read_query_database(strg_db, handle, szQuery, in_param, out_param, 1);
1066         dbg("Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
1067
1068         g_hash_table_iter_init(&iter, out_param);
1069         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1070                 GHashTableIter iter2;
1071                 gpointer key2, value2;
1072
1073                 if (value) {
1074                         g_hash_table_iter_init(&iter2, (GHashTable *)value);
1075                         while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
1076                                 if (g_str_equal(key2, "0") == TRUE) {
1077                                         g_free(network_name);
1078                                         network_name = g_strdup(value2);
1079
1080                                         /* TODO - Check this logic */
1081                                         break;
1082                                 }
1083                         }
1084                 }
1085         }
1086
1087         /* Free resources */
1088         g_hash_table_destroy(in_param);
1089         g_hash_table_destroy(out_param);
1090
1091         /* De-initialize Storage */
1092         tcore_storage_remove_handle(strg_db, handle);
1093
1094         return network_name;
1095 }
1096
1097 static int __ps_context_load_profile_id_from_database(gchar *cp_name)
1098 {
1099         gpointer handle;
1100         GHashTable *out_param;
1101         char szQuery[5000];
1102         gboolean rv = FALSE;
1103
1104         GHashTableIter iter;
1105         gpointer key, value;
1106
1107         int profile_id = -1;
1108
1109         /* Initialize Storage */
1110         if (g_str_has_suffix(cp_name, "1"))
1111                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
1112         else
1113                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
1114         if (handle == NULL) {
1115                 err("Failed to get Storage handle");
1116                 return profile_id;
1117         }
1118
1119         /* Initialize parameters */
1120         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
1121                         (GDestroyNotify) g_hash_table_destroy);
1122
1123         /* SQL query */
1124         memset(szQuery, 0x0, sizeof(szQuery));
1125         snprintf(szQuery, sizeof(szQuery), "%s",
1126                 "select max(profile_id) as last_profile from pdp_profile");
1127
1128         rv = tcore_storage_read_query_database(strg_db, handle, szQuery, NULL, out_param, 1);
1129         dbg("Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
1130
1131         g_hash_table_iter_init(&iter, out_param);
1132         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1133                 GHashTableIter iter2;
1134                 gpointer key2, value2;
1135
1136                 if (value) {
1137                         g_hash_table_iter_init(&iter2, (GHashTable *)value);
1138                         while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
1139                                 if (g_str_equal(key2, "0") == TRUE) {
1140                                         if (!value2 || (g_strcmp0((const char *)value2, "") == 0))
1141                                                 profile_id = 0;
1142                                         else
1143                                                 profile_id = atoi((const char *)value2);
1144
1145                                         /* TODO - Check this logic */
1146                                         break;
1147                                 }
1148                         }
1149                 }
1150         }
1151
1152         /* Free resources */
1153         g_hash_table_destroy(out_param);
1154
1155         /* De-initialize Storage */
1156         tcore_storage_remove_handle(strg_db, handle);
1157
1158         return profile_id;
1159 }
1160
1161 static int __ps_context_load_num_of_pdn_from_database(gchar *mccmnc, gchar *cp_name)
1162 {
1163         gpointer handle;
1164         GHashTable *in_param, *out_param;
1165         char szQuery[5000];
1166         gboolean rv = FALSE;
1167
1168         GHashTableIter iter;
1169         gpointer key, value;
1170
1171         int num_of_pdn = 0;
1172
1173         /* Initialize Storage */
1174         if (g_str_has_suffix(cp_name, "1"))
1175                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
1176         else
1177                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
1178         if (handle == NULL) {
1179                 err("Failed to get Storage handle");
1180                 return 0;
1181         }
1182
1183         /* Initialize parameters */
1184         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1185         g_hash_table_insert(in_param, "1", g_strdup(mccmnc));
1186
1187         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
1188                         (GDestroyNotify) g_hash_table_destroy);
1189
1190         /* SQL query */
1191         memset(szQuery, 0x0, sizeof(szQuery));
1192         snprintf(szQuery, sizeof(szQuery), "%s",
1193                 "select a.max_pdp_3g from max_pdp a, network_info b \
1194                 where a.network_info_id = b.network_info_id and b.mccmnc = ? ");
1195
1196         rv = tcore_storage_read_query_database(strg_db, handle, szQuery, in_param, out_param, 1);
1197         dbg("Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
1198
1199         g_hash_table_iter_init(&iter, out_param);
1200         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1201                 GHashTableIter iter2;
1202                 gpointer key2, value2;
1203
1204                 if (value) {
1205                         g_hash_table_iter_init(&iter2, (GHashTable *)value);
1206                         while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
1207                                 if (g_str_equal(key2, "0") == TRUE) {
1208                                         if (!value2 || (g_strcmp0((const char *)value2, "") == 0)) {
1209                                                 num_of_pdn = 3;
1210                                                 dbg("There is NO value... Using 'default'");
1211                                         } else {
1212                                                 num_of_pdn = atoi((const char *) value2);
1213                                                 dbg("value (%d)", num_of_pdn);
1214                                         }
1215
1216                                         /* TODO - Check this logic */
1217                                         break;
1218                                 }
1219                         }
1220                 }
1221         }
1222
1223         if (num_of_pdn <= 0) {
1224                 dbg("Loaded value is wrong... restoring to 'default'");
1225                 num_of_pdn = PS_MAX_CID;
1226         } else if (num_of_pdn > PS_MAX_CID) {
1227                 dbg("Loaded value is gretaer than 3... restoring to 'default'");
1228                 num_of_pdn = PS_MAX_CID;
1229         }
1230
1231         /* Free resources */
1232         g_hash_table_destroy(in_param);
1233         g_hash_table_destroy(out_param);
1234
1235         /* De-initialize Storage */
1236         tcore_storage_remove_handle(strg_db, handle);
1237
1238         return num_of_pdn;
1239 }
1240
1241 static gboolean __ps_context_remove_profile_tuple(dictionary *dic, int profile_index, gchar *cp_name)
1242 {
1243         gpointer handle;
1244         gboolean rv = FALSE;
1245         GHashTable *in_param;
1246         gchar *network_info_id;
1247         gchar *section_key = NULL;
1248         char szQuery[5000];
1249
1250         /* Initialize Storage */
1251         if (g_str_has_suffix(cp_name, "1"))
1252                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
1253         else
1254                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
1255         if (handle == NULL) {
1256                 err("Failed to get Storage handle");
1257                 return rv;
1258         }
1259
1260         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1261
1262         /* network info id */
1263         section_key = g_strdup_printf("connection:network_info_id_%d", profile_index);
1264         network_info_id = iniparser_getstring(dic, section_key, NULL);
1265         g_hash_table_insert(in_param, "1", g_strdup(network_info_id));
1266         g_free(section_key);
1267
1268         /* SQL query */
1269         memset(szQuery, 0x0, sizeof(szQuery));
1270         snprintf(szQuery, sizeof(szQuery), "%s",
1271                 " delete from pdp_profile where network_info_id = ?");
1272
1273         rv = tcore_storage_insert_query_database(strg_db, handle, szQuery, in_param);
1274         dbg("delete from pdp_profile where network_info_id = %s, result(%d)", network_info_id, rv);
1275         /* Free resources */
1276         g_hash_table_destroy(in_param);
1277
1278         /* De-initialize Storage */
1279         tcore_storage_remove_handle(strg_db, handle);
1280
1281         return rv;
1282 }
1283
1284 static gboolean __ps_context_insert_profile_tuple(dictionary *dic, int profile_index, gchar *cp_name)
1285 {
1286         gpointer handle;
1287         GHashTable *in_param;
1288         gboolean rv = FALSE;
1289
1290         /* Initialize Storage */
1291         if (g_str_has_suffix(cp_name, "1"))
1292                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
1293         else
1294                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
1295         if (handle == NULL) {
1296                 err("Failed to get Storage handle");
1297                 return rv;
1298         }
1299
1300         /* Initialize parameters */
1301         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1302
1303         {/* profile id */
1304                 gchar *profile_id;
1305                 gchar *item_key = NULL;
1306                 item_key = g_strdup_printf("connection:profile_id_%d", profile_index);
1307                 profile_id = iniparser_getstring(dic, item_key, NULL);
1308                 if (profile_id == NULL) {
1309                         g_free(item_key);
1310                         goto EXIT;
1311                 }
1312                 g_hash_table_insert(in_param, "1", g_strdup(profile_id));
1313                 g_free(item_key);
1314         }
1315
1316         {/* profile name */
1317                 gchar *profile_name;
1318                 gchar *item_key = NULL;
1319                 item_key = g_strdup_printf("connection:profile_name_%d", profile_index);
1320                 profile_name = iniparser_getstring(dic, item_key, NULL);
1321                 if (profile_name == NULL) {
1322                         g_free(item_key);
1323                         goto EXIT;
1324                 }
1325                 g_hash_table_insert(in_param, "2", g_strdup(profile_name));
1326                 g_free(item_key);
1327         }
1328
1329         {/* apn */
1330                 gchar *apn;
1331                 gchar *item_key = NULL;
1332                 item_key = g_strdup_printf("connection:apn_%d", profile_index);
1333                 apn = iniparser_getstring(dic, item_key, NULL);
1334                 if (apn == NULL) {
1335                         g_free(item_key);
1336                         goto EXIT;
1337                 }
1338                 g_hash_table_insert(in_param, "3", g_strdup(apn));
1339                 g_free(item_key);
1340         }
1341
1342         {/* auth type */
1343                 gchar *auth_type;
1344                 gchar *item_key = NULL;
1345                 item_key = g_strdup_printf("connection:auth_type_%d", profile_index);
1346                 auth_type = iniparser_getstring(dic, item_key, NULL);
1347                 if (auth_type == NULL)
1348                         g_hash_table_insert(in_param, "4", g_strdup_printf("%d", CONTEXT_AUTH_NONE));
1349                 else
1350                         g_hash_table_insert(in_param, "4", g_strdup(auth_type));
1351                 g_free(item_key);
1352         }
1353
1354         {/* auth id */
1355                 gchar *auth_id;
1356                 gchar *item_key = NULL;
1357                 item_key = g_strdup_printf("connection:auth_id_%d", profile_index);
1358                 auth_id = iniparser_getstring(dic, item_key, NULL);
1359                 g_hash_table_insert(in_param, "5", g_strdup(auth_id));
1360                 g_free(item_key);
1361         }
1362
1363         {/* auth pwd */
1364                 gchar *auth_pwd;
1365                 gchar *item_key = NULL;
1366                 item_key = g_strdup_printf("connection:auth_pwd_%d", profile_index);
1367                 auth_pwd = iniparser_getstring(dic, item_key, NULL);
1368                 g_hash_table_insert(in_param, "6", g_strdup(auth_pwd));
1369                 g_free(item_key);
1370         }
1371
1372         {/* pdp protocol */
1373                 gchar *pdp_protocol;
1374                 gchar *item_key = NULL;
1375                 item_key = g_strdup_printf("connection:pdp_protocol_%d", profile_index);
1376                 pdp_protocol = iniparser_getstring(dic, item_key, NULL);
1377                 if (pdp_protocol == NULL)
1378                         g_hash_table_insert(in_param, "7", g_strdup_printf("%d", CONTEXT_TYPE_IP));
1379                 else
1380                 g_hash_table_insert(in_param, "7", g_strdup(pdp_protocol));
1381                 g_free(item_key);
1382         }
1383
1384         {/* proxy ip */
1385                 gchar *proxy_ip_addr;
1386                 gchar *section_key = NULL;
1387                 section_key = g_strdup_printf("connection:proxy_ip_addr_%d", profile_index);
1388                 proxy_ip_addr = iniparser_getstring(dic, section_key, NULL);
1389                 g_hash_table_insert(in_param, "8", g_strdup(proxy_ip_addr));
1390                 g_free(section_key);
1391         }
1392
1393         {/* home url */
1394                 gchar *home_url;
1395                 gchar *section_key = NULL;
1396                 section_key = g_strdup_printf("connection:home_url_%d", profile_index);
1397                 home_url = iniparser_getstring(dic, section_key, NULL);
1398                 g_hash_table_insert(in_param, "9", g_strdup(home_url));
1399                 g_free(section_key);
1400         }
1401
1402         {/* linger time */
1403                 gchar *linger_time;
1404                 gchar *section_key = NULL;
1405                 section_key = g_strdup_printf("connection:linger_time_%d", profile_index);
1406                 linger_time = iniparser_getstring(dic, section_key, NULL);
1407                 g_hash_table_insert(in_param, "10", g_strdup(linger_time));
1408                 g_free(section_key);
1409         }
1410
1411         {/* traffic class */
1412                 gchar *traffic_class;
1413                 gchar *section_key = NULL;
1414                 section_key = g_strdup_printf("connection:traffic_class_%d", profile_index);
1415                 traffic_class = iniparser_getstring(dic, section_key, NULL);
1416                 g_hash_table_insert(in_param, "11", g_strdup(traffic_class));
1417                 g_free(section_key);
1418         }
1419
1420         {/* is static ip address */
1421                 gchar *is_static_ip_addr;
1422                 gchar *section_key = NULL;
1423                 section_key = g_strdup_printf("connection:is_static_ip_addr_%d", profile_index);
1424                 is_static_ip_addr = iniparser_getstring(dic, section_key, NULL);
1425                 g_hash_table_insert(in_param, "12", g_strdup(is_static_ip_addr));
1426                 g_free(section_key);
1427         }
1428
1429         {/* ip address if static ip is true */
1430                 gchar *ip_addr;
1431                 gchar *section_key = NULL;
1432                 section_key = g_strdup_printf("connection:ip_addr_%d", profile_index);
1433                 ip_addr = iniparser_getstring(dic, section_key, NULL);
1434                 g_hash_table_insert(in_param, "13", g_strdup(ip_addr));
1435                 g_free(section_key);
1436         }
1437
1438         {/* is static dns address */
1439                 gchar *is_static_dns_addr;
1440                 gchar *section_key = NULL;
1441                 section_key = g_strdup_printf("connection:is_static_dns_addr_%d", profile_index);
1442                 is_static_dns_addr = iniparser_getstring(dic, section_key, NULL);
1443                 g_hash_table_insert(in_param, "14", g_strdup(is_static_dns_addr));
1444                 g_free(section_key);
1445         }
1446
1447         {/* dns address 1 */
1448                 gchar *dns_addr1;
1449                 gchar *section_key = NULL;
1450                 section_key = g_strdup_printf("connection:dns_addr1_%d", profile_index);
1451                 dns_addr1 = iniparser_getstring(dic, section_key, NULL);
1452                 g_hash_table_insert(in_param, "15", g_strdup(dns_addr1));
1453                 g_free(section_key);
1454         }
1455
1456         {/* dns address 2 */
1457                 gchar *dns_addr2;
1458                 gchar *section_key = NULL;
1459                 section_key = g_strdup_printf("connection:dns_addr2_%d", profile_index);
1460                 dns_addr2 = iniparser_getstring(dic, section_key, NULL);
1461                 g_hash_table_insert(in_param, "16", g_strdup(dns_addr2));
1462                 g_free(section_key);
1463         }
1464
1465         {/* network info id */
1466                 gchar *network_info_id;
1467                 gchar *section_key = NULL;
1468                 section_key = g_strdup_printf("connection:network_info_id_%d", profile_index);
1469                 network_info_id = iniparser_getstring(dic, section_key, NULL);
1470                 g_hash_table_insert(in_param, "17", g_strdup(network_info_id));
1471                 g_free(section_key);
1472         }
1473
1474         {/* service category id */
1475                 gchar *svc_category_id;
1476                 gchar *section_key = NULL;
1477                 section_key = g_strdup_printf("connection:svc_category_id_%d", profile_index);
1478                 svc_category_id = iniparser_getstring(dic, section_key, NULL);
1479                 if (svc_category_id == NULL)
1480                         g_hash_table_insert(in_param, "18", g_strdup_printf("%d", CONTEXT_ROLE_UNKNOWN));
1481                 else
1482                 g_hash_table_insert(in_param, "18", g_strdup(svc_category_id));
1483                 g_free(section_key);
1484         }
1485
1486         {/* hidden */
1487                 gchar *hidden;
1488                 gchar *section_key = NULL;
1489                 section_key = g_strdup_printf("connection:hidden_%d", profile_index);
1490                 hidden = iniparser_getstring(dic, section_key, NULL);
1491                 if (hidden == NULL)
1492                         g_hash_table_insert(in_param, "19", g_strdup_printf("%d", FALSE));
1493                 else
1494                 g_hash_table_insert(in_param, "19", g_strdup(hidden));
1495                 g_free(section_key);
1496         }
1497
1498         {/* editable */
1499                 gchar *editable;
1500                 gchar *section_key = NULL;
1501                 section_key = g_strdup_printf("connection:editable_%d", profile_index);
1502                 editable = iniparser_getstring(dic, section_key, NULL);
1503                 if (editable == NULL)
1504                         g_hash_table_insert(in_param, "20", g_strdup_printf("%d", TRUE));
1505                 else
1506                 g_hash_table_insert(in_param, "20", g_strdup(editable));
1507                 g_free(section_key);
1508         }
1509
1510         {/* default internet connection */
1511                 gchar *default_internet_con;
1512                 gchar *section_key = NULL;
1513                 section_key = g_strdup_printf("connection:default_internet_con_%d", profile_index);
1514                 default_internet_con = iniparser_getstring(dic, section_key, NULL);
1515                 if (default_internet_con == NULL)
1516                         g_hash_table_insert(in_param, "21", g_strdup_printf("%d", FALSE));
1517                 else
1518                 g_hash_table_insert(in_param, "21", g_strdup(default_internet_con));
1519                 g_free(section_key);
1520         }
1521
1522         {/* insert data into table */
1523                 gchar *is_roaming_apn;
1524                 gchar *section_key = NULL;
1525                 section_key = g_strdup_printf("connection:is_roaming_apn_%d", profile_index);
1526                 is_roaming_apn = iniparser_getstring(dic, section_key, NULL);
1527                 if (is_roaming_apn == NULL)
1528                         g_hash_table_insert(in_param, "22", g_strdup_printf("%d", FALSE));
1529                 else
1530                         g_hash_table_insert(in_param, "22", g_strdup(is_roaming_apn));
1531                 g_free(section_key);
1532         }
1533
1534         {/* insert data into table */
1535                 char szQuery[5000];
1536
1537                 /* SQL query */
1538                 memset(szQuery, 0x0, sizeof(szQuery));
1539                 snprintf(szQuery, sizeof(szQuery), "%s",
1540                         " insert into pdp_profile(\
1541                          profile_id, profile_name, apn, auth_type, auth_id, auth_pwd, \
1542                          pdp_protocol, proxy_ip_addr, home_url, linger_time, \
1543                          traffic_class, is_static_ip_addr, ip_addr, is_static_dns_addr, dns_addr1, dns_addr2, \
1544                          network_info_id, svc_category_id, hidden, editable, default_internet_con, \
1545                          user_defined, is_roaming_apn, profile_enable) values(\
1546                          ?, ?, ?, ?, ?, ?, \
1547                          ?, ?, ?, ?, \
1548                          ?, ?, ?, ?, ?, ?, \
1549                          ?, ?, ?, ?, ?, 0, ?, 1)");
1550
1551                 rv = tcore_storage_insert_query_database(strg_db, handle, szQuery, in_param);
1552                 dbg("Insert to Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
1553         }
1554
1555 EXIT:
1556         /* Free resources */
1557         g_hash_table_destroy(in_param);
1558
1559         /* De-initialize Storage */
1560         tcore_storage_remove_handle(strg_db, handle);
1561
1562         return rv;
1563 }
1564
1565 static int __ps_context_get_network_id(gchar *mccmnc, gchar *cp_name)
1566 {
1567         int network_id;
1568
1569         network_id = __ps_context_load_network_id_from_database(mccmnc, cp_name);
1570         dbg("network id(%d)", network_id);
1571         if (network_id > 0)
1572                 return network_id;
1573
1574         network_id = __ps_context_insert_network_id_to_database(mccmnc, cp_name);
1575         if (network_id <= 0)
1576                 return -1;
1577
1578         return network_id;
1579 }
1580
1581 GVariant *__ps_context_get_profile_properties(gpointer object, GVariantBuilder *properties)
1582 {
1583         gchar *s_authtype = NULL, *s_role = NULL, *s_type = NULL;
1584         ps_context_t *context = NULL;
1585         char *apn, *username, *password, *proxy_addr, *home_url, *profile_name;
1586
1587         g_return_val_if_fail(object != NULL, NULL);
1588         g_return_val_if_fail(properties != NULL, NULL);
1589
1590         context = (ps_context_t *) object;
1591         ps_dbg_ex_co(_ps_service_ref_co_network(_ps_context_ref_service(context)),
1592                 "get profile properties, path(%s)", _ps_context_ref_path(context));
1593
1594         s_authtype = g_strdup_printf("%d", tcore_context_get_auth(context->co_context));
1595         s_role = g_strdup_printf("%d", tcore_context_get_role(context->co_context));
1596         s_type = g_strdup_printf("%d", tcore_context_get_type(context->co_context));
1597
1598         apn = tcore_context_get_apn(context->co_context);
1599         username = tcore_context_get_username(context->co_context);
1600         password = tcore_context_get_password(context->co_context);
1601         proxy_addr = tcore_context_get_proxy(context->co_context);
1602         home_url = tcore_context_get_mmsurl(context->co_context);
1603         profile_name = tcore_context_get_profile_name(context->co_context);
1604         g_variant_builder_init(properties, G_VARIANT_TYPE("a{ss}"));
1605
1606         g_variant_builder_add(properties, "{ss}", "path", context->path);
1607         if (apn)
1608                 g_variant_builder_add(properties, "{ss}", "apn", apn);
1609
1610         if (s_authtype)
1611                 g_variant_builder_add(properties, "{ss}", "auth_type", s_authtype);
1612
1613         if (username)
1614                 g_variant_builder_add(properties, "{ss}", "auth_id", username);
1615
1616         if (password)
1617                 g_variant_builder_add(properties, "{ss}", "auth_pwd", password);
1618
1619         if (s_type)
1620                 g_variant_builder_add(properties, "{ss}", "pdp_protocol", s_type);
1621
1622         if (proxy_addr)
1623                 g_variant_builder_add(properties, "{ss}", "proxy_addr", proxy_addr);
1624
1625         if (home_url)
1626                 g_variant_builder_add(properties, "{ss}", "home_url", home_url);
1627
1628         if (s_role)
1629                 g_variant_builder_add(properties, "{ss}", "svc_ctg_id", s_role);
1630
1631         g_variant_builder_add(properties, "{ss}", "profile_name", profile_name);
1632         g_variant_builder_add(properties, "{ss}", "hidden", BOOL2STRING(context->hidden));
1633         g_variant_builder_add(properties, "{ss}", "editable", BOOL2STRING(context->editable));
1634         g_variant_builder_add(properties, "{ss}", "default_internet_conn", BOOL2STRING(context->is_default));
1635         g_variant_builder_add(properties, "{ss}", "profile_enable", BOOL2STRING(context->profile_enable));
1636
1637         /* Freeing locally allocated memory */
1638         g_free(s_authtype);
1639         g_free(s_role);
1640         g_free(s_type);
1641         g_free(apn);
1642         g_free(username);
1643         g_free(password);
1644         g_free(proxy_addr);
1645         g_free(home_url);
1646         g_free(profile_name);
1647
1648         dbg("Exiting");
1649
1650         return g_variant_builder_end(properties);
1651 }
1652
1653 static gboolean __ps_context_set_default_connection_enable(gpointer object, gboolean enabled)
1654 {
1655         ps_context_t *context = object;
1656
1657         g_return_val_if_fail(context != NULL, FALSE);
1658
1659         context->is_default = enabled;
1660         return TRUE;
1661 }
1662
1663 static gpointer __ps_context_add_context(gpointer modem, gchar *mccmnc, int profile_id)
1664 {
1665         gpointer handle;
1666         GHashTable *in_param, *out_param;
1667         char szQuery[5000];
1668         gboolean rv = FALSE;
1669
1670         ps_modem_t *mdm = modem;
1671         CoreObject *co_modem = _ps_modem_ref_co_modem(mdm);
1672         GDBusConnection *conn = NULL;
1673         TcorePlugin *p = NULL;
1674         gchar *path = NULL;
1675
1676         GHashTableIter iter;
1677         gpointer object = NULL;
1678         gpointer key, value;
1679
1680         /* Initialize Storage */
1681         if (g_str_has_suffix(mdm->cp_name, "1"))
1682                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
1683         else
1684                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
1685         if (handle == NULL) {
1686                 ps_err_ex_co(co_modem, "Failed to get Storage handle");
1687                 return NULL;
1688         }
1689
1690         /* Initialize parameters */
1691         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1692         g_hash_table_insert(in_param, "1", g_strdup_printf("%d", profile_id));
1693
1694         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
1695                         (GDestroyNotify) g_hash_table_destroy);
1696
1697         /* SQL query */
1698         memset(szQuery, 0x0, sizeof(szQuery));
1699         snprintf(szQuery, sizeof(szQuery), "%s",
1700                 "select \
1701                  a.network_info_id, a.network_name, a.mccmnc, \
1702                  b.profile_id, b.profile_name, b.apn, \
1703                  b.auth_type, b.auth_id, b.auth_pwd, \
1704                  b.proxy_ip_addr, b.home_url, b.pdp_protocol, \
1705                  b.linger_time, b.traffic_class, b.is_static_ip_addr, b.ip_addr, \
1706                  b.is_static_dns_addr, b.dns_addr1, b.dns_addr2, b.svc_category_id, b.hidden, b.editable, \
1707                  b.default_internet_con, b.user_defined, b.is_roaming_apn, b.profile_enable \
1708                  from network_info a, pdp_profile b \
1709                  where b.profile_id = ? and a.network_info_id = b.network_info_id ");
1710
1711         rv = tcore_storage_read_query_database(strg_db, handle, szQuery, in_param, out_param, 26);
1712         ps_dbg_ex_co(co_modem, "Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
1713
1714         ps_dbg_ex_co(co_modem, "Create profile by Profile ID: [%d]", profile_id);
1715         conn = _ps_modem_ref_dbusconn(modem);
1716         p = _ps_modem_ref_plugin(modem);
1717
1718         g_hash_table_iter_init(&iter, out_param);
1719         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1720                 /* Create context */
1721                 object = __ps_context_create_context(conn, p, mccmnc, (GHashTable *)value, mdm->cp_name);
1722                 path = _ps_context_ref_path(object);
1723
1724                 /* Insert to contexts */
1725                 mdm->contexts = g_slist_append(mdm->contexts, object);
1726                 ps_dbg_ex_co(co_modem, "context (%p, %s) insert to hash", object, path);
1727         }
1728
1729         /* Free resources */
1730         g_hash_table_destroy(in_param);
1731         g_hash_table_destroy(out_param);
1732
1733         /* De-initialize Storage */
1734         tcore_storage_remove_handle(strg_db, handle);
1735
1736         return object;
1737 }
1738
1739 gboolean _ps_context_check_is_roaming_apn_support(gchar* mccmnc, gchar* cp_name)
1740 {
1741         gpointer handle;
1742         GHashTable *in_param, *out_param;
1743         char szQuery[5000];
1744         gboolean rv = FALSE, ret = FALSE;
1745         guint profile_cnt;
1746         int network_info_id = -1;
1747
1748         /* Initialize Storage */
1749         if (g_str_has_suffix(cp_name, "1"))
1750                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
1751         else
1752                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
1753         if (handle == NULL) {
1754                 err("Failed to get Storage handle");
1755                 return ret;
1756         }
1757
1758         network_info_id = __ps_context_load_network_id_from_database(mccmnc, cp_name);
1759         if (network_info_id == -1)
1760                 err("Failed to load network_info_id from mccmnc (%s)", mccmnc);
1761
1762         /* Initialize parameters */
1763         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1764         g_hash_table_insert(in_param, "1", g_strdup_printf("%d", network_info_id));
1765
1766         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
1767                         (GDestroyNotify) g_hash_table_destroy);
1768
1769         /* SQL query */
1770         memset(szQuery, 0x0, sizeof(szQuery));
1771         snprintf(szQuery, sizeof(szQuery), "%s", "select profile_id from pdp_profile \
1772                 where network_info_id = ? and is_roaming_apn = 1 ");
1773
1774         rv = tcore_storage_read_query_database(strg_db, handle, szQuery, in_param, out_param, 1);
1775         dbg("Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
1776
1777         profile_cnt = g_hash_table_size(out_param);
1778         if (profile_cnt > 0) {
1779                 dbg("roaming profiles for (mccmnc: %s [%d]) exists: count[%d]", mccmnc, network_info_id,  profile_cnt);
1780                 ret = TRUE;
1781         }
1782         /* Free resources */
1783         g_hash_table_destroy(in_param);
1784         g_hash_table_destroy(out_param);
1785
1786         /* De-initialize Storage */
1787         tcore_storage_remove_handle(strg_db, handle);
1788         return ret;
1789 }
1790
1791 gboolean _ps_context_initialize(gpointer plugin)
1792 {
1793         gboolean rv = TRUE;
1794
1795         rv &= __ps_context_create_storage_handle(plugin);
1796         dbg("Global variable initialized: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
1797
1798         return rv;
1799 }
1800
1801 gboolean _ps_context_reset_profile_table(gchar *cp_name)
1802 {
1803         gpointer handle;
1804         char szQuery[1000];
1805         gboolean rv = FALSE;
1806
1807         /* Initialize Storage */
1808         if (g_str_has_suffix(cp_name, "1"))
1809                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
1810         else
1811                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
1812         if (handle == NULL) {
1813                 err("Failed to get Storage handle");
1814                 return rv;
1815         }
1816
1817         /* SQL query */
1818         memset(szQuery, 0x0, sizeof(szQuery));
1819         snprintf(szQuery, sizeof(szQuery), "%s", " delete from pdp_profile");
1820
1821         rv = tcore_storage_remove_query_database(strg_db, handle, szQuery, NULL);
1822         dbg("Reset profile table: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
1823
1824         /* De-initialize Storage */
1825         tcore_storage_remove_handle(strg_db, handle);
1826
1827         return rv;
1828 }
1829
1830 gboolean _ps_context_fill_profile_table_from_ini_file(gchar *cp_name)
1831 {
1832         int profile_index = 1;
1833         int data_exist = 0;
1834         gchar *section_key = NULL;
1835         dictionary *dic = NULL;
1836
1837         if (g_str_has_suffix(cp_name, "1"))
1838                 dic = iniparser_load("/opt/system/csc-default/data/csc-default-data-connection-2.ini");
1839         else
1840                 dic = iniparser_load("/opt/system/csc-default/data/csc-default-data-connection.ini");
1841
1842         if (dic == NULL) {
1843                 dbg("fail to load the csc default file");
1844                 return FALSE;
1845         }
1846
1847         /* delete first */
1848         do {
1849                 section_key = g_strdup_printf("connection:profile_id_%d", profile_index);
1850                 dbg("section key (%s)", section_key);
1851                 data_exist = iniparser_find_entry(dic, section_key);
1852                 if (!data_exist) {
1853                         g_free(section_key);
1854                         dbg("no more data in ini");
1855                         break;
1856                 }
1857                 __ps_context_remove_profile_tuple(dic, profile_index, cp_name);
1858                 g_free(section_key);
1859                 profile_index++;
1860         } while (TRUE);
1861
1862         /* insert later */
1863         profile_index = 1;
1864         do {
1865                 section_key = g_strdup_printf("connection:profile_id_%d", profile_index);
1866                 dbg("section key (%s)", section_key);
1867                 data_exist = iniparser_find_entry(dic, section_key);
1868                 if (!data_exist) {
1869                         g_free(section_key);
1870                         iniparser_freedict(dic);
1871                         dbg("no more data in ini");
1872                         break;
1873                 }
1874                 __ps_context_insert_profile_tuple(dic, profile_index, cp_name);
1875                 g_free(section_key);
1876                 profile_index++;
1877         } while (TRUE);
1878
1879         return TRUE;
1880 }
1881
1882 GSList* _ps_context_create_hashtable(gpointer modem, gboolean roaming)
1883 {
1884         gpointer handle;
1885         GHashTable *in_param;
1886         GSList *out_param = NULL;
1887         char szQuery[5000];
1888         gboolean rv = FALSE, roaming_apn = FALSE;
1889         int retry = 1;
1890         unsigned int index;
1891
1892         ps_modem_t *mdm = modem;
1893         CoreObject *co_modem = _ps_modem_ref_co_modem(mdm);
1894
1895         /* Initialize Storage */
1896         if (g_str_has_suffix(mdm->cp_name, "1"))
1897                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
1898         else
1899                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
1900         if (handle == NULL) {
1901                 ps_err_ex_co(co_modem, "Failed to get Storage handle");
1902                 return NULL;
1903         }
1904
1905         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1906
1907         /* Check/Store roaming APN support at every SIM init complete. */
1908         roaming_apn = _ps_context_check_is_roaming_apn_support(mdm->operator, mdm->cp_name);
1909         _ps_modem_set_roaming_apn_support(modem, roaming_apn);
1910
1911         if(roaming) {
1912                 /* Check roaming APN support first in the Roaming network. */
1913                 ps_warn_ex_co(co_modem, "Roaming Network[%s], Roaming APN Support[%s]",
1914                         roaming ? "TRUE" : "FALSE", roaming_apn ? "TRUE" : "FALSE");
1915                 roaming = roaming_apn;
1916         }
1917         ps_dbg_ex_co(co_modem, "create profile by mccmnc (%s)", mdm->operator);
1918
1919         memset(szQuery, 0x0, sizeof(szQuery));
1920         snprintf(szQuery, sizeof(szQuery), "%s",
1921                 "select \
1922                  a.network_info_id, a.network_name, a.mccmnc, \
1923                  b.profile_id, b.profile_name, b.apn, \
1924                  b.auth_type, b.auth_id, b.auth_pwd, \
1925                  b.proxy_ip_addr, b.home_url, b.pdp_protocol, \
1926                  b.linger_time, b.traffic_class, b.is_static_ip_addr, b.ip_addr, \
1927                  b.is_static_dns_addr, b.dns_addr1, b.dns_addr2, b.svc_category_id, b.hidden, b.editable, \
1928                  b.default_internet_con, b.user_defined, b.is_roaming_apn, b.profile_enable \
1929                  from network_info a, pdp_profile b \
1930                  where a.mccmnc = ? and b.is_roaming_apn = ? and a.network_info_id = b.network_info_id ");
1931
1932         g_hash_table_insert(in_param, "1", g_strdup(mdm->operator));
1933         g_hash_table_insert(in_param, "2", g_strdup_printf("%d", roaming));
1934
1935         for (retry = 0; retry < 5; retry++) {
1936                 ps_dbg_ex_co(co_modem, "Reading database", mdm->operator);
1937                 rv = tcore_storage_read_query_database_in_order(strg_db, handle, szQuery, in_param, &out_param, 26);
1938                 if (rv != FALSE)
1939                         break;
1940         }
1941         ps_dbg_ex_co(co_modem, "Read Database: [%s], Retry[%d]", (rv == TRUE ? "SUCCESS" : "FAIL"), retry);
1942
1943         for (index = 0; index < g_slist_length(out_param); index++) {
1944                 gchar *path = NULL;
1945                 gpointer object = NULL;
1946                 GHashTable *value = NULL;
1947
1948                 value = g_slist_nth_data(out_param, index);
1949                 if(value == NULL)
1950                         continue;
1951                 /* Create new 'context' */
1952                 object = __ps_context_create_context(mdm->conn, mdm->plg, mdm->operator, value, mdm->cp_name);
1953                 path = _ps_context_ref_path(object);
1954
1955                 mdm->contexts = g_slist_append(mdm->contexts, object);
1956                 ps_dbg_ex_co(co_modem, "context (%p, %s) insert to linked-list", object, path);
1957         }
1958
1959         g_hash_table_destroy(in_param);
1960         g_slist_free_full(out_param, (GDestroyNotify)g_hash_table_destroy);
1961
1962         /* De-initialize Storage */
1963         tcore_storage_remove_handle(strg_db, handle);
1964
1965         dbg("Exiting");
1966         return mdm->contexts;
1967 }
1968
1969 gboolean _ps_context_add_context(gpointer modem, gchar *operator, GHashTable *property)
1970 {
1971         GHashTable *services = NULL;
1972         gpointer context = NULL;
1973         ps_modem_t *mdm = modem;
1974
1975         GHashTableIter iter;
1976         gpointer key, value;
1977         int network_id = 0;
1978         int profile_id = 0;
1979
1980         network_id = __ps_context_get_network_id(operator, mdm->cp_name);
1981         if (network_id <= 0) {
1982                 dbg("fail to add network info");
1983                 return FALSE;
1984         }
1985
1986         profile_id = __ps_context_insert_profile_to_database(property, network_id, mdm->cp_name);
1987         if (profile_id <= 0) {
1988                 dbg("fail to insert profile info to database");
1989                 return FALSE;
1990         }
1991
1992         context = __ps_context_add_context(modem, operator, profile_id);
1993         if (!context)
1994                 return FALSE;
1995
1996         services = _ps_modem_ref_services(modem);
1997         if (!services)
1998                 return FALSE;
1999
2000         g_hash_table_iter_init(&iter, services);
2001         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE)
2002                 _ps_service_ref_context(value, context);
2003
2004         return TRUE;
2005 }
2006
2007 gboolean _ps_context_get_properties_handler(gpointer object, GVariantBuilder *properties)
2008 {
2009         int context_state = 0;
2010         gboolean active = FALSE;
2011         ps_context_t *context = object;
2012         char *dev_name = NULL;
2013         char *proxy = NULL;
2014         char *ipv4_address, *ipv4_gateway, *ipv4_dns1, *ipv4_dns2;
2015         char *ipv6_address, *ipv6_gateway, *ipv6_dns1, *ipv6_dns2;
2016
2017
2018         dbg("get context properties");
2019         g_return_val_if_fail(context != NULL, FALSE);
2020         g_return_val_if_fail(properties != NULL, FALSE);
2021
2022         context_state =    tcore_context_get_state(context->co_context);
2023         if (context_state == CONTEXT_STATE_ACTIVATED)
2024                 active = TRUE;
2025
2026         /* IPV4 data */
2027         ipv4_address = tcore_context_get_ipv4_addr(context->co_context);
2028         ipv4_gateway = tcore_context_get_ipv4_gw(context->co_context);
2029         ipv4_dns1 = tcore_context_get_ipv4_dns1(context->co_context);
2030         ipv4_dns2 = tcore_context_get_ipv4_dns2(context->co_context);
2031
2032         /* IPV6 data */
2033         ipv6_address = tcore_context_get_ipv6_addr(context->co_context);
2034         ipv6_gateway = tcore_context_get_ipv6_gw(context->co_context);
2035         ipv6_dns1 = tcore_context_get_ipv6_dns1(context->co_context);
2036         ipv6_dns2 = tcore_context_get_ipv6_dns2(context->co_context);
2037
2038         proxy = tcore_context_get_proxy(context->co_context);
2039         dev_name = tcore_context_get_ipv4_devname(context->co_context);
2040
2041         g_variant_builder_open(properties, G_VARIANT_TYPE("a{ss}"));
2042
2043         g_variant_builder_add(properties, "{ss}", "path", (context->path));
2044         g_variant_builder_add(properties, "{ss}", "active", (BOOL2STRING(active)));
2045
2046         /* Adding IPV4 data to builder */
2047         if (ipv4_address)
2048                 g_variant_builder_add(properties, "{ss}", "ipv4_address", ipv4_address);
2049
2050         if (ipv4_gateway)
2051                 g_variant_builder_add(properties, "{ss}", "ipv4_gateway", ipv4_gateway);
2052
2053         if (ipv4_dns1)
2054                 g_variant_builder_add(properties, "{ss}", "ipv4_dns1", ipv4_dns1);
2055
2056         if (ipv4_dns2)
2057                 g_variant_builder_add(properties, "{ss}", "ipv4_dns2", ipv4_dns2);
2058
2059         /* Adding IPV6 data to builder */
2060         g_variant_builder_add(properties, "{ss}", "ipv6_address",
2061                 (ipv6_address == NULL ? "::" : ipv6_address));
2062         g_variant_builder_add(properties, "{ss}", "ipv6_gateway",
2063                 (ipv6_gateway == NULL ? "::" : ipv6_gateway));
2064         g_variant_builder_add(properties, "{ss}", "ipv6_dns1",
2065                 (ipv6_dns1 == NULL ? "::" : ipv6_dns1));
2066         g_variant_builder_add(properties, "{ss}", "ipv6_dns2",
2067                 (ipv6_dns2 == NULL ? "::" : ipv6_dns2));
2068
2069         if (proxy)
2070                 g_variant_builder_add(properties, "{ss}", "proxy", proxy);
2071
2072         if (dev_name)
2073                 g_variant_builder_add(properties, "{ss}", "dev_name", dev_name);
2074
2075         g_variant_builder_add(properties, "{ss}", "default_internet_conn", (BOOL2STRING(context->is_default)));
2076         g_variant_builder_close(properties);
2077
2078         /* Freeing local memory */
2079         g_free(ipv4_address);
2080         g_free(ipv4_gateway);
2081         g_free(ipv4_dns1);
2082         g_free(ipv4_dns2);
2083         g_free(ipv6_address);
2084         g_free(ipv6_gateway);
2085         g_free(ipv6_dns1);
2086         g_free(ipv6_dns2);
2087         g_free(proxy);
2088         g_free(dev_name);
2089
2090         dbg("Exiting");
2091         return TRUE;
2092 }
2093
2094
2095 GVariant *_ps_context_get_properties(gpointer object, GVariantBuilder *properties)
2096 {
2097         int context_state = 0;
2098         gboolean active = FALSE;
2099         ps_context_t *context = object;
2100         char *dev_name = NULL;
2101         char *proxy = NULL;
2102         char *ipv4_address, *ipv4_gateway, *ipv4_dns1, *ipv4_dns2;
2103         char *ipv6_address, *ipv6_gateway, *ipv6_dns1, *ipv6_dns2;
2104         pcscf_addr *pcscf_ipv4, *pcscf_ipv6;
2105         unsigned int i;
2106
2107         ps_dbg_ex_co(_ps_service_ref_co_network(_ps_context_ref_service(context)), "get context properties");
2108         g_return_val_if_fail(context != NULL, NULL);
2109         g_return_val_if_fail(properties != NULL, NULL);
2110
2111         context_state =    tcore_context_get_state(context->co_context);
2112         if (context_state == CONTEXT_STATE_ACTIVATED)
2113                 active = TRUE;
2114
2115         active &= context->b_active;
2116
2117         /* IPV4 data */
2118         ipv4_address = tcore_context_get_ipv4_addr(context->co_context);
2119         ipv4_gateway = tcore_context_get_ipv4_gw(context->co_context);
2120         ipv4_dns1 = tcore_context_get_ipv4_dns1(context->co_context);
2121         ipv4_dns2 = tcore_context_get_ipv4_dns2(context->co_context);
2122
2123         /* IPV6 data */
2124         ipv6_address = tcore_context_get_ipv6_addr(context->co_context);
2125         ipv6_gateway = tcore_context_get_ipv6_gw(context->co_context);
2126         ipv6_dns1 = tcore_context_get_ipv6_dns1(context->co_context);
2127         ipv6_dns2 = tcore_context_get_ipv6_dns2(context->co_context);
2128
2129         /* P-CSCF data */
2130         pcscf_ipv4 = tcore_context_get_pcscf_ipv4_addr(context->co_context);
2131         pcscf_ipv6 = tcore_context_get_pcscf_ipv6_addr(context->co_context);
2132
2133         proxy = tcore_context_get_proxy(context->co_context);
2134         dev_name = tcore_context_get_ipv4_devname(context->co_context);
2135
2136         g_variant_builder_init(properties, G_VARIANT_TYPE("a{ss}"));
2137
2138         g_variant_builder_add(properties, "{ss}", "path", (context->path));
2139         g_variant_builder_add(properties, "{ss}", "active", (BOOL2STRING(active)));
2140         g_variant_builder_add(properties, "{ss}", "routing_only", (BOOL2STRING(context->b_routing_only)));
2141
2142         /* Adding IPV4 data to builder */
2143         if (ipv4_address)
2144                 g_variant_builder_add(properties, "{ss}", "ipv4_address", ipv4_address);
2145
2146         if (ipv4_gateway)
2147                 g_variant_builder_add(properties, "{ss}", "ipv4_gateway", ipv4_gateway);
2148
2149         if (ipv4_dns1)
2150                 g_variant_builder_add(properties, "{ss}", "ipv4_dns1", ipv4_dns1);
2151
2152         if (ipv4_dns2)
2153                 g_variant_builder_add(properties, "{ss}", "ipv4_dns2", ipv4_dns2);
2154
2155         /* Adding IPV6 data to builder */
2156         g_variant_builder_add(properties, "{ss}", "ipv6_address",
2157                 (ipv6_address == NULL ? "::" : ipv6_address));
2158         g_variant_builder_add(properties, "{ss}", "ipv6_gateway",
2159                 (ipv6_gateway == NULL ? "::" : ipv6_gateway));
2160         g_variant_builder_add(properties, "{ss}", "ipv6_dns1",
2161                 (ipv6_dns1 == NULL ? "::" : ipv6_dns1));
2162         g_variant_builder_add(properties, "{ss}", "ipv6_dns2",
2163                 (ipv6_dns2 == NULL ? "::" : ipv6_dns2));
2164
2165         if (ipv6_address) {
2166                 gboolean ipv6_link_only = FALSE;
2167                 if (g_ascii_strncasecmp(ipv6_address, "fe80::", (gsize)6) == 0)
2168                         ipv6_link_only = TRUE;
2169                 g_variant_builder_add(properties, "{ss}", "ipv6_link_only", BOOL2STRING(ipv6_link_only));
2170         }
2171
2172         if (proxy)
2173                 g_variant_builder_add(properties, "{ss}", "proxy", proxy);
2174
2175         if (dev_name)
2176                 g_variant_builder_add(properties, "{ss}", "dev_name", dev_name);
2177
2178         if (pcscf_ipv4) {
2179                 char *buf;
2180
2181                 buf = g_strdup_printf("%d", pcscf_ipv4->count);
2182                 g_variant_builder_add(properties, "{ss}", "pcscf_ipv4_count", buf);
2183                 g_free(buf);
2184
2185                 for (i = 0; i < pcscf_ipv4->count; i++) {
2186                         buf = g_strdup_printf("%s_%d", "pcscf_ipv4_addr", i);
2187                         g_variant_builder_add(properties, "{ss}", buf, pcscf_ipv4->addr[i]);
2188                         g_free(buf);
2189                 }
2190         } else {
2191                 /* Update pCSCF address (IPv4) count as '0' */
2192                 g_variant_builder_add(properties, "{ss}", "pcscf_ipv4_count", "0");
2193         }
2194
2195         if (pcscf_ipv6) {
2196                 char *buf;
2197
2198                 buf = g_strdup_printf("%d", pcscf_ipv6->count);
2199                 g_variant_builder_add(properties, "{ss}", "pcscf_ipv6_count", buf);
2200                 g_free(buf);
2201
2202                 for (i = 0; i < pcscf_ipv6->count; i++) {
2203                         buf = g_strdup_printf("%s_%d", "pcscf_ipv6_addr", i);
2204                         g_variant_builder_add(properties, "{ss}", buf, pcscf_ipv6->addr[i]);
2205                         g_free(buf);
2206                 }
2207         } else {
2208                 /* Update pCSCF address (IPv6) count as '0' */
2209                 g_variant_builder_add(properties, "{ss}", "pcscf_ipv6_count", "0");
2210         }
2211
2212         g_variant_builder_add(properties, "{ss}", "default_internet_conn", BOOL2STRING(context->is_default));
2213
2214         /* Freeing local memory */
2215         if (pcscf_ipv4) {
2216                 for (i = 0; i < pcscf_ipv4->count; i++)
2217                         g_free(pcscf_ipv4->addr[i]);
2218                 g_free(pcscf_ipv4);
2219         }
2220         if (pcscf_ipv6) {
2221                 for (i = 0; i < pcscf_ipv6->count; i++)
2222                         g_free(pcscf_ipv6->addr[i]);
2223                 g_free(pcscf_ipv6);
2224         }
2225         g_free(ipv4_address);
2226         g_free(ipv4_gateway);
2227         g_free(ipv4_dns1);
2228         g_free(ipv4_dns2);
2229         g_free(ipv6_address);
2230         g_free(ipv6_gateway);
2231         g_free(ipv6_dns1);
2232         g_free(ipv6_dns2);
2233         g_free(proxy);
2234         g_free(dev_name);
2235
2236         dbg("Exiting");
2237         return g_variant_builder_end(properties);
2238 }
2239
2240 gboolean _ps_context_set_alwayson_enable(gpointer object, gboolean enabled)
2241 {
2242         ps_context_t *context = object;
2243         int role = CONTEXT_ROLE_UNKNOWN;
2244         g_return_val_if_fail(context != NULL, FALSE);
2245
2246         role = tcore_context_get_role(context->co_context);
2247
2248         if ((role == CONTEXT_ROLE_INTERNET) && context->is_default)
2249                 context->alwayson = enabled;
2250
2251 #ifdef PREPAID_SIM_APN_SUPPORT
2252         if ((role == CONTEXT_ROLE_PREPAID_INTERNET) && context->is_default)
2253                 context->prepaid_alwayson = enabled;
2254 #endif
2255         dbg("context (%p) alwayson (%d)", context, context->alwayson);
2256         return TRUE;
2257 }
2258
2259 gboolean _ps_context_get_default_context(gpointer object, int svc_cat_id)
2260 {
2261         ps_context_t *context = object;
2262         int role = CONTEXT_ROLE_UNKNOWN;
2263         g_return_val_if_fail(context != NULL, FALSE);
2264
2265         role = tcore_context_get_role(context->co_context);
2266         if (role == svc_cat_id && context->is_default)
2267                 return TRUE;
2268
2269         return FALSE;
2270 }
2271
2272 gboolean _ps_context_set_service(gpointer object, gpointer service)
2273 {
2274         ps_context_t *context = object;
2275         g_return_val_if_fail(context != NULL, FALSE);
2276
2277         context->p_service = service;
2278         return TRUE;
2279 }
2280
2281 gpointer _ps_context_ref_service(gpointer object)
2282 {
2283         ps_context_t *context = object;
2284         g_return_val_if_fail(context != NULL, NULL);
2285
2286         return context->p_service;
2287 }
2288
2289 gchar *_ps_context_ref_path(gpointer object)
2290 {
2291         ps_context_t *context = object;
2292         g_return_val_if_fail(context != NULL, NULL);
2293
2294         return context->path;
2295 }
2296
2297 gboolean _ps_context_get_alwayson_enable(gpointer object)
2298 {
2299         ps_context_t *context = object;
2300         g_return_val_if_fail(context != NULL, FALSE);
2301         dbg("context (%p) alwayson (%d)", context, context->alwayson);
2302         return context->alwayson;
2303 }
2304
2305 #ifdef PREPAID_SIM_APN_SUPPORT
2306 gboolean _ps_context_get_prepaid_alwayson_enable(gpointer object)
2307 {
2308         ps_context_t *context = object;
2309         g_return_val_if_fail(context != NULL, FALSE);
2310         dbg("prepaid context (%p) alwayson (%d)",
2311                 context, context->prepaid_alwayson);
2312         return context->prepaid_alwayson;
2313 }
2314
2315 int _ps_context_get_profile_id(gpointer object)
2316 {
2317         ps_context_t *context = object;
2318         g_return_val_if_fail(context != NULL, -1);
2319         dbg("profile_id[%d]", context->profile_id);
2320         return context->profile_id;
2321 }
2322 #endif
2323
2324 gpointer _ps_context_ref_co_context(gpointer object)
2325 {
2326         ps_context_t *context = object;
2327         g_return_val_if_fail(context != NULL, NULL);
2328
2329         return context->co_context;
2330 }
2331
2332 gboolean _ps_context_set_connected(gpointer object, gboolean enabled)
2333 {
2334         gchar *ipv4 = NULL;
2335         gchar *ipv6 = NULL;
2336         ps_context_t *context = object;
2337         Storage *strg_vconf = NULL;
2338         gpointer p_modem = NULL;
2339
2340         gboolean b_roaming_checker = TRUE;
2341         gboolean data_allowed = FALSE;
2342         gboolean b_mms_checker = FALSE;
2343         gboolean b_ims_checker = TRUE;
2344
2345         enum co_context_role role = CONTEXT_ROLE_UNKNOWN;
2346
2347         dbg("Entry [enabled :%d]", enabled);
2348
2349         g_return_val_if_fail(context != NULL, FALSE);
2350
2351         strg_vconf = tcore_server_find_storage(tcore_plugin_ref_server(context->plg), "vconf");
2352         data_allowed = tcore_storage_get_bool(strg_vconf, STORAGE_KEY_3G_ENABLE);
2353         ipv4 = tcore_context_get_ipv4_addr(context->co_context);
2354         ipv6 = tcore_context_get_ipv6_addr(context->co_context);
2355         role = tcore_context_get_role(context->co_context);
2356         p_modem = _ps_service_ref_modem(context->p_service);
2357
2358         if (role == CONTEXT_ROLE_MMS || role == CONTEXT_ROLE_PREPAID_MMS)
2359                 b_mms_checker = TRUE;
2360
2361         if (role == CONTEXT_ROLE_IMS || role == CONTEXT_ROLE_IMS_EMERGENCY)
2362                 b_ims_checker = TRUE;
2363
2364 #if !defined(TIZEN_SUPPORT_MMS_CONNECT_FORCE)
2365                 ps_dbg_ex_co(_ps_service_ref_co_network(_ps_context_ref_service(context)), "csc runtime feature disabled");
2366                 b_mms_checker = FALSE;
2367 #endif
2368
2369         context->b_active = enabled;
2370         if ((_ps_modem_get_roaming(p_modem)) && !(_ps_modem_get_data_roaming_allowed(p_modem))) {
2371                 dbg("roaming network is not allowed");
2372                 b_roaming_checker = FALSE;
2373         }
2374
2375         if (enabled) {
2376                 gint ps_mode = 0;
2377
2378                 tcore_context_set_state(context->co_context, CONTEXT_STATE_ACTIVATED);
2379
2380                 if (context->deact_required == TRUE) {
2381                         warn("Deactivation is required for context(%p)", context);
2382                         _ps_service_deactivate_context(context->p_service, context);
2383                         context->deact_required = FALSE;
2384                         goto EXIT;
2385                 }
2386
2387                 if (ipv4) {
2388                         if ((g_str_equal(ipv4, "0.0.0.0") == TRUE) && (ipv6 == NULL)) {
2389                                 dbg("ip address is NULL");
2390                                 _ps_service_deactivate_context(context->p_service, context);
2391                                 goto EXIT;
2392                         }
2393                 }
2394                 _ps_service_reset_connection_timer(context);
2395
2396                 /* In case of PDP is disconnected by network in UPS mode.
2397                  * default internet PDP could be activated if LCD was on at trigger time.
2398                  */
2399                 ps_mode = _ps_modem_get_psmode(p_modem);
2400                 warn("ps_mode: %d", ps_mode);
2401                 if (ps_mode > POWER_SAVING_MODE_NORMAL && ps_mode < POWER_SAVING_MODE_WEARABLE) {
2402                         gint pm_state = tcore_storage_get_int(strg_vconf, STORAGE_KEY_PM_STATE);
2403                         if (pm_state == 3 && _ps_context_get_default_context(context, CONTEXT_ROLE_INTERNET)) {
2404                                 char *devname = tcore_context_get_ipv4_devname(context->co_context);
2405                                 if (TCORE_RETURN_SUCCESS != tcore_util_netif_down(devname))
2406                                         err("Failed to bring up interface");
2407
2408                                 warn("[EXCEPTION] do not emit signal for PDP activation.");
2409                                 context->b_notify = TRUE;
2410                                 goto EXIT;
2411                         }
2412                 }
2413
2414                 if (b_roaming_checker && (data_allowed || b_mms_checker || b_ims_checker))
2415                         __ps_context_emit_property_changed_signal(context);
2416
2417         } else {
2418                 tcore_context_set_state(context->co_context, CONTEXT_STATE_DEACTIVATED);
2419                 tcore_context_reset_devinfo(context->co_context);
2420                 __ps_context_emit_property_changed_signal(context);
2421         }
2422 EXIT:
2423         if (ipv4)
2424                 free(ipv4);
2425
2426         g_free(ipv6);
2427
2428         return TRUE;
2429 }
2430
2431 gboolean _ps_context_set_profile_enable(gpointer object, gboolean value)
2432 {
2433         ps_context_t *context = object;
2434         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(context));
2435
2436         dbg("Entry [value :%d]", value);
2437
2438         g_return_val_if_fail(context != NULL, FALSE);
2439
2440         context->profile_enable = value;
2441         ps_dbg_ex_co(co_network, "context(%p) profile_enable(%d)", context, context->profile_enable);
2442         return TRUE;
2443 }
2444
2445 gboolean _ps_context_get_profile_enable(gpointer object)
2446 {
2447         ps_context_t *context = object;
2448
2449         ps_dbg_ex_co(_ps_service_ref_co_network(_ps_context_ref_service(context)), "context(%p), profile_enable(%d)", context, context->profile_enable);
2450
2451         return context->profile_enable;
2452 }
2453
2454 gboolean _ps_context_set_ps_defined(gpointer object, gboolean value)
2455 {
2456         ps_context_t *context = (ps_context_t *)object;
2457         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(context));
2458
2459         dbg("Entry [value :%d]", value);
2460
2461         g_return_val_if_fail(context != NULL, FALSE);
2462
2463         context->ps_defined = value;
2464         ps_dbg_ex_co(co_network, "context(%p) ps_defined(%d)", context, context->ps_defined);
2465         return TRUE;
2466 }
2467
2468 gboolean _ps_context_get_ps_defined(gpointer object)
2469 {
2470         ps_context_t *context = (ps_context_t *)object;
2471
2472         ps_dbg_ex_co(_ps_service_ref_co_network(_ps_context_ref_service(context)), "context(%p), ps_defined(%d)", context, context->ps_defined);
2473
2474         return context->ps_defined;
2475 }
2476
2477 gboolean _ps_context_reset_user_data(gpointer object)
2478 {
2479         ps_context_t *context = (ps_context_t *)object;
2480
2481         g_return_val_if_fail(context != NULL, FALSE);
2482         context->user_data = NULL;
2483
2484         return TRUE;
2485 }
2486
2487 gboolean _ps_context_set_bearer_info(gpointer object, struct tnoti_ps_dedicated_bearer_info *bearer_info)
2488 {
2489         ps_context_t *context = (ps_context_t *)object;
2490         CoreObject *co_context = NULL;
2491
2492         g_return_val_if_fail(context != NULL, FALSE);
2493         g_return_val_if_fail(bearer_info != NULL, FALSE);
2494
2495         co_context = _ps_context_ref_co_context(object);
2496
2497         if (bearer_info->dedicated_bearer.num_dedicated_bearer > 0) {
2498                 warn("num_dedicated_bearer: %d", bearer_info->dedicated_bearer.num_dedicated_bearer);
2499                 /* reset previous bearer info. */
2500                 tcore_context_reset_bearer_info(co_context);
2501                 tcore_context_set_bearer_info(co_context, bearer_info);
2502                 __ps_context_emit_dedicated_bearer_info_signal(context);
2503         }
2504
2505         return TRUE;
2506 }
2507
2508 gpointer _ps_context_get_user_data(gpointer object)
2509 {
2510         ps_context_t *context = (ps_context_t *)object;
2511         return context->user_data;
2512 }
2513
2514 TReturn _ps_connection_hdlr(gpointer object)
2515 {
2516         int rv = TCORE_RETURN_FAILURE;
2517         ps_context_t *pscontext = object;
2518         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(pscontext));
2519
2520         _ps_context_set_alwayson_enable(pscontext, TRUE);
2521         rv = _ps_service_activate_context(pscontext->p_service, pscontext);
2522         if (rv != TCORE_RETURN_SUCCESS) {
2523                 ps_dbg_ex_co(co_network, "fail to activate context connection");
2524                 return rv;
2525         }
2526
2527         ps_dbg_ex_co(co_network, "success to activate context");
2528         return rv;
2529 }
2530
2531 gboolean _ps_context_create_cdma_profile(gchar *mccmnc, gchar *cp_name)
2532 {
2533         gpointer handle;
2534         gboolean rv = FALSE;
2535         GHashTable *in_param;
2536         char szQuery[5000];
2537
2538         /* Initialize Storage */
2539         if (g_str_has_suffix(cp_name, "1"))
2540                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_1);
2541         else
2542                 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH_0);
2543         if (handle == NULL) {
2544                 err("Failed to get Storage handle");
2545                 return FALSE;
2546         }
2547
2548         /* Make default MMS profile for CDMA */
2549         memset(szQuery, 0, 5000);
2550         snprintf(szQuery, sizeof(szQuery), "%s",
2551         " update pdp_profile set  \
2552          home_url = ? \
2553          where profile_id = 1 and svc_category_id = 2 ");
2554
2555         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
2556
2557         g_hash_table_insert(in_param, "1", g_strdup("http://mms.vtext.com/servlets/mms")); /* home_url */
2558
2559         rv = tcore_storage_insert_query_database(strg_db, handle, szQuery, in_param);
2560         dbg("insert into pdp_profile result(%d)", rv);
2561         g_hash_table_destroy(in_param);
2562
2563         /* De-initialize Storage */
2564         tcore_storage_remove_handle(strg_db, handle);
2565
2566         return rv;
2567 }
2568
2569 void _ps_default_connection_hdlr(gpointer object)
2570 {
2571         ps_context_t *pscontext = (ps_context_t *)object;
2572         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(pscontext));
2573
2574         __ps_context_update_default_internet_to_db(pscontext, TRUE);
2575
2576         /* set request profile */
2577         tcore_context_set_default_profile(pscontext->co_context, TRUE);
2578         __ps_context_set_default_connection_enable(pscontext, TRUE);
2579         _ps_context_set_alwayson_enable(pscontext, TRUE);
2580         __ps_context_emit_property_changed_signal(pscontext);
2581
2582         if (CONTEXT_ROLE_INTERNET == tcore_context_get_role(_ps_context_ref_co_context(object))) {
2583                 int rv = TCORE_RETURN_FAILURE;
2584                 ps_dbg_ex_co(co_network, "activation requeset in new ps (%p)", pscontext);
2585                 /* request to connect */
2586                 _ps_service_reset_connection_timer(pscontext);
2587                 rv = _ps_service_connect_default_context(pscontext->p_service);
2588                 if (rv == TCORE_RETURN_PS_NETWORK_NOT_READY) {
2589                         unsigned char cid = -1;
2590                         ps_service_t * p_service = (ps_service_t *)pscontext->p_service;
2591
2592                         ps_dbg_ex_co(co_network, "PS is not attached yet, release resources.");
2593
2594                         cid = tcore_context_get_id(pscontext->co_context);
2595                         _ps_context_set_ps_defined((gpointer)pscontext, FALSE);
2596                         tcore_ps_set_cid_active(p_service->co_ps, cid, FALSE);
2597                         tcore_ps_clear_context_id(p_service->co_ps, pscontext->co_context);
2598                 }
2599         }
2600         ps_dbg_ex_co(co_network, "complete to change the default connection");
2601         return;
2602 }
2603
2604 gint _ps_context_get_number_of_pdn(gchar *operator, gchar *cp_name)
2605 {
2606         gint num_of_pdn = 0;
2607
2608         num_of_pdn = __ps_context_load_num_of_pdn_from_database(operator, cp_name);
2609         dbg("loaded num_of_pdn (%d)", num_of_pdn);
2610
2611         return num_of_pdn;
2612 }
2613
2614 gboolean _ps_context_handle_ifaceup(gpointer user_data)
2615 {
2616         ps_context_t *pscontext = user_data;
2617         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(pscontext));
2618         int context_state = 0;
2619
2620         context_state =  tcore_context_get_state(pscontext->co_context);
2621         ps_warn_ex_co(co_network, "context_state: %d", context_state);
2622         if (context_state == CONTEXT_STATE_ACTIVATED) {
2623                 char *devname = tcore_context_get_ipv4_devname(pscontext->co_context);
2624                 gint ps_mode = _ps_modem_get_psmode(_ps_service_ref_modem(pscontext->p_service));
2625                 if (TCORE_RETURN_SUCCESS != tcore_util_netif_up(devname))
2626                         ps_err_ex_co(co_network, "Failed to bring up interface");
2627
2628                 if (ps_mode > POWER_SAVING_MODE_NORMAL && ps_mode < POWER_SAVING_MODE_WEARABLE) {
2629                         /* If this flag is true, Connman won't update cellular service state. */
2630                         if (pscontext->b_notify) {
2631                                 ps_warn_ex_co(co_network, "[EXCEPTION] notify status change to upper layer.");
2632                                 pscontext->b_routing_only = FALSE;
2633                         } else {
2634                                 pscontext->b_routing_only = TRUE;
2635                         }
2636                 }
2637                 /*
2638                  * 20131212, Deprecated: Fixed by HTTP stack.
2639                  * ===============================================================================
2640                  * 20130801, JIRA DCM-2221: HTTP communication behavior while bearer switching
2641                  * Observations: When contiguous HTTP requests while ME handovers from wi-fi to 3G,
2642                  * libcurl does not make Aborted event to application
2643                  * even if libcurl receives socket closed event by SIOCKILLADDR.
2644                  * So, we add work-around patch set here.
2645                  * ===============================================================================
2646                  */
2647                 if (pscontext->b_active == FALSE) {
2648                         pscontext->b_active = TRUE;
2649                         ps_dbg_ex_co(co_network, "Celluar profile: Emit property signal to provide IP configuration, devname(%s)", devname);
2650                         __ps_context_emit_property_changed_signal(pscontext);
2651                 }
2652                 pscontext->b_routing_only = FALSE;
2653                 pscontext->b_notify = FALSE;
2654                 return TRUE;
2655         } else if (context_state == CONTEXT_STATE_DEACTIVATED) {
2656                 /* trigger PDP activation.  */
2657                 _ps_service_activate_context(pscontext->p_service, pscontext);
2658         }
2659         return FALSE;
2660 }
2661
2662 gboolean _ps_context_handle_ifacedown(gpointer user_data)
2663 {
2664         ps_context_t *pscontext = user_data;
2665         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(pscontext));
2666         int context_state = 0;
2667
2668         context_state =  tcore_context_get_state(pscontext->co_context);
2669         ps_warn_ex_co(co_network, "context_state: %d", context_state);
2670         if (context_state == CONTEXT_STATE_ACTIVATED) {
2671                 char *devname = tcore_context_get_ipv4_devname(pscontext->co_context);
2672                 gint ps_mode = _ps_modem_get_psmode(_ps_service_ref_modem(pscontext->p_service));
2673                 ps_dbg_ex_co(co_network, "Cellular profile: Do not send PDP deactivation request message to Modem.");
2674                 if (ps_mode > POWER_SAVING_MODE_NORMAL && ps_mode < POWER_SAVING_MODE_WEARABLE) {
2675                         /* If this flag is true, Connman won't update cellular service state. */
2676                         pscontext->b_routing_only = TRUE;
2677                 } else {
2678                         ps_warn_ex_co(co_network, "reset socket connections, devname(%s)", devname);
2679                         if (TCORE_RETURN_SUCCESS != tcore_util_reset_ipv4_socket(devname, tcore_context_get_ipv4_addr(pscontext->co_context)))
2680                                 ps_err_ex_co(co_network, "Failed to reset socket.");
2681                 }
2682
2683                 if (TCORE_RETURN_SUCCESS != tcore_util_netif_down(devname))
2684                         ps_err_ex_co(co_network, "Failed to bring down interface");
2685                 if (pscontext->b_active == TRUE) {
2686                         pscontext->b_active = FALSE;
2687                         __ps_context_emit_property_changed_signal(pscontext);
2688                 }
2689                 pscontext->b_routing_only = FALSE;
2690                 return TRUE;
2691         }
2692         return FALSE;
2693 }
2694
2695 static gboolean on_context_get_properties(PacketServiceContext *obj_context,
2696                 GDBusMethodInvocation *invocation,
2697                 gpointer user_data)
2698 {
2699         GVariant *gv = NULL;
2700         GVariantBuilder property;
2701         ps_context_t *pscontext = user_data;
2702         TcorePlugin *p = (pscontext) ? pscontext->plg : NULL;
2703         PsPrivInfo *priv_info = tcore_plugin_ref_user_data(p);
2704         cynara *p_cynara = (priv_info) ? priv_info->p_cynara : NULL;
2705
2706         if (!ps_util_check_access_control(p_cynara, invocation, AC_PS_PUBLIC, "r"))
2707                 return TRUE;
2708
2709         dbg("Entered");
2710         gv = _ps_context_get_properties(user_data, &property);
2711         packet_service_context_complete_get_properties(obj_context, invocation, gv);
2712         return TRUE;
2713 }
2714
2715 static gboolean on_context_get_profile(PacketServiceContext *obj_context,
2716                 GDBusMethodInvocation *invocation,
2717                 gpointer user_data)
2718 {
2719         GVariant *gv = NULL;
2720         GVariantBuilder profile;
2721         ps_context_t *pscontext = user_data;
2722         TcorePlugin *p = (pscontext) ? pscontext->plg : NULL;
2723         PsPrivInfo *priv_info = tcore_plugin_ref_user_data(p);
2724         cynara *p_cynara = (priv_info) ? priv_info->p_cynara : NULL;
2725
2726         if (!ps_util_check_access_control(p_cynara, invocation, AC_PS_PUBLIC, "r"))
2727                 return TRUE;
2728
2729         dbg("Entered");
2730         gv = __ps_context_get_profile_properties(user_data, &profile);
2731         packet_service_context_complete_get_profile(obj_context, invocation, gv);
2732
2733         return TRUE;
2734 }
2735
2736 static gboolean on_context_handle_activate(PacketServiceContext *obj_context,
2737                 GDBusMethodInvocation *invocation,
2738                 gpointer user_data)
2739 {
2740         gboolean rv = FALSE;
2741         TReturn result = TCORE_RETURN_FAILURE;
2742
2743         gchar *apn = NULL;
2744         int context_state = 0;
2745         unsigned int max_pdn = 0, num_of_active_cids = 0;
2746         gpointer p_service = NULL; gpointer co_ps = NULL;
2747         gpointer c_def_context = NULL; unsigned char cid_def = 0;
2748         GSList *active_cids = NULL;
2749         CoreObject *co_network;
2750
2751         ps_context_t *pscontext = user_data;
2752         TcorePlugin *p = (pscontext) ? pscontext->plg : NULL;
2753         PsPrivInfo *priv_info = tcore_plugin_ref_user_data(p);
2754         cynara *p_cynara = (priv_info) ? priv_info->p_cynara : NULL;
2755
2756         if (!ps_util_check_access_control(p_cynara, invocation, AC_PS_PRIVATE, "w"))
2757                 return TRUE;
2758
2759         dbg("Entered");
2760         if (pscontext == NULL) {
2761                 err("activation request object is NULL");
2762                 FAIL_RESPONSE(invocation,  PS_ERR_NO_PROFILE);
2763                 return TRUE;
2764         }
2765
2766         p_service = pscontext->p_service;
2767         if (!p_service) {
2768                 err("service object is null");
2769                 FAIL_RESPONSE(invocation,  PS_ERR_NO_PROFILE);
2770                 return TRUE;
2771         }
2772
2773         co_network = _ps_service_ref_co_network(_ps_context_ref_service(pscontext));
2774         co_ps = _ps_service_ref_co_ps(p_service);
2775         if (!co_ps) {
2776                 ps_err_ex_co(co_network, "core object is null");
2777                 FAIL_RESPONSE(invocation,  PS_ERR_NO_PROFILE);
2778                 return TRUE;
2779         }
2780
2781         ps_dbg_ex_co(co_network, "activate context(%s)", _ps_context_ref_path(pscontext));
2782
2783         apn = (gchar *)tcore_context_get_apn(pscontext->co_context);
2784
2785         context_state = tcore_context_get_state(pscontext->co_context);
2786         if (context_state != CONTEXT_STATE_DEACTIVATED) {
2787                 ps_warn_ex_co(co_network, "operation is in progress");
2788                 FAIL_RESPONSE(invocation,  PS_ERR_INTERNAL);
2789                 return TRUE;
2790         }
2791
2792         ps_dbg_ex_co(co_network, "requested context(%p) co_context(%p) apn (%s)", pscontext, pscontext->co_context, apn);
2793         /* check apn is activated or not */
2794         rv = tcore_ps_is_active_apn(co_ps, (const char *)apn);
2795         if (rv) {
2796                 ps_dbg_ex_co(co_network, "requested apn is already activated");
2797
2798                 result = _ps_connection_hdlr(pscontext);
2799                 if (result != TCORE_RETURN_SUCCESS) {
2800                         FAIL_RESPONSE(invocation,  PS_ERR_INTERNAL);
2801                         return TRUE;
2802                 }
2803
2804                 packet_service_context_complete_activate(obj_context, invocation, pscontext->path);
2805                 context_state = tcore_context_get_state(pscontext->co_context);
2806                 if (context_state == CONTEXT_STATE_ACTIVATED) {
2807                         ps_dbg_ex_co(co_network, "context is already connected");
2808                         _ps_context_set_connected(pscontext, TRUE);
2809                 }
2810
2811                 ps_dbg_ex_co(co_network, "success to open connection request");
2812                 return TRUE;
2813         }
2814
2815         active_cids = tcore_ps_get_active_cids(co_ps);
2816         num_of_active_cids = g_slist_length(active_cids);
2817         max_pdn = tcore_ps_get_num_of_pdn(co_ps);
2818         ps_dbg_ex_co(co_network, "activate cids(%d), max pdn(%d)", num_of_active_cids, max_pdn);
2819         if (num_of_active_cids < max_pdn) {
2820                 ps_dbg_ex_co(co_network, "enough to active another pdn");
2821                 result = _ps_connection_hdlr(pscontext);
2822                 if (result != TCORE_RETURN_SUCCESS) {
2823                         FAIL_RESPONSE(invocation,  PS_ERR_INTERNAL);
2824                         return TRUE;
2825                 }
2826
2827                 ps_dbg_ex_co(co_network, "success to open connection request");
2828                 packet_service_context_complete_activate(obj_context, invocation, pscontext->path);
2829                 return TRUE;
2830         }
2831
2832         /* find the current default connection */
2833         c_def_context = _ps_service_return_default_context(p_service, CONTEXT_ROLE_INTERNET);
2834         if (c_def_context == NULL) {
2835                 err("default context is NULL");
2836                 FAIL_RESPONSE(invocation,  PS_ERR_NO_PROFILE);
2837                 return TRUE;
2838         }
2839         cid_def = tcore_context_get_id(((ps_context_t *)c_def_context)->co_context);
2840         if (cid_def == 0) {
2841                 ps_err_ex_co(co_network, "it is not avaiable to open connection");
2842                 FAIL_RESPONSE(invocation,  PS_ERR_NO_PROFILE);
2843                 return TRUE;
2844         }
2845
2846         /* check the status of def context */
2847         context_state = tcore_context_get_state(((ps_context_t *)c_def_context)->co_context);
2848         if (context_state != CONTEXT_STATE_ACTIVATED) {
2849                 enum co_context_role context_role;
2850                 context_role = tcore_context_get_role(pscontext->co_context);
2851                 ps_err_ex_co(co_network, "default connection is in progress, new context_state[%d], role[%d]", context_state, context_role);
2852                 if (context_state == CONTEXT_STATE_ACTIVATING &&
2853                         (context_role == CONTEXT_ROLE_MMS ||context_role == CONTEXT_ROLE_PREPAID_MMS)) {
2854                         ((ps_context_t *)c_def_context)->user_data = pscontext;
2855                         packet_service_context_complete_activate(obj_context, invocation, pscontext->path);
2856                         return TRUE;
2857                 }
2858                 FAIL_RESPONSE(invocation,  PS_ERR_NO_PROFILE);
2859                 return TRUE;
2860         }
2861
2862         _ps_service_reset_connection_timer(c_def_context);
2863         ((ps_context_t *)c_def_context)->user_data = pscontext;
2864         result = tcore_ps_deactivate_cid(co_ps, cid_def);
2865         if (result != TCORE_RETURN_SUCCESS) {
2866                 ps_err_ex_co(co_network, "fail to deactivate exist network connection");
2867                 FAIL_RESPONSE(invocation,  PS_ERR_NO_PROFILE);
2868                 return TRUE;
2869         }
2870
2871         packet_service_context_complete_activate(obj_context, invocation, pscontext->path);
2872         return TRUE;
2873
2874 }
2875
2876 static gboolean on_context_handle_deactiavte(PacketServiceContext *obj_context,
2877                 GDBusMethodInvocation *invocation,
2878                 gpointer user_data)
2879 {
2880         gboolean rv = FALSE;
2881         CoreObject *co_network;
2882         int context_state = 0;
2883         ps_context_t *pscontext = user_data;
2884         TcorePlugin *p = (pscontext) ? pscontext->plg : NULL;
2885         PsPrivInfo *priv_info = tcore_plugin_ref_user_data(p);
2886         cynara *p_cynara = (priv_info) ? priv_info->p_cynara : NULL;
2887
2888         if (!ps_util_check_access_control(p_cynara, invocation, AC_PS_PRIVATE, "w"))
2889                 return TRUE;
2890
2891         dbg("Entered");
2892         if (pscontext == NULL) {
2893                 err("deactivation request object is NULL");
2894                 FAIL_RESPONSE(invocation,  PS_ERR_NO_PROFILE);
2895                 return TRUE;
2896         }
2897
2898         co_network = _ps_service_ref_co_network(_ps_context_ref_service(pscontext));
2899         context_state = tcore_context_get_state(pscontext->co_context);
2900         ps_dbg_ex_co(co_network, "requested context(%p) co_context(%p), context_state %d", pscontext, pscontext->co_context, context_state);
2901         if (context_state != CONTEXT_STATE_ACTIVATED) {
2902                 ps_err_ex_co(co_network, "operation is in progress");
2903                 FAIL_RESPONSE(invocation,  PS_ERR_INTERNAL);
2904                 return TRUE;
2905         }
2906
2907         ps_dbg_ex_co(co_network, "deactivate context(%s)", _ps_context_ref_path(pscontext));
2908
2909         _ps_service_reset_connection_timer(pscontext);
2910         _ps_context_set_alwayson_enable(pscontext, FALSE);
2911
2912         rv = _ps_service_deactivate_context(pscontext->p_service, pscontext);
2913         if (rv != TCORE_RETURN_SUCCESS) {
2914                 ps_err_ex_co(co_network, "fail to deactivate context connection");
2915                 FAIL_RESPONSE(invocation,  PS_ERR_TRASPORT);
2916                 return TRUE;
2917         }
2918
2919         ps_dbg_ex_co(co_network, "success to deactivate context");
2920         packet_service_context_complete_deactivate(obj_context, invocation, pscontext->path);
2921
2922         context_state =  tcore_context_get_state(pscontext->co_context);
2923         if (context_state == CONTEXT_STATE_DEACTIVATED) {
2924                 ps_dbg_ex_co(co_network, "context is already disconnected");
2925                 pscontext->ps_defined = FALSE;
2926                 _ps_context_set_connected(pscontext, FALSE);
2927         }
2928
2929         return TRUE;
2930 }
2931
2932 static gboolean on_context_set_default_connection(PacketServiceContext *obj_context,
2933                 GDBusMethodInvocation *invocation,
2934                 gpointer user_data)
2935 {
2936         int rv = 0;
2937         int role = CONTEXT_ROLE_UNKNOWN;
2938         gboolean is_default = FALSE;
2939
2940         gpointer co_ps = NULL;
2941         gpointer service = NULL;
2942         gpointer cur_default_ctx = NULL;
2943         ps_context_t *pscontext = user_data;
2944         TcorePlugin *p = (pscontext) ? pscontext->plg : NULL;
2945         PsPrivInfo *priv_info = tcore_plugin_ref_user_data(p);
2946         cynara *p_cynara = (priv_info) ? priv_info->p_cynara : NULL;
2947         CoreObject *co_network;
2948
2949         if (!ps_util_check_access_control(p_cynara, invocation, AC_PS_PROFILE, "w"))
2950                 return TRUE;
2951
2952         dbg("enter set default connection ps_context_t(%p)", pscontext);
2953         if (pscontext == NULL) {
2954                 err("activation request object is NULL");
2955                 FAIL_RESPONSE(invocation, PS_ERR_NO_PROFILE);
2956                 return TRUE;
2957         }
2958
2959         co_network = _ps_service_ref_co_network(_ps_context_ref_service(pscontext));
2960         ps_dbg_ex_co(co_network, "start default connection");
2961         role = tcore_context_get_role(pscontext->co_context);
2962         is_default = tcore_context_get_default_profile(pscontext->co_context);
2963
2964         if (is_default) {
2965                 ps_err_ex_co(co_network, "already default profile for role (%d).", role);
2966                 goto OUT;
2967         }
2968
2969         service = pscontext->p_service;
2970         cur_default_ctx = _ps_service_return_default_context(service, role);
2971         ps_dbg_ex_co(co_network, "current default connection (%p)", cur_default_ctx);
2972
2973         if (!cur_default_ctx) {
2974                 ps_err_ex_co(co_network, "No current default connection.");
2975                 goto OUT;
2976         }
2977
2978         /* First, send deactivation request first. */
2979         rv = _ps_service_deactivate_context(((ps_context_t *)cur_default_ctx)->p_service, cur_default_ctx);
2980         if (rv == TCORE_RETURN_PS_ACTIVATING) {
2981                 ps_dbg_ex_co(co_network, "fail to deactivate default connection, rv(%d)", rv);
2982                 FAIL_RESPONSE(invocation, PS_ERR_INTERNAL);
2983                 return TRUE;
2984         }
2985
2986         /* Normal deactivation case. */
2987         if (tcore_context_get_state(((ps_context_t *)cur_default_ctx)->co_context) == CONTEXT_STATE_DEACTIVATING) {
2988                 ps_dbg_ex_co(co_network, "deactivation request in current ps (%p)", cur_default_ctx);
2989                 ((ps_context_t *)cur_default_ctx)->user_data = pscontext;
2990         } else {
2991                 int cid = -1;
2992
2993                 ps_dbg_ex_co(co_network, "[Not normal] deactivation request in current ps (%p)", cur_default_ctx);
2994
2995                 cid = tcore_context_get_id(((ps_context_t *)cur_default_ctx)->co_context);
2996                 _ps_context_set_ps_defined(cur_default_ctx, FALSE);
2997                 co_ps = _ps_service_ref_co_ps(service);
2998                 tcore_ps_set_cid_active((CoreObject *)co_ps, cid, FALSE);
2999                 tcore_ps_clear_context_id((CoreObject *)co_ps, ((ps_context_t *)cur_default_ctx)->co_context);
3000         }
3001
3002         /* unset default info of previous connection */
3003         _ps_context_set_alwayson_enable(cur_default_ctx, FALSE);
3004         __ps_context_set_default_connection_enable(cur_default_ctx, FALSE);
3005         tcore_context_set_default_profile(((ps_context_t *)cur_default_ctx)->co_context, FALSE);
3006
3007         /* db update - release default connection */
3008         ps_dbg_ex_co(co_network, "context(%p): release default connection property.", cur_default_ctx);
3009         __ps_context_update_default_internet_to_db((ps_context_t *)cur_default_ctx, FALSE);
3010         /* Allow Connman to update profile information. */
3011         __ps_context_emit_property_changed_signal(cur_default_ctx);
3012
3013         ps_dbg_ex_co(co_network, "activation requeset in new ps (%p)", pscontext);
3014 OUT:
3015         _ps_default_connection_hdlr(pscontext);
3016         packet_service_context_complete_set_default_connection(obj_context, invocation, TRUE);
3017         return TRUE;
3018
3019 }
3020
3021 static gboolean on_context_modify_profile(PacketServiceContext *obj_context,
3022                 GDBusMethodInvocation *invocation,
3023                 GVariant *property,
3024                 gpointer user_data)
3025 {
3026         GVariantIter g_iter;
3027         gchar *g_value;
3028         gchar *g_key;
3029
3030         gboolean rv = FALSE;
3031         int context_state = 0;
3032         ps_context_t *context = user_data;
3033         TcorePlugin *p = (context) ? context->plg : NULL;
3034         PsPrivInfo *priv_info = tcore_plugin_ref_user_data(p);
3035         cynara *p_cynara = (priv_info) ? priv_info->p_cynara : NULL;
3036         CoreObject *co_network = _ps_service_ref_co_network(_ps_context_ref_service(context));
3037         GHashTable *profile_property = NULL;
3038
3039         if (!ps_util_check_access_control(p_cynara, invocation, AC_PS_PROFILE, "w"))
3040                 return TRUE;
3041
3042         ps_dbg_ex_co(co_network, "modify context's profile properties");
3043
3044         context_state = tcore_context_get_state(context->co_context);
3045         if (context_state == CONTEXT_STATE_ACTIVATING) {
3046                 ps_dbg_ex_co(co_network, "Modify profile in activating state, set deactivate flag.");
3047                 context->deact_required = TRUE;
3048         }
3049
3050         /*Creating the profile property hash for for internal handling*/
3051         /*Create a hash table for the profile property as all fucntion already use ghash table */
3052         profile_property = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
3053         g_variant_iter_init(&g_iter, property);
3054         while (g_variant_iter_next(&g_iter, "{ss}", &g_key, &g_value)) {
3055
3056                 g_hash_table_insert(profile_property, g_strdup(g_key), g_strdup(g_value));
3057                 /* must free data for ourselves */
3058                 g_free(g_value);
3059                 g_free(g_key);
3060         }
3061
3062         rv = __ps_context_update_profile(context, profile_property);
3063         if (rv != TRUE) {
3064                 FAIL_RESPONSE(invocation,  PS_ERR_INTERNAL);
3065                 g_hash_table_destroy(profile_property);
3066                 return TRUE;
3067         }
3068
3069         _ps_service_deactivate_context(context->p_service, context);
3070         if (context_state == CONTEXT_STATE_DEACTIVATED) {
3071                 ps_dbg_ex_co(co_network, "context is already disconnected");
3072                 _ps_context_set_connected(context, FALSE);
3073         }
3074
3075         packet_service_context_complete_modify_profile(obj_context, invocation, TRUE);
3076         g_hash_table_destroy(profile_property);
3077         return TRUE;
3078 }
3079
3080 static gboolean on_context_remove_profile(PacketServiceContext *obj_context,
3081         GDBusMethodInvocation *invocation, gpointer user_data)
3082 {
3083         ps_context_t *context = user_data;
3084         ps_service_t *service = _ps_context_ref_service(context);
3085         CoreObject *co_network = _ps_service_ref_co_network(service);
3086         TcorePlugin *p = (context) ? context->plg : NULL;
3087         PsPrivInfo *priv_info = tcore_plugin_ref_user_data(p);
3088         cynara *p_cynara = (priv_info) ? priv_info->p_cynara : NULL;
3089
3090         if (!ps_util_check_access_control(p_cynara, invocation, AC_PS_PROFILE, "w"))
3091                 return TRUE;
3092         g_return_val_if_fail(service != NULL, FALSE);
3093
3094         ps_dbg_ex_co(co_network, "Remove context.");
3095
3096         __ps_context_remove_database(context);
3097         _ps_service_unref_context(service, context);
3098         service->contexts = g_slist_remove(service->contexts, context);
3099         _ps_context_remove_context(context);
3100
3101         packet_service_context_complete_remove_profile(obj_context, invocation, TRUE);
3102
3103         ps_dbg_ex_co(co_network, "Exit");
3104         return TRUE;
3105 }
3106
3107 static void _ps_context_setup_interface(PacketServiceContext *context, ps_context_t *context_data)
3108 {
3109         dbg("Entered");
3110         g_signal_connect(context,
3111                         "handle-get-properties",
3112                         G_CALLBACK(on_context_get_properties),
3113                         context_data);
3114
3115         g_signal_connect(context,
3116                         "handle-get-profile",
3117                         G_CALLBACK(on_context_get_profile),
3118                         context_data);
3119
3120         g_signal_connect(context,
3121                         "handle-activate",
3122                         G_CALLBACK(on_context_handle_activate),
3123                         context_data);
3124
3125         g_signal_connect(context,
3126                         "handle-deactivate",
3127                         G_CALLBACK(on_context_handle_deactiavte),
3128                         context_data);
3129
3130         g_signal_connect(context,
3131                         "handle-set-default-connection",
3132                         G_CALLBACK(on_context_set_default_connection),
3133                         context_data);
3134
3135         g_signal_connect(context,
3136                         "handle-modify-profile",
3137                         G_CALLBACK(on_context_modify_profile),
3138                         context_data);
3139
3140         g_signal_connect(context,
3141                         "handle-remove-profile",
3142                         G_CALLBACK(on_context_remove_profile),
3143                         context_data);
3144         dbg("Exiting");
3145         return;
3146 }