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