Roshan Pius | 5926828 | 2016-10-06 20:23:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Roshan Pius | 5926828 | 2016-10-06 20:23:47 -0700 | [diff] [blame] | 17 | #include <android-base/logging.h> |
| 18 | |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 19 | #include "hidl_return_util.h" |
Roshan Pius | e3f72ff | 2016-12-05 16:18:43 -0800 | [diff] [blame] | 20 | #include "hidl_struct_util.h" |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 21 | #include "wifi_rtt_controller.h" |
Roshan Pius | 734fea0 | 2016-10-11 08:30:28 -0700 | [diff] [blame] | 22 | #include "wifi_status_util.h" |
Roshan Pius | 5926828 | 2016-10-06 20:23:47 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace wifi { |
Etan Cohen | 6ce5090 | 2017-09-14 07:30:57 -0700 | [diff] [blame] | 27 | namespace V1_2 { |
Roshan Pius | 5926828 | 2016-10-06 20:23:47 -0700 | [diff] [blame] | 28 | namespace implementation { |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 29 | using hidl_return_util::validateAndCall; |
Roshan Pius | 5926828 | 2016-10-06 20:23:47 -0700 | [diff] [blame] | 30 | |
| 31 | WifiRttController::WifiRttController( |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 32 | const std::string& iface_name, const sp<IWifiIface>& bound_iface, |
Roshan Pius | 6cedc97 | 2016-10-28 10:11:17 -0700 | [diff] [blame] | 33 | const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal) |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 34 | : ifname_(iface_name), |
| 35 | bound_iface_(bound_iface), |
| 36 | legacy_hal_(legacy_hal), |
Roshan Pius | 9377a0d | 2017-10-06 13:18:54 -0700 | [diff] [blame] | 37 | is_valid_(true) {} |
Roshan Pius | 5926828 | 2016-10-06 20:23:47 -0700 | [diff] [blame] | 38 | |
| 39 | void WifiRttController::invalidate() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 40 | legacy_hal_.reset(); |
| 41 | event_callbacks_.clear(); |
| 42 | is_valid_ = false; |
Roshan Pius | 5926828 | 2016-10-06 20:23:47 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 45 | bool WifiRttController::isValid() { return is_valid_; } |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 46 | |
Roshan Pius | e3f72ff | 2016-12-05 16:18:43 -0800 | [diff] [blame] | 47 | std::vector<sp<IWifiRttControllerEventCallback>> |
| 48 | WifiRttController::getEventCallbacks() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 49 | return event_callbacks_; |
Roshan Pius | e3f72ff | 2016-12-05 16:18:43 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Roshan Pius | 734fea0 | 2016-10-11 08:30:28 -0700 | [diff] [blame] | 52 | Return<void> WifiRttController::getBoundIface(getBoundIface_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 53 | return validateAndCall( |
| 54 | this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID, |
| 55 | &WifiRttController::getBoundIfaceInternal, hidl_status_cb); |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 58 | Return<void> WifiRttController::registerEventCallback( |
| 59 | const sp<IWifiRttControllerEventCallback>& callback, |
| 60 | registerEventCallback_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 61 | return validateAndCall(this, |
| 62 | WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID, |
| 63 | &WifiRttController::registerEventCallbackInternal, |
| 64 | hidl_status_cb, callback); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | Return<void> WifiRttController::rangeRequest( |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 68 | uint32_t cmd_id, const hidl_vec<RttConfig>& rtt_configs, |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 69 | rangeRequest_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 70 | return validateAndCall(this, |
| 71 | WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID, |
| 72 | &WifiRttController::rangeRequestInternal, |
| 73 | hidl_status_cb, cmd_id, rtt_configs); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | Return<void> WifiRttController::rangeCancel( |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 77 | uint32_t cmd_id, const hidl_vec<hidl_array<uint8_t, 6>>& addrs, |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 78 | rangeCancel_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 79 | return validateAndCall( |
| 80 | this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID, |
| 81 | &WifiRttController::rangeCancelInternal, hidl_status_cb, cmd_id, addrs); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 84 | Return<void> WifiRttController::getCapabilities( |
| 85 | getCapabilities_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 86 | return validateAndCall( |
| 87 | this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID, |
| 88 | &WifiRttController::getCapabilitiesInternal, hidl_status_cb); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 91 | Return<void> WifiRttController::setLci(uint32_t cmd_id, |
| 92 | const RttLciInformation& lci, |
| 93 | setLci_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 94 | return validateAndCall( |
| 95 | this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID, |
| 96 | &WifiRttController::setLciInternal, hidl_status_cb, cmd_id, lci); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | Return<void> WifiRttController::setLcr(uint32_t cmd_id, |
| 100 | const RttLcrInformation& lcr, |
| 101 | setLcr_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 102 | return validateAndCall( |
| 103 | this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID, |
| 104 | &WifiRttController::setLcrInternal, hidl_status_cb, cmd_id, lcr); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | Return<void> WifiRttController::getResponderInfo( |
| 108 | getResponderInfo_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 109 | return validateAndCall( |
| 110 | this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID, |
| 111 | &WifiRttController::getResponderInfoInternal, hidl_status_cb); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | Return<void> WifiRttController::enableResponder( |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 115 | uint32_t cmd_id, const WifiChannelInfo& channel_hint, |
| 116 | uint32_t max_duration_seconds, const RttResponder& info, |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 117 | enableResponder_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 118 | return validateAndCall( |
| 119 | this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID, |
| 120 | &WifiRttController::enableResponderInternal, hidl_status_cb, cmd_id, |
| 121 | channel_hint, max_duration_seconds, info); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | Return<void> WifiRttController::disableResponder( |
| 125 | uint32_t cmd_id, disableResponder_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 126 | return validateAndCall( |
| 127 | this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID, |
| 128 | &WifiRttController::disableResponderInternal, hidl_status_cb, cmd_id); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 131 | std::pair<WifiStatus, sp<IWifiIface>> |
| 132 | WifiRttController::getBoundIfaceInternal() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 133 | return {createWifiStatus(WifiStatusCode::SUCCESS), bound_iface_}; |
Roshan Pius | 5926828 | 2016-10-06 20:23:47 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 136 | WifiStatus WifiRttController::registerEventCallbackInternal( |
| 137 | const sp<IWifiRttControllerEventCallback>& callback) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 138 | // TODO(b/31632518): remove the callback when the client is destroyed |
| 139 | event_callbacks_.emplace_back(callback); |
| 140 | return createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | WifiStatus WifiRttController::rangeRequestInternal( |
Roshan Pius | e3f72ff | 2016-12-05 16:18:43 -0800 | [diff] [blame] | 144 | uint32_t cmd_id, const std::vector<RttConfig>& rtt_configs) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 145 | std::vector<legacy_hal::wifi_rtt_config> legacy_configs; |
| 146 | if (!hidl_struct_util::convertHidlVectorOfRttConfigToLegacy( |
| 147 | rtt_configs, &legacy_configs)) { |
| 148 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
Roshan Pius | e3f72ff | 2016-12-05 16:18:43 -0800 | [diff] [blame] | 149 | } |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 150 | android::wp<WifiRttController> weak_ptr_this(this); |
| 151 | const auto& on_results_callback = |
| 152 | [weak_ptr_this]( |
| 153 | legacy_hal::wifi_request_id id, |
| 154 | const std::vector<const legacy_hal::wifi_rtt_result*>& results) { |
| 155 | const auto shared_ptr_this = weak_ptr_this.promote(); |
| 156 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 157 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 158 | return; |
| 159 | } |
| 160 | std::vector<RttResult> hidl_results; |
| 161 | if (!hidl_struct_util::convertLegacyVectorOfRttResultToHidl( |
| 162 | results, &hidl_results)) { |
| 163 | LOG(ERROR) << "Failed to convert rtt results to HIDL structs"; |
| 164 | return; |
| 165 | } |
| 166 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 167 | callback->onResults(id, hidl_results); |
| 168 | } |
| 169 | }; |
| 170 | legacy_hal::wifi_error legacy_status = |
| 171 | legacy_hal_.lock()->startRttRangeRequest( |
| 172 | ifname_, cmd_id, legacy_configs, on_results_callback); |
| 173 | return createWifiStatusFromLegacyError(legacy_status); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | WifiStatus WifiRttController::rangeCancelInternal( |
Roshan Pius | e3f72ff | 2016-12-05 16:18:43 -0800 | [diff] [blame] | 177 | uint32_t cmd_id, const std::vector<hidl_array<uint8_t, 6>>& addrs) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 178 | std::vector<std::array<uint8_t, 6>> legacy_addrs; |
| 179 | for (const auto& addr : addrs) { |
| 180 | legacy_addrs.push_back(addr); |
| 181 | } |
| 182 | legacy_hal::wifi_error legacy_status = |
| 183 | legacy_hal_.lock()->cancelRttRangeRequest(ifname_, cmd_id, |
| 184 | legacy_addrs); |
| 185 | return createWifiStatusFromLegacyError(legacy_status); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 188 | std::pair<WifiStatus, RttCapabilities> |
| 189 | WifiRttController::getCapabilitiesInternal() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 190 | legacy_hal::wifi_error legacy_status; |
| 191 | legacy_hal::wifi_rtt_capabilities legacy_caps; |
| 192 | std::tie(legacy_status, legacy_caps) = |
| 193 | legacy_hal_.lock()->getRttCapabilities(ifname_); |
| 194 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 195 | return {createWifiStatusFromLegacyError(legacy_status), {}}; |
| 196 | } |
| 197 | RttCapabilities hidl_caps; |
| 198 | if (!hidl_struct_util::convertLegacyRttCapabilitiesToHidl(legacy_caps, |
| 199 | &hidl_caps)) { |
| 200 | return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}}; |
| 201 | } |
| 202 | return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps}; |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Roshan Pius | e3f72ff | 2016-12-05 16:18:43 -0800 | [diff] [blame] | 205 | WifiStatus WifiRttController::setLciInternal(uint32_t cmd_id, |
| 206 | const RttLciInformation& lci) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 207 | legacy_hal::wifi_lci_information legacy_lci; |
| 208 | if (!hidl_struct_util::convertHidlRttLciInformationToLegacy(lci, |
| 209 | &legacy_lci)) { |
| 210 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 211 | } |
| 212 | legacy_hal::wifi_error legacy_status = |
| 213 | legacy_hal_.lock()->setRttLci(ifname_, cmd_id, legacy_lci); |
| 214 | return createWifiStatusFromLegacyError(legacy_status); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Roshan Pius | e3f72ff | 2016-12-05 16:18:43 -0800 | [diff] [blame] | 217 | WifiStatus WifiRttController::setLcrInternal(uint32_t cmd_id, |
| 218 | const RttLcrInformation& lcr) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 219 | legacy_hal::wifi_lcr_information legacy_lcr; |
| 220 | if (!hidl_struct_util::convertHidlRttLcrInformationToLegacy(lcr, |
| 221 | &legacy_lcr)) { |
| 222 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 223 | } |
| 224 | legacy_hal::wifi_error legacy_status = |
| 225 | legacy_hal_.lock()->setRttLcr(ifname_, cmd_id, legacy_lcr); |
| 226 | return createWifiStatusFromLegacyError(legacy_status); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | std::pair<WifiStatus, RttResponder> |
| 230 | WifiRttController::getResponderInfoInternal() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 231 | legacy_hal::wifi_error legacy_status; |
| 232 | legacy_hal::wifi_rtt_responder legacy_responder; |
| 233 | std::tie(legacy_status, legacy_responder) = |
| 234 | legacy_hal_.lock()->getRttResponderInfo(ifname_); |
| 235 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 236 | return {createWifiStatusFromLegacyError(legacy_status), {}}; |
| 237 | } |
| 238 | RttResponder hidl_responder; |
| 239 | if (!hidl_struct_util::convertLegacyRttResponderToHidl(legacy_responder, |
| 240 | &hidl_responder)) { |
| 241 | return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}}; |
| 242 | } |
| 243 | return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_responder}; |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | WifiStatus WifiRttController::enableResponderInternal( |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 247 | uint32_t cmd_id, const WifiChannelInfo& channel_hint, |
| 248 | uint32_t max_duration_seconds, const RttResponder& info) { |
| 249 | legacy_hal::wifi_channel_info legacy_channel_info; |
| 250 | if (!hidl_struct_util::convertHidlWifiChannelInfoToLegacy( |
| 251 | channel_hint, &legacy_channel_info)) { |
| 252 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 253 | } |
| 254 | legacy_hal::wifi_rtt_responder legacy_responder; |
| 255 | if (!hidl_struct_util::convertHidlRttResponderToLegacy(info, |
| 256 | &legacy_responder)) { |
| 257 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 258 | } |
| 259 | legacy_hal::wifi_error legacy_status = |
| 260 | legacy_hal_.lock()->enableRttResponder( |
| 261 | ifname_, cmd_id, legacy_channel_info, max_duration_seconds, |
| 262 | legacy_responder); |
| 263 | return createWifiStatusFromLegacyError(legacy_status); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Roshan Pius | e3f72ff | 2016-12-05 16:18:43 -0800 | [diff] [blame] | 266 | WifiStatus WifiRttController::disableResponderInternal(uint32_t cmd_id) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame^] | 267 | legacy_hal::wifi_error legacy_status = |
| 268 | legacy_hal_.lock()->disableRttResponder(ifname_, cmd_id); |
| 269 | return createWifiStatusFromLegacyError(legacy_status); |
Roshan Pius | 7913f5e | 2016-10-27 17:09:30 -0700 | [diff] [blame] | 270 | } |
Roshan Pius | 5926828 | 2016-10-06 20:23:47 -0700 | [diff] [blame] | 271 | } // namespace implementation |
Etan Cohen | 6ce5090 | 2017-09-14 07:30:57 -0700 | [diff] [blame] | 272 | } // namespace V1_2 |
Roshan Pius | 5926828 | 2016-10-06 20:23:47 -0700 | [diff] [blame] | 273 | } // namespace wifi |
| 274 | } // namespace hardware |
| 275 | } // namespace android |