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