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");
314 if (connected == TRUE)
315 bt_device_state |= state;
316 else if (bt_device_state & state)
317 bt_device_state ^= state;
319 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0)
320 BT_ERR("vconf_set_int failed");
323 gboolean _bt_discovery_finished_cb(gpointer user_data)
325 int result = BLUETOOTH_ERROR_NONE;
327 GVariant *param = NULL;
328 if (_bt_get_discovering_property(DISCOVERY_ROLE_BREDR) == FALSE) {
329 if (_bt_get_cancel_by_user() == TRUE)
330 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
332 _bt_set_cancel_by_user(FALSE);
333 _bt_set_discovery_status(FALSE);
334 param = g_variant_new("(i)", result);
335 _bt_send_event(BT_ADAPTER_EVENT,
336 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
343 static gboolean __bt_le_discovery_finished_cb(gpointer user_data)
345 int result = BLUETOOTH_ERROR_NONE;
347 GVariant *param = NULL;
348 if (_bt_get_discovering_property(DISCOVERY_ROLE_LE) == FALSE) {
349 if (_bt_get_cancel_by_user() == TRUE)
350 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
352 g_list_free_full(p_adv_ind_list, __bt_free_bt_le_adv_info_t);
353 p_adv_ind_list = NULL;
355 _bt_set_cancel_by_user(FALSE);
356 _bt_disable_all_scanner_status();
357 _bt_set_le_scan_status(FALSE);
358 param = g_variant_new("(i)", result);
359 _bt_send_event(BT_LE_ADAPTER_EVENT,
360 BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED,
367 void __bt_update_remote_cache_devinfo(const char *address, gboolean paired_status)
371 ret_if(address == NULL);
374 bt_cache_info_t *cache_info;
375 bt_remote_dev_info_t *dev_info;
377 node = g_list_first(p_cache_list);
379 while (node != NULL) {
380 cache_info = (bt_cache_info_t *)node->data;
382 if (cache_info == NULL) {
383 node = g_list_next(node);
387 dev_info = cache_info->dev_info;
388 if (strcasecmp(dev_info->address,
390 BT_DBG("Device Found");
391 if (paired_status == TRUE)
392 cache_info->dev_info->paired = TRUE;
394 cache_info->dev_info->paired = FALSE;
397 node = g_list_next(node);
402 static void __bt_device_remote_connected_properties(
403 bt_remote_dev_info_t *remote_dev_info,
404 char *address, gboolean connected)
406 int result = BLUETOOTH_ERROR_NONE;
408 GVariant *param = NULL;
411 if (remote_dev_info->uuid_count > 0) {
412 for (i = 0; i < remote_dev_info->uuid_count; i++) {
413 char *uuid = remote_dev_info->uuids[i];
414 if (strcasecmp(uuid, HID_UUID) == 0) {
415 int event = BLUETOOTH_EVENT_NONE;
417 event = (connected == TRUE) ?
418 BLUETOOTH_HID_CONNECTED :
419 BLUETOOTH_HID_DISCONNECTED;
420 param = g_variant_new("(is)", result,
422 _bt_send_event(BT_HID_EVENT, event,
432 void _bt_handle_adapter_event(GVariant *msg, const char *member)
436 int result = BLUETOOTH_ERROR_NONE;
437 GVariant *param = NULL;
438 ret_if(member == NULL);
440 if (strcasecmp(member, "DeviceCreated") == 0) {
441 char *object_path = NULL;
443 bt_remote_dev_info_t *remote_dev_info;
445 ret_if(_bt_is_device_creating() == FALSE);
447 /* Bonding from remote device */
448 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
450 g_variant_get(msg, "(&o)", &object_path);
451 _bt_convert_device_path_to_address((const char*)object_path, address);
453 remote_dev_info = _bt_get_remote_device_info_by_object_path(object_path);
454 if (remote_dev_info == NULL) {
459 _bt_free_device_info(remote_dev_info);
461 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
462 char *object_path = NULL;
464 bt_cache_info_t *cache_info;
465 bt_remote_dev_info_t *dev_info;
468 /* Bonding from remote device */
469 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
473 g_variant_get(msg, "(&o)", &object_path);
475 _bt_convert_device_path_to_address((const char *)object_path, address);
477 node = g_list_first(p_cache_list);
479 while (node != NULL) {
480 cache_info = (bt_cache_info_t *)node->data;
482 if (cache_info == NULL) {
483 node = g_list_next(node);
487 dev_info = cache_info->dev_info;
488 if (strcasecmp(dev_info->address,
490 p_cache_list = g_list_remove(p_cache_list,
492 __bt_free_cache_info(cache_info);
495 node = g_list_next(node);
498 } else if (strcasecmp(member, "AdvertisingEnabled") == 0) {
499 BT_DBG("Advertising Enabled");
503 gboolean status = FALSE;
505 g_variant_get(msg, "(ib)", &slot_id, &status);
507 BT_DBG("Advertising Enabled : slot_id [%d] status [%d]", slot_id, status);
509 /* Send event to application */
510 _bt_set_advertising_status(slot_id, status);
512 adv_handle = _bt_get_adv_slot_adv_handle(slot_id);
515 event = BLUETOOTH_EVENT_ADVERTISING_STARTED;
517 event = BLUETOOTH_EVENT_ADVERTISING_STOPPED;
518 param = g_variant_new("(ii)", result,
523 sender = _bt_get_adv_slot_owner(slot_id);
524 _bt_send_event_to_dest(sender, BT_ADAPTER_EVENT,
528 _bt_send_event(BT_ADAPTER_EVENT, event, param);
531 if (event == BLUETOOTH_EVENT_ADVERTISING_STOPPED)
532 _bt_unregister_adv_slot_owner(slot_id);
533 } else if (strcasecmp(member, "RssiEnabled") == 0) {
534 BT_DBG("RSSI Enabled");
535 gboolean status = FALSE;
536 char *address = NULL;
538 g_variant_get(msg, "(sib)", &address, &link_type, &status);
540 BT_DBG("RSSI Enabled [%s %d]", address, status);
541 param = g_variant_new("(isib)", result,
542 address, link_type, status);
543 _bt_send_event(BT_DEVICE_EVENT,
544 BLUETOOTH_EVENT_RSSI_ENABLED,
547 } else if (strcasecmp(member, "RssiAlert") == 0) {
548 BT_DBG("RSSI Alert");
552 char *address = NULL;
553 g_variant_get(msg, "(siii)", &address, &link_type, &alert_type, &rssi_dbm);
555 BT_DBG("RSSI Alert: [Address %s LinkType %d] [Type %d DBM %d]",
556 address, alert_type, rssi_dbm);
557 param = g_variant_new("(isiii)", result,
558 address, link_type, alert_type, rssi_dbm);
559 _bt_send_event(BT_DEVICE_EVENT,
560 BLUETOOTH_EVENT_RSSI_ALERT,
563 } else if (strcasecmp(member, "RawRssi") == 0) {
567 char *address = NULL;
568 g_variant_get(msg, "(sii)", &address, &link_type, &rssi_dbm);
570 BT_DBG("Raw RSSI: [Address %s] [Link Type %d][RSSI DBM %d]",
571 address, link_type, rssi_dbm);
572 param = g_variant_new("(isii)", result,
573 address, link_type, rssi_dbm);
574 _bt_send_event(BT_DEVICE_EVENT,
575 BLUETOOTH_EVENT_RAW_RSSI,
578 } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
579 BT_ERR_C("Hardware error received from BLUEZ");
580 /* Don't add the recovery logic into platform */
582 _bt_recover_adapter();
584 } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
585 BT_ERR_C("Tx timeout error received from BLUEZ");
586 /* Don't add the recovery logic into platform */
588 _bt_recover_adapter();
594 static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
596 GDBusProxy *adapter_proxy;
598 int result = BLUETOOTH_ERROR_NONE;
599 GVariantIter value_iter;
600 GVariant *val = NULL;
601 GVariant *ret = NULL;
603 char *property = NULL;
604 GVariant *param = NULL;
605 g_variant_iter_init(&value_iter, msg);
606 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
607 BT_INFO("Property %s", property);
609 if (strcasecmp(property, "Discovering") == 0) {
610 gboolean discovering = FALSE;
611 g_variant_get(val, "b", &discovering);
612 /* Send event to application */
613 BT_DBG("Discovering %d", discovering);
614 if (discovering == TRUE) {
615 _bt_set_discovery_status(TRUE);
616 param = g_variant_new("(i)", result);
617 _bt_send_event(BT_ADAPTER_EVENT,
618 BLUETOOTH_EVENT_DISCOVERY_STARTED,
621 ret_if(event_id > 0);
623 adapter_proxy = _bt_get_adapter_proxy();
624 ret_if(adapter_proxy == NULL);
626 /* Need to stop searching */
627 ret = g_dbus_proxy_call_sync(adapter_proxy, "StopDiscovery",
629 G_DBUS_CALL_FLAGS_NONE,
633 BT_ERR("Dbus Error : %s", err->message);
637 g_variant_unref(ret);
639 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
640 (GSourceFunc)_bt_discovery_finished_cb, NULL);
642 } else if (strcasecmp(property, "LEDiscovering") == 0) {
643 gboolean le_discovering = FALSE;
645 g_variant_get(val, "b", &le_discovering);
646 /* Send event to application */
647 if (le_discovering == TRUE) {
648 _bt_set_le_scan_status(TRUE);
649 param = g_variant_new("(i)", result);
650 _bt_send_event(BT_LE_ADAPTER_EVENT,
651 BLUETOOTH_EVENT_LE_DISCOVERY_STARTED,
654 ret_if(event_id > 0);
656 adapter_proxy = _bt_get_adapter_proxy();
657 ret_if(adapter_proxy == NULL);
659 /* Need to stop searching */
660 ret = g_dbus_proxy_call_sync(adapter_proxy, "StopLEDiscovery",
662 G_DBUS_CALL_FLAGS_NONE,
666 BT_ERR("Dbus Error %s", err->message);
670 g_variant_unref(ret);
672 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
673 (GSourceFunc)__bt_le_discovery_finished_cb, NULL);
675 } else if (strcasecmp(property, "Name") == 0) {
677 g_variant_get(val, "s", &name);
678 param = g_variant_new("(is)", result, name);
679 /* Send event to application */
680 _bt_send_event(BT_ADAPTER_EVENT,
681 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
684 } else if (strcasecmp(property, "Alias") == 0) {
686 g_variant_get(val, "s", &alias);
687 param = g_variant_new("(is)", result, alias);
688 /* Send event to application */
689 _bt_send_event(BT_ADAPTER_EVENT,
690 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
693 } else if (strcasecmp(property, "Discoverable") == 0) {
694 gboolean discoverable = FALSE;
696 g_variant_get(val, "b", &discoverable);
697 BT_DBG("discoverable %d", discoverable);
699 if (discoverable == FALSE) {
700 if (_bt_get_discoverable_timeout_property() > 0) {
702 adapter_proxy = _bt_get_adapter_properties_proxy();
703 ret_if(adapter_proxy == NULL);
704 ret = g_dbus_proxy_call_sync(adapter_proxy, "Set",
705 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
706 "DiscoverableTimeout",
707 g_variant_new("i", time)),
708 G_DBUS_CALL_FLAGS_NONE,
713 BT_ERR("StopLEDiscovery Failed: %s", err->message);
717 g_variant_unref(ret);
720 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
722 /* Send event to application */
723 BT_INFO("[Connectable]");
724 param = g_variant_new("(in)", result, mode);
725 _bt_send_event(BT_ADAPTER_EVENT,
726 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
729 _bt_get_discoverable_mode(&mode);
731 /* Event will be sent by "DiscoverableTimeout" signal */
732 if (mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
734 g_variant_unref(val);
738 /* Send event to application */
739 BT_INFO("[General Discoverable]");
740 param = g_variant_new("(in)", result, mode);
741 _bt_send_event(BT_ADAPTER_EVENT,
742 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
745 } else if (strcasecmp(property, "DiscoverableTimeout") == 0) {
746 _bt_get_discoverable_mode(&mode);
748 /* Event was already sent by "Discoverable" signal */
749 if (mode == BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE ||
750 mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
752 g_variant_unref(val);
756 /* Send event to application */
757 BT_INFO("[Limited Discoverable (Timeout %u secs)]",
758 _bt_get_discoverable_timeout_property());
759 param = g_variant_new("(in)", result, mode);
760 _bt_send_event(BT_ADAPTER_EVENT,
761 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
763 } else if (strcasecmp(property, "Powered") == 0) {
764 /* TODO: Need to check this operation!! */
765 gboolean powered = FALSE;
768 g_variant_get(val, "b", &powered);
769 BT_DBG("Powered = %d", powered);
770 if (powered == FALSE) {
772 _bt_handle_adapter_removed();
774 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_state) == 0 &&
775 bt_state != VCONFKEY_BT_STATUS_OFF) {
776 _bt_disable_adapter();
779 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_state) == 0 &&
780 bt_state != VCONFKEY_BT_LE_STATUS_OFF) {
781 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
785 _bt_handle_adapter_added();
788 } else if (strcasecmp(property, "Connectable") == 0) {
789 gboolean connectable = FALSE;
791 g_variant_get(val, "b", &connectable);
793 BT_DBG("Connectable property is changed : %d", connectable);
794 param = g_variant_new("(b)", connectable);
795 _bt_send_event(BT_ADAPTER_EVENT,
796 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
799 if (_bt_adapter_get_status() == BT_DEACTIVATING &&
800 _bt_adapter_get_le_status() == BT_LE_ACTIVATED &&
802 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
804 } else if (strcasecmp(property, "SupportedLEFeatures") == 0) {
807 GVariantIter *iter = NULL;
808 g_variant_get(val, "as", &iter);
809 while (g_variant_iter_loop(iter, "s", &name)) {
810 BT_DBG("name = %s", name);
811 g_variant_iter_loop(iter, "s", &value);
812 BT_DBG("Value = %s", value);
813 if (FALSE == _bt_update_le_feature_support(name, value))
814 BT_INFO("Fail to update LE feature info");
817 g_variant_iter_free(iter);
818 } else if (strcasecmp(property, "IpspInitStateChanged") == 0) {
819 gboolean ipsp_initialized = FALSE;
821 g_variant_get(val, "b", &ipsp_initialized);
822 BT_DBG("IPSP init state changed: %d", ipsp_initialized);
823 param = g_variant_new("(b)", ipsp_initialized);
825 /* Send event to application */
826 _bt_send_event(BT_ADAPTER_EVENT,
827 BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,
830 BT_DBG("property : [%s]", property);
835 static void __bt_obex_property_changed_event(GVariant *msg, const char *path)
839 GVariantIter value_iter;
840 GVariant *child = NULL, *val = NULL;
841 char *property = NULL;
842 g_variant_iter_init(&value_iter, msg);
843 while ((child = g_variant_iter_next_value(&value_iter))) {
844 g_variant_get(child, "{sv}", &property, &val);
846 ret_if(property == NULL);
848 BT_DBG("property :%s", property);
850 if (strcasecmp(property, "Status") == 0) {
852 g_variant_get(val, "s", &status);
854 if (strcasecmp(status, "active") == 0) {
855 _bt_obex_transfer_started(path);
856 } else if (strcasecmp(status, "complete") == 0) {
857 _bt_obex_transfer_completed(path, TRUE);
858 _bt_pbap_obex_transfer_completed(path, TRUE);
859 } else if (strcasecmp(status, "error") == 0) {
860 _bt_obex_transfer_completed(path, FALSE);
861 _bt_pbap_obex_transfer_completed(path, FALSE);
864 } else if (strcasecmp(property, "Transferred") == 0) {
865 guint64 transferred = 0;
866 /* As Transferred is expected guint64 so change int to guint64 and
867 * eariler transferred is static because of it can overwrite data
868 * on present on opc_obex_conn or obexd_conn as these are
869 * static memory are in sequential */
870 g_variant_get(val, "t", &transferred);
872 _bt_obex_transfer_progress(path, transferred);
875 g_variant_unref(val);
876 g_variant_unref(child);
881 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
886 int result = BLUETOOTH_ERROR_NONE;
887 GVariantIter value_iter;
889 char *property = NULL;
891 GVariant *param = NULL;
892 bt_remote_dev_info_t *remote_dev_info;
893 g_variant_iter_init(&value_iter, msg);
894 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
895 BT_DBG("Property %s", property);
896 if (strcasecmp(property, "Connected") == 0) {
899 g_variant_get(val, "i", &connected);
901 event = (connected != BLUETOOTH_CONNECTED_LINK_NONE) ?
902 BLUETOOTH_EVENT_DEVICE_CONNECTED :
903 BLUETOOTH_EVENT_DEVICE_DISCONNECTED;
905 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
907 _bt_convert_device_path_to_address(path, address);
909 BT_DBG("connected: %d", connected);
910 BT_DBG("address: %s", address);
912 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
914 if (remote_dev_info != NULL) {
915 __bt_device_remote_connected_properties(
916 remote_dev_info, address,
917 connected != BLUETOOTH_CONNECTED_LINK_NONE ?
919 _bt_free_device_info(remote_dev_info);
921 param = g_variant_new("(is)", result, address);
922 /* Send event to application */
923 _bt_send_event(BT_DEVICE_EVENT,
927 } else if (strcasecmp(property, "RSSI") == 0) {
928 bt_remote_dev_info_t *remote_dev_info;
930 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
932 _bt_convert_device_path_to_address(path, address);
933 BT_DBG("address: %s", address);
935 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
936 if (remote_dev_info == NULL) {
938 g_variant_unref(val);
942 BT_DBG("Address type %d", remote_dev_info->addr_type);
944 if (remote_dev_info->addr_type == 0) {
945 BT_DBG("Name %s", remote_dev_info->name);
947 #ifdef TIZEN_DPM_ENABLE
948 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
950 bluetooth_device_class_t device_class;
951 _bt_divide_device_class(&device_class, remote_dev_info->class);
953 if (device_class.major_class ==
954 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
955 _bt_free_device_info(remote_dev_info);
957 g_variant_unref(val);
964 GVariant *uuids = NULL;
965 GVariantBuilder *builder = NULL;
967 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
968 for (i = 0; i < remote_dev_info->uuid_count; i++) {
969 g_variant_builder_add(builder, "s",
970 remote_dev_info->uuids[i]);
972 uuids = g_variant_new("as", builder);
973 g_variant_builder_unref(builder);
974 GVariant *manufacturer_data = NULL;
975 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
976 remote_dev_info->manufacturer_data,
977 remote_dev_info->manufacturer_data_len,
980 param = g_variant_new("(isunsbub@asn@ay)", result,
981 remote_dev_info->address,
982 remote_dev_info->class,
983 remote_dev_info->rssi,
984 remote_dev_info->name,
985 remote_dev_info->paired,
986 remote_dev_info->connected,
987 remote_dev_info->trust,
989 remote_dev_info->manufacturer_data_len,
992 _bt_send_event(BT_ADAPTER_EVENT,
993 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
997 _bt_free_device_info(remote_dev_info);
998 } else if (strcasecmp(property, "GattConnected") == 0) {
999 gboolean gatt_connected = FALSE;
1001 g_variant_get(val, "b", &gatt_connected);
1003 event = gatt_connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1004 BLUETOOTH_EVENT_GATT_DISCONNECTED;
1006 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1008 _bt_convert_device_path_to_address(path, address);
1010 BT_DBG("gatt_connected: %d", gatt_connected);
1011 BT_DBG("address: %s", address);
1012 param = g_variant_new("(is)", result, address);
1013 /* Send event to application */
1014 _bt_send_event(BT_DEVICE_EVENT,
1018 } else if (strcasecmp(property, "Paired") == 0) {
1019 gboolean paired = FALSE;
1020 bt_remote_dev_info_t *remote_dev_info;
1021 g_variant_get(val, "b", &paired);
1022 _bt_agent_set_canceled(FALSE);
1023 /* BlueZ sends paired signal for each paired device */
1024 /* during activation, We should ignore this, otherwise*/
1025 /* application thinks that a new device got paired */
1026 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1027 BT_DBG("BT is not activated, so ignore this");
1029 g_variant_unref(val);
1033 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1035 _bt_convert_device_path_to_address(path, address);
1037 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1038 if (remote_dev_info == NULL) {
1040 g_variant_unref(val);
1045 if (paired == FALSE) {
1046 BT_INFO("Unpaired: %s", address);
1047 __bt_update_remote_cache_devinfo(address, FALSE);
1048 param = g_variant_new("(is)", result, address);
1049 _bt_send_event(BT_ADAPTER_EVENT,
1050 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
1052 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1054 char *last_connected = NULL;
1055 last_connected = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1056 if (!g_strcmp0(address, last_connected))
1057 _bt_audio_set_auto_connect_device_addr("");
1059 free(last_connected);
1063 char secure_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
1065 _bt_convert_addr_string_to_secure_string(secure_addr, address);
1066 BT_INFO("### Paired: %s", secure_addr);
1067 __bt_update_remote_cache_devinfo(address, TRUE);
1069 if (_bt_is_device_creating() == TRUE) {
1070 BT_DBG("Try to Pair by me");
1071 _bt_free_device_info(remote_dev_info);
1074 g_variant_unref(val);
1077 GVariant *uuids = NULL;
1078 GVariantBuilder *builder = NULL;
1080 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1081 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1082 g_variant_builder_add(builder, "s",
1083 remote_dev_info->uuids[i]);
1085 uuids = g_variant_new("as", builder);
1086 g_variant_builder_unref(builder);
1087 GVariant *manufacturer_data = NULL;
1088 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1089 remote_dev_info->manufacturer_data,
1090 remote_dev_info->manufacturer_data_len,
1094 param = g_variant_new("(isunsbub@asn@ay)", result,
1095 address, remote_dev_info->class,
1096 remote_dev_info->rssi,
1097 remote_dev_info->name,
1098 remote_dev_info->paired,
1099 remote_dev_info->connected,
1100 remote_dev_info->trust,
1102 remote_dev_info->manufacturer_data_len,
1104 _bt_send_event(BT_ADAPTER_EVENT,
1105 BLUETOOTH_EVENT_BONDING_FINISHED,
1108 _bt_free_device_info(remote_dev_info);
1110 } else if (strcasecmp(property, "LegacyPaired") == 0) {
1111 gboolean paired = FALSE;
1112 bt_remote_dev_info_t *remote_dev_info;
1113 unsigned char auth_info[5] = {0, };
1115 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1116 BT_DBG("BT is not activated, so ignore this");
1118 g_variant_unref(val);
1122 g_variant_get(val, "b", &paired);
1123 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1124 BT_DBG("LegacyPaired: %d", paired);
1125 _bt_convert_device_path_to_address(path, address);
1127 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1128 if (remote_dev_info == NULL) {
1131 g_variant_unref(val);
1134 if (remote_dev_info->is_alias_set == FALSE) {
1135 /*minimum Size of the samsung specific manufacturer data is greater than 30 */
1136 if ((remote_dev_info->manufacturer_data_len > 30) &&
1137 (remote_dev_info->manufacturer_data[0] == 0x00) &&
1138 (remote_dev_info->manufacturer_data[1] == 0x75)) {
1140 /* 2 samsung (0x00 0x75) + 1 (control and version) + 1 (service ID) +
1141 1 (discovery version) + 1 (associated service ID)
1142 2 (Proxamity and locality) + 2 (Device type and icon) = 10 */
1144 memcpy(auth_info, &(remote_dev_info->manufacturer_data[10]), 5);
1148 BT_DBG("LegacyPairing Failed with %s. Show Error Popup", remote_dev_info->name);
1149 _bt_launch_system_popup(BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE,
1150 remote_dev_info->name, auth_info, NULL, NULL, NULL);
1152 _bt_free_device_info(remote_dev_info);
1154 } else if (strcasecmp(property, "Trusted") == 0) {
1155 gboolean trusted = FALSE;
1157 g_variant_get(val, "b", &trusted);
1159 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1160 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1162 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1164 _bt_convert_device_path_to_address(path, address);
1166 BT_DBG("trusted: %d", trusted);
1167 BT_DBG("address: %s", address);
1168 param = g_variant_new("(is)", result, address);
1169 /* Send event to application */
1170 _bt_send_event(BT_DEVICE_EVENT,
1174 } else if (strcasecmp(property, "TrustedProfiles") == 0) {
1177 g_variant_get(val, "u", &trusted);
1179 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1180 _bt_convert_device_path_to_address(path, address);
1182 BT_DBG("Address: %s, TrustedProfiles: %d", address, trusted);
1183 _bt_send_event(BT_DEVICE_EVENT,
1184 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1185 g_variant_new("(isi)", result, address, trusted));
1187 } else if (strcasecmp(property, "IpspConnected") == 0) {
1188 gboolean connected = FALSE;
1190 g_variant_get(val, "b", &connected);
1193 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
1194 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
1196 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1198 _bt_convert_device_path_to_address(path, address);
1200 BT_DBG("Ipspconnected: %d", connected);
1201 BT_DBG("address: %s", address);
1202 param = g_variant_new("(is)", result, address);
1204 /* Send event to application */
1205 _bt_send_event(BT_DEVICE_EVENT,
1209 } else if (strcasecmp(property, "IpspBtInterfaceInfo") == 0) {
1210 char *ifname = NULL;
1212 g_variant_get(val, "s", &ifname);
1214 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1216 _bt_convert_device_path_to_address(path, address);
1218 BT_DBG("Ipsp BT Interface Name: %s", ifname);
1219 BT_DBG("address: %s", address);
1220 param = g_variant_new("(iss)", result, address, ifname);
1222 /* Send event to application */
1223 _bt_send_event(BT_DEVICE_EVENT,
1224 BLUETOOTH_EVENT_IPSP_INTERFACE_INFO,
1232 static void __bt_media_control_changed_event(GVariant *msg, const char *path)
1235 int result = BLUETOOTH_ERROR_NONE;
1236 GVariantIter value_iter;
1237 char *property = NULL;
1239 GVariant *val = NULL;
1240 GVariant *child = NULL;
1241 bt_remote_dev_info_t *remote_dev_info;
1242 GVariant *param = NULL;
1243 g_variant_iter_init(&value_iter, msg);
1244 while ((child = g_variant_iter_next_value(&value_iter))) {
1245 g_variant_get(child, "{sv}", &property, &val);
1246 BT_INFO("Property %s", property);
1247 if (strcasecmp(property, "Connected") == 0) {
1248 gboolean connected = FALSE;
1250 g_variant_get(val, "b", &connected);
1252 event = connected ? BLUETOOTH_EVENT_AVRCP_CONNECTED :
1253 BLUETOOTH_EVENT_AVRCP_DISCONNECTED;
1255 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1257 _bt_convert_device_path_to_address(path, address);
1259 BT_DBG("connected: %d", connected);
1260 BT_DBG("address: %s", address);
1262 remote_dev_info = _bt_get_remote_device_info(address);
1264 if (remote_dev_info != NULL) {
1265 __bt_device_remote_connected_properties(
1266 remote_dev_info, address, connected);
1267 _bt_free_device_info(remote_dev_info);
1269 param = g_variant_new("(is)", result, address);
1270 /* Send event to application */
1271 _bt_send_event(BT_AVRCP_EVENT,
1277 g_variant_unref(child);
1278 g_variant_unref(val);
1283 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
1285 char *interface_name = NULL;
1286 GVariant *val = NULL;
1288 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &val, NULL);
1290 if (strcasecmp(interface_name, BT_ADAPTER_INTERFACE) == 0) {
1291 __bt_adapter_property_changed_event(val,
1293 } else if (strcasecmp(interface_name, BT_DEVICE_INTERFACE) == 0) {
1294 __bt_device_property_changed_event(val, object_path);
1295 } else if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1296 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
1297 __bt_obex_property_changed_event(val,
1299 } else if (strcasecmp(interface_name, BT_MEDIA_CONTROL_INTERFACE) == 0) {
1300 __bt_media_control_changed_event(val,
1302 } else if (strcasecmp(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
1303 _bt_handle_avrcp_control_event(val,
1305 } else if (strcasecmp(interface_name, BT_NETWORK_CLIENT_INTERFACE) == 0) {
1306 BT_DBG("BT_NETWORK_CLIENT_INTERFACE");
1307 _bt_handle_network_client_event(val,
1309 } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
1310 __bt_gatt_char_property_changed_event(val,
1313 g_variant_unref(val);
1316 void __bt_opc_property_changed_event(GVariant *msg,
1319 GVariantIter value_iter;
1320 char *property = NULL;
1321 GVariant *val = NULL;
1322 GVariant *child = NULL;
1324 g_variant_iter_init(&value_iter, msg);
1325 while ((child = g_variant_iter_next_value(&value_iter))) {
1326 g_variant_get(child, "{sv}", &property, &val);
1327 ret_if(property == NULL);
1329 if (strcasecmp(property, "Status") == 0) {
1330 char *status = NULL;
1331 g_variant_get(val, "s", &status);
1332 BT_DBG("Status is %s", status);
1334 if (strcasecmp(status, "active") == 0)
1335 _bt_obex_client_started(path);
1336 else if (strcasecmp(status, "complete") == 0)
1337 _bt_obex_client_completed(path, TRUE);
1338 else if (strcasecmp(status, "error") == 0)
1339 _bt_obex_client_completed(path, FALSE);
1342 } else if (strcasecmp(property, "Transferred") == 0) {
1343 guint64 transferred = 0;
1344 g_variant_get(val, "t", &transferred);
1346 _bt_obex_client_progress(path, transferred);
1348 BT_DBG("property : [%s]", property);
1351 g_variant_unref(child);
1352 g_variant_unref(val);
1356 void _bt_opc_property_changed_event(GVariant *msg, char *path)
1358 char *interface_name = NULL;
1359 GVariant *value = NULL;
1360 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1361 BT_INFO("interface_name = %s", interface_name);
1362 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1363 __bt_opc_property_changed_event(value,
1366 BT_DBG("interface_name : [%s]", interface_name);
1368 g_variant_unref(value);
1372 void _bt_handle_input_event(GVariant *msg, const char *path)
1374 int result = BLUETOOTH_ERROR_NONE;
1375 gboolean property_flag = FALSE;
1376 GVariantIter value_iter;
1377 char *property = NULL;
1378 GVariant *child = NULL, *val = NULL;
1379 bt_remote_dev_info_t *remote_dev_info;
1380 GVariant *param = NULL;
1381 g_variant_iter_init(&value_iter, msg);
1382 while ((child = g_variant_iter_next_value(&value_iter))) {
1383 g_variant_get(child, "{sv}", &property, &val);
1385 ret_if(property == NULL);
1387 if (strcasecmp(property, "Connected") == 0) {
1388 int event = BLUETOOTH_EVENT_NONE;
1390 g_variant_get(val, "b", &property_flag);
1392 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1394 _bt_convert_device_path_to_address(path, address);
1396 event = (property_flag == TRUE) ?
1397 BLUETOOTH_HID_CONNECTED :
1398 BLUETOOTH_HID_DISCONNECTED;
1399 param = g_variant_new("(is)", result, address);
1400 _bt_send_event(BT_HID_EVENT, event,
1402 /* Check HID connection type (Keyboard or Mouse) and update the status */
1403 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1405 if (property_flag == TRUE) {
1406 hid_connected_device_count++;
1407 __bt_set_device_values(TRUE,
1408 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1410 hid_connected_device_count--;
1411 if (hid_connected_device_count == 0)
1412 __bt_set_device_values(FALSE,
1413 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1416 if (remote_dev_info != NULL) {
1417 BT_DBG("HID device class [%x]", remote_dev_info->class);
1418 if (remote_dev_info->class &
1419 BLUETOOTH_DEVICE_MINOR_CLASS_KEY_BOARD) {
1420 __bt_set_device_values(property_flag,
1421 VCONFKEY_BT_DEVICE_HID_KEYBOARD_CONNECTED);
1425 if (remote_dev_info->class &
1426 BLUETOOTH_DEVICE_MINOR_CLASS_POINTING_DEVICE) {
1427 __bt_set_device_values(property_flag,
1428 VCONFKEY_BT_DEVICE_HID_MOUSE_CONNECTED);
1430 _bt_free_device_info(remote_dev_info);
1435 g_variant_unref(val);
1436 g_variant_unref(child);
1440 void _bt_handle_network_server_event(GVariant *msg, const char *member)
1442 int result = BLUETOOTH_ERROR_NONE;
1443 char *address = NULL;
1444 char *device = NULL;
1445 GVariant *param = NULL;
1446 ret_if(member == NULL);
1447 if (strcasecmp(member, "PeerConnected") == 0) {
1448 g_variant_get(msg, "(ss)", &device, &address);
1450 __bt_set_device_values(TRUE,
1451 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1452 param = g_variant_new("(iss)", result, device, address);
1453 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
1457 nap_connected_device_count++;
1458 } else if (strcasecmp(member, "PeerDisconnected") == 0) {
1459 g_variant_get(msg, "(ss)", &device, &address);
1460 nap_connected_device_count--;
1461 if (nap_connected_device_count == 0)
1462 __bt_set_device_values(FALSE,
1463 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1464 param = g_variant_new("(iss)", result, device, address);
1465 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
1472 void _bt_handle_network_client_event(GVariant *msg,
1477 int result = BLUETOOTH_ERROR_NONE;
1478 gboolean property_flag = FALSE;
1479 char *property = NULL;
1480 GVariant *val = NULL;
1481 GVariantIter value_iter;
1482 GVariant *param = NULL;
1483 g_variant_iter_init(&value_iter, msg);
1484 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1485 if (strcasecmp(property, "Connected") == 0) {
1486 int event = BLUETOOTH_EVENT_NONE;
1489 g_variant_get(val, "b", &property_flag);
1490 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1492 _bt_convert_device_path_to_address(path, address);
1494 BT_DBG("property_flag %d", property_flag);
1495 if (property_flag == TRUE) {
1496 event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
1497 nap_connected_device_count++;
1498 __bt_set_device_values(TRUE,
1499 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1501 event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
1502 nap_connected_device_count--;
1503 if (nap_connected_device_count == 0)
1504 __bt_set_device_values(FALSE,
1505 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1507 param = g_variant_new("(is)", result, address);
1508 _bt_send_event(BT_NETWORK_EVENT, event,
1517 void __bt_gatt_char_property_changed_event(GVariant *msg,
1520 GVariantIter value_iter;
1521 char *property = NULL;
1522 char * char_handle = NULL;
1523 GVariant *val = NULL;
1524 int result = BLUETOOTH_ERROR_NONE;
1525 GVariant *param = NULL;
1526 g_variant_iter_init(&value_iter, msg);
1527 char_handle = g_strdup(path);
1528 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1529 BT_INFO("Property %s", property);
1531 ret_if(property == NULL);
1533 if (strcasecmp(property, "Notifying") == 0) {
1534 gboolean property_flag = FALSE;
1535 g_variant_get(val, "b", &property_flag);
1536 if (property_flag == TRUE)
1537 BT_DBG("notifying is enabled");
1539 BT_DBG("notifying is disabled");
1540 } else if (strcasecmp(property, "ChangedValue") == 0) {
1542 GByteArray *gp_byte_array = NULL;
1543 BT_INFO("Type '%s'\n", g_variant_get_type_string(val));
1546 gp_byte_array = g_byte_array_new();
1547 len = g_variant_get_size(val);
1548 BT_DBG("Len = %d", len);
1549 g_byte_array_append(gp_byte_array,
1550 (const guint8 *) g_variant_get_data(val), len);
1551 if (gp_byte_array->len != 0) {
1552 GVariant *byte_array = NULL;
1553 byte_array = g_variant_new_from_data(
1554 G_VARIANT_TYPE_BYTESTRING,
1555 gp_byte_array->data,
1558 param = g_variant_new("(is@ay)", result, char_handle,
1561 /* Send event only registered client */
1562 _bt_send_char_value_changed_event(param);
1564 g_byte_array_free(gp_byte_array, TRUE);
1568 g_free(char_handle);
1571 void _bt_handle_gatt_event(GVariant *msg, const char *member, const char *path)
1573 ret_if(path == NULL);
1575 if (strcasecmp(member, "GattValueChanged") == 0) {
1578 /*** Debug only ***/
1579 GVariant *value = NULL;
1581 char *buffer = NULL;
1583 g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
1584 value_len = g_variant_get_size(value);
1585 if (value_len > 0) {
1586 char buf[8 * 5 + 1] = { 0 };
1589 buffer = (char *)g_variant_get_data(value);
1590 to = value_len > (sizeof(buf) / 5) ? sizeof(buf) / 5 : value_len;
1592 for (i = 0; i < to; i++)
1593 snprintf(&buf[i * 5], 6, "0x%02x ", buffer[i]);
1595 BT_DBG("GATT Val[%d] %s", value_len, buf);
1597 g_variant_unref(value);
1601 /* Send event only registered client */
1602 _bt_send_char_value_changed_event(msg);
1607 void _bt_handle_device_event(GVariant *msg, const char *member, const char *path)
1610 int result = BLUETOOTH_ERROR_NONE;
1612 char *dev_name = NULL;
1613 const char *property = NULL;
1614 GVariant *param = NULL;
1615 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1616 ret_if(path == NULL);
1618 if (strcasecmp(member, "PropertyChanged") == 0) {
1620 g_variant_get(msg, "(s)", &property);
1622 ret_if(property == NULL);
1624 if (strcasecmp(property, "GattConnected") == 0) {
1625 gboolean connected = FALSE;
1627 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1628 ret_if(address == NULL);
1630 _bt_convert_device_path_to_address(path, address);
1631 g_variant_get(msg, "(b)", &connected);
1633 event = connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1634 BLUETOOTH_EVENT_GATT_DISCONNECTED;
1635 param = g_variant_new("(is)", result, address);
1636 _bt_send_event(BT_DEVICE_EVENT,
1640 } else if (strcasecmp(property, "Paired") == 0) {
1641 gboolean paired = FALSE;
1642 bt_remote_dev_info_t *remote_dev_info;
1643 g_variant_get(msg, "(b)", &paired);
1645 ret_if(paired == FALSE);
1647 /* BlueZ sends paired signal for each paired device */
1648 /* during activation, We should ignore this, otherwise*/
1649 /* application thinks that a new device got paired */
1650 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1651 BT_DBG("BT is not activated, so ignore this");
1655 if (_bt_is_device_creating() == TRUE) {
1656 BT_DBG("Try to Pair by me");
1660 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1661 ret_if(address == NULL);
1663 _bt_convert_device_path_to_address(path, address);
1665 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1666 if (remote_dev_info == NULL) {
1670 GVariant *uuids = NULL;
1671 GVariantBuilder *builder = NULL;
1673 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1674 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1675 g_variant_builder_add(builder, "s",
1676 remote_dev_info->uuids[i]);
1678 uuids = g_variant_new("as", builder);
1679 g_variant_builder_unref(builder);
1680 GVariant *manufacturer_data = NULL;
1681 manufacturer_data = g_variant_new_from_data(
1682 G_VARIANT_TYPE_BYTESTRING,
1683 remote_dev_info->manufacturer_data,
1684 remote_dev_info->manufacturer_data_len,
1686 param = g_variant_new("(isunsbub@asn@ay)", result,
1688 remote_dev_info->class,
1689 remote_dev_info->rssi,
1690 remote_dev_info->name,
1691 remote_dev_info->paired,
1692 remote_dev_info->connected,
1693 remote_dev_info->trust,
1695 remote_dev_info->manufacturer_data_len,
1697 _bt_send_event(BT_ADAPTER_EVENT,
1698 BLUETOOTH_EVENT_BONDING_FINISHED,
1700 _bt_free_device_info(remote_dev_info);
1703 } else if (strcasecmp(property, "UUIDs") == 0) {
1704 /* Once we get the updated uuid information after
1705 * reverse service search, update it to application */
1707 bt_remote_dev_info_t *remote_dev_info;
1709 ret_if(_bt_is_device_creating() == TRUE);
1711 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1712 ret_if(address == NULL);
1714 _bt_convert_device_path_to_address(path, address);
1716 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1717 if (remote_dev_info == NULL) {
1722 BT_DBG("UUID's count = %d", remote_dev_info->uuid_count);
1723 if (remote_dev_info->paired && remote_dev_info->uuid_count) {
1724 GVariant *uuids = NULL;
1725 GVariantBuilder *builder = NULL;
1727 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1728 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1729 g_variant_builder_add(builder, "s",
1730 remote_dev_info->uuids[i]);
1732 uuids = g_variant_new("as", builder);
1733 g_variant_builder_unref(builder);
1734 GVariant *manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1735 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1738 param = g_variant_new("(isunsbub@asn@ay)", result,
1739 address, remote_dev_info->class,
1740 remote_dev_info->rssi,
1741 remote_dev_info->name,
1742 remote_dev_info->paired,
1743 remote_dev_info->connected,
1744 remote_dev_info->trust,
1746 remote_dev_info->manufacturer_data_len,
1748 _bt_send_event(BT_ADAPTER_EVENT,
1749 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1753 _bt_free_device_info(remote_dev_info);
1756 } else if (strcasecmp(member, "DeviceConnected") == 0) {
1757 unsigned char addr_type = 0;
1759 g_variant_get(msg, "(y)", &addr_type);
1761 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1762 ret_if(address == NULL);
1764 _bt_convert_device_path_to_address(path, address);
1765 dev_name = _bt_get_bonded_device_name(address);
1767 _bt_convert_addr_string_to_secure_string(secure_address, address);
1768 BT_INFO("Address : %s Type : %d", secure_address, addr_type);
1769 BT_ERR_C("### Connected [%s] [%s]", !addr_type ? "BREDR" : "LE",
1770 !addr_type ? dev_name : secure_address);
1773 _bt_logging_connection(TRUE, addr_type);
1774 param = g_variant_new("(isy)", result, address, addr_type);
1775 /*Send event to application*/
1776 _bt_send_event(BT_DEVICE_EVENT,
1777 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1780 } else if (strcasecmp(member, "Disconnected") == 0) {
1781 unsigned char disc_reason = 0;
1782 unsigned char addr_type = 0;
1783 char *dev_name = NULL;
1784 gboolean sending = FALSE;
1786 g_variant_get(msg, "(yys)", &addr_type, &disc_reason, &dev_name);
1788 result = disc_reason;
1790 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1791 ret_if(address == NULL);
1793 _bt_convert_device_path_to_address(path, address);
1795 /* 0x00 BDADDR_BRDER
1796 0x01 BDADDR_LE_PUBLIC
1797 0x02 BDADDR_LE_RANDOM */
1798 _bt_convert_addr_string_to_secure_string(secure_address, address);
1799 BT_INFO("Address : %s Type : %d", secure_address, addr_type);
1800 BT_ERR_C("### Disconnected [%s] [%d : %s] [%s]", !addr_type ? "BREDR" : "LE",
1801 disc_reason, _bt_convert_disc_reason_to_string(disc_reason),
1802 !addr_type ? dev_name : secure_address);
1805 _bt_headset_set_local_connection(FALSE);
1806 _bt_logging_connection(FALSE, addr_type);
1809 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1811 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1813 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
1814 BT_ERR("vconf_get_int failed");
1816 BT_INFO("conn_state[0x%x], adapter_state [%d]",
1817 bt_device_state, _bt_adapter_get_status());
1819 if (disc_reason == BLUETOOTH_ERROR_CONNECTION_TIMEOUT) {
1820 _bt_audio_start_auto_connect(TRUE);
1821 } else if (bt_device_state &
1822 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED) {
1823 BT_INFO("Disconnected due to turning BT off. Skip a address");
1825 char *last_connected = NULL;
1826 last_connected = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1827 if (!g_strcmp0(address, last_connected))
1828 _bt_audio_set_auto_connect_device_addr("");
1830 free(last_connected);
1835 /*Check for any OPP transfer on the device and cancel
1838 _bt_obex_check_pending_transfer(address);
1839 _bt_opp_client_is_sending(&sending);
1840 if (sending == TRUE)
1841 _bt_opp_client_check_pending_transfer(address);
1843 param = g_variant_new("(isy)", result, address, addr_type);
1844 _bt_send_event(BT_DEVICE_EVENT,
1845 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1848 } else if (strcasecmp(member, "ProfileStateChanged") == 0) {
1850 char *profile_uuid = NULL;
1851 bt_headset_wait_t *wait_list;
1852 bluetooth_device_address_t bd_addr;
1854 g_variant_get(msg, "(si)", &profile_uuid, &state);
1856 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1857 ret_if(address == NULL);
1859 _bt_convert_device_path_to_address(path, address);
1860 _bt_convert_addr_string_to_type(bd_addr.addr, address);
1862 _bt_convert_addr_string_to_secure_string(secure_address, address);
1863 BT_DBG("Address: %s", secure_address);
1864 BT_DBG("Profile UUID: %s", profile_uuid);
1865 BT_DBG("State: %d", state);
1867 if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1868 (state == BT_PROFILE_STATE_CONNECTED)) {
1870 int event = BLUETOOTH_EVENT_AV_CONNECTED;
1871 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1872 bluetooth_device_address_t device_address;
1874 bt_headset_wait_t *wait_list;
1875 guint restricted = 0x0;
1877 __bt_set_device_values(TRUE,
1878 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1880 __bt_connection_manager_set_state(address, event);
1882 _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_HFP_HS, &restricted);
1884 if (_bt_headset_get_local_connection() == FALSE) {
1885 if (restricted == 0x0) /* not restricted*/
1886 _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
1888 /* Connection Started from local device therefore no need to
1889 * intiate connection for pending profile */
1890 _bt_headset_set_local_connection(FALSE);
1892 param = g_variant_new("(is)", result, address);
1893 _bt_send_event(BT_HEADSET_EVENT, event,
1895 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
1898 if (g_strcmp0(connected_address, address) != 0) {
1899 _bt_convert_addr_string_to_type(
1900 device_address.addr,
1902 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
1903 &device_address, NULL);
1907 _bt_add_headset_to_list(BT_AUDIO_A2DP,
1908 BT_STATE_CONNECTED, address);
1910 wait_list = _bt_get_audio_wait_data();
1911 if (wait_list != NULL &&
1912 (g_strcmp0(wait_list->address, address) == 0))
1913 _bt_rel_wait_data();
1915 } else if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1916 (state == BT_PROFILE_STATE_DISCONNECTED)) {
1918 int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
1920 if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
1922 g_free(profile_uuid);
1926 __bt_set_device_values(FALSE,
1927 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1929 __bt_connection_manager_set_state(address, event);
1930 param = g_variant_new("(is)", result, address);
1931 _bt_send_event(BT_HEADSET_EVENT, event,
1933 /* Remove data from the connected list */
1934 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
1935 wait_list = _bt_get_audio_wait_data();
1937 if (wait_list == NULL) {
1939 g_free(profile_uuid);
1943 if (((wait_list->type == BT_AUDIO_ALL) &&
1944 (wait_list->ag_flag == TRUE)) ||
1945 (wait_list->type == BT_AUDIO_A2DP) ||
1946 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
1947 bluetooth_device_address_t device_address;
1948 _bt_convert_addr_string_to_type(
1949 device_address.addr,
1950 wait_list->address);
1952 _bt_audio_connect(wait_list->req_id,
1957 } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
1959 if (state == BT_PROFILE_STATE_CONNECTED) {
1960 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
1961 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1962 bluetooth_device_address_t device_address;
1964 param = g_variant_new("(is)", result, address);
1965 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1967 connected = _bt_is_headset_type_connected(
1971 if (g_strcmp0(connected_address,
1973 _bt_convert_addr_string_to_type(
1974 device_address.addr,
1976 _bt_audio_disconnect(0,
1978 &device_address, NULL);
1981 BT_DBG("device Path: %s", path);
1982 _bt_add_headset_to_list(BT_AVRCP,
1983 BT_STATE_CONNECTED, address);
1984 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1985 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
1986 param = g_variant_new("(is)", result, address);
1987 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1989 /* Remove data from the connected list */
1990 _bt_remove_headset_from_list(BT_AVRCP, address);
1992 } else if (strcasecmp(profile_uuid, A2DP_SOURCE_UUID) == 0) {
1993 if (state == BT_PROFILE_STATE_CONNECTED) {
1994 int event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED;
1995 BT_INFO("A2DP Source is connected");
1996 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
1997 __bt_set_device_values(TRUE,
1998 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED);
2001 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
2002 _bt_audio_set_auto_connect_device_addr(address);
2003 _bt_audio_stop_auto_connect();
2005 _bt_add_headset_to_list(BT_AUDIO_A2DP_SOURCE,
2006 BT_STATE_CONNECTED, address);
2007 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
2008 g_variant_new("(is)", result, address));
2009 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2010 int event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED;
2011 BT_INFO("A2DP Source Disconnected");
2012 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
2013 __bt_set_device_values(FALSE,
2014 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED);
2016 _bt_remove_headset_from_list(BT_AUDIO_A2DP_SOURCE, address);
2017 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
2018 g_variant_new("(is)", result, address));
2020 wait_list = _bt_get_audio_wait_data();
2021 if (wait_list && wait_list->type == BT_AUDIO_A2DP_SOURCE) {
2022 bluetooth_device_address_t device_address;
2023 _bt_convert_addr_string_to_type(
2024 device_address.addr,
2025 wait_list->address);
2027 _bt_audio_connect(wait_list->req_id,
2031 /* Now free the wait list */
2032 _bt_rel_wait_data();
2035 } else if ((strcmp(profile_uuid, HID_UUID) == 0) &&
2036 ((state == BT_PROFILE_STATE_CONNECTED) ||
2037 (state == BT_PROFILE_STATE_DISCONNECTED))) {
2039 if (state == BT_PROFILE_STATE_CONNECTED)
2040 event = BLUETOOTH_HID_CONNECTED;
2042 event = BLUETOOTH_HID_DISCONNECTED;
2043 param = g_variant_new("(is)", result, address);
2044 _bt_send_event(BT_HID_EVENT, event,
2046 } else if (strcmp(profile_uuid, HID_DEVICE_UUID) == 0) {
2047 if (state == BT_PROFILE_STATE_CONNECTED) {
2049 event = BLUETOOTH_HID_DEVICE_CONNECTED;
2050 param = g_variant_new("(is)", result, address);
2051 _bt_send_event(BT_HID_DEVICE_EVENT, event,
2053 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2054 event = BLUETOOTH_HID_DEVICE_DISCONNECTED;
2055 param = g_variant_new("(is)", result, address);
2056 _bt_send_event(BT_HID_DEVICE_EVENT, event,
2061 g_free(profile_uuid);
2062 } else if (strcasecmp(member, "AdvReport") == 0) {
2064 bt_remote_le_dev_info_t *le_dev_info = NULL;
2065 char *buffer = NULL;
2067 bt_le_adv_info_t *adv_info = NULL;
2068 GVariant *value = NULL;
2069 ret_if(_bt_is_le_scanning() == FALSE);
2071 le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
2072 if (le_dev_info == NULL)
2075 g_variant_get(msg, "(syyii@ay)", &le_dev_info->address,
2076 &le_dev_info->addr_type,
2077 &le_dev_info->adv_type,
2079 &le_dev_info->adv_data_len,
2081 _bt_convert_device_path_to_address(path, le_dev_info->address);
2083 buffer_len = g_variant_get_size(value);
2085 buffer = (char *)g_variant_get_data(value);
2087 le_dev_info->adv_data = g_memdup(buffer, buffer_len);
2088 if (le_dev_info->adv_data == NULL &&
2089 le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {
2090 _bt_free_le_device_info(le_dev_info);
2091 g_variant_unref(value);
2095 if (_bt_get_le_scan_type() == BT_LE_PASSIVE_SCAN) {
2096 _bt_send_scan_result_event(le_dev_info, NULL);
2097 _bt_free_le_device_info(le_dev_info);
2098 g_variant_unref(value);
2102 if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) { /* ADV_IND */
2103 adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
2104 if (adv_info == NULL) {
2105 _bt_free_le_device_info(le_dev_info);
2106 g_variant_unref(value);
2110 adv_info->addr = g_strdup(le_dev_info->address);
2111 adv_info->addr_type = le_dev_info->addr_type;
2112 adv_info->rssi = le_dev_info->rssi;
2113 adv_info->data_len = le_dev_info->adv_data_len;
2114 adv_info->data = g_malloc0(le_dev_info->adv_data_len);
2115 if (adv_info->data) {
2116 memcpy(adv_info->data, le_dev_info->adv_data,
2117 le_dev_info->adv_data_len);
2120 if (__bt_add_adv_ind_info(adv_info) == 0) {
2121 adv_info->timer_id = g_timeout_add(1000,
2122 (GSourceFunc)__bt_adv_scan_req_timeout_cb, (void*)adv_info);
2124 } else { /* SCAN_RSP */
2125 adv_info = __bt_get_adv_ind_info(le_dev_info->address);
2127 _bt_send_scan_result_event(le_dev_info, adv_info);
2128 __bt_del_adv_ind_info(le_dev_info->address);
2131 _bt_free_le_device_info(le_dev_info);
2132 g_variant_unref(value);
2133 } else if (strcasecmp(member, "LEDataLengthChanged") == 0) {
2134 guint16 tx_octets = 0;
2135 guint16 tx_time = 0;
2136 guint16 rx_octets = 0;
2137 guint16 rx_time = 0;
2139 g_variant_get(msg, "(qqqq)",
2140 &tx_octets, &tx_time, &rx_octets, &rx_time);
2142 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2143 _bt_convert_device_path_to_address(path, address);
2145 param = g_variant_new("(isqqqq)", result, address, tx_octets, tx_time,
2146 rx_octets, rx_time);
2147 /* Send event to application */
2148 _bt_send_event(BT_DEVICE_EVENT,
2149 BLUETOOTH_EVENT_LE_DATA_LENGTH_CHANGED, param);
2151 } else if (strcasecmp(member, "IpspStateChanged") == 0) {
2152 gboolean connected = FALSE;
2153 char *ifname = NULL;
2155 g_variant_get(msg, "(bs)", &connected, &ifname);
2157 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
2158 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
2160 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2161 _bt_convert_device_path_to_address(path, address);
2163 BT_DBG("Ipsp BT Interface Name: %s", ifname);
2164 BT_DBG("address: %s", address);
2165 param = g_variant_new("(iss)", result, address, ifname);
2167 /* Send event to application */
2168 _bt_send_event(BT_DEVICE_EVENT,
2172 } else if (strcasecmp(member, "iBeaconReport") == 0) {
2173 bt_remote_ibeacon_dev_info_t *ibeacon_dev_info = NULL;
2175 ret_if(_bt_is_le_scanning() == FALSE);
2177 ibeacon_dev_info = g_malloc0(sizeof(bt_remote_ibeacon_dev_info_t));
2178 if (ibeacon_dev_info == NULL)
2181 g_variant_get(msg, "(syuusuuy)", &ibeacon_dev_info->address,
2182 &ibeacon_dev_info->addr_type,
2183 &ibeacon_dev_info->company_id,
2184 &ibeacon_dev_info->ibeacon_type,
2185 &ibeacon_dev_info->uuid,
2186 &ibeacon_dev_info->major_id,
2187 &ibeacon_dev_info->minor_id,
2188 &ibeacon_dev_info->measured_power);
2189 _bt_send_ibeacon_scan_result_event(ibeacon_dev_info);
2190 g_free(ibeacon_dev_info);
2194 void __bt_set_audio_values(gboolean connected, char *address)
2197 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
2199 /* Set the headset name */
2200 if (connected == TRUE)
2201 name = _bt_get_bonded_device_name(address);
2203 name = g_strdup("");
2205 if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME, name) != 0)
2206 BT_ERR("vconf_set_str failed");
2210 /* Set the headset state */
2211 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
2212 BT_ERR("vconf_get_int failed");
2214 #ifdef TIZEN_SUPPORT_DUAL_HF
2215 if ((connected == TRUE) &&
2216 (FALSE == __bt_is_companion_device(address))) {
2217 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2218 } else if ((bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) &&
2219 (FALSE == __bt_is_companion_device(address))) {
2220 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2223 if (connected == TRUE)
2224 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2225 else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED)
2226 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2229 if (vconf_set_int(VCONFKEY_BT_DEVICE,
2230 bt_device_state) != 0) {
2231 BT_ERR("vconf_set_int failed");
2235 void _bt_handle_headset_event(GVariant *msg, const char *path)
2237 int result = BLUETOOTH_ERROR_NONE;
2238 gboolean property_flag = FALSE;
2239 char *property = NULL;
2240 GVariant *value = NULL;
2241 GVariant *param = NULL;
2242 g_variant_get(msg, "(sv)", &property, &value);
2243 bluetooth_device_address_t bd_addr;
2245 ret_if(property == NULL);
2247 BT_DBG("Property = %s \n", property);
2248 /* We allow only 1 headset connection (HSP or HFP)*/
2249 if (strcasecmp(property, "Connected") == 0) {
2250 int event = BLUETOOTH_EVENT_NONE;
2251 bt_headset_wait_t *wait_list;
2253 guint restricted = 0x0;
2254 g_variant_get(value, "b", &property_flag);
2256 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2257 if (address == NULL)
2260 _bt_convert_device_path_to_address(path, address);
2261 _bt_convert_addr_string_to_type(bd_addr.addr, address);
2263 if (property_flag == TRUE) {
2264 event = BLUETOOTH_EVENT_AG_CONNECTED;
2265 _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_A2DP, &restricted);
2267 if (_bt_headset_get_local_connection() == FALSE) {
2268 if (restricted == 0x0) /* not restricted*/
2269 _bt_start_timer_for_connection(address, BT_AUDIO_A2DP);
2271 _bt_headset_set_local_connection(FALSE);
2275 event = BLUETOOTH_EVENT_AG_DISCONNECTED;
2277 previous_state = _bt_get_device_state_from_list(BT_AUDIO_HSP, address);
2278 if (previous_state == BT_STATE_DISCONNECTING)
2279 _bt_send_hf_local_term_event(address);
2281 /* Set the State machine here */
2282 __bt_connection_manager_set_state(address, event);
2283 __bt_set_audio_values(property_flag, address);
2284 param = g_variant_new("(is)", result, address);
2285 _bt_send_event(BT_HEADSET_EVENT, event,
2288 if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
2289 /* Remove data from the connected list */
2290 _bt_remove_headset_from_list(BT_AUDIO_HSP, address);
2292 wait_list = _bt_get_audio_wait_data();
2293 if (wait_list == NULL) {
2298 bluetooth_device_address_t device_address;
2300 _bt_set_audio_wait_data_flag(TRUE);
2302 _bt_convert_addr_string_to_type(device_address.addr,
2303 wait_list->address);
2304 _bt_audio_connect(wait_list->req_id, wait_list->type,
2305 &device_address, NULL);
2306 _bt_rel_wait_data();
2307 } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
2308 /* Add data to the connected list */
2309 _bt_add_headset_to_list(BT_AUDIO_HSP,
2310 BT_STATE_CONNECTED, address);
2312 wait_list = _bt_get_audio_wait_data();
2313 if (wait_list != NULL &&
2314 (g_strcmp0(wait_list->address, address) == 0))
2315 _bt_rel_wait_data();
2317 BT_INFO("Check A2DP pending connect");
2318 _bt_audio_check_pending_connect();
2321 } else if (strcasecmp(property, "State") == 0) {
2324 g_variant_get(value, "s", &state);
2326 /* This code assumes we support only 1 headset connection */
2327 /* Need to use the headset list, if we support multi-headsets */
2328 if (strcasecmp(state, "Playing") == 0) {
2329 BT_DBG("Playing: Sco Connected");
2330 } else if (strcasecmp(state, "connected") == 0 ||
2331 strcasecmp(state, "disconnected") == 0) {
2332 BT_DBG("connected/disconnected: Sco Disconnected");
2334 BT_ERR("Not handled state - %s", state);
2339 } else if (strcasecmp(property, "SpeakerGain") == 0) {
2343 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2345 _bt_convert_device_path_to_address(path, address);
2347 spkr_gain = g_variant_get_uint16(value);
2349 BT_DBG("spkr_gain: %d", spkr_gain);
2351 param = g_variant_new("(i&sq)", result, address, spkr_gain);
2352 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
2356 } else if (strcasecmp(property, "MicrophoneGain") == 0) {
2360 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2362 _bt_convert_device_path_to_address(path, address);
2364 mic_gain = g_variant_get_uint16(value);
2366 param = g_variant_new("(i&sq)", result, address, mic_gain);
2367 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_MIC_GAIN,
2374 g_variant_unref(value);
2377 void _bt_handle_sink_event(GVariant *msg, const char *path)
2379 GVariantIter value_iter;
2380 char *property = NULL;
2382 bt_headset_wait_t *wait_list;
2384 GVariant *child = NULL;
2385 GVariant *val = NULL;
2386 GVariant *param = NULL;
2387 g_variant_iter_init(&value_iter, msg);
2388 while ((child = g_variant_iter_next_value(&value_iter))) {
2390 g_variant_get(child, "{sv}", &property, &val);
2392 ret_if(property == NULL);
2394 BT_DBG("Property = %s \n", property);
2397 if (strcasecmp(property, "State") == 0) {
2398 int result = BLUETOOTH_ERROR_NONE;
2401 g_variant_get(val, "s", &value);
2402 BT_DBG("value: %s", value);
2404 if (g_strcmp0(value, "disconnected") == 0) {
2407 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2409 _bt_convert_device_path_to_address(path, address);
2411 __bt_set_device_values(FALSE,
2412 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2413 param = g_variant_new("(is)", result, address);
2414 _bt_send_event(BT_HEADSET_EVENT,
2415 BLUETOOTH_EVENT_AV_DISCONNECTED,
2418 /* Remove data from the connected list */
2419 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2420 wait_list = _bt_get_audio_wait_data();
2421 if (wait_list == NULL) {
2424 g_variant_unref(val);
2425 g_variant_unref(child);
2430 if (((wait_list->type == BT_AUDIO_ALL) &&
2431 (wait_list->ag_flag == TRUE)) ||
2432 (wait_list->type == BT_AUDIO_A2DP) ||
2433 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2434 bluetooth_device_address_t device_address;
2435 _bt_convert_addr_string_to_type(
2436 device_address.addr,
2437 wait_list->address);
2439 _bt_audio_connect(wait_list->req_id,
2445 } else if (strcasecmp(value, "Connected") == 0) {
2447 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2448 bluetooth_device_address_t device_address;
2451 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2453 _bt_convert_device_path_to_address(path, address);
2455 __bt_set_device_values(TRUE,
2456 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2457 param = g_variant_new("(is)", result, address);
2458 _bt_send_event(BT_HEADSET_EVENT,
2459 BLUETOOTH_EVENT_AV_CONNECTED,
2461 /* Check for existing Media device to disconnect */
2462 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
2465 /* Match connected device address */
2466 if (g_strcmp0(connected_address, address) != 0) {
2467 /* Convert BD adress from string type */
2468 _bt_convert_addr_string_to_type(
2469 device_address.addr,
2471 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2472 &device_address, NULL);
2476 /* Add data to the connected list */
2477 _bt_add_headset_to_list(BT_AUDIO_A2DP,
2478 BT_STATE_CONNECTED, address);
2485 g_variant_unref(val);
2486 g_variant_unref(child);
2490 void _bt_handle_agent_event(GVariant *msg, const char *member)
2492 int result = BLUETOOTH_ERROR_NONE;
2493 char *address = NULL;
2496 GVariant *param = NULL;
2497 ret_if(member == NULL);
2499 if (strcasecmp(member, "ObexAuthorize") == 0) {
2500 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2501 param = g_variant_new("(iss)", result, address, name);
2502 _bt_send_event(BT_OPP_SERVER_EVENT,
2503 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
2507 } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2508 bt_rfcomm_server_info_t *server_info;
2510 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2512 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2513 ret_if(server_info == NULL);
2514 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2515 param = g_variant_new("(isssn)", result, address, uuid, name,
2516 server_info->control_fd);
2517 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2518 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2526 static int __bt_get_object_path(GVariant *msg, char **path)
2528 g_variant_get(msg, "(o*)", path, NULL);
2530 return BLUETOOTH_ERROR_INTERNAL;
2532 return BLUETOOTH_ERROR_NONE;
2535 static void __bt_devices_list_free(void)
2537 bt_cache_info_t *cache_info;
2540 node = g_list_first(p_cache_list);
2542 while (node != NULL) {
2543 cache_info = (bt_cache_info_t *)node->data;
2544 p_cache_list = g_list_remove(p_cache_list, cache_info);
2545 __bt_free_cache_info(cache_info);
2547 node = g_list_next(node);
2551 static int __bt_parse_event(GVariant *msg)
2555 char *interface_name = NULL;
2556 GVariant *inner_iter = NULL;
2558 g_variant_iter_init(&iter, msg);
2560 while ((child = g_variant_iter_next_value(&iter))) {
2561 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
2562 if (g_strcmp0(interface_name,
2563 BT_DEVICE_INTERFACE) == 0) {
2564 g_variant_unref(inner_iter);
2565 g_variant_unref(child);
2566 return BT_DEVICE_EVENT;
2567 } else if (g_strcmp0(interface_name,
2568 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2569 g_variant_unref(inner_iter);
2570 g_variant_unref(child);
2571 return BT_MEDIA_TRANSFER_EVENT;
2572 } else if (g_strcmp0(interface_name,
2573 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2574 g_variant_unref(inner_iter);
2575 g_variant_unref(child);
2576 return BT_AVRCP_CONTROL_EVENT;
2578 g_variant_unref(inner_iter);
2579 g_variant_unref(child);
2585 static void __bt_manager_event_filter(GDBusConnection *connection,
2586 const gchar *sender_name,
2587 const gchar *object_path,
2588 const gchar *interface_name,
2589 const gchar *signal_name,
2590 GVariant *parameters,
2593 bt_event_type_t bt_event = 0x00;
2594 int result = BLUETOOTH_ERROR_NONE;
2596 char *obj_path = NULL;
2597 GVariant *param = NULL;
2598 if (signal_name == NULL)
2600 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2601 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2603 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
2604 #ifdef USB_BLUETOOTH
2605 BT_DBG("Enable Adapter");
2606 _bt_enable_adapter();
2608 _bt_handle_adapter_added();
2611 bt_event = __bt_parse_event(value);
2612 if (bt_event == BT_DEVICE_EVENT) {
2613 bt_cache_info_t *cache_info;
2614 bt_remote_dev_info_t *dev_info;
2616 ret_if(_bt_is_discovering() == FALSE &&
2617 _bt_is_le_scanning() == FALSE);
2619 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2620 ret_if(cache_info == NULL);
2622 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2623 if (dev_info == NULL) {
2624 __bt_free_cache_info(cache_info);
2628 cache_info->dev_info = dev_info;
2630 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2631 BT_ERR("Fail to parse the properies");
2632 __bt_free_cache_info(cache_info);
2633 g_variant_unref(value);
2637 if (dev_info->addr_type != BDADDR_BREDR) {
2638 /* Whenever emit the property changed from bluez,
2639 some property doesn't reach to bt-service.
2640 So LE device is handled as AdvReport signal */
2641 __bt_free_cache_info(cache_info);
2642 g_variant_unref(value);
2646 if (dev_info->name == NULL)
2647 /* If Remote device name is NULL or still RNR is not done
2648 * then display address as name.
2650 dev_info->name = g_strdup(dev_info->address);
2652 #ifdef TIZEN_DPM_ENABLE
2653 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
2655 bluetooth_device_class_t device_class;
2656 _bt_divide_device_class(&device_class, dev_info->class);
2657 BT_DBG("[%s]device_class.major_class : %d", dev_info->name, device_class.major_class);
2659 if (device_class.major_class ==
2660 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
2661 __bt_free_cache_info(cache_info);
2662 g_variant_unref(value);
2668 GVariant *uuids = NULL;
2669 GVariantBuilder *builder = NULL;
2671 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2672 for (i = 0; i < dev_info->uuid_count; i++) {
2673 g_variant_builder_add(builder, "s",
2674 dev_info->uuids[i]);
2676 uuids = g_variant_new("as", builder);
2677 g_variant_builder_unref(builder);
2678 GVariant *manufacturer_data = NULL;
2679 manufacturer_data = g_variant_new_from_data(
2680 G_VARIANT_TYPE_BYTESTRING,
2681 dev_info->manufacturer_data,
2682 dev_info->manufacturer_data_len,
2684 param = g_variant_new("(isunsbub@asn@ay)", result,
2690 dev_info->connected,
2693 dev_info->manufacturer_data_len,
2695 _bt_send_event(BT_ADAPTER_EVENT,
2696 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2698 p_cache_list = g_list_append(p_cache_list, cache_info);
2699 } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2700 BT_DBG("Device path : %s ", obj_path);
2701 _bt_set_control_device_path(obj_path);
2704 g_variant_unref(value);
2705 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2706 #ifdef USB_BLUETOOTH
2707 BT_DBG("InterfacesRemoved");
2708 _bt_handle_adapter_removed();
2710 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0)
2711 bt_event = BT_MEDIA_TRANSFER_EVENT;
2712 else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0)
2713 bt_event = BT_DEVICE_EVENT;
2714 else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0)
2715 bt_event = BT_AVRCP_CONTROL_EVENT;
2717 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2718 _bt_handle_adapter_event(parameters, signal_name);
2719 if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2720 BT_INFO("Object Path %s", obj_path);
2721 _bt_remove_control_device_path(obj_path);
2724 } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2727 char *previous = NULL;
2728 char *current = NULL;
2730 if (__bt_get_owner_info(parameters, &name, &previous, ¤t)) {
2731 BT_ERR("Fail to get the owner info");
2735 if (*current != '\0') {
2744 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2745 BT_DBG("Bluetoothd is terminated");
2746 if (_bt_adapter_get_status() == BT_ACTIVATED)
2749 _bt_handle_adapter_removed();
2750 __bt_devices_list_free();
2753 _bt_obex_server_check_allocation(&value);
2755 if (value == TRUE) {
2756 /* Check if the obex server was terminated abnormally */
2757 _bt_obex_server_check_termination(name);
2760 _bt_rfcomm_server_check_existence(&value);
2762 if (value == TRUE) {
2763 /* The obex server was terminated abnormally */
2764 _bt_rfcomm_server_check_termination(name);
2767 /* Stop advertising started by terminated process */
2768 _bt_stop_advertising_by_terminated_process(name);
2770 _bt_stop_le_scan(name);
2774 } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2775 const char *path = object_path;
2777 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2778 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2781 _bt_handle_property_changed_event(parameters, object_path);
2782 } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2783 _bt_handle_adapter_event(parameters, signal_name);
2784 } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2785 _bt_handle_input_event(parameters, object_path);
2786 } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2787 _bt_handle_network_server_event(parameters, signal_name);
2788 } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2789 _bt_handle_headset_event(parameters, object_path);
2790 } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2791 _bt_handle_sink_event(parameters, object_path);
2792 } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2793 _bt_handle_agent_event(parameters, signal_name);
2794 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2795 _bt_handle_device_event(parameters, signal_name, object_path);
2796 } else if (g_strcmp0(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
2797 _bt_handle_gatt_event(parameters, signal_name, object_path);
2803 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2806 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2807 char *interface_name = NULL;
2809 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2810 retv_if(interface_name == NULL, FALSE);
2812 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2813 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2821 static void __bt_obexd_event_filter(GDBusConnection *connection,
2822 const gchar *sender_name,
2823 const gchar *object_path,
2824 const gchar *interface_name,
2825 const gchar *signal_name,
2826 GVariant *parameters,
2829 const char *member = signal_name;
2830 char *obj_path = NULL;
2831 ret_if(member == NULL);
2833 if (strcasecmp(member, "InterfacesAdded") == 0) {
2834 if (__bt_get_object_path(parameters, &obj_path)) {
2835 BT_ERR("Fail to get the path");
2838 BT_INFO("object_path = [%s]", obj_path);
2840 /*Handle OPP_SERVER_CONNECTED_EVENT here */
2841 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2842 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2847 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2848 BT_DBG("Obex_Server_Session_Transfer connected");
2849 _bt_obex_transfer_connected(obj_path);
2852 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2853 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
2854 if (__bt_get_object_path(parameters, &obj_path)) {
2855 BT_ERR("Fail to get the path");
2858 BT_INFO("object_path = [%s]", obj_path);
2860 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2861 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2862 BT_DBG("Call PBAP Disconnected");
2863 _bt_obex_pbap_client_disconnect(obj_path);
2866 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2867 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2872 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2873 BT_DBG("Obex_Server_Session_Transfer disconnected %s",
2876 _bt_obex_transfer_disconnected(obj_path);
2879 } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
2880 const char *path = object_path;
2882 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
2883 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
2884 strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2885 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2886 BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
2890 _bt_handle_property_changed_event(parameters, path);
2896 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
2900 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2901 char *interface_name = NULL;
2903 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2905 retv_if(interface_name == NULL, FALSE);
2907 if (strcasecmp(interface_name,
2908 BT_OBEX_TRANSFER_INTERFACE) == 0) {
2919 static void __bt_opc_event_filter(GDBusConnection *connection,
2920 const gchar *sender_name,
2921 const gchar *object_path,
2922 const gchar *interface_name,
2923 const gchar *signal_name,
2924 GVariant *parameters,
2927 const char *member = signal_name;
2928 char *obj_path = NULL;
2929 if (strcasecmp(member, "InterfacesAdded") == 0) {
2930 BT_DBG("InterfacesAdded");
2931 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2933 if (__bt_get_object_path(parameters, &obj_path)) {
2934 BT_ERR("Fail to get the path");
2938 BT_DBG("object_path = %s", obj_path);
2940 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2941 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
2942 || strstr(obj_path, "transfer") == NULL) {
2945 } else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2946 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2947 BT_DBG("Going to call opc disconnected");
2948 _bt_opc_disconnected(obj_path);
2951 _bt_sending_files();
2953 } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
2954 char *path = (char *)object_path;
2955 BT_INFO("object_path %s", path);
2956 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2957 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2958 BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
2962 _bt_opc_property_changed_event(parameters, path);
2968 int _bt_opp_client_event_init(void)
2970 GError *error = NULL;
2972 if (opc_obexd_conn == NULL) {
2973 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
2975 if (!opc_obexd_conn) {
2977 BT_ERR("Unable to connect to dbus: %s", error->message);
2978 g_clear_error(&error);
2980 return BLUETOOTH_ERROR_INTERNAL;
2984 if (_bt_register_service_event(opc_obexd_conn,
2985 BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
2986 g_object_unref(opc_obexd_conn);
2987 opc_obexd_conn = NULL;
2988 return BLUETOOTH_ERROR_INTERNAL;
2991 return BLUETOOTH_ERROR_NONE;
2994 void _bt_opp_client_event_deinit(void)
2996 if (opc_obexd_conn) {
2997 _bt_unregister_service_event(opc_obexd_conn,
2998 BT_OPP_CLIENT_EVENT);
2999 g_object_unref(opc_obexd_conn);
3000 opc_obexd_conn = NULL;
3004 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
3010 static int subs_interface_added_id = -1;
3011 static int subs_interface_removed_id = -1;
3012 static int subs_name_owner_id = -1;
3013 static int subs_property_id = -1;
3014 static int subs_adapter_id = -1;
3015 static int subs_gatt_id = -1;
3018 if (subs_interface_added_id == -1) {
3019 subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3020 NULL, BT_MANAGER_INTERFACE,
3021 BT_INTERFACES_ADDED, NULL, NULL, 0,
3022 __bt_manager_event_filter,
3025 if (subs_interface_removed_id == -1) {
3026 subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3027 NULL, BT_MANAGER_INTERFACE,
3028 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3029 __bt_manager_event_filter,
3032 if (subs_name_owner_id == -1) {
3033 subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
3034 NULL, BT_FREEDESKTOP_INTERFACE,
3035 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
3036 __bt_manager_event_filter,
3039 if (subs_property_id == -1) {
3040 subs_property_id = g_dbus_connection_signal_subscribe(conn,
3041 NULL, BT_PROPERTIES_INTERFACE,
3042 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3043 __bt_manager_event_filter,
3046 if (subs_adapter_id == -1) {
3047 subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
3048 NULL, BT_ADAPTER_INTERFACE,
3049 NULL, NULL, NULL, 0,
3050 __bt_manager_event_filter,
3053 if (subs_gatt_id == -1) {
3054 subs_gatt_id = g_dbus_connection_signal_subscribe(conn,
3055 NULL, BT_GATT_CHAR_INTERFACE,
3056 NULL, NULL, NULL, 0,
3057 __bt_manager_event_filter,
3061 if (subs_interface_added_id != -1) {
3062 g_dbus_connection_signal_unsubscribe(conn,
3063 subs_interface_added_id);
3064 subs_interface_added_id = -1;
3066 if (subs_interface_removed_id != -1) {
3067 g_dbus_connection_signal_unsubscribe(conn,
3068 subs_interface_removed_id);
3069 subs_interface_removed_id = -1;
3071 if (subs_name_owner_id != -1) {
3072 g_dbus_connection_signal_unsubscribe(conn,
3073 subs_name_owner_id);
3074 subs_name_owner_id = -1;
3076 if (subs_property_id != -1) {
3077 g_dbus_connection_signal_unsubscribe(conn,
3079 subs_property_id = -1;
3081 if (subs_adapter_id != -1) {
3082 g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
3083 subs_adapter_id = -1;
3085 if (subs_gatt_id != -1) {
3086 g_dbus_connection_signal_unsubscribe(conn, subs_gatt_id);
3093 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
3099 static int subs_device_id = -1;
3102 if (subs_device_id == -1) {
3103 subs_device_id = g_dbus_connection_signal_subscribe(conn,
3104 NULL, BT_DEVICE_INTERFACE,
3105 NULL, NULL, NULL, 0,
3106 __bt_manager_event_filter,
3110 if (subs_device_id != -1) {
3111 g_dbus_connection_signal_unsubscribe(conn,
3113 subs_device_id = -1;
3119 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
3125 static int subs_input_id = -1;
3128 if (subs_input_id == -1) {
3129 subs_input_id = g_dbus_connection_signal_subscribe(conn,
3130 NULL, BT_INPUT_INTERFACE,
3131 NULL, NULL, NULL, 0,
3132 __bt_manager_event_filter,
3136 if (subs_input_id != -1) {
3137 g_dbus_connection_signal_unsubscribe(conn,
3145 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
3151 static int subs_serv_id = -1;
3152 static int subs_client_id = -1;
3155 if (subs_serv_id == -1) {
3156 subs_serv_id = g_dbus_connection_signal_subscribe(conn,
3157 NULL, BT_NETWORK_SERVER_INTERFACE,
3158 NULL, NULL, NULL, 0,
3159 __bt_manager_event_filter,
3162 if (subs_client_id == -1) {
3163 subs_client_id = g_dbus_connection_signal_subscribe(conn,
3164 NULL, BT_NETWORK_CLIENT_INTERFACE,
3165 NULL, NULL, NULL, 0,
3166 __bt_manager_event_filter,
3170 if (subs_serv_id != -1) {
3171 g_dbus_connection_signal_unsubscribe(conn,
3175 if (subs_client_id != -1) {
3176 g_dbus_connection_signal_unsubscribe(conn,
3178 subs_client_id = -1;
3184 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
3190 static int subs_headset_id = -1;
3191 static int subs_sink_id = -1;
3194 if (subs_headset_id == -1) {
3195 subs_headset_id = g_dbus_connection_signal_subscribe(conn,
3196 NULL, BT_HEADSET_INTERFACE,
3197 NULL, NULL, NULL, 0,
3198 __bt_manager_event_filter,
3201 if (subs_sink_id == -1) {
3202 subs_sink_id = g_dbus_connection_signal_subscribe(conn,
3203 NULL, BT_SINK_INTERFACE,
3204 NULL, NULL, NULL, 0,
3205 __bt_manager_event_filter,
3209 if (subs_headset_id != -1) {
3210 g_dbus_connection_signal_unsubscribe(conn,
3212 subs_headset_id = -1;
3214 if (subs_sink_id != -1) {
3215 g_dbus_connection_signal_unsubscribe(conn,
3223 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
3229 static int subs_opp_server_interface_added_id = -1;
3230 static int subs_opp_server_interface_removed_id = -1;
3231 static int subs_opp_server_property_id = -1;
3235 if (subs_opp_server_interface_added_id == -1) {
3236 subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3237 NULL, BT_MANAGER_INTERFACE,
3238 BT_INTERFACES_ADDED, NULL, NULL, 0,
3239 __bt_obexd_event_filter,
3242 if (subs_opp_server_interface_removed_id == -1) {
3243 subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3244 NULL, BT_MANAGER_INTERFACE,
3245 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3246 __bt_obexd_event_filter,
3249 if (subs_opp_server_property_id == -1) {
3250 subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
3251 NULL, BT_PROPERTIES_INTERFACE,
3252 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3253 __bt_obexd_event_filter,
3257 if (subs_opp_server_interface_added_id != -1) {
3258 g_dbus_connection_signal_unsubscribe(conn,
3259 subs_opp_server_interface_added_id);
3260 subs_opp_server_interface_added_id = -1;
3262 if (subs_opp_server_interface_removed_id != -1) {
3263 g_dbus_connection_signal_unsubscribe(conn,
3264 subs_opp_server_interface_removed_id);
3265 subs_opp_server_interface_removed_id = -1;
3267 if (subs_opp_server_property_id != -1) {
3268 g_dbus_connection_signal_unsubscribe(conn,
3269 subs_opp_server_property_id);
3270 subs_opp_server_property_id = -1;
3276 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
3282 static int subs_opp_client_interface_added_id = -1;
3283 static int subs_opp_client_interface_removed_id = -1;
3284 static int subs_opp_client_property_id = -1;
3288 if (subs_opp_client_interface_added_id == -1) {
3289 subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3290 NULL, BT_MANAGER_INTERFACE,
3291 BT_INTERFACES_ADDED, NULL, NULL, 0,
3292 __bt_opc_event_filter,
3295 if (subs_opp_client_interface_removed_id == -1) {
3296 subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3297 NULL, BT_MANAGER_INTERFACE,
3298 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3299 __bt_opc_event_filter,
3302 if (subs_opp_client_property_id == -1) {
3303 subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
3304 NULL, BT_PROPERTIES_INTERFACE,
3305 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3306 __bt_opc_event_filter,
3310 if (subs_opp_client_interface_added_id != -1) {
3311 g_dbus_connection_signal_unsubscribe(conn,
3312 subs_opp_client_interface_added_id);
3313 subs_opp_client_interface_added_id = -1;
3315 if (subs_opp_client_interface_removed_id != -1) {
3316 g_dbus_connection_signal_unsubscribe(conn,
3317 subs_opp_client_interface_removed_id);
3318 subs_opp_client_interface_removed_id = -1;
3320 if (subs_opp_client_property_id != -1) {
3321 g_dbus_connection_signal_unsubscribe(conn,
3322 subs_opp_client_property_id);
3323 subs_opp_client_property_id = -1;
3329 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
3335 static int subs_a2dp_source_id = -1;
3336 static int subs_a2dp_sink_id = -1;
3339 if (subs_a2dp_source_id == -1) {
3340 subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
3341 NULL, BT_A2DP_SOURCE_INTERFACE,
3342 NULL, NULL, NULL, 0,
3343 __bt_opc_event_filter,
3346 if (subs_a2dp_sink_id == -1) {
3347 subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
3348 NULL, BT_SINK_INTERFACE,
3349 NULL, NULL, NULL, 0,
3350 __bt_opc_event_filter,
3354 if (subs_a2dp_source_id != -1) {
3355 g_dbus_connection_signal_unsubscribe(conn,
3356 subs_a2dp_source_id);
3357 subs_a2dp_source_id = -1;
3359 if (subs_a2dp_sink_id != -1) {
3360 g_dbus_connection_signal_unsubscribe(conn,
3362 subs_a2dp_sink_id = -1;
3368 static void __bt_dump_event_filter(GDBusConnection *connection,
3369 const gchar *sender_name,
3370 const gchar *object_path,
3371 const gchar *interface_name,
3372 const gchar *signal_name,
3373 GVariant *parameters,
3379 int __bt_register_dump_subscribe_signal(GDBusConnection *conn,
3385 static int subs_source_id = -1;
3388 if (subs_source_id == -1) {
3389 subs_source_id = g_dbus_connection_signal_subscribe(conn,
3390 NULL, BT_DUMP_SERVICE_INTERFACE,
3391 BT_DUMP_SERVICE_SIGNAL, BT_DUMP_SERVICE_PATH, NULL, 0,
3392 __bt_dump_event_filter,
3396 if (subs_source_id != -1) {
3397 g_dbus_connection_signal_unsubscribe(conn,
3399 subs_source_id = -1;
3405 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
3409 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3411 switch (event_type) {
3412 case BT_MANAGER_EVENT:
3413 _bt_register_manager_subscribe_signal(g_conn, TRUE);
3415 case BT_DEVICE_EVENT:
3416 _bt_register_device_subscribe_signal(g_conn, TRUE);
3419 _bt_register_input_subscribe_signal(g_conn, TRUE);
3421 case BT_NETWORK_EVENT:
3422 _bt_register_network_subscribe_signal(g_conn, TRUE);
3424 case BT_HEADSET_EVENT:
3425 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3428 case BT_OPP_SERVER_EVENT:
3429 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3430 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3432 case BT_OPP_CLIENT_EVENT:
3433 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3434 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3436 case BT_A2DP_SOURCE_EVENT:
3437 BT_INFO("A2dp Source event");
3438 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3441 BT_ERR("Unknown event");
3442 return BLUETOOTH_ERROR_INTERNAL;
3445 return BLUETOOTH_ERROR_NONE;
3448 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3452 ret_if(g_conn == NULL);
3454 switch (event_type) {
3455 case BT_MANAGER_EVENT:
3456 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3457 _bt_register_device_subscribe_signal(g_conn, FALSE);
3458 _bt_register_input_subscribe_signal(g_conn, FALSE);
3459 _bt_register_network_subscribe_signal(g_conn, FALSE);
3460 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3462 case BT_OPP_SERVER_EVENT:
3463 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3465 case BT_OPP_CLIENT_EVENT:
3466 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3469 BT_ERR("Unknown event");
3476 static int __bt_init_manager_receiver(void)
3480 GError *error = NULL;
3482 if (manager_conn == NULL) {
3483 manager_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3484 if (error != NULL) {
3485 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3486 g_clear_error(&error);
3488 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3491 if (_bt_register_service_event(manager_conn,
3492 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3494 if (_bt_register_service_event(manager_conn,
3495 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3498 if (_bt_register_service_event(manager_conn,
3499 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3502 if (_bt_register_service_event(manager_conn,
3503 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3506 if (_bt_register_service_event(manager_conn,
3507 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3510 __bt_register_dump_subscribe_signal(manager_conn, TRUE);
3511 return BLUETOOTH_ERROR_NONE;
3514 g_object_unref(manager_conn);
3515 manager_conn = NULL;
3520 return BLUETOOTH_ERROR_INTERNAL;
3523 static int __bt_init_obexd_receiver(void)
3526 #ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3527 GError *error = NULL;
3529 if (obexd_conn == NULL) {
3530 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3531 if (error != NULL) {
3532 BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3533 g_clear_error(&error);
3535 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3538 if (_bt_register_service_event(obexd_conn,
3539 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3540 BT_ERR("Error while registering service event");
3541 g_object_unref(obexd_conn);
3543 return BLUETOOTH_ERROR_INTERNAL;
3548 return BLUETOOTH_ERROR_NONE;
3551 gboolean __bt_reinit_obexd_receiver(gpointer user_data)
3553 static int retry_cnt = 0;
3554 int result = BLUETOOTH_ERROR_NONE;
3558 result = __bt_init_obexd_receiver();
3559 if (result != BLUETOOTH_ERROR_NONE) {
3560 /* 20 ms * 50 = 10 seconds. During 10 seconds fail to initialize,
3561 then it is not the timing issue. Just can't use the session bus connection */
3562 if (retry_cnt > 100) {
3563 BT_ERR("Fail to init obexd receiver by 50 times.");
3565 session_reinit_timer = 0;
3569 BT_DBG("Retry to initialize the obexd receiver");
3574 session_reinit_timer = 0;
3581 /* To receive the event from bluez */
3582 int _bt_init_service_event_receiver(void)
3588 result = __bt_init_manager_receiver();
3589 retv_if(result != BLUETOOTH_ERROR_NONE, result);
3591 result = __bt_init_obexd_receiver();
3592 if (result != BLUETOOTH_ERROR_NONE) {
3593 BT_ERR("Fail to init obexd receiver");
3595 /* Try to re-initialize obexd receiver in the timer */
3596 if (session_reinit_timer > 0)
3597 g_source_remove(session_reinit_timer);
3599 session_reinit_timer = g_timeout_add(BT_SESSION_BUS_GET_TIMEOUT,
3600 (GSourceFunc)__bt_reinit_obexd_receiver, NULL);
3605 return BLUETOOTH_ERROR_NONE;
3608 void _bt_deinit_service_event_receiver(void)
3612 _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3614 _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3616 __bt_register_dump_subscribe_signal(manager_conn, FALSE);
3619 g_object_unref(manager_conn);
3620 manager_conn = NULL;
3624 g_object_unref(obexd_conn);
3629 g_source_remove(event_id);