From cebe459176d9a64e42137a61e9c6831b3f3f892c Mon Sep 17 00:00:00 2001 From: Martin Brabham Date: Wed, 31 Mar 2021 09:41:34 -0700 Subject: [PATCH] OOB: Implement generateLocalOobData API Ignore-AOSP-First: Conflict with internal Bug: 178007935 Tag: #feature Test: manual Change-Id: I149907ef7ee3b6f9600ac57950afbc9616381dc0 --- binder/Android.bp | 2 + binder/android/bluetooth/IBluetooth.aidl | 2 + .../bluetooth/IBluetoothOobDataCallback.aidl | 30 +++++++++++++ btif/include/btif_common.h | 2 + btif/include/btif_dm.h | 1 + btif/src/bluetooth.cc | 40 ++++++++++++++++- btif/src/btif_dm.cc | 50 +++++++++------------- include/hardware/bluetooth.h | 12 ++++++ main/test/common/mock_btif_dm.cc | 2 +- service/hal/bluetooth_interface.cc | 3 +- service/hal/fake_bluetooth_interface.cc | 1 + 11 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 binder/android/bluetooth/IBluetoothOobDataCallback.aidl diff --git a/binder/Android.bp b/binder/Android.bp index fb1e32897..5f7173134 100644 --- a/binder/Android.bp +++ b/binder/Android.bp @@ -45,6 +45,7 @@ cc_library_shared { "android/bluetooth/IBluetoothGattCallback.aidl", "android/bluetooth/IBluetoothMetadataListener.aidl", "android/bluetooth/IBluetoothGattServerCallback.aidl", + "android/bluetooth/IBluetoothOobDataCallback.aidl", "android/bluetooth/le/IAdvertisingSetCallback.aidl", "android/bluetooth/le/IPeriodicAdvertisingCallback.aidl", "android/bluetooth/le/IScannerCallback.aidl" @@ -121,6 +122,7 @@ filegroup { "android/bluetooth/IBluetoothGattCallback.aidl", "android/bluetooth/IBluetoothMetadataListener.aidl", "android/bluetooth/IBluetoothGattServerCallback.aidl", + "android/bluetooth/IBluetoothOobDataCallback.aidl", "android/bluetooth/le/IAdvertisingSetCallback.aidl", "android/bluetooth/le/IPeriodicAdvertisingCallback.aidl", "android/bluetooth/le/IScannerCallback.aidl", diff --git a/binder/android/bluetooth/IBluetooth.aidl b/binder/android/bluetooth/IBluetooth.aidl index 5e7e19441..086a299dc 100644 --- a/binder/android/bluetooth/IBluetooth.aidl +++ b/binder/android/bluetooth/IBluetooth.aidl @@ -19,6 +19,7 @@ package android.bluetooth; import android.bluetooth.IBluetoothCallback; import android.bluetooth.IBluetoothConnectionCallback; import android.bluetooth.IBluetoothMetadataListener; +import android.bluetooth.IBluetoothOobDataCallback; import android.bluetooth.IBluetoothSocketManager; import android.bluetooth.IBluetoothStateChangeCallback; import android.bluetooth.BluetoothActivityEnergyInfo; @@ -153,4 +154,5 @@ interface IBluetooth boolean unregisterBluetoothConnectionCallback(in IBluetoothConnectionCallback callback); boolean canBondWithoutDialog(in BluetoothDevice device); + void generateLocalOobData(in int transport, IBluetoothOobDataCallback callback); } diff --git a/binder/android/bluetooth/IBluetoothOobDataCallback.aidl b/binder/android/bluetooth/IBluetoothOobDataCallback.aidl new file mode 100644 index 000000000..00975b6bb --- /dev/null +++ b/binder/android/bluetooth/IBluetoothOobDataCallback.aidl @@ -0,0 +1,30 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.bluetooth; + +import android.bluetooth.IBluetooth; +import android.bluetooth.OobData; + +/** + * API for receiving OobData from the host stack + * + * {@hide} + */ +oneway interface IBluetoothOobDataCallback { + void onOobData(int transport, in OobData oobData); + void onError(int errorCode); +} diff --git a/btif/include/btif_common.h b/btif/include/btif_common.h index ea3ead6ed..741263817 100644 --- a/btif/include/btif_common.h +++ b/btif/include/btif_common.h @@ -207,6 +207,8 @@ void invoke_pin_request_cb(RawAddress bd_addr, bt_bdname_t bd_name, void invoke_ssp_request_cb(RawAddress bd_addr, bt_bdname_t bd_name, uint32_t cod, bt_ssp_variant_t pairing_variant, uint32_t pass_key); +void invoke_oob_data_request_cb(tBT_TRANSPORT t, bool valid, Octet16 c, + Octet16 r); void invoke_bond_state_changed_cb(bt_status_t status, RawAddress bd_addr, bt_bond_state_t state); void invoke_acl_state_changed_cb(bt_status_t status, RawAddress bd_addr, diff --git a/btif/include/btif_dm.h b/btif/include/btif_dm.h index 0fe514300..98ff8d0da 100644 --- a/btif/include/btif_dm.h +++ b/btif/include/btif_dm.h @@ -62,6 +62,7 @@ void btif_dm_load_local_oob(void); void btif_dm_proc_loc_oob(bool valid, const Octet16& c, const Octet16& r); bool btif_dm_proc_rmt_oob(const RawAddress& bd_addr, Octet16* p_c, Octet16* p_r); +void btif_dm_generate_local_oob_data(tBT_TRANSPORT transport); #endif /* BTIF_DM_OOB_TEST */ /*callout for reading SMP properties from Text file*/ diff --git a/btif/src/bluetooth.cc b/btif/src/bluetooth.cc index 92029cb41..d02b2aa03 100644 --- a/btif/src/bluetooth.cc +++ b/btif/src/bluetooth.cc @@ -319,6 +319,14 @@ static int create_bond_out_of_band(const RawAddress* bd_addr, int transport, return BT_STATUS_SUCCESS; } +static int generate_local_oob_data(tBT_TRANSPORT transport) { + LOG_INFO("%s", __func__); + if (!interface_ready()) return BT_STATUS_NOT_READY; + + return do_in_main_thread( + FROM_HERE, base::BindOnce(btif_dm_generate_local_oob_data, transport)); +} + static int cancel_bond(const RawAddress* bd_addr) { if (!interface_ready()) return BT_STATUS_NOT_READY; @@ -595,7 +603,7 @@ EXPORT_SYMBOL bt_interface_t bluetoothInterface = { obfuscate_address, get_metric_id, set_dynamic_audio_buffer_size, -}; + generate_local_oob_data}; // callback reporting helpers @@ -721,6 +729,36 @@ void invoke_ssp_request_cb(RawAddress bd_addr, bt_bdname_t bd_name, bd_addr, bd_name, cod, pairing_variant, pass_key)); } +void invoke_oob_data_request_cb(tBT_TRANSPORT t, bool valid, Octet16 c, + Octet16 r) { + LOG_INFO("%s", __func__); + bt_oob_data_t oob_data; + // Each value (for C and R) is 16 octets in length + bool c_empty = true; + for (int i = 0; i < 16; i++) { + // C cannot be all 0s, if so then we want to fail + if (c[i] != 0) c_empty = false; + oob_data.c[i] = c[i]; + // R is optional and may be empty + oob_data.r[i] = r[i]; + } + oob_data.is_valid = valid && !c_empty; + // The oob_data_length is 2 octects in length. The value includes the length + // of itself. 16 + 16 + 2 = 34 Data 0x0022 Little Endian order 0x2200 + oob_data.oob_data_length[0] = 0; + oob_data.oob_data_length[1] = 34; + bt_status_t status = do_in_jni_thread( + FROM_HERE, base::BindOnce( + [](tBT_TRANSPORT t, bt_oob_data_t oob_data) { + HAL_CBACK(bt_hal_cbacks, generate_local_oob_data_cb, t, + oob_data); + }, + t, oob_data)); + if (status != BT_STATUS_SUCCESS) { + LOG_ERROR("%s: Failed to call callback!", __func__); + } +} + void invoke_bond_state_changed_cb(bt_status_t status, RawAddress bd_addr, bt_bond_state_t state) { do_in_jni_thread( diff --git a/btif/src/btif_dm.cc b/btif/src/btif_dm.cc index 365aeffb6..5c17ec857 100644 --- a/btif/src/btif_dm.cc +++ b/btif/src/btif_dm.cc @@ -2252,39 +2252,29 @@ void btif_dm_load_local_oob(void) { } } -void btif_dm_proc_loc_oob(bool valid, const Octet16& c, const Octet16& r) { - FILE* fp; - const char* path_a = "/data/misc/bluedroid/LOCAL/a.key"; - const char* path_b = "/data/misc/bluedroid/LOCAL/b.key"; - const char* path = NULL; - char prop_oob[PROPERTY_VALUE_MAX]; - BTIF_TRACE_DEBUG("%s: valid=%d", __func__, valid); - if (is_empty_128bit(oob_cb.p192_data.c) && valid) { - BTIF_TRACE_DEBUG("save local OOB data in memory"); - memcpy(oob_cb.p192_data.c, c.data(), OCTET16_LEN); - memcpy(oob_cb.p192_data.r, r.data(), OCTET16_LEN); - osi_property_get("service.brcm.bt.oob", prop_oob, "3"); - BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob); - if (prop_oob[0] == '1') - path = path_a; - else if (prop_oob[0] == '2') - path = path_b; - if (path) { - fp = fopen(path, "wb+"); - if (fp == NULL) { - BTIF_TRACE_DEBUG("%s: failed to save local OOB data to %s", __func__, - path); - } else { - BTIF_TRACE_DEBUG("%s: save local OOB data into file %s", __func__, - path); - fwrite(c.data(), 1, OCTET16_LEN, fp); - fwrite(r.data(), 1, OCTET16_LEN, fp); - fclose(fp); - } - } +/******************************************************************************* + * + * Function btif_dm_generate_local_oob_data + * + * Description Initiate oob data fetch from controller + * + * Parameters transport; Classic or LE + * + ******************************************************************************/ +void btif_dm_generate_local_oob_data(tBT_TRANSPORT transport) { + if (transport == BT_TRANSPORT_BR_EDR) { + BTM_ReadLocalOobData(); + } else if (transport == BT_TRANSPORT_LE) { + // TODO(184377951): Call LE Implementation (not yet implemented?) + } else { + BTIF_TRACE_ERROR("Bad transport type! %d", transport); } } +void btif_dm_proc_loc_oob(bool valid, const Octet16& c, const Octet16& r) { + invoke_oob_data_request_cb(BT_TRANSPORT_BR_EDR, valid, c, r); +} + /******************************************************************************* * * Function btif_dm_get_smp_config diff --git a/include/hardware/bluetooth.h b/include/hardware/bluetooth.h index cd7259529..4331119b4 100644 --- a/include/hardware/bluetooth.h +++ b/include/hardware/bluetooth.h @@ -24,6 +24,7 @@ #include "avrcp/avrcp.h" #include "bluetooth/uuid.h" +#include "bt_transport.h" #include "raw_address.h" /** @@ -335,6 +336,7 @@ typedef struct { /** Represents the actual Out of Band data itself */ typedef struct { // Both + bool is_valid = false; /* Default to invalid data; force caller to verify */ uint8_t address[7]; /* Bluetooth Device Address (6) plus Address Type (1) */ uint8_t c[16]; /* Simple Pairing Hash C-192/256 (Classic or LE) */ uint8_t r[16]; /* Simple Pairing Randomizer R-192/256 (Classic or LE) */ @@ -482,6 +484,10 @@ typedef void (*le_test_mode_callback)(bt_status_t status, uint16_t num_packets); typedef void (*energy_info_callback)(bt_activity_energy_info* energy_info, bt_uid_traffic_t* uid_data); +/** Callback invoked when OOB data is returned from the controller */ +typedef void (*generate_local_oob_data_callback)(tBT_TRANSPORT transport, + bt_oob_data_t oob_data); + /** TODO: Add callbacks for Link Up/Down and other generic * notifications/callbacks */ @@ -503,6 +509,7 @@ typedef struct { le_test_mode_callback le_test_mode_cb; energy_info_callback energy_info_cb; link_quality_report_callback link_quality_report_cb; + generate_local_oob_data_callback generate_local_oob_data_cb; } bt_callbacks_t; typedef void (*alarm_cb)(void* data); @@ -728,6 +735,11 @@ typedef struct { * Set the dynamic audio buffer size to the Controller */ int (*set_dynamic_audio_buffer_size)(int codec, int size); + + /** + * Fetches the local Out of Band data. + */ + int (*generate_local_oob_data)(tBT_TRANSPORT transport); } bt_interface_t; #define BLUETOOTH_INTERFACE_STRING "bluetoothInterface" diff --git a/main/test/common/mock_btif_dm.cc b/main/test/common/mock_btif_dm.cc index e2fbc322d..0e986b581 100644 --- a/main/test/common/mock_btif_dm.cc +++ b/main/test/common/mock_btif_dm.cc @@ -168,6 +168,6 @@ bool btif_dm_proc_rmt_oob(const RawAddress& bd_addr, Octet16* p_c, return false; } -void btif_dm_proc_loc_oob(bool valid, const Octet16& c, const Octet16& r) { +void btif_dm_proc_loc_oob(const Octet16& c, const Octet16& r) { mock_function_count_map[__func__]++; } diff --git a/service/hal/bluetooth_interface.cc b/service/hal/bluetooth_interface.cc index 3afe635c5..84a17859e 100644 --- a/service/hal/bluetooth_interface.cc +++ b/service/hal/bluetooth_interface.cc @@ -226,7 +226,8 @@ bt_callbacks_t bt_callbacks = { nullptr, /* dut_mode_recv_cb */ nullptr, /* le_test_mode_cb */ nullptr, /* energy_info_cb */ - LinkQualityReportCallback + LinkQualityReportCallback, + nullptr /* generate_local_oob_data_cb */ }; bt_os_callouts_t bt_os_callouts = {sizeof(bt_os_callouts_t), diff --git a/service/hal/fake_bluetooth_interface.cc b/service/hal/fake_bluetooth_interface.cc index cf6123d9f..5e7d5e8e8 100644 --- a/service/hal/fake_bluetooth_interface.cc +++ b/service/hal/fake_bluetooth_interface.cc @@ -77,6 +77,7 @@ bt_interface_t fake_bt_iface = { nullptr, /* obfuscate_address */ nullptr, /* get_metric_id */ nullptr, /* set_dynamic_audio_buffer_size */ + nullptr, /* generate_local_oob_data */ }; } // namespace -- 2.11.0