Handling le connection interval
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-event-receiver.c
index de64d20..d3a550e 100644 (file)
@@ -43,7 +43,7 @@
 
 #include "bt-service-opp-client.h"
 
-#ifdef TIZEN_FEATURE_BT_DPM
+#ifdef TIZEN_DPM_ENABLE
 #include "bt-service-dpm.h"
 #endif
 
@@ -55,7 +55,6 @@ static GDBusConnection *opc_obexd_conn;
 static GList *p_cache_list = NULL;
 
 static guint event_id;
-static guint le_scan_event_id = 0;
 static guint session_reinit_timer;
 guint nap_connected_device_count = 0;
 static guint hid_connected_device_count = 0;
@@ -218,7 +217,7 @@ static gboolean __bt_parse_device_properties(GVariant *item,
                                }
                                g_free(uuid_value);
                        }
-               } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
+               } else if (strcasecmp(key, "LegacyManufacturerDataLen") == 0) {
                        g_variant_get(val, "q", &dev_info->manufacturer_data_len);
                        if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
                                BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
@@ -227,7 +226,7 @@ static gboolean __bt_parse_device_properties(GVariant *item,
 
                        if (dev_info->manufacturer_data_len == 0)
                                dev_info->manufacturer_data = g_strdup("");
-               } else if (strcasecmp(key, "ManufacturerData") == 0) {
+               } else if (strcasecmp(key, "LegacyManufacturerData") == 0) {
                        int i = 0;
                        int len = 0;
                        GVariantIter *value_iter;
@@ -345,7 +344,7 @@ gboolean _bt_discovery_finished_cb(gpointer user_data)
 static gboolean __bt_le_discovery_finished_cb(gpointer user_data)
 {
        int result = BLUETOOTH_ERROR_NONE;
-       le_scan_event_id = 0;
+       event_id = 0;
        GVariant *param = NULL;
        if (_bt_get_discovering_property(DISCOVERY_ROLE_LE) == FALSE) {
                if (_bt_get_cancel_by_user() == TRUE)
@@ -594,13 +593,13 @@ void _bt_handle_adapter_event(GVariant *msg, const char *member)
                                param);
                g_free(address);
        } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
-               BT_ERR_C("Hardware error received from BLUEZ");
+               BT_ERR_C("### Hardware error received from BLUEZ");
 /* Don't add the recovery logic into platform */
 #if 0
                _bt_recover_adapter();
 #endif
        } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
-               BT_ERR_C("Tx timeout error received from BLUEZ");
+               BT_ERR_C("### Tx timeout error received from BLUEZ");
 /* Don't add the recovery logic into platform */
 #if 0
                _bt_recover_adapter();
@@ -669,7 +668,7 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                                BLUETOOTH_EVENT_LE_DISCOVERY_STARTED,
                                param);
                        } else {
-                               ret_if(le_scan_event_id > 0);
+                               ret_if(event_id > 0);
 
                                adapter_proxy = _bt_get_adapter_proxy();
                                ret_if(adapter_proxy == NULL);
@@ -687,7 +686,7 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                                if (ret)
                                        g_variant_unref(ret);
 
-                               le_scan_event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
+                               event_id = g_timeout_add(BT_DISCOVERY_FINISHED_DELAY,
                                                (GSourceFunc)__bt_le_discovery_finished_cb, NULL);
                                }
                } else if (strcasecmp(property, "Name") == 0) {
@@ -786,7 +785,7 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                        g_variant_get(val, "b", &powered);
                        BT_DBG("Powered = %d", powered);
                        if (powered == FALSE) {
-#ifdef TIZEN_FEATURE_BT_USB_DONGLE
+#ifdef USB_BLUETOOTH
                                _bt_handle_adapter_removed();
 #else
                                if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_state) == 0 &&
@@ -802,7 +801,7 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                                        _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
                                }
                        } else {
-#ifdef TIZEN_FEATURE_BT_USB_DONGLE
+#ifdef USB_BLUETOOTH
                                _bt_handle_adapter_added();
 #endif
                        }
