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