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
17 #include <android-base/logging.h>
18
19 #include "hidl_return_util.h"
20 #include "wifi.h"
21 #include "wifi_status_util.h"
22
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
28 namespace android {
29 namespace hardware {
30 namespace wifi {
31 namespace V1_1 {
32 namespace implementation {
33 using hidl_return_util::validateAndCall;
34 using hidl_return_util::validateAndCallWithLock;
35
Wifi()36 Wifi::Wifi()
37 : legacy_hal_(new legacy_hal::WifiLegacyHal()),
38 mode_controller_(new mode_controller::WifiModeController()),
39 run_state_(RunState::STOPPED) {}
40
isValid()41 bool Wifi::isValid() {
42 // This object is always valid.
43 return true;
44 }
45
registerEventCallback(const sp<IWifiEventCallback> & event_callback,registerEventCallback_cb hidl_status_cb)46 Return<void> Wifi::registerEventCallback(
47 const sp<IWifiEventCallback>& event_callback,
48 registerEventCallback_cb hidl_status_cb) {
49 return validateAndCall(this,
50 WifiStatusCode::ERROR_UNKNOWN,
51 &Wifi::registerEventCallbackInternal,
52 hidl_status_cb,
53 event_callback);
54 }
55
isStarted()56 Return<bool> Wifi::isStarted() {
57 return run_state_ != RunState::STOPPED;
58 }
59
start(start_cb hidl_status_cb)60 Return<void> Wifi::start(start_cb hidl_status_cb) {
61 return validateAndCall(this,
62 WifiStatusCode::ERROR_UNKNOWN,
63 &Wifi::startInternal,
64 hidl_status_cb);
65 }
66
stop(stop_cb hidl_status_cb)67 Return<void> Wifi::stop(stop_cb hidl_status_cb) {
68 return validateAndCallWithLock(this, WifiStatusCode::ERROR_UNKNOWN,
69 &Wifi::stopInternal, hidl_status_cb);
70 }
71
getChipIds(getChipIds_cb hidl_status_cb)72 Return<void> Wifi::getChipIds(getChipIds_cb hidl_status_cb) {
73 return validateAndCall(this,
74 WifiStatusCode::ERROR_UNKNOWN,
75 &Wifi::getChipIdsInternal,
76 hidl_status_cb);
77 }
78
getChip(ChipId chip_id,getChip_cb hidl_status_cb)79 Return<void> Wifi::getChip(ChipId chip_id, getChip_cb hidl_status_cb) {
80 return validateAndCall(this,
81 WifiStatusCode::ERROR_UNKNOWN,
82 &Wifi::getChipInternal,
83 hidl_status_cb,
84 chip_id);
85 }
86
registerEventCallbackInternal(const sp<IWifiEventCallback> & event_callback)87 WifiStatus Wifi::registerEventCallbackInternal(
88 const sp<IWifiEventCallback>& event_callback) {
89 if (!event_cb_handler_.addCallback(event_callback)) {
90 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
91 }
92 return createWifiStatus(WifiStatusCode::SUCCESS);
93 }
94
startInternal()95 WifiStatus Wifi::startInternal() {
96 if (run_state_ == RunState::STARTED) {
97 return createWifiStatus(WifiStatusCode::SUCCESS);
98 } else if (run_state_ == RunState::STOPPING) {
99 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
100 "HAL is stopping");
101 }
102 WifiStatus wifi_status = initializeLegacyHal();
103 if (wifi_status.code == WifiStatusCode::SUCCESS) {
104 // Create the chip instance once the HAL is started.
105 chip_ = new WifiChip(kChipId, legacy_hal_, mode_controller_);
106 run_state_ = RunState::STARTED;
107 for (const auto& callback : event_cb_handler_.getCallbacks()) {
108 if (!callback->onStart().isOk()) {
109 LOG(ERROR) << "Failed to invoke onStart callback";
110 };
111 }
112 LOG(INFO) << "Wifi HAL started";
113 } else {
114 for (const auto& callback : event_cb_handler_.getCallbacks()) {
115 if (!callback->onFailure(wifi_status).isOk()) {
116 LOG(ERROR) << "Failed to invoke onFailure callback";
117 }
118 }
119 LOG(ERROR) << "Wifi HAL start failed";
120 }
121 return wifi_status;
122 }
123
stopInternal(std::unique_lock<std::recursive_mutex> * lock)124 WifiStatus Wifi::stopInternal(
125 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) {
126 if (run_state_ == RunState::STOPPED) {
127 return createWifiStatus(WifiStatusCode::SUCCESS);
128 } else if (run_state_ == RunState::STOPPING) {
129 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
130 "HAL is stopping");
131 }
132 // Clear the chip object and its child objects since the HAL is now
133 // stopped.
134 if (chip_.get()) {
135 chip_->invalidate();
136 chip_.clear();
137 }
138 WifiStatus wifi_status = stopLegacyHalAndDeinitializeModeController(lock);
139 if (wifi_status.code == WifiStatusCode::SUCCESS) {
140 for (const auto& callback : event_cb_handler_.getCallbacks()) {
141 if (!callback->onStop().isOk()) {
142 LOG(ERROR) << "Failed to invoke onStop callback";
143 };
144 }
145 LOG(INFO) << "Wifi HAL stopped";
146 } else {
147 for (const auto& callback : event_cb_handler_.getCallbacks()) {
148 if (!callback->onFailure(wifi_status).isOk()) {
149 LOG(ERROR) << "Failed to invoke onFailure callback";
150 }
151 }
152 LOG(ERROR) << "Wifi HAL stop failed";
153 }
154 return wifi_status;
155 }
156
getChipIdsInternal()157 std::pair<WifiStatus, std::vector<ChipId>> Wifi::getChipIdsInternal() {
158 std::vector<ChipId> chip_ids;
159 if (chip_.get()) {
160 chip_ids.emplace_back(kChipId);
161 }
162 return {createWifiStatus(WifiStatusCode::SUCCESS), std::move(chip_ids)};
163 }
164
getChipInternal(ChipId chip_id)165 std::pair<WifiStatus, sp<IWifiChip>> Wifi::getChipInternal(ChipId chip_id) {
166 if (!chip_.get()) {
167 return {createWifiStatus(WifiStatusCode::ERROR_NOT_STARTED), nullptr};
168 }
169 if (chip_id != kChipId) {
170 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
171 }
172 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_};
173 }
174
initializeLegacyHal()175 WifiStatus Wifi::initializeLegacyHal() {
176 legacy_hal::wifi_error legacy_status = legacy_hal_->initialize();
177 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
178 LOG(ERROR) << "Failed to initialize legacy HAL: "
179 << legacyErrorToString(legacy_status);
180 return createWifiStatusFromLegacyError(legacy_status);
181 }
182 return createWifiStatus(WifiStatusCode::SUCCESS);
183 }
184
stopLegacyHalAndDeinitializeModeController(std::unique_lock<std::recursive_mutex> * lock)185 WifiStatus Wifi::stopLegacyHalAndDeinitializeModeController(
186 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) {
187 run_state_ = RunState::STOPPING;
188 legacy_hal::wifi_error legacy_status =
189 legacy_hal_->stop(lock, [&]() { run_state_ = RunState::STOPPED; });
190 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
191 LOG(ERROR) << "Failed to stop legacy HAL: "
192 << legacyErrorToString(legacy_status);
193 return createWifiStatusFromLegacyError(legacy_status);
194 }
195 if (!mode_controller_->deinitialize()) {
196 LOG(ERROR) << "Failed to deinitialize firmware mode controller";
197 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
198 }
199 return createWifiStatus(WifiStatusCode::SUCCESS);
200 }
201 } // namespace implementation
202 } // namespace V1_1
203 } // namespace wifi
204 } // namespace hardware
205 } // namespace android
206