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.
22 #include <vconf-internal-bt-keys.h>
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
27 #include "bt-service-common.h"
28 #include "bt-service-event.h"
29 #include "bt-service-main.h"
30 #include "bt-service-adapter.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-device.h"
33 #include "bt-service-avrcp.h"
34 #include "bt-service-obex-server.h"
35 #include "bt-service-rfcomm-server.h"
36 #include "bt-service-audio.h"
37 #include "bt-service-agent.h"
38 #include "bt-service-pbap.h"
39 #include "bt-service-headset-connection.h"
40 #include "bt-service-avrcp-controller.h"
42 #include "bt-service-opp-client.h"
44 #define DBUS_TIMEOUT 20 * 1000 /* 20 Sec */
45 static GDBusConnection *manager_conn;
46 static GDBusConnection *obexd_conn;
47 static GDBusConnection *opc_obexd_conn;
49 static GList *p_cache_list = NULL;
51 static guint event_id;
52 guint nap_connected_device_count = 0;
53 static guint hid_connected_device_count = 0;
54 static GList *p_adv_ind_list;
57 bt_remote_dev_info_t *dev_info;
61 * obexd connection type
69 OBEX_PCSUITE = (1 << 6),
70 OBEX_SYNCEVOLUTION = (1 << 7),
72 } bluetooth_obex_connection_type_t;
74 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path);
75 void _bt_opc_property_changed_event(GVariant *msg, char *path);
76 int _bt_register_service_event(GDBusConnection *g_conn, int event_type);
77 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type);
78 void _bt_opp_client_event_deinit(void);
79 void _bt_handle_network_client_event(GVariant *msg_iter,
81 void __bt_gatt_char_property_changed_event(GVariant *msg_iter,
84 static void __bt_free_bt_le_adv_info_t(bt_le_adv_info_t *adv_info)
86 g_free(adv_info->addr);
87 g_free(adv_info->data);
91 static bt_le_adv_info_t *__bt_get_adv_ind_info(char *addr)
94 bt_le_adv_info_t *adv_info = NULL;
95 GList *current = g_list_first((GList *)p_adv_ind_list);
96 while(current && current->data) {
97 adv_info = (bt_le_adv_info_t *)current->data;
98 retv_if(adv_info && !g_strcmp0(adv_info->addr, addr), adv_info);
99 current = g_list_next(current);
104 static void __bt_add_adv_ind_info(bt_le_adv_info_t *adv_info)
107 if (__bt_get_adv_ind_info(adv_info->addr) != NULL) {
108 BT_ERR("adv_info is already added");
109 __bt_free_bt_le_adv_info_t(adv_info);
112 p_adv_ind_list = g_list_append(p_adv_ind_list, adv_info);
115 static void __bt_del_adv_ind_info(char *addr)
118 ret_if(!p_adv_ind_list);
119 bt_le_adv_info_t *adv_info = NULL;
120 GList *current = g_list_first((GList *)p_adv_ind_list);
121 while(current && current->data) {
122 adv_info = (bt_le_adv_info_t *)current->data;
123 if (adv_info && !g_strcmp0(adv_info->addr, addr)) {
124 p_adv_ind_list = g_list_remove(p_adv_ind_list, adv_info);
125 __bt_free_bt_le_adv_info_t(adv_info);
128 current = g_list_next(current);
132 static void __bt_free_cache_info(bt_cache_info_t *cache_info)
134 ret_if(cache_info == NULL);
135 _bt_free_device_info(cache_info->dev_info);
139 static gboolean __bt_parse_device_properties(GVariant *item,
140 bt_remote_dev_info_t *dev_info)
149 g_variant_iter_init(&iter, item);
150 while (g_variant_iter_loop(&iter, "{sv}", &key, &val)) {
151 if (strcasecmp(key, "Address") == 0) {
152 dev_info->address = g_variant_dup_string(val, &len);
153 } else if (strcasecmp(key, "Class") == 0) {
154 dev_info->class = g_variant_get_uint32(val);
155 } else if (strcasecmp(key, "name") == 0) {
156 if (dev_info->name == NULL)
157 dev_info->name = g_variant_dup_string(val, &len);
158 } else if (strcasecmp(key, "Connected") == 0) {
159 dev_info->connected = g_variant_get_uint32(val);
160 } else if (strcasecmp(key, "paired") == 0) {
161 dev_info->paired = g_variant_get_boolean(val);
162 } else if (strcasecmp(key, "Trusted") == 0) {
163 dev_info->trust = g_variant_get_boolean(val);
164 } else if (strcasecmp(key, "RSSI") == 0) {
165 dev_info->rssi = g_variant_get_int16(val);
166 } else if (strcasecmp(key, "LastAddrType") == 0) {
167 dev_info->addr_type = g_variant_get_byte(val);
168 } else if (strcasecmp(key, "UUIDs") == 0) {
172 size = g_variant_get_size(val);
175 uuid_value = (char **)g_variant_get_strv(val, &size);
176 if (dev_info->uuids == NULL)
177 dev_info->uuids = g_malloc0(sizeof(char *) * size);
179 for (i = 0; uuid_value[i] != NULL; i++) {
180 dev_info->uuid_count++;
181 dev_info->uuids[i] = g_strdup(uuid_value[i]);
185 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
186 g_variant_get(val, "(i)", &dev_info->manufacturer_data_len);
187 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
188 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
189 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
192 if (dev_info->manufacturer_data_len == 0)
193 dev_info->manufacturer_data = g_strdup("");
194 } else if (strcasecmp(key, "ManufacturerData") == 0) {
196 GVariant *manufacturer_var;
197 g_variant_get(val, "@ay", &manufacturer_var);
198 len = g_variant_get_size(manufacturer_var);
200 char *manufacturer_data = (char *)g_variant_get_data(manufacturer_var);
201 dev_info->manufacturer_data = g_malloc0(len);
202 if (dev_info->manufacturer_data)
203 memcpy(dev_info->manufacturer_data, manufacturer_data,
206 g_variant_unref(manufacturer_var);
214 static gboolean __bt_parse_interface(GVariant *msg,
215 bt_remote_dev_info_t *dev_info)
218 GVariant *optional_param;
221 char *interface_name= NULL;
222 GVariant *inner_iter = NULL;
223 g_variant_get(msg, "(&o@a{sa{sv}})",
224 &path, &optional_param);
225 g_variant_iter_init(&iter, optional_param);
227 while ((child = g_variant_iter_next_value(&iter))) {
228 g_variant_get(child,"{&s@a{sv}}", &interface_name, &inner_iter);
229 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
230 BT_DBG("Found a device: %s", path);
231 if (__bt_parse_device_properties(inner_iter,
232 dev_info) == FALSE) {
233 g_variant_unref(inner_iter);
234 g_variant_unref(child);
235 g_variant_unref(optional_param);
236 BT_ERR("Fail to parse the properies");
239 g_variant_unref(inner_iter);
240 g_variant_unref(child);
241 g_variant_unref(optional_param);
245 g_variant_unref(inner_iter);
246 g_variant_unref(child);
249 g_variant_unref(optional_param);
254 static int __bt_get_owner_info(GVariant *msg, char **name,
255 char **previous, char **current)
257 g_variant_get(msg, "(sss)", name, previous, current);
258 return BLUETOOTH_ERROR_NONE;
261 static int __bt_get_agent_signal_info(GVariant *msg, char **address,
262 char **name, char **uuid)
264 g_variant_get(msg, "(sss)", address, name, uuid);
265 return BLUETOOTH_ERROR_NONE;
268 void __bt_set_device_values(gboolean connected, int state)
270 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
272 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0) {
273 BT_ERR("vconf_get_int failed");
276 if (connected == TRUE)
277 bt_device_state |= state;
278 else if (bt_device_state & state)
279 bt_device_state ^= state;
281 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
282 BT_ERR("vconf_set_int failed");
286 gboolean _bt_discovery_finished_cb(gpointer user_data)
288 int result = BLUETOOTH_ERROR_NONE;
290 GVariant *param = NULL;
291 if (_bt_get_discovering_property(DISCOVERY_ROLE_BREDR) == FALSE) {
292 if (_bt_get_cancel_by_user() == TRUE) {
293 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
296 _bt_set_cancel_by_user(FALSE);
297 _bt_set_discovery_status(FALSE);
298 param = g_variant_new("(i)", result);
299 _bt_send_event(BT_ADAPTER_EVENT,
300 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
307 static gboolean __bt_le_discovery_finished_cb(gpointer user_data)
309 int result = BLUETOOTH_ERROR_NONE;
311 GVariant *param = NULL;
312 if (_bt_get_discovering_property(DISCOVERY_ROLE_LE) == FALSE) {
313 if (_bt_get_cancel_by_user() == TRUE) {
314 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
317 _bt_set_cancel_by_user(FALSE);
318 _bt_disable_all_scanner_status();
319 _bt_set_le_scan_status(FALSE);
320 param = g_variant_new("(i)", result);
321 _bt_send_event(BT_LE_ADAPTER_EVENT,
322 BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED,
329 void __bt_update_remote_cache_devinfo(const char *address, gboolean paired_status)
333 ret_if(address == NULL);
336 bt_cache_info_t *cache_info;
337 bt_remote_dev_info_t *dev_info;
339 node = g_list_first(p_cache_list);
341 while (node != NULL){
342 cache_info = (bt_cache_info_t *)node->data;
344 if (cache_info == NULL) {
345 node = g_list_next(node);
349 dev_info = cache_info->dev_info;
350 if (strcasecmp(dev_info->address,
352 BT_DBG("Device Found");
353 if (paired_status == TRUE)
354 cache_info->dev_info->paired = TRUE;
356 cache_info->dev_info->paired = FALSE;
359 node = g_list_next(node);
364 static void __bt_device_remote_connected_properties(
365 bt_remote_dev_info_t *remote_dev_info,
366 char *address, gboolean connected)
368 int result = BLUETOOTH_ERROR_NONE;
370 GVariant *param = NULL;
373 if (remote_dev_info->uuid_count > 0 ) {
374 for (i = 0; i<remote_dev_info->uuid_count; i++) {
375 char *uuid = remote_dev_info->uuids[i];
376 if (strcasecmp(uuid, HID_UUID) == 0){
377 int event = BLUETOOTH_EVENT_NONE;
379 event = (connected == TRUE) ?
380 BLUETOOTH_HID_CONNECTED :
381 BLUETOOTH_HID_DISCONNECTED;
382 param = g_variant_new("(is)", result,
384 _bt_send_event(BT_HID_EVENT, event,
394 void _bt_handle_adapter_event(GVariant *msg, const char *member)
398 int result = BLUETOOTH_ERROR_NONE;
399 GVariant *param = NULL;
400 ret_if(member == NULL);
402 if (strcasecmp(member, "DeviceCreated") == 0) {
403 char *object_path = NULL;
405 bt_remote_dev_info_t *remote_dev_info;
407 ret_if(_bt_is_device_creating() == FALSE);
409 /* Bonding from remote device */
410 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
412 g_variant_get(msg, "(&o)", &object_path);
413 _bt_convert_device_path_to_address((const char*)object_path, address);
415 remote_dev_info = _bt_get_remote_device_info(address);
416 if (remote_dev_info == NULL) {
421 _bt_free_device_info(remote_dev_info);
423 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
424 char *object_path = NULL;
426 bt_cache_info_t *cache_info;
427 bt_remote_dev_info_t *dev_info;
430 /* Bonding from remote device */
431 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
432 g_variant_get(msg, "(&o)", &object_path);
434 /* Fix : NULL_RETURNS */
438 _bt_convert_device_path_to_address((const char *)object_path, address);
440 node = g_list_first(p_cache_list);
442 while (node != NULL){
443 cache_info = (bt_cache_info_t *)node->data;
445 if (cache_info == NULL) {
446 node = g_list_next(node);
450 dev_info = cache_info->dev_info;
451 if (strcasecmp(dev_info->address,
453 p_cache_list = g_list_remove(p_cache_list,
455 __bt_free_cache_info(cache_info);
458 node = g_list_next(node);
461 } else if (strcasecmp(member, "AdvertisingEnabled") == 0) {
462 BT_DBG("Advertising Enabled");
467 gboolean status = FALSE;
469 g_variant_get(msg, "(ib)", &slot_id, &status);
471 BT_DBG("Advertising Enabled : slot_id [%d] status [%d]", slot_id, status);
473 /* Send event to application */
474 _bt_set_advertising_status(slot_id, status);
476 sender = _bt_get_adv_slot_owner(slot_id);
477 adv_handle = _bt_get_adv_slot_adv_handle(slot_id);
480 event = BLUETOOTH_EVENT_ADVERTISING_STARTED;
482 event = BLUETOOTH_EVENT_ADVERTISING_STOPPED;
483 param = g_variant_new("(ii)", result,
485 _bt_send_event_to_dest(sender, BT_ADAPTER_EVENT,
489 if (event == BLUETOOTH_EVENT_ADVERTISING_STOPPED)
490 _bt_unregister_adv_slot_owner(slot_id);
491 } else if (strcasecmp(member, "RssiEnabled") == 0) {
492 BT_DBG("RSSI Enabled");
493 gboolean status = FALSE;
494 char *address = NULL;
496 g_variant_get(msg,"(sib)", &address, &link_type, &status);
498 BT_DBG("RSSI Enabled [%s %d]", address, status);
499 param = g_variant_new("(isib)", result,
500 address, link_type, status);
501 _bt_send_event(BT_DEVICE_EVENT,
502 BLUETOOTH_EVENT_RSSI_ENABLED,
505 } else if (strcasecmp(member, "RssiAlert") == 0) {
506 BT_DBG("RSSI Alert");
510 char *address = NULL;
511 g_variant_get(msg,"(siii)", &address, &link_type, &alert_type, &rssi_dbm);
513 BT_DBG("RSSI Alert: [Address %s LinkType %d] [Type %d DBM %d]",
514 address, alert_type, rssi_dbm);
515 param = g_variant_new("(isiii)", result,
516 address, link_type, alert_type, rssi_dbm);
517 _bt_send_event(BT_DEVICE_EVENT,
518 BLUETOOTH_EVENT_RSSI_ALERT,
521 } else if (strcasecmp(member, "RawRssi") == 0) {
525 char *address = NULL;
526 g_variant_get(msg,"(sii)", &address, &link_type, &rssi_dbm);
528 BT_DBG("Raw RSSI: [Address %s] [Link Type %d][RSSI DBM %d]",
529 address, link_type, rssi_dbm);
530 param = g_variant_new("(isii)", result,
531 address, link_type, rssi_dbm);
532 _bt_send_event(BT_DEVICE_EVENT,
533 BLUETOOTH_EVENT_RAW_RSSI,
536 } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
537 BT_ERR_C("Hardware error received from BLUEZ");
538 _bt_recover_adapter();
539 } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
540 BT_ERR_C("Tx timeout error received from BLUEZ");
541 _bt_recover_adapter();
546 static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
548 GDBusProxy *adapter_proxy;
550 int result = BLUETOOTH_ERROR_NONE;
551 GVariantIter value_iter;
552 GVariant *val = NULL;
554 char *property = NULL;
555 GVariant *param = NULL;
556 g_variant_iter_init (&value_iter, msg);
557 while ((g_variant_iter_loop (&value_iter,"{sv}", &property, &val))) {
558 BT_INFO("Property %s", property);
560 if (strcasecmp(property, "Discovering") == 0) {
561 gboolean discovering = FALSE;
562 g_variant_get(val, "b", &discovering);
563 /* Send event to application */
564 BT_DBG("Discovering %d", discovering);
565 if (discovering == TRUE) {
566 _bt_set_discovery_status(TRUE);
567 param = g_variant_new("(i)", result);
568 _bt_send_event(BT_ADAPTER_EVENT,
569 BLUETOOTH_EVENT_DISCOVERY_STARTED,
572 ret_if(event_id > 0);
574 adapter_proxy = _bt_get_adapter_proxy();
575 ret_if(adapter_proxy == NULL);
577 /* Need to stop searching */
578 g_dbus_proxy_call_sync(adapter_proxy, "StopDiscovery",
580 G_DBUS_CALL_FLAGS_NONE,
584 BT_ERR("Dbus Error : %s", err->message);
588 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
589 (GSourceFunc)_bt_discovery_finished_cb, NULL);
591 } else if (strcasecmp(property, "LEDiscovering") == 0) {
592 gboolean le_discovering = FALSE;
594 g_variant_get(val, "b", &le_discovering);
595 /* Send event to application */
596 if (le_discovering == TRUE) {
597 _bt_set_le_scan_status(TRUE);
598 param = g_variant_new("(i)", result);
599 _bt_send_event(BT_LE_ADAPTER_EVENT,
600 BLUETOOTH_EVENT_LE_DISCOVERY_STARTED,
603 ret_if(event_id > 0);
605 adapter_proxy = _bt_get_adapter_proxy();
606 ret_if(adapter_proxy == NULL);
608 /* Need to stop searching */
609 g_dbus_proxy_call_sync(adapter_proxy, "StopLEDiscovery",
611 G_DBUS_CALL_FLAGS_NONE,
615 BT_ERR("Dbus Error %s", err->message);
619 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
620 (GSourceFunc)__bt_le_discovery_finished_cb, NULL);
622 } else if (strcasecmp(property, "Name") == 0) {
624 g_variant_get(val, "s", &name);
625 param = g_variant_new("(is)", result, name);
626 /* Send event to application */
627 _bt_send_event(BT_ADAPTER_EVENT,
628 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
631 } else if (strcasecmp(property, "Alias") == 0) {
633 g_variant_get(val, "s", &alias);
634 param = g_variant_new("(is)", result, alias);
635 /* Send event to application */
636 _bt_send_event(BT_ADAPTER_EVENT,
637 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
640 } else if (strcasecmp(property, "Discoverable") == 0) {
641 gboolean discoverable = FALSE;
643 g_variant_get(val, "b", &discoverable);
644 BT_DBG("discoverable %d", discoverable);
646 if (discoverable == FALSE) {
647 if (_bt_get_discoverable_timeout_property() > 0) {
649 adapter_proxy = _bt_get_adapter_properties_proxy();
650 ret_if(adapter_proxy == NULL);
651 g_dbus_proxy_call_sync(adapter_proxy, "Set",
652 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
653 "DiscoverableTimeout",
654 g_variant_new("i", time)),
655 G_DBUS_CALL_FLAGS_NONE,
660 BT_ERR("StopLEDiscovery Failed: %s", err->message);
665 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
667 /* Send event to application */
668 BT_INFO("[Connectable]");
669 param = g_variant_new("(in)", result, mode);
670 _bt_send_event(BT_ADAPTER_EVENT,
671 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
674 _bt_get_discoverable_mode(&mode);
676 /* Event will be sent by "DiscoverableTimeout" signal */
677 if (mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
679 g_variant_unref(val);
683 /* Send event to application */
684 BT_INFO("[General Discoverable]");
685 param = g_variant_new("(in)", result, mode);
686 _bt_send_event(BT_ADAPTER_EVENT,
687 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
690 } else if (strcasecmp(property, "DiscoverableTimeout") == 0) {
691 _bt_get_discoverable_mode(&mode);
693 /* Event was already sent by "Discoverable" signal */
694 if (mode == BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE ||
695 mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
697 g_variant_unref(val);
701 /* Send event to application */
702 BT_INFO("[Limited Discoverable (Timeout %u secs)]",
703 _bt_get_discoverable_timeout_property());
704 param = g_variant_new("(in)", result, mode);
705 _bt_send_event(BT_ADAPTER_EVENT,
706 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
708 } else if (strcasecmp(property, "Powered") == 0) {
709 /* TODO: Need to check this operation!! */
710 gboolean powered = FALSE;
713 g_variant_get(val, "b" ,&powered);
714 BT_DBG("Powered = %d", powered);
715 if (powered == FALSE) {
717 _bt_handle_adapter_removed();
719 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_state) == 0 &&
720 bt_state != VCONFKEY_BT_STATUS_OFF) {
721 _bt_disable_adapter();
724 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_state) == 0 &&
725 bt_state != VCONFKEY_BT_LE_STATUS_OFF) {
726 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
731 _bt_handle_adapter_added();
734 } else if (strcasecmp(property, "Connectable") == 0) {
735 gboolean connectable = FALSE;
737 g_variant_get(val, "b", &connectable);
739 BT_DBG("Connectable property is changed : %d", connectable);
740 param = g_variant_new("(b)", connectable);
741 _bt_send_event(BT_ADAPTER_EVENT,
742 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
744 if (_bt_adapter_get_status() == BT_DEACTIVATING &&
745 _bt_adapter_get_le_status() == BT_LE_ACTIVATED &&
747 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
748 } else if (strcasecmp(property, "SupportedLEFeatures") == 0) {
751 GVariantIter *iter = NULL;
752 g_variant_get(val, "as", &iter);
754 while (g_variant_iter_loop(iter, "s", &name)) {
755 BT_DBG("name = %s", name);
756 g_variant_iter_loop(iter, "s", &value);
757 BT_DBG("Value = %s", value);
758 if (FALSE == _bt_update_le_feature_support(name, value))
759 BT_INFO("Fail to update LE feature info");
761 g_variant_iter_free(iter);
763 } else if (strcasecmp(property, "IpspInitStateChanged") == 0) {
764 gboolean ipsp_initialized = FALSE;
766 g_variant_get(val, "b", &ipsp_initialized);
767 BT_DBG("IPSP init state changed: %d", ipsp_initialized);
768 param = g_variant_new("(b)", ipsp_initialized);
770 /* Send event to application */
771 _bt_send_event(BT_ADAPTER_EVENT,
772 BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,
778 static void __bt_obex_property_changed_event(GVariant *msg, const char *path)
782 GVariantIter value_iter;
783 GVariant *child = NULL, *val = NULL;
784 char *property = NULL;
785 g_variant_iter_init (&value_iter, msg);
786 while ((child = g_variant_iter_next_value (&value_iter))) {
787 g_variant_get(child, "{sv}", &property, &val);
789 ret_if(property == NULL);
791 BT_DBG("property :%s", property);
793 if (strcasecmp(property, "Status") == 0) {
795 g_variant_get(val, "s", &status);
797 if (strcasecmp(status, "active") == 0){
798 _bt_obex_transfer_started(path);
799 } else if (strcasecmp(status, "complete") == 0) {
800 _bt_obex_transfer_completed(path, TRUE);
801 _bt_pbap_obex_transfer_completed(path, TRUE);
802 } else if (strcasecmp(status, "error") == 0){
803 _bt_obex_transfer_completed(path, FALSE);
804 _bt_pbap_obex_transfer_completed(path, FALSE);
807 } else if (strcasecmp(property, "Transferred") == 0) {
808 static int transferred = 0;
809 g_variant_get(val, "t", &transferred);
811 _bt_obex_transfer_progress(path,transferred);
814 g_variant_unref(val);
815 g_variant_unref(child);
820 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
825 int result = BLUETOOTH_ERROR_NONE;
826 GVariantIter value_iter;
828 char *property = NULL;
830 GVariant *param = NULL;
831 bt_remote_dev_info_t *remote_dev_info;
832 g_variant_iter_init (&value_iter, msg);
833 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
834 BT_DBG("Property %s", property);
835 if (strcasecmp(property, "Connected") == 0) {
838 g_variant_get(val, "i", &connected);
840 event = (connected != BLUETOOTH_CONNECTED_LINK_NONE) ?
841 BLUETOOTH_EVENT_DEVICE_CONNECTED :
842 BLUETOOTH_EVENT_DEVICE_DISCONNECTED;
844 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
846 _bt_convert_device_path_to_address(path, address);
848 BT_DBG("connected: %d", connected);
849 BT_DBG("address: %s", address);
851 remote_dev_info = _bt_get_remote_device_info(address);
853 if (remote_dev_info != NULL) {
854 __bt_device_remote_connected_properties(
855 remote_dev_info, address,
856 connected != BLUETOOTH_CONNECTED_LINK_NONE ?
858 _bt_free_device_info(remote_dev_info);
860 param = g_variant_new("(is)", result, address);
861 /* Send event to application */
862 _bt_send_event(BT_DEVICE_EVENT,
866 } else if (strcasecmp(property, "RSSI") == 0) {
867 bt_remote_dev_info_t *remote_dev_info;
869 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
871 _bt_convert_device_path_to_address(path, address);
872 BT_DBG("address: %s", address);
874 remote_dev_info = _bt_get_remote_device_info(address);
875 if (remote_dev_info == NULL) {
877 g_variant_unref(val);
881 BT_DBG("Address type %d", remote_dev_info->addr_type);
883 if (remote_dev_info->addr_type == 0) {
884 BT_DBG("Name %s", remote_dev_info->name);
886 GVariant *uuids = NULL;
887 GVariantBuilder *builder = NULL;
889 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
890 for (i=0; i < remote_dev_info->uuid_count; i++) {
891 g_variant_builder_add(builder, "s",
892 remote_dev_info->uuids[i]);
894 uuids = g_variant_new("as", builder);
895 g_variant_builder_unref(builder);
896 GVariant *manufacturer_data = NULL;
897 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
898 remote_dev_info->manufacturer_data,
899 remote_dev_info->manufacturer_data_len,
902 param = g_variant_new("(isunsbub@asn@ay)", result,
903 remote_dev_info->address,
904 remote_dev_info->class,
905 remote_dev_info->rssi,
906 remote_dev_info->name,
907 remote_dev_info->paired,
908 remote_dev_info->connected,
909 remote_dev_info->trust,
911 remote_dev_info->manufacturer_data_len,
914 _bt_send_event(BT_ADAPTER_EVENT,
915 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
919 _bt_free_device_info(remote_dev_info);
920 } else if (strcasecmp(property, "GattConnected") == 0) {
921 gboolean gatt_connected = FALSE;
923 g_variant_get(val, "b", &gatt_connected);
925 event = gatt_connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
926 BLUETOOTH_EVENT_GATT_DISCONNECTED;
928 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
930 _bt_convert_device_path_to_address(path, address);
932 BT_DBG("gatt_connected: %d", gatt_connected);
933 BT_DBG("address: %s", address);
934 param = g_variant_new("(is)", result, address);
935 /* Send event to application */
936 _bt_send_event(BT_DEVICE_EVENT,
940 } else if (strcasecmp(property, "Paired") == 0) {
941 gboolean paired = FALSE;
942 bt_remote_dev_info_t *remote_dev_info;
943 g_variant_get(val, "b", &paired);
944 _bt_agent_set_canceled(FALSE);
945 /* BlueZ sends paired signal for each paired device */
946 /* during activation, We should ignore this, otherwise*/
947 /* application thinks that a new device got paired */
948 if (_bt_adapter_get_status() != BT_ACTIVATED) {
949 BT_DBG("BT is not activated, so ignore this");
951 g_variant_unref(val);
955 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
957 _bt_convert_device_path_to_address(path, address);
959 remote_dev_info = _bt_get_remote_device_info(address);
960 if (remote_dev_info == NULL) {
962 g_variant_unref(val);
967 if(paired == FALSE) {
968 BT_INFO("Unpaired: %s", address);
969 __bt_update_remote_cache_devinfo(address, FALSE);
970 param = g_variant_new("(is)", result, address);
971 _bt_send_event(BT_ADAPTER_EVENT,
972 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
975 BT_INFO("Paired: %s", address);
976 __bt_update_remote_cache_devinfo(address, TRUE);
978 if (_bt_is_device_creating() == TRUE) {
979 BT_DBG("Try to Pair by me");
980 _bt_free_device_info(remote_dev_info);
983 g_variant_unref(val);
986 GVariant *uuids = NULL;
987 GVariantBuilder *builder = NULL;
989 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
990 for (i=0; i < remote_dev_info->uuid_count; i++) {
991 g_variant_builder_add(builder, "s",
992 remote_dev_info->uuids[i]);
994 uuids = g_variant_new("as", builder);
995 g_variant_builder_unref(builder);
996 GVariant *manufacturer_data = NULL;
997 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
998 remote_dev_info->manufacturer_data,
999 remote_dev_info->manufacturer_data_len,
1003 param = g_variant_new("(isunsbub@asn@ay)", result,
1004 address, remote_dev_info->class,
1005 remote_dev_info->rssi,
1006 remote_dev_info->name,
1007 remote_dev_info->paired,
1008 remote_dev_info->connected,
1009 remote_dev_info->trust,
1011 remote_dev_info->manufacturer_data_len,
1013 _bt_send_event(BT_ADAPTER_EVENT,
1014 BLUETOOTH_EVENT_BONDING_FINISHED,
1017 _bt_free_device_info(remote_dev_info);
1019 } else if (strcasecmp(property, "LegacyPaired") == 0) {
1020 gboolean paired = FALSE;
1021 bt_remote_dev_info_t *remote_dev_info;
1023 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1024 BT_DBG("BT is not activated, so ignore this");
1026 g_variant_unref(val);
1030 g_variant_get(val, "b", &paired);
1031 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1032 BT_DBG("LegacyPaired: %d", paired);
1033 _bt_convert_device_path_to_address(path, address);
1035 remote_dev_info = _bt_get_remote_device_info(address);
1036 if (remote_dev_info == NULL) {
1039 g_variant_unref(val);
1043 BT_DBG("LegacyPairing Failed with %s. Show Error Popup",
1044 remote_dev_info->name);
1045 _bt_launch_system_popup(BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE,
1046 remote_dev_info->name, NULL, NULL, NULL);
1048 _bt_free_device_info(remote_dev_info);
1050 } else if (strcasecmp(property, "Trusted") == 0) {
1051 gboolean trusted = FALSE;
1053 g_variant_get(val, "b", &trusted);
1055 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1056 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1058 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1060 _bt_convert_device_path_to_address(path, address);
1062 BT_DBG("trusted: %d", trusted);
1063 BT_DBG("address: %s", address);
1064 param = g_variant_new("(is)", result, address);
1065 /* Send event to application */
1066 _bt_send_event(BT_DEVICE_EVENT,
1070 } else if (strcasecmp(property, "IpspConnected") == 0) {
1071 gboolean connected = FALSE;
1073 g_variant_get(val, "b", &connected);
1076 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
1077 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
1079 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1081 _bt_convert_device_path_to_address(path, address);
1083 BT_DBG("Ipspconnected: %d", connected);
1084 BT_DBG("address: %s", address);
1085 param = g_variant_new("(is)", result, address);
1087 /* Send event to application */
1088 _bt_send_event(BT_DEVICE_EVENT,
1097 static void __bt_media_control_changed_event(GVariant *msg, const char *path)
1100 int result = BLUETOOTH_ERROR_NONE;
1101 GVariantIter value_iter;
1102 char *property = NULL;
1104 GVariant *val = NULL;
1105 GVariant *child = NULL;
1106 bt_remote_dev_info_t *remote_dev_info;
1107 GVariant *param = NULL;
1108 g_variant_iter_init (&value_iter, msg);
1109 while ((child = g_variant_iter_next_value (&value_iter))) {
1110 g_variant_get(child, "{sv}", &property, &val);
1111 BT_INFO("Property %s", property);
1112 if (strcasecmp(property, "Connected") == 0) {
1113 gboolean connected = FALSE;
1115 g_variant_get(val, "b", &connected);
1117 event = connected ? BLUETOOTH_EVENT_AVRCP_CONNECTED :
1118 BLUETOOTH_EVENT_AVRCP_DISCONNECTED;
1120 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1122 _bt_convert_device_path_to_address(path, address);
1124 BT_DBG("connected: %d", connected);
1125 BT_DBG("address: %s", address);
1127 remote_dev_info = _bt_get_remote_device_info(address);
1129 if (remote_dev_info != NULL) {
1130 __bt_device_remote_connected_properties(
1131 remote_dev_info, address, connected);
1132 _bt_free_device_info(remote_dev_info);
1134 param = g_variant_new("(is)", result, address);
1135 /* Send event to application */
1136 _bt_send_event(BT_AVRCP_EVENT,
1142 g_variant_unref(child);
1143 g_variant_unref(val);
1148 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
1150 char *interface_name = NULL;
1151 GVariant *val = NULL;
1153 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &val,NULL);
1155 if (strcasecmp(interface_name, BT_ADAPTER_INTERFACE) == 0) {
1156 __bt_adapter_property_changed_event(val,
1158 } else if (strcasecmp(interface_name, BT_DEVICE_INTERFACE) == 0) {
1159 __bt_device_property_changed_event(val,object_path);
1160 } else if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1161 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
1162 __bt_obex_property_changed_event(val,
1164 } else if (strcasecmp(interface_name, BT_MEDIA_CONTROL_INTERFACE) == 0) {
1165 __bt_media_control_changed_event(val,
1167 } else if (strcasecmp(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
1168 _bt_handle_avrcp_control_event(val,
1170 } else if (strcasecmp(interface_name, BT_NETWORK_CLIENT_INTERFACE) == 0) {
1171 BT_DBG("BT_NETWORK_CLIENT_INTERFACE");
1172 _bt_handle_network_client_event(val,
1174 } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
1175 __bt_gatt_char_property_changed_event(val,
1178 g_variant_unref(val);
1181 void __bt_opc_property_changed_event(GVariant *msg,
1184 GVariantIter value_iter;
1185 char *property = NULL;
1186 GVariant *val = NULL;
1187 GVariant *child = NULL;
1189 g_variant_iter_init (&value_iter, msg);
1190 while ((child = g_variant_iter_next_value (&value_iter))) {
1191 g_variant_get(child, "{sv}", &property, &val);
1192 ret_if(property == NULL);
1194 if (strcasecmp(property, "Status") == 0) {
1195 char *status = NULL;
1196 g_variant_get(val, "s", &status);
1197 BT_DBG("Status is %s", status);
1199 if(strcasecmp(status, "active") == 0){
1200 _bt_obex_client_started(path);
1201 }else if (strcasecmp(status, "complete") == 0) {
1202 _bt_obex_client_completed(path, TRUE);
1203 }else if (strcasecmp(status, "error") == 0){
1204 _bt_obex_client_completed(path, FALSE);
1207 } else if (strcasecmp(property, "Transferred") == 0) {
1208 static int transferred = 0;
1209 g_variant_get(val, "t", &transferred);
1211 _bt_obex_client_progress(path, transferred);
1213 BT_DBG("property : [%s]", property);
1216 g_variant_unref(child);
1217 g_variant_unref(val);
1221 void _bt_opc_property_changed_event(GVariant *msg, char *path)
1223 char *interface_name = NULL;
1224 GVariant *value = NULL;
1225 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1226 BT_INFO("interface_name = %s", interface_name);
1227 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1228 __bt_opc_property_changed_event(value,
1231 BT_DBG("interface_name : [%s]", interface_name);
1233 g_variant_unref(value);
1237 void _bt_handle_input_event(GVariant *msg, const char *path)
1239 int result = BLUETOOTH_ERROR_NONE;
1240 gboolean property_flag = FALSE;
1241 GVariantIter value_iter;
1242 char *property = NULL;
1243 GVariant *child = NULL, *val = NULL;
1244 bt_remote_dev_info_t *remote_dev_info;
1245 GVariant *param = NULL;
1246 g_variant_iter_init (&value_iter, msg);
1247 while ((child = g_variant_iter_next_value (&value_iter))) {
1248 g_variant_get(child, "{sv}", &property, &val);
1250 ret_if(property == NULL);
1252 if (strcasecmp(property, "Connected") == 0) {
1253 int event = BLUETOOTH_EVENT_NONE;
1255 g_variant_get(val, "b", &property_flag);
1257 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1259 _bt_convert_device_path_to_address(path, address);
1261 event = (property_flag == TRUE) ?
1262 BLUETOOTH_HID_CONNECTED :
1263 BLUETOOTH_HID_DISCONNECTED;
1264 param = g_variant_new("(is)", result, address);
1265 _bt_send_event(BT_HID_EVENT, event,
1267 /* Check HID connection type (Keyboard or Mouse) and update the status */
1268 remote_dev_info = _bt_get_remote_device_info(address);
1270 if (property_flag == TRUE) {
1271 hid_connected_device_count++;
1272 __bt_set_device_values(TRUE,
1273 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1275 hid_connected_device_count--;
1276 if (hid_connected_device_count == 0)
1277 __bt_set_device_values(FALSE,
1278 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1281 if (remote_dev_info != NULL) {
1282 BT_DBG("HID device class [%x]", remote_dev_info->class);
1283 if (remote_dev_info->class &
1284 BLUETOOTH_DEVICE_MINOR_CLASS_KEY_BOARD) {
1285 #ifdef ENABLE_TIZEN_2_4
1286 __bt_set_device_values(property_flag,
1287 VCONFKEY_BT_DEVICE_HID_KEYBOARD_CONNECTED);
1292 if (remote_dev_info->class &
1293 BLUETOOTH_DEVICE_MINOR_CLASS_POINTING_DEVICE) {
1294 #ifdef ENABLE_TIZEN_2_4
1295 __bt_set_device_values(property_flag,
1296 VCONFKEY_BT_DEVICE_HID_MOUSE_CONNECTED);
1299 _bt_free_device_info(remote_dev_info);
1304 g_variant_unref(val);
1305 g_variant_unref(child);
1309 void _bt_handle_network_server_event(GVariant *msg, const char *member)
1311 int result = BLUETOOTH_ERROR_NONE;
1312 char *address = NULL;
1313 char *device = NULL;
1314 GVariant *param = NULL;
1315 ret_if(member == NULL);
1316 if (strcasecmp(member, "PeerConnected") == 0) {
1317 g_variant_get(msg, "(ss)", &device, &address);
1319 __bt_set_device_values(TRUE,
1320 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1321 param = g_variant_new("(iss)", result, device, address);
1322 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
1326 nap_connected_device_count++;
1327 } else if (strcasecmp(member, "PeerDisconnected") == 0) {
1328 g_variant_get(msg, "(ss)", &device, &address);
1329 nap_connected_device_count--;
1330 if (nap_connected_device_count == 0)
1331 __bt_set_device_values(FALSE,
1332 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1333 param = g_variant_new("(iss)", result, device, address);
1334 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
1341 void _bt_handle_network_client_event(GVariant *msg,
1346 int result = BLUETOOTH_ERROR_NONE;
1347 gboolean property_flag = FALSE;
1348 char *property = NULL;
1349 GVariant *val = NULL;
1350 GVariantIter value_iter;
1351 GVariant *param = NULL;
1352 g_variant_iter_init (&value_iter, msg);
1353 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1354 if (strcasecmp(property, "Connected") == 0) {
1355 int event = BLUETOOTH_EVENT_NONE;
1358 g_variant_get(val, "b", &property_flag);
1359 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1361 _bt_convert_device_path_to_address(path, address);
1363 BT_DBG("property_flag %d", property_flag);
1364 if (property_flag == TRUE) {
1365 event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
1366 nap_connected_device_count++;
1367 __bt_set_device_values(TRUE,
1368 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1370 event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
1371 nap_connected_device_count--;
1372 if (nap_connected_device_count == 0)
1373 __bt_set_device_values(FALSE,
1374 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1376 param = g_variant_new("(is)", result, address);
1377 _bt_send_event(BT_NETWORK_EVENT, event,
1386 void __bt_gatt_char_property_changed_event(GVariant *msg,
1389 GVariantIter value_iter;
1390 char *property = NULL;
1391 char * char_handle = NULL;
1392 GVariant *val = NULL;
1393 int result = BLUETOOTH_ERROR_NONE;
1394 GVariant *param = NULL;
1395 g_variant_iter_init (&value_iter, msg);
1396 char_handle = g_strdup(path);
1397 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1398 BT_INFO("Property %s", property);
1400 ret_if(property == NULL);
1402 if (strcasecmp(property, "Notifying") == 0) {
1403 gboolean property_flag = FALSE;
1404 g_variant_get(val, "b", &property_flag);
1405 if (property_flag == TRUE)
1406 BT_DBG("notifying is enabled");
1408 BT_DBG("notifying is disabled");
1409 } else if (strcasecmp(property, "ChangedValue") == 0) {
1411 GByteArray *gp_byte_array = NULL;
1412 BT_INFO("Type '%s'\n", g_variant_get_type_string (val));
1415 gp_byte_array = g_byte_array_new();
1416 len = g_variant_get_size(val);
1417 BT_DBG("Len = %d", len);
1418 g_byte_array_append (gp_byte_array,
1419 (const guint8 *) g_variant_get_data(val), len);
1420 if (gp_byte_array->len != 0) {
1421 GVariant *byte_array = NULL;
1422 byte_array = g_variant_new_from_data(
1423 G_VARIANT_TYPE_BYTESTRING,
1424 gp_byte_array->data,
1427 param = g_variant_new("(is@ay)", result, char_handle,
1430 /* Send event only registered client */
1431 _bt_send_char_value_changed_event(param);
1433 g_byte_array_free(gp_byte_array, TRUE);
1437 g_free(char_handle);
1440 void _bt_handle_gatt_event(GVariant *msg, const char *member, const char *path)
1442 ret_if(path == NULL);
1444 if (strcasecmp(member, "GattValueChanged") == 0) {
1447 /*** Debug only ***/
1448 GVariant *value = NULL;
1450 char *buffer = NULL;
1452 g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
1453 value_len = g_variant_get_size(value);
1454 if (value_len > 0) {
1455 char buf[8 * 5 + 1] = { 0 };
1458 buffer = (char *)g_variant_get_data(value);
1459 to = value_len > (sizeof(buf) / 5) ? sizeof(buf) / 5 : value_len;
1461 for (i = 0; i < to; i++)
1462 snprintf(&buf[i * 5], 6, "0x%02x ", buffer[i]);
1464 BT_DBG("GATT Val[%d] %s", value_len, buf);
1466 g_variant_unref(value);
1470 /* Send event only registered client */
1471 _bt_send_char_value_changed_event(msg);
1476 void _bt_handle_device_event(GVariant *msg, const char *member, const char *path)
1479 int result = BLUETOOTH_ERROR_NONE;
1482 const char *property = NULL;
1483 GVariant *param = NULL;
1484 ret_if(path == NULL);
1486 if (strcasecmp(member, "PropertyChanged") == 0) {
1488 g_variant_get(msg, "(s)", &property);
1490 ret_if(property == NULL);
1492 if (strcasecmp(property, "GattConnected") == 0) {
1493 gboolean connected = FALSE;
1495 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1496 ret_if(address == NULL);
1498 _bt_convert_device_path_to_address(path, address);
1499 g_variant_get(msg, "(b)", &connected);
1501 event = connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1502 BLUETOOTH_EVENT_GATT_DISCONNECTED;
1503 param = g_variant_new("(is)", result, address);
1504 _bt_send_event(BT_DEVICE_EVENT,
1508 } else if (strcasecmp(property, "Paired") == 0) {
1509 gboolean paired = FALSE;
1510 bt_remote_dev_info_t *remote_dev_info;
1511 g_variant_get(msg, "(b)", &paired);
1513 ret_if(paired == FALSE);
1515 /* BlueZ sends paired signal for each paired device */
1516 /* during activation, We should ignore this, otherwise*/
1517 /* application thinks that a new device got paired */
1518 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1519 BT_DBG("BT is not activated, so ignore this");
1523 if (_bt_is_device_creating() == TRUE) {
1524 BT_DBG("Try to Pair by me");
1528 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1529 ret_if(address == NULL);
1531 _bt_convert_device_path_to_address(path, address);
1533 remote_dev_info = _bt_get_remote_device_info(address);
1534 if (remote_dev_info == NULL) {
1538 GVariant *uuids = NULL;
1539 GVariantBuilder *builder = NULL;
1541 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1542 for (i=0; i < remote_dev_info->uuid_count; i++) {
1543 g_variant_builder_add(builder, "s",
1544 remote_dev_info->uuids[i]);
1546 uuids = g_variant_new("as", builder);
1547 g_variant_builder_unref(builder);
1548 GVariant *manufacturer_data = NULL;
1549 manufacturer_data = g_variant_new_from_data(
1550 G_VARIANT_TYPE_BYTESTRING,
1551 remote_dev_info->manufacturer_data,
1552 remote_dev_info->manufacturer_data_len,
1554 param = g_variant_new("(isunsbub@asn@ay)", result,
1556 remote_dev_info->class,
1557 remote_dev_info->rssi,
1558 remote_dev_info->name,
1559 remote_dev_info->paired,
1560 remote_dev_info->connected,
1561 remote_dev_info->trust,
1563 remote_dev_info->manufacturer_data_len,
1565 _bt_send_event(BT_ADAPTER_EVENT,
1566 BLUETOOTH_EVENT_BONDING_FINISHED,
1568 _bt_free_device_info(remote_dev_info);
1571 } else if (strcasecmp(property, "UUIDs") == 0) {
1572 /* Once we get the updated uuid information after
1573 * reverse service search, update it to application */
1575 bt_remote_dev_info_t *remote_dev_info;
1577 ret_if(_bt_is_device_creating() == TRUE);
1579 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1580 ret_if(address == NULL);
1582 _bt_convert_device_path_to_address(path, address);
1584 remote_dev_info = _bt_get_remote_device_info(address);
1585 if (remote_dev_info == NULL) {
1590 BT_DBG("UUID's count = %d", remote_dev_info->uuid_count);
1591 if (remote_dev_info->paired && remote_dev_info->uuid_count) {
1592 GVariant *uuids = NULL;
1593 GVariantBuilder *builder = NULL;
1595 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1596 for (i=0; i < remote_dev_info->uuid_count; i++) {
1597 g_variant_builder_add(builder, "s",
1598 remote_dev_info->uuids[i]);
1600 uuids = g_variant_new("as", builder);
1601 g_variant_builder_unref(builder);
1602 GVariant *manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1603 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1606 param = g_variant_new("(isunsbub@asn@ay)", result,
1607 address, remote_dev_info->class,
1608 remote_dev_info->rssi,
1609 remote_dev_info->name,
1610 remote_dev_info->paired,
1611 remote_dev_info->connected,
1612 remote_dev_info->trust,
1614 remote_dev_info->manufacturer_data_len,
1616 _bt_send_event(BT_ADAPTER_EVENT,
1617 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1621 _bt_free_device_info(remote_dev_info);
1624 } else if (strcasecmp(member, "DeviceConnected") == 0) {
1625 unsigned char addr_type = 0;
1627 g_variant_get(msg, "(y)", &addr_type);
1629 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1630 ret_if(address == NULL);
1632 _bt_convert_device_path_to_address(path, address);
1633 dev_name = _bt_get_bonded_device_name(address);
1635 BT_INFO("Address : %s Type : %d", address, addr_type);
1636 BT_ERR_C("Connected [%s] [%s]", !addr_type ? "BREDR" : "LE",
1637 !addr_type ? dev_name : address);
1640 _bt_logging_connection(TRUE, addr_type);
1641 #ifdef ENABLE_TIZEN_2_4
1642 journal_bt_connected();
1644 param = g_variant_new("(isy)", result, address, addr_type);
1645 /*Send event to application*/
1646 _bt_send_event(BT_DEVICE_EVENT,
1647 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1650 } else if (strcasecmp(member, "Disconnected") == 0) {
1651 unsigned char disc_reason = 0;
1652 unsigned char addr_type = 0;
1653 gboolean sending = FALSE;
1655 g_variant_get(msg, "(yy)", &addr_type, &disc_reason);
1657 result = disc_reason;
1659 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1660 ret_if(address == NULL);
1662 _bt_convert_device_path_to_address(path, address);
1663 dev_name = _bt_get_bonded_device_name(address);
1664 #ifdef ENABLE_TIZEN_2_4
1665 journal_bt_disconnected();
1668 /* 0x00 BDADDR_BRDER
1669 0x01 BDADDR_LE_PUBLIC
1670 0x02 BDADDR_LE_RANDOM */
1671 BT_INFO("Address : %s Type : %d", address, addr_type);
1672 BT_ERR_C("Disconnected [%s] [%d : %s] [%s]", !addr_type ? "BREDR" : "LE",
1673 disc_reason, _bt_convert_disc_reason_to_string(disc_reason),
1674 !addr_type ? dev_name : address);
1677 _bt_headset_set_local_connection(FALSE);
1678 _bt_logging_connection(FALSE, addr_type);
1680 /*Check for any OPP transfer on the device and cancel
1683 _bt_obex_check_pending_transfer(address);
1684 _bt_opp_client_is_sending(&sending);
1686 _bt_opp_client_check_pending_transfer(address);
1687 param = g_variant_new("(isy)", result, address, addr_type);
1688 _bt_send_event(BT_DEVICE_EVENT,
1689 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1692 } else if (strcasecmp(member, "ProfileStateChanged") == 0) {
1694 char *profile_uuid = NULL;
1695 bt_headset_wait_t *wait_list;
1697 g_variant_get(msg, "(si)", &profile_uuid, &state);
1699 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1700 ret_if(address == NULL);
1702 _bt_convert_device_path_to_address(path, address);
1704 BT_DBG("Address: %s", address);
1705 BT_DBG("Profile UUID: %s", profile_uuid);
1706 BT_DBG("State: %d", state);
1708 if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1709 (state == BT_PROFILE_STATE_CONNECTED)) {
1711 int event = BLUETOOTH_EVENT_AV_CONNECTED;
1712 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1713 bluetooth_device_address_t device_address;
1715 bt_headset_wait_t *wait_list;
1717 __bt_set_device_values(TRUE,
1718 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1720 __bt_connection_manager_set_state(address, event);
1722 if (_bt_headset_get_local_connection() == FALSE)
1723 _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
1725 /* Connection Started from local device therefore no need to
1726 * intiate connection for pending profile */
1727 _bt_headset_set_local_connection(FALSE);
1729 param = g_variant_new("(is)", result, address);
1730 _bt_send_event(BT_HEADSET_EVENT, event,
1732 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
1735 if (g_strcmp0(connected_address, address) != 0) {
1736 _bt_convert_addr_string_to_type(
1737 device_address.addr,
1739 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
1740 &device_address, NULL);
1744 _bt_add_headset_to_list(BT_AUDIO_A2DP,
1745 BT_STATE_CONNECTED, address);
1747 wait_list = _bt_get_audio_wait_data();
1748 if (wait_list != NULL &&
1749 (g_strcmp0(wait_list->address, address) == 0))
1750 _bt_rel_wait_data();
1752 } else if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1753 (state == BT_PROFILE_STATE_DISCONNECTED)) {
1755 int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
1757 if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
1759 g_free(profile_uuid);
1763 __bt_set_device_values(FALSE,
1764 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1766 __bt_connection_manager_set_state(address, event);
1767 param = g_variant_new("(is)", result, address);
1768 _bt_send_event(BT_HEADSET_EVENT, event,
1770 /* Remove data from the connected list */
1771 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
1772 wait_list = _bt_get_audio_wait_data();
1774 if (wait_list == NULL) {
1776 g_free(profile_uuid);
1780 if (((wait_list->type == BT_AUDIO_ALL) &&
1781 (wait_list->ag_flag == TRUE)) ||
1782 (wait_list->type == BT_AUDIO_A2DP) ||
1783 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
1784 bluetooth_device_address_t device_address;
1785 _bt_convert_addr_string_to_type(
1786 device_address.addr,
1787 wait_list->address);
1789 _bt_audio_connect(wait_list->req_id,
1792 wait_list->out_param1);
1794 } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
1796 if (state == BT_PROFILE_STATE_CONNECTED) {
1797 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
1798 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1799 bluetooth_device_address_t device_address;
1801 param = g_variant_new("(is)", result, address);
1802 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1804 connected = _bt_is_headset_type_connected(
1808 if (g_strcmp0(connected_address,
1810 _bt_convert_addr_string_to_type(
1811 device_address.addr,
1813 _bt_audio_disconnect(0,
1815 &device_address, NULL);
1818 BT_DBG("device Path: %s", path);
1819 _bt_add_headset_to_list(BT_AVRCP,
1820 BT_STATE_CONNECTED, address);
1821 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1822 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
1823 param = g_variant_new("(is)", result, address);
1824 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1826 /* Remove data from the connected list */
1827 _bt_remove_headset_from_list(BT_AVRCP, address);
1829 } else if (strcasecmp(profile_uuid, A2DP_SOURCE_UUID) == 0) {
1830 if (state == BT_PROFILE_STATE_CONNECTED) {
1831 int event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED;
1832 BT_INFO("A2DP Source is connected");
1833 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
1834 g_variant_new("(is)", result, address));
1835 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1836 int event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED;
1837 BT_INFO("A2DP Source Disconnected");
1838 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
1839 g_variant_new("(is)", result, address));
1841 } else if ((strcmp(profile_uuid, HID_UUID) == 0) &&
1842 ((state == BT_PROFILE_STATE_CONNECTED) ||
1843 (state == BT_PROFILE_STATE_DISCONNECTED))) {
1845 if (state == BT_PROFILE_STATE_CONNECTED)
1846 event = BLUETOOTH_HID_CONNECTED;
1848 event = BLUETOOTH_HID_DISCONNECTED;
1849 param = g_variant_new("(is)", result, address);
1850 _bt_send_event(BT_HID_EVENT, event,
1853 if (state == BT_PROFILE_STATE_CONNECTED)
1854 __bt_set_device_values(TRUE,
1855 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1857 __bt_set_device_values(FALSE,
1858 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1861 g_free(profile_uuid);
1862 } else if (strcasecmp(member, "AdvReport") == 0) {
1864 bt_remote_le_dev_info_t *le_dev_info = NULL;
1865 char *buffer = NULL;
1867 bt_le_adv_info_t *adv_info = NULL;
1868 GVariant *value = NULL;
1869 ret_if(_bt_is_le_scanning() == FALSE);
1871 le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
1872 if (le_dev_info == NULL)
1875 g_variant_get(msg, "(syyii@ay)", &le_dev_info->address,
1876 &le_dev_info->addr_type,
1877 &le_dev_info->adv_type,
1879 &le_dev_info->adv_data_len,
1881 buffer_len = g_variant_get_size(value);
1883 buffer = (char *)g_variant_get_data(value);
1885 le_dev_info->adv_data = g_memdup(buffer, buffer_len);
1886 if (le_dev_info->adv_data == NULL &&
1887 le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {
1888 _bt_free_le_device_info(le_dev_info);
1889 g_variant_unref(value);
1893 if (_bt_get_le_scan_type() == BT_LE_PASSIVE_SCAN) {
1894 _bt_send_scan_result_event(le_dev_info, NULL);
1895 _bt_free_le_device_info(le_dev_info);
1896 g_variant_unref(value);
1900 if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) { /* ADV_IND */
1901 adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
1902 if (adv_info == NULL) {
1903 _bt_free_le_device_info(le_dev_info);
1904 g_variant_unref(value);
1908 adv_info->addr = g_strdup(le_dev_info->address);
1909 adv_info->data_len = le_dev_info->adv_data_len;
1910 adv_info->data = g_malloc0(le_dev_info->adv_data_len);
1911 if (adv_info->data) {
1912 memcpy(adv_info->data, le_dev_info->adv_data,
1913 le_dev_info->adv_data_len);
1915 __bt_add_adv_ind_info(adv_info);
1918 } else { /* SCAN_RSP */
1919 adv_info = __bt_get_adv_ind_info(le_dev_info->address);
1921 _bt_send_scan_result_event(le_dev_info, adv_info);
1922 __bt_del_adv_ind_info(le_dev_info->address);
1925 _bt_free_le_device_info(le_dev_info);
1926 g_variant_unref(value);
1927 } else if (strcasecmp(member, "LEDataLengthChanged") == 0) {
1933 g_variant_get(msg, "(qqqq)",
1934 tx_octets, tx_time, rx_octets, rx_time);
1936 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1937 _bt_convert_device_path_to_address(path, address);
1939 param = g_variant_new("(isqqqq)", result, address, tx_octets, tx_time,
1940 rx_octets, rx_time);
1941 /* Send event to application */
1942 _bt_send_event(BT_DEVICE_EVENT, event, param);
1948 void __bt_set_audio_values(gboolean connected, char *address)
1951 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1953 /* Set the headset name */
1954 if (connected == TRUE) {
1955 name = _bt_get_bonded_device_name(address);
1957 name = g_strdup("");
1960 if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME,
1962 BT_ERR("vconf_set_str failed");
1967 /* Set the headset state */
1968 if (vconf_get_int(VCONFKEY_BT_DEVICE,
1969 &bt_device_state) != 0) {
1970 BT_ERR("vconf_get_str failed");
1973 #ifdef TIZEN_SUPPORT_DUAL_HF
1974 if ((connected == TRUE) &&
1975 (FALSE == __bt_is_companion_device(address))) {
1976 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1977 } else if ((bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) &&
1978 (FALSE == __bt_is_companion_device(address))) {
1979 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1982 if (connected == TRUE) {
1983 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1984 } else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) {
1985 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1989 if (vconf_set_int(VCONFKEY_BT_DEVICE,
1990 bt_device_state) != 0) {
1991 BT_ERR("vconf_set_int failed");
1995 void _bt_handle_headset_event(GVariant *msg, const char *path)
1997 int result = BLUETOOTH_ERROR_NONE;
1998 gboolean property_flag = FALSE;
1999 char *property = NULL;
2000 GVariant *value = NULL;
2001 GVariant *param = NULL;
2002 g_variant_get(msg, "(sv)", &property, &value);
2004 ret_if(property == NULL);
2006 BT_DBG("Property = %s \n", property);
2007 /* We allow only 1 headset connection (HSP or HFP)*/
2008 if (strcasecmp(property, "Connected") == 0) {
2009 int event = BLUETOOTH_EVENT_NONE;
2010 bt_headset_wait_t *wait_list;
2012 g_variant_get(value, "b", &property_flag);
2014 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2016 /* Fix : NULL_RETURNS */
2017 if (address == NULL)
2020 _bt_convert_device_path_to_address(path, address);
2022 if (property_flag == TRUE) {
2023 event = BLUETOOTH_EVENT_AG_CONNECTED;
2024 if (_bt_headset_get_local_connection() == FALSE)
2025 _bt_start_timer_for_connection(address, BT_AUDIO_A2DP);
2027 _bt_headset_set_local_connection(FALSE);
2031 event = BLUETOOTH_EVENT_AG_DISCONNECTED;
2033 previous_state = _bt_get_device_state_from_list(BT_AUDIO_HSP, address);
2034 if (previous_state == BT_STATE_DISCONNECTING)
2035 _bt_send_hf_local_term_event(address);
2037 /* Set the State machine here */
2038 __bt_connection_manager_set_state(address, event);
2039 __bt_set_audio_values(property_flag, address);
2040 param = g_variant_new("(is)", result, address);
2041 _bt_send_event(BT_HEADSET_EVENT, event,
2044 if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
2045 /* Remove data from the connected list */
2046 _bt_remove_headset_from_list(BT_AUDIO_HSP, address);
2048 wait_list = _bt_get_audio_wait_data();
2049 if (wait_list == NULL) {
2054 bluetooth_device_address_t device_address;
2056 _bt_set_audio_wait_data_flag(TRUE);
2058 _bt_convert_addr_string_to_type(device_address.addr,
2059 wait_list->address);
2060 _bt_audio_connect(wait_list->req_id, wait_list->type,
2061 &device_address, wait_list->out_param1);
2062 _bt_rel_wait_data();
2063 } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
2064 /* Add data to the connected list */
2065 _bt_add_headset_to_list(BT_AUDIO_HSP,
2066 BT_STATE_CONNECTED, address);
2068 wait_list = _bt_get_audio_wait_data();
2069 if (wait_list != NULL &&
2070 (g_strcmp0(wait_list->address, address) == 0))
2071 _bt_rel_wait_data();
2073 BT_INFO("Check A2DP pending connect");
2074 _bt_audio_check_pending_connect();
2077 } else if (strcasecmp(property, "State") == 0) {
2080 g_variant_get(value, "s", &state);
2082 /* This code assumes we support only 1 headset connection */
2083 /* Need to use the headset list, if we support multi-headsets */
2084 if (strcasecmp(state, "Playing") == 0) {
2085 BT_DBG("Playing: Sco Connected");
2086 } else if (strcasecmp(state, "connected") == 0 ||
2087 strcasecmp(state, "disconnected") == 0) {
2088 BT_DBG("connected/disconnected: Sco Disconnected");
2090 BT_ERR("Not handled state - %s", state);
2095 } else if (strcasecmp(property, "SpeakerGain") == 0) {
2099 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2101 _bt_convert_device_path_to_address(path, address);
2103 g_variant_get(value, "i", &spkr_gain);
2104 param = g_variant_new("(isq)", result, address, spkr_gain);
2105 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
2109 } else if (strcasecmp(property, "MicrophoneGain") == 0) {
2113 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2115 _bt_convert_device_path_to_address(path, address);
2117 g_variant_get(value, "i", &mic_gain);
2118 param = g_variant_new("(isq)", result, address, mic_gain);
2119 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_MIC_GAIN,
2126 g_variant_unref(value);
2129 void _bt_handle_sink_event(GVariant *msg, const char *path)
2131 GVariantIter value_iter;
2132 char *property = NULL;
2134 bt_headset_wait_t *wait_list;
2136 GVariant *child = NULL;
2137 GVariant *val = NULL;
2138 GVariant *param = NULL;
2139 g_variant_iter_init (&value_iter, msg);
2140 while ((child = g_variant_iter_next_value (&value_iter))) {
2142 g_variant_get(child, "{sv}", &property, &val);
2144 ret_if(property == NULL);
2146 BT_DBG("Property = %s \n", property);
2149 if (strcasecmp(property, "State") == 0) {
2150 int result = BLUETOOTH_ERROR_NONE;
2153 g_variant_get(val, "s", &value);
2154 BT_DBG("value: %s", value);
2156 if (g_strcmp0(value, "disconnected") == 0) {
2159 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2161 _bt_convert_device_path_to_address(path, address);
2163 __bt_set_device_values(FALSE,
2164 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2165 param = g_variant_new("(is)", result, address);
2166 _bt_send_event(BT_HEADSET_EVENT,
2167 BLUETOOTH_EVENT_AV_DISCONNECTED,
2170 /* Remove data from the connected list */
2171 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2172 wait_list = _bt_get_audio_wait_data();
2173 if (wait_list == NULL) {
2176 g_variant_unref(val);
2177 g_variant_unref(child);
2182 if (((wait_list->type == BT_AUDIO_ALL) &&
2183 (wait_list->ag_flag == TRUE)) ||
2184 (wait_list->type == BT_AUDIO_A2DP) ||
2185 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2186 bluetooth_device_address_t device_address;
2187 _bt_convert_addr_string_to_type(
2188 device_address.addr,
2189 wait_list->address);
2191 _bt_audio_connect(wait_list->req_id,
2194 wait_list->out_param1);
2197 }else if (strcasecmp(value, "Connected") == 0) {
2199 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2200 bluetooth_device_address_t device_address;
2203 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2205 _bt_convert_device_path_to_address(path, address);
2207 __bt_set_device_values(TRUE,
2208 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2209 param = g_variant_new("(is)", result, address);
2210 _bt_send_event(BT_HEADSET_EVENT,
2211 BLUETOOTH_EVENT_AV_CONNECTED,
2213 /* Check for existing Media device to disconnect */
2214 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
2217 /* Match connected device address */
2218 if (g_strcmp0(connected_address, address) != 0) {
2219 /* Convert BD adress from string type */
2220 _bt_convert_addr_string_to_type(
2221 device_address.addr,
2223 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2224 &device_address, NULL);
2228 /* Add data to the connected list */
2229 _bt_add_headset_to_list(BT_AUDIO_A2DP,
2230 BT_STATE_CONNECTED, address);
2237 g_variant_unref(val);
2238 g_variant_unref(child);
2242 void _bt_handle_agent_event(GVariant *msg, const char *member)
2244 int result = BLUETOOTH_ERROR_NONE;
2245 char *address = NULL;
2248 GVariant *param = NULL;
2249 ret_if(member == NULL);
2251 if (strcasecmp(member, "ObexAuthorize") == 0) {
2252 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2253 param = g_variant_new("(iss)", result, address, name);
2254 _bt_send_event(BT_OPP_SERVER_EVENT,
2255 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
2259 } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2260 bt_rfcomm_server_info_t *server_info;
2262 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2264 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2265 ret_if(server_info == NULL);
2266 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2267 param = g_variant_new("(isssn)", result, address, uuid, name,
2268 server_info->control_fd);
2269 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2270 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2278 static int __bt_get_object_path(GVariant *msg, char **path)
2280 g_variant_get(msg, "(o*)", path, NULL);
2282 return BLUETOOTH_ERROR_INTERNAL;
2284 return BLUETOOTH_ERROR_NONE;
2287 static void __bt_devices_list_free(void)
2289 bt_cache_info_t *cache_info;
2292 node = g_list_first(p_cache_list);
2294 while (node != NULL){
2295 cache_info = (bt_cache_info_t *)node->data;
2296 p_cache_list = g_list_remove(p_cache_list, cache_info);
2297 __bt_free_cache_info(cache_info);
2299 node = g_list_next(node);
2303 static int __bt_parse_event(GVariant *msg)
2307 char *interface_name= NULL;
2308 GVariant *inner_iter = NULL;
2310 g_variant_iter_init(&iter, msg);
2312 while ((child = g_variant_iter_next_value(&iter))) {
2313 g_variant_get(child,"{&s@a{sv}}", &interface_name, &inner_iter);
2314 if (g_strcmp0(interface_name,
2315 BT_DEVICE_INTERFACE) == 0) {
2316 g_variant_unref(inner_iter);
2317 g_variant_unref(child);
2318 return BT_DEVICE_EVENT;
2319 } else if (g_strcmp0(interface_name,
2320 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2321 g_variant_unref(inner_iter);
2322 g_variant_unref(child);
2323 return BT_MEDIA_TRANSFER_EVENT;
2324 } else if (g_strcmp0(interface_name,
2325 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2326 g_variant_unref(inner_iter);
2327 g_variant_unref(child);
2328 return BT_AVRCP_CONTROL_EVENT;
2330 g_variant_unref(inner_iter);
2331 g_variant_unref(child);
2337 static void __bt_manager_event_filter(GDBusConnection *connection,
2338 const gchar *sender_name,
2339 const gchar *object_path,
2340 const gchar *interface_name,
2341 const gchar *signal_name,
2342 GVariant *parameters,
2345 bt_event_type_t bt_event = 0x00;
2346 int result = BLUETOOTH_ERROR_NONE;
2348 char *obj_path = NULL;
2349 GVariant *param = NULL;
2350 if (signal_name == NULL)
2352 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2353 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2355 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
2356 #ifdef USB_BLUETOOTH
2357 BT_DBG("Enable Adapter");
2358 _bt_enable_adapter();
2360 _bt_handle_adapter_added();
2364 bt_event = __bt_parse_event(value);
2365 if (bt_event == BT_DEVICE_EVENT) {
2366 bt_cache_info_t *cache_info;
2367 bt_remote_dev_info_t *dev_info;
2369 ret_if(_bt_is_discovering() == FALSE &&
2370 _bt_is_le_scanning() == FALSE);
2372 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2373 ret_if(cache_info == NULL);
2375 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2376 if (dev_info == NULL) {
2377 __bt_free_cache_info(cache_info);
2381 cache_info->dev_info = dev_info;
2383 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2384 BT_ERR("Fail to parse the properies");
2385 __bt_free_cache_info(cache_info);
2386 g_variant_unref(value);
2390 if (dev_info->addr_type != BDADDR_BREDR) {
2391 /* Whenever emit the property changed from bluez,
2392 some property doesn't reach to bt-service.
2393 So LE device is handled as AdvReport signal */
2394 __bt_free_cache_info(cache_info);
2395 g_variant_unref(value);
2399 if (dev_info->name == NULL)
2400 /* If Remote device name is NULL or still RNR is not done
2401 * then display address as name.
2403 dev_info->name = g_strdup(dev_info->address);
2405 GVariant *uuids = NULL;
2406 GVariantBuilder *builder = NULL;
2408 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2409 for (i=0; i < dev_info->uuid_count; i++) {
2410 g_variant_builder_add(builder, "s",
2411 dev_info->uuids[i]);
2413 uuids = g_variant_new("as", builder);
2414 g_variant_builder_unref(builder);
2415 GVariant *manufacturer_data = NULL;
2416 manufacturer_data = g_variant_new_from_data(
2417 G_VARIANT_TYPE_BYTESTRING,
2418 dev_info->manufacturer_data,
2419 dev_info->manufacturer_data_len,
2421 param = g_variant_new("(isunsbub@asn@ay)", result,
2427 dev_info->connected,
2430 dev_info->manufacturer_data_len,
2432 _bt_send_event(BT_ADAPTER_EVENT,
2433 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2435 p_cache_list = g_list_append(p_cache_list, cache_info);
2436 } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2437 BT_DBG("Device path : %s ", obj_path);
2438 _bt_set_control_device_path(obj_path);
2441 g_variant_unref(value);
2442 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2443 #ifdef USB_BLUETOOTH
2444 BT_DBG("InterfacesRemoved");
2445 _bt_handle_adapter_removed();
2447 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0) {
2448 bt_event = BT_MEDIA_TRANSFER_EVENT;
2449 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2450 bt_event = BT_DEVICE_EVENT;
2451 } else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
2452 bt_event = BT_AVRCP_CONTROL_EVENT;
2454 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2455 _bt_handle_adapter_event(parameters, signal_name);
2456 if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2457 BT_INFO("Object Path %s", obj_path);
2458 _bt_remove_control_device_path(obj_path);
2461 } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2464 char *previous = NULL;
2465 char *current = NULL;
2467 if (__bt_get_owner_info(parameters, &name, &previous, ¤t)) {
2468 BT_ERR("Fail to get the owner info");
2472 if (*current != '\0') {
2481 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2482 BT_DBG("Bluetoothd is terminated");
2483 if (_bt_adapter_get_status() == BT_ACTIVATED)
2486 _bt_handle_adapter_removed();
2487 __bt_devices_list_free();
2490 _bt_obex_server_check_allocation(&value);
2492 if (value == TRUE) {
2493 /* Check if the obex server was terminated abnormally */
2494 _bt_obex_server_check_termination(name);
2497 _bt_rfcomm_server_check_existence(&value);
2499 if (value == TRUE) {
2500 /* The obex server was terminated abnormally */
2501 _bt_rfcomm_server_check_termination(name);
2504 /* Stop advertising started by terminated process */
2505 _bt_stop_advertising_by_terminated_process(name);
2507 _bt_stop_le_scan(name);
2511 } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2512 const char *path = object_path;
2514 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2515 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2518 _bt_handle_property_changed_event(parameters, object_path);
2519 } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2520 _bt_handle_adapter_event(parameters, signal_name);
2521 } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2522 _bt_handle_input_event(parameters, object_path);
2523 } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2524 _bt_handle_network_server_event(parameters, signal_name);
2525 } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2526 _bt_handle_headset_event(parameters, object_path);
2527 } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2528 _bt_handle_sink_event(parameters, object_path);
2529 } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2530 _bt_handle_agent_event(parameters, signal_name);
2531 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2532 _bt_handle_device_event(parameters, signal_name, object_path);
2533 } else if (g_strcmp0(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
2534 _bt_handle_gatt_event(parameters, signal_name, object_path);
2540 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2543 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2544 char *interface_name = NULL;
2546 g_variant_get(msg,"(&s@a{sv}@as)", &interface_name, NULL, NULL);
2547 retv_if(interface_name == NULL, FALSE);
2549 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2550 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2558 static void __bt_obexd_event_filter(GDBusConnection *connection,
2559 const gchar *sender_name,
2560 const gchar *object_path,
2561 const gchar *interface_name,
2562 const gchar *signal_name,
2563 GVariant *parameters,
2566 const char *member = signal_name;
2567 char *obj_path = NULL;
2568 ret_if(member == NULL);
2570 if (strcasecmp(member, "InterfacesAdded") == 0) {
2571 if (__bt_get_object_path(parameters, &obj_path)) {
2572 BT_ERR("Fail to get the path");
2575 BT_INFO("object_path = [%s]", object_path);
2577 /*Handle OPP_SERVER_CONNECTED_EVENT here */
2578 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2579 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2584 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2585 BT_DBG("Obex_Server_Session_Transfer connected");
2586 _bt_obex_transfer_connected();
2589 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2590 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
2591 if (__bt_get_object_path(parameters, &obj_path)) {
2592 BT_ERR("Fail to get the path");
2595 BT_INFO("object_path = [%s]", object_path);
2597 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2598 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2599 BT_DBG("Call PBAP Disconnected");
2600 _bt_obex_pbap_client_disconnect(obj_path);
2603 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2604 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2609 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2610 BT_DBG("Obex_Server_Session_Transfer disconnected");
2611 _bt_obex_transfer_disconnected();
2614 } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
2615 const char *path = object_path;
2617 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
2618 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
2619 strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2620 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2621 BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
2625 _bt_handle_property_changed_event(parameters, path);
2631 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
2635 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2636 char *interface_name = NULL;
2638 g_variant_get(msg,"(&s@a{sv}@as)", &interface_name, NULL, NULL);
2640 retv_if(interface_name == NULL, FALSE);
2642 if (strcasecmp(interface_name,
2643 BT_OBEX_TRANSFER_INTERFACE) == 0) {
2654 static void __bt_opc_event_filter(GDBusConnection *connection,
2655 const gchar *sender_name,
2656 const gchar *object_path,
2657 const gchar *interface_name,
2658 const gchar *signal_name,
2659 GVariant *parameters,
2662 const char *member = signal_name;
2663 char *obj_path = NULL;
2664 if (strcasecmp(member, "InterfacesAdded") == 0) {
2665 BT_DBG("InterfacesAdded");
2666 }else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2668 if (__bt_get_object_path(parameters, &obj_path)) {
2669 BT_ERR("Fail to get the path");
2673 BT_DBG("object_path =%s",obj_path);
2675 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2676 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
2677 || strstr(obj_path, "transfer") == NULL) {
2681 else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2682 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2683 BT_DBG("Going to call opc disconnected");
2684 _bt_opc_disconnected(obj_path);
2687 _bt_sending_files();
2689 }else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE){
2690 char *path = (char *)object_path;
2691 BT_INFO("object_path %s", path);
2692 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2693 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2694 BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
2698 _bt_opc_property_changed_event(parameters, path);
2704 int _bt_opp_client_event_init(void)
2706 GError *error = NULL;
2708 if (opc_obexd_conn == NULL) {
2709 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
2711 if (!opc_obexd_conn) {
2713 BT_ERR("Unable to connect to dbus: %s", error->message);
2714 g_clear_error(&error);
2716 return BLUETOOTH_ERROR_INTERNAL;
2720 if (_bt_register_service_event(opc_obexd_conn,
2721 BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
2722 g_object_unref(opc_obexd_conn);
2723 opc_obexd_conn = NULL;
2724 return BLUETOOTH_ERROR_INTERNAL;
2727 return BLUETOOTH_ERROR_NONE;
2730 void _bt_opp_client_event_deinit(void)
2732 if (opc_obexd_conn) {
2733 _bt_unregister_service_event(opc_obexd_conn,
2734 BT_OPP_CLIENT_EVENT);
2735 g_object_unref(opc_obexd_conn);
2736 opc_obexd_conn = NULL;
2740 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
2746 static int subs_interface_added_id = -1;
2747 static int subs_interface_removed_id = -1;
2748 static int subs_name_owner_id = -1;
2749 static int subs_property_id = -1;
2750 static int subs_adapter_id = -1;
2751 static int subs_gatt_id = -1;
2754 if (subs_interface_added_id == -1) {
2755 subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
2756 NULL, BT_MANAGER_INTERFACE,
2757 BT_INTERFACES_ADDED, NULL, NULL, 0,
2758 __bt_manager_event_filter,
2761 if (subs_interface_removed_id == -1) {
2762 subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
2763 NULL, BT_MANAGER_INTERFACE,
2764 BT_INTERFACES_REMOVED, NULL, NULL, 0,
2765 __bt_manager_event_filter,
2768 if (subs_name_owner_id == -1) {
2769 subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
2770 NULL, BT_FREEDESKTOP_INTERFACE,
2771 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
2772 __bt_manager_event_filter,
2775 if (subs_property_id == -1) {
2776 subs_property_id = g_dbus_connection_signal_subscribe(conn,
2777 NULL, BT_PROPERTIES_INTERFACE,
2778 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
2779 __bt_manager_event_filter,
2782 if (subs_adapter_id == -1) {
2783 subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
2784 NULL, BT_ADAPTER_INTERFACE,
2785 NULL, NULL, NULL, 0,
2786 __bt_manager_event_filter,
2789 if (subs_gatt_id == -1) {
2790 subs_gatt_id = g_dbus_connection_signal_subscribe(conn,
2791 NULL, BT_GATT_CHAR_INTERFACE,
2792 NULL, NULL, NULL, 0,
2793 __bt_manager_event_filter,
2797 if (subs_interface_added_id != -1) {
2798 g_dbus_connection_signal_unsubscribe(conn,
2799 subs_interface_added_id);
2800 subs_interface_added_id = -1;
2802 if (subs_interface_removed_id != -1) {
2803 g_dbus_connection_signal_unsubscribe(conn,
2804 subs_interface_removed_id);
2805 subs_interface_removed_id = -1;
2807 if (subs_name_owner_id != -1) {
2808 g_dbus_connection_signal_unsubscribe(conn,
2809 subs_name_owner_id);
2810 subs_name_owner_id = -1;
2812 if (subs_property_id != -1) {
2813 g_dbus_connection_signal_unsubscribe(conn,
2815 subs_property_id = -1;
2817 if (subs_adapter_id != -1) {
2818 g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
2819 subs_adapter_id = -1;
2821 if (subs_gatt_id != -1) {
2822 g_dbus_connection_signal_unsubscribe(conn, subs_gatt_id);
2829 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
2835 static int subs_device_id = -1;
2838 if (subs_device_id == -1) {
2839 subs_device_id = g_dbus_connection_signal_subscribe(conn,
2840 NULL, BT_DEVICE_INTERFACE,
2841 NULL, NULL, NULL, 0,
2842 __bt_manager_event_filter,
2846 if (subs_device_id != -1) {
2847 g_dbus_connection_signal_unsubscribe(conn,
2849 subs_device_id = -1;
2855 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
2861 static int subs_input_id = -1;
2864 if (subs_input_id == -1) {
2865 subs_input_id = g_dbus_connection_signal_subscribe(conn,
2866 NULL, BT_INPUT_INTERFACE,
2867 NULL, NULL, NULL, 0,
2868 __bt_manager_event_filter,
2872 if (subs_input_id != -1) {
2873 g_dbus_connection_signal_unsubscribe(conn,
2881 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
2887 static int subs_serv_id = -1;
2888 static int subs_client_id = -1;
2891 if (subs_serv_id == -1) {
2892 subs_serv_id = g_dbus_connection_signal_subscribe(conn,
2893 NULL, BT_NETWORK_SERVER_INTERFACE,
2894 NULL, NULL, NULL, 0,
2895 __bt_manager_event_filter,
2898 if (subs_client_id == -1) {
2899 subs_client_id = g_dbus_connection_signal_subscribe(conn,
2900 NULL, BT_NETWORK_CLIENT_INTERFACE,
2901 NULL, NULL, NULL, 0,
2902 __bt_manager_event_filter,
2906 if (subs_serv_id != -1) {
2907 g_dbus_connection_signal_unsubscribe(conn,
2911 if (subs_client_id != -1) {
2912 g_dbus_connection_signal_unsubscribe(conn,
2914 subs_client_id = -1;
2920 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
2926 static int subs_headset_id = -1;
2927 static int subs_sink_id = -1;
2930 if (subs_headset_id == -1) {
2931 subs_headset_id = g_dbus_connection_signal_subscribe(conn,
2932 NULL, BT_HEADSET_INTERFACE,
2933 NULL, NULL, NULL, 0,
2934 __bt_manager_event_filter,
2937 if (subs_sink_id == -1) {
2938 subs_sink_id = g_dbus_connection_signal_subscribe(conn,
2939 NULL, BT_SINK_INTERFACE,
2940 NULL, NULL, NULL, 0,
2941 __bt_manager_event_filter,
2945 if (subs_headset_id != -1) {
2946 g_dbus_connection_signal_unsubscribe(conn,
2948 subs_headset_id = -1;
2950 if (subs_sink_id != -1) {
2951 g_dbus_connection_signal_unsubscribe(conn,
2959 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
2965 static int subs_opp_server_interface_added_id = -1;
2966 static int subs_opp_server_interface_removed_id = -1;
2967 static int subs_opp_server_property_id = -1;
2971 if (subs_opp_server_interface_added_id == -1) {
2972 subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
2973 NULL, BT_MANAGER_INTERFACE,
2974 BT_INTERFACES_ADDED, NULL, NULL, 0,
2975 __bt_obexd_event_filter,
2978 if (subs_opp_server_interface_removed_id == -1) {
2979 subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
2980 NULL, BT_MANAGER_INTERFACE,
2981 BT_INTERFACES_REMOVED, NULL, NULL, 0,
2982 __bt_obexd_event_filter,
2985 if (subs_opp_server_property_id == -1) {
2986 subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
2987 NULL, BT_PROPERTIES_INTERFACE,
2988 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
2989 __bt_obexd_event_filter,
2993 if (subs_opp_server_interface_added_id != -1) {
2994 g_dbus_connection_signal_unsubscribe(conn,
2995 subs_opp_server_interface_added_id);
2996 subs_opp_server_interface_added_id = -1;
2998 if (subs_opp_server_interface_removed_id != -1) {
2999 g_dbus_connection_signal_unsubscribe(conn,
3000 subs_opp_server_interface_removed_id);
3001 subs_opp_server_interface_removed_id = -1;
3003 if (subs_opp_server_property_id != -1) {
3004 g_dbus_connection_signal_unsubscribe(conn,
3005 subs_opp_server_property_id);
3006 subs_opp_server_property_id = -1;
3012 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
3018 static int subs_opp_client_interface_added_id = -1;
3019 static int subs_opp_client_interface_removed_id = -1;
3020 static int subs_opp_client_property_id = -1;
3024 if (subs_opp_client_interface_added_id == -1) {
3025 subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3026 NULL, BT_MANAGER_INTERFACE,
3027 BT_INTERFACES_ADDED, NULL, NULL, 0,
3028 __bt_opc_event_filter,
3031 if (subs_opp_client_interface_removed_id == -1) {
3032 subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3033 NULL, BT_MANAGER_INTERFACE,
3034 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3035 __bt_opc_event_filter,
3038 if (subs_opp_client_property_id == -1) {
3039 subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
3040 NULL, BT_PROPERTIES_INTERFACE,
3041 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3042 __bt_opc_event_filter,
3046 if (subs_opp_client_interface_added_id != -1) {
3047 g_dbus_connection_signal_unsubscribe(conn,
3048 subs_opp_client_interface_added_id);
3049 subs_opp_client_interface_added_id = -1;
3051 if (subs_opp_client_interface_removed_id != -1) {
3052 g_dbus_connection_signal_unsubscribe(conn,
3053 subs_opp_client_interface_removed_id);
3054 subs_opp_client_interface_removed_id = -1;
3056 if (subs_opp_client_property_id != -1) {
3057 g_dbus_connection_signal_unsubscribe(conn,
3058 subs_opp_client_property_id);
3059 subs_opp_client_property_id = -1;
3065 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
3071 static int subs_a2dp_source_id = -1;
3072 static int subs_a2dp_sink_id = -1;
3075 if (subs_a2dp_source_id == -1) {
3076 subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
3077 NULL, BT_A2DP_SOURCE_INTERFACE,
3078 NULL, NULL, NULL, 0,
3079 __bt_opc_event_filter,
3082 if (subs_a2dp_sink_id == -1) {
3083 subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
3084 NULL, BT_SINK_INTERFACE,
3085 NULL, NULL, NULL, 0,
3086 __bt_opc_event_filter,
3090 if (subs_a2dp_source_id != -1) {
3091 g_dbus_connection_signal_unsubscribe(conn,
3092 subs_a2dp_source_id);
3093 subs_a2dp_source_id = -1;
3095 if (subs_a2dp_sink_id != -1) {
3096 g_dbus_connection_signal_unsubscribe(conn,
3098 subs_a2dp_sink_id = -1;
3104 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
3108 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3110 switch (event_type) {
3111 case BT_MANAGER_EVENT:
3112 _bt_register_manager_subscribe_signal(g_conn, TRUE);
3114 case BT_DEVICE_EVENT:
3115 _bt_register_device_subscribe_signal(g_conn, TRUE);
3118 _bt_register_input_subscribe_signal(g_conn, TRUE);
3120 case BT_NETWORK_EVENT:
3121 _bt_register_network_subscribe_signal(g_conn, TRUE);
3123 case BT_HEADSET_EVENT:
3124 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3127 case BT_OPP_SERVER_EVENT:
3128 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3129 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3131 case BT_OPP_CLIENT_EVENT:
3132 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3133 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3135 case BT_A2DP_SOURCE_EVENT:
3136 BT_INFO("A2dp Source event");
3137 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3140 BT_ERR("Unknown event");
3141 return BLUETOOTH_ERROR_INTERNAL;
3144 return BLUETOOTH_ERROR_NONE;
3147 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3151 ret_if(g_conn == NULL);
3153 switch (event_type) {
3154 case BT_MANAGER_EVENT:
3155 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3156 _bt_register_device_subscribe_signal(g_conn, FALSE);
3157 _bt_register_input_subscribe_signal(g_conn, FALSE);
3158 _bt_register_network_subscribe_signal(g_conn, FALSE);
3159 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3161 case BT_OPP_SERVER_EVENT:
3162 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3164 case BT_OPP_CLIENT_EVENT:
3165 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3168 BT_ERR("Unknown event");
3175 static int __bt_init_manager_receiver(void)
3179 GError *error = NULL;
3181 if (manager_conn == NULL) {
3182 manager_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3183 if (error != NULL) {
3184 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3185 g_clear_error(&error);
3187 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3190 if (_bt_register_service_event(manager_conn,
3191 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3193 if (_bt_register_service_event(manager_conn,
3194 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3197 if (_bt_register_service_event(manager_conn,
3198 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3201 if (_bt_register_service_event(manager_conn,
3202 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3205 if (_bt_register_service_event(manager_conn,
3206 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3208 return BLUETOOTH_ERROR_NONE;
3211 g_object_unref(manager_conn);
3212 manager_conn = NULL;
3217 return BLUETOOTH_ERROR_INTERNAL;
3220 static int __bt_init_obexd_receiver(void)
3223 #ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3224 GError *error = NULL;
3226 if (obexd_conn == NULL) {
3227 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3228 if (error != NULL) {
3229 BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3230 g_clear_error(&error);
3232 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3235 if (_bt_register_service_event(obexd_conn,
3236 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3237 BT_ERR("Error while registering service event");
3238 g_object_unref(obexd_conn);
3240 return BLUETOOTH_ERROR_INTERNAL;
3245 return BLUETOOTH_ERROR_NONE;
3248 /* To receive the event from bluez */
3249 int _bt_init_service_event_receiver(void)
3255 result = __bt_init_manager_receiver();
3256 retv_if(result != BLUETOOTH_ERROR_NONE, result);
3258 result = __bt_init_obexd_receiver();
3259 if (result != BLUETOOTH_ERROR_NONE)
3260 BT_ERR("Fail to init obexd receiver");
3264 return BLUETOOTH_ERROR_NONE;
3267 void _bt_deinit_service_event_receiver(void)
3271 _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3273 _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3276 g_object_unref(manager_conn);
3277 manager_conn = NULL;
3281 g_object_unref(obexd_conn);
3286 g_source_remove(event_id);