diff --git a/system/bta/Android.bp b/system/bta/Android.bp index 43e5648736ef7e65be500856024d486d5d909f5e..d8037fb7b5e2b01904d60bf794cf73f3b6bdfc39 100644 --- a/system/bta/Android.bp +++ b/system/bta/Android.bp @@ -257,6 +257,7 @@ cc_test { "sys/bta_sys_main.cc", "sys/utl.cc", "test/bta_av_test.cc", + "test/bta_api_test.cc", "test/bta_dm_test.cc", "test/bta_gatt_test.cc", "test/bta_pan_test.cc", diff --git a/system/bta/include/bta_api.h b/system/bta/include/bta_api.h index 0a70e978ffb1d07cea564e78d9848ab842266f3f..d8eaefb72dba43da28139f21a2e87cc105d91526 100644 --- a/system/bta/include/bta_api.h +++ b/system/bta/include/bta_api.h @@ -58,6 +58,29 @@ typedef enum : uint8_t { BTA_WRONG_MODE = 5, } tBTA_STATUS; +#ifndef CASE_RETURN_TEXT +#define CASE_RETURN_TEXT(code) \ + case code: \ + return #code +#endif + +inline std::string bta_status_text(const tBTA_STATUS& status) { + switch (status) { + CASE_RETURN_TEXT(BTA_SUCCESS); + CASE_RETURN_TEXT(BTA_FAILURE); + CASE_RETURN_TEXT(BTA_PENDING); + CASE_RETURN_TEXT(BTA_BUSY); + CASE_RETURN_TEXT(BTA_NO_RESOURCES); + CASE_RETURN_TEXT(BTA_WRONG_MODE); + default: + return base::StringPrintf("UNKNOWN[%d]", status); + } +} + +#undef CASE_RETURN_TEXT + +using tSDP_DISC_WAIT = int; + /* * Service ID */ diff --git a/system/bta/test/bta_api_test.cc b/system/bta/test/bta_api_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..1892d64f0e7505041406c18ae0e1711acbc9e767 --- /dev/null +++ b/system/bta/test/bta_api_test.cc @@ -0,0 +1,69 @@ +/* + * Copyright 2022 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. + */ + +#include "bta/include/bta_api.h" + +#include <base/bind.h> +#include <base/location.h> +#include <gtest/gtest.h> + +#include <chrono> +#include <cstdint> +#include <utility> +#include <vector> + +#include "bta/sys/bta_sys.h" +#include "test/common/mock_functions.h" + +using namespace std::chrono_literals; + +namespace { + +const char* test_flags[] = { + "INIT_logging_debug_enabled_for_all=true", + nullptr, +}; + +} // namespace + +class BtaApiTest : public testing::Test { + protected: + void SetUp() override { + mock_function_count_map.clear(); + bluetooth::common::InitFlags::Load(test_flags); + } + void TearDown() override {} +}; + +TEST_F(BtaApiTest, bta_status_text) { + std::vector<std::pair<tBTA_STATUS, std::string>> statuses = { + std::make_pair(BTA_SUCCESS, "BTA_SUCCESS"), + std::make_pair(BTA_FAILURE, "BTA_FAILURE"), + std::make_pair(BTA_PENDING, "BTA_PENDING"), + std::make_pair(BTA_BUSY, "BTA_BUSY"), + std::make_pair(BTA_NO_RESOURCES, "BTA_NO_RESOURCES"), + std::make_pair(BTA_WRONG_MODE, "BTA_WRONG_MODE"), + }; + for (const auto& status : statuses) { + ASSERT_STREQ(status.second.c_str(), bta_status_text(status.first).c_str()); + } + auto unknown = + base::StringPrintf("UNKNOWN[%d]", std::numeric_limits<uint8_t>::max()); + ASSERT_STREQ(unknown.c_str(), + bta_status_text(static_cast<tBTA_STATUS>( + std::numeric_limits<uint8_t>::max())) + .c_str()); +}