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