blob: b2957db13f34776d29d0d56bd316889ba2b52c15 [file] [log] [blame]
Roshan Pius3e2d6712016-10-06 13:16:23 -07001/*
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 Pius3e2d6712016-10-06 13:16:23 -070017#include <android-base/logging.h>
18
Roshan Pius907d4a22016-10-27 12:48:12 -070019#include "hidl_return_util.h"
Roshan Pius7f4574d2017-02-22 09:48:03 -080020#include "hidl_struct_util.h"
Roshan Pius907d4a22016-10-27 12:48:12 -070021#include "wifi_ap_iface.h"
Roshan Pius734fea02016-10-11 08:30:28 -070022#include "wifi_status_util.h"
Roshan Pius3e2d6712016-10-06 13:16:23 -070023
24namespace android {
25namespace hardware {
26namespace wifi {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080027namespace V1_6 {
Roshan Pius3e2d6712016-10-06 13:16:23 -070028namespace implementation {
Roshan Pius907d4a22016-10-27 12:48:12 -070029using hidl_return_util::validateAndCall;
Roshan Pius3e2d6712016-10-06 13:16:23 -070030
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080031WifiApIface::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 Pius99dab382019-02-14 07:57:10 -080034 : ifname_(ifname),
lesl420c4fc2020-11-23 19:33:04 +080035 instances_(instances),
Roshan Pius99dab382019-02-14 07:57:10 -080036 legacy_hal_(legacy_hal),
37 iface_util_(iface_util),
Patrik Fimml6beae322019-10-09 17:34:01 +020038 is_valid_(true) {}
Roshan Pius3e2d6712016-10-06 13:16:23 -070039
40void WifiApIface::invalidate() {
Roshan Piusabcf78f2017-10-06 16:30:38 -070041 legacy_hal_.reset();
42 is_valid_ = false;
Roshan Pius3e2d6712016-10-06 13:16:23 -070043}
44
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080045bool WifiApIface::isValid() {
46 return is_valid_;
47}
Roshan Pius907d4a22016-10-27 12:48:12 -070048
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080049std::string WifiApIface::getName() {
50 return ifname_;
51}
Roshan Pius675609b2017-10-31 14:24:58 -070052
lesl669c9062021-01-22 19:37:47 +080053void WifiApIface::removeInstance(std::string instance) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080054 instances_.erase(std::remove(instances_.begin(), instances_.end(), instance), instances_.end());
lesl669c9062021-01-22 19:37:47 +080055}
56
Roshan Pius734fea02016-10-11 08:30:28 -070057Return<void> WifiApIface::getName(getName_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070058 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
59 &WifiApIface::getNameInternal, hidl_status_cb);
Roshan Pius3e2d6712016-10-06 13:16:23 -070060}
61
Roshan Pius734fea02016-10-11 08:30:28 -070062Return<void> WifiApIface::getType(getType_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070063 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
64 &WifiApIface::getTypeInternal, hidl_status_cb);
Roshan Pius907d4a22016-10-27 12:48:12 -070065}
66
Roshan Pius32fc12e2017-01-25 17:44:42 -080067Return<void> WifiApIface::setCountryCode(const hidl_array<int8_t, 2>& code,
68 setCountryCode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070069 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080070 &WifiApIface::setCountryCodeInternal, hidl_status_cb, code);
Roshan Pius32fc12e2017-01-25 17:44:42 -080071}
72
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080073Return<void> WifiApIface::getValidFrequenciesForBand(V1_0::WifiBand band,
74 getValidFrequenciesForBand_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070075 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080076 &WifiApIface::getValidFrequenciesForBandInternal, hidl_status_cb, band);
Roshan Pius7f4574d2017-02-22 09:48:03 -080077}
78
Patrik Fimmlc919f962019-09-11 14:31:56 +020079Return<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 ElArabawy687ce132022-01-11 16:42:48 -080082 &WifiApIface::setMacAddressInternal, hidl_status_cb, mac);
Patrik Fimmlc919f962019-09-11 14:31:56 +020083}
84
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080085Return<void> WifiApIface::getFactoryMacAddress(getFactoryMacAddress_cb hidl_status_cb) {
Patrik Fimmlc919f962019-09-11 14:31:56 +020086 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080087 &WifiApIface::getFactoryMacAddressInternal, hidl_status_cb,
lesl420c4fc2020-11-23 19:33:04 +080088 instances_.size() > 0 ? instances_[0] : ifname_);
89}
90
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080091Return<void> WifiApIface::resetToFactoryMacAddress(resetToFactoryMacAddress_cb hidl_status_cb) {
lesl420c4fc2020-11-23 19:33:04 +080092 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080093 &WifiApIface::resetToFactoryMacAddressInternal, hidl_status_cb);
Patrik Fimmlc919f962019-09-11 14:31:56 +020094}
95
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080096Return<void> WifiApIface::getBridgedInstances(getBridgedInstances_cb hidl_status_cb) {
lesl669c9062021-01-22 19:37:47 +080097 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080098 &WifiApIface::getBridgedInstancesInternal, hidl_status_cb);
lesl669c9062021-01-22 19:37:47 +080099}
100
Roshan Pius907d4a22016-10-27 12:48:12 -0700101std::pair<WifiStatus, std::string> WifiApIface::getNameInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700102 return {createWifiStatus(WifiStatusCode::SUCCESS), ifname_};
Roshan Pius907d4a22016-10-27 12:48:12 -0700103}
104
105std::pair<WifiStatus, IfaceType> WifiApIface::getTypeInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700106 return {createWifiStatus(WifiStatusCode::SUCCESS), IfaceType::AP};
Roshan Pius3e2d6712016-10-06 13:16:23 -0700107}
108
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800109WifiStatus WifiApIface::setCountryCodeInternal(const std::array<int8_t, 2>& code) {
lesl420c4fc2020-11-23 19:33:04 +0800110 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->setCountryCode(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800111 instances_.size() > 0 ? instances_[0] : ifname_, code);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700112 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius32fc12e2017-01-25 17:44:42 -0800113}
114
Roshan Pius7f4574d2017-02-22 09:48:03 -0800115std::pair<WifiStatus, std::vector<WifiChannelInMhz>>
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800116WifiApIface::getValidFrequenciesForBandInternal(V1_0::WifiBand band) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800117 static_assert(sizeof(WifiChannelInMhz) == sizeof(uint32_t), "Size mismatch");
Roshan Piusabcf78f2017-10-06 16:30:38 -0700118 legacy_hal::wifi_error legacy_status;
119 std::vector<uint32_t> valid_frequencies;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800120 std::tie(legacy_status, valid_frequencies) = legacy_hal_.lock()->getValidFrequenciesForBand(
lesl420c4fc2020-11-23 19:33:04 +0800121 instances_.size() > 0 ? instances_[0] : ifname_,
122 hidl_struct_util::convertHidlWifiBandToLegacy(band));
Roshan Piusabcf78f2017-10-06 16:30:38 -0700123 return {createWifiStatusFromLegacyError(legacy_status), valid_frequencies};
Roshan Pius7f4574d2017-02-22 09:48:03 -0800124}
Patrik Fimmlc919f962019-09-11 14:31:56 +0200125
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800126WifiStatus WifiApIface::setMacAddressInternal(const std::array<uint8_t, 6>& mac) {
lesl420c4fc2020-11-23 19:33:04 +0800127 // 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 Leeda9f5fe2021-06-29 22:48:11 +0800132 // reverse the bits to avoid collision
lesl420c4fc2020-11-23 19:33:04 +0800133 rmac[rbyte] = 0xff - rmac[rbyte];
Les Leeda9f5fe2021-06-29 22:48:11 +0800134 if (!iface_util_.lock()->setMacAddress(intf, rmac)) {
lesl420c4fc2020-11-23 19:33:04 +0800135 LOG(INFO) << "Failed to set random mac address on " << intf;
Les Leeda9f5fe2021-06-29 22:48:11 +0800136 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
lesl420c4fc2020-11-23 19:33:04 +0800137 }
138 rbyte++;
139 }
lesl420c4fc2020-11-23 19:33:04 +0800140 }
Les Leeda9f5fe2021-06-29 22:48:11 +0800141 // 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 Fimmlc919f962019-09-11 14:31:56 +0200146 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
147 }
148 return createWifiStatus(WifiStatusCode::SUCCESS);
149}
150
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800151std::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 Fimmlc919f962019-09-11 14:31:56 +0200155 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), mac};
156 }
157 return {createWifiStatus(WifiStatusCode::SUCCESS), mac};
158}
lesl420c4fc2020-11-23 19:33:04 +0800159
160WifiStatus 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 Leeda9f5fe2021-06-29 22:48:11 +0800171 // 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 ElArabawy687ce132022-01-11 16:42:48 -0800177 if (!iface_util_.lock()->setMacAddress(ifname_,
178 iface_util_.lock()->createRandomMacAddress())) {
179 LOG(ERROR) << "Fail to config MAC for bridged interface " << ifname_;
Les Leeda9f5fe2021-06-29 22:48:11 +0800180 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
181 }
lesl420c4fc2020-11-23 19:33:04 +0800182 } 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}
lesl669c9062021-01-22 19:37:47 +0800192
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800193std::pair<WifiStatus, std::vector<hidl_string>> WifiApIface::getBridgedInstancesInternal() {
lesl669c9062021-01-22 19:37:47 +0800194 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 Pius3e2d6712016-10-06 13:16:23 -0700200} // namespace implementation
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800201} // namespace V1_6
Roshan Pius3e2d6712016-10-06 13:16:23 -0700202} // namespace wifi
203} // namespace hardware
204} // namespace android