2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <vconf-internal-bt-keys.h>
23 #include "bluetooth-api.h"
24 #include "bt-internal-types.h"
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"
33 #ifdef TIZEN_FEATURE_BT_DPM
34 #include "bt-service-dpm.h"
37 #define BT_MEDIA_OBJECT_PATH "/Musicplayer"
38 static GDBusConnection *manager_conn;
42 static void __bt_ipsp_property_changed_event(GVariant *msg, const char *path);
45 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
50 int result = BLUETOOTH_ERROR_NONE;
51 GVariantIter value_iter;
53 char *property = NULL;
55 GVariant *param = NULL;
56 g_variant_iter_init(&value_iter, msg);
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;
63 g_variant_get(val, "b", &connected);
66 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
67 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
69 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
71 _bt_convert_device_path_to_address(path, address);
73 BT_DBG("Ipspconnected: %d", connected);
74 BT_DBG("address: %s", address);
75 param = g_variant_new("(is)", result, address);
77 /* Send event to application */
78 _bt_send_event(BT_DEVICE_EVENT,
82 } else if (strcasecmp(property, "IpspBtInterfaceInfo") == 0) {
85 g_variant_get(val, "s", &ifname);
87 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
89 _bt_convert_device_path_to_address(path, address);
91 BT_DBG("Ipsp BT Interface Name: %s", ifname);
92 BT_DBG("address: %s", address);
93 param = g_variant_new("(iss)", result, address, ifname);
95 /* Send event to application */
96 _bt_send_event(BT_DEVICE_EVENT,
97 BLUETOOTH_EVENT_IPSP_INTERFACE_INFO,
105 void _bt_handle_ipsp_device_event(GVariant *msg, const char *member, const char *path)
108 int result = BLUETOOTH_ERROR_NONE;
110 GVariant *param = NULL;
111 ret_if(path == NULL);
113 if (strcasecmp(member, "IpspStateChanged") == 0) {
114 gboolean connected = FALSE;
116 GVariant *ipsp_param = NULL;
118 g_variant_get(msg, "(bs)", &connected, &ifname);
120 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
121 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
123 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
124 _bt_convert_device_path_to_address(path, address);
126 BT_DBG("Ipsp BT Interface Name: %s, address: %s", ifname, address);
128 param = g_variant_new("(iss)", result, address, ifname);
129 ipsp_param = g_variant_new("(ss)", ifname, address);
134 GDBusProxy *ipsp_proxy;
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");
141 g_variant_unref(param);
142 g_variant_unref(ipsp_param);
146 g_dbus_proxy_call(ipsp_proxy, "SetIpv6Addr",
147 ipsp_param, G_DBUS_CALL_FLAGS_NONE,
148 -1, NULL, NULL, NULL);
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");
156 g_variant_unref(param);
160 g_dbus_proxy_call(ipsp_proxy, "DisableIpsp",
161 NULL, G_DBUS_CALL_FLAGS_NONE,
162 -1, NULL, NULL, NULL);
165 /* Send event to application */
166 _bt_send_event(BT_DEVICE_EVENT, event, param);
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,
180 char *inf_name = NULL;
181 GVariant *val = NULL;
182 const char *path = object_path;
184 if (signal_name == NULL)
187 BT_DBG("IPSP __bt_manager_event_filter Interface Name [%s]", interface_name);
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);
193 if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
195 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
196 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
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);
211 int _bt_register_ipsp_subscribe_signal(GDBusConnection *conn,
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;
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,
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,
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,
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,
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;
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;
264 if (subs_property_id != -1) {
265 g_dbus_connection_signal_unsubscribe(conn,
267 subs_property_id = -1;
269 if (subs_ipsp_state_id != -1) {
270 g_dbus_connection_signal_unsubscribe(conn,
272 subs_ipsp_state_id = -1;
278 int _bt_init_ipsp_receiver(void)
282 GError *error = NULL;
284 if (manager_conn == NULL) {
285 manager_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
287 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
288 g_clear_error(&error);
290 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
293 if (_bt_register_ipsp_subscribe_signal(manager_conn, TRUE) != BLUETOOTH_ERROR_NONE)
296 return BLUETOOTH_ERROR_NONE;
299 g_object_unref(manager_conn);
305 return BLUETOOTH_ERROR_INTERNAL;
308 void _bt_deinit_service_event_receiver(void)
312 _bt_register_ipsp_subscribe_signal(manager_conn, FALSE);
315 g_object_unref(manager_conn);
322 static void __bt_ipsp_property_changed_event(GVariant *msg, const char *path)
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);
332 if (strcasecmp(property, "IpspInitStateChanged") == 0) {
333 gboolean ipsp_initialized = FALSE;
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);
339 /* Send event to application */
340 _bt_send_event(BT_ADAPTER_EVENT,
341 BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,