blob: 73b921ada7b7a20badcb96a25a08ec4ad7557179 [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 Piusaabe5752016-09-29 09:03:59 -0700102 LOG(ERROR) << "Failed to start Wifi HAL";
Roshan Pius56476652016-10-26 14:43:05 -0700103 return createWifiStatusFromLegacyError(legacy_status,
104 "Failed to start HAL");
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700105 }
106
Roshan Piusaabe5752016-09-29 09:03:59 -0700107 // Create the chip instance once the HAL is started.
Roshan Piuscd566bd2016-10-10 08:03:42 -0700108 chip_ = new WifiChip(kChipId, legacy_hal_);
Roshan Piusaabe5752016-09-29 09:03:59 -0700109 run_state_ = RunState::STARTED;
Roshan Pius503582e2016-10-11 08:25:30 -0700110 for (const auto& callback : event_callbacks_) {
111 if (!callback->onStart().getStatus().isOk()) {
112 LOG(ERROR) << "Failed to invoke onStart callback";
113 };
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700114 }
Roshan Pius56476652016-10-26 14:43:05 -0700115 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700116}
117
Roshan Pius56476652016-10-26 14:43:05 -0700118WifiStatus Wifi::stopInternal() {
Roshan Piusaabe5752016-09-29 09:03:59 -0700119 if (run_state_ == RunState::STOPPED) {
Roshan Pius56476652016-10-26 14:43:05 -0700120 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Piusaabe5752016-09-29 09:03:59 -0700121 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius56476652016-10-26 14:43:05 -0700122 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
123 "HAL is stopping");
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700124 }
125
Roshan Piusaabe5752016-09-29 09:03:59 -0700126 LOG(INFO) << "Stopping HAL";
127 run_state_ = RunState::STOPPING;
128 const auto on_complete_callback_ = [&]() {
129 if (chip_.get()) {
130 chip_->invalidate();
131 }
132 chip_.clear();
133 run_state_ = RunState::STOPPED;
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 }
139 };
Roshan Pius503582e2016-10-11 08:25:30 -0700140 wifi_error legacy_status = legacy_hal_->stop(on_complete_callback_);
141 if (legacy_status != WIFI_SUCCESS) {
Roshan Piusaabe5752016-09-29 09:03:59 -0700142 LOG(ERROR) << "Failed to stop Wifi HAL";
Roshan Pius503582e2016-10-11 08:25:30 -0700143 WifiStatus wifi_status =
144 createWifiStatusFromLegacyError(legacy_status, "Failed to stop HAL");
145 for (const auto& callback : event_callbacks_) {
146 callback->onFailure(wifi_status);
Roshan Piusaabe5752016-09-29 09:03:59 -0700147 }
Roshan Pius56476652016-10-26 14:43:05 -0700148 return wifi_status;
Roshan Piusaabe5752016-09-29 09:03:59 -0700149 }
Roshan Pius56476652016-10-26 14:43:05 -0700150 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700151}
152
Roshan Pius56476652016-10-26 14:43:05 -0700153std::pair<WifiStatus, std::vector<ChipId>> Wifi::getChipIdsInternal() {
Roshan Piuscd566bd2016-10-10 08:03:42 -0700154 std::vector<ChipId> chip_ids;
155 if (chip_.get()) {
156 chip_ids.emplace_back(kChipId);
157 }
Roshan Pius56476652016-10-26 14:43:05 -0700158 return {createWifiStatus(WifiStatusCode::SUCCESS), std::move(chip_ids)};
Roshan Piuscd566bd2016-10-10 08:03:42 -0700159}
160
Roshan Pius56476652016-10-26 14:43:05 -0700161std::pair<WifiStatus, sp<IWifiChip>> Wifi::getChipInternal(ChipId chip_id) {
162 if (!chip_.get()) {
163 return {createWifiStatus(WifiStatusCode::ERROR_NOT_STARTED), nullptr};
Roshan Piuscd566bd2016-10-10 08:03:42 -0700164 }
Roshan Pius56476652016-10-26 14:43:05 -0700165 if (chip_id != kChipId) {
166 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
167 }
168 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_};
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700169}
Roshan Pius79a99752016-10-04 13:03:58 -0700170} // namespace implementation
171} // namespace V1_0
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700172} // namespace wifi
173} // namespace hardware
174} // namespace android