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> |
| 24 | |
| 25 | namespace { |
| 26 | std::string getWlanInterfaceName() { |
| 27 | char buffer[PROPERTY_VALUE_MAX]; |
| 28 | property_get("wifi.interface", buffer, "wlan0"); |
| 29 | return buffer; |
| 30 | } |
| 31 | |
| 32 | // Legacy HAL functions accept "C" style function pointers, so use global |
| 33 | // functions to pass to the legacy HAL function and store the corresponding |
| 34 | // std::function methods to be invoked. |
| 35 | // Callback to be invoked once |stop| is complete. |
| 36 | std::function<void(wifi_handle handle)> on_stop_complete_internal_callback; |
| 37 | void onStopComplete(wifi_handle handle) { |
| 38 | if (on_stop_complete_internal_callback) { |
| 39 | on_stop_complete_internal_callback(handle); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | namespace android { |
| 45 | namespace hardware { |
| 46 | namespace wifi { |
| 47 | namespace V1_0 { |
| 48 | namespace implementation { |
| 49 | |
| 50 | WifiLegacyHal::WifiLegacyHal() |
| 51 | : global_handle_(nullptr), |
| 52 | wlan_interface_handle_(nullptr), |
| 53 | awaiting_event_loop_termination_(false) { |
| 54 | CHECK_EQ(init_wifi_vendor_hal_func_table(&global_func_table_), WIFI_SUCCESS) |
| 55 | << "Failed to initialize legacy hal function table"; |
| 56 | } |
| 57 | |
| 58 | wifi_error WifiLegacyHal::start() { |
| 59 | // Ensure that we're starting in a good state. |
| 60 | CHECK(!global_handle_ && !wlan_interface_handle_ && |
| 61 | !awaiting_event_loop_termination_); |
| 62 | |
| 63 | LOG(INFO) << "Starting legacy HAL"; |
| 64 | wifi_error status = global_func_table_.wifi_initialize(&global_handle_); |
| 65 | if (status != WIFI_SUCCESS || !global_handle_) { |
| 66 | LOG(ERROR) << "Failed to retrieve global handle"; |
| 67 | return status; |
| 68 | } |
| 69 | event_loop_thread_ = std::thread(&WifiLegacyHal::runEventLoop, this); |
| 70 | status = retrieveWlanInterfaceHandle(); |
| 71 | if (status != WIFI_SUCCESS || !wlan_interface_handle_) { |
| 72 | LOG(ERROR) << "Failed to retrieve wlan interface handle"; |
| 73 | return status; |
| 74 | } |
| 75 | LOG(VERBOSE) << "Legacy HAL start complete"; |
| 76 | return WIFI_SUCCESS; |
| 77 | } |
| 78 | |
| 79 | wifi_error WifiLegacyHal::stop( |
| 80 | const std::function<void()>& on_stop_complete_user_callback) { |
| 81 | LOG(INFO) << "Stopping legacy HAL"; |
| 82 | on_stop_complete_internal_callback = [&](wifi_handle handle) { |
| 83 | CHECK_EQ(global_handle_, handle) << "Handle mismatch"; |
| 84 | on_stop_complete_user_callback(); |
| 85 | global_handle_ = nullptr; |
| 86 | wlan_interface_handle_ = nullptr; |
| 87 | on_stop_complete_internal_callback = nullptr; |
| 88 | }; |
| 89 | awaiting_event_loop_termination_ = true; |
| 90 | global_func_table_.wifi_cleanup(global_handle_, onStopComplete); |
| 91 | LOG(VERBOSE) << "Legacy HAL stop initiated"; |
| 92 | return WIFI_SUCCESS; |
| 93 | } |
| 94 | |
| 95 | wifi_error WifiLegacyHal::retrieveWlanInterfaceHandle() { |
| 96 | const std::string& ifname_to_find = getWlanInterfaceName(); |
| 97 | |
| 98 | wifi_interface_handle* iface_handles = nullptr; |
| 99 | int num_iface_handles = 0; |
| 100 | wifi_error status = global_func_table_.wifi_get_ifaces( |
| 101 | global_handle_, &num_iface_handles, &iface_handles); |
| 102 | if (status != WIFI_SUCCESS) { |
| 103 | LOG(ERROR) << "Failed to enumerate interface handles: " |
| 104 | << LegacyErrorToString(status); |
| 105 | return status; |
| 106 | } |
| 107 | for (int i = 0; i < num_iface_handles; ++i) { |
| 108 | std::array<char, IFNAMSIZ> current_ifname; |
| 109 | current_ifname.fill(0); |
| 110 | status = global_func_table_.wifi_get_iface_name( |
| 111 | iface_handles[i], current_ifname.data(), current_ifname.size()); |
| 112 | if (status != WIFI_SUCCESS) { |
| 113 | LOG(WARNING) << "Failed to get interface handle name: " |
| 114 | << LegacyErrorToString(status); |
| 115 | continue; |
| 116 | } |
| 117 | if (ifname_to_find == current_ifname.data()) { |
| 118 | wlan_interface_handle_ = iface_handles[i]; |
| 119 | return WIFI_SUCCESS; |
| 120 | } |
| 121 | } |
| 122 | return WIFI_ERROR_UNKNOWN; |
| 123 | } |
| 124 | |
| 125 | void WifiLegacyHal::runEventLoop() { |
| 126 | LOG(VERBOSE) << "Starting legacy HAL event loop"; |
| 127 | global_func_table_.wifi_event_loop(global_handle_); |
| 128 | if (!awaiting_event_loop_termination_) { |
| 129 | LOG(FATAL) << "Legacy HAL event loop terminated, but HAL was not stopping"; |
| 130 | } |
| 131 | LOG(VERBOSE) << "Legacy HAL event loop terminated"; |
| 132 | awaiting_event_loop_termination_ = false; |
| 133 | } |
| 134 | |
| 135 | } // namespace implementation |
| 136 | } // namespace V1_0 |
| 137 | } // namespace wifi |
| 138 | } // namespace hardware |
| 139 | } // namespace android |