Add dbus conf file & Fix daemon crash issue
[platform/core/telephony/tel-plugin-packetservice.git] / src / ps_main.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 #include <stdio.h>
24
25 #include <glib.h>
26
27 #include <tcore.h>
28 #include <plugin.h>
29
30 #include "ps_main.h"
31 #include <ps_common.h>
32
33 static void __packet_service_cleanup(PsPrivInfo *priv_info)
34 {
35         /* Sanity Check */
36         if (priv_info == NULL)
37                 return;
38
39         /* Free cynara handle */
40         if (priv_info->p_cynara) {
41                 cynara_finish(priv_info->p_cynara);
42                 priv_info->p_cynara = NULL;
43         }
44
45         /* Cleaning up the master list */
46         g_slist_foreach(priv_info->master,
47                 __remove_master, NULL);
48
49         /* Unowning the Gdbus */
50         g_bus_unown_name(priv_info->bus_id);
51
52         /* Free GDBusConnection */
53         g_object_unref(priv_info->conn);
54
55         /*
56          * Freeing the memory allocated to the
57          * custom data for Packet Service
58          */
59         g_free(priv_info);
60 }
61
62 static void on_bus_acquired(GDBusConnection *conn,
63         const gchar *name, gpointer user_data)
64 {
65         gboolean rv = FALSE;
66         gpointer *master = NULL;
67
68         TcorePlugin *p = user_data;
69         PsPrivInfo *priv_info = tcore_plugin_ref_user_data(p);
70
71         dbg("Bus is acquired: [%s]", name);
72
73         /*
74          * Create 'master'
75          */
76         master = _ps_master_create_master(conn, p);
77         if (!master) {
78                 err("Failed to create 'master' Object for Packet Service");
79                 goto FAILURE;
80         }
81
82         priv_info->master = g_slist_append(priv_info->master, master);
83
84         /*
85          * Create and initialize 'modem(s)'
86          */
87         rv = _ps_master_create_modems(master, NULL);
88         if (!rv) {
89                 dbg("Failure : Modem creation Failed ");
90                 goto FAILURE;
91         }
92
93         dbg("Packet Service plugin initialization: [Successful]");
94
95         return;
96
97 FAILURE:
98         ps_main_exit(p);
99 }
100
101 gboolean ps_main_init(TcorePlugin *p)
102 {
103         PsPrivInfo *priv_info = NULL;
104         GDBusConnection *conn = NULL;
105         gchar *address = NULL;
106         guint id;
107
108         GError *error = NULL;
109         gboolean rv = FALSE;
110         cynara *p_cynara = NULL;
111
112         if (!p)
113                 return FALSE;
114
115         /*
116          * Initialize context
117          */
118         rv = _ps_context_initialize(p);
119         if (rv != TRUE) {
120                 err("Failure : Initialize context global variable");
121                 return FALSE;
122         }
123
124         /*
125          * Memory allocation for private information
126          */
127         priv_info = g_try_malloc0(sizeof(PsPrivInfo));
128         if (!priv_info) {
129                 err("Failure :Memory allocation !!");
130                 return FALSE;
131         }
132
133         /* Initialize cynara handle */
134         if (CYNARA_API_SUCCESS == cynara_initialize(&p_cynara, NULL)) {
135                 dbg("cynara handle is successfully initialized.");
136         } else {
137                 err("Failed to initialize cynara handle.");
138                 return FALSE;
139         }
140
141         address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
142         g_assert_no_error(error);
143
144         conn = g_dbus_connection_new_for_address_sync(address,
145                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
146                 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
147                 NULL,
148                 NULL,
149                 &error);
150         g_assert_no_error(error);
151         if (!conn)
152                 dbg("Failure: G-dBus Connection failed"); /* TODO - Clean-up */
153
154         /*
155          * Storing the G-dBus connection in Private information of PS Plugin
156          */
157         priv_info->conn = conn;
158
159         id = g_bus_own_name_on_connection(conn,
160                 PS_DBUS_SERVICE,
161                 G_BUS_NAME_OWNER_FLAGS_REPLACE,
162                 on_bus_acquired,
163                 NULL,
164                 p,
165                 NULL);
166
167         dbg("PacketService - dBus address: [%s] dBus connection ID: [%d]",
168                 address, id);
169
170         /*
171          * Initializing custom data for Packet Service
172          */
173         priv_info->bus_id = id;
174         priv_info->master = NULL;
175         priv_info->p = p;
176         priv_info->p_cynara = p_cynara;
177
178         /*
179          * Setting User data of PS plugin
180          */
181         tcore_plugin_link_user_data(p, (void *)priv_info);
182
183         return TRUE;
184 }
185
186 /*
187  * Packet service de-initializer
188  */
189 void ps_main_exit(TcorePlugin *p)
190 {
191         PsPrivInfo *priv_info = tcore_plugin_ref_user_data(p);
192
193         /*
194          * Clean-up Packet Service
195          */
196         __packet_service_cleanup(priv_info);
197         tcore_plugin_link_user_data(p, NULL);
198
199         dbg("Packet Service exited!! ");
200 }