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