blob: a85b242a8af1cd2e439b5a0b16ea2c679150743d [file] [log] [blame]
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001/*
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 Pius3c4e8a32016-10-03 14:53:58 -070017#include <android-base/logging.h>
Roshan Pius3c4e8a32016-10-03 14:53:58 -070018
Roshan Pius56476652016-10-26 14:43:05 -070019#include "hidl_return_util.h"
20#include "wifi.h"
Roshan Pius503582e2016-10-11 08:25:30 -070021#include "wifi_status_util.h"
Roshan Pius3c4e8a32016-10-03 14:53:58 -070022
Roshan Piuscd566bd2016-10-10 08:03:42 -070023namespace {
Jimmy Chen2dddd792019-12-23 17:50:39 +020024// Starting Chip ID, will be assigned to primary chip
25static constexpr android::hardware::wifi::V1_0::ChipId kPrimaryChipId = 0;
Roshan Piuscd566bd2016-10-10 08:03:42 -070026} // namespace
27
Roshan Pius3c4e8a32016-10-03 14:53:58 -070028namespace android {
29namespace hardware {
30namespace wifi {
Jimmy Chend460df32019-11-29 17:31:22 +020031namespace V1_5 {
Roshan Pius79a99752016-10-04 13:03:58 -070032namespace implementation {
Roshan Pius56476652016-10-26 14:43:05 -070033using hidl_return_util::validateAndCall;
Roshan Pius155344b2017-08-11 15:47:42 -070034using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -070035
Roshan Pius200a17d2017-11-01 13:03:35 -070036Wifi::Wifi(
Roshan Piusc885df02019-05-21 14:49:05 -070037 const std::shared_ptr<wifi_system::InterfaceTool> iface_tool,
Jimmy Chen2dddd792019-12-23 17:50:39 +020038 const std::shared_ptr<legacy_hal::WifiLegacyHalFactory> legacy_hal_factory,
Roshan Pius200a17d2017-11-01 13:03:35 -070039 const std::shared_ptr<mode_controller::WifiModeController> mode_controller,
40 const std::shared_ptr<feature_flags::WifiFeatureFlags> feature_flags)
Roshan Piusc885df02019-05-21 14:49:05 -070041 : iface_tool_(iface_tool),
Jimmy Chen2dddd792019-12-23 17:50:39 +020042 legacy_hal_factory_(legacy_hal_factory),
Roshan Pius200a17d2017-11-01 13:03:35 -070043 mode_controller_(mode_controller),
44 feature_flags_(feature_flags),
Roshan Pius6cedc972016-10-28 10:11:17 -070045 run_state_(RunState::STOPPED) {}
Roshan Pius3c4e8a32016-10-03 14:53:58 -070046
Roshan Pius56476652016-10-26 14:43:05 -070047bool Wifi::isValid() {
Roshan Piusabcf78f2017-10-06 16:30:38 -070048 // This object is always valid.
49 return true;
Roshan Pius56476652016-10-26 14:43:05 -070050}
51
Roshan Pius3c4e8a32016-10-03 14:53:58 -070052Return<void> Wifi::registerEventCallback(
chenpaulb690fb82021-03-16 22:49:18 +080053 const sp<V1_0::IWifiEventCallback>& event_callback,
Roshan Pius56476652016-10-26 14:43:05 -070054 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070055 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN,
56 &Wifi::registerEventCallbackInternal, hidl_status_cb,
57 event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -070058}
59
chenpaulb690fb82021-03-16 22:49:18 +080060Return<void> Wifi::registerEventCallback_1_5(
61 const sp<V1_5::IWifiEventCallback>& event_callback,
62 registerEventCallback_1_5_cb hidl_status_cb) {
63 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN,
64 &Wifi::registerEventCallbackInternal_1_5,
65 hidl_status_cb, event_callback);
66}
67
Roshan Piusabcf78f2017-10-06 16:30:38 -070068Return<bool> Wifi::isStarted() { return run_state_ != RunState::STOPPED; }
Roshan Pius3c4e8a32016-10-03 14:53:58 -070069
Roshan Pius503582e2016-10-11 08:25:30 -070070Return<void> Wifi::start(start_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070071 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN,
72 &Wifi::startInternal, hidl_status_cb);
Roshan Pius56476652016-10-26 14:43:05 -070073}
74
75Return<void> Wifi::stop(stop_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070076 return validateAndCallWithLock(this, WifiStatusCode::ERROR_UNKNOWN,
77 &Wifi::stopInternal, hidl_status_cb);
Roshan Pius56476652016-10-26 14:43:05 -070078}
79
80Return<void> Wifi::getChipIds(getChipIds_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070081 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN,
82 &Wifi::getChipIdsInternal, hidl_status_cb);
Roshan Pius56476652016-10-26 14:43:05 -070083}
84
85Return<void> Wifi::getChip(ChipId chip_id, getChip_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070086 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN,
87 &Wifi::getChipInternal, hidl_status_cb, chip_id);
Roshan Pius56476652016-10-26 14:43:05 -070088}
89
xshu5899e8e2018-01-09 15:36:03 -080090Return<void> Wifi::debug(const hidl_handle& handle,
91 const hidl_vec<hidl_string>&) {
92 LOG(INFO) << "-----------Debug is called----------------";
Jimmy Chen2dddd792019-12-23 17:50:39 +020093 if (chips_.size() == 0) {
Roshan Pius7cf1df72018-01-24 19:13:09 -080094 return Void();
95 }
Jimmy Chen2dddd792019-12-23 17:50:39 +020096
97 for (sp<WifiChip> chip : chips_) {
98 if (!chip.get()) continue;
99
100 chip->debug(handle, {});
101 }
102 return Void();
xshu5899e8e2018-01-09 15:36:03 -0800103}
104
Roshan Pius56476652016-10-26 14:43:05 -0700105WifiStatus Wifi::registerEventCallbackInternal(
chenpaulb690fb82021-03-16 22:49:18 +0800106 const sp<V1_0::IWifiEventCallback>& event_callback __unused) {
107 // Deprecated support for this callback.
108 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
109}
110
111WifiStatus Wifi::registerEventCallbackInternal_1_5(
112 const sp<V1_5::IWifiEventCallback>& event_callback) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700113 if (!event_cb_handler_.addCallback(event_callback)) {
114 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
115 }
116 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius56476652016-10-26 14:43:05 -0700117}
118
119WifiStatus Wifi::startInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700120 if (run_state_ == RunState::STARTED) {
121 return createWifiStatus(WifiStatusCode::SUCCESS);
122 } else if (run_state_ == RunState::STOPPING) {
123 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
124 "HAL is stopping");
Roshan Pius52947fb2016-11-18 11:38:07 -0800125 }
Roshan Pius8fc6d172017-11-27 16:08:27 -0800126 WifiStatus wifi_status = initializeModeControllerAndLegacyHal();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700127 if (wifi_status.code == WifiStatusCode::SUCCESS) {
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700128 // Register the callback for subsystem restart
129 const auto& on_subsystem_restart_callback =
130 [this](const std::string& error) {
131 WifiStatus wifi_status =
132 createWifiStatus(WifiStatusCode::ERROR_UNKNOWN, error);
133 for (const auto& callback : event_cb_handler_.getCallbacks()) {
chenpaul69c3c222021-10-06 09:24:09 +0800134 LOG(INFO) << "Attempting to invoke onSubsystemRestart "
135 "callback";
chenpaulc6f57032021-03-05 17:06:50 +0800136 if (!callback->onSubsystemRestart(wifi_status).isOk()) {
chenpaul69c3c222021-10-06 09:24:09 +0800137 LOG(ERROR)
138 << "Failed to invoke onSubsystemRestart callback";
139 } else {
140 LOG(INFO) << "Succeeded to invoke onSubsystemRestart "
141 "callback";
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700142 }
143 }
144 };
145
Roshan Piusabcf78f2017-10-06 16:30:38 -0700146 // Create the chip instance once the HAL is started.
Jimmy Chen2dddd792019-12-23 17:50:39 +0200147 android::hardware::wifi::V1_0::ChipId chipId = kPrimaryChipId;
148 for (auto& hal : legacy_hals_) {
149 chips_.push_back(new WifiChip(
150 chipId, chipId == kPrimaryChipId, hal, mode_controller_,
Roshan Pius8c1a67b2021-03-02 10:00:23 -0800151 std::make_shared<iface_util::WifiIfaceUtil>(iface_tool_, hal),
152 feature_flags_, on_subsystem_restart_callback));
Jimmy Chen2dddd792019-12-23 17:50:39 +0200153 chipId++;
154 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700155 run_state_ = RunState::STARTED;
156 for (const auto& callback : event_cb_handler_.getCallbacks()) {
157 if (!callback->onStart().isOk()) {
158 LOG(ERROR) << "Failed to invoke onStart callback";
159 };
160 }
161 LOG(INFO) << "Wifi HAL started";
162 } else {
163 for (const auto& callback : event_cb_handler_.getCallbacks()) {
164 if (!callback->onFailure(wifi_status).isOk()) {
165 LOG(ERROR) << "Failed to invoke onFailure callback";
166 }
167 }
168 LOG(ERROR) << "Wifi HAL start failed";
Kyounghan Leef2e21c22020-01-29 14:56:15 +0900169 // Clear the event callback objects since the HAL start failed.
170 event_cb_handler_.invalidate();
Roshan Pius52947fb2016-11-18 11:38:07 -0800171 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700172 return wifi_status;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700173}
174
Roshan Pius155344b2017-08-11 15:47:42 -0700175WifiStatus Wifi::stopInternal(
176 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700177 if (run_state_ == RunState::STOPPED) {
178 return createWifiStatus(WifiStatusCode::SUCCESS);
179 } else if (run_state_ == RunState::STOPPING) {
180 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
181 "HAL is stopping");
Roshan Piusaabe5752016-09-29 09:03:59 -0700182 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700183 // Clear the chip object and its child objects since the HAL is now
184 // stopped.
Jimmy Chen2dddd792019-12-23 17:50:39 +0200185 for (auto& chip : chips_) {
186 if (chip.get()) {
187 chip->invalidate();
188 chip.clear();
189 }
Roshan Piusaabe5752016-09-29 09:03:59 -0700190 }
Jimmy Chen2dddd792019-12-23 17:50:39 +0200191 chips_.clear();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700192 WifiStatus wifi_status = stopLegacyHalAndDeinitializeModeController(lock);
193 if (wifi_status.code == WifiStatusCode::SUCCESS) {
194 for (const auto& callback : event_cb_handler_.getCallbacks()) {
195 if (!callback->onStop().isOk()) {
196 LOG(ERROR) << "Failed to invoke onStop callback";
197 };
198 }
199 LOG(INFO) << "Wifi HAL stopped";
200 } else {
201 for (const auto& callback : event_cb_handler_.getCallbacks()) {
202 if (!callback->onFailure(wifi_status).isOk()) {
203 LOG(ERROR) << "Failed to invoke onFailure callback";
204 }
205 }
206 LOG(ERROR) << "Wifi HAL stop failed";
207 }
Kyounghan Leef2e21c22020-01-29 14:56:15 +0900208 // Clear the event callback objects since the HAL is now stopped.
209 event_cb_handler_.invalidate();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700210 return wifi_status;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700211}
212
Roshan Pius56476652016-10-26 14:43:05 -0700213std::pair<WifiStatus, std::vector<ChipId>> Wifi::getChipIdsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700214 std::vector<ChipId> chip_ids;
Jimmy Chen2dddd792019-12-23 17:50:39 +0200215
216 for (auto& chip : chips_) {
217 ChipId chip_id = getChipIdFromWifiChip(chip);
218 if (chip_id != UINT32_MAX) chip_ids.emplace_back(chip_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700219 }
220 return {createWifiStatus(WifiStatusCode::SUCCESS), std::move(chip_ids)};
Roshan Piuscd566bd2016-10-10 08:03:42 -0700221}
222
Jimmy Chend460df32019-11-29 17:31:22 +0200223std::pair<WifiStatus, sp<V1_4::IWifiChip>> Wifi::getChipInternal(
224 ChipId chip_id) {
Jimmy Chen2dddd792019-12-23 17:50:39 +0200225 for (auto& chip : chips_) {
226 ChipId cand_id = getChipIdFromWifiChip(chip);
227 if ((cand_id != UINT32_MAX) && (cand_id == chip_id))
228 return {createWifiStatus(WifiStatusCode::SUCCESS), chip};
Roshan Piusabcf78f2017-10-06 16:30:38 -0700229 }
Jimmy Chen2dddd792019-12-23 17:50:39 +0200230
231 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700232}
Roshan Pius52947fb2016-11-18 11:38:07 -0800233
Roshan Pius8fc6d172017-11-27 16:08:27 -0800234WifiStatus Wifi::initializeModeControllerAndLegacyHal() {
235 if (!mode_controller_->initialize()) {
236 LOG(ERROR) << "Failed to initialize firmware mode controller";
237 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
238 }
Jimmy Chen2dddd792019-12-23 17:50:39 +0200239
240 legacy_hals_ = legacy_hal_factory_->getHals();
241 if (legacy_hals_.empty())
242 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
243 int index = 0; // for failure log
244 for (auto& hal : legacy_hals_) {
245 legacy_hal::wifi_error legacy_status = hal->initialize();
246 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
247 // Currently WifiLegacyHal::initialize does not allocate extra mem,
248 // only initializes the function table. If this changes, need to
249 // implement WifiLegacyHal::deinitialize and deinitalize the
250 // HALs already initialized
251 LOG(ERROR) << "Failed to initialize legacy HAL index: " << index
252 << " error: " << legacyErrorToString(legacy_status);
253 return createWifiStatusFromLegacyError(legacy_status);
254 }
255 index++;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700256 }
257 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius52947fb2016-11-18 11:38:07 -0800258}
259
Roshan Pius155344b2017-08-11 15:47:42 -0700260WifiStatus Wifi::stopLegacyHalAndDeinitializeModeController(
261 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) {
Jimmy Chen2dddd792019-12-23 17:50:39 +0200262 legacy_hal::wifi_error legacy_status = legacy_hal::WIFI_SUCCESS;
263 int index = 0;
264
Roshan Piusabcf78f2017-10-06 16:30:38 -0700265 run_state_ = RunState::STOPPING;
Jimmy Chen2dddd792019-12-23 17:50:39 +0200266 for (auto& hal : legacy_hals_) {
267 legacy_hal::wifi_error tmp = hal->stop(lock, [&]() {});
268 if (tmp != legacy_hal::WIFI_SUCCESS) {
269 LOG(ERROR) << "Failed to stop legacy HAL index: " << index
270 << " error: " << legacyErrorToString(legacy_status);
271 legacy_status = tmp;
272 }
273 index++;
274 }
275 run_state_ = RunState::STOPPED;
276
Roshan Piusabcf78f2017-10-06 16:30:38 -0700277 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Jimmy Chen2dddd792019-12-23 17:50:39 +0200278 LOG(ERROR) << "One or more legacy HALs failed to stop";
Roshan Piusabcf78f2017-10-06 16:30:38 -0700279 return createWifiStatusFromLegacyError(legacy_status);
280 }
281 if (!mode_controller_->deinitialize()) {
282 LOG(ERROR) << "Failed to deinitialize firmware mode controller";
283 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
284 }
285 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius52947fb2016-11-18 11:38:07 -0800286}
Jimmy Chen2dddd792019-12-23 17:50:39 +0200287
288ChipId Wifi::getChipIdFromWifiChip(sp<WifiChip>& chip) {
289 ChipId chip_id = UINT32_MAX;
290 if (chip.get()) {
291 chip->getId([&](WifiStatus status, uint32_t id) {
292 if (status.code == WifiStatusCode::SUCCESS) {
293 chip_id = id;
294 }
295 });
296 }
297
298 return chip_id;
299}
Roshan Pius79a99752016-10-04 13:03:58 -0700300} // namespace implementation
Jimmy Chend460df32019-11-29 17:31:22 +0200301} // namespace V1_5
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700302} // namespace wifi
303} // namespace hardware
304} // namespace android