1 /*
2 * Copyright (C) 2022 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 "wifi_rtt_controller.h"
18
19 #include <android-base/logging.h>
20
21 #include "aidl_return_util.h"
22 #include "aidl_struct_util.h"
23 #include "wifi_status_util.h"
24
25 namespace aidl {
26 namespace android {
27 namespace hardware {
28 namespace wifi {
29 using aidl_return_util::validateAndCall;
30
WifiRttController(const std::string & iface_name,const std::shared_ptr<IWifiStaIface> & bound_iface,const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal)31 WifiRttController::WifiRttController(const std::string& iface_name,
32 const std::shared_ptr<IWifiStaIface>& bound_iface,
33 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal)
34 : ifname_(iface_name), bound_iface_(bound_iface), legacy_hal_(legacy_hal), is_valid_(true) {}
35
create(const std::string & iface_name,const std::shared_ptr<IWifiStaIface> & bound_iface,const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal)36 std::shared_ptr<WifiRttController> WifiRttController::create(
37 const std::string& iface_name, const std::shared_ptr<IWifiStaIface>& bound_iface,
38 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal) {
39 std::shared_ptr<WifiRttController> ptr =
40 ndk::SharedRefBase::make<WifiRttController>(iface_name, bound_iface, legacy_hal);
41 std::weak_ptr<WifiRttController> weak_ptr_this(ptr);
42 ptr->setWeakPtr(weak_ptr_this);
43 return ptr;
44 }
45
invalidate()46 void WifiRttController::invalidate() {
47 legacy_hal_.reset();
48 event_callbacks_.clear();
49 is_valid_ = false;
50 };
51
isValid()52 bool WifiRttController::isValid() {
53 return is_valid_;
54 }
55
setWeakPtr(std::weak_ptr<WifiRttController> ptr)56 void WifiRttController::setWeakPtr(std::weak_ptr<WifiRttController> ptr) {
57 weak_ptr_this_ = ptr;
58 }
59
60 std::vector<std::shared_ptr<IWifiRttControllerEventCallback>>
getEventCallbacks()61 WifiRttController::getEventCallbacks() {
62 return event_callbacks_;
63 }
64
getIfaceName()65 std::string WifiRttController::getIfaceName() {
66 return ifname_;
67 }
68
getBoundIface(std::shared_ptr<IWifiStaIface> * _aidl_return)69 ndk::ScopedAStatus WifiRttController::getBoundIface(std::shared_ptr<IWifiStaIface>* _aidl_return) {
70 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
71 &WifiRttController::getBoundIfaceInternal, _aidl_return);
72 }
73
registerEventCallback(const std::shared_ptr<IWifiRttControllerEventCallback> & callback)74 ndk::ScopedAStatus WifiRttController::registerEventCallback(
75 const std::shared_ptr<IWifiRttControllerEventCallback>& callback) {
76 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
77 &WifiRttController::registerEventCallbackInternal, callback);
78 }
79
rangeRequest(int32_t in_cmdId,const std::vector<RttConfig> & in_rttConfigs)80 ndk::ScopedAStatus WifiRttController::rangeRequest(int32_t in_cmdId,
81 const std::vector<RttConfig>& in_rttConfigs) {
82 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
83 &WifiRttController::rangeRequestInternal, in_cmdId, in_rttConfigs);
84 }
85
rangeCancel(int32_t in_cmdId,const std::vector<MacAddress> & in_addrs)86 ndk::ScopedAStatus WifiRttController::rangeCancel(int32_t in_cmdId,
87 const std::vector<MacAddress>& in_addrs) {
88 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
89 &WifiRttController::rangeCancelInternal, in_cmdId, in_addrs);
90 }
91
getCapabilities(RttCapabilities * _aidl_return)92 ndk::ScopedAStatus WifiRttController::getCapabilities(RttCapabilities* _aidl_return) {
93 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
94 &WifiRttController::getCapabilitiesInternal, _aidl_return);
95 }
96
setLci(int32_t in_cmdId,const RttLciInformation & in_lci)97 ndk::ScopedAStatus WifiRttController::setLci(int32_t in_cmdId, const RttLciInformation& in_lci) {
98 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
99 &WifiRttController::setLciInternal, in_cmdId, in_lci);
100 }
101
setLcr(int32_t in_cmdId,const RttLcrInformation & in_lcr)102 ndk::ScopedAStatus WifiRttController::setLcr(int32_t in_cmdId, const RttLcrInformation& in_lcr) {
103 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
104 &WifiRttController::setLcrInternal, in_cmdId, in_lcr);
105 }
106
getResponderInfo(RttResponder * _aidl_return)107 ndk::ScopedAStatus WifiRttController::getResponderInfo(RttResponder* _aidl_return) {
108 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
109 &WifiRttController::getResponderInfoInternal, _aidl_return);
110 }
111
enableResponder(int32_t in_cmdId,const WifiChannelInfo & in_channelHint,int32_t in_maxDurationInSeconds,const RttResponder & in_info)112 ndk::ScopedAStatus WifiRttController::enableResponder(int32_t in_cmdId,
113 const WifiChannelInfo& in_channelHint,
114 int32_t in_maxDurationInSeconds,
115 const RttResponder& in_info) {
116 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
117 &WifiRttController::enableResponderInternal, in_cmdId, in_channelHint,
118 in_maxDurationInSeconds, in_info);
119 }
120
disableResponder(int32_t in_cmdId)121 ndk::ScopedAStatus WifiRttController::disableResponder(int32_t in_cmdId) {
122 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
123 &WifiRttController::disableResponderInternal, in_cmdId);
124 }
125
126 std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus>
getBoundIfaceInternal()127 WifiRttController::getBoundIfaceInternal() {
128 return {bound_iface_, ndk::ScopedAStatus::ok()};
129 }
130
registerEventCallbackInternal(const std::shared_ptr<IWifiRttControllerEventCallback> & callback)131 ndk::ScopedAStatus WifiRttController::registerEventCallbackInternal(
132 const std::shared_ptr<IWifiRttControllerEventCallback>& callback) {
133 event_callbacks_.emplace_back(callback);
134 return ndk::ScopedAStatus::ok();
135 }
136
rangeRequestInternal(int32_t cmd_id,const std::vector<RttConfig> & rtt_configs)137 ndk::ScopedAStatus WifiRttController::rangeRequestInternal(
138 int32_t cmd_id, const std::vector<RttConfig>& rtt_configs) {
139 // Try 11mc & 11az ranging (v3)
140 std::vector<legacy_hal::wifi_rtt_config_v3> legacy_configs_v3;
141 if (!aidl_struct_util::convertAidlVectorOfRttConfigToLegacyV3(rtt_configs,
142 &legacy_configs_v3)) {
143 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
144 }
145 std::weak_ptr<WifiRttController> weak_ptr_this = weak_ptr_this_;
146 const auto& on_results_callback_v3 =
147 [weak_ptr_this](legacy_hal::wifi_request_id id,
148 const std::vector<const legacy_hal::wifi_rtt_result_v3*>& results) {
149 const auto shared_ptr_this = weak_ptr_this.lock();
150 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
151 LOG(ERROR) << "v3 Callback invoked on an invalid object";
152 return;
153 }
154 std::vector<RttResult> aidl_results;
155 if (!aidl_struct_util::convertLegacyVectorOfRttResultV3ToAidl(results,
156 &aidl_results)) {
157 LOG(ERROR) << "Failed to convert rtt results v3 to AIDL structs";
158 return;
159 }
160 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
161 if (!callback->onResults(id, aidl_results).isOk()) {
162 LOG(ERROR) << "Failed to invoke the v3 callback";
163 }
164 }
165 };
166 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startRttRangeRequestV3(
167 ifname_, cmd_id, legacy_configs_v3, on_results_callback_v3);
168
169 if (legacy_status != legacy_hal::WIFI_ERROR_NOT_SUPPORTED) {
170 return createWifiStatusFromLegacyError(legacy_status);
171 }
172
173 // Fallback to 11mc ranging.
174 std::vector<legacy_hal::wifi_rtt_config> legacy_configs;
175 if (!aidl_struct_util::convertAidlVectorOfRttConfigToLegacy(rtt_configs, &legacy_configs)) {
176 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
177 }
178 const auto& on_results_callback =
179 [weak_ptr_this](legacy_hal::wifi_request_id id,
180 const std::vector<const legacy_hal::wifi_rtt_result*>& results) {
181 const auto shared_ptr_this = weak_ptr_this.lock();
182 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
183 LOG(ERROR) << "Callback invoked on an invalid object";
184 return;
185 }
186 std::vector<RttResult> aidl_results;
187 if (!aidl_struct_util::convertLegacyVectorOfRttResultToAidl(results,
188 &aidl_results)) {
189 LOG(ERROR) << "Failed to convert rtt results to AIDL structs";
190 return;
191 }
192 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
193 if (!callback->onResults(id, aidl_results).isOk()) {
194 LOG(ERROR) << "Failed to invoke the callback";
195 }
196 }
197 };
198 const auto& on_results_callback_v2 =
199 [weak_ptr_this](legacy_hal::wifi_request_id id,
200 const std::vector<const legacy_hal::wifi_rtt_result_v2*>& results) {
201 const auto shared_ptr_this = weak_ptr_this.lock();
202 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
203 LOG(ERROR) << "v2 Callback invoked on an invalid object";
204 return;
205 }
206 std::vector<RttResult> aidl_results;
207 if (!aidl_struct_util::convertLegacyVectorOfRttResultV2ToAidl(results,
208 &aidl_results)) {
209 LOG(ERROR) << "Failed to convert rtt results v2 to AIDL structs";
210 return;
211 }
212 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
213 if (!callback->onResults(id, aidl_results).isOk()) {
214 LOG(ERROR) << "Failed to invoke the v2 callback";
215 }
216 }
217 };
218 legacy_status = legacy_hal_.lock()->startRttRangeRequest(
219 ifname_, cmd_id, legacy_configs, on_results_callback, on_results_callback_v2);
220 return createWifiStatusFromLegacyError(legacy_status);
221 }
222
rangeCancelInternal(int32_t cmd_id,const std::vector<MacAddress> & addrs)223 ndk::ScopedAStatus WifiRttController::rangeCancelInternal(int32_t cmd_id,
224 const std::vector<MacAddress>& addrs) {
225 std::vector<std::array<uint8_t, ETH_ALEN>> legacy_addrs;
226 for (const auto& addr : addrs) {
227 std::array<uint8_t, ETH_ALEN> addr_array;
228 std::copy_n(addr.data.begin(), ETH_ALEN, addr_array.begin());
229 legacy_addrs.push_back(addr_array);
230 }
231 legacy_hal::wifi_error legacy_status =
232 legacy_hal_.lock()->cancelRttRangeRequest(ifname_, cmd_id, legacy_addrs);
233 return createWifiStatusFromLegacyError(legacy_status);
234 }
235
getCapabilitiesInternal()236 std::pair<RttCapabilities, ndk::ScopedAStatus> WifiRttController::getCapabilitiesInternal() {
237 legacy_hal::wifi_error legacy_status;
238 legacy_hal::wifi_rtt_capabilities_v3 legacy_caps_v3;
239 std::tie(legacy_status, legacy_caps_v3) = legacy_hal_.lock()->getRttCapabilitiesV3(ifname_);
240 // Try v3 API first, if it is not supported fallback.
241 if (legacy_status == legacy_hal::WIFI_ERROR_NOT_SUPPORTED) {
242 legacy_hal::wifi_rtt_capabilities legacy_caps;
243 std::tie(legacy_status, legacy_caps) = legacy_hal_.lock()->getRttCapabilities(ifname_);
244 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
245 return {RttCapabilities{}, createWifiStatusFromLegacyError(legacy_status)};
246 }
247
248 RttCapabilities aidl_caps;
249 if (!aidl_struct_util::convertLegacyRttCapabilitiesToAidl(legacy_caps, &aidl_caps)) {
250 return {RttCapabilities{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
251 }
252 return {aidl_caps, ndk::ScopedAStatus::ok()};
253 }
254
255 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
256 return {RttCapabilities{}, createWifiStatusFromLegacyError(legacy_status)};
257 }
258
259 RttCapabilities aidl_caps;
260 if (!aidl_struct_util::convertLegacyRttCapabilitiesV3ToAidl(legacy_caps_v3, &aidl_caps)) {
261 return {RttCapabilities{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
262 }
263 return {aidl_caps, ndk::ScopedAStatus::ok()};
264 }
265
setLciInternal(int32_t cmd_id,const RttLciInformation & lci)266 ndk::ScopedAStatus WifiRttController::setLciInternal(int32_t cmd_id, const RttLciInformation& lci) {
267 legacy_hal::wifi_lci_information legacy_lci;
268 if (!aidl_struct_util::convertAidlRttLciInformationToLegacy(lci, &legacy_lci)) {
269 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
270 }
271 legacy_hal::wifi_error legacy_status =
272 legacy_hal_.lock()->setRttLci(ifname_, cmd_id, legacy_lci);
273 return createWifiStatusFromLegacyError(legacy_status);
274 }
275
setLcrInternal(int32_t cmd_id,const RttLcrInformation & lcr)276 ndk::ScopedAStatus WifiRttController::setLcrInternal(int32_t cmd_id, const RttLcrInformation& lcr) {
277 legacy_hal::wifi_lcr_information legacy_lcr;
278 if (!aidl_struct_util::convertAidlRttLcrInformationToLegacy(lcr, &legacy_lcr)) {
279 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
280 }
281 legacy_hal::wifi_error legacy_status =
282 legacy_hal_.lock()->setRttLcr(ifname_, cmd_id, legacy_lcr);
283 return createWifiStatusFromLegacyError(legacy_status);
284 }
285
getResponderInfoInternal()286 std::pair<RttResponder, ndk::ScopedAStatus> WifiRttController::getResponderInfoInternal() {
287 legacy_hal::wifi_error legacy_status;
288 legacy_hal::wifi_rtt_responder legacy_responder;
289 std::tie(legacy_status, legacy_responder) = legacy_hal_.lock()->getRttResponderInfo(ifname_);
290 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
291 return {RttResponder{}, createWifiStatusFromLegacyError(legacy_status)};
292 }
293 RttResponder aidl_responder;
294 if (!aidl_struct_util::convertLegacyRttResponderToAidl(legacy_responder, &aidl_responder)) {
295 return {RttResponder{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
296 }
297 return {aidl_responder, ndk::ScopedAStatus::ok()};
298 }
299
enableResponderInternal(int32_t cmd_id,const WifiChannelInfo & channel_hint,int32_t max_duration_seconds,const RttResponder & info)300 ndk::ScopedAStatus WifiRttController::enableResponderInternal(int32_t cmd_id,
301 const WifiChannelInfo& channel_hint,
302 int32_t max_duration_seconds,
303 const RttResponder& info) {
304 legacy_hal::wifi_channel_info legacy_channel_info;
305 if (!aidl_struct_util::convertAidlWifiChannelInfoToLegacy(channel_hint, &legacy_channel_info)) {
306 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
307 }
308 legacy_hal::wifi_rtt_responder legacy_responder;
309 if (!aidl_struct_util::convertAidlRttResponderToLegacy(info, &legacy_responder)) {
310 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
311 }
312 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->enableRttResponder(
313 ifname_, cmd_id, legacy_channel_info, max_duration_seconds, legacy_responder);
314 return createWifiStatusFromLegacyError(legacy_status);
315 }
316
disableResponderInternal(int32_t cmd_id)317 ndk::ScopedAStatus WifiRttController::disableResponderInternal(int32_t cmd_id) {
318 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->disableRttResponder(ifname_, cmd_id);
319 return createWifiStatusFromLegacyError(legacy_status);
320 }
321
322 } // namespace wifi
323 } // namespace hardware
324 } // namespace android
325 } // namespace aidl
326