Remove journal related codes
[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
730                                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_state) == 0 &&
731                                         bt_state != VCONFKEY_BT_LE_STATUS_OFF) {
732                                         _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
733                                 }
734                         }
735                         else {
736 #ifdef USB_BLUETOOTH
737                                 _bt_handle_adapter_added();
738 #endif
739                         }
740                 } else if (strcasecmp(property, "Connectable") == 0) {
741                         gboolean connectable = FALSE;
742
743                         g_variant_get(val, "b", &connectable);
744
745                         BT_DBG("Connectable property is changed : %d", connectable);
746                         param = g_variant_new("(b)", connectable);
747                         _bt_send_event(BT_ADAPTER_EVENT,
748                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
749                                 param);
750                         if (_bt_adapter_get_status() == BT_DEACTIVATING &&
751                         _bt_adapter_get_le_status() == BT_LE_ACTIVATED &&
752                         connectable == 0)
753                         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
754                 } else if (strcasecmp(property, "SupportedLEFeatures") == 0) {
755                         char *name = NULL;
756                         char *value = NULL;
757                         GVariantIter *iter = NULL;
758                         g_variant_get(val, "as", &iter);
759                         while (g_variant_iter_loop(iter, "s", &name)) {
760                                 BT_DBG("name = %s", name);
761                                 g_variant_iter_loop(iter, "s", &value);
762                                 BT_DBG("Value = %s", value);
763                                 if (FALSE == _bt_update_le_feature_support(name, value))
764                                         BT_INFO("Fail to update LE feature info");
765                         }
766                         if (iter)
767                                 g_variant_iter_free(iter);
768                 } else {
769                         BT_DBG("property : [%s]", property);
770                 }
771         }
772 }
773
774 static void __bt_obex_property_changed_event(GVariant *msg, const char *path)
775 {
776         BT_DBG("+");
777
778         GVariantIter value_iter;
779         GVariant *child = NULL, *val = NULL;
780         char *property = NULL;
781         g_variant_iter_init (&value_iter, msg);
782         while ((child = g_variant_iter_next_value (&value_iter))) {
783                 g_variant_get(child, "{sv}", &property, &val);
784
785                 ret_if(property == NULL);
786
787                 BT_DBG("property :%s", property);
788
789                 if (strcasecmp(property, "Status") == 0) {
790                         char  *status;
791                         g_variant_get(val, "s", &status);
792
793                         if (strcasecmp(status, "active") == 0){
794                                 _bt_obex_transfer_started(path);
795                         } else if (strcasecmp(status, "complete") == 0) {
796                                 _bt_obex_transfer_completed(path, TRUE);
797                                 _bt_pbap_obex_transfer_completed(path, TRUE);
798                         } else if (strcasecmp(status, "error") == 0){
799                                 _bt_obex_transfer_completed(path, FALSE);
800                                 _bt_pbap_obex_transfer_completed(path, FALSE);
801                         }
802                         g_free(status);
803                 } else if (strcasecmp(property, "Transferred") == 0) {
804                         static int transferred  = 0;
805                         g_variant_get(val, "t", &transferred);
806
807                         _bt_obex_transfer_progress(path,transferred);
808                 }
809                 g_free(property);
810                 g_variant_unref(val);
811                 g_variant_unref(child);
812         }
813         BT_DBG("-");
814 }
815
816 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
817 {
818         BT_DBG("+");
819
820         int event;
821         int result = BLUETOOTH_ERROR_NONE;
822         GVariantIter value_iter;
823         GVariant *val;
824         char *property = NULL;
825         char *address;
826         GVariant *param = NULL;
827         bt_remote_dev_info_t *remote_dev_info;
828         g_variant_iter_init (&value_iter, msg);
829         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
830                 BT_DBG("Property %s", property);
831                 if (strcasecmp(property, "Connected") == 0) {
832                         guint connected = 0;
833
834                         g_variant_get(val, "i", &connected);
835
836                         event = (connected != BLUETOOTH_CONNECTED_LINK_NONE) ?
837                                 BLUETOOTH_EVENT_DEVICE_CONNECTED :
838                                 BLUETOOTH_EVENT_DEVICE_DISCONNECTED;
839
840                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
841
842                         _bt_convert_device_path_to_address(path, address);
843
844                         BT_DBG("connected: %d", connected);
845                         BT_DBG("address: %s", address);
846
847                         remote_dev_info = _bt_get_remote_device_info(address);
848
849                         if (remote_dev_info != NULL) {
850                                 __bt_device_remote_connected_properties(
851                                                 remote_dev_info, address,
852                                                 connected != BLUETOOTH_CONNECTED_LINK_NONE ?
853                                                 TRUE : FALSE);
854                                 _bt_free_device_info(remote_dev_info);
855                         }
856                         param = g_variant_new("(is)", result, address);
857                         /* Send event to application */
858                         _bt_send_event(BT_DEVICE_EVENT,
859                                         event,
860                                         param);
861                         g_free(address);
862                 } else if (strcasecmp(property, "RSSI") == 0) {
863                         bt_remote_dev_info_t *remote_dev_info;
864
865                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
866
867                         _bt_convert_device_path_to_address(path, address);
868                         BT_DBG("address: %s", address);
869
870                         remote_dev_info = _bt_get_remote_device_info(address);
871                         if (remote_dev_info == NULL) {
872                                 g_free(property);
873                                 g_variant_unref(val);
874                                 g_free(address);
875                                 return;
876                         }
877                         BT_DBG("Address type  %d", remote_dev_info->addr_type);
878
879                         if (remote_dev_info && remote_dev_info->addr_type == 0) {
880                                 BT_DBG("Name %s", remote_dev_info->name);
881
882                                 GVariant *uuids = NULL;
883                                 GVariantBuilder *builder = NULL;
884                                 int i = 0;
885                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
886                                 for (i=0; i < remote_dev_info->uuid_count; i++) {
887                                         g_variant_builder_add(builder, "s",
888                                                 remote_dev_info->uuids[i]);
889                                 }
890                                 uuids = g_variant_new("as", builder);
891                                 g_variant_builder_unref(builder);
892                                 GVariant *manufacturer_data =  NULL;
893                                 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
894                                                                         remote_dev_info->manufacturer_data,
895                                                                         remote_dev_info->manufacturer_data_len,
896                                                                         TRUE,
897                                                                         NULL, NULL);
898                                 param = g_variant_new("(isunsbub@asn@ay)", result,
899                                                         remote_dev_info->address,
900                                                         remote_dev_info->class,
901                                                         remote_dev_info->rssi,
902                                                         remote_dev_info->name,
903                                                         remote_dev_info->paired,
904                                                         remote_dev_info->connected,
905                                                         remote_dev_info->trust,
906                                                         uuids,
907                                                         remote_dev_info->manufacturer_data_len,
908                                                         manufacturer_data);
909
910                                 _bt_send_event(BT_ADAPTER_EVENT,
911                                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
912                                         param);
913                                 g_free(address);
914                         }
915                         _bt_free_device_info(remote_dev_info);
916                 } else if (strcasecmp(property, "GattConnected") == 0) {
917                         gboolean gatt_connected = FALSE;
918
919                         g_variant_get(val, "b", &gatt_connected);
920
921                         event = gatt_connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
922                                         BLUETOOTH_EVENT_GATT_DISCONNECTED;
923
924                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
925
926                         _bt_convert_device_path_to_address(path, address);
927
928                         BT_DBG("gatt_connected: %d", gatt_connected);
929                         BT_DBG("address: %s", address);
930                         param = g_variant_new("(is)", result, address);
931                         /* Send event to application */
932                         _bt_send_event(BT_DEVICE_EVENT,
933                                         event,
934                                         param);
935                         g_free(address);
936                 } else if (strcasecmp(property, "Paired") == 0) {
937                         gboolean paired = FALSE;
938                         bt_remote_dev_info_t *remote_dev_info;
939                         g_variant_get(val, "b", &paired);
940                         _bt_agent_set_canceled(FALSE);
941                         /* BlueZ sends paired signal for each paired device */
942                         /* during activation, We should ignore this, otherwise*/
943                         /* application thinks that a new device got paired */
944                         if (_bt_adapter_get_status() != BT_ACTIVATED) {
945                                 BT_DBG("BT is not activated, so ignore this");
946                                 g_free(property);
947                                 g_variant_unref(val);
948                                 return;
949                         }
950
951                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
952
953                         _bt_convert_device_path_to_address(path, address);
954
955                         remote_dev_info = _bt_get_remote_device_info(address);
956                         if (remote_dev_info == NULL) {
957                                 g_free(property);
958                                 g_variant_unref(val);
959                                 g_free(address);
960                                 return;
961                         }
962
963                         if(paired == FALSE) {
964                                 BT_INFO("Unpaired: %s", address);
965                                 __bt_update_remote_cache_devinfo(address, FALSE);
966                                 param = g_variant_new("(is)", result, address);
967                                 _bt_send_event(BT_ADAPTER_EVENT,
968                                         BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
969                                         param);
970                         } else {
971                                 BT_INFO("Paired: %s", address);
972                                 __bt_update_remote_cache_devinfo(address, TRUE);
973
974                                 if (_bt_is_device_creating() == TRUE) {
975                                         BT_DBG("Try to Pair by me");
976                                         _bt_free_device_info(remote_dev_info);
977                                         g_free(address);
978                                         g_free(property);
979                                         g_variant_unref(val);
980                                         return;
981                                 }
982                                 GVariant *uuids = NULL;
983                                 GVariantBuilder *builder = NULL;
984                                 int i = 0;
985                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
986                                 for (i=0; i < remote_dev_info->uuid_count; i++) {
987                                         g_variant_builder_add(builder, "s",
988                                                 remote_dev_info->uuids[i]);
989                                 }
990                                 uuids = g_variant_new("as", builder);
991                                 g_variant_builder_unref(builder);
992                                 GVariant *manufacturer_data =  NULL;
993                                 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
994                                                                         remote_dev_info->manufacturer_data,
995                                                                         remote_dev_info->manufacturer_data_len,
996                                                                         TRUE,
997                                                                         NULL, NULL);
998
999                                 param = g_variant_new("(isunsbub@asn@ay)", result,
1000                                                         address, remote_dev_info->class,
1001                                                         remote_dev_info->rssi,
1002                                                         remote_dev_info->name,
1003                                                         remote_dev_info->paired,
1004                                                         remote_dev_info->connected,
1005                                                         remote_dev_info->trust,
1006                                                         uuids,
1007                                                         remote_dev_info->manufacturer_data_len,
1008                                                         manufacturer_data);
1009                                 _bt_send_event(BT_ADAPTER_EVENT,
1010                                         BLUETOOTH_EVENT_BONDING_FINISHED,
1011                                         param);
1012                         }
1013                         _bt_free_device_info(remote_dev_info);
1014                         g_free(address);
1015                 } else if (strcasecmp(property, "LegacyPaired") == 0) {
1016                         gboolean paired = FALSE;
1017                         bt_remote_dev_info_t *remote_dev_info;
1018
1019                         if (_bt_adapter_get_status() != BT_ACTIVATED) {
1020                                 BT_DBG("BT is not activated, so ignore this");
1021                                 g_free(property);
1022                                 g_variant_unref(val);
1023                                 return;
1024                         }
1025
1026                         g_variant_get(val, "b", &paired);
1027                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1028                         BT_DBG("LegacyPaired: %d", paired);
1029                         _bt_convert_device_path_to_address(path, address);
1030
1031                         remote_dev_info = _bt_get_remote_device_info(address);
1032                         if (remote_dev_info == NULL) {
1033                                 g_free(address);
1034                                 g_free(property);
1035                                 g_variant_unref(val);
1036                                 return;
1037                         }
1038
1039                         BT_DBG("LegacyPairing Failed with %s. Show Error Popup",
1040                                         remote_dev_info->name);
1041                         _bt_launch_system_popup(BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE,
1042                                                 remote_dev_info->name, NULL, NULL, NULL);
1043
1044                         _bt_free_device_info(remote_dev_info);
1045                         g_free(address);
1046                 } else if (strcasecmp(property, "Trusted") == 0) {
1047                         gboolean trusted = FALSE;
1048
1049                         g_variant_get(val, "b", &trusted);
1050
1051                         event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1052                                         BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1053
1054                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1055
1056                         _bt_convert_device_path_to_address(path, address);
1057
1058                         BT_DBG("trusted: %d", trusted);
1059                         BT_DBG("address: %s", address);
1060                         param = g_variant_new("(is)", result, address);
1061                         /* Send event to application */
1062                         _bt_send_event(BT_DEVICE_EVENT,
1063                                         event,
1064                                         param);
1065                         g_free(address);
1066                 }
1067         }
1068         BT_DBG("-");
1069 }
1070
1071 static void __bt_media_control_changed_event(GVariant *msg, const char *path)
1072 {
1073         int event;
1074         int result = BLUETOOTH_ERROR_NONE;
1075         GVariantIter value_iter;
1076         char *property = NULL;
1077         char *address;
1078         GVariant *val = NULL;
1079         GVariant *child = NULL;
1080         bt_remote_dev_info_t *remote_dev_info;
1081         GVariant *param = NULL;
1082         g_variant_iter_init (&value_iter, msg);
1083         while ((child = g_variant_iter_next_value (&value_iter))) {
1084                 g_variant_get(child, "{sv}", &property, &val);
1085                 BT_INFO("Property %s", property);
1086                 if (strcasecmp(property, "Connected") == 0) {
1087                         gboolean connected = FALSE;
1088
1089                         g_variant_get(val, "b", &connected);
1090
1091                         event = connected ? BLUETOOTH_EVENT_AVRCP_CONNECTED :
1092                                         BLUETOOTH_EVENT_AVRCP_DISCONNECTED;
1093
1094                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1095
1096                         _bt_convert_device_path_to_address(path, address);
1097
1098                         BT_DBG("connected: %d", connected);
1099                         BT_DBG("address: %s", address);
1100
1101                         remote_dev_info = _bt_get_remote_device_info(address);
1102
1103                         if (remote_dev_info != NULL) {
1104                                 __bt_device_remote_connected_properties(
1105                                 remote_dev_info, address, connected);
1106                                 _bt_free_device_info(remote_dev_info);
1107                         }
1108                         param = g_variant_new("(is)", result, address);
1109                         /* Send event to application */
1110                         _bt_send_event(BT_AVRCP_EVENT,
1111                                 event,
1112                                 param);
1113                         g_free(address);
1114                 }
1115                 g_free(property);
1116                 g_variant_unref(child);
1117                 g_variant_unref(val);
1118         }
1119         BT_DBG("-");
1120 }
1121
1122 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
1123 {
1124         char *interface_name = NULL;
1125         GVariant *val = NULL;
1126
1127         g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &val,NULL);
1128
1129         if (strcasecmp(interface_name, BT_ADAPTER_INTERFACE) == 0) {
1130                 __bt_adapter_property_changed_event(val,
1131                                         object_path);
1132         } else if (strcasecmp(interface_name, BT_DEVICE_INTERFACE) == 0) {
1133                 __bt_device_property_changed_event(val,object_path);
1134         } else if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1135                 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
1136                 __bt_obex_property_changed_event(val,
1137                                         object_path);
1138         } else if (strcasecmp(interface_name, BT_MEDIA_CONTROL_INTERFACE) == 0) {
1139                 __bt_media_control_changed_event(val,
1140                                         object_path);
1141         } else if (strcasecmp(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
1142                 _bt_handle_avrcp_control_event(val,
1143                                         object_path);
1144         } else if (strcasecmp(interface_name, BT_NETWORK_CLIENT_INTERFACE) == 0) {
1145                 BT_DBG("BT_NETWORK_CLIENT_INTERFACE");
1146                 _bt_handle_network_client_event(val,
1147                                         object_path);
1148         } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
1149                 __bt_gatt_char_property_changed_event(val,
1150                                         object_path);
1151         }
1152         g_variant_unref(val);
1153 }
1154
1155 void __bt_opc_property_changed_event(GVariant *msg,
1156                                                 const char *path)
1157 {
1158         GVariantIter value_iter;
1159         char *property = NULL;
1160         GVariant *val = NULL;
1161         GVariant *child = NULL;
1162
1163         g_variant_iter_init (&value_iter, msg);
1164         while ((child = g_variant_iter_next_value (&value_iter))) {
1165                 g_variant_get(child, "{sv}", &property, &val);
1166                 ret_if(property == NULL);
1167
1168                 if (strcasecmp(property, "Status") == 0) {
1169                         char *status = NULL;
1170                         g_variant_get(val, "s", &status);
1171                         BT_DBG("Status is %s", status);
1172
1173                         if(strcasecmp(status, "active") == 0){
1174                                 _bt_obex_client_started(path);
1175                         }else if (strcasecmp(status, "complete") == 0) {
1176                                 _bt_obex_client_completed(path, TRUE);
1177                         }else if (strcasecmp(status, "error") == 0){
1178                                 _bt_obex_client_completed(path, FALSE);
1179                         }
1180                         g_free(status);
1181                 } else if (strcasecmp(property, "Transferred") == 0) {
1182                         static int transferred  = 0;
1183                         g_variant_get(val, "t", &transferred);
1184
1185                         _bt_obex_client_progress(path, transferred);
1186                 } else {
1187                         BT_DBG("property : [%s]", property);
1188                 }
1189                 g_free(property);
1190                 g_variant_unref(child);
1191                 g_variant_unref(val);
1192         }
1193 }
1194
1195 void _bt_opc_property_changed_event(GVariant *msg, char *path)
1196 {
1197         char *interface_name = NULL;
1198         GVariant *value = NULL;
1199         g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1200         BT_INFO("interface_name = %s", interface_name);
1201         if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1202                 __bt_opc_property_changed_event(value,
1203                                         path);
1204         } else {
1205                 BT_DBG("interface_name : [%s]", interface_name);
1206         }
1207         g_variant_unref(value);
1208 }
1209
1210
1211 void _bt_handle_input_event(GVariant *msg, const char *path)
1212 {
1213         int result = BLUETOOTH_ERROR_NONE;
1214         gboolean property_flag = FALSE;
1215         GVariantIter value_iter;
1216         char *property = NULL;
1217         GVariant *child = NULL, *val = NULL;
1218         bt_remote_dev_info_t *remote_dev_info;
1219         GVariant *param = NULL;
1220         g_variant_iter_init (&value_iter, msg);
1221         while ((child = g_variant_iter_next_value (&value_iter))) {
1222                 g_variant_get(child, "{sv}", &property, &val);
1223
1224                 ret_if(property == NULL);
1225
1226                 if (strcasecmp(property, "Connected") == 0) {
1227                         int event = BLUETOOTH_EVENT_NONE;
1228                         char *address;
1229                         g_variant_get(val, "b", &property_flag);
1230
1231                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1232
1233                         _bt_convert_device_path_to_address(path, address);
1234
1235                         event = (property_flag == TRUE) ?
1236                                         BLUETOOTH_HID_CONNECTED :
1237                                         BLUETOOTH_HID_DISCONNECTED;
1238                         param = g_variant_new("(is)", result, address);
1239                         _bt_send_event(BT_HID_EVENT, event,
1240                                 param);
1241                         /* Check HID connection type (Keyboard or Mouse) and update the status */
1242                         remote_dev_info = _bt_get_remote_device_info(address);
1243
1244                         if (property_flag == TRUE) {
1245                                 hid_connected_device_count++;
1246                                 __bt_set_device_values(TRUE,
1247                                                 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1248                         } else {
1249                                 hid_connected_device_count--;
1250                                 if (hid_connected_device_count == 0)
1251                                         __bt_set_device_values(FALSE,
1252                                                         VCONFKEY_BT_DEVICE_HID_CONNECTED);
1253                         }
1254
1255                         if (remote_dev_info != NULL) {
1256                                 BT_DBG("HID device class [%x]", remote_dev_info->class);
1257                                 if (remote_dev_info->class &
1258                                         BLUETOOTH_DEVICE_MINOR_CLASS_KEY_BOARD) {
1259 #ifdef ENABLE_TIZEN_2_4
1260                                         __bt_set_device_values(property_flag,
1261                                                 VCONFKEY_BT_DEVICE_HID_KEYBOARD_CONNECTED);
1262 #endif
1263
1264                                 }
1265
1266                                 if (remote_dev_info->class &
1267                                                 BLUETOOTH_DEVICE_MINOR_CLASS_POINTING_DEVICE) {
1268 #ifdef ENABLE_TIZEN_2_4
1269                                         __bt_set_device_values(property_flag,
1270                                                         VCONFKEY_BT_DEVICE_HID_MOUSE_CONNECTED);
1271 #endif
1272                                 }
1273                                 _bt_free_device_info(remote_dev_info);
1274                         }
1275                         g_free(address);
1276                 }
1277                 g_free(property);
1278                 g_variant_unref(val);
1279                 g_variant_unref(child);
1280          }
1281 }
1282
1283 void _bt_handle_network_server_event(GVariant *msg, const char *member)
1284 {
1285         int result = BLUETOOTH_ERROR_NONE;
1286         char *address = NULL;
1287         char *device = NULL;
1288         GVariant *param = NULL;
1289         ret_if(member == NULL);
1290         if (strcasecmp(member, "PeerConnected") == 0) {
1291                 g_variant_get(msg, "(ss)", &device, &address);
1292
1293                 __bt_set_device_values(TRUE,
1294                                 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1295                 param = g_variant_new("(iss)", result, device, address);
1296                 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
1297                         param);
1298                 g_free(device);
1299                 g_free(address);
1300                  nap_connected_device_count++;
1301         } else if (strcasecmp(member, "PeerDisconnected") == 0) {
1302                 g_variant_get(msg, "(ss)", &device, &address);
1303                 nap_connected_device_count--;
1304                 if (nap_connected_device_count == 0)
1305                         __bt_set_device_values(FALSE,
1306                                 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1307                 param = g_variant_new("(iss)", result, device, address);
1308                 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
1309                         param);
1310                 g_free(device);
1311                 g_free(address);
1312         }
1313 }
1314
1315 void _bt_handle_network_client_event(GVariant *msg,
1316                                 const char *path)
1317 {
1318         BT_DBG("+");
1319
1320         int result = BLUETOOTH_ERROR_NONE;
1321         gboolean property_flag = FALSE;
1322         char *property = NULL;
1323         GVariant *val = NULL;
1324         GVariantIter value_iter;
1325         GVariant *param = NULL;
1326         g_variant_iter_init (&value_iter, msg);
1327         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1328                 if (strcasecmp(property, "Connected") == 0) {
1329                         int event = BLUETOOTH_EVENT_NONE;
1330                         char *address;
1331
1332                         g_variant_get(val, "b", &property_flag);
1333                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1334
1335                         _bt_convert_device_path_to_address(path, address);
1336
1337                         BT_DBG("property_flag %d", property_flag);
1338                         if (property_flag == TRUE) {
1339                                 event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
1340                                 nap_connected_device_count++;
1341                                 __bt_set_device_values(TRUE,
1342                                         VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1343                         } else {
1344                                 event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
1345                                 nap_connected_device_count--;
1346                                 if (nap_connected_device_count == 0)
1347                                         __bt_set_device_values(FALSE,
1348                                                 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1349                         }
1350                         param = g_variant_new("(is)", result, address);
1351                         _bt_send_event(BT_NETWORK_EVENT, event,
1352                                 param);
1353
1354                         g_free(address);
1355                 }
1356         }
1357         BT_DBG("-");
1358 }
1359
1360 void __bt_gatt_char_property_changed_event(GVariant *msg,
1361                                 const char *path)
1362 {
1363         GVariantIter value_iter;
1364         char *property = NULL;
1365         char * char_handle = NULL;
1366         GVariant *val = NULL;
1367         int result = BLUETOOTH_ERROR_NONE;
1368         GVariant *param = NULL;
1369         g_variant_iter_init (&value_iter, msg);
1370         char_handle = g_strdup(path);
1371         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1372                 BT_INFO("Property %s", property);
1373
1374                 ret_if(property == NULL);
1375
1376                 if (strcasecmp(property, "Notifying") == 0) {
1377                         gboolean property_flag = FALSE;
1378                         g_variant_get(val, "b", &property_flag);
1379                         if (property_flag == TRUE)
1380                                 BT_DBG("notifying is enabled");
1381                         else
1382                                 BT_DBG("notifying is disabled");
1383                 } else if (strcasecmp(property, "ChangedValue") == 0) {
1384                         int len = 0;
1385                         GByteArray *gp_byte_array = NULL;
1386                         BT_INFO("Type '%s'\n", g_variant_get_type_string (val));
1387
1388                         if (val) {
1389                                 gp_byte_array = g_byte_array_new();
1390                                 len = g_variant_get_size(val);
1391                                 BT_DBG("Len = %d", len);
1392                                 g_byte_array_append (gp_byte_array,
1393                                         (const guint8 *) g_variant_get_data(val), len);
1394                                 if (gp_byte_array->len != 0) {
1395                                         GVariant *byte_array = NULL;
1396                                         byte_array = g_variant_new_from_data(
1397                                                                 G_VARIANT_TYPE_BYTESTRING,
1398                                                                 gp_byte_array->data,
1399                                                                 gp_byte_array->len,
1400                                                                 TRUE, NULL, NULL);
1401                                         param = g_variant_new("(is@ay)", result, char_handle,
1402                                                                 byte_array);
1403                                         _bt_send_event(BT_DEVICE_EVENT,
1404                                                 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
1405                                                 param);
1406                                 }
1407                                 g_byte_array_free(gp_byte_array, TRUE);
1408                         }
1409                 }
1410         }
1411         g_free(char_handle);
1412 }
1413 void _bt_handle_device_event(GVariant *msg, const char *member,const char *path)
1414 {
1415         int event;
1416         int result = BLUETOOTH_ERROR_NONE;
1417         char *address;
1418         char *dev_name;
1419         const char *property = NULL;
1420         GVariant *param = NULL;
1421         ret_if(path == NULL);
1422
1423         if (strcasecmp(member, "PropertyChanged") == 0) {
1424
1425                 g_variant_get(msg, "(s)", &property);
1426
1427                 ret_if(property == NULL);
1428
1429                 if (strcasecmp(property, "GattConnected") == 0) {
1430                         gboolean connected = FALSE;
1431                         char *address;
1432                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1433                         _bt_convert_device_path_to_address(path, address);
1434                         g_variant_get(msg, "(b)", &connected);
1435
1436                         event = connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1437                                         BLUETOOTH_EVENT_GATT_DISCONNECTED;
1438                         param = g_variant_new("(is)", result, address);
1439                         _bt_send_event(BT_DEVICE_EVENT,
1440                                         event,
1441                                         param);
1442                         g_free(address);
1443                 } else if (strcasecmp(property, "Paired") == 0) {
1444                         gboolean paired = FALSE;
1445                         bt_remote_dev_info_t *remote_dev_info;
1446                         g_variant_get(msg, "(b)", &paired);
1447
1448                         ret_if(paired == FALSE);
1449
1450                         /* BlueZ sends paired signal for each paired device */
1451                         /* during activation, We should ignore this, otherwise*/
1452                         /* application thinks that a new device got paired */
1453                         if (_bt_adapter_get_status() != BT_ACTIVATED) {
1454                                 BT_DBG("BT is not activated, so ignore this");
1455                                 return;
1456                         }
1457
1458                         if (_bt_is_device_creating() == TRUE) {
1459                                 BT_DBG("Try to Pair by me");
1460                                 return;
1461                         }
1462
1463                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1464
1465                         _bt_convert_device_path_to_address(path, address);
1466
1467                         remote_dev_info = _bt_get_remote_device_info(address);
1468                         if (remote_dev_info == NULL) {
1469                                 g_free(address);
1470                                 return;
1471                         }
1472                         GVariant *uuids = NULL;
1473                         GVariantBuilder *builder = NULL;
1474                         int i = 0;
1475                         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1476                         for (i=0; i < remote_dev_info->uuid_count; i++) {
1477                                 g_variant_builder_add(builder, "s",
1478                                         remote_dev_info->uuids[i]);
1479                         }
1480                         uuids = g_variant_new("as", builder);
1481                         g_variant_builder_unref(builder);
1482                         GVariant *manufacturer_data = NULL;
1483                         manufacturer_data = g_variant_new_from_data(
1484                                                 G_VARIANT_TYPE_BYTESTRING,
1485                                                 remote_dev_info->manufacturer_data,
1486                                                 remote_dev_info->manufacturer_data_len,
1487                                                 TRUE, NULL, NULL);
1488                         param = g_variant_new("(isunsbub@asn@ay)", result,
1489                                                 address,
1490                                                 remote_dev_info->class,
1491                                                 remote_dev_info->rssi,
1492                                                 remote_dev_info->name,
1493                                                 remote_dev_info->paired,
1494                                                 remote_dev_info->connected,
1495                                                 remote_dev_info->trust,
1496                                                 uuids,
1497                                                 remote_dev_info->manufacturer_data_len,
1498                                                 manufacturer_data);
1499                         _bt_send_event(BT_ADAPTER_EVENT,
1500                                 BLUETOOTH_EVENT_BONDING_FINISHED,
1501                                 param);
1502                         _bt_free_device_info(remote_dev_info);
1503                         g_free(address);
1504
1505                 } else if (strcasecmp(property, "UUIDs") == 0) {
1506                         /* Once we get the updated uuid information after
1507                          * reverse service search, update it to application */
1508
1509                         bt_remote_dev_info_t *remote_dev_info;
1510
1511                         ret_if(_bt_is_device_creating() == TRUE);
1512
1513                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1514
1515                         _bt_convert_device_path_to_address(path, address);
1516
1517                         remote_dev_info = _bt_get_remote_device_info(address);
1518                         if (remote_dev_info == NULL) {
1519                                 g_free(address);
1520                                 return;
1521                         }
1522
1523                         BT_DBG("UUID's count = %d", remote_dev_info->uuid_count);
1524                         if (remote_dev_info->paired && remote_dev_info->uuid_count) {
1525                                 GVariant *uuids = NULL;
1526                                 GVariantBuilder *builder = NULL;
1527                                 int i = 0;
1528                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1529                                 for (i=0; i < remote_dev_info->uuid_count; i++) {
1530                                         g_variant_builder_add(builder, "s",
1531                                                 remote_dev_info->uuids[i]);
1532                                 }
1533                                 uuids = g_variant_new("as", builder);
1534                                 g_variant_builder_unref(builder);
1535                                 GVariant *manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1536                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1537                                                 TRUE, NULL, NULL);
1538
1539                                 param = g_variant_new("(isunsbub@asn@ay)", result,
1540                                                         address, remote_dev_info->class,
1541                                                         remote_dev_info->rssi,
1542                                                         remote_dev_info->name,
1543                                                         remote_dev_info->paired,
1544                                                         remote_dev_info->connected,
1545                                                         remote_dev_info->trust,
1546                                                         uuids,
1547                                                         remote_dev_info->manufacturer_data_len,
1548                                                         manufacture_data);
1549                                 _bt_send_event(BT_ADAPTER_EVENT,
1550                                         BLUETOOTH_EVENT_SERVICE_SEARCHED,
1551                                         param);
1552                         }
1553
1554                         _bt_free_device_info(remote_dev_info);
1555                         g_free(address);
1556                 }
1557         } else if (strcasecmp(member, "DeviceConnected") == 0) {
1558                 unsigned char addr_type = 0;
1559
1560                 g_variant_get(msg, "(y)", &addr_type);
1561
1562                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1563
1564                 _bt_convert_device_path_to_address(path, address);
1565                 dev_name = _bt_get_bonded_device_name(address);
1566
1567                 BT_INFO("Address : %s Type : %d", address, addr_type);
1568                 BT_ERR_C("Connected [%s] [%s]", !addr_type ? "BREDR" : "LE",
1569                                 !addr_type ? dev_name : address);
1570                 g_free(dev_name);
1571
1572                 _bt_logging_connection(TRUE, addr_type);
1573 #ifdef ENABLE_TIZEN_2_4
1574                 journal_bt_connected();
1575 #endif
1576                 param = g_variant_new("(isy)", result, address, addr_type);
1577                 /*Send event to application*/
1578                 _bt_send_event(BT_DEVICE_EVENT,
1579                                         BLUETOOTH_EVENT_DEVICE_CONNECTED,
1580                                         param);
1581                 g_free(address);
1582         } else if (strcasecmp(member, "Disconnected") == 0) {
1583                 unsigned char disc_reason = 0;
1584                 unsigned char addr_type = 0;
1585                 gboolean sending = FALSE;
1586
1587                 g_variant_get(msg, "(yy)", &addr_type, &disc_reason);
1588
1589                 result = disc_reason;
1590
1591                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1592
1593                 _bt_convert_device_path_to_address(path, address);
1594                 dev_name = _bt_get_bonded_device_name(address);
1595 #ifdef ENABLE_TIZEN_2_4
1596                 journal_bt_disconnected();
1597 #endif
1598
1599                 /* 0x00 BDADDR_BRDER
1600                       0x01 BDADDR_LE_PUBLIC
1601                       0x02 BDADDR_LE_RANDOM */
1602                 BT_INFO("Address : %s Type : %d", address, addr_type);
1603                 BT_ERR_C("Disconnected [%s] [%d : %s] [%s]", !addr_type ? "BREDR" : "LE",
1604                                 disc_reason, _bt_convert_disc_reason_to_string(disc_reason),
1605                                 !addr_type ? dev_name : address);
1606                 g_free(dev_name);
1607
1608                 _bt_headset_set_local_connection(FALSE);
1609                 _bt_logging_connection(FALSE, addr_type);
1610
1611                 /*Check for any OPP transfer on the device and cancel
1612                  * the transfer
1613                  */
1614                 _bt_obex_check_pending_transfer(address);
1615                 _bt_opp_client_is_sending(&sending);
1616                 if(sending == TRUE)
1617                         _bt_opp_client_check_pending_transfer(address);
1618                 param = g_variant_new("(isy)", result, address, addr_type);
1619                 _bt_send_event(BT_DEVICE_EVENT,
1620                                         BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1621                                         param);
1622                 g_free(address);
1623         } else if (strcasecmp(member, "ProfileStateChanged") == 0) {
1624                 int state = 0;
1625                 char *profile_uuid = NULL;
1626                 bt_headset_wait_t *wait_list;
1627
1628                 g_variant_get(msg, "(si)", &profile_uuid, &state);
1629
1630                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1631
1632                 _bt_convert_device_path_to_address(path, address);
1633
1634                 BT_DBG("Address: %s", address);
1635                 BT_DBG("Profile UUID: %s", profile_uuid);
1636                 BT_DBG("State: %d", state);
1637
1638                 if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0)  &&
1639                         (state == BT_PROFILE_STATE_CONNECTED)) {
1640
1641                         int event = BLUETOOTH_EVENT_AV_CONNECTED;
1642                         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1643                         bluetooth_device_address_t device_address;
1644                         gboolean connected;
1645                         bt_headset_wait_t *wait_list;
1646
1647                         __bt_set_device_values(TRUE,
1648                                 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1649
1650                         __bt_connection_manager_set_state(address, event);
1651
1652                         if (_bt_headset_get_local_connection() == FALSE)
1653                                 _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
1654                         else {
1655                                 /* Connection Started from local device therefore no need to
1656                                  * intiate connection for pending profile */
1657                                 _bt_headset_set_local_connection(FALSE);
1658                         }
1659                         param = g_variant_new("(is)", result, address);
1660                         _bt_send_event(BT_HEADSET_EVENT, event,
1661                                 param);
1662                         connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
1663                                                 connected_address);
1664                         if (connected) {
1665                                 if (g_strcmp0(connected_address, address) != 0) {
1666                                         _bt_convert_addr_string_to_type(
1667                                                 device_address.addr,
1668                                                 connected_address);
1669                                         _bt_audio_disconnect(0, BT_AUDIO_A2DP,
1670                                                 &device_address, NULL);
1671                                 }
1672                         }
1673
1674                         _bt_add_headset_to_list(BT_AUDIO_A2DP,
1675                                                 BT_STATE_CONNECTED, address);
1676
1677                         wait_list = _bt_get_audio_wait_data();
1678                         if (wait_list != NULL &&
1679                                 (g_strcmp0(wait_list->address, address) == 0))
1680                                 _bt_rel_wait_data();
1681
1682                 } else if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0)  &&
1683                         (state == BT_PROFILE_STATE_DISCONNECTED)) {
1684
1685                         int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
1686
1687                         if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
1688                                 g_free(address);
1689                                 g_free(profile_uuid);
1690                                 return;
1691                         }
1692
1693                         __bt_set_device_values(FALSE,
1694                                 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1695
1696                         __bt_connection_manager_set_state(address, event);
1697                         param = g_variant_new("(is)", result, address);
1698                         _bt_send_event(BT_HEADSET_EVENT, event,
1699                                 param);
1700                         /* Remove data from the connected list */
1701                         _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
1702                         wait_list = _bt_get_audio_wait_data();
1703
1704                         if (wait_list == NULL) {
1705                                 g_free(address);
1706                                 g_free(profile_uuid);
1707                                 return;
1708                         }
1709
1710                         if (((wait_list->type == BT_AUDIO_ALL) &&
1711                                 (wait_list->ag_flag == TRUE)) ||
1712                                 (wait_list->type == BT_AUDIO_A2DP) ||
1713                                 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
1714                                 bluetooth_device_address_t device_address;
1715                                 _bt_convert_addr_string_to_type(
1716                                                         device_address.addr,
1717                                                         wait_list->address);
1718
1719                                 _bt_audio_connect(wait_list->req_id,
1720                                                         wait_list->type,
1721                                                         &device_address,
1722                                                         wait_list->out_param1);
1723                         }
1724                 } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
1725
1726                         if (state == BT_PROFILE_STATE_CONNECTED) {
1727                                 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
1728                                 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1729                                 bluetooth_device_address_t device_address;
1730                                 gboolean connected;
1731                                 param = g_variant_new("(is)", result, address);
1732                                 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1733                                         param);
1734                                 connected = _bt_is_headset_type_connected(
1735                                                         BT_AVRCP,
1736                                                         connected_address);
1737                                 if (connected) {
1738                                         if (g_strcmp0(connected_address,
1739                                                                 address) != 0) {
1740                                                 _bt_convert_addr_string_to_type(
1741                                                         device_address.addr,
1742                                                         connected_address);
1743                                                 _bt_audio_disconnect(0,
1744                                                         BT_AVRCP,
1745                                                         &device_address, NULL);
1746                                         }
1747                                 }
1748                                 BT_DBG("device Path: %s", path);
1749                                 _bt_add_headset_to_list(BT_AVRCP,
1750                                                 BT_STATE_CONNECTED, address);
1751                         } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1752                                 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
1753                                 param = g_variant_new("(is)", result, address);
1754                                 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
1755                                         param);
1756                                 /* Remove data from the connected list */
1757                                 _bt_remove_headset_from_list(BT_AVRCP, address);
1758                                 }
1759                 } else if (strcasecmp(profile_uuid, A2DP_SOURCE_UUID) == 0) {
1760                         if (state == BT_PROFILE_STATE_CONNECTED) {
1761                                 int event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED;
1762                                 BT_INFO("A2DP Source is connected");
1763                                 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
1764                                         g_variant_new("(is)", result, address));
1765                         } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
1766                                 int event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED;
1767                                 BT_INFO("A2DP Source Disconnected");
1768                                 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
1769                                                 g_variant_new("(is)", result, address));
1770                         }
1771                 } else if ((strcmp(profile_uuid, HID_UUID) == 0) &&
1772                         ((state == BT_PROFILE_STATE_CONNECTED) ||
1773                                 (state == BT_PROFILE_STATE_DISCONNECTED))) {
1774                         int event;
1775                         if (state == BT_PROFILE_STATE_CONNECTED)
1776                                 event = BLUETOOTH_HID_CONNECTED;
1777                         else
1778                                 event = BLUETOOTH_HID_DISCONNECTED;
1779                         param = g_variant_new("(is)", result, address);
1780                         _bt_send_event(BT_HID_EVENT, event,
1781                                 param);
1782
1783                         if (state == BT_PROFILE_STATE_CONNECTED)
1784                                 __bt_set_device_values(TRUE,
1785                                         VCONFKEY_BT_DEVICE_HID_CONNECTED);
1786                         else
1787                                 __bt_set_device_values(FALSE,
1788                                         VCONFKEY_BT_DEVICE_HID_CONNECTED);
1789                 }
1790                 g_free(address);
1791                 g_free(profile_uuid);
1792         } else if (strcasecmp(member, "AdvReport") == 0) {
1793
1794                 bt_remote_le_dev_info_t *le_dev_info = NULL;
1795                 char *buffer = NULL;
1796                 int buffer_len = 0;
1797                 bt_le_adv_info_t *adv_info = NULL;
1798                 GVariant *value = NULL;
1799                 ret_if(_bt_is_le_scanning() == FALSE);
1800
1801                 le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
1802
1803                 g_variant_get(msg, "(syyii@ay)", &le_dev_info->address,
1804                                                 &le_dev_info->addr_type,
1805                                                 &le_dev_info->adv_type,
1806                                                 &le_dev_info->rssi,
1807                                                 &le_dev_info->adv_data_len,
1808                                                 &value);
1809                 buffer_len = g_variant_get_size(value);
1810                 if (buffer_len > 0)
1811                         buffer = (char *)g_variant_get_data(value);
1812
1813                 le_dev_info->adv_data = g_memdup(buffer, buffer_len);
1814
1815                 if (_bt_get_le_scan_type() == BT_LE_PASSIVE_SCAN) {
1816                         _bt_send_scan_result_event(le_dev_info, NULL);
1817                         _bt_free_le_device_info(le_dev_info);
1818                         g_variant_unref(value);
1819                         return;
1820                 }
1821
1822                 if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {       /* ADV_IND */
1823                         adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
1824                         adv_info->addr = g_strdup(le_dev_info->address);
1825                         adv_info->data_len = le_dev_info->adv_data_len;
1826                         adv_info->data = g_malloc0(le_dev_info->adv_data_len);
1827                         memcpy(adv_info->data, le_dev_info->adv_data,
1828                                         le_dev_info->adv_data_len);
1829
1830                         __bt_add_adv_ind_info(adv_info);
1831
1832                 } else {     /* SCAN_RSP */
1833                         adv_info = __bt_get_adv_ind_info(le_dev_info->address);
1834                         if (adv_info) {
1835                                 _bt_send_scan_result_event(le_dev_info, adv_info);
1836                                 __bt_del_adv_ind_info(le_dev_info->address);
1837                         }
1838                 }
1839                 _bt_free_le_device_info(le_dev_info);
1840                 g_variant_unref(value);
1841         }
1842
1843 }
1844
1845 void __bt_set_audio_values(gboolean connected, char *address)
1846 {
1847         char *name = NULL;
1848         int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1849
1850         /*  Set the headset name */
1851         if (connected == TRUE) {
1852                 name = _bt_get_bonded_device_name(address);
1853         } else {
1854                 name = g_strdup("");
1855         }
1856
1857         if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME,
1858                                         name) != 0) {
1859                 BT_ERR("vconf_set_str failed");
1860         }
1861
1862         g_free(name);
1863
1864         /*  Set the headset state */
1865         if (vconf_get_int(VCONFKEY_BT_DEVICE,
1866                                 &bt_device_state) != 0) {
1867                 BT_ERR("vconf_get_str failed");
1868         }
1869
1870 #ifdef TIZEN_SUPPORT_DUAL_HF
1871         if ((connected == TRUE) &&
1872                 (FALSE == __bt_is_companion_device(address))) {
1873                 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1874         } else if ((bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) &&
1875                         (FALSE == __bt_is_companion_device(address))) {
1876                 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1877         }
1878 #else
1879         if (connected == TRUE) {
1880                 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1881         } else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) {
1882                 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
1883         }
1884 #endif
1885
1886         if (vconf_set_int(VCONFKEY_BT_DEVICE,
1887                                 bt_device_state) != 0) {
1888                 BT_ERR("vconf_set_int failed");
1889         }
1890 }
1891
1892 void _bt_handle_headset_event(GVariant *msg, const char *path)
1893 {
1894         int result = BLUETOOTH_ERROR_NONE;
1895         gboolean property_flag = FALSE;
1896         char *property = NULL;
1897         GVariant *value = NULL;
1898         GVariant *param = NULL;
1899         g_variant_get(msg, "(sv)", &property, &value);
1900
1901         ret_if(property == NULL);
1902
1903         BT_DBG("Property = %s \n", property);
1904         /* We allow only 1 headset connection (HSP or HFP)*/
1905         if (strcasecmp(property, "Connected") == 0) {
1906                 int event = BLUETOOTH_EVENT_NONE;
1907                 bt_headset_wait_t *wait_list;
1908                 char *address;
1909                 g_variant_get(value, "b", &property_flag);
1910
1911                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1912
1913                 /* Fix : NULL_RETURNS */
1914                 if (address == NULL)
1915                         return;
1916
1917                 _bt_convert_device_path_to_address(path, address);
1918
1919                 if (property_flag == TRUE) {
1920                         event = BLUETOOTH_EVENT_AG_CONNECTED;
1921                         if (_bt_headset_get_local_connection() == FALSE)
1922                                 _bt_start_timer_for_connection(address, BT_AUDIO_A2DP);
1923                         else
1924                                 _bt_headset_set_local_connection(FALSE);
1925                 } else {
1926                         int previous_state;
1927
1928                         event = BLUETOOTH_EVENT_AG_DISCONNECTED;
1929
1930                         previous_state = _bt_get_device_state_from_list(BT_AUDIO_HSP, address);
1931                         if (previous_state == BT_STATE_DISCONNECTING)
1932                                 _bt_send_hf_local_term_event(address);
1933                 }
1934                 /* Set the State machine here */
1935                 __bt_connection_manager_set_state(address, event);
1936                 __bt_set_audio_values(property_flag, address);
1937                 param = g_variant_new("(is)", result, address);
1938                 _bt_send_event(BT_HEADSET_EVENT, event,
1939                         param);
1940
1941                 if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
1942                         /* Remove data from the connected list */
1943                         _bt_remove_headset_from_list(BT_AUDIO_HSP, address);
1944
1945                         wait_list = _bt_get_audio_wait_data();
1946                         if (wait_list == NULL) {
1947                                 g_free(address);
1948                                 return;
1949                         }
1950
1951                         bluetooth_device_address_t device_address;
1952
1953                         _bt_set_audio_wait_data_flag(TRUE);
1954
1955                         _bt_convert_addr_string_to_type(device_address.addr,
1956                                                         wait_list->address);
1957                         _bt_audio_connect(wait_list->req_id, wait_list->type,
1958                                         &device_address, wait_list->out_param1);
1959                         _bt_rel_wait_data();
1960                 } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
1961                         /* Add data to the connected list */
1962                         _bt_add_headset_to_list(BT_AUDIO_HSP,
1963                                                 BT_STATE_CONNECTED, address);
1964
1965                         wait_list = _bt_get_audio_wait_data();
1966                         if (wait_list != NULL &&
1967                                 (g_strcmp0(wait_list->address, address) == 0))
1968                         _bt_rel_wait_data();
1969
1970                         BT_INFO("Check A2DP pending connect");
1971                         _bt_audio_check_pending_connect();
1972                 }
1973                 g_free(address);
1974         } else if (strcasecmp(property, "State") == 0) {
1975                 char *state = NULL;
1976
1977                 g_variant_get(value, "s", &state);
1978
1979                 /* This code assumes we support only 1 headset connection */
1980                 /* Need to use the headset list, if we support multi-headsets */
1981                 if (strcasecmp(state, "Playing") == 0) {
1982                         BT_DBG("Playing: Sco Connected");
1983                 } else if (strcasecmp(state, "connected") == 0 ||
1984                                 strcasecmp(state, "disconnected") == 0) {
1985                         BT_DBG("connected/disconnected: Sco Disconnected");
1986                 } else {
1987                         BT_ERR("Not handled state - %s", state);
1988                         g_free(state);
1989                         return;
1990                 }
1991                 g_free(state);
1992         } else if (strcasecmp(property, "SpeakerGain") == 0) {
1993                 guint16 spkr_gain;
1994                 char *address;
1995
1996                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1997
1998                 _bt_convert_device_path_to_address(path, address);
1999
2000                 g_variant_get(value, "i", &spkr_gain);
2001                 param = g_variant_new("(isq)", result, address, spkr_gain);
2002                 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
2003                         param);
2004
2005                 g_free(address);
2006         } else if (strcasecmp(property, "MicrophoneGain") == 0) {
2007                 guint16 mic_gain;
2008                 char *address;
2009
2010                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2011
2012                 _bt_convert_device_path_to_address(path, address);
2013
2014                 g_variant_get(value, "i", &mic_gain);
2015                 param = g_variant_new("(isq)", result, address, mic_gain);
2016                 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_MIC_GAIN,
2017                         param);
2018                 g_free(address);
2019         }
2020
2021         if (property)
2022                 g_free(property);
2023         g_variant_unref(value);
2024  }
2025
2026 void _bt_handle_sink_event(GVariant *msg, const char *path)
2027 {
2028         GVariantIter value_iter;
2029         char *property = NULL;
2030
2031         bt_headset_wait_t *wait_list;
2032
2033         GVariant *child = NULL;
2034         GVariant *val = NULL;
2035         GVariant *param = NULL;
2036         g_variant_iter_init (&value_iter, msg);
2037         while ((child = g_variant_iter_next_value (&value_iter))) {
2038
2039                 g_variant_get(child, "{sv}", &property, &val);
2040
2041                 ret_if(property == NULL);
2042
2043                 BT_DBG("Property = %s \n", property);
2044
2045
2046                 if (strcasecmp(property, "State") == 0) {
2047                         int result = BLUETOOTH_ERROR_NONE;
2048                         char *value;
2049
2050                         g_variant_get(val, "s", &value);
2051                         BT_DBG("value: %s", value);
2052
2053                         if (g_strcmp0(value, "disconnected") == 0) {
2054                                 char *address;
2055
2056                                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2057
2058                                 _bt_convert_device_path_to_address(path, address);
2059
2060                                 __bt_set_device_values(FALSE,
2061                                         VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2062                                 param = g_variant_new("(is)", result, address);
2063                                 _bt_send_event(BT_HEADSET_EVENT,
2064                                         BLUETOOTH_EVENT_AV_DISCONNECTED,
2065                                         param);
2066
2067                                 /* Remove data from the connected list */
2068                                 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2069                                 wait_list = _bt_get_audio_wait_data();
2070                                 if (wait_list == NULL) {
2071                                         g_free(value);
2072                                         g_free(property);
2073                                         g_variant_unref(val);
2074                                         g_variant_unref(child);
2075                                         g_free(address);
2076                                         return;
2077                                 }
2078
2079                                 if (((wait_list->type == BT_AUDIO_ALL) &&
2080                                         (wait_list->ag_flag == TRUE)) ||
2081                                         (wait_list->type == BT_AUDIO_A2DP) ||
2082                                         (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2083                                         bluetooth_device_address_t device_address;
2084                                         _bt_convert_addr_string_to_type(
2085                                                                 device_address.addr,
2086                                                                 wait_list->address);
2087
2088                                         _bt_audio_connect(wait_list->req_id,
2089                                                                 wait_list->type,
2090                                                                 &device_address,
2091                                                                 wait_list->out_param1);
2092                                 }
2093                                 g_free(address);
2094                         }else if (strcasecmp(value, "Connected") == 0) {
2095                                 char *address;
2096                                 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2097                                 bluetooth_device_address_t device_address;
2098                                 gboolean connected;
2099
2100                                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2101
2102                                 _bt_convert_device_path_to_address(path, address);
2103
2104                                 __bt_set_device_values(TRUE,
2105                                                 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2106                                 param = g_variant_new("(is)", result, address);
2107                                 _bt_send_event(BT_HEADSET_EVENT,
2108                                         BLUETOOTH_EVENT_AV_CONNECTED,
2109                                         param);
2110                                 /* Check for existing Media device to disconnect */
2111                                 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
2112                                                                         connected_address);
2113                                 if (connected) {
2114                                         /* Match connected device address */
2115                                         if (g_strcmp0(connected_address, address) != 0) {
2116                                                 /* Convert BD adress from string type */
2117                                                 _bt_convert_addr_string_to_type(
2118                                                                 device_address.addr,
2119                                                                 connected_address);
2120                                                 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2121                                                                 &device_address, NULL);
2122                                         }
2123                                 }
2124
2125                                 /* Add data to the connected list */
2126                                 _bt_add_headset_to_list(BT_AUDIO_A2DP,
2127                                                 BT_STATE_CONNECTED, address);
2128
2129                                 g_free(address);
2130                         }
2131                         g_free(value);
2132                 }
2133                 g_free(property);
2134                 g_variant_unref(val);
2135                 g_variant_unref(child);
2136         }
2137 }
2138
2139 void _bt_handle_agent_event(GVariant *msg, const char *member)
2140 {
2141         int result = BLUETOOTH_ERROR_NONE;
2142         char *address = NULL;
2143         char *name = NULL;
2144         char *uuid = NULL;
2145         GVariant *param = NULL;
2146         ret_if(member == NULL);
2147
2148         if (strcasecmp(member, "ObexAuthorize") == 0) {
2149                 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2150                 param = g_variant_new("(iss)", result, address, name);
2151                 _bt_send_event(BT_OPP_SERVER_EVENT,
2152                         BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
2153                         param);
2154                 g_free(address);
2155                 g_free(name);
2156         } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2157                 bt_rfcomm_server_info_t *server_info;
2158
2159                 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2160
2161                 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2162                 ret_if(server_info == NULL);
2163                 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2164                 param = g_variant_new("(isssn)", result, address, uuid, name,
2165                                         server_info->control_fd);
2166                 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2167                         BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2168                         param);
2169                 g_free(address);
2170                 g_free(uuid);
2171                 g_free(name);
2172         }
2173 }
2174
2175 static int __bt_get_object_path(GVariant *msg, char **path)
2176 {
2177         g_variant_get(msg, "(o*)", path, NULL);
2178         if (*path == NULL)
2179                 return BLUETOOTH_ERROR_INTERNAL;
2180
2181         return BLUETOOTH_ERROR_NONE;
2182 }
2183
2184 static void __bt_devices_list_free(void)
2185 {
2186         bt_cache_info_t *cache_info;
2187         GList *node;
2188
2189         node = g_list_first(p_cache_list);
2190
2191         while (node != NULL){
2192                 cache_info = (bt_cache_info_t *)node->data;
2193                 p_cache_list = g_list_remove(p_cache_list, cache_info);
2194                 __bt_free_cache_info(cache_info);
2195
2196                 node = g_list_next(node);
2197         }
2198 }
2199
2200 static int __bt_parse_event(GVariant *msg)
2201 {
2202         GVariantIter iter;
2203         GVariant *child;
2204         char *interface_name= NULL;
2205         GVariant *inner_iter = NULL;
2206
2207         g_variant_iter_init(&iter, msg);
2208
2209         while ((child = g_variant_iter_next_value(&iter))) {
2210                 g_variant_get(child,"{&s@a{sv}}", &interface_name, &inner_iter);
2211                 if (g_strcmp0(interface_name,
2212                                 BT_DEVICE_INTERFACE) == 0) {
2213                         g_variant_unref(inner_iter);
2214                         g_variant_unref(child);
2215                         return BT_DEVICE_EVENT;
2216                 } else if (g_strcmp0(interface_name,
2217                                 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2218                         g_variant_unref(inner_iter);
2219                         g_variant_unref(child);
2220                         return BT_MEDIA_TRANSFER_EVENT;
2221                 } else if (g_strcmp0(interface_name,
2222                                 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2223                         g_variant_unref(inner_iter);
2224                         g_variant_unref(child);
2225                         return BT_AVRCP_CONTROL_EVENT;
2226                 }
2227                 g_variant_unref(inner_iter);
2228                 g_variant_unref(child);
2229         }
2230
2231         return 0;
2232 }
2233
2234 static  void __bt_manager_event_filter(GDBusConnection *connection,
2235                                         const gchar *sender_name,
2236                                         const gchar *object_path,
2237                                         const gchar *interface_name,
2238                                         const gchar *signal_name,
2239                                         GVariant *parameters,
2240                                         gpointer user_data)
2241 {
2242         bt_event_type_t bt_event = 0x00;
2243         int result = BLUETOOTH_ERROR_NONE;
2244         GVariant *value;
2245         char *obj_path = NULL;
2246         GVariant *param = NULL;
2247         if (signal_name == NULL)
2248                 return;
2249         if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2250                 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2251
2252                 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0){
2253 #ifdef USB_BLUETOOTH
2254                         BT_DBG("Enable Adapter");
2255                         _bt_enable_adapter();
2256 #else
2257                         _bt_handle_adapter_added();
2258 #endif
2259                 }
2260                 else {
2261                         bt_event = __bt_parse_event(value);
2262                         if (bt_event == BT_DEVICE_EVENT) {
2263                                 bt_cache_info_t *cache_info;
2264                                 bt_remote_dev_info_t *dev_info;
2265
2266                                 ret_if(_bt_is_discovering() == FALSE &&
2267                                                 _bt_is_le_scanning() == FALSE);
2268
2269                                 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2270                                 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2271                                 cache_info->dev_info = dev_info;
2272
2273                                 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2274                                         BT_ERR("Fail to parse the properies");
2275                                         __bt_free_cache_info(cache_info);
2276                                         g_variant_unref(value);
2277                                         return;
2278                                 }
2279
2280                                 if (dev_info->addr_type != BDADDR_BREDR) {
2281                                         /* Whenever emit the property changed from bluez,
2282                                                 some property doesn't reach to bt-service.
2283                                                 So LE device is handled as AdvReport signal */
2284                                         __bt_free_cache_info(cache_info);
2285                                         g_variant_unref(value);
2286                                         return;
2287                                 }
2288
2289                                 if (dev_info->name == NULL)
2290                                         /* If Remote device name is NULL or still RNR is not done
2291                                          * then display address as name.
2292                                          */
2293                                         dev_info->name = g_strdup(dev_info->address);
2294
2295                                 GVariant *uuids = NULL;
2296                                 GVariantBuilder *builder = NULL;
2297                                 int i = 0;
2298                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2299                                 for (i=0; i < dev_info->uuid_count; i++) {
2300                                         g_variant_builder_add(builder, "s",
2301                                                 dev_info->uuids[i]);
2302                                 }
2303                                 uuids = g_variant_new("as", builder);
2304                                 g_variant_builder_unref(builder);
2305                                 GVariant *manufacturer_data = NULL;
2306                                 manufacturer_data = g_variant_new_from_data(
2307                                                         G_VARIANT_TYPE_BYTESTRING,
2308                                                         dev_info->manufacturer_data,
2309                                                         dev_info->manufacturer_data_len,
2310                                                         TRUE, NULL, NULL);
2311                                 param = g_variant_new("(isunsbub@asn@ay)", result,
2312                                                         dev_info->address,
2313                                                         dev_info->class,
2314                                                         dev_info->rssi,
2315                                                         dev_info->name,
2316                                                         dev_info->paired,
2317                                                         dev_info->connected,
2318                                                         dev_info->trust,
2319                                                         uuids,
2320                                                         dev_info->manufacturer_data_len,
2321                                                         manufacturer_data);
2322                                 _bt_send_event(BT_ADAPTER_EVENT,
2323                                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2324                                          param);
2325                                 p_cache_list = g_list_append(p_cache_list, cache_info);
2326                         } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2327                                 BT_DBG("Device path : %s ", obj_path);
2328                                 _bt_set_control_device_path(obj_path);
2329                         }
2330                 }
2331                 g_variant_unref(value);
2332         } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2333 #ifdef USB_BLUETOOTH
2334                 BT_DBG("InterfacesRemoved");
2335                 _bt_handle_adapter_removed();
2336 #endif
2337
2338                 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0) {
2339                         bt_event = BT_MEDIA_TRANSFER_EVENT;
2340                 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2341                         bt_event = BT_DEVICE_EVENT;
2342                 } else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
2343                         bt_event = BT_AVRCP_CONTROL_EVENT;
2344                 }
2345                 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2346                         _bt_handle_adapter_event(parameters, signal_name);
2347                         if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2348                                 BT_INFO("Object Path %s", obj_path);
2349                                 _bt_remove_control_device_path(obj_path);
2350                         }
2351                 }
2352         } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2353                 gboolean value;
2354                 char *name = NULL;
2355                 char *previous = NULL;
2356                 char *current = NULL;
2357
2358                 if (__bt_get_owner_info(parameters, &name, &previous, &current)) {
2359                         BT_ERR("Fail to get the owner info");
2360                         return;
2361                 }
2362
2363                 if (*current != '\0') {
2364                         if (name)
2365                                 g_free(name);
2366                         if (previous)
2367                                 g_free(previous);
2368                         if (current)
2369                                 g_free(current);
2370                         return;
2371                 }
2372
2373                 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2374                         BT_DBG("Bluetoothd is terminated");
2375                         if (_bt_adapter_get_status() != BT_DEACTIVATING) {
2376                                  __bt_disable_cb();
2377                         }
2378                         _bt_handle_adapter_removed();
2379                         __bt_devices_list_free();
2380                 }
2381
2382                 _bt_obex_server_check_allocation(&value);
2383
2384                 if (value == TRUE) {
2385                         /* Check if the obex server was terminated abnormally */
2386                         _bt_obex_server_check_termination(name);
2387                 }
2388
2389                 _bt_rfcomm_server_check_existence(&value);
2390
2391                 if (value == TRUE) {
2392                         /* The obex server was terminated abnormally */
2393                         _bt_rfcomm_server_check_termination(name);
2394                 }
2395
2396                 /* Stop advertising started by terminated process */
2397                 _bt_stop_advertising_by_terminated_process(name);
2398                 /* Stop LE Scan */
2399                 _bt_stop_le_scan(name);
2400                 g_free(name);
2401                 g_free(previous);
2402                 g_free(current);
2403         } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2404                 const char *path = object_path;
2405
2406                 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2407                                 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2408                         return;
2409
2410                 _bt_handle_property_changed_event(parameters, object_path);
2411         } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2412                 _bt_handle_adapter_event(parameters, signal_name);
2413         } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2414                 _bt_handle_input_event(parameters, object_path);
2415         } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2416                 _bt_handle_network_server_event(parameters, signal_name);
2417         } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2418                 _bt_handle_headset_event(parameters, object_path);
2419         } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2420                 _bt_handle_sink_event(parameters, object_path);
2421         } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2422                 _bt_handle_agent_event(parameters, signal_name);
2423         } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2424                 _bt_handle_device_event(parameters, signal_name, object_path);
2425         }
2426
2427         return;
2428 }
2429
2430 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2431 {
2432
2433         if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2434                 char *interface_name = NULL;
2435
2436                 g_variant_get(msg,"(&s@a{sv}@as)", &interface_name, NULL, NULL);
2437                 retv_if(interface_name == NULL, FALSE);
2438
2439                 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2440                         BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2441                         return TRUE;
2442                 }
2443         }
2444
2445         return FALSE;
2446 }
2447
2448 static  void __bt_obexd_event_filter(GDBusConnection *connection,
2449                                         const gchar *sender_name,
2450                                         const gchar *object_path,
2451                                         const gchar *interface_name,
2452                                         const gchar *signal_name,
2453                                         GVariant *parameters,
2454                                         gpointer user_data)
2455 {
2456         const char *member = signal_name;
2457         char *obj_path = NULL;
2458         ret_if(member == NULL);
2459
2460         if (strcasecmp(member, "InterfacesAdded") == 0) {
2461                 if (__bt_get_object_path(parameters, &obj_path)) {
2462                         BT_ERR("Fail to get the path");
2463                         return;
2464                 }
2465                 BT_INFO("object_path = [%s]", object_path);
2466
2467                 /*Handle OPP_SERVER_CONNECTED_EVENT here */
2468                 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2469                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2470                         g_free(obj_path);
2471                         return;
2472                 }
2473
2474                 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2475                         BT_DBG("Obex_Server_Session_Transfer connected");
2476                         _bt_obex_transfer_connected();
2477                 }
2478                 g_free(obj_path);
2479         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2480                 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
2481                 if (__bt_get_object_path(parameters, &obj_path)) {
2482                         BT_ERR("Fail to get the path");
2483                         return;
2484                 }
2485                 BT_INFO("object_path = [%s]", object_path);
2486
2487                 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2488                                 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2489                         BT_DBG("Call PBAP Disconnected");
2490                         _bt_obex_pbap_client_disconnect(obj_path);
2491                 }
2492
2493                 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2494                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2495                         g_free(obj_path);
2496                         return;
2497                 }
2498
2499                 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2500                         BT_DBG("Obex_Server_Session_Transfer disconnected");
2501                         _bt_obex_transfer_disconnected();
2502                 }
2503                 g_free(obj_path);
2504         } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
2505                 const char *path = object_path;
2506
2507                 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
2508                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
2509                         strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2510                                 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2511                         BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
2512                         return;
2513                 }
2514
2515                 _bt_handle_property_changed_event(parameters, path);
2516         }
2517         BT_DBG("-");
2518         return;
2519 }
2520
2521 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
2522 {
2523         BT_DBG("+");
2524
2525         if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2526                 char *interface_name = NULL;
2527
2528                 g_variant_get(msg,"(&s@a{sv}@as)", &interface_name, NULL, NULL);
2529
2530                 retv_if(interface_name == NULL, FALSE);
2531
2532                 if (strcasecmp(interface_name,
2533                                         BT_OBEX_TRANSFER_INTERFACE) == 0) {
2534                         BT_DBG("-");
2535                         return TRUE;
2536                 }
2537         }
2538
2539         BT_DBG("-");
2540
2541         return FALSE;
2542 }
2543
2544 static  void __bt_opc_event_filter(GDBusConnection *connection,
2545                                         const gchar *sender_name,
2546                                         const gchar *object_path,
2547                                         const gchar *interface_name,
2548                                         const gchar *signal_name,
2549                                         GVariant *parameters,
2550                                         gpointer user_data)
2551 {
2552         const char *member = signal_name;
2553         char *obj_path = NULL;
2554         if (strcasecmp(member, "InterfacesAdded") == 0) {
2555                 BT_DBG("InterfacesAdded");
2556         }else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2557
2558                 if (__bt_get_object_path(parameters, &obj_path)) {
2559                         BT_ERR("Fail to get the path");
2560                         return;
2561                 }
2562
2563                 BT_DBG("object_path =%s",obj_path);
2564
2565                 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2566                                 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
2567                                 || strstr(obj_path, "transfer") == NULL) {
2568                         g_free(obj_path);
2569                         return;
2570                 }
2571                 else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2572                                 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2573                         BT_DBG("Going to call opc disconnected");
2574                         _bt_opc_disconnected(obj_path);
2575                 }
2576
2577                 _bt_sending_files();
2578                 g_free(obj_path);
2579         }else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE){
2580                 char *path = (char *)object_path;
2581                 BT_INFO("object_path %s", path);
2582                 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2583                         strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2584                         BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
2585                         return;
2586                 }
2587
2588                 _bt_opc_property_changed_event(parameters, path);
2589         }
2590
2591         return;
2592 }
2593
2594 int _bt_opp_client_event_init(void)
2595 {
2596         GError *error = NULL;
2597
2598         if (opc_obexd_conn == NULL) {
2599                 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
2600
2601                 if (!opc_obexd_conn) {
2602                         if (error) {
2603                                 BT_ERR("Unable to connect to dbus: %s", error->message);
2604                                 g_clear_error(&error);
2605                         }
2606                 return BLUETOOTH_ERROR_INTERNAL;
2607                 }
2608         }
2609
2610         if (_bt_register_service_event(opc_obexd_conn,
2611                         BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
2612                         g_object_unref(opc_obexd_conn);
2613                         opc_obexd_conn = NULL;
2614                         return BLUETOOTH_ERROR_INTERNAL;
2615         }
2616
2617         return BLUETOOTH_ERROR_NONE;
2618 }
2619
2620 void _bt_opp_client_event_deinit(void)
2621 {
2622         if (opc_obexd_conn) {
2623                 _bt_unregister_service_event(opc_obexd_conn,
2624                                                 BT_OPP_CLIENT_EVENT);
2625                  g_object_unref(opc_obexd_conn);
2626                  opc_obexd_conn = NULL;
2627         }
2628 }
2629
2630 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
2631                 int subscribe)
2632 {
2633         if (conn == NULL)
2634                 return -1;
2635
2636         static int subs_interface_added_id = -1;
2637         static int subs_interface_removed_id = -1;
2638         static int subs_name_owner_id = -1;
2639         static int subs_property_id = -1;
2640         static int subs_adapter_id = -1;
2641
2642         if (subscribe) {
2643                 if (subs_interface_added_id == -1) {
2644                         subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
2645                                 NULL, BT_MANAGER_INTERFACE,
2646                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
2647                                 __bt_manager_event_filter,
2648                                 NULL, NULL);
2649                 }
2650                 if (subs_interface_removed_id == -1) {
2651                         subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
2652                                 NULL, BT_MANAGER_INTERFACE,
2653                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
2654                                 __bt_manager_event_filter,
2655                                 NULL, NULL);
2656                 }
2657                 if (subs_name_owner_id == -1) {
2658                         subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
2659                                 NULL, BT_FREEDESKTOP_INTERFACE,
2660                                 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
2661                                 __bt_manager_event_filter,
2662                                 NULL, NULL);
2663                 }
2664                 if (subs_property_id == -1) {
2665                         subs_property_id = g_dbus_connection_signal_subscribe(conn,
2666                                 NULL, BT_PROPERTIES_INTERFACE,
2667                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
2668                                 __bt_manager_event_filter,
2669                                 NULL, NULL);
2670                 }
2671                 if (subs_adapter_id == -1) {
2672                         subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
2673                                 NULL, BT_ADAPTER_INTERFACE,
2674                                 NULL, NULL, NULL, 0,
2675                                 __bt_manager_event_filter,
2676                                 NULL, NULL);
2677                 }
2678         } else {
2679                 if (subs_interface_added_id != -1) {
2680                         g_dbus_connection_signal_unsubscribe(conn,
2681                                         subs_interface_added_id);
2682                         subs_interface_added_id = -1;
2683                 }
2684                 if (subs_interface_removed_id != -1) {
2685                         g_dbus_connection_signal_unsubscribe(conn,
2686                                         subs_interface_removed_id);
2687                         subs_interface_removed_id = -1;
2688                 }
2689                 if (subs_name_owner_id != -1) {
2690                         g_dbus_connection_signal_unsubscribe(conn,
2691                                         subs_name_owner_id);
2692                         subs_name_owner_id = -1;
2693                 }
2694                 if (subs_property_id != -1) {
2695                         g_dbus_connection_signal_unsubscribe(conn,
2696                                         subs_property_id);
2697                         subs_property_id = -1;
2698                 }
2699                 if (subs_adapter_id == -1) {
2700                         g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
2701                         subs_adapter_id = -1;
2702                 }
2703         }
2704         return 0;
2705 }
2706
2707 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
2708                 int subscribe)
2709 {
2710         if (conn == NULL)
2711                 return -1;
2712
2713         static int subs_device_id = -1;
2714
2715         if (subscribe) {
2716                 if (subs_device_id == -1) {
2717                         subs_device_id = g_dbus_connection_signal_subscribe(conn,
2718                                 NULL, BT_DEVICE_INTERFACE,
2719                                 NULL, NULL, NULL, 0,
2720                                 __bt_manager_event_filter,
2721                                 NULL, NULL);
2722                 }
2723         } else {
2724                 if (subs_device_id != -1) {
2725                         g_dbus_connection_signal_unsubscribe(conn,
2726                                         subs_device_id);
2727                         subs_device_id = -1;
2728                 }
2729         }
2730         return 0;
2731 }
2732
2733 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
2734                 int subscribe)
2735 {
2736         if (conn == NULL)
2737                 return -1;
2738
2739         static int subs_input_id = -1;
2740
2741         if (subscribe) {
2742                 if (subs_input_id == -1) {
2743                         subs_input_id = g_dbus_connection_signal_subscribe(conn,
2744                                 NULL, BT_INPUT_INTERFACE,
2745                                 NULL, NULL, NULL, 0,
2746                                 __bt_manager_event_filter,
2747                                 NULL, NULL);
2748                 }
2749         } else {
2750                 if (subs_input_id != -1) {
2751                         g_dbus_connection_signal_unsubscribe(conn,
2752                                         subs_input_id);
2753                         subs_input_id = -1;
2754                 }
2755         }
2756         return 0;
2757 }
2758
2759 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
2760                 int subscribe)
2761 {
2762         if (conn == NULL)
2763                 return -1;
2764
2765         static int subs_serv_id = -1;
2766         static int subs_client_id = -1;
2767
2768         if (subscribe) {
2769                 if (subs_serv_id == -1) {
2770                         subs_serv_id = g_dbus_connection_signal_subscribe(conn,
2771                                 NULL, BT_NETWORK_SERVER_INTERFACE,
2772                                 NULL, NULL, NULL, 0,
2773                                 __bt_manager_event_filter,
2774                                 NULL, NULL);
2775                 }
2776                 if (subs_client_id == -1) {
2777                         subs_client_id = g_dbus_connection_signal_subscribe(conn,
2778                                 NULL, BT_NETWORK_CLIENT_INTERFACE,
2779                                 NULL, NULL, NULL, 0,
2780                                 __bt_manager_event_filter,
2781                                 NULL, NULL);
2782                 }
2783         } else {
2784                 if (subs_serv_id != -1) {
2785                         g_dbus_connection_signal_unsubscribe(conn,
2786                                         subs_serv_id);
2787                         subs_serv_id = -1;
2788                 }
2789                 if (subs_client_id != -1) {
2790                         g_dbus_connection_signal_unsubscribe(conn,
2791                                         subs_client_id);
2792                         subs_client_id = -1;
2793                 }
2794         }
2795         return 0;
2796 }
2797
2798 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
2799                 int subscribe)
2800 {
2801         if (conn == NULL)
2802                 return -1;
2803
2804         static int subs_headset_id = -1;
2805         static int subs_sink_id = -1;
2806
2807         if (subscribe) {
2808                 if (subs_headset_id == -1) {
2809                         subs_headset_id = g_dbus_connection_signal_subscribe(conn,
2810                                 NULL, BT_HEADSET_INTERFACE,
2811                                 NULL, NULL, NULL, 0,
2812                                 __bt_manager_event_filter,
2813                                 NULL, NULL);
2814                 }
2815                 if (subs_sink_id == -1) {
2816                         subs_sink_id = g_dbus_connection_signal_subscribe(conn,
2817                                 NULL, BT_SINK_INTERFACE,
2818                                 NULL, NULL, NULL, 0,
2819                                 __bt_manager_event_filter,
2820                                 NULL, NULL);
2821                 }
2822         } else {
2823                 if (subs_headset_id != -1) {
2824                         g_dbus_connection_signal_unsubscribe(conn,
2825                                         subs_headset_id);
2826                         subs_headset_id = -1;
2827                 }
2828                 if (subs_sink_id != -1) {
2829                         g_dbus_connection_signal_unsubscribe(conn,
2830                                         subs_sink_id);
2831                         subs_sink_id = -1;
2832                 }
2833         }
2834         return 0;
2835 }
2836
2837 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
2838                 int subscribe)
2839 {
2840         if (conn == NULL)
2841                 return -1;
2842
2843         static int subs_opp_server_interface_added_id = -1;
2844         static int subs_opp_server_interface_removed_id = -1;
2845         static int subs_opp_server_property_id = -1;
2846
2847
2848         if (subscribe) {
2849                 if (subs_opp_server_interface_added_id == -1) {
2850                         subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
2851                                 NULL, BT_MANAGER_INTERFACE,
2852                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
2853                                 __bt_obexd_event_filter,
2854                                 NULL, NULL);
2855                 }
2856                 if (subs_opp_server_interface_removed_id == -1) {
2857                         subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
2858                                 NULL, BT_MANAGER_INTERFACE,
2859                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
2860                                 __bt_obexd_event_filter,
2861                                 NULL, NULL);
2862                 }
2863                 if (subs_opp_server_property_id == -1) {
2864                         subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
2865                                 NULL, BT_PROPERTIES_INTERFACE,
2866                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
2867                                 __bt_obexd_event_filter,
2868                                 NULL, NULL);
2869                 }
2870         } else {
2871                 if (subs_opp_server_interface_added_id != -1) {
2872                         g_dbus_connection_signal_unsubscribe(conn,
2873                                         subs_opp_server_interface_added_id);
2874                         subs_opp_server_interface_added_id = -1;
2875                 }
2876                 if (subs_opp_server_interface_removed_id != -1) {
2877                         g_dbus_connection_signal_unsubscribe(conn,
2878                                         subs_opp_server_interface_removed_id);
2879                         subs_opp_server_interface_removed_id = -1;
2880                 }
2881                 if (subs_opp_server_property_id != -1) {
2882                         g_dbus_connection_signal_unsubscribe(conn,
2883                                         subs_opp_server_property_id);
2884                         subs_opp_server_property_id = -1;
2885                 }
2886         }
2887         return 0;
2888 }
2889
2890 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
2891                 int subscribe)
2892 {
2893         if (conn == NULL)
2894                 return -1;
2895
2896         static int subs_opp_client_interface_added_id = -1;
2897         static int subs_opp_client_interface_removed_id = -1;
2898         static int subs_opp_client_property_id = -1;
2899
2900
2901         if (subscribe) {
2902                 if (subs_opp_client_interface_added_id == -1) {
2903                         subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
2904                                 NULL, BT_MANAGER_INTERFACE,
2905                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
2906                                 __bt_opc_event_filter,
2907                                 NULL, NULL);
2908                 }
2909                 if (subs_opp_client_interface_removed_id == -1) {
2910                         subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
2911                                 NULL, BT_MANAGER_INTERFACE,
2912                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
2913                                 __bt_opc_event_filter,
2914                                 NULL, NULL);
2915                 }
2916                 if (subs_opp_client_property_id == -1) {
2917                         subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
2918                                 NULL, BT_PROPERTIES_INTERFACE,
2919                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
2920                                 __bt_opc_event_filter,
2921                                 NULL, NULL);
2922                 }
2923         } else {
2924                 if (subs_opp_client_interface_added_id != -1) {
2925                         g_dbus_connection_signal_unsubscribe(conn,
2926                                         subs_opp_client_interface_added_id);
2927                         subs_opp_client_interface_added_id = -1;
2928                 }
2929                 if (subs_opp_client_interface_removed_id != -1) {
2930                         g_dbus_connection_signal_unsubscribe(conn,
2931                                         subs_opp_client_interface_removed_id);
2932                         subs_opp_client_interface_removed_id = -1;
2933                 }
2934                 if (subs_opp_client_property_id != -1) {
2935                         g_dbus_connection_signal_unsubscribe(conn,
2936                                         subs_opp_client_property_id);
2937                         subs_opp_client_property_id = -1;
2938                 }
2939         }
2940         return 0;
2941 }
2942
2943 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
2944                 int subscribe)
2945 {
2946         if (conn == NULL)
2947                 return -1;
2948
2949         static int subs_a2dp_source_id = -1;
2950         static int subs_a2dp_sink_id = -1;
2951
2952         if (subscribe) {
2953                 if (subs_a2dp_source_id == -1) {
2954                         subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
2955                                 NULL, BT_A2DP_SOURCE_INTERFACE,
2956                                 NULL, NULL, NULL, 0,
2957                                 __bt_opc_event_filter,
2958                                 NULL, NULL);
2959                 }
2960                 if (subs_a2dp_sink_id == -1) {
2961                         subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
2962                                 NULL, BT_SINK_INTERFACE,
2963                                 NULL, NULL, NULL, 0,
2964                                 __bt_opc_event_filter,
2965                                 NULL, NULL);
2966                 }
2967         } else {
2968                 if (subs_a2dp_source_id != -1) {
2969                         g_dbus_connection_signal_unsubscribe(conn,
2970                                         subs_a2dp_source_id);
2971                         subs_a2dp_source_id = -1;
2972                 }
2973                 if (subs_a2dp_sink_id != -1) {
2974                         g_dbus_connection_signal_unsubscribe(conn,
2975                                         subs_a2dp_sink_id);
2976                         subs_a2dp_sink_id = -1;
2977                 }
2978         }
2979         return 0;
2980 }
2981
2982 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
2983 {
2984         BT_DBG("+");
2985
2986         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2987
2988         switch (event_type) {
2989         case BT_MANAGER_EVENT:
2990                 _bt_register_manager_subscribe_signal(g_conn, TRUE);
2991                 break;
2992         case BT_DEVICE_EVENT:
2993                 _bt_register_device_subscribe_signal(g_conn, TRUE);
2994                 break;
2995         case BT_HID_EVENT:
2996                 _bt_register_input_subscribe_signal(g_conn, TRUE);
2997                 break;
2998         case BT_NETWORK_EVENT:
2999                 _bt_register_network_subscribe_signal(g_conn, TRUE);
3000                 break;
3001         case BT_HEADSET_EVENT:
3002                 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3003                 break;
3004
3005         case BT_OPP_SERVER_EVENT:
3006                 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3007                 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3008                 break;
3009         case BT_OPP_CLIENT_EVENT:
3010                 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3011                 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3012                 break;
3013         case BT_A2DP_SOURCE_EVENT:
3014                 BT_INFO("A2dp Source event");
3015                 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3016                 break;
3017         default:
3018                 BT_ERR("Unknown event");
3019                 return BLUETOOTH_ERROR_INTERNAL;
3020         }
3021
3022         return BLUETOOTH_ERROR_NONE;
3023 }
3024
3025 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3026 {
3027         BT_DBG("+");
3028
3029         ret_if(g_conn == NULL);
3030
3031         switch (event_type) {
3032         case BT_MANAGER_EVENT:
3033                 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3034                 _bt_register_device_subscribe_signal(g_conn, FALSE);
3035                 _bt_register_input_subscribe_signal(g_conn, FALSE);
3036                 _bt_register_network_subscribe_signal(g_conn, FALSE);
3037                 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3038                 break;
3039         case BT_OPP_SERVER_EVENT:
3040                 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3041                 break;
3042         case BT_OPP_CLIENT_EVENT:
3043                 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3044                 break;
3045         default:
3046                 BT_ERR("Unknown event");
3047                 return;
3048         }
3049
3050         BT_DBG("-");
3051 }
3052
3053 static int __bt_init_manager_receiver(void)
3054 {
3055         BT_DBG("+");
3056
3057         GError *error = NULL;
3058
3059         if (manager_conn == NULL) {
3060                 manager_conn =  g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3061                 if (error != NULL) {
3062                         BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3063                         g_clear_error(&error);
3064                 }
3065                 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3066         }
3067
3068         if (_bt_register_service_event(manager_conn,
3069                                 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3070                 goto fail;
3071         if (_bt_register_service_event(manager_conn,
3072                                 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3073                 goto fail;
3074
3075         if (_bt_register_service_event(manager_conn,
3076                                 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3077                 goto fail;
3078
3079         if (_bt_register_service_event(manager_conn,
3080                                 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3081                 goto fail;
3082
3083         if (_bt_register_service_event(manager_conn,
3084                                 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3085                 goto fail;
3086         return BLUETOOTH_ERROR_NONE;
3087 fail:
3088         if (manager_conn) {
3089                 g_object_unref(manager_conn);
3090                 manager_conn = NULL;
3091         }
3092
3093         BT_DBG("-");
3094
3095         return BLUETOOTH_ERROR_INTERNAL;
3096 }
3097
3098 static int __bt_init_obexd_receiver(void)
3099 {
3100         BT_DBG("+");
3101 #ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3102         GError *error = NULL;
3103
3104         if (obexd_conn == NULL) {
3105                 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3106                 if (error != NULL) {
3107                         BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3108                         g_clear_error(&error);
3109                 }
3110                 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3111         }
3112
3113         if (_bt_register_service_event(obexd_conn,
3114                                 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3115                 BT_ERR("Error while registering service event");
3116                 g_object_unref(obexd_conn);
3117                 obexd_conn = NULL;
3118                 return BLUETOOTH_ERROR_INTERNAL;
3119         }
3120 #endif
3121         BT_DBG("-");
3122
3123         return BLUETOOTH_ERROR_NONE;
3124 }
3125
3126 /* To receive the event from bluez */
3127 int _bt_init_service_event_receiver(void)
3128 {
3129         BT_DBG("+");
3130
3131         int result;
3132
3133         result = __bt_init_manager_receiver();
3134         retv_if(result != BLUETOOTH_ERROR_NONE, result);
3135
3136         result = __bt_init_obexd_receiver();
3137         if (result != BLUETOOTH_ERROR_NONE)
3138                 BT_ERR("Fail to init obexd receiver");
3139
3140         BT_DBG("-");
3141
3142         return BLUETOOTH_ERROR_NONE;
3143 }
3144
3145 void _bt_deinit_service_event_receiver(void)
3146 {
3147         BT_DBG("+");
3148
3149         _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3150
3151         _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3152
3153         if (manager_conn) {
3154                 g_object_unref(manager_conn);
3155                 manager_conn = NULL;
3156         }
3157
3158         if (obexd_conn) {
3159                 g_object_unref(obexd_conn);
3160                 obexd_conn = NULL;
3161         }
3162
3163         if (event_id > 0)
3164                 g_source_remove(event_id);
3165
3166         BT_DBG("-");
3167 }