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