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