blob: 332363de6a20d7de6ee8fdcae1af609e36ed407c [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 {
24// Chip ID to use for the only supported chip.
25static constexpr android::hardware::wifi::V1_0::ChipId kChipId = 0;
26} // namespace
27
Roshan Pius3c4e8a32016-10-03 14:53:58 -070028namespace android {
29namespace hardware {
30namespace wifi {
Roshan Pius79a99752016-10-04 13:03:58 -070031namespace V1_0 {
32namespace implementation {
Roshan Pius56476652016-10-26 14:43:05 -070033using hidl_return_util::validateAndCall;
Roshan Pius3c4e8a32016-10-03 14:53:58 -070034
Roshan Piusaabe5752016-09-29 09:03:59 -070035Wifi::Wifi()
Roshan Pius6cedc972016-10-28 10:11:17 -070036 : legacy_hal_(new legacy_hal::WifiLegacyHal()),
Roshan Pius52947fb2016-11-18 11:38:07 -080037 mode_controller_(new mode_controller::WifiModeController()),
Roshan Pius6cedc972016-10-28 10:11:17 -070038 run_state_(RunState::STOPPED) {}
Roshan Pius3c4e8a32016-10-03 14:53:58 -070039
Roshan Pius56476652016-10-26 14:43:05 -070040bool Wifi::isValid() {
41 // This object is always valid.
42 return true;
43}
44
Roshan Pius3c4e8a32016-10-03 14:53:58 -070045Return<void> Wifi::registerEventCallback(
Roshan Pius56476652016-10-26 14:43:05 -070046 const sp<IWifiEventCallback>& event_callback,
47 registerEventCallback_cb hidl_status_cb) {
48 return validateAndCall(this,
49 WifiStatusCode::ERROR_UNKNOWN,
50 &Wifi::registerEventCallbackInternal,
51 hidl_status_cb,
52 event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -070053}
54
55Return<bool> Wifi::isStarted() {
Roshan Piusaabe5752016-09-29 09:03:59 -070056 return run_state_ != RunState::STOPPED;
Roshan Pius3c4e8a32016-10-03 14:53:58 -070057}
58
Roshan Pius503582e2016-10-11 08:25:30 -070059Return<void> Wifi::start(start_cb hidl_status_cb) {
Roshan Pius56476652016-10-26 14:43:05 -070060 return validateAndCall(this,
61 WifiStatusCode::ERROR_UNKNOWN,
62 &Wifi::startInternal,
63 hidl_status_cb);
64}
65
66Return<void> Wifi::stop(stop_cb hidl_status_cb) {
67 return validateAndCall(
68 this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::stopInternal, hidl_status_cb);
69}
70
71Return<void> Wifi::getChipIds(getChipIds_cb hidl_status_cb) {
72 return validateAndCall(this,
73 WifiStatusCode::ERROR_UNKNOWN,
74 &Wifi::getChipIdsInternal,
75 hidl_status_cb);
76}
77
78Return<void> Wifi::getChip(ChipId chip_id, getChip_cb hidl_status_cb) {
79 return validateAndCall(this,
80 WifiStatusCode::ERROR_UNKNOWN,
81 &Wifi::getChipInternal,
82 hidl_status_cb,
83 chip_id);
84}
85
86WifiStatus Wifi::registerEventCallbackInternal(
87 const sp<IWifiEventCallback>& event_callback) {
88 // TODO(b/31632518): remove the callback when the client is destroyed
89 event_callbacks_.emplace_back(event_callback);
90 return createWifiStatus(WifiStatusCode::SUCCESS);
91}
92
93WifiStatus Wifi::startInternal() {
Roshan Piusaabe5752016-09-29 09:03:59 -070094 if (run_state_ == RunState::STARTED) {
Roshan Pius56476652016-10-26 14:43:05 -070095 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Piusaabe5752016-09-29 09:03:59 -070096 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius56476652016-10-26 14:43:05 -070097 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
98 "HAL is stopping");
Roshan Pius3c4e8a32016-10-03 14:53:58 -070099 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800100 WifiStatus wifi_status = initializeLegacyHal();
101 if (wifi_status.code == WifiStatusCode::SUCCESS) {
102 // Create the chip instance once the HAL is started.
103 chip_ = new WifiChip(kChipId, legacy_hal_, mode_controller_);
104 run_state_ = RunState::STARTED;
105 for (const auto& callback : event_callbacks_) {
106 if (!callback->onStart().getStatus().isOk()) {
107 LOG(ERROR) << "Failed to invoke onStart callback";
108 };
109 }
110 for (const auto& callback : event_callbacks_) {
111 if (!callback->onFailure(wifi_status).getStatus().isOk()) {
112 LOG(ERROR) << "Failed to invoke onFailure callback";
113 }
114 }
115 } else {
116 for (const auto& callback : event_callbacks_) {
117 if (!callback->onFailure(wifi_status).getStatus().isOk()) {
118 LOG(ERROR) << "Failed to invoke onFailure callback";
119 }
120 }
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700121 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800122 return wifi_status;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700123}
124
Roshan Pius56476652016-10-26 14:43:05 -0700125WifiStatus Wifi::stopInternal() {
Roshan Piusaabe5752016-09-29 09:03:59 -0700126 if (run_state_ == RunState::STOPPED) {
Roshan Pius56476652016-10-26 14:43:05 -0700127 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Piusaabe5752016-09-29 09:03:59 -0700128 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius56476652016-10-26 14:43:05 -0700129 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
130 "HAL is stopping");
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700131 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800132 WifiStatus wifi_status = stopLegacyHalAndDeinitializeModeController();
133 if (wifi_status.code == WifiStatusCode::SUCCESS) {
Roshan Pius503582e2016-10-11 08:25:30 -0700134 for (const auto& callback : event_callbacks_) {
135 if (!callback->onStop().getStatus().isOk()) {
136 LOG(ERROR) << "Failed to invoke onStop callback";
137 };
Roshan Piusaabe5752016-09-29 09:03:59 -0700138 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800139 } else {
Roshan Pius503582e2016-10-11 08:25:30 -0700140 for (const auto& callback : event_callbacks_) {
Roshan Pius52947fb2016-11-18 11:38:07 -0800141 if (!callback->onFailure(wifi_status).getStatus().isOk()) {
142 LOG(ERROR) << "Failed to invoke onFailure callback";
143 }
Roshan Piusaabe5752016-09-29 09:03:59 -0700144 }
145 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800146 return wifi_status;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700147}
148
Roshan Pius56476652016-10-26 14:43:05 -0700149std::pair<WifiStatus, std::vector<ChipId>> Wifi::getChipIdsInternal() {
Roshan Piuscd566bd2016-10-10 08:03:42 -0700150 std::vector<ChipId> chip_ids;
151 if (chip_.get()) {
152 chip_ids.emplace_back(kChipId);
153 }
Roshan Pius56476652016-10-26 14:43:05 -0700154 return {createWifiStatus(WifiStatusCode::SUCCESS), std::move(chip_ids)};
Roshan Piuscd566bd2016-10-10 08:03:42 -0700155}
156
Roshan Pius56476652016-10-26 14:43:05 -0700157std::pair<WifiStatus, sp<IWifiChip>> Wifi::getChipInternal(ChipId chip_id) {
158 if (!chip_.get()) {
159 return {createWifiStatus(WifiStatusCode::ERROR_NOT_STARTED), nullptr};
Roshan Piuscd566bd2016-10-10 08:03:42 -0700160 }
Roshan Pius56476652016-10-26 14:43:05 -0700161 if (chip_id != kChipId) {
162 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
163 }
164 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_};
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700165}
Roshan Pius52947fb2016-11-18 11:38:07 -0800166
167WifiStatus Wifi::initializeLegacyHal() {
Roshan Pius52947fb2016-11-18 11:38:07 -0800168 legacy_hal::wifi_error legacy_status = legacy_hal_->initialize();
169 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
170 LOG(ERROR) << "Failed to initialize legacy HAL: "
171 << legacyErrorToString(legacy_status);
172 return createWifiStatusFromLegacyError(legacy_status);
173 }
174 return createWifiStatus(WifiStatusCode::SUCCESS);
175}
176
177WifiStatus Wifi::stopLegacyHalAndDeinitializeModeController() {
Roshan Pius52947fb2016-11-18 11:38:07 -0800178 run_state_ = RunState::STOPPING;
179 const auto on_complete_callback_ = [&]() {
180 if (chip_.get()) {
181 chip_->invalidate();
182 }
183 chip_.clear();
184 run_state_ = RunState::STOPPED;
185 };
186 legacy_hal::wifi_error legacy_status =
187 legacy_hal_->stop(on_complete_callback_);
188 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
189 LOG(ERROR) << "Failed to stop legacy HAL: "
190 << legacyErrorToString(legacy_status);
191 return createWifiStatusFromLegacyError(legacy_status);
192 }
193 if (!mode_controller_->deinitialize()) {
194 LOG(ERROR) << "Failed to deinitialize firmware mode controller";
195 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
196 }
197 return createWifiStatus(WifiStatusCode::SUCCESS);
198}
Roshan Pius79a99752016-10-04 13:03:58 -0700199} // namespace implementation
200} // namespace V1_0
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700201} // namespace wifi
202} // namespace hardware
203} // namespace android