blob: ff2eb4cb2ed138f9ce0172b31c0b734d15b7f534 [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
21#include "failure_reason_util.h"
22#include "wifi_chip.h"
23
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 Pius79a99752016-10-04 13:03:58 -070039 const sp<IWifiEventCallback>& callback) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070040 // TODO(b/31632518): remove the callback when the client is destroyed
Roshan Piuscd566bd2016-10-10 08:03:42 -070041 callbacks_.emplace_back(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
49Return<void> Wifi::start() {
Roshan Piusaabe5752016-09-29 09:03:59 -070050 if (run_state_ == RunState::STARTED) {
51 for (const auto& callback : callbacks_) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070052 callback->onStart();
53 }
54 return Void();
Roshan Piusaabe5752016-09-29 09:03:59 -070055 } else if (run_state_ == RunState::STOPPING) {
56 for (const auto& callback : callbacks_) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070057 callback->onStartFailure(CreateFailureReason(
58 CommandFailureReason::NOT_AVAILABLE, "HAL is stopping"));
59 }
60 return Void();
61 }
62
Roshan Piusaabe5752016-09-29 09:03:59 -070063 LOG(INFO) << "Starting HAL";
64 wifi_error status = legacy_hal_->start();
Roshan Pius3c4e8a32016-10-03 14:53:58 -070065 if (status != WIFI_SUCCESS) {
Roshan Piusaabe5752016-09-29 09:03:59 -070066 LOG(ERROR) << "Failed to start Wifi HAL";
Roshan Pius3c4e8a32016-10-03 14:53:58 -070067 for (auto& callback : callbacks_) {
Roshan Pius79a99752016-10-04 13:03:58 -070068 callback->onStartFailure(
Roshan Piusaabe5752016-09-29 09:03:59 -070069 CreateFailureReasonLegacyError(status, "Failed to start HAL"));
Roshan Pius3c4e8a32016-10-03 14:53:58 -070070 }
71 return Void();
72 }
73
Roshan Piusaabe5752016-09-29 09:03:59 -070074 // Create the chip instance once the HAL is started.
Roshan Piuscd566bd2016-10-10 08:03:42 -070075 chip_ = new WifiChip(kChipId, legacy_hal_);
Roshan Piusaabe5752016-09-29 09:03:59 -070076 run_state_ = RunState::STARTED;
77 for (const auto& callback : callbacks_) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070078 callback->onStart();
79 }
80 return Void();
81}
82
Roshan Pius3c4e8a32016-10-03 14:53:58 -070083Return<void> Wifi::stop() {
Roshan Piusaabe5752016-09-29 09:03:59 -070084 if (run_state_ == RunState::STOPPED) {
85 for (const auto& callback : callbacks_) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070086 callback->onStop();
87 }
88 return Void();
Roshan Piusaabe5752016-09-29 09:03:59 -070089 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070090 return Void();
91 }
92
Roshan Piusaabe5752016-09-29 09:03:59 -070093 LOG(INFO) << "Stopping HAL";
94 run_state_ = RunState::STOPPING;
95 const auto on_complete_callback_ = [&]() {
96 if (chip_.get()) {
97 chip_->invalidate();
98 }
99 chip_.clear();
100 run_state_ = RunState::STOPPED;
101 for (const auto& callback : callbacks_) {
102 callback->onStop();
103 }
104 };
105 wifi_error status = legacy_hal_->stop(on_complete_callback_);
106 if (status != WIFI_SUCCESS) {
107 LOG(ERROR) << "Failed to stop Wifi HAL";
108 for (const auto& callback : callbacks_) {
109 callback->onFailure(
110 CreateFailureReasonLegacyError(status, "Failed to stop HAL"));
111 }
112 }
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700113 return Void();
114}
115
Roshan Piuscd566bd2016-10-10 08:03:42 -0700116Return<void> Wifi::getChipIds(getChipIds_cb cb) {
117 std::vector<ChipId> chip_ids;
118 if (chip_.get()) {
119 chip_ids.emplace_back(kChipId);
120 }
121 hidl_vec<ChipId> hidl_data;
122 hidl_data.setToExternal(chip_ids.data(), chip_ids.size());
123 cb(hidl_data);
124 return Void();
125}
126
127Return<void> Wifi::getChip(ChipId chip_id, getChip_cb cb) {
128 if (chip_.get() && chip_id == kChipId) {
129 cb(chip_);
130 } else {
131 cb(nullptr);
132 }
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700133 return Void();
134}
135
Roshan Pius79a99752016-10-04 13:03:58 -0700136} // namespace implementation
137} // namespace V1_0
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700138} // namespace wifi
139} // namespace hardware
140} // namespace android