1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <aidl/Gtest.h>
18 #include <aidl/Vintf.h>
19 #include <aidl/android/hardware/bluetooth/ranging/BnBluetoothChannelSoundingSessionCallback.h>
20 #include <aidl/android/hardware/bluetooth/ranging/IBluetoothChannelSounding.h>
21 #include <aidl/android/hardware/bluetooth/ranging/IBluetoothChannelSoundingSessionCallback.h>
22 #include <android-base/logging.h>
23 #include <android/binder_manager.h>
24 #include <android/binder_process.h>
25 #include <binder/IServiceManager.h>
26 #include <utils/Log.h>
27
28 using aidl::android::hardware::bluetooth::ranging::
29 BluetoothChannelSoundingParameters;
30 using aidl::android::hardware::bluetooth::ranging::
31 BnBluetoothChannelSoundingSessionCallback;
32 using aidl::android::hardware::bluetooth::ranging::ChannelSoudingRawData;
33 using aidl::android::hardware::bluetooth::ranging::ChannelSoundingProcedureData;
34 using aidl::android::hardware::bluetooth::ranging::Config;
35 using aidl::android::hardware::bluetooth::ranging::CsSecurityLevel;
36 using aidl::android::hardware::bluetooth::ranging::IBluetoothChannelSounding;
37 using aidl::android::hardware::bluetooth::ranging::
38 IBluetoothChannelSoundingSession;
39 using aidl::android::hardware::bluetooth::ranging::
40 IBluetoothChannelSoundingSessionCallback;
41 using aidl::android::hardware::bluetooth::ranging::ProcedureEnableConfig;
42 using aidl::android::hardware::bluetooth::ranging::RangingResult;
43 using aidl::android::hardware::bluetooth::ranging::Reason;
44 using aidl::android::hardware::bluetooth::ranging::ResultType;
45 using aidl::android::hardware::bluetooth::ranging::SessionType;
46 using aidl::android::hardware::bluetooth::ranging::VendorSpecificData;
47 using ndk::ScopedAStatus;
48
49 enum class RangingHalVersion : uint8_t {
50 V_UNAVAILABLE = 0,
51 V_1,
52 V_2,
53 };
54
55 class BluetoothChannelSoundingSessionCallback
56 : public BnBluetoothChannelSoundingSessionCallback {
57 public:
58 ScopedAStatus onOpened(Reason reason) override;
59 ScopedAStatus onOpenFailed(Reason reason) override;
60 ScopedAStatus onResult(const RangingResult& in_result) override;
61 ScopedAStatus onClose(Reason reason) override;
62 ScopedAStatus onCloseFailed(Reason reason) override;
63 };
64
onOpened(Reason)65 ScopedAStatus BluetoothChannelSoundingSessionCallback::onOpened(
66 Reason /*reason*/) {
67 return ::ndk::ScopedAStatus::ok();
68 }
onOpenFailed(Reason)69 ScopedAStatus BluetoothChannelSoundingSessionCallback::onOpenFailed(
70 Reason /*reason*/) {
71 return ::ndk::ScopedAStatus::ok();
72 }
onResult(const RangingResult &)73 ScopedAStatus BluetoothChannelSoundingSessionCallback::onResult(
74 const RangingResult& /*in_result*/) {
75 return ::ndk::ScopedAStatus::ok();
76 }
onClose(Reason)77 ScopedAStatus BluetoothChannelSoundingSessionCallback::onClose(
78 Reason /*reason*/) {
79 return ::ndk::ScopedAStatus::ok();
80 }
onCloseFailed(Reason)81 ScopedAStatus BluetoothChannelSoundingSessionCallback::onCloseFailed(
82 Reason /*reason*/) {
83 return ::ndk::ScopedAStatus::ok();
84 }
85
86 class BluetoothRangingTest : public ::testing::TestWithParam<std::string> {
87 public:
SetUp()88 virtual void SetUp() override {
89 ALOGI("SetUp Ranging Test");
90 bluetooth_channel_sounding_ = IBluetoothChannelSounding::fromBinder(
91 ndk::SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
92 hal_version_ = GetRangingHalVersion();
93 ASSERT_GT(hal_version_, RangingHalVersion::V_UNAVAILABLE);
94 ASSERT_NE(bluetooth_channel_sounding_, nullptr);
95 }
96
TearDown()97 virtual void TearDown() override {
98 ALOGI("TearDown Ranging Test");
99 bluetooth_channel_sounding_ = nullptr;
100 ASSERT_EQ(bluetooth_channel_sounding_, nullptr);
101 }
102
103 ScopedAStatus getVendorSpecificData(
104 std::optional<std::vector<std::optional<VendorSpecificData>>>*
105 _aidl_return);
106 ScopedAStatus getSupportedSessionTypes(
107 std::optional<std::vector<SessionType>>* _aidl_return);
108 ScopedAStatus getMaxSupportedCsSecurityLevel(CsSecurityLevel* _aidl_return);
109 ScopedAStatus getSupportedCsSecurityLevels(
110 std::vector<CsSecurityLevel>* _aidl_return);
111 ScopedAStatus openSession(
112 const BluetoothChannelSoundingParameters& in_params,
113 const std::shared_ptr<IBluetoothChannelSoundingSessionCallback>&
114 in_callback,
115 std::shared_ptr<IBluetoothChannelSoundingSession>* _aidl_return);
116
initBluetoothChannelSoundingSession(std::shared_ptr<IBluetoothChannelSoundingSession> * session)117 ScopedAStatus initBluetoothChannelSoundingSession(
118 std::shared_ptr<IBluetoothChannelSoundingSession>* session) {
119 BluetoothChannelSoundingParameters params;
120 std::shared_ptr<BluetoothChannelSoundingSessionCallback> callback = nullptr;
121 callback =
122 ndk::SharedRefBase::make<BluetoothChannelSoundingSessionCallback>();
123 ScopedAStatus status = openSession(params, callback, session);
124 return status;
125 }
126
GetRangingHalVersion()127 RangingHalVersion GetRangingHalVersion() {
128 int32_t aidl_version = 0;
129 if (bluetooth_channel_sounding_ == nullptr) {
130 return RangingHalVersion::V_UNAVAILABLE;
131 }
132 auto aidl_ret_val =
133 bluetooth_channel_sounding_->getInterfaceVersion(&aidl_version);
134 if (!aidl_ret_val.isOk()) {
135 return RangingHalVersion::V_UNAVAILABLE;
136 }
137 switch (aidl_version) {
138 case 1:
139 return RangingHalVersion::V_1;
140 case 2:
141 return RangingHalVersion::V_2;
142 default:
143 return RangingHalVersion::V_UNAVAILABLE;
144 }
145 return RangingHalVersion::V_UNAVAILABLE;
146 }
147
148 public:
149 RangingHalVersion hal_version_ = RangingHalVersion::V_UNAVAILABLE;
150
151 private:
152 std::shared_ptr<IBluetoothChannelSounding> bluetooth_channel_sounding_;
153 };
154
getVendorSpecificData(std::optional<std::vector<std::optional<VendorSpecificData>>> * _aidl_return)155 ScopedAStatus BluetoothRangingTest::getVendorSpecificData(
156 std::optional<std::vector<std::optional<VendorSpecificData>>>*
157 _aidl_return) {
158 return bluetooth_channel_sounding_->getVendorSpecificData(_aidl_return);
159 }
getSupportedSessionTypes(std::optional<std::vector<SessionType>> * _aidl_return)160 ScopedAStatus BluetoothRangingTest::getSupportedSessionTypes(
161 std::optional<std::vector<SessionType>>* _aidl_return) {
162 return bluetooth_channel_sounding_->getSupportedSessionTypes(_aidl_return);
163 }
164
getMaxSupportedCsSecurityLevel(CsSecurityLevel * _aidl_return)165 ScopedAStatus BluetoothRangingTest::getMaxSupportedCsSecurityLevel(
166 CsSecurityLevel* _aidl_return) {
167 return bluetooth_channel_sounding_->getMaxSupportedCsSecurityLevel(
168 _aidl_return);
169 }
170
getSupportedCsSecurityLevels(std::vector<CsSecurityLevel> * _aidl_return)171 ScopedAStatus BluetoothRangingTest::getSupportedCsSecurityLevels(
172 std::vector<CsSecurityLevel>* _aidl_return) {
173 return bluetooth_channel_sounding_->getSupportedCsSecurityLevels(
174 _aidl_return);
175 }
176
openSession(const BluetoothChannelSoundingParameters & in_params,const std::shared_ptr<IBluetoothChannelSoundingSessionCallback> & in_callback,std::shared_ptr<IBluetoothChannelSoundingSession> * _aidl_return)177 ScopedAStatus BluetoothRangingTest::openSession(
178 const BluetoothChannelSoundingParameters& in_params,
179 const std::shared_ptr<IBluetoothChannelSoundingSessionCallback>&
180 in_callback,
181 std::shared_ptr<IBluetoothChannelSoundingSession>* _aidl_return) {
182 return bluetooth_channel_sounding_->openSession(in_params, in_callback,
183 _aidl_return);
184 }
185
TEST_P(BluetoothRangingTest,SetupAndTearDown)186 TEST_P(BluetoothRangingTest, SetupAndTearDown) {}
187
TEST_P(BluetoothRangingTest,GetVendorSpecificData)188 TEST_P(BluetoothRangingTest, GetVendorSpecificData) {
189 std::optional<std::vector<std::optional<VendorSpecificData>>>
190 vendor_specific_data;
191 ScopedAStatus status = getVendorSpecificData(&vendor_specific_data);
192 ASSERT_TRUE(status.isOk());
193 }
194
TEST_P(BluetoothRangingTest,GetSupportedSessionTypes)195 TEST_P(BluetoothRangingTest, GetSupportedSessionTypes) {
196 std::optional<std::vector<SessionType>> supported_session_types;
197 ScopedAStatus status = getSupportedSessionTypes(&supported_session_types);
198 ASSERT_TRUE(status.isOk());
199 }
200
TEST_P(BluetoothRangingTest,GetMaxSupportedCsSecurityLevel)201 TEST_P(BluetoothRangingTest, GetMaxSupportedCsSecurityLevel) {
202 if (hal_version_ > RangingHalVersion::V_1) {
203 GTEST_SKIP();
204 }
205 CsSecurityLevel security_level;
206 ScopedAStatus status = getMaxSupportedCsSecurityLevel(&security_level);
207 ASSERT_TRUE(status.isOk());
208 }
209
TEST_P(BluetoothRangingTest,GetSupportedCsSecurityLevels)210 TEST_P(BluetoothRangingTest, GetSupportedCsSecurityLevels) {
211 if (hal_version_ < RangingHalVersion::V_2) {
212 GTEST_SKIP();
213 }
214 std::vector<CsSecurityLevel> supported_security_levels;
215 ScopedAStatus status =
216 getSupportedCsSecurityLevels(&supported_security_levels);
217 ASSERT_GT(static_cast<uint8_t>(supported_security_levels.size()), 0);
218 ASSERT_TRUE(status.isOk());
219 }
220
TEST_P(BluetoothRangingTest,OpenSession)221 TEST_P(BluetoothRangingTest, OpenSession) {
222 BluetoothChannelSoundingParameters params;
223 std::shared_ptr<BluetoothChannelSoundingSessionCallback> callback = nullptr;
224 callback =
225 ndk::SharedRefBase::make<BluetoothChannelSoundingSessionCallback>();
226 std::shared_ptr<IBluetoothChannelSoundingSession> session;
227 ScopedAStatus status = openSession(params, callback, &session);
228 ASSERT_TRUE(status.isOk());
229 }
230
TEST_P(BluetoothRangingTest,GetVendorSpecificReplies)231 TEST_P(BluetoothRangingTest, GetVendorSpecificReplies) {
232 std::shared_ptr<IBluetoothChannelSoundingSession> session;
233 auto status = initBluetoothChannelSoundingSession(&session);
234 ASSERT_TRUE(status.isOk());
235 if (session != nullptr) {
236 std::optional<std::vector<std::optional<VendorSpecificData>>>
237 vendor_specific_data;
238 status = session->getVendorSpecificReplies(&vendor_specific_data);
239 ASSERT_TRUE(status.isOk());
240 }
241 }
242
TEST_P(BluetoothRangingTest,GetSupportedResultTypes)243 TEST_P(BluetoothRangingTest, GetSupportedResultTypes) {
244 std::shared_ptr<IBluetoothChannelSoundingSession> session;
245 auto status = initBluetoothChannelSoundingSession(&session);
246 ASSERT_TRUE(status.isOk());
247 if (session != nullptr) {
248 std::vector<ResultType> supported_result_types;
249 status = session->getSupportedResultTypes(&supported_result_types);
250 ASSERT_TRUE(status.isOk());
251 }
252 }
253
TEST_P(BluetoothRangingTest,IsAbortedProcedureRequired)254 TEST_P(BluetoothRangingTest, IsAbortedProcedureRequired) {
255 std::shared_ptr<IBluetoothChannelSoundingSession> session;
256 auto status = initBluetoothChannelSoundingSession(&session);
257 ASSERT_TRUE(status.isOk());
258 if (session != nullptr) {
259 bool is_abort_procedure_required = true;
260 status = session->isAbortedProcedureRequired(&is_abort_procedure_required);
261 ASSERT_TRUE(status.isOk());
262 }
263 }
264
TEST_P(BluetoothRangingTest,WriteRawData)265 TEST_P(BluetoothRangingTest, WriteRawData) {
266 if (hal_version_ > RangingHalVersion::V_1) {
267 GTEST_SKIP();
268 }
269 std::shared_ptr<IBluetoothChannelSoundingSession> session;
270 auto status = initBluetoothChannelSoundingSession(&session);
271 ASSERT_TRUE(status.isOk());
272 if (session != nullptr) {
273 ChannelSoudingRawData raw_data;
274 status = session->writeRawData(raw_data);
275 ASSERT_TRUE(status.isOk());
276 }
277 }
278
TEST_P(BluetoothRangingTest,WriteProcedureData)279 TEST_P(BluetoothRangingTest, WriteProcedureData) {
280 if (hal_version_ < RangingHalVersion::V_2) {
281 GTEST_SKIP();
282 }
283 std::shared_ptr<IBluetoothChannelSoundingSession> session;
284 auto status = initBluetoothChannelSoundingSession(&session);
285 ASSERT_TRUE(status.isOk());
286 if (session != nullptr) {
287 ChannelSoundingProcedureData procedure_data;
288 status = session->writeProcedureData(procedure_data);
289 ASSERT_TRUE(status.isOk());
290 }
291 }
292
TEST_P(BluetoothRangingTest,UpdateChannelSoundingConfig)293 TEST_P(BluetoothRangingTest, UpdateChannelSoundingConfig) {
294 if (hal_version_ < RangingHalVersion::V_2) {
295 GTEST_SKIP();
296 }
297 std::shared_ptr<IBluetoothChannelSoundingSession> session;
298 auto status = initBluetoothChannelSoundingSession(&session);
299 ASSERT_TRUE(status.isOk());
300 if (session != nullptr) {
301 Config config;
302 status = session->updateChannelSoundingConfig(config);
303 ASSERT_TRUE(status.isOk());
304 }
305 }
306
TEST_P(BluetoothRangingTest,UpdateProcedureEnableConfig)307 TEST_P(BluetoothRangingTest, UpdateProcedureEnableConfig) {
308 if (hal_version_ < RangingHalVersion::V_2) {
309 GTEST_SKIP();
310 }
311 std::shared_ptr<IBluetoothChannelSoundingSession> session;
312 auto status = initBluetoothChannelSoundingSession(&session);
313 ASSERT_TRUE(status.isOk());
314 if (session != nullptr) {
315 ProcedureEnableConfig procedure_enable_config;
316 status = session->updateProcedureEnableConfig(procedure_enable_config);
317 ASSERT_TRUE(status.isOk());
318 }
319 }
320
TEST_P(BluetoothRangingTest,UpdateBleConnInterval)321 TEST_P(BluetoothRangingTest, UpdateBleConnInterval) {
322 if (hal_version_ < RangingHalVersion::V_2) {
323 GTEST_SKIP();
324 }
325 std::shared_ptr<IBluetoothChannelSoundingSession> session;
326 auto status = initBluetoothChannelSoundingSession(&session);
327 ASSERT_TRUE(status.isOk());
328 if (session != nullptr) {
329 int ble_conn_interval = 10;
330 status = session->updateBleConnInterval(ble_conn_interval);
331 ASSERT_TRUE(status.isOk());
332 }
333 }
334
TEST_P(BluetoothRangingTest,CloseSession)335 TEST_P(BluetoothRangingTest, CloseSession) {
336 std::shared_ptr<IBluetoothChannelSoundingSession> session;
337 auto status = initBluetoothChannelSoundingSession(&session);
338 ASSERT_TRUE(status.isOk());
339 if (session != nullptr) {
340 status = session->close(Reason::LOCAL_STACK_REQUEST);
341 ASSERT_TRUE(status.isOk());
342 }
343 }
344
345 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothRangingTest);
346 INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothRangingTest,
347 testing::ValuesIn(android::getAidlHalInstanceNames(
348 IBluetoothChannelSounding::descriptor)),
349 android::PrintInstanceNameToString);
350
main(int argc,char ** argv)351 int main(int argc, char** argv) {
352 ::testing::InitGoogleTest(&argc, argv);
353 ABinderProcess_startThreadPool();
354 int status = RUN_ALL_TESTS();
355 ALOGI("Test result = %d", status);
356 return status;
357 }