[OAL] Add support for 'Reset Adapter'
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-adapter-mgr.c
old mode 100755 (executable)
new mode 100644 (file)
index c16515e..7bc9d71
@@ -32,6 +32,7 @@
 #include "oal-hardware.h"
 #include "oal-common.h"
 #include "oal-utils.h"
+#include "oal-gatt.h"
 
 #define CHECK_MAX(max, x) (((max) > (x)) ? (x) : (max))
 
@@ -46,6 +47,9 @@ static int discoverable_timeout = 0;
 /* Forward declarations */
 oal_status_t convert_to_oal_status(bt_status_t status);
 static gboolean retry_enable_adapter(gpointer data);
+#ifdef TIZEN_BT_HAL
+static gboolean retry_enable_le(gpointer data);
+#endif
 oal_status_t oal_mgr_init_internal(void);
 
 
@@ -55,22 +59,43 @@ static void cb_adapter_discovery_state_changed(bt_discovery_state_t state);
 static void cb_adapter_device_found(int num_properties, bt_property_t *properties);
 static void cb_adapter_properties (bt_status_t status,
                int num_properties, bt_property_t *properties);
+extern void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
+               int num_properties, bt_property_t *properties);
+extern void cb_device_bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
+                                        bt_bond_state_t state);
+extern void cb_device_acl_state_changed(bt_status_t status, bt_bdaddr_t *remote_bd_addr,
+                                            bt_acl_state_t state);
+extern void cb_device_pin_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class);
+extern void cb_device_ssp_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class,
+                       bt_ssp_variant_t pairing_variant, uint32_t pass_key);
+extern void cb_device_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_service_id_t service_d);
+extern void cb_device_trust_state_changed(bt_bdaddr_t *remote_bd_addr, bt_device_trust_state_t trusted);
+#ifdef TIZEN_BT_HAL
+extern void cb_socket_conn_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_uuid_t *uuid);
+static void cb_ble_state_change(bt_state_t status);
+#endif
 
 static bt_callbacks_t callbacks = {
        sizeof(callbacks),
        cb_adapter_state_change,
        cb_adapter_properties,
-       NULL, /* remote_device_properties_callback */
+       cb_device_properties,
        cb_adapter_device_found,
        cb_adapter_discovery_state_changed,
-       NULL, /* pin_request_callback */
-       NULL, /* ssp_request_callback */
-       NULL, /* bond_state_changed_callback */
-       NULL, /* acl_state_changed_callback */
+       cb_device_pin_request,
+       cb_device_ssp_request,
+       cb_device_bond_state_changed,
+       cb_device_acl_state_changed,
        NULL, /* callback_thread_event */
        NULL, /* dut_mode_recv_callback */
        NULL, /* le_test_mode_callback*/
        NULL, /* energy_info_callback */
+       cb_device_authorize_request,
+       cb_device_trust_state_changed,
+#ifdef TIZEN_BT_HAL
+       cb_socket_conn_authorize_request,
+       cb_ble_state_change,
+#endif
 };
 
 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
@@ -138,7 +163,100 @@ oal_status_t adapter_disable(void)
        return OAL_STATUS_SUCCESS;
 }
 
