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