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