-oal_status_t adapter_start_inquiry(void)
+oal_status_t le_enable(void)
+{
+       int ret = BT_STATUS_SUCCESS;
+
+       API_TRACE();
+       CHECK_OAL_INITIALIZED();
+
+#ifdef TIZEN_BT_HAL
+       if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
+               g_timeout_add(200, retry_enable_le, NULL);
+               return OAL_STATUS_PENDING;
+       }
+
+       ret = blued_api->le_enable();
+
+       if (ret != BT_STATUS_SUCCESS) {
+               BT_ERR("Enable failed: [%s]", status2string(ret));
+               return convert_to_oal_status(ret);
+       }
+#else
+       BT_INFO("Not Supported");
+       ret = OAL_STATUS_NOT_SUPPORT;
+#endif
+
+       return ret;
+}
+
+oal_status_t le_disable(void)
+{
+       int ret;
+
+       API_TRACE();
+
+       CHECK_OAL_INITIALIZED();
+
+#ifdef TIZEN_BT_HAL
+       ret = blued_api->le_disable();
+
+       if (ret != BT_STATUS_SUCCESS) {
+               BT_ERR("Disable failed: [%s]", status2string(ret));
+               return convert_to_oal_status(ret);
+       }
+#else
+       BT_INFO("Not Supported");
+       ret = OAL_STATUS_NOT_SUPPORT;
+#endif
+       return ret;
+}
+
+oal_status_t adapter_start_custom_inquiry(discovery_type_t disc_type)
+{
+       int ret;
+
+       API_TRACE();
+
+       CHECK_OAL_INITIALIZED();
+       BT_INFO("Custom Discovery Type [0x%x]", disc_type);
+
+#ifdef TIZEN_BT_HAL
+       ret = blued_api->start_custom_discovery(disc_type);
+       if (ret != BT_STATUS_SUCCESS) {
+               BT_ERR("start_custom_discovery failed: [%s]", status2string(ret));
+               return convert_to_oal_status(ret);
+       }
+#else
+       BT_INFO("Not Supported");
+       ret = OAL_STATUS_NOT_SUPPORT;
+#endif
+       return ret;
+}
+
+oal_status_t adapter_reset(void)
+{
+       int ret;
+
+       API_TRACE();
+
+       CHECK_OAL_INITIALIZED();
+       BT_INFO("Adapter Reset");
+
+#ifdef TIZEN_BT_HAL
+       ret = blued_api->reset();
+       if (ret != BT_STATUS_SUCCESS) {
+               BT_ERR("Adapter Reset failed: [%s]", status2string(ret));
+               return convert_to_oal_status(ret);
+       }
+#else
+       BT_INFO("Not Supported");
+       ret = OAL_STATUS_NOT_SUPPORT;
+#endif
+       return ret;
+}
+
+oal_status_t adapter_start_inquiry(unsigned short duration)
 {
        int ret;
 
@@ -183,12 +301,48 @@ static void cb_adapter_state_change(bt_state_t status)
        send_event(event, NULL, 0);
 }
 
+#ifdef TIZEN_BT_HAL
+/* Callbacks from Stack */
+static void cb_ble_state_change(bt_state_t status)
+{
+       BT_DBG("+");
+       oal_event_t event;
+
+       event = (BT_STATE_ON == status)?OAL_EVENT_BLE_ENABLED:OAL_EVENT_BLE_DISABLED;
+
+       send_event(event, NULL, 0);
+}
+#endif
+
 static gboolean retry_enable_adapter(gpointer data)
 {
        adapter_enable();
        return FALSE;
 }
 
+#ifdef TIZEN_BT_HAL
+static gboolean retry_enable_le(gpointer data)
+{
+       le_enable();
+       return FALSE;
+}
+#endif
+oal_status_t adapter_get_properties(void)
+{
+       int ret;
+
+       API_TRACE();
+       CHECK_OAL_INITIALIZED();
+
+       ret = blued_api->get_adapter_properties();
+       if (ret != BT_STATUS_SUCCESS) {
+               BT_ERR("get_adapter_properties failed: [%s]", status2string(ret));
+               return convert_to_oal_status(ret);
+       }
+
+       return OAL_STATUS_SUCCESS;
+}
+
 oal_status_t adapter_get_address(void)
 {
        int ret;
@@ -311,6 +465,23 @@ oal_status_t adapter_get_service_uuids(void)
        return OAL_STATUS_SUCCESS;
 }
 
+oal_status_t adapter_get_bonded_devices(void)
+{
+       int ret;
+
+       CHECK_OAL_INITIALIZED();
+
+       API_TRACE();
+
+       ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
+       if (ret != BT_STATUS_SUCCESS) {
+               BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
+               return convert_to_oal_status(ret);
+       }
+
+       return OAL_STATUS_SUCCESS;
+}
+
 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
 {
        bt_property_t prop;
@@ -346,6 +517,97 @@ oal_status_t adapter_set_connectable(int connectable)
        return set_scan_mode(mode);
 }
 
