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