xref: /aosp_15_r20/hardware/interfaces/wifi/aidl/default/wifi_ap_iface.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
2  * Copyright (C) 2022 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_ap_iface.h"
18 
19 #include <android-base/logging.h>
20 
21 #include "aidl_return_util.h"
22 #include "aidl_struct_util.h"
23 #include "wifi_status_util.h"
24 
25 namespace aidl {
26 namespace android {
27 namespace hardware {
28 namespace wifi {
29 using aidl_return_util::validateAndCall;
30 
WifiApIface(const std::string & ifname,const bool usesMlo,const std::vector<std::string> & instances,const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)31 WifiApIface::WifiApIface(const std::string& ifname, const bool usesMlo,
32                          const std::vector<std::string>& instances,
33                          const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
34                          const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)
35     : ifname_(ifname),
36       uses_mlo_(usesMlo),
37       instances_(instances),
38       legacy_hal_(legacy_hal),
39       iface_util_(iface_util),
40       is_valid_(true) {}
41 
invalidate()42 void WifiApIface::invalidate() {
43     legacy_hal_.reset();
44     is_valid_ = false;
45 }
46 
isValid()47 bool WifiApIface::isValid() {
48     return is_valid_;
49 }
50 
getName()51 std::string WifiApIface::getName() {
52     return ifname_;
53 }
54 
usesMlo()55 bool WifiApIface::usesMlo() {
56     return uses_mlo_;
57 }
58 
removeInstance(std::string instance)59 void WifiApIface::removeInstance(std::string instance) {
60     instances_.erase(std::remove(instances_.begin(), instances_.end(), instance), instances_.end());
61 }
62 
getName(std::string * _aidl_return)63 ndk::ScopedAStatus WifiApIface::getName(std::string* _aidl_return) {
64     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
65                            &WifiApIface::getNameInternal, _aidl_return);
66 }
67 
setCountryCode(const std::array<uint8_t,2> & in_code)68 ndk::ScopedAStatus WifiApIface::setCountryCode(const std::array<uint8_t, 2>& in_code) {
69     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
70                            &WifiApIface::setCountryCodeInternal, in_code);
71 }
72 
setMacAddress(const std::array<uint8_t,6> & in_mac)73 ndk::ScopedAStatus WifiApIface::setMacAddress(const std::array<uint8_t, 6>& in_mac) {
74     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
75                            &WifiApIface::setMacAddressInternal, in_mac);
76 }
77 
getFactoryMacAddress(std::array<uint8_t,6> * _aidl_return)78 ndk::ScopedAStatus WifiApIface::getFactoryMacAddress(std::array<uint8_t, 6>* _aidl_return) {
79     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
80                            &WifiApIface::getFactoryMacAddressInternal, _aidl_return,
81                            getOperatingInstanceName());
82 }
83 
resetToFactoryMacAddress()84 ndk::ScopedAStatus WifiApIface::resetToFactoryMacAddress() {
85     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
86                            &WifiApIface::resetToFactoryMacAddressInternal);
87 }
88 
getBridgedInstances(std::vector<std::string> * _aidl_return)89 ndk::ScopedAStatus WifiApIface::getBridgedInstances(std::vector<std::string>* _aidl_return) {
90     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
91                            &WifiApIface::getBridgedInstancesInternal, _aidl_return);
92 }
93 
getNameInternal()94 std::pair<std::string, ndk::ScopedAStatus> WifiApIface::getNameInternal() {
95     return {ifname_, ndk::ScopedAStatus::ok()};
96 }
97 
setCountryCodeInternal(const std::array<uint8_t,2> & code)98 ndk::ScopedAStatus WifiApIface::setCountryCodeInternal(const std::array<uint8_t, 2>& code) {
99     legacy_hal::wifi_error legacy_status =
100             legacy_hal_.lock()->setCountryCode(getOperatingInstanceName(), code);
101     return createWifiStatusFromLegacyError(legacy_status);
102 }
103 
setMacAddressInternal(const std::array<uint8_t,6> & mac)104 ndk::ScopedAStatus WifiApIface::setMacAddressInternal(const std::array<uint8_t, 6>& mac) {
105     // Support random MAC up to 2 interfaces
106     if (instances_.size() == 2 && !uses_mlo_) {
107         int rbyte = 1;
108         for (auto const& intf : instances_) {
109             std::array<uint8_t, 6> rmac = mac;
110             // reverse the bits to avoid collision
111             rmac[rbyte] = 0xff - rmac[rbyte];
112             if (!iface_util_.lock()->setMacAddress(intf, rmac)) {
113                 LOG(INFO) << "Failed to set random mac address on " << intf;
114                 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
115             }
116             rbyte++;
117         }
118     }
119     // It also needs to set mac address for bridged interface, otherwise the mac
120     // address of bridged interface will be changed after one of instance
121     // down.
122     if (!iface_util_.lock()->setMacAddress(ifname_, mac)) {
123         LOG(ERROR) << "Fail to config MAC for interface " << ifname_;
124         return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
125     }
126     return ndk::ScopedAStatus::ok();
127 }
128 
getFactoryMacAddressInternal(const std::string & ifaceName)129 std::pair<std::array<uint8_t, 6>, ndk::ScopedAStatus> WifiApIface::getFactoryMacAddressInternal(
130         const std::string& ifaceName) {
131     std::array<uint8_t, 6> mac = iface_util_.lock()->getFactoryMacAddress(ifaceName);
132     if (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 && mac[4] == 0 && mac[5] == 0) {
133         return {mac, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
134     }
135     return {mac, ndk::ScopedAStatus::ok()};
136 }
137 
resetToFactoryMacAddressInternal()138 ndk::ScopedAStatus WifiApIface::resetToFactoryMacAddressInternal() {
139     std::pair<std::array<uint8_t, 6>, ndk::ScopedAStatus> getMacResult;
140     if (instances_.size() == 2 && !uses_mlo_) {
141         for (auto const& intf : instances_) {
142             getMacResult = getFactoryMacAddressInternal(intf);
143             LOG(DEBUG) << "Reset MAC to factory MAC on " << intf;
144             if (!getMacResult.second.isOk() ||
145                 !iface_util_.lock()->setMacAddress(intf, getMacResult.first)) {
146                 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
147             }
148         }
149         // We need to set mac address for bridged interface, otherwise the mac
150         // address of the bridged interface will be changed after one of the
151         // instances goes down. Thus we are generating a random MAC address for
152         // the bridged interface even if we got the request to reset the Factory
153         // MAC. This is because the bridged interface is an internal interface
154         // for the operation of bpf and other networking operations.
155         if (!iface_util_.lock()->setMacAddress(ifname_,
156                                                iface_util_.lock()->createRandomMacAddress())) {
157             LOG(ERROR) << "Fail to config MAC for bridged interface " << ifname_;
158             return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
159         }
160     } else {
161         getMacResult = getFactoryMacAddressInternal(ifname_);
162         LOG(DEBUG) << "Reset MAC to factory MAC on " << ifname_;
163         if (!getMacResult.second.isOk() ||
164             !iface_util_.lock()->setMacAddress(ifname_, getMacResult.first)) {
165             return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
166         }
167     }
168     return ndk::ScopedAStatus::ok();
169 }
170 
getBridgedInstancesInternal()171 std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiApIface::getBridgedInstancesInternal() {
172     return {instances_, ndk::ScopedAStatus::ok()};
173 }
174 
usesMlo(bool * _aidl_return)175 ndk::ScopedAStatus WifiApIface::usesMlo(bool* _aidl_return) {
176     *_aidl_return = uses_mlo_;
177     return ndk::ScopedAStatus::ok();
178 }
179 
180 }  // namespace wifi
181 }  // namespace hardware
182 }  // namespace android
183 }  // namespace aidl
184