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