blob: aa0fc5b65fa3b17b6a60a4b9f4391e5b2ebc8183 [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>
20#include <cutils/properties.h>
21
22#include "failure_reason_util.h"
23#include "wifi_chip.h"
24
Roshan Pius3c4e8a32016-10-03 14:53:58 -070025using RunState = ::android::hardware::wifi::WifiHalState::RunState;
26
27namespace {
28std::string GetWlanInterfaceName() {
29 char buffer[PROPERTY_VALUE_MAX];
30 property_get("wifi.interface", buffer, "wlan0");
31 return buffer;
32}
33}
34
35namespace android {
36namespace hardware {
37namespace wifi {
Roshan Pius79a99752016-10-04 13:03:58 -070038namespace V1_0 {
39namespace implementation {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070040
41Wifi::Wifi(sp<Looper>& looper) : state_(looper) {
42 CHECK_EQ(init_wifi_vendor_hal_func_table(&state_.func_table_), WIFI_SUCCESS)
43 << "Failed to initialize hal func table";
44}
45
46Return<void> Wifi::registerEventCallback(
Roshan Pius79a99752016-10-04 13:03:58 -070047 const sp<IWifiEventCallback>& callback) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -070048 // TODO(b/31632518): remove the callback when the client is destroyed
49 callbacks_.insert(callback);
50 return Void();
51}
52
53Return<bool> Wifi::isStarted() {
54 return state_.run_state_ != RunState::STOPPED;
55}
56
57Return<void> Wifi::start() {
58 if (state_.run_state_ == RunState::STARTED) {
59 for (auto& callback : callbacks_) {
60 callback->onStart();
61 }
62 return Void();
63 } else if (state_.run_state_ == RunState::STOPPING) {
64 for (auto& callback : callbacks_) {
65 callback->onStartFailure(CreateFailureReason(
66 CommandFailureReason::NOT_AVAILABLE, "HAL is stopping"));
67 }
68 return Void();
69 }
70
71 LOG(INFO) << "Initializing HAL";
72 wifi_error status = state_.func_table_.wifi_initialize(&state_.hal_handle_);
73 if (status != WIFI_SUCCESS) {
74 LOG(ERROR) << "Failed to initialize Wifi HAL";
75 for (auto& callback : callbacks_) {
Roshan Pius79a99752016-10-04 13:03:58 -070076 callback->onStartFailure(
77 CreateFailureReasonLegacyError(status, "Failed to initialize HAL"));
Roshan Pius3c4e8a32016-10-03 14:53:58 -070078 }
79 return Void();
80 }
81
82 event_loop_thread_ = std::thread(&Wifi::DoHalEventLoop, this);
83
84 wifi_interface_handle iface_handle =
85 FindInterfaceHandle(GetWlanInterfaceName());
86 if (iface_handle != kInterfaceNotFoundHandle) {
87 chip_ = new WifiChip(&state_, iface_handle);
88 } else {
89 // TODO fail to init?
90 }
91
92 state_.run_state_ = RunState::STARTED;
93 for (auto& callback : callbacks_) {
94 callback->onStart();
95 }
96 return Void();
97}
98
Roshan Pius79a99752016-10-04 13:03:58 -070099wifi_interface_handle Wifi::FindInterfaceHandle(const std::string& ifname) {
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700100 int num_iface_handles = 0;
101 wifi_interface_handle* iface_handles = nullptr;
102 wifi_error ret = state_.func_table_.wifi_get_ifaces(
103 state_.hal_handle_, &num_iface_handles, &iface_handles);
104 if (ret != WIFI_SUCCESS) {
105 LOG(ERROR) << "Failed to enumerate interface handles: "
106 << LegacyErrorToString(ret);
107 return kInterfaceNotFoundHandle;
108 }
109
110 char buffer[IFNAMSIZ];
111 for (int i = 0; i < num_iface_handles; ++i) {
112 bzero(buffer, sizeof(buffer));
113 ret = state_.func_table_.wifi_get_iface_name(
114 iface_handles[i], buffer, sizeof(buffer));
115 if (ret != WIFI_SUCCESS) {
116 LOG(WARNING) << "Failed to get interface handle name: "
117 << LegacyErrorToString(ret);
118 continue;
119 }
120 if (ifname == buffer) {
121 return iface_handles[i];
122 }
123 }
124 return kInterfaceNotFoundHandle;
125}
126
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700127void NoopHalCleanupHandler(wifi_handle) {}
128
129Return<void> Wifi::stop() {
130 if (state_.run_state_ == RunState::STOPPED) {
131 for (auto& callback : callbacks_) {
132 callback->onStop();
133 }
134 return Void();
135 } else if (state_.run_state_ == RunState::STOPPING) {
136 return Void();
137 }
138
139 LOG(INFO) << "Cleaning up HAL";
140 awaiting_hal_cleanup_command_ = true;
141 awaiting_hal_event_loop_termination_ = true;
142 state_.run_state_ = RunState::STOPPING;
143
Roshan Pius79a99752016-10-04 13:03:58 -0700144 if (chip_.get())
145 chip_->Invalidate();
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700146 chip_.clear();
147
148 state_.func_table_.wifi_cleanup(state_.hal_handle_, NoopHalCleanupHandler);
149 awaiting_hal_cleanup_command_ = false;
150 LOG(VERBOSE) << "HAL cleanup command complete";
151 FinishHalCleanup();
152 return Void();
153}
154
155void Wifi::DoHalEventLoop() {
156 LOG(VERBOSE) << "Starting HAL event loop";
157 state_.func_table_.wifi_event_loop(state_.hal_handle_);
158 if (state_.run_state_ != RunState::STOPPING) {
159 LOG(FATAL) << "HAL event loop terminated, but HAL was not stopping";
160 }
161 LOG(VERBOSE) << "HAL Event loop terminated";
162 event_loop_thread_.detach();
Roshan Pius79a99752016-10-04 13:03:58 -0700163 state_.PostTask([this]() {
164 awaiting_hal_event_loop_termination_ = false;
165 FinishHalCleanup();
166 });
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700167}
168
169void Wifi::FinishHalCleanup() {
170 if (!awaiting_hal_cleanup_command_ && !awaiting_hal_event_loop_termination_) {
171 state_.run_state_ = RunState::STOPPED;
172 LOG(INFO) << "HAL cleanup complete";
173 for (auto& callback : callbacks_) {
174 callback->onStop();
175 }
176 }
177}
178
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700179Return<void> Wifi::getChip(getChip_cb cb) {
180 cb(chip_);
181 return Void();
182}
183
Roshan Pius79a99752016-10-04 13:03:58 -0700184} // namespace implementation
185} // namespace V1_0
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700186} // namespace wifi
187} // namespace hardware
188} // namespace android