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