blob: 19f7e53bd3d50dcbd1ae2bced9cac0a9f2a21647 [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()),
37 run_state_(RunState::STOPPED) {}
Roshan Pius3c4e8a32016-10-03 14:53:58 -070038
Roshan Pius56476652016-10-26 14:43:05 -070039bool Wifi::isValid() {
40 // This object is always valid.
41 return true;
42}
43
Roshan Pius3c4e8a32016-10-03 14:53:58 -070044Return<void> Wifi::registerEventCallback(
Roshan Pius56476652016-10-26 14:43:05 -070045 const sp<IWifiEventCallback>& event_callback,
46 registerEventCallback_cb hidl_status_cb) {
47 return validateAndCall(this,
48 WifiStatusCode::ERROR_UNKNOWN,
49 &Wifi::registerEventCallbackInternal,
50 hidl_status_cb,
51 event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -070052}
53
54Return<bool> Wifi::isStarted() {
Roshan Piusaabe5752016-09-29 09:03:59 -070055 return run_state_ != RunState::STOPPED;
Roshan Pius3c4e8a32016-10-03 14:53:58 -070056}
57
Roshan Pius503582e2016-10-11 08:25:30 -070058Return<void> Wifi::start(start_cb hidl_status_cb) {
Roshan Pius56476652016-10-26 14:43:05 -070059 return validateAndCall(this,
60 WifiStatusCode::ERROR_UNKNOWN,
61 &Wifi::startInternal,
62 hidl_status_cb);
63}
64
65Return<void> Wifi::stop(stop_cb hidl_status_cb) {
66 return validateAndCall(
67 this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::stopInternal, hidl_status_cb);
68}
69
70Return<void> Wifi::getChipIds(getChipIds_cb hidl_status_cb) {
71 return validateAndCall(this,
72 WifiStatusCode::ERROR_UNKNOWN,
73 &Wifi::getChipIdsInternal,
74 hidl_status_cb);
75}
76
77Return<void> Wifi::getChip(ChipId chip_id, getChip_cb hidl_status_cb) {
78 return validateAndCall(this,
79 WifiStatusCode::ERROR_UNKNOWN,
80 &Wifi::getChipInternal,
81 hidl_status_cb,
82 chip_id);
83}
84
85WifiStatus Wifi::registerEventCallbackInternal(
86 const sp<IWifiEventCallback>& event_callback) {
87 // TODO(b/31632518): remove the callback when the client is destroyed
88 event_callbacks_.emplace_back(event_callback);
89 return createWifiStatus(WifiStatusCode::SUCCESS);
90}
91
92WifiStatus Wifi::startInternal() {
Roshan Piusaabe5752016-09-29 09:03:59 -070093 if (run_state_ == RunState::STARTED) {
Roshan Pius56476652016-10-26 14:43:05 -070094 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Piusaabe5752016-09-29 09:03:59 -070095 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius56476652016-10-26 14:43:05 -070096 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
97 "HAL is stopping");
Roshan Pius3c4e8a32016-10-03 14:53:58 -070098 }
99
Roshan Piusaabe5752016-09-29 09:03:59 -0700100 LOG(INFO) << "Starting HAL";
Roshan Piusa4854ff2016-12-01 13:47:41 -0800101 legacy_hal::wifi_error legacy_status = legacy_hal_->start();
102 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Roshan Pius511cc492016-10-28 09:54:26 -0700103 LOG(ERROR) << "Failed to start Wifi HAL: "
104 << legacyErrorToString(legacy_status);
Roshan Pius56476652016-10-26 14:43:05 -0700105 return createWifiStatusFromLegacyError(legacy_status,
106 "Failed to start HAL");
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700107 }
108
Roshan Piusaabe5752016-09-29 09:03:59 -0700109 // Create the chip instance once the HAL is started.
Roshan Piuscd566bd2016-10-10 08:03:42 -0700110 chip_ = new WifiChip(kChipId, legacy_hal_);
Roshan Piusaabe5752016-09-29 09:03:59 -0700111 run_state_ = RunState::STARTED;
Roshan Pius503582e2016-10-11 08:25:30 -0700112 for (const auto& callback : event_callbacks_) {
113 if (!callback->onStart().getStatus().isOk()) {
114 LOG(ERROR) << "Failed to invoke onStart callback";
115 };
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700116 }
Roshan Pius56476652016-10-26 14:43:05 -0700117 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700118}
119
Roshan Pius56476652016-10-26 14:43:05 -0700120WifiStatus Wifi::stopInternal() {
Roshan Piusaabe5752016-09-29 09:03:59 -0700121 if (run_state_ == RunState::STOPPED) {
Roshan Pius56476652016-10-26 14:43:05 -0700122 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Piusaabe5752016-09-29 09:03:59 -0700123 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius56476652016-10-26 14:43:05 -0700124 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
125 "HAL is stopping");
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700126 }
127
Roshan Piusaabe5752016-09-29 09:03:59 -0700128 LOG(INFO) << "Stopping HAL";
129 run_state_ = RunState::STOPPING;
130 const auto on_complete_callback_ = [&]() {
131 if (chip_.get()) {
132 chip_->invalidate();
133 }
134 chip_.clear();
135 run_state_ = RunState::STOPPED;
Roshan Pius503582e2016-10-11 08:25:30 -0700136 for (const auto& callback : event_callbacks_) {
137 if (!callback->onStop().getStatus().isOk()) {
138 LOG(ERROR) << "Failed to invoke onStop callback";
139 };
Roshan Piusaabe5752016-09-29 09:03:59 -0700140 }
141 };
Roshan Piusa4854ff2016-12-01 13:47:41 -0800142 legacy_hal::wifi_error legacy_status =
143 legacy_hal_->stop(on_complete_callback_);
144 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Roshan Pius511cc492016-10-28 09:54:26 -0700145 LOG(ERROR) << "Failed to stop Wifi HAL: "
146 << legacyErrorToString(legacy_status);
Roshan Pius503582e2016-10-11 08:25:30 -0700147 WifiStatus wifi_status =
148 createWifiStatusFromLegacyError(legacy_status, "Failed to stop HAL");
149 for (const auto& callback : event_callbacks_) {
150 callback->onFailure(wifi_status);
Roshan Piusaabe5752016-09-29 09:03:59 -0700151 }
Roshan Pius56476652016-10-26 14:43:05 -0700152 return wifi_status;
Roshan Piusaabe5752016-09-29 09:03:59 -0700153 }
Roshan Pius56476652016-10-26 14:43:05 -0700154 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700155}
156
Roshan Pius56476652016-10-26 14:43:05 -0700157std::pair<WifiStatus, std::vector<ChipId>> Wifi::getChipIdsInternal() {
Roshan Piuscd566bd2016-10-10 08:03:42 -0700158 std::vector<ChipId> chip_ids;
159 if (chip_.get()) {
160 chip_ids.emplace_back(kChipId);
161 }
Roshan Pius56476652016-10-26 14:43:05 -0700162 return {createWifiStatus(WifiStatusCode::SUCCESS), std::move(chip_ids)};
Roshan Piuscd566bd2016-10-10 08:03:42 -0700163}
164
Roshan Pius56476652016-10-26 14:43:05 -0700165std::pair<WifiStatus, sp<IWifiChip>> Wifi::getChipInternal(ChipId chip_id) {
166 if (!chip_.get()) {
167 return {createWifiStatus(WifiStatusCode::ERROR_NOT_STARTED), nullptr};
Roshan Piuscd566bd2016-10-10 08:03:42 -0700168 }
Roshan Pius56476652016-10-26 14:43:05 -0700169 if (chip_id != kChipId) {
170 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
171 }
172 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_};
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700173}
Roshan Pius79a99752016-10-04 13:03:58 -0700174} // namespace implementation
175} // namespace V1_0
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700176} // namespace wifi
177} // namespace hardware
178} // namespace android