blob: cb254c3e66b54e19226453d0e6a852f95abac0ef [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
Roshan Piusaabe5752016-09-29 09:03:59 -070019#include "wifi_legacy_hal.h"
20
21#include <android-base/logging.h>
22#include <cutils/properties.h>
Roshan Pius908a69a2016-10-03 13:33:23 -070023#include <wifi_system/hal_tool.h>
24#include <wifi_system/interface_tool.h>
Roshan Piusaabe5752016-09-29 09:03:59 -070025
26namespace {
Roshan Pius511cc492016-10-28 09:54:26 -070027static constexpr uint32_t kMaxVersionStringLength = 256;
28
Roshan Piusaabe5752016-09-29 09:03:59 -070029// Legacy HAL functions accept "C" style function pointers, so use global
30// functions to pass to the legacy HAL function and store the corresponding
31// std::function methods to be invoked.
32// Callback to be invoked once |stop| is complete.
33std::function<void(wifi_handle handle)> on_stop_complete_internal_callback;
34void onStopComplete(wifi_handle handle) {
35 if (on_stop_complete_internal_callback) {
36 on_stop_complete_internal_callback(handle);
37 }
38}
Roshan Piuscdb77f32016-10-03 14:09:57 -070039
40// Callback to be invoked for driver dump.
41std::function<void(char*, int)> on_driver_memory_dump_internal_callback;
42void onDriverMemoryDump(char* buffer, int buffer_size) {
43 if (on_driver_memory_dump_internal_callback) {
44 on_driver_memory_dump_internal_callback(buffer, buffer_size);
45 }
46}
47
48// Callback to be invoked for firmware dump.
49std::function<void(char*, int)> on_firmware_memory_dump_internal_callback;
50void onFirmwareMemoryDump(char* buffer, int buffer_size) {
51 if (on_firmware_memory_dump_internal_callback) {
52 on_firmware_memory_dump_internal_callback(buffer, buffer_size);
53 }
54}
Roshan Piusaabe5752016-09-29 09:03:59 -070055}
56
57namespace android {
58namespace hardware {
59namespace wifi {
60namespace V1_0 {
61namespace implementation {
Roshan Piusaabe5752016-09-29 09:03:59 -070062WifiLegacyHal::WifiLegacyHal()
63 : global_handle_(nullptr),
64 wlan_interface_handle_(nullptr),
Roshan Pius908a69a2016-10-03 13:33:23 -070065 awaiting_event_loop_termination_(false) {}
Roshan Piusaabe5752016-09-29 09:03:59 -070066
67wifi_error WifiLegacyHal::start() {
68 // Ensure that we're starting in a good state.
69 CHECK(!global_handle_ && !wlan_interface_handle_ &&
70 !awaiting_event_loop_termination_);
71
Roshan Pius908a69a2016-10-03 13:33:23 -070072 android::wifi_system::HalTool hal_tool;
73 android::wifi_system::InterfaceTool if_tool;
74 if (!hal_tool.InitFunctionTable(&global_func_table_)) {
75 LOG(ERROR) << "Failed to initialize legacy hal function table";
76 return WIFI_ERROR_UNKNOWN;
77 }
78 if (!if_tool.SetWifiUpState(true)) {
79 LOG(ERROR) << "Failed to set WiFi interface up";
80 return WIFI_ERROR_UNKNOWN;
81 }
82
Roshan Piusaabe5752016-09-29 09:03:59 -070083 LOG(INFO) << "Starting legacy HAL";
84 wifi_error status = global_func_table_.wifi_initialize(&global_handle_);
85 if (status != WIFI_SUCCESS || !global_handle_) {
86 LOG(ERROR) << "Failed to retrieve global handle";
87 return status;
88 }
89 event_loop_thread_ = std::thread(&WifiLegacyHal::runEventLoop, this);
90 status = retrieveWlanInterfaceHandle();
91 if (status != WIFI_SUCCESS || !wlan_interface_handle_) {
92 LOG(ERROR) << "Failed to retrieve wlan interface handle";
93 return status;
94 }
95 LOG(VERBOSE) << "Legacy HAL start complete";
96 return WIFI_SUCCESS;
97}
98
99wifi_error WifiLegacyHal::stop(
100 const std::function<void()>& on_stop_complete_user_callback) {
101 LOG(INFO) << "Stopping legacy HAL";
102 on_stop_complete_internal_callback = [&](wifi_handle handle) {
103 CHECK_EQ(global_handle_, handle) << "Handle mismatch";
104 on_stop_complete_user_callback();
Roshan Pius511cc492016-10-28 09:54:26 -0700105 // Invalidate all the internal pointers now that the HAL is
106 // stopped.
107 invalidate();
Roshan Piusaabe5752016-09-29 09:03:59 -0700108 };
109 awaiting_event_loop_termination_ = true;
110 global_func_table_.wifi_cleanup(global_handle_, onStopComplete);
111 LOG(VERBOSE) << "Legacy HAL stop initiated";
112 return WIFI_SUCCESS;
113}
114
Roshan Piusab5c4712016-10-06 14:37:15 -0700115std::string WifiLegacyHal::getApIfaceName() {
116 // Fake name. This interface does not exist in legacy HAL
117 // API's.
118 return "ap0";
119}
120
121std::string WifiLegacyHal::getNanIfaceName() {
122 // Fake name. This interface does not exist in legacy HAL
123 // API's.
124 return "nan0";
125}
126
127std::string WifiLegacyHal::getP2pIfaceName() {
128 std::array<char, PROPERTY_VALUE_MAX> buffer;
129 property_get("wifi.direct.interface", buffer.data(), "p2p0");
130 return buffer.data();
131}
132
133std::string WifiLegacyHal::getStaIfaceName() {
134 std::array<char, PROPERTY_VALUE_MAX> buffer;
135 property_get("wifi.interface", buffer.data(), "wlan0");
136 return buffer.data();
137}
138
139std::pair<wifi_error, std::string> WifiLegacyHal::getDriverVersion() {
Roshan Pius4b26c832016-10-03 12:49:58 -0700140 std::array<char, kMaxVersionStringLength> buffer;
141 buffer.fill(0);
142 wifi_error status = global_func_table_.wifi_get_driver_version(
143 wlan_interface_handle_, buffer.data(), buffer.size());
144 return std::make_pair(status, buffer.data());
145}
146
Roshan Piusab5c4712016-10-06 14:37:15 -0700147std::pair<wifi_error, std::string> WifiLegacyHal::getFirmwareVersion() {
Roshan Pius4b26c832016-10-03 12:49:58 -0700148 std::array<char, kMaxVersionStringLength> buffer;
149 buffer.fill(0);
150 wifi_error status = global_func_table_.wifi_get_firmware_version(
151 wlan_interface_handle_, buffer.data(), buffer.size());
152 return std::make_pair(status, buffer.data());
153}
154
Roshan Pius3c868522016-10-27 12:43:49 -0700155std::pair<wifi_error, std::vector<uint8_t>>
Roshan Piusab5c4712016-10-06 14:37:15 -0700156WifiLegacyHal::requestDriverMemoryDump() {
Roshan Pius3c868522016-10-27 12:43:49 -0700157 std::vector<uint8_t> driver_dump;
Roshan Piuscdb77f32016-10-03 14:09:57 -0700158 on_driver_memory_dump_internal_callback = [&driver_dump](char* buffer,
159 int buffer_size) {
Roshan Pius3c868522016-10-27 12:43:49 -0700160 driver_dump.insert(driver_dump.end(),
161 reinterpret_cast<uint8_t*>(buffer),
162 reinterpret_cast<uint8_t*>(buffer) + buffer_size);
Roshan Piuscdb77f32016-10-03 14:09:57 -0700163 };
164 wifi_error status = global_func_table_.wifi_get_driver_memory_dump(
165 wlan_interface_handle_, {onDriverMemoryDump});
166 on_driver_memory_dump_internal_callback = nullptr;
167 return std::make_pair(status, std::move(driver_dump));
168}
169
Roshan Pius3c868522016-10-27 12:43:49 -0700170std::pair<wifi_error, std::vector<uint8_t>>
Roshan Piusab5c4712016-10-06 14:37:15 -0700171WifiLegacyHal::requestFirmwareMemoryDump() {
Roshan Pius3c868522016-10-27 12:43:49 -0700172 std::vector<uint8_t> firmware_dump;
Roshan Piuscdb77f32016-10-03 14:09:57 -0700173 on_firmware_memory_dump_internal_callback = [&firmware_dump](
174 char* buffer, int buffer_size) {
Roshan Pius3c868522016-10-27 12:43:49 -0700175 firmware_dump.insert(firmware_dump.end(),
176 reinterpret_cast<uint8_t*>(buffer),
177 reinterpret_cast<uint8_t*>(buffer) + buffer_size);
Roshan Piuscdb77f32016-10-03 14:09:57 -0700178 };
179 wifi_error status = global_func_table_.wifi_get_firmware_memory_dump(
180 wlan_interface_handle_, {onFirmwareMemoryDump});
181 on_firmware_memory_dump_internal_callback = nullptr;
182 return std::make_pair(status, std::move(firmware_dump));
183}
184
Roshan Piusaabe5752016-09-29 09:03:59 -0700185wifi_error WifiLegacyHal::retrieveWlanInterfaceHandle() {
Roshan Piusab5c4712016-10-06 14:37:15 -0700186 const std::string& ifname_to_find = getStaIfaceName();
Roshan Piusaabe5752016-09-29 09:03:59 -0700187 wifi_interface_handle* iface_handles = nullptr;
188 int num_iface_handles = 0;
189 wifi_error status = global_func_table_.wifi_get_ifaces(
190 global_handle_, &num_iface_handles, &iface_handles);
191 if (status != WIFI_SUCCESS) {
Roshan Pius511cc492016-10-28 09:54:26 -0700192 LOG(ERROR) << "Failed to enumerate interface handles";
Roshan Piusaabe5752016-09-29 09:03:59 -0700193 return status;
194 }
195 for (int i = 0; i < num_iface_handles; ++i) {
196 std::array<char, IFNAMSIZ> current_ifname;
197 current_ifname.fill(0);
198 status = global_func_table_.wifi_get_iface_name(
199 iface_handles[i], current_ifname.data(), current_ifname.size());
200 if (status != WIFI_SUCCESS) {
Roshan Pius511cc492016-10-28 09:54:26 -0700201 LOG(WARNING) << "Failed to get interface handle name";
Roshan Piusaabe5752016-09-29 09:03:59 -0700202 continue;
203 }
204 if (ifname_to_find == current_ifname.data()) {
205 wlan_interface_handle_ = iface_handles[i];
206 return WIFI_SUCCESS;
207 }
208 }
209 return WIFI_ERROR_UNKNOWN;
210}
211
212void WifiLegacyHal::runEventLoop() {
213 LOG(VERBOSE) << "Starting legacy HAL event loop";
214 global_func_table_.wifi_event_loop(global_handle_);
215 if (!awaiting_event_loop_termination_) {
216 LOG(FATAL) << "Legacy HAL event loop terminated, but HAL was not stopping";
217 }
218 LOG(VERBOSE) << "Legacy HAL event loop terminated";
219 awaiting_event_loop_termination_ = false;
Roshan Pius908a69a2016-10-03 13:33:23 -0700220 android::wifi_system::InterfaceTool if_tool;
221 if_tool.SetWifiUpState(false);
Roshan Piusaabe5752016-09-29 09:03:59 -0700222}
223
Roshan Pius511cc492016-10-28 09:54:26 -0700224void WifiLegacyHal::invalidate() {
225 global_handle_ = nullptr;
226 wlan_interface_handle_ = nullptr;
227 on_stop_complete_internal_callback = nullptr;
228 on_driver_memory_dump_internal_callback = nullptr;
229 on_firmware_memory_dump_internal_callback = nullptr;
230}
Roshan Piusaabe5752016-09-29 09:03:59 -0700231} // namespace implementation
232} // namespace V1_0
233} // namespace wifi
234} // namespace hardware
235} // namespace android