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