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