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"
41 #include "bt-service-hid.h"
43 #include "bt-service-opp-client.h"
45 #ifdef TIZEN_DPM_ENABLE
46 #include "bt-service-dpm.h"
49 #define DBUS_TIMEOUT 20 * 1000 /* 20 Sec */
50 static GDBusConnection *manager_conn;
51 static GDBusConnection *obexd_conn;
52 static GDBusConnection *opc_obexd_conn;
54 static GList *p_cache_list = NULL;
56 static guint event_id;
57 static guint session_reinit_timer;
58 guint nap_connected_device_count = 0;
59 static guint hid_connected_device_count = 0;
60 static GList *p_adv_ind_list = NULL;
63 bt_remote_dev_info_t *dev_info;
67 * obexd connection type
75 OBEX_PCSUITE = (1 << 6),
76 OBEX_SYNCEVOLUTION = (1 << 7),
78 } bluetooth_obex_connection_type_t;
80 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path);
81 void _bt_opc_property_changed_event(GVariant *msg, char *path);
82 int _bt_register_service_event(GDBusConnection *g_conn, int event_type);
83 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type);
84 void _bt_opp_client_event_deinit(void);
85 void _bt_handle_network_client_event(GVariant *msg_iter,
87 void __bt_gatt_char_property_changed_event(GVariant *msg_iter,
90 static void __bt_free_bt_le_adv_info_t(gpointer data)
92 bt_le_adv_info_t *adv_info = data;
94 if (adv_info->timer_id)
95 g_source_remove(adv_info->timer_id);
96 adv_info->timer_id = 0;
98 g_free(adv_info->addr);
99 g_free(adv_info->data);
103 static bt_le_adv_info_t *__bt_get_adv_ind_info(char *addr)
105 retv_if(!addr, NULL);
106 bt_le_adv_info_t *adv_info = NULL;
107 GList *current = g_list_first((GList *)p_adv_ind_list);
108 while(current && current->data) {
109 adv_info = (bt_le_adv_info_t *)current->data;
110 if (adv_info && !g_strcmp0(adv_info->addr, addr))
112 current = g_list_next(current);
117 static int __bt_add_adv_ind_info(bt_le_adv_info_t *adv_info)
119 retv_if(!adv_info, -1);
120 if (__bt_get_adv_ind_info(adv_info->addr) != NULL) {
121 __bt_free_bt_le_adv_info_t(adv_info);
124 p_adv_ind_list = g_list_append(p_adv_ind_list, adv_info);
129 static void __bt_del_adv_ind_info(char *addr)
132 ret_if(!p_adv_ind_list);
133 bt_le_adv_info_t *adv_info = NULL;
134 GList *current = g_list_first((GList *)p_adv_ind_list);
135 while (current && current->data) {
136 adv_info = (bt_le_adv_info_t *)current->data;
137 if (adv_info && !g_strcmp0(adv_info->addr, addr)) {
138 p_adv_ind_list = g_list_remove(p_adv_ind_list, adv_info);
139 __bt_free_bt_le_adv_info_t(adv_info);
142 current = g_list_next(current);
146 static gboolean __bt_adv_scan_req_timeout_cb(gpointer user_data)
148 bt_remote_le_dev_info_t le_dev_info;
149 bt_le_adv_info_t *adv_info = (bt_le_adv_info_t*)user_data;
151 memset(&le_dev_info, 0x00, sizeof(bt_remote_le_dev_info_t));
153 le_dev_info.address = adv_info->addr;
154 le_dev_info.addr_type = adv_info->addr_type;
155 le_dev_info.rssi = adv_info->rssi;
156 le_dev_info.adv_data = adv_info->data;
157 le_dev_info.adv_data_len = 0;
158 adv_info->timer_id = 0;
160 _bt_send_scan_result_event(&le_dev_info, adv_info);
161 __bt_del_adv_ind_info(adv_info->addr);
166 static void __bt_free_cache_info(bt_cache_info_t *cache_info)
168 ret_if(cache_info == NULL);
169 _bt_free_device_info(cache_info->dev_info);
173 static gboolean __bt_parse_device_properties(GVariant *item,
174 bt_remote_dev_info_t *dev_info)
183 g_variant_iter_init(&iter, item);
184 while (g_variant_iter_loop(&iter, "{sv}", &key, &val)) {
185 if (strcasecmp(key, "Address") == 0) {
186 dev_info->address = g_variant_dup_string(val, &len);
187 } else if (strcasecmp(key, "Class") == 0) {
188 dev_info->class = g_variant_get_uint32(val);
189 } else if (strcasecmp(key, "name") == 0) {
190 if (dev_info->name == NULL)
191 dev_info->name = g_variant_dup_string(val, &len);
192 } else if (strcasecmp(key, "Connected") == 0) {
193 dev_info->connected = g_variant_get_byte(val);
194 } else if (strcasecmp(key, "paired") == 0) {
195 dev_info->paired = g_variant_get_boolean(val);
196 } else if (strcasecmp(key, "Trusted") == 0) {
197 dev_info->trust = g_variant_get_boolean(val);
198 } else if (strcasecmp(key, "RSSI") == 0) {
199 dev_info->rssi = g_variant_get_int16(val);
200 } else if (strcasecmp(key, "LastAddrType") == 0) {
201 dev_info->addr_type = g_variant_get_byte(val);
202 } else if (strcasecmp(key, "UUIDs") == 0) {
206 size = g_variant_get_size(val);
209 uuid_value = (char **)g_variant_get_strv(val, &size);
210 if (dev_info->uuids == NULL)
211 dev_info->uuids = g_malloc0(sizeof(char *) * size);
213 for (i = 0; uuid_value[i] != NULL; i++) {
214 dev_info->uuid_count++;
215 dev_info->uuids[i] = g_strdup(uuid_value[i]);
219 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
220 g_variant_get(val, "q", &dev_info->manufacturer_data_len);
221 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
222 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
223 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
226 if (dev_info->manufacturer_data_len == 0)
227 dev_info->manufacturer_data = g_strdup("");
228 } else if (strcasecmp(key, "ManufacturerData") == 0) {
231 GVariantIter *value_iter;
234 len = g_variant_get_size(val);
238 dev_info->manufacturer_data = g_malloc0(len);
240 g_variant_get(val, "ay", &value_iter);
241 while (g_variant_iter_loop(value_iter, "y", &m_value))
242 dev_info->manufacturer_data[i++] = m_value;
243 g_variant_iter_free(value_iter);
251 static gboolean __bt_parse_interface(GVariant *msg,
252 bt_remote_dev_info_t *dev_info)
255 GVariant *optional_param = NULL;
258 char *interface_name = NULL;
259 GVariant *inner_iter = NULL;
260 g_variant_get(msg, "(&o@a{sa{sv}})",
261 &path, &optional_param);
262 g_variant_iter_init(&iter, optional_param);
264 retv_if(optional_param == NULL, FALSE);
266 while ((child = g_variant_iter_next_value(&iter))) {
267 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
268 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
269 BT_DBG("Found a device: %s", path);
270 if (__bt_parse_device_properties(inner_iter,
271 dev_info) == FALSE) {
272 g_variant_unref(inner_iter);
273 g_variant_unref(child);
274 g_variant_unref(optional_param);
275 BT_ERR("Fail to parse the properies");
278 g_variant_unref(inner_iter);
279 g_variant_unref(child);
280 g_variant_unref(optional_param);
284 g_variant_unref(inner_iter);
285 g_variant_unref(child);
288 g_variant_unref(optional_param);
293 static int __bt_get_owner_info(GVariant *msg, char **name,
294 char **previous, char **current)
296 g_variant_get(msg, "(sss)", name, previous, current);
297 return BLUETOOTH_ERROR_NONE;
300 static int __bt_get_agent_signal_info(GVariant *msg, char **address,
301 char **name, char **uuid)
303 g_variant_get(msg, "(sss)", address, name, uuid);
304 return BLUETOOTH_ERROR_NONE;
307 void __bt_set_device_values(gboolean connected, int state)
309 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
311 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0) {
312 BT_ERR("vconf_get_int failed");
315 if (connected == TRUE)
316 bt_device_state |= state;
317 else if (bt_device_state & state)
318 bt_device_state ^= state;
320 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
321 BT_ERR("vconf_set_int failed");
325 gboolean _bt_discovery_finished_cb(gpointer user_data)
327 int result = BLUETOOTH_ERROR_NONE;
329 GVariant *param = NULL;
330 if (_bt_get_discovering_property(DISCOVERY_ROLE_BREDR) == FALSE) {
331 if (_bt_get_cancel_by_user() == TRUE) {
332 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
335 _bt_set_cancel_by_user(FALSE);
336 _bt_set_discovery_status(FALSE);
337 param = g_variant_new("(i)", result);
338 _bt_send_event(BT_ADAPTER_EVENT,
339 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
346 static gboolean __bt_le_discovery_finished_cb(gpointer user_data)
348 int result = BLUETOOTH_ERROR_NONE;
350 GVariant *param = NULL;
351 if (_bt_get_discovering_property(DISCOVERY_ROLE_LE) == FALSE) {
352 if (_bt_get_cancel_by_user() == TRUE) {
353 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
356 g_list_free_full(p_adv_ind_list, __bt_free_bt_le_adv_info_t);
357 p_adv_ind_list = NULL;
359 _bt_set_cancel_by_user(FALSE);
360 _bt_disable_all_scanner_status();
361 _bt_set_le_scan_status(FALSE);
362 param = g_variant_new("(i)", result);
363 _bt_send_event(BT_LE_ADAPTER_EVENT,
364 BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED,
371 void __bt_update_remote_cache_devinfo(const char *address, gboolean paired_status)
375 ret_if(address == NULL);
378 bt_cache_info_t *cache_info;
379 bt_remote_dev_info_t *dev_info;
381 node = g_list_first(p_cache_list);
383 while (node != NULL) {
384 cache_info = (bt_cache_info_t *)node->data;
386 if (cache_info == NULL) {
387 node = g_list_next(node);
391 dev_info = cache_info->dev_info;
392 if (strcasecmp(dev_info->address,
394 BT_DBG("Device Found");
395 if (paired_status == TRUE)
396 cache_info->dev_info->paired = TRUE;
398 cache_info->dev_info->paired = FALSE;
401 node = g_list_next(node);
406 static void __bt_device_remote_connected_properties(
407 bt_remote_dev_info_t *remote_dev_info,
408 char *address, gboolean connected)
410 int result = BLUETOOTH_ERROR_NONE;
412 GVariant *param = NULL;
415 if (remote_dev_info->uuid_count > 0) {
416 for (i = 0; i < remote_dev_info->uuid_count; i++) {
417 char *uuid = remote_dev_info->uuids[i];
418 if (strcasecmp(uuid, HID_UUID) == 0) {
419 int event = BLUETOOTH_EVENT_NONE;
421 event = (connected == TRUE) ?
422 BLUETOOTH_HID_CONNECTED :
423 BLUETOOTH_HID_DISCONNECTED;
424 param = g_variant_new("(is)", result,
426 _bt_send_event(BT_HID_EVENT, event,
436 void _bt_handle_adapter_event(GVariant *msg, const char *member)
440 int result = BLUETOOTH_ERROR_NONE;
441 GVariant *param = NULL;
442 ret_if(member == NULL);
444 if (strcasecmp(member, "DeviceCreated") == 0) {
445 char *object_path = NULL;
447 bt_remote_dev_info_t *remote_dev_info;
449 ret_if(_bt_is_device_creating() == FALSE);
451 /* Bonding from remote device */
452 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
454 g_variant_get(msg, "(&o)", &object_path);
455 _bt_convert_device_path_to_address((const char*)object_path, address);
457 remote_dev_info = _bt_get_remote_device_info_by_object_path(object_path);
458 if (remote_dev_info == NULL) {
463 _bt_free_device_info(remote_dev_info);
465 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
466 char *object_path = NULL;
468 bt_cache_info_t *cache_info;
469 bt_remote_dev_info_t *dev_info;
472 /* Bonding from remote device */
473 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
477 g_variant_get(msg, "(&o)", &object_path);
479 _bt_convert_device_path_to_address((const char *)object_path, address);
481 node = g_list_first(p_cache_list);
483 while (node != NULL) {
484 cache_info = (bt_cache_info_t *)node->data;
486 if (cache_info == NULL) {
487 node = g_list_next(node);
491 dev_info = cache_info->dev_info;
492 if (strcasecmp(dev_info->address,
494 p_cache_list = g_list_remove(p_cache_list,
496 __bt_free_cache_info(cache_info);
499 node = g_list_next(node);
502 } else if (strcasecmp(member, "AdvertisingEnabled") == 0) {
503 BT_DBG("Advertising Enabled");
507 gboolean status = FALSE;
509 g_variant_get(msg, "(ib)", &slot_id, &status);
511 BT_DBG("Advertising Enabled : slot_id [%d] status [%d]", slot_id, status);
513 /* Send event to application */
514 _bt_set_advertising_status(slot_id, status);
516 adv_handle = _bt_get_adv_slot_adv_handle(slot_id);
519 event = BLUETOOTH_EVENT_ADVERTISING_STARTED;
521 event = BLUETOOTH_EVENT_ADVERTISING_STOPPED;
522 param = g_variant_new("(ii)", result,
527 sender = _bt_get_adv_slot_owner(slot_id);
528 _bt_send_event_to_dest(sender, BT_ADAPTER_EVENT,
532 _bt_send_event(BT_ADAPTER_EVENT, event, param);
535 if (event == BLUETOOTH_EVENT_ADVERTISING_STOPPED)
536 _bt_unregister_adv_slot_owner(slot_id);
537 } else if (strcasecmp(member, "RssiEnabled") == 0) {
538 BT_DBG("RSSI Enabled");
539 gboolean status = FALSE;
540 char *address = NULL;
542 g_variant_get(msg, "(sib)", &address, &link_type, &status);
544 BT_DBG("RSSI Enabled [%s %d]", address, status);
545 param = g_variant_new("(isib)", result,
546 address, link_type, status);
547 _bt_send_event(BT_DEVICE_EVENT,
548 BLUETOOTH_EVENT_RSSI_ENABLED,
551 } else if (strcasecmp(member, "RssiAlert") == 0) {
552 BT_DBG("RSSI Alert");
556 char *address = NULL;
557 g_variant_get(msg, "(siii)", &address, &link_type, &alert_type, &rssi_dbm);
559 BT_DBG("RSSI Alert: [Address %s LinkType %d] [Type %d DBM %d]",
560 address, alert_type, rssi_dbm);
561 param = g_variant_new("(isiii)", result,
562 address, link_type, alert_type, rssi_dbm);
563 _bt_send_event(BT_DEVICE_EVENT,
564 BLUETOOTH_EVENT_RSSI_ALERT,
567 } else if (strcasecmp(member, "RawRssi") == 0) {
571 char *address = NULL;
572 g_variant_get(msg, "(sii)", &address, &link_type, &rssi_dbm);
574 BT_DBG("Raw RSSI: [Address %s] [Link Type %d][RSSI DBM %d]",
575 address, link_type, rssi_dbm);
576 param = g_variant_new("(isii)", result,
577 address, link_type, rssi_dbm);
578 _bt_send_event(BT_DEVICE_EVENT,
579 BLUETOOTH_EVENT_RAW_RSSI,
582 } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
583 BT_ERR_C("Hardware error received from BLUEZ");
584 /* Don't add the recovery logic into platform */
586 _bt_recover_adapter();
588 } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
589 BT_ERR_C("Tx timeout error received from BLUEZ");
590 /* Don't add the recovery logic into platform */
592 _bt_recover_adapter();
598 static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
600 GDBusProxy *adapter_proxy;
602 int result = BLUETOOTH_ERROR_NONE;
603 GVariantIter value_iter;
604 GVariant *val = NULL;
605 GVariant *ret = NULL;
607 char *property = NULL;
608 GVariant *param = NULL;
609 g_variant_iter_init(&value_iter, msg);
610 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
611 BT_INFO("Property %s", property);
613 if (strcasecmp(property, "Discovering") == 0) {
614 gboolean discovering = FALSE;
615 g_variant_get(val, "b", &discovering);
616 /* Send event to application */
617 BT_DBG("Discovering %d", discovering);
618 if (discovering == TRUE) {
619 _bt_set_discovery_status(TRUE);
620 param = g_variant_new("(i)", result);
621 _bt_send_event(BT_ADAPTER_EVENT,
622 BLUETOOTH_EVENT_DISCOVERY_STARTED,
625 ret_if(event_id > 0);
627 adapter_proxy = _bt_get_adapter_proxy();
628 ret_if(adapter_proxy == NULL);
630 /* Need to stop searching */
631 ret = g_dbus_proxy_call_sync(adapter_proxy, "StopDiscovery",
633 G_DBUS_CALL_FLAGS_NONE,
637 BT_ERR("Dbus Error : %s", err->message);
641 g_variant_unref(ret);
643 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
644 (GSourceFunc)_bt_discovery_finished_cb, NULL);
646 } else if (strcasecmp(property, "LEDiscovering") == 0) {
647 gboolean le_discovering = FALSE;
649 g_variant_get(val, "b", &le_discovering);
650 /* Send event to application */
651 if (le_discovering == TRUE) {
652 _bt_set_le_scan_status(TRUE);
653 param = g_variant_new("(i)", result);
654 _bt_send_event(BT_LE_ADAPTER_EVENT,
655 BLUETOOTH_EVENT_LE_DISCOVERY_STARTED,
658 ret_if(event_id > 0);
660 adapter_proxy = _bt_get_adapter_proxy();
661 ret_if(adapter_proxy == NULL);
663 /* Need to stop searching */
664 ret = g_dbus_proxy_call_sync(adapter_proxy, "StopLEDiscovery",
666 G_DBUS_CALL_FLAGS_NONE,
670 BT_ERR("Dbus Error %s", err->message);
674 g_variant_unref(ret);
676 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
677 (GSourceFunc)__bt_le_discovery_finished_cb, NULL);
679 } else if (strcasecmp(property, "Name") == 0) {
681 g_variant_get(val, "s", &name);
682 param = g_variant_new("(is)", result, name);
683 /* Send event to application */
684 _bt_send_event(BT_ADAPTER_EVENT,
685 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
688 } else if (strcasecmp(property, "Alias") == 0) {
690 g_variant_get(val, "s", &alias);
691 param = g_variant_new("(is)", result, alias);
692 /* Send event to application */
693 _bt_send_event(BT_ADAPTER_EVENT,
694 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
697 } else if (strcasecmp(property, "Discoverable") == 0) {
698 gboolean discoverable = FALSE;
700 g_variant_get(val, "b", &discoverable);
701 BT_DBG("discoverable %d", discoverable);
703 if (discoverable == FALSE) {
704 if (_bt_get_discoverable_timeout_property() > 0) {
706 adapter_proxy = _bt_get_adapter_properties_proxy();
707 ret_if(adapter_proxy == NULL);
708 ret = g_dbus_proxy_call_sync(adapter_proxy, "Set",
709 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
710 "DiscoverableTimeout",
711 g_variant_new("i", time)),
712 G_DBUS_CALL_FLAGS_NONE,
717 BT_ERR("StopLEDiscovery Failed: %s", err->message);
721 g_variant_unref(ret);
724 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
726 /* Send event to application */
727 BT_INFO("[Connectable]");
728 param = g_variant_new("(in)", result, mode);
729 _bt_send_event(BT_ADAPTER_EVENT,
730 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
733 _bt_get_discoverable_mode(&mode);
735 /* Event will be sent by "DiscoverableTimeout" signal */
736 if (mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
738 g_variant_unref(val);
742 /* Send event to application */
743 BT_INFO("[General Discoverable]");
744 param = g_variant_new("(in)", result, mode);
745 _bt_send_event(BT_ADAPTER_EVENT,
746 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
749 } else if (strcasecmp(property, "DiscoverableTimeout") == 0) {
750 _bt_get_discoverable_mode(&mode);
752 /* Event was already sent by "Discoverable" signal */
753 if (mode == BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE ||
754 mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
756 g_variant_unref(val);
760 /* Send event to application */
761 BT_INFO("[Limited Discoverable (Timeout %u secs)]",
762 _bt_get_discoverable_timeout_property());
763 param = g_variant_new("(in)", result, mode);
764 _bt_send_event(BT_ADAPTER_EVENT,
765 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
767 } else if (strcasecmp(property, "Powered") == 0) {
768 /* TODO: Need to check this operation!! */
769 gboolean powered = FALSE;
772 g_variant_get(val, "b", &powered);
773 BT_DBG("Powered = %d", powered);
774 if (powered == FALSE) {
776 _bt_handle_adapter_removed();
778 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_state) == 0 &&
779 bt_state != VCONFKEY_BT_STATUS_OFF) {
780 _bt_disable_adapter();
783 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_state) == 0 &&
784 bt_state != VCONFKEY_BT_LE_STATUS_OFF) {
785 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
789 _bt_handle_adapter_added();
792 } else if (strcasecmp(property, "Connectable") == 0) {
793 gboolean connectable = FALSE;
795 g_variant_get(val, "b", &connectable);
797 BT_DBG("Connectable property is changed : %d", connectable);
798 param = g_variant_new("(b)", connectable);
799 _bt_send_event(BT_ADAPTER_EVENT,
800 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
803 if (_bt_adapter_get_status() == BT_DEACTIVATING &&
804 _bt_adapter_get_le_status() == BT_LE_ACTIVATED &&
806 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
808 } else if (strcasecmp(property, "SupportedLEFeatures") == 0) {
811 GVariantIter *iter = NULL;
812 g_variant_get(val, "as", &iter);
813 while (g_variant_iter_loop(iter, "s", &name)) {
814 BT_DBG("name = %s", name);
815 g_variant_iter_loop(iter, "s", &value);
816 BT_DBG("Value = %s", value);
817 if (FALSE == _bt_update_le_feature_support(name, value))
818 BT_INFO("Fail to update LE feature info");
821 g_variant_iter_free(iter);
822 } else if (strcasecmp(property, "IpspInitStateChanged") == 0) {
823 gboolean ipsp_initialized = FALSE;
825 g_variant_get(val, "b", &ipsp_initialized);
826 BT_DBG("IPSP init state changed: %d", ipsp_initialized);
827 param = g_variant_new("(b)", ipsp_initialized);
829 /* Send event to application */
830 _bt_send_event(BT_ADAPTER_EVENT,
831 BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,
834 BT_DBG("property : [%s]", property);
839 static void __bt_obex_property_changed_event(GVariant *msg, const char *path)
843 GVariantIter value_iter;
844 GVariant *child = NULL, *val = NULL;
845 char *property = NULL;
846 g_variant_iter_init(&value_iter, msg);
847 while ((child = g_variant_iter_next_value(&value_iter))) {
848 g_variant_get(child, "{sv}", &property, &val);
850 ret_if(property == NULL);
852 BT_DBG("property :%s", property);
854 if (strcasecmp(property, "Status") == 0) {
856 g_variant_get(val, "s", &status);
858 if (strcasecmp(status, "active") == 0) {
859 _bt_obex_transfer_started(path);
860 } else if (strcasecmp(status, "complete") == 0) {
861 _bt_obex_transfer_completed(path, TRUE);
862 _bt_pbap_obex_transfer_completed(path, TRUE);
863 } else if (strcasecmp(status, "error") == 0) {
864 _bt_obex_transfer_completed(path, FALSE);
865 _bt_pbap_obex_transfer_completed(path, FALSE);
868 } else if (strcasecmp(property, "Transferred") == 0) {
869 guint64 transferred = 0;
870 /* As Transferred is expected guint64 so change int to guint64 and
871 * eariler transferred is static because of it can overwrite data
872 * on present on opc_obex_conn or obexd_conn as these are
873 * static memory are in sequential */
874 g_variant_get(val, "t", &transferred);
876 _bt_obex_transfer_progress(path, transferred);
879 g_variant_unref(val);
880 g_variant_unref(child);
885 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
890 int result = BLUETOOTH_ERROR_NONE;
891 GVariantIter value_iter;
893 char *property = NULL;
895 GVariant *param = NULL;
896 bt_remote_dev_info_t *remote_dev_info;
897 g_variant_iter_init(&value_iter, msg);
898 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
899 BT_DBG("Property %s", property);
900 if (strcasecmp(property, "Connected") == 0) {
903 g_variant_get(val, "i", &connected);
905 event = (connected != BLUETOOTH_CONNECTED_LINK_NONE) ?
906 BLUETOOTH_EVENT_DEVICE_CONNECTED :
907 BLUETOOTH_EVENT_DEVICE_DISCONNECTED;
909 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
911 _bt_convert_device_path_to_address(path, address);
913 BT_DBG("connected: %d", connected);
914 BT_DBG("address: %s", address);
916 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
918 if (remote_dev_info != NULL) {
919 __bt_device_remote_connected_properties(
920 remote_dev_info, address,
921 connected != BLUETOOTH_CONNECTED_LINK_NONE ?
923 _bt_free_device_info(remote_dev_info);
925 param = g_variant_new("(is)", result, address);
926 /* Send event to application */
927 _bt_send_event(BT_DEVICE_EVENT,
931 } else if (strcasecmp(property, "RSSI") == 0) {
932 bt_remote_dev_info_t *remote_dev_info;
934 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
936 _bt_convert_device_path_to_address(path, address);
937 BT_DBG("address: %s", address);
939 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
940 if (remote_dev_info == NULL) {
942 g_variant_unref(val);
946 BT_DBG("Address type %d", remote_dev_info->addr_type);
948 if (remote_dev_info->addr_type == 0) {
949 BT_DBG("Name %s", remote_dev_info->name);
951 #ifdef TIZEN_DPM_ENABLE
952 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
954 bluetooth_device_class_t device_class;
955 _bt_divide_device_class(&device_class, remote_dev_info->class);
957 if (device_class.major_class ==
958 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
959 _bt_free_device_info(remote_dev_info);
961 g_variant_unref(val);
968 GVariant *uuids = NULL;
969 GVariantBuilder *builder = NULL;
971 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
972 for (i = 0; i < remote_dev_info->uuid_count; i++) {
973 g_variant_builder_add(builder, "s",
974 remote_dev_info->uuids[i]);
976 uuids = g_variant_new("as", builder);
977 g_variant_builder_unref(builder);
978 GVariant *manufacturer_data = NULL;
979 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
980 remote_dev_info->manufacturer_data,
981 remote_dev_info->manufacturer_data_len,
984 param = g_variant_new("(isunsbub@asn@ay)", result,
985 remote_dev_info->address,
986 remote_dev_info->class,
987 remote_dev_info->rssi,
988 remote_dev_info->name,
989 remote_dev_info->paired,
990 remote_dev_info->connected,
991 remote_dev_info->trust,
993 remote_dev_info->manufacturer_data_len,
996 _bt_send_event(BT_ADAPTER_EVENT,
997 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1001 _bt_free_device_info(remote_dev_info);
1002 } else if (strcasecmp(property, "GattConnected") == 0) {
1003 gboolean gatt_connected = FALSE;
1005 g_variant_get(val, "b", &gatt_connected);
1007 event = gatt_connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1008 BLUETOOTH_EVENT_GATT_DISCONNECTED;
1010 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1012 _bt_convert_device_path_to_address(path, address);
1014 BT_DBG("gatt_connected: %d", gatt_connected);
1015 BT_DBG("address: %s", address);
1016 param = g_variant_new("(is)", result, address);
1017 /* Send event to application */
1018 _bt_send_event(BT_DEVICE_EVENT,
1022 } else if (strcasecmp(property, "Paired") == 0) {
1023 gboolean paired = FALSE;
1024 bt_remote_dev_info_t *remote_dev_info;
1025 g_variant_get(val, "b", &paired);
1026 _bt_agent_set_canceled(FALSE);
1027 /* BlueZ sends paired signal for each paired device */
1028 /* during activation, We should ignore this, otherwise*/
1029 /* application thinks that a new device got paired */
1030 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1031 BT_DBG("BT is not activated, so ignore this");
1033 g_variant_unref(val);
1037 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1039 _bt_convert_device_path_to_address(path, address);
1041 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1042 if (remote_dev_info == NULL) {
1044 g_variant_unref(val);
1049 if (paired == FALSE) {
1050 BT_INFO("Unpaired: %s", address);
1051 __bt_update_remote_cache_devinfo(address, FALSE);
1052 param = g_variant_new("(is)", result, address);
1053 _bt_send_event(BT_ADAPTER_EVENT,
1054 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
1056 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1058 char *last_connected = NULL;
1059 last_connected = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1060 if (!g_strcmp0(address, last_connected))
1061 _bt_audio_set_auto_connect_device_addr("");
1063 free(last_connected);
1067 char secure_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
1069 _bt_convert_addr_string_to_secure_string(secure_addr, address);
1070 BT_INFO("### Paired: %s", secure_addr);
1071 __bt_update_remote_cache_devinfo(address, TRUE);
1073 if (_bt_is_device_creating() == TRUE) {
1074 BT_DBG("Try to Pair by me");
1075 _bt_free_device_info(remote_dev_info);
1078 g_variant_unref(val);
1081 GVariant *uuids = NULL;
1082 GVariantBuilder *builder = NULL;
1084 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1085 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1086 g_variant_builder_add(builder, "s",
1087 remote_dev_info->uuids[i]);
1089 uuids = g_variant_new("as", builder);
1090 g_variant_builder_unref(builder);
1091 GVariant *manufacturer_data = NULL;
1092 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1093 remote_dev_info->manufacturer_data,
1094 remote_dev_info->manufacturer_data_len,
1098 param = g_variant_new("(isunsbub@asn@ay)", result,
1099 address, remote_dev_info->class,
1100 remote_dev_info->rssi,
1101 remote_dev_info->name,
1102 remote_dev_info->paired,
1103 remote_dev_info->connected,
1104 remote_dev_info->trust,
1106 remote_dev_info->manufacturer_data_len,
1108 _bt_send_event(BT_ADAPTER_EVENT,
1109 BLUETOOTH_EVENT_BONDING_FINISHED,
1112 _bt_free_device_info(remote_dev_info);
1114 } else if (strcasecmp(property, "LegacyPaired") == 0) {
1115 gboolean paired = FALSE;
1116 bt_remote_dev_info_t *remote_dev_info;
1117 unsigned char auth_info[5] = {0, };
1119 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1120 BT_DBG("BT is not activated, so ignore this");
1122 g_variant_unref(val);
1126 g_variant_get(val, "b", &paired);
1127 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1128 BT_DBG("LegacyPaired: %d", paired);
1129 _bt_convert_device_path_to_address(path, address);
1131 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1132 if (remote_dev_info == NULL) {
1135 g_variant_unref(val);
1138 if (remote_dev_info->is_alias_set == FALSE) {
1139 /*minimum Size of the samsung specific manufacturer data is greater than 30 */
1140 if ((remote_dev_info->manufacturer_data_len > 30) &&
1141 (remote_dev_info->manufacturer_data[0] == 0x00) &&
1142 (remote_dev_info->manufacturer_data[1] == 0x75)) {
1144 /* 2 samsung (0x00 0x75) + 1 (control and version) + 1 (service ID) +
1145 1 (discovery version) + 1 (associated service ID)
1146 2 (Proxamity and locality) + 2 (Device type and icon) = 10 */
1148 memcpy(auth_info, &(remote_dev_info->manufacturer_data[10]), 5);
1152 BT_DBG("LegacyPairing Failed with %s. Show Error Popup", remote_dev_info->name);
1153 _bt_launch_system_popup(BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE,
1154 remote_dev_info->name, auth_info, NULL, NULL, NULL);
1156 _bt_free_device_info(remote_dev_info);
1158 } else if (strcasecmp(property, "Trusted") == 0) {
1159 gboolean trusted = FALSE;
1161 g_variant_get(val, "b", &trusted);
1163 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1164 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1166 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1168 _bt_convert_device_path_to_address(path, address);
1170 BT_DBG("trusted: %d", trusted);
1171 BT_DBG("address: %s", address);
1172 param = g_variant_new("(is)", result, address);
1173 /* Send event to application */
1174 _bt_send_event(BT_DEVICE_EVENT,
1178 } else if (strcasecmp(property, "TrustedProfiles") == 0) {
1181 g_variant_get(val, "u", &trusted);
1183 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1184 _bt_convert_device_path_to_address(path, address);
1186 BT_DBG("Address: %s, TrustedProfiles: %d", address, trusted);
1187 _bt_send_event(BT_DEVICE_EVENT,
1188 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1189 g_variant_new("(isi)", result, address, trusted));
1191 } else if (strcasecmp(property, "IpspConnected") == 0) {
1192 gboolean connected = FALSE;
1194 g_variant_get(val, "b", &connected);
1197 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
1198 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
1200 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1202 _bt_convert_device_path_to_address(path, address);
1204 BT_DBG("Ipspconnected: %d", connected);
1205 BT_DBG("address: %s", address);
1206 param = g_variant_new("(is)", result, address);
1208 /* Send event to application */
1209 _bt_send_event(BT_DEVICE_EVENT,
1213 } else if (strcasecmp(property, "IpspBtInterfaceInfo") == 0) {
1214 char *ifname = NULL;
1216 g_variant_get(val, "s", &ifname);
1218 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1220 _bt_convert_device_path_to_address(path, address);
1222 BT_DBG("Ipsp BT Interface Name: %s", ifname);
1223 BT_DBG("address: %s", address);
1224 param = g_variant_new("(iss)", result, address, ifname);
1226 /* Send event to application */
1227 _bt_send_event(BT_DEVICE_EVENT,
1228 BLUETOOTH_EVENT_IPSP_INTERFACE_INFO,
1236 static void __bt_media_control_changed_event(GVariant *msg, const char *path)
1239 int result = BLUETOOTH_ERROR_NONE;
1240 GVariantIter value_iter;
1241 char *property = NULL;
1243 GVariant *val = NULL;
1244 GVariant *child = NULL;
1245 bt_remote_dev_info_t *remote_dev_info;
1246 GVariant *param = NULL;
1247 g_variant_iter_init(&value_iter, msg);
1248 while ((child = g_variant_iter_next_value(&value_iter))) {
1249 g_variant_get(child, "{sv}", &property, &val);
1250 BT_INFO("Property %s", property);
1251 if (strcasecmp(property, "Connected") == 0) {
1252 gboolean connected = FALSE;
1254 g_variant_get(val, "b", &connected);
1256 event = connected ? BLUETOOTH_EVENT_AVRCP_CONNECTED :
1257 BLUETOOTH_EVENT_AVRCP_DISCONNECTED;
1259 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1261 _bt_convert_device_path_to_address(path, address);
1263 BT_DBG("connected: %d", connected);
1264 BT_DBG("address: %s", address);
1266 remote_dev_info = _bt_get_remote_device_info(address);
1268 if (remote_dev_info != NULL) {
1269 __bt_device_remote_connected_properties(
1270 remote_dev_info, address, connected);
1271 _bt_free_device_info(remote_dev_info);
1273 param = g_variant_new("(is)", result, address);
1274 /* Send event to application */
1275 _bt_send_event(BT_AVRCP_EVENT,
1281 g_variant_unref(child);
1282 g_variant_unref(val);
1287 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
1289 char *interface_name = NULL;
1290 GVariant *val = NULL;
1292 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &val, NULL);
1294 if (strcasecmp(interface_name, BT_ADAPTER_INTERFACE) == 0) {
1295 __bt_adapter_property_changed_event(val,
1297 } else if (strcasecmp(interface_name, BT_DEVICE_INTERFACE) == 0) {
1298 __bt_device_property_changed_event(val, object_path);
1299 } else if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1300 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
1301 __bt_obex_property_changed_event(val,
1303 } else if (strcasecmp(interface_name, BT_MEDIA_CONTROL_INTERFACE) == 0) {
1304 __bt_media_control_changed_event(val,
1306 } else if (strcasecmp(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
1307 _bt_handle_avrcp_control_event(val,
1309 } else if (strcasecmp(interface_name, BT_NETWORK_CLIENT_INTERFACE) == 0) {
1310 BT_DBG("BT_NETWORK_CLIENT_INTERFACE");
1311 _bt_handle_network_client_event(val,
1313 } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
1314 __bt_gatt_char_property_changed_event(val,
1317 g_variant_unref(val);
1320 void __bt_opc_property_changed_event(GVariant *msg,
1323 GVariantIter value_iter;
1324 char *property = NULL;
1325 GVariant *val = NULL;
1326 GVariant *child = NULL;
1328 g_variant_iter_init(&value_iter, msg);
1329 while ((child = g_variant_iter_next_value(&value_iter))) {
1330 g_variant_get(child, "{sv}", &property, &val);
1331 ret_if(property == NULL);
1333 if (strcasecmp(property, "Status") == 0) {
1334 char *status = NULL;
1335 g_variant_get(val, "s", &status);
1336 BT_DBG("Status is %s", status);
1338 if (strcasecmp(status, "active") == 0) {
1339 _bt_obex_client_started(path);
1340 } else if (strcasecmp(status, "complete") == 0) {
1341 _bt_obex_client_completed(path, TRUE);
1342 } else if (strcasecmp(status, "error") == 0) {
1343 _bt_obex_client_completed(path, FALSE);
1346 } else if (strcasecmp(property, "Transferred") == 0) {
1347 guint64 transferred = 0;
1348 g_variant_get(val, "t", &transferred);
1350 _bt_obex_client_progress(path, transferred);
1352 BT_DBG("property : [%s]", property);
1355 g_variant_unref(child);
1356 g_variant_unref(val);
1360 void _bt_opc_property_changed_event(GVariant *msg, char *path)
1362 char *interface_name = NULL;
1363 GVariant *value = NULL;
1364 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1365 BT_INFO("interface_name = %s", interface_name);
1366 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1367 __bt_opc_property_changed_event(value,
1370 BT_DBG("interface_name : [%s]", interface_name);
1372 g_variant_unref(value);
1376 void _bt_handle_input_event(GVariant *msg, const char *path)
1378 int result = BLUETOOTH_ERROR_NONE;
1379 gboolean property_flag = FALSE;
1380 GVariantIter value_iter;
1381 char *property = NULL;
1382 GVariant *child = NULL, *val = NULL;
1383 bt_remote_dev_info_t *remote_dev_info;
1384 GVariant *param = NULL;
1385 g_variant_iter_init(&value_iter, msg);
1386 while ((child = g_variant_iter_next_value(&value_iter))) {
1387 g_variant_get(child, "{sv}", &property, &val);
1389 ret_if(property == NULL);
1391 if (strcasecmp(property, "Connected") == 0) {
1392 int event = BLUETOOTH_EVENT_NONE;
1394 g_variant_get(val, "b", &property_flag);
1396 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1398 _bt_convert_device_path_to_address(path, address);
1400 event = (property_flag == TRUE) ?
1401 BLUETOOTH_HID_CONNECTED :
1402 BLUETOOTH_HID_DISCONNECTED;
1403 param = g_variant_new("(is)", result, address);
1404 _bt_send_event(BT_HID_EVENT, event,
1406 /* Check HID connection type (Keyboard or Mouse) and update the status */
1407 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1409 if (property_flag == TRUE) {
1410 hid_connected_device_count++;
1411 __bt_set_device_values(TRUE,
1412 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1414 hid_connected_device_count--;
1415 if (hid_connected_device_count == 0)
1416 __bt_set_device_values(FALSE,
1417 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1420 if (remote_dev_info != NULL) {
1421 BT_DBG("HID device class [%x]", remote_dev_info->class);
1422 if (remote_dev_info->class &
1423 BLUETOOTH_DEVICE_MINOR_CLASS_KEY_BOARD) {
1424 __bt_set_device_values(property_flag,
1425 VCONFKEY_BT_DEVICE_HID_KEYBOARD_CONNECTED);
1429 if (remote_dev_info->class &
1430 BLUETOOTH_DEVICE_MINOR_CLASS_POINTING_DEVICE) {
1431 __bt_set_device_values(property_flag,
1432 VCONFKEY_BT_DEVICE_HID_MOUSE_CONNECTED);
1434 _bt_free_device_info(remote_dev_info);
1439 g_variant_unref(val);
1440 g_variant_unref(child);
1444 void _bt_handle_network_server_event(GVariant *msg, const char *member)
1446 int result = BLUETOOTH_ERROR_NONE;
1447 char *address = NULL;
1448 char *device = NULL;
1449 GVariant *param = NULL;
1450 ret_if(member == NULL);
1451 if (strcasecmp(member, "PeerConnected") == 0) {
1452 g_variant_get(msg, "(ss)", &device, &address);
1454 __bt_set_device_values(TRUE,
1455 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1456 param = g_variant_new("(iss)", result, device, address);
1457 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
1461 nap_connected_device_count++;
1462 } else if (strcasecmp(member, "PeerDisconnected") == 0) {
1463 g_variant_get(msg, "(ss)", &device, &address);
1464 nap_connected_device_count--;
1465 if (nap_connected_device_count == 0)
1466 __bt_set_device_values(FALSE,
1467 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1468 param = g_variant_new("(iss)", result, device, address);
1469 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
1476 void _bt_handle_network_client_event(GVariant *msg,
1481 int result = BLUETOOTH_ERROR_NONE;
1482 gboolean property_flag = FALSE;
1483 char *property = NULL;
1484 GVariant *val = NULL;
1485 GVariantIter value_iter;
1486 GVariant *param = NULL;
1487 g_variant_iter_init(&value_iter, msg);
1488 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1489 if (strcasecmp(property, "Connected") == 0) {
1490 int event = BLUETOOTH_EVENT_NONE;
1493 g_variant_get(val, "b", &property_flag);
1494 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1496 _bt_convert_device_path_to_address(path, address);
1498 BT_DBG("property_flag %d", property_flag);
1499 if (property_flag == TRUE) {
1500 event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
1501 nap_connected_device_count++;
1502 __bt_set_device_values(TRUE,
1503 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1505 event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
1506 nap_connected_device_count--;
1507 if (nap_connected_device_count == 0)
1508 __bt_set_device_values(FALSE,
1509 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1511 param = g_variant_new("(is)", result, address);
1512 _bt_send_event(BT_NETWORK_EVENT, event,
1521 void __bt_gatt_char_property_changed_event(GVariant *msg,
1524 GVariantIter value_iter;
1525 char *property = NULL;
1526 char * char_handle = NULL;
1527 GVariant *val = NULL;
1528 int result = BLUETOOTH_ERROR_NONE;
1529 GVariant *param = NULL;
1530 g_variant_iter_init(&value_iter, msg);
1531 char_handle = g_strdup(path);
1532 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1533 BT_INFO("Property %s", property);
1535 ret_if(property == NULL);
1537 if (strcasecmp(property, "Notifying") == 0) {
1538 gboolean property_flag = FALSE;
1539 g_variant_get(val, "b", &property_flag);
1540 if (property_flag == TRUE)
1541 BT_DBG("notifying is enabled");
1543 BT_DBG("notifying is disabled");
1544 } else if (strcasecmp(property, "ChangedValue") == 0) {
1546 GByteArray *gp_byte_array = NULL;
1547 BT_INFO("Type '%s'\n", g_variant_get_type_string(val));
1550 gp_byte_array = g_byte_array_new();
1551 len = g_variant_get_size(val);
1552 BT_DBG("Len = %d", len);
1553 g_byte_array_append(gp_byte_array,
1554 (const guint8 *) g_variant_get_data(val), len);
1555 if (gp_byte_array->len != 0) {
1556 GVariant *byte_array = NULL;
1557 byte_array = g_variant_new_from_data(
1558 G_VARIANT_TYPE_BYTESTRING,
1559 gp_byte_array->data,
1562 param = g_variant_new("(is@ay)", result, char_handle,
1565 /* Send event only registered client */
1566 _bt_send_char_value_changed_event(param);
1568 g_byte_array_free(gp_byte_array, TRUE);
1572 g_free(char_handle);
1575 void _bt_handle_gatt_event(GVariant *msg, const char *member, const char *path)
1577 ret_if(path == NULL);
1579 if (strcasecmp(member, "GattValueChanged") == 0) {
1582 /*** Debug only ***/
1583 GVariant *value = NULL;
1585 char *buffer = NULL;
1587 g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
1588 value_len = g_variant_get_size(value);
1589 if (value_len > 0) {
1590 char buf[8 * 5 + 1] = { 0 };
1593 buffer = (char *)g_variant_get_data(value);
1594 to = value_len > (sizeof(buf) / 5) ? sizeof(buf) / 5 : value_len;
1596 for (i = 0; i < to; i++)
1597 snprintf(&buf[i * 5], 6, "0x%02x ", buffer[i]);
1599 BT_DBG("GATT Val[%d] %s", value_len, buf);
1601 g_variant_unref(value);
1605 /* Send event only registered client */
1606 _bt_send_char_value_changed_event(msg);
1611 void _bt_handle_device_event(GVariant *msg, const char *member, const char *path)
1614 int result = BLUETOOTH_ERROR_NONE;
1616 char *dev_name = NULL;
1617 const char *property = NULL;
1618 GVariant *param = NULL;
1619 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1620 ret_if(path == NULL);
1622 if (strcasecmp(member, "PropertyChanged") == 0) {
1624 g_variant_get(msg, "(s)", &property);
1626 ret_if(property == NULL);
1628 if (strcasecmp(property, "GattConnected") == 0) {
1629 gboolean connected = FALSE;
1631 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1632 ret_if(address == NULL);
1634 _bt_convert_device_path_to_address(path, address);
1635 g_variant_get(msg, "(b)", &connected);
1637 event = connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1638 BLUETOOTH_EVENT_GATT_DISCONNECTED;
1639 param = g_variant_new("(is)", result, address);
1640 _bt_send_event(BT_DEVICE_EVENT,
1644 } else if (strcasecmp(property, "Paired") == 0) {
1645 gboolean paired = FALSE;
1646 bt_remote_dev_info_t *remote_dev_info;
1647 g_variant_get(msg, "(b)", &paired);
1649 ret_if(paired == FALSE);
1651 /* BlueZ sends paired signal for each paired device */
1652 /* during activation, We should ignore this, otherwise*/
1653 /* application thinks that a new device got paired */
1654 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1655 BT_DBG("BT is not activated, so ignore this");
1659 if (_bt_is_device_creating() == TRUE) {
1660 BT_DBG("Try to Pair by me");
1664 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1665 ret_if(address == NULL);
1667 _bt_convert_device_path_to_address(path, address);
1669 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1670 if (remote_dev_info == NULL) {
1674 GVariant *uuids = NULL;
1675 GVariantBuilder *builder = NULL;
1677 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1678 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1679 g_variant_builder_add(builder, "s",
1680 remote_dev_info->uuids[i]);
1682 uuids = g_variant_new("as", builder);
1683 g_variant_builder_unref(builder);
1684 GVariant *manufacturer_data = NULL;
1685 manufacturer_data = g_variant_new_from_data(
1686 G_VARIANT_TYPE_BYTESTRING,
1687 remote_dev_info->manufacturer_data,
1688 remote_dev_info->manufacturer_data_len,
1690 param = g_variant_new("(isunsbub@asn@ay)", result,
1692 remote_dev_info->class,
1693 remote_dev_info->rssi,
1694 remote_dev_info->name,
1695 remote_dev_info->paired,
1696 remote_dev_info->connected,
1697 remote_dev_info->trust,
1699 remote_dev_info->manufacturer_data_len,
1701 _bt_send_event(BT_ADAPTER_EVENT,
1702 BLUETOOTH_EVENT_BONDING_FINISHED,
1704 _bt_free_device_info(remote_dev_info);
1707 } else if (strcasecmp(property, "UUIDs") == 0) {
1708 /* Once we get the updated uuid information after
1709 * reverse service search, update it to application */
1711 bt_remote_dev_info_t *remote_dev_info;
1713 ret_if(_bt_is_device_creating() == TRUE);
1715 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1716 ret_if(address == NULL);
1718 _bt_convert_device_path_to_address(path, address);
1720 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1721 if (remote_dev_info == NULL) {
1726 BT_DBG("UUID's count = %d", remote_dev_info->uuid_count);
1727 if (remote_dev_info->paired && remote_dev_info->uuid_count) {
1728 GVariant *uuids = NULL;
1729 GVariantBuilder *builder = NULL;
1731 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1732 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1733 g_variant_builder_add(builder, "s",
1734 remote_dev_info->uuids[i]);
1736 uuids = g_variant_new("as", builder);
1737 g_variant_builder_unref(builder);
1738 GVariant *manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1739 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1742 param = g_variant_new("(isunsbub@asn@ay)", result,
1743 address, remote_dev_info->class,
1744 remote_dev_info->rssi,
1745 remote_dev_info->name,
1746 remote_dev_info->paired,
1747 remote_dev_info->connected,
1748 remote_dev_info->trust,
1750 remote_dev_info->manufacturer_data_len,
1752 _bt_send_event(BT_ADAPTER_EVENT,
1753 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1757 _bt_free_device_info(remote_dev_info);
1760 } else if (strcasecmp(member, "DeviceConnected") == 0) {
1761 unsigned char addr_type = 0;
1763 g_variant_get(msg, "(y)", &addr_type);
1765 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1766 ret_if(address == NULL);
1768 _bt_convert_device_path_to_address(path, address);
1769 dev_name = _bt_get_bonded_device_name(address);
1771 _bt_convert_addr_string_to_secure_string(secure_address, address);
1772 BT_INFO("Address : %s Type : %d", secure_address, addr_type);
1773 BT_ERR_C("### Connected [%s] [%s]", !addr_type ? "BREDR" : "LE",
1774 !addr_type ? dev_name : secure_address);
1777 _bt_logging_connection(TRUE, addr_type);
1778 param = g_variant_new("(isy)", result, address, addr_type);
1779 /*Send event to application*/
1780 _bt_send_event(BT_DEVICE_EVENT,
1781 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1784 } else if (strcasecmp(member, "Disconnected") == 0) {
1785 unsigned char disc_reason = 0;
1786 unsigned char addr_type = 0;
1787 char *dev_name = NULL;
1788 gboolean sending = FALSE;
1790 g_variant_get(msg, "(yys)", &addr_type, &disc_reason, &dev_name);
1792 result = disc_reason;
1794 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1795 ret_if(address == NULL);
1797 _bt_convert_device_path_to_address(path, address);
1799 /* 0x00 BDADDR_BRDER
1800 0x01 BDADDR_LE_PUBLIC
1801 0x02 BDADDR_LE_RANDOM */
1802 _bt_convert_addr_string_to_secure_string(secure_address, address);
1803 BT_INFO("Address : %s Type : %d", secure_address, addr_type);
1804 BT_ERR_C("### Disconnected [%s] [%d : %s] [%s]", !addr_type ? "BREDR" : "LE",
1805 disc_reason, _bt_convert_disc_reason_to_string(disc_reason),
1806 !addr_type ? dev_name : secure_address);
1809 _bt_headset_set_local_connection(FALSE);
1810 _bt_logging_connection(FALSE, addr_type);
1813 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1815 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1817 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
1818 BT_ERR("vconf_get_int failed");
1820 BT_INFO("conn_state[0x%x], adapter_state [%d]",
1821 bt_device_state, _bt_adapter_get_status());
1823 if (disc_reason == BLUETOOTH_ERROR_CONNECTION_TIMEOUT) {
1824 _bt_audio_start_auto_connect(TRUE);
1825 } else if (bt_device_state &
1826 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED) {
1827 BT_INFO("Disconnected due to turning BT off. Skip a address");
1829 char *last_connected = NULL;
1830 last_connected = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1831 if (!g_strcmp0(address, last_connected))
1832 _bt_audio_set_auto_connect_device_addr("");
1834 free(last_connected);
1839 /*Check for any OPP transfer on the device and cancel
1842 _bt_obex_check_pending_transfer(address);
1843 _bt_opp_client_is_sending(&sending);
1845 _bt_opp_client_check_pending_transfer(address);
1847 param = g_variant_new("(isy)", result, address, addr_type);
1848 _bt_send_event(BT_DEVICE_EVENT,
1849 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1852 } else if (strcasecmp(member, "ProfileStateChanged") == 0) {
1854 char *profile_uuid = NULL;
1855 bt_headset_wait_t *wait_list;
1856 bluetooth_device_address_t bd_addr;
1858 g_variant_get(msg, "(si)", &profile_uuid, &state);
1860 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1861 ret_if(address == NULL);
1863 _bt_convert_device_path_to_address(path, address);
1864 _bt_convert_addr_string_to_type(bd_addr.addr, address);
1866 _bt_convert_addr_string_to_secure_string(secure_address, address);
1867 BT_DBG("Address: %s", secure_address);
1868 BT_DBG("Profile UUID: %s", profile_uuid);
1869 BT_DBG("State: %d", state);
1871 if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1872 (state == BT_PROFILE_STATE_CONNECTED)) {
1874 int event = BLUETOOTH_EVENT_AV_CONNECTED;
1875 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1876 bluetooth_device_address_t device_address;
1878 bt_headset_wait_t *wait_list;
1879 guint restricted = 0x0;
1881 __bt_set_device_values(TRUE,
1882 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1884 __bt_connection_manager_set_state(address, event);
1886 _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_HFP_HS, &restricted);
1888 if (_bt_headset_get_local_connection() == FALSE) {
1889 if (restricted == 0x0) /* not restricted*/
1890 _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
1892 /* Connection Started from local device therefore no need to
1893 * intiate connection for pending profile */
1894 _bt_headset_set_local_connection(FALSE);
1896 param = g_variant_new("(is)", result, address);
1897 _bt_send_event(BT_HEADSET_EVENT, event,
1899 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
1902 if (g_strcmp0(connected_address, address) != 0) {
1903 _bt_convert_addr_string_to_type(
1904 device_address.addr,
1906 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
1907 &device_address, NULL);
1911 _bt_add_headset_to_list(BT_AUDIO_A2DP,
1912 BT_STATE_CONNECTED, address);
1914 wait_list = _bt_get_audio_wait_data();
1915 if (wait_list != NULL &&
1916 (g_strcmp0(wait_list->address, address) == 0))
1917 _bt_rel_wait_data();
1919 } else if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1920 (state == BT_PROFILE_STATE_DISCONNECTED)) {
1922 int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
1924 if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
1926 g_free(profile_uuid);
1930 __bt_set_device_values(FALSE,
1931 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1933 __bt_connection_manager_set_state(address, event);
1934 param = g_variant_new("(is)", result, address);
1935 _bt_send_event(BT_HEADSET_EVENT, event,
1937 /* Remove data from the connected list */
1938 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
1939 wait_list = _bt_get_audio_wait_data();
1941 if (wait_list == NULL) {
1943 g_free(profile_uuid);
1947 if (((wait_list->type == BT_AUDIO_ALL) &&
1948 (wait_list->ag_flag == TRUE)) ||
1949 (wait_list->type == BT_AUDIO_A2DP) ||
1950 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
1951 bluetooth_device_address_t device_address;
1952 _bt_convert_addr_string_to_type(
1953 device_address.addr,
1954 wait_list->address);
1956 _bt_audio_connect(wait_list->req_id,
1959 wait_list->out_param1);
1961 } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
1963 if (state == BT_PROFILE_STATE_CONNECTED) {
1964 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
1965 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1966 bluetooth_device_address_t device_address;
1968 param = g_variant_new("(is)", result, address);
1969 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1971 connected = _bt_is_headset_type_connected(
1975 if (g_strcmp0(connected_address,
1977 _bt_convert_addr_string_to_type(
1978 device_address.addr,
1980 _bt_audio_disconnect(0,
1982 &device_address, NULL);
1985 BT_DBG("device Path: %s", path);
1986 _bt_add_headset_to_list(BT_AVRCP,
1987 BT_STATE_CONNECTED, address);
1988 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1989 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
1990 param = g_variant_new("(is)", result, address);
1991 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1993 /* Remove data from the connected list */
1994 _bt_remove_headset_from_list(BT_AVRCP, address);
1996 } else if (strcasecmp(profile_uuid, A2DP_SOURCE_UUID) == 0) {
1997 if (state == BT_PROFILE_STATE_CONNECTED) {
1998 int event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED;
1999 BT_INFO("A2DP Source is connected");
2000 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
2001 __bt_set_device_values(TRUE,
2002 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED);
2005 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
2006 _bt_audio_set_auto_connect_device_addr(address);
2007 _bt_audio_stop_auto_connect();
2009 _bt_add_headset_to_list(BT_AUDIO_A2DP_SOURCE,
2010 BT_STATE_CONNECTED, address);
2011 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
2012 g_variant_new("(is)", result, address));
2013 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2014 int event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED;
2015 BT_INFO("A2DP Source Disconnected");
2016 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
2017 __bt_set_device_values(FALSE,
2018 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED);
2020 _bt_remove_headset_from_list(BT_AUDIO_A2DP_SOURCE, address);
2021 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
2022 g_variant_new("(is)", result, address));
2024 wait_list = _bt_get_audio_wait_data();
2025 if (wait_list && wait_list->type == BT_AUDIO_A2DP_SOURCE) {
2026 bluetooth_device_address_t device_address;
2027 _bt_convert_addr_string_to_type(
2028 device_address.addr,
2029 wait_list->address);
2031 _bt_audio_connect(wait_list->req_id,
2034 wait_list->out_param1);
2035 /* Now free the wait list */
2036 _bt_rel_wait_data();
2039 } else if ((strcmp(profile_uuid, HID_UUID) == 0) &&
2040 ((state == BT_PROFILE_STATE_CONNECTED) ||
2041 (state == BT_PROFILE_STATE_DISCONNECTED))) {
2043 if (state == BT_PROFILE_STATE_CONNECTED)
2044 event = BLUETOOTH_HID_CONNECTED;
2046 event = BLUETOOTH_HID_DISCONNECTED;
2047 param = g_variant_new("(is)", result, address);
2048 _bt_send_event(BT_HID_EVENT, event,
2050 } else if (strcmp(profile_uuid, HID_DEVICE_UUID) == 0) {
2051 if (state == BT_PROFILE_STATE_CONNECTED) {
2053 event = BLUETOOTH_HID_DEVICE_CONNECTED;
2054 param = g_variant_new("(is)", result, address);
2055 _bt_send_event(BT_HID_DEVICE_EVENT, event,
2057 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2058 event = BLUETOOTH_HID_DEVICE_DISCONNECTED;
2059 param = g_variant_new("(is)", result, address);
2060 _bt_send_event(BT_HID_DEVICE_EVENT, event,
2065 g_free(profile_uuid);
2066 } else if (strcasecmp(member, "AdvReport") == 0) {
2068 bt_remote_le_dev_info_t *le_dev_info = NULL;
2069 char *buffer = NULL;
2071 bt_le_adv_info_t *adv_info = NULL;
2072 GVariant *value = NULL;
2073 ret_if(_bt_is_le_scanning() == FALSE);
2075 le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
2076 if (le_dev_info == NULL)
2079 g_variant_get(msg, "(syyii@ay)", &le_dev_info->address,
2080 &le_dev_info->addr_type,
2081 &le_dev_info->adv_type,
2083 &le_dev_info->adv_data_len,
2085 _bt_convert_device_path_to_address(path, le_dev_info->address);
2087 buffer_len = g_variant_get_size(value);
2089 buffer = (char *)g_variant_get_data(value);
2091 le_dev_info->adv_data = g_memdup(buffer, buffer_len);
2092 if (le_dev_info->adv_data == NULL &&
2093 le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {
2094 _bt_free_le_device_info(le_dev_info);
2095 g_variant_unref(value);
2099 if (_bt_get_le_scan_type() == BT_LE_PASSIVE_SCAN) {
2100 _bt_send_scan_result_event(le_dev_info, NULL);
2101 _bt_free_le_device_info(le_dev_info);
2102 g_variant_unref(value);
2106 if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) { /* ADV_IND */
2107 adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
2108 if (adv_info == NULL) {
2109 _bt_free_le_device_info(le_dev_info);
2110 g_variant_unref(value);
2114 adv_info->addr = g_strdup(le_dev_info->address);
2115 adv_info->addr_type = le_dev_info->addr_type;
2116 adv_info->rssi = le_dev_info->rssi;
2117 adv_info->data_len = le_dev_info->adv_data_len;
2118 adv_info->data = g_malloc0(le_dev_info->adv_data_len);
2119 if (adv_info->data) {
2120 memcpy(adv_info->data, le_dev_info->adv_data,
2121 le_dev_info->adv_data_len);
2124 if (__bt_add_adv_ind_info(adv_info) == 0) {
2125 adv_info->timer_id = g_timeout_add(1000,
2126 (GSourceFunc)__bt_adv_scan_req_timeout_cb, (void*)adv_info);
2128 } else { /* SCAN_RSP */
2129 adv_info = __bt_get_adv_ind_info(le_dev_info->address);
2131 _bt_send_scan_result_event(le_dev_info, adv_info);
2132 __bt_del_adv_ind_info(le_dev_info->address);
2135 _bt_free_le_device_info(le_dev_info);
2136 g_variant_unref(value);
2137 } else if (strcasecmp(member, "LEDataLengthChanged") == 0) {
2138 guint16 tx_octets = 0;
2139 guint16 tx_time = 0;
2140 guint16 rx_octets = 0;
2141 guint16 rx_time = 0;
2143 g_variant_get(msg, "(qqqq)",
2144 &tx_octets, &tx_time, &rx_octets, &rx_time);
2146 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2147 _bt_convert_device_path_to_address(path, address);
2149 param = g_variant_new("(isqqqq)", result, address, tx_octets, tx_time,
2150 rx_octets, rx_time);
2151 /* Send event to application */
2152 _bt_send_event(BT_DEVICE_EVENT,
2153 BLUETOOTH_EVENT_LE_DATA_LENGTH_CHANGED, param);
2155 } else if (strcasecmp(member, "IpspStateChanged") == 0) {
2156 gboolean connected = FALSE;
2157 char *ifname = NULL;
2159 g_variant_get(msg, "(bs)", &connected, &ifname);
2161 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
2162 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
2164 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2165 _bt_convert_device_path_to_address(path, address);
2167 BT_DBG("Ipsp BT Interface Name: %s", ifname);
2168 BT_DBG("address: %s", address);
2169 param = g_variant_new("(iss)", result, address, ifname);
2171 /* Send event to application */
2172 _bt_send_event(BT_DEVICE_EVENT,
2176 } else if (strcasecmp(member, "iBeaconReport") == 0) {
2177 bt_remote_ibeacon_dev_info_t *ibeacon_dev_info = NULL;
2179 ret_if(_bt_is_le_scanning() == FALSE);
2181 ibeacon_dev_info = g_malloc0(sizeof(bt_remote_ibeacon_dev_info_t));
2182 if (ibeacon_dev_info == NULL)
2185 g_variant_get(msg, "(syuusuuy)", &ibeacon_dev_info->address,
2186 &ibeacon_dev_info->addr_type,
2187 &ibeacon_dev_info->company_id,
2188 &ibeacon_dev_info->ibeacon_type,
2189 &ibeacon_dev_info->uuid,
2190 &ibeacon_dev_info->major_id,
2191 &ibeacon_dev_info->minor_id,
2192 &ibeacon_dev_info->measured_power);
2193 _bt_send_ibeacon_scan_result_event(ibeacon_dev_info);
2194 g_free(ibeacon_dev_info);
2198 void __bt_set_audio_values(gboolean connected, char *address)
2201 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
2203 /* Set the headset name */
2204 if (connected == TRUE) {
2205 name = _bt_get_bonded_device_name(address);
2207 name = g_strdup("");
2210 if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME,
2212 BT_ERR("vconf_set_str failed");
2217 /* Set the headset state */
2218 if (vconf_get_int(VCONFKEY_BT_DEVICE,
2219 &bt_device_state) != 0) {
2220 BT_ERR("vconf_get_int failed");
2223 #ifdef TIZEN_SUPPORT_DUAL_HF
2224 if ((connected == TRUE) &&
2225 (FALSE == __bt_is_companion_device(address))) {
2226 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2227 } else if ((bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) &&
2228 (FALSE == __bt_is_companion_device(address))) {
2229 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2232 if (connected == TRUE) {
2233 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2234 } else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) {
2235 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2239 if (vconf_set_int(VCONFKEY_BT_DEVICE,
2240 bt_device_state) != 0) {
2241 BT_ERR("vconf_set_int failed");
2245 void _bt_handle_headset_event(GVariant *msg, const char *path)
2247 int result = BLUETOOTH_ERROR_NONE;
2248 gboolean property_flag = FALSE;
2249 char *property = NULL;
2250 GVariant *value = NULL;
2251 GVariant *param = NULL;
2252 g_variant_get(msg, "(sv)", &property, &value);
2253 bluetooth_device_address_t bd_addr;
2255 ret_if(property == NULL);
2257 BT_DBG("Property = %s \n", property);
2258 /* We allow only 1 headset connection (HSP or HFP)*/
2259 if (strcasecmp(property, "Connected") == 0) {
2260 int event = BLUETOOTH_EVENT_NONE;
2261 bt_headset_wait_t *wait_list;
2263 guint restricted = 0x0;
2264 g_variant_get(value, "b", &property_flag);
2266 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2267 if (address == NULL)
2270 _bt_convert_device_path_to_address(path, address);
2271 _bt_convert_addr_string_to_type(bd_addr.addr, address);
2273 if (property_flag == TRUE) {
2274 event = BLUETOOTH_EVENT_AG_CONNECTED;
2275 _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_A2DP, &restricted);
2277 if (_bt_headset_get_local_connection() == FALSE) {
2278 if (restricted == 0x0) /* not restricted*/
2279 _bt_start_timer_for_connection(address, BT_AUDIO_A2DP);
2281 _bt_headset_set_local_connection(FALSE);
2285 event = BLUETOOTH_EVENT_AG_DISCONNECTED;
2287 previous_state = _bt_get_device_state_from_list(BT_AUDIO_HSP, address);
2288 if (previous_state == BT_STATE_DISCONNECTING)
2289 _bt_send_hf_local_term_event(address);
2291 /* Set the State machine here */
2292 __bt_connection_manager_set_state(address, event);
2293 __bt_set_audio_values(property_flag, address);
2294 param = g_variant_new("(is)", result, address);
2295 _bt_send_event(BT_HEADSET_EVENT, event,
2298 if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
2299 /* Remove data from the connected list */
2300 _bt_remove_headset_from_list(BT_AUDIO_HSP, address);
2302 wait_list = _bt_get_audio_wait_data();
2303 if (wait_list == NULL) {
2308 bluetooth_device_address_t device_address;
2310 _bt_set_audio_wait_data_flag(TRUE);
2312 _bt_convert_addr_string_to_type(device_address.addr,
2313 wait_list->address);
2314 _bt_audio_connect(wait_list->req_id, wait_list->type,
2315 &device_address, wait_list->out_param1);
2316 _bt_rel_wait_data();
2317 } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
2318 /* Add data to the connected list */
2319 _bt_add_headset_to_list(BT_AUDIO_HSP,
2320 BT_STATE_CONNECTED, address);
2322 wait_list = _bt_get_audio_wait_data();
2323 if (wait_list != NULL &&
2324 (g_strcmp0(wait_list->address, address) == 0))
2325 _bt_rel_wait_data();
2327 BT_INFO("Check A2DP pending connect");
2328 _bt_audio_check_pending_connect();
2331 } else if (strcasecmp(property, "State") == 0) {
2334 g_variant_get(value, "s", &state);
2336 /* This code assumes we support only 1 headset connection */
2337 /* Need to use the headset list, if we support multi-headsets */
2338 if (strcasecmp(state, "Playing") == 0) {
2339 BT_DBG("Playing: Sco Connected");
2340 } else if (strcasecmp(state, "connected") == 0 ||
2341 strcasecmp(state, "disconnected") == 0) {
2342 BT_DBG("connected/disconnected: Sco Disconnected");
2344 BT_ERR("Not handled state - %s", state);
2349 } else if (strcasecmp(property, "SpeakerGain") == 0) {
2353 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2355 _bt_convert_device_path_to_address(path, address);
2357 spkr_gain = g_variant_get_uint16(value);
2359 BT_DBG("spkr_gain: %d", spkr_gain);
2361 param = g_variant_new("(i&sq)", result, address, spkr_gain);
2362 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
2366 } else if (strcasecmp(property, "MicrophoneGain") == 0) {
2370 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2372 _bt_convert_device_path_to_address(path, address);
2374 mic_gain = g_variant_get_uint16(value);
2376 param = g_variant_new("(i&sq)", result, address, mic_gain);
2377 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_MIC_GAIN,
2384 g_variant_unref(value);
2387 void _bt_handle_sink_event(GVariant *msg, const char *path)
2389 GVariantIter value_iter;
2390 char *property = NULL;
2392 bt_headset_wait_t *wait_list;
2394 GVariant *child = NULL;
2395 GVariant *val = NULL;
2396 GVariant *param = NULL;
2397 g_variant_iter_init(&value_iter, msg);
2398 while ((child = g_variant_iter_next_value(&value_iter))) {
2400 g_variant_get(child, "{sv}", &property, &val);
2402 ret_if(property == NULL);
2404 BT_DBG("Property = %s \n", property);
2407 if (strcasecmp(property, "State") == 0) {
2408 int result = BLUETOOTH_ERROR_NONE;
2411 g_variant_get(val, "s", &value);
2412 BT_DBG("value: %s", value);
2414 if (g_strcmp0(value, "disconnected") == 0) {
2417 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2419 _bt_convert_device_path_to_address(path, address);
2421 __bt_set_device_values(FALSE,
2422 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2423 param = g_variant_new("(is)", result, address);
2424 _bt_send_event(BT_HEADSET_EVENT,
2425 BLUETOOTH_EVENT_AV_DISCONNECTED,
2428 /* Remove data from the connected list */
2429 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2430 wait_list = _bt_get_audio_wait_data();
2431 if (wait_list == NULL) {
2434 g_variant_unref(val);
2435 g_variant_unref(child);
2440 if (((wait_list->type == BT_AUDIO_ALL) &&
2441 (wait_list->ag_flag == TRUE)) ||
2442 (wait_list->type == BT_AUDIO_A2DP) ||
2443 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2444 bluetooth_device_address_t device_address;
2445 _bt_convert_addr_string_to_type(
2446 device_address.addr,
2447 wait_list->address);
2449 _bt_audio_connect(wait_list->req_id,
2452 wait_list->out_param1);
2455 } else if (strcasecmp(value, "Connected") == 0) {
2457 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2458 bluetooth_device_address_t device_address;
2461 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2463 _bt_convert_device_path_to_address(path, address);
2465 __bt_set_device_values(TRUE,
2466 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2467 param = g_variant_new("(is)", result, address);
2468 _bt_send_event(BT_HEADSET_EVENT,
2469 BLUETOOTH_EVENT_AV_CONNECTED,
2471 /* Check for existing Media device to disconnect */
2472 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
2475 /* Match connected device address */
2476 if (g_strcmp0(connected_address, address) != 0) {
2477 /* Convert BD adress from string type */
2478 _bt_convert_addr_string_to_type(
2479 device_address.addr,
2481 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2482 &device_address, NULL);
2486 /* Add data to the connected list */
2487 _bt_add_headset_to_list(BT_AUDIO_A2DP,
2488 BT_STATE_CONNECTED, address);
2495 g_variant_unref(val);
2496 g_variant_unref(child);
2500 void _bt_handle_agent_event(GVariant *msg, const char *member)
2502 int result = BLUETOOTH_ERROR_NONE;
2503 char *address = NULL;
2506 GVariant *param = NULL;
2507 ret_if(member == NULL);
2509 if (strcasecmp(member, "ObexAuthorize") == 0) {
2510 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2511 param = g_variant_new("(iss)", result, address, name);
2512 _bt_send_event(BT_OPP_SERVER_EVENT,
2513 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
2517 } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2518 bt_rfcomm_server_info_t *server_info;
2520 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2522 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2523 ret_if(server_info == NULL);
2524 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2525 param = g_variant_new("(isssn)", result, address, uuid, name,
2526 server_info->control_fd);
2527 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2528 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2536 static int __bt_get_object_path(GVariant *msg, char **path)
2538 g_variant_get(msg, "(o*)", path, NULL);
2540 return BLUETOOTH_ERROR_INTERNAL;
2542 return BLUETOOTH_ERROR_NONE;
2545 static void __bt_devices_list_free(void)
2547 bt_cache_info_t *cache_info;
2550 node = g_list_first(p_cache_list);
2552 while (node != NULL) {
2553 cache_info = (bt_cache_info_t *)node->data;
2554 p_cache_list = g_list_remove(p_cache_list, cache_info);
2555 __bt_free_cache_info(cache_info);
2557 node = g_list_next(node);
2561 static int __bt_parse_event(GVariant *msg)
2565 char *interface_name = NULL;
2566 GVariant *inner_iter = NULL;
2568 g_variant_iter_init(&iter, msg);
2570 while ((child = g_variant_iter_next_value(&iter))) {
2571 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
2572 if (g_strcmp0(interface_name,
2573 BT_DEVICE_INTERFACE) == 0) {
2574 g_variant_unref(inner_iter);
2575 g_variant_unref(child);
2576 return BT_DEVICE_EVENT;
2577 } else if (g_strcmp0(interface_name,
2578 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2579 g_variant_unref(inner_iter);
2580 g_variant_unref(child);
2581 return BT_MEDIA_TRANSFER_EVENT;
2582 } else if (g_strcmp0(interface_name,
2583 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2584 g_variant_unref(inner_iter);
2585 g_variant_unref(child);
2586 return BT_AVRCP_CONTROL_EVENT;
2588 g_variant_unref(inner_iter);
2589 g_variant_unref(child);
2595 static void __bt_manager_event_filter(GDBusConnection *connection,
2596 const gchar *sender_name,
2597 const gchar *object_path,
2598 const gchar *interface_name,
2599 const gchar *signal_name,
2600 GVariant *parameters,
2603 bt_event_type_t bt_event = 0x00;
2604 int result = BLUETOOTH_ERROR_NONE;
2606 char *obj_path = NULL;
2607 GVariant *param = NULL;
2608 if (signal_name == NULL)
2610 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2611 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2613 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
2614 #ifdef USB_BLUETOOTH
2615 BT_DBG("Enable Adapter");
2616 _bt_enable_adapter();
2618 _bt_handle_adapter_added();
2621 bt_event = __bt_parse_event(value);
2622 if (bt_event == BT_DEVICE_EVENT) {
2623 bt_cache_info_t *cache_info;
2624 bt_remote_dev_info_t *dev_info;
2626 ret_if(_bt_is_discovering() == FALSE &&
2627 _bt_is_le_scanning() == FALSE);
2629 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2630 ret_if(cache_info == NULL);
2632 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2633 if (dev_info == NULL) {
2634 __bt_free_cache_info(cache_info);
2638 cache_info->dev_info = dev_info;
2640 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2641 BT_ERR("Fail to parse the properies");
2642 __bt_free_cache_info(cache_info);
2643 g_variant_unref(value);
2647 if (dev_info->addr_type != BDADDR_BREDR) {
2648 /* Whenever emit the property changed from bluez,
2649 some property doesn't reach to bt-service.
2650 So LE device is handled as AdvReport signal */
2651 __bt_free_cache_info(cache_info);
2652 g_variant_unref(value);
2656 if (dev_info->name == NULL)
2657 /* If Remote device name is NULL or still RNR is not done
2658 * then display address as name.
2660 dev_info->name = g_strdup(dev_info->address);
2662 #ifdef TIZEN_DPM_ENABLE
2663 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
2665 bluetooth_device_class_t device_class;
2666 _bt_divide_device_class(&device_class, dev_info->class);
2667 BT_DBG("[%s]device_class.major_class : %d", dev_info->name, device_class.major_class);
2669 if (device_class.major_class ==
2670 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
2671 __bt_free_cache_info(cache_info);
2672 g_variant_unref(value);
2678 GVariant *uuids = NULL;
2679 GVariantBuilder *builder = NULL;
2681 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2682 for (i = 0; i < dev_info->uuid_count; i++) {
2683 g_variant_builder_add(builder, "s",
2684 dev_info->uuids[i]);
2686 uuids = g_variant_new("as", builder);
2687 g_variant_builder_unref(builder);
2688 GVariant *manufacturer_data = NULL;
2689 manufacturer_data = g_variant_new_from_data(
2690 G_VARIANT_TYPE_BYTESTRING,
2691 dev_info->manufacturer_data,
2692 dev_info->manufacturer_data_len,
2694 param = g_variant_new("(isunsbub@asn@ay)", result,
2700 dev_info->connected,
2703 dev_info->manufacturer_data_len,
2705 _bt_send_event(BT_ADAPTER_EVENT,
2706 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2708 p_cache_list = g_list_append(p_cache_list, cache_info);
2709 } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2710 BT_DBG("Device path : %s ", obj_path);
2711 _bt_set_control_device_path(obj_path);
2714 g_variant_unref(value);
2715 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2716 #ifdef USB_BLUETOOTH
2717 BT_DBG("InterfacesRemoved");
2718 _bt_handle_adapter_removed();
2720 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0) {
2721 bt_event = BT_MEDIA_TRANSFER_EVENT;
2722 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2723 bt_event = BT_DEVICE_EVENT;
2724 } else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
2725 bt_event = BT_AVRCP_CONTROL_EVENT;
2727 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2728 _bt_handle_adapter_event(parameters, signal_name);
2729 if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2730 BT_INFO("Object Path %s", obj_path);
2731 _bt_remove_control_device_path(obj_path);
2734 } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2737 char *previous = NULL;
2738 char *current = NULL;
2740 if (__bt_get_owner_info(parameters, &name, &previous, ¤t)) {
2741 BT_ERR("Fail to get the owner info");
2745 if (*current != '\0') {
2754 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2755 BT_DBG("Bluetoothd is terminated");
2756 if (_bt_adapter_get_status() == BT_ACTIVATED)
2759 _bt_handle_adapter_removed();
2760 __bt_devices_list_free();
2763 _bt_obex_server_check_allocation(&value);
2765 if (value == TRUE) {
2766 /* Check if the obex server was terminated abnormally */
2767 _bt_obex_server_check_termination(name);
2770 _bt_rfcomm_server_check_existence(&value);
2772 if (value == TRUE) {
2773 /* The obex server was terminated abnormally */
2774 _bt_rfcomm_server_check_termination(name);
2777 /* Stop advertising started by terminated process */
2778 _bt_stop_advertising_by_terminated_process(name);
2780 _bt_stop_le_scan(name);
2784 } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2785 const char *path = object_path;
2787 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2788 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2791 _bt_handle_property_changed_event(parameters, object_path);
2792 } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2793 _bt_handle_adapter_event(parameters, signal_name);
2794 } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2795 _bt_handle_input_event(parameters, object_path);
2796 } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2797 _bt_handle_network_server_event(parameters, signal_name);
2798 } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2799 _bt_handle_headset_event(parameters, object_path);
2800 } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2801 _bt_handle_sink_event(parameters, object_path);
2802 } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2803 _bt_handle_agent_event(parameters, signal_name);
2804 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2805 _bt_handle_device_event(parameters, signal_name, object_path);
2806 } else if (g_strcmp0(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
2807 _bt_handle_gatt_event(parameters, signal_name, object_path);
2813 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2816 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2817 char *interface_name = NULL;
2819 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2820 retv_if(interface_name == NULL, FALSE);
2822 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2823 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2831 static void __bt_obexd_event_filter(GDBusConnection *connection,
2832 const gchar *sender_name,
2833 const gchar *object_path,
2834 const gchar *interface_name,
2835 const gchar *signal_name,
2836 GVariant *parameters,
2839 const char *member = signal_name;
2840 char *obj_path = NULL;
2841 ret_if(member == NULL);
2843 if (strcasecmp(member, "InterfacesAdded") == 0) {
2844 if (__bt_get_object_path(parameters, &obj_path)) {
2845 BT_ERR("Fail to get the path");
2848 BT_INFO("object_path = [%s]", obj_path);
2850 /*Handle OPP_SERVER_CONNECTED_EVENT here */
2851 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2852 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2857 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2858 BT_DBG("Obex_Server_Session_Transfer connected");
2859 _bt_obex_transfer_connected(obj_path);
2862 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2863 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
2864 if (__bt_get_object_path(parameters, &obj_path)) {
2865 BT_ERR("Fail to get the path");
2868 BT_INFO("object_path = [%s]", obj_path);
2870 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2871 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2872 BT_DBG("Call PBAP Disconnected");
2873 _bt_obex_pbap_client_disconnect(obj_path);
2876 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2877 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2882 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2883 BT_DBG("Obex_Server_Session_Transfer disconnected %s",
2886 _bt_obex_transfer_disconnected(obj_path);
2889 } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
2890 const char *path = object_path;
2892 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
2893 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
2894 strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2895 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2896 BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
2900 _bt_handle_property_changed_event(parameters, path);
2906 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
2910 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2911 char *interface_name = NULL;
2913 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2915 retv_if(interface_name == NULL, FALSE);
2917 if (strcasecmp(interface_name,
2918 BT_OBEX_TRANSFER_INTERFACE) == 0) {
2929 static void __bt_opc_event_filter(GDBusConnection *connection,
2930 const gchar *sender_name,
2931 const gchar *object_path,
2932 const gchar *interface_name,
2933 const gchar *signal_name,
2934 GVariant *parameters,
2937 const char *member = signal_name;
2938 char *obj_path = NULL;
2939 if (strcasecmp(member, "InterfacesAdded") == 0) {
2940 BT_DBG("InterfacesAdded");
2941 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2943 if (__bt_get_object_path(parameters, &obj_path)) {
2944 BT_ERR("Fail to get the path");
2948 BT_DBG("object_path = %s", obj_path);
2950 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2951 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
2952 || strstr(obj_path, "transfer") == NULL) {
2955 } else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2956 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2957 BT_DBG("Going to call opc disconnected");
2958 _bt_opc_disconnected(obj_path);
2961 _bt_sending_files();
2963 } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
2964 char *path = (char *)object_path;
2965 BT_INFO("object_path %s", path);
2966 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2967 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2968 BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
2972 _bt_opc_property_changed_event(parameters, path);
2978 int _bt_opp_client_event_init(void)
2980 GError *error = NULL;
2982 if (opc_obexd_conn == NULL) {
2983 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
2985 if (!opc_obexd_conn) {
2987 BT_ERR("Unable to connect to dbus: %s", error->message);
2988 g_clear_error(&error);
2990 return BLUETOOTH_ERROR_INTERNAL;
2994 if (_bt_register_service_event(opc_obexd_conn,
2995 BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
2996 g_object_unref(opc_obexd_conn);
2997 opc_obexd_conn = NULL;
2998 return BLUETOOTH_ERROR_INTERNAL;
3001 return BLUETOOTH_ERROR_NONE;
3004 void _bt_opp_client_event_deinit(void)
3006 if (opc_obexd_conn) {
3007 _bt_unregister_service_event(opc_obexd_conn,
3008 BT_OPP_CLIENT_EVENT);
3009 g_object_unref(opc_obexd_conn);
3010 opc_obexd_conn = NULL;
3014 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
3020 static int subs_interface_added_id = -1;
3021 static int subs_interface_removed_id = -1;
3022 static int subs_name_owner_id = -1;
3023 static int subs_property_id = -1;
3024 static int subs_adapter_id = -1;
3025 static int subs_gatt_id = -1;
3028 if (subs_interface_added_id == -1) {
3029 subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3030 NULL, BT_MANAGER_INTERFACE,
3031 BT_INTERFACES_ADDED, NULL, NULL, 0,
3032 __bt_manager_event_filter,
3035 if (subs_interface_removed_id == -1) {
3036 subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3037 NULL, BT_MANAGER_INTERFACE,
3038 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3039 __bt_manager_event_filter,
3042 if (subs_name_owner_id == -1) {
3043 subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
3044 NULL, BT_FREEDESKTOP_INTERFACE,
3045 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
3046 __bt_manager_event_filter,
3049 if (subs_property_id == -1) {
3050 subs_property_id = g_dbus_connection_signal_subscribe(conn,
3051 NULL, BT_PROPERTIES_INTERFACE,
3052 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3053 __bt_manager_event_filter,
3056 if (subs_adapter_id == -1) {
3057 subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
3058 NULL, BT_ADAPTER_INTERFACE,
3059 NULL, NULL, NULL, 0,
3060 __bt_manager_event_filter,
3063 if (subs_gatt_id == -1) {
3064 subs_gatt_id = g_dbus_connection_signal_subscribe(conn,
3065 NULL, BT_GATT_CHAR_INTERFACE,
3066 NULL, NULL, NULL, 0,
3067 __bt_manager_event_filter,
3071 if (subs_interface_added_id != -1) {
3072 g_dbus_connection_signal_unsubscribe(conn,
3073 subs_interface_added_id);
3074 subs_interface_added_id = -1;
3076 if (subs_interface_removed_id != -1) {
3077 g_dbus_connection_signal_unsubscribe(conn,
3078 subs_interface_removed_id);
3079 subs_interface_removed_id = -1;
3081 if (subs_name_owner_id != -1) {
3082 g_dbus_connection_signal_unsubscribe(conn,
3083 subs_name_owner_id);
3084 subs_name_owner_id = -1;
3086 if (subs_property_id != -1) {
3087 g_dbus_connection_signal_unsubscribe(conn,
3089 subs_property_id = -1;
3091 if (subs_adapter_id != -1) {
3092 g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
3093 subs_adapter_id = -1;
3095 if (subs_gatt_id != -1) {
3096 g_dbus_connection_signal_unsubscribe(conn, subs_gatt_id);
3103 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
3109 static int subs_device_id = -1;
3112 if (subs_device_id == -1) {
3113 subs_device_id = g_dbus_connection_signal_subscribe(conn,
3114 NULL, BT_DEVICE_INTERFACE,
3115 NULL, NULL, NULL, 0,
3116 __bt_manager_event_filter,
3120 if (subs_device_id != -1) {
3121 g_dbus_connection_signal_unsubscribe(conn,
3123 subs_device_id = -1;
3129 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
3135 static int subs_input_id = -1;
3138 if (subs_input_id == -1) {
3139 subs_input_id = g_dbus_connection_signal_subscribe(conn,
3140 NULL, BT_INPUT_INTERFACE,
3141 NULL, NULL, NULL, 0,
3142 __bt_manager_event_filter,
3146 if (subs_input_id != -1) {
3147 g_dbus_connection_signal_unsubscribe(conn,
3155 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
3161 static int subs_serv_id = -1;
3162 static int subs_client_id = -1;
3165 if (subs_serv_id == -1) {
3166 subs_serv_id = g_dbus_connection_signal_subscribe(conn,
3167 NULL, BT_NETWORK_SERVER_INTERFACE,
3168 NULL, NULL, NULL, 0,
3169 __bt_manager_event_filter,
3172 if (subs_client_id == -1) {
3173 subs_client_id = g_dbus_connection_signal_subscribe(conn,
3174 NULL, BT_NETWORK_CLIENT_INTERFACE,
3175 NULL, NULL, NULL, 0,
3176 __bt_manager_event_filter,
3180 if (subs_serv_id != -1) {
3181 g_dbus_connection_signal_unsubscribe(conn,
3185 if (subs_client_id != -1) {
3186 g_dbus_connection_signal_unsubscribe(conn,
3188 subs_client_id = -1;
3194 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
3200 static int subs_headset_id = -1;
3201 static int subs_sink_id = -1;
3204 if (subs_headset_id == -1) {
3205 subs_headset_id = g_dbus_connection_signal_subscribe(conn,
3206 NULL, BT_HEADSET_INTERFACE,
3207 NULL, NULL, NULL, 0,
3208 __bt_manager_event_filter,
3211 if (subs_sink_id == -1) {
3212 subs_sink_id = g_dbus_connection_signal_subscribe(conn,
3213 NULL, BT_SINK_INTERFACE,
3214 NULL, NULL, NULL, 0,
3215 __bt_manager_event_filter,
3219 if (subs_headset_id != -1) {
3220 g_dbus_connection_signal_unsubscribe(conn,
3222 subs_headset_id = -1;
3224 if (subs_sink_id != -1) {
3225 g_dbus_connection_signal_unsubscribe(conn,
3233 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
3239 static int subs_opp_server_interface_added_id = -1;
3240 static int subs_opp_server_interface_removed_id = -1;
3241 static int subs_opp_server_property_id = -1;
3245 if (subs_opp_server_interface_added_id == -1) {
3246 subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3247 NULL, BT_MANAGER_INTERFACE,
3248 BT_INTERFACES_ADDED, NULL, NULL, 0,
3249 __bt_obexd_event_filter,
3252 if (subs_opp_server_interface_removed_id == -1) {
3253 subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3254 NULL, BT_MANAGER_INTERFACE,
3255 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3256 __bt_obexd_event_filter,
3259 if (subs_opp_server_property_id == -1) {
3260 subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
3261 NULL, BT_PROPERTIES_INTERFACE,
3262 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3263 __bt_obexd_event_filter,
3267 if (subs_opp_server_interface_added_id != -1) {
3268 g_dbus_connection_signal_unsubscribe(conn,
3269 subs_opp_server_interface_added_id);
3270 subs_opp_server_interface_added_id = -1;
3272 if (subs_opp_server_interface_removed_id != -1) {
3273 g_dbus_connection_signal_unsubscribe(conn,
3274 subs_opp_server_interface_removed_id);
3275 subs_opp_server_interface_removed_id = -1;
3277 if (subs_opp_server_property_id != -1) {
3278 g_dbus_connection_signal_unsubscribe(conn,
3279 subs_opp_server_property_id);
3280 subs_opp_server_property_id = -1;
3286 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
3292 static int subs_opp_client_interface_added_id = -1;
3293 static int subs_opp_client_interface_removed_id = -1;
3294 static int subs_opp_client_property_id = -1;
3298 if (subs_opp_client_interface_added_id == -1) {
3299 subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3300 NULL, BT_MANAGER_INTERFACE,
3301 BT_INTERFACES_ADDED, NULL, NULL, 0,
3302 __bt_opc_event_filter,
3305 if (subs_opp_client_interface_removed_id == -1) {
3306 subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3307 NULL, BT_MANAGER_INTERFACE,
3308 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3309 __bt_opc_event_filter,
3312 if (subs_opp_client_property_id == -1) {
3313 subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
3314 NULL, BT_PROPERTIES_INTERFACE,
3315 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3316 __bt_opc_event_filter,
3320 if (subs_opp_client_interface_added_id != -1) {
3321 g_dbus_connection_signal_unsubscribe(conn,
3322 subs_opp_client_interface_added_id);
3323 subs_opp_client_interface_added_id = -1;
3325 if (subs_opp_client_interface_removed_id != -1) {
3326 g_dbus_connection_signal_unsubscribe(conn,
3327 subs_opp_client_interface_removed_id);
3328 subs_opp_client_interface_removed_id = -1;
3330 if (subs_opp_client_property_id != -1) {
3331 g_dbus_connection_signal_unsubscribe(conn,
3332 subs_opp_client_property_id);
3333 subs_opp_client_property_id = -1;
3339 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
3345 static int subs_a2dp_source_id = -1;
3346 static int subs_a2dp_sink_id = -1;
3349 if (subs_a2dp_source_id == -1) {
3350 subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
3351 NULL, BT_A2DP_SOURCE_INTERFACE,
3352 NULL, NULL, NULL, 0,
3353 __bt_opc_event_filter,
3356 if (subs_a2dp_sink_id == -1) {
3357 subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
3358 NULL, BT_SINK_INTERFACE,
3359 NULL, NULL, NULL, 0,
3360 __bt_opc_event_filter,
3364 if (subs_a2dp_source_id != -1) {
3365 g_dbus_connection_signal_unsubscribe(conn,
3366 subs_a2dp_source_id);
3367 subs_a2dp_source_id = -1;
3369 if (subs_a2dp_sink_id != -1) {
3370 g_dbus_connection_signal_unsubscribe(conn,
3372 subs_a2dp_sink_id = -1;
3378 static void __bt_dump_event_filter(GDBusConnection *connection,
3379 const gchar *sender_name,
3380 const gchar *object_path,
3381 const gchar *interface_name,
3382 const gchar *signal_name,
3383 GVariant *parameters,
3389 int __bt_register_dump_subscribe_signal(GDBusConnection *conn,
3395 static int subs_source_id = -1;
3398 if (subs_source_id == -1) {
3399 subs_source_id = g_dbus_connection_signal_subscribe(conn,
3400 NULL, BT_DUMP_SERVICE_INTERFACE,
3401 BT_DUMP_SERVICE_SIGNAL, BT_DUMP_SERVICE_PATH, NULL, 0,
3402 __bt_dump_event_filter,
3406 if (subs_source_id != -1) {
3407 g_dbus_connection_signal_unsubscribe(conn,
3409 subs_source_id = -1;
3415 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
3419 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3421 switch (event_type) {
3422 case BT_MANAGER_EVENT:
3423 _bt_register_manager_subscribe_signal(g_conn, TRUE);
3425 case BT_DEVICE_EVENT:
3426 _bt_register_device_subscribe_signal(g_conn, TRUE);
3429 _bt_register_input_subscribe_signal(g_conn, TRUE);
3431 case BT_NETWORK_EVENT:
3432 _bt_register_network_subscribe_signal(g_conn, TRUE);
3434 case BT_HEADSET_EVENT:
3435 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3438 case BT_OPP_SERVER_EVENT:
3439 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3440 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3442 case BT_OPP_CLIENT_EVENT:
3443 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3444 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3446 case BT_A2DP_SOURCE_EVENT:
3447 BT_INFO("A2dp Source event");
3448 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3451 BT_ERR("Unknown event");
3452 return BLUETOOTH_ERROR_INTERNAL;
3455 return BLUETOOTH_ERROR_NONE;
3458 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3462 ret_if(g_conn == NULL);
3464 switch (event_type) {
3465 case BT_MANAGER_EVENT:
3466 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3467 _bt_register_device_subscribe_signal(g_conn, FALSE);
3468 _bt_register_input_subscribe_signal(g_conn, FALSE);
3469 _bt_register_network_subscribe_signal(g_conn, FALSE);
3470 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3472 case BT_OPP_SERVER_EVENT:
3473 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3475 case BT_OPP_CLIENT_EVENT:
3476 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3479 BT_ERR("Unknown event");
3486 static int __bt_init_manager_receiver(void)
3490 GError *error = NULL;
3492 if (manager_conn == NULL) {
3493 manager_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3494 if (error != NULL) {
3495 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3496 g_clear_error(&error);
3498 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3501 if (_bt_register_service_event(manager_conn,
3502 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3504 if (_bt_register_service_event(manager_conn,
3505 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3508 if (_bt_register_service_event(manager_conn,
3509 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3512 if (_bt_register_service_event(manager_conn,
3513 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3516 if (_bt_register_service_event(manager_conn,
3517 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3520 __bt_register_dump_subscribe_signal(manager_conn, TRUE);
3521 return BLUETOOTH_ERROR_NONE;
3524 g_object_unref(manager_conn);
3525 manager_conn = NULL;
3530 return BLUETOOTH_ERROR_INTERNAL;
3533 static int __bt_init_obexd_receiver(void)
3536 #ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3537 GError *error = NULL;
3539 if (obexd_conn == NULL) {
3540 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3541 if (error != NULL) {
3542 BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3543 g_clear_error(&error);
3545 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3548 if (_bt_register_service_event(obexd_conn,
3549 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3550 BT_ERR("Error while registering service event");
3551 g_object_unref(obexd_conn);
3553 return BLUETOOTH_ERROR_INTERNAL;
3558 return BLUETOOTH_ERROR_NONE;
3561 gboolean __bt_reinit_obexd_receiver(gpointer user_data)
3563 static int retry_cnt = 0;
3564 int result = BLUETOOTH_ERROR_NONE;
3568 result = __bt_init_obexd_receiver();
3569 if (result != BLUETOOTH_ERROR_NONE) {
3570 /* 20 ms * 50 = 10 seconds. During 10 seconds fail to initialize,
3571 then it is not the timing issue. Just can't use the session bus connection */
3572 if (retry_cnt > 100) {
3573 BT_ERR("Fail to init obexd receiver by 50 times.");
3575 session_reinit_timer = 0;
3579 BT_DBG("Retry to initialize the obexd receiver");
3584 session_reinit_timer = 0;
3591 /* To receive the event from bluez */
3592 int _bt_init_service_event_receiver(void)
3598 result = __bt_init_manager_receiver();
3599 retv_if(result != BLUETOOTH_ERROR_NONE, result);
3601 result = __bt_init_obexd_receiver();
3602 if (result != BLUETOOTH_ERROR_NONE) {
3603 BT_ERR("Fail to init obexd receiver");
3605 /* Try to re-initialize obexd receiver in the timer */
3606 if (session_reinit_timer > 0)
3607 g_source_remove(session_reinit_timer);
3609 session_reinit_timer = g_timeout_add(BT_SESSION_BUS_GET_TIMEOUT,
3610 (GSourceFunc)__bt_reinit_obexd_receiver, NULL);
3615 return BLUETOOTH_ERROR_NONE;
3618 void _bt_deinit_service_event_receiver(void)
3622 _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3624 _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3626 __bt_register_dump_subscribe_signal(manager_conn, FALSE);
3629 g_object_unref(manager_conn);
3630 manager_conn = NULL;
3634 g_object_unref(obexd_conn);
3639 g_source_remove(event_id);