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