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