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 | |
| 21 | #include "failure_reason_util.h" |
| 22 | #include "wifi_chip.h" |
| 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 | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 39 | const sp<IWifiEventCallback>& 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 | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame^] | 41 | callbacks_.emplace_back(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 | |
| 49 | Return<void> Wifi::start() { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 50 | if (run_state_ == RunState::STARTED) { |
| 51 | for (const auto& callback : callbacks_) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 52 | callback->onStart(); |
| 53 | } |
| 54 | return Void(); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 55 | } else if (run_state_ == RunState::STOPPING) { |
| 56 | for (const auto& callback : callbacks_) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 57 | callback->onStartFailure(CreateFailureReason( |
| 58 | CommandFailureReason::NOT_AVAILABLE, "HAL is stopping")); |
| 59 | } |
| 60 | return Void(); |
| 61 | } |
| 62 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 63 | LOG(INFO) << "Starting HAL"; |
| 64 | wifi_error status = legacy_hal_->start(); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 65 | if (status != WIFI_SUCCESS) { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 66 | LOG(ERROR) << "Failed to start Wifi HAL"; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 67 | for (auto& callback : callbacks_) { |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 68 | callback->onStartFailure( |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 69 | CreateFailureReasonLegacyError(status, "Failed to start HAL")); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 70 | } |
| 71 | return Void(); |
| 72 | } |
| 73 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 74 | // Create the chip instance once the HAL is started. |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame^] | 75 | chip_ = new WifiChip(kChipId, legacy_hal_); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 76 | run_state_ = RunState::STARTED; |
| 77 | for (const auto& callback : callbacks_) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 78 | callback->onStart(); |
| 79 | } |
| 80 | return Void(); |
| 81 | } |
| 82 | |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 83 | Return<void> Wifi::stop() { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 84 | if (run_state_ == RunState::STOPPED) { |
| 85 | for (const auto& callback : callbacks_) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 86 | callback->onStop(); |
| 87 | } |
| 88 | return Void(); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 89 | } else if (run_state_ == RunState::STOPPING) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 90 | return Void(); |
| 91 | } |
| 92 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame] | 93 | LOG(INFO) << "Stopping HAL"; |
| 94 | run_state_ = RunState::STOPPING; |
| 95 | const auto on_complete_callback_ = [&]() { |
| 96 | if (chip_.get()) { |
| 97 | chip_->invalidate(); |
| 98 | } |
| 99 | chip_.clear(); |
| 100 | run_state_ = RunState::STOPPED; |
| 101 | for (const auto& callback : callbacks_) { |
| 102 | callback->onStop(); |
| 103 | } |
| 104 | }; |
| 105 | wifi_error status = legacy_hal_->stop(on_complete_callback_); |
| 106 | if (status != WIFI_SUCCESS) { |
| 107 | LOG(ERROR) << "Failed to stop Wifi HAL"; |
| 108 | for (const auto& callback : callbacks_) { |
| 109 | callback->onFailure( |
| 110 | CreateFailureReasonLegacyError(status, "Failed to stop HAL")); |
| 111 | } |
| 112 | } |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 113 | return Void(); |
| 114 | } |
| 115 | |
Roshan Pius | cd566bd | 2016-10-10 08:03:42 -0700 | [diff] [blame^] | 116 | Return<void> Wifi::getChipIds(getChipIds_cb cb) { |
| 117 | std::vector<ChipId> chip_ids; |
| 118 | if (chip_.get()) { |
| 119 | chip_ids.emplace_back(kChipId); |
| 120 | } |
| 121 | hidl_vec<ChipId> hidl_data; |
| 122 | hidl_data.setToExternal(chip_ids.data(), chip_ids.size()); |
| 123 | cb(hidl_data); |
| 124 | return Void(); |
| 125 | } |
| 126 | |
| 127 | Return<void> Wifi::getChip(ChipId chip_id, getChip_cb cb) { |
| 128 | if (chip_.get() && chip_id == kChipId) { |
| 129 | cb(chip_); |
| 130 | } else { |
| 131 | cb(nullptr); |
| 132 | } |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 133 | return Void(); |
| 134 | } |
| 135 | |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 136 | } // namespace implementation |
| 137 | } // namespace V1_0 |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 138 | } // namespace wifi |
| 139 | } // namespace hardware |
| 140 | } // namespace android |