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