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