2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <vconf-internal-bt-keys.h>
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
27 #include "bt-service-common.h"
28 #include "bt-service-event.h"
29 #include "bt-service-main.h"
30 #include "bt-service-adapter.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-device.h"
33 #include "bt-service-avrcp.h"
34 #include "bt-service-obex-server.h"
35 #include "bt-service-rfcomm-server.h"
36 #include "bt-service-audio.h"
37 #include "bt-service-agent.h"
38 #include "bt-service-pbap.h"
39 #include "bt-service-headset-connection.h"
40 #include "bt-service-avrcp-controller.h"
41 #include "bt-service-hid.h"
43 #include "bt-service-opp-client.h"
45 #ifdef TIZEN_DPM_ENABLE
46 #include "bt-service-dpm.h"
49 #define DBUS_TIMEOUT 20 * 1000 /* 20 Sec */
50 static GDBusConnection *manager_conn;
51 static GDBusConnection *obexd_conn;
52 static GDBusConnection *opc_obexd_conn;
54 static GList *p_cache_list = NULL;
56 static guint event_id;
57 static guint session_reinit_timer;
58 guint nap_connected_device_count = 0;
59 static guint hid_connected_device_count = 0;
60 static GList *p_adv_ind_list = NULL;
63 bt_remote_dev_info_t *dev_info;
67 * obexd connection type
75 OBEX_PCSUITE = (1 << 6),
76 OBEX_SYNCEVOLUTION = (1 << 7),
78 } bluetooth_obex_connection_type_t;
80 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path);
81 void _bt_opc_property_changed_event(GVariant *msg, char *path);
82 int _bt_register_service_event(GDBusConnection *g_conn, int event_type);
83 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type);
84 void _bt_opp_client_event_deinit(void);
85 void _bt_handle_network_client_event(GVariant *msg_iter,
87 void __bt_gatt_char_property_changed_event(GVariant *msg_iter,
90 static void __bt_free_bt_le_adv_info_t(gpointer data)
92 bt_le_adv_info_t *adv_info = data;
94 if (adv_info->timer_id)
95 g_source_remove(adv_info->timer_id);
96 adv_info->timer_id = 0;
98 g_free(adv_info->addr);
99 g_free(adv_info->data);
103 static bt_le_adv_info_t *__bt_get_adv_ind_info(char *addr)
105 retv_if(!addr, NULL);
106 bt_le_adv_info_t *adv_info = NULL;
107 GList *current = g_list_first((GList *)p_adv_ind_list);
108 while(current && current->data) {
109 adv_info = (bt_le_adv_info_t *)current->data;
110 if (adv_info && !g_strcmp0(adv_info->addr, addr))
112 current = g_list_next(current);
117 static int __bt_add_adv_ind_info(bt_le_adv_info_t *adv_info)
119 retv_if(!adv_info, -1);
120 if (__bt_get_adv_ind_info(adv_info->addr) != NULL) {
121 __bt_free_bt_le_adv_info_t(adv_info);
124 p_adv_ind_list = g_list_append(p_adv_ind_list, adv_info);
129 static void __bt_del_adv_ind_info(char *addr)
132 ret_if(!p_adv_ind_list);
133 bt_le_adv_info_t *adv_info = NULL;
134 GList *current = g_list_first((GList *)p_adv_ind_list);
135 while (current && current->data) {
136 adv_info = (bt_le_adv_info_t *)current->data;
137 if (adv_info && !g_strcmp0(adv_info->addr, addr)) {
138 p_adv_ind_list = g_list_remove(p_adv_ind_list, adv_info);
139 __bt_free_bt_le_adv_info_t(adv_info);
142 current = g_list_next(current);
146 static gboolean __bt_adv_scan_req_timeout_cb(gpointer user_data)
148 bt_remote_le_dev_info_t le_dev_info;
149 bt_le_adv_info_t *adv_info = (bt_le_adv_info_t*)user_data;
151 memset(&le_dev_info, 0x00, sizeof(bt_remote_le_dev_info_t));
153 le_dev_info.address = adv_info->addr;
154 le_dev_info.addr_type = adv_info->addr_type;
155 le_dev_info.rssi = adv_info->rssi;
156 le_dev_info.adv_data = adv_info->data;
157 le_dev_info.adv_data_len = 0;
158 adv_info->timer_id = 0;
160 _bt_send_scan_result_event(&le_dev_info, adv_info);
161 __bt_del_adv_ind_info(adv_info->addr);
166 static void __bt_free_cache_info(bt_cache_info_t *cache_info)
168 ret_if(cache_info == NULL);
169 _bt_free_device_info(cache_info->dev_info);
173 static gboolean __bt_parse_device_properties(GVariant *item,
174 bt_remote_dev_info_t *dev_info)
183 g_variant_iter_init(&iter, item);
184 while (g_variant_iter_loop(&iter, "{sv}", &key, &val)) {
185 if (strcasecmp(key, "Address") == 0) {
186 dev_info->address = g_variant_dup_string(val, &len);
187 } else if (strcasecmp(key, "Class") == 0) {
188 dev_info->class = g_variant_get_uint32(val);
189 } else if (strcasecmp(key, "name") == 0) {
190 if (dev_info->name == NULL)
191 dev_info->name = g_variant_dup_string(val, &len);
192 } else if (strcasecmp(key, "Connected") == 0) {
193 dev_info->connected = g_variant_get_byte(val);
194 } else if (strcasecmp(key, "paired") == 0) {
195 dev_info->paired = g_variant_get_boolean(val);
196 } else if (strcasecmp(key, "Trusted") == 0) {
197 dev_info->trust = g_variant_get_boolean(val);
198 } else if (strcasecmp(key, "RSSI") == 0) {
199 dev_info->rssi = g_variant_get_int16(val);
200 } else if (strcasecmp(key, "LastAddrType") == 0) {
201 dev_info->addr_type = g_variant_get_byte(val);
202 } else if (strcasecmp(key, "UUIDs") == 0) {
206 size = g_variant_get_size(val);
209 uuid_value = (char **)g_variant_get_strv(val, &size);
210 if (dev_info->uuids == NULL)
211 dev_info->uuids = g_malloc0(sizeof(char *) * size);
213 for (i = 0; uuid_value[i] != NULL; i++) {
214 dev_info->uuid_count++;
215 dev_info->uuids[i] = g_strdup(uuid_value[i]);
219 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
220 g_variant_get(val, "q", &dev_info->manufacturer_data_len);
221 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
222 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
223 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
226 if (dev_info->manufacturer_data_len == 0)
227 dev_info->manufacturer_data = g_strdup("");
228 } else if (strcasecmp(key, "ManufacturerData") == 0) {
231 GVariantIter *value_iter;
234 len = g_variant_get_size(val);
238 dev_info->manufacturer_data = g_malloc0(len);
240 g_variant_get(val, "ay", &value_iter);
241 while (g_variant_iter_loop(value_iter, "y", &m_value))
242 dev_info->manufacturer_data[i++] = m_value;
243 g_variant_iter_free(value_iter);
251 static gboolean __bt_parse_interface(GVariant *msg,
252 bt_remote_dev_info_t *dev_info)
255 GVariant *optional_param = NULL;
258 char *interface_name = NULL;
259 GVariant *inner_iter = NULL;
260 g_variant_get(msg, "(&o@a{sa{sv}})",
261 &path, &optional_param);
262 g_variant_iter_init(&iter, optional_param);
264 retv_if(optional_param == NULL, FALSE);
266 while ((child = g_variant_iter_next_value(&iter))) {
267 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
268 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
269 BT_DBG("Found a device: %s", path);
270 if (__bt_parse_device_properties(inner_iter,
271 dev_info) == FALSE) {
272 g_variant_unref(inner_iter);
273 g_variant_unref(child);
274 g_variant_unref(optional_param);
275 BT_ERR("Fail to parse the properies");
278 g_variant_unref(inner_iter);
279 g_variant_unref(child);
280 g_variant_unref(optional_param);
284 g_variant_unref(inner_iter);
285 g_variant_unref(child);
288 g_variant_unref(optional_param);
293 static int __bt_get_owner_info(GVariant *msg, char **name,
294 char **previous, char **current)
296 g_variant_get(msg, "(sss)", name, previous, current);
297 return BLUETOOTH_ERROR_NONE;
300 static int __bt_get_agent_signal_info(GVariant *msg, char **address,
301 char **name, char **uuid)
303 g_variant_get(msg, "(sss)", address, name, uuid);
304 return BLUETOOTH_ERROR_NONE;
307 void __bt_set_device_values(gboolean connected, int state)
309 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
311 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0) {
312 BT_ERR("vconf_get_int failed");
315 if (connected == TRUE)
316 bt_device_state |= state;
317 else if (bt_device_state & state)
318 bt_device_state ^= state;
320 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
321 BT_ERR("vconf_set_int failed");
325 gboolean _bt_discovery_finished_cb(gpointer user_data)
327 int result = BLUETOOTH_ERROR_NONE;
329 GVariant *param = NULL;
330 if (_bt_get_discovering_property(DISCOVERY_ROLE_BREDR) == FALSE) {
331 if (_bt_get_cancel_by_user() == TRUE) {
332 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
335 _bt_set_cancel_by_user(FALSE);
336 _bt_set_discovery_status(FALSE);
337 param = g_variant_new("(i)", result);
338 _bt_send_event(BT_ADAPTER_EVENT,
339 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
346 static gboolean __bt_le_discovery_finished_cb(gpointer user_data)
348 int result = BLUETOOTH_ERROR_NONE;
350 GVariant *param = NULL;
351 if (_bt_get_discovering_property(DISCOVERY_ROLE_LE) == FALSE) {
352 if (_bt_get_cancel_by_user() == TRUE) {
353 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
356 g_list_free_full(p_adv_ind_list, __bt_free_bt_le_adv_info_t);
357 p_adv_ind_list = NULL;
359 _bt_set_cancel_by_user(FALSE);
360 _bt_disable_all_scanner_status();
361 _bt_set_le_scan_status(FALSE);
362 param = g_variant_new("(i)", result);
363 _bt_send_event(BT_LE_ADAPTER_EVENT,
364 BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED,
371 void __bt_update_remote_cache_devinfo(const char *address, gboolean paired_status)
375 ret_if(address == NULL);
378 bt_cache_info_t *cache_info;
379 bt_remote_dev_info_t *dev_info;
381 node = g_list_first(p_cache_list);
383 while (node != NULL) {
384 cache_info = (bt_cache_info_t *)node->data;
386 if (cache_info == NULL) {
387 node = g_list_next(node);
391 dev_info = cache_info->dev_info;
392 if (strcasecmp(dev_info->address,
394 BT_DBG("Device Found");
395 if (paired_status == TRUE)
396 cache_info->dev_info->paired = TRUE;
398 cache_info->dev_info->paired = FALSE;
401 node = g_list_next(node);
406 static void __bt_device_remote_connected_properties(
407 bt_remote_dev_info_t *remote_dev_info,
408 char *address, gboolean connected)
410 int result = BLUETOOTH_ERROR_NONE;
412 GVariant *param = NULL;
415 if (remote_dev_info->uuid_count > 0) {
416 for (i = 0; i < remote_dev_info->uuid_count; i++) {
417 char *uuid = remote_dev_info->uuids[i];
418 if (strcasecmp(uuid, HID_UUID) == 0) {
419 int event = BLUETOOTH_EVENT_NONE;
421 event = (connected == TRUE) ?
422 BLUETOOTH_HID_CONNECTED :
423 BLUETOOTH_HID_DISCONNECTED;
424 param = g_variant_new("(is)", result,
426 _bt_send_event(BT_HID_EVENT, event,
436 void _bt_handle_adapter_event(GVariant *msg, const char *member)
440 int result = BLUETOOTH_ERROR_NONE;
441 GVariant *param = NULL;
442 ret_if(member == NULL);
444 if (strcasecmp(member, "DeviceCreated") == 0) {
445 char *object_path = NULL;
447 bt_remote_dev_info_t *remote_dev_info;
449 ret_if(_bt_is_device_creating() == FALSE);
451 /* Bonding from remote device */
452 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
454 g_variant_get(msg, "(&o)", &object_path);
455 _bt_convert_device_path_to_address((const char*)object_path, address);
457 remote_dev_info = _bt_get_remote_device_info_by_object_path(object_path);
458 if (remote_dev_info == NULL) {
463 _bt_free_device_info(remote_dev_info);
465 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
466 char *object_path = NULL;
468 bt_cache_info_t *cache_info;
469 bt_remote_dev_info_t *dev_info;
472 /* Bonding from remote device */
473 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
477 g_variant_get(msg, "(&o)", &object_path);
479 _bt_convert_device_path_to_address((const char *)object_path, address);
481 node = g_list_first(p_cache_list);
483 while (node != NULL) {
484 cache_info = (bt_cache_info_t *)node->data;
486 if (cache_info == NULL) {
487 node = g_list_next(node);
491 dev_info = cache_info->dev_info;
492 if (strcasecmp(dev_info->address,
494 p_cache_list = g_list_remove(p_cache_list,
496 __bt_free_cache_info(cache_info);
499 node = g_list_next(node);
502 } else if (strcasecmp(member, "AdvertisingEnabled") == 0) {
503 BT_DBG("Advertising Enabled");
507 gboolean status = FALSE;
509 g_variant_get(msg, "(ib)", &slot_id, &status);
511 BT_DBG("Advertising Enabled : slot_id [%d] status [%d]", slot_id, status);
513 /* Send event to application */
514 _bt_set_advertising_status(slot_id, status);
516 adv_handle = _bt_get_adv_slot_adv_handle(slot_id);
519 event = BLUETOOTH_EVENT_ADVERTISING_STARTED;
521 event = BLUETOOTH_EVENT_ADVERTISING_STOPPED;
522 param = g_variant_new("(ii)", result,
527 sender = _bt_get_adv_slot_owner(slot_id);
528 _bt_send_event_to_dest(sender, BT_ADAPTER_EVENT,
532 _bt_send_event(BT_ADAPTER_EVENT, event, param);
535 if (event == BLUETOOTH_EVENT_ADVERTISING_STOPPED)
536 _bt_unregister_adv_slot_owner(slot_id);
537 } else if (strcasecmp(member, "RssiEnabled") == 0) {
538 BT_DBG("RSSI Enabled");
539 gboolean status = FALSE;
540 char *address = NULL;
542 g_variant_get(msg, "(sib)", &address, &link_type, &status);
544 BT_DBG("RSSI Enabled [%s %d]", address, status);
545 param = g_variant_new("(isib)", result,
546 address, link_type, status);
547 _bt_send_event(BT_DEVICE_EVENT,
548 BLUETOOTH_EVENT_RSSI_ENABLED,
551 } else if (strcasecmp(member, "RssiAlert") == 0) {
552 BT_DBG("RSSI Alert");
556 char *address = NULL;
557 g_variant_get(msg, "(siii)", &address, &link_type, &alert_type, &rssi_dbm);
559 BT_DBG("RSSI Alert: [Address %s LinkType %d] [Type %d DBM %d]",
560 address, alert_type, rssi_dbm);
561 param = g_variant_new("(isiii)", result,
562 address, link_type, alert_type, rssi_dbm);
563 _bt_send_event(BT_DEVICE_EVENT,
564 BLUETOOTH_EVENT_RSSI_ALERT,
567 } else if (strcasecmp(member, "RawRssi") == 0) {
571 char *address = NULL;
572 g_variant_get(msg, "(sii)", &address, &link_type, &rssi_dbm);
574 BT_DBG("Raw RSSI: [Address %s] [Link Type %d][RSSI DBM %d]",
575 address, link_type, rssi_dbm);
576 param = g_variant_new("(isii)", result,
577 address, link_type, rssi_dbm);
578 _bt_send_event(BT_DEVICE_EVENT,
579 BLUETOOTH_EVENT_RAW_RSSI,
582 } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
583 BT_ERR_C("Hardware error received from BLUEZ");
584 _bt_recover_adapter();
585 } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
586 BT_ERR_C("Tx timeout error received from BLUEZ");
587 _bt_recover_adapter();
592 static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
594 GDBusProxy *adapter_proxy;
596 int result = BLUETOOTH_ERROR_NONE;
597 GVariantIter value_iter;
598 GVariant *val = NULL;
599 GVariant *ret = NULL;
601 char *property = NULL;
602 GVariant *param = NULL;
603 g_variant_iter_init(&value_iter, msg);
604 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
605 BT_INFO("Property %s", property);
607 if (strcasecmp(property, "Discovering") == 0) {
608 gboolean discovering = FALSE;
609 g_variant_get(val, "b", &discovering);
610 /* Send event to application */
611 BT_DBG("Discovering %d", discovering);
612 if (discovering == TRUE) {
613 _bt_set_discovery_status(TRUE);
614 param = g_variant_new("(i)", result);
615 _bt_send_event(BT_ADAPTER_EVENT,
616 BLUETOOTH_EVENT_DISCOVERY_STARTED,
619 ret_if(event_id > 0);
621 adapter_proxy = _bt_get_adapter_proxy();
622 ret_if(adapter_proxy == NULL);
624 /* Need to stop searching */
625 ret = g_dbus_proxy_call_sync(adapter_proxy, "StopDiscovery",
627 G_DBUS_CALL_FLAGS_NONE,
631 BT_ERR("Dbus Error : %s", err->message);
635 g_variant_unref(ret);
637 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
638 (GSourceFunc)_bt_discovery_finished_cb, NULL);
640 } else if (strcasecmp(property, "LEDiscovering") == 0) {
641 gboolean le_discovering = FALSE;
643 g_variant_get(val, "b", &le_discovering);
644 /* Send event to application */
645 if (le_discovering == TRUE) {
646 _bt_set_le_scan_status(TRUE);
647 param = g_variant_new("(i)", result);
648 _bt_send_event(BT_LE_ADAPTER_EVENT,
649 BLUETOOTH_EVENT_LE_DISCOVERY_STARTED,
652 ret_if(event_id > 0);
654 adapter_proxy = _bt_get_adapter_proxy();
655 ret_if(adapter_proxy == NULL);
657 /* Need to stop searching */
658 ret = g_dbus_proxy_call_sync(adapter_proxy, "StopLEDiscovery",
660 G_DBUS_CALL_FLAGS_NONE,
664 BT_ERR("Dbus Error %s", err->message);
668 g_variant_unref(ret);
670 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
671 (GSourceFunc)__bt_le_discovery_finished_cb, NULL);
673 } else if (strcasecmp(property, "Name") == 0) {
675 g_variant_get(val, "s", &name);
676 param = g_variant_new("(is)", result, name);
677 /* Send event to application */
678 _bt_send_event(BT_ADAPTER_EVENT,
679 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
682 } else if (strcasecmp(property, "Alias") == 0) {
684 g_variant_get(val, "s", &alias);
685 param = g_variant_new("(is)", result, alias);
686 /* Send event to application */
687 _bt_send_event(BT_ADAPTER_EVENT,
688 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
691 } else if (strcasecmp(property, "Discoverable") == 0) {
692 gboolean discoverable = FALSE;
694 g_variant_get(val, "b", &discoverable);
695 BT_DBG("discoverable %d", discoverable);
697 if (discoverable == FALSE) {
698 if (_bt_get_discoverable_timeout_property() > 0) {
700 adapter_proxy = _bt_get_adapter_properties_proxy();
701 ret_if(adapter_proxy == NULL);
702 ret = g_dbus_proxy_call_sync(adapter_proxy, "Set",
703 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
704 "DiscoverableTimeout",
705 g_variant_new("i", time)),
706 G_DBUS_CALL_FLAGS_NONE,
711 BT_ERR("StopLEDiscovery Failed: %s", err->message);
715 g_variant_unref(ret);
718 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
720 /* Send event to application */
721 BT_INFO("[Connectable]");
722 param = g_variant_new("(in)", result, mode);
723 _bt_send_event(BT_ADAPTER_EVENT,
724 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
727 _bt_get_discoverable_mode(&mode);
729 /* Event will be sent by "DiscoverableTimeout" signal */
730 if (mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
732 g_variant_unref(val);
736 /* Send event to application */
737 BT_INFO("[General Discoverable]");
738 param = g_variant_new("(in)", result, mode);
739 _bt_send_event(BT_ADAPTER_EVENT,
740 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
743 } else if (strcasecmp(property, "DiscoverableTimeout") == 0) {
744 _bt_get_discoverable_mode(&mode);
746 /* Event was already sent by "Discoverable" signal */
747 if (mode == BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE ||
748 mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
750 g_variant_unref(val);
754 /* Send event to application */
755 BT_INFO("[Limited Discoverable (Timeout %u secs)]",
756 _bt_get_discoverable_timeout_property());
757 param = g_variant_new("(in)", result, mode);
758 _bt_send_event(BT_ADAPTER_EVENT,
759 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
761 } else if (strcasecmp(property, "Powered") == 0) {
762 /* TODO: Need to check this operation!! */
763 gboolean powered = FALSE;
766 g_variant_get(val, "b", &powered);
767 BT_DBG("Powered = %d", powered);
768 if (powered == FALSE) {
770 _bt_handle_adapter_removed();
772 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_state) == 0 &&
773 bt_state != VCONFKEY_BT_STATUS_OFF) {
774 _bt_disable_adapter();
777 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_state) == 0 &&
778 bt_state != VCONFKEY_BT_LE_STATUS_OFF) {
779 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
783 _bt_handle_adapter_added();
786 } else if (strcasecmp(property, "Connectable") == 0) {
787 gboolean connectable = FALSE;
789 g_variant_get(val, "b", &connectable);
791 BT_DBG("Connectable property is changed : %d", connectable);
792 param = g_variant_new("(b)", connectable);
793 _bt_send_event(BT_ADAPTER_EVENT,
794 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
797 if (_bt_adapter_get_status() == BT_DEACTIVATING &&
798 _bt_adapter_get_le_status() == BT_LE_ACTIVATED &&
800 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
802 } else if (strcasecmp(property, "SupportedLEFeatures") == 0) {
805 GVariantIter *iter = NULL;
806 g_variant_get(val, "as", &iter);
807 while (g_variant_iter_loop(iter, "s", &name)) {
808 BT_DBG("name = %s", name);
809 g_variant_iter_loop(iter, "s", &value);
810 BT_DBG("Value = %s", value);
811 if (FALSE == _bt_update_le_feature_support(name, value))
812 BT_INFO("Fail to update LE feature info");
815 g_variant_iter_free(iter);
816 } else if (strcasecmp(property, "IpspInitStateChanged") == 0) {
817 gboolean ipsp_initialized = FALSE;
819 g_variant_get(val, "b", &ipsp_initialized);
820 BT_DBG("IPSP init state changed: %d", ipsp_initialized);
821 param = g_variant_new("(b)", ipsp_initialized);
823 /* Send event to application */
824 _bt_send_event(BT_ADAPTER_EVENT,
825 BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,
828 BT_DBG("property : [%s]", property);
833 static void __bt_obex_property_changed_event(GVariant *msg, const char *path)
837 GVariantIter value_iter;
838 GVariant *child = NULL, *val = NULL;
839 char *property = NULL;
840 g_variant_iter_init(&value_iter, msg);
841 while ((child = g_variant_iter_next_value(&value_iter))) {
842 g_variant_get(child, "{sv}", &property, &val);
844 ret_if(property == NULL);
846 BT_DBG("property :%s", property);
848 if (strcasecmp(property, "Status") == 0) {
850 g_variant_get(val, "s", &status);
852 if (strcasecmp(status, "active") == 0) {
853 _bt_obex_transfer_started(path);
854 } else if (strcasecmp(status, "complete") == 0) {
855 _bt_obex_transfer_completed(path, TRUE);
856 _bt_pbap_obex_transfer_completed(path, TRUE);
857 } else if (strcasecmp(status, "error") == 0) {
858 _bt_obex_transfer_completed(path, FALSE);
859 _bt_pbap_obex_transfer_completed(path, FALSE);
862 } else if (strcasecmp(property, "Transferred") == 0) {
863 guint64 transferred = 0;
864 /* As Transferred is expected guint64 so change int to guint64 and
865 * eariler transferred is static because of it can overwrite data
866 * on present on opc_obex_conn or obexd_conn as these are
867 * static memory are in sequential */
868 g_variant_get(val, "t", &transferred);
870 _bt_obex_transfer_progress(path, transferred);
873 g_variant_unref(val);
874 g_variant_unref(child);
879 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
884 int result = BLUETOOTH_ERROR_NONE;
885 GVariantIter value_iter;
887 char *property = NULL;
889 GVariant *param = NULL;
890 bt_remote_dev_info_t *remote_dev_info;
891 g_variant_iter_init(&value_iter, msg);
892 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
893 BT_DBG("Property %s", property);
894 if (strcasecmp(property, "Connected") == 0) {
897 g_variant_get(val, "i", &connected);
899 event = (connected != BLUETOOTH_CONNECTED_LINK_NONE) ?
900 BLUETOOTH_EVENT_DEVICE_CONNECTED :
901 BLUETOOTH_EVENT_DEVICE_DISCONNECTED;
903 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
905 _bt_convert_device_path_to_address(path, address);
907 BT_DBG("connected: %d", connected);
908 BT_DBG("address: %s", address);
910 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
912 if (remote_dev_info != NULL) {
913 __bt_device_remote_connected_properties(
914 remote_dev_info, address,
915 connected != BLUETOOTH_CONNECTED_LINK_NONE ?
917 _bt_free_device_info(remote_dev_info);
919 param = g_variant_new("(is)", result, address);
920 /* Send event to application */
921 _bt_send_event(BT_DEVICE_EVENT,
925 } else if (strcasecmp(property, "RSSI") == 0) {
926 bt_remote_dev_info_t *remote_dev_info;
928 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
930 _bt_convert_device_path_to_address(path, address);
931 BT_DBG("address: %s", address);
933 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
934 if (remote_dev_info == NULL) {
936 g_variant_unref(val);
940 BT_DBG("Address type %d", remote_dev_info->addr_type);
942 if (remote_dev_info->addr_type == 0) {
943 BT_DBG("Name %s", remote_dev_info->name);
945 #ifdef TIZEN_DPM_ENABLE
946 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
948 bluetooth_device_class_t device_class;
949 _bt_divide_device_class(&device_class, remote_dev_info->class);
951 if (device_class.major_class ==
952 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
953 _bt_free_device_info(remote_dev_info);
955 g_variant_unref(val);
962 GVariant *uuids = NULL;
963 GVariantBuilder *builder = NULL;
965 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
966 for (i = 0; i < remote_dev_info->uuid_count; i++) {
967 g_variant_builder_add(builder, "s",
968 remote_dev_info->uuids[i]);
970 uuids = g_variant_new("as", builder);
971 g_variant_builder_unref(builder);
972 GVariant *manufacturer_data = NULL;
973 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
974 remote_dev_info->manufacturer_data,
975 remote_dev_info->manufacturer_data_len,
978 param = g_variant_new("(isunsbub@asn@ay)", result,
979 remote_dev_info->address,
980 remote_dev_info->class,
981 remote_dev_info->rssi,
982 remote_dev_info->name,
983 remote_dev_info->paired,
984 remote_dev_info->connected,
985 remote_dev_info->trust,
987 remote_dev_info->manufacturer_data_len,
990 _bt_send_event(BT_ADAPTER_EVENT,
991 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
995 _bt_free_device_info(remote_dev_info);
996 } else if (strcasecmp(property, "GattConnected") == 0) {
997 gboolean gatt_connected = FALSE;
999 g_variant_get(val, "b", &gatt_connected);
1001 event = gatt_connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1002 BLUETOOTH_EVENT_GATT_DISCONNECTED;
1004 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1006 _bt_convert_device_path_to_address(path, address);
1008 BT_DBG("gatt_connected: %d", gatt_connected);
1009 BT_DBG("address: %s", address);
1010 param = g_variant_new("(is)", result, address);
1011 /* Send event to application */
1012 _bt_send_event(BT_DEVICE_EVENT,
1016 } else if (strcasecmp(property, "Paired") == 0) {
1017 gboolean paired = FALSE;
1018 bt_remote_dev_info_t *remote_dev_info;
1019 g_variant_get(val, "b", &paired);
1020 _bt_agent_set_canceled(FALSE);
1021 /* BlueZ sends paired signal for each paired device */
1022 /* during activation, We should ignore this, otherwise*/
1023 /* application thinks that a new device got paired */
1024 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1025 BT_DBG("BT is not activated, so ignore this");
1027 g_variant_unref(val);
1031 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1033 _bt_convert_device_path_to_address(path, address);
1035 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1036 if (remote_dev_info == NULL) {
1038 g_variant_unref(val);
1043 if (paired == FALSE) {
1044 BT_INFO("Unpaired: %s", address);
1045 __bt_update_remote_cache_devinfo(address, FALSE);
1046 param = g_variant_new("(is)", result, address);
1047 _bt_send_event(BT_ADAPTER_EVENT,
1048 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
1050 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1052 char *last_connected = NULL;
1053 last_connected = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1054 if (!g_strcmp0(address, last_connected))
1055 _bt_audio_set_auto_connect_device_addr("");
1057 free(last_connected);
1061 char secure_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
1063 _bt_convert_addr_string_to_secure_string(secure_addr, address);
1064 BT_INFO("### Paired: %s", secure_addr);
1065 __bt_update_remote_cache_devinfo(address, TRUE);
1067 if (_bt_is_device_creating() == TRUE) {
1068 BT_DBG("Try to Pair by me");
1069 _bt_free_device_info(remote_dev_info);
1072 g_variant_unref(val);
1075 GVariant *uuids = NULL;
1076 GVariantBuilder *builder = NULL;
1078 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1079 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1080 g_variant_builder_add(builder, "s",
1081 remote_dev_info->uuids[i]);
1083 uuids = g_variant_new("as", builder);
1084 g_variant_builder_unref(builder);
1085 GVariant *manufacturer_data = NULL;
1086 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1087 remote_dev_info->manufacturer_data,
1088 remote_dev_info->manufacturer_data_len,
1092 param = g_variant_new("(isunsbub@asn@ay)", result,
1093 address, remote_dev_info->class,
1094 remote_dev_info->rssi,
1095 remote_dev_info->name,
1096 remote_dev_info->paired,
1097 remote_dev_info->connected,
1098 remote_dev_info->trust,
1100 remote_dev_info->manufacturer_data_len,
1102 _bt_send_event(BT_ADAPTER_EVENT,
1103 BLUETOOTH_EVENT_BONDING_FINISHED,
1106 _bt_free_device_info(remote_dev_info);
1108 } else if (strcasecmp(property, "LegacyPaired") == 0) {
1109 gboolean paired = FALSE;
1110 bt_remote_dev_info_t *remote_dev_info;
1111 unsigned char auth_info[5] = {0, };
1113 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1114 BT_DBG("BT is not activated, so ignore this");
1116 g_variant_unref(val);
1120 g_variant_get(val, "b", &paired);
1121 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1122 BT_DBG("LegacyPaired: %d", paired);
1123 _bt_convert_device_path_to_address(path, address);
1125 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1126 if (remote_dev_info == NULL) {
1129 g_variant_unref(val);
1132 if (remote_dev_info->is_alias_set == FALSE) {
1133 /*minimum Size of the samsung specific manufacturer data is greater than 30 */
1134 if ((remote_dev_info->manufacturer_data_len > 30) &&
1135 (remote_dev_info->manufacturer_data[0] == 0x00) &&
1136 (remote_dev_info->manufacturer_data[1] == 0x75)) {
1138 /* 2 samsung (0x00 0x75) + 1 (control and version) + 1 (service ID) +
1139 1 (discovery version) + 1 (associated service ID)
1140 2 (Proxamity and locality) + 2 (Device type and icon) = 10 */
1142 memcpy(auth_info, &(remote_dev_info->manufacturer_data[10]), 5);
1146 BT_DBG("LegacyPairing Failed with %s. Show Error Popup", remote_dev_info->name);
1147 _bt_launch_system_popup(BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE,
1148 remote_dev_info->name, auth_info, NULL, NULL, NULL);
1150 _bt_free_device_info(remote_dev_info);
1152 } else if (strcasecmp(property, "Trusted") == 0) {
1153 gboolean trusted = FALSE;
1155 g_variant_get(val, "b", &trusted);
1157 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1158 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1160 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1162 _bt_convert_device_path_to_address(path, address);
1164 BT_DBG("trusted: %d", trusted);
1165 BT_DBG("address: %s", address);
1166 param = g_variant_new("(is)", result, address);
1167 /* Send event to application */
1168 _bt_send_event(BT_DEVICE_EVENT,
1172 } else if (strcasecmp(property, "TrustedProfiles") == 0) {
1175 g_variant_get(val, "u", &trusted);
1177 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1178 _bt_convert_device_path_to_address(path, address);
1180 BT_DBG("Address: %s, TrustedProfiles: %d", address, trusted);
1181 _bt_send_event(BT_DEVICE_EVENT,
1182 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1183 g_variant_new("(isi)", result, address, trusted));
1185 } else if (strcasecmp(property, "IpspConnected") == 0) {
1186 gboolean connected = FALSE;
1188 g_variant_get(val, "b", &connected);
1191 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
1192 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
1194 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1196 _bt_convert_device_path_to_address(path, address);
1198 BT_DBG("Ipspconnected: %d", connected);
1199 BT_DBG("address: %s", address);
1200 param = g_variant_new("(is)", result, address);
1202 /* Send event to application */
1203 _bt_send_event(BT_DEVICE_EVENT,
1207 } else if (strcasecmp(property, "IpspBtInterfaceInfo") == 0) {
1208 char *ifname = NULL;
1210 g_variant_get(val, "s", &ifname);
1212 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1214 _bt_convert_device_path_to_address(path, address);
1216 BT_DBG("Ipsp BT Interface Name: %s", ifname);
1217 BT_DBG("address: %s", address);
1218 param = g_variant_new("(iss)", result, address, ifname);
1220 /* Send event to application */
1221 _bt_send_event(BT_DEVICE_EVENT,
1222 BLUETOOTH_EVENT_IPSP_INTERFACE_INFO,
1230 static void __bt_media_control_changed_event(GVariant *msg, const char *path)
1233 int result = BLUETOOTH_ERROR_NONE;
1234 GVariantIter value_iter;
1235 char *property = NULL;
1237 GVariant *val = NULL;
1238 GVariant *child = NULL;
1239 bt_remote_dev_info_t *remote_dev_info;
1240 GVariant *param = NULL;
1241 g_variant_iter_init(&value_iter, msg);
1242 while ((child = g_variant_iter_next_value(&value_iter))) {
1243 g_variant_get(child, "{sv}", &property, &val);
1244 BT_INFO("Property %s", property);
1245 if (strcasecmp(property, "Connected") == 0) {
1246 gboolean connected = FALSE;
1248 g_variant_get(val, "b", &connected);
1250 event = connected ? BLUETOOTH_EVENT_AVRCP_CONNECTED :
1251 BLUETOOTH_EVENT_AVRCP_DISCONNECTED;
1253 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1255 _bt_convert_device_path_to_address(path, address);
1257 BT_DBG("connected: %d", connected);
1258 BT_DBG("address: %s", address);
1260 remote_dev_info = _bt_get_remote_device_info(address);
1262 if (remote_dev_info != NULL) {
1263 __bt_device_remote_connected_properties(
1264 remote_dev_info, address, connected);
1265 _bt_free_device_info(remote_dev_info);
1267 param = g_variant_new("(is)", result, address);
1268 /* Send event to application */
1269 _bt_send_event(BT_AVRCP_EVENT,
1275 g_variant_unref(child);
1276 g_variant_unref(val);
1281 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
1283 char *interface_name = NULL;
1284 GVariant *val = NULL;
1286 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &val, NULL);
1288 if (strcasecmp(interface_name, BT_ADAPTER_INTERFACE) == 0) {
1289 __bt_adapter_property_changed_event(val,
1291 } else if (strcasecmp(interface_name, BT_DEVICE_INTERFACE) == 0) {
1292 __bt_device_property_changed_event(val, object_path);
1293 } else if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1294 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
1295 __bt_obex_property_changed_event(val,
1297 } else if (strcasecmp(interface_name, BT_MEDIA_CONTROL_INTERFACE) == 0) {
1298 __bt_media_control_changed_event(val,
1300 } else if (strcasecmp(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
1301 _bt_handle_avrcp_control_event(val,
1303 } else if (strcasecmp(interface_name, BT_NETWORK_CLIENT_INTERFACE) == 0) {
1304 BT_DBG("BT_NETWORK_CLIENT_INTERFACE");
1305 _bt_handle_network_client_event(val,
1307 } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
1308 __bt_gatt_char_property_changed_event(val,
1311 g_variant_unref(val);
1314 void __bt_opc_property_changed_event(GVariant *msg,
1317 GVariantIter value_iter;
1318 char *property = NULL;
1319 GVariant *val = NULL;
1320 GVariant *child = NULL;
1322 g_variant_iter_init(&value_iter, msg);
1323 while ((child = g_variant_iter_next_value(&value_iter))) {
1324 g_variant_get(child, "{sv}", &property, &val);
1325 ret_if(property == NULL);
1327 if (strcasecmp(property, "Status") == 0) {
1328 char *status = NULL;
1329 g_variant_get(val, "s", &status);
1330 BT_DBG("Status is %s", status);
1332 if (strcasecmp(status, "active") == 0) {
1333 _bt_obex_client_started(path);
1334 } else if (strcasecmp(status, "complete") == 0) {
1335 _bt_obex_client_completed(path, TRUE);
1336 } else if (strcasecmp(status, "error") == 0) {
1337 _bt_obex_client_completed(path, FALSE);
1340 } else if (strcasecmp(property, "Transferred") == 0) {
1341 guint64 transferred = 0;
1342 g_variant_get(val, "t", &transferred);
1344 _bt_obex_client_progress(path, transferred);
1346 BT_DBG("property : [%s]", property);
1349 g_variant_unref(child);
1350 g_variant_unref(val);
1354 void _bt_opc_property_changed_event(GVariant *msg, char *path)
1356 char *interface_name = NULL;
1357 GVariant *value = NULL;
1358 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1359 BT_INFO("interface_name = %s", interface_name);
1360 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1361 __bt_opc_property_changed_event(value,
1364 BT_DBG("interface_name : [%s]", interface_name);
1366 g_variant_unref(value);
1370 void _bt_handle_input_event(GVariant *msg, const char *path)
1372 int result = BLUETOOTH_ERROR_NONE;
1373 gboolean property_flag = FALSE;
1374 GVariantIter value_iter;
1375 char *property = NULL;
1376 GVariant *child = NULL, *val = NULL;
1377 bt_remote_dev_info_t *remote_dev_info;
1378 GVariant *param = NULL;
1379 g_variant_iter_init(&value_iter, msg);
1380 while ((child = g_variant_iter_next_value(&value_iter))) {
1381 g_variant_get(child, "{sv}", &property, &val);
1383 ret_if(property == NULL);
1385 if (strcasecmp(property, "Connected") == 0) {
1386 int event = BLUETOOTH_EVENT_NONE;
1388 g_variant_get(val, "b", &property_flag);
1390 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1392 _bt_convert_device_path_to_address(path, address);
1394 event = (property_flag == TRUE) ?
1395 BLUETOOTH_HID_CONNECTED :
1396 BLUETOOTH_HID_DISCONNECTED;
1397 param = g_variant_new("(is)", result, address);
1398 _bt_send_event(BT_HID_EVENT, event,
1400 /* Check HID connection type (Keyboard or Mouse) and update the status */
1401 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1403 if (property_flag == TRUE) {
1404 hid_connected_device_count++;
1405 __bt_set_device_values(TRUE,
1406 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1408 hid_connected_device_count--;
1409 if (hid_connected_device_count == 0)
1410 __bt_set_device_values(FALSE,
1411 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1414 if (remote_dev_info != NULL) {
1415 BT_DBG("HID device class [%x]", remote_dev_info->class);
1416 if (remote_dev_info->class &
1417 BLUETOOTH_DEVICE_MINOR_CLASS_KEY_BOARD) {
1418 __bt_set_device_values(property_flag,
1419 VCONFKEY_BT_DEVICE_HID_KEYBOARD_CONNECTED);
1423 if (remote_dev_info->class &
1424 BLUETOOTH_DEVICE_MINOR_CLASS_POINTING_DEVICE) {
1425 __bt_set_device_values(property_flag,
1426 VCONFKEY_BT_DEVICE_HID_MOUSE_CONNECTED);
1428 _bt_free_device_info(remote_dev_info);
1433 g_variant_unref(val);
1434 g_variant_unref(child);
1438 void _bt_handle_network_server_event(GVariant *msg, const char *member)
1440 int result = BLUETOOTH_ERROR_NONE;
1441 char *address = NULL;
1442 char *device = NULL;
1443 GVariant *param = NULL;
1444 ret_if(member == NULL);
1445 if (strcasecmp(member, "PeerConnected") == 0) {
1446 g_variant_get(msg, "(ss)", &device, &address);
1448 __bt_set_device_values(TRUE,
1449 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1450 param = g_variant_new("(iss)", result, device, address);
1451 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
1455 nap_connected_device_count++;
1456 } else if (strcasecmp(member, "PeerDisconnected") == 0) {
1457 g_variant_get(msg, "(ss)", &device, &address);
1458 nap_connected_device_count--;
1459 if (nap_connected_device_count == 0)
1460 __bt_set_device_values(FALSE,
1461 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1462 param = g_variant_new("(iss)", result, device, address);
1463 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
1470 void _bt_handle_network_client_event(GVariant *msg,
1475 int result = BLUETOOTH_ERROR_NONE;
1476 gboolean property_flag = FALSE;
1477 char *property = NULL;
1478 GVariant *val = NULL;
1479 GVariantIter value_iter;
1480 GVariant *param = NULL;
1481 g_variant_iter_init(&value_iter, msg);
1482 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1483 if (strcasecmp(property, "Connected") == 0) {
1484 int event = BLUETOOTH_EVENT_NONE;
1487 g_variant_get(val, "b", &property_flag);
1488 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1490 _bt_convert_device_path_to_address(path, address);
1492 BT_DBG("property_flag %d", property_flag);
1493 if (property_flag == TRUE) {
1494 event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
1495 nap_connected_device_count++;
1496 __bt_set_device_values(TRUE,
1497 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1499 event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
1500 nap_connected_device_count--;
1501 if (nap_connected_device_count == 0)
1502 __bt_set_device_values(FALSE,
1503 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1505 param = g_variant_new("(is)", result, address);
1506 _bt_send_event(BT_NETWORK_EVENT, event,
1515 void __bt_gatt_char_property_changed_event(GVariant *msg,
1518 GVariantIter value_iter;
1519 char *property = NULL;
1520 char * char_handle = NULL;
1521 GVariant *val = NULL;
1522 int result = BLUETOOTH_ERROR_NONE;
1523 GVariant *param = NULL;
1524 g_variant_iter_init(&value_iter, msg);
1525 char_handle = g_strdup(path);
1526 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1527 BT_INFO("Property %s", property);
1529 ret_if(property == NULL);
1531 if (strcasecmp(property, "Notifying") == 0) {
1532 gboolean property_flag = FALSE;
1533 g_variant_get(val, "b", &property_flag);
1534 if (property_flag == TRUE)
1535 BT_DBG("notifying is enabled");
1537 BT_DBG("notifying is disabled");
1538 } else if (strcasecmp(property, "ChangedValue") == 0) {
1540 GByteArray *gp_byte_array = NULL;
1541 BT_INFO("Type '%s'\n", g_variant_get_type_string(val));
1544 gp_byte_array = g_byte_array_new();
1545 len = g_variant_get_size(val);
1546 BT_DBG("Len = %d", len);
1547 g_byte_array_append(gp_byte_array,
1548 (const guint8 *) g_variant_get_data(val), len);
1549 if (gp_byte_array->len != 0) {
1550 GVariant *byte_array = NULL;
1551 byte_array = g_variant_new_from_data(
1552 G_VARIANT_TYPE_BYTESTRING,
1553 gp_byte_array->data,
1556 param = g_variant_new("(is@ay)", result, char_handle,
1559 /* Send event only registered client */
1560 _bt_send_char_value_changed_event(param);
1562 g_byte_array_free(gp_byte_array, TRUE);
1566 g_free(char_handle);
1569 void _bt_handle_gatt_event(GVariant *msg, const char *member, const char *path)
1571 ret_if(path == NULL);
1573 if (strcasecmp(member, "GattValueChanged") == 0) {
1576 /*** Debug only ***/
1577 GVariant *value = NULL;
1579 char *buffer = NULL;
1581 g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
1582 value_len = g_variant_get_size(value);
1583 if (value_len > 0) {
1584 char buf[8 * 5 + 1] = { 0 };
1587 buffer = (char *)g_variant_get_data(value);
1588 to = value_len > (sizeof(buf) / 5) ? sizeof(buf) / 5 : value_len;
1590 for (i = 0; i < to; i++)
1591 snprintf(&buf[i * 5], 6, "0x%02x ", buffer[i]);
1593 BT_DBG("GATT Val[%d] %s", value_len, buf);
1595 g_variant_unref(value);
1599 /* Send event only registered client */
1600 _bt_send_char_value_changed_event(msg);
1605 void _bt_handle_device_event(GVariant *msg, const char *member, const char *path)
1608 int result = BLUETOOTH_ERROR_NONE;
1610 char *dev_name = NULL;
1611 const char *property = NULL;
1612 GVariant *param = NULL;
1613 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1614 ret_if(path == NULL);
1616 if (strcasecmp(member, "PropertyChanged") == 0) {
1618 g_variant_get(msg, "(s)", &property);
1620 ret_if(property == NULL);
1622 if (strcasecmp(property, "GattConnected") == 0) {
1623 gboolean connected = FALSE;
1625 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1626 ret_if(address == NULL);
1628 _bt_convert_device_path_to_address(path, address);
1629 g_variant_get(msg, "(b)", &connected);
1631 event = connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1632 BLUETOOTH_EVENT_GATT_DISCONNECTED;
1633 param = g_variant_new("(is)", result, address);
1634 _bt_send_event(BT_DEVICE_EVENT,
1638 } else if (strcasecmp(property, "Paired") == 0) {
1639 gboolean paired = FALSE;
1640 bt_remote_dev_info_t *remote_dev_info;
1641 g_variant_get(msg, "(b)", &paired);
1643 ret_if(paired == FALSE);
1645 /* BlueZ sends paired signal for each paired device */
1646 /* during activation, We should ignore this, otherwise*/
1647 /* application thinks that a new device got paired */
1648 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1649 BT_DBG("BT is not activated, so ignore this");
1653 if (_bt_is_device_creating() == TRUE) {
1654 BT_DBG("Try to Pair by me");
1658 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1659 ret_if(address == NULL);
1661 _bt_convert_device_path_to_address(path, address);
1663 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1664 if (remote_dev_info == NULL) {
1668 GVariant *uuids = NULL;
1669 GVariantBuilder *builder = NULL;
1671 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1672 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1673 g_variant_builder_add(builder, "s",
1674 remote_dev_info->uuids[i]);
1676 uuids = g_variant_new("as", builder);
1677 g_variant_builder_unref(builder);
1678 GVariant *manufacturer_data = NULL;
1679 manufacturer_data = g_variant_new_from_data(
1680 G_VARIANT_TYPE_BYTESTRING,
1681 remote_dev_info->manufacturer_data,
1682 remote_dev_info->manufacturer_data_len,
1684 param = g_variant_new("(isunsbub@asn@ay)", result,
1686 remote_dev_info->class,
1687 remote_dev_info->rssi,
1688 remote_dev_info->name,
1689 remote_dev_info->paired,
1690 remote_dev_info->connected,
1691 remote_dev_info->trust,
1693 remote_dev_info->manufacturer_data_len,
1695 _bt_send_event(BT_ADAPTER_EVENT,
1696 BLUETOOTH_EVENT_BONDING_FINISHED,
1698 _bt_free_device_info(remote_dev_info);
1701 } else if (strcasecmp(property, "UUIDs") == 0) {
1702 /* Once we get the updated uuid information after
1703 * reverse service search, update it to application */
1705 bt_remote_dev_info_t *remote_dev_info;
1707 ret_if(_bt_is_device_creating() == TRUE);
1709 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1710 ret_if(address == NULL);
1712 _bt_convert_device_path_to_address(path, address);
1714 remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1715 if (remote_dev_info == NULL) {
1720 BT_DBG("UUID's count = %d", remote_dev_info->uuid_count);
1721 if (remote_dev_info->paired && remote_dev_info->uuid_count) {
1722 GVariant *uuids = NULL;
1723 GVariantBuilder *builder = NULL;
1725 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1726 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1727 g_variant_builder_add(builder, "s",
1728 remote_dev_info->uuids[i]);
1730 uuids = g_variant_new("as", builder);
1731 g_variant_builder_unref(builder);
1732 GVariant *manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1733 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1736 param = g_variant_new("(isunsbub@asn@ay)", result,
1737 address, remote_dev_info->class,
1738 remote_dev_info->rssi,
1739 remote_dev_info->name,
1740 remote_dev_info->paired,
1741 remote_dev_info->connected,
1742 remote_dev_info->trust,
1744 remote_dev_info->manufacturer_data_len,
1746 _bt_send_event(BT_ADAPTER_EVENT,
1747 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1751 _bt_free_device_info(remote_dev_info);
1754 } else if (strcasecmp(member, "DeviceConnected") == 0) {
1755 unsigned char addr_type = 0;
1757 g_variant_get(msg, "(y)", &addr_type);
1759 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1760 ret_if(address == NULL);
1762 _bt_convert_device_path_to_address(path, address);
1763 dev_name = _bt_get_bonded_device_name(address);
1765 _bt_convert_addr_string_to_secure_string(secure_address, address);
1766 BT_INFO("Address : %s Type : %d", secure_address, addr_type);
1767 BT_ERR_C("### Connected [%s] [%s]", !addr_type ? "BREDR" : "LE",
1768 !addr_type ? dev_name : secure_address);
1771 _bt_logging_connection(TRUE, addr_type);
1772 param = g_variant_new("(isy)", result, address, addr_type);
1773 /*Send event to application*/
1774 _bt_send_event(BT_DEVICE_EVENT,
1775 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1778 } else if (strcasecmp(member, "Disconnected") == 0) {
1779 unsigned char disc_reason = 0;
1780 unsigned char addr_type = 0;
1781 char *dev_name = NULL;
1782 gboolean sending = FALSE;
1784 g_variant_get(msg, "(yys)", &addr_type, &disc_reason, &dev_name);
1786 result = disc_reason;
1788 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1789 ret_if(address == NULL);
1791 _bt_convert_device_path_to_address(path, address);
1793 /* 0x00 BDADDR_BRDER
1794 0x01 BDADDR_LE_PUBLIC
1795 0x02 BDADDR_LE_RANDOM */
1796 _bt_convert_addr_string_to_secure_string(secure_address, address);
1797 BT_INFO("Address : %s Type : %d", secure_address, addr_type);
1798 BT_ERR_C("### Disconnected [%s] [%d : %s] [%s]", !addr_type ? "BREDR" : "LE",
1799 disc_reason, _bt_convert_disc_reason_to_string(disc_reason),
1800 !addr_type ? dev_name : secure_address);
1803 _bt_headset_set_local_connection(FALSE);
1804 _bt_logging_connection(FALSE, addr_type);
1807 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1809 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1811 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
1812 BT_ERR("vconf_get_int failed");
1814 BT_INFO("conn_state[0x%x], adapter_state [%d]",
1815 bt_device_state, _bt_adapter_get_status());
1817 if (disc_reason == BLUETOOTH_ERROR_CONNECTION_TIMEOUT) {
1818 _bt_audio_start_auto_connect(TRUE);
1819 } else if (bt_device_state &
1820 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED) {
1821 BT_INFO("Disconnected due to turning BT off. Skip a address");
1823 char *last_connected = NULL;
1824 last_connected = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1825 if (!g_strcmp0(address, last_connected))
1826 _bt_audio_set_auto_connect_device_addr("");
1828 free(last_connected);
1833 /*Check for any OPP transfer on the device and cancel
1836 _bt_obex_check_pending_transfer(address);
1837 _bt_opp_client_is_sending(&sending);
1839 _bt_opp_client_check_pending_transfer(address);
1841 param = g_variant_new("(isy)", result, address, addr_type);
1842 _bt_send_event(BT_DEVICE_EVENT,
1843 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1846 } else if (strcasecmp(member, "ProfileStateChanged") == 0) {
1848 char *profile_uuid = NULL;
1849 bt_headset_wait_t *wait_list;
1850 bluetooth_device_address_t bd_addr;
1852 g_variant_get(msg, "(si)", &profile_uuid, &state);
1854 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1855 ret_if(address == NULL);
1857 _bt_convert_device_path_to_address(path, address);
1858 _bt_convert_addr_string_to_type(bd_addr.addr, address);
1860 _bt_convert_addr_string_to_secure_string(secure_address, address);
1861 BT_DBG("Address: %s", secure_address);
1862 BT_DBG("Profile UUID: %s", profile_uuid);
1863 BT_DBG("State: %d", state);
1865 if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1866 (state == BT_PROFILE_STATE_CONNECTED)) {
1868 int event = BLUETOOTH_EVENT_AV_CONNECTED;
1869 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1870 bluetooth_device_address_t device_address;
1872 bt_headset_wait_t *wait_list;
1873 guint restricted = 0x0;
1875 __bt_set_device_values(TRUE,
1876 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1878 __bt_connection_manager_set_state(address, event);
1880 _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_HFP_HS, &restricted);
1882 if (_bt_headset_get_local_connection() == FALSE) {
1883 if (restricted == 0x0) /* not restricted*/
1884 _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
1886 /* Connection Started from local device therefore no need to
1887 * intiate connection for pending profile */
1888 _bt_headset_set_local_connection(FALSE);
1890 param = g_variant_new("(is)", result, address);
1891 _bt_send_event(BT_HEADSET_EVENT, event,
1893 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
1896 if (g_strcmp0(connected_address, address) != 0) {
1897 _bt_convert_addr_string_to_type(
1898 device_address.addr,
1900 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
1901 &device_address, NULL);
1905 _bt_add_headset_to_list(BT_AUDIO_A2DP,
1906 BT_STATE_CONNECTED, address);
1908 wait_list = _bt_get_audio_wait_data();
1909 if (wait_list != NULL &&
1910 (g_strcmp0(wait_list->address, address) == 0))
1911 _bt_rel_wait_data();
1913 } else if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1914 (state == BT_PROFILE_STATE_DISCONNECTED)) {
1916 int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
1918 if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
1920 g_free(profile_uuid);
1924 __bt_set_device_values(FALSE,
1925 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1927 __bt_connection_manager_set_state(address, event);
1928 param = g_variant_new("(is)", result, address);
1929 _bt_send_event(BT_HEADSET_EVENT, event,
1931 /* Remove data from the connected list */
1932 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
1933 wait_list = _bt_get_audio_wait_data();
1935 if (wait_list == NULL) {
1937 g_free(profile_uuid);
1941 if (((wait_list->type == BT_AUDIO_ALL) &&
1942 (wait_list->ag_flag == TRUE)) ||
1943 (wait_list->type == BT_AUDIO_A2DP) ||
1944 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
1945 bluetooth_device_address_t device_address;
1946 _bt_convert_addr_string_to_type(
1947 device_address.addr,
1948 wait_list->address);
1950 _bt_audio_connect(wait_list->req_id,
1953 wait_list->out_param1);
1955 } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
1957 if (state == BT_PROFILE_STATE_CONNECTED) {
1958 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
1959 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1960 bluetooth_device_address_t device_address;
1962 param = g_variant_new("(is)", result, address);
1963 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1965 connected = _bt_is_headset_type_connected(
1969 if (g_strcmp0(connected_address,
1971 _bt_convert_addr_string_to_type(
1972 device_address.addr,
1974 _bt_audio_disconnect(0,
1976 &device_address, NULL);
1979 BT_DBG("device Path: %s", path);
1980 _bt_add_headset_to_list(BT_AVRCP,
1981 BT_STATE_CONNECTED, address);
1982 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1983 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
1984 param = g_variant_new("(is)", result, address);
1985 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1987 /* Remove data from the connected list */
1988 _bt_remove_headset_from_list(BT_AVRCP, address);
1990 } else if (strcasecmp(profile_uuid, A2DP_SOURCE_UUID) == 0) {
1991 if (state == BT_PROFILE_STATE_CONNECTED) {
1992 int event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED;
1993 BT_INFO("A2DP Source is connected");
1994 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
1995 __bt_set_device_values(TRUE,
1996 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED);
1999 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
2000 _bt_audio_set_auto_connect_device_addr(address);
2001 _bt_audio_stop_auto_connect();
2003 _bt_add_headset_to_list(BT_AUDIO_A2DP_SOURCE,
2004 BT_STATE_CONNECTED, address);
2005 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
2006 g_variant_new("(is)", result, address));
2007 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2008 int event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED;
2009 BT_INFO("A2DP Source Disconnected");
2010 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
2011 __bt_set_device_values(FALSE,
2012 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED);
2014 _bt_remove_headset_from_list(BT_AUDIO_A2DP_SOURCE, address);
2015 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
2016 g_variant_new("(is)", result, address));
2018 wait_list = _bt_get_audio_wait_data();
2019 if (wait_list && wait_list->type == BT_AUDIO_A2DP_SOURCE) {
2020 bluetooth_device_address_t device_address;
2021 _bt_convert_addr_string_to_type(
2022 device_address.addr,
2023 wait_list->address);
2025 _bt_audio_connect(wait_list->req_id,
2028 wait_list->out_param1);
2029 /* Now free the wait list */
2030 _bt_rel_wait_data();
2033 } else if ((strcmp(profile_uuid, HID_UUID) == 0) &&
2034 ((state == BT_PROFILE_STATE_CONNECTED) ||
2035 (state == BT_PROFILE_STATE_DISCONNECTED))) {
2037 if (state == BT_PROFILE_STATE_CONNECTED)
2038 event = BLUETOOTH_HID_CONNECTED;
2040 event = BLUETOOTH_HID_DISCONNECTED;
2041 param = g_variant_new("(is)", result, address);
2042 _bt_send_event(BT_HID_EVENT, event,
2044 } else if (strcmp(profile_uuid, HID_DEVICE_UUID) == 0) {
2045 if (state == BT_PROFILE_STATE_CONNECTED) {
2047 event = BLUETOOTH_HID_DEVICE_CONNECTED;
2048 param = g_variant_new("(is)", result, address);
2049 _bt_send_event(BT_HID_DEVICE_EVENT, event,
2051 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2052 event = BLUETOOTH_HID_DEVICE_DISCONNECTED;
2053 param = g_variant_new("(is)", result, address);
2054 _bt_send_event(BT_HID_DEVICE_EVENT, event,
2059 g_free(profile_uuid);
2060 } else if (strcasecmp(member, "AdvReport") == 0) {
2062 bt_remote_le_dev_info_t *le_dev_info = NULL;
2063 char *buffer = NULL;
2065 bt_le_adv_info_t *adv_info = NULL;
2066 GVariant *value = NULL;
2067 ret_if(_bt_is_le_scanning() == FALSE);
2069 le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
2070 if (le_dev_info == NULL)
2073 g_variant_get(msg, "(syyii@ay)", &le_dev_info->address,
2074 &le_dev_info->addr_type,
2075 &le_dev_info->adv_type,
2077 &le_dev_info->adv_data_len,
2079 _bt_convert_device_path_to_address(path, le_dev_info->address);
2081 buffer_len = g_variant_get_size(value);
2083 buffer = (char *)g_variant_get_data(value);
2085 le_dev_info->adv_data = g_memdup(buffer, buffer_len);
2086 if (le_dev_info->adv_data == NULL &&
2087 le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {
2088 _bt_free_le_device_info(le_dev_info);
2089 g_variant_unref(value);
2093 if (_bt_get_le_scan_type() == BT_LE_PASSIVE_SCAN) {
2094 _bt_send_scan_result_event(le_dev_info, NULL);
2095 _bt_free_le_device_info(le_dev_info);
2096 g_variant_unref(value);
2100 if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) { /* ADV_IND */
2101 adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
2102 if (adv_info == NULL) {
2103 _bt_free_le_device_info(le_dev_info);
2104 g_variant_unref(value);
2108 adv_info->addr = g_strdup(le_dev_info->address);
2109 adv_info->addr_type = le_dev_info->addr_type;
2110 adv_info->rssi = le_dev_info->rssi;
2111 adv_info->data_len = le_dev_info->adv_data_len;
2112 adv_info->data = g_malloc0(le_dev_info->adv_data_len);
2113 if (adv_info->data) {
2114 memcpy(adv_info->data, le_dev_info->adv_data,
2115 le_dev_info->adv_data_len);
2118 if (__bt_add_adv_ind_info(adv_info) == 0) {
2119 adv_info->timer_id = g_timeout_add(1000,
2120 (GSourceFunc)__bt_adv_scan_req_timeout_cb, (void*)adv_info);
2122 } else { /* SCAN_RSP */
2123 adv_info = __bt_get_adv_ind_info(le_dev_info->address);
2125 _bt_send_scan_result_event(le_dev_info, adv_info);
2126 __bt_del_adv_ind_info(le_dev_info->address);
2129 _bt_free_le_device_info(le_dev_info);
2130 g_variant_unref(value);
2131 } else if (strcasecmp(member, "LEDataLengthChanged") == 0) {
2132 guint16 tx_octets = 0;
2133 guint16 tx_time = 0;
2134 guint16 rx_octets = 0;
2135 guint16 rx_time = 0;
2137 g_variant_get(msg, "(qqqq)",
2138 &tx_octets, &tx_time, &rx_octets, &rx_time);
2140 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2141 _bt_convert_device_path_to_address(path, address);
2143 param = g_variant_new("(isqqqq)", result, address, tx_octets, tx_time,
2144 rx_octets, rx_time);
2145 /* Send event to application */
2146 _bt_send_event(BT_DEVICE_EVENT,
2147 BLUETOOTH_EVENT_LE_DATA_LENGTH_CHANGED, param);
2149 } else if (strcasecmp(member, "IpspStateChanged") == 0) {
2150 gboolean connected = FALSE;
2151 char *ifname = NULL;
2153 g_variant_get(msg, "(bs)", &connected, &ifname);
2155 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
2156 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
2158 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2159 _bt_convert_device_path_to_address(path, address);
2161 BT_DBG("Ipsp BT Interface Name: %s", ifname);
2162 BT_DBG("address: %s", address);
2163 param = g_variant_new("(iss)", result, address, ifname);
2165 /* Send event to application */
2166 _bt_send_event(BT_DEVICE_EVENT,
2174 void __bt_set_audio_values(gboolean connected, char *address)
2177 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
2179 /* Set the headset name */
2180 if (connected == TRUE) {
2181 name = _bt_get_bonded_device_name(address);
2183 name = g_strdup("");
2186 if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME,
2188 BT_ERR("vconf_set_str failed");
2193 /* Set the headset state */
2194 if (vconf_get_int(VCONFKEY_BT_DEVICE,
2195 &bt_device_state) != 0) {
2196 BT_ERR("vconf_get_int failed");
2199 #ifdef TIZEN_SUPPORT_DUAL_HF
2200 if ((connected == TRUE) &&
2201 (FALSE == __bt_is_companion_device(address))) {
2202 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2203 } else if ((bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) &&
2204 (FALSE == __bt_is_companion_device(address))) {
2205 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2208 if (connected == TRUE) {
2209 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2210 } else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) {
2211 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2215 if (vconf_set_int(VCONFKEY_BT_DEVICE,
2216 bt_device_state) != 0) {
2217 BT_ERR("vconf_set_int failed");
2221 void _bt_handle_headset_event(GVariant *msg, const char *path)
2223 int result = BLUETOOTH_ERROR_NONE;
2224 gboolean property_flag = FALSE;
2225 char *property = NULL;
2226 GVariant *value = NULL;
2227 GVariant *param = NULL;
2228 g_variant_get(msg, "(sv)", &property, &value);
2229 bluetooth_device_address_t bd_addr;
2231 ret_if(property == NULL);
2233 BT_DBG("Property = %s \n", property);
2234 /* We allow only 1 headset connection (HSP or HFP)*/
2235 if (strcasecmp(property, "Connected") == 0) {
2236 int event = BLUETOOTH_EVENT_NONE;
2237 bt_headset_wait_t *wait_list;
2239 guint restricted = 0x0;
2240 g_variant_get(value, "b", &property_flag);
2242 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2243 if (address == NULL)
2246 _bt_convert_device_path_to_address(path, address);
2247 _bt_convert_addr_string_to_type(bd_addr.addr, address);
2249 if (property_flag == TRUE) {
2250 event = BLUETOOTH_EVENT_AG_CONNECTED;
2251 _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_A2DP, &restricted);
2253 if (_bt_headset_get_local_connection() == FALSE) {
2254 if (restricted == 0x0) /* not restricted*/
2255 _bt_start_timer_for_connection(address, BT_AUDIO_A2DP);
2257 _bt_headset_set_local_connection(FALSE);
2261 event = BLUETOOTH_EVENT_AG_DISCONNECTED;
2263 previous_state = _bt_get_device_state_from_list(BT_AUDIO_HSP, address);
2264 if (previous_state == BT_STATE_DISCONNECTING)
2265 _bt_send_hf_local_term_event(address);
2267 /* Set the State machine here */
2268 __bt_connection_manager_set_state(address, event);
2269 __bt_set_audio_values(property_flag, address);
2270 param = g_variant_new("(is)", result, address);
2271 _bt_send_event(BT_HEADSET_EVENT, event,
2274 if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
2275 /* Remove data from the connected list */
2276 _bt_remove_headset_from_list(BT_AUDIO_HSP, address);
2278 wait_list = _bt_get_audio_wait_data();
2279 if (wait_list == NULL) {
2284 bluetooth_device_address_t device_address;
2286 _bt_set_audio_wait_data_flag(TRUE);
2288 _bt_convert_addr_string_to_type(device_address.addr,
2289 wait_list->address);
2290 _bt_audio_connect(wait_list->req_id, wait_list->type,
2291 &device_address, wait_list->out_param1);
2292 _bt_rel_wait_data();
2293 } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
2294 /* Add data to the connected list */
2295 _bt_add_headset_to_list(BT_AUDIO_HSP,
2296 BT_STATE_CONNECTED, address);
2298 wait_list = _bt_get_audio_wait_data();
2299 if (wait_list != NULL &&
2300 (g_strcmp0(wait_list->address, address) == 0))
2301 _bt_rel_wait_data();
2303 BT_INFO("Check A2DP pending connect");
2304 _bt_audio_check_pending_connect();
2307 } else if (strcasecmp(property, "State") == 0) {
2310 g_variant_get(value, "s", &state);
2312 /* This code assumes we support only 1 headset connection */
2313 /* Need to use the headset list, if we support multi-headsets */
2314 if (strcasecmp(state, "Playing") == 0) {
2315 BT_DBG("Playing: Sco Connected");
2316 } else if (strcasecmp(state, "connected") == 0 ||
2317 strcasecmp(state, "disconnected") == 0) {
2318 BT_DBG("connected/disconnected: Sco Disconnected");
2320 BT_ERR("Not handled state - %s", state);
2325 } else if (strcasecmp(property, "SpeakerGain") == 0) {
2329 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2331 _bt_convert_device_path_to_address(path, address);
2333 spkr_gain = g_variant_get_uint16(value);
2335 BT_DBG("spkr_gain: %d", spkr_gain);
2337 param = g_variant_new("(i&sq)", result, address, spkr_gain);
2338 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
2342 } else if (strcasecmp(property, "MicrophoneGain") == 0) {
2346 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2348 _bt_convert_device_path_to_address(path, address);
2350 mic_gain = g_variant_get_uint16(value);
2352 param = g_variant_new("(i&sq)", result, address, mic_gain);
2353 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_MIC_GAIN,
2360 g_variant_unref(value);
2363 void _bt_handle_sink_event(GVariant *msg, const char *path)
2365 GVariantIter value_iter;
2366 char *property = NULL;
2368 bt_headset_wait_t *wait_list;
2370 GVariant *child = NULL;
2371 GVariant *val = NULL;
2372 GVariant *param = NULL;
2373 g_variant_iter_init(&value_iter, msg);
2374 while ((child = g_variant_iter_next_value(&value_iter))) {
2376 g_variant_get(child, "{sv}", &property, &val);
2378 ret_if(property == NULL);
2380 BT_DBG("Property = %s \n", property);
2383 if (strcasecmp(property, "State") == 0) {
2384 int result = BLUETOOTH_ERROR_NONE;
2387 g_variant_get(val, "s", &value);
2388 BT_DBG("value: %s", value);
2390 if (g_strcmp0(value, "disconnected") == 0) {
2393 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2395 _bt_convert_device_path_to_address(path, address);
2397 __bt_set_device_values(FALSE,
2398 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2399 param = g_variant_new("(is)", result, address);
2400 _bt_send_event(BT_HEADSET_EVENT,
2401 BLUETOOTH_EVENT_AV_DISCONNECTED,
2404 /* Remove data from the connected list */
2405 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2406 wait_list = _bt_get_audio_wait_data();
2407 if (wait_list == NULL) {
2410 g_variant_unref(val);
2411 g_variant_unref(child);
2416 if (((wait_list->type == BT_AUDIO_ALL) &&
2417 (wait_list->ag_flag == TRUE)) ||
2418 (wait_list->type == BT_AUDIO_A2DP) ||
2419 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2420 bluetooth_device_address_t device_address;
2421 _bt_convert_addr_string_to_type(
2422 device_address.addr,
2423 wait_list->address);
2425 _bt_audio_connect(wait_list->req_id,
2428 wait_list->out_param1);
2431 } else if (strcasecmp(value, "Connected") == 0) {
2433 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2434 bluetooth_device_address_t device_address;
2437 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2439 _bt_convert_device_path_to_address(path, address);
2441 __bt_set_device_values(TRUE,
2442 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2443 param = g_variant_new("(is)", result, address);
2444 _bt_send_event(BT_HEADSET_EVENT,
2445 BLUETOOTH_EVENT_AV_CONNECTED,
2447 /* Check for existing Media device to disconnect */
2448 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
2451 /* Match connected device address */
2452 if (g_strcmp0(connected_address, address) != 0) {
2453 /* Convert BD adress from string type */
2454 _bt_convert_addr_string_to_type(
2455 device_address.addr,
2457 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2458 &device_address, NULL);
2462 /* Add data to the connected list */
2463 _bt_add_headset_to_list(BT_AUDIO_A2DP,
2464 BT_STATE_CONNECTED, address);
2471 g_variant_unref(val);
2472 g_variant_unref(child);
2476 void _bt_handle_agent_event(GVariant *msg, const char *member)
2478 int result = BLUETOOTH_ERROR_NONE;
2479 char *address = NULL;
2482 GVariant *param = NULL;
2483 ret_if(member == NULL);
2485 if (strcasecmp(member, "ObexAuthorize") == 0) {
2486 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2487 param = g_variant_new("(iss)", result, address, name);
2488 _bt_send_event(BT_OPP_SERVER_EVENT,
2489 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
2493 } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2494 bt_rfcomm_server_info_t *server_info;
2496 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2498 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2499 ret_if(server_info == NULL);
2500 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2501 param = g_variant_new("(isssn)", result, address, uuid, name,
2502 server_info->control_fd);
2503 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2504 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2512 static int __bt_get_object_path(GVariant *msg, char **path)
2514 g_variant_get(msg, "(o*)", path, NULL);
2516 return BLUETOOTH_ERROR_INTERNAL;
2518 return BLUETOOTH_ERROR_NONE;
2521 static void __bt_devices_list_free(void)
2523 bt_cache_info_t *cache_info;
2526 node = g_list_first(p_cache_list);
2528 while (node != NULL) {
2529 cache_info = (bt_cache_info_t *)node->data;
2530 p_cache_list = g_list_remove(p_cache_list, cache_info);
2531 __bt_free_cache_info(cache_info);
2533 node = g_list_next(node);
2537 static int __bt_parse_event(GVariant *msg)
2541 char *interface_name = NULL;
2542 GVariant *inner_iter = NULL;
2544 g_variant_iter_init(&iter, msg);
2546 while ((child = g_variant_iter_next_value(&iter))) {
2547 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
2548 if (g_strcmp0(interface_name,
2549 BT_DEVICE_INTERFACE) == 0) {
2550 g_variant_unref(inner_iter);
2551 g_variant_unref(child);
2552 return BT_DEVICE_EVENT;
2553 } else if (g_strcmp0(interface_name,
2554 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2555 g_variant_unref(inner_iter);
2556 g_variant_unref(child);
2557 return BT_MEDIA_TRANSFER_EVENT;
2558 } else if (g_strcmp0(interface_name,
2559 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2560 g_variant_unref(inner_iter);
2561 g_variant_unref(child);
2562 return BT_AVRCP_CONTROL_EVENT;
2564 g_variant_unref(inner_iter);
2565 g_variant_unref(child);
2571 static void __bt_manager_event_filter(GDBusConnection *connection,
2572 const gchar *sender_name,
2573 const gchar *object_path,
2574 const gchar *interface_name,
2575 const gchar *signal_name,
2576 GVariant *parameters,
2579 bt_event_type_t bt_event = 0x00;
2580 int result = BLUETOOTH_ERROR_NONE;
2582 char *obj_path = NULL;
2583 GVariant *param = NULL;
2584 if (signal_name == NULL)
2586 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2587 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2589 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
2590 #ifdef USB_BLUETOOTH
2591 BT_DBG("Enable Adapter");
2592 _bt_enable_adapter();
2594 _bt_handle_adapter_added();
2597 bt_event = __bt_parse_event(value);
2598 if (bt_event == BT_DEVICE_EVENT) {
2599 bt_cache_info_t *cache_info;
2600 bt_remote_dev_info_t *dev_info;
2602 ret_if(_bt_is_discovering() == FALSE &&
2603 _bt_is_le_scanning() == FALSE);
2605 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2606 ret_if(cache_info == NULL);
2608 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2609 if (dev_info == NULL) {
2610 __bt_free_cache_info(cache_info);
2614 cache_info->dev_info = dev_info;
2616 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2617 BT_ERR("Fail to parse the properies");
2618 __bt_free_cache_info(cache_info);
2619 g_variant_unref(value);
2623 if (dev_info->addr_type != BDADDR_BREDR) {
2624 /* Whenever emit the property changed from bluez,
2625 some property doesn't reach to bt-service.
2626 So LE device is handled as AdvReport signal */
2627 __bt_free_cache_info(cache_info);
2628 g_variant_unref(value);
2632 if (dev_info->name == NULL)
2633 /* If Remote device name is NULL or still RNR is not done
2634 * then display address as name.
2636 dev_info->name = g_strdup(dev_info->address);
2638 #ifdef TIZEN_DPM_ENABLE
2639 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
2641 bluetooth_device_class_t device_class;
2642 _bt_divide_device_class(&device_class, dev_info->class);
2643 BT_DBG("[%s]device_class.major_class : %d", dev_info->name, device_class.major_class);
2645 if (device_class.major_class ==
2646 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
2647 __bt_free_cache_info(cache_info);
2648 g_variant_unref(value);
2654 GVariant *uuids = NULL;
2655 GVariantBuilder *builder = NULL;
2657 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2658 for (i = 0; i < dev_info->uuid_count; i++) {
2659 g_variant_builder_add(builder, "s",
2660 dev_info->uuids[i]);
2662 uuids = g_variant_new("as", builder);
2663 g_variant_builder_unref(builder);
2664 GVariant *manufacturer_data = NULL;
2665 manufacturer_data = g_variant_new_from_data(
2666 G_VARIANT_TYPE_BYTESTRING,
2667 dev_info->manufacturer_data,
2668 dev_info->manufacturer_data_len,
2670 param = g_variant_new("(isunsbub@asn@ay)", result,
2676 dev_info->connected,
2679 dev_info->manufacturer_data_len,
2681 _bt_send_event(BT_ADAPTER_EVENT,
2682 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2684 p_cache_list = g_list_append(p_cache_list, cache_info);
2685 } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2686 BT_DBG("Device path : %s ", obj_path);
2687 _bt_set_control_device_path(obj_path);
2690 g_variant_unref(value);
2691 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2692 #ifdef USB_BLUETOOTH
2693 BT_DBG("InterfacesRemoved");
2694 _bt_handle_adapter_removed();
2696 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0) {
2697 bt_event = BT_MEDIA_TRANSFER_EVENT;
2698 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2699 bt_event = BT_DEVICE_EVENT;
2700 } else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
2701 bt_event = BT_AVRCP_CONTROL_EVENT;
2703 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2704 _bt_handle_adapter_event(parameters, signal_name);
2705 if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2706 BT_INFO("Object Path %s", obj_path);
2707 _bt_remove_control_device_path(obj_path);
2710 } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2713 char *previous = NULL;
2714 char *current = NULL;
2716 if (__bt_get_owner_info(parameters, &name, &previous, ¤t)) {
2717 BT_ERR("Fail to get the owner info");
2721 if (*current != '\0') {
2730 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2731 BT_DBG("Bluetoothd is terminated");
2732 if (_bt_adapter_get_status() == BT_ACTIVATED)
2735 _bt_handle_adapter_removed();
2736 __bt_devices_list_free();
2739 _bt_obex_server_check_allocation(&value);
2741 if (value == TRUE) {
2742 /* Check if the obex server was terminated abnormally */
2743 _bt_obex_server_check_termination(name);
2746 _bt_rfcomm_server_check_existence(&value);
2748 if (value == TRUE) {
2749 /* The obex server was terminated abnormally */
2750 _bt_rfcomm_server_check_termination(name);
2753 /* Stop advertising started by terminated process */
2754 _bt_stop_advertising_by_terminated_process(name);
2756 _bt_stop_le_scan(name);
2760 } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2761 const char *path = object_path;
2763 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2764 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2767 _bt_handle_property_changed_event(parameters, object_path);
2768 } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2769 _bt_handle_adapter_event(parameters, signal_name);
2770 } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2771 _bt_handle_input_event(parameters, object_path);
2772 } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2773 _bt_handle_network_server_event(parameters, signal_name);
2774 } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2775 _bt_handle_headset_event(parameters, object_path);
2776 } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2777 _bt_handle_sink_event(parameters, object_path);
2778 } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2779 _bt_handle_agent_event(parameters, signal_name);
2780 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2781 _bt_handle_device_event(parameters, signal_name, object_path);
2782 } else if (g_strcmp0(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
2783 _bt_handle_gatt_event(parameters, signal_name, object_path);
2789 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2792 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2793 char *interface_name = NULL;
2795 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2796 retv_if(interface_name == NULL, FALSE);
2798 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2799 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2807 static void __bt_obexd_event_filter(GDBusConnection *connection,
2808 const gchar *sender_name,
2809 const gchar *object_path,
2810 const gchar *interface_name,
2811 const gchar *signal_name,
2812 GVariant *parameters,
2815 const char *member = signal_name;
2816 char *obj_path = NULL;
2817 ret_if(member == NULL);
2819 if (strcasecmp(member, "InterfacesAdded") == 0) {
2820 if (__bt_get_object_path(parameters, &obj_path)) {
2821 BT_ERR("Fail to get the path");
2824 BT_INFO("object_path = [%s]", obj_path);
2826 /*Handle OPP_SERVER_CONNECTED_EVENT here */
2827 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2828 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2833 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2834 BT_DBG("Obex_Server_Session_Transfer connected");
2835 _bt_obex_transfer_connected(obj_path);
2838 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2839 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
2840 if (__bt_get_object_path(parameters, &obj_path)) {
2841 BT_ERR("Fail to get the path");
2844 BT_INFO("object_path = [%s]", obj_path);
2846 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2847 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2848 BT_DBG("Call PBAP Disconnected");
2849 _bt_obex_pbap_client_disconnect(obj_path);
2852 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2853 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2858 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2859 BT_DBG("Obex_Server_Session_Transfer disconnected %s",
2862 _bt_obex_transfer_disconnected(obj_path);
2865 } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
2866 const char *path = object_path;
2868 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
2869 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
2870 strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2871 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2872 BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
2876 _bt_handle_property_changed_event(parameters, path);
2882 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
2886 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2887 char *interface_name = NULL;
2889 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2891 retv_if(interface_name == NULL, FALSE);
2893 if (strcasecmp(interface_name,
2894 BT_OBEX_TRANSFER_INTERFACE) == 0) {
2905 static void __bt_opc_event_filter(GDBusConnection *connection,
2906 const gchar *sender_name,
2907 const gchar *object_path,
2908 const gchar *interface_name,
2909 const gchar *signal_name,
2910 GVariant *parameters,
2913 const char *member = signal_name;
2914 char *obj_path = NULL;
2915 if (strcasecmp(member, "InterfacesAdded") == 0) {
2916 BT_DBG("InterfacesAdded");
2917 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2919 if (__bt_get_object_path(parameters, &obj_path)) {
2920 BT_ERR("Fail to get the path");
2924 BT_DBG("object_path = %s", obj_path);
2926 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2927 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
2928 || strstr(obj_path, "transfer") == NULL) {
2931 } else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2932 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2933 BT_DBG("Going to call opc disconnected");
2934 _bt_opc_disconnected(obj_path);
2937 _bt_sending_files();
2939 } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
2940 char *path = (char *)object_path;
2941 BT_INFO("object_path %s", path);
2942 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2943 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2944 BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
2948 _bt_opc_property_changed_event(parameters, path);
2954 int _bt_opp_client_event_init(void)
2956 GError *error = NULL;
2958 if (opc_obexd_conn == NULL) {
2959 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
2961 if (!opc_obexd_conn) {
2963 BT_ERR("Unable to connect to dbus: %s", error->message);
2964 g_clear_error(&error);
2966 return BLUETOOTH_ERROR_INTERNAL;
2970 if (_bt_register_service_event(opc_obexd_conn,
2971 BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
2972 g_object_unref(opc_obexd_conn);
2973 opc_obexd_conn = NULL;
2974 return BLUETOOTH_ERROR_INTERNAL;
2977 return BLUETOOTH_ERROR_NONE;
2980 void _bt_opp_client_event_deinit(void)
2982 if (opc_obexd_conn) {
2983 _bt_unregister_service_event(opc_obexd_conn,
2984 BT_OPP_CLIENT_EVENT);
2985 g_object_unref(opc_obexd_conn);
2986 opc_obexd_conn = NULL;
2990 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
2996 static int subs_interface_added_id = -1;
2997 static int subs_interface_removed_id = -1;
2998 static int subs_name_owner_id = -1;
2999 static int subs_property_id = -1;
3000 static int subs_adapter_id = -1;
3001 static int subs_gatt_id = -1;
3004 if (subs_interface_added_id == -1) {
3005 subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3006 NULL, BT_MANAGER_INTERFACE,
3007 BT_INTERFACES_ADDED, NULL, NULL, 0,
3008 __bt_manager_event_filter,
3011 if (subs_interface_removed_id == -1) {
3012 subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3013 NULL, BT_MANAGER_INTERFACE,
3014 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3015 __bt_manager_event_filter,
3018 if (subs_name_owner_id == -1) {
3019 subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
3020 NULL, BT_FREEDESKTOP_INTERFACE,
3021 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
3022 __bt_manager_event_filter,
3025 if (subs_property_id == -1) {
3026 subs_property_id = g_dbus_connection_signal_subscribe(conn,
3027 NULL, BT_PROPERTIES_INTERFACE,
3028 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3029 __bt_manager_event_filter,
3032 if (subs_adapter_id == -1) {
3033 subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
3034 NULL, BT_ADAPTER_INTERFACE,
3035 NULL, NULL, NULL, 0,
3036 __bt_manager_event_filter,
3039 if (subs_gatt_id == -1) {
3040 subs_gatt_id = g_dbus_connection_signal_subscribe(conn,
3041 NULL, BT_GATT_CHAR_INTERFACE,
3042 NULL, NULL, NULL, 0,
3043 __bt_manager_event_filter,
3047 if (subs_interface_added_id != -1) {
3048 g_dbus_connection_signal_unsubscribe(conn,
3049 subs_interface_added_id);
3050 subs_interface_added_id = -1;
3052 if (subs_interface_removed_id != -1) {
3053 g_dbus_connection_signal_unsubscribe(conn,
3054 subs_interface_removed_id);
3055 subs_interface_removed_id = -1;
3057 if (subs_name_owner_id != -1) {
3058 g_dbus_connection_signal_unsubscribe(conn,
3059 subs_name_owner_id);
3060 subs_name_owner_id = -1;
3062 if (subs_property_id != -1) {
3063 g_dbus_connection_signal_unsubscribe(conn,
3065 subs_property_id = -1;
3067 if (subs_adapter_id != -1) {
3068 g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
3069 subs_adapter_id = -1;
3071 if (subs_gatt_id != -1) {
3072 g_dbus_connection_signal_unsubscribe(conn, subs_gatt_id);
3079 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
3085 static int subs_device_id = -1;
3088 if (subs_device_id == -1) {
3089 subs_device_id = g_dbus_connection_signal_subscribe(conn,
3090 NULL, BT_DEVICE_INTERFACE,
3091 NULL, NULL, NULL, 0,
3092 __bt_manager_event_filter,
3096 if (subs_device_id != -1) {
3097 g_dbus_connection_signal_unsubscribe(conn,
3099 subs_device_id = -1;
3105 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
3111 static int subs_input_id = -1;
3114 if (subs_input_id == -1) {
3115 subs_input_id = g_dbus_connection_signal_subscribe(conn,
3116 NULL, BT_INPUT_INTERFACE,
3117 NULL, NULL, NULL, 0,
3118 __bt_manager_event_filter,
3122 if (subs_input_id != -1) {
3123 g_dbus_connection_signal_unsubscribe(conn,
3131 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
3137 static int subs_serv_id = -1;
3138 static int subs_client_id = -1;
3141 if (subs_serv_id == -1) {
3142 subs_serv_id = g_dbus_connection_signal_subscribe(conn,
3143 NULL, BT_NETWORK_SERVER_INTERFACE,
3144 NULL, NULL, NULL, 0,
3145 __bt_manager_event_filter,
3148 if (subs_client_id == -1) {
3149 subs_client_id = g_dbus_connection_signal_subscribe(conn,
3150 NULL, BT_NETWORK_CLIENT_INTERFACE,
3151 NULL, NULL, NULL, 0,
3152 __bt_manager_event_filter,
3156 if (subs_serv_id != -1) {
3157 g_dbus_connection_signal_unsubscribe(conn,
3161 if (subs_client_id != -1) {
3162 g_dbus_connection_signal_unsubscribe(conn,
3164 subs_client_id = -1;
3170 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
3176 static int subs_headset_id = -1;
3177 static int subs_sink_id = -1;
3180 if (subs_headset_id == -1) {
3181 subs_headset_id = g_dbus_connection_signal_subscribe(conn,
3182 NULL, BT_HEADSET_INTERFACE,
3183 NULL, NULL, NULL, 0,
3184 __bt_manager_event_filter,
3187 if (subs_sink_id == -1) {
3188 subs_sink_id = g_dbus_connection_signal_subscribe(conn,
3189 NULL, BT_SINK_INTERFACE,
3190 NULL, NULL, NULL, 0,
3191 __bt_manager_event_filter,
3195 if (subs_headset_id != -1) {
3196 g_dbus_connection_signal_unsubscribe(conn,
3198 subs_headset_id = -1;
3200 if (subs_sink_id != -1) {
3201 g_dbus_connection_signal_unsubscribe(conn,
3209 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
3215 static int subs_opp_server_interface_added_id = -1;
3216 static int subs_opp_server_interface_removed_id = -1;
3217 static int subs_opp_server_property_id = -1;
3221 if (subs_opp_server_interface_added_id == -1) {
3222 subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3223 NULL, BT_MANAGER_INTERFACE,
3224 BT_INTERFACES_ADDED, NULL, NULL, 0,
3225 __bt_obexd_event_filter,
3228 if (subs_opp_server_interface_removed_id == -1) {
3229 subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3230 NULL, BT_MANAGER_INTERFACE,
3231 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3232 __bt_obexd_event_filter,
3235 if (subs_opp_server_property_id == -1) {
3236 subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
3237 NULL, BT_PROPERTIES_INTERFACE,
3238 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3239 __bt_obexd_event_filter,
3243 if (subs_opp_server_interface_added_id != -1) {
3244 g_dbus_connection_signal_unsubscribe(conn,
3245 subs_opp_server_interface_added_id);
3246 subs_opp_server_interface_added_id = -1;
3248 if (subs_opp_server_interface_removed_id != -1) {
3249 g_dbus_connection_signal_unsubscribe(conn,
3250 subs_opp_server_interface_removed_id);
3251 subs_opp_server_interface_removed_id = -1;
3253 if (subs_opp_server_property_id != -1) {
3254 g_dbus_connection_signal_unsubscribe(conn,
3255 subs_opp_server_property_id);
3256 subs_opp_server_property_id = -1;
3262 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
3268 static int subs_opp_client_interface_added_id = -1;
3269 static int subs_opp_client_interface_removed_id = -1;
3270 static int subs_opp_client_property_id = -1;
3274 if (subs_opp_client_interface_added_id == -1) {
3275 subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3276 NULL, BT_MANAGER_INTERFACE,
3277 BT_INTERFACES_ADDED, NULL, NULL, 0,
3278 __bt_opc_event_filter,
3281 if (subs_opp_client_interface_removed_id == -1) {
3282 subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3283 NULL, BT_MANAGER_INTERFACE,
3284 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3285 __bt_opc_event_filter,
3288 if (subs_opp_client_property_id == -1) {
3289 subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
3290 NULL, BT_PROPERTIES_INTERFACE,
3291 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3292 __bt_opc_event_filter,
3296 if (subs_opp_client_interface_added_id != -1) {
3297 g_dbus_connection_signal_unsubscribe(conn,
3298 subs_opp_client_interface_added_id);
3299 subs_opp_client_interface_added_id = -1;
3301 if (subs_opp_client_interface_removed_id != -1) {
3302 g_dbus_connection_signal_unsubscribe(conn,
3303 subs_opp_client_interface_removed_id);
3304 subs_opp_client_interface_removed_id = -1;
3306 if (subs_opp_client_property_id != -1) {
3307 g_dbus_connection_signal_unsubscribe(conn,
3308 subs_opp_client_property_id);
3309 subs_opp_client_property_id = -1;
3315 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
3321 static int subs_a2dp_source_id = -1;
3322 static int subs_a2dp_sink_id = -1;
3325 if (subs_a2dp_source_id == -1) {
3326 subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
3327 NULL, BT_A2DP_SOURCE_INTERFACE,
3328 NULL, NULL, NULL, 0,
3329 __bt_opc_event_filter,
3332 if (subs_a2dp_sink_id == -1) {
3333 subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
3334 NULL, BT_SINK_INTERFACE,
3335 NULL, NULL, NULL, 0,
3336 __bt_opc_event_filter,
3340 if (subs_a2dp_source_id != -1) {
3341 g_dbus_connection_signal_unsubscribe(conn,
3342 subs_a2dp_source_id);
3343 subs_a2dp_source_id = -1;
3345 if (subs_a2dp_sink_id != -1) {
3346 g_dbus_connection_signal_unsubscribe(conn,
3348 subs_a2dp_sink_id = -1;
3354 static void __bt_dump_event_filter(GDBusConnection *connection,
3355 const gchar *sender_name,
3356 const gchar *object_path,
3357 const gchar *interface_name,
3358 const gchar *signal_name,
3359 GVariant *parameters,
3365 int __bt_register_dump_subscribe_signal(GDBusConnection *conn,
3371 static int subs_source_id = -1;
3374 if (subs_source_id == -1) {
3375 subs_source_id = g_dbus_connection_signal_subscribe(conn,
3376 NULL, BT_DUMP_SERVICE_INTERFACE,
3377 BT_DUMP_SERVICE_SIGNAL, BT_DUMP_SERVICE_PATH, NULL, 0,
3378 __bt_dump_event_filter,
3382 if (subs_source_id != -1) {
3383 g_dbus_connection_signal_unsubscribe(conn,
3385 subs_source_id = -1;
3391 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
3395 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3397 switch (event_type) {
3398 case BT_MANAGER_EVENT:
3399 _bt_register_manager_subscribe_signal(g_conn, TRUE);
3401 case BT_DEVICE_EVENT:
3402 _bt_register_device_subscribe_signal(g_conn, TRUE);
3405 _bt_register_input_subscribe_signal(g_conn, TRUE);
3407 case BT_NETWORK_EVENT:
3408 _bt_register_network_subscribe_signal(g_conn, TRUE);
3410 case BT_HEADSET_EVENT:
3411 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3414 case BT_OPP_SERVER_EVENT:
3415 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3416 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3418 case BT_OPP_CLIENT_EVENT:
3419 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3420 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3422 case BT_A2DP_SOURCE_EVENT:
3423 BT_INFO("A2dp Source event");
3424 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3427 BT_ERR("Unknown event");
3428 return BLUETOOTH_ERROR_INTERNAL;
3431 return BLUETOOTH_ERROR_NONE;
3434 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3438 ret_if(g_conn == NULL);
3440 switch (event_type) {
3441 case BT_MANAGER_EVENT:
3442 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3443 _bt_register_device_subscribe_signal(g_conn, FALSE);
3444 _bt_register_input_subscribe_signal(g_conn, FALSE);
3445 _bt_register_network_subscribe_signal(g_conn, FALSE);
3446 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3448 case BT_OPP_SERVER_EVENT:
3449 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3451 case BT_OPP_CLIENT_EVENT:
3452 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3455 BT_ERR("Unknown event");
3462 static int __bt_init_manager_receiver(void)
3466 GError *error = NULL;
3468 if (manager_conn == NULL) {
3469 manager_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3470 if (error != NULL) {
3471 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3472 g_clear_error(&error);
3474 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3477 if (_bt_register_service_event(manager_conn,
3478 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3480 if (_bt_register_service_event(manager_conn,
3481 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3484 if (_bt_register_service_event(manager_conn,
3485 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3488 if (_bt_register_service_event(manager_conn,
3489 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3492 if (_bt_register_service_event(manager_conn,
3493 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3496 __bt_register_dump_subscribe_signal(manager_conn, TRUE);
3497 return BLUETOOTH_ERROR_NONE;
3500 g_object_unref(manager_conn);
3501 manager_conn = NULL;
3506 return BLUETOOTH_ERROR_INTERNAL;
3509 static int __bt_init_obexd_receiver(void)
3512 #ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3513 GError *error = NULL;
3515 if (obexd_conn == NULL) {
3516 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3517 if (error != NULL) {
3518 BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3519 g_clear_error(&error);
3521 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3524 if (_bt_register_service_event(obexd_conn,
3525 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3526 BT_ERR("Error while registering service event");
3527 g_object_unref(obexd_conn);
3529 return BLUETOOTH_ERROR_INTERNAL;
3534 return BLUETOOTH_ERROR_NONE;
3537 gboolean __bt_reinit_obexd_receiver(gpointer user_data)
3539 static int retry_cnt = 0;
3540 int result = BLUETOOTH_ERROR_NONE;
3544 result = __bt_init_obexd_receiver();
3545 if (result != BLUETOOTH_ERROR_NONE) {
3546 /* 20 ms * 50 = 10 seconds. During 10 seconds fail to initialize,
3547 then it is not the timing issue. Just can't use the session bus connection */
3548 if (retry_cnt > 100) {
3549 BT_ERR("Fail to init obexd receiver by 50 times.");
3551 session_reinit_timer = 0;
3555 BT_DBG("Retry to initialize the obexd receiver");
3560 session_reinit_timer = 0;
3567 /* To receive the event from bluez */
3568 int _bt_init_service_event_receiver(void)
3574 result = __bt_init_manager_receiver();
3575 retv_if(result != BLUETOOTH_ERROR_NONE, result);
3577 result = __bt_init_obexd_receiver();
3578 if (result != BLUETOOTH_ERROR_NONE) {
3579 BT_ERR("Fail to init obexd receiver");
3581 /* Try to re-initialize obexd receiver in the timer */
3582 if (session_reinit_timer > 0)
3583 g_source_remove(session_reinit_timer);
3585 session_reinit_timer = g_timeout_add(BT_SESSION_BUS_GET_TIMEOUT,
3586 (GSourceFunc)__bt_reinit_obexd_receiver, NULL);
3591 return BLUETOOTH_ERROR_NONE;
3594 void _bt_deinit_service_event_receiver(void)
3598 _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3600 _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3602 __bt_register_dump_subscribe_signal(manager_conn, FALSE);
3605 g_object_unref(manager_conn);
3606 manager_conn = NULL;
3610 g_object_unref(obexd_conn);
3615 g_source_remove(event_id);