Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -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 "wifi.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <cutils/properties.h> |
| 21 | |
| 22 | #include "failure_reason_util.h" |
| 23 | #include "wifi_chip.h" |
| 24 | |
| 25 | using ::android::hardware::wifi::V1_0::CommandFailureReason; |
| 26 | using RunState = ::android::hardware::wifi::WifiHalState::RunState; |
| 27 | |
| 28 | namespace { |
| 29 | std::string GetWlanInterfaceName() { |
| 30 | char buffer[PROPERTY_VALUE_MAX]; |
| 31 | property_get("wifi.interface", buffer, "wlan0"); |
| 32 | return buffer; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | namespace android { |
| 37 | namespace hardware { |
| 38 | namespace wifi { |
| 39 | |
| 40 | Wifi::Wifi(sp<Looper>& looper) : state_(looper) { |
| 41 | CHECK_EQ(init_wifi_vendor_hal_func_table(&state_.func_table_), WIFI_SUCCESS) |
| 42 | << "Failed to initialize hal func table"; |
| 43 | } |
| 44 | |
| 45 | Return<void> Wifi::registerEventCallback( |
| 46 | const sp<V1_0::IWifiEventCallback>& callback) { |
| 47 | // TODO(b/31632518): remove the callback when the client is destroyed |
| 48 | callbacks_.insert(callback); |
| 49 | return Void(); |
| 50 | } |
| 51 | |
| 52 | Return<bool> Wifi::isStarted() { |
| 53 | return state_.run_state_ != RunState::STOPPED; |
| 54 | } |
| 55 | |
| 56 | Return<void> Wifi::start() { |
| 57 | if (state_.run_state_ == RunState::STARTED) { |
| 58 | for (auto& callback : callbacks_) { |
| 59 | callback->onStart(); |
| 60 | } |
| 61 | return Void(); |
| 62 | } else if (state_.run_state_ == RunState::STOPPING) { |
| 63 | for (auto& callback : callbacks_) { |
| 64 | callback->onStartFailure(CreateFailureReason( |
| 65 | CommandFailureReason::NOT_AVAILABLE, "HAL is stopping")); |
| 66 | } |
| 67 | return Void(); |
| 68 | } |
| 69 | |
| 70 | LOG(INFO) << "Initializing HAL"; |
| 71 | wifi_error status = state_.func_table_.wifi_initialize(&state_.hal_handle_); |
| 72 | if (status != WIFI_SUCCESS) { |
| 73 | LOG(ERROR) << "Failed to initialize Wifi HAL"; |
| 74 | for (auto& callback : callbacks_) { |
| 75 | callback->onStartFailure(CreateFailureReasonLegacyError( |
| 76 | status, "Failed to initialize HAL")); |
| 77 | } |
| 78 | return Void(); |
| 79 | } |
| 80 | |
| 81 | event_loop_thread_ = std::thread(&Wifi::DoHalEventLoop, this); |
| 82 | |
| 83 | wifi_interface_handle iface_handle = |
| 84 | FindInterfaceHandle(GetWlanInterfaceName()); |
| 85 | if (iface_handle != kInterfaceNotFoundHandle) { |
| 86 | chip_ = new WifiChip(&state_, iface_handle); |
| 87 | } else { |
| 88 | // TODO fail to init? |
| 89 | } |
| 90 | |
| 91 | state_.run_state_ = RunState::STARTED; |
| 92 | for (auto& callback : callbacks_) { |
| 93 | callback->onStart(); |
| 94 | } |
| 95 | return Void(); |
| 96 | } |
| 97 | |
| 98 | wifi_interface_handle Wifi::FindInterfaceHandle( |
| 99 | const std::string& ifname) { |
| 100 | int num_iface_handles = 0; |
| 101 | wifi_interface_handle* iface_handles = nullptr; |
| 102 | wifi_error ret = state_.func_table_.wifi_get_ifaces( |
| 103 | state_.hal_handle_, &num_iface_handles, &iface_handles); |
| 104 | if (ret != WIFI_SUCCESS) { |
| 105 | LOG(ERROR) << "Failed to enumerate interface handles: " |
| 106 | << LegacyErrorToString(ret); |
| 107 | return kInterfaceNotFoundHandle; |
| 108 | } |
| 109 | |
| 110 | char buffer[IFNAMSIZ]; |
| 111 | for (int i = 0; i < num_iface_handles; ++i) { |
| 112 | bzero(buffer, sizeof(buffer)); |
| 113 | ret = state_.func_table_.wifi_get_iface_name( |
| 114 | iface_handles[i], buffer, sizeof(buffer)); |
| 115 | if (ret != WIFI_SUCCESS) { |
| 116 | LOG(WARNING) << "Failed to get interface handle name: " |
| 117 | << LegacyErrorToString(ret); |
| 118 | continue; |
| 119 | } |
| 120 | if (ifname == buffer) { |
| 121 | return iface_handles[i]; |
| 122 | } |
| 123 | } |
| 124 | return kInterfaceNotFoundHandle; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | void NoopHalCleanupHandler(wifi_handle) {} |
| 129 | |
| 130 | Return<void> Wifi::stop() { |
| 131 | if (state_.run_state_ == RunState::STOPPED) { |
| 132 | for (auto& callback : callbacks_) { |
| 133 | callback->onStop(); |
| 134 | } |
| 135 | return Void(); |
| 136 | } else if (state_.run_state_ == RunState::STOPPING) { |
| 137 | return Void(); |
| 138 | } |
| 139 | |
| 140 | LOG(INFO) << "Cleaning up HAL"; |
| 141 | awaiting_hal_cleanup_command_ = true; |
| 142 | awaiting_hal_event_loop_termination_ = true; |
| 143 | state_.run_state_ = RunState::STOPPING; |
| 144 | |
| 145 | if (chip_.get()) chip_->Invalidate(); |
| 146 | chip_.clear(); |
| 147 | |
| 148 | state_.func_table_.wifi_cleanup(state_.hal_handle_, NoopHalCleanupHandler); |
| 149 | awaiting_hal_cleanup_command_ = false; |
| 150 | LOG(VERBOSE) << "HAL cleanup command complete"; |
| 151 | FinishHalCleanup(); |
| 152 | return Void(); |
| 153 | } |
| 154 | |
| 155 | void Wifi::DoHalEventLoop() { |
| 156 | LOG(VERBOSE) << "Starting HAL event loop"; |
| 157 | state_.func_table_.wifi_event_loop(state_.hal_handle_); |
| 158 | if (state_.run_state_ != RunState::STOPPING) { |
| 159 | LOG(FATAL) << "HAL event loop terminated, but HAL was not stopping"; |
| 160 | } |
| 161 | LOG(VERBOSE) << "HAL Event loop terminated"; |
| 162 | event_loop_thread_.detach(); |
| 163 | state_.PostTask([this](){ |
| 164 | awaiting_hal_event_loop_termination_ = false; |
| 165 | FinishHalCleanup(); |
| 166 | }); |
| 167 | } |
| 168 | |
| 169 | void Wifi::FinishHalCleanup() { |
| 170 | if (!awaiting_hal_cleanup_command_ && !awaiting_hal_event_loop_termination_) { |
| 171 | state_.run_state_ = RunState::STOPPED; |
| 172 | LOG(INFO) << "HAL cleanup complete"; |
| 173 | for (auto& callback : callbacks_) { |
| 174 | callback->onStop(); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | Return<void> Wifi::getChip(getChip_cb cb) { |
| 181 | cb(chip_); |
| 182 | return Void(); |
| 183 | } |
| 184 | |
| 185 | } // namespace wifi |
| 186 | } // namespace hardware |
| 187 | } // namespace android |