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