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