067a4e9071c5711bb8da3421929fe64f6db0ffff
[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         int is_flight_mode = 0;
437         int ret = -1;
438
439         if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
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         }
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                                 if (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                                 }
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                                 if (TIZEN_FEATURE_BT_USB_DONGLE)
804                                         _bt_handle_adapter_added();
805                         }
806                 } else if (strcasecmp(property, "Connectable") == 0) {
807                         gboolean connectable = FALSE;
808
809                         g_variant_get(val, "b", &connectable);
810
811                         BT_DBG("Connectable property is changed : %d", connectable);
812                         param = g_variant_new("(b)", connectable);
813                         _bt_send_event(BT_ADAPTER_EVENT,
814                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
815                                 param);
816 #if 0
817                         if (_bt_adapter_get_status() == BT_DEACTIVATING &&
818                         _bt_adapter_get_le_status() == BT_LE_ACTIVATED &&
819                         connectable == 0)
820                         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
821 #endif
822                 } else if (strcasecmp(property, "SupportedLEFeatures") == 0) {
823                         char *name = NULL;
824                         char *value = NULL;
825                         GVariantIter *iter = NULL;
826                         g_variant_get(val, "as", &iter);
827
828                         ret_if(iter == NULL);
829
830                         while (g_variant_iter_loop(iter, "s", &name)) {
831                                 BT_DBG("name = %s", name);
832                                 g_variant_iter_loop(iter, "s", &value);
833                                 BT_DBG("Value = %s", value);
834                                 if (FALSE == _bt_update_le_feature_support(name, value))
835                                         BT_INFO("Fail to update LE feature info");
836                         }
837
838                         g_variant_iter_free(iter);
839                 } else if (strcasecmp(property, "IpspInitStateChanged") == 0) {
840                         gboolean ipsp_initialized = FALSE;
841
842                         g_variant_get(val, "b", &ipsp_initialized);
843                         BT_DBG("IPSP init state changed: %d", ipsp_initialized);
844                         param = g_variant_new("(b)", ipsp_initialized);
845
846                         /* Send event to application */
847                         _bt_send_event(BT_ADAPTER_EVENT,
848                                         BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,
849                                         param);
850                 } else {
851                         BT_DBG("property : [%s]", property);
852                 }
853         }
854 }
855
856 static void __bt_obex_property_changed_event(GVariant *msg, const char *path)
857 {
858         BT_DBG("+");
859
860         GVariantIter value_iter;
861         GVariant *child = NULL, *val = NULL;
862         char *property = NULL;
863         g_variant_iter_init(&value_iter, msg);
864         while ((child = g_variant_iter_next_value(&value_iter))) {
865                 g_variant_get(child, "{sv}", &property, &val);
866
867                 ret_if(property == NULL);
868
869                 BT_DBG("property :%s", property);
870
871                 if (strcasecmp(property, "Status") == 0) {
872                         char  *status;
873                         g_variant_get(val, "s", &status);
874
875                         if (strcasecmp(status, "active") == 0) {
876                                 _bt_obex_transfer_started(path);
877                         } else if (strcasecmp(status, "complete") == 0) {
878                                 _bt_obex_transfer_completed(path, TRUE);
879                                 _bt_pbap_obex_transfer_completed(path, TRUE);
880                         } else if (strcasecmp(status, "error") == 0) {
881                                 _bt_obex_transfer_completed(path, FALSE);
882                                 _bt_pbap_obex_transfer_completed(path, FALSE);
883                         }
884                         g_free(status);
885                 } else if (strcasecmp(property, "Transferred") == 0) {
886                         guint64 transferred  = 0;
887                         /* As Transferred is expected guint64 so change int to guint64 and
888                          * eariler transferred is static because of it can overwrite data
889                          * on present on opc_obex_conn or obexd_conn as these are
890                          * static memory are in sequential */
891                         g_variant_get(val, "t", &transferred);
892
893                         _bt_obex_transfer_progress(path, transferred);
894                 }
895                 g_free(property);
896                 g_variant_unref(val);
897                 g_variant_unref(child);
898         }
899         BT_DBG("-");
900 }
901
902 static void __bt_device_property_changed_event(GVariant *msg, const char *path)
903 {
904         BT_DBG("+");
905
906         int event;
907         int result = BLUETOOTH_ERROR_NONE;
908         GVariantIter value_iter;
909         GVariant *val;
910         char *property = NULL;
911         char *address;
912         GVariant *param = NULL;
913         bt_remote_dev_info_t *remote_dev_info;
914         g_variant_iter_init(&value_iter, msg);
915 #ifdef TIZEN_FEATURE_BT_DPM
916         int desktop_state = DPM_BT_ERROR;
917 #endif
918         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
919                 BT_DBG("Property %s", property);
920                 if (strcasecmp(property, "Connected") == 0) {
921                         guint connected = 0;
922
923                         g_variant_get(val, "i", &connected);
924
925                         event = (connected != BLUETOOTH_CONNECTED_LINK_NONE) ?
926                                 BLUETOOTH_EVENT_DEVICE_CONNECTED :
927                                 BLUETOOTH_EVENT_DEVICE_DISCONNECTED;
928
929                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
930
931                         _bt_convert_device_path_to_address(path, address);
932
933                         BT_DBG("connected: %d", connected);
934                         BT_DBG("address: %s", address);
935
936                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
937
938                         if (remote_dev_info != NULL) {
939                                 __bt_device_remote_connected_properties(
940                                                 remote_dev_info, address,
941                                                 connected != BLUETOOTH_CONNECTED_LINK_NONE ?
942                                                 TRUE : FALSE);
943                                 _bt_free_device_info(remote_dev_info);
944                         }
945                         param = g_variant_new("(is)", result, address);
946                         /* Send event to application */
947                         _bt_send_event(BT_DEVICE_EVENT,
948                                         event,
949                                         param);
950                         g_free(address);
951                 } else if (strcasecmp(property, "RSSI") == 0) {
952                         bt_remote_dev_info_t *remote_dev_info;
953
954                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
955
956                         _bt_convert_device_path_to_address(path, address);
957                         BT_DBG("address: %s", address);
958
959                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
960                         if (remote_dev_info == NULL) {
961                                 g_free(property);
962                                 g_variant_unref(val);
963                                 g_free(address);
964                                 return;
965                         }
966                         BT_DBG("Address type  %d", remote_dev_info->addr_type);
967
968                         if (remote_dev_info->addr_type == 0) {
969                                 BT_DBG("Name %s", remote_dev_info->name);
970
971 #ifdef TIZEN_FEATURE_BT_DPM
972                                 _bt_dpm_get_bluetooth_desktop_connectivity_state(&desktop_state);
973                                 if (desktop_state == DPM_RESTRICTED) {
974                                         bluetooth_device_class_t device_class;
975                                         _bt_divide_device_class(&device_class, remote_dev_info->class);
976
977                                         if (device_class.major_class ==
978                                                 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
979                                                 _bt_free_device_info(remote_dev_info);
980                                                 g_free(property);
981                                                 g_variant_unref(val);
982                                                 g_free(address);
983                                                 return;
984                                         }
985                                 }
986 #endif
987
988                                 GVariant *uuids = NULL;
989                                 GVariantBuilder *builder = NULL;
990                                 int i = 0;
991                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
992                                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
993                                         g_variant_builder_add(builder, "s",
994                                                 remote_dev_info->uuids[i]);
995                                 }
996                                 uuids = g_variant_new("as", builder);
997                                 g_variant_builder_unref(builder);
998                                 GVariant *manufacturer_data =  NULL;
999                                 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1000                                                                         remote_dev_info->manufacturer_data,
1001                                                                         remote_dev_info->manufacturer_data_len,
1002                                                                         TRUE,
1003                                                                         NULL, NULL);
1004                                 param = g_variant_new("(isunsbub@asn@ay)", result,
1005                                                         remote_dev_info->address,
1006                                                         remote_dev_info->class,
1007                                                         remote_dev_info->rssi,
1008                                                         remote_dev_info->name,
1009                                                         remote_dev_info->paired,
1010                                                         remote_dev_info->connected,
1011                                                         remote_dev_info->trust,
1012                                                         uuids,
1013                                                         remote_dev_info->manufacturer_data_len,
1014                                                         manufacturer_data);
1015
1016                                 _bt_send_event(BT_ADAPTER_EVENT,
1017                                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1018                                         param);
1019                                 g_free(address);
1020                         }
1021                         _bt_free_device_info(remote_dev_info);
1022                 } else if (strcasecmp(property, "GattConnected") == 0) {
1023                         gboolean gatt_connected = FALSE;
1024
1025                         g_variant_get(val, "b", &gatt_connected);
1026
1027                         event = gatt_connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1028                                         BLUETOOTH_EVENT_GATT_DISCONNECTED;
1029
1030                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1031
1032                         _bt_convert_device_path_to_address(path, address);
1033
1034                         BT_DBG("gatt_connected: %d", gatt_connected);
1035                         BT_DBG("address: %s", address);
1036                         param = g_variant_new("(is)", result, address);
1037                         /* Send event to application */
1038                         _bt_send_event(BT_DEVICE_EVENT,
1039                                         event,
1040                                         param);
1041                         g_free(address);
1042                 } else if (strcasecmp(property, "Paired") == 0) {
1043                         gboolean paired = FALSE;
1044                         bt_remote_dev_info_t *remote_dev_info;
1045                         g_variant_get(val, "b", &paired);
1046                         _bt_agent_set_canceled(FALSE);
1047                         /* BlueZ sends paired signal for each paired device */
1048                         /* during activation, We should ignore this, otherwise*/
1049                         /* application thinks that a new device got paired */
1050                         if (_bt_adapter_get_status() != BT_ACTIVATED) {
1051                                 BT_DBG("BT is not activated, so ignore this");
1052                                 g_free(property);
1053                                 g_variant_unref(val);
1054                                 return;
1055                         }
1056
1057                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1058
1059                         _bt_convert_device_path_to_address(path, address);
1060
1061                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1062                         if (remote_dev_info == NULL) {
1063                                 g_free(property);
1064                                 g_variant_unref(val);
1065                                 g_free(address);
1066                                 return;
1067                         }
1068
1069                         if (paired == FALSE) {
1070                                 BT_INFO("Unpaired: %s", address);
1071                                 __bt_update_remote_cache_devinfo(address, FALSE);
1072                                 param = g_variant_new("(is)", result, address);
1073                                 _bt_send_event(BT_ADAPTER_EVENT,
1074                                         BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
1075                                         param);
1076 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1077                                 {
1078                                         char *last_connected = NULL;
1079                                         last_connected = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1080                                         if (!g_strcmp0(address, last_connected))
1081                                                 _bt_audio_set_auto_connect_device_addr("");
1082                                         if (last_connected)
1083                                                 free(last_connected);
1084                                 }
1085 #endif
1086                         } else {
1087                                 char secure_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
1088
1089                                 _bt_convert_addr_string_to_secure_string(secure_addr, address);
1090                                 BT_INFO("### Paired: %s", secure_addr);
1091                                 __bt_update_remote_cache_devinfo(address, TRUE);
1092
1093                                 if (_bt_is_device_creating() == TRUE) {
1094                                         BT_DBG("Try to Pair by me");
1095                                         _bt_free_device_info(remote_dev_info);
1096                                         g_free(address);
1097                                         g_free(property);
1098                                         g_variant_unref(val);
1099                                         return;
1100                                 }
1101                                 GVariant *uuids = NULL;
1102                                 GVariantBuilder *builder = NULL;
1103                                 int i = 0;
1104                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1105                                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1106                                         g_variant_builder_add(builder, "s",
1107                                                 remote_dev_info->uuids[i]);
1108                                 }
1109                                 uuids = g_variant_new("as", builder);
1110                                 g_variant_builder_unref(builder);
1111                                 GVariant *manufacturer_data =  NULL;
1112                                 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1113                                                                         remote_dev_info->manufacturer_data,
1114                                                                         remote_dev_info->manufacturer_data_len,
1115                                                                         TRUE,
1116                                                                         NULL, NULL);
1117
1118                                 param = g_variant_new("(isunsbub@asn@ay)", result,
1119                                                         address, remote_dev_info->class,
1120                                                         remote_dev_info->rssi,
1121                                                         remote_dev_info->name,
1122                                                         remote_dev_info->paired,
1123                                                         remote_dev_info->connected,
1124                                                         remote_dev_info->trust,
1125                                                         uuids,
1126                                                         remote_dev_info->manufacturer_data_len,
1127                                                         manufacturer_data);
1128                                 _bt_send_event(BT_ADAPTER_EVENT,
1129                                         BLUETOOTH_EVENT_BONDING_FINISHED,
1130                                         param);
1131                         }
1132                         _bt_free_device_info(remote_dev_info);
1133                         g_free(address);
1134                 } else if (strcasecmp(property, "LegacyPaired") == 0) {
1135                         gboolean paired = FALSE;
1136                         bt_remote_dev_info_t *remote_dev_info;
1137                         unsigned char auth_info[5] = {0, };
1138
1139                         if (_bt_adapter_get_status() != BT_ACTIVATED) {
1140                                 BT_DBG("BT is not activated, so ignore this");
1141                                 g_free(property);
1142                                 g_variant_unref(val);
1143                                 return;
1144                         }
1145
1146                         g_variant_get(val, "b", &paired);
1147                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1148                         BT_DBG("LegacyPaired: %d", paired);
1149                         _bt_convert_device_path_to_address(path, address);
1150
1151                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1152                         if (remote_dev_info == NULL) {
1153                                 g_free(address);
1154                                 g_free(property);
1155                                 g_variant_unref(val);
1156                                 return;
1157                         }
1158                         if (remote_dev_info->is_alias_set == FALSE) {
1159                                 /*minimum Size of the samsung specific manufacturer data is greater than 30 */
1160                                 if ((remote_dev_info->manufacturer_data_len > 30) &&
1161                                         (remote_dev_info->manufacturer_data[0] == 0x00) &&
1162                                         (remote_dev_info->manufacturer_data[1] == 0x75)) {
1163
1164                                         /* 2  samsung (0x00 0x75) + 1 (control and version) + 1 (service ID) +
1165                                         1 (discovery version) + 1 (associated service ID)
1166                                         2 (Proxamity and locality) + 2 (Device type and icon) = 10 */
1167
1168                                         memcpy(auth_info, &(remote_dev_info->manufacturer_data[10]), 5);
1169                                 }
1170                         }
1171
1172                         BT_DBG("LegacyPairing Failed with %s. Show Error Popup", remote_dev_info->name);
1173                         _bt_launch_system_popup(BT_AGENT_EVENT_LEGACY_PAIR_FAILED_FROM_REMOTE,
1174                                                 remote_dev_info->name, auth_info, NULL, NULL, NULL);
1175
1176                         _bt_free_device_info(remote_dev_info);
1177                         g_free(address);
1178                 } else if (strcasecmp(property, "Trusted") == 0) {
1179                         gboolean trusted = FALSE;
1180
1181                         g_variant_get(val, "b", &trusted);
1182
1183                         event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1184                                         BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1185
1186                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1187
1188                         _bt_convert_device_path_to_address(path, address);
1189
1190                         BT_DBG("trusted: %d", trusted);
1191                         BT_DBG("address: %s", address);
1192                         param = g_variant_new("(is)", result, address);
1193                         /* Send event to application */
1194                         _bt_send_event(BT_DEVICE_EVENT,
1195                                         event,
1196                                         param);
1197                         g_free(address);
1198                 } else if (strcasecmp(property, "TrustedProfiles") == 0) {
1199                         int trusted = 0;
1200
1201                         g_variant_get(val, "u", &trusted);
1202
1203                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1204                         _bt_convert_device_path_to_address(path, address);
1205
1206                         BT_DBG("Address: %s, TrustedProfiles: %d", address, trusted);
1207                         _bt_send_event(BT_DEVICE_EVENT,
1208                                         BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1209                                         g_variant_new("(isi)", result, address, trusted));
1210                         g_free(address);
1211                 } else if (strcasecmp(property, "IpspConnected") == 0) {
1212                         gboolean connected = FALSE;
1213
1214                         g_variant_get(val, "b", &connected);
1215
1216
1217                         event = connected ? BLUETOOTH_EVENT_IPSP_CONNECTED :
1218                                                         BLUETOOTH_EVENT_IPSP_DISCONNECTED;
1219
1220                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1221
1222                         _bt_convert_device_path_to_address(path, address);
1223
1224                         BT_DBG("Ipspconnected: %d", connected);
1225                         BT_DBG("address: %s", address);
1226                         param = g_variant_new("(is)", result, address);
1227
1228                         /* Send event to application */
1229                         _bt_send_event(BT_DEVICE_EVENT,
1230                                         event,
1231                                         param);
1232                         g_free(address);
1233                 } else if (strcasecmp(property, "IpspBtInterfaceInfo") == 0) {
1234                         char *ifname = NULL;
1235
1236                         g_variant_get(val, "s", &ifname);
1237
1238                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1239
1240                         _bt_convert_device_path_to_address(path, address);
1241
1242                         BT_DBG("Ipsp BT Interface Name: %s", ifname);
1243                         BT_DBG("address: %s", address);
1244                         param = g_variant_new("(iss)", result, address, ifname);
1245
1246                         /* Send event to application */
1247                         _bt_send_event(BT_DEVICE_EVENT,
1248                                         BLUETOOTH_EVENT_IPSP_INTERFACE_INFO,
1249                                         param);
1250                         g_free(address);
1251                 }
1252         }
1253         BT_DBG("-");
1254 }
1255
1256 static void __bt_media_control_changed_event(GVariant *msg, const char *path)
1257 {
1258         int event;
1259         int result = BLUETOOTH_ERROR_NONE;
1260         GVariantIter value_iter;
1261         char *property = NULL;
1262         char *address;
1263         GVariant *val = NULL;
1264         GVariant *child = NULL;
1265         bt_remote_dev_info_t *remote_dev_info;
1266         GVariant *param = NULL;
1267         g_variant_iter_init(&value_iter, msg);
1268         while ((child = g_variant_iter_next_value(&value_iter))) {
1269                 g_variant_get(child, "{sv}", &property, &val);
1270                 BT_INFO("Property %s", property);
1271                 if (strcasecmp(property, "Connected") == 0) {
1272                         gboolean connected = FALSE;
1273
1274                         g_variant_get(val, "b", &connected);
1275
1276                         event = connected ? BLUETOOTH_EVENT_AVRCP_CONNECTED :
1277                                         BLUETOOTH_EVENT_AVRCP_DISCONNECTED;
1278
1279                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1280
1281                         _bt_convert_device_path_to_address(path, address);
1282
1283                         BT_DBG("connected: %d", connected);
1284                         BT_DBG("address: %s", address);
1285
1286                         remote_dev_info = _bt_get_remote_device_info(address);
1287
1288                         if (remote_dev_info != NULL) {
1289                                 __bt_device_remote_connected_properties(
1290                                 remote_dev_info, address, connected);
1291                                 _bt_free_device_info(remote_dev_info);
1292                         }
1293                         param = g_variant_new("(is)", result, address);
1294                         /* Send event to application */
1295                         _bt_send_event(BT_AVRCP_EVENT,
1296                                 event,
1297                                 param);
1298                         g_free(address);
1299                 }
1300                 g_free(property);
1301                 g_variant_unref(child);
1302                 g_variant_unref(val);
1303         }
1304         BT_DBG("-");
1305 }
1306
1307 int get_alert_level_enum(const char *alert)
1308 {
1309         int lvl = -1;
1310
1311         if (strcmp(alert, "none") == 0)
1312                 lvl = BT_PXP_ALERT_NONE;
1313         else if (strcmp(alert, "mild") == 0)
1314                 lvl = BT_PXP_ALERT_MILD;
1315         else if (strcmp(alert, "high") == 0)
1316                 lvl = BT_PXP_ALERT_HIGH;
1317
1318         return lvl;
1319 }
1320
1321 static void _bt_handle_pxp_property_changed_event(GVariant *msg, const char *path, int role)
1322 {
1323         int result = BLUETOOTH_ERROR_NONE;
1324         int service_type;
1325         int alert_lvl;
1326         GVariantIter value_iter;
1327         char *property = NULL;
1328         char *address;
1329         GVariant *val = NULL;
1330         GVariant *child = NULL;
1331         GVariant *param = NULL;
1332         g_variant_iter_init(&value_iter, msg);
1333
1334         BT_DBG("+");
1335         while ((child = g_variant_iter_next_value(&value_iter))) {
1336                 g_variant_get(child, "{sv}", &property, &val);
1337                 BT_INFO("Property %s", property);
1338
1339                 if ((strcasecmp(property, "LinkLossAlertLevel") == 0) ||
1340                         (strcasecmp(property, "ImmediateAlertLevel") == 0)) {
1341                         char *alert_str = NULL;
1342
1343                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1344                         _bt_convert_device_path_to_address(path, address);
1345
1346                         if (strcasecmp(property, "LinkLossAlertLevel") == 0)
1347                                 service_type = BT_PXP_PROPERTY_LLS;
1348                         else
1349                                 service_type = BT_PXP_PROPERTY_IAS;
1350
1351                         g_variant_get(val, "s", &alert_str);
1352                         alert_lvl = get_alert_level_enum(alert_str);
1353
1354                         param = g_variant_new("(isiii)", result, address,
1355                                                                 role, service_type, alert_lvl);
1356
1357                         /* Send event to application */
1358                         _bt_send_event(BT_DEVICE_EVENT,
1359                                         BLUETOOTH_EVENT_PXP_PROPERTY_CHANGED,
1360                                         param);
1361                         g_free(address);
1362                         g_free(alert_str);
1363                 }
1364                 g_free(property);
1365                 g_variant_unref(child);
1366                 g_variant_unref(val);
1367         }
1368         BT_DBG("-");
1369 }
1370
1371 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
1372 {
1373         char *interface_name = NULL;
1374         GVariant *val = NULL;
1375
1376         g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &val, NULL);
1377
1378         if (strcasecmp(interface_name, BT_ADAPTER_INTERFACE) == 0) {
1379                 __bt_adapter_property_changed_event(val,
1380                                         object_path);
1381         } else if (strcasecmp(interface_name, BT_DEVICE_INTERFACE) == 0) {
1382                 __bt_device_property_changed_event(val, object_path);
1383         } else if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1384                 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
1385                 __bt_obex_property_changed_event(val,
1386                                         object_path);
1387         } else if (strcasecmp(interface_name, BT_MEDIA_CONTROL_INTERFACE) == 0) {
1388                 __bt_media_control_changed_event(val,
1389                                         object_path);
1390         } else if (strcasecmp(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
1391                 _bt_handle_avrcp_control_event(val,
1392                                         object_path);
1393         } else if (strcasecmp(interface_name, BT_NETWORK_CLIENT_INTERFACE) == 0) {
1394                 BT_DBG("BT_NETWORK_CLIENT_INTERFACE");
1395                 _bt_handle_network_client_event(val,
1396                                         object_path);
1397         } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
1398                 __bt_gatt_char_property_changed_event(val,
1399                                         object_path);
1400         } else if (strcasecmp(interface_name, BT_PROXIMITY_REPORTER_INTERFACE) == 0) {
1401                 BT_DBG("BT_PROXIMITY_REPORTER_INTERFACE");
1402                 _bt_handle_pxp_property_changed_event(val,
1403                                         object_path, BT_PXP_REPORTER_ROLE);
1404         }
1405         g_variant_unref(val);
1406 }
1407
1408 void __bt_opc_property_changed_event(GVariant *msg,
1409                                                 const char *path)
1410 {
1411         GVariantIter value_iter;
1412         char *property = NULL;
1413         GVariant *val = NULL;
1414         GVariant *child = NULL;
1415
1416         g_variant_iter_init(&value_iter, msg);
1417         while ((child = g_variant_iter_next_value(&value_iter))) {
1418                 g_variant_get(child, "{sv}", &property, &val);
1419                 ret_if(property == NULL);
1420
1421                 if (strcasecmp(property, "Status") == 0) {
1422                         char *status = NULL;
1423                         g_variant_get(val, "s", &status);
1424                         BT_DBG("Status is %s", status);
1425
1426                         if (strcasecmp(status, "active") == 0)
1427                                 _bt_obex_client_started(path);
1428                         else if (strcasecmp(status, "complete") == 0)
1429                                 _bt_obex_client_completed(path, TRUE);
1430                         else if (strcasecmp(status, "error") == 0)
1431                                 _bt_obex_client_completed(path, FALSE);
1432
1433                         g_free(status);
1434                 } else if (strcasecmp(property, "Transferred") == 0) {
1435                         guint64 transferred  = 0;
1436                         g_variant_get(val, "t", &transferred);
1437
1438                         _bt_obex_client_progress(path, transferred);
1439                 } else {
1440                         BT_DBG("property : [%s]", property);
1441                 }
1442                 g_free(property);
1443                 g_variant_unref(child);
1444                 g_variant_unref(val);
1445         }
1446 }
1447
1448 void _bt_opc_property_changed_event(GVariant *msg, char *path)
1449 {
1450         char *interface_name = NULL;
1451         GVariant *value = NULL;
1452         g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1453         BT_INFO("interface_name = %s", interface_name);
1454         if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
1455                 __bt_opc_property_changed_event(value,
1456                                         path);
1457         } else {
1458                 BT_DBG("interface_name : [%s]", interface_name);
1459         }
1460         g_variant_unref(value);
1461 }
1462
1463
1464 void _bt_handle_input_event(GVariant *msg, const char *path)
1465 {
1466         int result = BLUETOOTH_ERROR_NONE;
1467         gboolean property_flag = FALSE;
1468         GVariantIter value_iter;
1469         char *property = NULL;
1470         GVariant *child = NULL, *val = NULL;
1471         bt_remote_dev_info_t *remote_dev_info;
1472         GVariant *param = NULL;
1473         g_variant_iter_init(&value_iter, msg);
1474         while ((child = g_variant_iter_next_value(&value_iter))) {
1475                 g_variant_get(child, "{sv}", &property, &val);
1476
1477                 ret_if(property == NULL);
1478
1479                 if (strcasecmp(property, "Connected") == 0) {
1480                         int event = BLUETOOTH_EVENT_NONE;
1481                         char *address;
1482                         g_variant_get(val, "b", &property_flag);
1483
1484                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1485
1486                         _bt_convert_device_path_to_address(path, address);
1487
1488                         event = (property_flag == TRUE) ?
1489                                         BLUETOOTH_HID_CONNECTED :
1490                                         BLUETOOTH_HID_DISCONNECTED;
1491                         param = g_variant_new("(is)", result, address);
1492                         _bt_send_event(BT_HID_EVENT, event,
1493                                 param);
1494                         /* Check HID connection type (Keyboard or Mouse) and update the status */
1495                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1496
1497                         if (property_flag == TRUE) {
1498                                 hid_connected_device_count++;
1499                                 __bt_set_device_values(TRUE,
1500                                                 VCONFKEY_BT_DEVICE_HID_CONNECTED);
1501                         } else {
1502                                 hid_connected_device_count--;
1503                                 if (hid_connected_device_count == 0)
1504                                         __bt_set_device_values(FALSE,
1505                                                         VCONFKEY_BT_DEVICE_HID_CONNECTED);
1506                         }
1507
1508                         if (remote_dev_info != NULL) {
1509                                 BT_DBG("HID device class [%x]", remote_dev_info->class);
1510                                 if (remote_dev_info->class &
1511                                         BLUETOOTH_DEVICE_MINOR_CLASS_KEY_BOARD) {
1512                                         __bt_set_device_values(property_flag,
1513                                                 VCONFKEY_BT_DEVICE_HID_KEYBOARD_CONNECTED);
1514
1515                                 }
1516
1517                                 if (remote_dev_info->class &
1518                                                 BLUETOOTH_DEVICE_MINOR_CLASS_POINTING_DEVICE) {
1519                                         __bt_set_device_values(property_flag,
1520                                                         VCONFKEY_BT_DEVICE_HID_MOUSE_CONNECTED);
1521                                 }
1522                                 _bt_free_device_info(remote_dev_info);
1523                         }
1524                         g_free(address);
1525                 }
1526                 g_free(property);
1527                 g_variant_unref(val);
1528                 g_variant_unref(child);
1529          }
1530 }
1531
1532 void _bt_handle_network_server_event(GVariant *msg, const char *member)
1533 {
1534         int result = BLUETOOTH_ERROR_NONE;
1535         char *address = NULL;
1536         char *device = NULL;
1537         GVariant *param = NULL;
1538         ret_if(member == NULL);
1539         if (strcasecmp(member, "PeerConnected") == 0) {
1540                 g_variant_get(msg, "(ss)", &device, &address);
1541
1542                 __bt_set_device_values(TRUE,
1543                                 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1544                 param = g_variant_new("(iss)", result, device, address);
1545                 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
1546                         param);
1547                 g_free(device);
1548                 g_free(address);
1549                  nap_connected_device_count++;
1550         } else if (strcasecmp(member, "PeerDisconnected") == 0) {
1551                 g_variant_get(msg, "(ss)", &device, &address);
1552                 nap_connected_device_count--;
1553                 if (nap_connected_device_count == 0)
1554                         __bt_set_device_values(FALSE,
1555                                 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1556                 param = g_variant_new("(iss)", result, device, address);
1557                 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
1558                         param);
1559                 g_free(device);
1560                 g_free(address);
1561         }
1562 }
1563
1564 void _bt_handle_network_client_event(GVariant *msg,
1565                                 const char *path)
1566 {
1567         BT_DBG("+");
1568
1569         int result = BLUETOOTH_ERROR_NONE;
1570         gboolean property_flag = FALSE;
1571         char *property = NULL;
1572         GVariant *val = NULL;
1573         GVariantIter value_iter;
1574         GVariant *param = NULL;
1575         g_variant_iter_init(&value_iter, msg);
1576         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1577                 if (strcasecmp(property, "Connected") == 0) {
1578                         int event = BLUETOOTH_EVENT_NONE;
1579                         char *address;
1580
1581                         g_variant_get(val, "b", &property_flag);
1582                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1583
1584                         _bt_convert_device_path_to_address(path, address);
1585
1586                         BT_DBG("property_flag %d", property_flag);
1587                         if (property_flag == TRUE) {
1588                                 event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
1589                                 nap_connected_device_count++;
1590                                 __bt_set_device_values(TRUE,
1591                                         VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1592                         } else {
1593                                 event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
1594                                 nap_connected_device_count--;
1595                                 if (nap_connected_device_count == 0)
1596                                         __bt_set_device_values(FALSE,
1597                                                 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
1598                         }
1599                         param = g_variant_new("(is)", result, address);
1600                         _bt_send_event(BT_NETWORK_EVENT, event,
1601                                 param);
1602
1603                         g_free(address);
1604                 }
1605         }
1606         BT_DBG("-");
1607 }
1608
1609 void __bt_gatt_char_property_changed_event(GVariant *msg,
1610                                 const char *path)
1611 {
1612         GVariantIter value_iter;
1613         char *property = NULL;
1614         char * char_handle = NULL;
1615         GVariant *val = NULL;
1616         int result = BLUETOOTH_ERROR_NONE;
1617         GVariant *param = NULL;
1618         g_variant_iter_init(&value_iter, msg);
1619         char_handle = g_strdup(path);
1620         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1621                 BT_INFO("Property %s", property);
1622
1623                 ret_if(property == NULL);
1624
1625                 if (strcasecmp(property, "Notifying") == 0) {
1626                         gboolean property_flag = FALSE;
1627                         g_variant_get(val, "b", &property_flag);
1628                         if (property_flag == TRUE)
1629                                 BT_DBG("notifying is enabled");
1630                         else
1631                                 BT_DBG("notifying is disabled");
1632                 } else if (strcasecmp(property, "ChangedValue") == 0) {
1633                         int len = 0;
1634                         GByteArray *gp_byte_array = NULL;
1635                         BT_INFO("Type '%s'\n", g_variant_get_type_string(val));
1636
1637                         if (val) {
1638                                 gp_byte_array = g_byte_array_new();
1639                                 len = g_variant_get_size(val);
1640                                 BT_DBG("Len = %d", len);
1641                                 g_byte_array_append(gp_byte_array,
1642                                         (const guint8 *) g_variant_get_data(val), len);
1643                                 if (gp_byte_array->len != 0) {
1644                                         GVariant *byte_array = NULL;
1645                                         byte_array = g_variant_new_from_data(
1646                                                                 G_VARIANT_TYPE_BYTESTRING,
1647                                                                 gp_byte_array->data,
1648                                                                 gp_byte_array->len,
1649                                                                 TRUE, NULL, NULL);
1650                                         param = g_variant_new("(is@ay)", result, char_handle,
1651                                                                 byte_array);
1652
1653                                         /* Send event only registered client */
1654                                         _bt_send_char_value_changed_event(param);
1655                                 }
1656                                 g_byte_array_free(gp_byte_array, TRUE);
1657                         }
1658                 }
1659         }
1660         g_free(char_handle);
1661 }
1662
1663 void _bt_handle_gatt_event(GVariant *msg, const char *member, const char *path)
1664 {
1665         ret_if(path == NULL);
1666
1667         if (strcasecmp(member, "GattValueChanged") == 0) {
1668
1669 #if 0 // Debug Only
1670                 /*** Debug only ***/
1671                 GVariant *value = NULL;
1672                 int value_len = 0;
1673                 char *buffer = NULL;
1674
1675                 g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
1676                 value_len = g_variant_get_size(value);
1677                 if (value_len > 0) {
1678                         char buf[8 * 5 + 1] = { 0 };
1679                         int i;
1680                         int to;
1681                         buffer = (char *)g_variant_get_data(value);
1682                         to = value_len > (sizeof(buf) / 5) ? sizeof(buf) / 5 : value_len;
1683
1684                         for (i = 0; i < to; i++)
1685                                 snprintf(&buf[i * 5], 6, "0x%02x ", buffer[i]);
1686                         buf[i * 5] = '\0';
1687                         BT_DBG("GATT Val[%d] %s", value_len, buf);
1688                 }
1689                 g_variant_unref(value);
1690                 /******/
1691 #endif
1692
1693                 /* Send event only registered client */
1694                 _bt_send_char_value_changed_event(msg);
1695         }
1696 }
1697
1698
1699 void _bt_handle_device_event(GVariant *msg, const char *member, const char *path)
1700 {
1701         int event = 0;
1702         int result = BLUETOOTH_ERROR_NONE;
1703         char *address;
1704         char *dev_name = NULL;
1705         const char *property = NULL;
1706         GVariant *param = NULL;
1707         char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1708         ret_if(path == NULL);
1709
1710         if (strcasecmp(member, "PropertyChanged") == 0) {
1711
1712                 g_variant_get(msg, "(s)", &property);
1713
1714                 ret_if(property == NULL);
1715
1716                 if (strcasecmp(property, "GattConnected") == 0) {
1717                         gboolean connected = FALSE;
1718                         char *address;
1719                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1720
1721                         _bt_convert_device_path_to_address(path, address);
1722                         g_variant_get(msg, "(b)", &connected);
1723
1724                         event = connected ? BLUETOOTH_EVENT_GATT_CONNECTED :
1725                                         BLUETOOTH_EVENT_GATT_DISCONNECTED;
1726                         param = g_variant_new("(is)", result, address);
1727                         _bt_send_event(BT_DEVICE_EVENT,
1728                                         event,
1729                                         param);
1730                         g_free(address);
1731                 } else if (strcasecmp(property, "Paired") == 0) {
1732                         gboolean paired = FALSE;
1733                         bt_remote_dev_info_t *remote_dev_info;
1734                         g_variant_get(msg, "(b)", &paired);
1735
1736                         ret_if(paired == FALSE);
1737
1738                         /* BlueZ sends paired signal for each paired device */
1739                         /* during activation, We should ignore this, otherwise*/
1740                         /* application thinks that a new device got paired */
1741                         if (_bt_adapter_get_status() != BT_ACTIVATED) {
1742                                 BT_DBG("BT is not activated, so ignore this");
1743                                 return;
1744                         }
1745
1746                         if (_bt_is_device_creating() == TRUE) {
1747                                 BT_DBG("Try to Pair by me");
1748                                 return;
1749                         }
1750
1751                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1752
1753                         _bt_convert_device_path_to_address(path, address);
1754
1755                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1756                         if (remote_dev_info == NULL) {
1757                                 g_free(address);
1758                                 return;
1759                         }
1760                         GVariant *uuids = NULL;
1761                         GVariantBuilder *builder = NULL;
1762                         int i = 0;
1763                         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1764                         for (i = 0; i < remote_dev_info->uuid_count; i++) {
1765                                 g_variant_builder_add(builder, "s",
1766                                         remote_dev_info->uuids[i]);
1767                         }
1768                         uuids = g_variant_new("as", builder);
1769                         g_variant_builder_unref(builder);
1770                         GVariant *manufacturer_data = NULL;
1771                         manufacturer_data = g_variant_new_from_data(
1772                                                 G_VARIANT_TYPE_BYTESTRING,
1773                                                 remote_dev_info->manufacturer_data,
1774                                                 remote_dev_info->manufacturer_data_len,
1775                                                 TRUE, NULL, NULL);
1776                         param = g_variant_new("(isunsbub@asn@ay)", result,
1777                                                 address,
1778                                                 remote_dev_info->class,
1779                                                 remote_dev_info->rssi,
1780                                                 remote_dev_info->name,
1781                                                 remote_dev_info->paired,
1782                                                 remote_dev_info->connected,
1783                                                 remote_dev_info->trust,
1784                                                 uuids,
1785                                                 remote_dev_info->manufacturer_data_len,
1786                                                 manufacturer_data);
1787                         _bt_send_event(BT_ADAPTER_EVENT,
1788                                 BLUETOOTH_EVENT_BONDING_FINISHED,
1789                                 param);
1790                         _bt_free_device_info(remote_dev_info);
1791                         g_free(address);
1792
1793                 } else if (strcasecmp(property, "UUIDs") == 0) {
1794                         /* Once we get the updated uuid information after
1795                          * reverse service search, update it to application */
1796
1797                         bt_remote_dev_info_t *remote_dev_info;
1798
1799                         ret_if(_bt_is_device_creating() == TRUE);
1800
1801                         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1802
1803                         _bt_convert_device_path_to_address(path, address);
1804
1805                         remote_dev_info = _bt_get_remote_device_info_by_object_path(path);
1806                         if (remote_dev_info == NULL) {
1807                                 g_free(address);
1808                                 return;
1809                         }
1810
1811                         BT_DBG("UUID's count = %d", remote_dev_info->uuid_count);
1812                         if (remote_dev_info->paired && remote_dev_info->uuid_count) {
1813                                 GVariant *uuids = NULL;
1814                                 GVariantBuilder *builder = NULL;
1815                                 int i = 0;
1816                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1817                                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1818                                         g_variant_builder_add(builder, "s",
1819                                                 remote_dev_info->uuids[i]);
1820                                 }
1821                                 uuids = g_variant_new("as", builder);
1822                                 g_variant_builder_unref(builder);
1823                                 GVariant *manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1824                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1825                                                 TRUE, NULL, NULL);
1826
1827                                 param = g_variant_new("(isunsbub@asn@ay)", result,
1828                                                         address, remote_dev_info->class,
1829                                                         remote_dev_info->rssi,
1830                                                         remote_dev_info->name,
1831                                                         remote_dev_info->paired,
1832                                                         remote_dev_info->connected,
1833                                                         remote_dev_info->trust,
1834                                                         uuids,
1835                                                         remote_dev_info->manufacturer_data_len,
1836                                                         manufacture_data);
1837                                 _bt_send_event(BT_ADAPTER_EVENT,
1838                                         BLUETOOTH_EVENT_SERVICE_SEARCHED,
1839                                         param);
1840                         }
1841
1842                         _bt_free_device_info(remote_dev_info);
1843                         g_free(address);
1844                 }
1845         } else if (strcasecmp(member, "DeviceConnected") == 0) {
1846                 unsigned char addr_type = 0;
1847
1848                 g_variant_get(msg, "(y)", &addr_type);
1849
1850                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1851
1852                 _bt_convert_device_path_to_address(path, address);
1853                 dev_name = _bt_get_bonded_device_name(address);
1854
1855                 _bt_convert_addr_string_to_secure_string(secure_address, address);
1856                 BT_INFO("Address : %s Type : %d", secure_address, addr_type);
1857                 BT_ERR_C("### Connected [%s] [%s]", !addr_type ? "BREDR" : "LE",
1858                                 !addr_type ? dev_name : secure_address);
1859                 g_free(dev_name);
1860
1861                 _bt_logging_connection(TRUE, addr_type);
1862                 param = g_variant_new("(isy)", result, address, addr_type);
1863                 /*Send event to application*/
1864                 _bt_send_event(BT_DEVICE_EVENT,
1865                                         BLUETOOTH_EVENT_DEVICE_CONNECTED,
1866                                         param);
1867                 g_free(address);
1868         } else if (strcasecmp(member, "Disconnected") == 0) {
1869                 unsigned char disc_reason = 0;
1870                 unsigned char addr_type = 0;
1871                 char *dev_name = NULL;
1872                 gboolean sending = FALSE;
1873
1874                 g_variant_get(msg, "(yys)", &addr_type, &disc_reason, &dev_name);
1875
1876                 result = disc_reason;
1877
1878                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1879
1880                 _bt_convert_device_path_to_address(path, address);
1881
1882                 /* 0x00 BDADDR_BRDER
1883                       0x01 BDADDR_LE_PUBLIC
1884                       0x02 BDADDR_LE_RANDOM */
1885                 _bt_convert_addr_string_to_secure_string(secure_address, address);
1886                 BT_INFO("Address : %s Type : %d", secure_address, addr_type);
1887                 BT_ERR_C("### Disconnected [%s] [%d : %s] [%s]", !addr_type ? "BREDR" : "LE",
1888                                 disc_reason, _bt_convert_disc_reason_to_string(disc_reason),
1889                                 !addr_type ? dev_name : secure_address);
1890                 g_free(dev_name);
1891
1892                 _bt_headset_set_local_connection(FALSE);
1893                 _bt_logging_connection(FALSE, addr_type);
1894
1895                 if (!addr_type) {
1896 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1897                         {
1898                                 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1899
1900                                 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
1901                                         BT_ERR("vconf_get_int failed");
1902
1903                                 BT_INFO("conn_state[0x%x], adapter_state [%d]",
1904                                                         bt_device_state, _bt_adapter_get_status());
1905
1906                                 if (disc_reason == BLUETOOTH_ERROR_CONNECTION_TIMEOUT) {
1907                                         _bt_audio_start_auto_connect(TRUE);
1908                                 } else if (bt_device_state &
1909                                                         VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED) {
1910                                         BT_INFO("Disconnected due to turning BT off. Skip a address");
1911                                 } else {
1912                                         char *last_connected = NULL;
1913                                         last_connected = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1914                                         if (!g_strcmp0(address, last_connected))
1915                                                 _bt_audio_set_auto_connect_device_addr("");
1916                                         if (last_connected)
1917                                                 free(last_connected);
1918                                 }
1919                         }
1920
1921 #endif
1922                         /*Check for any OPP transfer on the device and cancel
1923                          * the transfer
1924                          */
1925                         _bt_obex_check_pending_transfer(address);
1926                         _bt_opp_client_is_sending(&sending);
1927                         if (sending == TRUE)
1928                                 _bt_opp_client_check_pending_transfer(address);
1929                 }
1930                 param = g_variant_new("(isy)", result, address, addr_type);
1931                 _bt_send_event(BT_DEVICE_EVENT,
1932                                         BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1933                                         param);
1934                 g_free(address);
1935         } else if (strcasecmp(member, "ProfileStateChanged") == 0) {
1936                 int state = 0;
1937                 char *profile_uuid = NULL;
1938                 bt_headset_wait_t *wait_list;
1939                 bluetooth_device_address_t bd_addr;
1940
1941                 g_variant_get(msg, "(si)", &profile_uuid, &state);
1942
1943                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1944
1945                 _bt_convert_device_path_to_address(path, address);
1946                 _bt_convert_addr_string_to_type(bd_addr.addr, address);
1947
1948                 _bt_convert_addr_string_to_secure_string(secure_address, address);
1949                 BT_DBG("Address: %s", secure_address);
1950                 BT_DBG("Profile UUID: %s", profile_uuid);
1951                 BT_DBG("State: %d", state);
1952
1953                 if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0)  &&
1954                         (state == BT_PROFILE_STATE_CONNECTED)) {
1955
1956                         int event = BLUETOOTH_EVENT_AV_CONNECTED;
1957                         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
1958                         bluetooth_device_address_t device_address;
1959                         gboolean connected;
1960                         bt_headset_wait_t *wait_list;
1961                         guint restricted = 0x0;
1962
1963                         __bt_set_device_values(TRUE,
1964                                 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
1965
1966                         __bt_connection_manager_set_state(address, event);
1967
1968                         _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_HFP_HS, &restricted);
1969
1970                         if (_bt_headset_get_local_connection() == FALSE) {
1971                                 if (restricted == 0x0) /* not restricted*/
1972                                         _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
1973                         } else {
1974                                 /* Connection Started from local device therefore no need to
1975                                  * intiate connection for pending profile */
1976                                 _bt_headset_set_local_connection(FALSE);
1977                         }
1978                         param = g_variant_new("(is)", result, address);
1979                         _bt_send_event(BT_HEADSET_EVENT, event,
1980                                 param);
1981                         connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
1982                                                 connected_address);
1983                         if (connected) {
1984                                 if (g_strcmp0(connected_address, address) != 0) {
1985                                         _bt_convert_addr_string_to_type(
1986                                                 device_address.addr,
1987                                                 connected_address);
1988                                         _bt_audio_disconnect(0, BT_AUDIO_A2DP,
1989                                                 &device_address, NULL);
1990                                 }
1991                         }
1992
1993                         _bt_add_headset_to_list(BT_AUDIO_A2DP,
1994                                                 BT_STATE_CONNECTED, address);
1995
1996                         wait_list = _bt_get_audio_wait_data();
1997                         if (wait_list != NULL &&
1998                                 (g_strcmp0(wait_list->address, address) == 0))
1999                                 _bt_rel_wait_data();
2000
2001                 } else if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0)  &&
2002                         (state == BT_PROFILE_STATE_DISCONNECTED)) {
2003
2004                         int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
2005
2006                         if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
2007                                 g_free(address);
2008                                 g_free(profile_uuid);
2009                                 return;
2010                         }
2011
2012                         __bt_set_device_values(FALSE,
2013                                 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2014
2015                         __bt_connection_manager_set_state(address, event);
2016                         param = g_variant_new("(is)", result, address);
2017                         _bt_send_event(BT_HEADSET_EVENT, event,
2018                                 param);
2019                         /* Remove data from the connected list */
2020                         _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2021                         wait_list = _bt_get_audio_wait_data();
2022
2023                         if (wait_list == NULL) {
2024                                 g_free(address);
2025                                 g_free(profile_uuid);
2026                                 return;
2027                         }
2028
2029                         if (((wait_list->type == BT_AUDIO_ALL) &&
2030                                 (wait_list->ag_flag == TRUE)) ||
2031                                 (wait_list->type == BT_AUDIO_A2DP) ||
2032                                 (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2033                                 bluetooth_device_address_t device_address;
2034                                 _bt_convert_addr_string_to_type(
2035                                                         device_address.addr,
2036                                                         wait_list->address);
2037
2038                                 _bt_audio_connect(wait_list->req_id,
2039                                                         wait_list->type,
2040                                                         &device_address,
2041                                                         NULL);
2042                         }
2043                 } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
2044
2045                         if (state == BT_PROFILE_STATE_CONNECTED) {
2046                                 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
2047                                 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2048                                 bluetooth_device_address_t device_address;
2049                                 gboolean connected;
2050                                 param = g_variant_new("(is)", result, address);
2051                                 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
2052                                         param);
2053                                 connected = _bt_is_headset_type_connected(
2054                                                         BT_AVRCP,
2055                                                         connected_address);
2056                                 if (connected) {
2057                                         if (g_strcmp0(connected_address,
2058                                                                 address) != 0) {
2059                                                 _bt_convert_addr_string_to_type(
2060                                                         device_address.addr,
2061                                                         connected_address);
2062                                                 _bt_audio_disconnect(0,
2063                                                         BT_AVRCP,
2064                                                         &device_address, NULL);
2065                                         }
2066                                 }
2067                                 BT_DBG("device Path: %s", path);
2068                                 _bt_add_headset_to_list(BT_AVRCP,
2069                                                 BT_STATE_CONNECTED, address);
2070                         } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2071                                 int event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
2072                                 param = g_variant_new("(is)", result, address);
2073                                 _bt_send_event(BT_AVRCP_CONTROL_EVENT, event,
2074                                         param);
2075                                 /* Remove data from the connected list */
2076                                 _bt_remove_headset_from_list(BT_AVRCP, address);
2077                                 }
2078                 } else if (strcasecmp(profile_uuid, A2DP_SOURCE_UUID) == 0) {
2079                         if (state == BT_PROFILE_STATE_CONNECTED) {
2080                                 int event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED;
2081                                 BT_INFO("A2DP Source is connected");
2082 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
2083                                 __bt_set_device_values(TRUE,
2084                                                 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED);
2085 #endif
2086
2087 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
2088                                 _bt_audio_set_auto_connect_device_addr(address);
2089                                 _bt_audio_stop_auto_connect();
2090 #endif
2091                                 _bt_add_headset_to_list(BT_AUDIO_A2DP_SOURCE,
2092                                                 BT_STATE_CONNECTED, address);
2093                                 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
2094                                         g_variant_new("(is)", result, address));
2095                         } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2096                                 int event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED;
2097                                 BT_INFO("A2DP Source Disconnected");
2098 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
2099                                 __bt_set_device_values(FALSE,
2100                                                 VCONFKEY_BT_DEVICE_A2DP_SOURCE_CONNECTED);
2101 #endif
2102                                 _bt_remove_headset_from_list(BT_AUDIO_A2DP_SOURCE, address);
2103                                 _bt_send_event(BT_A2DP_SOURCE_EVENT, event,
2104                                                 g_variant_new("(is)", result, address));
2105
2106                                 wait_list = _bt_get_audio_wait_data();
2107                                 if (wait_list && wait_list->type == BT_AUDIO_A2DP_SOURCE) {
2108                                         bluetooth_device_address_t device_address;
2109                                         _bt_convert_addr_string_to_type(
2110                                                         device_address.addr,
2111                                                         wait_list->address);
2112
2113                                         _bt_audio_connect(wait_list->req_id,
2114                                                         wait_list->type,
2115                                                         &device_address,
2116                                                         NULL);
2117                                         /* Now free the wait list */
2118                                         _bt_rel_wait_data();
2119                                 }
2120                         }
2121                 } else if ((strcmp(profile_uuid, HID_UUID) == 0) &&
2122                         ((state == BT_PROFILE_STATE_CONNECTED) ||
2123                                 (state == BT_PROFILE_STATE_DISCONNECTED))) {
2124                         int event;
2125                         if (state == BT_PROFILE_STATE_CONNECTED)
2126                                 event = BLUETOOTH_HID_CONNECTED;
2127                         else
2128                                 event = BLUETOOTH_HID_DISCONNECTED;
2129                         param = g_variant_new("(is)", result, address);
2130                         _bt_send_event(BT_HID_EVENT, event,
2131                                 param);
2132                 } else if (strcmp(profile_uuid, HID_DEVICE_UUID) == 0) {
2133                         if (state == BT_PROFILE_STATE_CONNECTED) {
2134                                 int event;
2135                                 event = BLUETOOTH_HID_DEVICE_CONNECTED;
2136                                 param = g_variant_new("(is)", result, address);
2137                                 _bt_send_event(BT_HID_DEVICE_EVENT, event,
2138                                         param);
2139                         } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
2140                                 event = BLUETOOTH_HID_DEVICE_DISCONNECTED;
2141                                 param = g_variant_new("(is)", result, address);
2142                                 _bt_send_event(BT_HID_DEVICE_EVENT, event,
2143                                         param);
2144                         }
2145                 }
2146                 g_free(address);
2147                 g_free(profile_uuid);
2148         } else if (strcasecmp(member, "AdvReport") == 0) {
2149
2150                 bt_remote_le_dev_info_t *le_dev_info = NULL;
2151                 char *buffer = NULL;
2152                 int buffer_len = 0;
2153                 bt_le_adv_info_t *adv_info = NULL;
2154                 GVariant *value = NULL;
2155                 ret_if(_bt_is_le_scanning() == FALSE);
2156
2157                 le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
2158
2159                 g_variant_get(msg, "(syyii@ay)", &le_dev_info->address,
2160                                                 &le_dev_info->addr_type,
2161                                                 &le_dev_info->adv_type,
2162                                                 &le_dev_info->rssi,
2163                                                 &le_dev_info->adv_data_len,
2164                                                 &value);
2165
2166                 ret_if(value == NULL);
2167
2168                 _bt_convert_device_path_to_address(path, le_dev_info->address);
2169
2170                 buffer_len = g_variant_get_size(value);
2171                 if (buffer_len > 0)
2172                         buffer = (char *)g_variant_get_data(value);
2173
2174                 le_dev_info->adv_data = g_memdup(buffer, buffer_len);
2175                 if (le_dev_info->adv_data == NULL &&
2176                         le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {
2177                         _bt_free_le_device_info(le_dev_info);
2178                         g_variant_unref(value);
2179                         return;
2180                 }
2181
2182                 if (_bt_get_le_scan_type() == BT_LE_PASSIVE_SCAN) {
2183                         _bt_send_scan_result_event(le_dev_info, NULL);
2184                         _bt_free_le_device_info(le_dev_info);
2185                         g_variant_unref(value);
2186                         return;
2187                 }
2188
2189                 if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {       /* ADV_IND */
2190                         adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
2191                         adv_info->addr = g_strdup(le_dev_info->address);
2192                         adv_info->addr_type = le_dev_info->addr_type;
2193                         adv_info->rssi = le_dev_info->rssi;
2194                         adv_info->data_len = le_dev_info->adv_data_len;
2195                         adv_info->data = g_malloc0(le_dev_info->adv_data_len);
2196                         memcpy(adv_info->data, le_dev_info->adv_data,
2197                                         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                         if (TIZEN_FEATURE_BT_USB_DONGLE) {
2663                                 BT_DBG("Enable Adapter");
2664                                 _bt_enable_adapter();
2665                         } else {
2666                                 _bt_handle_adapter_added();
2667                         }
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                 if (TIZEN_FEATURE_BT_USB_DONGLE) {
2762                         BT_DBG("InterfacesRemoved");
2763                         _bt_handle_adapter_removed();
2764                 }
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         if (!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         }
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