blob: e3ae1094427e893725e733f8541a9adb47444354 [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}
44}
45
46namespace android {
47namespace hardware {
48namespace wifi {
49namespace V1_0 {
50namespace implementation {
51
Roshan Pius4b26c832016-10-03 12:49:58 -070052const uint32_t WifiLegacyHal::kMaxVersionStringLength = 256;
53
Roshan Piusaabe5752016-09-29 09:03:59 -070054WifiLegacyHal::WifiLegacyHal()
55 : global_handle_(nullptr),
56 wlan_interface_handle_(nullptr),
Roshan Pius908a69a2016-10-03 13:33:23 -070057 awaiting_event_loop_termination_(false) {}
Roshan Piusaabe5752016-09-29 09:03:59 -070058
59wifi_error WifiLegacyHal::start() {
60 // Ensure that we're starting in a good state.
61 CHECK(!global_handle_ && !wlan_interface_handle_ &&
62 !awaiting_event_loop_termination_);
63
Roshan Pius908a69a2016-10-03 13:33:23 -070064 android::wifi_system::HalTool hal_tool;
65 android::wifi_system::InterfaceTool if_tool;
66 if (!hal_tool.InitFunctionTable(&global_func_table_)) {
67 LOG(ERROR) << "Failed to initialize legacy hal function table";
68 return WIFI_ERROR_UNKNOWN;
69 }
70 if (!if_tool.SetWifiUpState(true)) {
71 LOG(ERROR) << "Failed to set WiFi interface up";
72 return WIFI_ERROR_UNKNOWN;
73 }
74
Roshan Piusaabe5752016-09-29 09:03:59 -070075 LOG(INFO) << "Starting legacy HAL";
76 wifi_error status = global_func_table_.wifi_initialize(&global_handle_);
77 if (status != WIFI_SUCCESS || !global_handle_) {
78 LOG(ERROR) << "Failed to retrieve global handle";
79 return status;
80 }
81 event_loop_thread_ = std::thread(&WifiLegacyHal::runEventLoop, this);
82 status = retrieveWlanInterfaceHandle();
83 if (status != WIFI_SUCCESS || !wlan_interface_handle_) {
84 LOG(ERROR) << "Failed to retrieve wlan interface handle";
85 return status;
86 }
87 LOG(VERBOSE) << "Legacy HAL start complete";
88 return WIFI_SUCCESS;
89}
90
91wifi_error WifiLegacyHal::stop(
92 const std::function<void()>& on_stop_complete_user_callback) {
93 LOG(INFO) << "Stopping legacy HAL";
94 on_stop_complete_internal_callback = [&](wifi_handle handle) {
95 CHECK_EQ(global_handle_, handle) << "Handle mismatch";
96 on_stop_complete_user_callback();
97 global_handle_ = nullptr;
98 wlan_interface_handle_ = nullptr;
99 on_stop_complete_internal_callback = nullptr;
100 };
101 awaiting_event_loop_termination_ = true;
102 global_func_table_.wifi_cleanup(global_handle_, onStopComplete);
103 LOG(VERBOSE) << "Legacy HAL stop initiated";
104 return WIFI_SUCCESS;
105}
106
Roshan Pius4b26c832016-10-03 12:49:58 -0700107std::pair<wifi_error, std::string> WifiLegacyHal::getWlanDriverVersion() {
108 std::array<char, kMaxVersionStringLength> buffer;
109 buffer.fill(0);
110 wifi_error status = global_func_table_.wifi_get_driver_version(
111 wlan_interface_handle_, buffer.data(), buffer.size());
112 return std::make_pair(status, buffer.data());
113}
114
115std::pair<wifi_error, std::string> WifiLegacyHal::getWlanFirmwareVersion() {
116 std::array<char, kMaxVersionStringLength> buffer;
117 buffer.fill(0);
118 wifi_error status = global_func_table_.wifi_get_firmware_version(
119 wlan_interface_handle_, buffer.data(), buffer.size());
120 return std::make_pair(status, buffer.data());
121}
122
Roshan Piusaabe5752016-09-29 09:03:59 -0700123wifi_error WifiLegacyHal::retrieveWlanInterfaceHandle() {
124 const std::string& ifname_to_find = getWlanInterfaceName();
125
126 wifi_interface_handle* iface_handles = nullptr;
127 int num_iface_handles = 0;
128 wifi_error status = global_func_table_.wifi_get_ifaces(
129 global_handle_, &num_iface_handles, &iface_handles);
130 if (status != WIFI_SUCCESS) {
131 LOG(ERROR) << "Failed to enumerate interface handles: "
132 << LegacyErrorToString(status);
133 return status;
134 }
135 for (int i = 0; i < num_iface_handles; ++i) {
136 std::array<char, IFNAMSIZ> current_ifname;
137 current_ifname.fill(0);
138 status = global_func_table_.wifi_get_iface_name(
139 iface_handles[i], current_ifname.data(), current_ifname.size());
140 if (status != WIFI_SUCCESS) {
141 LOG(WARNING) << "Failed to get interface handle name: "
142 << LegacyErrorToString(status);
143 continue;
144 }
145 if (ifname_to_find == current_ifname.data()) {
146 wlan_interface_handle_ = iface_handles[i];
147 return WIFI_SUCCESS;
148 }
149 }
150 return WIFI_ERROR_UNKNOWN;
151}
152
153void WifiLegacyHal::runEventLoop() {
154 LOG(VERBOSE) << "Starting legacy HAL event loop";
155 global_func_table_.wifi_event_loop(global_handle_);
156 if (!awaiting_event_loop_termination_) {
157 LOG(FATAL) << "Legacy HAL event loop terminated, but HAL was not stopping";
158 }
159 LOG(VERBOSE) << "Legacy HAL event loop terminated";
160 awaiting_event_loop_termination_ = false;
Roshan Pius908a69a2016-10-03 13:33:23 -0700161 android::wifi_system::InterfaceTool if_tool;
162 if_tool.SetWifiUpState(false);
Roshan Piusaabe5752016-09-29 09:03:59 -0700163}
164
165} // namespace implementation
166} // namespace V1_0
167} // namespace wifi
168} // namespace hardware
169} // namespace android