Merge "Revert "Fix VTS Fuzz issue"" am: e959d0923d am: cfb439b4eb am: b6f1abab75

Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/2646737

Change-Id: I78379a5e03447b878d035432cfefc4a81ef818cf
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot 2023-07-03 14:51:39 +00:00 committed by Automerger Merge Worker
commit b2a55b46de
4 changed files with 117 additions and 208 deletions

View file

@ -33,8 +33,7 @@ static const uint8_t HCI_DATA_TYPE_SCO = 3;
class BluetoothDeathRecipient : public hidl_death_recipient {
public:
BluetoothDeathRecipient(const sp<IBluetoothHci> hci)
: mHci(hci), has_died_(false) {}
BluetoothDeathRecipient(const sp<IBluetoothHci> hci) : mHci(hci) {}
virtual void serviceDied(
uint64_t /*cookie*/,
@ -52,7 +51,7 @@ class BluetoothDeathRecipient : public hidl_death_recipient {
};
BluetoothHci::BluetoothHci()
: death_recipient_(new BluetoothDeathRecipient(this)) {bt_enabled = 0;}
: death_recipient_(new BluetoothDeathRecipient(this)) {}
Return<void> BluetoothHci::initialize(
const ::android::sp<IBluetoothHciCallbacks>& cb) {
@ -62,19 +61,8 @@ Return<void> BluetoothHci::initialize(
return Void();
}
if (bt_enabled == 1) {
ALOGE("initialize was called!");
return Void();
}
bt_enabled = 1;
death_recipient_->setHasDied(false);
cb->linkToDeath(death_recipient_, 0);
unlink_cb_ = [cb](sp<BluetoothDeathRecipient>& death_recipient) {
if (death_recipient->getHasDied())
ALOGI("Skipping unlink call, service died.");
else
cb->unlinkToDeath(death_recipient);
};
bool rc = VendorInterface::Initialize(
[cb](bool status) {
@ -124,12 +112,6 @@ Return<void> BluetoothHci::initialize(
Return<void> BluetoothHci::close() {
ALOGI("BluetoothHci::close()");
if (bt_enabled != 1) {
ALOGE("should initialize first!");
return Void();
}
bt_enabled = 0;
unlink_cb_(death_recipient_);
VendorInterface::Shutdown();
return Void();
@ -152,11 +134,6 @@ Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& data) {
void BluetoothHci::sendDataToController(const uint8_t type,
const hidl_vec<uint8_t>& data) {
if (bt_enabled != 1) {
ALOGE("should initialize first!");
return;
}
VendorInterface::get()->Send(type, data.data(), data.size());
}

View file

@ -48,7 +48,6 @@ class BluetoothHci : public IBluetoothHci {
void sendDataToController(const uint8_t type, const hidl_vec<uint8_t>& data);
::android::sp<BluetoothDeathRecipient> death_recipient_;
std::function<void(sp<BluetoothDeathRecipient>&)> unlink_cb_;
int bt_enabled;
};
extern "C" IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* name);

View file

@ -36,8 +36,6 @@ static const char* VENDOR_LIBRARY_SYMBOL_NAME =
"BLUETOOTH_VENDOR_LIB_INTERFACE";
static const int INVALID_FD = -1;
std::mutex vendor_mutex_;
std::mutex initcb_mutex_;
namespace {
@ -49,25 +47,13 @@ struct {
uint16_t opcode;
} internal_command;
enum {
VENDOR_STATE_INIT = 1,
VENDOR_STATE_OPENING, /* during opening */
VENDOR_STATE_OPENED, /* open in fops_open */
VENDOR_STATE_CLOSING, /* during closing */
VENDOR_STATE_CLOSED, /* closed */
VENDOR_STATE_MSG_NUM
} ;
uint8_t vstate = VENDOR_STATE_INIT;
// True when LPM is not enabled yet or wake is not asserted.
bool lpm_wake_deasserted;
uint32_t lpm_timeout_ms;
bool recent_activity_flag;
VendorInterface* g_vendor_interface = nullptr;
static VendorInterface vendor_interface;
std::mutex wakeup_mutex_;
HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
size_t packet_size = data.size() + sizeof(HC_BT_HDR);
@ -181,8 +167,11 @@ bool VendorInterface::Initialize(
InitializeCompleteCallback initialize_complete_cb,
PacketReadCallback event_cb, PacketReadCallback acl_cb,
PacketReadCallback sco_cb, PacketReadCallback iso_cb) {
ALOGI("%s: VendorInterface::Initialize", __func__);
g_vendor_interface = &vendor_interface;
if (g_vendor_interface) {
ALOGE("%s: No previous Shutdown()?", __func__);
return false;
}
g_vendor_interface = new VendorInterface();
return g_vendor_interface->Open(initialize_complete_cb, event_cb, acl_cb,
sco_cb, iso_cb);
}
@ -190,8 +179,9 @@ bool VendorInterface::Initialize(
void VendorInterface::Shutdown() {
LOG_ALWAYS_FATAL_IF(!g_vendor_interface, "%s: No Vendor interface!",
__func__);
ALOGI("%s: VendorInterface::Shutdown", __func__);
g_vendor_interface->Close();
delete g_vendor_interface;
g_vendor_interface = nullptr;
}
VendorInterface* VendorInterface::get() { return g_vendor_interface; }
@ -201,189 +191,144 @@ bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
PacketReadCallback acl_cb,
PacketReadCallback sco_cb,
PacketReadCallback iso_cb) {
{
std::unique_lock<std::mutex> guard(vendor_mutex_);
if (vstate == VENDOR_STATE_OPENED) {
ALOGW("VendorInterface opened!");
return true;
}
initialize_complete_cb_ = initialize_complete_cb;
if ((vstate == VENDOR_STATE_CLOSING) ||
(vstate == VENDOR_STATE_OPENING)) {
ALOGW("VendorInterface open/close is on-going !");
return true;
}
// Initialize vendor interface
vstate = VENDOR_STATE_OPENING;
ALOGI("%s: VendorInterface::Open", __func__);
lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
if (!lib_handle_) {
ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
dlerror());
return false;
}
initialize_complete_cb_ = initialize_complete_cb;
// Initialize vendor interface
lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
if (!lib_handle_) {
ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
dlerror());
return false;
}
lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
if (!lib_interface_) {
ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
return false;
}
lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
if (!lib_interface_) {
ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
return false;
}
// Get the local BD address
uint8_t local_bda[BluetoothAddress::kBytes] = {0, 0, 0, 0, 0, 0};
if (!BluetoothAddress::get_local_address(local_bda)) {
// BT driver will get BD address from NVRAM for MTK solution
ALOGW("%s: No pre-set Bluetooth Address!", __func__);
}
int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
if (status) {
ALOGE("%s unable to initialize vendor library: %d", __func__, status);
return false;
}
uint8_t local_bda[BluetoothAddress::kBytes];
if (!BluetoothAddress::get_local_address(local_bda)) {
LOG_ALWAYS_FATAL("%s: No Bluetooth Address!", __func__);
}
int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
if (status) {
ALOGE("%s unable to initialize vendor library: %d", __func__, status);
return false;
}
ALOGD("%s vendor library loaded", __func__);
ALOGD("%s vendor library loaded", __func__);
// Power on the controller
int power_state = BT_VND_PWR_ON;
lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
int power_state = BT_VND_PWR_ON;
lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
// Get the UART socket(s)
int fd_list[CH_MAX] = {0};
int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
int fd_list[CH_MAX] = {0};
int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
if (fd_count < 1 || fd_count > CH_MAX - 1) {
ALOGE("%s: fd_count %d is invalid!", __func__, fd_count);
if (fd_count < 1 || fd_count > CH_MAX - 1) {
ALOGE("%s: fd_count %d is invalid!", __func__, fd_count);
return false;
}
for (int i = 0; i < fd_count; i++) {
if (fd_list[i] == INVALID_FD) {
ALOGE("%s: fd %d is invalid!", __func__, fd_list[i]);
return false;
}
}
for (int i = 0; i < fd_count; i++) {
if (fd_list[i] == INVALID_FD) {
ALOGE("%s: fd %d is invalid!", __func__, fd_list[i]);
return false;
}
}
event_cb_ = event_cb;
PacketReadCallback intercept_events = [this](const hidl_vec<uint8_t>& event) {
HandleIncomingEvent(event);
};
event_cb_ = event_cb;
PacketReadCallback intercept_events = [this](const hidl_vec<uint8_t>& event) {
HandleIncomingEvent(event);
};
if (fd_count == 1) {
hci::H4Protocol* h4_hci =
new hci::H4Protocol(fd_list[0], intercept_events, acl_cb, sco_cb, iso_cb);
fd_watcher_.WatchFdForNonBlockingReads(
fd_list[0], [h4_hci](int fd) { h4_hci->OnDataReady(fd); });
hci_ = h4_hci;
} else {
hci::MctProtocol* mct_hci =
new hci::MctProtocol(fd_list, intercept_events, acl_cb);
fd_watcher_.WatchFdForNonBlockingReads(
fd_list[CH_EVT], [mct_hci](int fd) { mct_hci->OnEventDataReady(fd); });
fd_watcher_.WatchFdForNonBlockingReads(
fd_list[CH_ACL_IN],
[mct_hci](int fd) { mct_hci->OnAclDataReady(fd); });
hci_ = mct_hci;
}
if (fd_count == 1) {
hci::H4Protocol* h4_hci =
new hci::H4Protocol(fd_list[0], intercept_events, acl_cb, sco_cb, iso_cb);
fd_watcher_.WatchFdForNonBlockingReads(
fd_list[0], [h4_hci](int fd) { h4_hci->OnDataReady(fd); });
hci_ = h4_hci;
} else {
hci::MctProtocol* mct_hci =
new hci::MctProtocol(fd_list, intercept_events, acl_cb);
fd_watcher_.WatchFdForNonBlockingReads(
fd_list[CH_EVT], [mct_hci](int fd) { mct_hci->OnEventDataReady(fd); });
fd_watcher_.WatchFdForNonBlockingReads(
fd_list[CH_ACL_IN], [mct_hci](int fd) { mct_hci->OnAclDataReady(fd); });
hci_ = mct_hci;
}
// Initially, the power management is off.
lpm_wake_deasserted = true;
lpm_wake_deasserted = true;
// Start configuring the firmware
firmware_startup_timer_ = new FirmwareStartupTimer();
lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
firmware_startup_timer_ = new FirmwareStartupTimer();
lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
vstate = VENDOR_STATE_OPENED;
ALOGI("%s: VendorInterface::Open done!!!", __func__);
} // vendor_mutex_ done
return true;
}
void VendorInterface::Close() {
// These callbacks may send HCI events (vendor-dependent), so make sure to
// StopWatching the file descriptor after this.
if (vstate != VENDOR_STATE_OPENED) {
ALOGW("VendorInterface is not allow close(%d)", vstate);
return;
}
vstate = VENDOR_STATE_CLOSING;
ALOGI("%s: VendorInterface::Close", __func__);
if (lib_interface_ != nullptr) {
lib_interface_->cleanup();
bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE;
lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
}
{
std::unique_lock<std::mutex> guard(vendor_mutex_);
fd_watcher_.StopWatchingFileDescriptors();
fd_watcher_.StopWatchingFileDescriptors();
if (hci_ != nullptr) {
delete hci_;
hci_ = nullptr;
}
if (hci_ != nullptr) {
delete hci_;
hci_ = nullptr;
}
if (lib_interface_ != nullptr) {
lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
if (lib_interface_ != nullptr) {
lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
int power_state = BT_VND_PWR_OFF;
lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
int power_state = BT_VND_PWR_OFF;
lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
lib_interface_ = nullptr;
}
lib_interface_->cleanup();
lib_interface_ = nullptr;
}
if (lib_handle_ != nullptr) {
dlclose(lib_handle_);
lib_handle_ = nullptr;
}
if (lib_handle_ != nullptr) {
dlclose(lib_handle_);
lib_handle_ = nullptr;
}
if (firmware_startup_timer_ != nullptr) {
delete firmware_startup_timer_;
firmware_startup_timer_ = nullptr;
}
vstate = VENDOR_STATE_CLOSED;
} // vendor_mutex_ done
ALOGI("%s: VendorInterface::Close done!!!", __func__);
if (firmware_startup_timer_ != nullptr) {
delete firmware_startup_timer_;
firmware_startup_timer_ = nullptr;
}
}
size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
{
std::unique_lock<std::mutex> guard(vendor_mutex_);
std::unique_lock<std::mutex> lock(wakeup_mutex_);
recent_activity_flag = true;
if (vstate != VENDOR_STATE_OPENED) {
ALOGW("VendorInterface is not open yet(%d)!", vstate);
return 0;
}
ALOGI("%s: VendorInterface::Send", __func__);
if (lib_interface_ == nullptr) {
ALOGE("lib_interface_ is null");
return 0;
}
recent_activity_flag = true;
if (lpm_wake_deasserted == true) {
// Restart the timer.
fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
if (lpm_wake_deasserted == true) {
// Restart the timer.
fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
[this]() { OnTimeout(); });
// Assert wake.
lpm_wake_deasserted = false;
bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT;
lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
}
// Assert wake.
lpm_wake_deasserted = false;
bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT;
lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
}
return hci_ ? hci_->Send(type, data, length) : 0;
} // vendor_mutex_ done
return hci_->Send(type, data, length);
}
void VendorInterface::OnFirmwareConfigured(uint8_t result) {
@ -394,36 +339,25 @@ void VendorInterface::OnFirmwareConfigured(uint8_t result) {
firmware_startup_timer_ = nullptr;
}
{
std::unique_lock<std::mutex> guard(initcb_mutex_);
ALOGD("%s OnFirmwareConfigured get lock", __func__);
if (initialize_complete_cb_ != nullptr) {
LOG_ALWAYS_FATAL_IF((result != 0),
"%s: Failed to init firmware!", __func__);
initialize_complete_cb_(result == 0);
}
} // initcb_mutex_ done
if (lib_interface_ != nullptr) {
lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);
bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE;
lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
ALOGD("%s Calling StartLowPowerWatchdog()", __func__);
fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
[this]() { OnTimeout(); });
}
else {
ALOGE("lib_interface_ is null");
if (initialize_complete_cb_ != nullptr) {
initialize_complete_cb_(result == 0);
initialize_complete_cb_ = nullptr;
}
initialize_complete_cb_ = nullptr;
lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);
bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE;
lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
ALOGD("%s Calling StartLowPowerWatchdog()", __func__);
fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
[this]() { OnTimeout(); });
}
void VendorInterface::OnTimeout() {
ALOGV("%s", __func__);
std::unique_lock<std::mutex> lock(wakeup_mutex_);
if (recent_activity_flag == false) {
lpm_wake_deasserted = true;
bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT;

View file

@ -22,8 +22,6 @@
#include "bt_vendor_lib.h"
#include "hci_protocol.h"
extern std::mutex initcb_mutex_;
namespace android {
namespace hardware {
namespace bluetooth {
@ -47,9 +45,10 @@ class VendorInterface {
size_t Send(uint8_t type, const uint8_t* data, size_t length);
void OnFirmwareConfigured(uint8_t result);
virtual ~VendorInterface() = default;
private:
virtual ~VendorInterface() = default;
bool Open(InitializeCompleteCallback initialize_complete_cb,
PacketReadCallback event_cb, PacketReadCallback acl_cb,
PacketReadCallback sco_cb, PacketReadCallback iso_cb);