blob: a6df996fc1a2b5d70b952fe98fa2f054a04ab042 [file] [log] [blame]
Roshan Piusaabe5752016-09-29 09:03:59 -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 <array>
18
19#include "failure_reason_util.h"
20#include "wifi_legacy_hal.h"
21
22#include <android-base/logging.h>
23#include <cutils/properties.h>
Roshan Pius908a69a2016-10-03 13:33:23 -070024#include <wifi_system/hal_tool.h>
25#include <wifi_system/interface_tool.h>
Roshan Piusaabe5752016-09-29 09:03:59 -070026
27namespace {
28std::string getWlanInterfaceName() {
29 char buffer[PROPERTY_VALUE_MAX];
30 property_get("wifi.interface", buffer, "wlan0");
31 return buffer;
32}
33
34// Legacy HAL functions accept "C" style function pointers, so use global
35// functions to pass to the legacy HAL function and store the corresponding
36// std::function methods to be invoked.
37// Callback to be invoked once |stop| is complete.
38std::function<void(wifi_handle handle)> on_stop_complete_internal_callback;
39void onStopComplete(wifi_handle handle) {
40 if (on_stop_complete_internal_callback) {
41 on_stop_complete_internal_callback(handle);
42 }
43}
Roshan Piuscdb77f32016-10-03 14:09:57 -070044
45// Callback to be invoked for driver dump.
46std::function<void(char*, int)> on_driver_memory_dump_internal_callback;
47void onDriverMemoryDump(char* buffer, int buffer_size) {
48 if (on_driver_memory_dump_internal_callback) {
49 on_driver_memory_dump_internal_callback(buffer, buffer_size);
50 }
51}
52
53// Callback to be invoked for firmware dump.
54std::function<void(char*, int)> on_firmware_memory_dump_internal_callback;
55void onFirmwareMemoryDump(char* buffer, int buffer_size) {
56 if (on_firmware_memory_dump_internal_callback) {
57 on_firmware_memory_dump_internal_callback(buffer, buffer_size);
58 }
59}
Roshan Piusaabe5752016-09-29 09:03:59 -070060}
61
62namespace android {
63namespace hardware {
64namespace wifi {
65namespace V1_0 {
66namespace implementation {
67
Roshan Pius4b26c832016-10-03 12:49:58 -070068const uint32_t WifiLegacyHal::kMaxVersionStringLength = 256;
69
Roshan Piusaabe5752016-09-29 09:03:59 -070070WifiLegacyHal::WifiLegacyHal()
71 : global_handle_(nullptr),
72 wlan_interface_handle_(nullptr),
Roshan Pius908a69a2016-10-03 13:33:23 -070073 awaiting_event_loop_termination_(false) {}
Roshan Piusaabe5752016-09-29 09:03:59 -070074
75wifi_error WifiLegacyHal::start() {
76 // Ensure that we're starting in a good state.
77 CHECK(!global_handle_ && !wlan_interface_handle_ &&
78 !awaiting_event_loop_termination_);
79
Roshan Pius908a69a2016-10-03 13:33:23 -070080 android::wifi_system::HalTool hal_tool;
81 android::wifi_system::InterfaceTool if_tool;
82 if (!hal_tool.InitFunctionTable(&global_func_table_)) {
83 LOG(ERROR) << "Failed to initialize legacy hal function table";
84 return WIFI_ERROR_UNKNOWN;
85 }
86 if (!if_tool.SetWifiUpState(true)) {
87 LOG(ERROR) << "Failed to set WiFi interface up";
88 return WIFI_ERROR_UNKNOWN;
89 }
90
Roshan Piusaabe5752016-09-29 09:03:59 -070091 LOG(INFO) << "Starting legacy HAL";
92 wifi_error status = global_func_table_.wifi_initialize(&global_handle_);
93 if (status != WIFI_SUCCESS || !global_handle_) {
94 LOG(ERROR) << "Failed to retrieve global handle";
95 return status;
96 }
97 event_loop_thread_ = std::thread(&WifiLegacyHal::runEventLoop, this);
98 status = retrieveWlanInterfaceHandle();
99 if (status != WIFI_SUCCESS || !wlan_interface_handle_) {
100 LOG(ERROR) << "Failed to retrieve wlan interface handle";
101 return status;
102 }
103 LOG(VERBOSE) << "Legacy HAL start complete";
104 return WIFI_SUCCESS;
105}
106
107wifi_error WifiLegacyHal::stop(
108 const std::function<void()>& on_stop_complete_user_callback) {
109 LOG(INFO) << "Stopping legacy HAL";
110 on_stop_complete_internal_callback = [&](wifi_handle handle) {
111 CHECK_EQ(global_handle_, handle) << "Handle mismatch";
112 on_stop_complete_user_callback();
113 global_handle_ = nullptr;
114 wlan_interface_handle_ = nullptr;
115 on_stop_complete_internal_callback = nullptr;
116 };
117 awaiting_event_loop_termination_ = true;
118 global_func_table_.wifi_cleanup(global_handle_, onStopComplete);
119 LOG(VERBOSE) << "Legacy HAL stop initiated";
120 return WIFI_SUCCESS;
121}
122
Roshan Pius4b26c832016-10-03 12:49:58 -0700123std::pair<wifi_error, std::string> WifiLegacyHal::getWlanDriverVersion() {
124 std::array<char, kMaxVersionStringLength> buffer;
125 buffer.fill(0);
126 wifi_error status = global_func_table_.wifi_get_driver_version(
127 wlan_interface_handle_, buffer.data(), buffer.size());
128 return std::make_pair(status, buffer.data());
129}
130
131std::pair<wifi_error, std::string> WifiLegacyHal::getWlanFirmwareVersion() {
132 std::array<char, kMaxVersionStringLength> buffer;
133 buffer.fill(0);
134 wifi_error status = global_func_table_.wifi_get_firmware_version(
135 wlan_interface_handle_, buffer.data(), buffer.size());
136 return std::make_pair(status, buffer.data());
137}
138
Roshan Piuscdb77f32016-10-03 14:09:57 -0700139std::pair<wifi_error, std::vector<char>>
140WifiLegacyHal::requestWlanDriverMemoryDump() {
141 std::vector<char> driver_dump;
142 on_driver_memory_dump_internal_callback = [&driver_dump](char* buffer,
143 int buffer_size) {
144 driver_dump.insert(driver_dump.end(), buffer, buffer + buffer_size);
145 };
146 wifi_error status = global_func_table_.wifi_get_driver_memory_dump(
147 wlan_interface_handle_, {onDriverMemoryDump});
148 on_driver_memory_dump_internal_callback = nullptr;
149 return std::make_pair(status, std::move(driver_dump));
150}
151
152std::pair<wifi_error, std::vector<char>>
153WifiLegacyHal::requestWlanFirmwareMemoryDump() {
154 std::vector<char> firmware_dump;
155 on_firmware_memory_dump_internal_callback = [&firmware_dump](
156 char* buffer, int buffer_size) {
157 firmware_dump.insert(firmware_dump.end(), buffer, buffer + buffer_size);
158 };
159 wifi_error status = global_func_table_.wifi_get_firmware_memory_dump(
160 wlan_interface_handle_, {onFirmwareMemoryDump});
161 on_firmware_memory_dump_internal_callback = nullptr;
162 return std::make_pair(status, std::move(firmware_dump));
163}
164
Roshan Piusaabe5752016-09-29 09:03:59 -0700165wifi_error WifiLegacyHal::retrieveWlanInterfaceHandle() {
166 const std::string& ifname_to_find = getWlanInterfaceName();
167
168 wifi_interface_handle* iface_handles = nullptr;
169 int num_iface_handles = 0;
170 wifi_error status = global_func_table_.wifi_get_ifaces(
171 global_handle_, &num_iface_handles, &iface_handles);
172 if (status != WIFI_SUCCESS) {
173 LOG(ERROR) << "Failed to enumerate interface handles: "
174 << LegacyErrorToString(status);
175 return status;
176 }
177 for (int i = 0; i < num_iface_handles; ++i) {
178 std::array<char, IFNAMSIZ> current_ifname;
179 current_ifname.fill(0);
180 status = global_func_table_.wifi_get_iface_name(
181 iface_handles[i], current_ifname.data(), current_ifname.size());
182 if (status != WIFI_SUCCESS) {
183 LOG(WARNING) << "Failed to get interface handle name: "
184 << LegacyErrorToString(status);
185 continue;
186 }
187 if (ifname_to_find == current_ifname.data()) {
188 wlan_interface_handle_ = iface_handles[i];
189 return WIFI_SUCCESS;
190 }
191 }
192 return WIFI_ERROR_UNKNOWN;
193}
194
195void WifiLegacyHal::runEventLoop() {
196 LOG(VERBOSE) << "Starting legacy HAL event loop";
197 global_func_table_.wifi_event_loop(global_handle_);
198 if (!awaiting_event_loop_termination_) {
199 LOG(FATAL) << "Legacy HAL event loop terminated, but HAL was not stopping";
200 }
201 LOG(VERBOSE) << "Legacy HAL event loop terminated";
202 awaiting_event_loop_termination_ = false;
Roshan Pius908a69a2016-10-03 13:33:23 -0700203 android::wifi_system::InterfaceTool if_tool;
204 if_tool.SetWifiUpState(false);
Roshan Piusaabe5752016-09-29 09:03:59 -0700205}
206
207} // namespace implementation
208} // namespace V1_0
209} // namespace wifi
210} // namespace hardware
211} // namespace android