blob: c302ce24342082e442641575cab3928082ef1b97 [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 {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080031namespace V1_6 {
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
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080036Wifi::Wifi(const std::shared_ptr<wifi_system::InterfaceTool> iface_tool,
37 const std::shared_ptr<legacy_hal::WifiLegacyHalFactory> legacy_hal_factory,
38 const std::shared_ptr<mode_controller::WifiModeController> mode_controller,
39 const std::shared_ptr<feature_flags::WifiFeatureFlags> feature_flags)
Roshan Piusc885df02019-05-21 14:49:05 -070040 : iface_tool_(iface_tool),
Jimmy Chen2dddd792019-12-23 17:50:39 +020041 legacy_hal_factory_(legacy_hal_factory),
Roshan Pius200a17d2017-11-01 13:03:35 -070042 mode_controller_(mode_controller),
43 feature_flags_(feature_flags),
Roshan Pius6cedc972016-10-28 10:11:17 -070044 run_state_(RunState::STOPPED) {}
Roshan Pius3c4e8a32016-10-03 14:53:58 -070045
Roshan Pius56476652016-10-26 14:43:05 -070046bool Wifi::isValid() {
Roshan Piusabcf78f2017-10-06 16:30:38 -070047 // This object is always valid.
48 return true;
Roshan Pius56476652016-10-26 14:43:05 -070049}
50
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080051Return<void> Wifi::registerEventCallback(const sp<V1_0::IWifiEventCallback>& event_callback,
52 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070053 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080054 &Wifi::registerEventCallbackInternal, hidl_status_cb, event_callback);
55}
56
57Return<void> Wifi::registerEventCallback_1_5(const sp<V1_5::IWifiEventCallback>& event_callback,
58 registerEventCallback_1_5_cb hidl_status_cb) {
59 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN,
60 &Wifi::registerEventCallbackInternal_1_5, hidl_status_cb,
Roshan Piusabcf78f2017-10-06 16:30:38 -070061 event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -070062}
63
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080064Return<bool> Wifi::isStarted() {
65 return run_state_ != RunState::STOPPED;
chenpaulb690fb82021-03-16 22:49:18 +080066}
67
Roshan Pius503582e2016-10-11 08:25:30 -070068Return<void> Wifi::start(start_cb hidl_status_cb) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080069 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::startInternal,
70 hidl_status_cb);
Roshan Pius56476652016-10-26 14:43:05 -070071}
72
73Return<void> Wifi::stop(stop_cb hidl_status_cb) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080074 return validateAndCallWithLock(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::stopInternal,
75 hidl_status_cb);
Roshan Pius56476652016-10-26 14:43:05 -070076}
77
78Return<void> Wifi::getChipIds(getChipIds_cb hidl_status_cb) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080079 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::getChipIdsInternal,
80 hidl_status_cb);
Roshan Pius56476652016-10-26 14:43:05 -070081}
82
83Return<void> Wifi::getChip(ChipId chip_id, getChip_cb hidl_status_cb) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080084 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::getChipInternal,
85 hidl_status_cb, chip_id);
Roshan Pius56476652016-10-26 14:43:05 -070086}
87
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080088Return<void> Wifi::debug(const hidl_handle& handle, const hidl_vec<hidl_string>&) {
xshu5899e8e2018-01-09 15:36:03 -080089 LOG(INFO) << "-----------Debug is called----------------";
Jimmy Chen2dddd792019-12-23 17:50:39 +020090 if (chips_.size() == 0) {
Roshan Pius7cf1df72018-01-24 19:13:09 -080091 return Void();
92 }
Jimmy Chen2dddd792019-12-23 17:50:39 +020093
94 for (sp<WifiChip> chip : chips_) {
95 if (!chip.get()) continue;
96
97 chip->debug(handle, {});
98 }
99 return Void();
xshu5899e8e2018-01-09 15:36:03 -0800100}
101
Roshan Pius56476652016-10-26 14:43:05 -0700102WifiStatus Wifi::registerEventCallbackInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800103 const sp<V1_0::IWifiEventCallback>& event_callback __unused) {
chenpaulb690fb82021-03-16 22:49:18 +0800104 // Deprecated support for this callback.
105 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
106}
107
108WifiStatus Wifi::registerEventCallbackInternal_1_5(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800109 const sp<V1_5::IWifiEventCallback>& event_callback) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700110 if (!event_cb_handler_.addCallback(event_callback)) {
111 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
112 }
113 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius56476652016-10-26 14:43:05 -0700114}
115
116WifiStatus Wifi::startInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700117 if (run_state_ == RunState::STARTED) {
118 return createWifiStatus(WifiStatusCode::SUCCESS);
119 } else if (run_state_ == RunState::STOPPING) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800120 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, "HAL is stopping");
Roshan Pius52947fb2016-11-18 11:38:07 -0800121 }
Roshan Pius8fc6d172017-11-27 16:08:27 -0800122 WifiStatus wifi_status = initializeModeControllerAndLegacyHal();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700123 if (wifi_status.code == WifiStatusCode::SUCCESS) {
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700124 // Register the callback for subsystem restart
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800125 const auto& on_subsystem_restart_callback = [this](const std::string& error) {
126 WifiStatus wifi_status = createWifiStatus(WifiStatusCode::ERROR_UNKNOWN, error);
127 for (const auto& callback : event_cb_handler_.getCallbacks()) {
128 LOG(INFO) << "Attempting to invoke onSubsystemRestart "
129 "callback";
130 if (!callback->onSubsystemRestart(wifi_status).isOk()) {
131 LOG(ERROR) << "Failed to invoke onSubsystemRestart callback";
132 } else {
133 LOG(INFO) << "Succeeded to invoke onSubsystemRestart "
chenpaul2fa7d522021-10-06 09:24:09 +0800134 "callback";
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700135 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800136 }
137 };
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700138
Roshan Piusabcf78f2017-10-06 16:30:38 -0700139 // Create the chip instance once the HAL is started.
Jimmy Chen2dddd792019-12-23 17:50:39 +0200140 android::hardware::wifi::V1_0::ChipId chipId = kPrimaryChipId;
141 for (auto& hal : legacy_hals_) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800142 chips_.push_back(
143 new WifiChip(chipId, chipId == kPrimaryChipId, hal, mode_controller_,
144 std::make_shared<iface_util::WifiIfaceUtil>(iface_tool_, hal),
145 feature_flags_, on_subsystem_restart_callback));
Jimmy Chen2dddd792019-12-23 17:50:39 +0200146 chipId++;
147 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700148 run_state_ = RunState::STARTED;
149 for (const auto& callback : event_cb_handler_.getCallbacks()) {
150 if (!callback->onStart().isOk()) {
151 LOG(ERROR) << "Failed to invoke onStart callback";
152 };
153 }
154 LOG(INFO) << "Wifi HAL started";
155 } else {
156 for (const auto& callback : event_cb_handler_.getCallbacks()) {
157 if (!callback->onFailure(wifi_status).isOk()) {
158 LOG(ERROR) << "Failed to invoke onFailure callback";
159 }
160 }
161 LOG(ERROR) << "Wifi HAL start failed";
Kyounghan Leef2e21c22020-01-29 14:56:15 +0900162 // Clear the event callback objects since the HAL start failed.
163 event_cb_handler_.invalidate();
Roshan Pius52947fb2016-11-18 11:38:07 -0800164 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700165 return wifi_status;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700166}
167
Roshan Pius155344b2017-08-11 15:47:42 -0700168WifiStatus Wifi::stopInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800169 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700170 if (run_state_ == RunState::STOPPED) {
171 return createWifiStatus(WifiStatusCode::SUCCESS);
172 } else if (run_state_ == RunState::STOPPING) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800173 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, "HAL is stopping");
Roshan Piusaabe5752016-09-29 09:03:59 -0700174 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700175 // Clear the chip object and its child objects since the HAL is now
176 // stopped.
Jimmy Chen2dddd792019-12-23 17:50:39 +0200177 for (auto& chip : chips_) {
178 if (chip.get()) {
179 chip->invalidate();
180 chip.clear();
181 }
Roshan Piusaabe5752016-09-29 09:03:59 -0700182 }
Jimmy Chen2dddd792019-12-23 17:50:39 +0200183 chips_.clear();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700184 WifiStatus wifi_status = stopLegacyHalAndDeinitializeModeController(lock);
185 if (wifi_status.code == WifiStatusCode::SUCCESS) {
186 for (const auto& callback : event_cb_handler_.getCallbacks()) {
187 if (!callback->onStop().isOk()) {
188 LOG(ERROR) << "Failed to invoke onStop callback";
189 };
190 }
191 LOG(INFO) << "Wifi HAL stopped";
192 } else {
193 for (const auto& callback : event_cb_handler_.getCallbacks()) {
194 if (!callback->onFailure(wifi_status).isOk()) {
195 LOG(ERROR) << "Failed to invoke onFailure callback";
196 }
197 }
198 LOG(ERROR) << "Wifi HAL stop failed";
199 }
Kyounghan Leef2e21c22020-01-29 14:56:15 +0900200 // Clear the event callback objects since the HAL is now stopped.
201 event_cb_handler_.invalidate();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700202 return wifi_status;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700203}
204
Roshan Pius56476652016-10-26 14:43:05 -0700205std::pair<WifiStatus, std::vector<ChipId>> Wifi::getChipIdsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700206 std::vector<ChipId> chip_ids;
Jimmy Chen2dddd792019-12-23 17:50:39 +0200207
208 for (auto& chip : chips_) {
209 ChipId chip_id = getChipIdFromWifiChip(chip);
210 if (chip_id != UINT32_MAX) chip_ids.emplace_back(chip_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700211 }
212 return {createWifiStatus(WifiStatusCode::SUCCESS), std::move(chip_ids)};
Roshan Piuscd566bd2016-10-10 08:03:42 -0700213}
214
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800215std::pair<WifiStatus, sp<V1_4::IWifiChip>> Wifi::getChipInternal(ChipId chip_id) {
Jimmy Chen2dddd792019-12-23 17:50:39 +0200216 for (auto& chip : chips_) {
217 ChipId cand_id = getChipIdFromWifiChip(chip);
218 if ((cand_id != UINT32_MAX) && (cand_id == chip_id))
219 return {createWifiStatus(WifiStatusCode::SUCCESS), chip};
Roshan Piusabcf78f2017-10-06 16:30:38 -0700220 }
Jimmy Chen2dddd792019-12-23 17:50:39 +0200221
222 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700223}
Roshan Pius52947fb2016-11-18 11:38:07 -0800224
Roshan Pius8fc6d172017-11-27 16:08:27 -0800225WifiStatus Wifi::initializeModeControllerAndLegacyHal() {
226 if (!mode_controller_->initialize()) {
227 LOG(ERROR) << "Failed to initialize firmware mode controller";
228 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
229 }
Jimmy Chen2dddd792019-12-23 17:50:39 +0200230
231 legacy_hals_ = legacy_hal_factory_->getHals();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800232 if (legacy_hals_.empty()) return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
Jimmy Chen2dddd792019-12-23 17:50:39 +0200233 int index = 0; // for failure log
234 for (auto& hal : legacy_hals_) {
235 legacy_hal::wifi_error legacy_status = hal->initialize();
236 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
237 // Currently WifiLegacyHal::initialize does not allocate extra mem,
238 // only initializes the function table. If this changes, need to
239 // implement WifiLegacyHal::deinitialize and deinitalize the
240 // HALs already initialized
241 LOG(ERROR) << "Failed to initialize legacy HAL index: " << index
242 << " error: " << legacyErrorToString(legacy_status);
243 return createWifiStatusFromLegacyError(legacy_status);
244 }
245 index++;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700246 }
247 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius52947fb2016-11-18 11:38:07 -0800248}
249
Roshan Pius155344b2017-08-11 15:47:42 -0700250WifiStatus Wifi::stopLegacyHalAndDeinitializeModeController(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800251 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) {
Jimmy Chen2dddd792019-12-23 17:50:39 +0200252 legacy_hal::wifi_error legacy_status = legacy_hal::WIFI_SUCCESS;
253 int index = 0;
254
Roshan Piusabcf78f2017-10-06 16:30:38 -0700255 run_state_ = RunState::STOPPING;
Jimmy Chen2dddd792019-12-23 17:50:39 +0200256 for (auto& hal : legacy_hals_) {
257 legacy_hal::wifi_error tmp = hal->stop(lock, [&]() {});
258 if (tmp != legacy_hal::WIFI_SUCCESS) {
259 LOG(ERROR) << "Failed to stop legacy HAL index: " << index
260 << " error: " << legacyErrorToString(legacy_status);
261 legacy_status = tmp;
262 }
263 index++;
264 }
265 run_state_ = RunState::STOPPED;
266
Roshan Piusabcf78f2017-10-06 16:30:38 -0700267 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Jimmy Chen2dddd792019-12-23 17:50:39 +0200268 LOG(ERROR) << "One or more legacy HALs failed to stop";
Roshan Piusabcf78f2017-10-06 16:30:38 -0700269 return createWifiStatusFromLegacyError(legacy_status);
270 }
271 if (!mode_controller_->deinitialize()) {
272 LOG(ERROR) << "Failed to deinitialize firmware mode controller";
273 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
274 }
275 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius52947fb2016-11-18 11:38:07 -0800276}
Jimmy Chen2dddd792019-12-23 17:50:39 +0200277
278ChipId Wifi::getChipIdFromWifiChip(sp<WifiChip>& chip) {
279 ChipId chip_id = UINT32_MAX;
280 if (chip.get()) {
281 chip->getId([&](WifiStatus status, uint32_t id) {
282 if (status.code == WifiStatusCode::SUCCESS) {
283 chip_id = id;
284 }
285 });
286 }
287
288 return chip_id;
289}
Roshan Pius79a99752016-10-04 13:03:58 -0700290} // namespace implementation
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800291} // namespace V1_6
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700292} // namespace wifi
293} // namespace hardware
294} // namespace android