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 | } |
| 44 | } |
| 45 | |
| 46 | namespace android { |
| 47 | namespace hardware { |
| 48 | namespace wifi { |
| 49 | namespace V1_0 { |
| 50 | namespace implementation { |
| 51 | |
Roshan Pius | 4b26c83 | 2016-10-03 12:49:58 -0700 | [diff] [blame^] | 52 | const uint32_t WifiLegacyHal::kMaxVersionStringLength = 256; |
| 53 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 54 | WifiLegacyHal::WifiLegacyHal() |
| 55 | : global_handle_(nullptr), |
| 56 | wlan_interface_handle_(nullptr), |
Roshan Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 57 | awaiting_event_loop_termination_(false) {} |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 58 | |
| 59 | wifi_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 Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 64 | 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 Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 75 | 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 | |
| 91 | wifi_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 Pius | 4b26c83 | 2016-10-03 12:49:58 -0700 | [diff] [blame^] | 107 | std::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 | |
| 115 | std::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 Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 123 | wifi_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 | |
| 153 | void 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 Pius | 908a69a | 2016-10-03 13:33:23 -0700 | [diff] [blame] | 161 | android::wifi_system::InterfaceTool if_tool; |
| 162 | if_tool.SetWifiUpState(false); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | } // namespace implementation |
| 166 | } // namespace V1_0 |
| 167 | } // namespace wifi |
| 168 | } // namespace hardware |
| 169 | } // namespace android |