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