blob: 35f27e21d281f7ad9d4df6f7e7984e9d74f41647 [file] [log] [blame]
Gabriel Birenf3262f92022-07-15 23:25:39 +00001/*
2 * Copyright (C) 2022 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
17#include "wifi.h"
18
Kevin Jeon03317722023-04-13 12:14:11 -040019#include <android-base/file.h>
Gabriel Birenf3262f92022-07-15 23:25:39 +000020#include <android-base/logging.h>
21
22#include "aidl_return_util.h"
23#include "wifi_status_util.h"
24
25namespace {
26// Starting Chip ID, will be assigned to primary chip
27static constexpr int32_t kPrimaryChipId = 0;
28} // namespace
29
30namespace aidl {
31namespace android {
32namespace hardware {
33namespace wifi {
34using aidl_return_util::validateAndCall;
35using aidl_return_util::validateAndCallWithLock;
36
37Wifi::Wifi(const std::shared_ptr<::android::wifi_system::InterfaceTool> iface_tool,
38 const std::shared_ptr<legacy_hal::WifiLegacyHalFactory> legacy_hal_factory,
39 const std::shared_ptr<mode_controller::WifiModeController> mode_controller,
40 const std::shared_ptr<feature_flags::WifiFeatureFlags> feature_flags)
41 : iface_tool_(iface_tool),
42 legacy_hal_factory_(legacy_hal_factory),
43 mode_controller_(mode_controller),
44 feature_flags_(feature_flags),
45 run_state_(RunState::STOPPED) {}
46
47bool Wifi::isValid() {
48 // This object is always valid.
49 return true;
50}
51
52ndk::ScopedAStatus Wifi::registerEventCallback(
53 const std::shared_ptr<IWifiEventCallback>& in_callback) {
54 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN,
55 &Wifi::registerEventCallbackInternal, in_callback);
56}
57
58ndk::ScopedAStatus Wifi::isStarted(bool* _aidl_return) {
59 *_aidl_return = (run_state_ != RunState::STOPPED);
60 return ndk::ScopedAStatus::ok();
61}
62
63ndk::ScopedAStatus Wifi::start() {
64 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::startInternal);
65}
66
67ndk::ScopedAStatus Wifi::stop() {
68 return validateAndCallWithLock(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::stopInternal);
69}
70
71ndk::ScopedAStatus Wifi::getChipIds(std::vector<int32_t>* _aidl_return) {
72 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::getChipIdsInternal,
73 _aidl_return);
74}
75
76ndk::ScopedAStatus Wifi::getChip(int32_t in_chipId, std::shared_ptr<IWifiChip>* _aidl_return) {
77 return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::getChipInternal,
78 _aidl_return, in_chipId);
79}
80
81binder_status_t Wifi::dump(int fd, const char** args, uint32_t numArgs) {
82 LOG(INFO) << "-----------Debug was called----------------";
83 if (chips_.size() == 0) {
84 LOG(INFO) << "No chips to display.";
85 return STATUS_OK;
86 }
87
88 for (std::shared_ptr<WifiChip> chip : chips_) {
89 if (!chip.get()) continue;
90 chip->dump(fd, args, numArgs);
91 }
Kevin Jeon03317722023-04-13 12:14:11 -040092 ::android::base::WriteStringToFd("\n", fd);
Gabriel Birenf3262f92022-07-15 23:25:39 +000093 return STATUS_OK;
94}
95
96ndk::ScopedAStatus Wifi::registerEventCallbackInternal(
97 const std::shared_ptr<IWifiEventCallback>& event_callback) {
98 if (!event_cb_handler_.addCallback(event_callback)) {
99 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
100 }
101 return ndk::ScopedAStatus::ok();
102}
103
104ndk::ScopedAStatus Wifi::startInternal() {
105 if (run_state_ == RunState::STARTED) {
106 return ndk::ScopedAStatus::ok();
107 } else if (run_state_ == RunState::STOPPING) {
108 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, "HAL is stopping");
109 }
110 ndk::ScopedAStatus wifi_status = initializeModeControllerAndLegacyHal();
111 if (wifi_status.isOk()) {
112 // Register the callback for subsystem restart
113 const auto& on_subsystem_restart_callback = [this](const std::string& error) {
114 ndk::ScopedAStatus wifi_status = createWifiStatus(WifiStatusCode::ERROR_UNKNOWN, error);
115 for (const auto& callback : event_cb_handler_.getCallbacks()) {
116 LOG(INFO) << "Attempting to invoke onSubsystemRestart "
117 "callback";
118 WifiStatusCode errorCode =
119 static_cast<WifiStatusCode>(wifi_status.getServiceSpecificError());
120 if (!callback->onSubsystemRestart(errorCode).isOk()) {
121 LOG(ERROR) << "Failed to invoke onSubsystemRestart callback";
122 } else {
123 LOG(INFO) << "Succeeded to invoke onSubsystemRestart "
124 "callback";
125 }
126 }
127 };
128
129 // Create the chip instance once the HAL is started.
130 int32_t chipId = kPrimaryChipId;
131 for (auto& hal : legacy_hals_) {
132 chips_.push_back(
133 WifiChip::create(chipId, chipId == kPrimaryChipId, hal, mode_controller_,
134 std::make_shared<iface_util::WifiIfaceUtil>(iface_tool_, hal),
135 feature_flags_, on_subsystem_restart_callback));
136 chipId++;
137 }
138 run_state_ = RunState::STARTED;
139 for (const auto& callback : event_cb_handler_.getCallbacks()) {
140 if (!callback->onStart().isOk()) {
141 LOG(ERROR) << "Failed to invoke onStart callback";
142 };
143 }
144 LOG(INFO) << "Wifi HAL started";
145 } else {
146 for (const auto& callback : event_cb_handler_.getCallbacks()) {
147 WifiStatusCode errorCode =
148 static_cast<WifiStatusCode>(wifi_status.getServiceSpecificError());
149 if (!callback->onFailure(errorCode).isOk()) {
150 LOG(ERROR) << "Failed to invoke onFailure callback";
151 }
152 }
153 LOG(ERROR) << "Wifi HAL start failed";
154 // Clear the event callback objects since the HAL start failed.
155 event_cb_handler_.invalidate();
156 }
157 return wifi_status;
158}
159
160ndk::ScopedAStatus Wifi::stopInternal(
161 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) {
162 if (run_state_ == RunState::STOPPED) {
163 return ndk::ScopedAStatus::ok();
164 } else if (run_state_ == RunState::STOPPING) {
165 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, "HAL is stopping");
166 }
167 // Clear the chip object and its child objects since the HAL is now
168 // stopped.
169 for (auto& chip : chips_) {
170 if (chip.get()) {
171 chip->invalidate();
172 chip.reset();
173 }
174 }
175 chips_.clear();
176 ndk::ScopedAStatus wifi_status = stopLegacyHalAndDeinitializeModeController(lock);
177 if (wifi_status.isOk()) {
178 for (const auto& callback : event_cb_handler_.getCallbacks()) {
179 if (!callback->onStop().isOk()) {
180 LOG(ERROR) << "Failed to invoke onStop callback";
181 };
182 }
183 LOG(INFO) << "Wifi HAL stopped";
184 } else {
185 for (const auto& callback : event_cb_handler_.getCallbacks()) {
186 WifiStatusCode errorCode =
187 static_cast<WifiStatusCode>(wifi_status.getServiceSpecificError());
188 if (!callback->onFailure(errorCode).isOk()) {
189 LOG(ERROR) << "Failed to invoke onFailure callback";
190 }
191 }
192 LOG(ERROR) << "Wifi HAL stop failed";
193 }
194 // Clear the event callback objects since the HAL is now stopped.
195 event_cb_handler_.invalidate();
196 return wifi_status;
197}
198
199std::pair<std::vector<int32_t>, ndk::ScopedAStatus> Wifi::getChipIdsInternal() {
200 std::vector<int32_t> chip_ids;
201
202 for (auto& chip : chips_) {
203 int32_t chip_id = getChipIdFromWifiChip(chip);
204 if (chip_id != INT32_MAX) chip_ids.emplace_back(chip_id);
205 }
206 return {std::move(chip_ids), ndk::ScopedAStatus::ok()};
207}
208
209std::pair<std::shared_ptr<IWifiChip>, ndk::ScopedAStatus> Wifi::getChipInternal(int32_t chip_id) {
210 for (auto& chip : chips_) {
211 int32_t cand_id = getChipIdFromWifiChip(chip);
212 if ((cand_id != INT32_MAX) && (cand_id == chip_id)) return {chip, ndk::ScopedAStatus::ok()};
213 }
214
215 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
216}
217
218ndk::ScopedAStatus Wifi::initializeModeControllerAndLegacyHal() {
219 if (!mode_controller_->initialize()) {
220 LOG(ERROR) << "Failed to initialize firmware mode controller";
221 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
222 }
223
224 legacy_hals_ = legacy_hal_factory_->getHals();
225 if (legacy_hals_.empty()) return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
226 int index = 0; // for failure log
227 for (auto& hal : legacy_hals_) {
228 legacy_hal::wifi_error legacy_status = hal->initialize();
229 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
230 // Currently WifiLegacyHal::initialize does not allocate extra mem,
231 // only initializes the function table. If this changes, need to
232 // implement WifiLegacyHal::deinitialize and deinitalize the
233 // HALs already initialized
234 LOG(ERROR) << "Failed to initialize legacy HAL index: " << index
235 << " error: " << legacyErrorToString(legacy_status);
236 return createWifiStatusFromLegacyError(legacy_status);
237 }
238 index++;
239 }
240 return ndk::ScopedAStatus::ok();
241}
242
243ndk::ScopedAStatus Wifi::stopLegacyHalAndDeinitializeModeController(
244 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) {
245 legacy_hal::wifi_error legacy_status = legacy_hal::WIFI_SUCCESS;
246 int index = 0;
247
248 run_state_ = RunState::STOPPING;
249 for (auto& hal : legacy_hals_) {
250 legacy_hal::wifi_error tmp = hal->stop(lock, [&]() {});
251 if (tmp != legacy_hal::WIFI_SUCCESS) {
252 LOG(ERROR) << "Failed to stop legacy HAL index: " << index
253 << " error: " << legacyErrorToString(legacy_status);
254 legacy_status = tmp;
255 }
256 index++;
257 }
258 run_state_ = RunState::STOPPED;
259
260 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
261 LOG(ERROR) << "One or more legacy HALs failed to stop";
262 return createWifiStatusFromLegacyError(legacy_status);
263 }
264 if (!mode_controller_->deinitialize()) {
265 LOG(ERROR) << "Failed to deinitialize firmware mode controller";
266 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
267 }
268 return ndk::ScopedAStatus::ok();
269}
270
271int32_t Wifi::getChipIdFromWifiChip(std::shared_ptr<WifiChip>& chip) {
272 int32_t chip_id = INT32_MAX;
273 if (chip.get()) {
274 ndk::ScopedAStatus status = chip->getId(&chip_id);
275 if (!status.isOk()) {
276 // Reset value if operation failed.
277 chip_id = INT32_MAX;
278 }
279 }
280 return chip_id;
281}
282
283} // namespace wifi
284} // namespace hardware
285} // namespace android
286} // namespace aidl