Fix the function and variable name for IPSP
[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         }
1990
1991 }
1992
1993 void __bt_set_audio_values(gboolean connected, char *address)
1994 {
1995         char *name = NULL;
1996         int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1997
1998         /*  Set the headset name */
1999         if (connected == TRUE) {
2000                 name = _bt_get_bonded_device_name(address);
2001         } else {
2002                 name = g_strdup("");
2003         }
2004
2005         if (vconf_set_str(VCONFKEY_BT_HEADSET_NAME,
2006                                         name) != 0) {
2007                 BT_ERR("vconf_set_str failed");
2008         }
2009
2010         g_free(name);
2011
2012         /*  Set the headset state */
2013         if (vconf_get_int(VCONFKEY_BT_DEVICE,
2014                                 &bt_device_state) != 0) {
2015                 BT_ERR("vconf_get_str failed");
2016         }
2017
2018 #ifdef TIZEN_SUPPORT_DUAL_HF
2019         if ((connected == TRUE) &&
2020                 (FALSE == __bt_is_companion_device(address))) {
2021                 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2022         } else if ((bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) &&
2023                         (FALSE == __bt_is_companion_device(address))) {
2024                 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2025         }
2026 #else
2027         if (connected == TRUE) {
2028                 bt_device_state |= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2029         } else if (bt_device_state & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED) {
2030                 bt_device_state ^= VCONFKEY_BT_DEVICE_HEADSET_CONNECTED;
2031         }
2032 #endif
2033
2034         if (vconf_set_int(VCONFKEY_BT_DEVICE,
2035                                 bt_device_state) != 0) {
2036                 BT_ERR("vconf_set_int failed");
2037         }
2038 }
2039
2040 void _bt_handle_headset_event(GVariant *msg, const char *path)
2041 {
2042         int result = BLUETOOTH_ERROR_NONE;
2043         gboolean property_flag = FALSE;
2044         char *property = NULL;
2045         GVariant *value = NULL;
2046         GVariant *param = NULL;
2047         g_variant_get(msg, "(sv)", &property, &value);
2048
2049         ret_if(property == NULL);
2050
2051         BT_DBG("Property = %s \n", property);
2052         /* We allow only 1 headset connection (HSP or HFP)*/
2053         if (strcasecmp(property, "Connected") == 0) {
2054                 int event = BLUETOOTH_EVENT_NONE;
2055                 bt_headset_wait_t *wait_list;
2056                 char *address;
2057                 g_variant_get(value, "b", &property_flag);
2058
2059                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2060
2061                 /* Fix : NULL_RETURNS */
2062                 if (address == NULL)
2063                         return;
2064
2065                 _bt_convert_device_path_to_address(path, address);
2066
2067                 if (property_flag == TRUE) {
2068                         event = BLUETOOTH_EVENT_AG_CONNECTED;
2069                         if (_bt_headset_get_local_connection() == FALSE)
2070                                 _bt_start_timer_for_connection(address, BT_AUDIO_A2DP);
2071                         else
2072                                 _bt_headset_set_local_connection(FALSE);
2073                 } else {
2074                         int previous_state;
2075
2076                         event = BLUETOOTH_EVENT_AG_DISCONNECTED;
2077
2078                         previous_state = _bt_get_device_state_from_list(BT_AUDIO_HSP, address);
2079                         if (previous_state == BT_STATE_DISCONNECTING)
2080                                 _bt_send_hf_local_term_event(address);
2081                 }
2082                 /* Set the State machine here */
2083                 __bt_connection_manager_set_state(address, event);
2084                 __bt_set_audio_values(property_flag, address);
2085                 param = g_variant_new("(is)", result, address);
2086                 _bt_send_event(BT_HEADSET_EVENT, event,
2087                         param);
2088
2089                 if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
2090                         /* Remove data from the connected list */
2091                         _bt_remove_headset_from_list(BT_AUDIO_HSP, address);
2092
2093                         wait_list = _bt_get_audio_wait_data();
2094                         if (wait_list == NULL) {
2095                                 g_free(address);
2096                                 return;
2097                         }
2098
2099                         bluetooth_device_address_t device_address;
2100
2101                         _bt_set_audio_wait_data_flag(TRUE);
2102
2103                         _bt_convert_addr_string_to_type(device_address.addr,
2104                                                         wait_list->address);
2105                         _bt_audio_connect(wait_list->req_id, wait_list->type,
2106                                         &device_address, wait_list->out_param1);
2107                         _bt_rel_wait_data();
2108                 } else if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
2109                         /* Add data to the connected list */
2110                         _bt_add_headset_to_list(BT_AUDIO_HSP,
2111                                                 BT_STATE_CONNECTED, address);
2112
2113                         wait_list = _bt_get_audio_wait_data();
2114                         if (wait_list != NULL &&
2115                                 (g_strcmp0(wait_list->address, address) == 0))
2116                         _bt_rel_wait_data();
2117
2118                         BT_INFO("Check A2DP pending connect");
2119                         _bt_audio_check_pending_connect();
2120                 }
2121                 g_free(address);
2122         } else if (strcasecmp(property, "State") == 0) {
2123                 char *state = NULL;
2124
2125                 g_variant_get(value, "s", &state);
2126
2127                 /* This code assumes we support only 1 headset connection */
2128                 /* Need to use the headset list, if we support multi-headsets */
2129                 if (strcasecmp(state, "Playing") == 0) {
2130                         BT_DBG("Playing: Sco Connected");
2131                 } else if (strcasecmp(state, "connected") == 0 ||
2132                                 strcasecmp(state, "disconnected") == 0) {
2133                         BT_DBG("connected/disconnected: Sco Disconnected");
2134                 } else {
2135                         BT_ERR("Not handled state - %s", state);
2136                         g_free(state);
2137                         return;
2138                 }
2139                 g_free(state);
2140         } else if (strcasecmp(property, "SpeakerGain") == 0) {
2141                 guint16 spkr_gain;
2142                 char *address;
2143
2144                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2145
2146                 _bt_convert_device_path_to_address(path, address);
2147
2148                 spkr_gain = g_variant_get_uint16(value);
2149
2150                 BT_DBG("spkr_gain: %d", spkr_gain);
2151
2152                 param = g_variant_new("(i&sq)", result, address, spkr_gain);
2153                 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
2154                         param);
2155
2156                 g_free(address);
2157         } else if (strcasecmp(property, "MicrophoneGain") == 0) {
2158                 guint16 mic_gain;
2159                 char *address;
2160
2161                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2162
2163                 _bt_convert_device_path_to_address(path, address);
2164
2165                 mic_gain = g_variant_get_uint16(value);
2166
2167                 param = g_variant_new("(i&sq)", result, address, mic_gain);
2168                 _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AG_MIC_GAIN,
2169                         param);
2170                 g_free(address);
2171         }
2172
2173         if (property)
2174                 g_free(property);
2175         g_variant_unref(value);
2176  }
2177
2178 void _bt_handle_sink_event(GVariant *msg, const char *path)
2179 {
2180         GVariantIter value_iter;
2181         char *property = NULL;
2182
2183         bt_headset_wait_t *wait_list;
2184
2185         GVariant *child = NULL;
2186         GVariant *val = NULL;
2187         GVariant *param = NULL;
2188         g_variant_iter_init(&value_iter, msg);
2189         while ((child = g_variant_iter_next_value(&value_iter))) {
2190
2191                 g_variant_get(child, "{sv}", &property, &val);
2192
2193                 ret_if(property == NULL);
2194
2195                 BT_DBG("Property = %s \n", property);
2196
2197
2198                 if (strcasecmp(property, "State") == 0) {
2199                         int result = BLUETOOTH_ERROR_NONE;
2200                         char *value;
2201
2202                         g_variant_get(val, "s", &value);
2203                         BT_DBG("value: %s", value);
2204
2205                         if (g_strcmp0(value, "disconnected") == 0) {
2206                                 char *address;
2207
2208                                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2209
2210                                 _bt_convert_device_path_to_address(path, address);
2211
2212                                 __bt_set_device_values(FALSE,
2213                                         VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2214                                 param = g_variant_new("(is)", result, address);
2215                                 _bt_send_event(BT_HEADSET_EVENT,
2216                                         BLUETOOTH_EVENT_AV_DISCONNECTED,
2217                                         param);
2218
2219                                 /* Remove data from the connected list */
2220                                 _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
2221                                 wait_list = _bt_get_audio_wait_data();
2222                                 if (wait_list == NULL) {
2223                                         g_free(value);
2224                                         g_free(property);
2225                                         g_variant_unref(val);
2226                                         g_variant_unref(child);
2227                                         g_free(address);
2228                                         return;
2229                                 }
2230
2231                                 if (((wait_list->type == BT_AUDIO_ALL) &&
2232                                         (wait_list->ag_flag == TRUE)) ||
2233                                         (wait_list->type == BT_AUDIO_A2DP) ||
2234                                         (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
2235                                         bluetooth_device_address_t device_address;
2236                                         _bt_convert_addr_string_to_type(
2237                                                                 device_address.addr,
2238                                                                 wait_list->address);
2239
2240                                         _bt_audio_connect(wait_list->req_id,
2241                                                                 wait_list->type,
2242                                                                 &device_address,
2243                                                                 wait_list->out_param1);
2244                                 }
2245                                 g_free(address);
2246                         } else if (strcasecmp(value, "Connected") == 0) {
2247                                 char *address;
2248                                 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
2249                                 bluetooth_device_address_t device_address;
2250                                 gboolean connected;
2251
2252                                 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2253
2254                                 _bt_convert_device_path_to_address(path, address);
2255
2256                                 __bt_set_device_values(TRUE,
2257                                                 VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
2258                                 param = g_variant_new("(is)", result, address);
2259                                 _bt_send_event(BT_HEADSET_EVENT,
2260                                         BLUETOOTH_EVENT_AV_CONNECTED,
2261                                         param);
2262                                 /* Check for existing Media device to disconnect */
2263                                 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
2264                                                                         connected_address);
2265                                 if (connected) {
2266                                         /* Match connected device address */
2267                                         if (g_strcmp0(connected_address, address) != 0) {
2268                                                 /* Convert BD adress from string type */
2269                                                 _bt_convert_addr_string_to_type(
2270                                                                 device_address.addr,
2271                                                                 connected_address);
2272                                                 _bt_audio_disconnect(0, BT_AUDIO_A2DP,
2273                                                                 &device_address, NULL);
2274                                         }
2275                                 }
2276
2277                                 /* Add data to the connected list */
2278                                 _bt_add_headset_to_list(BT_AUDIO_A2DP,
2279                                                 BT_STATE_CONNECTED, address);
2280
2281                                 g_free(address);
2282                         }
2283                         g_free(value);
2284                 }
2285                 g_free(property);
2286                 g_variant_unref(val);
2287                 g_variant_unref(child);
2288         }
2289 }
2290
2291 void _bt_handle_agent_event(GVariant *msg, const char *member)
2292 {
2293         int result = BLUETOOTH_ERROR_NONE;
2294         char *address = NULL;
2295         char *name = NULL;
2296         char *uuid = NULL;
2297         GVariant *param = NULL;
2298         ret_if(member == NULL);
2299
2300         if (strcasecmp(member, "ObexAuthorize") == 0) {
2301                 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2302                 param = g_variant_new("(iss)", result, address, name);
2303                 _bt_send_event(BT_OPP_SERVER_EVENT,
2304                         BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
2305                         param);
2306                 g_free(address);
2307                 g_free(name);
2308         } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
2309                 bt_rfcomm_server_info_t *server_info;
2310
2311                 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
2312
2313                 server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
2314                 ret_if(server_info == NULL);
2315                 ret_if(server_info->server_type != BT_CUSTOM_SERVER);
2316                 param = g_variant_new("(isssn)", result, address, uuid, name,
2317                                         server_info->control_fd);
2318                 _bt_send_event(BT_RFCOMM_SERVER_EVENT,
2319                         BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
2320                         param);
2321                 g_free(address);
2322                 g_free(uuid);
2323                 g_free(name);
2324         }
2325 }
2326
2327 static int __bt_get_object_path(GVariant *msg, char **path)
2328 {
2329         g_variant_get(msg, "(o*)", path, NULL);
2330         if (*path == NULL)
2331                 return BLUETOOTH_ERROR_INTERNAL;
2332
2333         return BLUETOOTH_ERROR_NONE;
2334 }
2335
2336 static void __bt_devices_list_free(void)
2337 {
2338         bt_cache_info_t *cache_info;
2339         GList *node;
2340
2341         node = g_list_first(p_cache_list);
2342
2343         while (node != NULL) {
2344                 cache_info = (bt_cache_info_t *)node->data;
2345                 p_cache_list = g_list_remove(p_cache_list, cache_info);
2346                 __bt_free_cache_info(cache_info);
2347
2348                 node = g_list_next(node);
2349         }
2350 }
2351
2352 static int __bt_parse_event(GVariant *msg)
2353 {
2354         GVariantIter iter;
2355         GVariant *child;
2356         char *interface_name = NULL;
2357         GVariant *inner_iter = NULL;
2358
2359         g_variant_iter_init(&iter, msg);
2360
2361         while ((child = g_variant_iter_next_value(&iter))) {
2362                 g_variant_get(child, "{&s@a{sv}}", &interface_name, &inner_iter);
2363                 if (g_strcmp0(interface_name,
2364                                 BT_DEVICE_INTERFACE) == 0) {
2365                         g_variant_unref(inner_iter);
2366                         g_variant_unref(child);
2367                         return BT_DEVICE_EVENT;
2368                 } else if (g_strcmp0(interface_name,
2369                                 BT_MEDIATRANSPORT_INTERFACE) == 0) {
2370                         g_variant_unref(inner_iter);
2371                         g_variant_unref(child);
2372                         return BT_MEDIA_TRANSFER_EVENT;
2373                 } else if (g_strcmp0(interface_name,
2374                                 BT_PLAYER_CONTROL_INTERFACE) == 0) {
2375                         g_variant_unref(inner_iter);
2376                         g_variant_unref(child);
2377                         return BT_AVRCP_CONTROL_EVENT;
2378                 }
2379                 g_variant_unref(inner_iter);
2380                 g_variant_unref(child);
2381         }
2382
2383         return 0;
2384 }
2385
2386 static  void __bt_manager_event_filter(GDBusConnection *connection,
2387                                         const gchar *sender_name,
2388                                         const gchar *object_path,
2389                                         const gchar *interface_name,
2390                                         const gchar *signal_name,
2391                                         GVariant *parameters,
2392                                         gpointer user_data)
2393 {
2394         bt_event_type_t bt_event = 0x00;
2395         int result = BLUETOOTH_ERROR_NONE;
2396         GVariant *value;
2397         char *obj_path = NULL;
2398         GVariant *param = NULL;
2399         if (signal_name == NULL)
2400                 return;
2401         if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2402                 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
2403
2404                 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
2405 #ifdef USB_BLUETOOTH
2406                         BT_DBG("Enable Adapter");
2407                         _bt_enable_adapter();
2408 #else
2409                         _bt_handle_adapter_added();
2410 #endif
2411                 } else {
2412                         bt_event = __bt_parse_event(value);
2413                         if (bt_event == BT_DEVICE_EVENT) {
2414                                 bt_cache_info_t *cache_info;
2415                                 bt_remote_dev_info_t *dev_info;
2416
2417                                 ret_if(_bt_is_discovering() == FALSE &&
2418                                                 _bt_is_le_scanning() == FALSE);
2419
2420                                 cache_info = g_malloc0(sizeof(bt_cache_info_t));
2421                                 ret_if(cache_info == NULL);
2422
2423                                 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
2424                                 if (dev_info == NULL) {
2425                                         __bt_free_cache_info(cache_info);
2426                                         return;
2427                                 }
2428
2429                                 cache_info->dev_info = dev_info;
2430
2431                                 if (__bt_parse_interface(parameters, dev_info) == FALSE) {
2432                                         BT_ERR("Fail to parse the properies");
2433                                         __bt_free_cache_info(cache_info);
2434                                         g_variant_unref(value);
2435                                         return;
2436                                 }
2437
2438                                 if (dev_info->addr_type != BDADDR_BREDR) {
2439                                         /* Whenever emit the property changed from bluez,
2440                                                 some property doesn't reach to bt-service.
2441                                                 So LE device is handled as AdvReport signal */
2442                                         __bt_free_cache_info(cache_info);
2443                                         g_variant_unref(value);
2444                                         return;
2445                                 }
2446
2447                                 if (dev_info->name == NULL)
2448                                         /* If Remote device name is NULL or still RNR is not done
2449                                          * then display address as name.
2450                                          */
2451                                         dev_info->name = g_strdup(dev_info->address);
2452
2453 #ifdef TIZEN_DPM_ENABLE
2454                                 if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
2455                                                         DPM_RESTRICTED) {
2456                                         bluetooth_device_class_t device_class;
2457                                         _bt_divide_device_class(&device_class, dev_info->class);
2458                                         BT_DBG("[%s]device_class.major_class : %d", dev_info->name, device_class.major_class);
2459
2460                                         if (device_class.major_class ==
2461                                                 BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
2462                                                 __bt_free_cache_info(cache_info);
2463                                                 g_variant_unref(value);
2464                                                 return;
2465                                         }
2466                                 }
2467 #endif
2468
2469                                 GVariant *uuids = NULL;
2470                                 GVariantBuilder *builder = NULL;
2471                                 int i = 0;
2472                                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2473                                 for (i = 0; i < dev_info->uuid_count; i++) {
2474                                         g_variant_builder_add(builder, "s",
2475                                                 dev_info->uuids[i]);
2476                                 }
2477                                 uuids = g_variant_new("as", builder);
2478                                 g_variant_builder_unref(builder);
2479                                 GVariant *manufacturer_data = NULL;
2480                                 manufacturer_data = g_variant_new_from_data(
2481                                                         G_VARIANT_TYPE_BYTESTRING,
2482                                                         dev_info->manufacturer_data,
2483                                                         dev_info->manufacturer_data_len,
2484                                                         TRUE, NULL, NULL);
2485                                 param = g_variant_new("(isunsbub@asn@ay)", result,
2486                                                         dev_info->address,
2487                                                         dev_info->class,
2488                                                         dev_info->rssi,
2489                                                         dev_info->name,
2490                                                         dev_info->paired,
2491                                                         dev_info->connected,
2492                                                         dev_info->trust,
2493                                                         uuids,
2494                                                         dev_info->manufacturer_data_len,
2495                                                         manufacturer_data);
2496                                 _bt_send_event(BT_ADAPTER_EVENT,
2497                                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
2498                                          param);
2499                                 p_cache_list = g_list_append(p_cache_list, cache_info);
2500                         } else if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2501                                 BT_DBG("Device path : %s ", obj_path);
2502                                 _bt_set_control_device_path(obj_path);
2503                         }
2504                 }
2505                 g_variant_unref(value);
2506         } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
2507 #ifdef USB_BLUETOOTH
2508                 BT_DBG("InterfacesRemoved");
2509                 _bt_handle_adapter_removed();
2510 #endif
2511                 if (g_strcmp0(interface_name, BT_MEDIATRANSPORT_INTERFACE) == 0) {
2512                         bt_event = BT_MEDIA_TRANSFER_EVENT;
2513                 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2514                         bt_event = BT_DEVICE_EVENT;
2515                 } else if (g_strcmp0(interface_name, BT_PLAYER_CONTROL_INTERFACE) == 0) {
2516                         bt_event = BT_AVRCP_CONTROL_EVENT;
2517                 }
2518                 if ((bt_event != 0) && (bt_event != BT_MEDIA_TRANSFER_EVENT)) {
2519                         _bt_handle_adapter_event(parameters, signal_name);
2520                         if (bt_event == BT_AVRCP_CONTROL_EVENT) {
2521                                 BT_INFO("Object Path %s", obj_path);
2522                                 _bt_remove_control_device_path(obj_path);
2523                         }
2524                 }
2525         } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
2526                 gboolean value;
2527                 char *name = NULL;
2528                 char *previous = NULL;
2529                 char *current = NULL;
2530
2531                 if (__bt_get_owner_info(parameters, &name, &previous, &current)) {
2532                         BT_ERR("Fail to get the owner info");
2533                         return;
2534                 }
2535
2536                 if (*current != '\0') {
2537                         g_free(current);
2538                         if (name)
2539                                 g_free(name);
2540                         if (previous)
2541                                 g_free(previous);
2542                         return;
2543                 }
2544
2545                 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
2546                         BT_DBG("Bluetoothd is terminated");
2547                         if (_bt_adapter_get_status() == BT_ACTIVATED)
2548                                  __bt_disable_cb();
2549
2550                         _bt_handle_adapter_removed();
2551                         __bt_devices_list_free();
2552                 }
2553
2554                 _bt_obex_server_check_allocation(&value);
2555
2556                 if (value == TRUE) {
2557                         /* Check if the obex server was terminated abnormally */
2558                         _bt_obex_server_check_termination(name);
2559                 }
2560
2561                 _bt_rfcomm_server_check_existence(&value);
2562
2563                 if (value == TRUE) {
2564                         /* The obex server was terminated abnormally */
2565                         _bt_rfcomm_server_check_termination(name);
2566                 }
2567
2568                 /* Stop advertising started by terminated process */
2569                 _bt_stop_advertising_by_terminated_process(name);
2570                 /* Stop LE Scan */
2571                 _bt_stop_le_scan(name);
2572                 g_free(name);
2573                 g_free(previous);
2574                 g_free(current);
2575         } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
2576                 const char *path = object_path;
2577
2578                 if (strncmp(path, BT_MEDIA_OBJECT_PATH,
2579                                 strlen(BT_MEDIA_OBJECT_PATH)) == 0)
2580                         return;
2581
2582                 _bt_handle_property_changed_event(parameters, object_path);
2583         } else if (g_strcmp0(interface_name, BT_ADAPTER_INTERFACE) == 0) {
2584                 _bt_handle_adapter_event(parameters, signal_name);
2585         } else if (g_strcmp0(interface_name, BT_INPUT_INTERFACE) == 0) {
2586                 _bt_handle_input_event(parameters, object_path);
2587         } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
2588                 _bt_handle_network_server_event(parameters, signal_name);
2589         } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
2590                 _bt_handle_headset_event(parameters, object_path);
2591         } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
2592                 _bt_handle_sink_event(parameters, object_path);
2593         } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
2594                 _bt_handle_agent_event(parameters, signal_name);
2595         } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2596                 _bt_handle_device_event(parameters, signal_name, object_path);
2597         } else if (g_strcmp0(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
2598                 _bt_handle_gatt_event(parameters, signal_name, object_path);
2599         }
2600
2601         return;
2602 }
2603
2604 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
2605 {
2606
2607         if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2608                 char *interface_name = NULL;
2609
2610                 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2611                 retv_if(interface_name == NULL, FALSE);
2612
2613                 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
2614                         BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
2615                         return TRUE;
2616                 }
2617         }
2618
2619         return FALSE;
2620 }
2621
2622 static  void __bt_obexd_event_filter(GDBusConnection *connection,
2623                                         const gchar *sender_name,
2624                                         const gchar *object_path,
2625                                         const gchar *interface_name,
2626                                         const gchar *signal_name,
2627                                         GVariant *parameters,
2628                                         gpointer user_data)
2629 {
2630         const char *member = signal_name;
2631         char *obj_path = NULL;
2632         ret_if(member == NULL);
2633
2634         if (strcasecmp(member, "InterfacesAdded") == 0) {
2635                 if (__bt_get_object_path(parameters, &obj_path)) {
2636                         BT_ERR("Fail to get the path");
2637                         return;
2638                 }
2639                 BT_INFO("object_path = [%s]", object_path);
2640
2641                 /*Handle OPP_SERVER_CONNECTED_EVENT here */
2642                 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2643                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2644                         g_free(obj_path);
2645                         return;
2646                 }
2647
2648                 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2649                         BT_DBG("Obex_Server_Session_Transfer connected");
2650                         _bt_obex_transfer_connected();
2651                 }
2652                 g_free(obj_path);
2653         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2654                 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
2655                 if (__bt_get_object_path(parameters, &obj_path)) {
2656                         BT_ERR("Fail to get the path");
2657                         return;
2658                 }
2659                 BT_INFO("object_path = [%s]", object_path);
2660
2661                 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2662                                 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2663                         BT_DBG("Call PBAP Disconnected");
2664                         _bt_obex_pbap_client_disconnect(obj_path);
2665                 }
2666
2667                 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
2668                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
2669                         g_free(obj_path);
2670                         return;
2671                 }
2672
2673                 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
2674                         BT_DBG("Obex_Server_Session_Transfer disconnected");
2675                         _bt_obex_transfer_disconnected();
2676                 }
2677                 g_free(obj_path);
2678         } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
2679                 const char *path = object_path;
2680
2681                 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
2682                                 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
2683                         strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2684                                 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2685                         BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
2686                         return;
2687                 }
2688
2689                 _bt_handle_property_changed_event(parameters, path);
2690         }
2691         BT_DBG("-");
2692         return;
2693 }
2694
2695 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
2696 {
2697         BT_DBG("+");
2698
2699         if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
2700                 char *interface_name = NULL;
2701
2702                 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
2703
2704                 retv_if(interface_name == NULL, FALSE);
2705
2706                 if (strcasecmp(interface_name,
2707                                         BT_OBEX_TRANSFER_INTERFACE) == 0) {
2708                         BT_DBG("-");
2709                         return TRUE;
2710                 }
2711         }
2712
2713         BT_DBG("-");
2714
2715         return FALSE;
2716 }
2717
2718 static  void __bt_opc_event_filter(GDBusConnection *connection,
2719                                         const gchar *sender_name,
2720                                         const gchar *object_path,
2721                                         const gchar *interface_name,
2722                                         const gchar *signal_name,
2723                                         GVariant *parameters,
2724                                         gpointer user_data)
2725 {
2726         const char *member = signal_name;
2727         char *obj_path = NULL;
2728         if (strcasecmp(member, "InterfacesAdded") == 0) {
2729                 BT_DBG("InterfacesAdded");
2730         } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
2731
2732                 if (__bt_get_object_path(parameters, &obj_path)) {
2733                         BT_ERR("Fail to get the path");
2734                         return;
2735                 }
2736
2737                 BT_DBG("object_path = %s", obj_path);
2738
2739                 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2740                                 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
2741                                 || strstr(obj_path, "transfer") == NULL) {
2742                         g_free(obj_path);
2743                         return;
2744                 } else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
2745                                 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
2746                         BT_DBG("Going to call opc disconnected");
2747                         _bt_opc_disconnected(obj_path);
2748                 }
2749
2750                 _bt_sending_files();
2751                 g_free(obj_path);
2752         } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
2753                 char *path = (char *)object_path;
2754                 BT_INFO("object_path %s", path);
2755                 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
2756                         strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
2757                         BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
2758                         return;
2759                 }
2760
2761                 _bt_opc_property_changed_event(parameters, path);
2762         }
2763
2764         return;
2765 }
2766
2767 int _bt_opp_client_event_init(void)
2768 {
2769         GError *error = NULL;
2770
2771         if (opc_obexd_conn == NULL) {
2772                 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
2773
2774                 if (!opc_obexd_conn) {
2775                         if (error) {
2776                                 BT_ERR("Unable to connect to dbus: %s", error->message);
2777                                 g_clear_error(&error);
2778                         }
2779                 return BLUETOOTH_ERROR_INTERNAL;
2780                 }
2781         }
2782
2783         if (_bt_register_service_event(opc_obexd_conn,
2784                         BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
2785                         g_object_unref(opc_obexd_conn);
2786                         opc_obexd_conn = NULL;
2787                         return BLUETOOTH_ERROR_INTERNAL;
2788         }
2789
2790         return BLUETOOTH_ERROR_NONE;
2791 }
2792
2793 void _bt_opp_client_event_deinit(void)
2794 {
2795         if (opc_obexd_conn) {
2796                 _bt_unregister_service_event(opc_obexd_conn,
2797                                                 BT_OPP_CLIENT_EVENT);
2798                  g_object_unref(opc_obexd_conn);
2799                  opc_obexd_conn = NULL;
2800         }
2801 }
2802
2803 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
2804                 int subscribe)
2805 {
2806         if (conn == NULL)
2807                 return -1;
2808
2809         static int subs_interface_added_id = -1;
2810         static int subs_interface_removed_id = -1;
2811         static int subs_name_owner_id = -1;
2812         static int subs_property_id = -1;
2813         static int subs_adapter_id = -1;
2814         static int subs_gatt_id = -1;
2815
2816         if (subscribe) {
2817                 if (subs_interface_added_id == -1) {
2818                         subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
2819                                 NULL, BT_MANAGER_INTERFACE,
2820                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
2821                                 __bt_manager_event_filter,
2822                                 NULL, NULL);
2823                 }
2824                 if (subs_interface_removed_id == -1) {
2825                         subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
2826                                 NULL, BT_MANAGER_INTERFACE,
2827                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
2828                                 __bt_manager_event_filter,
2829                                 NULL, NULL);
2830                 }
2831                 if (subs_name_owner_id == -1) {
2832                         subs_name_owner_id = g_dbus_connection_signal_subscribe(conn,
2833                                 NULL, BT_FREEDESKTOP_INTERFACE,
2834                                 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
2835                                 __bt_manager_event_filter,
2836                                 NULL, NULL);
2837                 }
2838                 if (subs_property_id == -1) {
2839                         subs_property_id = g_dbus_connection_signal_subscribe(conn,
2840                                 NULL, BT_PROPERTIES_INTERFACE,
2841                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
2842                                 __bt_manager_event_filter,
2843                                 NULL, NULL);
2844                 }
2845                 if (subs_adapter_id == -1) {
2846                         subs_adapter_id = g_dbus_connection_signal_subscribe(conn,
2847                                 NULL, BT_ADAPTER_INTERFACE,
2848                                 NULL, NULL, NULL, 0,
2849                                 __bt_manager_event_filter,
2850                                 NULL, NULL);
2851                 }
2852                 if (subs_gatt_id == -1) {
2853                         subs_gatt_id = g_dbus_connection_signal_subscribe(conn,
2854                                 NULL, BT_GATT_CHAR_INTERFACE,
2855                                 NULL, NULL, NULL, 0,
2856                                 __bt_manager_event_filter,
2857                                 NULL, NULL);
2858                 }
2859         } else {
2860                 if (subs_interface_added_id != -1) {
2861                         g_dbus_connection_signal_unsubscribe(conn,
2862                                         subs_interface_added_id);
2863                         subs_interface_added_id = -1;
2864                 }
2865                 if (subs_interface_removed_id != -1) {
2866                         g_dbus_connection_signal_unsubscribe(conn,
2867                                         subs_interface_removed_id);
2868                         subs_interface_removed_id = -1;
2869                 }
2870                 if (subs_name_owner_id != -1) {
2871                         g_dbus_connection_signal_unsubscribe(conn,
2872                                         subs_name_owner_id);
2873                         subs_name_owner_id = -1;
2874                 }
2875                 if (subs_property_id != -1) {
2876                         g_dbus_connection_signal_unsubscribe(conn,
2877                                         subs_property_id);
2878                         subs_property_id = -1;
2879                 }
2880                 if (subs_adapter_id != -1) {
2881                         g_dbus_connection_signal_unsubscribe(conn, subs_adapter_id);
2882                         subs_adapter_id = -1;
2883                 }
2884                 if (subs_gatt_id != -1) {
2885                         g_dbus_connection_signal_unsubscribe(conn, subs_gatt_id);
2886                         subs_gatt_id = -1;
2887                 }
2888         }
2889         return 0;
2890 }
2891
2892 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
2893                 int subscribe)
2894 {
2895         if (conn == NULL)
2896                 return -1;
2897
2898         static int subs_device_id = -1;
2899
2900         if (subscribe) {
2901                 if (subs_device_id == -1) {
2902                         subs_device_id = g_dbus_connection_signal_subscribe(conn,
2903                                 NULL, BT_DEVICE_INTERFACE,
2904                                 NULL, NULL, NULL, 0,
2905                                 __bt_manager_event_filter,
2906                                 NULL, NULL);
2907                 }
2908         } else {
2909                 if (subs_device_id != -1) {
2910                         g_dbus_connection_signal_unsubscribe(conn,
2911                                         subs_device_id);
2912                         subs_device_id = -1;
2913                 }
2914         }
2915         return 0;
2916 }
2917
2918 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
2919                 int subscribe)
2920 {
2921         if (conn == NULL)
2922                 return -1;
2923
2924         static int subs_input_id = -1;
2925
2926         if (subscribe) {
2927                 if (subs_input_id == -1) {
2928                         subs_input_id = g_dbus_connection_signal_subscribe(conn,
2929                                 NULL, BT_INPUT_INTERFACE,
2930                                 NULL, NULL, NULL, 0,
2931                                 __bt_manager_event_filter,
2932                                 NULL, NULL);
2933                 }
2934         } else {
2935                 if (subs_input_id != -1) {
2936                         g_dbus_connection_signal_unsubscribe(conn,
2937                                         subs_input_id);
2938                         subs_input_id = -1;
2939                 }
2940         }
2941         return 0;
2942 }
2943
2944 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
2945                 int subscribe)
2946 {
2947         if (conn == NULL)
2948                 return -1;
2949
2950         static int subs_serv_id = -1;
2951         static int subs_client_id = -1;
2952
2953         if (subscribe) {
2954                 if (subs_serv_id == -1) {
2955                         subs_serv_id = g_dbus_connection_signal_subscribe(conn,
2956                                 NULL, BT_NETWORK_SERVER_INTERFACE,
2957                                 NULL, NULL, NULL, 0,
2958                                 __bt_manager_event_filter,
2959                                 NULL, NULL);
2960                 }
2961                 if (subs_client_id == -1) {
2962                         subs_client_id = g_dbus_connection_signal_subscribe(conn,
2963                                 NULL, BT_NETWORK_CLIENT_INTERFACE,
2964                                 NULL, NULL, NULL, 0,
2965                                 __bt_manager_event_filter,
2966                                 NULL, NULL);
2967                 }
2968         } else {
2969                 if (subs_serv_id != -1) {
2970                         g_dbus_connection_signal_unsubscribe(conn,
2971                                         subs_serv_id);
2972                         subs_serv_id = -1;
2973                 }
2974                 if (subs_client_id != -1) {
2975                         g_dbus_connection_signal_unsubscribe(conn,
2976                                         subs_client_id);
2977                         subs_client_id = -1;
2978                 }
2979         }
2980         return 0;
2981 }
2982
2983 int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
2984                 int subscribe)
2985 {
2986         if (conn == NULL)
2987                 return -1;
2988
2989         static int subs_headset_id = -1;
2990         static int subs_sink_id = -1;
2991
2992         if (subscribe) {
2993                 if (subs_headset_id == -1) {
2994                         subs_headset_id = g_dbus_connection_signal_subscribe(conn,
2995                                 NULL, BT_HEADSET_INTERFACE,
2996                                 NULL, NULL, NULL, 0,
2997                                 __bt_manager_event_filter,
2998                                 NULL, NULL);
2999                 }
3000                 if (subs_sink_id == -1) {
3001                         subs_sink_id = g_dbus_connection_signal_subscribe(conn,
3002                                 NULL, BT_SINK_INTERFACE,
3003                                 NULL, NULL, NULL, 0,
3004                                 __bt_manager_event_filter,
3005                                 NULL, NULL);
3006                 }
3007         } else {
3008                 if (subs_headset_id != -1) {
3009                         g_dbus_connection_signal_unsubscribe(conn,
3010                                         subs_headset_id);
3011                         subs_headset_id = -1;
3012                 }
3013                 if (subs_sink_id != -1) {
3014                         g_dbus_connection_signal_unsubscribe(conn,
3015                                         subs_sink_id);
3016                         subs_sink_id = -1;
3017                 }
3018         }
3019         return 0;
3020 }
3021
3022 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
3023                 int subscribe)
3024 {
3025         if (conn == NULL)
3026                 return -1;
3027
3028         static int subs_opp_server_interface_added_id = -1;
3029         static int subs_opp_server_interface_removed_id = -1;
3030         static int subs_opp_server_property_id = -1;
3031
3032
3033         if (subscribe) {
3034                 if (subs_opp_server_interface_added_id == -1) {
3035                         subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3036                                 NULL, BT_MANAGER_INTERFACE,
3037                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3038                                 __bt_obexd_event_filter,
3039                                 NULL, NULL);
3040                 }
3041                 if (subs_opp_server_interface_removed_id == -1) {
3042                         subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3043                                 NULL, BT_MANAGER_INTERFACE,
3044                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3045                                 __bt_obexd_event_filter,
3046                                 NULL, NULL);
3047                 }
3048                 if (subs_opp_server_property_id == -1) {
3049                         subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
3050                                 NULL, BT_PROPERTIES_INTERFACE,
3051                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3052                                 __bt_obexd_event_filter,
3053                                 NULL, NULL);
3054                 }
3055         } else {
3056                 if (subs_opp_server_interface_added_id != -1) {
3057                         g_dbus_connection_signal_unsubscribe(conn,
3058                                         subs_opp_server_interface_added_id);
3059                         subs_opp_server_interface_added_id = -1;
3060                 }
3061                 if (subs_opp_server_interface_removed_id != -1) {
3062                         g_dbus_connection_signal_unsubscribe(conn,
3063                                         subs_opp_server_interface_removed_id);
3064                         subs_opp_server_interface_removed_id = -1;
3065                 }
3066                 if (subs_opp_server_property_id != -1) {
3067                         g_dbus_connection_signal_unsubscribe(conn,
3068                                         subs_opp_server_property_id);
3069                         subs_opp_server_property_id = -1;
3070                 }
3071         }
3072         return 0;
3073 }
3074
3075 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
3076                 int subscribe)
3077 {
3078         if (conn == NULL)
3079                 return -1;
3080
3081         static int subs_opp_client_interface_added_id = -1;
3082         static int subs_opp_client_interface_removed_id = -1;
3083         static int subs_opp_client_property_id = -1;
3084
3085
3086         if (subscribe) {
3087                 if (subs_opp_client_interface_added_id == -1) {
3088                         subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
3089                                 NULL, BT_MANAGER_INTERFACE,
3090                                 BT_INTERFACES_ADDED, NULL, NULL, 0,
3091                                 __bt_opc_event_filter,
3092                                 NULL, NULL);
3093                 }
3094                 if (subs_opp_client_interface_removed_id == -1) {
3095                         subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
3096                                 NULL, BT_MANAGER_INTERFACE,
3097                                 BT_INTERFACES_REMOVED, NULL, NULL, 0,
3098                                 __bt_opc_event_filter,
3099                                 NULL, NULL);
3100                 }
3101                 if (subs_opp_client_property_id == -1) {
3102                         subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
3103                                 NULL, BT_PROPERTIES_INTERFACE,
3104                                 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
3105                                 __bt_opc_event_filter,
3106                                 NULL, NULL);
3107                 }
3108         } else {
3109                 if (subs_opp_client_interface_added_id != -1) {
3110                         g_dbus_connection_signal_unsubscribe(conn,
3111                                         subs_opp_client_interface_added_id);
3112                         subs_opp_client_interface_added_id = -1;
3113                 }
3114                 if (subs_opp_client_interface_removed_id != -1) {
3115                         g_dbus_connection_signal_unsubscribe(conn,
3116                                         subs_opp_client_interface_removed_id);
3117                         subs_opp_client_interface_removed_id = -1;
3118                 }
3119                 if (subs_opp_client_property_id != -1) {
3120                         g_dbus_connection_signal_unsubscribe(conn,
3121                                         subs_opp_client_property_id);
3122                         subs_opp_client_property_id = -1;
3123                 }
3124         }
3125         return 0;
3126 }
3127
3128 int _bt_register_a2dp_subscribe_signal(GDBusConnection *conn,
3129                 int subscribe)
3130 {
3131         if (conn == NULL)
3132                 return -1;
3133
3134         static int subs_a2dp_source_id = -1;
3135         static int subs_a2dp_sink_id = -1;
3136
3137         if (subscribe) {
3138                 if (subs_a2dp_source_id == -1) {
3139                         subs_a2dp_source_id = g_dbus_connection_signal_subscribe(conn,
3140                                 NULL, BT_A2DP_SOURCE_INTERFACE,
3141                                 NULL, NULL, NULL, 0,
3142                                 __bt_opc_event_filter,
3143                                 NULL, NULL);
3144                 }
3145                 if (subs_a2dp_sink_id == -1) {
3146                         subs_a2dp_sink_id = g_dbus_connection_signal_subscribe(conn,
3147                                 NULL, BT_SINK_INTERFACE,
3148                                 NULL, NULL, NULL, 0,
3149                                 __bt_opc_event_filter,
3150                                 NULL, NULL);
3151                 }
3152         } else {
3153                 if (subs_a2dp_source_id != -1) {
3154                         g_dbus_connection_signal_unsubscribe(conn,
3155                                         subs_a2dp_source_id);
3156                         subs_a2dp_source_id = -1;
3157                 }
3158                 if (subs_a2dp_sink_id != -1) {
3159                         g_dbus_connection_signal_unsubscribe(conn,
3160                                         subs_a2dp_sink_id);
3161                         subs_a2dp_sink_id = -1;
3162                 }
3163         }
3164         return 0;
3165 }
3166
3167 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
3168 {
3169         BT_DBG("+");
3170
3171         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3172
3173         switch (event_type) {
3174         case BT_MANAGER_EVENT:
3175                 _bt_register_manager_subscribe_signal(g_conn, TRUE);
3176                 break;
3177         case BT_DEVICE_EVENT:
3178                 _bt_register_device_subscribe_signal(g_conn, TRUE);
3179                 break;
3180         case BT_HID_EVENT:
3181                 _bt_register_input_subscribe_signal(g_conn, TRUE);
3182                 break;
3183         case BT_NETWORK_EVENT:
3184                 _bt_register_network_subscribe_signal(g_conn, TRUE);
3185                 break;
3186         case BT_HEADSET_EVENT:
3187                 _bt_register_audio_subscribe_signal(g_conn, TRUE);
3188                 break;
3189
3190         case BT_OPP_SERVER_EVENT:
3191                 BT_ERR("BT_OPP_SERVER_EVENT: register service event");
3192                 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
3193                 break;
3194         case BT_OPP_CLIENT_EVENT:
3195                 BT_ERR("BT_OPP_CLIENT_EVENT: register service event");
3196                 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
3197                 break;
3198         case BT_A2DP_SOURCE_EVENT:
3199                 BT_INFO("A2dp Source event");
3200                 _bt_register_a2dp_subscribe_signal(g_conn, TRUE);
3201                 break;
3202         default:
3203                 BT_ERR("Unknown event");
3204                 return BLUETOOTH_ERROR_INTERNAL;
3205         }
3206
3207         return BLUETOOTH_ERROR_NONE;
3208 }
3209
3210 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
3211 {
3212         BT_DBG("+");
3213
3214         ret_if(g_conn == NULL);
3215
3216         switch (event_type) {
3217         case BT_MANAGER_EVENT:
3218                 _bt_register_manager_subscribe_signal(g_conn, FALSE);
3219                 _bt_register_device_subscribe_signal(g_conn, FALSE);
3220                 _bt_register_input_subscribe_signal(g_conn, FALSE);
3221                 _bt_register_network_subscribe_signal(g_conn, FALSE);
3222                 _bt_register_audio_subscribe_signal(g_conn, FALSE);
3223                 break;
3224         case BT_OPP_SERVER_EVENT:
3225                 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
3226                 break;
3227         case BT_OPP_CLIENT_EVENT:
3228                 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
3229                 break;
3230         default:
3231                 BT_ERR("Unknown event");
3232                 return;
3233         }
3234
3235         BT_DBG("-");
3236 }
3237
3238 static int __bt_init_manager_receiver(void)
3239 {
3240         BT_DBG("+");
3241
3242         GError *error = NULL;
3243
3244         if (manager_conn == NULL) {
3245                 manager_conn =  g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
3246                 if (error != NULL) {
3247                         BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
3248                         g_clear_error(&error);
3249                 }
3250                 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3251         }
3252
3253         if (_bt_register_service_event(manager_conn,
3254                                 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
3255                 goto fail;
3256         if (_bt_register_service_event(manager_conn,
3257                                 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
3258                 goto fail;
3259
3260         if (_bt_register_service_event(manager_conn,
3261                                 BT_HID_EVENT) != BLUETOOTH_ERROR_NONE)
3262                 goto fail;
3263
3264         if (_bt_register_service_event(manager_conn,
3265                                 BT_HEADSET_EVENT) != BLUETOOTH_ERROR_NONE)
3266                 goto fail;
3267
3268         if (_bt_register_service_event(manager_conn,
3269                                 BT_NETWORK_EVENT) != BLUETOOTH_ERROR_NONE)
3270                 goto fail;
3271         return BLUETOOTH_ERROR_NONE;
3272 fail:
3273         if (manager_conn) {
3274                 g_object_unref(manager_conn);
3275                 manager_conn = NULL;
3276         }
3277
3278         BT_DBG("-");
3279
3280         return BLUETOOTH_ERROR_INTERNAL;
3281 }
3282
3283 static int __bt_init_obexd_receiver(void)
3284 {
3285         BT_DBG("+");
3286 #ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
3287         GError *error = NULL;
3288
3289         if (obexd_conn == NULL) {
3290                 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
3291                 if (error != NULL) {
3292                         BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
3293                         g_clear_error(&error);
3294                 }
3295                 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3296         }
3297
3298         if (_bt_register_service_event(obexd_conn,
3299                                 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
3300                 BT_ERR("Error while registering service event");
3301                 g_object_unref(obexd_conn);
3302                 obexd_conn = NULL;
3303                 return BLUETOOTH_ERROR_INTERNAL;
3304         }
3305 #endif
3306         BT_DBG("-");
3307
3308         return BLUETOOTH_ERROR_NONE;
3309 }
3310
3311 /* To receive the event from bluez */
3312 int _bt_init_service_event_receiver(void)
3313 {
3314         BT_DBG("+");
3315
3316         int result;
3317
3318         result = __bt_init_manager_receiver();
3319         retv_if(result != BLUETOOTH_ERROR_NONE, result);
3320
3321         result = __bt_init_obexd_receiver();
3322         if (result != BLUETOOTH_ERROR_NONE)
3323                 BT_ERR("Fail to init obexd receiver");
3324
3325         BT_DBG("-");
3326
3327         return BLUETOOTH_ERROR_NONE;
3328 }
3329
3330 void _bt_deinit_service_event_receiver(void)
3331 {
3332         BT_DBG("+");
3333
3334         _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
3335
3336         _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
3337
3338         if (manager_conn) {
3339                 g_object_unref(manager_conn);
3340                 manager_conn = NULL;
3341         }
3342
3343         if (obexd_conn) {
3344                 g_object_unref(obexd_conn);
3345                 obexd_conn = NULL;
3346         }
3347
3348         if (event_id > 0)
3349                 g_source_remove(event_id);
3350
3351         BT_DBG("-");
3352 }