Enhance debug message
[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                 g_free(address);
2646                 g_free(name);
2647         } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2648                 bt_rfcomm_server_info_t *server_info;
2649
2650                 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2651
2652                 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2653                 ret_if(server_info == NULL);
2654                 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2655                 param = g_variant_new("(isssn)", result, address, uuid, name,
2656                                         server_info->control_fd);
2657                 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2658                         BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2659                         param);
2660                 g_free(address);
2661                 g_free(uuid);
2662                 g_free(name);
2663         }
2664 }
2665
2666 void _bt_handle_tds_provider_event(GVariant *msg, const char *member, const char *path)
2667 {
2668         BT_INFO("member: %s, path: %s", member, path);
2669
2670         ret_if(path == NULL);
2671
2672         if (strcasecmp(member, "TdsActivationRequested") == 0) {
2673                 GVariant *value = NULL;
2674                 int len = 0;
2675                 unsigned char *buffer = NULL;
2676                 unsigned char org_id;
2677
2678                 g_variant_get(msg, "(y@ay)", &org_id, &value);
2679                 BT_DBG("org_id: %.2x", org_id);
2680                 len = g_variant_get_size(value);
2681                 if (len > 0) {
2682                         int i;
2683                         buffer = (unsigned char *)g_variant_get_data(value);
2684                         for (i = 0; i < len; i++)
2685                                 BT_DBG("%.2x", buffer[i]);
2686                 }
2687
2688                 /* Send event only registered client */
2689                 _bt_tds_handle_activation_request(path, org_id, buffer, len);
2690                 g_variant_unref(value);
2691         }
2692 }
2693
2694 static int __bt_get_object_path(GVariant *msg, char **path)
2695 {
2696         g_variant_get(msg, "(o*)", path, NULL);
2697         if (*path == NULL)
2698                 return BLUETOOTH_ERROR_INTERNAL;
2699
2700         return BLUETOOTH_ERROR_NONE;
2701 }
2702
2703 static void __bt_devices_list_free(void)
2704 {
2705         bt_cache_info_t *cache_info;
2706         GList *node;
2707
2708         node = g_list_first(p_cache_list);
2709
2710         while (node != NULL) {
2711                 cache_info = (bt_cache_info_t *)node->data;
2712                 p_cache_list = g_list_remove(p_cache_list, cache_info);
2713                 __bt_free_cache_info(cache_info);
2714
2715                 node = g_list_next(node);
2716         }
2717 }
2718
2719 static int __bt_parse_event(GVariant *msg)
2720 {
2721         GVariantIter iter;
2722         GVariant *child;
2723         char *interface_name = NULL;
2724         GVariant *inner_iter = NULL;
2725
2726         g_variant_iter_init(&iter, msg);
2727
2728         while ((child = g_variant_iter_next_value(&iter))) {
2729                 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
2730                 if (g_strcmp0(interface_name,
2731                                 BT_DEVICE_INTERFACE) == 0) {
2732                         g_variant_unref(inner_iter);
2733                         g_variant_unref(child);
2734                         return BT_DEVICE_EVENT;
2735                 } else if (g_strcmp0(interface_name,
2736                                 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2737                         g_variant_unref(inner_iter);
2738                         g_variant_unref(child);
2739                         return BT_MEDIA_TRANSFER_EVENT;
2740                 } else if (g_strcmp0(interface_name,
2741                                 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2742                         g_variant_unref(inner_iter);
2743                         g_variant_unref(child);
2744                         return BT_AVRCP_CONTROL_EVENT;
2745                 }
2746                 g_variant_unref(inner_iter);
2747                 g_variant_unref(child);
2748         }
2749
2750         return 0;
2751 }
2752
2753 static  void __bt_manager_event_filter(GDBusConnection *connection,
2754                                         const gchar *sender_name,
2755                                         const gchar *object_path,
2756                                         const gchar *interface_name,
2757                                         const gchar *signal_name,
2758                                         GVariant *parameters,
2759                                         gpointer user_data)
2760 {
2761         bt_event_type_t bt_event = 0x00;
2762         int result = BLUETOOTH_ERROR_NONE;
2763         GVariant *value;
2764         char *obj_path = NULL;
2765         GVariant *param = NULL;
2766         if (signal_name == NULL)
2767                 return;
2768         if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2769                 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2770
2771                 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
2772                         if (TIZEN_FEATURE_BT_USB_DONGLE) {
2773                                 BT_DBG("Enable Adapter");
2774                                 _bt_enable_adapter();
2775                         } else {
2776                                 _bt_handle_adapter_added();
2777                         }
2778                 } else {
2779                         bt_event = __bt_parse_event(value);
2780                         if (bt_event == BT_DEVICE_EVENT) {
2781                                 bt_cache_info_t *cache_info;
2782                                 bt_remote_dev_info_t *dev_info;
2783 #ifdef TIZEN_FEATURE_BT_DPM
2784                                 int desktop_state = DPM_BT_ERROR;
2785 #endif
2786                                 if (_bt_is_discovering() == FALSE &&
2787                                     _bt_is_le_scanning() == FALSE) {
2788                                         g_variant_unref(value);
2789                                         return;
2790                                 }
2791
2792                                 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2793
2794                                 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2795
2796                                 cache_info->dev_info = dev_info;
2797
2798                                 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2799                                         BT_ERR("Fail to parse the properies");
2800                                         __bt_free_cache_info(cache_info);
2801                                         g_variant_unref(value);
2802                                         return;
2803                                 }
2804
2805                                 if (dev_info->addr_type != BDADDR_BREDR) {
2806                                         /* Whenever emit the property changed from bluez,
2807                                                 some property doesn't reach to bt-service.
2808                                                 So LE device is handled as AdvReport signal */
2809                                         __bt_free_cache_info(cache_info);
2810                                         g_variant_unref(value);
2811                                         return;
2812                                 }
2813
2814                                 if (dev_info->name == NULL)
2815                                         /* If Remote device name is NULL or still RNR is not done
2816                                          * then display address as name.
2817                                          */
2818                                         dev_info->name = g_strdup(dev_info->address);
2819
2820 #ifdef TIZEN_FEATURE_BT_DPM
2821                                 _bt_dpm_get_bluetooth_desktop_connectivity_state(&desktop_state);
2822                                 if (desktop_state == DPM_RESTRICTED) {
2823                                         bluetooth_device_class_t device_class;
2824                                         _bt_divide_device_class(&device_class, dev_info->class);
2825                                         BT_DBG("[%s]device_class.major_class : %d", dev_info->name, device_class.major_class);
2826
2827                                         if (device_class.major_class ==
2828                                                 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
2829                                                 __bt_free_cache_info(cache_info);
2830                                                 g_variant_unref(value);
2831                                                 return;
2832                                         }
2833                                 }
2834 #endif
2835
2836                                 GVariant *uuids = NULL;
2837                                 GVariantBuilder *builder = NULL;
2838                                 int i = 0;
2839                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2840                                 for (i = 0; i < dev_info->uuid_count; i++) {
2841                                         g_variant_builder_add(builder, "s",
2842                                                 dev_info->uuids[i]);
2843                                 }
2844                                 uuids = g_variant_new("as", builder);
2845                                 g_variant_builder_unref(builder);
2846                                 GVariant *manufacturer_data = NULL;
2847                                 manufacturer_data = g_variant_new_from_data(
2848                                                         G_VARIANT_TYPE_BYTESTRING,
2849                                                         dev_info->manufacturer_data,
2850                                                         dev_info->manufacturer_data_len,
2851                                                         TRUE, NULL, NULL);
2852                                 param = g_variant_new("(isunsbub@asn@ay)", result,
2853                                                         dev_info->address,
2854                                                         dev_info->class,
2855                                                         dev_info->rssi,
2856                                                         dev_info->name,
2857                                                         dev_info->paired,
2858                                                         dev_info->connected,
2859                                                         dev_info->trust,
2860                                                         uuids,
2861                                                         dev_info->manufacturer_data_len,
2862                                                         manufacturer_data);
2863                                 _bt_send_event(BT_ADAPTER_EVENT,
2864                                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2865                                          param);
2866                                 p_cache_list = g_list_append(p_cache_list, cache_info);
2867                         } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2868                                 BT_DBG("Device path : %s ", obj_path);
2869                                 _bt_set_control_device_path(obj_path);
2870                         }
2871                 }
2872                 g_variant_unref(value);
2873         } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2874                 if (TIZEN_FEATURE_BT_USB_DONGLE) {
2875                         BT_DBG("InterfacesRemoved");
2876                         _bt_handle_adapter_removed();
2877                 }
2878                 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0)
2879                         bt_event = BT_MEDIA_TRANSFER_EVENT;
2880                 else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0)
2881                         bt_event = BT_DEVICE_EVENT;
2882                 else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0)
2883                         bt_event = BT_AVRCP_CONTROL_EVENT;
2884
2885                 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2886                         _bt_handle_adapter_event(parameters, signal_name);
2887                         if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2888                                 BT_INFO("Object Path %s", obj_path);
2889                                 _bt_remove_control_device_path(obj_path);
2890                         }
2891                 }
2892         } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2893                 gboolean value;
2894                 char *name = NULL;
2895                 char *previous = NULL;
2896                 char *current = NULL;
2897
2898                 if (__bt_get_owner_info(parameters, &name, &previous, &current)) {
2899                         BT_ERR("Fail to get the owner info");
2900                         return;
2901                 }
2902
2903                 if (*current != '\0') {
2904                         g_free(current);
2905                         if (name)
2906                                 g_free(name);
2907                         if (previous)
2908                                 g_free(previous);
2909                         return;
2910                 }
2911
2912                 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2913                         BT_INFO_C("### Bluetoothd is terminated");
2914                         if (_bt_adapter_get_status() != BT_DEACTIVATING &&
2915                                 _bt_adapter_get_status() != BT_DEACTIVATED) {
2916                                 BT_INFO_C("Turn Off Bluetooth");
2917                                 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
2918                                 _bt_disable_cb();
2919                         }
2920                         _bt_handle_adapter_removed();
2921                         __bt_devices_list_free();
2922                         _bt_service_adapter_le_deinit();
2923                 }
2924
2925                 _bt_obex_server_check_allocation(&value);
2926
2927                 if (value == TRUE) {
2928                         /* Check if the obex server was terminated abnormally */
2929                         _bt_obex_server_check_termination(name);
2930                 }
2931
2932                 _bt_rfcomm_server_check_existence(&value);
2933
2934                 if (value == TRUE) {
2935                         /* The obex server was terminated abnormally */
2936                         _bt_rfcomm_server_check_termination(name);
2937                 }
2938
2939                 /* Reset connection interval */
2940                 _bt_remove_all_le_conn_param_info(name);
2941
2942                 /* Stop advertising started by terminated process */
2943                 _bt_stop_advertising_by_terminated_process(name);
2944                 /* Stop LE Scan */
2945                 _bt_stop_le_scan(name);
2946
2947                 /* Stop the Proximity reporter service */
2948                 _bt_proximity_reporter_stop_by_terminated_process(name);
2949
2950                 /* Stop the Transport Discovery service */
2951                 _bt_tds_stop_by_terminated_process(name);
2952
2953                 g_free(name);
2954                 g_free(previous);
2955                 g_free(current);
2956         } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2957                 const char *path = object_path;
2958
2959                 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2960                                 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2961                         return;
2962
2963                 _bt_handle_property_changed_event(parameters, object_path);
2964         } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2965                 _bt_handle_adapter_event(parameters, signal_name);
2966         } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2967                 _bt_handle_input_event(parameters, object_path);
2968         } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2969                 _bt_handle_network_server_event(parameters, signal_name);
2970         } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2971                 _bt_handle_headset_event(parameters, object_path);
2972         } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2973                 _bt_handle_sink_event(parameters, object_path);
2974         } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2975                 _bt_handle_agent_event(parameters, signal_name);
2976         } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2977                 _bt_handle_device_event(parameters, signal_name, object_path);
2978         } else if (g_strcmp0(interface_name, BT_TDS_PROVIDER_INTERFACE) == 0) {
2979                 _bt_handle_tds_provider_event(parameters, signal_name, object_path);
2980         } else if (g_strcmp0(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
2981                 _bt_handle_gatt_event(parameters, signal_name, object_path);
2982         }
2983
2984         return;
2985 }
2986
2987 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2988 {
2989
2990         if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2991                 char *interface_name = NULL;
2992
2993                 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2994                 retv_if(interface_name == NULL, FALSE);
2995
2996                 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2997                         BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2998                         return TRUE;
2999                 }
3000         }
3001
3002         return FALSE;
3003 }
3004
3005 static  void __bt_obexd_event_filter(GDBusConnection *connection,
3006                                         const gchar *sender_name,
3007                                         const gchar *object_path,
3008                                         const gchar *interface_name,
3009                                         const gchar *signal_name,
3010                                         GVariant *parameters,
3011                                         gpointer user_data)
3012 {
3013         const char *member = signal_name;
3014         char *obj_path = NULL;
3015         ret_if(member == NULL);
3016
3017         if (strcasecmp(member, "InterfacesAdded") == 0) {
3018                 if (__bt_get_object_path(parameters, &obj_path)) {
3019                         BT_ERR("Fail to get the path");
3020                         return;
3021                 }
3022                 BT_INFO("object_path = [%s]", obj_path);
3023
3024                 /*Handle OPP_SERVER_CONNECTED_EVENT here */
3025                 /* TODO: MAP? see above */
3026                 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
3027                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
3028                         g_free(obj_path);
3029                         return;
3030                 }
3031
3032                 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
3033                         BT_DBG("Obex_Server_Session_Transfer connected");
3034                         _bt_obex_transfer_connected(obj_path);
3035                 }
3036                 g_free(obj_path);
3037         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
3038                 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
3039                 /* TODO: MAP? see above */
3040                 if (__bt_get_object_path(parameters, &obj_path)) {
3041                         BT_ERR("Fail to get the path");
3042                         return;
3043                 }
3044                 BT_INFO("object_path = [%s]", obj_path);
3045
3046                 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
3047                                 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
3048                         BT_DBG("Call PBAP Disconnected");
3049                         _bt_obex_pbap_client_disconnect(obj_path);
3050                 }
3051
3052                 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
3053                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
3054                         g_free(obj_path);
3055                         return;
3056                 }
3057
3058                 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
3059                         BT_DBG("Obex_Server_Session_Transfer disconnected %s",
3060                                                                 obj_path);
3061
3062                         _bt_obex_transfer_disconnected(obj_path);
3063                 }
3064                 g_free(obj_path);
3065         } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
3066                 const char *path = object_path;
3067
3068                 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
3069                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
3070                         strncmp(path, BT_SESSION_BASEPATH_CLIENT,
3071                                 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
3072                         BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
3073                         return;
3074                 }
3075
3076                 _bt_handle_property_changed_event(parameters, path);
3077         }
3078         BT_DBG("-");
3079         return;
3080 }
3081
3082 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
3083 {
3084         BT_DBG("+");
3085
3086         if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
3087                 char *interface_name = NULL;
3088
3089                 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
3090
3091                 retv_if(interface_name == NULL, FALSE);
3092
3093                 if (strcasecmp(interface_name,
3094                                         BT_OBEX_TRANSFER_INTERFACE) == 0) {
3095                         BT_DBG("-");
3096                         return TRUE;
3097                 }
3098         }
3099
3100         BT_DBG("-");
3101
3102         return FALSE;
3103 }
3104
3105 static  void __bt_opc_event_filter(GDBusConnection *connection,
3106                                         const gchar *sender_name,
3107                                         const gchar *object_path,
3108                                         const gchar *interface_name,
3109                                         const gchar *signal_name,
3110                                         GVariant *parameters,
3111                                         gpointer user_data)
3112 {
3113         const char *member = signal_name;
3114         char *obj_path = NULL;
3115         if (strcasecmp(member, "InterfacesAdded") == 0) {
3116                 BT_DBG("InterfacesAdded");
3117         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
3118
3119                 if (__bt_get_object_path(parameters, &obj_path)) {
3120                         BT_ERR("Fail to get the path");
3121                         return;
3122                 }
3123
3124                 BT_DBG("object_path = %s", obj_path);
3125
3126                 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
3127                                 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
3128                                 || strstr(obj_path, "transfer") == NULL) {
3129                         g_free(obj_path);
3130                         return;
3131                 } else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
3132                                 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
3133                         BT_DBG("Going to call opc disconnected");
3134                         _bt_opc_disconnected(obj_path);
3135                 }
3136
3137                 _bt_sending_files();
3138                 g_free(obj_path);
3139         } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
3140                 char *path = (char *)object_path;
3141                 BT_INFO("object_path %s", path);
3142                 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
3143                         strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
3144                         BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
3145                         return;
3146                 }
3147
3148                 _bt_opc_property_changed_event(parameters, path);
3149         }
3150
3151         return;
3152 }
3153
3154 int _bt_opp_client_event_init(void)
3155 {
3156         GError *error = NULL;
3157
3158         if (opc_obexd_conn == NULL) {
3159                 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3160
3161                 if (!opc_obexd_conn) {
3162                         if (error) {
3163                                 BT_ERR("Unable to connect to dbus: %s", error->message);
3164                                 g_clear_error(&error);
3165                         }
3166                 return BLUETOOTH_ERROR_INTERNAL;
3167                 }
3168         }
3169
3170         if (_bt_register_service_event(opc_obexd_conn,
3171                         BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
3172                         g_object_unref(opc_obexd_conn);
3173                         opc_obexd_conn = NULL;
3174                         return BLUETOOTH_ERROR_INTERNAL;
3175         }
3176
3177         return BLUETOOTH_ERROR_NONE;
3178 }
3179
3180 void _bt_opp_client_event_deinit(void)
3181 {
3182         if (opc_obexd_conn) {
3183                 _bt_unregister_service_event(opc_obexd_conn,
3184                                                 BT_OPP_CLIENT_EVENT);
3185                  g_object_unref(opc_obexd_conn);
3186                  opc_obexd_conn = NULL;
3187         }
3188 }
3189
3190 static  void __bt_map_event_filter(GDBusConnection *connection,
3191                                         const gchar *sender_name,
3192                                         const gchar *object_path,
3193                                         const gchar *interface_name,
3194                                         const gchar *signal_name,
3195                                         GVariant *parameters,
3196                                         gpointer user_data)
3197 {
3198         BT_DBG("Entered __bt_map_event_filter");
3199         const char *member = signal_name;
3200
3201         if (strcasecmp(member, "InterfacesAdded") == 0) {
3202                 BT_DBG("------------------------------------ADDED------------------------------------");
3203                 // currently doing nothing
3204         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
3205                 BT_DBG("------------------------------------REMOVED------------------------------------");
3206                 // TODO check if something should be called here?
3207                 //_bt_map_on_transfer_finished(object_path, error);
3208         } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
3209                 BT_DBG("------------------------------------CLIENT EVENT------------------------------------");
3210                 _bt_map_property_changed_event(parameters, object_path);
3211         }
3212
3213         return;
3214 }
3215
3216 int _bt_map_client_event_init(void)
3217 {
3218         GError *error = NULL;
3219
3220         if (map_obexd_conn == NULL) {
3221                 map_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3222
3223                 if (!map_obexd_conn) {
3224                         if (error) {
3225                                 BT_ERR("Unable to connect to dbus: %s", error->message);
3226                                 g_clear_error(&error);
3227                         }
3228                 return BLUETOOTH_ERROR_INTERNAL;
3229                 }
3230         }
3231
3232         if (_bt_register_service_event(map_obexd_conn,
3233                         BT_MAP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
3234                         g_object_unref(map_obexd_conn);
3235                         map_obexd_conn = NULL;
3236                         return BLUETOOTH_ERROR_INTERNAL;
3237         }
3238
3239         return BLUETOOTH_ERROR_NONE;
3240 }
3241
3242 void _bt_map_client_event_deinit(void)
3243 {
3244         if (map_obexd_conn) {
3245                 _bt_unregister_service_event(map_obexd_conn,
3246                                                 BT_MAP_CLIENT_EVENT);
3247                  g_object_unref(map_obexd_conn);
3248                  map_obexd_conn = NULL;
3249         }
3250 }
3251
3252 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
3253                 int subscribe)
3254 {
3255         if (conn == NULL)
3256                 return -1;
3257
3258         static guint subs_interface_added_id;
3259         static guint subs_interface_removed_id;
3260         static guint subs_name_owner_id;
3261         static guint subs_property_id;
3262         static guint subs_adapter_id;
3263         static guint subs_gatt_id;
3264
3265         if (subscribe) {
3266                 if (subs_interface_added_id == 0) {
3267                         subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3268                                 NULL, BT_MANAGER_INTERFACE,
3269                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3270                                 __bt_manager_event_filter,
3271                                 NULL, NULL);
3272                 }
3273                 if (subs_interface_removed_id == 0) {
3274                         subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3275                                 NULL, BT_MANAGER_INTERFACE,
3276                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3277                                 __bt_manager_event_filter,
3278                                 NULL, NULL);
3279                 }
3280                 if (subs_name_owner_id == 0) {
3281                         subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
3282                                 NULL, BT_FREEDESKTOP_INTERFACE,
3283                                 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
3284                                 __bt_manager_event_filter,
3285                                 NULL, NULL);
3286                 }
3287                 if (subs_property_id == 0) {
3288                         subs_property_id = g_dbus_connection_signal_subscribe(conn,
3289                                 NULL, BT_PROPERTIES_INTERFACE,
3290                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3291                                 __bt_manager_event_filter,
3292                                 NULL, NULL);
3293                 }
3294                 if (subs_adapter_id == 0) {
3295                         subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
3296                                 NULL, BT_ADAPTER_INTERFACE,
3297                                 NULL, NULL, NULL, 0,
3298                                 __bt_manager_event_filter,
3299                                 NULL, NULL);
3300                 }
3301                 if (subs_gatt_id == 0) {
3302                         subs_gatt_id = g_dbus_connection_signal_subscribe(conn,
3303                                 NULL, BT_GATT_CHAR_INTERFACE,
3304                                 NULL, NULL, NULL, 0,
3305                                 __bt_manager_event_filter,
3306                                 NULL, NULL);
3307                 }
3308         } else {
3309                 if (subs_interface_added_id > 0) {
3310                         g_dbus_connection_signal_unsubscribe(conn,
3311                                         subs_interface_added_id);
3312                         subs_interface_added_id = 0;
3313                 }
3314                 if (subs_interface_removed_id > 0) {
3315                         g_dbus_connection_signal_unsubscribe(conn,
3316                                         subs_interface_removed_id);
3317                         subs_interface_removed_id = 0;
3318                 }
3319                 if (subs_name_owner_id > 0) {
3320                         g_dbus_connection_signal_unsubscribe(conn,
3321                                         subs_name_owner_id);
3322                         subs_name_owner_id = 0;
3323                 }
3324                 if (subs_property_id > 0) {
3325                         g_dbus_connection_signal_unsubscribe(conn,
3326                                         subs_property_id);
3327                         subs_property_id = 0;
3328                 }
3329                 if (subs_adapter_id > 0) {
3330                         g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
3331                         subs_adapter_id = 0;
3332                 }
3333                 if (subs_gatt_id > 0) {
3334                         g_dbus_connection_signal_unsubscribe(conn, subs_gatt_id);
3335                         subs_gatt_id = 0;
3336                 }
3337         }
3338         return 0;
3339 }
3340
3341 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
3342                 int subscribe)
3343 {
3344         if (conn == NULL)
3345                 return -1;
3346
3347         static guint subs_device_id = 0;
3348
3349         if (subscribe) {
3350                 if (subs_device_id == 0) {
3351                         subs_device_id = g_dbus_connection_signal_subscribe(conn,
3352                                 NULL, BT_DEVICE_INTERFACE,
3353                                 NULL, NULL, NULL, 0,
3354                                 __bt_manager_event_filter,
3355                                 NULL, NULL);
3356                 }
3357         } else {
3358                 if (subs_device_id > 0) {
3359                         g_dbus_connection_signal_unsubscribe(conn,
3360                                         subs_device_id);
3361                         subs_device_id = 0;
3362                 }
3363         }
3364         return 0;
3365 }
3366
3367 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
3368                 int subscribe)
3369 {
3370         if (conn == NULL)
3371                 return -1;
3372
3373         static int subs_input_id = 0;
3374
3375         if (subscribe) {
3376                 if (subs_input_id == 0) {
3377                         subs_input_id = g_dbus_connection_signal_subscribe(conn,
3378                                 NULL, BT_INPUT_INTERFACE,
3379                                 NULL, NULL, NULL, 0,
3380                                 __bt_manager_event_filter,
3381                                 NULL, NULL);
3382                 }
3383         } else {
3384                 if (subs_input_id > 0) {
3385                         g_dbus_connection_signal_unsubscribe(conn,
3386                                         subs_input_id);
3387                         subs_input_id = 0;
3388                 }
3389         }
3390         return 0;
3391 }
3392
3393 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
3394                 int subscribe)
3395 {
3396         if (conn == NULL)
3397                 return -1;
3398
3399         static guint subs_serv_id = 0;
3400         static guint subs_client_id = 0;
3401
3402         if (subscribe) {
3403                 if (subs_serv_id == 0) {
3404                         subs_serv_id = g_dbus_connection_signal_subscribe(conn,
3405                                 NULL, BT_NETWORK_SERVER_INTERFACE,
3406                                 NULL, NULL, NULL, 0,
3407                                 __bt_manager_event_filter,
3408                                 NULL, NULL);
3409                 }
3410                 if (subs_client_id == 0) {
3411                         subs_client_id = g_dbus_connection_signal_subscribe(conn,
3412                                 NULL, BT_NETWORK_CLIENT_INTERFACE,
3413                                 NULL, NULL, NULL, 0,
3414                                 __bt_manager_event_filter,
3415                                 NULL, NULL);
3416                 }
3417         } else {
3418                 if (subs_serv_id > 0) {
3419                         g_dbus_connection_signal_unsubscribe(conn,
3420                                         subs_serv_id);
3421                         subs_serv_id = 0;
3422                 }
3423                 if (subs_client_id > 0) {
3424                         g_dbus_connection_signal_unsubscribe(conn,
3425                                         subs_client_id);
3426                         subs_client_id = 0;
3427                 }
3428         }
3429         return 0;
3430 }
3431
3432 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
3433                 int subscribe)
3434 {
3435         if (conn == NULL)
3436                 return -1;
3437
3438         static guint subs_headset_id = 0;
3439         static guint subs_sink_id = 0;
3440
3441         if (subscribe) {
3442                 if (subs_headset_id == 0) {
3443                         subs_headset_id = g_dbus_connection_signal_subscribe(conn,
3444                                 NULL, BT_HEADSET_INTERFACE,
3445                                 NULL, NULL, NULL, 0,
3446                                 __bt_manager_event_filter,
3447                                 NULL, NULL);
3448                 }
3449                 if (subs_sink_id == 0) {
3450                         subs_sink_id = g_dbus_connection_signal_subscribe(conn,
3451                                 NULL, BT_SINK_INTERFACE,
3452                                 NULL, NULL, NULL, 0,
3453                                 __bt_manager_event_filter,
3454                                 NULL, NULL);
3455                 }
3456         } else {
3457                 if (subs_headset_id > 0) {
3458                         g_dbus_connection_signal_unsubscribe(conn,
3459                                         subs_headset_id);
3460                         subs_headset_id = 0;
3461                 }
3462                 if (subs_sink_id > 0) {
3463                         g_dbus_connection_signal_unsubscribe(conn,
3464                                         subs_sink_id);
3465                         subs_sink_id = 0;
3466                 }
3467         }
3468         return 0;
3469 }
3470
3471 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
3472                 int subscribe)
3473 {
3474         if (conn == NULL)
3475                 return -1;
3476
3477         static guint subs_opp_server_interface_added_id = 0;
3478         static guint subs_opp_server_interface_removed_id = 0;
3479         static guint subs_opp_server_property_id = 0;
3480
3481
3482         if (subscribe) {
3483                 if (subs_opp_server_interface_added_id == 0) {
3484                         subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3485                                 NULL, BT_MANAGER_INTERFACE,
3486                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3487                                 __bt_obexd_event_filter,
3488                                 NULL, NULL);
3489                 }
3490                 if (subs_opp_server_interface_removed_id == 0) {
3491                         subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3492                                 NULL, BT_MANAGER_INTERFACE,
3493                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3494                                 __bt_obexd_event_filter,
3495                                 NULL, NULL);
3496                 }
3497                 if (subs_opp_server_property_id == 0) {
3498                         subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
3499                                 NULL, BT_PROPERTIES_INTERFACE,
3500                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3501                                 __bt_obexd_event_filter,
3502                                 NULL, NULL);
3503                 }
3504         } else {
3505                 if (subs_opp_server_interface_added_id > 0) {
3506                         g_dbus_connection_signal_unsubscribe(conn,
3507                                         subs_opp_server_interface_added_id);
3508                         subs_opp_server_interface_added_id = 0;
3509                 }
3510                 if (subs_opp_server_interface_removed_id > 0) {
3511                         g_dbus_connection_signal_unsubscribe(conn,
3512                                         subs_opp_server_interface_removed_id);
3513                         subs_opp_server_interface_removed_id = 0;
3514                 }
3515                 if (subs_opp_server_property_id > 0) {
3516                         g_dbus_connection_signal_unsubscribe(conn,
3517                                         subs_opp_server_property_id);
3518                         subs_opp_server_property_id = 0;
3519                 }
3520         }
3521         return 0;
3522 }
3523
3524 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
3525                 int subscribe)
3526 {
3527         if (conn == NULL)
3528                 return -1;
3529
3530         static guint subs_opp_client_interface_added_id = 0;
3531         static guint subs_opp_client_interface_removed_id = 0;
3532         static guint subs_opp_client_property_id = 0;
3533
3534
3535         if (subscribe) {
3536                 if (subs_opp_client_interface_added_id == 0) {
3537                         subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3538                                 NULL, BT_MANAGER_INTERFACE,
3539                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3540                                 __bt_opc_event_filter,
3541                                 NULL, NULL);
3542                 }
3543                 if (subs_opp_client_interface_removed_id == 0) {
3544                         subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3545                                 NULL, BT_MANAGER_INTERFACE,
3546                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3547                                 __bt_opc_event_filter,
3548                                 NULL, NULL);
3549                 }
3550                 if (subs_opp_client_property_id == 0) {
3551                         subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
3552                                 NULL, BT_PROPERTIES_INTERFACE,
3553                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3554                                 __bt_opc_event_filter,
3555                                 NULL, NULL);
3556                 }
3557         } else {
3558                 if (subs_opp_client_interface_added_id > 0) {
3559                         g_dbus_connection_signal_unsubscribe(conn,
3560                                         subs_opp_client_interface_added_id);
3561                         subs_opp_client_interface_added_id = 0;
3562                 }
3563                 if (subs_opp_client_interface_removed_id > 0) {
3564                         g_dbus_connection_signal_unsubscribe(conn,
3565                                         subs_opp_client_interface_removed_id);
3566                         subs_opp_client_interface_removed_id = 0;
3567                 }
3568                 if (subs_opp_client_property_id > 0) {
3569                         g_dbus_connection_signal_unsubscribe(conn,
3570                                         subs_opp_client_property_id);
3571                         subs_opp_client_property_id = 0;
3572                 }
3573         }
3574         return 0;
3575 }
3576
3577 int _bt_register_map_client_subscribe_signal(GDBusConnection *conn,
3578                 int subscribe)
3579 {
3580         if (conn == NULL)
3581                 return -1;
3582
3583         static guint subs_map_client_interface_added_id = 0;
3584         static guint subs_map_client_interface_removed_id = 0;
3585         static guint subs_map_client_property_id = 0;
3586
3587
3588         if (subscribe) {
3589                 if (subs_map_client_interface_added_id == 0) {
3590                         subs_map_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3591                                 NULL, BT_MANAGER_INTERFACE,
3592                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3593                                 __bt_map_event_filter,
3594                                 NULL, NULL);
3595                 }
3596                 if (subs_map_client_interface_removed_id == 0) {
3597                         subs_map_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3598                                 NULL, BT_MANAGER_INTERFACE,
3599                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3600                                 __bt_map_event_filter,
3601                                 NULL, NULL);
3602                 }
3603                 if (subs_map_client_property_id == 0) {
3604                         subs_map_client_property_id = g_dbus_connection_signal_subscribe(conn,
3605                                 NULL, BT_PROPERTIES_INTERFACE,
3606                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3607                                 __bt_map_event_filter,
3608                                 NULL, NULL);
3609                 }
3610         } else {
3611                 if (subs_map_client_interface_added_id > 0) {
3612                         g_dbus_connection_signal_unsubscribe(conn,
3613                                         subs_map_client_interface_added_id);
3614                         subs_map_client_interface_added_id = 0;
3615                 }
3616                 if (subs_map_client_interface_removed_id > 0) {
3617                         g_dbus_connection_signal_unsubscribe(conn,
3618                                         subs_map_client_interface_removed_id);
3619                         subs_map_client_interface_removed_id = 0;
3620                 }
3621                 if (subs_map_client_property_id > 0) {
3622                         g_dbus_connection_signal_unsubscribe(conn,
3623                                         subs_map_client_property_id);
3624                         subs_map_client_property_id = 0;
3625                 }
3626         }
3627         return 0;
3628 }
3629
3630 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
3631                 int subscribe)
3632 {
3633         if (conn == NULL)
3634                 return -1;
3635
3636         static guint subs_a2dp_source_id = 0;
3637         static guint subs_a2dp_sink_id = 0;
3638
3639         if (subscribe) {
3640                 if (subs_a2dp_source_id == 0) {
3641                         subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
3642                                 NULL, BT_A2DP_SOURCE_INTERFACE,
3643                                 NULL, NULL, NULL, 0,
3644                                 __bt_opc_event_filter,
3645                                 NULL, NULL);
3646                 }
3647                 if (subs_a2dp_sink_id == 0) {
3648                         subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
3649                                 NULL, BT_SINK_INTERFACE,
3650                                 NULL, NULL, NULL, 0,
3651                                 __bt_opc_event_filter,
3652                                 NULL, NULL);
3653                 }
3654         } else {
3655                 if (subs_a2dp_source_id > 0) {
3656                         g_dbus_connection_signal_unsubscribe(conn,
3657                                         subs_a2dp_source_id);
3658                         subs_a2dp_source_id = 0;
3659                 }
3660                 if (subs_a2dp_sink_id > 0) {
3661                         g_dbus_connection_signal_unsubscribe(conn,
3662                                         subs_a2dp_sink_id);
3663                         subs_a2dp_sink_id = 0;
3664                 }
3665         }
3666         return 0;
3667 }
3668
3669 int _bt_register_tds_provider_subscribe_signal(GDBusConnection *conn, int subscribe)
3670 {
3671         if (conn == NULL)
3672                 return -1;
3673
3674         static guint subs_custom_id = 0;
3675
3676         if (subscribe) {
3677                 if (subs_custom_id == 0) {
3678                         subs_custom_id = g_dbus_connection_signal_subscribe(conn,
3679                                 NULL, BT_TDS_PROVIDER_INTERFACE,
3680                                 NULL, NULL, NULL, 0,
3681                                 __bt_manager_event_filter,
3682                                 NULL, NULL);
3683                 }
3684         } else {
3685                 if (subs_custom_id > 0) {
3686                         g_dbus_connection_signal_unsubscribe(conn,
3687                                         subs_custom_id);
3688                         subs_custom_id = 0;
3689                 }
3690         }
3691
3692         return 0;
3693 }
3694
3695 static void __bt_dump_event_filter(GDBusConnection *connection,
3696                                         const gchar *sender_name,
3697                                         const gchar *object_path,
3698                                         const gchar *interface_name,
3699                                         const gchar *signal_name,
3700                                         GVariant *parameters,
3701                                         gpointer user_data)
3702 {
3703         return;
3704 }
3705
3706 int __bt_register_dump_subscribe_signal(GDBusConnection *conn,
3707                 gboolean subscribe)
3708 {
3709         if (conn == NULL)
3710                 return -1;
3711
3712         static guint subs_source_id = 0;
3713
3714         if (subscribe) {
3715                 if (subs_source_id == 0) {
3716                         subs_source_id = g_dbus_connection_signal_subscribe(conn,
3717                                 NULL, BT_DUMP_SERVICE_INTERFACE,
3718                                 BT_DUMP_SERVICE_SIGNAL, BT_DUMP_SERVICE_PATH, NULL, 0,
3719                                 __bt_dump_event_filter,
3720                                 NULL, NULL);
3721                 }
3722         } else {
3723                 if (subs_source_id > 0) {
3724                         g_dbus_connection_signal_unsubscribe(conn,
3725                                         subs_source_id);
3726                         subs_source_id = 0;
3727                 }
3728         }
3729         return 0;
3730 }
3731
3732 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
3733 {
3734         BT_DBG("+");
3735
3736         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3737
3738         switch (event_type) {
3739         case BT_MANAGER_EVENT:
3740                 _bt_register_manager_subscribe_signal(g_conn, TRUE);
3741                 break;
3742         case BT_DEVICE_EVENT:
3743                 _bt_register_device_subscribe_signal(g_conn, TRUE);
3744                 break;
3745         case BT_HID_EVENT:
3746                 _bt_register_input_subscribe_signal(g_conn, TRUE);
3747                 break;
3748         case BT_NETWORK_EVENT:
3749                 _bt_register_network_subscribe_signal(g_conn, TRUE);
3750                 break;
3751         case BT_HEADSET_EVENT:
3752                 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3753                 break;
3754         case BT_OPP_SERVER_EVENT:
3755                 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3756                 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3757                 break;
3758
3759         case BT_OPP_CLIENT_EVENT:
3760                 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3761                 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3762                 break;
3763         case BT_MAP_CLIENT_EVENT:
3764                 BT_ERR("BT_MAP_CLIENT_EVENT: register service event");
3765                 _bt_register_map_client_subscribe_signal(g_conn, TRUE);
3766                 break;
3767         case BT_A2DP_SOURCE_EVENT:
3768                 BT_INFO("A2dp Source event");
3769                 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3770                 break;
3771         case BT_TDS_EVENT:
3772                 BT_INFO("TDS provider event");
3773                 _bt_register_tds_provider_subscribe_signal(g_conn, TRUE);
3774                 break;
3775         default:
3776                 BT_ERR("Unknown event");
3777                 return BLUETOOTH_ERROR_INTERNAL;
3778         }
3779
3780         return BLUETOOTH_ERROR_NONE;
3781 }
3782
3783 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3784 {
3785         BT_DBG("+");
3786
3787         ret_if(g_conn == NULL);
3788
3789         switch (event_type) {
3790         case BT_MANAGER_EVENT:
3791                 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3792                 _bt_register_device_subscribe_signal(g_conn, FALSE);
3793                 _bt_register_input_subscribe_signal(g_conn, FALSE);
3794                 _bt_register_network_subscribe_signal(g_conn, FALSE);
3795                 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3796                 break;
3797         case BT_OPP_SERVER_EVENT:
3798                 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3799                 break;
3800         case BT_OPP_CLIENT_EVENT:
3801                 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3802                 break;
3803         case BT_MAP_CLIENT_EVENT:
3804                 _bt_register_map_client_subscribe_signal(g_conn, FALSE);
3805                 break;
3806         default:
3807                 BT_ERR("Unknown event");
3808                 return;
3809         }
3810
3811         BT_DBG("-");
3812 }
3813
3814 static int __bt_init_manager_receiver(void)
3815 {
3816         BT_DBG("+");
3817
3818         GError *error = NULL;
3819
3820         if (manager_conn == NULL) {
3821                 manager_conn =  g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3822                 if (error != NULL) {
3823                         BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3824                         g_clear_error(&error);
3825                 }
3826                 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3827         }
3828
3829         if (_bt_register_service_event(manager_conn,
3830                                 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3831                 goto fail;
3832         if (_bt_register_service_event(manager_conn,
3833                                 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3834                 goto fail;
3835
3836         if (_bt_register_service_event(manager_conn,
3837                                 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3838                 goto fail;
3839
3840         if (_bt_register_service_event(manager_conn,
3841                                 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3842                 goto fail;
3843
3844         if (_bt_register_service_event(manager_conn,
3845                                 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3846                 goto fail;
3847
3848         if (_bt_register_service_event(manager_conn,
3849                                 BT_TDS_EVENT) != BLUETOOTH_ERROR_NONE)
3850                 goto fail;
3851
3852         __bt_register_dump_subscribe_signal(manager_conn, TRUE);
3853         return BLUETOOTH_ERROR_NONE;
3854 fail:
3855         if (manager_conn) {
3856                 g_object_unref(manager_conn);
3857                 manager_conn = NULL;
3858         }
3859
3860         BT_DBG("-");
3861
3862         return BLUETOOTH_ERROR_INTERNAL;
3863 }
3864
3865 static int __bt_init_obexd_receiver(void)
3866 {
3867         BT_DBG("+");
3868         if (!TIZEN_PROFILE_TV) { /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3869                 GError *error = NULL;
3870
3871                 if (obexd_conn == NULL) {
3872                         obexd_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3873                         if (error != NULL) {
3874                                 BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3875                                 g_clear_error(&error);
3876                         }
3877                         retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3878                 }
3879
3880                 if (_bt_register_service_event(obexd_conn,
3881                                         BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3882                         BT_ERR("Error while registering service event");
3883                         g_object_unref(obexd_conn);
3884                         obexd_conn = NULL;
3885                         return BLUETOOTH_ERROR_INTERNAL;
3886                 }
3887         }
3888         BT_DBG("-");
3889
3890         return BLUETOOTH_ERROR_NONE;
3891 }
3892
3893 gboolean __bt_reinit_obexd_receiver(gpointer user_data)
3894 {
3895         static int retry_cnt = 0;
3896         int result = BLUETOOTH_ERROR_NONE;
3897
3898         BT_DBG("+");
3899
3900         result = __bt_init_obexd_receiver();
3901         if (result != BLUETOOTH_ERROR_NONE) {
3902                 /* 20 ms * 50 = 10 seconds. During 10 seconds fail to initialize,
3903                    then it is not the timing issue. Just can't use the session bus connection */
3904                 if (retry_cnt > 100) {
3905                         BT_ERR("Fail to init obexd receiver by 50 times.");
3906                         retry_cnt = 0;
3907                         session_reinit_timer = 0;
3908                         return FALSE;
3909                 }
3910                 retry_cnt++;
3911                 BT_DBG("Retry to initialize the obexd receiver");
3912                 return TRUE;
3913         }
3914
3915         retry_cnt = 0;
3916         session_reinit_timer = 0;
3917
3918         BT_DBG("-");
3919
3920         return FALSE;
3921 }
3922
3923 /* To receive the event from bluez */
3924 int _bt_init_service_event_receiver(void)
3925 {
3926         BT_DBG("+");
3927
3928         int result;
3929
3930         result = __bt_init_manager_receiver();
3931         retv_if(result != BLUETOOTH_ERROR_NONE, result);
3932
3933         result = __bt_init_obexd_receiver();
3934         if (result != BLUETOOTH_ERROR_NONE) {
3935                 BT_ERR("Fail to init obexd receiver");
3936
3937                 /* Try to re-initialize obexd receiver in the timer */
3938                 if (session_reinit_timer > 0)
3939                         g_source_remove(session_reinit_timer);
3940
3941                 session_reinit_timer = g_timeout_add(BT_SESSION_BUS_GET_TIMEOUT,
3942                                                         (GSourceFunc)__bt_reinit_obexd_receiver, NULL);
3943         }
3944
3945         BT_DBG("-");
3946
3947         return BLUETOOTH_ERROR_NONE;
3948 }
3949
3950 void _bt_deinit_service_event_receiver(void)
3951 {
3952         BT_DBG("+");
3953
3954         _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3955
3956         _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3957
3958         __bt_register_dump_subscribe_signal(manager_conn, FALSE);
3959
3960         if (manager_conn) {
3961                 g_object_unref(manager_conn);
3962                 manager_conn = NULL;
3963         }
3964
3965         if (obexd_conn) {
3966                 g_object_unref(obexd_conn);
3967                 obexd_conn = NULL;
3968         }
3969
3970         if (event_id > 0)
3971                 g_source_remove(event_id);
3972         event_id = 0;
3973
3974         if (le_scan_event_id > 0)
3975                 g_source_remove(le_scan_event_id);
3976         le_scan_event_id = 0;
3977
3978         BT_DBG("-");
3979 }
3980