blob: e10826bfbb2ca19f50df3f1724d40ac18879accd [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
17#include "wifi.h"
18
19#include <android-base/logging.h>
Roshan Pius3c4e8a32016-10-03 14:53:58 -070020
Roshan Pius3c4e8a32016-10-03 14:53:58 -070021#include "wifi_chip.h"
Roshan Pius503582e2016-10-11 08:25:30 -070022#include "wifi_status_util.h"
Roshan Pius3c4e8a32016-10-03 14:53:58 -070023
Roshan Piuscd566bd2016-10-10 08:03:42 -070024namespace {
25// Chip ID to use for the only supported chip.
26static constexpr android::hardware::wifi::V1_0::ChipId kChipId = 0;
27} // namespace
28
Roshan Pius3c4e8a32016-10-03 14:53:58 -070029namespace android {
30namespace hardware {
31namespace wifi {
Roshan Pius79a99752016-10-04 13:03:58 -070032namespace V1_0 {
33namespace implementation {
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
38Return<void> Wifi::registerEventCallback(
Roshan Pius503582e2016-10-11 08:25:30 -070039 const sp<IWifiEventCallback>& event_callback) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070040 // TODO(b/31632518): remove the callback when the client is destroyed
Roshan Pius503582e2016-10-11 08:25:30 -070041 event_callbacks_.emplace_back(event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -070042 return Void();
43}
44
45Return<bool> Wifi::isStarted() {
Roshan Piusaabe5752016-09-29 09:03:59 -070046 return run_state_ != RunState::STOPPED;
Roshan Pius3c4e8a32016-10-03 14:53:58 -070047}
48
Roshan Pius503582e2016-10-11 08:25:30 -070049Return<void> Wifi::start(start_cb hidl_status_cb) {
Roshan Piusaabe5752016-09-29 09:03:59 -070050 if (run_state_ == RunState::STARTED) {
Roshan Pius503582e2016-10-11 08:25:30 -070051 hidl_status_cb(createWifiStatus(WifiStatusCode::SUCCESS));
Roshan Pius3c4e8a32016-10-03 14:53:58 -070052 return Void();
Roshan Piusaabe5752016-09-29 09:03:59 -070053 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius503582e2016-10-11 08:25:30 -070054 hidl_status_cb(createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
55 "HAL is stopping"));
Roshan Pius3c4e8a32016-10-03 14:53:58 -070056 return Void();
57 }
58
Roshan Piusaabe5752016-09-29 09:03:59 -070059 LOG(INFO) << "Starting HAL";
Roshan Pius503582e2016-10-11 08:25:30 -070060 wifi_error legacy_status = legacy_hal_->start();
61 if (legacy_status != WIFI_SUCCESS) {
Roshan Piusaabe5752016-09-29 09:03:59 -070062 LOG(ERROR) << "Failed to start Wifi HAL";
Roshan Pius503582e2016-10-11 08:25:30 -070063 hidl_status_cb(
64 createWifiStatusFromLegacyError(legacy_status, "Failed to start HAL"));
Roshan Pius3c4e8a32016-10-03 14:53:58 -070065 return Void();
66 }
67
Roshan Piusaabe5752016-09-29 09:03:59 -070068 // Create the chip instance once the HAL is started.
Roshan Piuscd566bd2016-10-10 08:03:42 -070069 chip_ = new WifiChip(kChipId, legacy_hal_);
Roshan Piusaabe5752016-09-29 09:03:59 -070070 run_state_ = RunState::STARTED;
Roshan Pius503582e2016-10-11 08:25:30 -070071 for (const auto& callback : event_callbacks_) {
72 if (!callback->onStart().getStatus().isOk()) {
73 LOG(ERROR) << "Failed to invoke onStart callback";
74 };
Roshan Pius3c4e8a32016-10-03 14:53:58 -070075 }
Roshan Pius503582e2016-10-11 08:25:30 -070076 hidl_status_cb(createWifiStatus(WifiStatusCode::SUCCESS));
Roshan Pius3c4e8a32016-10-03 14:53:58 -070077 return Void();
78}
79
Roshan Pius503582e2016-10-11 08:25:30 -070080Return<void> Wifi::stop(stop_cb hidl_status_cb) {
Roshan Piusaabe5752016-09-29 09:03:59 -070081 if (run_state_ == RunState::STOPPED) {
Roshan Pius503582e2016-10-11 08:25:30 -070082 hidl_status_cb(createWifiStatus(WifiStatusCode::SUCCESS));
Roshan Pius3c4e8a32016-10-03 14:53:58 -070083 return Void();
Roshan Piusaabe5752016-09-29 09:03:59 -070084 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius503582e2016-10-11 08:25:30 -070085 hidl_status_cb(createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE,
86 "HAL is stopping"));
Roshan Pius3c4e8a32016-10-03 14:53:58 -070087 return Void();
88 }
89
Roshan Piusaabe5752016-09-29 09:03:59 -070090 LOG(INFO) << "Stopping HAL";
91 run_state_ = RunState::STOPPING;
92 const auto on_complete_callback_ = [&]() {
93 if (chip_.get()) {
94 chip_->invalidate();
95 }
96 chip_.clear();
97 run_state_ = RunState::STOPPED;
Roshan Pius503582e2016-10-11 08:25:30 -070098 for (const auto& callback : event_callbacks_) {
99 if (!callback->onStop().getStatus().isOk()) {
100 LOG(ERROR) << "Failed to invoke onStop callback";
101 };
Roshan Piusaabe5752016-09-29 09:03:59 -0700102 }
103 };
Roshan Pius503582e2016-10-11 08:25:30 -0700104 wifi_error legacy_status = legacy_hal_->stop(on_complete_callback_);
105 if (legacy_status != WIFI_SUCCESS) {
Roshan Piusaabe5752016-09-29 09:03:59 -0700106 LOG(ERROR) << "Failed to stop Wifi HAL";
Roshan Pius503582e2016-10-11 08:25:30 -0700107 WifiStatus wifi_status =
108 createWifiStatusFromLegacyError(legacy_status, "Failed to stop HAL");
109 for (const auto& callback : event_callbacks_) {
110 callback->onFailure(wifi_status);
Roshan Piusaabe5752016-09-29 09:03:59 -0700111 }
Roshan Pius503582e2016-10-11 08:25:30 -0700112 hidl_status_cb(wifi_status);
113 return Void();
Roshan Piusaabe5752016-09-29 09:03:59 -0700114 }
Roshan Pius503582e2016-10-11 08:25:30 -0700115 hidl_status_cb(createWifiStatus(WifiStatusCode::SUCCESS));
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700116 return Void();
117}
118
Roshan Pius503582e2016-10-11 08:25:30 -0700119Return<void> Wifi::getChipIds(getChipIds_cb hidl_status_cb) {
Roshan Piuscd566bd2016-10-10 08:03:42 -0700120 std::vector<ChipId> chip_ids;
121 if (chip_.get()) {
122 chip_ids.emplace_back(kChipId);
123 }
124 hidl_vec<ChipId> hidl_data;
125 hidl_data.setToExternal(chip_ids.data(), chip_ids.size());
Roshan Pius503582e2016-10-11 08:25:30 -0700126 hidl_status_cb(hidl_data);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700127 return Void();
128}
129
Roshan Pius503582e2016-10-11 08:25:30 -0700130Return<void> Wifi::getChip(ChipId chip_id, getChip_cb hidl_status_cb) {
Roshan Piuscd566bd2016-10-10 08:03:42 -0700131 if (chip_.get() && chip_id == kChipId) {
Roshan Pius503582e2016-10-11 08:25:30 -0700132 hidl_status_cb(chip_);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700133 } else {
Roshan Pius503582e2016-10-11 08:25:30 -0700134 hidl_status_cb(nullptr);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700135 }
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700136 return Void();
137}
138
Roshan Pius79a99752016-10-04 13:03:58 -0700139} // namespace implementation
140} // namespace V1_0
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700141} // namespace wifi
142} // namespace hardware
143} // namespace android