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