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