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 | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace wifi { |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 27 | namespace V1_0 { |
| 28 | namespace implementation { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 29 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 30 | Wifi::Wifi() |
| 31 | : legacy_hal_(new WifiLegacyHal()), run_state_(RunState::STOPPED) {} |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 32 | |
| 33 | Return<void> Wifi::registerEventCallback( |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 34 | const sp<IWifiEventCallback>& callback) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 35 | // TODO(b/31632518): remove the callback when the client is destroyed |
| 36 | callbacks_.insert(callback); |
| 37 | return Void(); |
| 38 | } |
| 39 | |
| 40 | Return<bool> Wifi::isStarted() { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 41 | return run_state_ != RunState::STOPPED; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | Return<void> Wifi::start() { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 45 | if (run_state_ == RunState::STARTED) { |
| 46 | for (const auto& callback : callbacks_) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 47 | callback->onStart(); |
| 48 | } |
| 49 | return Void(); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 50 | } else if (run_state_ == RunState::STOPPING) { |
| 51 | for (const auto& callback : callbacks_) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 52 | callback->onStartFailure(CreateFailureReason( |
| 53 | CommandFailureReason::NOT_AVAILABLE, "HAL is stopping")); |
| 54 | } |
| 55 | return Void(); |
| 56 | } |
| 57 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 58 | LOG(INFO) << "Starting HAL"; |
| 59 | wifi_error status = legacy_hal_->start(); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 60 | if (status != WIFI_SUCCESS) { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 61 | LOG(ERROR) << "Failed to start Wifi HAL"; |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 62 | for (auto& callback : callbacks_) { |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 63 | callback->onStartFailure( |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 64 | CreateFailureReasonLegacyError(status, "Failed to start HAL")); |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 65 | } |
| 66 | return Void(); |
| 67 | } |
| 68 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 69 | // Create the chip instance once the HAL is started. |
| 70 | chip_ = new WifiChip(legacy_hal_); |
| 71 | run_state_ = RunState::STARTED; |
| 72 | for (const auto& callback : callbacks_) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 73 | callback->onStart(); |
| 74 | } |
| 75 | return Void(); |
| 76 | } |
| 77 | |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 78 | Return<void> Wifi::stop() { |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 79 | if (run_state_ == RunState::STOPPED) { |
| 80 | for (const auto& callback : callbacks_) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 81 | callback->onStop(); |
| 82 | } |
| 83 | return Void(); |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 84 | } else if (run_state_ == RunState::STOPPING) { |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 85 | return Void(); |
| 86 | } |
| 87 | |
Roshan Pius | aabe575 | 2016-09-29 09:03:59 -0700 | [diff] [blame^] | 88 | LOG(INFO) << "Stopping HAL"; |
| 89 | run_state_ = RunState::STOPPING; |
| 90 | const auto on_complete_callback_ = [&]() { |
| 91 | if (chip_.get()) { |
| 92 | chip_->invalidate(); |
| 93 | } |
| 94 | chip_.clear(); |
| 95 | run_state_ = RunState::STOPPED; |
| 96 | for (const auto& callback : callbacks_) { |
| 97 | callback->onStop(); |
| 98 | } |
| 99 | }; |
| 100 | wifi_error status = legacy_hal_->stop(on_complete_callback_); |
| 101 | if (status != WIFI_SUCCESS) { |
| 102 | LOG(ERROR) << "Failed to stop Wifi HAL"; |
| 103 | for (const auto& callback : callbacks_) { |
| 104 | callback->onFailure( |
| 105 | CreateFailureReasonLegacyError(status, "Failed to stop HAL")); |
| 106 | } |
| 107 | } |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 108 | return Void(); |
| 109 | } |
| 110 | |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 111 | Return<void> Wifi::getChip(getChip_cb cb) { |
| 112 | cb(chip_); |
| 113 | return Void(); |
| 114 | } |
| 115 | |
Roshan Pius | 79a9975 | 2016-10-04 13:03:58 -0700 | [diff] [blame] | 116 | } // namespace implementation |
| 117 | } // namespace V1_0 |
Roshan Pius | 3c4e8a3 | 2016-10-03 14:53:58 -0700 | [diff] [blame] | 118 | } // namespace wifi |
| 119 | } // namespace hardware |
| 120 | } // namespace android |