2 * PacketService Control Module
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: DongHoo Park <donghoo.park@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include "ps-context.h"
31 #include <core_object.h>
32 #include <co_context.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
37 #define PROP_DEFAULT FALSE
38 #define PROP_DEFAULT_STR NULL
39 #define BOOL2STRING(a) ((a==TRUE) ? ("TRUE"):("FALSE"))
40 #define DATABASE_PATH "/opt/dbspace/.dnet.db"
53 SIG_CONTEXT_PROPERTY_CHANGED,
57 static guint32 signals[SIG_CONTEXT_LAST] = { 0, };
59 struct PsContextClass {
63 void (*property_changed)(PsContext *context, GHashTable *context_property);
71 DBusGConnection *conn;
77 CoreObject *co_context;
80 static Storage *strg_db;
81 static gpointer handle;
82 static GHashTable *contexts;
84 G_DEFINE_TYPE(PsContext, ps_context, G_TYPE_OBJECT);
86 static void __ps_context_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
87 static void __ps_context_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
89 gboolean ps_iface_context_get_properties(PsContext *pscontext, DBusGMethodInvocation *context);
90 gboolean ps_iface_context_get_profile(PsContext *pscontext, DBusGMethodInvocation *context);
91 gboolean ps_iface_context_activate(PsContext *pscontext, DBusGMethodInvocation *context);
92 gboolean ps_iface_context_deactivate(PsContext *pscontext, DBusGMethodInvocation *context);
93 gboolean ps_iface_context_modify_profile(PsContext *pscontext, GHashTable *profile_property, gboolean* result, GError **error);
94 gboolean ps_iface_context_remove_profile(PsContext *pscontext, gboolean* result, GError **error);
96 static void __ps_context_emit_property_changed_signal(PsContext *context);
98 static void __remove_context(gpointer data);
99 static gboolean __remove_contexts(gpointer key, gpointer value, gpointer user_data);
100 static gboolean __ps_context_remove_context(PsContext *context);
101 static gboolean __ps_context_create_storage_handle(gpointer plugin);
102 static gboolean __ps_context_create_context_hash(void);
103 static gchar* __ps_context_create_path(char *profile_name,int svc_ctg_id);
104 static gboolean __ps_context_create_co_context(gpointer context, GHashTable *property);
105 static gboolean __ps_context_update_profile(PsContext *context, GHashTable *property);
106 static gboolean __ps_context_update_database(PsContext *context);
107 static gboolean __ps_context_remove_database(PsContext *context);
108 static int __ps_context_insert_network_id_to_database(gchar *mccmnc);
109 static int __ps_context_load_network_id_from_database(gchar *mccmnc);
110 static int __ps_context_load_profile_id_from_database(void);
111 static int __ps_context_insert_profile_to_database(GHashTable *property, int network_id);
112 static gboolean __ps_context_reset_database(void);
113 static int __ps_context_get_network_id(gchar *mccmnc);
114 static gboolean __ps_context_get_profile_properties(gpointer context, GHashTable *properties);
115 static gboolean __ps_context_set_alwayson_enable(gpointer object, gboolean enabled);
117 #include "ps-iface-context-glue.h"
119 static void ps_context_init(PsContext *context)
121 dbg("context initialize");
123 context->path = NULL;
124 context->mccmnc = NULL;
125 context->conn = NULL;
128 context->alwayson = PROP_DEFAULT;
129 context->p_service = NULL;
130 context->profile_id = 0;
131 context->co_context = NULL;
136 static void ps_context_class_init(PsContextClass *klass)
138 GObjectClass *object_class = G_OBJECT_CLASS(klass);
141 dbg("context class init");
143 object_class->get_property = __ps_context_get_property;
144 object_class->set_property = __ps_context_set_property;
147 dbus_g_object_type_install_info(PS_TYPE_CONTEXT, &dbus_glib_ps_iface_context_object_info);
150 g_object_class_install_property(
153 g_param_spec_boxed("conn", "CONNECTION", "DBus connection", DBUS_TYPE_G_CONNECTION,
154 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
155 g_object_class_install_property(
158 g_param_spec_string("path", "PATH", "Context Path", PROP_DEFAULT_STR,
160 g_object_class_install_property(
163 g_param_spec_string("mccmnc", "MCCMNC", "Profile Country and Context Provider Code",
164 PROP_DEFAULT_STR, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
165 g_object_class_install_property(
168 g_param_spec_pointer("plg", "PLUGIN", "Plug in Object",
169 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
172 signals[SIG_CONTEXT_PROPERTY_CHANGED] = g_signal_new("property-changed",
173 G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_LAST,
174 G_STRUCT_OFFSET(PsContextClass, property_changed), NULL, NULL,
175 g_cclosure_marshal_VOID__BOXED, G_TYPE_NONE, 1, DBUS_TYPE_G_STRING_STRING_HASHTABLE);
180 static void __ps_context_set_property(GObject *object, guint prop_id, const GValue *value,
183 PsContext *context = PS_CONTEXT(object);
186 case PROP_CONTEXT_CONN: {
187 context->conn = g_value_get_boxed(value);
188 msg("context (%p) set conn(%p)", context, context->conn);
191 case PROP_CONTEXT_PLUGIN: {
192 context->plg = g_value_get_pointer(value);
193 msg("context (%p) set plg(%p)", context, context->plg);
196 case PROP_CONTEXT_PATH: {
198 g_free(context->path);
200 context->path = g_value_dup_string(value);
201 msg("context (%p) path(%s)", context, context->path);
204 case PROP_CONTEXT_MCCMNC: {
205 if (context->mccmnc) {
206 g_free(context->mccmnc);
208 context->mccmnc = g_value_dup_string(value);
209 msg("context (%p) mccmnc(%s)", context, context->mccmnc);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
220 static void __ps_context_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
225 static void __ps_context_emit_property_changed_signal(PsContext *context)
227 GHashTable *property;
229 property = g_hash_table_new(g_str_hash, g_str_equal);
230 _ps_context_get_properties(context, property);
231 g_signal_emit(context, signals[SIG_CONTEXT_PROPERTY_CHANGED], 0, property);
232 g_hash_table_destroy(property);
233 dbg("context (%p) emit the context property changed signal", context);
237 gboolean ps_iface_context_get_properties(PsContext *pscontext, DBusGMethodInvocation *context)
239 GHashTable *property;
241 dbg("Get properties of context(%s)", _ps_context_ref_path(pscontext));
242 property = g_hash_table_new(g_str_hash, g_str_equal);
243 _ps_context_get_properties(pscontext, property);
245 dbus_g_method_return(context, property);
246 g_hash_table_destroy(property);
250 gboolean ps_iface_context_get_profile(PsContext *pscontext, DBusGMethodInvocation *context)
254 dbg("Get profile properties of context(%s)", _ps_context_ref_path(pscontext));
255 profile = g_hash_table_new(g_str_hash, g_str_equal);
256 __ps_context_get_profile_properties(pscontext, profile);
258 dbus_g_method_return(context, profile);
259 g_hash_table_destroy(profile);
263 gboolean ps_iface_context_activate(PsContext *pscontext, DBusGMethodInvocation *context)
266 int context_state = 0;
267 GError *error = NULL;
269 dbg("activate context(%s)", _ps_context_ref_path(pscontext));
271 /*support always on connection*/
272 __ps_context_set_alwayson_enable(pscontext, TRUE);
273 _ps_service_reset_connection_timer(pscontext);
275 rv = _ps_service_activate_context(pscontext->p_service, pscontext);
276 if (rv != TCORE_RETURN_SUCCESS) {
277 dbg("fail to activate context connection");
278 g_set_error(&error, PS_ERROR, PS_ERR_TRASPORT, "fail to activate context err(%d)", rv);
282 dbg("success to activate context");
283 dbus_g_method_return(context, pscontext->path);
285 context_state = tcore_context_get_state(pscontext->co_context);
286 if (context_state == CONTEXT_STATE_ACTIVATED) {
287 dbg("context is already connected");
288 _ps_context_set_connected(pscontext, TRUE);
293 FAIL: dbus_g_method_return_error(context, error);
297 gboolean ps_iface_context_deactivate(PsContext *pscontext, DBusGMethodInvocation *context)
300 int context_state = 0;
301 GError *error = NULL;
303 dbg("deactivate context(%s)", _ps_context_ref_path(pscontext));
305 __ps_context_set_alwayson_enable(pscontext, FALSE);
306 rv = _ps_service_deactivate_context(pscontext->p_service, pscontext);
307 if (rv != TCORE_RETURN_SUCCESS) {
308 dbg("fail to deactivate context connection");
309 g_set_error(&error, PS_ERROR, PS_ERR_TRASPORT, "fail to deactivate context err(%d)", rv);
313 dbg("success to deactivate context");
314 dbus_g_method_return(context, pscontext->path);
316 context_state = tcore_context_get_state(pscontext->co_context);
317 if (context_state == CONTEXT_STATE_DEACTIVATED) {
318 dbg("context is already disconnected");
319 _ps_context_set_connected(pscontext, FALSE);
324 FAIL: dbus_g_method_return_error(context, error);
328 gboolean ps_iface_context_modify_profile(PsContext *context, GHashTable *profile_property,
329 gboolean* result, GError **error)
332 int context_state = 0;
335 dbg("modify context's profile properties");
337 rv = __ps_context_update_profile(context, profile_property);
339 g_set_error(error, PS_ERROR, PS_ERR_INTERNAL, "fail to modify profile");
344 context_state = tcore_context_get_state(context->co_context);
345 if (context_state == CONTEXT_STATE_DEACTIVATED)
348 _ps_service_deactivate_context(context->p_service, context);
349 context_state = tcore_context_get_state(context->co_context);
350 if (context_state == CONTEXT_STATE_DEACTIVATED) {
351 dbg("context is already disconnected");
352 _ps_context_set_connected(context, FALSE);
358 gboolean ps_iface_context_remove_profile(PsContext *context, gboolean* result, GError **error)
361 __ps_context_remove_database(context);
362 rv = __ps_context_remove_context(context);
367 static void __remove_context(gpointer data)
369 dbg("context removed");
373 static gboolean __remove_contexts(gpointer key, gpointer value, gpointer user_data)
375 gchar *context_path = (gchar *) key;
376 dbg("context(%s) remove", context_path);
377 __ps_context_remove_context(value);
381 static gboolean __ps_context_remove_context(PsContext *context)
383 dbg("remove context and profile");
385 dbus_g_connection_unregister_g_object(context->conn, (GObject *) context);
387 __ps_context_set_alwayson_enable(context, FALSE);
388 _ps_service_deactivate_context(context->p_service, context);
389 _ps_context_set_connected(context, FALSE);
390 _ps_service_unref_context(context->p_service, context);
391 g_hash_table_remove(contexts, _ps_context_ref_path(context));
393 tcore_context_free(context->co_context);
394 //__ps_context_remove_database(context);
395 g_object_unref(context);
400 static gboolean __ps_context_create_storage_handle(gpointer plugin)
402 TcorePlugin *p = plugin;
403 Server *s = tcore_plugin_ref_server(p);
404 strg_db = tcore_server_find_storage(s, "database");
406 handle = tcore_storage_create_handle(strg_db, DATABASE_PATH);
408 err("fail to create database handle");
410 dbg("storage(%p) handle (%p)", strg_db, handle);
414 static gboolean __ps_context_create_context_hash()
416 g_return_val_if_fail(contexts == NULL, FALSE);
418 contexts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, __remove_context);
419 if (contexts == NULL) {
420 err("fail to create context hashtable");
424 dbg("context hashtable(%p)", contexts);
428 static gchar* __ps_context_create_path(char *profile_name, int svc_ctg_id)
430 gchar **strv, *str, *path;
432 strv = g_strsplit(profile_name, " ", 0);
433 str = g_strjoinv("_", strv);
434 path = g_strdup_printf("/context/%s_%d", str, svc_ctg_id);
437 dbg("path (%s)", path);
442 static gboolean __ps_context_create_co_context(gpointer object, GHashTable *property)
446 PsContext *context = NULL;
447 CoreObject *co_context = NULL;
451 gchar *profile_name = NULL;
454 gchar *auth_id = NULL, *auth_pwd = NULL, *home_url = NULL, *proxy_addr = NULL;
455 int pdp_protocol = 0, svc_ctg_id = 0;
457 g_hash_table_iter_init(&iter, (GHashTable *) property);
458 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
459 if (g_str_equal(key, "3") == TRUE) { /*Profile ID*/
460 profile_id = atoi((const char*) value);
461 dbg("profile id (%d)", profile_id);
463 else if (g_str_equal(key, "4") == TRUE) {
464 profile_name = g_strdup((const char*) value);
465 dbg("profile name (%s)", profile_name);
467 else if (g_str_equal(key, "5") == TRUE) {
468 apn = g_strdup((const char*) value);
469 dbg("APN (%s)", apn);
471 else if (g_str_equal(key, "6") == TRUE) {
472 auth_type = atoi((const char*) value);
473 dbg("auth type (%d)", auth_type);
475 else if (g_str_equal(key, "7") == TRUE) {
476 auth_id = g_strdup((const char*) value);
477 dbg("auth id (%s)", auth_id);
479 else if (g_str_equal(key, "8") == TRUE) {
480 auth_pwd = g_strdup((const char*) value);
481 dbg("auth pwd (%s)", auth_pwd);
483 else if (g_str_equal(key, "9") == TRUE) {
484 proxy_addr = g_strdup((const char*) value);
485 dbg("proxy addr (%s)", proxy_addr);
487 else if (g_str_equal(key, "10") == TRUE) {
488 home_url = g_strdup((const char*) value);
489 dbg("home url (%s)", home_url);
491 else if (g_str_equal(key, "11") == TRUE) {
492 pdp_protocol = atoi((const char*) value);
493 dbg("pdp protocol (%d)", pdp_protocol);
495 else if (g_str_equal(key, "21") == TRUE) {
496 svc_ctg_id = atoi((const char*) value);
497 dbg("context category type (%d)", svc_ctg_id);
501 path = __ps_context_create_path(profile_name, svc_ctg_id);
503 context = (PsContext *) object;
504 co_context = tcore_context_new(context->plg, path, NULL);
505 tcore_context_set_state(co_context, CONTEXT_STATE_DEACTIVATED);
506 tcore_context_set_role(co_context, svc_ctg_id);
507 tcore_context_set_apn(co_context, apn);
508 tcore_context_set_auth(co_context, auth_type);
509 tcore_context_set_username(co_context, auth_id);
510 tcore_context_set_password(co_context, auth_pwd);
511 tcore_context_set_proxy(co_context, proxy_addr);
512 tcore_context_set_mmsurl(co_context, home_url);
514 context->profile_id = profile_id;
515 context->path = g_strdup(path);
516 context->co_context = co_context;
522 static gpointer __ps_context_create_context(DBusGConnection *conn, TcorePlugin *p,
523 gchar *mccmnc, GHashTable *property)
526 GError *error = NULL;
528 GObject *object = NULL;
531 proxy = dbus_g_proxy_new_for_name(conn, "org.freedesktop.DBus", "/org/freedesktop/DBus",
532 "org.freedesktop.DBus");
534 if (!dbus_g_proxy_call(proxy, "RequestName", &error, G_TYPE_STRING, PS_DBUS_SERVICE,
535 G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_UINT, &rv, G_TYPE_INVALID)) {
536 err("Failed to acquire context(%s) error(%s)", PS_DBUS_SERVICE, error->message);
540 object = g_object_new(PS_TYPE_CONTEXT, "conn", conn, "plg", p, "mccmnc", mccmnc);
542 __ps_context_create_co_context(object, property);
543 __ps_context_set_alwayson_enable(object, TRUE);
544 path = _ps_context_ref_path(object);
546 dbus_g_connection_register_g_object(conn, g_strdup(path), object);
547 msg("context(%p) register dbus path(%s)", object, path);
552 static gboolean __ps_context_update_profile(PsContext *context, GHashTable *property)
554 CoreObject *co_context = NULL;
558 co_context = context->co_context;
562 g_hash_table_iter_init(&iter, property);
563 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
564 if (g_str_equal(key, "apn") == TRUE) {
565 tcore_context_set_apn(co_context, (const char *) value);
567 else if (g_str_equal(key, "auth_type") == TRUE) {
569 i_tmp = atoi((const char *) value);
570 tcore_context_set_auth(co_context, i_tmp);
572 else if (g_str_equal(key, "auth_id") == TRUE) {
573 tcore_context_set_username(co_context, (const char *) value);
575 else if (g_str_equal(key, "auth_pwd") == TRUE) {
576 tcore_context_set_password(co_context, (const char *) value);
578 else if (g_str_equal(key, "proxy_addr") == TRUE) {
579 tcore_context_set_proxy(co_context, (const char *) value);
581 else if (g_str_equal(key, "home_url") == TRUE) {
582 tcore_context_set_mmsurl(co_context, (const char *) value);
586 return __ps_context_update_database(context);
589 static gboolean __ps_context_update_database(PsContext *context)
591 gchar *s_id = NULL, *s_authtype = NULL;
592 gchar *s_apn = NULL, *s_username = NULL, *s_pwd = NULL, *s_proxy = NULL, *s_mms = NULL;
596 GHashTable *in_param;
597 in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
599 strcpy(szQuery, " update pdp_profile set ");
600 strcat(szQuery, " apn = ?, auth_type = ?, auth_id = ?, auth_pwd = ?, ");
601 strcat(szQuery, " proxy_ip_addr = ?, home_url = ?");
602 strcat(szQuery, " where profile_id = ?");
604 s_id = g_strdup_printf("%d", context->profile_id);
605 s_authtype = g_strdup_printf("%d", tcore_context_get_auth(context->co_context));
607 s_apn = tcore_context_get_apn(context->co_context);
608 s_username = tcore_context_get_username(context->co_context);
609 s_pwd = tcore_context_get_password(context->co_context);
610 s_proxy = tcore_context_get_proxy(context->co_context);
611 s_mms = tcore_context_get_mmsurl(context->co_context);
613 g_hash_table_insert(in_param, "1", g_strdup(s_apn));
614 g_hash_table_insert(in_param, "2", g_strdup(s_authtype));
615 g_hash_table_insert(in_param, "3", g_strdup(s_username));
616 g_hash_table_insert(in_param, "4", g_strdup(s_pwd));
617 g_hash_table_insert(in_param, "5", g_strdup(s_proxy));
618 g_hash_table_insert(in_param, "6", g_strdup(s_mms));
619 g_hash_table_insert(in_param, "7", g_strdup(s_id));
621 rv = tcore_storage_update_query_database(strg_db, handle, szQuery, in_param);
622 g_hash_table_destroy(in_param);
635 static gboolean __ps_context_remove_database(PsContext *context)
641 GHashTable *in_param;
642 in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
644 memset(szQuery, 0, sizeof(szQuery));
645 strcpy(szQuery, " delete from pdp_profile where profile_id = ? ");
647 s_id = g_strdup_printf("%d", context->profile_id);
648 g_hash_table_insert(in_param, "1", g_strdup(s_id));
650 rv = tcore_storage_remove_query_database(strg_db, handle, szQuery, in_param);
652 g_hash_table_destroy(in_param);
657 static int __ps_context_insert_network_id_to_database(gchar *mccmnc)
662 gchar *insert_key = NULL;
665 GHashTable *in_param, *out_param;
667 in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
668 out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
669 (GDestroyNotify) g_hash_table_destroy);
671 memset(szQuery, 0, sizeof(szQuery));
672 strcpy(szQuery," select max(network_info_id) from network_info ");
674 tcore_storage_read_query_database(strg_db, handle, szQuery, NULL, out_param, 1);
676 g_hash_table_iter_init(&iter, out_param);
677 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
678 GHashTableIter iter2;
679 gpointer key2, value2;
682 g_hash_table_iter_init(&iter2, (GHashTable *) value);
683 while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
684 if (g_str_equal(key, "0") == TRUE) {
685 network_id = atoi((const char*) value2);
692 g_hash_table_destroy(out_param);
696 memset(szQuery, 0, sizeof(szQuery));
697 strcpy(szQuery," insert into network_info( network_info_id, network_name, mccmnc) values( ?, ?, ?) ");
699 insert_key = g_strdup_printf("%d", network_id);
700 g_hash_table_insert(in_param, "1", g_strdup(insert_key));
701 g_hash_table_insert(in_param, "2", "TEMP_NETWORK");
702 g_hash_table_insert(in_param, "3", g_strdup(mccmnc));
704 rv = tcore_storage_insert_query_database(strg_db, handle, szQuery, in_param);
712 static int __ps_context_insert_profile_to_database(GHashTable *property, int network_id)
720 GHashTable *in_param;
721 gchar *insert_key1 = NULL, *insert_key2 = NULL;
722 gchar *profile_name=NULL, *apn=NULL, *auth_type=NULL, *auth_id = NULL, *auth_pwd = NULL;
723 gchar *proxy_addr = NULL, *home_url = NULL, *svc_id = NULL;
725 in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
727 g_hash_table_iter_init(&iter, property);
728 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
730 if (g_str_equal(key, "apn") == TRUE) {
731 apn = g_strdup(value);
733 else if (g_str_equal(key, "auth_type") == TRUE) {
734 auth_type = g_strdup(value);
736 else if (g_str_equal(key, "auth_id") == TRUE) {
737 auth_id = g_strdup(value);
739 else if (g_str_equal(key, "auth_pwd") == TRUE) {
740 auth_pwd = g_strdup(value);
742 else if (g_str_equal(key, "proxy_addr") == TRUE) {
743 proxy_addr = g_strdup(value);
745 else if (g_str_equal(key, "home_url") == TRUE) {
746 home_url = g_strdup(value);
748 else if (g_str_equal(key, "svc_ctg_id") == TRUE) {
749 svc_id = g_strdup(value);
754 dbg("apn (%s), auth_type (%s), auth_id(%s), auth_pwd(%s), proxy_addr(%s), home_url(%s), svc_id(%s)",
755 apn, auth_type, auth_id, auth_pwd, proxy_addr, home_url, svc_id);
757 profile_id = __ps_context_load_profile_id_from_database();
759 dbg("fail to get last profile id");
764 memset(szQuery, 0, sizeof(szQuery));
765 strcpy(szQuery," insert into pdp_profile( ");
766 strcat(szQuery," profile_id, transport_type, profile_name, apn, auth_type, auth_id, auth_pwd, ");
767 strcat(szQuery," pdp_protocol, proxy_ip_addr, home_url, linger_time, is_secure_connection, app_protocol_type, ");
768 strcat(szQuery," network_info_id, svc_category_id) values( ");
769 strcat(szQuery," ?, 1, ? , ?, ?, ?, ?, ");//1,2,3,4,5,6
770 strcat(szQuery," 1, ?, ?, 300, 0, 1, ");//7,8
771 strcat(szQuery," ?, ? )");//9,10
773 insert_key1 = g_strdup_printf("%d", profile_id);
774 insert_key2 = g_strdup_printf("%d", network_id);
775 profile_name = g_strdup_printf("tmp_profile%d", profile_id);
777 g_hash_table_insert(in_param, "1", g_strdup(insert_key1));
778 g_hash_table_insert(in_param, "2", g_strdup(profile_name));
779 g_hash_table_insert(in_param, "3", g_strdup(apn));
780 g_hash_table_insert(in_param, "4", g_strdup(auth_type));
781 g_hash_table_insert(in_param, "5", g_strdup(auth_id));
782 g_hash_table_insert(in_param, "6", g_strdup(auth_pwd));
783 g_hash_table_insert(in_param, "7", g_strdup(proxy_addr));
784 g_hash_table_insert(in_param, "8", g_strdup(home_url));
785 g_hash_table_insert(in_param, "9", g_strdup(insert_key2));
786 g_hash_table_insert(in_param, "10", g_strdup(svc_id));
788 g_free(insert_key1);g_free(insert_key2);g_free(profile_name);
789 g_free(apn);g_free(auth_type);g_free(auth_id);g_free(auth_pwd);
790 g_free(proxy_addr);g_free(home_url);g_free(svc_id);
792 rv = tcore_storage_insert_query_database(strg_db, handle, szQuery, in_param);
793 g_hash_table_destroy(in_param);
801 static int __ps_context_load_network_id_from_database(gchar *mccmnc)
807 GHashTable *in_param, *out_param;
809 in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
810 out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
811 (GDestroyNotify) g_hash_table_destroy);
813 memset(szQuery, 0, sizeof(szQuery));
814 strcpy(szQuery,"select network_info_id from network_info where mccmnc = ? ");
816 g_hash_table_insert(in_param, "1", g_strdup(mccmnc));
817 tcore_storage_read_query_database(strg_db, handle, szQuery, in_param, out_param, 1);
819 g_hash_table_iter_init(&iter, out_param);
820 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
821 GHashTableIter iter2;
822 gpointer key2, value2;
825 g_hash_table_iter_init(&iter2, (GHashTable *) value);
826 while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
827 if (g_str_equal(key, "0") == TRUE) {
828 network_id = atoi((const char*) value2);
835 g_hash_table_destroy(in_param);
836 g_hash_table_destroy(out_param);
841 static int __ps_context_load_profile_id_from_database(void)
847 GHashTable *out_param;
849 out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
850 (GDestroyNotify) g_hash_table_destroy);
852 memset(szQuery, 0, sizeof(szQuery));
853 strcpy(szQuery,"select max(profile_id) as last_profile from pdp_profile");
855 tcore_storage_read_query_database(strg_db, handle, szQuery, NULL, out_param, 1);
857 g_hash_table_iter_init(&iter, out_param);
858 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
859 GHashTableIter iter2;
860 gpointer key2, value2;
863 g_hash_table_iter_init(&iter2, (GHashTable *) value);
864 while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
865 if (g_str_equal(key, "0") == TRUE) {
866 profile_id = atoi((const char*) value2);
873 g_hash_table_destroy(out_param);
877 static gboolean __ps_context_reset_database(void)
880 gchar *extract_sql, *recover_sql, *remove_tmp;
882 extract_sql = g_strdup("tar --extract --file=/opt/system/customer_selected/preconfig/customer.tar.gz opt/dbspace/.dnet.db -C /tmp");
883 dbg("system extract command (%s)", extract_sql);
884 rv = system(extract_sql);
886 recover_sql = g_strdup("sqlite3 /opt/dbspace/.dnet.db \"attach database '/tmp/opt/dbspace/.dnet.db' as csc; replace into pdp_profile select * from csc.pdp_profile;\" ");
887 dbg("system recover db command (%s)", recover_sql);
888 rv = system(recover_sql);
890 remove_tmp = g_strdup("rm -rf /tmp/opt/dbspace/.dnet.db");
891 dbg("system recover db command (%s)", remove_tmp);
892 rv = system(remove_tmp);
901 static int __ps_context_get_network_id(gchar *mccmnc)
905 network_id = __ps_context_load_network_id_from_database(mccmnc);
909 network_id = __ps_context_insert_network_id_to_database(mccmnc);
916 static gboolean __ps_context_get_profile_properties(gpointer object, GHashTable *properties)
918 gchar *s_authtype = NULL, *s_role = NULL;
919 PsContext *context = NULL;
921 g_return_val_if_fail(object != NULL, FALSE);
922 g_return_val_if_fail(properties != NULL, FALSE);
924 context = (PsContext *) object;
925 dbg("get profile properties");
927 s_authtype = g_strdup_printf("%d", tcore_context_get_auth(context->co_context));
928 s_role = g_strdup_printf("%d", tcore_context_get_role(context->co_context));
930 g_hash_table_insert(properties, "path", g_strdup(context->path));
931 g_hash_table_insert(properties, "apn", tcore_context_get_apn(context->co_context));
932 g_hash_table_insert(properties, "auth_type", g_strdup(s_authtype));
933 g_hash_table_insert(properties, "auth_id", tcore_context_get_username(context->co_context));
934 g_hash_table_insert(properties, "auth_pwd", tcore_context_get_password(context->co_context));
935 g_hash_table_insert(properties, "proxy_addr", tcore_context_get_proxy(context->co_context));
936 g_hash_table_insert(properties, "home_url", tcore_context_get_mmsurl(context->co_context));
937 g_hash_table_insert(properties, "svc_ctg_id", g_strdup(s_role));
945 static gboolean __ps_context_set_alwayson_enable(gpointer object, gboolean enabled)
947 PsContext *context = object;
948 int role = CONTEXT_ROLE_UNKNOWN;
949 g_return_val_if_fail(context != NULL, FALSE);
951 role = tcore_context_get_role(context->co_context);
952 if(role == CONTEXT_ROLE_INTERNET){
953 context->alwayson = enabled;
959 static gpointer __ps_context_add_context(gpointer modem, gchar *mccmnc, int profile_id)
962 DBusGConnection *conn = NULL;
963 TcorePlugin *p = NULL;
966 gpointer object = NULL;
968 gchar *insert_key1 = NULL;
969 GHashTable *in_param, *out_param;
970 in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
971 out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
972 (GDestroyNotify) g_hash_table_destroy);
974 dbg("create profile by profile id (%d)", profile_id);
975 conn = _ps_modem_ref_dbusconn(modem);
976 p = _ps_modem_ref_plugin(modem);
978 memset(szQuery, '\0', 5000);
979 strcpy(szQuery, "select");
980 strcat(szQuery, " a.network_info_id, a.network_name, a.mccmnc,"); //0 , 1, 2
981 strcat(szQuery, " b.profile_id, b.profile_name, b.apn, "); //3, 4, 5
982 strcat(szQuery, " b.auth_type, b.auth_id, b.auth_pwd,"); //6, 7, 8
983 strcat(szQuery, " b.proxy_ip_addr, b.home_url, b.pdp_protocol, "); //9, 10 , 11
984 strcat(szQuery, " b.linger_time, b.is_secure_connection, b.app_protocol_type, b.traffic_class,"); //12, 13, 14, 15
985 strcat(szQuery, " b.is_static_ip_addr, b.ip_addr, b.is_static_dns_addr, b.dns_addr1, b.dns_addr2, b.svc_category_id"); //16,17, 18, 19, 20, 21
986 strcat(szQuery, " from network_info a, pdp_profile b");
987 strcat(szQuery, " where b.profile_id = ? and a.network_info_id = b.network_info_id ");
989 insert_key1 = g_strdup_printf("%d", profile_id);
990 g_hash_table_insert(in_param, "1", g_strdup(insert_key1));
991 tcore_storage_read_query_database(strg_db, handle, szQuery, in_param, out_param, 22);
993 g_hash_table_iter_init(&iter, out_param);
994 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
997 object = __ps_context_create_context(conn, p, mccmnc, (GHashTable *) value);
998 path = _ps_context_ref_path(object);
1000 g_hash_table_insert(contexts, g_strdup(path), object);
1001 dbg("context (%p, %s) insert to hash", object, path);
1004 g_hash_table_destroy(in_param);
1005 g_hash_table_destroy(out_param);
1006 g_free(insert_key1);
1011 gboolean _ps_context_initialize(gpointer plugin)
1015 dbg("global variable initialized");
1016 rv &=__ps_context_create_storage_handle(plugin);
1017 rv &=__ps_context_create_context_hash();
1022 gboolean _ps_context_reset_hashtable(void)
1027 g_hash_table_foreach_remove(contexts, __remove_contexts, NULL);
1028 __ps_context_reset_database();
1032 GHashTable* _ps_context_create_hashtable(DBusGConnection *conn, TcorePlugin *p, gchar *mccmnc)
1035 GHashTableIter iter;
1036 gpointer key, value;
1037 GHashTable *in_param, *out_param;
1038 in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1039 out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
1040 (GDestroyNotify) g_hash_table_destroy);
1042 dbg("create profile by mccmnc (%s)", mccmnc);
1044 memset(szQuery, '\0', 5000);
1045 strcpy(szQuery, "select");
1046 strcat(szQuery, " a.network_info_id, a.network_name, a.mccmnc,"); //0 , 1, 2
1047 strcat(szQuery, " b.profile_id, b.profile_name, b.apn, "); //3, 4, 5
1048 strcat(szQuery, " b.auth_type, b.auth_id, b.auth_pwd,"); //6, 7, 8
1049 strcat(szQuery, " b.proxy_ip_addr, b.home_url, b.pdp_protocol, "); //9, 10 , 11
1050 strcat(szQuery, " b.linger_time, b.is_secure_connection, b.app_protocol_type, b.traffic_class,"); //12, 13, 14, 15
1051 strcat(szQuery, " b.is_static_ip_addr, b.ip_addr, b.is_static_dns_addr, b.dns_addr1, b.dns_addr2, b.svc_category_id"); //16,17, 18, 19, 20, 21
1052 strcat(szQuery, " from network_info a, pdp_profile b");
1053 strcat(szQuery, " where a.mccmnc= ? and a.network_info_id = b.network_info_id ");
1055 g_hash_table_insert(in_param, "1", g_strdup(mccmnc));
1056 tcore_storage_read_query_database(strg_db, handle, szQuery, in_param, out_param, 22);
1058 g_hash_table_iter_init(&iter, out_param);
1059 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1061 gpointer object = NULL;
1063 object = __ps_context_create_context(conn, p, mccmnc, (GHashTable *) value);
1064 path = _ps_context_ref_path(object);
1066 g_hash_table_insert(contexts, g_strdup(path), object);
1067 dbg("context (%p, %s) insert to hash", object, path);
1070 g_hash_table_destroy(in_param);
1071 g_hash_table_destroy(out_param);
1076 GHashTable* _ps_context_ref_hashtable(void)
1078 g_return_val_if_fail(contexts != NULL, NULL);
1082 gboolean _ps_context_add_context(gpointer modem, gchar *operator, GHashTable *property)
1084 GHashTable *services = NULL;
1085 gpointer context = NULL;
1087 GHashTableIter iter;
1088 gpointer key, value;
1092 network_id = __ps_context_get_network_id(operator);
1093 if(network_id <= 0){
1094 dbg("fail to add network info");
1098 profile_id = __ps_context_insert_profile_to_database(property, network_id);
1099 if(profile_id <= 0){
1100 dbg("fail to insert profile info to database");
1104 context = __ps_context_add_context(modem, operator, profile_id);
1108 services = _ps_modem_ref_services(modem);
1112 g_hash_table_iter_init(&iter, services);
1113 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1114 _ps_service_ref_context(value, context);
1120 gboolean _ps_context_get_properties(gpointer object, GHashTable *properties)
1122 int context_state = 0;
1123 gboolean active = FALSE;
1124 PsContext *context = object;
1126 dbg("get context properties");
1127 g_return_val_if_fail(context != NULL, FALSE);
1128 g_return_val_if_fail(properties != NULL, FALSE);
1130 context_state = tcore_context_get_state(context->co_context);
1131 if (context_state == CONTEXT_STATE_ACTIVATED)
1134 g_hash_table_insert(properties, "path", g_strdup(context->path));
1135 g_hash_table_insert(properties, "active", g_strdup(BOOL2STRING(active)));
1136 g_hash_table_insert(properties, "ipv4_address", tcore_context_get_ipv4_addr(context->co_context));
1137 g_hash_table_insert(properties, "ipv4_gateway", tcore_context_get_ipv4_gw(context->co_context));
1138 g_hash_table_insert(properties, "ipv4_dns1", tcore_context_get_ipv4_dns1(context->co_context));
1139 g_hash_table_insert(properties, "ipv4_dns2", tcore_context_get_ipv4_dns2(context->co_context));
1140 g_hash_table_insert(properties, "ipv6_address", "::" );
1141 g_hash_table_insert(properties, "ipv6_gateway", "::" );
1142 g_hash_table_insert(properties, "ipv6_dns1", "::" );
1143 g_hash_table_insert(properties, "ipv6_dns2", "::" );
1144 g_hash_table_insert(properties, "proxy", tcore_context_get_proxy(context->co_context));
1145 g_hash_table_insert(properties, "dev_name", tcore_context_get_ipv4_devname(context->co_context));
1150 gboolean _ps_context_set_service(gpointer object, gpointer service)
1152 PsContext *context = object;
1153 g_return_val_if_fail(context != NULL, FALSE);
1155 context->p_service = service;
1159 gpointer _ps_context_ref_service(gpointer object)
1161 PsContext *context = object;
1162 g_return_val_if_fail(context != NULL, FALSE);
1164 return context->p_service;
1167 gchar* _ps_context_ref_path(gpointer object)
1169 PsContext *context = object;
1170 g_return_val_if_fail(context != NULL, NULL);
1172 return context->path;
1175 gboolean _ps_context_get_alwayson_enable(gpointer object)
1177 PsContext *context = object;
1178 g_return_val_if_fail(context != NULL, FALSE);
1180 return context->alwayson;
1183 gpointer _ps_context_ref_co_context(gpointer object)
1185 PsContext *context = object;
1186 g_return_val_if_fail(context != NULL, NULL);
1188 return context->co_context;
1191 gboolean _ps_context_set_connected(gpointer object, gboolean enabled)
1194 PsContext *context = object;
1195 g_return_val_if_fail(context != NULL, FALSE);
1198 ipv4 = tcore_context_get_ipv4_addr(context->co_context);
1202 tcore_context_set_state(context->co_context, CONTEXT_STATE_ACTIVATED);
1203 if( g_str_equal(ipv4, "0.0.0.0") == TRUE ){
1204 dbg("ip address is 0.0.0.0");
1205 _ps_service_deactivate_context(context->p_service, context);
1208 _ps_service_reset_connection_timer(context);
1212 tcore_context_set_state(context->co_context, CONTEXT_STATE_DEACTIVATED);
1213 tcore_context_reset_devinfo(context->co_context);
1214 _ps_service_connection_timer(context->p_service, context);
1217 __ps_context_emit_property_changed_signal(context);