2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <vconf-internal-bt-keys.h>
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
27 #include "bt-service-common.h"
28 #include "bt-service-event.h"
29 #include "bt-service-main.h"
30 #include "bt-service-adapter.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-device.h"
33 #include "bt-service-avrcp.h"
34 #include "bt-service-obex-server.h"
35 #include "bt-service-rfcomm-server.h"
36 #include "bt-service-audio.h"
37 #include "bt-service-agent.h"
38 #include "bt-service-pbap.h"
39 #include "bt-service-headset-connection.h"
40 #include "bt-service-avrcp-controller.h"
42 #include "bt-service-opp-client.h"
44 #define DBUS_TIMEOUT 20 * 1000 /* 20 Sec */
45 static GDBusConnection *manager_conn;
46 static GDBusConnection *obexd_conn;
47 static GDBusConnection *opc_obexd_conn;
49 static GList *p_cache_list = NULL;
51 static guint event_id;
52 guint nap_connected_device_count = 0;
53 static guint hid_connected_device_count = 0;
54 static GList *p_adv_ind_list;
57 bt_remote_dev_info_t *dev_info;
61 * obexd connection type
69 OBEX_PCSUITE = (1 << 6),
70 OBEX_SYNCEVOLUTION = (1 << 7),
72 } bluetooth_obex_connection_type_t;
74 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path);
75 void _bt_opc_property_changed_event(GVariant *msg, char *path);
76 int _bt_register_service_event(GDBusConnection *g_conn, int event_type);
77 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type);
78 void _bt_opp_client_event_deinit(void);
79 void _bt_handle_network_client_event(GVariant *msg_iter,
81 void __bt_gatt_char_property_changed_event(GVariant *msg_iter,
84 static void __bt_free_bt_le_adv_info_t(bt_le_adv_info_t *adv_info)
86 g_free(adv_info->addr);
87 g_free(adv_info->data);
91 static bt_le_adv_info_t *__bt_get_adv_ind_info(char *addr)
94 bt_le_adv_info_t *adv_info = NULL;
95 GList *current = g_list_first((GList *)p_adv_ind_list);
96 while(current && current->data) {
97 adv_info = (bt_le_adv_info_t *)current->data;
98 retv_if(adv_info && !g_strcmp0(adv_info->addr, addr), adv_info);
99 current = g_list_next(current);
104 static void __bt_add_adv_ind_info(bt_le_adv_info_t *adv_info)
107 if (__bt_get_adv_ind_info(adv_info->addr) != NULL) {
108 BT_ERR("adv_info is already added");
109 __bt_free_bt_le_adv_info_t(adv_info);
112 p_adv_ind_list = g_list_append(p_adv_ind_list, adv_info);
115 static void __bt_del_adv_ind_info(char *addr)
118 ret_if(!p_adv_ind_list);
119 bt_le_adv_info_t *adv_info = NULL;
120 GList *current = g_list_first((GList *)p_adv_ind_list);
121 while(current && current->data) {
122 adv_info = (bt_le_adv_info_t *)current->data;
123 if (adv_info && !g_strcmp0(adv_info->addr, addr)) {
124 p_adv_ind_list = g_list_remove(p_adv_ind_list, adv_info);
125 __bt_free_bt_le_adv_info_t(adv_info);
128 current = g_list_next(current);
132 static void __bt_free_cache_info(bt_cache_info_t *cache_info)
134 ret_if(cache_info == NULL);
135 _bt_free_device_info(cache_info->dev_info);
139 static gboolean __bt_parse_device_properties(GVariant *item,
140 bt_remote_dev_info_t *dev_info)
149 g_variant_iter_init(&iter, item);
150 while (g_variant_iter_loop(&iter, "{sv}", &key, &val)) {
151 if (strcasecmp(key, "Address") == 0) {
152 dev_info->address = g_variant_dup_string(val, &len);
153 } else if (strcasecmp(key, "Class") == 0) {
154 dev_info->class = g_variant_get_uint32(val);
155 } else if (strcasecmp(key, "name") == 0) {
156 if (dev_info->name == NULL)
157 dev_info->name = g_variant_dup_string(val, &len);
158 } else if (strcasecmp(key, "Connected") == 0) {
159 dev_info->connected = g_variant_get_uint32(val);
160 } else if (strcasecmp(key, "paired") == 0) {
161 dev_info->paired = g_variant_get_boolean(val);
162 } else if (strcasecmp(key, "Trusted") == 0) {
163 dev_info->trust = g_variant_get_boolean(val);
164 } else if (strcasecmp(key, "RSSI") == 0) {
165 dev_info->rssi = g_variant_get_int16(val);
166 } else if (strcasecmp(key, "LastAddrType") == 0) {
167 dev_info->addr_type = g_variant_get_byte(val);
168 } else if (strcasecmp(key, "UUIDs") == 0) {
172 size = g_variant_get_size(val);
175 uuid_value = (char **)g_variant_get_strv(val, &size);
176 if (dev_info->uuids == NULL)
177 dev_info->uuids = g_malloc0(sizeof(char *) * size);
179 for (i = 0; uuid_value[i] != NULL; i++) {
180 dev_info->uuid_count++;
181 dev_info->uuids[i] = g_strdup(uuid_value[i]);
185 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
186 g_variant_get(val, "(i)", &dev_info->manufacturer_data_len);
187 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
188 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
189 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
192 if (dev_info->manufacturer_data_len == 0)
193 dev_info->manufacturer_data = g_strdup("");
194 } else if (strcasecmp(key, "ManufacturerData") == 0) {
196 GVariant *manufacturer_var;
197 g_variant_get(val, "@ay", &manufacturer_var);
198 len = g_variant_get_size(manufacturer_var);
200 char *manufacturer_data = (char *)g_variant_get_data(manufacturer_var);
201 dev_info->manufacturer_data = g_malloc0(len);
202 if (dev_info->manufacturer_data)
203 memcpy(dev_info->manufacturer_data, manufacturer_data,
206 g_variant_unref(manufacturer_var);
214 static gboolean __bt_parse_interface(GVariant *msg,
215 bt_remote_dev_info_t *dev_info)
218 GVariant *optional_param;
221 char *interface_name= NULL;
222 GVariant *inner_iter = NULL;
223 g_variant_get(msg, "(&o@a{sa{sv}})",
224 &path, &optional_param);
225 g_variant_iter_init(&iter, optional_param);
227 while ((child = g_variant_iter_next_value(&iter))) {
228 g_variant_get(child,"{&s@a{sv}}", &interface_name, &inner_iter);
229 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
230 BT_DBG("Found a device: %s", path);
231 if (__bt_parse_device_properties(inner_iter,
232 dev_info) == FALSE) {
233 g_variant_unref(inner_iter);
234 g_variant_unref(child);
235 g_variant_unref(optional_param);
236 BT_ERR("Fail to parse the properies");
239 g_variant_unref(inner_iter);
240 g_variant_unref(child);
241 g_variant_unref(optional_param);
245 g_variant_unref(inner_iter);
246 g_variant_unref(child);
249 g_variant_unref(optional_param);
254 static int __bt_get_owner_info(GVariant *msg, char **name,
255 char **previous, char **current)
257 g_variant_get(msg, "(sss)", name, previous, current);
258 return BLUETOOTH_ERROR_NONE;
261 static int __bt_get_agent_signal_info(GVariant *msg, char **address,
262 char **name, char **uuid)
264 g_variant_get(msg, "(sss)", address, name, uuid);
265 return BLUETOOTH_ERROR_NONE;
268 void __bt_set_device_values(gboolean connected, int state)
270 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
272 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0) {
273 BT_ERR("vconf_get_int failed");
276 if (connected == TRUE)
277 bt_device_state |= state;
278 else if (bt_device_state & state)
279 bt_device_state ^= state;
281 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
282 BT_ERR("vconf_set_int failed");
286 gboolean _bt_discovery_finished_cb(gpointer user_data)
288 int result = BLUETOOTH_ERROR_NONE;
290 GVariant *param = NULL;
291 if (_bt_get_discovering_property(DISCOVERY_ROLE_BREDR) == FALSE) {
292 if (_bt_get_cancel_by_user() == TRUE) {
293 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
296 _bt_set_cancel_by_user(FALSE);
297 _bt_set_discovery_status(FALSE);
298 param = g_variant_new("(i)", result);
299 _bt_send_event(BT_ADAPTER_EVENT,
300 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
307 static gboolean __bt_le_discovery_finished_cb(gpointer user_data)
309 int result = BLUETOOTH_ERROR_NONE;
311 GVariant *param = NULL;
312 if (_bt_get_discovering_property(DISCOVERY_ROLE_LE) == FALSE) {
313 if (_bt_get_cancel_by_user() == TRUE) {
314 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
317 _bt_set_cancel_by_user(FALSE);
318 _bt_disable_all_scanner_status();
319 _bt_set_le_scan_status(FALSE);
320 param = g_variant_new("(i)", result);
321 _bt_send_event(BT_LE_ADAPTER_EVENT,
322 BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED,
329 void __bt_update_remote_cache_devinfo(const char *address, gboolean paired_status)
333 ret_if(address == NULL);
336 bt_cache_info_t *cache_info;
337 bt_remote_dev_info_t *dev_info;
339 node = g_list_first(p_cache_list);
341 while (node != NULL){
342 cache_info = (bt_cache_info_t *)node->data;
344 if (cache_info == NULL) {
345 node = g_list_next(node);
349 dev_info = cache_info->dev_info;
350 if (strcasecmp(dev_info->address,
352 BT_DBG("Device Found");
353 if (paired_status == TRUE)
354 cache_info->dev_info->paired = TRUE;
356 cache_info->dev_info->paired = FALSE;
359 node = g_list_next(node);
364 static void __bt_device_remote_connected_properties(
365 bt_remote_dev_info_t *remote_dev_info,
366 char *address, gboolean connected)
368 int result = BLUETOOTH_ERROR_NONE;
370 GVariant *param = NULL;
373 if (remote_dev_info->uuid_count > 0 ) {
374 for (i = 0; i<remote_dev_info->uuid_count; i++) {
375 char *uuid = remote_dev_info->uuids[i];
376 if (strcasecmp(uuid, HID_UUID) == 0){
377 int event = BLUETOOTH_EVENT_NONE;
379 event = (connected == TRUE) ?
380 BLUETOOTH_HID_CONNECTED :
381 BLUETOOTH_HID_DISCONNECTED;
382 param = g_variant_new("(is)", result,
384 _bt_send_event(BT_HID_EVENT, event,
394 void _bt_handle_adapter_event(GVariant *msg, const char *member)
398 int result = BLUETOOTH_ERROR_NONE;
399 GVariant *param = NULL;
400 ret_if(member == NULL);
402 if (strcasecmp(member, "DeviceCreated") == 0) {
403 char *object_path = NULL;
405 bt_remote_dev_info_t *remote_dev_info;
407 ret_if(_bt_is_device_creating() == FALSE);
409 /* Bonding from remote device */
410 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
412 g_variant_get(msg, "(&o)", &object_path);
413 _bt_convert_device_path_to_address((const char*)object_path, address);
415 remote_dev_info = _bt_get_remote_device_info(address);
416 if (remote_dev_info == NULL) {
421 _bt_free_device_info(remote_dev_info);
423 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
424 char *object_path = NULL;
426 bt_cache_info_t *cache_info;
427 bt_remote_dev_info_t *dev_info;
430 /* Bonding from remote device */
431 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
432 g_variant_get(msg, "(&o)", &object_path);
434 /* Fix : NULL_RETURNS */
438 _bt_convert_device_path_to_address((const char *)object_path, address);
440 node = g_list_first(p_cache_list);
442 while (node != NULL){
443 cache_info = (bt_cache_info_t *)node->data;
445 if (cache_info == NULL) {
446 node = g_list_next(node);
450 dev_info = cache_info->dev_info;
451 if (strcasecmp(dev_info->address,
453 p_cache_list = g_list_remove(p_cache_list,
455 __bt_free_cache_info(cache_info);
458 node = g_list_next(node);
461 } else if (strcasecmp(member, "AdvertisingEnabled") == 0) {
462 BT_DBG("Advertising Enabled");
467 gboolean status = FALSE;
469 g_variant_get(msg, "(ib)", &slot_id, &status);
471 BT_DBG("Advertising Enabled : slot_id [%d] status [%d]", slot_id, status);
473 /* Send event to application */
474 _bt_set_advertising_status(slot_id, status);
476 sender = _bt_get_adv_slot_owner(slot_id);
477 adv_handle = _bt_get_adv_slot_adv_handle(slot_id);
480 event = BLUETOOTH_EVENT_ADVERTISING_STARTED;
482 event = BLUETOOTH_EVENT_ADVERTISING_STOPPED;
483 param = g_variant_new("(ii)", result,
487 _bt_send_event_to_dest(sender, BT_ADAPTER_EVENT,
491 _bt_send_event(BT_ADAPTER_EVENT, event, param);
494 if (event == BLUETOOTH_EVENT_ADVERTISING_STOPPED)
495 _bt_unregister_adv_slot_owner(slot_id);
496 } else if (strcasecmp(member, "RssiEnabled") == 0) {
497 BT_DBG("RSSI Enabled");
498 gboolean status = FALSE;
499 char *address = NULL;
501 g_variant_get(msg,"(sib)", &address, &link_type, &status);
503 BT_DBG("RSSI Enabled [%s %d]", address, status);
504 param = g_variant_new("(isib)", result,
505 address, link_type, status);
506 _bt_send_event(BT_DEVICE_EVENT,
507 BLUETOOTH_EVENT_RSSI_ENABLED,
510 } else if (strcasecmp(member, "RssiAlert") == 0) {
511 BT_DBG("RSSI Alert");
515 char *address = NULL;
516 g_variant_get(msg,"(siii)", &address, &link_type, &alert_type, &rssi_dbm);
518 BT_DBG("RSSI Alert: [Address %s LinkType %d] [Type %d DBM %d]",
519 address, alert_type, rssi_dbm);
520 param = g_variant_new("(isiii)", result,
521 address, link_type, alert_type, rssi_dbm);
522 _bt_send_event(BT_DEVICE_EVENT,
523 BLUETOOTH_EVENT_RSSI_ALERT,
526 } else if (strcasecmp(member, "RawRssi") == 0) {
530 char *address = NULL;
531 g_variant_get(msg,"(sii)", &address, &link_type, &rssi_dbm);
533 BT_DBG("Raw RSSI: [Address %s] [Link Type %d][RSSI DBM %d]",
534 address, link_type, rssi_dbm);
535 param = g_variant_new("(isii)", result,
536 address, link_type, rssi_dbm);
537 _bt_send_event(BT_DEVICE_EVENT,
538 BLUETOOTH_EVENT_RAW_RSSI,
541 } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
542 BT_ERR_C("Hardware error received from BLUEZ");
543 _bt_recover_adapter();
544 } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
545 BT_ERR_C("Tx timeout error received from BLUEZ");
546 _bt_recover_adapter();
551 static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
553 GDBusProxy *adapter_proxy;
555 int result = BLUETOOTH_ERROR_NONE;
556 GVariantIter value_iter;
557 GVariant *val = NULL;
559 char *property = NULL;
560 GVariant *param = NULL;
561 g_variant_iter_init (&value_iter, msg);
562 while ((g_variant_iter_loop (&value_iter,"{sv}", &property, &val))) {
563 BT_INFO("Property %s", property);
565 if (strcasecmp(property, "Discovering") == 0) {
566 gboolean discovering = FALSE;
567 g_variant_get(val, "b", &discovering);
568 /* Send event to application */
569 BT_DBG("Discovering %d", discovering);
570 if (discovering == TRUE) {
571 _bt_set_discovery_status(TRUE);
572 param = g_variant_new("(i)", result);
573 _bt_send_event(BT_ADAPTER_EVENT,
574 BLUETOOTH_EVENT_DISCOVERY_STARTED,
577 ret_if(event_id > 0);
579 adapter_proxy = _bt_get_adapter_proxy();
580 ret_if(adapter_proxy == NULL);
582 /* Need to stop searching */
583 g_dbus_proxy_call_sync(adapter_proxy, "StopDiscovery",
585 G_DBUS_CALL_FLAGS_NONE,
589 BT_ERR("Dbus Error : %s", err->message);
593 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
594 (GSourceFunc)_bt_discovery_finished_cb, NULL);
596 } else if (strcasecmp(property, "LEDiscovering") == 0) {
597 gboolean le_discovering = FALSE;
599 g_variant_get(val, "b", &le_discovering);
600 /* Send event to application */
601 if (le_discovering == TRUE) {
602 _bt_set_le_scan_status(TRUE);
603 param = g_variant_new("(i)", result);
604 _bt_send_event(BT_LE_ADAPTER_EVENT,
605 BLUETOOTH_EVENT_LE_DISCOVERY_STARTED,
608 ret_if(event_id > 0);
610 adapter_proxy = _bt_get_adapter_proxy();
611 ret_if(adapter_proxy == NULL);
613 /* Need to stop searching */
614 g_dbus_proxy_call_sync(adapter_proxy, "StopLEDiscovery",
616 G_DBUS_CALL_FLAGS_NONE,
620 BT_ERR("Dbus Error %s", err->message);
624 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
625 (GSourceFunc)__bt_le_discovery_finished_cb, NULL);
627 } else if (strcasecmp(property, "Name") == 0) {
629 g_variant_get(val, "s", &name);
630 param = g_variant_new("(is)", result, name);
631 /* Send event to application */
632 _bt_send_event(BT_ADAPTER_EVENT,
633 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
636 } else if (strcasecmp(property, "Alias") == 0) {
638 g_variant_get(val, "s", &alias);
639 param = g_variant_new("(is)", result, alias);
640 /* Send event to application */
641 _bt_send_event(BT_ADAPTER_EVENT,
642 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
645 } else if (strcasecmp(property, "Discoverable") == 0) {
646 gboolean discoverable = FALSE;
648 g_variant_get(val, "b", &discoverable);
649 BT_DBG("discoverable %d", discoverable);
651 if (discoverable == FALSE) {
652 if (_bt_get_discoverable_timeout_property() > 0) {
654 adapter_proxy = _bt_get_adapter_properties_proxy();
655 ret_if(adapter_proxy == NULL);
656 g_dbus_proxy_call_sync(adapter_proxy, "Set",
657 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
658 "DiscoverableTimeout",
659 g_variant_new("i", time)),
660 G_DBUS_CALL_FLAGS_NONE,
665 BT_ERR("StopLEDiscovery Failed: %s", err->message);
670 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
672 /* Send event to application */
673 BT_INFO("[Connectable]");
674 param = g_variant_new("(in)", result, mode);
675 _bt_send_event(BT_ADAPTER_EVENT,
676 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
679 _bt_get_discoverable_mode(&mode);
681 /* Event will be sent by "DiscoverableTimeout" signal */
682 if (mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
684 g_variant_unref(val);
688 /* Send event to application */
689 BT_INFO("[General Discoverable]");
690 param = g_variant_new("(in)", result, mode);
691 _bt_send_event(BT_ADAPTER_EVENT,
692 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
695 } else if (strcasecmp(property, "DiscoverableTimeout") == 0) {
696 _bt_get_discoverable_mode(&mode);
698 /* Event was already sent by "Discoverable" signal */
699 if (mode == BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE ||
700 mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
702 g_variant_unref(val);
706 /* Send event to application */
707 BT_INFO("[Limited Discoverable (Timeout %u secs)]",
708 _bt_get_discoverable_timeout_property());
709 param = g_variant_new("(in)", result, mode);
710 _bt_send_event(BT_ADAPTER_EVENT,
711 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
713 } else if (strcasecmp(property, "Powered") == 0) {
714 /* TODO: Need to check this operation!! */
715 gboolean powered = FALSE;
718 g_variant_get(val, "b" ,&powered);
719 BT_DBG("Powered = %d", powered);
720 if (powered == FALSE) {
722 _bt_handle_adapter_removed();
724 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_state) == 0 &&
725 bt_state != VCONFKEY_BT_STATUS_OFF) {
726 _bt_disable_adapter();
729 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_state) == 0 &&
730 bt_state != VCONFKEY_BT_LE_STATUS_OFF) {
731 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
736 _bt_handle_adapter_added();
739 } else if (strcasecmp(property, "Connectable") == 0) {
740 gboolean connectable = FALSE;
742 g_variant_get(val, "b", &connectable);
744 BT_DBG("Connectable property is changed : %d", connectable);
745 param = g_variant_new("(b)", connectable);
746 _bt_send_event(BT_ADAPTER_EVENT,
747 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
749 if (_bt_adapter_get_status() == BT_DEACTIVATING &&
750 _bt_adapter_get_le_status() == BT_LE_ACTIVATED &&
752 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
753 } else if (strcasecmp(property, "SupportedLEFeatures") == 0) {
756 GVariantIter *iter = NULL;
757 g_variant_get(val, "as", &iter);
759 while (g_variant_iter_loop(iter, "s", &name)) {
760 BT_DBG("name = %s", name);
761 g_variant_iter_loop(iter, "s", &value);
762 BT_DBG("Value = %s", value);
763 if (FALSE == _bt_update_le_feature_support(name, value))
764 BT_INFO("Fail to update LE feature info");
766 g_variant_iter_free(iter);
768 } else if (strcasecmp(property, "IpspInitStateChanged") == 0) {
769 gboolean ipsp_initialized = FALSE;
771 g_variant_get(val, "b", &ipsp_initialized);
772 BT_DBG("IPSP init state changed: %d", ipsp_initialized);
773 param = g_variant_new("(b)", ipsp_initialized);
775 /* Send event to application */
776 _bt_send_event(BT_ADAPTER_EVENT,
777 BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,
783 static void __bt_obex_property_changed_event(GVariant *msg, const char *path)
787 GVariantIter value_iter;
788 GVariant *child = NULL, *val = NULL;
789 char *property = NULL;
790 g_variant_iter_init (&value_iter, msg);
791 while ((child = g_variant_iter_next_value (&value_iter))) {
792 g_variant_get(child, "{sv}", &property, &val);
794 ret_if(property == NULL);
796 BT_DBG("property :%s", property);
798 if (strcasecmp(property, "Status") == 0) {
800 g_variant_get(val, "s", &status);
802 if (strcasecmp(status, "active") == 0){
803 _bt_obex_transfer_started(path);
804 } else if (strcasecmp(status, "complete") == 0) {
805 _bt_obex_transfer_completed(path, TRUE);
806 _bt_pbap_obex_transfer_completed(path, TRUE);
807 } else if (strcasecmp(status, "error") == 0){
808 _bt_obex_transfer_completed(path, FALSE);
809 _bt_pbap_obex_transfer_completed(path, FALSE);
812 } else if (strcasecmp(property, "Transferred") == 0) {
813 static int transferred = 0;
814 g_variant_get(val, "t", &transferred);
816 _bt_obex_transfer_progress(path,transferred);
819 g_variant_unref(val);
820 g_variant_unref(child);
825 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
830 int result = BLUETOOTH_ERROR_NONE;
831 GVariantIter value_iter;
833 char *property = NULL;
835 GVariant *param = NULL;
836 bt_remote_dev_info_t *remote_dev_info;
837 g_variant_iter_init (&value_iter, msg);
838 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
839 BT_DBG("Property %s", property);
840 if (strcasecmp(property, "Connected") == 0) {
843 g_variant_get(val, "i", &connected);
845 event = (connected != BLUETOOTH_CONNECTED_LINK_NONE) ?
846 BLUETOOTH_EVENT_DEVICE_CONNECTED :
847 BLUETOOTH_EVENT_DEVICE_DISCONNECTED;
849 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
851 _bt_convert_device_path_to_address(path, address);
853 BT_DBG("connected: %d", connected);
854 BT_DBG("address: %s", address);
856 remote_dev_info = _bt_get_remote_device_info(address);
858 if (remote_dev_info != NULL) {
859 __bt_device_remote_connected_properties(
860 remote_dev_info, address,
861 connected != BLUETOOTH_CONNECTED_LINK_NONE ?
863 _bt_free_device_info(remote_dev_info);
865 param = g_variant_new("(is)", result, address);
866 /* Send event to application */
867 _bt_send_event(BT_DEVICE_EVENT,
871 } else if (strcasecmp(property, "RSSI") == 0) {
872 bt_remote_dev_info_t *remote_dev_info;
874 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
876 _bt_convert_device_path_to_address(path, address);
877 BT_DBG("address: %s", address);
879 remote_dev_info = _bt_get_remote_device_info(address);
880 if (remote_dev_info == NULL) {
882 g_variant_unref(val);
886 BT_DBG("Address type %d", remote_dev_info->addr_type);
888 if (remote_dev_info->addr_type == 0) {
889 BT_DBG("Name %s", remote_dev_info->name);
891 GVariant *uuids = NULL;
892 GVariantBuilder *builder = NULL;
894 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
895 for (i=0; i < remote_dev_info->uuid_count; i++) {
896 g_variant_builder_add(builder, "s",
897 remote_dev_info->uuids[i]);
899 uuids = g_variant_new("as", builder);
900 g_variant_builder_unref(builder);
901 GVariant *manufacturer_data = NULL;
902 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
903 remote_dev_info->manufacturer_data,
904 remote_dev_info->manufacturer_data_len,
907 param = g_variant_new("(isunsbub@asn@ay)", result,
908 remote_dev_info->address,
909 remote_dev_info->class,
910 remote_dev_info->rssi,
911 remote_dev_info->name,
912 remote_dev_info->paired,
913 remote_dev_info->connected,
914 remote_dev_info->trust,
916 remote_dev_info->manufacturer_data_len,
919 _bt_send_event(BT_ADAPTER_EVENT,
920 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
924 _bt_free_device_info(remote_dev_info);
925 } else if (strcasecmp(property, "GattConnected") == 0) {
926 gboolean gatt_connected = FALSE;
928 g_variant_get(val, "b", &gatt_connected);
930 event = gatt_connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
931 BLUETOOTH_EVENT_GATT_DISCONNECTED;
933 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
935 _bt_convert_device_path_to_address(path, address);
937 BT_DBG("gatt_connected: %d", gatt_connected);
938 BT_DBG("address: %s", address);
939 param = g_variant_new("(is)", result, address);
940 /* Send event to application */
941 _bt_send_event(BT_DEVICE_EVENT,
945 } else if (strcasecmp(property, "Paired") == 0) {
946 gboolean paired = FALSE;
947 bt_remote_dev_info_t *remote_dev_info;
948 g_variant_get(val, "b", &paired);
949 _bt_agent_set_canceled(FALSE);
950 /* BlueZ sends paired signal for each paired device */
951 /* during activation, We should ignore this, otherwise*/
952 /* application thinks that a new device got paired */
953 if (_bt_adapter_get_status() != BT_ACTIVATED) {
954 BT_DBG("BT is not activated, so ignore this");
956 g_variant_unref(val);
960 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
962 _bt_convert_device_path_to_address(path, address);
964 remote_dev_info = _bt_get_remote_device_info(address);
965 if (remote_dev_info == NULL) {
967 g_variant_unref(val);
972 if(paired == FALSE) {
973 BT_INFO("Unpaired: %s", address);
974 __bt_update_remote_cache_devinfo(address, FALSE);
975 param = g_variant_new("(is)", result, address);
976 _bt_send_event(BT_ADAPTER_EVENT,
977 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
980 BT_INFO("Paired: %s", address);
981 __bt_update_remote_cache_devinfo(address, TRUE);
983 if (_bt_is_device_creating() == TRUE) {
984 BT_DBG("Try to Pair by me");
985 _bt_free_device_info(remote_dev_info);
988 g_variant_unref(val);
991 GVariant *uuids = NULL;
992 GVariantBuilder *builder = NULL;
994 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
995 for (i=0; i < remote_dev_info->uuid_count; i++) {
996 g_variant_builder_add(builder, "s",
997 remote_dev_info->uuids[i]);
999 uuids = g_variant_new("as", builder);
1000 g_variant_builder_unref(builder);
1001 GVariant *manufacturer_data = NULL;
1002 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1003 remote_dev_info->manufacturer_data,
1004 remote_dev_info->manufacturer_data_len,
1008 param = g_variant_new("(isunsbub@asn@ay)", result,
1009 address, remote_dev_info->class,
1010 remote_dev_info->rssi,
1011 remote_dev_info->name,
1012 remote_dev_info->paired,
1013 remote_dev_info->connected,
1014 remote_dev_info->trust,
1016 remote_dev_info->manufacturer_data_len,
1018 _bt_send_event(BT_ADAPTER_EVENT,
1019 BLUETOOTH_EVENT_BONDING_FINISHED,
1022 _bt_free_device_info(remote_dev_info);
1024 } else if (strcasecmp(property, "LegacyPaired") == 0) {
1025 gboolean paired = FALSE;
1026 bt_remote_dev_info_t *remote_dev_info;
1028 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1029 BT_DBG("BT is not activated, so ignore this");
1031 g_variant_unref(val);
1035 g_variant_get(val, "b", &paired);
1036 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1037 BT_DBG("LegacyPaired: %d", paired);
1038 _bt_convert_device_path_to_address(path, address);
1040 remote_dev_info = _bt_get_remote_device_info(address);
1041 if (remote_dev_info == NULL) {
1044 g_variant_unref(val);
1048 BT_DBG("LegacyPairing Failed with %s. Show Error Popup",
1049 remote_dev_info->name);
1050 _bt_launch_system_popup(BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE,
1051 remote_dev_info->name, NULL, NULL, NULL);
1053 _bt_free_device_info(remote_dev_info);
1055 } else if (strcasecmp(property, "Trusted") == 0) {
1056 gboolean trusted = FALSE;
1058 g_variant_get(val, "b", &trusted);
1060 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1061 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1063 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1065 _bt_convert_device_path_to_address(path, address);
1067 BT_DBG("trusted: %d", trusted);
1068 BT_DBG("address: %s", address);
1069 param = g_variant_new("(is)", result, address);
1070 /* Send event to application */
1071 _bt_send_event(BT_DEVICE_EVENT,
1075 } else if (strcasecmp(property, "IpspConnected") == 0) {
1076 gboolean connected = FALSE;
1078 g_variant_get(val, "b", &connected);
1081 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
1082 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
1084 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1086 _bt_convert_device_path_to_address(path, address);
1088 BT_DBG("Ipspconnected: %d", connected);
1089 BT_DBG("address: %s", address);
1090 param = g_variant_new("(is)", result, address);
1092 /* Send event to application */
1093 _bt_send_event(BT_DEVICE_EVENT,
1102 static void __bt_media_control_changed_event(GVariant *msg, const char *path)
1105 int result = BLUETOOTH_ERROR_NONE;
1106 GVariantIter value_iter;
1107 char *property = NULL;
1109 GVariant *val = NULL;
1110 GVariant *child = NULL;
1111 bt_remote_dev_info_t *remote_dev_info;
1112 GVariant *param = NULL;
1113 g_variant_iter_init (&value_iter, msg);
1114 while ((child = g_variant_iter_next_value (&value_iter))) {
1115 g_variant_get(child, "{sv}", &property, &val);
1116 BT_INFO("Property %s", property);
1117 if (strcasecmp(property, "Connected") == 0) {
1118 gboolean connected = FALSE;
1120 g_variant_get(val, "b", &connected);
1122 event = connected ? BLUETOOTH_EVENT_AVRCP_CONNECTED :
1123 BLUETOOTH_EVENT_AVRCP_DISCONNECTED;
1125 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1127 _bt_convert_device_path_to_address(path, address);
1129 BT_DBG("connected: %d", connected);
1130 BT_DBG("address: %s", address);
1132 remote_dev_info = _bt_get_remote_device_info(address);
1134 if (remote_dev_info != NULL) {
1135 __bt_device_remote_connected_properties(
1136 remote_dev_info, address, connected);
1137 _bt_free_device_info(remote_dev_info);
1139 param = g_variant_new("(is)", result, address);
1140 /* Send event to application */
1141 _bt_send_event(BT_AVRCP_EVENT,
1147 g_variant_unref(child);
1148 g_variant_unref(val);
1153 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
1155 char *interface_name = NULL;
1156 GVariant *val = NULL;
1158 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &val,NULL);
1160 if (strcasecmp(interface_name, BT_ADAPTER_INTERFACE) == 0) {
1161 __bt_adapter_property_changed_event(val,
1163 } else if (strcasecmp(interface_name, BT_DEVICE_INTERFACE) == 0) {
1164 __bt_device_property_changed_event(val,object_path);
1165 } else if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1166 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
1167 __bt_obex_property_changed_event(val,
1169 } else if (strcasecmp(interface_name, BT_MEDIA_CONTROL_INTERFACE) == 0) {
1170 __bt_media_control_changed_event(val,
1172 } else if (strcasecmp(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
1173 _bt_handle_avrcp_control_event(val,
1175 } else if (strcasecmp(interface_name, BT_NETWORK_CLIENT_INTERFACE) == 0) {
1176 BT_DBG("BT_NETWORK_CLIENT_INTERFACE");
1177 _bt_handle_network_client_event(val,
1179 } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
1180 __bt_gatt_char_property_changed_event(val,
1183 g_variant_unref(val);
1186 void __bt_opc_property_changed_event(GVariant *msg,
1189 GVariantIter value_iter;
1190 char *property = NULL;
1191 GVariant *val = NULL;
1192 GVariant *child = NULL;
1194 g_variant_iter_init (&value_iter, msg);
1195 while ((child = g_variant_iter_next_value (&value_iter))) {
1196 g_variant_get(child, "{sv}", &property, &val);
1197 ret_if(property == NULL);
1199 if (strcasecmp(property, "Status") == 0) {
1200 char *status = NULL;
1201 g_variant_get(val, "s", &status);
1202 BT_DBG("Status is %s", status);
1204 if(strcasecmp(status, "active") == 0){
1205 _bt_obex_client_started(path);
1206 }else if (strcasecmp(status, "complete") == 0) {
1207 _bt_obex_client_completed(path, TRUE);
1208 }else if (strcasecmp(status, "error") == 0){
1209 _bt_obex_client_completed(path, FALSE);
1212 } else if (strcasecmp(property, "Transferred") == 0) {
1213 static int transferred = 0;
1214 g_variant_get(val, "t", &transferred);
1216 _bt_obex_client_progress(path, transferred);
1218 BT_DBG("property : [%s]", property);
1221 g_variant_unref(child);
1222 g_variant_unref(val);
1226 void _bt_opc_property_changed_event(GVariant *msg, char *path)
1228 char *interface_name = NULL;
1229 GVariant *value = NULL;
1230 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1231 BT_INFO("interface_name = %s", interface_name);
1232 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1233 __bt_opc_property_changed_event(value,
1236 BT_DBG("interface_name : [%s]", interface_name);
1238 g_variant_unref(value);
1242 void _bt_handle_input_event(GVariant *msg, const char *path)
1244 int result = BLUETOOTH_ERROR_NONE;
1245 gboolean property_flag = FALSE;
1246 GVariantIter value_iter;
1247 char *property = NULL;
1248 GVariant *child = NULL, *val = NULL;
1249 bt_remote_dev_info_t *remote_dev_info;
1250 GVariant *param = NULL;
1251 g_variant_iter_init (&value_iter, msg);
1252 while ((child = g_variant_iter_next_value (&value_iter))) {
1253 g_variant_get(child, "{sv}", &property, &val);
1255 ret_if(property == NULL);
1257 if (strcasecmp(property, "Connected") == 0) {
1258 int event = BLUETOOTH_EVENT_NONE;
1260 g_variant_get(val, "b", &property_flag);
1262 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1264 _bt_convert_device_path_to_address(path, address);
1266 event = (property_flag == TRUE) ?
1267 BLUETOOTH_HID_CONNECTED :
1268 BLUETOOTH_HID_DISCONNECTED;
1269 param = g_variant_new("(is)", result, address);
1270 _bt_send_event(BT_HID_EVENT, event,
1272 /* Check HID connection type (Keyboard or Mouse) and update the status */
1273 remote_dev_info = _bt_get_remote_device_info(address);
1275 if (property_flag == TRUE) {
1276 hid_connected_device_count++;
1277 __bt_set_device_values(TRUE,
1278 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1280 hid_connected_device_count--;
1281 if (hid_connected_device_count == 0)
1282 __bt_set_device_values(FALSE,
1283 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1286 if (remote_dev_info != NULL) {
1287 BT_DBG("HID device class [%x]", remote_dev_info->class);
1288 if (remote_dev_info->class &
1289 BLUETOOTH_DEVICE_MINOR_CLASS_KEY_BOARD) {
1290 #ifdef ENABLE_TIZEN_2_4
1291 __bt_set_device_values(property_flag,
1292 VCONFKEY_BT_DEVICE_HID_KEYBOARD_CONNECTED);
1297 if (remote_dev_info->class &
1298 BLUETOOTH_DEVICE_MINOR_CLASS_POINTING_DEVICE) {
1299 #ifdef ENABLE_TIZEN_2_4
1300 __bt_set_device_values(property_flag,
1301 VCONFKEY_BT_DEVICE_HID_MOUSE_CONNECTED);
1304 _bt_free_device_info(remote_dev_info);
1309 g_variant_unref(val);
1310 g_variant_unref(child);
1314 void _bt_handle_network_server_event(GVariant *msg, const char *member)
1316 int result = BLUETOOTH_ERROR_NONE;
1317 char *address = NULL;
1318 char *device = NULL;
1319 GVariant *param = NULL;
1320 ret_if(member == NULL);
1321 if (strcasecmp(member, "PeerConnected") == 0) {
1322 g_variant_get(msg, "(ss)", &device, &address);
1324 __bt_set_device_values(TRUE,
1325 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1326 param = g_variant_new("(iss)", result, device, address);
1327 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
1331 nap_connected_device_count++;
1332 } else if (strcasecmp(member, "PeerDisconnected") == 0) {
1333 g_variant_get(msg, "(ss)", &device, &address);
1334 nap_connected_device_count--;
1335 if (nap_connected_device_count == 0)
1336 __bt_set_device_values(FALSE,
1337 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1338 param = g_variant_new("(iss)", result, device, address);
1339 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
1346 void _bt_handle_network_client_event(GVariant *msg,
1351 int result = BLUETOOTH_ERROR_NONE;
1352 gboolean property_flag = FALSE;
1353 char *property = NULL;
1354 GVariant *val = NULL;
1355 GVariantIter value_iter;
1356 GVariant *param = NULL;
1357 g_variant_iter_init (&value_iter, msg);
1358 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1359 if (strcasecmp(property, "Connected") == 0) {
1360 int event = BLUETOOTH_EVENT_NONE;
1363 g_variant_get(val, "b", &property_flag);
1364 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1366 _bt_convert_device_path_to_address(path, address);
1368 BT_DBG("property_flag %d", property_flag);
1369 if (property_flag == TRUE) {
1370 event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
1371 nap_connected_device_count++;
1372 __bt_set_device_values(TRUE,
1373 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1375 event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
1376 nap_connected_device_count--;
1377 if (nap_connected_device_count == 0)
1378 __bt_set_device_values(FALSE,
1379 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1381 param = g_variant_new("(is)", result, address);
1382 _bt_send_event(BT_NETWORK_EVENT, event,
1391 void __bt_gatt_char_property_changed_event(GVariant *msg,
1394 GVariantIter value_iter;
1395 char *property = NULL;
1396 char * char_handle = NULL;
1397 GVariant *val = NULL;
1398 int result = BLUETOOTH_ERROR_NONE;
1399 GVariant *param = NULL;
1400 g_variant_iter_init (&value_iter, msg);
1401 char_handle = g_strdup(path);
1402 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1403 BT_INFO("Property %s", property);
1405 ret_if(property == NULL);
1407 if (strcasecmp(property, "Notifying") == 0) {
1408 gboolean property_flag = FALSE;
1409 g_variant_get(val, "b", &property_flag);
1410 if (property_flag == TRUE)
1411 BT_DBG("notifying is enabled");
1413 BT_DBG("notifying is disabled");
1414 } else if (strcasecmp(property, "ChangedValue") == 0) {
1416 GByteArray *gp_byte_array = NULL;
1417 BT_INFO("Type '%s'\n", g_variant_get_type_string (val));
1420 gp_byte_array = g_byte_array_new();
1421 len = g_variant_get_size(val);
1422 BT_DBG("Len = %d", len);
1423 g_byte_array_append (gp_byte_array,
1424 (const guint8 *) g_variant_get_data(val), len);
1425 if (gp_byte_array->len != 0) {
1426 GVariant *byte_array = NULL;
1427 byte_array = g_variant_new_from_data(
1428 G_VARIANT_TYPE_BYTESTRING,
1429 gp_byte_array->data,
1432 param = g_variant_new("(is@ay)", result, char_handle,
1435 /* Send event only registered client */
1436 _bt_send_char_value_changed_event(param);
1438 g_byte_array_free(gp_byte_array, TRUE);
1442 g_free(char_handle);
1445 void _bt_handle_gatt_event(GVariant *msg, const char *member, const char *path)
1447 ret_if(path == NULL);
1449 if (strcasecmp(member, "GattValueChanged") == 0) {
1452 /*** Debug only ***/
1453 GVariant *value = NULL;
1455 char *buffer = NULL;
1457 g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
1458 value_len = g_variant_get_size(value);
1459 if (value_len > 0) {
1460 char buf[8 * 5 + 1] = { 0 };
1463 buffer = (char *)g_variant_get_data(value);
1464 to = value_len > (sizeof(buf) / 5) ? sizeof(buf) / 5 : value_len;
1466 for (i = 0; i < to; i++)
1467 snprintf(&buf[i * 5], 6, "0x%02x ", buffer[i]);
1469 BT_DBG("GATT Val[%d] %s", value_len, buf);
1471 g_variant_unref(value);
1475 /* Send event only registered client */
1476 _bt_send_char_value_changed_event(msg);
1481 void _bt_handle_device_event(GVariant *msg, const char *member, const char *path)
1484 int result = BLUETOOTH_ERROR_NONE;
1487 const char *property = NULL;
1488 GVariant *param = NULL;
1489 ret_if(path == NULL);
1491 if (strcasecmp(member, "PropertyChanged") == 0) {
1493 g_variant_get(msg, "(s)", &property);
1495 ret_if(property == NULL);
1497 if (strcasecmp(property, "GattConnected") == 0) {
1498 gboolean connected = FALSE;
1500 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1501 ret_if(address == NULL);
1503 _bt_convert_device_path_to_address(path, address);
1504 g_variant_get(msg, "(b)", &connected);
1506 event = connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1507 BLUETOOTH_EVENT_GATT_DISCONNECTED;
1508 param = g_variant_new("(is)", result, address);
1509 _bt_send_event(BT_DEVICE_EVENT,
1513 } else if (strcasecmp(property, "Paired") == 0) {
1514 gboolean paired = FALSE;
1515 bt_remote_dev_info_t *remote_dev_info;
1516 g_variant_get(msg, "(b)", &paired);
1518 ret_if(paired == FALSE);
1520 /* BlueZ sends paired signal for each paired device */
1521 /* during activation, We should ignore this, otherwise*/
1522 /* application thinks that a new device got paired */
1523 if (_bt_adapter_get_status() != BT_ACTIVATED) {
1524 BT_DBG("BT is not activated, so ignore this");
1528 if (_bt_is_device_creating() == TRUE) {
1529 BT_DBG("Try to Pair by me");
1533 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1534 ret_if(address == NULL);
1536 _bt_convert_device_path_to_address(path, address);
1538 remote_dev_info = _bt_get_remote_device_info(address);
1539 if (remote_dev_info == NULL) {
1543 GVariant *uuids = NULL;
1544 GVariantBuilder *builder = NULL;
1546 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1547 for (i=0; i < remote_dev_info->uuid_count; i++) {
1548 g_variant_builder_add(builder, "s",
1549 remote_dev_info->uuids[i]);
1551 uuids = g_variant_new("as", builder);
1552 g_variant_builder_unref(builder);
1553 GVariant *manufacturer_data = NULL;
1554 manufacturer_data = g_variant_new_from_data(
1555 G_VARIANT_TYPE_BYTESTRING,
1556 remote_dev_info->manufacturer_data,
1557 remote_dev_info->manufacturer_data_len,
1559 param = g_variant_new("(isunsbub@asn@ay)", result,
1561 remote_dev_info->class,
1562 remote_dev_info->rssi,
1563 remote_dev_info->name,
1564 remote_dev_info->paired,
1565 remote_dev_info->connected,
1566 remote_dev_info->trust,
1568 remote_dev_info->manufacturer_data_len,
1570 _bt_send_event(BT_ADAPTER_EVENT,
1571 BLUETOOTH_EVENT_BONDING_FINISHED,
1573 _bt_free_device_info(remote_dev_info);
1576 } else if (strcasecmp(property, "UUIDs") == 0) {
1577 /* Once we get the updated uuid information after
1578 * reverse service search, update it to application */
1580 bt_remote_dev_info_t *remote_dev_info;
1582 ret_if(_bt_is_device_creating() == TRUE);
1584 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1585 ret_if(address == NULL);
1587 _bt_convert_device_path_to_address(path, address);
1589 remote_dev_info = _bt_get_remote_device_info(address);
1590 if (remote_dev_info == NULL) {
1595 BT_DBG("UUID's count = %d", remote_dev_info->uuid_count);
1596 if (remote_dev_info->paired && remote_dev_info->uuid_count) {
1597 GVariant *uuids = NULL;
1598 GVariantBuilder *builder = NULL;
1600 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1601 for (i=0; i < remote_dev_info->uuid_count; i++) {
1602 g_variant_builder_add(builder, "s",
1603 remote_dev_info->uuids[i]);
1605 uuids = g_variant_new("as", builder);
1606 g_variant_builder_unref(builder);
1607 GVariant *manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1608 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1611 param = g_variant_new("(isunsbub@asn@ay)", result,
1612 address, remote_dev_info->class,
1613 remote_dev_info->rssi,
1614 remote_dev_info->name,
1615 remote_dev_info->paired,
1616 remote_dev_info->connected,
1617 remote_dev_info->trust,
1619 remote_dev_info->manufacturer_data_len,
1621 _bt_send_event(BT_ADAPTER_EVENT,
1622 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1626 _bt_free_device_info(remote_dev_info);
1629 } else if (strcasecmp(member, "DeviceConnected") == 0) {
1630 unsigned char addr_type = 0;
1632 g_variant_get(msg, "(y)", &addr_type);
1634 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1635 ret_if(address == NULL);
1637 _bt_convert_device_path_to_address(path, address);
1638 dev_name = _bt_get_bonded_device_name(address);
1640 BT_INFO("Address : %s Type : %d", address, addr_type);
1641 BT_ERR_C("Connected [%s] [%s]", !addr_type ? "BREDR" : "LE",
1642 !addr_type ? dev_name : address);
1645 _bt_logging_connection(TRUE, addr_type);
1646 #ifdef ENABLE_TIZEN_2_4
1647 journal_bt_connected();
1649 param = g_variant_new("(isy)", result, address, addr_type);
1650 /*Send event to application*/
1651 _bt_send_event(BT_DEVICE_EVENT,
1652 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1655 } else if (strcasecmp(member, "Disconnected") == 0) {
1656 unsigned char disc_reason = 0;
1657 unsigned char addr_type = 0;
1658 gboolean sending = FALSE;
1660 g_variant_get(msg, "(yy)", &addr_type, &disc_reason);
1662 result = disc_reason;
1664 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1665 ret_if(address == NULL);
1667 _bt_convert_device_path_to_address(path, address);
1668 dev_name = _bt_get_bonded_device_name(address);
1669 #ifdef ENABLE_TIZEN_2_4
1670 journal_bt_disconnected();
1673 /* 0x00 BDADDR_BRDER
1674 0x01 BDADDR_LE_PUBLIC
1675 0x02 BDADDR_LE_RANDOM */
1676 BT_INFO("Address : %s Type : %d", address, addr_type);
1677 BT_ERR_C("Disconnected [%s] [%d : %s] [%s]", !addr_type ? "BREDR" : "LE",
1678 disc_reason, _bt_convert_disc_reason_to_string(disc_reason),
1679 !addr_type ? dev_name : address);
1682 _bt_headset_set_local_connection(FALSE);
1683 _bt_logging_connection(FALSE, addr_type);
1685 /*Check for any OPP transfer on the device and cancel
1688 _bt_obex_check_pending_transfer(address);
1689 _bt_opp_client_is_sending(&sending);
1691 _bt_opp_client_check_pending_transfer(address);
1692 param = g_variant_new("(isy)", result, address, addr_type);
1693 _bt_send_event(BT_DEVICE_EVENT,
1694 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1697 } else if (strcasecmp(member, "ProfileStateChanged") == 0) {
1699 char *profile_uuid = NULL;
1700 bt_headset_wait_t *wait_list;
1702 g_variant_get(msg, "(si)", &profile_uuid, &state);
1704 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1705 ret_if(address == NULL);
1707 _bt_convert_device_path_to_address(path, address);
1709 BT_DBG("Address: %s", address);
1710 BT_DBG("Profile UUID: %s", profile_uuid);
1711 BT_DBG("State: %d", state);
1713 if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1714 (state == BT_PROFILE_STATE_CONNECTED)) {
1716 int event = BLUETOOTH_EVENT_AV_CONNECTED;
1717 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1718 bluetooth_device_address_t device_address;
1720 bt_headset_wait_t *wait_list;
1722 __bt_set_device_values(TRUE,
1723 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1725 __bt_connection_manager_set_state(address, event);
1727 if (_bt_headset_get_local_connection() == FALSE)
1728 _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
1730 /* Connection Started from local device therefore no need to
1731 * intiate connection for pending profile */
1732 _bt_headset_set_local_connection(FALSE);
1734 param = g_variant_new("(is)", result, address);
1735 _bt_send_event(BT_HEADSET_EVENT, event,
1737 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
1740 if (g_strcmp0(connected_address, address) != 0) {
1741 _bt_convert_addr_string_to_type(
1742 device_address.addr,
1744 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
1745 &device_address, NULL);
1749 _bt_add_headset_to_list(BT_AUDIO_A2DP,
1750 BT_STATE_CONNECTED, address);
1752 wait_list = _bt_get_audio_wait_data();
1753 if (wait_list != NULL &&
1754 (g_strcmp0(wait_list->address, address) == 0))
1755 _bt_rel_wait_data();
1757 } else if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0) &&
1758 (state == BT_PROFILE_STATE_DISCONNECTED)) {
1760 int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
1762 if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
1764 g_free(profile_uuid);
1768 __bt_set_device_values(FALSE,
1769 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1771 __bt_connection_manager_set_state(address, event);
1772 param = g_variant_new("(is)", result, address);
1773 _bt_send_event(BT_HEADSET_EVENT, event,
1775 /* Remove data from the connected list */
1776 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
1777 wait_list = _bt_get_audio_wait_data();
1779 if (wait_list == NULL) {
1781 g_free(profile_uuid);
1785 if (((wait_list->type == BT_AUDIO_ALL) &&
1786 (wait_list->ag_flag == TRUE)) ||
1787 (wait_list->type == BT_AUDIO_A2DP) ||
1788 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
1789 bluetooth_device_address_t device_address;
1790 _bt_convert_addr_string_to_type(
1791 device_address.addr,
1792 wait_list->address);
1794 _bt_audio_connect(wait_list->req_id,
1797 wait_list->out_param1);
1799 } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
1801 if (state == BT_PROFILE_STATE_CONNECTED) {
1802 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
1803 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1804 bluetooth_device_address_t device_address;
1806 param = g_variant_new("(is)", result, address);
1807 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1809 connected = _bt_is_headset_type_connected(
1813 if (g_strcmp0(connected_address,
1815 _bt_convert_addr_string_to_type(
1816 device_address.addr,
1818 _bt_audio_disconnect(0,
1820 &device_address, NULL);
1823 BT_DBG("device Path: %s", path);
1824 _bt_add_headset_to_list(BT_AVRCP,
1825 BT_STATE_CONNECTED, address);
1826 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1827 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
1828 param = g_variant_new("(is)", result, address);
1829 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1831 /* Remove data from the connected list */
1832 _bt_remove_headset_from_list(BT_AVRCP, address);
1834 } else if (strcasecmp(profile_uuid, A2DP_SOURCE_UUID) == 0) {
1835 if (state == BT_PROFILE_STATE_CONNECTED) {
1836 int event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED;
1837 BT_INFO("A2DP Source is connected");
1838 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
1839 g_variant_new("(is)", result, address));
1840 } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1841 int event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED;
1842 BT_INFO("A2DP Source Disconnected");
1843 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
1844 g_variant_new("(is)", result, address));
1846 } else if ((strcmp(profile_uuid, HID_UUID) == 0) &&
1847 ((state == BT_PROFILE_STATE_CONNECTED) ||
1848 (state == BT_PROFILE_STATE_DISCONNECTED))) {
1850 if (state == BT_PROFILE_STATE_CONNECTED)
1851 event = BLUETOOTH_HID_CONNECTED;
1853 event = BLUETOOTH_HID_DISCONNECTED;
1854 param = g_variant_new("(is)", result, address);
1855 _bt_send_event(BT_HID_EVENT, event,
1858 if (state == BT_PROFILE_STATE_CONNECTED)
1859 __bt_set_device_values(TRUE,
1860 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1862 __bt_set_device_values(FALSE,
1863 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1866 g_free(profile_uuid);
1867 } else if (strcasecmp(member, "AdvReport") == 0) {
1869 bt_remote_le_dev_info_t *le_dev_info = NULL;
1870 char *buffer = NULL;
1872 bt_le_adv_info_t *adv_info = NULL;
1873 GVariant *value = NULL;
1874 ret_if(_bt_is_le_scanning() == FALSE);
1876 le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
1877 if (le_dev_info == NULL)
1880 g_variant_get(msg, "(syyii@ay)", &le_dev_info->address,
1881 &le_dev_info->addr_type,
1882 &le_dev_info->adv_type,
1884 &le_dev_info->adv_data_len,
1886 buffer_len = g_variant_get_size(value);
1888 buffer = (char *)g_variant_get_data(value);
1890 le_dev_info->adv_data = g_memdup(buffer, buffer_len);
1891 if (le_dev_info->adv_data == NULL &&
1892 le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {
1893 _bt_free_le_device_info(le_dev_info);
1894 g_variant_unref(value);
1898 if (_bt_get_le_scan_type() == BT_LE_PASSIVE_SCAN) {
1899 _bt_send_scan_result_event(le_dev_info, NULL);
1900 _bt_free_le_device_info(le_dev_info);
1901 g_variant_unref(value);
1905 if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) { /* ADV_IND */
1906 adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
1907 if (adv_info == NULL) {
1908 _bt_free_le_device_info(le_dev_info);
1909 g_variant_unref(value);
1913 adv_info->addr = g_strdup(le_dev_info->address);
1914 adv_info->data_len = le_dev_info->adv_data_len;
1915 adv_info->data = g_malloc0(le_dev_info->adv_data_len);
1916 if (adv_info->data) {
1917 memcpy(adv_info->data, le_dev_info->adv_data,
1918 le_dev_info->adv_data_len);
1920 __bt_add_adv_ind_info(adv_info);
1923 } else { /* SCAN_RSP */
1924 adv_info = __bt_get_adv_ind_info(le_dev_info->address);
1926 _bt_send_scan_result_event(le_dev_info, adv_info);
1927 __bt_del_adv_ind_info(le_dev_info->address);
1930 _bt_free_le_device_info(le_dev_info);
1931 g_variant_unref(value);
1932 } else if (strcasecmp(member, "LEDataLengthChanged") == 0) {
1938 g_variant_get(msg, "(qqqq)",
1939 tx_octets, tx_time, rx_octets, rx_time);
1941 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1942 _bt_convert_device_path_to_address(path, address);
1944 param = g_variant_new("(isqqqq)", result, address, tx_octets, tx_time,
1945 rx_octets, rx_time);
1946 /* Send event to application */
1947 _bt_send_event(BT_DEVICE_EVENT, event, param);
1953 void __bt_set_audio_values(gboolean connected, char *address)
1956 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1958 /* Set the headset name */
1959 if (connected == TRUE) {
1960 name = _bt_get_bonded_device_name(address);
1962 name = g_strdup("");
1965 if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME,
1967 BT_ERR("vconf_set_str failed");
1972 /* Set the headset state */
1973 if (vconf_get_int(VCONFKEY_BT_DEVICE,
1974 &bt_device_state) != 0) {
1975 BT_ERR("vconf_get_str failed");
1978 #ifdef TIZEN_SUPPORT_DUAL_HF
1979 if ((connected == TRUE) &&
1980 (FALSE == __bt_is_companion_device(address))) {
1981 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1982 } else if ((bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) &&
1983 (FALSE == __bt_is_companion_device(address))) {
1984 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1987 if (connected == TRUE) {
1988 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1989 } else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) {
1990 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1994 if (vconf_set_int(VCONFKEY_BT_DEVICE,
1995 bt_device_state) != 0) {
1996 BT_ERR("vconf_set_int failed");
2000 void _bt_handle_headset_event(GVariant *msg, const char *path)
2002 int result = BLUETOOTH_ERROR_NONE;
2003 gboolean property_flag = FALSE;
2004 char *property = NULL;
2005 GVariant *value = NULL;
2006 GVariant *param = NULL;
2007 g_variant_get(msg, "(sv)", &property, &value);
2009 ret_if(property == NULL);
2011 BT_DBG("Property = %s \n", property);
2012 /* We allow only 1 headset connection (HSP or HFP)*/
2013 if (strcasecmp(property, "Connected") == 0) {
2014 int event = BLUETOOTH_EVENT_NONE;
2015 bt_headset_wait_t *wait_list;
2017 g_variant_get(value, "b", &property_flag);
2019 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2021 /* Fix : NULL_RETURNS */
2022 if (address == NULL)
2025 _bt_convert_device_path_to_address(path, address);
2027 if (property_flag == TRUE) {
2028 event = BLUETOOTH_EVENT_AG_CONNECTED;
2029 if (_bt_headset_get_local_connection() == FALSE)
2030 _bt_start_timer_for_connection(address, BT_AUDIO_A2DP);
2032 _bt_headset_set_local_connection(FALSE);
2036 event = BLUETOOTH_EVENT_AG_DISCONNECTED;
2038 previous_state = _bt_get_device_state_from_list(BT_AUDIO_HSP, address);
2039 if (previous_state == BT_STATE_DISCONNECTING)
2040 _bt_send_hf_local_term_event(address);
2042 /* Set the State machine here */
2043 __bt_connection_manager_set_state(address, event);
2044 __bt_set_audio_values(property_flag, address);
2045 param = g_variant_new("(is)", result, address);
2046 _bt_send_event(BT_HEADSET_EVENT, event,
2049 if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
2050 /* Remove data from the connected list */
2051 _bt_remove_headset_from_list(BT_AUDIO_HSP, address);
2053 wait_list = _bt_get_audio_wait_data();
2054 if (wait_list == NULL) {
2059 bluetooth_device_address_t device_address;
2061 _bt_set_audio_wait_data_flag(TRUE);
2063 _bt_convert_addr_string_to_type(device_address.addr,
2064 wait_list->address);
2065 _bt_audio_connect(wait_list->req_id, wait_list->type,
2066 &device_address, wait_list->out_param1);
2067 _bt_rel_wait_data();
2068 } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
2069 /* Add data to the connected list */
2070 _bt_add_headset_to_list(BT_AUDIO_HSP,
2071 BT_STATE_CONNECTED, address);
2073 wait_list = _bt_get_audio_wait_data();
2074 if (wait_list != NULL &&
2075 (g_strcmp0(wait_list->address, address) == 0))
2076 _bt_rel_wait_data();
2078 BT_INFO("Check A2DP pending connect");
2079 _bt_audio_check_pending_connect();
2082 } else if (strcasecmp(property, "State") == 0) {
2085 g_variant_get(value, "s", &state);
2087 /* This code assumes we support only 1 headset connection */
2088 /* Need to use the headset list, if we support multi-headsets */
2089 if (strcasecmp(state, "Playing") == 0) {
2090 BT_DBG("Playing: Sco Connected");
2091 } else if (strcasecmp(state, "connected") == 0 ||
2092 strcasecmp(state, "disconnected") == 0) {
2093 BT_DBG("connected/disconnected: Sco Disconnected");
2095 BT_ERR("Not handled state - %s", state);
2100 } else if (strcasecmp(property, "SpeakerGain") == 0) {
2104 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2106 _bt_convert_device_path_to_address(path, address);
2108 g_variant_get(value, "i", &spkr_gain);
2109 param = g_variant_new("(isq)", result, address, spkr_gain);
2110 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
2114 } else if (strcasecmp(property, "MicrophoneGain") == 0) {
2118 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2120 _bt_convert_device_path_to_address(path, address);
2122 g_variant_get(value, "i", &mic_gain);
2123 param = g_variant_new("(isq)", result, address, mic_gain);
2124 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_MIC_GAIN,
2131 g_variant_unref(value);
2134 void _bt_handle_sink_event(GVariant *msg, const char *path)
2136 GVariantIter value_iter;
2137 char *property = NULL;
2139 bt_headset_wait_t *wait_list;
2141 GVariant *child = NULL;
2142 GVariant *val = NULL;
2143 GVariant *param = NULL;
2144 g_variant_iter_init (&value_iter, msg);
2145 while ((child = g_variant_iter_next_value (&value_iter))) {
2147 g_variant_get(child, "{sv}", &property, &val);
2149 ret_if(property == NULL);
2151 BT_DBG("Property = %s \n", property);
2154 if (strcasecmp(property, "State") == 0) {
2155 int result = BLUETOOTH_ERROR_NONE;
2158 g_variant_get(val, "s", &value);
2159 BT_DBG("value: %s", value);
2161 if (g_strcmp0(value, "disconnected") == 0) {
2164 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2166 _bt_convert_device_path_to_address(path, address);
2168 __bt_set_device_values(FALSE,
2169 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2170 param = g_variant_new("(is)", result, address);
2171 _bt_send_event(BT_HEADSET_EVENT,
2172 BLUETOOTH_EVENT_AV_DISCONNECTED,
2175 /* Remove data from the connected list */
2176 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2177 wait_list = _bt_get_audio_wait_data();
2178 if (wait_list == NULL) {
2181 g_variant_unref(val);
2182 g_variant_unref(child);
2187 if (((wait_list->type == BT_AUDIO_ALL) &&
2188 (wait_list->ag_flag == TRUE)) ||
2189 (wait_list->type == BT_AUDIO_A2DP) ||
2190 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2191 bluetooth_device_address_t device_address;
2192 _bt_convert_addr_string_to_type(
2193 device_address.addr,
2194 wait_list->address);
2196 _bt_audio_connect(wait_list->req_id,
2199 wait_list->out_param1);
2202 }else if (strcasecmp(value, "Connected") == 0) {
2204 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2205 bluetooth_device_address_t device_address;
2208 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2210 _bt_convert_device_path_to_address(path, address);
2212 __bt_set_device_values(TRUE,
2213 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2214 param = g_variant_new("(is)", result, address);
2215 _bt_send_event(BT_HEADSET_EVENT,
2216 BLUETOOTH_EVENT_AV_CONNECTED,
2218 /* Check for existing Media device to disconnect */
2219 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
2222 /* Match connected device address */
2223 if (g_strcmp0(connected_address, address) != 0) {
2224 /* Convert BD adress from string type */
2225 _bt_convert_addr_string_to_type(
2226 device_address.addr,
2228 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2229 &device_address, NULL);
2233 /* Add data to the connected list */
2234 _bt_add_headset_to_list(BT_AUDIO_A2DP,
2235 BT_STATE_CONNECTED, address);
2242 g_variant_unref(val);
2243 g_variant_unref(child);
2247 void _bt_handle_agent_event(GVariant *msg, const char *member)
2249 int result = BLUETOOTH_ERROR_NONE;
2250 char *address = NULL;
2253 GVariant *param = NULL;
2254 ret_if(member == NULL);
2256 if (strcasecmp(member, "ObexAuthorize") == 0) {
2257 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2258 param = g_variant_new("(iss)", result, address, name);
2259 _bt_send_event(BT_OPP_SERVER_EVENT,
2260 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
2264 } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2265 bt_rfcomm_server_info_t *server_info;
2267 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2269 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2270 ret_if(server_info == NULL);
2271 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2272 param = g_variant_new("(isssn)", result, address, uuid, name,
2273 server_info->control_fd);
2274 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2275 BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2283 static int __bt_get_object_path(GVariant *msg, char **path)
2285 g_variant_get(msg, "(o*)", path, NULL);
2287 return BLUETOOTH_ERROR_INTERNAL;
2289 return BLUETOOTH_ERROR_NONE;
2292 static void __bt_devices_list_free(void)
2294 bt_cache_info_t *cache_info;
2297 node = g_list_first(p_cache_list);
2299 while (node != NULL){
2300 cache_info = (bt_cache_info_t *)node->data;
2301 p_cache_list = g_list_remove(p_cache_list, cache_info);
2302 __bt_free_cache_info(cache_info);
2304 node = g_list_next(node);
2308 static int __bt_parse_event(GVariant *msg)
2312 char *interface_name= NULL;
2313 GVariant *inner_iter = NULL;
2315 g_variant_iter_init(&iter, msg);
2317 while ((child = g_variant_iter_next_value(&iter))) {
2318 g_variant_get(child,"{&s@a{sv}}", &interface_name, &inner_iter);
2319 if (g_strcmp0(interface_name,
2320 BT_DEVICE_INTERFACE) == 0) {
2321 g_variant_unref(inner_iter);
2322 g_variant_unref(child);
2323 return BT_DEVICE_EVENT;
2324 } else if (g_strcmp0(interface_name,
2325 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2326 g_variant_unref(inner_iter);
2327 g_variant_unref(child);
2328 return BT_MEDIA_TRANSFER_EVENT;
2329 } else if (g_strcmp0(interface_name,
2330 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2331 g_variant_unref(inner_iter);
2332 g_variant_unref(child);
2333 return BT_AVRCP_CONTROL_EVENT;
2335 g_variant_unref(inner_iter);
2336 g_variant_unref(child);
2342 static void __bt_manager_event_filter(GDBusConnection *connection,
2343 const gchar *sender_name,
2344 const gchar *object_path,
2345 const gchar *interface_name,
2346 const gchar *signal_name,
2347 GVariant *parameters,
2350 bt_event_type_t bt_event = 0x00;
2351 int result = BLUETOOTH_ERROR_NONE;
2353 char *obj_path = NULL;
2354 GVariant *param = NULL;
2355 if (signal_name == NULL)
2357 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2358 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2360 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
2361 #ifdef USB_BLUETOOTH
2362 BT_DBG("Enable Adapter");
2363 _bt_enable_adapter();
2365 _bt_handle_adapter_added();
2369 bt_event = __bt_parse_event(value);
2370 if (bt_event == BT_DEVICE_EVENT) {
2371 bt_cache_info_t *cache_info;
2372 bt_remote_dev_info_t *dev_info;
2374 ret_if(_bt_is_discovering() == FALSE &&
2375 _bt_is_le_scanning() == FALSE);
2377 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2378 ret_if(cache_info == NULL);
2380 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2381 if (dev_info == NULL) {
2382 __bt_free_cache_info(cache_info);
2386 cache_info->dev_info = dev_info;
2388 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2389 BT_ERR("Fail to parse the properies");
2390 __bt_free_cache_info(cache_info);
2391 g_variant_unref(value);
2395 if (dev_info->addr_type != BDADDR_BREDR) {
2396 /* Whenever emit the property changed from bluez,
2397 some property doesn't reach to bt-service.
2398 So LE device is handled as AdvReport signal */
2399 __bt_free_cache_info(cache_info);
2400 g_variant_unref(value);
2404 if (dev_info->name == NULL)
2405 /* If Remote device name is NULL or still RNR is not done
2406 * then display address as name.
2408 dev_info->name = g_strdup(dev_info->address);
2410 GVariant *uuids = NULL;
2411 GVariantBuilder *builder = NULL;
2413 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2414 for (i=0; i < dev_info->uuid_count; i++) {
2415 g_variant_builder_add(builder, "s",
2416 dev_info->uuids[i]);
2418 uuids = g_variant_new("as", builder);
2419 g_variant_builder_unref(builder);
2420 GVariant *manufacturer_data = NULL;
2421 manufacturer_data = g_variant_new_from_data(
2422 G_VARIANT_TYPE_BYTESTRING,
2423 dev_info->manufacturer_data,
2424 dev_info->manufacturer_data_len,
2426 param = g_variant_new("(isunsbub@asn@ay)", result,
2432 dev_info->connected,
2435 dev_info->manufacturer_data_len,
2437 _bt_send_event(BT_ADAPTER_EVENT,
2438 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2440 p_cache_list = g_list_append(p_cache_list, cache_info);
2441 } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2442 BT_DBG("Device path : %s ", obj_path);
2443 _bt_set_control_device_path(obj_path);
2446 g_variant_unref(value);
2447 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2448 #ifdef USB_BLUETOOTH
2449 BT_DBG("InterfacesRemoved");
2450 _bt_handle_adapter_removed();
2452 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0) {
2453 bt_event = BT_MEDIA_TRANSFER_EVENT;
2454 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2455 bt_event = BT_DEVICE_EVENT;
2456 } else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
2457 bt_event = BT_AVRCP_CONTROL_EVENT;
2459 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2460 _bt_handle_adapter_event(parameters, signal_name);
2461 if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2462 BT_INFO("Object Path %s", obj_path);
2463 _bt_remove_control_device_path(obj_path);
2466 } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2469 char *previous = NULL;
2470 char *current = NULL;
2472 if (__bt_get_owner_info(parameters, &name, &previous, ¤t)) {
2473 BT_ERR("Fail to get the owner info");
2477 if (*current != '\0') {
2486 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2487 BT_DBG("Bluetoothd is terminated");
2488 if (_bt_adapter_get_status() == BT_ACTIVATED)
2491 _bt_handle_adapter_removed();
2492 __bt_devices_list_free();
2495 _bt_obex_server_check_allocation(&value);
2497 if (value == TRUE) {
2498 /* Check if the obex server was terminated abnormally */
2499 _bt_obex_server_check_termination(name);
2502 _bt_rfcomm_server_check_existence(&value);
2504 if (value == TRUE) {
2505 /* The obex server was terminated abnormally */
2506 _bt_rfcomm_server_check_termination(name);
2509 /* Stop advertising started by terminated process */
2510 _bt_stop_advertising_by_terminated_process(name);
2512 _bt_stop_le_scan(name);
2516 } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2517 const char *path = object_path;
2519 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2520 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2523 _bt_handle_property_changed_event(parameters, object_path);
2524 } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2525 _bt_handle_adapter_event(parameters, signal_name);
2526 } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2527 _bt_handle_input_event(parameters, object_path);
2528 } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2529 _bt_handle_network_server_event(parameters, signal_name);
2530 } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2531 _bt_handle_headset_event(parameters, object_path);
2532 } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2533 _bt_handle_sink_event(parameters, object_path);
2534 } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2535 _bt_handle_agent_event(parameters, signal_name);
2536 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2537 _bt_handle_device_event(parameters, signal_name, object_path);
2538 } else if (g_strcmp0(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
2539 _bt_handle_gatt_event(parameters, signal_name, object_path);
2545 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2548 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2549 char *interface_name = NULL;
2551 g_variant_get(msg,"(&s@a{sv}@as)", &interface_name, NULL, NULL);
2552 retv_if(interface_name == NULL, FALSE);
2554 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2555 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2563 static void __bt_obexd_event_filter(GDBusConnection *connection,
2564 const gchar *sender_name,
2565 const gchar *object_path,
2566 const gchar *interface_name,
2567 const gchar *signal_name,
2568 GVariant *parameters,
2571 const char *member = signal_name;
2572 char *obj_path = NULL;
2573 ret_if(member == NULL);
2575 if (strcasecmp(member, "InterfacesAdded") == 0) {
2576 if (__bt_get_object_path(parameters, &obj_path)) {
2577 BT_ERR("Fail to get the path");
2580 BT_INFO("object_path = [%s]", object_path);
2582 /*Handle OPP_SERVER_CONNECTED_EVENT here */
2583 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2584 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2589 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2590 BT_DBG("Obex_Server_Session_Transfer connected");
2591 _bt_obex_transfer_connected();
2594 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2595 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
2596 if (__bt_get_object_path(parameters, &obj_path)) {
2597 BT_ERR("Fail to get the path");
2600 BT_INFO("object_path = [%s]", object_path);
2602 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2603 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2604 BT_DBG("Call PBAP Disconnected");
2605 _bt_obex_pbap_client_disconnect(obj_path);
2608 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2609 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2614 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2615 BT_DBG("Obex_Server_Session_Transfer disconnected");
2616 _bt_obex_transfer_disconnected();
2619 } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
2620 const char *path = object_path;
2622 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
2623 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
2624 strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2625 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2626 BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
2630 _bt_handle_property_changed_event(parameters, path);
2636 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
2640 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2641 char *interface_name = NULL;
2643 g_variant_get(msg,"(&s@a{sv}@as)", &interface_name, NULL, NULL);
2645 retv_if(interface_name == NULL, FALSE);
2647 if (strcasecmp(interface_name,
2648 BT_OBEX_TRANSFER_INTERFACE) == 0) {
2659 static void __bt_opc_event_filter(GDBusConnection *connection,
2660 const gchar *sender_name,
2661 const gchar *object_path,
2662 const gchar *interface_name,
2663 const gchar *signal_name,
2664 GVariant *parameters,
2667 const char *member = signal_name;
2668 char *obj_path = NULL;
2669 if (strcasecmp(member, "InterfacesAdded") == 0) {
2670 BT_DBG("InterfacesAdded");
2671 }else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2673 if (__bt_get_object_path(parameters, &obj_path)) {
2674 BT_ERR("Fail to get the path");
2678 BT_DBG("object_path =%s",obj_path);
2680 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2681 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
2682 || strstr(obj_path, "transfer") == NULL) {
2686 else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2687 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2688 BT_DBG("Going to call opc disconnected");
2689 _bt_opc_disconnected(obj_path);
2692 _bt_sending_files();
2694 }else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE){
2695 char *path = (char *)object_path;
2696 BT_INFO("object_path %s", path);
2697 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2698 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2699 BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
2703 _bt_opc_property_changed_event(parameters, path);
2709 int _bt_opp_client_event_init(void)
2711 GError *error = NULL;
2713 if (opc_obexd_conn == NULL) {
2714 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
2716 if (!opc_obexd_conn) {
2718 BT_ERR("Unable to connect to dbus: %s", error->message);
2719 g_clear_error(&error);
2721 return BLUETOOTH_ERROR_INTERNAL;
2725 if (_bt_register_service_event(opc_obexd_conn,
2726 BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
2727 g_object_unref(opc_obexd_conn);
2728 opc_obexd_conn = NULL;
2729 return BLUETOOTH_ERROR_INTERNAL;
2732 return BLUETOOTH_ERROR_NONE;
2735 void _bt_opp_client_event_deinit(void)
2737 if (opc_obexd_conn) {
2738 _bt_unregister_service_event(opc_obexd_conn,
2739 BT_OPP_CLIENT_EVENT);
2740 g_object_unref(opc_obexd_conn);
2741 opc_obexd_conn = NULL;
2745 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
2751 static int subs_interface_added_id = -1;
2752 static int subs_interface_removed_id = -1;
2753 static int subs_name_owner_id = -1;
2754 static int subs_property_id = -1;
2755 static int subs_adapter_id = -1;
2756 static int subs_gatt_id = -1;
2759 if (subs_interface_added_id == -1) {
2760 subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
2761 NULL, BT_MANAGER_INTERFACE,
2762 BT_INTERFACES_ADDED, NULL, NULL, 0,
2763 __bt_manager_event_filter,
2766 if (subs_interface_removed_id == -1) {
2767 subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
2768 NULL, BT_MANAGER_INTERFACE,
2769 BT_INTERFACES_REMOVED, NULL, NULL, 0,
2770 __bt_manager_event_filter,
2773 if (subs_name_owner_id == -1) {
2774 subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
2775 NULL, BT_FREEDESKTOP_INTERFACE,
2776 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
2777 __bt_manager_event_filter,
2780 if (subs_property_id == -1) {
2781 subs_property_id = g_dbus_connection_signal_subscribe(conn,
2782 NULL, BT_PROPERTIES_INTERFACE,
2783 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
2784 __bt_manager_event_filter,
2787 if (subs_adapter_id == -1) {
2788 subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
2789 NULL, BT_ADAPTER_INTERFACE,
2790 NULL, NULL, NULL, 0,
2791 __bt_manager_event_filter,
2794 if (subs_gatt_id == -1) {
2795 subs_gatt_id = g_dbus_connection_signal_subscribe(conn,
2796 NULL, BT_GATT_CHAR_INTERFACE,
2797 NULL, NULL, NULL, 0,
2798 __bt_manager_event_filter,
2802 if (subs_interface_added_id != -1) {
2803 g_dbus_connection_signal_unsubscribe(conn,
2804 subs_interface_added_id);
2805 subs_interface_added_id = -1;
2807 if (subs_interface_removed_id != -1) {
2808 g_dbus_connection_signal_unsubscribe(conn,
2809 subs_interface_removed_id);
2810 subs_interface_removed_id = -1;
2812 if (subs_name_owner_id != -1) {
2813 g_dbus_connection_signal_unsubscribe(conn,
2814 subs_name_owner_id);
2815 subs_name_owner_id = -1;
2817 if (subs_property_id != -1) {
2818 g_dbus_connection_signal_unsubscribe(conn,
2820 subs_property_id = -1;
2822 if (subs_adapter_id != -1) {
2823 g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
2824 subs_adapter_id = -1;
2826 if (subs_gatt_id != -1) {
2827 g_dbus_connection_signal_unsubscribe(conn, subs_gatt_id);
2834 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
2840 static int subs_device_id = -1;
2843 if (subs_device_id == -1) {
2844 subs_device_id = g_dbus_connection_signal_subscribe(conn,
2845 NULL, BT_DEVICE_INTERFACE,
2846 NULL, NULL, NULL, 0,
2847 __bt_manager_event_filter,
2851 if (subs_device_id != -1) {
2852 g_dbus_connection_signal_unsubscribe(conn,
2854 subs_device_id = -1;
2860 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
2866 static int subs_input_id = -1;
2869 if (subs_input_id == -1) {
2870 subs_input_id = g_dbus_connection_signal_subscribe(conn,
2871 NULL, BT_INPUT_INTERFACE,
2872 NULL, NULL, NULL, 0,
2873 __bt_manager_event_filter,
2877 if (subs_input_id != -1) {
2878 g_dbus_connection_signal_unsubscribe(conn,
2886 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
2892 static int subs_serv_id = -1;
2893 static int subs_client_id = -1;
2896 if (subs_serv_id == -1) {
2897 subs_serv_id = g_dbus_connection_signal_subscribe(conn,
2898 NULL, BT_NETWORK_SERVER_INTERFACE,
2899 NULL, NULL, NULL, 0,
2900 __bt_manager_event_filter,
2903 if (subs_client_id == -1) {
2904 subs_client_id = g_dbus_connection_signal_subscribe(conn,
2905 NULL, BT_NETWORK_CLIENT_INTERFACE,
2906 NULL, NULL, NULL, 0,
2907 __bt_manager_event_filter,
2911 if (subs_serv_id != -1) {
2912 g_dbus_connection_signal_unsubscribe(conn,
2916 if (subs_client_id != -1) {
2917 g_dbus_connection_signal_unsubscribe(conn,
2919 subs_client_id = -1;
2925 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
2931 static int subs_headset_id = -1;
2932 static int subs_sink_id = -1;
2935 if (subs_headset_id == -1) {
2936 subs_headset_id = g_dbus_connection_signal_subscribe(conn,
2937 NULL, BT_HEADSET_INTERFACE,
2938 NULL, NULL, NULL, 0,
2939 __bt_manager_event_filter,
2942 if (subs_sink_id == -1) {
2943 subs_sink_id = g_dbus_connection_signal_subscribe(conn,
2944 NULL, BT_SINK_INTERFACE,
2945 NULL, NULL, NULL, 0,
2946 __bt_manager_event_filter,
2950 if (subs_headset_id != -1) {
2951 g_dbus_connection_signal_unsubscribe(conn,
2953 subs_headset_id = -1;
2955 if (subs_sink_id != -1) {
2956 g_dbus_connection_signal_unsubscribe(conn,
2964 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
2970 static int subs_opp_server_interface_added_id = -1;
2971 static int subs_opp_server_interface_removed_id = -1;
2972 static int subs_opp_server_property_id = -1;
2976 if (subs_opp_server_interface_added_id == -1) {
2977 subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
2978 NULL, BT_MANAGER_INTERFACE,
2979 BT_INTERFACES_ADDED, NULL, NULL, 0,
2980 __bt_obexd_event_filter,
2983 if (subs_opp_server_interface_removed_id == -1) {
2984 subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
2985 NULL, BT_MANAGER_INTERFACE,
2986 BT_INTERFACES_REMOVED, NULL, NULL, 0,
2987 __bt_obexd_event_filter,
2990 if (subs_opp_server_property_id == -1) {
2991 subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
2992 NULL, BT_PROPERTIES_INTERFACE,
2993 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
2994 __bt_obexd_event_filter,
2998 if (subs_opp_server_interface_added_id != -1) {
2999 g_dbus_connection_signal_unsubscribe(conn,
3000 subs_opp_server_interface_added_id);
3001 subs_opp_server_interface_added_id = -1;
3003 if (subs_opp_server_interface_removed_id != -1) {
3004 g_dbus_connection_signal_unsubscribe(conn,
3005 subs_opp_server_interface_removed_id);
3006 subs_opp_server_interface_removed_id = -1;
3008 if (subs_opp_server_property_id != -1) {
3009 g_dbus_connection_signal_unsubscribe(conn,
3010 subs_opp_server_property_id);
3011 subs_opp_server_property_id = -1;
3017 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
3023 static int subs_opp_client_interface_added_id = -1;
3024 static int subs_opp_client_interface_removed_id = -1;
3025 static int subs_opp_client_property_id = -1;
3029 if (subs_opp_client_interface_added_id == -1) {
3030 subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3031 NULL, BT_MANAGER_INTERFACE,
3032 BT_INTERFACES_ADDED, NULL, NULL, 0,
3033 __bt_opc_event_filter,
3036 if (subs_opp_client_interface_removed_id == -1) {
3037 subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3038 NULL, BT_MANAGER_INTERFACE,
3039 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3040 __bt_opc_event_filter,
3043 if (subs_opp_client_property_id == -1) {
3044 subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
3045 NULL, BT_PROPERTIES_INTERFACE,
3046 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3047 __bt_opc_event_filter,
3051 if (subs_opp_client_interface_added_id != -1) {
3052 g_dbus_connection_signal_unsubscribe(conn,
3053 subs_opp_client_interface_added_id);
3054 subs_opp_client_interface_added_id = -1;
3056 if (subs_opp_client_interface_removed_id != -1) {
3057 g_dbus_connection_signal_unsubscribe(conn,
3058 subs_opp_client_interface_removed_id);
3059 subs_opp_client_interface_removed_id = -1;
3061 if (subs_opp_client_property_id != -1) {
3062 g_dbus_connection_signal_unsubscribe(conn,
3063 subs_opp_client_property_id);
3064 subs_opp_client_property_id = -1;
3070 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
3076 static int subs_a2dp_source_id = -1;
3077 static int subs_a2dp_sink_id = -1;
3080 if (subs_a2dp_source_id == -1) {
3081 subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
3082 NULL, BT_A2DP_SOURCE_INTERFACE,
3083 NULL, NULL, NULL, 0,
3084 __bt_opc_event_filter,
3087 if (subs_a2dp_sink_id == -1) {
3088 subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
3089 NULL, BT_SINK_INTERFACE,
3090 NULL, NULL, NULL, 0,
3091 __bt_opc_event_filter,
3095 if (subs_a2dp_source_id != -1) {
3096 g_dbus_connection_signal_unsubscribe(conn,
3097 subs_a2dp_source_id);
3098 subs_a2dp_source_id = -1;
3100 if (subs_a2dp_sink_id != -1) {
3101 g_dbus_connection_signal_unsubscribe(conn,
3103 subs_a2dp_sink_id = -1;
3109 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
3113 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3115 switch (event_type) {
3116 case BT_MANAGER_EVENT:
3117 _bt_register_manager_subscribe_signal(g_conn, TRUE);
3119 case BT_DEVICE_EVENT:
3120 _bt_register_device_subscribe_signal(g_conn, TRUE);
3123 _bt_register_input_subscribe_signal(g_conn, TRUE);
3125 case BT_NETWORK_EVENT:
3126 _bt_register_network_subscribe_signal(g_conn, TRUE);
3128 case BT_HEADSET_EVENT:
3129 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3132 case BT_OPP_SERVER_EVENT:
3133 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3134 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3136 case BT_OPP_CLIENT_EVENT:
3137 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3138 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3140 case BT_A2DP_SOURCE_EVENT:
3141 BT_INFO("A2dp Source event");
3142 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3145 BT_ERR("Unknown event");
3146 return BLUETOOTH_ERROR_INTERNAL;
3149 return BLUETOOTH_ERROR_NONE;
3152 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3156 ret_if(g_conn == NULL);
3158 switch (event_type) {
3159 case BT_MANAGER_EVENT:
3160 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3161 _bt_register_device_subscribe_signal(g_conn, FALSE);
3162 _bt_register_input_subscribe_signal(g_conn, FALSE);
3163 _bt_register_network_subscribe_signal(g_conn, FALSE);
3164 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3166 case BT_OPP_SERVER_EVENT:
3167 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3169 case BT_OPP_CLIENT_EVENT:
3170 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3173 BT_ERR("Unknown event");
3180 static int __bt_init_manager_receiver(void)
3184 GError *error = NULL;
3186 if (manager_conn == NULL) {
3187 manager_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3188 if (error != NULL) {
3189 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3190 g_clear_error(&error);
3192 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3195 if (_bt_register_service_event(manager_conn,
3196 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3198 if (_bt_register_service_event(manager_conn,
3199 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3202 if (_bt_register_service_event(manager_conn,
3203 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3206 if (_bt_register_service_event(manager_conn,
3207 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3210 if (_bt_register_service_event(manager_conn,
3211 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3213 return BLUETOOTH_ERROR_NONE;
3216 g_object_unref(manager_conn);
3217 manager_conn = NULL;
3222 return BLUETOOTH_ERROR_INTERNAL;
3225 static int __bt_init_obexd_receiver(void)
3228 #ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3229 GError *error = NULL;
3231 if (obexd_conn == NULL) {
3232 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3233 if (error != NULL) {
3234 BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3235 g_clear_error(&error);
3237 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3240 if (_bt_register_service_event(obexd_conn,
3241 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3242 BT_ERR("Error while registering service event");
3243 g_object_unref(obexd_conn);
3245 return BLUETOOTH_ERROR_INTERNAL;
3250 return BLUETOOTH_ERROR_NONE;
3253 /* To receive the event from bluez */
3254 int _bt_init_service_event_receiver(void)
3260 result = __bt_init_manager_receiver();
3261 retv_if(result != BLUETOOTH_ERROR_NONE, result);
3263 result = __bt_init_obexd_receiver();
3264 if (result != BLUETOOTH_ERROR_NONE)
3265 BT_ERR("Fail to init obexd receiver");
3269 return BLUETOOTH_ERROR_NONE;
3272 void _bt_deinit_service_event_receiver(void)
3276 _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3278 _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3281 g_object_unref(manager_conn);
3282 manager_conn = NULL;
3286 g_object_unref(obexd_conn);
3291 g_source_remove(event_id);