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