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 | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 102 | LOG(ERROR) << "Failed to start Wifi HAL"; |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame^] | 103 | return createWifiStatusFromLegacyError(legacy_status, |
| 104 | "Failed to start HAL"); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 107 | // Create the chip instance once the HAL is started. |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 108 | chip_ = new WifiChip(kChipId, legacy_hal_); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 109 | run_state_ = RunState::STARTED; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 110 | for (const auto& callback : event_callbacks_) { |
| 111 | if (!callback->onStart().getStatus().isOk()) { |
| 112 | LOG(ERROR) << "Failed to invoke onStart callback"; |
| 113 | }; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 114 | } |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame^] | 115 | return createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame^] | 118 | WifiStatus Wifi::stopInternal() { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 119 | if (run_state_ == RunState::STOPPED) { |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame^] | 120 | return createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 121 | } else if (run_state_ == RunState::STOPPING) { |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame^] | 122 | return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, |
| 123 | "HAL is stopping"); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 126 | LOG(INFO) << "Stopping HAL"; |
| 127 | run_state_ = RunState::STOPPING; |
| 128 | const auto on_complete_callback_ = [&]() { |
| 129 | if (chip_.get()) { |
| 130 | chip_->invalidate(); |
| 131 | } |
| 132 | chip_.clear(); |
| 133 | run_state_ = RunState::STOPPED; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 134 | for (const auto& callback : event_callbacks_) { |
| 135 | if (!callback->onStop().getStatus().isOk()) { |
| 136 | LOG(ERROR) << "Failed to invoke onStop callback"; |
| 137 | }; |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 138 | } |
| 139 | }; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 140 | wifi_error legacy_status = legacy_hal_->stop(on_complete_callback_); |
| 141 | if (legacy_status != WIFI_SUCCESS) { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 142 | LOG(ERROR) << "Failed to stop Wifi HAL"; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame] | 143 | WifiStatus wifi_status = |
| 144 | createWifiStatusFromLegacyError(legacy_status, "Failed to stop HAL"); |
| 145 | for (const auto& callback : event_callbacks_) { |
| 146 | callback->onFailure(wifi_status); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 147 | } |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame^] | 148 | return 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 createWifiStatus(WifiStatusCode::SUCCESS); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame^] | 153 | std::pair<WifiStatus, std::vector<ChipId>> Wifi::getChipIdsInternal() { |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 154 | std::vector<ChipId> chip_ids; |
| 155 | if (chip_.get()) { |
| 156 | chip_ids.emplace_back(kChipId); |
| 157 | } |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame^] | 158 | return {createWifiStatus(WifiStatusCode::SUCCESS), std::move(chip_ids)}; |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame^] | 161 | std::pair<WifiStatus, sp<IWifiChip>> Wifi::getChipInternal(ChipId chip_id) { |
| 162 | if (!chip_.get()) { |
| 163 | return {createWifiStatus(WifiStatusCode::ERROR_NOT_STARTED), nullptr}; |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 164 | } |
Roshan Pius | 5647665 | 2016-10-26 14:43:05 -0700 | [diff] [blame^] | 165 | if (chip_id != kChipId) { |
| 166 | return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr}; |
| 167 | } |
| 168 | return {createWifiStatus(WifiStatusCode::SUCCESS), chip_}; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 169 | } |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 170 | } // namespace implementation |
| 171 | } // namespace V1_0 |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 172 | } // namespace wifi |
| 173 | } // namespace hardware |
| 174 | } // namespace android |