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> |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 20 | |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 21 | #include "wifi_chip.h" |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 22 | #include "wifi_status_util.h" |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 23 | |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 24 | namespace { |
| 25 | // Chip ID to use for the only supported chip. |
| 26 | static constexpr android::hardware::wifi::V1_0::ChipId kChipId = 0; |
| 27 | } // namespace |
| 28 | |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace hardware { |
| 31 | namespace wifi { |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 32 | namespace V1_0 { |
| 33 | namespace implementation { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 34 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 35 | Wifi::Wifi() |
| 36 | : legacy_hal_(new WifiLegacyHal()), run_state_(RunState::STOPPED) {} |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 37 | |
| 38 | Return<void> Wifi::registerEventCallback( |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 39 | const sp<IWifiEventCallback>& event_callback) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 40 | // TODO(b/31632518): remove the callback when the client is destroyed |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 41 | event_callbacks_.emplace_back(event_callback); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 42 | return Void(); |
| 43 | } |
| 44 | |
| 45 | Return<bool> Wifi::isStarted() { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 46 | return run_state_ != RunState::STOPPED; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 49 | Return<void> Wifi::start(start_cb hidl_status_cb) { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 50 | if (run_state_ == RunState::STARTED) { |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 51 | hidl_status_cb(createWifiStatus(WifiStatusCode::SUCCESS)); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 52 | return Void(); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 53 | } else if (run_state_ == RunState::STOPPING) { |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 54 | hidl_status_cb(createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, |
| 55 | "HAL is stopping")); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 56 | return Void(); |
| 57 | } |
| 58 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 59 | LOG(INFO) << "Starting HAL"; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 60 | wifi_error legacy_status = legacy_hal_->start(); |
| 61 | if (legacy_status != WIFI_SUCCESS) { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 62 | LOG(ERROR) << "Failed to start Wifi HAL"; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 63 | hidl_status_cb( |
| 64 | createWifiStatusFromLegacyError(legacy_status, "Failed to start HAL")); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 65 | return Void(); |
| 66 | } |
| 67 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 68 | // Create the chip instance once the HAL is started. |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 69 | chip_ = new WifiChip(kChipId, legacy_hal_); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 70 | run_state_ = RunState::STARTED; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 71 | for (const auto& callback : event_callbacks_) { |
| 72 | if (!callback->onStart().getStatus().isOk()) { |
| 73 | LOG(ERROR) << "Failed to invoke onStart callback"; |
| 74 | }; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 75 | } |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 76 | hidl_status_cb(createWifiStatus(WifiStatusCode::SUCCESS)); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 77 | return Void(); |
| 78 | } |
| 79 | |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 80 | Return<void> Wifi::stop(stop_cb hidl_status_cb) { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 81 | if (run_state_ == RunState::STOPPED) { |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 82 | hidl_status_cb(createWifiStatus(WifiStatusCode::SUCCESS)); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 83 | return Void(); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 84 | } else if (run_state_ == RunState::STOPPING) { |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 85 | hidl_status_cb(createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, |
| 86 | "HAL is stopping")); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 87 | return Void(); |
| 88 | } |
| 89 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 90 | LOG(INFO) << "Stopping HAL"; |
| 91 | run_state_ = RunState::STOPPING; |
| 92 | const auto on_complete_callback_ = [&]() { |
| 93 | if (chip_.get()) { |
| 94 | chip_->invalidate(); |
| 95 | } |
| 96 | chip_.clear(); |
| 97 | run_state_ = RunState::STOPPED; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 98 | for (const auto& callback : event_callbacks_) { |
| 99 | if (!callback->onStop().getStatus().isOk()) { |
| 100 | LOG(ERROR) << "Failed to invoke onStop callback"; |
| 101 | }; |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 102 | } |
| 103 | }; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 104 | wifi_error legacy_status = legacy_hal_->stop(on_complete_callback_); |
| 105 | if (legacy_status != WIFI_SUCCESS) { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 106 | LOG(ERROR) << "Failed to stop Wifi HAL"; |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 107 | WifiStatus wifi_status = |
| 108 | createWifiStatusFromLegacyError(legacy_status, "Failed to stop HAL"); |
| 109 | for (const auto& callback : event_callbacks_) { |
| 110 | callback->onFailure(wifi_status); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 111 | } |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 112 | hidl_status_cb(wifi_status); |
| 113 | return Void(); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 114 | } |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 115 | hidl_status_cb(createWifiStatus(WifiStatusCode::SUCCESS)); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 116 | return Void(); |
| 117 | } |
| 118 | |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 119 | Return<void> Wifi::getChipIds(getChipIds_cb hidl_status_cb) { |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 120 | std::vector<ChipId> chip_ids; |
| 121 | if (chip_.get()) { |
| 122 | chip_ids.emplace_back(kChipId); |
| 123 | } |
| 124 | hidl_vec<ChipId> hidl_data; |
| 125 | hidl_data.setToExternal(chip_ids.data(), chip_ids.size()); |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 126 | hidl_status_cb(hidl_data); |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 127 | return Void(); |
| 128 | } |
| 129 | |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 130 | Return<void> Wifi::getChip(ChipId chip_id, getChip_cb hidl_status_cb) { |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 131 | if (chip_.get() && chip_id == kChipId) { |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 132 | hidl_status_cb(chip_); |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 133 | } else { |
Roshan Pius | 503582e | 2016-10-11 08:25:30 -0700 | [diff] [blame^] | 134 | hidl_status_cb(nullptr); |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame] | 135 | } |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 136 | return Void(); |
| 137 | } |
| 138 | |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 139 | } // namespace implementation |
| 140 | } // namespace V1_0 |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 141 | } // namespace wifi |
| 142 | } // namespace hardware |
| 143 | } // namespace android |