blob: d5b69b815d656614833246b32d6e338842eead77 [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 Pius3c4e8a32016-10-03 14:53:58 -070024namespace android {
25namespace hardware {
26namespace wifi {
Roshan Pius79a99752016-10-04 13:03:58 -070027namespace V1_0 {
28namespace implementation {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070029
Roshan Piusaabe5752016-09-29 09:03:59 -070030Wifi::Wifi()
31 : legacy_hal_(new WifiLegacyHal()), run_state_(RunState::STOPPED) {}
Roshan Pius3c4e8a32016-10-03 14:53:58 -070032
33Return<void> Wifi::registerEventCallback(
Roshan Pius79a99752016-10-04 13:03:58 -070034 const sp<IWifiEventCallback>& callback) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070035 // TODO(b/31632518): remove the callback when the client is destroyed
36 callbacks_.insert(callback);
37 return Void();
38}
39
40Return<bool> Wifi::isStarted() {
Roshan Piusaabe5752016-09-29 09:03:59 -070041 return run_state_ != RunState::STOPPED;
Roshan Pius3c4e8a32016-10-03 14:53:58 -070042}
43
44Return<void> Wifi::start() {
Roshan Piusaabe5752016-09-29 09:03:59 -070045 if (run_state_ == RunState::STARTED) {
46 for (const auto& callback : callbacks_) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070047 callback->onStart();
48 }
49 return Void();
Roshan Piusaabe5752016-09-29 09:03:59 -070050 } else if (run_state_ == RunState::STOPPING) {
51 for (const auto& callback : callbacks_) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070052 callback->onStartFailure(CreateFailureReason(
53 CommandFailureReason::NOT_AVAILABLE, "HAL is stopping"));
54 }
55 return Void();
56 }
57
Roshan Piusaabe5752016-09-29 09:03:59 -070058 LOG(INFO) << "Starting HAL";
59 wifi_error status = legacy_hal_->start();
Roshan Pius3c4e8a32016-10-03 14:53:58 -070060 if (status != WIFI_SUCCESS) {
Roshan Piusaabe5752016-09-29 09:03:59 -070061 LOG(ERROR) << "Failed to start Wifi HAL";
Roshan Pius3c4e8a32016-10-03 14:53:58 -070062 for (auto& callback : callbacks_) {
Roshan Pius79a99752016-10-04 13:03:58 -070063 callback->onStartFailure(
Roshan Piusaabe5752016-09-29 09:03:59 -070064 CreateFailureReasonLegacyError(status, "Failed to start HAL"));
Roshan Pius3c4e8a32016-10-03 14:53:58 -070065 }
66 return Void();
67 }
68
Roshan Piusaabe5752016-09-29 09:03:59 -070069 // Create the chip instance once the HAL is started.
70 chip_ = new WifiChip(legacy_hal_);
71 run_state_ = RunState::STARTED;
72 for (const auto& callback : callbacks_) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070073 callback->onStart();
74 }
75 return Void();
76}
77
Roshan Pius3c4e8a32016-10-03 14:53:58 -070078Return<void> Wifi::stop() {
Roshan Piusaabe5752016-09-29 09:03:59 -070079 if (run_state_ == RunState::STOPPED) {
80 for (const auto& callback : callbacks_) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070081 callback->onStop();
82 }
83 return Void();
Roshan Piusaabe5752016-09-29 09:03:59 -070084 } else if (run_state_ == RunState::STOPPING) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070085 return Void();
86 }
87
Roshan Piusaabe5752016-09-29 09:03:59 -070088 LOG(INFO) << "Stopping HAL";
89 run_state_ = RunState::STOPPING;
90 const auto on_complete_callback_ = [&]() {
91 if (chip_.get()) {
92 chip_->invalidate();
93 }
94 chip_.clear();
95 run_state_ = RunState::STOPPED;
96 for (const auto& callback : callbacks_) {
97 callback->onStop();
98 }
99 };
100 wifi_error status = legacy_hal_->stop(on_complete_callback_);
101 if (status != WIFI_SUCCESS) {
102 LOG(ERROR) << "Failed to stop Wifi HAL";
103 for (const auto& callback : callbacks_) {
104 callback->onFailure(
105 CreateFailureReasonLegacyError(status, "Failed to stop HAL"));
106 }
107 }
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700108 return Void();
109}
110
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700111Return<void> Wifi::getChip(getChip_cb cb) {
112 cb(chip_);
113 return Void();
114}
115
Roshan Pius79a99752016-10-04 13:03:58 -0700116} // namespace implementation
117} // namespace V1_0
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700118} // namespace wifi
119} // namespace hardware
120} // namespace android