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