blob: 5bd2454a99605bcf32ebfcdebefdd3ee38348c05 [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"
Roshan Pius734fea02016-10-11 08:30:28 -070020#include "wifi_status_util.h"
Roshan Piusaabe5752016-09-29 09:03:59 -070021
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 {
Roshan Piusaabe5752016-09-29 09:03:59 -070028// Legacy HAL functions accept "C" style function pointers, so use global
29// functions to pass to the legacy HAL function and store the corresponding
30// std::function methods to be invoked.
31// Callback to be invoked once |stop| is complete.
32std::function<void(wifi_handle handle)> on_stop_complete_internal_callback;
33void onStopComplete(wifi_handle handle) {
34 if (on_stop_complete_internal_callback) {
35 on_stop_complete_internal_callback(handle);
36 }
37}
Roshan Piuscdb77f32016-10-03 14:09:57 -070038
39// Callback to be invoked for driver dump.
40std::function<void(char*, int)> on_driver_memory_dump_internal_callback;
41void onDriverMemoryDump(char* buffer, int buffer_size) {
42 if (on_driver_memory_dump_internal_callback) {
43 on_driver_memory_dump_internal_callback(buffer, buffer_size);
44 }
45}
46
47// Callback to be invoked for firmware dump.
48std::function<void(char*, int)> on_firmware_memory_dump_internal_callback;
49void onFirmwareMemoryDump(char* buffer, int buffer_size) {
50 if (on_firmware_memory_dump_internal_callback) {
51 on_firmware_memory_dump_internal_callback(buffer, buffer_size);
52 }
53}
Roshan Piusaabe5752016-09-29 09:03:59 -070054}
55
56namespace android {
57namespace hardware {
58namespace wifi {
59namespace V1_0 {
60namespace implementation {
61
Roshan Pius4b26c832016-10-03 12:49:58 -070062const uint32_t WifiLegacyHal::kMaxVersionStringLength = 256;
63
Roshan Piusaabe5752016-09-29 09:03:59 -070064WifiLegacyHal::WifiLegacyHal()
65 : global_handle_(nullptr),
66 wlan_interface_handle_(nullptr),
Roshan Pius908a69a2016-10-03 13:33:23 -070067 awaiting_event_loop_termination_(false) {}
Roshan Piusaabe5752016-09-29 09:03:59 -070068
69wifi_error WifiLegacyHal::start() {
70 // Ensure that we're starting in a good state.
71 CHECK(!global_handle_ && !wlan_interface_handle_ &&
72 !awaiting_event_loop_termination_);
73
Roshan Pius908a69a2016-10-03 13:33:23 -070074 android::wifi_system::HalTool hal_tool;
75 android::wifi_system::InterfaceTool if_tool;
76 if (!hal_tool.InitFunctionTable(&global_func_table_)) {
77 LOG(ERROR) << "Failed to initialize legacy hal function table";
78 return WIFI_ERROR_UNKNOWN;
79 }
80 if (!if_tool.SetWifiUpState(true)) {
81 LOG(ERROR) << "Failed to set WiFi interface up";
82 return WIFI_ERROR_UNKNOWN;
83 }
84
Roshan Piusaabe5752016-09-29 09:03:59 -070085 LOG(INFO) << "Starting legacy HAL";
86 wifi_error status = global_func_table_.wifi_initialize(&global_handle_);
87 if (status != WIFI_SUCCESS || !global_handle_) {
88 LOG(ERROR) << "Failed to retrieve global handle";
89 return status;
90 }
91 event_loop_thread_ = std::thread(&WifiLegacyHal::runEventLoop, this);
92 status = retrieveWlanInterfaceHandle();
93 if (status != WIFI_SUCCESS || !wlan_interface_handle_) {
94 LOG(ERROR) << "Failed to retrieve wlan interface handle";
95 return status;
96 }
97 LOG(VERBOSE) << "Legacy HAL start complete";
98 return WIFI_SUCCESS;
99}
100
101wifi_error WifiLegacyHal::stop(
102 const std::function<void()>& on_stop_complete_user_callback) {
103 LOG(INFO) << "Stopping legacy HAL";
104 on_stop_complete_internal_callback = [&](wifi_handle handle) {
105 CHECK_EQ(global_handle_, handle) << "Handle mismatch";
106 on_stop_complete_user_callback();
107 global_handle_ = nullptr;
108 wlan_interface_handle_ = nullptr;
109 on_stop_complete_internal_callback = nullptr;
110 };
111 awaiting_event_loop_termination_ = true;
112 global_func_table_.wifi_cleanup(global_handle_, onStopComplete);
113 LOG(VERBOSE) << "Legacy HAL stop initiated";
114 return WIFI_SUCCESS;
115}
116
Roshan Piusab5c4712016-10-06 14:37:15 -0700117std::string WifiLegacyHal::getApIfaceName() {
118 // Fake name. This interface does not exist in legacy HAL
119 // API's.
120 return "ap0";
121}
122
123std::string WifiLegacyHal::getNanIfaceName() {
124 // Fake name. This interface does not exist in legacy HAL
125 // API's.
126 return "nan0";
127}
128
129std::string WifiLegacyHal::getP2pIfaceName() {
130 std::array<char, PROPERTY_VALUE_MAX> buffer;
131 property_get("wifi.direct.interface", buffer.data(), "p2p0");
132 return buffer.data();
133}
134
135std::string WifiLegacyHal::getStaIfaceName() {
136 std::array<char, PROPERTY_VALUE_MAX> buffer;
137 property_get("wifi.interface", buffer.data(), "wlan0");
138 return buffer.data();
139}
140
141std::pair<wifi_error, std::string> WifiLegacyHal::getDriverVersion() {
Roshan Pius4b26c832016-10-03 12:49:58 -0700142 std::array<char, kMaxVersionStringLength> buffer;
143 buffer.fill(0);
144 wifi_error status = global_func_table_.wifi_get_driver_version(
145 wlan_interface_handle_, buffer.data(), buffer.size());
146 return std::make_pair(status, buffer.data());
147}
148
Roshan Piusab5c4712016-10-06 14:37:15 -0700149std::pair<wifi_error, std::string> WifiLegacyHal::getFirmwareVersion() {
Roshan Pius4b26c832016-10-03 12:49:58 -0700150 std::array<char, kMaxVersionStringLength> buffer;
151 buffer.fill(0);
152 wifi_error status = global_func_table_.wifi_get_firmware_version(
153 wlan_interface_handle_, buffer.data(), buffer.size());
154 return std::make_pair(status, buffer.data());
155}
156
Roshan Pius3c868522016-10-27 12:43:49 -0700157std::pair<wifi_error, std::vector<uint8_t>>
Roshan Piusab5c4712016-10-06 14:37:15 -0700158WifiLegacyHal::requestDriverMemoryDump() {
Roshan Pius3c868522016-10-27 12:43:49 -0700159 std::vector<uint8_t> driver_dump;
Roshan Piuscdb77f32016-10-03 14:09:57 -0700160 on_driver_memory_dump_internal_callback = [&driver_dump](char* buffer,
161 int buffer_size) {
Roshan Pius3c868522016-10-27 12:43:49 -0700162 driver_dump.insert(driver_dump.end(),
163 reinterpret_cast<uint8_t*>(buffer),
164 reinterpret_cast<uint8_t*>(buffer) + buffer_size);
Roshan Piuscdb77f32016-10-03 14:09:57 -0700165 };
166 wifi_error status = global_func_table_.wifi_get_driver_memory_dump(
167 wlan_interface_handle_, {onDriverMemoryDump});
168 on_driver_memory_dump_internal_callback = nullptr;
169 return std::make_pair(status, std::move(driver_dump));
170}
171
Roshan Pius3c868522016-10-27 12:43:49 -0700172std::pair<wifi_error, std::vector<uint8_t>>
Roshan Piusab5c4712016-10-06 14:37:15 -0700173WifiLegacyHal::requestFirmwareMemoryDump() {
Roshan Pius3c868522016-10-27 12:43:49 -0700174 std::vector<uint8_t> firmware_dump;
Roshan Piuscdb77f32016-10-03 14:09:57 -0700175 on_firmware_memory_dump_internal_callback = [&firmware_dump](
176 char* buffer, int buffer_size) {
Roshan Pius3c868522016-10-27 12:43:49 -0700177 firmware_dump.insert(firmware_dump.end(),
178 reinterpret_cast<uint8_t*>(buffer),
179 reinterpret_cast<uint8_t*>(buffer) + buffer_size);
Roshan Piuscdb77f32016-10-03 14:09:57 -0700180 };
181 wifi_error status = global_func_table_.wifi_get_firmware_memory_dump(
182 wlan_interface_handle_, {onFirmwareMemoryDump});
183 on_firmware_memory_dump_internal_callback = nullptr;
184 return std::make_pair(status, std::move(firmware_dump));
185}
186
Roshan Piusaabe5752016-09-29 09:03:59 -0700187wifi_error WifiLegacyHal::retrieveWlanInterfaceHandle() {
Roshan Piusab5c4712016-10-06 14:37:15 -0700188 const std::string& ifname_to_find = getStaIfaceName();
Roshan Piusaabe5752016-09-29 09:03:59 -0700189 wifi_interface_handle* iface_handles = nullptr;
190 int num_iface_handles = 0;
191 wifi_error status = global_func_table_.wifi_get_ifaces(
192 global_handle_, &num_iface_handles, &iface_handles);
193 if (status != WIFI_SUCCESS) {
194 LOG(ERROR) << "Failed to enumerate interface handles: "
Roshan Pius734fea02016-10-11 08:30:28 -0700195 << legacyErrorToString(status);
Roshan Piusaabe5752016-09-29 09:03:59 -0700196 return status;
197 }
198 for (int i = 0; i < num_iface_handles; ++i) {
199 std::array<char, IFNAMSIZ> current_ifname;
200 current_ifname.fill(0);
201 status = global_func_table_.wifi_get_iface_name(
202 iface_handles[i], current_ifname.data(), current_ifname.size());
203 if (status != WIFI_SUCCESS) {
204 LOG(WARNING) << "Failed to get interface handle name: "
Roshan Pius734fea02016-10-11 08:30:28 -0700205 << legacyErrorToString(status);
Roshan Piusaabe5752016-09-29 09:03:59 -0700206 continue;
207 }
208 if (ifname_to_find == current_ifname.data()) {
209 wlan_interface_handle_ = iface_handles[i];
210 return WIFI_SUCCESS;
211 }
212 }
213 return WIFI_ERROR_UNKNOWN;
214}
215
216void WifiLegacyHal::runEventLoop() {
217 LOG(VERBOSE) << "Starting legacy HAL event loop";
218 global_func_table_.wifi_event_loop(global_handle_);
219 if (!awaiting_event_loop_termination_) {
220 LOG(FATAL) << "Legacy HAL event loop terminated, but HAL was not stopping";
221 }
222 LOG(VERBOSE) << "Legacy HAL event loop terminated";
223 awaiting_event_loop_termination_ = false;
Roshan Pius908a69a2016-10-03 13:33:23 -0700224 android::wifi_system::InterfaceTool if_tool;
225 if_tool.SetWifiUpState(false);
Roshan Piusaabe5752016-09-29 09:03:59 -0700226}
227
228} // namespace implementation
229} // namespace V1_0
230} // namespace wifi
231} // namespace hardware
232} // namespace android