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