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