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