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