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