+oal_status_t adapter_set_discoverable(void)
+{
+       CHECK_OAL_INITIALIZED();
+       API_TRACE();
+
+       return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
+}
+
+oal_status_t adapter_set_discoverable_timeout(int timeout)
+{
+       bt_property_t prop;
+       int res;
+       uint32_t prop_val = timeout;
+
+       CHECK_OAL_INITIALIZED();
+       API_TRACE("%d", timeout);
+
+       prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
+       prop.len = sizeof(prop_val);
+       prop.val = &prop_val;
+       res = blued_api->set_adapter_property(&prop);
+       if (res != BT_STATUS_SUCCESS) {
+               BT_ERR("set_adapter_property failed [%s]", status2string(res));
+               return convert_to_oal_status(res);
+       }
+       return OAL_STATUS_SUCCESS;
+}
+
+oal_status_t adapter_ble_multi_adv_update(int Ins_id, int min_intv, int max_intv,
+                       int adv_type, int chnl_map, int tx_power, int timeout_s)
+{
+       int res;
+       CHECK_OAL_INITIALIZED();
+       API_TRACE();
+
+       res = gatts_multi_adv_update(Ins_id, min_intv, max_intv,
+                       adv_type, chnl_map, tx_power, timeout_s);
+       if (res != OAL_STATUS_SUCCESS) {
+               BT_ERR("gatts_multi_adv_update: [%d]", res);
+               return res;
+       }
+       return OAL_STATUS_SUCCESS;
+}
+
+oal_status_t adapter_ble_multi_adv_set_inst_data(int instance_id,
+                       oal_ble_multi_adv_param_setup_t * adv_param_setup)
+{
+       int res;
+       CHECK_OAL_INITIALIZED();
+       OAL_CHECK_PARAMETER(adv_param_setup, return);
+
+       API_TRACE();
+
+       res = gatts_multi_adv_set_inst_data(instance_id, adv_param_setup);
+       if (res != OAL_STATUS_SUCCESS) {
+               BT_ERR("failed: [%d]", res);
+               return res;
+       }
+       return OAL_STATUS_SUCCESS;
+}
+
+oal_status_t adapter_ble_multi_adv_enable(int instance_id)
+{
+       int res;
+       CHECK_OAL_INITIALIZED();
+       API_TRACE();
+
+       res = gatts_multi_adv_enable(instance_id);
+       if (res != OAL_STATUS_SUCCESS) {
+               BT_ERR("failed: [%d]", res);
+               return res;
+       }
+
+       return OAL_STATUS_SUCCESS;
+}
+
+oal_status_t adapter_ble_multi_adv_disable(int instance_id)
+{
+        int res;
+        CHECK_OAL_INITIALIZED();
+        API_TRACE();
+
+        res = gatts_multi_adv_disable(instance_id);
+        if (res != OAL_STATUS_SUCCESS) {
+                BT_ERR("failed: [%d]", res);
+                return res;
+        }
+
+        return OAL_STATUS_SUCCESS;
+}
+
 static void cb_adapter_properties(bt_status_t status,
                                                int num_properties,
                                                bt_property_t *properties)
@@ -425,7 +687,7 @@ static void cb_adapter_properties(bt_status_t status,
 
                                /* Application has requested this property SET/GET hence send EVENT */
                                send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
-                                               uuids_event, (num_uuid * sizeof(bt_uuid_t)));
+                                               uuids_event, (sizeof(event_adapter_services_t) + num_uuid * sizeof(bt_uuid_t)));
                        }
                        break;
                }
@@ -483,7 +745,7 @@ static void cb_adapter_properties(bt_status_t status,
                                memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
 
                        send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
-                                       event_data, (num_bonded * sizeof(bt_bdaddr_t)));
+                                       event_data, (sizeof(event_device_list_t) + num_bonded * sizeof(bt_bdaddr_t)));
                        break;
                }
                default:
@@ -509,7 +771,7 @@ static void cb_adapter_device_found(int num_properties, bt_property_t *propertie
        ble_adv_data_t adv_info;
        oal_event_t event;
        gpointer event_data;
-       gsize properties_size = 0;
+       gsize size = 0;
        BT_DBG("+");
 
        if (num_properties == 0) {
@@ -521,9 +783,9 @@ static void cb_adapter_device_found(int num_properties, bt_property_t *propertie
        memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
 
        print_bt_properties(num_properties, properties);
-       parse_device_properties(num_properties, properties, &dev_info, &adv_info, &properties_size);
+       parse_device_properties(num_properties, properties, &dev_info, &adv_info);
 
-       BT_INFO("number of properties= [%d] total size [%u]", num_properties, properties_size);
+       BT_INFO("number of properties= [%d] ", num_properties, size);
 
        if (dev_info.type != DEV_TYPE_BREDR) {
                /* BLE Single or DUAL mode found, so it should have Adv data */
@@ -540,6 +802,7 @@ static void cb_adapter_device_found(int num_properties, bt_property_t *propertie
                ble_dev_event->device_info = dev_info;
 
                event_data = ble_dev_event;
+               size = sizeof(event_ble_dev_found_t);
                event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
        } else {
                /* BREDR device, so No Adv data */
@@ -547,10 +810,11 @@ static void cb_adapter_device_found(int num_properties, bt_property_t *propertie
 
                memcpy(dev_event, &dev_info, sizeof(remote_device_t));
                event_data = dev_event;
+               size = sizeof(remote_device_t);
                event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
        }
 
-       send_event(event, event_data, properties_size);
+       send_event(event, event_data, size);
 
        BT_DBG("-");
 }