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::CsSecurityLevel;
34 using aidl::android::hardware::bluetooth::ranging::IBluetoothChannelSounding;
35 using aidl::android::hardware::bluetooth::ranging::
36     IBluetoothChannelSoundingSession;
37 using aidl::android::hardware::bluetooth::ranging::
38     IBluetoothChannelSoundingSessionCallback;
39 using aidl::android::hardware::bluetooth::ranging::RangingResult;
40 using aidl::android::hardware::bluetooth::ranging::Reason;
41 using aidl::android::hardware::bluetooth::ranging::ResultType;
42 using aidl::android::hardware::bluetooth::ranging::SessionType;
43 using aidl::android::hardware::bluetooth::ranging::VendorSpecificData;
44 using ndk::ScopedAStatus;
45 
46 class BluetoothChannelSoundingSessionCallback
47     : public BnBluetoothChannelSoundingSessionCallback {
48  public:
49   ScopedAStatus onOpened(Reason reason) override;
50   ScopedAStatus onOpenFailed(Reason reason) override;
51   ScopedAStatus onResult(const RangingResult& in_result) override;
52   ScopedAStatus onClose(Reason reason) override;
53   ScopedAStatus onCloseFailed(Reason reason) override;
54 };
55 
onOpened(Reason)56 ScopedAStatus BluetoothChannelSoundingSessionCallback::onOpened(
57     Reason /*reason*/) {
58   return ::ndk::ScopedAStatus::ok();
59 }
onOpenFailed(Reason)60 ScopedAStatus BluetoothChannelSoundingSessionCallback::onOpenFailed(
61     Reason /*reason*/) {
62   return ::ndk::ScopedAStatus::ok();
63 }
onResult(const RangingResult &)64 ScopedAStatus BluetoothChannelSoundingSessionCallback::onResult(
65     const RangingResult& /*in_result*/) {
66   return ::ndk::ScopedAStatus::ok();
67 }
onClose(Reason)68 ScopedAStatus BluetoothChannelSoundingSessionCallback::onClose(
69     Reason /*reason*/) {
70   return ::ndk::ScopedAStatus::ok();
71 }
onCloseFailed(Reason)72 ScopedAStatus BluetoothChannelSoundingSessionCallback::onCloseFailed(
73     Reason /*reason*/) {
74   return ::ndk::ScopedAStatus::ok();
75 }
76 
77 class BluetoothRangingTest : public ::testing::TestWithParam<std::string> {
78  public:
SetUp()79   virtual void SetUp() override {
80     ALOGI("SetUp Ranging Test");
81     bluetooth_channel_sounding_ = IBluetoothChannelSounding::fromBinder(
82         ndk::SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
83     ASSERT_NE(bluetooth_channel_sounding_, nullptr);
84   }
85 
TearDown()86   virtual void TearDown() override {
87     ALOGI("TearDown Ranging Test");
88     bluetooth_channel_sounding_ = nullptr;
89     ASSERT_EQ(bluetooth_channel_sounding_, nullptr);
90   }
91 
92   ScopedAStatus getVendorSpecificData(
93       std::optional<std::vector<std::optional<VendorSpecificData>>>*
94           _aidl_return);
95   ScopedAStatus getSupportedSessionTypes(
96       std::optional<std::vector<SessionType>>* _aidl_return);
97   ScopedAStatus getMaxSupportedCsSecurityLevel(CsSecurityLevel* _aidl_return);
98   ScopedAStatus openSession(
99       const BluetoothChannelSoundingParameters& in_params,
100       const std::shared_ptr<IBluetoothChannelSoundingSessionCallback>&
101           in_callback,
102       std::shared_ptr<IBluetoothChannelSoundingSession>* _aidl_return);
103 
initBluetoothChannelSoundingSession(std::shared_ptr<IBluetoothChannelSoundingSession> * session)104   ScopedAStatus initBluetoothChannelSoundingSession(
105       std::shared_ptr<IBluetoothChannelSoundingSession>* session) {
106     BluetoothChannelSoundingParameters params;
107     std::shared_ptr<BluetoothChannelSoundingSessionCallback> callback = nullptr;
108     callback =
109         ndk::SharedRefBase::make<BluetoothChannelSoundingSessionCallback>();
110     ScopedAStatus status = openSession(params, callback, session);
111     return status;
112   }
113 
114  private:
115   std::shared_ptr<IBluetoothChannelSounding> bluetooth_channel_sounding_;
116 };
117 
getVendorSpecificData(std::optional<std::vector<std::optional<VendorSpecificData>>> * _aidl_return)118 ScopedAStatus BluetoothRangingTest::getVendorSpecificData(
119     std::optional<std::vector<std::optional<VendorSpecificData>>>*
120         _aidl_return) {
121   return bluetooth_channel_sounding_->getVendorSpecificData(_aidl_return);
122 }
getSupportedSessionTypes(std::optional<std::vector<SessionType>> * _aidl_return)123 ScopedAStatus BluetoothRangingTest::getSupportedSessionTypes(
124     std::optional<std::vector<SessionType>>* _aidl_return) {
125   return bluetooth_channel_sounding_->getSupportedSessionTypes(_aidl_return);
126 }
127 
getMaxSupportedCsSecurityLevel(CsSecurityLevel * _aidl_return)128 ScopedAStatus BluetoothRangingTest::getMaxSupportedCsSecurityLevel(
129     CsSecurityLevel* _aidl_return) {
130   return bluetooth_channel_sounding_->getMaxSupportedCsSecurityLevel(
131       _aidl_return);
132 }
openSession(const BluetoothChannelSoundingParameters & in_params,const std::shared_ptr<IBluetoothChannelSoundingSessionCallback> & in_callback,std::shared_ptr<IBluetoothChannelSoundingSession> * _aidl_return)133 ScopedAStatus BluetoothRangingTest::openSession(
134     const BluetoothChannelSoundingParameters& in_params,
135     const std::shared_ptr<IBluetoothChannelSoundingSessionCallback>&
136         in_callback,
137     std::shared_ptr<IBluetoothChannelSoundingSession>* _aidl_return) {
138   return bluetooth_channel_sounding_->openSession(in_params, in_callback,
139                                                   _aidl_return);
140 }
141 
TEST_P(BluetoothRangingTest,SetupAndTearDown)142 TEST_P(BluetoothRangingTest, SetupAndTearDown) {}
143 
TEST_P(BluetoothRangingTest,GetVendorSpecificData)144 TEST_P(BluetoothRangingTest, GetVendorSpecificData) {
145   std::optional<std::vector<std::optional<VendorSpecificData>>>
146       vendor_specific_data;
147   ScopedAStatus status = getVendorSpecificData(&vendor_specific_data);
148   ASSERT_TRUE(status.isOk());
149 }
150 
TEST_P(BluetoothRangingTest,GetSupportedSessionTypes)151 TEST_P(BluetoothRangingTest, GetSupportedSessionTypes) {
152   std::optional<std::vector<SessionType>> supported_session_types;
153   ScopedAStatus status = getSupportedSessionTypes(&supported_session_types);
154   ASSERT_TRUE(status.isOk());
155 }
156 
TEST_P(BluetoothRangingTest,GetMaxSupportedCsSecurityLevel)157 TEST_P(BluetoothRangingTest, GetMaxSupportedCsSecurityLevel) {
158   CsSecurityLevel security_level;
159   ScopedAStatus status = getMaxSupportedCsSecurityLevel(&security_level);
160   ASSERT_TRUE(status.isOk());
161 }
162 
TEST_P(BluetoothRangingTest,OpenSession)163 TEST_P(BluetoothRangingTest, OpenSession) {
164   BluetoothChannelSoundingParameters params;
165   std::shared_ptr<BluetoothChannelSoundingSessionCallback> callback = nullptr;
166   callback =
167       ndk::SharedRefBase::make<BluetoothChannelSoundingSessionCallback>();
168   std::shared_ptr<IBluetoothChannelSoundingSession> session;
169   ScopedAStatus status = openSession(params, callback, &session);
170   ASSERT_TRUE(status.isOk());
171 }
172 
TEST_P(BluetoothRangingTest,GetVendorSpecificReplies)173 TEST_P(BluetoothRangingTest, GetVendorSpecificReplies) {
174   std::shared_ptr<IBluetoothChannelSoundingSession> session;
175   auto status = initBluetoothChannelSoundingSession(&session);
176   ASSERT_TRUE(status.isOk());
177   if (session != nullptr) {
178     std::optional<std::vector<std::optional<VendorSpecificData>>>
179         vendor_specific_data;
180     status = session->getVendorSpecificReplies(&vendor_specific_data);
181     ASSERT_TRUE(status.isOk());
182   }
183 }
184 
TEST_P(BluetoothRangingTest,GetSupportedResultTypes)185 TEST_P(BluetoothRangingTest, GetSupportedResultTypes) {
186   std::shared_ptr<IBluetoothChannelSoundingSession> session;
187   auto status = initBluetoothChannelSoundingSession(&session);
188   ASSERT_TRUE(status.isOk());
189   if (session != nullptr) {
190     std::vector<ResultType> supported_result_types;
191     status = session->getSupportedResultTypes(&supported_result_types);
192     ASSERT_TRUE(status.isOk());
193   }
194 }
195 
TEST_P(BluetoothRangingTest,IsAbortedProcedureRequired)196 TEST_P(BluetoothRangingTest, IsAbortedProcedureRequired) {
197   std::shared_ptr<IBluetoothChannelSoundingSession> session;
198   auto status = initBluetoothChannelSoundingSession(&session);
199   ASSERT_TRUE(status.isOk());
200   if (session != nullptr) {
201     bool is_abort_procedure_required = true;
202     status = session->isAbortedProcedureRequired(&is_abort_procedure_required);
203     ASSERT_TRUE(status.isOk());
204   }
205 }
206 
TEST_P(BluetoothRangingTest,WriteRawData)207 TEST_P(BluetoothRangingTest, WriteRawData) {
208   std::shared_ptr<IBluetoothChannelSoundingSession> session;
209   auto status = initBluetoothChannelSoundingSession(&session);
210   ASSERT_TRUE(status.isOk());
211   if (session != nullptr) {
212     ChannelSoudingRawData raw_data;
213     status = session->writeRawData(raw_data);
214     ASSERT_TRUE(status.isOk());
215   }
216 }
217 
TEST_P(BluetoothRangingTest,CloseSession)218 TEST_P(BluetoothRangingTest, CloseSession) {
219   std::shared_ptr<IBluetoothChannelSoundingSession> session;
220   auto status = initBluetoothChannelSoundingSession(&session);
221   ASSERT_TRUE(status.isOk());
222   if (session != nullptr) {
223     status = session->close(Reason::LOCAL_STACK_REQUEST);
224     ASSERT_TRUE(status.isOk());
225   }
226 }
227 
228 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothRangingTest);
229 INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothRangingTest,
230                          testing::ValuesIn(android::getAidlHalInstanceNames(
231                              IBluetoothChannelSounding::descriptor)),
232                          android::PrintInstanceNameToString);
233 
main(int argc,char ** argv)234 int main(int argc, char** argv) {
235   ::testing::InitGoogleTest(&argc, argv);
236   ABinderProcess_startThreadPool();
237   int status = RUN_ALL_TESTS();
238   ALOGI("Test result = %d", status);
239   return status;
240 }