Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -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 | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 17 | #include <android-base/logging.h> |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 18 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 19 | #include "hidl_return_util.h" |
| 20 | #include "wifi.h" |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 21 | #include "wifi_status_util.h" |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 22 | |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | // Chip ID to use for the only supported chip. |
| 25 | static constexpr android::hardware::wifi::V1_0::ChipId kChipId = 0; |
| 26 | } // namespace |
| 27 | |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace hardware { |
| 30 | namespace wifi { |
Jong Wook Kim | da830c9 | 2018-07-23 15:29:38 -0700 | [diff] [blame] | 31 | namespace V1_3 { |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 32 | namespace implementation { |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 33 | using hidl_return_util::validateAndCall; |
Roshan Pius | 155344b | 2017-08-11 15:47:42 -0700 | [diff] [blame] | 34 | using hidl_return_util::validateAndCallWithLock; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 35 | |
Roshan Pius | 200a17d | 2017-11-01 13:03:35 -0700 | [diff] [blame] | 36 | Wifi::Wifi( |
| 37 | const std::shared_ptr<legacy_hal::WifiLegacyHal> legacy_hal, |
| 38 | const std::shared_ptr<mode_controller::WifiModeController> mode_controller, |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 39 | const std::shared_ptr<iface_util::WifiIfaceUtil> iface_util, |
Roshan Pius | 200a17d | 2017-11-01 13:03:35 -0700 | [diff] [blame] | 40 | const std::shared_ptr<feature_flags::WifiFeatureFlags> feature_flags) |
| 41 | : legacy_hal_(legacy_hal), |
| 42 | mode_controller_(mode_controller), |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 43 | iface_util_(iface_util), |
Roshan Pius | 200a17d | 2017-11-01 13:03:35 -0700 | [diff] [blame] | 44 | feature_flags_(feature_flags), |
Roshan Pius | 6cedc97 | 2016-10-28 10:11:17 -0700 | [diff] [blame] | 45 | run_state_(RunState::STOPPED) {} |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 46 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 47 | bool Wifi::isValid() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 48 | // This object is always valid. |
| 49 | return true; |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 52 | Return<void> Wifi::registerEventCallback( |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 53 | const sp<IWifiEventCallback>& event_callback, |
| 54 | registerEventCallback_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 55 | return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, |
| 56 | &Wifi::registerEventCallbackInternal, hidl_status_cb, |
| 57 | event_callback); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 60 | Return<bool> Wifi::isStarted() { return run_state_ != RunState::STOPPED; } |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 61 | |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 62 | Return<void> Wifi::start(start_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 63 | return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, |
| 64 | &Wifi::startInternal, hidl_status_cb); |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | Return<void> Wifi::stop(stop_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 68 | return validateAndCallWithLock(this, WifiStatusCode::ERROR_UNKNOWN, |
| 69 | &Wifi::stopInternal, hidl_status_cb); |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | Return<void> Wifi::getChipIds(getChipIds_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 73 | return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, |
| 74 | &Wifi::getChipIdsInternal, hidl_status_cb); |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | Return<void> Wifi::getChip(ChipId chip_id, getChip_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 78 | return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, |
| 79 | &Wifi::getChipInternal, hidl_status_cb, chip_id); |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 80 | } |
| 81 | |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 82 | Return<void> Wifi::debug(const hidl_handle& handle, |
| 83 | const hidl_vec<hidl_string>&) { |
| 84 | LOG(INFO) << "-----------Debug is called----------------"; |
Roshan Pius | 7cf1df7 | 2018-01-24 19:13:09 -0800 | [diff] [blame] | 85 | if (!chip_.get()) { |
| 86 | return Void(); |
| 87 | } |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 88 | return chip_->debug(handle, {}); |
| 89 | } |
| 90 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 91 | WifiStatus Wifi::registerEventCallbackInternal( |
| 92 | const sp<IWifiEventCallback>& event_callback) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 93 | if (!event_cb_handler_.addCallback(event_callback)) { |
| 94 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 95 | } |
| 96 | return createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | WifiStatus Wifi::startInternal() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 100 | if (run_state_ == RunState::STARTED) { |
| 101 | return createWifiStatus(WifiStatusCode::SUCCESS); |
| 102 | } else if (run_state_ == RunState::STOPPING) { |
| 103 | return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, |
| 104 | "HAL is stopping"); |
Roshan Pius | 52947fb | 2016-11-18 11:38:07 -0800 | [diff] [blame] | 105 | } |
Roshan Pius | 8fc6d17 | 2017-11-27 16:08:27 -0800 | [diff] [blame] | 106 | WifiStatus wifi_status = initializeModeControllerAndLegacyHal(); |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 107 | if (wifi_status.code == WifiStatusCode::SUCCESS) { |
| 108 | // Create the chip instance once the HAL is started. |
Roshan Pius | 200a17d | 2017-11-01 13:03:35 -0700 | [diff] [blame] | 109 | chip_ = new WifiChip(kChipId, legacy_hal_, mode_controller_, |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 110 | iface_util_, feature_flags_); |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 111 | run_state_ = RunState::STARTED; |
| 112 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 113 | if (!callback->onStart().isOk()) { |
| 114 | LOG(ERROR) << "Failed to invoke onStart callback"; |
| 115 | }; |
| 116 | } |
| 117 | LOG(INFO) << "Wifi HAL started"; |
| 118 | } else { |
| 119 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 120 | if (!callback->onFailure(wifi_status).isOk()) { |
| 121 | LOG(ERROR) << "Failed to invoke onFailure callback"; |
| 122 | } |
| 123 | } |
| 124 | LOG(ERROR) << "Wifi HAL start failed"; |
Roshan Pius | 52947fb | 2016-11-18 11:38:07 -0800 | [diff] [blame] | 125 | } |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 126 | return wifi_status; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Roshan Pius | 155344b | 2017-08-11 15:47:42 -0700 | [diff] [blame] | 129 | WifiStatus Wifi::stopInternal( |
| 130 | /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 131 | if (run_state_ == RunState::STOPPED) { |
| 132 | return createWifiStatus(WifiStatusCode::SUCCESS); |
| 133 | } else if (run_state_ == RunState::STOPPING) { |
| 134 | return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, |
| 135 | "HAL is stopping"); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 136 | } |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 137 | // Clear the chip object and its child objects since the HAL is now |
| 138 | // stopped. |
| 139 | if (chip_.get()) { |
| 140 | chip_->invalidate(); |
| 141 | chip_.clear(); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 142 | } |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 143 | WifiStatus wifi_status = stopLegacyHalAndDeinitializeModeController(lock); |
| 144 | if (wifi_status.code == WifiStatusCode::SUCCESS) { |
| 145 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 146 | if (!callback->onStop().isOk()) { |
| 147 | LOG(ERROR) << "Failed to invoke onStop callback"; |
| 148 | }; |
| 149 | } |
| 150 | LOG(INFO) << "Wifi HAL stopped"; |
| 151 | } else { |
| 152 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 153 | if (!callback->onFailure(wifi_status).isOk()) { |
| 154 | LOG(ERROR) << "Failed to invoke onFailure callback"; |
| 155 | } |
| 156 | } |
| 157 | LOG(ERROR) << "Wifi HAL stop failed"; |
| 158 | } |
| 159 | return wifi_status; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 162 | std::pair<WifiStatus, std::vector<ChipId>> Wifi::getChipIdsInternal() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 163 | std::vector<ChipId> chip_ids; |
| 164 | if (chip_.get()) { |
| 165 | chip_ids.emplace_back(kChipId); |
| 166 | } |
| 167 | return {createWifiStatus(WifiStatusCode::SUCCESS), std::move(chip_ids)}; |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 170 | std::pair<WifiStatus, sp<IWifiChip>> Wifi::getChipInternal(ChipId chip_id) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 171 | if (!chip_.get()) { |
| 172 | return {createWifiStatus(WifiStatusCode::ERROR_NOT_STARTED), nullptr}; |
| 173 | } |
| 174 | if (chip_id != kChipId) { |
| 175 | return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr}; |
| 176 | } |
| 177 | return {createWifiStatus(WifiStatusCode::SUCCESS), chip_}; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 178 | } |
Roshan Pius | 52947fb | 2016-11-18 11:38:07 -0800 | [diff] [blame] | 179 | |
Roshan Pius | 8fc6d17 | 2017-11-27 16:08:27 -0800 | [diff] [blame] | 180 | WifiStatus Wifi::initializeModeControllerAndLegacyHal() { |
| 181 | if (!mode_controller_->initialize()) { |
| 182 | LOG(ERROR) << "Failed to initialize firmware mode controller"; |
| 183 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 184 | } |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 185 | legacy_hal::wifi_error legacy_status = legacy_hal_->initialize(); |
| 186 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 187 | LOG(ERROR) << "Failed to initialize legacy HAL: " |
| 188 | << legacyErrorToString(legacy_status); |
| 189 | return createWifiStatusFromLegacyError(legacy_status); |
| 190 | } |
| 191 | return createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | 52947fb | 2016-11-18 11:38:07 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Roshan Pius | 155344b | 2017-08-11 15:47:42 -0700 | [diff] [blame] | 194 | WifiStatus Wifi::stopLegacyHalAndDeinitializeModeController( |
| 195 | /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 196 | run_state_ = RunState::STOPPING; |
| 197 | legacy_hal::wifi_error legacy_status = |
| 198 | legacy_hal_->stop(lock, [&]() { run_state_ = RunState::STOPPED; }); |
| 199 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 200 | LOG(ERROR) << "Failed to stop legacy HAL: " |
| 201 | << legacyErrorToString(legacy_status); |
| 202 | return createWifiStatusFromLegacyError(legacy_status); |
| 203 | } |
| 204 | if (!mode_controller_->deinitialize()) { |
| 205 | LOG(ERROR) << "Failed to deinitialize firmware mode controller"; |
| 206 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 207 | } |
| 208 | return createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | 52947fb | 2016-11-18 11:38:07 -0800 | [diff] [blame] | 209 | } |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 210 | } // namespace implementation |
Jong Wook Kim | da830c9 | 2018-07-23 15:29:38 -0700 | [diff] [blame] | 211 | } // namespace V1_3 |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 212 | } // namespace wifi |
| 213 | } // namespace hardware |
| 214 | } // namespace android |