Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 24 | #include <wifi_system/hal_tool.h> |
| 25 | #include <wifi_system/interface_tool.h> |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | std::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. |
| 38 | std::function<void(wifi_handle handle)> on_stop_complete_internal_callback; |
| 39 | void onStopComplete(wifi_handle handle) { |
| 40 | if (on_stop_complete_internal_callback) { |
| 41 | on_stop_complete_internal_callback(handle); |
| 42 | } |
| 43 | } |
Roshan Pius | cdb77f3 | 2016-10-03 14:09:57 -0700 | [diff] [blame^] | 44 | |
| 45 | // Callback to be invoked for driver dump. |
| 46 | std::function<void(char*, int)> on_driver_memory_dump_internal_callback; |
| 47 | void 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. |
| 54 | std::function<void(char*, int)> on_firmware_memory_dump_internal_callback; |
| 55 | void 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 Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | namespace android { |
| 63 | namespace hardware { |
| 64 | namespace wifi { |
| 65 | namespace V1_0 { |
| 66 | namespace implementation { |
| 67 | |
Roshan Pius | 4b26c83 | 2016-10-03 12:49:58 -0700 | [diff] [blame] | 68 | const uint32_t WifiLegacyHal::kMaxVersionStringLength = 256; |
| 69 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 70 | WifiLegacyHal::WifiLegacyHal() |
| 71 | : global_handle_(nullptr), |
| 72 | wlan_interface_handle_(nullptr), |
Roshan Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 73 | awaiting_event_loop_termination_(false) {} |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 74 | |
| 75 | wifi_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 Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 80 | 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 Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 91 | 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 | |
| 107 | wifi_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 Pius | 4b26c83 | 2016-10-03 12:49:58 -0700 | [diff] [blame] | 123 | std::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 | |
| 131 | std::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 Pius | cdb77f3 | 2016-10-03 14:09:57 -0700 | [diff] [blame^] | 139 | std::pair<wifi_error, std::vector<char>> |
| 140 | WifiLegacyHal::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 | |
| 152 | std::pair<wifi_error, std::vector<char>> |
| 153 | WifiLegacyHal::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 Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 165 | wifi_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 | |
| 195 | void 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 Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 203 | android::wifi_system::InterfaceTool if_tool; |
| 204 | if_tool.SetWifiUpState(false); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | } // namespace implementation |
| 208 | } // namespace V1_0 |
| 209 | } // namespace wifi |
| 210 | } // namespace hardware |
| 211 | } // namespace android |