blob: fd2cb9caebad4d026376a96d55f92fbf1b75de11 [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()
36 : legacy_hal_(new WifiLegacyHal()), run_state_(RunState::STOPPED) {}
Roshan Pius3c4e8a32016-10-03 14:53:58 -070037
Roshan Pius56476652016-10-26 14:43:05 -070038bool Wifi::isValid() {
39 // This object is always valid.
40 return true;
41}
42
Roshan Pius3c4e8a32016-10-03 14:53:58 -070043Return<void> Wifi::registerEventCallback(
Roshan Pius56476652016-10-26 14:43:05 -070044 const sp<IWifiEventCallback>& event_callback,
45 registerEventCallback_cb hidl_status_cb) {
46 return validateAndCall(this,
47 WifiStatusCode::ERROR_UNKNOWN,
48 &Wifi::registerEventCallbackInternal,
49 hidl_status_cb,
50 event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -070051}
52
53Return<bool> Wifi::isStarted() {
Roshan Piusaabe5752016-09-29 09:03:59 -070054 return run_state_ != RunState::STOPPED;
Roshan Pius3c4e8a32016-10-03 14:53:58 -070055}
56
Roshan Pius503582e2016-10-11 08:25:30 -070057Return<void> Wifi::start(start_cb hidl_status_cb) {
Roshan Pius56476652016-10-26 14:43:05 -070058 return validateAndCall(this,
59 WifiStatusCode::ERROR_UNKNOWN,
60 &Wifi::startInternal,
61 hidl_status_cb);
62}
63
64Return<void> Wifi::stop(stop_cb hidl_status_cb) {
65 return validateAndCall(
66 this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::stopInternal, hidl_status_cb);
67}
68
69Return<void> Wifi::getChipIds(getChipIds_cb hidl_status_cb) {
70 return validateAndCall(this,
71 WifiStatusCode::ERROR_UNKNOWN,
72 &Wifi::getChipIdsInternal,
73 hidl_status_cb);
74}
75
76Return<void> Wifi::getChip(ChipId chip_id, getChip_cb hidl_status_cb) {
77 return validateAndCall(this,
78 WifiStatusCode::ERROR_UNKNOWN,
79 &Wifi::getChipInternal,
80 hidl_status_cb,
81 chip_id);
82}
83
84WifiStatus Wifi::registerEventCallbackInternal(
85 const sp<IWifiEventCallback>& event_callback) {
86 // TODO(b/31632518): remove the callback when the client is destroyed
87 event_callbacks_.emplace_back(event_callback);
88 return createWifiStatus(WifiStatusCode::SUCCESS);
89}
90
91WifiStatus Wifi::startInternal() {
Roshan Piusaabe5752016-09-29 09:03:59 -070092 if (run_state_ == RunState::STARTED) {
Roshan Pius56476652016-10-26 14:43:05 -070093 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Piusaabe5752016-09-29 09:03:59 -070094 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius56476652016-10-26 14:43:05 -070095 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
96 "HAL is stopping");
Roshan Pius3c4e8a32016-10-03 14:53:58 -070097 }
98
Roshan Piusaabe5752016-09-29 09:03:59 -070099 LOG(INFO) << "Starting HAL";
Roshan Pius503582e2016-10-11 08:25:30 -0700100 wifi_error legacy_status = legacy_hal_->start();
101 if (legacy_status != WIFI_SUCCESS) {
Roshan Pius511cc492016-10-28 09:54:26 -0700102 LOG(ERROR) << "Failed to start Wifi HAL: "
103 << legacyErrorToString(legacy_status);
Roshan Pius56476652016-10-26 14:43:05 -0700104 return createWifiStatusFromLegacyError(legacy_status,
105 "Failed to start HAL");
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700106 }
107
Roshan Piusaabe5752016-09-29 09:03:59 -0700108 // Create the chip instance once the HAL is started.
Roshan Piuscd566bd2016-10-10 08:03:42 -0700109 chip_ = new WifiChip(kChipId, legacy_hal_);
Roshan Piusaabe5752016-09-29 09:03:59 -0700110 run_state_ = RunState::STARTED;
Roshan Pius503582e2016-10-11 08:25:30 -0700111 for (const auto& callback : event_callbacks_) {
112 if (!callback->onStart().getStatus().isOk()) {
113 LOG(ERROR) << "Failed to invoke onStart callback";
114 };
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700115 }
Roshan Pius56476652016-10-26 14:43:05 -0700116 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700117}
118
Roshan Pius56476652016-10-26 14:43:05 -0700119WifiStatus Wifi::stopInternal() {
Roshan Piusaabe5752016-09-29 09:03:59 -0700120 if (run_state_ == RunState::STOPPED) {
Roshan Pius56476652016-10-26 14:43:05 -0700121 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Piusaabe5752016-09-29 09:03:59 -0700122 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius56476652016-10-26 14:43:05 -0700123 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
124 "HAL is stopping");
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700125 }
126
Roshan Piusaabe5752016-09-29 09:03:59 -0700127 LOG(INFO) << "Stopping HAL";
128 run_state_ = RunState::STOPPING;
129 const auto on_complete_callback_ = [&]() {
130 if (chip_.get()) {
131 chip_->invalidate();
132 }
133 chip_.clear();
134 run_state_ = RunState::STOPPED;
Roshan Pius503582e2016-10-11 08:25:30 -0700135 for (const auto& callback : event_callbacks_) {
136 if (!callback->onStop().getStatus().isOk()) {
137 LOG(ERROR) << "Failed to invoke onStop callback";
138 };
Roshan Piusaabe5752016-09-29 09:03:59 -0700139 }
140 };
Roshan Pius503582e2016-10-11 08:25:30 -0700141 wifi_error legacy_status = legacy_hal_->stop(on_complete_callback_);
142 if (legacy_status != WIFI_SUCCESS) {
Roshan Pius511cc492016-10-28 09:54:26 -0700143 LOG(ERROR) << "Failed to stop Wifi HAL: "
144 << legacyErrorToString(legacy_status);
Roshan Pius503582e2016-10-11 08:25:30 -0700145 WifiStatus wifi_status =
146 createWifiStatusFromLegacyError(legacy_status, "Failed to stop HAL");
147 for (const auto& callback : event_callbacks_) {
148 callback->onFailure(wifi_status);
Roshan Piusaabe5752016-09-29 09:03:59 -0700149 }
Roshan Pius56476652016-10-26 14:43:05 -0700150 return wifi_status;
Roshan Piusaabe5752016-09-29 09:03:59 -0700151 }
Roshan Pius56476652016-10-26 14:43:05 -0700152 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700153}
154
Roshan Pius56476652016-10-26 14:43:05 -0700155std::pair<WifiStatus, std::vector<ChipId>> Wifi::getChipIdsInternal() {
Roshan Piuscd566bd2016-10-10 08:03:42 -0700156 std::vector<ChipId> chip_ids;
157 if (chip_.get()) {
158 chip_ids.emplace_back(kChipId);
159 }
Roshan Pius56476652016-10-26 14:43:05 -0700160 return {createWifiStatus(WifiStatusCode::SUCCESS), std::move(chip_ids)};
Roshan Piuscd566bd2016-10-10 08:03:42 -0700161}
162
Roshan Pius56476652016-10-26 14:43:05 -0700163std::pair<WifiStatus, sp<IWifiChip>> Wifi::getChipInternal(ChipId chip_id) {
164 if (!chip_.get()) {
165 return {createWifiStatus(WifiStatusCode::ERROR_NOT_STARTED), nullptr};
Roshan Piuscd566bd2016-10-10 08:03:42 -0700166 }
Roshan Pius56476652016-10-26 14:43:05 -0700167 if (chip_id != kChipId) {
168 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
169 }
170 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_};
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700171}
Roshan Pius79a99752016-10-04 13:03:58 -0700172} // namespace implementation
173} // namespace V1_0
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700174} // namespace wifi
175} // namespace hardware
176} // namespace android