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