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