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