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