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