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 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | #include <cutils/properties.h> |
Roshan Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 21 | #include <wifi_system/interface_tool.h> |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 22 | |
Roshan Pius | 955542e | 2016-10-28 09:42:44 -0700 | [diff] [blame] | 23 | #include "wifi_legacy_hal.h" |
| 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace wifi { |
| 28 | namespace V1_0 { |
| 29 | namespace implementation { |
| 30 | namespace legacy_hal { |
| 31 | // Constants used in the class. |
Roshan Pius | 511cc49 | 2016-10-28 09:54:26 -0700 | [diff] [blame] | 32 | static constexpr uint32_t kMaxVersionStringLength = 256; |
| 33 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 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 | 955542e | 2016-10-28 09:42:44 -0700 | [diff] [blame] | 60 | // End of the free-standing "C" style callbacks. |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 61 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 62 | WifiLegacyHal::WifiLegacyHal() |
| 63 | : global_handle_(nullptr), |
| 64 | wlan_interface_handle_(nullptr), |
Roshan Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 65 | awaiting_event_loop_termination_(false) {} |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 66 | |
| 67 | wifi_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 Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 72 | android::wifi_system::InterfaceTool if_tool; |
Roshan Pius | 955542e | 2016-10-28 09:42:44 -0700 | [diff] [blame] | 73 | // TODO: Add back the HAL Tool if we need to. All we need from the HAL tool |
| 74 | // for now is this function call which we can directly call. |
| 75 | wifi_error status = init_wifi_vendor_hal_func_table(&global_func_table_); |
| 76 | if (status != WIFI_SUCCESS) { |
Roshan Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 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 Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 85 | LOG(INFO) << "Starting legacy HAL"; |
Roshan Pius | 955542e | 2016-10-28 09:42:44 -0700 | [diff] [blame] | 86 | status = global_func_table_.wifi_initialize(&global_handle_); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 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 | |
| 101 | wifi_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(); |
Roshan Pius | 511cc49 | 2016-10-28 09:54:26 -0700 | [diff] [blame] | 107 | // Invalidate all the internal pointers now that the HAL is |
| 108 | // stopped. |
| 109 | invalidate(); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 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 Pius | ab5c471 | 2016-10-06 14:37:15 -0700 | [diff] [blame] | 117 | std::string WifiLegacyHal::getApIfaceName() { |
| 118 | // Fake name. This interface does not exist in legacy HAL |
| 119 | // API's. |
| 120 | return "ap0"; |
| 121 | } |
| 122 | |
| 123 | std::string WifiLegacyHal::getNanIfaceName() { |
| 124 | // Fake name. This interface does not exist in legacy HAL |
| 125 | // API's. |
| 126 | return "nan0"; |
| 127 | } |
| 128 | |
| 129 | std::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 | |
| 135 | std::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 | |
| 141 | std::pair<wifi_error, std::string> WifiLegacyHal::getDriverVersion() { |
Roshan Pius | 4b26c83 | 2016-10-03 12:49:58 -0700 | [diff] [blame] | 142 | 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()); |
Roshan Pius | 0a47c18 | 2016-10-28 10:23:00 -0700 | [diff] [blame^] | 146 | return {status, buffer.data()}; |
Roshan Pius | 4b26c83 | 2016-10-03 12:49:58 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Roshan Pius | ab5c471 | 2016-10-06 14:37:15 -0700 | [diff] [blame] | 149 | std::pair<wifi_error, std::string> WifiLegacyHal::getFirmwareVersion() { |
Roshan Pius | 4b26c83 | 2016-10-03 12:49:58 -0700 | [diff] [blame] | 150 | 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()); |
Roshan Pius | 0a47c18 | 2016-10-28 10:23:00 -0700 | [diff] [blame^] | 154 | return {status, buffer.data()}; |
Roshan Pius | 4b26c83 | 2016-10-03 12:49:58 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Roshan Pius | 3c86852 | 2016-10-27 12:43:49 -0700 | [diff] [blame] | 157 | std::pair<wifi_error, std::vector<uint8_t>> |
Roshan Pius | ab5c471 | 2016-10-06 14:37:15 -0700 | [diff] [blame] | 158 | WifiLegacyHal::requestDriverMemoryDump() { |
Roshan Pius | 3c86852 | 2016-10-27 12:43:49 -0700 | [diff] [blame] | 159 | std::vector<uint8_t> driver_dump; |
Roshan Pius | cdb77f3 | 2016-10-03 14:09:57 -0700 | [diff] [blame] | 160 | on_driver_memory_dump_internal_callback = [&driver_dump](char* buffer, |
| 161 | int buffer_size) { |
Roshan Pius | 3c86852 | 2016-10-27 12:43:49 -0700 | [diff] [blame] | 162 | driver_dump.insert(driver_dump.end(), |
| 163 | reinterpret_cast<uint8_t*>(buffer), |
| 164 | reinterpret_cast<uint8_t*>(buffer) + buffer_size); |
Roshan Pius | cdb77f3 | 2016-10-03 14:09:57 -0700 | [diff] [blame] | 165 | }; |
| 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; |
Roshan Pius | 0a47c18 | 2016-10-28 10:23:00 -0700 | [diff] [blame^] | 169 | return {status, std::move(driver_dump)}; |
Roshan Pius | cdb77f3 | 2016-10-03 14:09:57 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Roshan Pius | 3c86852 | 2016-10-27 12:43:49 -0700 | [diff] [blame] | 172 | std::pair<wifi_error, std::vector<uint8_t>> |
Roshan Pius | ab5c471 | 2016-10-06 14:37:15 -0700 | [diff] [blame] | 173 | WifiLegacyHal::requestFirmwareMemoryDump() { |
Roshan Pius | 3c86852 | 2016-10-27 12:43:49 -0700 | [diff] [blame] | 174 | std::vector<uint8_t> firmware_dump; |
Roshan Pius | cdb77f3 | 2016-10-03 14:09:57 -0700 | [diff] [blame] | 175 | on_firmware_memory_dump_internal_callback = [&firmware_dump]( |
| 176 | char* buffer, int buffer_size) { |
Roshan Pius | 3c86852 | 2016-10-27 12:43:49 -0700 | [diff] [blame] | 177 | firmware_dump.insert(firmware_dump.end(), |
| 178 | reinterpret_cast<uint8_t*>(buffer), |
| 179 | reinterpret_cast<uint8_t*>(buffer) + buffer_size); |
Roshan Pius | cdb77f3 | 2016-10-03 14:09:57 -0700 | [diff] [blame] | 180 | }; |
| 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; |
Roshan Pius | 0a47c18 | 2016-10-28 10:23:00 -0700 | [diff] [blame^] | 184 | return {status, std::move(firmware_dump)}; |
| 185 | } |
| 186 | |
| 187 | std::pair<wifi_error, uint32_t> WifiLegacyHal::getSupportedFeatureSet() { |
| 188 | feature_set set; |
| 189 | static_assert(sizeof(set) == sizeof(uint32_t), |
| 190 | "Some features can not be represented in output"); |
| 191 | wifi_error status = global_func_table_.wifi_get_supported_feature_set( |
| 192 | wlan_interface_handle_, &set); |
| 193 | return {status, static_cast<uint32_t>(set)}; |
| 194 | } |
| 195 | |
| 196 | std::pair<wifi_error, PacketFilterCapabilities> |
| 197 | WifiLegacyHal::getPacketFilterCapabilities() { |
| 198 | PacketFilterCapabilities caps; |
| 199 | wifi_error status = global_func_table_.wifi_get_packet_filter_capabilities( |
| 200 | wlan_interface_handle_, &caps.version, &caps.max_len); |
| 201 | return {status, caps}; |
| 202 | } |
| 203 | |
| 204 | wifi_error WifiLegacyHal::setPacketFilter(const std::vector<uint8_t>& program) { |
| 205 | return global_func_table_.wifi_set_packet_filter( |
| 206 | wlan_interface_handle_, program.data(), program.size()); |
Roshan Pius | cdb77f3 | 2016-10-03 14:09:57 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 209 | wifi_error WifiLegacyHal::retrieveWlanInterfaceHandle() { |
Roshan Pius | ab5c471 | 2016-10-06 14:37:15 -0700 | [diff] [blame] | 210 | const std::string& ifname_to_find = getStaIfaceName(); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 211 | wifi_interface_handle* iface_handles = nullptr; |
| 212 | int num_iface_handles = 0; |
| 213 | wifi_error status = global_func_table_.wifi_get_ifaces( |
| 214 | global_handle_, &num_iface_handles, &iface_handles); |
| 215 | if (status != WIFI_SUCCESS) { |
Roshan Pius | 511cc49 | 2016-10-28 09:54:26 -0700 | [diff] [blame] | 216 | LOG(ERROR) << "Failed to enumerate interface handles"; |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 217 | return status; |
| 218 | } |
| 219 | for (int i = 0; i < num_iface_handles; ++i) { |
| 220 | std::array<char, IFNAMSIZ> current_ifname; |
| 221 | current_ifname.fill(0); |
| 222 | status = global_func_table_.wifi_get_iface_name( |
| 223 | iface_handles[i], current_ifname.data(), current_ifname.size()); |
| 224 | if (status != WIFI_SUCCESS) { |
Roshan Pius | 511cc49 | 2016-10-28 09:54:26 -0700 | [diff] [blame] | 225 | LOG(WARNING) << "Failed to get interface handle name"; |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 226 | continue; |
| 227 | } |
| 228 | if (ifname_to_find == current_ifname.data()) { |
| 229 | wlan_interface_handle_ = iface_handles[i]; |
| 230 | return WIFI_SUCCESS; |
| 231 | } |
| 232 | } |
| 233 | return WIFI_ERROR_UNKNOWN; |
| 234 | } |
| 235 | |
| 236 | void WifiLegacyHal::runEventLoop() { |
| 237 | LOG(VERBOSE) << "Starting legacy HAL event loop"; |
| 238 | global_func_table_.wifi_event_loop(global_handle_); |
| 239 | if (!awaiting_event_loop_termination_) { |
| 240 | LOG(FATAL) << "Legacy HAL event loop terminated, but HAL was not stopping"; |
| 241 | } |
| 242 | LOG(VERBOSE) << "Legacy HAL event loop terminated"; |
| 243 | awaiting_event_loop_termination_ = false; |
Roshan Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 244 | android::wifi_system::InterfaceTool if_tool; |
| 245 | if_tool.SetWifiUpState(false); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Roshan Pius | 511cc49 | 2016-10-28 09:54:26 -0700 | [diff] [blame] | 248 | void WifiLegacyHal::invalidate() { |
| 249 | global_handle_ = nullptr; |
| 250 | wlan_interface_handle_ = nullptr; |
| 251 | on_stop_complete_internal_callback = nullptr; |
| 252 | on_driver_memory_dump_internal_callback = nullptr; |
| 253 | on_firmware_memory_dump_internal_callback = nullptr; |
| 254 | } |
Roshan Pius | 955542e | 2016-10-28 09:42:44 -0700 | [diff] [blame] | 255 | |
| 256 | } // namespace legacy_hal |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 257 | } // namespace implementation |
| 258 | } // namespace V1_0 |
| 259 | } // namespace wifi |
| 260 | } // namespace hardware |
| 261 | } // namespace android |