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