Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 46 | void WifiRttController::invalidate() { |
| 47 | legacy_hal_.reset(); |
| 48 | event_callbacks_.clear(); |
| 49 | is_valid_ = false; |
| 50 | }; |
| 51 | |
| 52 | bool WifiRttController::isValid() { |
| 53 | return is_valid_; |
| 54 | } |
| 55 | |
| 56 | void WifiRttController::setWeakPtr(std::weak_ptr<WifiRttController> ptr) { |
| 57 | weak_ptr_this_ = ptr; |
| 58 | } |
| 59 | |
| 60 | std::vector<std::shared_ptr<IWifiRttControllerEventCallback>> |
| 61 | WifiRttController::getEventCallbacks() { |
| 62 | return event_callbacks_; |
| 63 | } |
| 64 | |
| 65 | std::string WifiRttController::getIfaceName() { |
| 66 | return ifname_; |
| 67 | } |
| 68 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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> |
| 127 | WifiRttController::getBoundIfaceInternal() { |
| 128 | return {bound_iface_, ndk::ScopedAStatus::ok()}; |
| 129 | } |
| 130 | |
| 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 | |
| 137 | ndk::ScopedAStatus WifiRttController::rangeRequestInternal( |
| 138 | int32_t cmd_id, const std::vector<RttConfig>& rtt_configs) { |
| 139 | std::vector<legacy_hal::wifi_rtt_config> legacy_configs; |
| 140 | if (!aidl_struct_util::convertAidlVectorOfRttConfigToLegacy(rtt_configs, &legacy_configs)) { |
| 141 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 142 | } |
| 143 | std::weak_ptr<WifiRttController> weak_ptr_this = weak_ptr_this_; |
| 144 | const auto& on_results_callback = |
| 145 | [weak_ptr_this](legacy_hal::wifi_request_id id, |
| 146 | const std::vector<const legacy_hal::wifi_rtt_result*>& results) { |
| 147 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 148 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 149 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 150 | return; |
| 151 | } |
| 152 | std::vector<RttResult> aidl_results; |
| 153 | if (!aidl_struct_util::convertLegacyVectorOfRttResultToAidl(results, |
| 154 | &aidl_results)) { |
| 155 | LOG(ERROR) << "Failed to convert rtt results to AIDL structs"; |
| 156 | return; |
| 157 | } |
| 158 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 159 | if (!callback->onResults(id, aidl_results).isOk()) { |
| 160 | LOG(ERROR) << "Failed to invoke the callback"; |
| 161 | } |
| 162 | } |
| 163 | }; |
Sunil Ravi | f8fc237 | 2022-11-10 18:37:41 +0000 | [diff] [blame] | 164 | const auto& on_results_callback_v2 = |
| 165 | [weak_ptr_this](legacy_hal::wifi_request_id id, |
| 166 | const std::vector<const legacy_hal::wifi_rtt_result_v2*>& results) { |
| 167 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 168 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 169 | LOG(ERROR) << "v2 Callback invoked on an invalid object"; |
| 170 | return; |
| 171 | } |
| 172 | std::vector<RttResult> aidl_results; |
| 173 | if (!aidl_struct_util::convertLegacyVectorOfRttResultV2ToAidl(results, |
| 174 | &aidl_results)) { |
| 175 | LOG(ERROR) << "Failed to convert rtt results v2 to AIDL structs"; |
| 176 | return; |
| 177 | } |
| 178 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 179 | if (!callback->onResults(id, aidl_results).isOk()) { |
| 180 | LOG(ERROR) << "Failed to invoke the v2 callback"; |
| 181 | } |
| 182 | } |
| 183 | }; |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 184 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startRttRangeRequest( |
Sunil Ravi | f8fc237 | 2022-11-10 18:37:41 +0000 | [diff] [blame] | 185 | ifname_, cmd_id, legacy_configs, on_results_callback, on_results_callback_v2); |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 186 | return createWifiStatusFromLegacyError(legacy_status); |
| 187 | } |
| 188 | |
| 189 | ndk::ScopedAStatus WifiRttController::rangeCancelInternal(int32_t cmd_id, |
| 190 | const std::vector<MacAddress>& addrs) { |
| 191 | std::vector<std::array<uint8_t, ETH_ALEN>> legacy_addrs; |
| 192 | for (const auto& addr : addrs) { |
| 193 | std::array<uint8_t, ETH_ALEN> addr_array; |
| 194 | std::copy_n(addr.data.begin(), ETH_ALEN, addr_array.begin()); |
| 195 | legacy_addrs.push_back(addr_array); |
| 196 | } |
| 197 | legacy_hal::wifi_error legacy_status = |
| 198 | legacy_hal_.lock()->cancelRttRangeRequest(ifname_, cmd_id, legacy_addrs); |
| 199 | return createWifiStatusFromLegacyError(legacy_status); |
| 200 | } |
| 201 | |
| 202 | std::pair<RttCapabilities, ndk::ScopedAStatus> WifiRttController::getCapabilitiesInternal() { |
| 203 | legacy_hal::wifi_error legacy_status; |
| 204 | legacy_hal::wifi_rtt_capabilities legacy_caps; |
| 205 | std::tie(legacy_status, legacy_caps) = legacy_hal_.lock()->getRttCapabilities(ifname_); |
| 206 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 207 | return {RttCapabilities{}, createWifiStatusFromLegacyError(legacy_status)}; |
| 208 | } |
| 209 | RttCapabilities aidl_caps; |
| 210 | if (!aidl_struct_util::convertLegacyRttCapabilitiesToAidl(legacy_caps, &aidl_caps)) { |
| 211 | return {RttCapabilities{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)}; |
| 212 | } |
| 213 | return {aidl_caps, ndk::ScopedAStatus::ok()}; |
| 214 | } |
| 215 | |
| 216 | ndk::ScopedAStatus WifiRttController::setLciInternal(int32_t cmd_id, const RttLciInformation& lci) { |
| 217 | legacy_hal::wifi_lci_information legacy_lci; |
| 218 | if (!aidl_struct_util::convertAidlRttLciInformationToLegacy(lci, &legacy_lci)) { |
| 219 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 220 | } |
| 221 | legacy_hal::wifi_error legacy_status = |
| 222 | legacy_hal_.lock()->setRttLci(ifname_, cmd_id, legacy_lci); |
| 223 | return createWifiStatusFromLegacyError(legacy_status); |
| 224 | } |
| 225 | |
| 226 | ndk::ScopedAStatus WifiRttController::setLcrInternal(int32_t cmd_id, const RttLcrInformation& lcr) { |
| 227 | legacy_hal::wifi_lcr_information legacy_lcr; |
| 228 | if (!aidl_struct_util::convertAidlRttLcrInformationToLegacy(lcr, &legacy_lcr)) { |
| 229 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 230 | } |
| 231 | legacy_hal::wifi_error legacy_status = |
| 232 | legacy_hal_.lock()->setRttLcr(ifname_, cmd_id, legacy_lcr); |
| 233 | return createWifiStatusFromLegacyError(legacy_status); |
| 234 | } |
| 235 | |
| 236 | std::pair<RttResponder, ndk::ScopedAStatus> WifiRttController::getResponderInfoInternal() { |
| 237 | legacy_hal::wifi_error legacy_status; |
| 238 | legacy_hal::wifi_rtt_responder legacy_responder; |
| 239 | std::tie(legacy_status, legacy_responder) = legacy_hal_.lock()->getRttResponderInfo(ifname_); |
| 240 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 241 | return {RttResponder{}, createWifiStatusFromLegacyError(legacy_status)}; |
| 242 | } |
| 243 | RttResponder aidl_responder; |
| 244 | if (!aidl_struct_util::convertLegacyRttResponderToAidl(legacy_responder, &aidl_responder)) { |
| 245 | return {RttResponder{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)}; |
| 246 | } |
| 247 | return {aidl_responder, ndk::ScopedAStatus::ok()}; |
| 248 | } |
| 249 | |
| 250 | ndk::ScopedAStatus WifiRttController::enableResponderInternal(int32_t cmd_id, |
| 251 | const WifiChannelInfo& channel_hint, |
| 252 | int32_t max_duration_seconds, |
| 253 | const RttResponder& info) { |
| 254 | legacy_hal::wifi_channel_info legacy_channel_info; |
| 255 | if (!aidl_struct_util::convertAidlWifiChannelInfoToLegacy(channel_hint, &legacy_channel_info)) { |
| 256 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 257 | } |
| 258 | legacy_hal::wifi_rtt_responder legacy_responder; |
| 259 | if (!aidl_struct_util::convertAidlRttResponderToLegacy(info, &legacy_responder)) { |
| 260 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 261 | } |
| 262 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->enableRttResponder( |
| 263 | ifname_, cmd_id, legacy_channel_info, max_duration_seconds, legacy_responder); |
| 264 | return createWifiStatusFromLegacyError(legacy_status); |
| 265 | } |
| 266 | |
| 267 | ndk::ScopedAStatus WifiRttController::disableResponderInternal(int32_t cmd_id) { |
| 268 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->disableRttResponder(ifname_, cmd_id); |
| 269 | return createWifiStatusFromLegacyError(legacy_status); |
| 270 | } |
| 271 | |
| 272 | } // namespace wifi |
| 273 | } // namespace hardware |
| 274 | } // namespace android |
| 275 | } // namespace aidl |