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