@@ -965,7 +964,7 @@ static void __bt_device_property_changed_event(GVariant *msg, const char *path)
                        if (remote_dev_info->addr_type == 0) {
                                BT_DBG("Name %s", remote_dev_info->name);
 
-#ifdef TIZEN_FEATURE_BT_DPM
+#ifdef TIZEN_DPM_ENABLE
                                if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
                                                        DPM_RESTRICTED) {
                                        bluetooth_device_class_t device_class;
@@ -1619,17 +1618,14 @@ void __bt_gatt_char_property_changed_event(GVariant *msg,
        g_variant_iter_init(&value_iter, msg);
        char_handle = g_strdup(path);
        while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
-               BT_INFO("Property %s", property);
+               BT_DBG("Property %s", property);
 
                ret_if(property == NULL);
 
                if (strcasecmp(property, "Notifying") == 0) {
                        gboolean property_flag = FALSE;
                        g_variant_get(val, "b", &property_flag);
-                       if (property_flag == TRUE)
-                               BT_DBG("notifying is enabled");
-                       else
-                               BT_DBG("notifying is disabled");
+                       BT_INFO("Notifying is %s", property_flag ? "enabled" : "disabled");
                } else if (strcasecmp(property, "ChangedValue") == 0) {
                        int len = 0;
                        GByteArray *gp_byte_array = NULL;
@@ -1718,7 +1714,6 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                        gboolean connected = FALSE;
                        char *address;
                        address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-                       ret_if(address == NULL);
 
                        _bt_convert_device_path_to_address(path, address);
                        g_variant_get(msg, "(b)", &connected);
@@ -1751,7 +1746,6 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                        }
 
                        address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-                       ret_if(address == NULL);
 
                        _bt_convert_device_path_to_address(path, address);
 
@@ -1802,7 +1796,6 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                        ret_if(_bt_is_device_creating() == TRUE);
 
                        address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-                       ret_if(address == NULL);
 
                        _bt_convert_device_path_to_address(path, address);
 
@@ -1852,7 +1845,6 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                g_variant_get(msg, "(y)", &addr_type);
 
                address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-               ret_if(address == NULL);
 
                _bt_convert_device_path_to_address(path, address);
                dev_name = _bt_get_bonded_device_name(address);
@@ -1863,6 +1855,9 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                                !addr_type ? dev_name : secure_address);
                g_free(dev_name);
 
+               if (addr_type)
+                       _bt_add_le_connected_dev_info(address);
+
                _bt_logging_connection(TRUE, addr_type);
                param = g_variant_new("(isy)", result, address, addr_type);
                /*Send event to application*/
@@ -1881,7 +1876,6 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                result = disc_reason;
 
                address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-               ret_if(address == NULL);
 
                _bt_convert_device_path_to_address(path, address);
 
@@ -1890,15 +1884,17 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                      0x02 BDADDR_LE_RANDOM */
                _bt_convert_addr_string_to_secure_string(secure_address, address);
                BT_INFO("Address : %s Type : %d", secure_address, addr_type);
-               BT_ERR_C("### Disconnected [%s] [%d : %s] [%s]", !addr_type ? "BREDR" : "LE",
-                               disc_reason, _bt_convert_disc_reason_to_string(disc_reason),
-                               !addr_type ? dev_name : secure_address);
+               BT_ERR_C("### Disconnected [%s] [%s] [%d : %s]", !addr_type ? "BREDR" : "LE",
+                               !addr_type ? dev_name : secure_address,
+                               disc_reason, _bt_convert_disc_reason_to_string(disc_reason));
                g_free(dev_name);
 
                _bt_headset_set_local_connection(FALSE);
                _bt_logging_connection(FALSE, addr_type);
 
-               if (!addr_type) {
+               if (addr_type)
+                       _bt_remove_le_connected_dev_info(address);
+               else {
 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
                        {
                                int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
@@ -1947,15 +1943,14 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                g_variant_get(msg, "(si)", &profile_uuid, &state);
 
                address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-               ret_if(address == NULL);
 
                _bt_convert_device_path_to_address(path, address);
                _bt_convert_addr_string_to_type(bd_addr.addr, address);
 
                _bt_convert_addr_string_to_secure_string(secure_address, address);
-               BT_DBG("Address: %s", secure_address);
-               BT_DBG("Profile UUID: %s", profile_uuid);
-               BT_DBG("State: %d", state);
+               BT_INFO_C("[%s] state: %s, addr: %s",
+                       _bt_convert_uuid_to_string(profile_uuid),
+                       _bt_convert_profile_state_to_string(state), secure_address);
 
                if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0)  &&
                        (state == BT_PROFILE_STATE_CONNECTED)) {
@@ -2162,15 +2157,18 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                ret_if(_bt_is_le_scanning() == FALSE);
 
                le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
-               if (le_dev_info == NULL)
-                       return;
 
-               g_variant_get(msg, "(syyii@ay)", &le_dev_info->address,
+               g_variant_get(msg, "(&syyii@ay)", &le_dev_info->address,
                                                &le_dev_info->addr_type,
                                                &le_dev_info->adv_type,
                                                &le_dev_info->rssi,
                                                &le_dev_info->adv_data_len,
                                                &value);
+               if (value == NULL) {
+                       _bt_free_le_device_info(le_dev_info);
+                       return;
+               }
+
                _bt_convert_device_path_to_address(path, le_dev_info->address);
 
                buffer_len = g_variant_get_size(value);
@@ -2194,21 +2192,13 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
 
                if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {       /* ADV_IND */
                        adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
-                       if (adv_info == NULL) {
-                               _bt_free_le_device_info(le_dev_info);
-                               g_variant_unref(value);
-                               return;
-                       }
-
                        adv_info->addr = g_strdup(le_dev_info->address);
                        adv_info->addr_type = le_dev_info->addr_type;
                        adv_info->rssi = le_dev_info->rssi;
                        adv_info->data_len = le_dev_info->adv_data_len;
                        adv_info->data = g_malloc0(le_dev_info->adv_data_len);
-                       if (adv_info->data) {
-                               memcpy(adv_info->data, le_dev_info->adv_data,
-                                               le_dev_info->adv_data_len);
-                       }
+                       memcpy(adv_info->data, le_dev_info->adv_data,
+                                       le_dev_info->adv_data_len);
 
                        if (__bt_add_adv_ind_info(adv_info) == 0) {
                                adv_info->timer_id = g_timeout_add(1000,
@@ -2262,6 +2252,23 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                                                event,
                                                param);
                g_free(address);
+       } else if (strcasecmp(member, "iBeaconReport") == 0) {
+               bt_remote_ibeacon_dev_info_t *ibeacon_dev_info = NULL;
+
+               ret_if(_bt_is_le_scanning() == FALSE);
+
+               ibeacon_dev_info = g_malloc0(sizeof(bt_remote_ibeacon_dev_info_t));
+
+               g_variant_get(msg, "(syuusuuy)", &ibeacon_dev_info->address,
+                                               &ibeacon_dev_info->addr_type,
+                                               &ibeacon_dev_info->company_id,
+                                               &ibeacon_dev_info->ibeacon_type,
+                                               &ibeacon_dev_info->uuid,
+                                               &ibeacon_dev_info->major_id,
+                                               &ibeacon_dev_info->minor_id,
+                                               &ibeacon_dev_info->measured_power);
+               _bt_send_ibeacon_scan_result_event(ibeacon_dev_info);
+               g_free(ibeacon_dev_info);
        }
 }
 
@@ -2328,8 +2335,6 @@ void _bt_handle_headset_event(GVariant *msg, const char *path)
                g_variant_get(value, "b", &property_flag);
 
                address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-               if (address == NULL)
-                       return;
 
                _bt_convert_device_path_to_address(path, address);
                _bt_convert_addr_string_to_type(bd_addr.addr, address);
@@ -2675,7 +2680,7 @@ static  void __bt_manager_event_filter(GDBusConnection *connection,
                g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
 
                if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0) {
-#ifdef TIZEN_FEATURE_BT_USB_DONGLE
+#ifdef USB_BLUETOOTH
                        BT_DBG("Enable Adapter");
                        _bt_enable_adapter();
 #else
@@ -2687,17 +2692,15 @@ static  void __bt_manager_event_filter(GDBusConnection *connection,
                                bt_cache_info_t *cache_info;
                                bt_remote_dev_info_t *dev_info;
 
-                               ret_if(_bt_is_discovering() == FALSE &&
-                                               _bt_is_le_scanning() == FALSE);
+                               if (_bt_is_discovering() == FALSE &&
+                                   _bt_is_le_scanning() == FALSE) {
+                                       g_variant_unref(value);
+                                       return;
+                               }
 
                                cache_info = g_malloc0(sizeof(bt_cache_info_t));
-                               ret_if(cache_info == NULL);
 
                                dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
-                               if (dev_info == NULL) {
-                                       __bt_free_cache_info(cache_info);
-                                       return;
-                               }
 
                                cache_info->dev_info = dev_info;
 
@@ -2723,7 +2726,7 @@ static  void __bt_manager_event_filter(GDBusConnection *connection,
                                         */
                                        dev_info->name = g_strdup(dev_info->address);
 
-#ifdef TIZEN_FEATURE_BT_DPM
+#ifdef TIZEN_DPM_ENABLE
                                if (_bt_dpm_get_bluetooth_desktop_connectivity_state() ==
                                                        DPM_RESTRICTED) {
                                        bluetooth_device_class_t device_class;
@@ -2777,7 +2780,7 @@ static  void __bt_manager_event_filter(GDBusConnection *connection,
                }
                g_variant_unref(value);
        } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
-#ifdef TIZEN_FEATURE_BT_USB_DONGLE
+#ifdef USB_BLUETOOTH
                BT_DBG("InterfacesRemoved");
                _bt_handle_adapter_removed();
 #endif
@@ -2838,10 +2841,15 @@ static  void __bt_manager_event_filter(GDBusConnection *connection,
                        _bt_rfcomm_server_check_termination(name);
                }
 
+               /* Reset connection interval */
+               _bt_remove_all_le_conn_param_info(name);
+
                /* Stop advertising started by terminated process */
                _bt_stop_advertising_by_terminated_process(name);
+
                /* Stop LE Scan */
                _bt_stop_le_scan(name);
+
                g_free(name);
                g_free(previous);
                g_free(current);
@@ -3597,7 +3605,7 @@ fail:
 static int __bt_init_obexd_receiver(void)
 {
        BT_DBG("+");
-#ifndef TIZEN_PROFILE_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
+#ifndef TIZEN_TV /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
        GError *error = NULL;
 
        if (obexd_conn == NULL) {
@@ -3703,10 +3711,5 @@ void _bt_deinit_service_event_receiver(void)
                g_source_remove(event_id);
        event_id = 0;
 
-       if (le_scan_event_id > 0)
-               g_source_remove(le_scan_event_id);
-       le_scan_event_id = 0;
-
        BT_DBG("-");
 }
-