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