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