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