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