blob: e755feace20f3afc25c598eaa2891abcc16160f0 [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
52WifiLegacyHal::WifiLegacyHal()
53 : global_handle_(nullptr),
54 wlan_interface_handle_(nullptr),
Roshan Pius908a69a2016-10-03 13:33:23 -070055 awaiting_event_loop_termination_(false) {}
Roshan Piusaabe5752016-09-29 09:03:59 -070056
57wifi_error WifiLegacyHal::start() {
58 // Ensure that we're starting in a good state.
59 CHECK(!global_handle_ && !wlan_interface_handle_ &&
60 !awaiting_event_loop_termination_);
61
Roshan Pius908a69a2016-10-03 13:33:23 -070062 android::wifi_system::HalTool hal_tool;
63 android::wifi_system::InterfaceTool if_tool;
64 if (!hal_tool.InitFunctionTable(&global_func_table_)) {
65 LOG(ERROR) << "Failed to initialize legacy hal function table";
66 return WIFI_ERROR_UNKNOWN;
67 }
68 if (!if_tool.SetWifiUpState(true)) {
69 LOG(ERROR) << "Failed to set WiFi interface up";
70 return WIFI_ERROR_UNKNOWN;
71 }
72
Roshan Piusaabe5752016-09-29 09:03:59 -070073 LOG(INFO) << "Starting legacy HAL";
74 wifi_error status = global_func_table_.wifi_initialize(&global_handle_);
75 if (status != WIFI_SUCCESS || !global_handle_) {
76 LOG(ERROR) << "Failed to retrieve global handle";
77 return status;
78 }
79 event_loop_thread_ = std::thread(&WifiLegacyHal::runEventLoop, this);
80 status = retrieveWlanInterfaceHandle();
81 if (status != WIFI_SUCCESS || !wlan_interface_handle_) {
82 LOG(ERROR) << "Failed to retrieve wlan interface handle";
83 return status;
84 }
85 LOG(VERBOSE) << "Legacy HAL start complete";
86 return WIFI_SUCCESS;
87}
88
89wifi_error WifiLegacyHal::stop(
90 const std::function<void()>& on_stop_complete_user_callback) {
91 LOG(INFO) << "Stopping legacy HAL";
92 on_stop_complete_internal_callback = [&](wifi_handle handle) {
93 CHECK_EQ(global_handle_, handle) << "Handle mismatch";
94 on_stop_complete_user_callback();
95 global_handle_ = nullptr;
96 wlan_interface_handle_ = nullptr;
97 on_stop_complete_internal_callback = nullptr;
98 };
99 awaiting_event_loop_termination_ = true;
100 global_func_table_.wifi_cleanup(global_handle_, onStopComplete);
101 LOG(VERBOSE) << "Legacy HAL stop initiated";
102 return WIFI_SUCCESS;
103}
104
105wifi_error WifiLegacyHal::retrieveWlanInterfaceHandle() {
106 const std::string& ifname_to_find = getWlanInterfaceName();
107
108 wifi_interface_handle* iface_handles = nullptr;
109 int num_iface_handles = 0;
110 wifi_error status = global_func_table_.wifi_get_ifaces(
111 global_handle_, &num_iface_handles, &iface_handles);
112 if (status != WIFI_SUCCESS) {
113 LOG(ERROR) << "Failed to enumerate interface handles: "
114 << LegacyErrorToString(status);
115 return status;
116 }
117 for (int i = 0; i < num_iface_handles; ++i) {
118 std::array<char, IFNAMSIZ> current_ifname;
119 current_ifname.fill(0);
120 status = global_func_table_.wifi_get_iface_name(
121 iface_handles[i], current_ifname.data(), current_ifname.size());
122 if (status != WIFI_SUCCESS) {
123 LOG(WARNING) << "Failed to get interface handle name: "
124 << LegacyErrorToString(status);
125 continue;
126 }
127 if (ifname_to_find == current_ifname.data()) {
128 wlan_interface_handle_ = iface_handles[i];
129 return WIFI_SUCCESS;
130 }
131 }
132 return WIFI_ERROR_UNKNOWN;
133}
134
135void WifiLegacyHal::runEventLoop() {
136 LOG(VERBOSE) << "Starting legacy HAL event loop";
137 global_func_table_.wifi_event_loop(global_handle_);
138 if (!awaiting_event_loop_termination_) {
139 LOG(FATAL) << "Legacy HAL event loop terminated, but HAL was not stopping";
140 }
141 LOG(VERBOSE) << "Legacy HAL event loop terminated";
142 awaiting_event_loop_termination_ = false;
Roshan Pius908a69a2016-10-03 13:33:23 -0700143 android::wifi_system::InterfaceTool if_tool;
144 if_tool.SetWifiUpState(false);
Roshan Piusaabe5752016-09-29 09:03:59 -0700145}
146
147} // namespace implementation
148} // namespace V1_0
149} // namespace wifi
150} // namespace hardware
151} // namespace android