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