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