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