Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -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 | |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 17 | #include <android-base/logging.h> |
| 18 | |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 19 | #include "hidl_return_util.h" |
Roshan Pius | 7f4574d | 2017-02-22 09:48:03 -0800 | [diff] [blame] | 20 | #include "hidl_struct_util.h" |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 21 | #include "wifi_ap_iface.h" |
Roshan Pius | 734fea0 | 2016-10-11 08:30:28 -0700 | [diff] [blame] | 22 | #include "wifi_status_util.h" |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace wifi { |
Jimmy Chen | d460df3 | 2019-11-29 17:31:22 +0200 | [diff] [blame] | 27 | namespace V1_5 { |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 28 | namespace implementation { |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 29 | using hidl_return_util::validateAndCall; |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 30 | |
Roshan Pius | 6cedc97 | 2016-10-28 10:11:17 -0700 | [diff] [blame] | 31 | WifiApIface::WifiApIface( |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 32 | const std::string& ifname, const std::vector<std::string>& instances, |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 33 | const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal, |
Patrik Fimml | 6beae32 | 2019-10-09 17:34:01 +0200 | [diff] [blame] | 34 | const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util) |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 35 | : ifname_(ifname), |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 36 | instances_(instances), |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 37 | legacy_hal_(legacy_hal), |
| 38 | iface_util_(iface_util), |
Patrik Fimml | 6beae32 | 2019-10-09 17:34:01 +0200 | [diff] [blame] | 39 | is_valid_(true) {} |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 40 | |
| 41 | void WifiApIface::invalidate() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 42 | legacy_hal_.reset(); |
| 43 | is_valid_ = false; |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 46 | bool WifiApIface::isValid() { return is_valid_; } |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 47 | |
Roshan Pius | 675609b | 2017-10-31 14:24:58 -0700 | [diff] [blame] | 48 | std::string WifiApIface::getName() { return ifname_; } |
| 49 | |
Roshan Pius | 734fea0 | 2016-10-11 08:30:28 -0700 | [diff] [blame] | 50 | Return<void> WifiApIface::getName(getName_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 51 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 52 | &WifiApIface::getNameInternal, hidl_status_cb); |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Roshan Pius | 734fea0 | 2016-10-11 08:30:28 -0700 | [diff] [blame] | 55 | Return<void> WifiApIface::getType(getType_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 56 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 57 | &WifiApIface::getTypeInternal, hidl_status_cb); |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Roshan Pius | 32fc12e | 2017-01-25 17:44:42 -0800 | [diff] [blame] | 60 | Return<void> WifiApIface::setCountryCode(const hidl_array<int8_t, 2>& code, |
| 61 | setCountryCode_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 62 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 63 | &WifiApIface::setCountryCodeInternal, hidl_status_cb, |
| 64 | code); |
Roshan Pius | 32fc12e | 2017-01-25 17:44:42 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Roshan Pius | 7f4574d | 2017-02-22 09:48:03 -0800 | [diff] [blame] | 67 | Return<void> WifiApIface::getValidFrequenciesForBand( |
Ahmed ElArabawy | fd809fc | 2019-11-15 18:19:15 -0800 | [diff] [blame] | 68 | V1_0::WifiBand band, getValidFrequenciesForBand_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 69 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 70 | &WifiApIface::getValidFrequenciesForBandInternal, |
| 71 | hidl_status_cb, band); |
Roshan Pius | 7f4574d | 2017-02-22 09:48:03 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 74 | Return<void> WifiApIface::setMacAddress(const hidl_array<uint8_t, 6>& mac, |
| 75 | setMacAddress_cb hidl_status_cb) { |
| 76 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 77 | &WifiApIface::setMacAddressInternal, hidl_status_cb, |
| 78 | mac); |
| 79 | } |
| 80 | |
| 81 | Return<void> WifiApIface::getFactoryMacAddress( |
| 82 | getFactoryMacAddress_cb hidl_status_cb) { |
| 83 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 84 | &WifiApIface::getFactoryMacAddressInternal, |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 85 | hidl_status_cb, |
| 86 | instances_.size() > 0 ? instances_[0] : ifname_); |
| 87 | } |
| 88 | |
| 89 | Return<void> WifiApIface::resetToFactoryMacAddress( |
| 90 | resetToFactoryMacAddress_cb hidl_status_cb) { |
| 91 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 92 | &WifiApIface::resetToFactoryMacAddressInternal, |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 93 | hidl_status_cb); |
| 94 | } |
| 95 | |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 96 | std::pair<WifiStatus, std::string> WifiApIface::getNameInternal() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 97 | return {createWifiStatus(WifiStatusCode::SUCCESS), ifname_}; |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | std::pair<WifiStatus, IfaceType> WifiApIface::getTypeInternal() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 101 | return {createWifiStatus(WifiStatusCode::SUCCESS), IfaceType::AP}; |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Roshan Pius | 32fc12e | 2017-01-25 17:44:42 -0800 | [diff] [blame] | 104 | WifiStatus WifiApIface::setCountryCodeInternal( |
| 105 | const std::array<int8_t, 2>& code) { |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 106 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->setCountryCode( |
| 107 | instances_.size() > 0 ? instances_[0] : ifname_, code); |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 108 | return createWifiStatusFromLegacyError(legacy_status); |
Roshan Pius | 32fc12e | 2017-01-25 17:44:42 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Roshan Pius | 7f4574d | 2017-02-22 09:48:03 -0800 | [diff] [blame] | 111 | std::pair<WifiStatus, std::vector<WifiChannelInMhz>> |
Ahmed ElArabawy | fd809fc | 2019-11-15 18:19:15 -0800 | [diff] [blame] | 112 | WifiApIface::getValidFrequenciesForBandInternal(V1_0::WifiBand band) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 113 | static_assert(sizeof(WifiChannelInMhz) == sizeof(uint32_t), |
| 114 | "Size mismatch"); |
| 115 | legacy_hal::wifi_error legacy_status; |
| 116 | std::vector<uint32_t> valid_frequencies; |
| 117 | std::tie(legacy_status, valid_frequencies) = |
| 118 | legacy_hal_.lock()->getValidFrequenciesForBand( |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 119 | instances_.size() > 0 ? instances_[0] : ifname_, |
| 120 | hidl_struct_util::convertHidlWifiBandToLegacy(band)); |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 121 | return {createWifiStatusFromLegacyError(legacy_status), valid_frequencies}; |
Roshan Pius | 7f4574d | 2017-02-22 09:48:03 -0800 | [diff] [blame] | 122 | } |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 123 | |
| 124 | WifiStatus WifiApIface::setMacAddressInternal( |
| 125 | const std::array<uint8_t, 6>& mac) { |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 126 | bool status; |
| 127 | // Support random MAC up to 2 interfaces |
| 128 | if (instances_.size() == 2) { |
| 129 | int rbyte = 1; |
| 130 | for (auto const& intf : instances_) { |
| 131 | std::array<uint8_t, 6> rmac = mac; |
| 132 | // reverse the bits to avoid clision |
| 133 | rmac[rbyte] = 0xff - rmac[rbyte]; |
| 134 | status = iface_util_.lock()->setMacAddress(intf, rmac); |
| 135 | if (!status) { |
| 136 | LOG(INFO) << "Failed to set random mac address on " << intf; |
| 137 | } |
| 138 | rbyte++; |
| 139 | } |
| 140 | } else { |
| 141 | status = iface_util_.lock()->setMacAddress(ifname_, mac); |
| 142 | } |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 143 | if (!status) { |
| 144 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 145 | } |
| 146 | return createWifiStatus(WifiStatusCode::SUCCESS); |
| 147 | } |
| 148 | |
| 149 | std::pair<WifiStatus, std::array<uint8_t, 6>> |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 150 | WifiApIface::getFactoryMacAddressInternal(const std::string& ifaceName) { |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 151 | std::array<uint8_t, 6> mac = |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 152 | iface_util_.lock()->getFactoryMacAddress(ifaceName); |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 153 | if (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 && |
| 154 | mac[4] == 0 && mac[5] == 0) { |
| 155 | return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), mac}; |
| 156 | } |
| 157 | return {createWifiStatus(WifiStatusCode::SUCCESS), mac}; |
| 158 | } |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 159 | |
| 160 | WifiStatus WifiApIface::resetToFactoryMacAddressInternal() { |
| 161 | std::pair<WifiStatus, std::array<uint8_t, 6>> getMacResult; |
| 162 | if (instances_.size() == 2) { |
| 163 | for (auto const& intf : instances_) { |
| 164 | getMacResult = getFactoryMacAddressInternal(intf); |
| 165 | LOG(DEBUG) << "Reset MAC to factory MAC on " << intf; |
| 166 | if (getMacResult.first.code != WifiStatusCode::SUCCESS || |
| 167 | !iface_util_.lock()->setMacAddress(intf, getMacResult.second)) { |
| 168 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 169 | } |
| 170 | } |
| 171 | } else { |
| 172 | getMacResult = getFactoryMacAddressInternal(ifname_); |
| 173 | LOG(DEBUG) << "Reset MAC to factory MAC on " << ifname_; |
| 174 | if (getMacResult.first.code != WifiStatusCode::SUCCESS || |
| 175 | !iface_util_.lock()->setMacAddress(ifname_, getMacResult.second)) { |
| 176 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 177 | } |
| 178 | } |
| 179 | return createWifiStatus(WifiStatusCode::SUCCESS); |
| 180 | } |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 181 | } // namespace implementation |
Jimmy Chen | d460df3 | 2019-11-29 17:31:22 +0200 | [diff] [blame] | 182 | } // namespace V1_5 |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 183 | } // namespace wifi |
| 184 | } // namespace hardware |
| 185 | } // namespace android |