Fix : Some properties of adapter couldn't be handled
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-event-receiver.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <glib.h>
19 #include <string.h>
20 #include <dlog.h>
21 #include <vconf.h>
22 #include <vconf-internal-bt-keys.h>
23
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
26
27 #include "bt-service-common.h"
28 #include "bt-service-event.h"
29 #include "bt-service-main.h"
30 #include "bt-service-adapter.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-device.h"
33 #include "bt-service-avrcp.h"
34 #include "bt-service-obex-server.h"
35 #include "bt-service-rfcomm-server.h"
36 #include "bt-service-audio.h"
37 #include "bt-service-agent.h"
38 #include "bt-service-pbap.h"
39 #include "bt-service-headset-connection.h"
40 #include "bt-service-avrcp-controller.h"
41 #include "bt-service-hid.h"
42 #include "bt-service-proximity.h"
43
44 #include "bt-service-opp-client.h"
45
46 #ifdef TIZEN_DPM_ENABLE
47 #include "bt-service-dpm.h"
48 #endif
49
50 #define DBUS_TIMEOUT 20 * 1000 /* 20 Sec */
51 static GDBusConnection *manager_conn;
52 static GDBusConnection *obexd_conn;
53 static GDBusConnection *opc_obexd_conn;
54
55 static GList *p_cache_list = NULL;
56
57 static guint event_id;
58 static guint session_reinit_timer;
59 guint nap_connected_device_count = 0;
60 static guint hid_connected_device_count = 0;
61 static GList *p_adv_ind_list = NULL;
62
63 typedef struct {
64         bt_remote_dev_info_t *dev_info;
65 } bt_cache_info_t;
66
67 /**
68  * obexd connection type
69  */
70 typedef enum {
71         OBEX_OPP = (1 << 1),
72         OBEX_FTP = (1 << 2),
73         OBEX_BIP = (1 << 3),
74         OBEX_PBAP = (1 << 4),
75         OBEX_IRMC = (1 << 5),
76         OBEX_PCSUITE = (1 << 6),
77         OBEX_SYNCEVOLUTION = (1 << 7),
78         OBEX_MAS = (1 << 8),
79 } bluetooth_obex_connection_type_t;
80
81 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path);
82 void _bt_opc_property_changed_event(GVariant *msg, char *path);
83 int _bt_register_service_event(GDBusConnection *g_conn, int event_type);
84 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type);
85 void _bt_opp_client_event_deinit(void);
86 void _bt_handle_network_client_event(GVariant *msg_iter,
87                                 const char *path);
88 void __bt_gatt_char_property_changed_event(GVariant *msg_iter,
89                                 const char *path);
90
91 static void __bt_free_bt_le_adv_info_t(gpointer data)
92 {
93         bt_le_adv_info_t *adv_info = data;
94
95         if (adv_info->timer_id)
96                 g_source_remove(adv_info->timer_id);
97         adv_info->timer_id = 0;
98
99         g_free(adv_info->addr);
100         g_free(adv_info->data);
101         g_free(adv_info);
102 }
103
104 static bt_le_adv_info_t *__bt_get_adv_ind_info(char *addr)
105 {
106         retv_if(!addr, NULL);
107         bt_le_adv_info_t *adv_info = NULL;
108         GList *current = g_list_first((GList *)p_adv_ind_list);
109         while (current && current->data) {
110                 adv_info = (bt_le_adv_info_t *)current->data;
111                 if (adv_info && !g_strcmp0(adv_info->addr, addr))
112                         return adv_info;
113                 current = g_list_next(current);
114         }
115         return NULL;
116 }
117
118 static int __bt_add_adv_ind_info(bt_le_adv_info_t *adv_info)
119 {
120         retv_if(!adv_info, -1);
121         if (__bt_get_adv_ind_info(adv_info->addr) != NULL) {
122                 __bt_free_bt_le_adv_info_t(adv_info);
123                 return -1;
124         }
125         p_adv_ind_list = g_list_append(p_adv_ind_list, adv_info);
126
127         return 0;
128 }
129
130 static void __bt_del_adv_ind_info(char *addr)
131 {
132         ret_if(!addr);
133         ret_if(!p_adv_ind_list);
134         bt_le_adv_info_t *adv_info = NULL;
135         GList *current = g_list_first((GList *)p_adv_ind_list);
136         while (current && current->data) {
137                 adv_info = (bt_le_adv_info_t *)current->data;
138                 if (adv_info && !g_strcmp0(adv_info->addr, addr)) {
139                         p_adv_ind_list = g_list_remove(p_adv_ind_list, adv_info);
140                         __bt_free_bt_le_adv_info_t(adv_info);
141                         return;
142                 }
143                 current = g_list_next(current);
144         }
145 }
146
147 static gboolean __bt_adv_scan_req_timeout_cb(gpointer user_data)
148 {
149         bt_remote_le_dev_info_t le_dev_info;
150         bt_le_adv_info_t *adv_info = (bt_le_adv_info_t*)user_data;
151
152         memset(&le_dev_info, 0x00, sizeof(bt_remote_le_dev_info_t));
153
154         le_dev_info.address = adv_info->addr;
155         le_dev_info.addr_type = adv_info->addr_type;
156         le_dev_info.rssi = adv_info->rssi;
157         le_dev_info.adv_data = adv_info->data;
158         le_dev_info.adv_data_len = 0;
159         adv_info->timer_id = 0;
160
161         _bt_send_scan_result_event(&le_dev_info, adv_info);
162         __bt_del_adv_ind_info(adv_info->addr);
163
164         return FALSE;
165 }
166
167 static void __bt_free_cache_info(bt_cache_info_t *cache_info)
168 {
169         ret_if(cache_info == NULL);
170         _bt_free_device_info(cache_info->dev_info);
171         g_free(cache_info);
172 }
173
174 static gboolean __bt_parse_device_properties(GVariant *item,
175                                                 bt_remote_dev_info_t *dev_info)
176 {
177         GVariantIter iter;
178         gchar *key;
179         GVariant *val;
180         gsize len = 0;
181         if (item == NULL)
182                 return FALSE;
183
184         g_variant_iter_init(&iter, item);
185         while (g_variant_iter_loop(&iter, "{sv}", &key, &val)) {
186                 if (strcasecmp(key, "Address") == 0)  {
187                         dev_info->address = g_variant_dup_string(val, &len);
188                 } else if (strcasecmp(key, "Class") == 0) {
189                         dev_info->class = g_variant_get_uint32(val);
190                 } else if (strcasecmp(key, "name") == 0) {
191                         if (dev_info->name == NULL)
192                                 dev_info->name = g_variant_dup_string(val, &len);
193                 } else if (strcasecmp(key, "Connected") == 0) {
194                         dev_info->connected = g_variant_get_byte(val);
195                 } else if (strcasecmp(key, "paired") == 0) {
196                         dev_info->paired = g_variant_get_boolean(val);
197                 } else if (strcasecmp(key, "Trusted") == 0) {
198                         dev_info->trust = g_variant_get_boolean(val);
199                 } else if (strcasecmp(key, "RSSI") == 0) {
200                         dev_info->rssi = g_variant_get_int16(val);
201                 } else if (strcasecmp(key, "LastAddrType") == 0) {
202                         dev_info->addr_type = g_variant_get_byte(val);
203                 } else if (strcasecmp(key, "UUIDs") == 0) {
204                         char **uuid_value;
205                         gsize size = 0;
206                         int i = 0;
207                         size = g_variant_get_size(val);
208
209                         if (size > 0) {
210                                 uuid_value = (char **)g_variant_get_strv(val, &size);
211                                 if (dev_info->uuids == NULL)
212                                         dev_info->uuids = g_malloc0(sizeof(char *) * size);
213
214                                 for (i = 0; uuid_value[i] != NULL; i++) {
215                                         dev_info->uuid_count++;
216                                         dev_info->uuids[i] = g_strdup(uuid_value[i]);
217                                 }
218                                 g_free(uuid_value);
219                         }
220                 } else if (strcasecmp(key, "LegacyManufacturerDataLen") == 0) {
221                         g_variant_get(val, "q", &dev_info->manufacturer_data_len);
222                         if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
223                                 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
224                                 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
225                         }
226
227                         if (dev_info->manufacturer_data_len == 0)
228                                 dev_info->manufacturer_data = g_strdup("");
229                 } else if (strcasecmp(key, "LegacyManufacturerData") == 0) {
230                         int i = 0;
231                         int len = 0;
232                         GVariantIter *value_iter;
233                         guint8 m_value;
234
235                         len = g_variant_get_size(val);
236                         if (len <= 0)
237                                 continue;
238
239                         dev_info->manufacturer_data = g_malloc0(len);
240
241                         g_variant_get(val, "ay", &value_iter);
242                         while (g_variant_iter_loop(value_iter, "y", &m_value))
243                                 dev_info->manufacturer_data[i++] = m_value;
244                         g_variant_iter_free(value_iter);
245                 }
246         }
247
248         BT_DBG("-");
249         return TRUE;
250 }
251
252 static gboolean __bt_parse_interface(GVariant *msg,
253                                         bt_remote_dev_info_t *dev_info)
254 {
255         char *path = NULL;
256         GVariant *optional_param = NULL;
257         GVariantIter iter;
258         GVariant *child;
259         char *interface_name = NULL;
260         GVariant *inner_iter = NULL;
261         g_variant_get(msg, "(&o@a{sa{sv}})",
262                                         &path, &optional_param);
263         g_variant_iter_init(&iter, optional_param);
264
265         retv_if(optional_param == NULL, FALSE);
266
267         while ((child = g_variant_iter_next_value(&iter))) {
268                 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
269                 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
270                         BT_DBG("Found a device: %s", path);
271                         if (__bt_parse_device_properties(inner_iter,
272                                 dev_info) == FALSE) {
273                                 g_variant_unref(inner_iter);
274                                 g_variant_unref(child);
275                                 g_variant_unref(optional_param);
276                                 BT_ERR("Fail to parse the properies");
277                                 return FALSE;
278                         } else {
279                                 g_variant_unref(inner_iter);
280                                 g_variant_unref(child);
281                                 g_variant_unref(optional_param);
282                                 return TRUE;
283                         }
284                 }
285                 g_variant_unref(inner_iter);
286                 g_variant_unref(child);
287         }
288
289         g_variant_unref(optional_param);
290
291         return FALSE;
292 }
293
294 static int __bt_get_owner_info(GVariant *msg, char **name,
295                                 char **previous, char **current)
296 {
297         g_variant_get(msg, "(sss)", name, previous, current);
298         return BLUETOOTH_ERROR_NONE;
299 }
300
301 static int __bt_get_agent_signal_info(GVariant *msg, char **address,
302                                 char **name, char **uuid)
303 {
304         g_variant_get(msg, "(sss)", address, name, uuid);
305         return BLUETOOTH_ERROR_NONE;
306 }
307
308 void __bt_set_device_values(gboolean connected, int state)
309 {
310         int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
311
312         if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
313                 BT_ERR("vconf_get_int failed");
314
315         if (connected == TRUE)
316                 bt_device_state |= state;
317         else if (bt_device_state & state)
318                 bt_device_state ^= state;
319
320         if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0)
321                 BT_ERR("vconf_set_int failed");
322 }
323
324 gboolean _bt_discovery_finished_cb(gpointer user_data)
325 {
326         int result = BLUETOOTH_ERROR_NONE;
327         event_id = 0;
328         GVariant *param = NULL;
329         if (_bt_get_discovering_property(DISCOVERY_ROLE_BREDR) == FALSE) {
330                 if (_bt_get_cancel_by_user() == TRUE)
331                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
332
333                 _bt_set_cancel_by_user(FALSE);
334                 _bt_set_discovery_status(FALSE);
335                 param = g_variant_new("(i)", result);
336                 _bt_send_event(BT_ADAPTER_EVENT,
337                         BLUETOOTH_EVENT_DISCOVERY_FINISHED,
338                         param);
339         }
340
341         return FALSE;
342 }
343
344 static gboolean __bt_le_discovery_finished_cb(gpointer user_data)
345 {
346         int result = BLUETOOTH_ERROR_NONE;
347         event_id = 0;
348         GVariant *param = NULL;
349         if (_bt_get_discovering_property(DISCOVERY_ROLE_LE) == FALSE) {
350                 if (_bt_get_cancel_by_user() == TRUE)
351                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
352
353                 g_list_free_full(p_adv_ind_list, __bt_free_bt_le_adv_info_t);
354                 p_adv_ind_list = NULL;
355
356                 _bt_set_cancel_by_user(FALSE);
357                 _bt_disable_all_scanner_status();
358                 _bt_set_le_scan_status(FALSE);
359                 param = g_variant_new("(i)", result);
360                 _bt_send_event(BT_LE_ADAPTER_EVENT,
361                         BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED,
362                         param);
363         }
364
365         return FALSE;
366 }
367
368 void __bt_update_remote_cache_devinfo(const char *address, gboolean paired_status)
369 {
370         BT_DBG("+");
371
372         ret_if(address == NULL);
373
374         GList * node;
375         bt_cache_info_t *cache_info;
376         bt_remote_dev_info_t *dev_info;
377
378         node = g_list_first(p_cache_list);
379
380         while (node != NULL) {
381                 cache_info = (bt_cache_info_t *)node->data;
382
383                 if (cache_info == NULL) {
384                         node = g_list_next(node);
385                         continue;
386                 }
387
388                 dev_info = cache_info->dev_info;
389                 if (strcasecmp(dev_info->address,
390                                         address) == 0) {
391                         BT_DBG("Device Found");
392                         if (paired_status == TRUE)
393                                 cache_info->dev_info->paired = TRUE;
394                         else
395                                 cache_info->dev_info->paired = FALSE;
396                         break;
397                 }
398                 node = g_list_next(node);
399         }
400         BT_DBG("-");
401 }
402
403 static void __bt_device_remote_connected_properties(
404                                 bt_remote_dev_info_t *remote_dev_info,
405                                 char *address, gboolean connected)
406 {
407         int result = BLUETOOTH_ERROR_NONE;
408         int i;
409         GVariant *param = NULL;
410         BT_DBG("+");
411
412         if (remote_dev_info->uuid_count > 0) {
413                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
414                         char *uuid = remote_dev_info->uuids[i];
415                         if (strcasecmp(uuid, HID_UUID) == 0) {
416                                 int event = BLUETOOTH_EVENT_NONE;
417
418                                 event = (connected == TRUE) ?
419                                         BLUETOOTH_HID_CONNECTED :
420                                         BLUETOOTH_HID_DISCONNECTED;
421                                 param = g_variant_new("(is)", result,
422                                                         address);
423                                 _bt_send_event(BT_HID_EVENT, event,
424                                         param);
425                                 break;
426                         }
427                 }
428         }
429
430         BT_DBG("-");
431 }
432
433 gboolean __bt_handle_is_flight_mode_enabled(void)
434 {
435 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
436         int is_flight_mode = 0;
437         int ret = -1;
438
439         ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &is_flight_mode);
440         if (ret != 0)
441                 BT_ERR("vconf_get_bool failed");
442
443         return (is_flight_mode == 0) ? FALSE : TRUE;
444 #else
445         return FALSE;
446 #endif
447 }
448
449 void _bt_handle_adapter_event(GVariant *msg, const char *member)
450 {
451         BT_DBG("+");
452
453         int result = BLUETOOTH_ERROR_NONE;
454         GVariant *param = NULL;
455         ret_if(member == NULL);
456
457         if (strcasecmp(member, "DeviceCreated") == 0) {
458                 char *object_path = NULL;
459                 char *address;
460                 bt_remote_dev_info_t *remote_dev_info;
461
462                 ret_if(_bt_is_device_creating() == FALSE);
463
464                 /* Bonding from remote device */
465                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
466
467                 g_variant_get(msg, "(&o)", &object_path);
468                 _bt_convert_device_path_to_address((const char*)object_path, address);
469
470                 remote_dev_info = _bt_get_remote_device_info_by_object_path(object_path);
471                 if (remote_dev_info == NULL) {
472                         g_free(address);
473                         return;
474                 }
475
476                 _bt_free_device_info(remote_dev_info);
477                 g_free(address);
478         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
479                 char *object_path = NULL;
480                 char *address;
481                 bt_cache_info_t *cache_info;
482                 bt_remote_dev_info_t *dev_info;
483                 GList * node;
484
485                 /* Bonding from remote device */
486                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
487
488                 g_variant_get(msg, "(&o)", &object_path);
489
490                 _bt_convert_device_path_to_address((const char *)object_path, address);
491
492                 node = g_list_first(p_cache_list);
493
494                 while (node != NULL) {
495                         cache_info = (bt_cache_info_t *)node->data;
496
497                         if (cache_info == NULL) {
498                                 node = g_list_next(node);
499                                 continue;
500                         }
501
502                         dev_info = cache_info->dev_info;
503                         if (strcasecmp(dev_info->address,
504                                                 address) == 0) {
505                                 p_cache_list = g_list_remove(p_cache_list,
506                                                 cache_info);
507                                 __bt_free_cache_info(cache_info);
508                                 break;
509                         }
510                         node = g_list_next(node);
511                 }
512                 g_free(address);
513         } else if (strcasecmp(member, "AdvertisingEnabled") == 0) {
514                 BT_DBG("Advertising Enabled");
515                 int slot_id;
516                 int event;
517                 int adv_handle;
518                 gboolean status = FALSE;
519
520                 g_variant_get(msg, "(ib)", &slot_id, &status);
521
522                 BT_DBG("Advertising Enabled : slot_id [%d]  status [%d]", slot_id, status);
523
524                 /* Send event to application */
525                 _bt_set_advertising_status(slot_id, status);
526
527                 adv_handle = _bt_get_adv_slot_adv_handle(slot_id);
528
529                 if (status)
530                         event = BLUETOOTH_EVENT_ADVERTISING_STARTED;
531                 else
532                         event = BLUETOOTH_EVENT_ADVERTISING_STOPPED;
533                 param = g_variant_new("(ii)", result,
534                                         adv_handle);
535
536 #if 0
537                 const char *sender;
538                 sender = _bt_get_adv_slot_owner(slot_id);
539                 _bt_send_event_to_dest(sender, BT_ADAPTER_EVENT,
540                                 event,
541                                 param);
542 #else
543                 _bt_send_event(BT_ADAPTER_EVENT, event, param);
544 #endif
545
546                 if (event == BLUETOOTH_EVENT_ADVERTISING_STOPPED)
547                         _bt_unregister_adv_slot_owner(slot_id);
548         } else if (strcasecmp(member, "RssiEnabled") == 0) {
549                 BT_DBG("RSSI Enabled");
550                 gboolean status = FALSE;
551                 char *address = NULL;
552                 int link_type;
553                 g_variant_get(msg, "(sib)", &address, &link_type, &status);
554
555                 BT_DBG("RSSI Enabled [%s %d]", address, status);
556                 param = g_variant_new("(isib)", result,
557                                         address, link_type, status);
558                 _bt_send_event(BT_DEVICE_EVENT,
559                                 BLUETOOTH_EVENT_RSSI_ENABLED,
560                                 param);
561                 g_free(address);
562         } else if (strcasecmp(member, "RssiAlert") == 0) {
563                 BT_DBG("RSSI Alert");
564                 int alert_type;
565                 int rssi_dbm;
566                 int link_type;
567                 char *address = NULL;
568                 g_variant_get(msg, "(siii)", &address, &link_type, &alert_type, &rssi_dbm);
569
570                 BT_DBG("RSSI Alert: [Address %s LinkType %d] [Type %d DBM %d]",
571                                 address, alert_type, rssi_dbm);
572                 param = g_variant_new("(isiii)", result,
573                                         address, link_type, alert_type, rssi_dbm);
574                 _bt_send_event(BT_DEVICE_EVENT,
575                                 BLUETOOTH_EVENT_RSSI_ALERT,
576                                 param);
577                 g_free(address);
578         } else if (strcasecmp(member, "RawRssi") == 0) {
579                 BT_DBG("RSSI Raw");
580                 int rssi_dbm;
581                 int link_type;
582                 char *address = NULL;
583                 g_variant_get(msg, "(sii)", &address, &link_type, &rssi_dbm);
584
585                 BT_DBG("Raw RSSI: [Address %s] [Link Type %d][RSSI DBM %d]",
586                                 address, link_type, rssi_dbm);
587                 param = g_variant_new("(isii)", result,
588                                         address, link_type, rssi_dbm);
589                 _bt_send_event(BT_DEVICE_EVENT,
590                                 BLUETOOTH_EVENT_RAW_RSSI,
591                                 param);
592                 g_free(address);
593         } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
594                 BT_ERR_C("### Hardware error received from BLUEZ");
595 /* Don't add the recovery logic into platform */
596 #if 0
597                 _bt_recover_adapter();
598 #endif
599         } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
600                 BT_ERR_C("### Tx timeout error received from BLUEZ");
601 /* Don't add the recovery logic into platform */
602 #if 0
603                 _bt_recover_adapter();
604 #endif
605         }
606         BT_DBG("-");
607 }
608
609 static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
610 {
611         GDBusProxy *adapter_proxy;
612         int mode = 0;
613         int result = BLUETOOTH_ERROR_NONE;
614         GVariantIter value_iter;
615         GVariant *val = NULL;
616         GVariant *ret = NULL;
617         GError *err = NULL;
618         char *property = NULL;
619         GVariant *param = NULL;
620
621         g_variant_iter_init(&value_iter, msg);
622         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
623                 BT_INFO("Property %s", property);
624
625                 if (strcasecmp(property, "Discovering") == 0) {
626                         gboolean discovering = FALSE;
627
628                         if (_bt_adapter_get_status() != BT_ACTIVATED)
629                                 continue;
630
631                         g_variant_get(val, "b", &discovering);
632                         BT_DBG("Discovering %d", discovering);
633
634                         /* Send event to application */
635                         if (discovering == TRUE) {
636                                 _bt_set_discovery_status(TRUE);
637                                 param = g_variant_new("(i)", result);
638                                 _bt_send_event(BT_ADAPTER_EVENT,
639                                         BLUETOOTH_EVENT_DISCOVERY_STARTED,
640                                         param);
641                         } else {
642                                 if (event_id > 0)
643                                         continue;
644
645                                 adapter_proxy = _bt_get_adapter_proxy();
646                                 if (adapter_proxy == NULL)
647                                         continue;
648
649                                 /* Need to stop searching */
650                                 ret = g_dbus_proxy_call_sync(adapter_proxy, "StopDiscovery",
651                                         NULL,
652                                         G_DBUS_CALL_FLAGS_NONE,
653                                         DBUS_TIMEOUT, NULL,
654                                         &err);
655                                 if (err) {
656                                         BT_ERR("Dbus Error : %s", err->message);
657                                         g_clear_error(&err);
658                                 }
659                                 if (ret)
660                                         g_variant_unref(ret);
661
662                                 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
663                                   (GSourceFunc)_bt_discovery_finished_cb, NULL);
664                         }
665                 } else if (strcasecmp(property, "LEDiscovering") == 0) {
666                         gboolean le_discovering = FALSE;
667
668                         if (_bt_adapter_get_status() != BT_ACTIVATED ||
669                             _bt_adapter_get_le_status() != BT_LE_ACTIVATED)
670                                 continue;
671
672                         g_variant_get(val, "b", &le_discovering);
673                         BT_DBG("LEDiscovering %d", le_discovering);
674
675                         /* Send event to application */
676                         if (le_discovering == TRUE) {
677                                 _bt_set_le_scan_status(TRUE);
678                                 param = g_variant_new("(i)", result);
679                                 _bt_send_event(BT_LE_ADAPTER_EVENT,
680                                 BLUETOOTH_EVENT_LE_DISCOVERY_STARTED,
681                                 param);
682                         } else {
683                                 if (event_id > 0)
684                                         continue;
685
686                                 adapter_proxy = _bt_get_adapter_proxy();
687                                 if (adapter_proxy == NULL)
688                                         continue;
689
690                                 /* Need to stop searching */
691                                 ret = g_dbus_proxy_call_sync(adapter_proxy, "StopLEDiscovery",
692                                         NULL,
693                                         G_DBUS_CALL_FLAGS_NONE,
694                                         DBUS_TIMEOUT, NULL,
695                                         &err);
696                                 if (err) {
697                                         BT_ERR("Dbus Error %s", err->message);
698                                         g_clear_error(&err);
699                                 }
700                                 if (ret)
701                                         g_variant_unref(ret);
702
703                                 event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
704                                                 (GSourceFunc)__bt_le_discovery_finished_cb, NULL);
705                                 }
706                 } else if (strcasecmp(property, "Name") == 0) {
707                         char *name = NULL;
708                         g_variant_get(val, "&s", &name);
709                         param = g_variant_new("(is)", result, name);
710                         /* Send event to application */
711                         _bt_send_event(BT_ADAPTER_EVENT,
712                                 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
713                                 param);
714                 } else if (strcasecmp(property, "Alias") == 0) {
715                         char *alias = NULL;
716                         g_variant_get(val, "&s", &alias);
717                         param = g_variant_new("(is)", result, alias);
718                         /* Send event to application */
719                         _bt_send_event(BT_ADAPTER_EVENT,
720                                 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
721                                 param);
722                 } else if (strcasecmp(property, "Discoverable") == 0) {
723                         gboolean discoverable = FALSE;
724
725                         g_variant_get(val, "b", &discoverable);
726                         BT_DBG("discoverable %d", discoverable);
727
728                         if (discoverable == FALSE) {
729                                 if (_bt_get_discoverable_timeout_property() > 0) {
730                                         unsigned int time = 0;
731
732                                         adapter_proxy = _bt_get_adapter_properties_proxy();
733                                         if (adapter_proxy == NULL)
734                                                 continue;
735
736                                         ret = g_dbus_proxy_call_sync(adapter_proxy, "Set",
737                                                 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
738                                                 "DiscoverableTimeout",
739                                                 g_variant_new("u", time)),
740                                                 G_DBUS_CALL_FLAGS_NONE,
741                                                 DBUS_TIMEOUT, NULL,
742                                                 &err);
743
744                                         if (err != NULL) {
745                                                 BT_ERR("Set DiscoverableTimeout Failed: %s", err->message);
746                                                 g_clear_error(&err);
747                                         }
748                                         if (ret)
749                                                 g_variant_unref(ret);
750                                 }
751
752                                 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
753
754                                 /* Send event to application */
755                                 BT_INFO("[Non Discoverable]");
756                                 param = g_variant_new("(in)", result, mode);
757                                 _bt_send_event(BT_ADAPTER_EVENT,
758                                         BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
759                                         param);
760                         } else {
761                                 _bt_get_discoverable_mode(&mode);
762
763                                 /* Event will be sent by "DiscoverableTimeout" signal */
764                                 if (mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
765                                         g_free(property);
766                                         g_variant_unref(val);
767                                         return;
768                                 }
769
770                                 /* Send event to application */
771                                 BT_INFO("[General Discoverable]");
772                                 param = g_variant_new("(in)", result, mode);
773                                 _bt_send_event(BT_ADAPTER_EVENT,
774                                         BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
775                                         param);
776                         }
777                 } else if (strcasecmp(property, "DiscoverableTimeout") == 0) {
778                         _bt_get_discoverable_mode(&mode);
779
780                         /* Event was already sent by "Discoverable" signal */
781                         if (mode == BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE ||
782                             mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
783                                 continue;
784
785                         /* Send event to application */
786                         BT_INFO("[Limited Discoverable (Timeout %u secs)]",
787                         _bt_get_discoverable_timeout_property());
788                         param = g_variant_new("(in)", result, mode);
789                         _bt_send_event(BT_ADAPTER_EVENT,
790                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
791                                 param);
792                 } else if (strcasecmp(property, "Powered") == 0) {
793                         /* TODO: Need to check this operation!! */
794                         gboolean powered = FALSE;
795                         int bt_state;
796
797                         g_variant_get(val, "b", &powered);
798                         BT_DBG("Powered = %d", powered);
799                         if (powered == FALSE) {
800 #ifdef USB_BLUETOOTH
801                                 _bt_handle_adapter_removed();
802 #else
803                                 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_state) == 0 &&
804                                 bt_state != VCONFKEY_BT_STATUS_OFF) {
805                                         if (__bt_handle_is_flight_mode_enabled() == FALSE)
806                                                 _bt_disable_adapter();
807                                         else
808                                                 _bt_handle_adapter_removed();
809                                 }
810 #endif
811                                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_state) == 0 &&
812                                         bt_state != VCONFKEY_BT_LE_STATUS_OFF) {
813                                         _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
814                                 }
815                         } else {
816 #ifdef USB_BLUETOOTH
817                                 _bt_handle_adapter_added();
818 #endif
819                         }
820                 } else if (strcasecmp(property, "Connectable") == 0) {
821                         gboolean connectable = FALSE;
822
823                         g_variant_get(val, "b", &connectable);
824
825                         BT_DBG("Connectable property is changed : %d", connectable);
826                         param = g_variant_new("(b)", connectable);
827                         _bt_send_event(BT_ADAPTER_EVENT,
828                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
829                                 param);
830 #if 0
831                         if (_bt_adapter_get_status() == BT_DEACTIVATING &&
832                         _bt_adapter_get_le_status() == BT_LE_ACTIVATED &&
833                         connectable == 0)
834                         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
835 #endif
836                 } else if (strcasecmp(property, "SupportedLEFeatures") == 0) {
837                         char *name = NULL;
838                         char *value = NULL;
839                         GVariantIter *iter = NULL;
840
841                         g_variant_get(val, "as", &iter);
842                         if (iter == NULL)
843                                 continue;
844
845                         while (g_variant_iter_next(iter, "&s", &name) &&
846                                g_variant_iter_next(iter, "&s", &value)) {
847                                 BT_DBG("name = %s, Value = %s", name, value);
848                                 if (!_bt_update_le_feature_support(name, value))
849                                         BT_ERR("Failed to update LE feature (name = %s, value = %s)", name, value);
850                         }
851                         g_variant_iter_free(iter);
852                 } else if (strcasecmp(property, "IpspInitStateChanged") == 0) {
853                         gboolean ipsp_initialized = FALSE;
854
855                         g_variant_get(val, "b", &ipsp_initialized);
856                         BT_DBG("IPSP init state changed: %d", ipsp_initialized);
857                         param = g_variant_new("(b)", ipsp_initialized);
858
859                         /* Send event to application */
860                         _bt_send_event(BT_ADAPTER_EVENT,
861                                         BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,
862                                         param);
863                 } else {
864                         BT_DBG("Unhandled property : [%s]", property);
865                 }
866         }
867 }
868
869 static void __bt_obex_property_changed_event(GVariant *msg, const char *path)
870 {
871         BT_DBG("+");
872
873         GVariantIter value_iter;
874         GVariant *child = NULL, *val = NULL;
875         char *property = NULL;
876         g_variant_iter_init(&value_iter, msg);
877         while ((child = g_variant_iter_next_value(&value_iter))) {
878                 g_variant_get(child, "{sv}", &property, &val);
879
880                 ret_if(property == NULL);
881
882                 BT_DBG("property :%s", property);
883
884                 if (strcasecmp(property, "Status") == 0) {
885                         char  *status;
886                         g_variant_get(val, "s", &status);
887
888                         if (strcasecmp(status, "active") == 0) {
889                                 _bt_obex_transfer_started(path);
890                         } else if (strcasecmp(status, "complete") == 0) {
891                                 _bt_obex_transfer_completed(path, TRUE);
892                                 _bt_pbap_obex_transfer_completed(path, TRUE);
893                         } else if (strcasecmp(status, "error") == 0) {
894                                 _bt_obex_transfer_completed(path, FALSE);
895                                 _bt_pbap_obex_transfer_completed(path, FALSE);
896                         }
897                         g_free(status);
898                 } else if (strcasecmp(property, "Transferred") == 0) {
899                         guint64 transferred  = 0;
900                         /* As Transferred is expected guint64 so change int to guint64 and
901                          * eariler transferred is static because of it can overwrite data
902                          * on present on opc_obex_conn or obexd_conn as these are
903                          * static memory are in sequential */
904                         g_variant_get(val, "t", &transferred);
905
906                         _bt_obex_transfer_progress(path, transferred);
907                 }
908                 g_free(property);
909                 g_variant_unref(val);
910                 g_variant_unref(child);
911         }
912         BT_DBG("-");
913 }
914
915 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
916 {
917         BT_DBG("+");
918
919         int event;
920         int result = BLUETOOTH_ERROR_NONE;
921         GVariantIter value_iter;
922         GVariant *val;
923         char *property = NULL;
924         char *address;
925         GVariant *param = NULL;
926         bt_remote_dev_info_t *remote_dev_info;
927         g_variant_iter_init(&value_iter, msg);
928         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
929                 BT_DBG("Property %s", property);
930                 if (strcasecmp(property, "Connected") == 0) {
931                         guint connected = 0;
932
933                         g_variant_get(val, "i", &connected);
934
935                         event = (connected != BLUETOOTH_CONNECTED_LINK_NONE) ?
936                                 BLUETOOTH_EVENT_DEVICE_CONNECTED :
937                                 BLUETOOTH_EVENT_DEVICE_DISCONNECTED;
938
939                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
940
941                         _bt_convert_device_path_to_address(path, address);
942
943                         BT_DBG("connected: %d", connected);
944                         BT_DBG("address: %s", address);
945
946                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
947
948                         if (remote_dev_info != NULL) {
949                                 __bt_device_remote_connected_properties(
950                                                 remote_dev_info, address,
951                                                 connected != BLUETOOTH_CONNECTED_LINK_NONE ?
952                                                 TRUE : FALSE);
953                                 _bt_free_device_info(remote_dev_info);
954                         }
955                         param = g_variant_new("(is)", result, address);
956                         /* Send event to application */
957                         _bt_send_event(BT_DEVICE_EVENT,
958                                         event,
959                                         param);
960                         g_free(address);
961                 } else if (strcasecmp(property, "RSSI") == 0) {
962                         bt_remote_dev_info_t *remote_dev_info;
963
964                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
965
966                         _bt_convert_device_path_to_address(path, address);
967                         BT_DBG("address: %s", address);
968
969                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
970                         if (remote_dev_info == NULL) {
971                                 g_free(property);
972                                 g_variant_unref(val);
973                                 g_free(address);
974                                 return;
975                         }
976                         BT_DBG("Address type  %d", remote_dev_info->addr_type);
977
978                         if (remote_dev_info->addr_type == 0) {
979                                 BT_DBG("Name %s", remote_dev_info->name);
980
981 #ifdef TIZEN_DPM_ENABLE
982                                 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
983                                                         DPM_RESTRICTED) {
984                                         bluetooth_device_class_t device_class;
985                                         _bt_divide_device_class(&device_class, remote_dev_info->class);
986
987                                         if (device_class.major_class ==
988                                                 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
989                                                 _bt_free_device_info(remote_dev_info);
990                                                 g_free(property);
991                                                 g_variant_unref(val);
992                                                 g_free(address);
993                                                 return;
994                                         }
995                                 }
996 #endif
997
998                                 GVariant *uuids = NULL;
999                                 GVariantBuilder *builder = NULL;
1000                                 int i = 0;
1001                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1002                                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1003                                         g_variant_builder_add(builder, "s",
1004                                                 remote_dev_info->uuids[i]);
1005                                 }
1006                                 uuids = g_variant_new("as", builder);
1007                                 g_variant_builder_unref(builder);
1008                                 GVariant *manufacturer_data =  NULL;
1009                                 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1010                                                                         remote_dev_info->manufacturer_data,
1011                                                                         remote_dev_info->manufacturer_data_len,
1012                                                                         TRUE,
1013                                                                         NULL, NULL);
1014                                 param = g_variant_new("(isunsbub@asn@ay)", result,
1015                                                         remote_dev_info->address,
1016                                                         remote_dev_info->class,
1017                                                         remote_dev_info->rssi,
1018                                                         remote_dev_info->name,
1019                                                         remote_dev_info->paired,
1020                                                         remote_dev_info->connected,
1021                                                         remote_dev_info->trust,
1022                                                         uuids,
1023                                                         remote_dev_info->manufacturer_data_len,
1024                                                         manufacturer_data);
1025
1026                                 _bt_send_event(BT_ADAPTER_EVENT,
1027                                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1028                                         param);
1029                                 g_free(address);
1030                         }
1031                         _bt_free_device_info(remote_dev_info);
1032                 } else if (strcasecmp(property, "GattConnected") == 0) {
1033                         gboolean gatt_connected = FALSE;
1034
1035                         g_variant_get(val, "b", &gatt_connected);
1036
1037                         event = gatt_connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1038                                         BLUETOOTH_EVENT_GATT_DISCONNECTED;
1039
1040                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1041
1042                         _bt_convert_device_path_to_address(path, address);
1043
1044                         BT_DBG("gatt_connected: %d", gatt_connected);
1045                         BT_DBG("address: %s", address);
1046                         param = g_variant_new("(is)", result, address);
1047                         /* Send event to application */
1048                         _bt_send_event(BT_DEVICE_EVENT,
1049                                         event,
1050                                         param);
1051                         g_free(address);
1052                 } else if (strcasecmp(property, "Paired") == 0) {
1053                         gboolean paired = FALSE;
1054                         bt_remote_dev_info_t *remote_dev_info;
1055                         g_variant_get(val, "b", &paired);
1056                         _bt_agent_set_canceled(FALSE);
1057                         /* BlueZ sends paired signal for each paired device */
1058                         /* during activation, We should ignore this, otherwise*/
1059                         /* application thinks that a new device got paired */
1060                         if (_bt_adapter_get_status() != BT_ACTIVATED) {
1061                                 BT_DBG("BT is not activated, so ignore this");
1062                                 g_free(property);
1063                                 g_variant_unref(val);
1064                                 return;
1065                         }
1066
1067                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1068
1069                         _bt_convert_device_path_to_address(path, address);
1070
1071                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1072                         if (remote_dev_info == NULL) {
1073                                 g_free(property);
1074                                 g_variant_unref(val);
1075                                 g_free(address);
1076                                 return;
1077                         }
1078
1079                         if (paired == FALSE) {
1080                                 BT_INFO("Unpaired: %s", address);
1081                                 __bt_update_remote_cache_devinfo(address, FALSE);
1082                                 param = g_variant_new("(is)", result, address);
1083                                 _bt_send_event(BT_ADAPTER_EVENT,
1084                                         BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
1085                                         param);
1086 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1087                                 {
1088                                         char *last_connected = NULL;
1089                                         last_connected = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1090                                         if (!g_strcmp0(address, last_connected))
1091                                                 _bt_audio_set_auto_connect_device_addr("");
1092                                         if (last_connected)
1093                                                 free(last_connected);
1094                                 }
1095 #endif
1096                         } else {
1097                                 char secure_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
1098
1099                                 _bt_convert_addr_string_to_secure_string(secure_addr, address);
1100                                 BT_INFO("### Paired: %s", secure_addr);
1101                                 __bt_update_remote_cache_devinfo(address, TRUE);
1102
1103                                 if (_bt_is_device_creating() == TRUE) {
1104                                         BT_DBG("Try to Pair by me");
1105                                         _bt_free_device_info(remote_dev_info);
1106                                         g_free(address);
1107                                         g_free(property);
1108                                         g_variant_unref(val);
1109                                         return;
1110                                 }
1111                                 GVariant *uuids = NULL;
1112                                 GVariantBuilder *builder = NULL;
1113                                 int i = 0;
1114                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1115                                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1116                                         g_variant_builder_add(builder, "s",
1117                                                 remote_dev_info->uuids[i]);
1118                                 }
1119                                 uuids = g_variant_new("as", builder);
1120                                 g_variant_builder_unref(builder);
1121                                 GVariant *manufacturer_data =  NULL;
1122                                 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1123                                                                         remote_dev_info->manufacturer_data,
1124                                                                         remote_dev_info->manufacturer_data_len,
1125                                                                         TRUE,
1126                                                                         NULL, NULL);
1127
1128                                 param = g_variant_new("(isunsbub@asn@ay)", result,
1129                                                         address, remote_dev_info->class,
1130                                                         remote_dev_info->rssi,
1131                                                         remote_dev_info->name,
1132                                                         remote_dev_info->paired,
1133                                                         remote_dev_info->connected,
1134                                                         remote_dev_info->trust,
1135                                                         uuids,
1136                                                         remote_dev_info->manufacturer_data_len,
1137                                                         manufacturer_data);
1138                                 _bt_send_event(BT_ADAPTER_EVENT,
1139                                         BLUETOOTH_EVENT_BONDING_FINISHED,
1140                                         param);
1141                         }
1142                         _bt_free_device_info(remote_dev_info);
1143                         g_free(address);
1144                 } else if (strcasecmp(property, "LegacyPaired") == 0) {
1145                         gboolean paired = FALSE;
1146                         bt_remote_dev_info_t *remote_dev_info;
1147                         unsigned char auth_info[5] = {0, };
1148
1149                         if (_bt_adapter_get_status() != BT_ACTIVATED) {
1150                                 BT_DBG("BT is not activated, so ignore this");
1151                                 g_free(property);
1152                                 g_variant_unref(val);
1153                                 return;
1154                         }
1155
1156                         g_variant_get(val, "b", &paired);
1157                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1158                         BT_DBG("LegacyPaired: %d", paired);
1159                         _bt_convert_device_path_to_address(path, address);
1160
1161                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1162                         if (remote_dev_info == NULL) {
1163                                 g_free(address);
1164                                 g_free(property);
1165                                 g_variant_unref(val);
1166                                 return;
1167                         }
1168                         if (remote_dev_info->is_alias_set == FALSE) {
1169                                 /*minimum Size of the samsung specific manufacturer data is greater than 30 */
1170                                 if ((remote_dev_info->manufacturer_data_len > 30) &&
1171                                         (remote_dev_info->manufacturer_data[0] == 0x00) &&
1172                                         (remote_dev_info->manufacturer_data[1] == 0x75)) {
1173
1174                                         /* 2  samsung (0x00 0x75) + 1 (control and version) + 1 (service ID) +
1175                                         1 (discovery version) + 1 (associated service ID)
1176                                         2 (Proxamity and locality) + 2 (Device type and icon) = 10 */
1177
1178                                         memcpy(auth_info, &(remote_dev_info->manufacturer_data[10]), 5);
1179                                 }
1180                         }
1181
1182                         BT_DBG("LegacyPairing Failed with %s. Show Error Popup", remote_dev_info->name);
1183                         _bt_launch_system_popup(BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE,
1184                                                 remote_dev_info->name, auth_info, NULL, NULL, NULL);
1185
1186                         _bt_free_device_info(remote_dev_info);
1187                         g_free(address);
1188                 } else if (strcasecmp(property, "Trusted") == 0) {
1189                         gboolean trusted = FALSE;
1190
1191                         g_variant_get(val, "b", &trusted);
1192
1193                         event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1194                                         BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1195
1196                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1197
1198                         _bt_convert_device_path_to_address(path, address);
1199
1200                         BT_DBG("trusted: %d", trusted);
1201                         BT_DBG("address: %s", address);
1202                         param = g_variant_new("(is)", result, address);
1203                         /* Send event to application */
1204                         _bt_send_event(BT_DEVICE_EVENT,
1205                                         event,
1206                                         param);
1207                         g_free(address);
1208                 } else if (strcasecmp(property, "TrustedProfiles") == 0) {
1209                         int trusted = 0;
1210
1211                         g_variant_get(val, "u", &trusted);
1212
1213                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1214                         _bt_convert_device_path_to_address(path, address);
1215
1216                         BT_DBG("Address: %s, TrustedProfiles: %d", address, trusted);
1217                         _bt_send_event(BT_DEVICE_EVENT,
1218                                         BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1219                                         g_variant_new("(isi)", result, address, trusted));
1220                         g_free(address);
1221                 } else if (strcasecmp(property, "IpspConnected") == 0) {
1222                         gboolean connected = FALSE;
1223
1224                         g_variant_get(val, "b", &connected);
1225
1226
1227                         event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
1228                                                         BLUETOOTH_EVENT_IPSP_DISCONNECTED;
1229
1230                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1231
1232                         _bt_convert_device_path_to_address(path, address);
1233
1234                         BT_DBG("Ipspconnected: %d", connected);
1235                         BT_DBG("address: %s", address);
1236                         param = g_variant_new("(is)", result, address);
1237
1238                         /* Send event to application */
1239                         _bt_send_event(BT_DEVICE_EVENT,
1240                                         event,
1241                                         param);
1242                         g_free(address);
1243                 } else if (strcasecmp(property, "IpspBtInterfaceInfo") == 0) {
1244                         char *ifname = NULL;
1245
1246                         g_variant_get(val, "s", &ifname);
1247
1248                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1249
1250                         _bt_convert_device_path_to_address(path, address);
1251
1252                         BT_DBG("Ipsp BT Interface Name: %s", ifname);
1253                         BT_DBG("address: %s", address);
1254                         param = g_variant_new("(iss)", result, address, ifname);
1255
1256                         /* Send event to application */
1257                         _bt_send_event(BT_DEVICE_EVENT,
1258                                         BLUETOOTH_EVENT_IPSP_INTERFACE_INFO,
1259                                         param);
1260                         g_free(address);
1261                 }
1262         }
1263         BT_DBG("-");
1264 }
1265
1266 static void __bt_media_control_changed_event(GVariant *msg, const char *path)
1267 {
1268         int event;
1269         int result = BLUETOOTH_ERROR_NONE;
1270         GVariantIter value_iter;
1271         char *property = NULL;
1272         char *address;
1273         GVariant *val = NULL;
1274         GVariant *child = NULL;
1275         bt_remote_dev_info_t *remote_dev_info;
1276         GVariant *param = NULL;
1277         g_variant_iter_init(&value_iter, msg);
1278         while ((child = g_variant_iter_next_value(&value_iter))) {
1279                 g_variant_get(child, "{sv}", &property, &val);
1280                 BT_INFO("Property %s", property);
1281                 if (strcasecmp(property, "Connected") == 0) {
1282                         gboolean connected = FALSE;
1283
1284                         g_variant_get(val, "b", &connected);
1285
1286                         event = connected ? BLUETOOTH_EVENT_AVRCP_CONNECTED :
1287                                         BLUETOOTH_EVENT_AVRCP_DISCONNECTED;
1288
1289                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1290
1291                         _bt_convert_device_path_to_address(path, address);
1292
1293                         BT_DBG("connected: %d", connected);
1294                         BT_DBG("address: %s", address);
1295
1296                         remote_dev_info = _bt_get_remote_device_info(address);
1297
1298                         if (remote_dev_info != NULL) {
1299                                 __bt_device_remote_connected_properties(
1300                                 remote_dev_info, address, connected);
1301                                 _bt_free_device_info(remote_dev_info);
1302                         }
1303                         param = g_variant_new("(is)", result, address);
1304                         /* Send event to application */
1305                         _bt_send_event(BT_AVRCP_EVENT,
1306                                 event,
1307                                 param);
1308                         g_free(address);
1309                 }
1310                 g_free(property);
1311                 g_variant_unref(child);
1312                 g_variant_unref(val);
1313         }
1314         BT_DBG("-");
1315 }
1316
1317 int get_alert_level_enum(const char *alert)
1318 {
1319         int lvl = -1;
1320
1321         if (strcmp(alert, "none") == 0)
1322                 lvl = BT_PXP_ALERT_NONE;
1323         else if (strcmp(alert, "mild") == 0)
1324                 lvl = BT_PXP_ALERT_MILD;
1325         else if (strcmp(alert, "high") == 0)
1326                 lvl = BT_PXP_ALERT_HIGH;
1327
1328         return lvl;
1329 }
1330
1331 static void _bt_handle_pxp_property_changed_event(GVariant *msg, const char *path, int role)
1332 {
1333         int result = BLUETOOTH_ERROR_NONE;
1334         int service_type;
1335         int alert_lvl;
1336         GVariantIter value_iter;
1337         char *property = NULL;
1338         char *address;
1339         GVariant *val = NULL;
1340         GVariant *child = NULL;
1341         GVariant *param = NULL;
1342         g_variant_iter_init(&value_iter, msg);
1343
1344         BT_DBG("+");
1345         while ((child = g_variant_iter_next_value(&value_iter))) {
1346                 g_variant_get(child, "{sv}", &property, &val);
1347                 BT_INFO("Property %s", property);
1348
1349                 if ((strcasecmp(property, "LinkLossAlertLevel") == 0) ||
1350                         (strcasecmp(property, "ImmediateAlertLevel") == 0)) {
1351                         char *alert_str = NULL;
1352
1353                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1354                         _bt_convert_device_path_to_address(path, address);
1355
1356                         if (strcasecmp(property, "LinkLossAlertLevel") == 0)
1357                                 service_type = BT_PXP_PROPERTY_LLS;
1358                         else
1359                                 service_type = BT_PXP_PROPERTY_IAS;
1360
1361                         g_variant_get(val, "s", &alert_str);
1362                         alert_lvl = get_alert_level_enum(alert_str);
1363
1364                         param = g_variant_new("(isiii)", result, address,
1365                                                                 role, service_type, alert_lvl);
1366
1367                         /* Send event to application */
1368                         _bt_send_event(BT_DEVICE_EVENT,
1369                                         BLUETOOTH_EVENT_PXP_PROPERTY_CHANGED,
1370                                         param);
1371                         g_free(address);
1372                         g_free(alert_str);
1373                 }
1374                 g_free(property);
1375                 g_variant_unref(child);
1376                 g_variant_unref(val);
1377         }
1378         BT_DBG("-");
1379 }
1380
1381 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
1382 {
1383         char *interface_name = NULL;
1384         GVariant *val = NULL;
1385
1386         g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &val, NULL);
1387
1388         if (strcasecmp(interface_name, BT_ADAPTER_INTERFACE) == 0) {
1389                 __bt_adapter_property_changed_event(val,
1390                                         object_path);
1391         } else if (strcasecmp(interface_name, BT_DEVICE_INTERFACE) == 0) {
1392                 __bt_device_property_changed_event(val, object_path);
1393         } else if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1394                 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
1395                 __bt_obex_property_changed_event(val,
1396                                         object_path);
1397         } else if (strcasecmp(interface_name, BT_MEDIA_CONTROL_INTERFACE) == 0) {
1398                 __bt_media_control_changed_event(val,
1399                                         object_path);
1400         } else if (strcasecmp(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
1401                 _bt_handle_avrcp_control_event(val,
1402                                         object_path);
1403         } else if (strcasecmp(interface_name, BT_NETWORK_CLIENT_INTERFACE) == 0) {
1404                 BT_DBG("BT_NETWORK_CLIENT_INTERFACE");
1405                 _bt_handle_network_client_event(val,
1406                                         object_path);
1407         } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
1408                 __bt_gatt_char_property_changed_event(val,
1409                                         object_path);
1410         } else if (strcasecmp(interface_name, BT_PROXIMITY_REPORTER_INTERFACE) == 0) {
1411                 BT_DBG("BT_PROXIMITY_REPORTER_INTERFACE");
1412                 _bt_handle_pxp_property_changed_event(val,
1413                                         object_path, BT_PXP_REPORTER_ROLE);
1414         } else if (strcasecmp(interface_name, BT_PROXIMITY_MONITOR_INTERFACE) == 0) {
1415                 BT_DBG("BT_PROXIMITY_MONITOR_INTERFACE");
1416                 _bt_handle_pxp_property_changed_event(val,
1417                                         object_path, BT_PXP_MONITOR_ROLE);
1418         }
1419         g_variant_unref(val);
1420 }
1421
1422 void __bt_opc_property_changed_event(GVariant *msg,
1423                                                 const char *path)
1424 {
1425         GVariantIter value_iter;
1426         char *property = NULL;
1427         GVariant *val = NULL;
1428         GVariant *child = NULL;
1429
1430         g_variant_iter_init(&value_iter, msg);
1431         while ((child = g_variant_iter_next_value(&value_iter))) {
1432                 g_variant_get(child, "{sv}", &property, &val);
1433                 ret_if(property == NULL);
1434
1435                 if (strcasecmp(property, "Status") == 0) {
1436                         char *status = NULL;
1437                         g_variant_get(val, "s", &status);
1438                         BT_DBG("Status is %s", status);
1439
1440                         if (strcasecmp(status, "active") == 0)
1441                                 _bt_obex_client_started(path);
1442                         else if (strcasecmp(status, "complete") == 0)
1443                                 _bt_obex_client_completed(path, TRUE);
1444                         else if (strcasecmp(status, "error") == 0)
1445                                 _bt_obex_client_completed(path, FALSE);
1446
1447                         g_free(status);
1448                 } else if (strcasecmp(property, "Transferred") == 0) {
1449                         guint64 transferred  = 0;
1450                         g_variant_get(val, "t", &transferred);
1451
1452                         _bt_obex_client_progress(path, transferred);
1453                 } else {
1454                         BT_DBG("property : [%s]", property);
1455                 }
1456                 g_free(property);
1457                 g_variant_unref(child);
1458                 g_variant_unref(val);
1459         }
1460 }
1461
1462 void _bt_opc_property_changed_event(GVariant *msg, char *path)
1463 {
1464         char *interface_name = NULL;
1465         GVariant *value = NULL;
1466         g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1467         BT_INFO("interface_name = %s", interface_name);
1468         if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1469                 __bt_opc_property_changed_event(value,
1470                                         path);
1471         } else {
1472                 BT_DBG("interface_name : [%s]", interface_name);
1473         }
1474         g_variant_unref(value);
1475 }
1476
1477
1478 void _bt_handle_input_event(GVariant *msg, const char *path)
1479 {
1480         int result = BLUETOOTH_ERROR_NONE;
1481         gboolean property_flag = FALSE;
1482         GVariantIter value_iter;
1483         char *property = NULL;
1484         GVariant *child = NULL, *val = NULL;
1485         bt_remote_dev_info_t *remote_dev_info;
1486         GVariant *param = NULL;
1487         g_variant_iter_init(&value_iter, msg);
1488         while ((child = g_variant_iter_next_value(&value_iter))) {
1489                 g_variant_get(child, "{sv}", &property, &val);
1490
1491                 ret_if(property == NULL);
1492
1493                 if (strcasecmp(property, "Connected") == 0) {
1494                         int event = BLUETOOTH_EVENT_NONE;
1495                         char *address;
1496                         g_variant_get(val, "b", &property_flag);
1497
1498                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1499
1500                         _bt_convert_device_path_to_address(path, address);
1501
1502                         event = (property_flag == TRUE) ?
1503                                         BLUETOOTH_HID_CONNECTED :
1504                                         BLUETOOTH_HID_DISCONNECTED;
1505                         param = g_variant_new("(is)", result, address);
1506                         _bt_send_event(BT_HID_EVENT, event,
1507                                 param);
1508                         /* Check HID connection type (Keyboard or Mouse) and update the status */
1509                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1510
1511                         if (property_flag == TRUE) {
1512                                 hid_connected_device_count++;
1513                                 __bt_set_device_values(TRUE,
1514                                                 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1515                         } else {
1516                                 hid_connected_device_count--;
1517                                 if (hid_connected_device_count == 0)
1518                                         __bt_set_device_values(FALSE,
1519                                                         VCONFKEY_BT_DEVICE_HID_CONNECTED);
1520                         }
1521
1522                         if (remote_dev_info != NULL) {
1523                                 BT_DBG("HID device class [%x]", remote_dev_info->class);
1524                                 if (remote_dev_info->class &
1525                                         BLUETOOTH_DEVICE_MINOR_CLASS_KEY_BOARD) {
1526                                         __bt_set_device_values(property_flag,
1527                                                 VCONFKEY_BT_DEVICE_HID_KEYBOARD_CONNECTED);
1528
1529                                 }
1530
1531                                 if (remote_dev_info->class &
1532                                                 BLUETOOTH_DEVICE_MINOR_CLASS_POINTING_DEVICE) {
1533                                         __bt_set_device_values(property_flag,
1534                                                         VCONFKEY_BT_DEVICE_HID_MOUSE_CONNECTED);
1535                                 }
1536                                 _bt_free_device_info(remote_dev_info);
1537                         }
1538                         g_free(address);
1539                 }
1540                 g_free(property);
1541                 g_variant_unref(val);
1542                 g_variant_unref(child);
1543          }
1544 }
1545
1546 void _bt_handle_network_server_event(GVariant *msg, const char *member)
1547 {
1548         int result = BLUETOOTH_ERROR_NONE;
1549         char *address = NULL;
1550         char *device = NULL;
1551         GVariant *param = NULL;
1552         ret_if(member == NULL);
1553         if (strcasecmp(member, "PeerConnected") == 0) {
1554                 g_variant_get(msg, "(ss)", &device, &address);
1555
1556                 __bt_set_device_values(TRUE,
1557                                 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1558                 param = g_variant_new("(iss)", result, device, address);
1559                 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
1560                         param);
1561                 g_free(device);
1562                 g_free(address);
1563                  nap_connected_device_count++;
1564         } else if (strcasecmp(member, "PeerDisconnected") == 0) {
1565                 g_variant_get(msg, "(ss)", &device, &address);
1566                 nap_connected_device_count--;
1567                 if (nap_connected_device_count == 0)
1568                         __bt_set_device_values(FALSE,
1569                                 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1570                 param = g_variant_new("(iss)", result, device, address);
1571                 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
1572                         param);
1573                 g_free(device);
1574                 g_free(address);
1575         }
1576 }
1577
1578 void _bt_handle_network_client_event(GVariant *msg,
1579                                 const char *path)
1580 {
1581         BT_DBG("+");
1582
1583         int result = BLUETOOTH_ERROR_NONE;
1584         gboolean property_flag = FALSE;
1585         char *property = NULL;
1586         GVariant *val = NULL;
1587         GVariantIter value_iter;
1588         GVariant *param = NULL;
1589         g_variant_iter_init(&value_iter, msg);
1590         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1591                 if (strcasecmp(property, "Connected") == 0) {
1592                         int event = BLUETOOTH_EVENT_NONE;
1593                         char *address;
1594
1595                         g_variant_get(val, "b", &property_flag);
1596                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1597
1598                         _bt_convert_device_path_to_address(path, address);
1599
1600                         BT_DBG("property_flag %d", property_flag);
1601                         if (property_flag == TRUE) {
1602                                 event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
1603                                 nap_connected_device_count++;
1604                                 __bt_set_device_values(TRUE,
1605                                         VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1606                         } else {
1607                                 event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
1608                                 nap_connected_device_count--;
1609                                 if (nap_connected_device_count == 0)
1610                                         __bt_set_device_values(FALSE,
1611                                                 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1612                         }
1613                         param = g_variant_new("(is)", result, address);
1614                         _bt_send_event(BT_NETWORK_EVENT, event,
1615                                 param);
1616
1617                         g_free(address);
1618                 }
1619         }
1620         BT_DBG("-");
1621 }
1622
1623 void __bt_gatt_char_property_changed_event(GVariant *msg,
1624                                 const char *path)
1625 {
1626         GVariantIter value_iter;
1627         char *property = NULL;
1628         char * char_handle = NULL;
1629         GVariant *val = NULL;
1630         int result = BLUETOOTH_ERROR_NONE;
1631         GVariant *param = NULL;
1632         g_variant_iter_init(&value_iter, msg);
1633         char_handle = g_strdup(path);
1634         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1635                 BT_DBG("Property %s", property);
1636
1637                 ret_if(property == NULL);
1638
1639                 if (strcasecmp(property, "Notifying") == 0) {
1640                         gboolean property_flag = FALSE;
1641                         g_variant_get(val, "b", &property_flag);
1642                         BT_INFO("Notifying is %s", property_flag ? "enabled" : "disabled");
1643                 } else if (strcasecmp(property, "ChangedValue") == 0) {
1644                         int len = 0;
1645                         GByteArray *gp_byte_array = NULL;
1646                         BT_INFO("Type '%s'\n", g_variant_get_type_string(val));
1647
1648                         if (val) {
1649                                 gp_byte_array = g_byte_array_new();
1650                                 len = g_variant_get_size(val);
1651                                 BT_DBG("Len = %d", len);
1652                                 g_byte_array_append(gp_byte_array,
1653                                         (const guint8 *) g_variant_get_data(val), len);
1654                                 if (gp_byte_array->len != 0) {
1655                                         GVariant *byte_array = NULL;
1656                                         byte_array = g_variant_new_from_data(
1657                                                                 G_VARIANT_TYPE_BYTESTRING,
1658                                                                 gp_byte_array->data,
1659                                                                 gp_byte_array->len,
1660                                                                 TRUE, NULL, NULL);
1661                                         param = g_variant_new("(is@ay)", result, char_handle,
1662                                                                 byte_array);
1663
1664                                         /* Send event only registered client */
1665                                         _bt_send_char_value_changed_event(param);
1666                                 }
1667                                 g_byte_array_free(gp_byte_array, TRUE);
1668                         }
1669                 }
1670         }
1671         g_free(char_handle);
1672 }
1673
1674 void _bt_handle_gatt_event(GVariant *msg, const char *member, const char *path)
1675 {
1676         ret_if(path == NULL);
1677
1678         if (strcasecmp(member, "GattValueChanged") == 0) {
1679
1680 #if 0 // Debug Only
1681                 /*** Debug only ***/
1682                 GVariant *value = NULL;
1683                 int value_len = 0;
1684                 char *buffer = NULL;
1685
1686                 g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
1687                 value_len = g_variant_get_size(value);
1688                 if (value_len > 0) {
1689                         char buf[8 * 5 + 1] = { 0 };
1690                         int i;
1691                         int to;
1692                         buffer = (char *)g_variant_get_data(value);
1693                         to = value_len > (sizeof(buf) / 5) ? sizeof(buf) / 5 : value_len;
1694
1695                         for (i = 0; i < to; i++)
1696                                 snprintf(&buf[i * 5], 6, "0x%02x ", buffer[i]);
1697                         buf[i * 5] = '\0';
1698                         BT_DBG("GATT Val[%d] %s", value_len, buf);
1699                 }
1700                 g_variant_unref(value);
1701                 /******/
1702 #endif
1703
1704                 /* Send event only registered client */
1705                 _bt_send_char_value_changed_event(msg);
1706         }
1707 }
1708
1709
1710 void _bt_handle_device_event(GVariant *msg, const char *member, const char *path)
1711 {
1712         int event = 0;
1713         int result = BLUETOOTH_ERROR_NONE;
1714         char *address;
1715         char *dev_name = NULL;
1716         const char *property = NULL;
1717         GVariant *param = NULL;
1718         char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1719         ret_if(path == NULL);
1720
1721         if (strcasecmp(member, "PropertyChanged") == 0) {
1722
1723                 g_variant_get(msg, "(s)", &property);
1724
1725                 ret_if(property == NULL);
1726
1727                 if (strcasecmp(property, "GattConnected") == 0) {
1728                         gboolean connected = FALSE;
1729                         char *address;
1730                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1731
1732                         _bt_convert_device_path_to_address(path, address);
1733                         g_variant_get(msg, "(b)", &connected);
1734
1735                         event = connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1736                                         BLUETOOTH_EVENT_GATT_DISCONNECTED;
1737                         param = g_variant_new("(is)", result, address);
1738                         _bt_send_event(BT_DEVICE_EVENT,
1739                                         event,
1740                                         param);
1741                         g_free(address);
1742                 } else if (strcasecmp(property, "Paired") == 0) {
1743                         gboolean paired = FALSE;
1744                         bt_remote_dev_info_t *remote_dev_info;
1745                         g_variant_get(msg, "(b)", &paired);
1746
1747                         ret_if(paired == FALSE);
1748
1749                         /* BlueZ sends paired signal for each paired device */
1750                         /* during activation, We should ignore this, otherwise*/
1751                         /* application thinks that a new device got paired */
1752                         if (_bt_adapter_get_status() != BT_ACTIVATED) {
1753                                 BT_DBG("BT is not activated, so ignore this");
1754                                 return;
1755                         }
1756
1757                         if (_bt_is_device_creating() == TRUE) {
1758                                 BT_DBG("Try to Pair by me");
1759                                 return;
1760                         }
1761
1762                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1763
1764                         _bt_convert_device_path_to_address(path, address);
1765
1766                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1767                         if (remote_dev_info == NULL) {
1768                                 g_free(address);
1769                                 return;
1770                         }
1771                         GVariant *uuids = NULL;
1772                         GVariantBuilder *builder = NULL;
1773                         int i = 0;
1774                         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1775                         for (i = 0; i < remote_dev_info->uuid_count; i++) {
1776                                 g_variant_builder_add(builder, "s",
1777                                         remote_dev_info->uuids[i]);
1778                         }
1779                         uuids = g_variant_new("as", builder);
1780                         g_variant_builder_unref(builder);
1781                         GVariant *manufacturer_data = NULL;
1782                         manufacturer_data = g_variant_new_from_data(
1783                                                 G_VARIANT_TYPE_BYTESTRING,
1784                                                 remote_dev_info->manufacturer_data,
1785                                                 remote_dev_info->manufacturer_data_len,
1786                                                 TRUE, NULL, NULL);
1787                         param = g_variant_new("(isunsbub@asn@ay)", result,
1788                                                 address,
1789                                                 remote_dev_info->class,
1790                                                 remote_dev_info->rssi,
1791                                                 remote_dev_info->name,
1792                                                 remote_dev_info->paired,
1793                                                 remote_dev_info->connected,
1794                                                 remote_dev_info->trust,
1795                                                 uuids,
1796                                                 remote_dev_info->manufacturer_data_len,
1797                                                 manufacturer_data);
1798                         _bt_send_event(BT_ADAPTER_EVENT,
1799                                 BLUETOOTH_EVENT_BONDING_FINISHED,
1800                                 param);
1801                         _bt_free_device_info(remote_dev_info);
1802                         g_free(address);
1803
1804                 } else if (strcasecmp(property, "UUIDs") == 0) {
1805                         /* Once we get the updated uuid information after
1806                          * reverse service search, update it to application */
1807
1808                         bt_remote_dev_info_t *remote_dev_info;
1809
1810                         ret_if(_bt_is_device_creating() == TRUE);
1811
1812                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1813
1814                         _bt_convert_device_path_to_address(path, address);
1815
1816                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1817                         if (remote_dev_info == NULL) {
1818                                 g_free(address);
1819                                 return;
1820                         }
1821
1822                         BT_DBG("UUID's count = %d", remote_dev_info->uuid_count);
1823                         if (remote_dev_info->paired && remote_dev_info->uuid_count) {
1824                                 GVariant *uuids = NULL;
1825                                 GVariantBuilder *builder = NULL;
1826                                 int i = 0;
1827                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1828                                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1829                                         g_variant_builder_add(builder, "s",
1830                                                 remote_dev_info->uuids[i]);
1831                                 }
1832                                 uuids = g_variant_new("as", builder);
1833                                 g_variant_builder_unref(builder);
1834                                 GVariant *manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1835                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1836                                                 TRUE, NULL, NULL);
1837
1838                                 param = g_variant_new("(isunsbub@asn@ay)", result,
1839                                                         address, remote_dev_info->class,
1840                                                         remote_dev_info->rssi,
1841                                                         remote_dev_info->name,
1842                                                         remote_dev_info->paired,
1843                                                         remote_dev_info->connected,
1844                                                         remote_dev_info->trust,
1845                                                         uuids,
1846                                                         remote_dev_info->manufacturer_data_len,
1847                                                         manufacture_data);
1848                                 _bt_send_event(BT_ADAPTER_EVENT,
1849                                         BLUETOOTH_EVENT_SERVICE_SEARCHED,
1850                                         param);
1851                         }
1852
1853                         _bt_free_device_info(remote_dev_info);
1854                         g_free(address);
1855                 }
1856         } else if (strcasecmp(member, "DeviceConnected") == 0) {
1857                 unsigned char addr_type = 0;
1858
1859                 g_variant_get(msg, "(y)", &addr_type);
1860
1861                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1862
1863                 _bt_convert_device_path_to_address(path, address);
1864                 dev_name = _bt_get_device_name(address);
1865
1866                 _bt_convert_addr_string_to_secure_string(secure_address, address);
1867                 BT_INFO("Address : %s Type : %d", secure_address, addr_type);
1868                 BT_ERR_C("### Connected [%s] [%s]", !addr_type ? "BREDR" : "LE",
1869                                 !addr_type ? dev_name : secure_address);
1870                 g_free(dev_name);
1871
1872                 if (addr_type)
1873                         _bt_add_le_connected_dev_info(address);
1874
1875                 _bt_logging_connection(TRUE, addr_type);
1876                 param = g_variant_new("(isy)", result, address, addr_type);
1877                 /*Send event to application*/
1878                 _bt_send_event(BT_DEVICE_EVENT,
1879                                         BLUETOOTH_EVENT_DEVICE_CONNECTED,
1880                                         param);
1881                 g_free(address);
1882         } else if (strcasecmp(member, "Disconnected") == 0) {
1883                 unsigned char disc_reason = 0;
1884                 unsigned char addr_type = 0;
1885                 char *dev_name = NULL;
1886                 gboolean sending = FALSE;
1887
1888                 g_variant_get(msg, "(yys)", &addr_type, &disc_reason, &dev_name);
1889
1890                 result = disc_reason;
1891
1892                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1893
1894                 _bt_convert_device_path_to_address(path, address);
1895
1896                 /* 0x00 BDADDR_BRDER
1897                       0x01 BDADDR_LE_PUBLIC
1898                       0x02 BDADDR_LE_RANDOM */
1899                 _bt_convert_addr_string_to_secure_string(secure_address, address);
1900                 BT_INFO("Address : %s Type : %d", secure_address, addr_type);
1901                 BT_ERR_C("### Disconnected [%s] [%s] [%d : %s]", !addr_type ? "BREDR" : "LE",
1902                                 !addr_type ? dev_name : secure_address,
1903                                 disc_reason, _bt_convert_disc_reason_to_string(disc_reason));
1904                 g_free(dev_name);
1905
1906                 _bt_headset_set_local_connection(FALSE);
1907                 _bt_logging_connection(FALSE, addr_type);
1908
1909                 if (addr_type)
1910                         _bt_remove_le_connected_dev_info(address);
1911                 else {
1912 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1913                         {
1914                                 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1915
1916                                 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
1917                                         BT_ERR("vconf_get_int failed");
1918
1919                                 BT_INFO("conn_state[0x%x], adapter_state [%d]",
1920                                                         bt_device_state, _bt_adapter_get_status());
1921
1922                                 if (disc_reason == BLUETOOTH_ERROR_CONNECTION_TIMEOUT) {
1923                                         _bt_audio_start_auto_connect(TRUE);
1924                                 } else if (bt_device_state &
1925                                                         VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED) {
1926                                         BT_INFO("Disconnected due to turning BT off. Skip a address");
1927                                 } else {
1928                                         char *last_connected = NULL;
1929                                         last_connected = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1930                                         if (!g_strcmp0(address, last_connected))
1931                                                 _bt_audio_set_auto_connect_device_addr("");
1932                                         if (last_connected)
1933                                                 free(last_connected);
1934                                 }
1935                         }
1936
1937 #endif
1938                         /*Check for any OPP transfer on the device and cancel
1939                          * the transfer
1940                          */
1941                         _bt_obex_check_pending_transfer(address);
1942                         _bt_opp_client_is_sending(&sending);
1943                         if (sending == TRUE)
1944                                 _bt_opp_client_check_pending_transfer(address);
1945                 }
1946                 param = g_variant_new("(isy)", result, address, addr_type);
1947                 _bt_send_event(BT_DEVICE_EVENT,
1948                                         BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1949                                         param);
1950                 g_free(address);
1951         } else if (strcasecmp(member, "ProfileStateChanged") == 0) {
1952                 int state = 0;
1953                 char *profile_uuid = NULL;
1954                 bt_headset_wait_t *wait_list;
1955                 bluetooth_device_address_t bd_addr;
1956
1957                 g_variant_get(msg, "(si)", &profile_uuid, &state);
1958
1959                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1960
1961                 _bt_convert_device_path_to_address(path, address);
1962                 _bt_convert_addr_string_to_type(bd_addr.addr, address);
1963
1964                 _bt_convert_addr_string_to_secure_string(secure_address, address);
1965                 BT_INFO_C("[%s] state: %s, addr: %s",
1966                         _bt_convert_uuid_to_string(profile_uuid),
1967                         _bt_convert_profile_state_to_string(state), secure_address);
1968
1969                 if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0)  &&
1970                         (state == BT_PROFILE_STATE_CONNECTED)) {
1971
1972                         int event = BLUETOOTH_EVENT_AV_CONNECTED;
1973                         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1974                         bluetooth_device_address_t device_address;
1975                         gboolean connected;
1976                         bt_headset_wait_t *wait_list;
1977                         guint restricted = 0x0;
1978
1979                         __bt_set_device_values(TRUE,
1980                                 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1981
1982                         __bt_connection_manager_set_state(address, event);
1983
1984                         _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_HFP_HS, &restricted);
1985
1986                         if (_bt_headset_get_local_connection() == FALSE) {
1987                                 if (restricted == 0x0) /* not restricted*/
1988                                         _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
1989                         } else {
1990                                 /* Connection Started from local device therefore no need to
1991                                  * intiate connection for pending profile */
1992                                 _bt_headset_set_local_connection(FALSE);
1993                         }
1994                         param = g_variant_new("(is)", result, address);
1995                         _bt_send_event(BT_HEADSET_EVENT, event,
1996                                 param);
1997                         connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
1998                                                 connected_address);
1999                         if (connected) {
2000                                 if (g_strcmp0(connected_address, address) != 0) {
2001                                         _bt_convert_addr_string_to_type(
2002                                                 device_address.addr,
2003                                                 connected_address);
2004                                         _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2005                                                 &device_address, NULL);
2006                                 }
2007                         }
2008
2009                         _bt_add_headset_to_list(BT_AUDIO_A2DP,
2010                                                 BT_STATE_CONNECTED, address);
2011
2012                         wait_list = _bt_get_audio_wait_data();
2013                         if (wait_list != NULL &&
2014                                 (g_strcmp0(wait_list->address, address) == 0))
2015                                 _bt_rel_wait_data();
2016
2017                 } else if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0)  &&
2018                         (state == BT_PROFILE_STATE_DISCONNECTED)) {
2019
2020                         int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
2021
2022                         if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
2023                                 g_free(address);
2024                                 g_free(profile_uuid);
2025                                 return;
2026                         }
2027
2028                         __bt_set_device_values(FALSE,
2029                                 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2030
2031                         __bt_connection_manager_set_state(address, event);
2032                         param = g_variant_new("(is)", result, address);
2033                         _bt_send_event(BT_HEADSET_EVENT, event,
2034                                 param);
2035                         /* Remove data from the connected list */
2036                         _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2037                         wait_list = _bt_get_audio_wait_data();
2038
2039                         if (wait_list == NULL) {
2040                                 g_free(address);
2041                                 g_free(profile_uuid);
2042                                 return;
2043                         }
2044
2045                         if (((wait_list->type == BT_AUDIO_ALL) &&
2046                                 (wait_list->ag_flag == TRUE)) ||
2047                                 (wait_list->type == BT_AUDIO_A2DP) ||
2048                                 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2049                                 bluetooth_device_address_t device_address;
2050                                 _bt_convert_addr_string_to_type(
2051                                                         device_address.addr,
2052                                                         wait_list->address);
2053
2054                                 _bt_audio_connect(wait_list->req_id,
2055                                                         wait_list->type,
2056                                                         &device_address,
2057                                                         NULL);
2058                         }
2059                 } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
2060
2061                         if (state == BT_PROFILE_STATE_CONNECTED) {
2062                                 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
2063                                 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2064                                 bluetooth_device_address_t device_address;
2065                                 gboolean connected;
2066                                 param = g_variant_new("(is)", result, address);
2067                                 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
2068                                         param);
2069                                 connected = _bt_is_headset_type_connected(
2070                                                         BT_AVRCP,
2071                                                         connected_address);
2072                                 if (connected) {
2073                                         if (g_strcmp0(connected_address,
2074                                                                 address) != 0) {
2075                                                 _bt_convert_addr_string_to_type(
2076                                                         device_address.addr,
2077                                                         connected_address);
2078                                                 _bt_audio_disconnect(0,
2079                                                         BT_AVRCP,
2080                                                         &device_address, NULL);
2081                                         }
2082                                 }
2083                                 BT_DBG("device Path: %s", path);
2084                                 _bt_add_headset_to_list(BT_AVRCP,
2085                                                 BT_STATE_CONNECTED, address);
2086                         } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2087                                 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
2088                                 param = g_variant_new("(is)", result, address);
2089                                 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
2090                                         param);
2091                                 /* Remove data from the connected list */
2092                                 _bt_remove_headset_from_list(BT_AVRCP, address);
2093                                 }
2094                 } else if (strcasecmp(profile_uuid, A2DP_SOURCE_UUID) == 0) {
2095                         if (state == BT_PROFILE_STATE_CONNECTED) {
2096                                 int event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED;
2097                                 BT_INFO("A2DP Source is connected");
2098 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
2099                                 __bt_set_device_values(TRUE,
2100                                                 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED);
2101 #endif
2102
2103 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
2104                                 _bt_audio_set_auto_connect_device_addr(address);
2105                                 _bt_audio_stop_auto_connect();
2106 #endif
2107                                 _bt_add_headset_to_list(BT_AUDIO_A2DP_SOURCE,
2108                                                 BT_STATE_CONNECTED, address);
2109                                 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
2110                                         g_variant_new("(is)", result, address));
2111                         } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2112                                 int event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED;
2113                                 BT_INFO("A2DP Source Disconnected");
2114 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
2115                                 __bt_set_device_values(FALSE,
2116                                                 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED);
2117 #endif
2118                                 _bt_remove_headset_from_list(BT_AUDIO_A2DP_SOURCE, address);
2119                                 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
2120                                                 g_variant_new("(is)", result, address));
2121
2122                                 wait_list = _bt_get_audio_wait_data();
2123                                 if (wait_list && wait_list->type == BT_AUDIO_A2DP_SOURCE) {
2124                                         bluetooth_device_address_t device_address;
2125                                         _bt_convert_addr_string_to_type(
2126                                                         device_address.addr,
2127                                                         wait_list->address);
2128
2129                                         _bt_audio_connect(wait_list->req_id,
2130                                                         wait_list->type,
2131                                                         &device_address,
2132                                                         NULL);
2133                                         /* Now free the wait list */
2134                                         _bt_rel_wait_data();
2135                                 }
2136                         }
2137                 } else if ((strcmp(profile_uuid, HID_UUID) == 0) &&
2138                         ((state == BT_PROFILE_STATE_CONNECTED) ||
2139                                 (state == BT_PROFILE_STATE_DISCONNECTED))) {
2140                         int event;
2141                         if (state == BT_PROFILE_STATE_CONNECTED)
2142                                 event = BLUETOOTH_HID_CONNECTED;
2143                         else
2144                                 event = BLUETOOTH_HID_DISCONNECTED;
2145                         param = g_variant_new("(is)", result, address);
2146                         _bt_send_event(BT_HID_EVENT, event,
2147                                 param);
2148                 } else if (strcmp(profile_uuid, HID_DEVICE_UUID) == 0) {
2149                         if (state == BT_PROFILE_STATE_CONNECTED) {
2150                                 int event;
2151                                 event = BLUETOOTH_HID_DEVICE_CONNECTED;
2152                                 param = g_variant_new("(is)", result, address);
2153                                 _bt_send_event(BT_HID_DEVICE_EVENT, event,
2154                                         param);
2155                         } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2156                                 event = BLUETOOTH_HID_DEVICE_DISCONNECTED;
2157                                 param = g_variant_new("(is)", result, address);
2158                                 _bt_send_event(BT_HID_DEVICE_EVENT, event,
2159                                         param);
2160                         }
2161                 }
2162                 g_free(address);
2163                 g_free(profile_uuid);
2164         } else if (strcasecmp(member, "AdvReport") == 0) {
2165
2166                 bt_remote_le_dev_info_t *le_dev_info = NULL;
2167                 char *buffer = NULL;
2168                 int buffer_len = 0;
2169                 bt_le_adv_info_t *adv_info = NULL;
2170                 GVariant *value = NULL;
2171                 ret_if(_bt_is_le_scanning() == FALSE);
2172
2173                 le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
2174
2175                 g_variant_get(msg, "(&syyii@ay)", &le_dev_info->address,
2176                                                 &le_dev_info->addr_type,
2177                                                 &le_dev_info->adv_type,
2178                                                 &le_dev_info->rssi,
2179                                                 &le_dev_info->adv_data_len,
2180                                                 &value);
2181                 if (value == NULL) {
2182                         _bt_free_le_device_info(le_dev_info);
2183                         return;
2184                 }
2185
2186                 _bt_convert_device_path_to_address(path, le_dev_info->address);
2187
2188                 buffer_len = g_variant_get_size(value);
2189                 if (buffer_len > 0)
2190                         buffer = (char *)g_variant_get_data(value);
2191
2192                 le_dev_info->adv_data = g_memdup(buffer, buffer_len);
2193                 if (le_dev_info->adv_data == NULL &&
2194                         le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {
2195                         _bt_free_le_device_info(le_dev_info);
2196                         g_variant_unref(value);
2197                         return;
2198                 }
2199
2200                 if (_bt_get_le_scan_type() == BT_LE_PASSIVE_SCAN) {
2201                         _bt_send_scan_result_event(le_dev_info, NULL);
2202                         _bt_free_le_device_info(le_dev_info);
2203                         g_variant_unref(value);
2204                         return;
2205                 }
2206
2207                 if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {       /* ADV_IND */
2208                         adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
2209                         adv_info->addr = g_strdup(le_dev_info->address);
2210                         adv_info->addr_type = le_dev_info->addr_type;
2211                         adv_info->rssi = le_dev_info->rssi;
2212                         adv_info->data_len = le_dev_info->adv_data_len;
2213                         adv_info->data = g_malloc0(le_dev_info->adv_data_len);
2214                         memcpy(adv_info->data, le_dev_info->adv_data,
2215                                         le_dev_info->adv_data_len);
2216
2217                         if (__bt_add_adv_ind_info(adv_info) == 0) {
2218                                 adv_info->timer_id = g_timeout_add(1000,
2219                                         (GSourceFunc)__bt_adv_scan_req_timeout_cb, (void*)adv_info);
2220                         }
2221                 } else {     /* SCAN_RSP */
2222                         adv_info = __bt_get_adv_ind_info(le_dev_info->address);
2223                         if (adv_info) {
2224                                 _bt_send_scan_result_event(le_dev_info, adv_info);
2225                                 __bt_del_adv_ind_info(le_dev_info->address);
2226                         }
2227                 }
2228                 _bt_free_le_device_info(le_dev_info);
2229                 g_variant_unref(value);
2230         } else if  (strcasecmp(member, "LEDataLengthChanged") == 0) {
2231                 guint16 tx_octets = 0;
2232                 guint16 tx_time = 0;
2233                 guint16 rx_octets = 0;
2234                 guint16 rx_time = 0;
2235
2236                 g_variant_get(msg, "(qqqq)",
2237                                 &tx_octets, &tx_time, &rx_octets, &rx_time);
2238
2239                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2240                 _bt_convert_device_path_to_address(path, address);
2241
2242                 param = g_variant_new("(isqqqq)", result, address, tx_octets, tx_time,
2243                                 rx_octets, rx_time);
2244                 /* Send event to application */
2245                 _bt_send_event(BT_DEVICE_EVENT,
2246                                 BLUETOOTH_EVENT_LE_DATA_LENGTH_CHANGED, param);
2247                 g_free(address);
2248         } else if  (strcasecmp(member, "IpspStateChanged") == 0) {
2249                 gboolean connected = FALSE;
2250                 char *ifname = NULL;
2251
2252                 g_variant_get(msg, "(bs)", &connected, &ifname);
2253
2254                 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
2255                                                 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
2256
2257                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2258                 _bt_convert_device_path_to_address(path, address);
2259
2260                 BT_DBG("Ipsp BT Interface Name: %s", ifname);
2261                 BT_DBG("address: %s", address);
2262                 param = g_variant_new("(iss)", result, address, ifname);
2263
2264                 /* Send event to application */
2265                 _bt_send_event(BT_DEVICE_EVENT,
2266                                                 event,
2267                                                 param);
2268                 g_free(address);
2269         } else if (strcasecmp(member, "iBeaconReport") == 0) {
2270                 bt_remote_ibeacon_dev_info_t *ibeacon_dev_info = NULL;
2271
2272                 ret_if(_bt_is_le_scanning() == FALSE);
2273
2274                 ibeacon_dev_info = g_malloc0(sizeof(bt_remote_ibeacon_dev_info_t));
2275
2276                 g_variant_get(msg, "(syuusuuy)", &ibeacon_dev_info->address,
2277                                                 &ibeacon_dev_info->addr_type,
2278                                                 &ibeacon_dev_info->company_id,
2279                                                 &ibeacon_dev_info->ibeacon_type,
2280                                                 &ibeacon_dev_info->uuid,
2281                                                 &ibeacon_dev_info->major_id,
2282                                                 &ibeacon_dev_info->minor_id,
2283                                                 &ibeacon_dev_info->measured_power);
2284                 _bt_send_ibeacon_scan_result_event(ibeacon_dev_info);
2285                 g_free(ibeacon_dev_info);
2286         }
2287 }
2288
2289 void __bt_set_audio_values(gboolean connected, char *address)
2290 {
2291         char *name = NULL;
2292         int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
2293
2294         /*  Set the headset name */
2295         if (connected == TRUE)
2296                 name = _bt_get_device_name(address);
2297         else
2298                 name = g_strdup("");
2299
2300         if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME, name) != 0)
2301                 BT_ERR("vconf_set_str failed");
2302
2303         g_free(name);
2304
2305         /*  Set the headset state */
2306         if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
2307                 BT_ERR("vconf_get_int failed");
2308
2309 #ifdef TIZEN_SUPPORT_DUAL_HF
2310         if ((connected == TRUE) &&
2311                 (FALSE == __bt_is_companion_device(address))) {
2312                 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2313         } else if ((bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) &&
2314                         (FALSE == __bt_is_companion_device(address))) {
2315                 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2316         }
2317 #else
2318         if (connected == TRUE)
2319                 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2320         else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED)
2321                 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2322 #endif
2323
2324         if (vconf_set_int(VCONFKEY_BT_DEVICE,
2325                                 bt_device_state) != 0) {
2326                 BT_ERR("vconf_set_int failed");
2327         }
2328 }
2329
2330 void _bt_handle_headset_event(GVariant *msg, const char *path)
2331 {
2332         int result = BLUETOOTH_ERROR_NONE;
2333         gboolean property_flag = FALSE;
2334         char *property = NULL;
2335         GVariant *value = NULL;
2336         GVariant *param = NULL;
2337         g_variant_get(msg, "(sv)", &property, &value);
2338         bluetooth_device_address_t bd_addr;
2339
2340         ret_if(property == NULL);
2341
2342         BT_DBG("Property = %s \n", property);
2343         /* We allow only 1 headset connection (HSP or HFP)*/
2344         if (strcasecmp(property, "Connected") == 0) {
2345                 int event = BLUETOOTH_EVENT_NONE;
2346                 bt_headset_wait_t *wait_list;
2347                 char *address;
2348                 guint restricted = 0x0;
2349                 g_variant_get(value, "b", &property_flag);
2350
2351                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2352
2353                 _bt_convert_device_path_to_address(path, address);
2354                 _bt_convert_addr_string_to_type(bd_addr.addr, address);
2355
2356                 if (property_flag == TRUE) {
2357                         event = BLUETOOTH_EVENT_AG_CONNECTED;
2358                         _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_A2DP, &restricted);
2359
2360                         if (_bt_headset_get_local_connection() == FALSE) {
2361                                 if (restricted == 0x0) /* not restricted*/
2362                                         _bt_start_timer_for_connection(address, BT_AUDIO_A2DP);
2363                         } else
2364                                 _bt_headset_set_local_connection(FALSE);
2365                 } else {
2366                         int previous_state;
2367
2368                         event = BLUETOOTH_EVENT_AG_DISCONNECTED;
2369
2370                         previous_state = _bt_get_device_state_from_list(BT_AUDIO_HSP, address);
2371                         if (previous_state == BT_STATE_DISCONNECTING)
2372                                 _bt_send_hf_local_term_event(address);
2373                 }
2374                 /* Set the State machine here */
2375                 __bt_connection_manager_set_state(address, event);
2376                 __bt_set_audio_values(property_flag, address);
2377                 param = g_variant_new("(is)", result, address);
2378                 _bt_send_event(BT_HEADSET_EVENT, event,
2379                         param);
2380
2381                 if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
2382                         /* Remove data from the connected list */
2383                         _bt_remove_headset_from_list(BT_AUDIO_HSP, address);
2384
2385                         wait_list = _bt_get_audio_wait_data();
2386                         if (wait_list == NULL) {
2387                                 g_free(address);
2388                                 return;
2389                         }
2390
2391                         bluetooth_device_address_t device_address;
2392
2393                         _bt_set_audio_wait_data_flag(TRUE);
2394
2395                         _bt_convert_addr_string_to_type(device_address.addr,
2396                                                         wait_list->address);
2397                         _bt_audio_connect(wait_list->req_id, wait_list->type,
2398                                         &device_address, NULL);
2399                         _bt_rel_wait_data();
2400                 } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
2401                         /* Add data to the connected list */
2402                         _bt_add_headset_to_list(BT_AUDIO_HSP,
2403                                                 BT_STATE_CONNECTED, address);
2404
2405                         wait_list = _bt_get_audio_wait_data();
2406                         if (wait_list != NULL &&
2407                                 (g_strcmp0(wait_list->address, address) == 0))
2408                         _bt_rel_wait_data();
2409
2410                         BT_INFO("Check A2DP pending connect");
2411                         _bt_audio_check_pending_connect();
2412                 }
2413                 g_free(address);
2414         } else if (strcasecmp(property, "State") == 0) {
2415                 char *state = NULL;
2416
2417                 g_variant_get(value, "s", &state);
2418
2419                 /* This code assumes we support only 1 headset connection */
2420                 /* Need to use the headset list, if we support multi-headsets */
2421                 if (strcasecmp(state, "Playing") == 0) {
2422                         BT_DBG("Playing: Sco Connected");
2423                 } else if (strcasecmp(state, "connected") == 0 ||
2424                                 strcasecmp(state, "disconnected") == 0) {
2425                         BT_DBG("connected/disconnected: Sco Disconnected");
2426                 } else {
2427                         BT_ERR("Not handled state - %s", state);
2428                         g_free(state);
2429                         return;
2430                 }
2431                 g_free(state);
2432         } else if (strcasecmp(property, "SpeakerGain") == 0) {
2433                 guint16 spkr_gain;
2434                 char *address;
2435
2436                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2437
2438                 _bt_convert_device_path_to_address(path, address);
2439
2440                 spkr_gain = g_variant_get_uint16(value);
2441
2442                 BT_DBG("spkr_gain: %d", spkr_gain);
2443
2444                 param = g_variant_new("(i&sq)", result, address, spkr_gain);
2445                 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
2446                         param);
2447
2448                 g_free(address);
2449         } else if (strcasecmp(property, "MicrophoneGain") == 0) {
2450                 guint16 mic_gain;
2451                 char *address;
2452
2453                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2454
2455                 _bt_convert_device_path_to_address(path, address);
2456
2457                 mic_gain = g_variant_get_uint16(value);
2458
2459                 param = g_variant_new("(i&sq)", result, address, mic_gain);
2460                 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_MIC_GAIN,
2461                         param);
2462                 g_free(address);
2463         }
2464
2465         if (property)
2466                 g_free(property);
2467         g_variant_unref(value);
2468 }
2469
2470 void _bt_handle_sink_event(GVariant *msg, const char *path)
2471 {
2472         GVariantIter value_iter;
2473         char *property = NULL;
2474
2475         bt_headset_wait_t *wait_list;
2476
2477         GVariant *child = NULL;
2478         GVariant *val = NULL;
2479         GVariant *param = NULL;
2480         g_variant_iter_init(&value_iter, msg);
2481         while ((child = g_variant_iter_next_value(&value_iter))) {
2482
2483                 g_variant_get(child, "{sv}", &property, &val);
2484
2485                 ret_if(property == NULL);
2486
2487                 BT_DBG("Property = %s \n", property);
2488
2489
2490                 if (strcasecmp(property, "State") == 0) {
2491                         int result = BLUETOOTH_ERROR_NONE;
2492                         char *value;
2493
2494                         g_variant_get(val, "s", &value);
2495                         BT_DBG("value: %s", value);
2496
2497                         if (g_strcmp0(value, "disconnected") == 0) {
2498                                 char *address;
2499
2500                                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2501
2502                                 _bt_convert_device_path_to_address(path, address);
2503
2504                                 __bt_set_device_values(FALSE,
2505                                         VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2506                                 param = g_variant_new("(is)", result, address);
2507                                 _bt_send_event(BT_HEADSET_EVENT,
2508                                         BLUETOOTH_EVENT_AV_DISCONNECTED,
2509                                         param);
2510
2511                                 /* Remove data from the connected list */
2512                                 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2513                                 wait_list = _bt_get_audio_wait_data();
2514                                 if (wait_list == NULL) {
2515                                         g_free(value);
2516                                         g_free(property);
2517                                         g_variant_unref(val);
2518                                         g_variant_unref(child);
2519                                         g_free(address);
2520                                         return;
2521                                 }
2522
2523                                 if (((wait_list->type == BT_AUDIO_ALL) &&
2524                                         (wait_list->ag_flag == TRUE)) ||
2525                                         (wait_list->type == BT_AUDIO_A2DP) ||
2526                                         (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2527                                         bluetooth_device_address_t device_address;
2528                                         _bt_convert_addr_string_to_type(
2529                                                                 device_address.addr,
2530                                                                 wait_list->address);
2531
2532                                         _bt_audio_connect(wait_list->req_id,
2533                                                                 wait_list->type,
2534                                                                 &device_address,
2535                                                                 NULL);
2536                                 }
2537                                 g_free(address);
2538                         } else if (strcasecmp(value, "Connected") == 0) {
2539                                 char *address;
2540                                 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2541                                 bluetooth_device_address_t device_address;
2542                                 gboolean connected;
2543
2544                                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2545
2546                                 _bt_convert_device_path_to_address(path, address);
2547
2548                                 __bt_set_device_values(TRUE,
2549                                                 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2550                                 param = g_variant_new("(is)", result, address);
2551                                 _bt_send_event(BT_HEADSET_EVENT,
2552                                         BLUETOOTH_EVENT_AV_CONNECTED,
2553                                         param);
2554                                 /* Check for existing Media device to disconnect */
2555                                 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
2556                                                                         connected_address);
2557                                 if (connected) {
2558                                         /* Match connected device address */
2559                                         if (g_strcmp0(connected_address, address) != 0) {
2560                                                 /* Convert BD adress from string type */
2561                                                 _bt_convert_addr_string_to_type(
2562                                                                 device_address.addr,
2563                                                                 connected_address);
2564                                                 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2565                                                                 &device_address, NULL);
2566                                         }
2567                                 }
2568
2569                                 /* Add data to the connected list */
2570                                 _bt_add_headset_to_list(BT_AUDIO_A2DP,
2571                                                 BT_STATE_CONNECTED, address);
2572
2573                                 g_free(address);
2574                         }
2575                         g_free(value);
2576                 }
2577                 g_free(property);
2578                 g_variant_unref(val);
2579                 g_variant_unref(child);
2580         }
2581 }
2582
2583 void _bt_handle_agent_event(GVariant *msg, const char *member)
2584 {
2585         int result = BLUETOOTH_ERROR_NONE;
2586         char *address = NULL;
2587         char *name = NULL;
2588         char *uuid = NULL;
2589         GVariant *param = NULL;
2590         ret_if(member == NULL);
2591
2592         if (strcasecmp(member, "ObexAuthorize") == 0) {
2593                 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2594                 param = g_variant_new("(iss)", result, address, name);
2595                 _bt_send_event(BT_OPP_SERVER_EVENT,
2596                         BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
2597                         param);
2598                 g_free(address);
2599                 g_free(name);
2600         } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2601                 bt_rfcomm_server_info_t *server_info;
2602
2603                 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2604
2605                 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2606                 ret_if(server_info == NULL);
2607                 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2608                 param = g_variant_new("(isssn)", result, address, uuid, name,
2609                                         server_info->control_fd);
2610                 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2611                         BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2612                         param);
2613                 g_free(address);
2614                 g_free(uuid);
2615                 g_free(name);
2616         }
2617 }
2618
2619 static int __bt_get_object_path(GVariant *msg, char **path)
2620 {
2621         g_variant_get(msg, "(o*)", path, NULL);
2622         if (*path == NULL)
2623                 return BLUETOOTH_ERROR_INTERNAL;
2624
2625         return BLUETOOTH_ERROR_NONE;
2626 }
2627
2628 static void __bt_devices_list_free(void)
2629 {
2630         bt_cache_info_t *cache_info;
2631         GList *node;
2632
2633         node = g_list_first(p_cache_list);
2634
2635         while (node != NULL) {
2636                 cache_info = (bt_cache_info_t *)node->data;
2637                 p_cache_list = g_list_remove(p_cache_list, cache_info);
2638                 __bt_free_cache_info(cache_info);
2639
2640                 node = g_list_next(node);
2641         }
2642 }
2643
2644 static int __bt_parse_event(GVariant *msg)
2645 {
2646         GVariantIter iter;
2647         GVariant *child;
2648         char *interface_name = NULL;
2649         GVariant *inner_iter = NULL;
2650
2651         g_variant_iter_init(&iter, msg);
2652
2653         while ((child = g_variant_iter_next_value(&iter))) {
2654                 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
2655                 if (g_strcmp0(interface_name,
2656                                 BT_DEVICE_INTERFACE) == 0) {
2657                         g_variant_unref(inner_iter);
2658                         g_variant_unref(child);
2659                         return BT_DEVICE_EVENT;
2660                 } else if (g_strcmp0(interface_name,
2661                                 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2662                         g_variant_unref(inner_iter);
2663                         g_variant_unref(child);
2664                         return BT_MEDIA_TRANSFER_EVENT;
2665                 } else if (g_strcmp0(interface_name,
2666                                 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2667                         g_variant_unref(inner_iter);
2668                         g_variant_unref(child);
2669                         return BT_AVRCP_CONTROL_EVENT;
2670                 }
2671                 g_variant_unref(inner_iter);
2672                 g_variant_unref(child);
2673         }
2674
2675         return 0;
2676 }
2677
2678 static  void __bt_manager_event_filter(GDBusConnection *connection,
2679                                         const gchar *sender_name,
2680                                         const gchar *object_path,
2681                                         const gchar *interface_name,
2682                                         const gchar *signal_name,
2683                                         GVariant *parameters,
2684                                         gpointer user_data)
2685 {
2686         bt_event_type_t bt_event = 0x00;
2687         int result = BLUETOOTH_ERROR_NONE;
2688         GVariant *value;
2689         char *obj_path = NULL;
2690         GVariant *param = NULL;
2691         if (signal_name == NULL)
2692                 return;
2693         if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2694                 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2695
2696                 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
2697 #ifdef USB_BLUETOOTH
2698                         BT_DBG("Enable Adapter");
2699                         _bt_enable_adapter();
2700 #else
2701                         _bt_handle_adapter_added();
2702 #endif
2703                 } else {
2704                         bt_event = __bt_parse_event(value);
2705                         if (bt_event == BT_DEVICE_EVENT) {
2706                                 bt_cache_info_t *cache_info;
2707                                 bt_remote_dev_info_t *dev_info;
2708
2709                                 if (_bt_is_discovering() == FALSE &&
2710                                     _bt_is_le_scanning() == FALSE) {
2711                                         g_variant_unref(value);
2712                                         return;
2713                                 }
2714
2715                                 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2716
2717                                 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2718
2719                                 cache_info->dev_info = dev_info;
2720
2721                                 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2722                                         BT_ERR("Fail to parse the properies");
2723                                         __bt_free_cache_info(cache_info);
2724                                         g_variant_unref(value);
2725                                         return;
2726                                 }
2727
2728                                 if (dev_info->addr_type != BDADDR_BREDR) {
2729                                         /* Whenever emit the property changed from bluez,
2730                                                 some property doesn't reach to bt-service.
2731                                                 So LE device is handled as AdvReport signal */
2732                                         __bt_free_cache_info(cache_info);
2733                                         g_variant_unref(value);
2734                                         return;
2735                                 }
2736
2737                                 if (dev_info->name == NULL)
2738                                         /* If Remote device name is NULL or still RNR is not done
2739                                          * then display address as name.
2740                                          */
2741                                         dev_info->name = g_strdup(dev_info->address);
2742
2743 #ifdef TIZEN_DPM_ENABLE
2744                                 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
2745                                                         DPM_RESTRICTED) {
2746                                         bluetooth_device_class_t device_class;
2747                                         _bt_divide_device_class(&device_class, dev_info->class);
2748                                         BT_DBG("[%s]device_class.major_class : %d", dev_info->name, device_class.major_class);
2749
2750                                         if (device_class.major_class ==
2751                                                 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
2752                                                 __bt_free_cache_info(cache_info);
2753                                                 g_variant_unref(value);
2754                                                 return;
2755                                         }
2756                                 }
2757 #endif
2758
2759                                 GVariant *uuids = NULL;
2760                                 GVariantBuilder *builder = NULL;
2761                                 int i = 0;
2762                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2763                                 for (i = 0; i < dev_info->uuid_count; i++) {
2764                                         g_variant_builder_add(builder, "s",
2765                                                 dev_info->uuids[i]);
2766                                 }
2767                                 uuids = g_variant_new("as", builder);
2768                                 g_variant_builder_unref(builder);
2769                                 GVariant *manufacturer_data = NULL;
2770                                 manufacturer_data = g_variant_new_from_data(
2771                                                         G_VARIANT_TYPE_BYTESTRING,
2772                                                         dev_info->manufacturer_data,
2773                                                         dev_info->manufacturer_data_len,
2774                                                         TRUE, NULL, NULL);
2775                                 param = g_variant_new("(isunsbub@asn@ay)", result,
2776                                                         dev_info->address,
2777                                                         dev_info->class,
2778                                                         dev_info->rssi,
2779                                                         dev_info->name,
2780                                                         dev_info->paired,
2781                                                         dev_info->connected,
2782                                                         dev_info->trust,
2783                                                         uuids,
2784                                                         dev_info->manufacturer_data_len,
2785                                                         manufacturer_data);
2786                                 _bt_send_event(BT_ADAPTER_EVENT,
2787                                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2788                                          param);
2789                                 p_cache_list = g_list_append(p_cache_list, cache_info);
2790                         } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2791                                 BT_DBG("Device path : %s ", obj_path);
2792                                 _bt_set_control_device_path(obj_path);
2793                         }
2794                 }
2795                 g_variant_unref(value);
2796         } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2797 #ifdef USB_BLUETOOTH
2798                 BT_DBG("InterfacesRemoved");
2799                 _bt_handle_adapter_removed();
2800 #endif
2801                 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0)
2802                         bt_event = BT_MEDIA_TRANSFER_EVENT;
2803                 else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0)
2804                         bt_event = BT_DEVICE_EVENT;
2805                 else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0)
2806                         bt_event = BT_AVRCP_CONTROL_EVENT;
2807
2808                 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2809                         _bt_handle_adapter_event(parameters, signal_name);
2810                         if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2811                                 BT_INFO("Object Path %s", obj_path);
2812                                 _bt_remove_control_device_path(obj_path);
2813                         }
2814                 }
2815         } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2816                 gboolean value;
2817                 char *name = NULL;
2818                 char *previous = NULL;
2819                 char *current = NULL;
2820
2821                 if (__bt_get_owner_info(parameters, &name, &previous, &current)) {
2822                         BT_ERR("Fail to get the owner info");
2823                         return;
2824                 }
2825
2826                 if (*current != '\0') {
2827                         g_free(current);
2828                         if (name)
2829                                 g_free(name);
2830                         if (previous)
2831                                 g_free(previous);
2832                         return;
2833                 }
2834
2835                 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2836                         BT_INFO_C("### Bluetoothd is terminated");
2837                         if (_bt_adapter_get_status() == BT_ACTIVATED)
2838                                  _bt_disable_cb();
2839
2840                         _bt_handle_adapter_removed();
2841                         __bt_devices_list_free();
2842                         _bt_service_adapter_le_deinit();
2843                 }
2844
2845                 _bt_obex_server_check_allocation(&value);
2846
2847                 if (value == TRUE) {
2848                         /* Check if the obex server was terminated abnormally */
2849                         _bt_obex_server_check_termination(name);
2850                 }
2851
2852                 _bt_rfcomm_server_check_existence(&value);
2853
2854                 if (value == TRUE) {
2855                         /* The obex server was terminated abnormally */
2856                         _bt_rfcomm_server_check_termination(name);
2857                 }
2858
2859                 /* Reset connection interval */
2860                 _bt_remove_all_le_conn_param_info(name);
2861
2862                 /* Stop advertising started by terminated process */
2863                 _bt_stop_advertising_by_terminated_process(name);
2864
2865                 /* Stop LE Scan */
2866                 _bt_stop_le_scan(name);
2867
2868                 g_free(name);
2869                 g_free(previous);
2870                 g_free(current);
2871         } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2872                 const char *path = object_path;
2873
2874                 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2875                                 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2876                         return;
2877
2878                 _bt_handle_property_changed_event(parameters, object_path);
2879         } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2880                 _bt_handle_adapter_event(parameters, signal_name);
2881         } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2882                 _bt_handle_input_event(parameters, object_path);
2883         } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2884                 _bt_handle_network_server_event(parameters, signal_name);
2885         } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2886                 _bt_handle_headset_event(parameters, object_path);
2887         } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2888                 _bt_handle_sink_event(parameters, object_path);
2889         } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2890                 _bt_handle_agent_event(parameters, signal_name);
2891         } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2892                 _bt_handle_device_event(parameters, signal_name, object_path);
2893         } else if (g_strcmp0(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
2894                 _bt_handle_gatt_event(parameters, signal_name, object_path);
2895         }
2896
2897         return;
2898 }
2899
2900 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2901 {
2902
2903         if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2904                 char *interface_name = NULL;
2905
2906                 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2907                 retv_if(interface_name == NULL, FALSE);
2908
2909                 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2910                         BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2911                         return TRUE;
2912                 }
2913         }
2914
2915         return FALSE;
2916 }
2917
2918 static  void __bt_obexd_event_filter(GDBusConnection *connection,
2919                                         const gchar *sender_name,
2920                                         const gchar *object_path,
2921                                         const gchar *interface_name,
2922                                         const gchar *signal_name,
2923                                         GVariant *parameters,
2924                                         gpointer user_data)
2925 {
2926         const char *member = signal_name;
2927         char *obj_path = NULL;
2928         ret_if(member == NULL);
2929
2930         if (strcasecmp(member, "InterfacesAdded") == 0) {
2931                 if (__bt_get_object_path(parameters, &obj_path)) {
2932                         BT_ERR("Fail to get the path");
2933                         return;
2934                 }
2935                 BT_INFO("object_path = [%s]", obj_path);
2936
2937                 /*Handle OPP_SERVER_CONNECTED_EVENT here */
2938                 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2939                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2940                         g_free(obj_path);
2941                         return;
2942                 }
2943
2944                 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2945                         BT_DBG("Obex_Server_Session_Transfer connected");
2946                         _bt_obex_transfer_connected(obj_path);
2947                 }
2948                 g_free(obj_path);
2949         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2950                 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
2951                 if (__bt_get_object_path(parameters, &obj_path)) {
2952                         BT_ERR("Fail to get the path");
2953                         return;
2954                 }
2955                 BT_INFO("object_path = [%s]", obj_path);
2956
2957                 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2958                                 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2959                         BT_DBG("Call PBAP Disconnected");
2960                         _bt_obex_pbap_client_disconnect(obj_path);
2961                 }
2962
2963                 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2964                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2965                         g_free(obj_path);
2966                         return;
2967                 }
2968
2969                 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2970                         BT_DBG("Obex_Server_Session_Transfer disconnected %s",
2971                                                                 obj_path);
2972
2973                         _bt_obex_transfer_disconnected(obj_path);
2974                 }
2975                 g_free(obj_path);
2976         } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
2977                 const char *path = object_path;
2978
2979                 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
2980                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
2981                         strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2982                                 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2983                         BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
2984                         return;
2985                 }
2986
2987                 _bt_handle_property_changed_event(parameters, path);
2988         }
2989         BT_DBG("-");
2990         return;
2991 }
2992
2993 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
2994 {
2995         BT_DBG("+");
2996
2997         if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2998                 char *interface_name = NULL;
2999
3000                 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
3001
3002                 retv_if(interface_name == NULL, FALSE);
3003
3004                 if (strcasecmp(interface_name,
3005                                         BT_OBEX_TRANSFER_INTERFACE) == 0) {
3006                         BT_DBG("-");
3007                         return TRUE;
3008                 }
3009         }
3010
3011         BT_DBG("-");
3012
3013         return FALSE;
3014 }
3015
3016 static  void __bt_opc_event_filter(GDBusConnection *connection,
3017                                         const gchar *sender_name,
3018                                         const gchar *object_path,
3019                                         const gchar *interface_name,
3020                                         const gchar *signal_name,
3021                                         GVariant *parameters,
3022                                         gpointer user_data)
3023 {
3024         const char *member = signal_name;
3025         char *obj_path = NULL;
3026         if (strcasecmp(member, "InterfacesAdded") == 0) {
3027                 BT_DBG("InterfacesAdded");
3028         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
3029
3030                 if (__bt_get_object_path(parameters, &obj_path)) {
3031                         BT_ERR("Fail to get the path");
3032                         return;
3033                 }
3034
3035                 BT_DBG("object_path = %s", obj_path);
3036
3037                 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
3038                                 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
3039                                 || strstr(obj_path, "transfer") == NULL) {
3040                         g_free(obj_path);
3041                         return;
3042                 } else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
3043                                 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
3044                         BT_DBG("Going to call opc disconnected");
3045                         _bt_opc_disconnected(obj_path);
3046                 }
3047
3048                 _bt_sending_files();
3049                 g_free(obj_path);
3050         } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
3051                 char *path = (char *)object_path;
3052                 BT_INFO("object_path %s", path);
3053                 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
3054                         strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
3055                         BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
3056                         return;
3057                 }
3058
3059                 _bt_opc_property_changed_event(parameters, path);
3060         }
3061
3062         return;
3063 }
3064
3065 int _bt_opp_client_event_init(void)
3066 {
3067         GError *error = NULL;
3068
3069         if (opc_obexd_conn == NULL) {
3070                 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3071
3072                 if (!opc_obexd_conn) {
3073                         if (error) {
3074                                 BT_ERR("Unable to connect to dbus: %s", error->message);
3075                                 g_clear_error(&error);
3076                         }
3077                 return BLUETOOTH_ERROR_INTERNAL;
3078                 }
3079         }
3080
3081         if (_bt_register_service_event(opc_obexd_conn,
3082                         BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
3083                         g_object_unref(opc_obexd_conn);
3084                         opc_obexd_conn = NULL;
3085                         return BLUETOOTH_ERROR_INTERNAL;
3086         }
3087
3088         return BLUETOOTH_ERROR_NONE;
3089 }
3090
3091 void _bt_opp_client_event_deinit(void)
3092 {
3093         if (opc_obexd_conn) {
3094                 _bt_unregister_service_event(opc_obexd_conn,
3095                                                 BT_OPP_CLIENT_EVENT);
3096                  g_object_unref(opc_obexd_conn);
3097                  opc_obexd_conn = NULL;
3098         }
3099 }
3100
3101 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
3102                 int subscribe)
3103 {
3104         if (conn == NULL)
3105                 return -1;
3106
3107         static int subs_interface_added_id = -1;
3108         static int subs_interface_removed_id = -1;
3109         static int subs_name_owner_id = -1;
3110         static int subs_property_id = -1;
3111         static int subs_adapter_id = -1;
3112         static int subs_gatt_id = -1;
3113
3114         if (subscribe) {
3115                 if (subs_interface_added_id == -1) {
3116                         subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3117                                 NULL, BT_MANAGER_INTERFACE,
3118                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3119                                 __bt_manager_event_filter,
3120                                 NULL, NULL);
3121                 }
3122                 if (subs_interface_removed_id == -1) {
3123                         subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3124                                 NULL, BT_MANAGER_INTERFACE,
3125                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3126                                 __bt_manager_event_filter,
3127                                 NULL, NULL);
3128                 }
3129                 if (subs_name_owner_id == -1) {
3130                         subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
3131                                 NULL, BT_FREEDESKTOP_INTERFACE,
3132                                 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
3133                                 __bt_manager_event_filter,
3134                                 NULL, NULL);
3135                 }
3136                 if (subs_property_id == -1) {
3137                         subs_property_id = g_dbus_connection_signal_subscribe(conn,
3138                                 NULL, BT_PROPERTIES_INTERFACE,
3139                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3140                                 __bt_manager_event_filter,
3141                                 NULL, NULL);
3142                 }
3143                 if (subs_adapter_id == -1) {
3144                         subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
3145                                 NULL, BT_ADAPTER_INTERFACE,
3146                                 NULL, NULL, NULL, 0,
3147                                 __bt_manager_event_filter,
3148                                 NULL, NULL);
3149                 }
3150                 if (subs_gatt_id == -1) {
3151                         subs_gatt_id = g_dbus_connection_signal_subscribe(conn,
3152                                 NULL, BT_GATT_CHAR_INTERFACE,
3153                                 NULL, NULL, NULL, 0,
3154                                 __bt_manager_event_filter,
3155                                 NULL, NULL);
3156                 }
3157         } else {
3158                 if (subs_interface_added_id != -1) {
3159                         g_dbus_connection_signal_unsubscribe(conn,
3160                                         subs_interface_added_id);
3161                         subs_interface_added_id = -1;
3162                 }
3163                 if (subs_interface_removed_id != -1) {
3164                         g_dbus_connection_signal_unsubscribe(conn,
3165                                         subs_interface_removed_id);
3166                         subs_interface_removed_id = -1;
3167                 }
3168                 if (subs_name_owner_id != -1) {
3169                         g_dbus_connection_signal_unsubscribe(conn,
3170                                         subs_name_owner_id);
3171                         subs_name_owner_id = -1;
3172                 }
3173                 if (subs_property_id != -1) {
3174                         g_dbus_connection_signal_unsubscribe(conn,
3175                                         subs_property_id);
3176                         subs_property_id = -1;
3177                 }
3178                 if (subs_adapter_id != -1) {
3179                         g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
3180                         subs_adapter_id = -1;
3181                 }
3182                 if (subs_gatt_id != -1) {
3183                         g_dbus_connection_signal_unsubscribe(conn, subs_gatt_id);
3184                         subs_gatt_id = -1;
3185                 }
3186         }
3187         return 0;
3188 }
3189
3190 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
3191                 int subscribe)
3192 {
3193         if (conn == NULL)
3194                 return -1;
3195
3196         static int subs_device_id = -1;
3197
3198         if (subscribe) {
3199                 if (subs_device_id == -1) {
3200                         subs_device_id = g_dbus_connection_signal_subscribe(conn,
3201                                 NULL, BT_DEVICE_INTERFACE,
3202                                 NULL, NULL, NULL, 0,
3203                                 __bt_manager_event_filter,
3204                                 NULL, NULL);
3205                 }
3206         } else {
3207                 if (subs_device_id != -1) {
3208                         g_dbus_connection_signal_unsubscribe(conn,
3209                                         subs_device_id);
3210                         subs_device_id = -1;
3211                 }
3212         }
3213         return 0;
3214 }
3215
3216 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
3217                 int subscribe)
3218 {
3219         if (conn == NULL)
3220                 return -1;
3221
3222         static int subs_input_id = -1;
3223
3224         if (subscribe) {
3225                 if (subs_input_id == -1) {
3226                         subs_input_id = g_dbus_connection_signal_subscribe(conn,
3227                                 NULL, BT_INPUT_INTERFACE,
3228                                 NULL, NULL, NULL, 0,
3229                                 __bt_manager_event_filter,
3230                                 NULL, NULL);
3231                 }
3232         } else {
3233                 if (subs_input_id != -1) {
3234                         g_dbus_connection_signal_unsubscribe(conn,
3235                                         subs_input_id);
3236                         subs_input_id = -1;
3237                 }
3238         }
3239         return 0;
3240 }
3241
3242 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
3243                 int subscribe)
3244 {
3245         if (conn == NULL)
3246                 return -1;
3247
3248         static int subs_serv_id = -1;
3249         static int subs_client_id = -1;
3250
3251         if (subscribe) {
3252                 if (subs_serv_id == -1) {
3253                         subs_serv_id = g_dbus_connection_signal_subscribe(conn,
3254                                 NULL, BT_NETWORK_SERVER_INTERFACE,
3255                                 NULL, NULL, NULL, 0,
3256                                 __bt_manager_event_filter,
3257                                 NULL, NULL);
3258                 }
3259                 if (subs_client_id == -1) {
3260                         subs_client_id = g_dbus_connection_signal_subscribe(conn,
3261                                 NULL, BT_NETWORK_CLIENT_INTERFACE,
3262                                 NULL, NULL, NULL, 0,
3263                                 __bt_manager_event_filter,
3264                                 NULL, NULL);
3265                 }
3266         } else {
3267                 if (subs_serv_id != -1) {
3268                         g_dbus_connection_signal_unsubscribe(conn,
3269                                         subs_serv_id);
3270                         subs_serv_id = -1;
3271                 }
3272                 if (subs_client_id != -1) {
3273                         g_dbus_connection_signal_unsubscribe(conn,
3274                                         subs_client_id);
3275                         subs_client_id = -1;
3276                 }
3277         }
3278         return 0;
3279 }
3280
3281 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
3282                 int subscribe)
3283 {
3284         if (conn == NULL)
3285                 return -1;
3286
3287         static int subs_headset_id = -1;
3288         static int subs_sink_id = -1;
3289
3290         if (subscribe) {
3291                 if (subs_headset_id == -1) {
3292                         subs_headset_id = g_dbus_connection_signal_subscribe(conn,
3293                                 NULL, BT_HEADSET_INTERFACE,
3294                                 NULL, NULL, NULL, 0,
3295                                 __bt_manager_event_filter,
3296                                 NULL, NULL);
3297                 }
3298                 if (subs_sink_id == -1) {
3299                         subs_sink_id = g_dbus_connection_signal_subscribe(conn,
3300                                 NULL, BT_SINK_INTERFACE,
3301                                 NULL, NULL, NULL, 0,
3302                                 __bt_manager_event_filter,
3303                                 NULL, NULL);
3304                 }
3305         } else {
3306                 if (subs_headset_id != -1) {
3307                         g_dbus_connection_signal_unsubscribe(conn,
3308                                         subs_headset_id);
3309                         subs_headset_id = -1;
3310                 }
3311                 if (subs_sink_id != -1) {
3312                         g_dbus_connection_signal_unsubscribe(conn,
3313                                         subs_sink_id);
3314                         subs_sink_id = -1;
3315                 }
3316         }
3317         return 0;
3318 }
3319
3320 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
3321                 int subscribe)
3322 {
3323         if (conn == NULL)
3324                 return -1;
3325
3326         static int subs_opp_server_interface_added_id = -1;
3327         static int subs_opp_server_interface_removed_id = -1;
3328         static int subs_opp_server_property_id = -1;
3329
3330
3331         if (subscribe) {
3332                 if (subs_opp_server_interface_added_id == -1) {
3333                         subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3334                                 NULL, BT_MANAGER_INTERFACE,
3335                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3336                                 __bt_obexd_event_filter,
3337                                 NULL, NULL);
3338                 }
3339                 if (subs_opp_server_interface_removed_id == -1) {
3340                         subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3341                                 NULL, BT_MANAGER_INTERFACE,
3342                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3343                                 __bt_obexd_event_filter,
3344                                 NULL, NULL);
3345                 }
3346                 if (subs_opp_server_property_id == -1) {
3347                         subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
3348                                 NULL, BT_PROPERTIES_INTERFACE,
3349                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3350                                 __bt_obexd_event_filter,
3351                                 NULL, NULL);
3352                 }
3353         } else {
3354                 if (subs_opp_server_interface_added_id != -1) {
3355                         g_dbus_connection_signal_unsubscribe(conn,
3356                                         subs_opp_server_interface_added_id);
3357                         subs_opp_server_interface_added_id = -1;
3358                 }
3359                 if (subs_opp_server_interface_removed_id != -1) {
3360                         g_dbus_connection_signal_unsubscribe(conn,
3361                                         subs_opp_server_interface_removed_id);
3362                         subs_opp_server_interface_removed_id = -1;
3363                 }
3364                 if (subs_opp_server_property_id != -1) {
3365                         g_dbus_connection_signal_unsubscribe(conn,
3366                                         subs_opp_server_property_id);
3367                         subs_opp_server_property_id = -1;
3368                 }
3369         }
3370         return 0;
3371 }
3372
3373 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
3374                 int subscribe)
3375 {
3376         if (conn == NULL)
3377                 return -1;
3378
3379         static int subs_opp_client_interface_added_id = -1;
3380         static int subs_opp_client_interface_removed_id = -1;
3381         static int subs_opp_client_property_id = -1;
3382
3383
3384         if (subscribe) {
3385                 if (subs_opp_client_interface_added_id == -1) {
3386                         subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3387                                 NULL, BT_MANAGER_INTERFACE,
3388                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3389                                 __bt_opc_event_filter,
3390                                 NULL, NULL);
3391                 }
3392                 if (subs_opp_client_interface_removed_id == -1) {
3393                         subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3394                                 NULL, BT_MANAGER_INTERFACE,
3395                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3396                                 __bt_opc_event_filter,
3397                                 NULL, NULL);
3398                 }
3399                 if (subs_opp_client_property_id == -1) {
3400                         subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
3401                                 NULL, BT_PROPERTIES_INTERFACE,
3402                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3403                                 __bt_opc_event_filter,
3404                                 NULL, NULL);
3405                 }
3406         } else {
3407                 if (subs_opp_client_interface_added_id != -1) {
3408                         g_dbus_connection_signal_unsubscribe(conn,
3409                                         subs_opp_client_interface_added_id);
3410                         subs_opp_client_interface_added_id = -1;
3411                 }
3412                 if (subs_opp_client_interface_removed_id != -1) {
3413                         g_dbus_connection_signal_unsubscribe(conn,
3414                                         subs_opp_client_interface_removed_id);
3415                         subs_opp_client_interface_removed_id = -1;
3416                 }
3417                 if (subs_opp_client_property_id != -1) {
3418                         g_dbus_connection_signal_unsubscribe(conn,
3419                                         subs_opp_client_property_id);
3420                         subs_opp_client_property_id = -1;
3421                 }
3422         }
3423         return 0;
3424 }
3425
3426 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
3427                 int subscribe)
3428 {
3429         if (conn == NULL)
3430                 return -1;
3431
3432         static int subs_a2dp_source_id = -1;
3433         static int subs_a2dp_sink_id = -1;
3434
3435         if (subscribe) {
3436                 if (subs_a2dp_source_id == -1) {
3437                         subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
3438                                 NULL, BT_A2DP_SOURCE_INTERFACE,
3439                                 NULL, NULL, NULL, 0,
3440                                 __bt_opc_event_filter,
3441                                 NULL, NULL);
3442                 }
3443                 if (subs_a2dp_sink_id == -1) {
3444                         subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
3445                                 NULL, BT_SINK_INTERFACE,
3446                                 NULL, NULL, NULL, 0,
3447                                 __bt_opc_event_filter,
3448                                 NULL, NULL);
3449                 }
3450         } else {
3451                 if (subs_a2dp_source_id != -1) {
3452                         g_dbus_connection_signal_unsubscribe(conn,
3453                                         subs_a2dp_source_id);
3454                         subs_a2dp_source_id = -1;
3455                 }
3456                 if (subs_a2dp_sink_id != -1) {
3457                         g_dbus_connection_signal_unsubscribe(conn,
3458                                         subs_a2dp_sink_id);
3459                         subs_a2dp_sink_id = -1;
3460                 }
3461         }
3462         return 0;
3463 }
3464
3465 static void __bt_dump_event_filter(GDBusConnection *connection,
3466                                         const gchar *sender_name,
3467                                         const gchar *object_path,
3468                                         const gchar *interface_name,
3469                                         const gchar *signal_name,
3470                                         GVariant *parameters,
3471                                         gpointer user_data)
3472 {
3473         return;
3474 }
3475
3476 int __bt_register_dump_subscribe_signal(GDBusConnection *conn,
3477                 gboolean subscribe)
3478 {
3479         if (conn == NULL)
3480                 return -1;
3481
3482         static int subs_source_id = -1;
3483
3484         if (subscribe) {
3485                 if (subs_source_id == -1) {
3486                         subs_source_id = g_dbus_connection_signal_subscribe(conn,
3487                                 NULL, BT_DUMP_SERVICE_INTERFACE,
3488                                 BT_DUMP_SERVICE_SIGNAL, BT_DUMP_SERVICE_PATH, NULL, 0,
3489                                 __bt_dump_event_filter,
3490                                 NULL, NULL);
3491                 }
3492         } else {
3493                 if (subs_source_id != -1) {
3494                         g_dbus_connection_signal_unsubscribe(conn,
3495                                         subs_source_id);
3496                         subs_source_id = -1;
3497                 }
3498         }
3499         return 0;
3500 }
3501
3502 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
3503 {
3504         BT_DBG("+");
3505
3506         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3507
3508         switch (event_type) {
3509         case BT_MANAGER_EVENT:
3510                 _bt_register_manager_subscribe_signal(g_conn, TRUE);
3511                 break;
3512         case BT_DEVICE_EVENT:
3513                 _bt_register_device_subscribe_signal(g_conn, TRUE);
3514                 break;
3515         case BT_HID_EVENT:
3516                 _bt_register_input_subscribe_signal(g_conn, TRUE);
3517                 break;
3518         case BT_NETWORK_EVENT:
3519                 _bt_register_network_subscribe_signal(g_conn, TRUE);
3520                 break;
3521         case BT_HEADSET_EVENT:
3522                 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3523                 break;
3524
3525         case BT_OPP_SERVER_EVENT:
3526                 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3527                 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3528                 break;
3529         case BT_OPP_CLIENT_EVENT:
3530                 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3531                 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3532                 break;
3533         case BT_A2DP_SOURCE_EVENT:
3534                 BT_INFO("A2dp Source event");
3535                 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3536                 break;
3537         default:
3538                 BT_ERR("Unknown event");
3539                 return BLUETOOTH_ERROR_INTERNAL;
3540         }
3541
3542         return BLUETOOTH_ERROR_NONE;
3543 }
3544
3545 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3546 {
3547         BT_DBG("+");
3548
3549         ret_if(g_conn == NULL);
3550
3551         switch (event_type) {
3552         case BT_MANAGER_EVENT:
3553                 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3554                 _bt_register_device_subscribe_signal(g_conn, FALSE);
3555                 _bt_register_input_subscribe_signal(g_conn, FALSE);
3556                 _bt_register_network_subscribe_signal(g_conn, FALSE);
3557                 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3558                 break;
3559         case BT_OPP_SERVER_EVENT:
3560                 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3561                 break;
3562         case BT_OPP_CLIENT_EVENT:
3563                 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3564                 break;
3565         default:
3566                 BT_ERR("Unknown event");
3567                 return;
3568         }
3569
3570         BT_DBG("-");
3571 }
3572
3573 static int __bt_init_manager_receiver(void)
3574 {
3575         BT_DBG("+");
3576
3577         GError *error = NULL;
3578
3579         if (manager_conn == NULL) {
3580                 manager_conn =  g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3581                 if (error != NULL) {
3582                         BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3583                         g_clear_error(&error);
3584                 }
3585                 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3586         }
3587
3588         if (_bt_register_service_event(manager_conn,
3589                                 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3590                 goto fail;
3591         if (_bt_register_service_event(manager_conn,
3592                                 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3593                 goto fail;
3594
3595         if (_bt_register_service_event(manager_conn,
3596                                 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3597                 goto fail;
3598
3599         if (_bt_register_service_event(manager_conn,
3600                                 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3601                 goto fail;
3602
3603         if (_bt_register_service_event(manager_conn,
3604                                 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3605                 goto fail;
3606
3607         __bt_register_dump_subscribe_signal(manager_conn, TRUE);
3608         return BLUETOOTH_ERROR_NONE;
3609 fail:
3610         if (manager_conn) {
3611                 g_object_unref(manager_conn);
3612                 manager_conn = NULL;
3613         }
3614
3615         BT_DBG("-");
3616
3617         return BLUETOOTH_ERROR_INTERNAL;
3618 }
3619
3620 static int __bt_init_obexd_receiver(void)
3621 {
3622         BT_DBG("+");
3623 #ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3624         GError *error = NULL;
3625
3626         if (obexd_conn == NULL) {
3627                 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3628                 if (error != NULL) {
3629                         BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3630                         g_clear_error(&error);
3631                 }
3632                 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3633         }
3634
3635         if (_bt_register_service_event(obexd_conn,
3636                                 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3637                 BT_ERR("Error while registering service event");
3638                 g_object_unref(obexd_conn);
3639                 obexd_conn = NULL;
3640                 return BLUETOOTH_ERROR_INTERNAL;
3641         }
3642 #endif
3643         BT_DBG("-");
3644
3645         return BLUETOOTH_ERROR_NONE;
3646 }
3647
3648 gboolean __bt_reinit_obexd_receiver(gpointer user_data)
3649 {
3650         static int retry_cnt = 0;
3651         int result = BLUETOOTH_ERROR_NONE;
3652
3653         BT_DBG("+");
3654
3655         result = __bt_init_obexd_receiver();
3656         if (result != BLUETOOTH_ERROR_NONE) {
3657                 /* 20 ms * 50 = 10 seconds. During 10 seconds fail to initialize,
3658                    then it is not the timing issue. Just can't use the session bus connection */
3659                 if (retry_cnt > 100) {
3660                         BT_ERR("Fail to init obexd receiver by 50 times.");
3661                         retry_cnt = 0;
3662                         session_reinit_timer = 0;
3663                         return FALSE;
3664                 }
3665                 retry_cnt++;
3666                 BT_DBG("Retry to initialize the obexd receiver");
3667                 return TRUE;
3668         }
3669
3670         retry_cnt = 0;
3671         session_reinit_timer = 0;
3672
3673         BT_DBG("-");
3674
3675         return FALSE;
3676 }
3677
3678 /* To receive the event from bluez */
3679 int _bt_init_service_event_receiver(void)
3680 {
3681         BT_DBG("+");
3682
3683         int result;
3684
3685         result = __bt_init_manager_receiver();
3686         retv_if(result != BLUETOOTH_ERROR_NONE, result);
3687
3688         result = __bt_init_obexd_receiver();
3689         if (result != BLUETOOTH_ERROR_NONE) {
3690                 BT_ERR("Fail to init obexd receiver");
3691
3692                 /* Try to re-initialize obexd receiver in the timer */
3693                 if (session_reinit_timer > 0)
3694                         g_source_remove(session_reinit_timer);
3695
3696                 session_reinit_timer = g_timeout_add(BT_SESSION_BUS_GET_TIMEOUT,
3697                                                         (GSourceFunc)__bt_reinit_obexd_receiver, NULL);
3698         }
3699
3700         BT_DBG("-");
3701
3702         return BLUETOOTH_ERROR_NONE;
3703 }
3704
3705 void _bt_deinit_service_event_receiver(void)
3706 {
3707         BT_DBG("+");
3708
3709         _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3710
3711         _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3712
3713         __bt_register_dump_subscribe_signal(manager_conn, FALSE);
3714
3715         if (manager_conn) {
3716                 g_object_unref(manager_conn);
3717                 manager_conn = NULL;
3718         }
3719
3720         if (obexd_conn) {
3721                 g_object_unref(obexd_conn);
3722                 obexd_conn = NULL;
3723         }
3724
3725         if (event_id > 0)
3726                 g_source_remove(event_id);
3727         event_id = 0;
3728
3729         BT_DBG("-");
3730 }