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()), |
| 37 | run_state_(RunState::STOPPED) {} |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 38 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 39 | bool Wifi::isValid() { |
| 40 | // This object is always valid. |
| 41 | return true; |
| 42 | } |
| 43 | |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 44 | Return<void> Wifi::registerEventCallback( |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 45 | const sp<IWifiEventCallback>& event_callback, |
| 46 | registerEventCallback_cb hidl_status_cb) { |
| 47 | return validateAndCall(this, |
| 48 | WifiStatusCode::ERROR_UNKNOWN, |
| 49 | &Wifi::registerEventCallbackInternal, |
| 50 | hidl_status_cb, |
| 51 | event_callback); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | Return<bool> Wifi::isStarted() { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 55 | return run_state_ != RunState::STOPPED; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 58 | Return<void> Wifi::start(start_cb hidl_status_cb) { |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 59 | return validateAndCall(this, |
| 60 | WifiStatusCode::ERROR_UNKNOWN, |
| 61 | &Wifi::startInternal, |
| 62 | hidl_status_cb); |
| 63 | } |
| 64 | |
| 65 | Return<void> Wifi::stop(stop_cb hidl_status_cb) { |
| 66 | return validateAndCall( |
| 67 | this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::stopInternal, hidl_status_cb); |
| 68 | } |
| 69 | |
| 70 | Return<void> Wifi::getChipIds(getChipIds_cb hidl_status_cb) { |
| 71 | return validateAndCall(this, |
| 72 | WifiStatusCode::ERROR_UNKNOWN, |
| 73 | &Wifi::getChipIdsInternal, |
| 74 | hidl_status_cb); |
| 75 | } |
| 76 | |
| 77 | Return<void> Wifi::getChip(ChipId chip_id, getChip_cb hidl_status_cb) { |
| 78 | return validateAndCall(this, |
| 79 | WifiStatusCode::ERROR_UNKNOWN, |
| 80 | &Wifi::getChipInternal, |
| 81 | hidl_status_cb, |
| 82 | chip_id); |
| 83 | } |
| 84 | |
| 85 | WifiStatus Wifi::registerEventCallbackInternal( |
| 86 | const sp<IWifiEventCallback>& event_callback) { |
| 87 | // TODO(b/31632518): remove the callback when the client is destroyed |
| 88 | event_callbacks_.emplace_back(event_callback); |
| 89 | return createWifiStatus(WifiStatusCode::SUCCESS); |
| 90 | } |
| 91 | |
| 92 | WifiStatus Wifi::startInternal() { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 93 | if (run_state_ == RunState::STARTED) { |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 94 | return createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 95 | } else if (run_state_ == RunState::STOPPING) { |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 96 | return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, |
| 97 | "HAL is stopping"); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 100 | LOG(INFO) << "Starting HAL"; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 101 | wifi_error legacy_status = legacy_hal_->start(); |
| 102 | if (legacy_status != WIFI_SUCCESS) { |
Roshan Pius | 511cc49 | 2016-10-28 09:54:26 -0700 | [diff] [blame] | 103 | LOG(ERROR) << "Failed to start Wifi HAL: " |
| 104 | << legacyErrorToString(legacy_status); |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 105 | return createWifiStatusFromLegacyError(legacy_status, |
| 106 | "Failed to start HAL"); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 109 | // Create the chip instance once the HAL is started. |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 110 | chip_ = new WifiChip(kChipId, legacy_hal_); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 111 | run_state_ = RunState::STARTED; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 112 | for (const auto& callback : event_callbacks_) { |
| 113 | if (!callback->onStart().getStatus().isOk()) { |
| 114 | LOG(ERROR) << "Failed to invoke onStart callback"; |
| 115 | }; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 116 | } |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 117 | return createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 120 | WifiStatus Wifi::stopInternal() { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 121 | if (run_state_ == RunState::STOPPED) { |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 122 | return createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 123 | } else if (run_state_ == RunState::STOPPING) { |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 124 | return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, |
| 125 | "HAL is stopping"); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 128 | LOG(INFO) << "Stopping HAL"; |
| 129 | run_state_ = RunState::STOPPING; |
| 130 | const auto on_complete_callback_ = [&]() { |
| 131 | if (chip_.get()) { |
| 132 | chip_->invalidate(); |
| 133 | } |
| 134 | chip_.clear(); |
| 135 | run_state_ = RunState::STOPPED; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 136 | for (const auto& callback : event_callbacks_) { |
| 137 | if (!callback->onStop().getStatus().isOk()) { |
| 138 | LOG(ERROR) << "Failed to invoke onStop callback"; |
| 139 | }; |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 140 | } |
| 141 | }; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 142 | wifi_error legacy_status = legacy_hal_->stop(on_complete_callback_); |
| 143 | if (legacy_status != WIFI_SUCCESS) { |
Roshan Pius | 511cc49 | 2016-10-28 09:54:26 -0700 | [diff] [blame] | 144 | LOG(ERROR) << "Failed to stop Wifi HAL: " |
| 145 | << legacyErrorToString(legacy_status); |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 146 | WifiStatus wifi_status = |
| 147 | createWifiStatusFromLegacyError(legacy_status, "Failed to stop HAL"); |
| 148 | for (const auto& callback : event_callbacks_) { |
| 149 | callback->onFailure(wifi_status); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 150 | } |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 151 | return wifi_status; |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 152 | } |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 153 | return createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 156 | std::pair<WifiStatus, std::vector<ChipId>> Wifi::getChipIdsInternal() { |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 157 | std::vector<ChipId> chip_ids; |
| 158 | if (chip_.get()) { |
| 159 | chip_ids.emplace_back(kChipId); |
| 160 | } |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 161 | return {createWifiStatus(WifiStatusCode::SUCCESS), std::move(chip_ids)}; |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 164 | std::pair<WifiStatus, sp<IWifiChip>> Wifi::getChipInternal(ChipId chip_id) { |
| 165 | if (!chip_.get()) { |
| 166 | return {createWifiStatus(WifiStatusCode::ERROR_NOT_STARTED), nullptr}; |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 167 | } |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame] | 168 | if (chip_id != kChipId) { |
| 169 | return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr}; |
| 170 | } |
| 171 | return {createWifiStatus(WifiStatusCode::SUCCESS), chip_}; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 172 | } |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 173 | } // namespace implementation |
| 174 | } // namespace V1_0 |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 175 | } // namespace wifi |
| 176 | } // namespace hardware |
| 177 | } // namespace android |