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