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