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