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 { |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 27 | namespace V1_6 { |
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 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 31 | WifiApIface::WifiApIface(const std::string& ifname, const std::vector<std::string>& instances, |
| 32 | const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal, |
| 33 | const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util) |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 34 | : ifname_(ifname), |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 35 | instances_(instances), |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 36 | legacy_hal_(legacy_hal), |
| 37 | iface_util_(iface_util), |
Patrik Fimml | 6beae32 | 2019-10-09 17:34:01 +0200 | [diff] [blame] | 38 | is_valid_(true) {} |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 39 | |
| 40 | void WifiApIface::invalidate() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 41 | legacy_hal_.reset(); |
| 42 | is_valid_ = false; |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 45 | bool WifiApIface::isValid() { |
| 46 | return is_valid_; |
| 47 | } |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 48 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 49 | std::string WifiApIface::getName() { |
| 50 | return ifname_; |
| 51 | } |
Roshan Pius | 675609b | 2017-10-31 14:24:58 -0700 | [diff] [blame] | 52 | |
lesl | 669c906 | 2021-01-22 19:37:47 +0800 | [diff] [blame] | 53 | void WifiApIface::removeInstance(std::string instance) { |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 54 | instances_.erase(std::remove(instances_.begin(), instances_.end(), instance), instances_.end()); |
lesl | 669c906 | 2021-01-22 19:37:47 +0800 | [diff] [blame] | 55 | } |
| 56 | |
Roshan Pius | 734fea0 | 2016-10-11 08:30:28 -0700 | [diff] [blame] | 57 | Return<void> WifiApIface::getName(getName_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 58 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 59 | &WifiApIface::getNameInternal, hidl_status_cb); |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Roshan Pius | 734fea0 | 2016-10-11 08:30:28 -0700 | [diff] [blame] | 62 | Return<void> WifiApIface::getType(getType_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 63 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 64 | &WifiApIface::getTypeInternal, hidl_status_cb); |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Roshan Pius | 32fc12e | 2017-01-25 17:44:42 -0800 | [diff] [blame] | 67 | Return<void> WifiApIface::setCountryCode(const hidl_array<int8_t, 2>& code, |
| 68 | setCountryCode_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, |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 70 | &WifiApIface::setCountryCodeInternal, hidl_status_cb, code); |
Roshan Pius | 32fc12e | 2017-01-25 17:44:42 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 73 | Return<void> WifiApIface::getValidFrequenciesForBand(V1_0::WifiBand band, |
| 74 | getValidFrequenciesForBand_cb hidl_status_cb) { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 75 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 76 | &WifiApIface::getValidFrequenciesForBandInternal, hidl_status_cb, band); |
Roshan Pius | 7f4574d | 2017-02-22 09:48:03 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 79 | Return<void> WifiApIface::setMacAddress(const hidl_array<uint8_t, 6>& mac, |
| 80 | setMacAddress_cb hidl_status_cb) { |
| 81 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 82 | &WifiApIface::setMacAddressInternal, hidl_status_cb, mac); |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 85 | Return<void> WifiApIface::getFactoryMacAddress(getFactoryMacAddress_cb hidl_status_cb) { |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 86 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 87 | &WifiApIface::getFactoryMacAddressInternal, hidl_status_cb, |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 88 | instances_.size() > 0 ? instances_[0] : ifname_); |
| 89 | } |
| 90 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 91 | Return<void> WifiApIface::resetToFactoryMacAddress(resetToFactoryMacAddress_cb hidl_status_cb) { |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 92 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 93 | &WifiApIface::resetToFactoryMacAddressInternal, hidl_status_cb); |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 96 | Return<void> WifiApIface::getBridgedInstances(getBridgedInstances_cb hidl_status_cb) { |
lesl | 669c906 | 2021-01-22 19:37:47 +0800 | [diff] [blame] | 97 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 98 | &WifiApIface::getBridgedInstancesInternal, hidl_status_cb); |
lesl | 669c906 | 2021-01-22 19:37:47 +0800 | [diff] [blame] | 99 | } |
| 100 | |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 101 | std::pair<WifiStatus, std::string> WifiApIface::getNameInternal() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 102 | return {createWifiStatus(WifiStatusCode::SUCCESS), ifname_}; |
Roshan Pius | 907d4a2 | 2016-10-27 12:48:12 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | std::pair<WifiStatus, IfaceType> WifiApIface::getTypeInternal() { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 106 | return {createWifiStatus(WifiStatusCode::SUCCESS), IfaceType::AP}; |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 109 | WifiStatus WifiApIface::setCountryCodeInternal(const std::array<int8_t, 2>& code) { |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 110 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->setCountryCode( |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 111 | instances_.size() > 0 ? instances_[0] : ifname_, code); |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 112 | return createWifiStatusFromLegacyError(legacy_status); |
Roshan Pius | 32fc12e | 2017-01-25 17:44:42 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Roshan Pius | 7f4574d | 2017-02-22 09:48:03 -0800 | [diff] [blame] | 115 | std::pair<WifiStatus, std::vector<WifiChannelInMhz>> |
Ahmed ElArabawy | fd809fc | 2019-11-15 18:19:15 -0800 | [diff] [blame] | 116 | WifiApIface::getValidFrequenciesForBandInternal(V1_0::WifiBand band) { |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 117 | static_assert(sizeof(WifiChannelInMhz) == sizeof(uint32_t), "Size mismatch"); |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 118 | legacy_hal::wifi_error legacy_status; |
| 119 | std::vector<uint32_t> valid_frequencies; |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 120 | std::tie(legacy_status, valid_frequencies) = legacy_hal_.lock()->getValidFrequenciesForBand( |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 121 | instances_.size() > 0 ? instances_[0] : ifname_, |
| 122 | hidl_struct_util::convertHidlWifiBandToLegacy(band)); |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 123 | return {createWifiStatusFromLegacyError(legacy_status), valid_frequencies}; |
Roshan Pius | 7f4574d | 2017-02-22 09:48:03 -0800 | [diff] [blame] | 124 | } |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 125 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 126 | WifiStatus WifiApIface::setMacAddressInternal(const std::array<uint8_t, 6>& mac) { |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 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; |
Les Lee | da9f5fe | 2021-06-29 22:48:11 +0800 | [diff] [blame] | 132 | // reverse the bits to avoid collision |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 133 | rmac[rbyte] = 0xff - rmac[rbyte]; |
Les Lee | da9f5fe | 2021-06-29 22:48:11 +0800 | [diff] [blame] | 134 | if (!iface_util_.lock()->setMacAddress(intf, rmac)) { |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 135 | LOG(INFO) << "Failed to set random mac address on " << intf; |
Les Lee | da9f5fe | 2021-06-29 22:48:11 +0800 | [diff] [blame] | 136 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 137 | } |
| 138 | rbyte++; |
| 139 | } |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 140 | } |
Les Lee | da9f5fe | 2021-06-29 22:48:11 +0800 | [diff] [blame] | 141 | // It also needs to set mac address for bridged interface, otherwise the mac |
| 142 | // address of bridged interface will be changed after one of instance |
| 143 | // down. |
| 144 | if (!iface_util_.lock()->setMacAddress(ifname_, mac)) { |
| 145 | LOG(ERROR) << "Fail to config MAC for interface " << ifname_; |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 146 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 147 | } |
| 148 | return createWifiStatus(WifiStatusCode::SUCCESS); |
| 149 | } |
| 150 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 151 | std::pair<WifiStatus, std::array<uint8_t, 6>> WifiApIface::getFactoryMacAddressInternal( |
| 152 | const std::string& ifaceName) { |
| 153 | std::array<uint8_t, 6> mac = iface_util_.lock()->getFactoryMacAddress(ifaceName); |
| 154 | if (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 && mac[4] == 0 && mac[5] == 0) { |
Patrik Fimml | c919f96 | 2019-09-11 14:31:56 +0200 | [diff] [blame] | 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 | } |
Les Lee | da9f5fe | 2021-06-29 22:48:11 +0800 | [diff] [blame] | 171 | // It needs to set mac address for bridged interface, otherwise the mac |
| 172 | // address of the bridged interface will be changed after one of the |
| 173 | // instance down. Thus we are generating a random MAC address for the |
| 174 | // bridged interface even if we got the request to reset the Factory |
| 175 | // MAC. Since the bridged interface is an internal interface for the |
| 176 | // operation of bpf and others networking operation. |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 177 | if (!iface_util_.lock()->setMacAddress(ifname_, |
| 178 | iface_util_.lock()->createRandomMacAddress())) { |
| 179 | LOG(ERROR) << "Fail to config MAC for bridged interface " << ifname_; |
Les Lee | da9f5fe | 2021-06-29 22:48:11 +0800 | [diff] [blame] | 180 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 181 | } |
lesl | 420c4fc | 2020-11-23 19:33:04 +0800 | [diff] [blame] | 182 | } else { |
| 183 | getMacResult = getFactoryMacAddressInternal(ifname_); |
| 184 | LOG(DEBUG) << "Reset MAC to factory MAC on " << ifname_; |
| 185 | if (getMacResult.first.code != WifiStatusCode::SUCCESS || |
| 186 | !iface_util_.lock()->setMacAddress(ifname_, getMacResult.second)) { |
| 187 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 188 | } |
| 189 | } |
| 190 | return createWifiStatus(WifiStatusCode::SUCCESS); |
| 191 | } |
lesl | 669c906 | 2021-01-22 19:37:47 +0800 | [diff] [blame] | 192 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 193 | std::pair<WifiStatus, std::vector<hidl_string>> WifiApIface::getBridgedInstancesInternal() { |
lesl | 669c906 | 2021-01-22 19:37:47 +0800 | [diff] [blame] | 194 | std::vector<hidl_string> instances; |
| 195 | for (const auto& instance_name : instances_) { |
| 196 | instances.push_back(instance_name); |
| 197 | } |
| 198 | return {createWifiStatus(WifiStatusCode::SUCCESS), instances}; |
| 199 | } |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 200 | } // namespace implementation |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 201 | } // namespace V1_6 |
Roshan Pius | 3e2d671 | 2016-10-06 13:16:23 -0700 | [diff] [blame] | 202 | } // namespace wifi |
| 203 | } // namespace hardware |
| 204 | } // namespace android |