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