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