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