Merge "Porting flight mode logic to tizen5.5" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-event-receiver.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <glib.h>
19 #include <string.h>
20 #include <dlog.h>
21 #include <vconf.h>
22 #include <vconf-internal-bt-keys.h>
23
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
26
27 #include "bt-service-common.h"
28 #include "bt-service-event.h"
29 #include "bt-service-main.h"
30 #include "bt-service-adapter.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-device.h"
33 #include "bt-service-avrcp.h"
34 #include "bt-service-obex-server.h"
35 #include "bt-service-rfcomm-server.h"
36 #include "bt-service-audio.h"
37 #include "bt-service-agent.h"
38 #include "bt-service-pbap.h"
39 #include "bt-service-headset-connection.h"
40 #include "bt-service-avrcp-controller.h"
41 #include "bt-service-hid.h"
42 #include "bt-service-proximity.h"
43
44 #include "bt-service-opp-client.h"
45
46 #ifdef TIZEN_DPM_ENABLE
47 #include "bt-service-dpm.h"
48 #endif
49
50 #define DBUS_TIMEOUT 20 * 1000 /* 20 Sec */
51 static GDBusConnection *manager_conn;
52 static GDBusConnection *obexd_conn;
53 static GDBusConnection *opc_obexd_conn;
54
55 static GList *p_cache_list = NULL;
56
57 static guint event_id;
58 static guint session_reinit_timer;
59 guint nap_connected_device_count = 0;
60 static guint hid_connected_device_count = 0;
61 static GList *p_adv_ind_list = NULL;
62
63 typedef struct {
64         bt_remote_dev_info_t *dev_info;
65 } bt_cache_info_t;
66
67 /**
68  * obexd connection type
69  */
70 typedef enum {
71         OBEX_OPP = (1 << 1),
72         OBEX_FTP = (1 << 2),
73         OBEX_BIP = (1 << 3),
74         OBEX_PBAP = (1 << 4),
75         OBEX_IRMC = (1 << 5),
76         OBEX_PCSUITE = (1 << 6),
77         OBEX_SYNCEVOLUTION = (1 << 7),
78         OBEX_MAS = (1 << 8),
79 } bluetooth_obex_connection_type_t;
80
81 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path);
82 void _bt_opc_property_changed_event(GVariant *msg, char *path);
83 int _bt_register_service_event(GDBusConnection *g_conn, int event_type);
84 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type);
85 void _bt_opp_client_event_deinit(void);
86 void _bt_handle_network_client_event(GVariant *msg_iter,
87                                 const char *path);
88 void __bt_gatt_char_property_changed_event(GVariant *msg_iter,
89                                 const char *path);
90
91 static void __bt_free_bt_le_adv_info_t(gpointer data)
92 {
93         bt_le_adv_info_t *adv_info = data;
94
95         if (adv_info->timer_id)
96                 g_source_remove(adv_info->timer_id);
97         adv_info->timer_id = 0;
98
99         g_free(adv_info->addr);
100         g_free(adv_info->data);
101         g_free(adv_info);
102 }
103
104 static bt_le_adv_info_t *__bt_get_adv_ind_info(char *addr)
105 {
106         retv_if(!addr, NULL);
107         bt_le_adv_info_t *adv_info = NULL;
108         GList *current = g_list_first((GList *)p_adv_ind_list);
109         while (current && current->data) {
110                 adv_info = (bt_le_adv_info_t *)current->data;
111                 if (adv_info && !g_strcmp0(adv_info->addr, addr))
112                         return adv_info;
113                 current = g_list_next(current);
114         }
115         return NULL;
116 }
117
118 static int __bt_add_adv_ind_info(bt_le_adv_info_t *adv_info)
119 {
120         retv_if(!adv_info, -1);
121         if (__bt_get_adv_ind_info(adv_info->addr) != NULL) {
122                 __bt_free_bt_le_adv_info_t(adv_info);
123                 return -1;
124         }
125         p_adv_ind_list = g_list_append(p_adv_ind_list, adv_info);
126
127         return 0;
128 }
129
130 static void __bt_del_adv_ind_info(char *addr)
131 {
132         ret_if(!addr);
133         ret_if(!p_adv_ind_list);
134         bt_le_adv_info_t *adv_info = NULL;
135         GList *current = g_list_first((GList *)p_adv_ind_list);
136         while (current && current->data) {
137                 adv_info = (bt_le_adv_info_t *)current->data;
138                 if (adv_info && !g_strcmp0(adv_info->addr, addr)) {
139                         p_adv_ind_list = g_list_remove(p_adv_ind_list, adv_info);
140                         __bt_free_bt_le_adv_info_t(adv_info);
141                         return;
142                 }
143                 current = g_list_next(current);
144         }
145 }
146
147 static gboolean __bt_adv_scan_req_timeout_cb(gpointer user_data)
148 {
149         bt_remote_le_dev_info_t le_dev_info;
150         bt_le_adv_info_t *adv_info = (bt_le_adv_info_t*)user_data;
151
152         memset(&le_dev_info, 0x00, sizeof(bt_remote_le_dev_info_t));
153
154         le_dev_info.address = adv_info->addr;
155         le_dev_info.addr_type = adv_info->addr_type;
156         le_dev_info.rssi = adv_info->rssi;
157         le_dev_info.adv_data = adv_info->data;
158         le_dev_info.adv_data_len = 0;
159         adv_info->timer_id = 0;
160
161         _bt_send_scan_result_event(&le_dev_info, adv_info);
162         __bt_del_adv_ind_info(adv_info->addr);
163
164         return FALSE;
165 }
166
167 static void __bt_free_cache_info(bt_cache_info_t *cache_info)
168 {
169         ret_if(cache_info == NULL);
170         _bt_free_device_info(cache_info->dev_info);
171         g_free(cache_info);
172 }
173
174 static gboolean __bt_parse_device_properties(GVariant *item,
175                                                 bt_remote_dev_info_t *dev_info)
176 {
177         GVariantIter iter;
178         gchar *key;
179         GVariant *val;
180         gsize len = 0;
181         if (item == NULL)
182                 return FALSE;
183
184         g_variant_iter_init(&iter, item);
185         while (g_variant_iter_loop(&iter, "{sv}", &key, &val)) {
186                 if (strcasecmp(key, "Address") == 0)  {
187                         dev_info->address = g_variant_dup_string(val, &len);
188                 } else if (strcasecmp(key, "Class") == 0) {
189                         dev_info->class = g_variant_get_uint32(val);
190                 } else if (strcasecmp(key, "name") == 0) {
191                         if (dev_info->name == NULL)
192                                 dev_info->name = g_variant_dup_string(val, &len);
193                 } else if (strcasecmp(key, "Connected") == 0) {
194                         dev_info->connected = g_variant_get_byte(val);
195                 } else if (strcasecmp(key, "paired") == 0) {
196                         dev_info->paired = g_variant_get_boolean(val);
197                 } else if (strcasecmp(key, "Trusted") == 0) {
198                         dev_info->trust = g_variant_get_boolean(val);
199                 } else if (strcasecmp(key, "RSSI") == 0) {
200                         dev_info->rssi = g_variant_get_int16(val);
201                 } else if (strcasecmp(key, "LastAddrType") == 0) {
202                         dev_info->addr_type = g_variant_get_byte(val);
203                 } else if (strcasecmp(key, "UUIDs") == 0) {
204                         char **uuid_value;
205                         gsize size = 0;
206                         int i = 0;
207                         size = g_variant_get_size(val);
208
209                         if (size > 0) {
210                                 uuid_value = (char **)g_variant_get_strv(val, &size);
211                                 if (dev_info->uuids == NULL)
212                                         dev_info->uuids = g_malloc0(sizeof(char *) * size);
213
214                                 for (i = 0; uuid_value[i] != NULL; i++) {
215                                         dev_info->uuid_count++;
216                                         dev_info->uuids[i] = g_strdup(uuid_value[i]);
217                                 }
218                                 g_free(uuid_value);
219                         }
220                 } else if (strcasecmp(key, "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, "(sss)", name, previous, current);
298         return BLUETOOTH_ERROR_NONE;
299 }
300
301 static int __bt_get_agent_signal_info(GVariant *msg, char **address,
302                                 char **name, char **uuid)
303 {
304         g_variant_get(msg, "(sss)", address, name, uuid);
305         return BLUETOOTH_ERROR_NONE;
306 }
307
308 void __bt_set_device_values(gboolean connected, int state)
309 {
310         int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
311
312         if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
313                 BT_ERR("vconf_get_int failed");
314
315         if (connected == TRUE)
316                 bt_device_state |= state;
317         else if (bt_device_state & state)
318                 bt_device_state ^= state;
319
320         if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0)
321                 BT_ERR("vconf_set_int failed");
322 }
323
324 gboolean _bt_discovery_finished_cb(gpointer user_data)
325 {
326         int result = BLUETOOTH_ERROR_NONE;
327         event_id = 0;
328         GVariant *param = NULL;
329         if (_bt_get_discovering_property(DISCOVERY_ROLE_BREDR) == FALSE) {
330                 if (_bt_get_cancel_by_user() == TRUE)
331                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
332
333                 _bt_set_cancel_by_user(FALSE);
334                 _bt_set_discovery_status(FALSE);
335                 param = g_variant_new("(i)", result);
336                 _bt_send_event(BT_ADAPTER_EVENT,
337                         BLUETOOTH_EVENT_DISCOVERY_FINISHED,
338                         param);
339         }
340
341         return FALSE;
342 }
343
344 static gboolean __bt_le_discovery_finished_cb(gpointer user_data)
345 {
346         int result = BLUETOOTH_ERROR_NONE;
347         event_id = 0;
348         GVariant *param = NULL;
349         if (_bt_get_discovering_property(DISCOVERY_ROLE_LE) == FALSE) {
350                 if (_bt_get_cancel_by_user() == TRUE)
351                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
352
353                 g_list_free_full(p_adv_ind_list, __bt_free_bt_le_adv_info_t);
354                 p_adv_ind_list = NULL;
355
356                 _bt_set_cancel_by_user(FALSE);
357                 _bt_disable_all_scanner_status();
358                 _bt_set_le_scan_status(FALSE);
359                 param = g_variant_new("(i)", result);
360                 _bt_send_event(BT_LE_ADAPTER_EVENT,
361                         BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED,
362                         param);
363         }
364
365         return FALSE;
366 }
367
368 void __bt_update_remote_cache_devinfo(const char *address, gboolean paired_status)
369 {
370         BT_DBG("+");
371
372         ret_if(address == NULL);
373
374         GList * node;
375         bt_cache_info_t *cache_info;
376         bt_remote_dev_info_t *dev_info;
377
378         node = g_list_first(p_cache_list);
379
380         while (node != NULL) {
381                 cache_info = (bt_cache_info_t *)node->data;
382
383                 if (cache_info == NULL) {
384                         node = g_list_next(node);
385                         continue;
386                 }
387
388                 dev_info = cache_info->dev_info;
389                 if (strcasecmp(dev_info->address,
390                                         address) == 0) {
391                         BT_DBG("Device Found");
392                         if (paired_status == TRUE)
393                                 cache_info->dev_info->paired = TRUE;
394                         else
395                                 cache_info->dev_info->paired = FALSE;
396                         break;
397                 }
398                 node = g_list_next(node);
399         }
400         BT_DBG("-");
401 }
402
403 static void __bt_device_remote_connected_properties(
404                                 bt_remote_dev_info_t *remote_dev_info,
405                                 char *address, gboolean connected)
406 {
407         int result = BLUETOOTH_ERROR_NONE;
408         int i;
409         GVariant *param = NULL;
410         BT_DBG("+");
411
412         if (remote_dev_info->uuid_count > 0) {
413                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
414                         char *uuid = remote_dev_info->uuids[i];
415                         if (strcasecmp(uuid, HID_UUID) == 0) {
416                                 int event = BLUETOOTH_EVENT_NONE;
417
418                                 event = (connected == TRUE) ?
419                                         BLUETOOTH_HID_CONNECTED :
420                                         BLUETOOTH_HID_DISCONNECTED;
421                                 param = g_variant_new("(is)", result,
422                                                         address);
423                                 _bt_send_event(BT_HID_EVENT, event,
424                                         param);
425                                 break;
426                         }
427                 }
428         }
429
430         BT_DBG("-");
431 }
432
433 void _bt_handle_adapter_event(GVariant *msg, const char *member)
434 {
435         BT_DBG("+");
436
437         int result = BLUETOOTH_ERROR_NONE;
438         GVariant *param = NULL;
439         ret_if(member == NULL);
440
441         if (strcasecmp(member, "DeviceCreated") == 0) {
442                 char *object_path = NULL;
443                 char *address;
444                 bt_remote_dev_info_t *remote_dev_info;
445
446                 ret_if(_bt_is_device_creating() == FALSE);
447
448                 /* Bonding from remote device */
449                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
450
451                 g_variant_get(msg, "(&o)", &object_path);
452                 _bt_convert_device_path_to_address((const char*)object_path, address);
453
454                 remote_dev_info = _bt_get_remote_device_info_by_object_path(object_path);
455                 if (remote_dev_info == NULL) {
456                         g_free(address);
457                         return;
458                 }
459
460                 _bt_free_device_info(remote_dev_info);
461                 g_free(address);
462         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
463                 char *object_path = NULL;
464                 char *address;
465                 bt_cache_info_t *cache_info;
466                 bt_remote_dev_info_t *dev_info;
467                 GList * node;
468
469                 /* Bonding from remote device */
470                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
471
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_SCAN_RSP) {       /* ADV_IND */
2187                         adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
2188                         adv_info->addr = g_strdup(le_dev_info->address);
2189                         adv_info->addr_type = le_dev_info->addr_type;
2190                         adv_info->rssi = le_dev_info->rssi;
2191                         adv_info->data_len = le_dev_info->adv_data_len;
2192                         adv_info->data = g_malloc0(le_dev_info->adv_data_len);
2193                         memcpy(adv_info->data, le_dev_info->adv_data,
2194                                         le_dev_info->adv_data_len);
2195
2196                         if (__bt_add_adv_ind_info(adv_info) == 0) {
2197                                 adv_info->timer_id = g_timeout_add(1000,
2198                                         (GSourceFunc)__bt_adv_scan_req_timeout_cb, (void*)adv_info);
2199                         }
2200                 } else {     /* SCAN_RSP */
2201                         adv_info = __bt_get_adv_ind_info(le_dev_info->address);
2202                         if (adv_info) {
2203                                 _bt_send_scan_result_event(le_dev_info, adv_info);
2204                                 __bt_del_adv_ind_info(le_dev_info->address);
2205                         }
2206                 }
2207                 _bt_free_le_device_info(le_dev_info);
2208                 g_variant_unref(value);
2209         } else if  (strcasecmp(member, "LEDataLengthChanged") == 0) {
2210                 guint16 tx_octets = 0;
2211                 guint16 tx_time = 0;
2212                 guint16 rx_octets = 0;
2213                 guint16 rx_time = 0;
2214
2215                 g_variant_get(msg, "(qqqq)",
2216                                 &tx_octets, &tx_time, &rx_octets, &rx_time);
2217
2218                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2219                 _bt_convert_device_path_to_address(path, address);
2220
2221                 param = g_variant_new("(isqqqq)", result, address, tx_octets, tx_time,
2222                                 rx_octets, rx_time);
2223                 /* Send event to application */
2224                 _bt_send_event(BT_DEVICE_EVENT,
2225                                 BLUETOOTH_EVENT_LE_DATA_LENGTH_CHANGED, param);
2226                 g_free(address);
2227         } else if  (strcasecmp(member, "IpspStateChanged") == 0) {
2228                 gboolean connected = FALSE;
2229                 char *ifname = NULL;
2230
2231                 g_variant_get(msg, "(bs)", &connected, &ifname);
2232
2233                 event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
2234                                                 BLUETOOTH_EVENT_IPSP_DISCONNECTED;
2235
2236                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2237                 _bt_convert_device_path_to_address(path, address);
2238
2239                 BT_DBG("Ipsp BT Interface Name: %s", ifname);
2240                 BT_DBG("address: %s", address);
2241                 param = g_variant_new("(iss)", result, address, ifname);
2242
2243                 /* Send event to application */
2244                 _bt_send_event(BT_DEVICE_EVENT,
2245                                                 event,
2246                                                 param);
2247                 g_free(address);
2248         } else if (strcasecmp(member, "iBeaconReport") == 0) {
2249                 bt_remote_ibeacon_dev_info_t *ibeacon_dev_info = NULL;
2250
2251                 ret_if(_bt_is_le_scanning() == FALSE);
2252
2253                 ibeacon_dev_info = g_malloc0(sizeof(bt_remote_ibeacon_dev_info_t));
2254
2255                 g_variant_get(msg, "(syuusuuy)", &ibeacon_dev_info->address,
2256                                                 &ibeacon_dev_info->addr_type,
2257                                                 &ibeacon_dev_info->company_id,
2258                                                 &ibeacon_dev_info->ibeacon_type,
2259                                                 &ibeacon_dev_info->uuid,
2260                                                 &ibeacon_dev_info->major_id,
2261                                                 &ibeacon_dev_info->minor_id,
2262                                                 &ibeacon_dev_info->measured_power);
2263                 _bt_send_ibeacon_scan_result_event(ibeacon_dev_info);
2264                 g_free(ibeacon_dev_info);
2265         }
2266 }
2267
2268 void __bt_set_audio_values(gboolean connected, char *address)
2269 {
2270         char *name = NULL;
2271         int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
2272
2273         /*  Set the headset name */
2274         if (connected == TRUE)
2275                 name = _bt_get_device_name(address);
2276         else
2277                 name = g_strdup("");
2278
2279         if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME, name) != 0)
2280                 BT_ERR("vconf_set_str failed");
2281
2282         g_free(name);
2283
2284         /*  Set the headset state */
2285         if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
2286                 BT_ERR("vconf_get_int failed");
2287
2288 #ifdef TIZEN_SUPPORT_DUAL_HF
2289         if ((connected == TRUE) &&
2290                 (FALSE == __bt_is_companion_device(address))) {
2291                 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2292         } else if ((bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) &&
2293                         (FALSE == __bt_is_companion_device(address))) {
2294                 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2295         }
2296 #else
2297         if (connected == TRUE)
2298                 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2299         else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED)
2300                 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2301 #endif
2302
2303         if (vconf_set_int(VCONFKEY_BT_DEVICE,
2304                                 bt_device_state) != 0) {
2305                 BT_ERR("vconf_set_int failed");
2306         }
2307 }
2308
2309 void _bt_handle_headset_event(GVariant *msg, const char *path)
2310 {
2311         int result = BLUETOOTH_ERROR_NONE;
2312         gboolean property_flag = FALSE;
2313         char *property = NULL;
2314         GVariant *value = NULL;
2315         GVariant *param = NULL;
2316         g_variant_get(msg, "(sv)", &property, &value);
2317         bluetooth_device_address_t bd_addr;
2318
2319         ret_if(property == NULL);
2320
2321         BT_DBG("Property = %s \n", property);
2322         /* We allow only 1 headset connection (HSP or HFP)*/
2323         if (strcasecmp(property, "Connected") == 0) {
2324                 int event = BLUETOOTH_EVENT_NONE;
2325                 bt_headset_wait_t *wait_list;
2326                 char *address;
2327                 guint restricted = 0x0;
2328                 g_variant_get(value, "b", &property_flag);
2329
2330                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2331
2332                 _bt_convert_device_path_to_address(path, address);
2333                 _bt_convert_addr_string_to_type(bd_addr.addr, address);
2334
2335                 if (property_flag == TRUE) {
2336                         event = BLUETOOTH_EVENT_AG_CONNECTED;
2337                         _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_A2DP, &restricted);
2338
2339                         if (_bt_headset_get_local_connection() == FALSE) {
2340                                 if (restricted == 0x0) /* not restricted*/
2341                                         _bt_start_timer_for_connection(address, BT_AUDIO_A2DP);
2342                         } else
2343                                 _bt_headset_set_local_connection(FALSE);
2344                 } else {
2345                         int previous_state;
2346
2347                         event = BLUETOOTH_EVENT_AG_DISCONNECTED;
2348
2349                         previous_state = _bt_get_device_state_from_list(BT_AUDIO_HSP, address);
2350                         if (previous_state == BT_STATE_DISCONNECTING)
2351                                 _bt_send_hf_local_term_event(address);
2352                 }
2353                 /* Set the State machine here */
2354                 __bt_connection_manager_set_state(address, event);
2355                 __bt_set_audio_values(property_flag, address);
2356                 param = g_variant_new("(is)", result, address);
2357                 _bt_send_event(BT_HEADSET_EVENT, event,
2358                         param);
2359
2360                 if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
2361                         /* Remove data from the connected list */
2362                         _bt_remove_headset_from_list(BT_AUDIO_HSP, address);
2363
2364                         wait_list = _bt_get_audio_wait_data();
2365                         if (wait_list == NULL) {
2366                                 g_free(address);
2367                                 return;
2368                         }
2369
2370                         bluetooth_device_address_t device_address;
2371
2372                         _bt_set_audio_wait_data_flag(TRUE);
2373
2374                         _bt_convert_addr_string_to_type(device_address.addr,
2375                                                         wait_list->address);
2376                         _bt_audio_connect(wait_list->req_id, wait_list->type,
2377                                         &device_address, NULL);
2378                         _bt_rel_wait_data();
2379                 } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
2380                         /* Add data to the connected list */
2381                         _bt_add_headset_to_list(BT_AUDIO_HSP,
2382                                                 BT_STATE_CONNECTED, address);
2383
2384                         wait_list = _bt_get_audio_wait_data();
2385                         if (wait_list != NULL &&
2386                                 (g_strcmp0(wait_list->address, address) == 0))
2387                         _bt_rel_wait_data();
2388
2389                         BT_INFO("Check A2DP pending connect");
2390                         _bt_audio_check_pending_connect();
2391                 }
2392                 g_free(address);
2393         } else if (strcasecmp(property, "State") == 0) {
2394                 char *state = NULL;
2395
2396                 g_variant_get(value, "s", &state);
2397
2398                 /* This code assumes we support only 1 headset connection */
2399                 /* Need to use the headset list, if we support multi-headsets */
2400                 if (strcasecmp(state, "Playing") == 0) {
2401                         BT_DBG("Playing: Sco Connected");
2402                 } else if (strcasecmp(state, "connected") == 0 ||
2403                                 strcasecmp(state, "disconnected") == 0) {
2404                         BT_DBG("connected/disconnected: Sco Disconnected");
2405                 } else {
2406                         BT_ERR("Not handled state - %s", state);
2407                         g_free(state);
2408                         return;
2409                 }
2410                 g_free(state);
2411         } else if (strcasecmp(property, "SpeakerGain") == 0) {
2412                 guint16 spkr_gain;
2413                 char *address;
2414
2415                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2416
2417                 _bt_convert_device_path_to_address(path, address);
2418
2419                 spkr_gain = g_variant_get_uint16(value);
2420
2421                 BT_DBG("spkr_gain: %d", spkr_gain);
2422
2423                 param = g_variant_new("(i&sq)", result, address, spkr_gain);
2424                 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
2425                         param);
2426
2427                 g_free(address);
2428         } else if (strcasecmp(property, "MicrophoneGain") == 0) {
2429                 guint16 mic_gain;
2430                 char *address;
2431
2432                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2433
2434                 _bt_convert_device_path_to_address(path, address);
2435
2436                 mic_gain = g_variant_get_uint16(value);
2437
2438                 param = g_variant_new("(i&sq)", result, address, mic_gain);
2439                 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_MIC_GAIN,
2440                         param);
2441                 g_free(address);
2442         }
2443
2444         if (property)
2445                 g_free(property);
2446         g_variant_unref(value);
2447 }
2448
2449 void _bt_handle_sink_event(GVariant *msg, const char *path)
2450 {
2451         GVariantIter value_iter;
2452         char *property = NULL;
2453
2454         bt_headset_wait_t *wait_list;
2455
2456         GVariant *child = NULL;
2457         GVariant *val = NULL;
2458         GVariant *param = NULL;
2459         g_variant_iter_init(&value_iter, msg);
2460         while ((child = g_variant_iter_next_value(&value_iter))) {
2461
2462                 g_variant_get(child, "{sv}", &property, &val);
2463
2464                 ret_if(property == NULL);
2465
2466                 BT_DBG("Property = %s \n", property);
2467
2468
2469                 if (strcasecmp(property, "State") == 0) {
2470                         int result = BLUETOOTH_ERROR_NONE;
2471                         char *value;
2472
2473                         g_variant_get(val, "s", &value);
2474                         BT_DBG("value: %s", value);
2475
2476                         if (g_strcmp0(value, "disconnected") == 0) {
2477                                 char *address;
2478
2479                                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2480
2481                                 _bt_convert_device_path_to_address(path, address);
2482
2483                                 __bt_set_device_values(FALSE,
2484                                         VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2485                                 param = g_variant_new("(is)", result, address);
2486                                 _bt_send_event(BT_HEADSET_EVENT,
2487                                         BLUETOOTH_EVENT_AV_DISCONNECTED,
2488                                         param);
2489
2490                                 /* Remove data from the connected list */
2491                                 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2492                                 wait_list = _bt_get_audio_wait_data();
2493                                 if (wait_list == NULL) {
2494                                         g_free(value);
2495                                         g_free(property);
2496                                         g_variant_unref(val);
2497                                         g_variant_unref(child);
2498                                         g_free(address);
2499                                         return;
2500                                 }
2501
2502                                 if (((wait_list->type == BT_AUDIO_ALL) &&
2503                                         (wait_list->ag_flag == TRUE)) ||
2504                                         (wait_list->type == BT_AUDIO_A2DP) ||
2505                                         (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2506                                         bluetooth_device_address_t device_address;
2507                                         _bt_convert_addr_string_to_type(
2508                                                                 device_address.addr,
2509                                                                 wait_list->address);
2510
2511                                         _bt_audio_connect(wait_list->req_id,
2512                                                                 wait_list->type,
2513                                                                 &device_address,
2514                                                                 NULL);
2515                                 }
2516                                 g_free(address);
2517                         } else if (strcasecmp(value, "Connected") == 0) {
2518                                 char *address;
2519                                 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2520                                 bluetooth_device_address_t device_address;
2521                                 gboolean connected;
2522
2523                                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2524
2525                                 _bt_convert_device_path_to_address(path, address);
2526
2527                                 __bt_set_device_values(TRUE,
2528                                                 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2529                                 param = g_variant_new("(is)", result, address);
2530                                 _bt_send_event(BT_HEADSET_EVENT,
2531                                         BLUETOOTH_EVENT_AV_CONNECTED,
2532                                         param);
2533                                 /* Check for existing Media device to disconnect */
2534                                 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
2535                                                                         connected_address);
2536                                 if (connected) {
2537                                         /* Match connected device address */
2538                                         if (g_strcmp0(connected_address, address) != 0) {
2539                                                 /* Convert BD address from string type */
2540                                                 _bt_convert_addr_string_to_type(
2541                                                                 device_address.addr,
2542                                                                 connected_address);
2543                                                 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2544                                                                 &device_address, NULL);
2545                                         }
2546                                 }
2547
2548                                 /* Add data to the connected list */
2549                                 _bt_add_headset_to_list(BT_AUDIO_A2DP,
2550                                                 BT_STATE_CONNECTED, address);
2551
2552                                 g_free(address);
2553                         }
2554                         g_free(value);
2555                 }
2556                 g_free(property);
2557                 g_variant_unref(val);
2558                 g_variant_unref(child);
2559         }
2560 }
2561
2562 void _bt_handle_agent_event(GVariant *msg, const char *member)
2563 {
2564         int result = BLUETOOTH_ERROR_NONE;
2565         char *address = NULL;
2566         char *name = NULL;
2567         char *uuid = NULL;
2568         GVariant *param = NULL;
2569         ret_if(member == NULL);
2570
2571         if (strcasecmp(member, "ObexAuthorize") == 0) {
2572                 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2573                 param = g_variant_new("(iss)", result, address, name);
2574                 _bt_send_event(BT_OPP_SERVER_EVENT,
2575                         BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
2576                         param);
2577                 g_free(address);
2578                 g_free(name);
2579         } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2580                 bt_rfcomm_server_info_t *server_info;
2581
2582                 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2583
2584                 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2585                 ret_if(server_info == NULL);
2586                 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2587                 param = g_variant_new("(isssn)", result, address, uuid, name,
2588                                         server_info->control_fd);
2589                 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2590                         BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2591                         param);
2592                 g_free(address);
2593                 g_free(uuid);
2594                 g_free(name);
2595         }
2596 }
2597
2598 static int __bt_get_object_path(GVariant *msg, char **path)
2599 {
2600         g_variant_get(msg, "(o*)", path, NULL);
2601         if (*path == NULL)
2602                 return BLUETOOTH_ERROR_INTERNAL;
2603
2604         return BLUETOOTH_ERROR_NONE;
2605 }
2606
2607 static void __bt_devices_list_free(void)
2608 {
2609         bt_cache_info_t *cache_info;
2610         GList *node;
2611
2612         node = g_list_first(p_cache_list);
2613
2614         while (node != NULL) {
2615                 cache_info = (bt_cache_info_t *)node->data;
2616                 p_cache_list = g_list_remove(p_cache_list, cache_info);
2617                 __bt_free_cache_info(cache_info);
2618
2619                 node = g_list_next(node);
2620         }
2621 }
2622
2623 static int __bt_parse_event(GVariant *msg)
2624 {
2625         GVariantIter iter;
2626         GVariant *child;
2627         char *interface_name = NULL;
2628         GVariant *inner_iter = NULL;
2629
2630         g_variant_iter_init(&iter, msg);
2631
2632         while ((child = g_variant_iter_next_value(&iter))) {
2633                 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
2634                 if (g_strcmp0(interface_name,
2635                                 BT_DEVICE_INTERFACE) == 0) {
2636                         g_variant_unref(inner_iter);
2637                         g_variant_unref(child);
2638                         return BT_DEVICE_EVENT;
2639                 } else if (g_strcmp0(interface_name,
2640                                 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2641                         g_variant_unref(inner_iter);
2642                         g_variant_unref(child);
2643                         return BT_MEDIA_TRANSFER_EVENT;
2644                 } else if (g_strcmp0(interface_name,
2645                                 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2646                         g_variant_unref(inner_iter);
2647                         g_variant_unref(child);
2648                         return BT_AVRCP_CONTROL_EVENT;
2649                 }
2650                 g_variant_unref(inner_iter);
2651                 g_variant_unref(child);
2652         }
2653
2654         return 0;
2655 }
2656
2657 static  void __bt_manager_event_filter(GDBusConnection *connection,
2658                                         const gchar *sender_name,
2659                                         const gchar *object_path,
2660                                         const gchar *interface_name,
2661                                         const gchar *signal_name,
2662                                         GVariant *parameters,
2663                                         gpointer user_data)
2664 {
2665         bt_event_type_t bt_event = 0x00;
2666         int result = BLUETOOTH_ERROR_NONE;
2667         GVariant *value;
2668         char *obj_path = NULL;
2669         GVariant *param = NULL;
2670         if (signal_name == NULL)
2671                 return;
2672         if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2673                 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2674
2675                 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
2676 #ifdef USB_BLUETOOTH
2677                         BT_DBG("Enable Adapter");
2678                         _bt_enable_adapter();
2679 #else
2680                         _bt_handle_adapter_added();
2681 #endif
2682                 } else {
2683                         bt_event = __bt_parse_event(value);
2684                         if (bt_event == BT_DEVICE_EVENT) {
2685                                 bt_cache_info_t *cache_info;
2686                                 bt_remote_dev_info_t *dev_info;
2687
2688                                 if (_bt_is_discovering() == FALSE &&
2689                                     _bt_is_le_scanning() == FALSE) {
2690                                         g_variant_unref(value);
2691                                         return;
2692                                 }
2693
2694                                 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2695
2696                                 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2697
2698                                 cache_info->dev_info = dev_info;
2699
2700                                 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2701                                         BT_ERR("Fail to parse the properies");
2702                                         __bt_free_cache_info(cache_info);
2703                                         g_variant_unref(value);
2704                                         return;
2705                                 }
2706
2707                                 if (dev_info->addr_type != BDADDR_BREDR) {
2708                                         /* Whenever emit the property changed from bluez,
2709                                                 some property doesn't reach to bt-service.
2710                                                 So LE device is handled as AdvReport signal */
2711                                         __bt_free_cache_info(cache_info);
2712                                         g_variant_unref(value);
2713                                         return;
2714                                 }
2715
2716                                 if (dev_info->name == NULL)
2717                                         /* If Remote device name is NULL or still RNR is not done
2718                                          * then display address as name.
2719                                          */
2720                                         dev_info->name = g_strdup(dev_info->address);
2721
2722 #ifdef TIZEN_DPM_ENABLE
2723                                 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
2724                                                         DPM_RESTRICTED) {
2725                                         bluetooth_device_class_t device_class;
2726                                         _bt_divide_device_class(&device_class, dev_info->class);
2727                                         BT_DBG("[%s]device_class.major_class : %d", dev_info->name, device_class.major_class);
2728
2729                                         if (device_class.major_class ==
2730                                                 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
2731                                                 __bt_free_cache_info(cache_info);
2732                                                 g_variant_unref(value);
2733                                                 return;
2734                                         }
2735                                 }
2736 #endif
2737
2738                                 GVariant *uuids = NULL;
2739                                 GVariantBuilder *builder = NULL;
2740                                 int i = 0;
2741                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2742                                 for (i = 0; i < dev_info->uuid_count; i++) {
2743                                         g_variant_builder_add(builder, "s",
2744                                                 dev_info->uuids[i]);
2745                                 }
2746                                 uuids = g_variant_new("as", builder);
2747                                 g_variant_builder_unref(builder);
2748                                 GVariant *manufacturer_data = NULL;
2749                                 manufacturer_data = g_variant_new_from_data(
2750                                                         G_VARIANT_TYPE_BYTESTRING,
2751                                                         dev_info->manufacturer_data,
2752                                                         dev_info->manufacturer_data_len,
2753                                                         TRUE, NULL, NULL);
2754                                 param = g_variant_new("(isunsbub@asn@ay)", result,
2755                                                         dev_info->address,
2756                                                         dev_info->class,
2757                                                         dev_info->rssi,
2758                                                         dev_info->name,
2759                                                         dev_info->paired,
2760                                                         dev_info->connected,
2761                                                         dev_info->trust,
2762                                                         uuids,
2763                                                         dev_info->manufacturer_data_len,
2764                                                         manufacturer_data);
2765                                 _bt_send_event(BT_ADAPTER_EVENT,
2766                                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2767                                          param);
2768                                 p_cache_list = g_list_append(p_cache_list, cache_info);
2769                         } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2770                                 BT_DBG("Device path : %s ", obj_path);
2771                                 _bt_set_control_device_path(obj_path);
2772                         }
2773                 }
2774                 g_variant_unref(value);
2775         } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2776 #ifdef USB_BLUETOOTH
2777                 BT_DBG("InterfacesRemoved");
2778                 _bt_handle_adapter_removed();
2779 #endif
2780                 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0)
2781                         bt_event = BT_MEDIA_TRANSFER_EVENT;
2782                 else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0)
2783                         bt_event = BT_DEVICE_EVENT;
2784                 else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0)
2785                         bt_event = BT_AVRCP_CONTROL_EVENT;
2786
2787                 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2788                         _bt_handle_adapter_event(parameters, signal_name);
2789                         if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2790                                 BT_INFO("Object Path %s", obj_path);
2791                                 _bt_remove_control_device_path(obj_path);
2792                         }
2793                 }
2794         } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2795                 gboolean value;
2796                 char *name = NULL;
2797                 char *previous = NULL;
2798                 char *current = NULL;
2799
2800                 if (__bt_get_owner_info(parameters, &name, &previous, &current)) {
2801                         BT_ERR("Fail to get the owner info");
2802                         return;
2803                 }
2804
2805                 if (*current != '\0') {
2806                         g_free(current);
2807                         if (name)
2808                                 g_free(name);
2809                         if (previous)
2810                                 g_free(previous);
2811                         return;
2812                 }
2813
2814                 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2815                         BT_INFO_C("### Bluetoothd is terminated");
2816                         if (_bt_adapter_get_status() == BT_ACTIVATED)
2817                                  _bt_disable_cb();
2818
2819                         _bt_handle_adapter_removed();
2820                         __bt_devices_list_free();
2821                         _bt_service_adapter_le_deinit();
2822                 }
2823
2824                 _bt_obex_server_check_allocation(&value);
2825
2826                 if (value == TRUE) {
2827                         /* Check if the obex server was terminated abnormally */
2828                         _bt_obex_server_check_termination(name);
2829                 }
2830
2831                 _bt_rfcomm_server_check_existence(&value);
2832
2833                 if (value == TRUE) {
2834                         /* The obex server was terminated abnormally */
2835                         _bt_rfcomm_server_check_termination(name);
2836                 }
2837
2838                 /* Reset connection interval */
2839                 _bt_remove_all_le_conn_param_info(name);
2840
2841                 /* Stop advertising started by terminated process */
2842                 _bt_stop_advertising_by_terminated_process(name);
2843
2844                 /* Stop LE Scan */
2845                 _bt_stop_le_scan(name);
2846
2847                 g_free(name);
2848                 g_free(previous);
2849                 g_free(current);
2850         } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2851                 const char *path = object_path;
2852
2853                 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2854                                 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2855                         return;
2856
2857                 _bt_handle_property_changed_event(parameters, object_path);
2858         } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2859                 _bt_handle_adapter_event(parameters, signal_name);
2860         } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2861                 _bt_handle_input_event(parameters, object_path);
2862         } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2863                 _bt_handle_network_server_event(parameters, signal_name);
2864         } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2865                 if (g_strcmp0(signal_name, "PropertyChanged") == 0) {
2866                         _bt_handle_headset_event(parameters, object_path);
2867                 }
2868         } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2869                 _bt_handle_sink_event(parameters, object_path);
2870         } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2871                 _bt_handle_agent_event(parameters, signal_name);
2872         } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2873                 _bt_handle_device_event(parameters, signal_name, object_path);
2874         } else if (g_strcmp0(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
2875                 _bt_handle_gatt_event(parameters, signal_name, object_path);
2876         }
2877
2878         return;
2879 }
2880
2881 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2882 {
2883
2884         if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2885                 char *interface_name = NULL;
2886
2887                 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2888                 retv_if(interface_name == NULL, FALSE);
2889
2890                 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2891                         BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2892                         return TRUE;
2893                 }
2894         }
2895
2896         return FALSE;
2897 }
2898
2899 static  void __bt_obexd_event_filter(GDBusConnection *connection,
2900                                         const gchar *sender_name,
2901                                         const gchar *object_path,
2902                                         const gchar *interface_name,
2903                                         const gchar *signal_name,
2904                                         GVariant *parameters,
2905                                         gpointer user_data)
2906 {
2907         const char *member = signal_name;
2908         char *obj_path = NULL;
2909         ret_if(member == NULL);
2910
2911         if (strcasecmp(member, "InterfacesAdded") == 0) {
2912                 if (__bt_get_object_path(parameters, &obj_path)) {
2913                         BT_ERR("Fail to get the path");
2914                         return;
2915                 }
2916                 BT_INFO("object_path = [%s]", obj_path);
2917
2918                 /*Handle OPP_SERVER_CONNECTED_EVENT here */
2919                 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2920                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2921                         g_free(obj_path);
2922                         return;
2923                 }
2924
2925                 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2926                         BT_DBG("Obex_Server_Session_Transfer connected");
2927                         _bt_obex_transfer_connected(obj_path);
2928                 }
2929                 g_free(obj_path);
2930         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2931                 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
2932                 if (__bt_get_object_path(parameters, &obj_path)) {
2933                         BT_ERR("Fail to get the path");
2934                         return;
2935                 }
2936                 BT_INFO("object_path = [%s]", obj_path);
2937
2938                 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2939                                 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2940                         BT_DBG("Call PBAP Disconnected");
2941                         _bt_obex_pbap_client_disconnect(obj_path);
2942                 }
2943
2944                 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2945                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2946                         g_free(obj_path);
2947                         return;
2948                 }
2949
2950                 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2951                         BT_DBG("Obex_Server_Session_Transfer disconnected %s",
2952                                                                 obj_path);
2953
2954                         _bt_obex_transfer_disconnected(obj_path);
2955                 }
2956                 g_free(obj_path);
2957         } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
2958                 const char *path = object_path;
2959
2960                 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
2961                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
2962                         strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2963                                 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2964                         BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
2965                         return;
2966                 }
2967
2968                 _bt_handle_property_changed_event(parameters, path);
2969         }
2970         BT_DBG("-");
2971         return;
2972 }
2973
2974 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
2975 {
2976         BT_DBG("+");
2977
2978         if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2979                 char *interface_name = NULL;
2980
2981                 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2982
2983                 retv_if(interface_name == NULL, FALSE);
2984
2985                 if (strcasecmp(interface_name,
2986                                         BT_OBEX_TRANSFER_INTERFACE) == 0) {
2987                         BT_DBG("-");
2988                         return TRUE;
2989                 }
2990         }
2991
2992         BT_DBG("-");
2993
2994         return FALSE;
2995 }
2996
2997 static  void __bt_opc_event_filter(GDBusConnection *connection,
2998                                         const gchar *sender_name,
2999                                         const gchar *object_path,
3000                                         const gchar *interface_name,
3001                                         const gchar *signal_name,
3002                                         GVariant *parameters,
3003                                         gpointer user_data)
3004 {
3005         const char *member = signal_name;
3006         char *obj_path = NULL;
3007         if (strcasecmp(member, "InterfacesAdded") == 0) {
3008                 BT_DBG("InterfacesAdded");
3009         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
3010
3011                 if (__bt_get_object_path(parameters, &obj_path)) {
3012                         BT_ERR("Fail to get the path");
3013                         return;
3014                 }
3015
3016                 BT_DBG("object_path = %s", obj_path);
3017
3018                 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
3019                                 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
3020                                 || strstr(obj_path, "transfer") == NULL) {
3021                         g_free(obj_path);
3022                         return;
3023                 } else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
3024                                 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
3025                         BT_DBG("Going to call opc disconnected");
3026                         _bt_opc_disconnected(obj_path);
3027                 }
3028
3029                 _bt_sending_files();
3030                 g_free(obj_path);
3031         } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
3032                 char *path = (char *)object_path;
3033                 BT_INFO("object_path %s", path);
3034                 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
3035                         strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
3036                         BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
3037                         return;
3038                 }
3039
3040                 _bt_opc_property_changed_event(parameters, path);
3041         }
3042
3043         return;
3044 }
3045
3046 int _bt_opp_client_event_init(void)
3047 {
3048         GError *error = NULL;
3049
3050         if (opc_obexd_conn == NULL) {
3051                 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3052
3053                 if (!opc_obexd_conn) {
3054                         if (error) {
3055                                 BT_ERR("Unable to connect to dbus: %s", error->message);
3056                                 g_clear_error(&error);
3057                         }
3058                 return BLUETOOTH_ERROR_INTERNAL;
3059                 }
3060         }
3061
3062         if (_bt_register_service_event(opc_obexd_conn,
3063                         BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
3064                         g_object_unref(opc_obexd_conn);
3065                         opc_obexd_conn = NULL;
3066                         return BLUETOOTH_ERROR_INTERNAL;
3067         }
3068
3069         return BLUETOOTH_ERROR_NONE;
3070 }
3071
3072 void _bt_opp_client_event_deinit(void)
3073 {
3074         if (opc_obexd_conn) {
3075                 _bt_unregister_service_event(opc_obexd_conn,
3076                                                 BT_OPP_CLIENT_EVENT);
3077                  g_object_unref(opc_obexd_conn);
3078                  opc_obexd_conn = NULL;
3079         }
3080 }
3081
3082 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
3083                 int subscribe)
3084 {
3085         if (conn == NULL)
3086                 return -1;
3087
3088         static int subs_interface_added_id = -1;
3089         static int subs_interface_removed_id = -1;
3090         static int subs_name_owner_id = -1;
3091         static int subs_property_id = -1;
3092         static int subs_adapter_id = -1;
3093         static int subs_gatt_id = -1;
3094
3095         if (subscribe) {
3096                 if (subs_interface_added_id == -1) {
3097                         subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3098                                 NULL, BT_MANAGER_INTERFACE,
3099                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3100                                 __bt_manager_event_filter,
3101                                 NULL, NULL);
3102                 }
3103                 if (subs_interface_removed_id == -1) {
3104                         subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3105                                 NULL, BT_MANAGER_INTERFACE,
3106                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3107                                 __bt_manager_event_filter,
3108                                 NULL, NULL);
3109                 }
3110                 if (subs_name_owner_id == -1) {
3111                         subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
3112                                 NULL, BT_FREEDESKTOP_INTERFACE,
3113                                 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
3114                                 __bt_manager_event_filter,
3115                                 NULL, NULL);
3116                 }
3117                 if (subs_property_id == -1) {
3118                         subs_property_id = g_dbus_connection_signal_subscribe(conn,
3119                                 NULL, BT_PROPERTIES_INTERFACE,
3120                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3121                                 __bt_manager_event_filter,
3122                                 NULL, NULL);
3123                 }
3124                 if (subs_adapter_id == -1) {
3125                         subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
3126                                 NULL, BT_ADAPTER_INTERFACE,
3127                                 NULL, NULL, NULL, 0,
3128                                 __bt_manager_event_filter,
3129                                 NULL, NULL);
3130                 }
3131                 if (subs_gatt_id == -1) {
3132                         subs_gatt_id = g_dbus_connection_signal_subscribe(conn,
3133                                 NULL, BT_GATT_CHAR_INTERFACE,
3134                                 NULL, NULL, NULL, 0,
3135                                 __bt_manager_event_filter,
3136                                 NULL, NULL);
3137                 }
3138         } else {
3139                 if (subs_interface_added_id != -1) {
3140                         g_dbus_connection_signal_unsubscribe(conn,
3141                                         subs_interface_added_id);
3142                         subs_interface_added_id = -1;
3143                 }
3144                 if (subs_interface_removed_id != -1) {
3145                         g_dbus_connection_signal_unsubscribe(conn,
3146                                         subs_interface_removed_id);
3147                         subs_interface_removed_id = -1;
3148                 }
3149                 if (subs_name_owner_id != -1) {
3150                         g_dbus_connection_signal_unsubscribe(conn,
3151                                         subs_name_owner_id);
3152                         subs_name_owner_id = -1;
3153                 }
3154                 if (subs_property_id != -1) {
3155                         g_dbus_connection_signal_unsubscribe(conn,
3156                                         subs_property_id);
3157                         subs_property_id = -1;
3158                 }
3159                 if (subs_adapter_id != -1) {
3160                         g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
3161                         subs_adapter_id = -1;
3162                 }
3163                 if (subs_gatt_id != -1) {
3164                         g_dbus_connection_signal_unsubscribe(conn, subs_gatt_id);
3165                         subs_gatt_id = -1;
3166                 }
3167         }
3168         return 0;
3169 }
3170
3171 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
3172                 int subscribe)
3173 {
3174         if (conn == NULL)
3175                 return -1;
3176
3177         static int subs_device_id = -1;
3178
3179         if (subscribe) {
3180                 if (subs_device_id == -1) {
3181                         subs_device_id = g_dbus_connection_signal_subscribe(conn,
3182                                 NULL, BT_DEVICE_INTERFACE,
3183                                 NULL, NULL, NULL, 0,
3184                                 __bt_manager_event_filter,
3185                                 NULL, NULL);
3186                 }
3187         } else {
3188                 if (subs_device_id != -1) {
3189                         g_dbus_connection_signal_unsubscribe(conn,
3190                                         subs_device_id);
3191                         subs_device_id = -1;
3192                 }
3193         }
3194         return 0;
3195 }
3196
3197 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
3198                 int subscribe)
3199 {
3200         if (conn == NULL)
3201                 return -1;
3202
3203         static int subs_input_id = -1;
3204
3205         if (subscribe) {
3206                 if (subs_input_id == -1) {
3207                         subs_input_id = g_dbus_connection_signal_subscribe(conn,
3208                                 NULL, BT_INPUT_INTERFACE,
3209                                 NULL, NULL, NULL, 0,
3210                                 __bt_manager_event_filter,
3211                                 NULL, NULL);
3212                 }
3213         } else {
3214                 if (subs_input_id != -1) {
3215                         g_dbus_connection_signal_unsubscribe(conn,
3216                                         subs_input_id);
3217                         subs_input_id = -1;
3218                 }
3219         }
3220         return 0;
3221 }
3222
3223 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
3224                 int subscribe)
3225 {
3226         if (conn == NULL)
3227                 return -1;
3228
3229         static int subs_serv_id = -1;
3230         static int subs_client_id = -1;
3231
3232         if (subscribe) {
3233                 if (subs_serv_id == -1) {
3234                         subs_serv_id = g_dbus_connection_signal_subscribe(conn,
3235                                 NULL, BT_NETWORK_SERVER_INTERFACE,
3236                                 NULL, NULL, NULL, 0,
3237                                 __bt_manager_event_filter,
3238                                 NULL, NULL);
3239                 }
3240                 if (subs_client_id == -1) {
3241                         subs_client_id = g_dbus_connection_signal_subscribe(conn,
3242                                 NULL, BT_NETWORK_CLIENT_INTERFACE,
3243                                 NULL, NULL, NULL, 0,
3244                                 __bt_manager_event_filter,
3245                                 NULL, NULL);
3246                 }
3247         } else {
3248                 if (subs_serv_id != -1) {
3249                         g_dbus_connection_signal_unsubscribe(conn,
3250                                         subs_serv_id);
3251                         subs_serv_id = -1;
3252                 }
3253                 if (subs_client_id != -1) {
3254                         g_dbus_connection_signal_unsubscribe(conn,
3255                                         subs_client_id);
3256                         subs_client_id = -1;
3257                 }
3258         }
3259         return 0;
3260 }
3261
3262 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
3263                 int subscribe)
3264 {
3265         if (conn == NULL)
3266                 return -1;
3267
3268         static int subs_headset_id = -1;
3269         static int subs_sink_id = -1;
3270
3271         if (subscribe) {
3272                 if (subs_headset_id == -1) {
3273                         subs_headset_id = g_dbus_connection_signal_subscribe(conn,
3274                                 NULL, BT_HEADSET_INTERFACE,
3275                                 "PropertyChanged", NULL, NULL, 0,
3276                                 __bt_manager_event_filter,
3277                                 NULL, NULL);
3278                 }
3279                 if (subs_sink_id == -1) {
3280                         subs_sink_id = g_dbus_connection_signal_subscribe(conn,
3281                                 NULL, BT_SINK_INTERFACE,
3282                                 NULL, NULL, NULL, 0,
3283                                 __bt_manager_event_filter,
3284                                 NULL, NULL);
3285                 }
3286         } else {
3287                 if (subs_headset_id != -1) {
3288                         g_dbus_connection_signal_unsubscribe(conn,
3289                                         subs_headset_id);
3290                         subs_headset_id = -1;
3291                 }
3292                 if (subs_sink_id != -1) {
3293                         g_dbus_connection_signal_unsubscribe(conn,
3294                                         subs_sink_id);
3295                         subs_sink_id = -1;
3296                 }
3297         }
3298         return 0;
3299 }
3300
3301 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
3302                 int subscribe)
3303 {
3304         if (conn == NULL)
3305                 return -1;
3306
3307         static int subs_opp_server_interface_added_id = -1;
3308         static int subs_opp_server_interface_removed_id = -1;
3309         static int subs_opp_server_property_id = -1;
3310
3311
3312         if (subscribe) {
3313                 if (subs_opp_server_interface_added_id == -1) {
3314                         subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3315                                 NULL, BT_MANAGER_INTERFACE,
3316                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3317                                 __bt_obexd_event_filter,
3318                                 NULL, NULL);
3319                 }
3320                 if (subs_opp_server_interface_removed_id == -1) {
3321                         subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3322                                 NULL, BT_MANAGER_INTERFACE,
3323                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3324                                 __bt_obexd_event_filter,
3325                                 NULL, NULL);
3326                 }
3327                 if (subs_opp_server_property_id == -1) {
3328                         subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
3329                                 NULL, BT_PROPERTIES_INTERFACE,
3330                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3331                                 __bt_obexd_event_filter,
3332                                 NULL, NULL);
3333                 }
3334         } else {
3335                 if (subs_opp_server_interface_added_id != -1) {
3336                         g_dbus_connection_signal_unsubscribe(conn,
3337                                         subs_opp_server_interface_added_id);
3338                         subs_opp_server_interface_added_id = -1;
3339                 }
3340                 if (subs_opp_server_interface_removed_id != -1) {
3341                         g_dbus_connection_signal_unsubscribe(conn,
3342                                         subs_opp_server_interface_removed_id);
3343                         subs_opp_server_interface_removed_id = -1;
3344                 }
3345                 if (subs_opp_server_property_id != -1) {
3346                         g_dbus_connection_signal_unsubscribe(conn,
3347                                         subs_opp_server_property_id);
3348                         subs_opp_server_property_id = -1;
3349                 }
3350         }
3351         return 0;
3352 }
3353
3354 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
3355                 int subscribe)
3356 {
3357         if (conn == NULL)
3358                 return -1;
3359
3360         static int subs_opp_client_interface_added_id = -1;
3361         static int subs_opp_client_interface_removed_id = -1;
3362         static int subs_opp_client_property_id = -1;
3363
3364
3365         if (subscribe) {
3366                 if (subs_opp_client_interface_added_id == -1) {
3367                         subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3368                                 NULL, BT_MANAGER_INTERFACE,
3369                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3370                                 __bt_opc_event_filter,
3371                                 NULL, NULL);
3372                 }
3373                 if (subs_opp_client_interface_removed_id == -1) {
3374                         subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3375                                 NULL, BT_MANAGER_INTERFACE,
3376                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3377                                 __bt_opc_event_filter,
3378                                 NULL, NULL);
3379                 }
3380                 if (subs_opp_client_property_id == -1) {
3381                         subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
3382                                 NULL, BT_PROPERTIES_INTERFACE,
3383                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3384                                 __bt_opc_event_filter,
3385                                 NULL, NULL);
3386                 }
3387         } else {
3388                 if (subs_opp_client_interface_added_id != -1) {
3389                         g_dbus_connection_signal_unsubscribe(conn,
3390                                         subs_opp_client_interface_added_id);
3391                         subs_opp_client_interface_added_id = -1;
3392                 }
3393                 if (subs_opp_client_interface_removed_id != -1) {
3394                         g_dbus_connection_signal_unsubscribe(conn,
3395                                         subs_opp_client_interface_removed_id);
3396                         subs_opp_client_interface_removed_id = -1;
3397                 }
3398                 if (subs_opp_client_property_id != -1) {
3399                         g_dbus_connection_signal_unsubscribe(conn,
3400                                         subs_opp_client_property_id);
3401                         subs_opp_client_property_id = -1;
3402                 }
3403         }
3404         return 0;
3405 }
3406
3407 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
3408                 int subscribe)
3409 {
3410         if (conn == NULL)
3411                 return -1;
3412
3413         static int subs_a2dp_source_id = -1;
3414         static int subs_a2dp_sink_id = -1;
3415
3416         if (subscribe) {
3417                 if (subs_a2dp_source_id == -1) {
3418                         subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
3419                                 NULL, BT_A2DP_SOURCE_INTERFACE,
3420                                 NULL, NULL, NULL, 0,
3421                                 __bt_opc_event_filter,
3422                                 NULL, NULL);
3423                 }
3424                 if (subs_a2dp_sink_id == -1) {
3425                         subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
3426                                 NULL, BT_SINK_INTERFACE,
3427                                 NULL, NULL, NULL, 0,
3428                                 __bt_opc_event_filter,
3429                                 NULL, NULL);
3430                 }
3431         } else {
3432                 if (subs_a2dp_source_id != -1) {
3433                         g_dbus_connection_signal_unsubscribe(conn,
3434                                         subs_a2dp_source_id);
3435                         subs_a2dp_source_id = -1;
3436                 }
3437                 if (subs_a2dp_sink_id != -1) {
3438                         g_dbus_connection_signal_unsubscribe(conn,
3439                                         subs_a2dp_sink_id);
3440                         subs_a2dp_sink_id = -1;
3441                 }
3442         }
3443         return 0;
3444 }
3445
3446 static void __bt_dump_event_filter(GDBusConnection *connection,
3447                                         const gchar *sender_name,
3448                                         const gchar *object_path,
3449                                         const gchar *interface_name,
3450                                         const gchar *signal_name,
3451                                         GVariant *parameters,
3452                                         gpointer user_data)
3453 {
3454         return;
3455 }
3456
3457 int __bt_register_dump_subscribe_signal(GDBusConnection *conn,
3458                 gboolean subscribe)
3459 {
3460         if (conn == NULL)
3461                 return -1;
3462
3463         static int subs_source_id = -1;
3464
3465         if (subscribe) {
3466                 if (subs_source_id == -1) {
3467                         subs_source_id = g_dbus_connection_signal_subscribe(conn,
3468                                 NULL, BT_DUMP_SERVICE_INTERFACE,
3469                                 BT_DUMP_SERVICE_SIGNAL, BT_DUMP_SERVICE_PATH, NULL, 0,
3470                                 __bt_dump_event_filter,
3471                                 NULL, NULL);
3472                 }
3473         } else {
3474                 if (subs_source_id != -1) {
3475                         g_dbus_connection_signal_unsubscribe(conn,
3476                                         subs_source_id);
3477                         subs_source_id = -1;
3478                 }
3479         }
3480         return 0;
3481 }
3482
3483 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
3484 {
3485         BT_DBG("+");
3486
3487         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3488
3489         switch (event_type) {
3490         case BT_MANAGER_EVENT:
3491                 _bt_register_manager_subscribe_signal(g_conn, TRUE);
3492                 break;
3493         case BT_DEVICE_EVENT:
3494                 _bt_register_device_subscribe_signal(g_conn, TRUE);
3495                 break;
3496         case BT_HID_EVENT:
3497                 _bt_register_input_subscribe_signal(g_conn, TRUE);
3498                 break;
3499         case BT_NETWORK_EVENT:
3500                 _bt_register_network_subscribe_signal(g_conn, TRUE);
3501                 break;
3502         case BT_HEADSET_EVENT:
3503                 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3504                 break;
3505
3506         case BT_OPP_SERVER_EVENT:
3507                 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3508                 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3509                 break;
3510         case BT_OPP_CLIENT_EVENT:
3511                 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3512                 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3513                 break;
3514         case BT_A2DP_SOURCE_EVENT:
3515                 BT_INFO("A2dp Source event");
3516                 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3517                 break;
3518         default:
3519                 BT_ERR("Unknown event");
3520                 return BLUETOOTH_ERROR_INTERNAL;
3521         }
3522
3523         return BLUETOOTH_ERROR_NONE;
3524 }
3525
3526 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3527 {
3528         BT_DBG("+");
3529
3530         ret_if(g_conn == NULL);
3531
3532         switch (event_type) {
3533         case BT_MANAGER_EVENT:
3534                 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3535                 _bt_register_device_subscribe_signal(g_conn, FALSE);
3536                 _bt_register_input_subscribe_signal(g_conn, FALSE);
3537                 _bt_register_network_subscribe_signal(g_conn, FALSE);
3538                 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3539                 break;
3540         case BT_OPP_SERVER_EVENT:
3541                 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3542                 break;
3543         case BT_OPP_CLIENT_EVENT:
3544                 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3545                 break;
3546         default:
3547                 BT_ERR("Unknown event");
3548                 return;
3549         }
3550
3551         BT_DBG("-");
3552 }
3553
3554 static int __bt_init_manager_receiver(void)
3555 {
3556         BT_DBG("+");
3557
3558         GError *error = NULL;
3559
3560         if (manager_conn == NULL) {
3561                 manager_conn =  g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3562                 if (error != NULL) {
3563                         BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3564                         g_clear_error(&error);
3565                 }
3566                 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3567         }
3568
3569         if (_bt_register_service_event(manager_conn,
3570                                 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3571                 goto fail;
3572         if (_bt_register_service_event(manager_conn,
3573                                 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3574                 goto fail;
3575
3576         if (_bt_register_service_event(manager_conn,
3577                                 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3578                 goto fail;
3579
3580         if (_bt_register_service_event(manager_conn,
3581                                 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3582                 goto fail;
3583
3584         if (_bt_register_service_event(manager_conn,
3585                                 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3586                 goto fail;
3587
3588         __bt_register_dump_subscribe_signal(manager_conn, TRUE);
3589         return BLUETOOTH_ERROR_NONE;
3590 fail:
3591         if (manager_conn) {
3592                 g_object_unref(manager_conn);
3593                 manager_conn = NULL;
3594         }
3595
3596         BT_DBG("-");
3597
3598         return BLUETOOTH_ERROR_INTERNAL;
3599 }
3600
3601 static int __bt_init_obexd_receiver(void)
3602 {
3603         BT_DBG("+");
3604 #ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3605         GError *error = NULL;
3606
3607         if (obexd_conn == NULL) {
3608                 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3609                 if (error != NULL) {
3610                         BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3611                         g_clear_error(&error);
3612                 }
3613                 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3614         }
3615
3616         if (_bt_register_service_event(obexd_conn,
3617                                 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3618                 BT_ERR("Error while registering service event");
3619                 g_object_unref(obexd_conn);
3620                 obexd_conn = NULL;
3621                 return BLUETOOTH_ERROR_INTERNAL;
3622         }
3623 #endif
3624         BT_DBG("-");
3625
3626         return BLUETOOTH_ERROR_NONE;
3627 }
3628
3629 gboolean __bt_reinit_obexd_receiver(gpointer user_data)
3630 {
3631         static int retry_cnt = 0;
3632         int result = BLUETOOTH_ERROR_NONE;
3633
3634         BT_DBG("+");
3635
3636         result = __bt_init_obexd_receiver();
3637         if (result != BLUETOOTH_ERROR_NONE) {
3638                 /* 20 ms * 50 = 10 seconds. During 10 seconds fail to initialize,
3639                    then it is not the timing issue. Just can't use the session bus connection */
3640                 if (retry_cnt > 100) {
3641                         BT_ERR("Fail to init obexd receiver by 50 times.");
3642                         retry_cnt = 0;
3643                         session_reinit_timer = 0;
3644                         return FALSE;
3645                 }
3646                 retry_cnt++;
3647                 BT_DBG("Retry to initialize the obexd receiver");
3648                 return TRUE;
3649         }
3650
3651         retry_cnt = 0;
3652         session_reinit_timer = 0;
3653
3654         BT_DBG("-");
3655
3656         return FALSE;
3657 }
3658
3659 /* To receive the event from bluez */
3660 int _bt_init_service_event_receiver(void)
3661 {
3662         BT_DBG("+");
3663
3664         int result;
3665
3666         result = __bt_init_manager_receiver();
3667         retv_if(result != BLUETOOTH_ERROR_NONE, result);
3668
3669         result = __bt_init_obexd_receiver();
3670         if (result != BLUETOOTH_ERROR_NONE) {
3671                 BT_ERR("Fail to init obexd receiver");
3672
3673                 /* Try to re-initialize obexd receiver in the timer */
3674                 if (session_reinit_timer > 0)
3675                         g_source_remove(session_reinit_timer);
3676
3677                 session_reinit_timer = g_timeout_add(BT_SESSION_BUS_GET_TIMEOUT,
3678                                                         (GSourceFunc)__bt_reinit_obexd_receiver, NULL);
3679         }
3680
3681         BT_DBG("-");
3682
3683         return BLUETOOTH_ERROR_NONE;
3684 }
3685
3686 void _bt_deinit_service_event_receiver(void)
3687 {
3688         BT_DBG("+");
3689
3690         _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3691
3692         _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3693
3694         __bt_register_dump_subscribe_signal(manager_conn, FALSE);
3695
3696         if (manager_conn) {
3697                 g_object_unref(manager_conn);
3698                 manager_conn = NULL;
3699         }
3700
3701         if (obexd_conn) {
3702                 g_object_unref(obexd_conn);
3703                 obexd_conn = NULL;
3704         }
3705
3706         if (event_id > 0)
3707                 g_source_remove(event_id);
3708         event_id = 0;
3709
3710         BT_DBG("-");
3711 }