resolved the code rule warnings
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / ipsp / bt-service-ipsp-event-receiver.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include <glib.h>
18 #include <string.h>
19 #include <dlog.h>
20 #include <vconf.h>
21 #include <vconf-internal-bt-keys.h>
22
23 #include "bluetooth-api.h"
24 #include "bt-internal-types.h"
25
26 #include "bt-service-common.h"
27 #include "bt-service-event.h"
28 #include "bt-service-main.h"
29 #include "bt-service-core-adapter.h"
30 #include "bt-service-core-adapter-le.h"
31 #include "bt-service-device.h"
32
33 #ifdef TIZEN_FEATURE_BT_DPM
34 #include "bt-service-dpm.h"
35 #endif
36
37 #define BT_MEDIA_OBJECT_PATH "/Musicplayer"
38 static GDBusConnection *manager_conn;
39
40
41
42 static void __bt_ipsp_property_changed_event(GVariant *msg, const char *path);
43
44
45 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
46 {
47         BT_DBG("+");
48
49         int event;
50         int result = BLUETOOTH_ERROR_NONE;
51         GVariantIter value_iter;
52         GVariant *val;
53         char *property = NULL;
54         char *address;
55         GVariant *param = NULL;
56         g_variant_iter_init(&value_iter, msg);
57
58         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
59                 BT_DBG("Property %s", property);
60                 if (strcasecmp(property, "IpspConnected") == 0) {
61                         gboolean connected = FALSE;
62
63                         g_variant_get(val, "b", &connected);
64
65
66                         event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
67                                                         BLUETOOTH_EVENT_IPSP_DISCONNECTED;
68
69                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
70
71                         _bt_convert_device_path_to_address(path, address);
72
73                         BT_DBG("Ipspconnected: %d", connected);
74                         BT_DBG("address: %s", address);
75                         param = g_variant_new("(is)", result, address);
76
77                         /* Send event to application */
78                         _bt_send_event(BT_DEVICE_EVENT,
79                                         event,
80                                         param);
81                         g_free(address);
82                 } else if (strcasecmp(property, "IpspBtInterfaceInfo") == 0) {
83                         char *ifname = NULL;
84
85                         g_variant_get(val, "s", &ifname);
86
87                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
88
89                         _bt_convert_device_path_to_address(path, address);
90
91                         BT_DBG("Ipsp BT Interface Name: %s", ifname);
92                         BT_DBG("address: %s", address);
93                         param = g_variant_new("(iss)", result, address, ifname);
94
95                         /* Send event to application */
96                         _bt_send_event(BT_DEVICE_EVENT,
97                                         BLUETOOTH_EVENT_IPSP_INTERFACE_INFO,
98                                         param);
99                         g_free(address);
100                 }
101         }
102         BT_DBG("-");
103 }
104
105 void _bt_handle_ipsp_device_event(GVariant *msg, const char *member, const char *path)
106 {
107         int event = 0;
108         int result = BLUETOOTH_ERROR_NONE;
109         char *address;
110         GVariant *param = NULL;
111         ret_if(path == NULL);
112
113         if  (strcasecmp(member, "IpspStateChanged") == 0) {
114                 gboolean connected = FALSE;
115                 char *ifname = NULL;
116                 GVariant *ipsp_param = NULL;
117
118                 g_variant_get(msg, "(bs)", &connected, &ifname);
119
120                 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
121                                                 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
122
123                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
124                 _bt_convert_device_path_to_address(path, address);
125
126                 BT_DBG("Ipsp BT Interface Name: %s, address: %s", ifname, address);
127
128                 param = g_variant_new("(iss)", result, address, ifname);
129                 ipsp_param = g_variant_new("(ss)", ifname, address);
130
131                 g_free(ifname);
132
133                 /* Set Ipv6 Addr */
134                 GDBusProxy *ipsp_proxy;
135                 if (connected) {
136                         BT_DBG("IPSP connected, Set Ipv6 Addr");
137                         ipsp_proxy = _bt_get_ipsp_proxy();
138                         if (ipsp_proxy == NULL) {
139                                 BT_ERR("can not get ipsp proxy");
140                                 g_free(address);
141                                 g_variant_unref(param);
142                                 g_variant_unref(ipsp_param);
143                                 return;
144                         }
145
146                         g_dbus_proxy_call(ipsp_proxy, "SetIpv6Addr",
147                                         ipsp_param, G_DBUS_CALL_FLAGS_NONE,
148                                         -1, NULL, NULL, NULL);
149                 } else {
150                         g_variant_unref(ipsp_param);
151                         BT_DBG("IPSP disconnected");
152                         ipsp_proxy = _bt_get_ipsp_proxy();
153                         if (ipsp_proxy == NULL) {
154                                 BT_ERR("can not get ipsp proxy");
155                                 g_free(address);
156                                 g_variant_unref(param);
157                                 return;
158                         }
159
160                         g_dbus_proxy_call(ipsp_proxy, "DisableIpsp",
161                                         NULL, G_DBUS_CALL_FLAGS_NONE,
162                                         -1, NULL, NULL, NULL);
163                 }
164
165                 /* Send event to application */
166                 _bt_send_event(BT_DEVICE_EVENT, event, param);
167                 g_free(address);
168         }
169 }
170
171
172 static  void __bt_manager_event_filter(GDBusConnection *connection,
173                                         const gchar *sender_name,
174                                         const gchar *object_path,
175                                         const gchar *interface_name,
176                                         const gchar *signal_name,
177                                         GVariant *parameters,
178                                         gpointer user_data)
179 {
180         char *inf_name = NULL;
181         GVariant *val = NULL;
182         const char *path = object_path;
183
184         if (signal_name == NULL)
185                 return;
186
187         BT_DBG("IPSP __bt_manager_event_filter Interface Name [%s]", interface_name);
188
189
190         g_variant_get(parameters, "(&s@a{sv}@as)", &inf_name, &val, NULL);
191         BT_DBG("Interface Name [%s] Signal Name : [%s] ", inf_name, signal_name);
192
193         if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
194
195                 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
196                                 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
197                         return;
198                 if (strcasecmp(inf_name, BT_ADAPTER_INTERFACE) == 0)
199                         __bt_ipsp_property_changed_event(val, object_path);
200                 else if (strcasecmp(inf_name, BT_DEVICE_INTERFACE) == 0)
201                         __bt_device_property_changed_event(val, object_path);
202         } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
203                         _bt_handle_ipsp_device_event(parameters, signal_name, object_path);
204                         g_variant_unref(val);
205         }
206
207         return;
208 }
209
210
211 int _bt_register_ipsp_subscribe_signal(GDBusConnection *conn,
212                 int subscribe)
213 {
214
215         if (conn == NULL)
216                 return -1;
217
218         static int subs_interface_added_id = -1;
219         static int subs_interface_removed_id = -1;
220         static int subs_property_id = -1;
221         static int subs_ipsp_state_id = -1;
222
223
224         if (subscribe) {
225                 if (subs_interface_added_id == -1) {
226                         subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
227                                 NULL, BT_MANAGER_INTERFACE,
228                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
229                                 __bt_manager_event_filter,
230                                 NULL, NULL);
231                 }
232                 if (subs_interface_removed_id == -1) {
233                         subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
234                                 NULL, BT_MANAGER_INTERFACE,
235                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
236                                 __bt_manager_event_filter,
237                                 NULL, NULL);
238                 }
239                 if (subs_property_id == -1) {
240                         subs_property_id = g_dbus_connection_signal_subscribe(conn,
241                                 NULL, BT_PROPERTIES_INTERFACE,
242                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
243                                 __bt_manager_event_filter,
244                                 NULL, NULL);
245                 }
246                 if (subs_ipsp_state_id == -1) {
247                         subs_ipsp_state_id = g_dbus_connection_signal_subscribe(conn,
248                                 NULL, BT_DEVICE_INTERFACE,
249                                 BT_IPSP_STATE_CHANGE, NULL, NULL, 0,
250                                 __bt_manager_event_filter,
251                                 NULL, NULL);
252                 }
253         } else {
254                 if (subs_interface_added_id != -1) {
255                         g_dbus_connection_signal_unsubscribe(conn,
256                                         subs_interface_added_id);
257                         subs_interface_added_id = -1;
258                 }
259                 if (subs_interface_removed_id != -1) {
260                         g_dbus_connection_signal_unsubscribe(conn,
261                                         subs_interface_removed_id);
262                         subs_interface_removed_id = -1;
263                 }
264                 if (subs_property_id != -1) {
265                         g_dbus_connection_signal_unsubscribe(conn,
266                                         subs_property_id);
267                         subs_property_id = -1;
268                 }
269                 if (subs_ipsp_state_id != -1) {
270                         g_dbus_connection_signal_unsubscribe(conn,
271                                         subs_ipsp_state_id);
272                         subs_ipsp_state_id = -1;
273                 }
274         }
275         return 0;
276 }
277
278 int _bt_init_ipsp_receiver(void)
279 {
280         BT_DBG("+");
281
282         GError *error = NULL;
283
284         if (manager_conn == NULL) {
285                 manager_conn =  g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
286                 if (error != NULL) {
287                         BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
288                         g_clear_error(&error);
289                 }
290                 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
291         }
292
293         if (_bt_register_ipsp_subscribe_signal(manager_conn, TRUE) != BLUETOOTH_ERROR_NONE)
294                 goto fail;
295
296         return BLUETOOTH_ERROR_NONE;
297 fail:
298         if (manager_conn) {
299                 g_object_unref(manager_conn);
300                 manager_conn = NULL;
301         }
302
303         BT_DBG("-");
304
305         return BLUETOOTH_ERROR_INTERNAL;
306 }
307
308 void _bt_deinit_service_event_receiver(void)
309 {
310         BT_DBG("+");
311
312         _bt_register_ipsp_subscribe_signal(manager_conn, FALSE);
313
314         if (manager_conn) {
315                 g_object_unref(manager_conn);
316                 manager_conn = NULL;
317         }
318
319         BT_DBG("-");
320 }
321
322 static void __bt_ipsp_property_changed_event(GVariant *msg, const char *path)
323 {
324         GVariantIter value_iter;
325         GVariant *val = NULL;
326         char *property = NULL;
327         GVariant *param = NULL;
328         g_variant_iter_init(&value_iter, msg);
329         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
330                 BT_INFO("Property %s", property);
331
332                 if (strcasecmp(property, "IpspInitStateChanged") == 0) {
333                         gboolean ipsp_initialized = FALSE;
334
335                         g_variant_get(val, "b", &ipsp_initialized);
336                         BT_DBG("IPSP init state changed: %d", ipsp_initialized);
337                         param = g_variant_new("(b)", ipsp_initialized);
338
339                         /* Send event to application */
340                         _bt_send_event(BT_ADAPTER_EVENT,
341                                         BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,
342                                         param);
343                 }
344         }
345 }
346