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