2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <vconf-internal-bt-keys.h>
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
27 #include "bt-service-common.h"
28 #include "bt-service-event.h"
29 #include "bt-service-main.h"
30 #include "bt-service-adapter.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-device.h"
33 #include "bt-service-avrcp.h"
34 #include "bt-service-obex-server.h"
35 #include "bt-service-rfcomm-server.h"
36 #include "bt-service-audio.h"
37 #include "bt-service-agent.h"
38 #include "bt-service-pbap.h"
39 #include "bt-service-headset-connection.h"
40 #include "bt-service-avrcp-controller.h"
42 #include "bt-service-opp-client.h"
44 #ifdef TIZEN_DPM_ENABLE
45 #include "bt-service-dpm.h"
48 #define DBUS_TIMEOUT 20 * 1000 /* 20 Sec */
49 static GDBusConnection *manager_conn;
50 static GDBusConnection *obexd_conn;
51 static GDBusConnection *opc_obexd_conn;
53 static GList *p_cache_list = NULL;
55 static guint event_id;
56 guint nap_connected_device_count = 0;
57 static guint hid_connected_device_count = 0;
58 static GList *p_adv_ind_list;
61 bt_remote_dev_info_t *dev_info;
65 * obexd connection type
73 OBEX_PCSUITE = (1 << 6),
74 OBEX_SYNCEVOLUTION = (1 << 7),
76 } bluetooth_obex_connection_type_t;
78 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path);
79 void _bt_opc_property_changed_event(GVariant *msg, char *path);
80 int _bt_register_service_event(GDBusConnection *g_conn, int event_type);
81 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type);
82 void _bt_opp_client_event_deinit(void);
83 void _bt_handle_network_client_event(GVariant *msg_iter,
85 void __bt_gatt_char_property_changed_event(GVariant *msg_iter,
88 static void __bt_free_bt_le_adv_info_t(bt_le_adv_info_t *adv_info)
90 g_free(adv_info->addr);
91 g_free(adv_info->data);
95 static bt_le_adv_info_t *__bt_get_adv_ind_info(char *addr)
98 bt_le_adv_info_t *adv_info = NULL;
99 GList *current = g_list_first((GList *)p_adv_ind_list);
100 while (current && current->data) {
101 adv_info = (bt_le_adv_info_t *)current->data;
102 retv_if(adv_info && !g_strcmp0(adv_info->addr, addr), adv_info);
103 current = g_list_next(current);
108 static void __bt_add_adv_ind_info(bt_le_adv_info_t *adv_info)
111 if (__bt_get_adv_ind_info(adv_info->addr) != NULL) {
112 BT_ERR("adv_info is already added");
113 __bt_free_bt_le_adv_info_t(adv_info);
116 p_adv_ind_list = g_list_append(p_adv_ind_list, adv_info);
119 static void __bt_del_adv_ind_info(char *addr)
122 ret_if(!p_adv_ind_list);
123 bt_le_adv_info_t *adv_info = NULL;
124 GList *current = g_list_first((GList *)p_adv_ind_list);
125 while (current && current->data) {
126 adv_info = (bt_le_adv_info_t *)current->data;
127 if (adv_info && !g_strcmp0(adv_info->addr, addr)) {
128 p_adv_ind_list = g_list_remove(p_adv_ind_list, adv_info);
129 __bt_free_bt_le_adv_info_t(adv_info);
132 current = g_list_next(current);
136 static void __bt_free_cache_info(bt_cache_info_t *cache_info)
138 ret_if(cache_info == NULL);
139 _bt_free_device_info(cache_info->dev_info);
143 static gboolean __bt_parse_device_properties(GVariant *item,
144 bt_remote_dev_info_t *dev_info)
153 g_variant_iter_init(&iter, item);
154 while (g_variant_iter_loop(&iter, "{sv}", &key, &val)) {
155 if (strcasecmp(key, "Address") == 0) {
156 dev_info->address = g_variant_dup_string(val, &len);
157 } else if (strcasecmp(key, "Class") == 0) {
158 dev_info->class = g_variant_get_uint32(val);
159 } else if (strcasecmp(key, "name") == 0) {
160 if (dev_info->name == NULL)
161 dev_info->name = g_variant_dup_string(val, &len);
162 } else if (strcasecmp(key, "Connected") == 0) {
163 dev_info->connected = g_variant_get_uint32(val);
164 } else if (strcasecmp(key, "paired") == 0) {
165 dev_info->paired = g_variant_get_boolean(val);
166 } else if (strcasecmp(key, "Trusted") == 0) {
167 dev_info->trust = g_variant_get_boolean(val);
168 } else if (strcasecmp(key, "RSSI") == 0) {
169 dev_info->rssi = g_variant_get_int16(val);
170 } else if (strcasecmp(key, "LastAddrType") == 0) {
171 dev_info->addr_type = g_variant_get_byte(val);
172 } else if (strcasecmp(key, "UUIDs") == 0) {
176 size = g_variant_get_size(val);
179 uuid_value = (char **)g_variant_get_strv(val, &size);
180 if (dev_info->uuids == NULL)
181 dev_info->uuids = g_malloc0(sizeof(char *) * size);
183 for (i = 0; uuid_value[i] != NULL; i++) {
184 dev_info->uuid_count++;
185 dev_info->uuids[i] = g_strdup(uuid_value[i]);
189 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
190 g_variant_get(val, "(i)", &dev_info->manufacturer_data_len);
191 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
192 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
193 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
196 if (dev_info->manufacturer_data_len == 0)
197 dev_info->manufacturer_data = g_strdup("");
198 } else if (strcasecmp(key, "ManufacturerData") == 0) {
200 GVariant *manufacturer_var;
201 g_variant_get(val, "@ay", &manufacturer_var);
202 len = g_variant_get_size(manufacturer_var);
204 char *manufacturer_data = (char *)g_variant_get_data(manufacturer_var);
205 dev_info->manufacturer_data = g_malloc0(len);
206 if (dev_info->manufacturer_data)
207 memcpy(dev_info->manufacturer_data, manufacturer_data,
210 g_variant_unref(manufacturer_var);
218 static gboolean __bt_parse_interface(GVariant *msg,
219 bt_remote_dev_info_t *dev_info)
222 GVariant *optional_param;
225 char *interface_name = NULL;
226 GVariant *inner_iter = NULL;
227 g_variant_get(msg, "(&o@a{sa{sv}})",
228 &path, &optional_param);
229 g_variant_iter_init(&iter, optional_param);
231 while ((child = g_variant_iter_next_value(&iter))) {
232 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
233 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
234 BT_DBG("Found a device: %s", path);
235 if (__bt_parse_device_properties(inner_iter,
236 dev_info) == FALSE) {
237 g_variant_unref(inner_iter);
238 g_variant_unref(child);
239 g_variant_unref(optional_param);
240 BT_ERR("Fail to parse the properies");
243 g_variant_unref(inner_iter);
244 g_variant_unref(child);
245 g_variant_unref(optional_param);
249 g_variant_unref(inner_iter);
250 g_variant_unref(child);
253 g_variant_unref(optional_param);
258 static int __bt_get_owner_info(GVariant *msg, char **name,
259 char **previous, char **current)
261 g_variant_get(msg, "(sss)", name, previous, current);
262 return BLUETOOTH_ERROR_NONE;
265 static int __bt_get_agent_signal_info(GVariant *msg, char **address,
266 char **name, char **uuid)
268 g_variant_get(msg, "(sss)", address, name, uuid);
269 return BLUETOOTH_ERROR_NONE;
272 void __bt_set_device_values(gboolean connected, int state)
274 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
276 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0) {
277 BT_ERR("vconf_get_int failed");
280 if (connected == TRUE)
281 bt_device_state |= state;
282 else if (bt_device_state & state)
283 bt_device_state ^= state;
285 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
286 BT_ERR("vconf_set_int failed");
290 gboolean _bt_discovery_finished_cb(gpointer user_data)
292 int result = BLUETOOTH_ERROR_NONE;
294 GVariant *param = NULL;
295 if (_bt_get_discovering_property(DISCOVERY_ROLE_BREDR) == FALSE) {
296 if (_bt_get_cancel_by_user() == TRUE) {
297 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
300 _bt_set_cancel_by_user(FALSE);
301 _bt_set_discovery_status(FALSE);
302 param = g_variant_new("(i)", result);
303 _bt_send_event(BT_ADAPTER_EVENT,
304 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
311 static gboolean __bt_le_discovery_finished_cb(gpointer user_data)
313 int result = BLUETOOTH_ERROR_NONE;
315 GVariant *param = NULL;
316 if (_bt_get_discovering_property(DISCOVERY_ROLE_LE) == FALSE) {
317 if (_bt_get_cancel_by_user() == TRUE) {
318 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
321 _bt_set_cancel_by_user(FALSE);
322 _bt_disable_all_scanner_status();
323 _bt_set_le_scan_status(FALSE);
324 param = g_variant_new("(i)", result);
325 _bt_send_event(BT_LE_ADAPTER_EVENT,
326 BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED,
333 void __bt_update_remote_cache_devinfo(const char *address, gboolean paired_status)
337 ret_if(address == NULL);
340 bt_cache_info_t *cache_info;
341 bt_remote_dev_info_t *dev_info;
343 node = g_list_first(p_cache_list);
345 while (node != NULL) {
346 cache_info = (bt_cache_info_t *)node->data;
348 if (cache_info == NULL) {
349 node = g_list_next(node);
353 dev_info = cache_info->dev_info;
354 if (strcasecmp(dev_info->address,
356 BT_DBG("Device Found");
357 if (paired_status == TRUE)
358 cache_info->dev_info->paired = TRUE;
360 cache_info->dev_info->paired = FALSE;
363 node = g_list_next(node);
368 static void __bt_device_remote_connected_properties(
369 bt_remote_dev_info_t *remote_dev_info,
370 char *address, gboolean connected)
372 int result = BLUETOOTH_ERROR_NONE;
374 GVariant *param = NULL;
377 if (remote_dev_info->uuid_count > 0) {
378 for (i = 0; i < remote_dev_info->uuid_count; i++) {
379 char *uuid = remote_dev_info->uuids[i];
380 if (strcasecmp(uuid, HID_UUID) == 0) {
381 int event = BLUETOOTH_EVENT_NONE;
383 event = (connected == TRUE) ?
384 BLUETOOTH_HID_CONNECTED :
385 BLUETOOTH_HID_DISCONNECTED;
386 param = g_variant_new("(is)", result,
388 _bt_send_event(BT_HID_EVENT, event,
398 void _bt_handle_adapter_event(GVariant *msg, const char *member)
402 int result = BLUETOOTH_ERROR_NONE;
403 GVariant *param = NULL;
404 ret_if(member == NULL);
406 if (strcasecmp(member, "DeviceCreated") == 0) {
407 char *object_path = NULL;
409 bt_remote_dev_info_t *remote_dev_info;
411 ret_if(_bt_is_device_creating() == FALSE);
413 /* Bonding from remote device */
414 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
416 g_variant_get(msg, "(&o)", &object_path);
417 _bt_convert_device_path_to_address((const char*)object_path, address);
419 remote_dev_info = _bt_get_remote_device_info(address);
420 if (remote_dev_info == NULL) {
425 _bt_free_device_info(remote_dev_info);
427 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
428 char *object_path = NULL;
430 bt_cache_info_t *cache_info;
431 bt_remote_dev_info_t *dev_info;
434 /* Bonding from remote device */
435 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
436 g_variant_get(msg, "(&o)", &object_path);
438 /* Fix : NULL_RETURNS */
442 _bt_convert_device_path_to_address((const char *)object_path, address);
444 node = g_list_first(p_cache_list);
446 while (node != NULL) {
447 cache_info = (bt_cache_info_t *)node->data;
449 if (cache_info == NULL) {
450 node = g_list_next(node);
454 dev_info = cache_info->dev_info;
455 if (strcasecmp(dev_info->address,
457 p_cache_list = g_list_remove(p_cache_list,
459 __bt_free_cache_info(cache_info);
462 node = g_list_next(node);
465 } else if (strcasecmp(member, "AdvertisingEnabled") == 0) {
466 BT_DBG("Advertising Enabled");
470 gboolean status = FALSE;
472 g_variant_get(msg, "(ib)", &slot_id, &status);
474 BT_DBG("Advertising Enabled : slot_id [%d] status [%d]", slot_id, status);
476 /* Send event to application */
477 _bt_set_advertising_status(slot_id, status);
479 adv_handle = _bt_get_adv_slot_adv_handle(slot_id);
482 event = BLUETOOTH_EVENT_ADVERTISING_STARTED;
484 event = BLUETOOTH_EVENT_ADVERTISING_STOPPED;
485 param = g_variant_new("(ii)", result,
490 sender = _bt_get_adv_slot_owner(slot_id);
491 _bt_send_event_to_dest(sender, BT_ADAPTER_EVENT,
495 _bt_send_event(BT_ADAPTER_EVENT, event, param);
498 if (event == BLUETOOTH_EVENT_ADVERTISING_STOPPED)
499 _bt_unregister_adv_slot_owner(slot_id);
500 } else if (strcasecmp(member, "RssiEnabled") == 0) {
501 BT_DBG("RSSI Enabled");
502 gboolean status = FALSE;
503 char *address = NULL;
505 g_variant_get(msg, "(sib)", &address, &link_type, &status);
507 BT_DBG("RSSI Enabled [%s %d]", address, status);
508 param = g_variant_new("(isib)", result,
509 address, link_type, status);
510 _bt_send_event(BT_DEVICE_EVENT,
511 BLUETOOTH_EVENT_RSSI_ENABLED,
514 } else if (strcasecmp(member, "RssiAlert") == 0) {
515 BT_DBG("RSSI Alert");
519 char *address = NULL;
520 g_variant_get(msg, "(siii)", &address, &link_type, &alert_type, &rssi_dbm);
522 BT_DBG("RSSI Alert: [Address %s LinkType %d] [Type %d DBM %d]",
523 address, alert_type, rssi_dbm);
524 param = g_variant_new("(isiii)", result,
525 address, link_type, alert_type, rssi_dbm);
526 _bt_send_event(BT_DEVICE_EVENT,
527 BLUETOOTH_EVENT_RSSI_ALERT,
530 } else if (strcasecmp(member, "RawRssi") == 0) {
534 char *address = NULL;
535 g_variant_get(msg, "(sii)", &address, &link_type, &rssi_dbm);
537 BT_DBG("Raw RSSI: [Address %s] [Link Type %d][RSSI DBM %d]",
538 address, link_type, rssi_dbm);
539 param = g_variant_new("(isii)", result,
540 address, link_type, rssi_dbm);
541 _bt_send_event(BT_DEVICE_EVENT,
542 BLUETOOTH_EVENT_RAW_RSSI,
545 } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
546 BT_ERR_C("Hardware error received from BLUEZ");
547 _bt_recover_adapter();
548 } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
549 BT_ERR_C("Tx timeout error received from BLUEZ");
550 _bt_recover_adapter();
555 static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
557 GDBusProxy *adapter_proxy;
559 int result = BLUETOOTH_ERROR_NONE;
560 GVariantIter value_iter;
561 GVariant *val = NULL;
563 char *property = NULL;
564 GVariant *param = NULL;
565 g_variant_iter_init(&value_iter, msg);
566 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
567 BT_INFO("Property %s", property);
569 if (strcasecmp(property, "Discovering") == 0) {
570 gboolean discovering = FALSE;
571 g_variant_get(val, "b", &discovering);
572 /* Send event to application */
573 BT_DBG("Discovering %d", discovering);
574 if (discovering == TRUE) {
575 _bt_set_discovery_status(TRUE);
576 param = g_variant_new("(i)", result);
577 _bt_send_event(BT_ADAPTER_EVENT,
578 BLUETOOTH_EVENT_DISCOVERY_STARTED,
581 ret_if(event_id > 0);
583 adapter_proxy = _bt_get_adapter_proxy();
584 ret_if(adapter_proxy == NULL);
586 /* Need to stop searching */
587 g_dbus_proxy_call_sync(adapter_proxy, "StopDiscovery",
589 G_DBUS_CALL_FLAGS_NONE,
593 BT_ERR("Dbus Error : %s", err->message);
597 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
598 (GSourceFunc)_bt_discovery_finished_cb, NULL);
600 } else if (strcasecmp(property, "LEDiscovering") == 0) {
601 gboolean le_discovering = FALSE;
603 g_variant_get(val, "b", &le_discovering);
604 /* Send event to application */
605 if (le_discovering == TRUE) {
606 _bt_set_le_scan_status(TRUE);
607 param = g_variant_new("(i)", result);
608 _bt_send_event(BT_LE_ADAPTER_EVENT,
609 BLUETOOTH_EVENT_LE_DISCOVERY_STARTED,
612 ret_if(event_id > 0);
614 adapter_proxy = _bt_get_adapter_proxy();
615 ret_if(adapter_proxy == NULL);
617 /* Need to stop searching */
618 g_dbus_proxy_call_sync(adapter_proxy, "StopLEDiscovery",
620 G_DBUS_CALL_FLAGS_NONE,
624 BT_ERR("Dbus Error %s", err->message);
628 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
629 (GSourceFunc)__bt_le_discovery_finished_cb, NULL);
631 } else if (strcasecmp(property, "Name") == 0) {
633 g_variant_get(val, "s", &name);
634 param = g_variant_new("(is)", result, name);
635 /* Send event to application */
636 _bt_send_event(BT_ADAPTER_EVENT,
637 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
640 } else if (strcasecmp(property, "Alias") == 0) {
642 g_variant_get(val, "s", &alias);
643 param = g_variant_new("(is)", result, alias);
644 /* Send event to application */
645 _bt_send_event(BT_ADAPTER_EVENT,
646 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
649 } else if (strcasecmp(property, "Discoverable") == 0) {
650 gboolean discoverable = FALSE;
652 g_variant_get(val, "b", &discoverable);
653 BT_DBG("discoverable %d", discoverable);
655 if (discoverable == FALSE) {
656 if (_bt_get_discoverable_timeout_property() > 0) {
658 adapter_proxy = _bt_get_adapter_properties_proxy();
659 ret_if(adapter_proxy == NULL);
660 g_dbus_proxy_call_sync(adapter_proxy, "Set",
661 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
662 "DiscoverableTimeout",
663 g_variant_new("i", time)),
664 G_DBUS_CALL_FLAGS_NONE,
669 BT_ERR("StopLEDiscovery Failed: %s", err->message);
674 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
676 /* Send event to application */
677 BT_INFO("[Connectable]");
678 param = g_variant_new("(in)", result, mode);
679 _bt_send_event(BT_ADAPTER_EVENT,
680 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
683 _bt_get_discoverable_mode(&mode);
685 /* Event will be sent by "DiscoverableTimeout" signal */
686 if (mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
688 g_variant_unref(val);
692 /* Send event to application */
693 BT_INFO("[General Discoverable]");
694 param = g_variant_new("(in)", result, mode);
695 _bt_send_event(BT_ADAPTER_EVENT,
696 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
699 } else if (strcasecmp(property, "DiscoverableTimeout") == 0) {
700 _bt_get_discoverable_mode(&mode);
702 /* Event was already sent by "Discoverable" signal */
703 if (mode == BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE ||
704 mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
706 g_variant_unref(val);
710 /* Send event to application */
711 BT_INFO("[Limited Discoverable (Timeout %u secs)]",
712 _bt_get_discoverable_timeout_property());
713 param = g_variant_new("(in)", result, mode);
714 _bt_send_event(BT_ADAPTER_EVENT,
715 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
717 } else if (strcasecmp(property, "Powered") == 0) {
718 /* TODO: Need to check this operation!! */
719 gboolean powered = FALSE;
722 g_variant_get(val, "b", &powered);
723 BT_DBG("Powered = %d", powered);
724 if (powered == FALSE) {
726 _bt_handle_adapter_removed();
728 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_state) == 0 &&
729 bt_state != VCONFKEY_BT_STATUS_OFF) {
730 _bt_disable_adapter();
733 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_state) == 0 &&
734 bt_state != VCONFKEY_BT_LE_STATUS_OFF) {
735 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
739 _bt_handle_adapter_added();
742 } else if (strcasecmp(property, "Connectable") == 0) {
743 gboolean connectable = FALSE;
745 g_variant_get(val, "b", &connectable);
747 BT_DBG("Connectable property is changed : %d", connectable);
748 param = g_variant_new("(b)", connectable);
749 _bt_send_event(BT_ADAPTER_EVENT,
750 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
753 if (_bt_adapter_get_status() == BT_DEACTIVATING &&
754 _bt_adapter_get_le_status() == BT_LE_ACTIVATED &&
756 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
758 } else if (strcasecmp(property, "SupportedLEFeatures") == 0) {
761 GVariantIter *iter = NULL;
762 g_variant_get(val, "as", &iter);
764 while (g_variant_iter_loop(iter, "s", &name)) {
765 BT_DBG("name = %s", name);
766 g_variant_iter_loop(iter, "s", &value);
767 BT_DBG("Value = %s", value);
768 if (FALSE == _bt_update_le_feature_support(name, value))
769 BT_INFO("Fail to update LE feature info");
771 g_variant_iter_free(iter);
773 } else if (strcasecmp(property, "IpspInitStateChanged") == 0) {
774 gboolean ipsp_initialized = FALSE;
776 g_variant_get(val, "b", &ipsp_initialized);
777 BT_DBG("IPSP init state changed: %d", ipsp_initialized);
778 param = g_variant_new("(b)", ipsp_initialized);
780 /* Send event to application */
781 _bt_send_event(BT_ADAPTER_EVENT,
782 BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,
788 static void __bt_obex_property_changed_event(GVariant *msg, const char *path)
792 GVariantIter value_iter;
793 GVariant *child = NULL, *val = NULL;
794 char *property = NULL;
795 g_variant_iter_init(&value_iter, msg);
796 while ((child = g_variant_iter_next_value(&value_iter))) {
797 g_variant_get(child, "{sv}", &property, &val);
799 ret_if(property == NULL);
801 BT_DBG("property :%s", property);
803 if (strcasecmp(property, "Status") == 0) {
805 g_variant_get(val, "s", &status);
807 if (strcasecmp(status, "active") == 0) {
808 _bt_obex_transfer_started(path);
809 } else if (strcasecmp(status, "complete") == 0) {
810 _bt_obex_transfer_completed(path, TRUE);
811 _bt_pbap_obex_transfer_completed(path, TRUE);
812 } else if (strcasecmp(status, "error") == 0) {
813 _bt_obex_transfer_completed(path, FALSE);
814 _bt_pbap_obex_transfer_completed(path, FALSE);
817 } else if (strcasecmp(property, "Transferred") == 0) {
818 static int transferred = 0;
819 g_variant_get(val, "t", &transferred);
821 _bt_obex_transfer_progress(path, transferred);
824 g_variant_unref(val);
825 g_variant_unref(child);
830 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
835 int result = BLUETOOTH_ERROR_NONE;
836 GVariantIter value_iter;
838 char *property = NULL;
840 GVariant *param = NULL;
841 bt_remote_dev_info_t *remote_dev_info;
842 g_variant_iter_init(&value_iter, msg);
843 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
844 BT_DBG("Property %s", property);
845 if (strcasecmp(property, "Connected") == 0) {
848 g_variant_get(val, "i", &connected);
850 event = (connected != BLUETOOTH_CONNECTED_LINK_NONE) ?
851 BLUETOOTH_EVENT_DEVICE_CONNECTED :
852 BLUETOOTH_EVENT_DEVICE_DISCONNECTED;
854 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
856 _bt_convert_device_path_to_address(path, address);
858 BT_DBG("connected: %d", connected);
859 BT_DBG("address: %s", address);
861 remote_dev_info = _bt_get_remote_device_info(address);
863 if (remote_dev_info != NULL) {
864 __bt_device_remote_connected_properties(
865 remote_dev_info, address,
866 connected != BLUETOOTH_CONNECTED_LINK_NONE ?
868 _bt_free_device_info(remote_dev_info);
870 param = g_variant_new("(is)", result, address);
871 /* Send event to application */
872 _bt_send_event(BT_DEVICE_EVENT,
876 } else if (strcasecmp(property, "RSSI") == 0) {
877 bt_remote_dev_info_t *remote_dev_info;
879 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
881 _bt_convert_device_path_to_address(path, address);
882 BT_DBG("address: %s", address);
884 remote_dev_info = _bt_get_remote_device_info(address);
885 if (remote_dev_info == NULL) {
887 g_variant_unref(val);
891 BT_DBG("Address type %d", remote_dev_info->addr_type);
893 if (remote_dev_info->addr_type == 0) {
894 BT_DBG("Name %s", remote_dev_info->name);
896 #ifdef TIZEN_DPM_ENABLE
897 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
899 bluetooth_device_class_t device_class;
900 _bt_divide_device_class(&device_class, remote_dev_info->class);
902 if (device_class.major_class ==
903 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
904 _bt_free_device_info(remote_dev_info);
906 g_variant_unref(val);
913 GVariant *uuids = NULL;
914 GVariantBuilder *builder = NULL;
916 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
917 for (i = 0; i < remote_dev_info->uuid_count; i++) {
918 g_variant_builder_add(builder, "s",
919 remote_dev_info->uuids[i]);
921 uuids = g_variant_new("as", builder);
922 g_variant_builder_unref(builder);
923 GVariant *manufacturer_data = NULL;
924 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
925 remote_dev_info->manufacturer_data,
926 remote_dev_info->manufacturer_data_len,
929 param = g_variant_new("(isunsbub@asn@ay)", result,
930 remote_dev_info->address,
931 remote_dev_info->class,
932 remote_dev_info->rssi,
933 remote_dev_info->name,
934 remote_dev_info->paired,
935 remote_dev_info->connected,
936 remote_dev_info->trust,
938 remote_dev_info->manufacturer_data_len,
941 _bt_send_event(BT_ADAPTER_EVENT,
942 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
946 _bt_free_device_info(remote_dev_info);
947 } else if (strcasecmp(property, "GattConnected") == 0) {
948 gboolean gatt_connected = FALSE;
950 g_variant_get(val, "b", &gatt_connected);
952 event = gatt_connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
953 BLUETOOTH_EVENT_GATT_DISCONNECTED;
955 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
957 _bt_convert_device_path_to_address(path, address);
959 BT_DBG("gatt_connected: %d", gatt_connected);
960 BT_DBG("address: %s", address);
961 param = g_variant_new("(is)", result, address);
962 /* Send event to application */
963 _bt_send_event(BT_DEVICE_EVENT,
967 } else if (strcasecmp(property, "Paired") == 0) {
968 gboolean paired = FALSE;
969 bt_remote_dev_info_t *remote_dev_info;
970 g_variant_get(val, "b", &paired);
971 _bt_agent_set_canceled(FALSE);
972 /* BlueZ sends paired signal for each paired device */
973 /* during activation, We should ignore this, otherwise*/
974 /* application thinks that a new device got paired */
975 if (_bt_adapter_get_status() != BT_ACTIVATED) {
976 BT_DBG("BT is not activated, so ignore this");
978 g_variant_unref(val);
982 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
984 _bt_convert_device_path_to_address(path, address);
986 remote_dev_info = _bt_get_remote_device_info(address);
987 if (remote_dev_info == NULL) {
989 g_variant_unref(val);
994 if (paired == FALSE) {
995 BT_INFO("Unpaired: %s", address);
996 __bt_update_remote_cache_devinfo(address, FALSE);
997 param = g_variant_new("(is)", result, address);
998 _bt_send_event(BT_ADAPTER_EVENT,
999 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
1002 BT_INFO("Paired: %s", address);
1003 __bt_update_remote_cache_devinfo(address, TRUE);
1005 if (_bt_is_device_creating() == TRUE) {
1006 BT_DBG("Try to Pair by me");
1007 _bt_free_device_info(remote_dev_info);
1010 g_variant_unref(val);
1013 GVariant *uuids = NULL;
1014 GVariantBuilder *builder = NULL;
1016 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1017 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1018 g_variant_builder_add(builder, "s",
1019 remote_dev_info->uuids[i]);
1021 uuids = g_variant_new("as", builder);
1022 g_variant_builder_unref(builder);
1023 GVariant *manufacturer_data = NULL;
1024 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1025 remote_dev_info->manufacturer_data,
1026 remote_dev_info->manufacturer_data_len,
1030 param = g_variant_new("(isunsbub@asn@ay)", result,
1031 address, remote_dev_info->class,
1032 remote_dev_info->rssi,
1033 remote_dev_info->name,
1034 remote_dev_info->paired,
1035 remote_dev_info->connected,
1036 remote_dev_info->trust,
1038 remote_dev_info->manufacturer_data_len,
1040 _bt_send_event(BT_ADAPTER_EVENT,
1041 BLUETOOTH_EVENT_BONDING_FINISHED,
1044 _bt_free_device_info(remote_dev_info);
1046 } else if (strcasecmp(property, "LegacyPaired") == 0) {
1047 gboolean paired = FALSE;
1048 bt_remote_dev_info_t *remote_dev_info;
1050 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1051 BT_DBG("BT is not activated, so ignore this");
1053 g_variant_unref(val);
1057 g_variant_get(val, "b", &paired);
1058 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1059 BT_DBG("LegacyPaired: %d", paired);
1060 _bt_convert_device_path_to_address(path, address);
1062 remote_dev_info = _bt_get_remote_device_info(address);
1063 if (remote_dev_info == NULL) {
1066 g_variant_unref(val);
1070 BT_DBG("LegacyPairing Failed with %s. Show Error Popup",
1071 remote_dev_info->name);
1072 _bt_launch_system_popup(BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE,
1073 remote_dev_info->name, NULL, NULL, NULL);
1075 _bt_free_device_info(remote_dev_info);
1077 } else if (strcasecmp(property, "Trusted") == 0) {
1078 gboolean trusted = FALSE;
1080 g_variant_get(val, "b", &trusted);
1082 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1083 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1085 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1087 _bt_convert_device_path_to_address(path, address);
1089 BT_DBG("trusted: %d", trusted);
1090 BT_DBG("address: %s", address);
1091 param = g_variant_new("(is)", result, address);
1092 /* Send event to application */
1093 _bt_send_event(BT_DEVICE_EVENT,
1097 } else if (strcasecmp(property, "IpspConnected") == 0) {
1098 gboolean connected = FALSE;
1100 g_variant_get(val, "b", &connected);
1103 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
1104 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
1106 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1108 _bt_convert_device_path_to_address(path, address);
1110 BT_DBG("Ipspconnected: %d", connected);
1111 BT_DBG("address: %s", address);
1112 param = g_variant_new("(is)", result, address);
1114 /* Send event to application */
1115 _bt_send_event(BT_DEVICE_EVENT,
1119 } else if (strcasecmp(property, "IpspBtInterfaceInfo") == 0) {
1120 char *ifname = NULL;
1122 g_variant_get(val, "s", &ifname);
1124 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1126 _bt_convert_device_path_to_address(path, address);
1128 BT_DBG("Ipsp BT Interface Name: %s", ifname);
1129 BT_DBG("address: %s", address);
1130 param = g_variant_new("(iss)", result, address, ifname);
1132 /* Send event to application */
1133 _bt_send_event(BT_DEVICE_EVENT,
1134 BLUETOOTH_EVENT_IPSP_INTERFACE_INFO,
1142 static void __bt_media_control_changed_event(GVariant *msg, const char *path)
1145 int result = BLUETOOTH_ERROR_NONE;
1146 GVariantIter value_iter;
1147 char *property = NULL;
1149 GVariant *val = NULL;
1150 GVariant *child = NULL;
1151 bt_remote_dev_info_t *remote_dev_info;
1152 GVariant *param = NULL;
1153 g_variant_iter_init(&value_iter, msg);
1154 while ((child = g_variant_iter_next_value(&value_iter))) {
1155 g_variant_get(child, "{sv}", &property, &val);
1156 BT_INFO("Property %s", property);
1157 if (strcasecmp(property, "Connected") == 0) {
1158 gboolean connected = FALSE;
1160 g_variant_get(val, "b", &connected);
1162 event = connected ? BLUETOOTH_EVENT_AVRCP_CONNECTED :
1163 BLUETOOTH_EVENT_AVRCP_DISCONNECTED;
1165 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1167 _bt_convert_device_path_to_address(path, address);
1169 BT_DBG("connected: %d", connected);
1170 BT_DBG("address: %s", address);
1172 remote_dev_info = _bt_get_remote_device_info(address);
1174 if (remote_dev_info != NULL) {
1175 __bt_device_remote_connected_properties(
1176 remote_dev_info, address, connected);
1177 _bt_free_device_info(remote_dev_info);
1179 param = g_variant_new("(is)", result, address);
1180 /* Send event to application */
1181 _bt_send_event(BT_AVRCP_EVENT,
1187 g_variant_unref(child);
1188 g_variant_unref(val);
1193 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
1195 char *interface_name = NULL;
1196 GVariant *val = NULL;
1198 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &val, NULL);
1200 if (strcasecmp(interface_name, BT_ADAPTER_INTERFACE) == 0) {
1201 __bt_adapter_property_changed_event(val,
1203 } else if (strcasecmp(interface_name, BT_DEVICE_INTERFACE) == 0) {
1204 __bt_device_property_changed_event(val, object_path);
1205 } else if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1206 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
1207 __bt_obex_property_changed_event(val,
1209 } else if (strcasecmp(interface_name, BT_MEDIA_CONTROL_INTERFACE) == 0) {
1210 __bt_media_control_changed_event(val,
1212 } else if (strcasecmp(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
1213 _bt_handle_avrcp_control_event(val,
1215 } else if (strcasecmp(interface_name, BT_NETWORK_CLIENT_INTERFACE) == 0) {
1216 BT_DBG("BT_NETWORK_CLIENT_INTERFACE");
1217 _bt_handle_network_client_event(val,
1219 } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
1220 __bt_gatt_char_property_changed_event(val,
1223 g_variant_unref(val);
1226 void __bt_opc_property_changed_event(GVariant *msg,
1229 GVariantIter value_iter;
1230 char *property = NULL;
1231 GVariant *val = NULL;
1232 GVariant *child = NULL;
1234 g_variant_iter_init(&value_iter, msg);
1235 while ((child = g_variant_iter_next_value(&value_iter))) {
1236 g_variant_get(child, "{sv}", &property, &val);
1237 ret_if(property == NULL);
1239 if (strcasecmp(property, "Status") == 0) {
1240 char *status = NULL;
1241 g_variant_get(val, "s", &status);
1242 BT_DBG("Status is %s", status);
1244 if (strcasecmp(status, "active") == 0) {
1245 _bt_obex_client_started(path);
1246 } else if (strcasecmp(status, "complete") == 0) {
1247 _bt_obex_client_completed(path, TRUE);
1248 } else if (strcasecmp(status, "error") == 0) {
1249 _bt_obex_client_completed(path, FALSE);
1252 } else if (strcasecmp(property, "Transferred") == 0) {
1253 static int transferred = 0;
1254 g_variant_get(val, "t", &transferred);
1256 _bt_obex_client_progress(path, transferred);
1258 BT_DBG("property : [%s]", property);
1261 g_variant_unref(child);
1262 g_variant_unref(val);
1266 void _bt_opc_property_changed_event(GVariant *msg, char *path)
1268 char *interface_name = NULL;
1269 GVariant *value = NULL;
1270 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1271 BT_INFO("interface_name = %s", interface_name);
1272 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1273 __bt_opc_property_changed_event(value,
1276 BT_DBG("interface_name : [%s]", interface_name);
1278 g_variant_unref(value);
1282 void _bt_handle_input_event(GVariant *msg, const char *path)
1284 int result = BLUETOOTH_ERROR_NONE;
1285 gboolean property_flag = FALSE;
1286 GVariantIter value_iter;
1287 char *property = NULL;
1288 GVariant *child = NULL, *val = NULL;
1289 bt_remote_dev_info_t *remote_dev_info;
1290 GVariant *param = NULL;
1291 g_variant_iter_init(&value_iter, msg);
1292 while ((child = g_variant_iter_next_value(&value_iter))) {
1293 g_variant_get(child, "{sv}", &property, &val);
1295 ret_if(property == NULL);
1297 if (strcasecmp(property, "Connected") == 0) {
1298 int event = BLUETOOTH_EVENT_NONE;
1300 g_variant_get(val, "b", &property_flag);
1302 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1304 _bt_convert_device_path_to_address(path, address);
1306 event = (property_flag == TRUE) ?
1307 BLUETOOTH_HID_CONNECTED :
1308 BLUETOOTH_HID_DISCONNECTED;
1309 param = g_variant_new("(is)", result, address);
1310 _bt_send_event(BT_HID_EVENT, event,
1312 /* Check HID connection type (Keyboard or Mouse) and update the status */
1313 remote_dev_info = _bt_get_remote_device_info(address);
1315 if (property_flag == TRUE) {
1316 hid_connected_device_count++;
1317 __bt_set_device_values(TRUE,
1318 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1320 hid_connected_device_count--;
1321 if (hid_connected_device_count == 0)
1322 __bt_set_device_values(FALSE,
1323 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1326 if (remote_dev_info != NULL) {
1327 BT_DBG("HID device class [%x]", remote_dev_info->class);
1328 if (remote_dev_info->class &
1329 BLUETOOTH_DEVICE_MINOR_CLASS_KEY_BOARD) {
1330 #ifdef ENABLE_TIZEN_2_4
1331 __bt_set_device_values(property_flag,
1332 VCONFKEY_BT_DEVICE_HID_KEYBOARD_CONNECTED);
1337 if (remote_dev_info->class &
1338 BLUETOOTH_DEVICE_MINOR_CLASS_POINTING_DEVICE) {
1339 #ifdef ENABLE_TIZEN_2_4
1340 __bt_set_device_values(property_flag,
1341 VCONFKEY_BT_DEVICE_HID_MOUSE_CONNECTED);
1344 _bt_free_device_info(remote_dev_info);
1349 g_variant_unref(val);
1350 g_variant_unref(child);
1354 void _bt_handle_network_server_event(GVariant *msg, const char *member)
1356 int result = BLUETOOTH_ERROR_NONE;
1357 char *address = NULL;
1358 char *device = NULL;
1359 GVariant *param = NULL;
1360 ret_if(member == NULL);
1361 if (strcasecmp(member, "PeerConnected") == 0) {
1362 g_variant_get(msg, "(ss)", &device, &address);
1364 __bt_set_device_values(TRUE,
1365 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1366 param = g_variant_new("(iss)", result, device, address);
1367 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
1371 nap_connected_device_count++;
1372 } else if (strcasecmp(member, "PeerDisconnected") == 0) {
1373 g_variant_get(msg, "(ss)", &device, &address);
1374 nap_connected_device_count--;
1375 if (nap_connected_device_count == 0)
1376 __bt_set_device_values(FALSE,
1377 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1378 param = g_variant_new("(iss)", result, device, address);
1379 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
1386 void _bt_handle_network_client_event(GVariant *msg,
1391 int result = BLUETOOTH_ERROR_NONE;
1392 gboolean property_flag = FALSE;
1393 char *property = NULL;
1394 GVariant *val = NULL;
1395 GVariantIter value_iter;
1396 GVariant *param = NULL;
1397 g_variant_iter_init(&value_iter, msg);
1398 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1399 if (strcasecmp(property, "Connected") == 0) {
1400 int event = BLUETOOTH_EVENT_NONE;
1403 g_variant_get(val, "b", &property_flag);
1404 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1406 _bt_convert_device_path_to_address(path, address);
1408 BT_DBG("property_flag %d", property_flag);
1409 if (property_flag == TRUE) {
1410 event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
1411 nap_connected_device_count++;
1412 __bt_set_device_values(TRUE,
1413 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1415 event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
1416 nap_connected_device_count--;
1417 if (nap_connected_device_count == 0)
1418 __bt_set_device_values(FALSE,
1419 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1421 param = g_variant_new("(is)", result, address);
1422 _bt_send_event(BT_NETWORK_EVENT, event,
1431 void __bt_gatt_char_property_changed_event(GVariant *msg,
1434 GVariantIter value_iter;
1435 char *property = NULL;
1436 char * char_handle = NULL;
1437 GVariant *val = NULL;
1438 int result = BLUETOOTH_ERROR_NONE;
1439 GVariant *param = NULL;
1440 g_variant_iter_init(&value_iter, msg);
1441 char_handle = g_strdup(path);
1442 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1443 BT_INFO("Property %s", property);
1445 ret_if(property == NULL);
1447 if (strcasecmp(property, "Notifying") == 0) {
1448 gboolean property_flag = FALSE;
1449 g_variant_get(val, "b", &property_flag);
1450 if (property_flag == TRUE)
1451 BT_DBG("notifying is enabled");
1453 BT_DBG("notifying is disabled");
1454 } else if (strcasecmp(property, "ChangedValue") == 0) {
1456 GByteArray *gp_byte_array = NULL;
1457 BT_INFO("Type '%s'\n", g_variant_get_type_string(val));
1460 gp_byte_array = g_byte_array_new();
1461 len = g_variant_get_size(val);
1462 BT_DBG("Len = %d", len);
1463 g_byte_array_append(gp_byte_array,
1464 (const guint8 *) g_variant_get_data(val), len);
1465 if (gp_byte_array->len != 0) {
1466 GVariant *byte_array = NULL;
1467 byte_array = g_variant_new_from_data(
1468 G_VARIANT_TYPE_BYTESTRING,
1469 gp_byte_array->data,
1472 param = g_variant_new("(is@ay)", result, char_handle,
1475 /* Send event only registered client */
1476 _bt_send_char_value_changed_event(param);
1478 g_byte_array_free(gp_byte_array, TRUE);
1482 g_free(char_handle);
1485 void _bt_handle_gatt_event(GVariant *msg, const char *member, const char *path)
1487 ret_if(path == NULL);
1489 if (strcasecmp(member, "GattValueChanged") == 0) {
1492 /*** Debug only ***/
1493 GVariant *value = NULL;
1495 char *buffer = NULL;
1497 g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
1498 value_len = g_variant_get_size(value);
1499 if (value_len > 0) {
1500 char buf[8 * 5 + 1] = { 0 };
1503 buffer = (char *)g_variant_get_data(value);
1504 to = value_len > (sizeof(buf) / 5) ? sizeof(buf) / 5 : value_len;
1506 for (i = 0; i < to; i++)
1507 snprintf(&buf[i * 5], 6, "0x%02x ", buffer[i]);
1509 BT_DBG("GATT Val[%d] %s", value_len, buf);
1511 g_variant_unref(value);
1515 /* Send event only registered client */
1516 _bt_send_char_value_changed_event(msg);
1521 void _bt_handle_device_event(GVariant *msg, const char *member, const char *path)
1524 int result = BLUETOOTH_ERROR_NONE;
1527 const char *property = NULL;
1528 GVariant *param = NULL;
1529 ret_if(path == NULL);
1531 if (strcasecmp(member, "PropertyChanged") == 0) {
1533 g_variant_get(msg, "(s)", &property);
1535 ret_if(property == NULL);
1537 if (strcasecmp(property, "GattConnected") == 0) {
1538 gboolean connected = FALSE;
1540 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1541 ret_if(address == NULL);
1543 _bt_convert_device_path_to_address(path, address);
1544 g_variant_get(msg, "(b)", &connected);
1546 event = connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1547 BLUETOOTH_EVENT_GATT_DISCONNECTED;
1548 param = g_variant_new("(is)", result, address);
1549 _bt_send_event(BT_DEVICE_EVENT,
1553 } else if (strcasecmp(property, "Paired") == 0) {
1554 gboolean paired = FALSE;
1555 bt_remote_dev_info_t *remote_dev_info;
1556 g_variant_get(msg, "(b)", &paired);
1558 ret_if(paired == FALSE);
1560 /* BlueZ sends paired signal for each paired device */
1561 /* during activation, We should ignore this, otherwise*/
1562 /* application thinks that a new device got paired */
1563 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1564 BT_DBG("BT is not activated, so ignore this");
1568 if (_bt_is_device_creating() == TRUE) {
1569 BT_DBG("Try to Pair by me");
1573 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1574 ret_if(address == NULL);
1576 _bt_convert_device_path_to_address(path, address);
1578 remote_dev_info = _bt_get_remote_device_info(address);
1579 if (remote_dev_info == NULL) {
1583 GVariant *uuids = NULL;
1584 GVariantBuilder *builder = NULL;
1586 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1587 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1588 g_variant_builder_add(builder, "s",
1589 remote_dev_info->uuids[i]);
1591 uuids = g_variant_new("as", builder);
1592 g_variant_builder_unref(builder);
1593 GVariant *manufacturer_data = NULL;
1594 manufacturer_data = g_variant_new_from_data(
1595 G_VARIANT_TYPE_BYTESTRING,
1596 remote_dev_info->manufacturer_data,
1597 remote_dev_info->manufacturer_data_len,
1599 param = g_variant_new("(isunsbub@asn@ay)", result,
1601 remote_dev_info->class,
1602 remote_dev_info->rssi,
1603 remote_dev_info->name,
1604 remote_dev_info->paired,
1605 remote_dev_info->connected,
1606 remote_dev_info->trust,
1608 remote_dev_info->manufacturer_data_len,
1610 _bt_send_event(BT_ADAPTER_EVENT,
1611 BLUETOOTH_EVENT_BONDING_FINISHED,
1613 _bt_free_device_info(remote_dev_info);
1616 } else if (strcasecmp(property, "UUIDs") == 0) {
1617 /* Once we get the updated uuid information after
1618 * reverse service search, update it to application */
1620 bt_remote_dev_info_t *remote_dev_info;
1622 ret_if(_bt_is_device_creating() == TRUE);
1624 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1625 ret_if(address == NULL);
1627 _bt_convert_device_path_to_address(path, address);
1629 remote_dev_info = _bt_get_remote_device_info(address);
1630 if (remote_dev_info == NULL) {
1635 BT_DBG("UUID's count = %d", remote_dev_info->uuid_count);
1636 if (remote_dev_info->paired && remote_dev_info->uuid_count) {
1637 GVariant *uuids = NULL;
1638 GVariantBuilder *builder = NULL;
1640 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1641 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1642 g_variant_builder_add(builder, "s",
1643 remote_dev_info->uuids[i]);
1645 uuids = g_variant_new("as", builder);
1646 g_variant_builder_unref(builder);
1647 GVariant *manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1648 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1651 param = g_variant_new("(isunsbub@asn@ay)", result,
1652 address, remote_dev_info->class,
1653 remote_dev_info->rssi,
1654 remote_dev_info->name,
1655 remote_dev_info->paired,
1656 remote_dev_info->connected,
1657 remote_dev_info->trust,
1659 remote_dev_info->manufacturer_data_len,
1661 _bt_send_event(BT_ADAPTER_EVENT,
1662 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1666 _bt_free_device_info(remote_dev_info);
1669 } else if (strcasecmp(member, "DeviceConnected") == 0) {
1670 unsigned char addr_type = 0;
1672 g_variant_get(msg, "(y)", &addr_type);
1674 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1675 ret_if(address == NULL);
1677 _bt_convert_device_path_to_address(path, address);
1678 dev_name = _bt_get_bonded_device_name(address);
1680 BT_INFO("Address : %s Type : %d", address, addr_type);
1681 BT_ERR_C("Connected [%s] [%s]", !addr_type ? "BREDR" : "LE",
1682 !addr_type ? dev_name : address);
1685 _bt_logging_connection(TRUE, addr_type);
1686 #ifdef ENABLE_TIZEN_2_4
1687 journal_bt_connected();
1689 param = g_variant_new("(isy)", result, address, addr_type);
1690 /*Send event to application*/
1691 _bt_send_event(BT_DEVICE_EVENT,
1692 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1695 } else if (strcasecmp(member, "Disconnected") == 0) {
1696 unsigned char disc_reason = 0;
1697 unsigned char addr_type = 0;
1698 gboolean sending = FALSE;
1700 g_variant_get(msg, "(yy)", &addr_type, &disc_reason);
1702 result = disc_reason;
1704 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1705 ret_if(address == NULL);
1707 _bt_convert_device_path_to_address(path, address);
1708 dev_name = _bt_get_bonded_device_name(address);
1709 #ifdef ENABLE_TIZEN_2_4
1710 journal_bt_disconnected();
1713 /* 0x00 BDADDR_BRDER
1714 0x01 BDADDR_LE_PUBLIC
1715 0x02 BDADDR_LE_RANDOM */
1716 BT_INFO("Address : %s Type : %d", address, addr_type);
1717 BT_ERR_C("Disconnected [%s] [%d : %s] [%s]", !addr_type ? "BREDR" : "LE",
1718 disc_reason, _bt_convert_disc_reason_to_string(disc_reason),
1719 !addr_type ? dev_name : address);
1722 _bt_headset_set_local_connection(FALSE);
1723 _bt_logging_connection(FALSE, addr_type);
1725 /*Check for any OPP transfer on the device and cancel
1728 _bt_obex_check_pending_transfer(address);
1729 _bt_opp_client_is_sending(&sending);
1730 if (sending == TRUE)
1731 _bt_opp_client_check_pending_transfer(address);
1732 param = g_variant_new("(isy)", result, address, addr_type);
1733 _bt_send_event(BT_DEVICE_EVENT,
1734 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1737 } else if (strcasecmp(member, "ProfileStateChanged") == 0) {
1739 char *profile_uuid = NULL;
1740 bt_headset_wait_t *wait_list;
1742 g_variant_get(msg, "(si)", &profile_uuid, &state);
1744 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1745 ret_if(address == NULL);
1747 _bt_convert_device_path_to_address(path, address);
1749 BT_DBG("Address: %s", address);
1750 BT_DBG("Profile UUID: %s", profile_uuid);
1751 BT_DBG("State: %d", state);
1753 if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1754 (state == BT_PROFILE_STATE_CONNECTED)) {
1756 int event = BLUETOOTH_EVENT_AV_CONNECTED;
1757 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1758 bluetooth_device_address_t device_address;
1760 bt_headset_wait_t *wait_list;
1762 __bt_set_device_values(TRUE,
1763 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1765 __bt_connection_manager_set_state(address, event);
1767 if (_bt_headset_get_local_connection() == FALSE)
1768 _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
1770 /* Connection Started from local device therefore no need to
1771 * intiate connection for pending profile */
1772 _bt_headset_set_local_connection(FALSE);
1774 param = g_variant_new("(is)", result, address);
1775 _bt_send_event(BT_HEADSET_EVENT, event,
1777 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
1780 if (g_strcmp0(connected_address, address) != 0) {
1781 _bt_convert_addr_string_to_type(
1782 device_address.addr,
1784 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
1785 &device_address, NULL);
1789 _bt_add_headset_to_list(BT_AUDIO_A2DP,
1790 BT_STATE_CONNECTED, address);
1792 wait_list = _bt_get_audio_wait_data();
1793 if (wait_list != NULL &&
1794 (g_strcmp0(wait_list->address, address) == 0))
1795 _bt_rel_wait_data();
1797 } else if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1798 (state == BT_PROFILE_STATE_DISCONNECTED)) {
1800 int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
1802 if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
1804 g_free(profile_uuid);
1808 __bt_set_device_values(FALSE,
1809 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1811 __bt_connection_manager_set_state(address, event);
1812 param = g_variant_new("(is)", result, address);
1813 _bt_send_event(BT_HEADSET_EVENT, event,
1815 /* Remove data from the connected list */
1816 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
1817 wait_list = _bt_get_audio_wait_data();
1819 if (wait_list == NULL) {
1821 g_free(profile_uuid);
1825 if (((wait_list->type == BT_AUDIO_ALL) &&
1826 (wait_list->ag_flag == TRUE)) ||
1827 (wait_list->type == BT_AUDIO_A2DP) ||
1828 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
1829 bluetooth_device_address_t device_address;
1830 _bt_convert_addr_string_to_type(
1831 device_address.addr,
1832 wait_list->address);
1834 _bt_audio_connect(wait_list->req_id,
1837 wait_list->out_param1);
1839 } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
1841 if (state == BT_PROFILE_STATE_CONNECTED) {
1842 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
1843 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1844 bluetooth_device_address_t device_address;
1846 param = g_variant_new("(is)", result, address);
1847 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1849 connected = _bt_is_headset_type_connected(
1853 if (g_strcmp0(connected_address,
1855 _bt_convert_addr_string_to_type(
1856 device_address.addr,
1858 _bt_audio_disconnect(0,
1860 &device_address, NULL);
1863 BT_DBG("device Path: %s", path);
1864 _bt_add_headset_to_list(BT_AVRCP,
1865 BT_STATE_CONNECTED, address);
1866 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1867 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
1868 param = g_variant_new("(is)", result, address);
1869 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1871 /* Remove data from the connected list */
1872 _bt_remove_headset_from_list(BT_AVRCP, address);
1874 } else if (strcasecmp(profile_uuid, A2DP_SOURCE_UUID) == 0) {
1875 if (state == BT_PROFILE_STATE_CONNECTED) {
1876 int event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED;
1877 BT_INFO("A2DP Source is connected");
1878 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
1879 g_variant_new("(is)", result, address));
1880 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1881 int event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED;
1882 BT_INFO("A2DP Source Disconnected");
1883 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
1884 g_variant_new("(is)", result, address));
1886 } else if ((strcmp(profile_uuid, HID_UUID) == 0) &&
1887 ((state == BT_PROFILE_STATE_CONNECTED) ||
1888 (state == BT_PROFILE_STATE_DISCONNECTED))) {
1890 if (state == BT_PROFILE_STATE_CONNECTED)
1891 event = BLUETOOTH_HID_CONNECTED;
1893 event = BLUETOOTH_HID_DISCONNECTED;
1894 param = g_variant_new("(is)", result, address);
1895 _bt_send_event(BT_HID_EVENT, event,
1898 if (state == BT_PROFILE_STATE_CONNECTED)
1899 __bt_set_device_values(TRUE,
1900 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1902 __bt_set_device_values(FALSE,
1903 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1906 g_free(profile_uuid);
1907 } else if (strcasecmp(member, "AdvReport") == 0) {
1909 bt_remote_le_dev_info_t *le_dev_info = NULL;
1910 char *buffer = NULL;
1912 bt_le_adv_info_t *adv_info = NULL;
1913 GVariant *value = NULL;
1914 ret_if(_bt_is_le_scanning() == FALSE);
1916 le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
1917 if (le_dev_info == NULL)
1920 g_variant_get(msg, "(syyii@ay)", &le_dev_info->address,
1921 &le_dev_info->addr_type,
1922 &le_dev_info->adv_type,
1924 &le_dev_info->adv_data_len,
1926 buffer_len = g_variant_get_size(value);
1928 buffer = (char *)g_variant_get_data(value);
1930 le_dev_info->adv_data = g_memdup(buffer, buffer_len);
1931 if (le_dev_info->adv_data == NULL &&
1932 le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {
1933 _bt_free_le_device_info(le_dev_info);
1934 g_variant_unref(value);
1938 if (_bt_get_le_scan_type() == BT_LE_PASSIVE_SCAN) {
1939 _bt_send_scan_result_event(le_dev_info, NULL);
1940 _bt_free_le_device_info(le_dev_info);
1941 g_variant_unref(value);
1945 if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) { /* ADV_IND */
1946 adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
1947 if (adv_info == NULL) {
1948 _bt_free_le_device_info(le_dev_info);
1949 g_variant_unref(value);
1953 adv_info->addr = g_strdup(le_dev_info->address);
1954 adv_info->data_len = le_dev_info->adv_data_len;
1955 adv_info->data = g_malloc0(le_dev_info->adv_data_len);
1956 if (adv_info->data) {
1957 memcpy(adv_info->data, le_dev_info->adv_data,
1958 le_dev_info->adv_data_len);
1960 __bt_add_adv_ind_info(adv_info);
1963 } else { /* SCAN_RSP */
1964 adv_info = __bt_get_adv_ind_info(le_dev_info->address);
1966 _bt_send_scan_result_event(le_dev_info, adv_info);
1967 __bt_del_adv_ind_info(le_dev_info->address);
1970 _bt_free_le_device_info(le_dev_info);
1971 g_variant_unref(value);
1972 } else if (strcasecmp(member, "LEDataLengthChanged") == 0) {
1978 g_variant_get(msg, "(qqqq)",
1979 tx_octets, tx_time, rx_octets, rx_time);
1981 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1982 _bt_convert_device_path_to_address(path, address);
1984 param = g_variant_new("(isqqqq)", result, address, tx_octets, tx_time,
1985 rx_octets, rx_time);
1986 /* Send event to application */
1987 _bt_send_event(BT_DEVICE_EVENT, event, param);
1989 } else if (strcasecmp(member, "IpspStateChanged") == 0) {
1990 gboolean connected = FALSE;
1991 char *ifname = NULL;
1993 g_variant_get(msg, "(bs)", &connected, &ifname);
1995 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
1996 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
1998 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1999 _bt_convert_device_path_to_address(path, address);
2001 BT_DBG("Ipsp BT Interface Name: %s", ifname);
2002 BT_DBG("address: %s", address);
2003 param = g_variant_new("(iss)", result, address, ifname);
2005 /* Send event to application */
2006 _bt_send_event(BT_DEVICE_EVENT,
2014 void __bt_set_audio_values(gboolean connected, char *address)
2017 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
2019 /* Set the headset name */
2020 if (connected == TRUE) {
2021 name = _bt_get_bonded_device_name(address);
2023 name = g_strdup("");
2026 if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME,
2028 BT_ERR("vconf_set_str failed");
2033 /* Set the headset state */
2034 if (vconf_get_int(VCONFKEY_BT_DEVICE,
2035 &bt_device_state) != 0) {
2036 BT_ERR("vconf_get_str failed");
2039 #ifdef TIZEN_SUPPORT_DUAL_HF
2040 if ((connected == TRUE) &&
2041 (FALSE == __bt_is_companion_device(address))) {
2042 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2043 } else if ((bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) &&
2044 (FALSE == __bt_is_companion_device(address))) {
2045 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2048 if (connected == TRUE) {
2049 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2050 } else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) {
2051 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2055 if (vconf_set_int(VCONFKEY_BT_DEVICE,
2056 bt_device_state) != 0) {
2057 BT_ERR("vconf_set_int failed");
2061 void _bt_handle_headset_event(GVariant *msg, const char *path)
2063 int result = BLUETOOTH_ERROR_NONE;
2064 gboolean property_flag = FALSE;
2065 char *property = NULL;
2066 GVariant *value = NULL;
2067 GVariant *param = NULL;
2068 g_variant_get(msg, "(sv)", &property, &value);
2070 ret_if(property == NULL);
2072 BT_DBG("Property = %s \n", property);
2073 /* We allow only 1 headset connection (HSP or HFP)*/
2074 if (strcasecmp(property, "Connected") == 0) {
2075 int event = BLUETOOTH_EVENT_NONE;
2076 bt_headset_wait_t *wait_list;
2078 g_variant_get(value, "b", &property_flag);
2080 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2082 /* Fix : NULL_RETURNS */
2083 if (address == NULL)
2086 _bt_convert_device_path_to_address(path, address);
2088 if (property_flag == TRUE) {
2089 event = BLUETOOTH_EVENT_AG_CONNECTED;
2090 if (_bt_headset_get_local_connection() == FALSE)
2091 _bt_start_timer_for_connection(address, BT_AUDIO_A2DP);
2093 _bt_headset_set_local_connection(FALSE);
2097 event = BLUETOOTH_EVENT_AG_DISCONNECTED;
2099 previous_state = _bt_get_device_state_from_list(BT_AUDIO_HSP, address);
2100 if (previous_state == BT_STATE_DISCONNECTING)
2101 _bt_send_hf_local_term_event(address);
2103 /* Set the State machine here */
2104 __bt_connection_manager_set_state(address, event);
2105 __bt_set_audio_values(property_flag, address);
2106 param = g_variant_new("(is)", result, address);
2107 _bt_send_event(BT_HEADSET_EVENT, event,
2110 if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
2111 /* Remove data from the connected list */
2112 _bt_remove_headset_from_list(BT_AUDIO_HSP, address);
2114 wait_list = _bt_get_audio_wait_data();
2115 if (wait_list == NULL) {
2120 bluetooth_device_address_t device_address;
2122 _bt_set_audio_wait_data_flag(TRUE);
2124 _bt_convert_addr_string_to_type(device_address.addr,
2125 wait_list->address);
2126 _bt_audio_connect(wait_list->req_id, wait_list->type,
2127 &device_address, wait_list->out_param1);
2128 _bt_rel_wait_data();
2129 } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
2130 /* Add data to the connected list */
2131 _bt_add_headset_to_list(BT_AUDIO_HSP,
2132 BT_STATE_CONNECTED, address);
2134 wait_list = _bt_get_audio_wait_data();
2135 if (wait_list != NULL &&
2136 (g_strcmp0(wait_list->address, address) == 0))
2137 _bt_rel_wait_data();
2139 BT_INFO("Check A2DP pending connect");
2140 _bt_audio_check_pending_connect();
2143 } else if (strcasecmp(property, "State") == 0) {
2146 g_variant_get(value, "s", &state);
2148 /* This code assumes we support only 1 headset connection */
2149 /* Need to use the headset list, if we support multi-headsets */
2150 if (strcasecmp(state, "Playing") == 0) {
2151 BT_DBG("Playing: Sco Connected");
2152 } else if (strcasecmp(state, "connected") == 0 ||
2153 strcasecmp(state, "disconnected") == 0) {
2154 BT_DBG("connected/disconnected: Sco Disconnected");
2156 BT_ERR("Not handled state - %s", state);
2161 } else if (strcasecmp(property, "SpeakerGain") == 0) {
2165 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2167 _bt_convert_device_path_to_address(path, address);
2169 spkr_gain = g_variant_get_uint16(value);
2171 BT_DBG("spkr_gain: %d", spkr_gain);
2173 param = g_variant_new("(i&sq)", result, address, spkr_gain);
2174 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
2178 } else if (strcasecmp(property, "MicrophoneGain") == 0) {
2182 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2184 _bt_convert_device_path_to_address(path, address);
2186 mic_gain = g_variant_get_uint16(value);
2188 param = g_variant_new("(i&sq)", result, address, mic_gain);
2189 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_MIC_GAIN,
2196 g_variant_unref(value);
2199 void _bt_handle_sink_event(GVariant *msg, const char *path)
2201 GVariantIter value_iter;
2202 char *property = NULL;
2204 bt_headset_wait_t *wait_list;
2206 GVariant *child = NULL;
2207 GVariant *val = NULL;
2208 GVariant *param = NULL;
2209 g_variant_iter_init(&value_iter, msg);
2210 while ((child = g_variant_iter_next_value(&value_iter))) {
2212 g_variant_get(child, "{sv}", &property, &val);
2214 ret_if(property == NULL);
2216 BT_DBG("Property = %s \n", property);
2219 if (strcasecmp(property, "State") == 0) {
2220 int result = BLUETOOTH_ERROR_NONE;
2223 g_variant_get(val, "s", &value);
2224 BT_DBG("value: %s", value);
2226 if (g_strcmp0(value, "disconnected") == 0) {
2229 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2231 _bt_convert_device_path_to_address(path, address);
2233 __bt_set_device_values(FALSE,
2234 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2235 param = g_variant_new("(is)", result, address);
2236 _bt_send_event(BT_HEADSET_EVENT,
2237 BLUETOOTH_EVENT_AV_DISCONNECTED,
2240 /* Remove data from the connected list */
2241 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2242 wait_list = _bt_get_audio_wait_data();
2243 if (wait_list == NULL) {
2246 g_variant_unref(val);
2247 g_variant_unref(child);
2252 if (((wait_list->type == BT_AUDIO_ALL) &&
2253 (wait_list->ag_flag == TRUE)) ||
2254 (wait_list->type == BT_AUDIO_A2DP) ||
2255 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2256 bluetooth_device_address_t device_address;
2257 _bt_convert_addr_string_to_type(
2258 device_address.addr,
2259 wait_list->address);
2261 _bt_audio_connect(wait_list->req_id,
2264 wait_list->out_param1);
2267 } else if (strcasecmp(value, "Connected") == 0) {
2269 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2270 bluetooth_device_address_t device_address;
2273 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2275 _bt_convert_device_path_to_address(path, address);
2277 __bt_set_device_values(TRUE,
2278 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2279 param = g_variant_new("(is)", result, address);
2280 _bt_send_event(BT_HEADSET_EVENT,
2281 BLUETOOTH_EVENT_AV_CONNECTED,
2283 /* Check for existing Media device to disconnect */
2284 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
2287 /* Match connected device address */
2288 if (g_strcmp0(connected_address, address) != 0) {
2289 /* Convert BD adress from string type */
2290 _bt_convert_addr_string_to_type(
2291 device_address.addr,
2293 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2294 &device_address, NULL);
2298 /* Add data to the connected list */
2299 _bt_add_headset_to_list(BT_AUDIO_A2DP,
2300 BT_STATE_CONNECTED, address);
2307 g_variant_unref(val);
2308 g_variant_unref(child);
2312 void _bt_handle_agent_event(GVariant *msg, const char *member)
2314 int result = BLUETOOTH_ERROR_NONE;
2315 char *address = NULL;
2318 GVariant *param = NULL;
2319 ret_if(member == NULL);
2321 if (strcasecmp(member, "ObexAuthorize") == 0) {
2322 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2323 param = g_variant_new("(iss)", result, address, name);
2324 _bt_send_event(BT_OPP_SERVER_EVENT,
2325 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
2329 } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2330 bt_rfcomm_server_info_t *server_info;
2332 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2334 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2335 ret_if(server_info == NULL);
2336 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2337 param = g_variant_new("(isssn)", result, address, uuid, name,
2338 server_info->control_fd);
2339 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2340 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2348 static int __bt_get_object_path(GVariant *msg, char **path)
2350 g_variant_get(msg, "(o*)", path, NULL);
2352 return BLUETOOTH_ERROR_INTERNAL;
2354 return BLUETOOTH_ERROR_NONE;
2357 static void __bt_devices_list_free(void)
2359 bt_cache_info_t *cache_info;
2362 node = g_list_first(p_cache_list);
2364 while (node != NULL) {
2365 cache_info = (bt_cache_info_t *)node->data;
2366 p_cache_list = g_list_remove(p_cache_list, cache_info);
2367 __bt_free_cache_info(cache_info);
2369 node = g_list_next(node);
2373 static int __bt_parse_event(GVariant *msg)
2377 char *interface_name = NULL;
2378 GVariant *inner_iter = NULL;
2380 g_variant_iter_init(&iter, msg);
2382 while ((child = g_variant_iter_next_value(&iter))) {
2383 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
2384 if (g_strcmp0(interface_name,
2385 BT_DEVICE_INTERFACE) == 0) {
2386 g_variant_unref(inner_iter);
2387 g_variant_unref(child);
2388 return BT_DEVICE_EVENT;
2389 } else if (g_strcmp0(interface_name,
2390 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2391 g_variant_unref(inner_iter);
2392 g_variant_unref(child);
2393 return BT_MEDIA_TRANSFER_EVENT;
2394 } else if (g_strcmp0(interface_name,
2395 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2396 g_variant_unref(inner_iter);
2397 g_variant_unref(child);
2398 return BT_AVRCP_CONTROL_EVENT;
2400 g_variant_unref(inner_iter);
2401 g_variant_unref(child);
2407 static void __bt_manager_event_filter(GDBusConnection *connection,
2408 const gchar *sender_name,
2409 const gchar *object_path,
2410 const gchar *interface_name,
2411 const gchar *signal_name,
2412 GVariant *parameters,
2415 bt_event_type_t bt_event = 0x00;
2416 int result = BLUETOOTH_ERROR_NONE;
2418 char *obj_path = NULL;
2419 GVariant *param = NULL;
2420 if (signal_name == NULL)
2422 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2423 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2425 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
2426 #ifdef USB_BLUETOOTH
2427 BT_DBG("Enable Adapter");
2428 _bt_enable_adapter();
2430 _bt_handle_adapter_added();
2433 bt_event = __bt_parse_event(value);
2434 if (bt_event == BT_DEVICE_EVENT) {
2435 bt_cache_info_t *cache_info;
2436 bt_remote_dev_info_t *dev_info;
2438 ret_if(_bt_is_discovering() == FALSE &&
2439 _bt_is_le_scanning() == FALSE);
2441 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2442 ret_if(cache_info == NULL);
2444 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2445 if (dev_info == NULL) {
2446 __bt_free_cache_info(cache_info);
2450 cache_info->dev_info = dev_info;
2452 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2453 BT_ERR("Fail to parse the properies");
2454 __bt_free_cache_info(cache_info);
2455 g_variant_unref(value);
2459 if (dev_info->addr_type != BDADDR_BREDR) {
2460 /* Whenever emit the property changed from bluez,
2461 some property doesn't reach to bt-service.
2462 So LE device is handled as AdvReport signal */
2463 __bt_free_cache_info(cache_info);
2464 g_variant_unref(value);
2468 if (dev_info->name == NULL)
2469 /* If Remote device name is NULL or still RNR is not done
2470 * then display address as name.
2472 dev_info->name = g_strdup(dev_info->address);
2474 #ifdef TIZEN_DPM_ENABLE
2475 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
2477 bluetooth_device_class_t device_class;
2478 _bt_divide_device_class(&device_class, dev_info->class);
2479 BT_DBG("[%s]device_class.major_class : %d", dev_info->name, device_class.major_class);
2481 if (device_class.major_class ==
2482 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
2483 __bt_free_cache_info(cache_info);
2484 g_variant_unref(value);
2490 GVariant *uuids = NULL;
2491 GVariantBuilder *builder = NULL;
2493 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2494 for (i = 0; i < dev_info->uuid_count; i++) {
2495 g_variant_builder_add(builder, "s",
2496 dev_info->uuids[i]);
2498 uuids = g_variant_new("as", builder);
2499 g_variant_builder_unref(builder);
2500 GVariant *manufacturer_data = NULL;
2501 manufacturer_data = g_variant_new_from_data(
2502 G_VARIANT_TYPE_BYTESTRING,
2503 dev_info->manufacturer_data,
2504 dev_info->manufacturer_data_len,
2506 param = g_variant_new("(isunsbub@asn@ay)", result,
2512 dev_info->connected,
2515 dev_info->manufacturer_data_len,
2517 _bt_send_event(BT_ADAPTER_EVENT,
2518 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2520 p_cache_list = g_list_append(p_cache_list, cache_info);
2521 } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2522 BT_DBG("Device path : %s ", obj_path);
2523 _bt_set_control_device_path(obj_path);
2526 g_variant_unref(value);
2527 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2528 #ifdef USB_BLUETOOTH
2529 BT_DBG("InterfacesRemoved");
2530 _bt_handle_adapter_removed();
2532 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0) {
2533 bt_event = BT_MEDIA_TRANSFER_EVENT;
2534 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2535 bt_event = BT_DEVICE_EVENT;
2536 } else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
2537 bt_event = BT_AVRCP_CONTROL_EVENT;
2539 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2540 _bt_handle_adapter_event(parameters, signal_name);
2541 if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2542 BT_INFO("Object Path %s", obj_path);
2543 _bt_remove_control_device_path(obj_path);
2546 } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2549 char *previous = NULL;
2550 char *current = NULL;
2552 if (__bt_get_owner_info(parameters, &name, &previous, ¤t)) {
2553 BT_ERR("Fail to get the owner info");
2557 if (*current != '\0') {
2566 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2567 BT_DBG("Bluetoothd is terminated");
2568 if (_bt_adapter_get_status() == BT_ACTIVATED)
2571 _bt_handle_adapter_removed();
2572 __bt_devices_list_free();
2575 _bt_obex_server_check_allocation(&value);
2577 if (value == TRUE) {
2578 /* Check if the obex server was terminated abnormally */
2579 _bt_obex_server_check_termination(name);
2582 _bt_rfcomm_server_check_existence(&value);
2584 if (value == TRUE) {
2585 /* The obex server was terminated abnormally */
2586 _bt_rfcomm_server_check_termination(name);
2589 /* Stop advertising started by terminated process */
2590 _bt_stop_advertising_by_terminated_process(name);
2592 _bt_stop_le_scan(name);
2596 } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2597 const char *path = object_path;
2599 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2600 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2603 _bt_handle_property_changed_event(parameters, object_path);
2604 } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2605 _bt_handle_adapter_event(parameters, signal_name);
2606 } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2607 _bt_handle_input_event(parameters, object_path);
2608 } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2609 _bt_handle_network_server_event(parameters, signal_name);
2610 } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2611 _bt_handle_headset_event(parameters, object_path);
2612 } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2613 _bt_handle_sink_event(parameters, object_path);
2614 } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2615 _bt_handle_agent_event(parameters, signal_name);
2616 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2617 _bt_handle_device_event(parameters, signal_name, object_path);
2618 } else if (g_strcmp0(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
2619 _bt_handle_gatt_event(parameters, signal_name, object_path);
2625 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2628 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2629 char *interface_name = NULL;
2631 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2632 retv_if(interface_name == NULL, FALSE);
2634 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2635 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2643 static void __bt_obexd_event_filter(GDBusConnection *connection,
2644 const gchar *sender_name,
2645 const gchar *object_path,
2646 const gchar *interface_name,
2647 const gchar *signal_name,
2648 GVariant *parameters,
2651 const char *member = signal_name;
2652 char *obj_path = NULL;
2653 ret_if(member == NULL);
2655 if (strcasecmp(member, "InterfacesAdded") == 0) {
2656 if (__bt_get_object_path(parameters, &obj_path)) {
2657 BT_ERR("Fail to get the path");
2660 BT_INFO("object_path = [%s]", object_path);
2662 /*Handle OPP_SERVER_CONNECTED_EVENT here */
2663 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2664 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2669 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2670 BT_DBG("Obex_Server_Session_Transfer connected");
2671 _bt_obex_transfer_connected();
2674 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2675 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
2676 if (__bt_get_object_path(parameters, &obj_path)) {
2677 BT_ERR("Fail to get the path");
2680 BT_INFO("object_path = [%s]", object_path);
2682 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2683 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2684 BT_DBG("Call PBAP Disconnected");
2685 _bt_obex_pbap_client_disconnect(obj_path);
2688 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2689 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2694 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2695 BT_DBG("Obex_Server_Session_Transfer disconnected");
2696 _bt_obex_transfer_disconnected();
2699 } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
2700 const char *path = object_path;
2702 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
2703 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
2704 strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2705 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2706 BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
2710 _bt_handle_property_changed_event(parameters, path);
2716 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
2720 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2721 char *interface_name = NULL;
2723 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2725 retv_if(interface_name == NULL, FALSE);
2727 if (strcasecmp(interface_name,
2728 BT_OBEX_TRANSFER_INTERFACE) == 0) {
2739 static void __bt_opc_event_filter(GDBusConnection *connection,
2740 const gchar *sender_name,
2741 const gchar *object_path,
2742 const gchar *interface_name,
2743 const gchar *signal_name,
2744 GVariant *parameters,
2747 const char *member = signal_name;
2748 char *obj_path = NULL;
2749 if (strcasecmp(member, "InterfacesAdded") == 0) {
2750 BT_DBG("InterfacesAdded");
2751 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2753 if (__bt_get_object_path(parameters, &obj_path)) {
2754 BT_ERR("Fail to get the path");
2758 BT_DBG("object_path = %s", obj_path);
2760 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2761 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
2762 || strstr(obj_path, "transfer") == NULL) {
2765 } else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2766 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2767 BT_DBG("Going to call opc disconnected");
2768 _bt_opc_disconnected(obj_path);
2771 _bt_sending_files();
2773 } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
2774 char *path = (char *)object_path;
2775 BT_INFO("object_path %s", path);
2776 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2777 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2778 BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
2782 _bt_opc_property_changed_event(parameters, path);
2788 int _bt_opp_client_event_init(void)
2790 GError *error = NULL;
2792 if (opc_obexd_conn == NULL) {
2793 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
2795 if (!opc_obexd_conn) {
2797 BT_ERR("Unable to connect to dbus: %s", error->message);
2798 g_clear_error(&error);
2800 return BLUETOOTH_ERROR_INTERNAL;
2804 if (_bt_register_service_event(opc_obexd_conn,
2805 BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
2806 g_object_unref(opc_obexd_conn);
2807 opc_obexd_conn = NULL;
2808 return BLUETOOTH_ERROR_INTERNAL;
2811 return BLUETOOTH_ERROR_NONE;
2814 void _bt_opp_client_event_deinit(void)
2816 if (opc_obexd_conn) {
2817 _bt_unregister_service_event(opc_obexd_conn,
2818 BT_OPP_CLIENT_EVENT);
2819 g_object_unref(opc_obexd_conn);
2820 opc_obexd_conn = NULL;
2824 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
2830 static int subs_interface_added_id = -1;
2831 static int subs_interface_removed_id = -1;
2832 static int subs_name_owner_id = -1;
2833 static int subs_property_id = -1;
2834 static int subs_adapter_id = -1;
2835 static int subs_gatt_id = -1;
2838 if (subs_interface_added_id == -1) {
2839 subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
2840 NULL, BT_MANAGER_INTERFACE,
2841 BT_INTERFACES_ADDED, NULL, NULL, 0,
2842 __bt_manager_event_filter,
2845 if (subs_interface_removed_id == -1) {
2846 subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
2847 NULL, BT_MANAGER_INTERFACE,
2848 BT_INTERFACES_REMOVED, NULL, NULL, 0,
2849 __bt_manager_event_filter,
2852 if (subs_name_owner_id == -1) {
2853 subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
2854 NULL, BT_FREEDESKTOP_INTERFACE,
2855 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
2856 __bt_manager_event_filter,
2859 if (subs_property_id == -1) {
2860 subs_property_id = g_dbus_connection_signal_subscribe(conn,
2861 NULL, BT_PROPERTIES_INTERFACE,
2862 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
2863 __bt_manager_event_filter,
2866 if (subs_adapter_id == -1) {
2867 subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
2868 NULL, BT_ADAPTER_INTERFACE,
2869 NULL, NULL, NULL, 0,
2870 __bt_manager_event_filter,
2873 if (subs_gatt_id == -1) {
2874 subs_gatt_id = g_dbus_connection_signal_subscribe(conn,
2875 NULL, BT_GATT_CHAR_INTERFACE,
2876 NULL, NULL, NULL, 0,
2877 __bt_manager_event_filter,
2881 if (subs_interface_added_id != -1) {
2882 g_dbus_connection_signal_unsubscribe(conn,
2883 subs_interface_added_id);
2884 subs_interface_added_id = -1;
2886 if (subs_interface_removed_id != -1) {
2887 g_dbus_connection_signal_unsubscribe(conn,
2888 subs_interface_removed_id);
2889 subs_interface_removed_id = -1;
2891 if (subs_name_owner_id != -1) {
2892 g_dbus_connection_signal_unsubscribe(conn,
2893 subs_name_owner_id);
2894 subs_name_owner_id = -1;
2896 if (subs_property_id != -1) {
2897 g_dbus_connection_signal_unsubscribe(conn,
2899 subs_property_id = -1;
2901 if (subs_adapter_id != -1) {
2902 g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
2903 subs_adapter_id = -1;
2905 if (subs_gatt_id != -1) {
2906 g_dbus_connection_signal_unsubscribe(conn, subs_gatt_id);
2913 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
2919 static int subs_device_id = -1;
2922 if (subs_device_id == -1) {
2923 subs_device_id = g_dbus_connection_signal_subscribe(conn,
2924 NULL, BT_DEVICE_INTERFACE,
2925 NULL, NULL, NULL, 0,
2926 __bt_manager_event_filter,
2930 if (subs_device_id != -1) {
2931 g_dbus_connection_signal_unsubscribe(conn,
2933 subs_device_id = -1;
2939 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
2945 static int subs_input_id = -1;
2948 if (subs_input_id == -1) {
2949 subs_input_id = g_dbus_connection_signal_subscribe(conn,
2950 NULL, BT_INPUT_INTERFACE,
2951 NULL, NULL, NULL, 0,
2952 __bt_manager_event_filter,
2956 if (subs_input_id != -1) {
2957 g_dbus_connection_signal_unsubscribe(conn,
2965 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
2971 static int subs_serv_id = -1;
2972 static int subs_client_id = -1;
2975 if (subs_serv_id == -1) {
2976 subs_serv_id = g_dbus_connection_signal_subscribe(conn,
2977 NULL, BT_NETWORK_SERVER_INTERFACE,
2978 NULL, NULL, NULL, 0,
2979 __bt_manager_event_filter,
2982 if (subs_client_id == -1) {
2983 subs_client_id = g_dbus_connection_signal_subscribe(conn,
2984 NULL, BT_NETWORK_CLIENT_INTERFACE,
2985 NULL, NULL, NULL, 0,
2986 __bt_manager_event_filter,
2990 if (subs_serv_id != -1) {
2991 g_dbus_connection_signal_unsubscribe(conn,
2995 if (subs_client_id != -1) {
2996 g_dbus_connection_signal_unsubscribe(conn,
2998 subs_client_id = -1;
3004 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
3010 static int subs_headset_id = -1;
3011 static int subs_sink_id = -1;
3014 if (subs_headset_id == -1) {
3015 subs_headset_id = g_dbus_connection_signal_subscribe(conn,
3016 NULL, BT_HEADSET_INTERFACE,
3017 NULL, NULL, NULL, 0,
3018 __bt_manager_event_filter,
3021 if (subs_sink_id == -1) {
3022 subs_sink_id = g_dbus_connection_signal_subscribe(conn,
3023 NULL, BT_SINK_INTERFACE,
3024 NULL, NULL, NULL, 0,
3025 __bt_manager_event_filter,
3029 if (subs_headset_id != -1) {
3030 g_dbus_connection_signal_unsubscribe(conn,
3032 subs_headset_id = -1;
3034 if (subs_sink_id != -1) {
3035 g_dbus_connection_signal_unsubscribe(conn,
3043 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
3049 static int subs_opp_server_interface_added_id = -1;
3050 static int subs_opp_server_interface_removed_id = -1;
3051 static int subs_opp_server_property_id = -1;
3055 if (subs_opp_server_interface_added_id == -1) {
3056 subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3057 NULL, BT_MANAGER_INTERFACE,
3058 BT_INTERFACES_ADDED, NULL, NULL, 0,
3059 __bt_obexd_event_filter,
3062 if (subs_opp_server_interface_removed_id == -1) {
3063 subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3064 NULL, BT_MANAGER_INTERFACE,
3065 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3066 __bt_obexd_event_filter,
3069 if (subs_opp_server_property_id == -1) {
3070 subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
3071 NULL, BT_PROPERTIES_INTERFACE,
3072 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3073 __bt_obexd_event_filter,
3077 if (subs_opp_server_interface_added_id != -1) {
3078 g_dbus_connection_signal_unsubscribe(conn,
3079 subs_opp_server_interface_added_id);
3080 subs_opp_server_interface_added_id = -1;
3082 if (subs_opp_server_interface_removed_id != -1) {
3083 g_dbus_connection_signal_unsubscribe(conn,
3084 subs_opp_server_interface_removed_id);
3085 subs_opp_server_interface_removed_id = -1;
3087 if (subs_opp_server_property_id != -1) {
3088 g_dbus_connection_signal_unsubscribe(conn,
3089 subs_opp_server_property_id);
3090 subs_opp_server_property_id = -1;
3096 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
3102 static int subs_opp_client_interface_added_id = -1;
3103 static int subs_opp_client_interface_removed_id = -1;
3104 static int subs_opp_client_property_id = -1;
3108 if (subs_opp_client_interface_added_id == -1) {
3109 subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3110 NULL, BT_MANAGER_INTERFACE,
3111 BT_INTERFACES_ADDED, NULL, NULL, 0,
3112 __bt_opc_event_filter,
3115 if (subs_opp_client_interface_removed_id == -1) {
3116 subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3117 NULL, BT_MANAGER_INTERFACE,
3118 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3119 __bt_opc_event_filter,
3122 if (subs_opp_client_property_id == -1) {
3123 subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
3124 NULL, BT_PROPERTIES_INTERFACE,
3125 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3126 __bt_opc_event_filter,
3130 if (subs_opp_client_interface_added_id != -1) {
3131 g_dbus_connection_signal_unsubscribe(conn,
3132 subs_opp_client_interface_added_id);
3133 subs_opp_client_interface_added_id = -1;
3135 if (subs_opp_client_interface_removed_id != -1) {
3136 g_dbus_connection_signal_unsubscribe(conn,
3137 subs_opp_client_interface_removed_id);
3138 subs_opp_client_interface_removed_id = -1;
3140 if (subs_opp_client_property_id != -1) {
3141 g_dbus_connection_signal_unsubscribe(conn,
3142 subs_opp_client_property_id);
3143 subs_opp_client_property_id = -1;
3149 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
3155 static int subs_a2dp_source_id = -1;
3156 static int subs_a2dp_sink_id = -1;
3159 if (subs_a2dp_source_id == -1) {
3160 subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
3161 NULL, BT_A2DP_SOURCE_INTERFACE,
3162 NULL, NULL, NULL, 0,
3163 __bt_opc_event_filter,
3166 if (subs_a2dp_sink_id == -1) {
3167 subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
3168 NULL, BT_SINK_INTERFACE,
3169 NULL, NULL, NULL, 0,
3170 __bt_opc_event_filter,
3174 if (subs_a2dp_source_id != -1) {
3175 g_dbus_connection_signal_unsubscribe(conn,
3176 subs_a2dp_source_id);
3177 subs_a2dp_source_id = -1;
3179 if (subs_a2dp_sink_id != -1) {
3180 g_dbus_connection_signal_unsubscribe(conn,
3182 subs_a2dp_sink_id = -1;
3188 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
3192 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3194 switch (event_type) {
3195 case BT_MANAGER_EVENT:
3196 _bt_register_manager_subscribe_signal(g_conn, TRUE);
3198 case BT_DEVICE_EVENT:
3199 _bt_register_device_subscribe_signal(g_conn, TRUE);
3202 _bt_register_input_subscribe_signal(g_conn, TRUE);
3204 case BT_NETWORK_EVENT:
3205 _bt_register_network_subscribe_signal(g_conn, TRUE);
3207 case BT_HEADSET_EVENT:
3208 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3211 case BT_OPP_SERVER_EVENT:
3212 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3213 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3215 case BT_OPP_CLIENT_EVENT:
3216 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3217 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3219 case BT_A2DP_SOURCE_EVENT:
3220 BT_INFO("A2dp Source event");
3221 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3224 BT_ERR("Unknown event");
3225 return BLUETOOTH_ERROR_INTERNAL;
3228 return BLUETOOTH_ERROR_NONE;
3231 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3235 ret_if(g_conn == NULL);
3237 switch (event_type) {
3238 case BT_MANAGER_EVENT:
3239 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3240 _bt_register_device_subscribe_signal(g_conn, FALSE);
3241 _bt_register_input_subscribe_signal(g_conn, FALSE);
3242 _bt_register_network_subscribe_signal(g_conn, FALSE);
3243 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3245 case BT_OPP_SERVER_EVENT:
3246 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3248 case BT_OPP_CLIENT_EVENT:
3249 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3252 BT_ERR("Unknown event");
3259 static int __bt_init_manager_receiver(void)
3263 GError *error = NULL;
3265 if (manager_conn == NULL) {
3266 manager_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3267 if (error != NULL) {
3268 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3269 g_clear_error(&error);
3271 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3274 if (_bt_register_service_event(manager_conn,
3275 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3277 if (_bt_register_service_event(manager_conn,
3278 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3281 if (_bt_register_service_event(manager_conn,
3282 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3285 if (_bt_register_service_event(manager_conn,
3286 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3289 if (_bt_register_service_event(manager_conn,
3290 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3292 return BLUETOOTH_ERROR_NONE;
3295 g_object_unref(manager_conn);
3296 manager_conn = NULL;
3301 return BLUETOOTH_ERROR_INTERNAL;
3304 static int __bt_init_obexd_receiver(void)
3307 #ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3308 GError *error = NULL;
3310 if (obexd_conn == NULL) {
3311 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3312 if (error != NULL) {
3313 BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3314 g_clear_error(&error);
3316 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3319 if (_bt_register_service_event(obexd_conn,
3320 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3321 BT_ERR("Error while registering service event");
3322 g_object_unref(obexd_conn);
3324 return BLUETOOTH_ERROR_INTERNAL;
3329 return BLUETOOTH_ERROR_NONE;
3332 /* To receive the event from bluez */
3333 int _bt_init_service_event_receiver(void)
3339 result = __bt_init_manager_receiver();
3340 retv_if(result != BLUETOOTH_ERROR_NONE, result);
3342 result = __bt_init_obexd_receiver();
3343 if (result != BLUETOOTH_ERROR_NONE)
3344 BT_ERR("Fail to init obexd receiver");
3348 return BLUETOOTH_ERROR_NONE;
3351 void _bt_deinit_service_event_receiver(void)
3355 _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3357 _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3360 g_object_unref(manager_conn);
3361 manager_conn = NULL;
3365 g_object_unref(obexd_conn);
3370 g_source_remove(event_id);