blob: 6cd932df419a1bc243a860c7dca804076bd987b3 [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
67ndk::ScopedAStatus WifiApIface::getValidFrequenciesForBand(WifiBand in_band,
68 std::vector<int32_t>* _aidl_return) {
69 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
70 &WifiApIface::getValidFrequenciesForBandInternal, _aidl_return, in_band);
71}
72
73ndk::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
78ndk::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 instances_.size() > 0 ? instances_[0] : ifname_);
82}
83
84ndk::ScopedAStatus WifiApIface::resetToFactoryMacAddress() {
85 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
86 &WifiApIface::resetToFactoryMacAddressInternal);
87}
88
89ndk::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
94std::pair<std::string, ndk::ScopedAStatus> WifiApIface::getNameInternal() {
95 return {ifname_, ndk::ScopedAStatus::ok()};
96}
97
98ndk::ScopedAStatus WifiApIface::setCountryCodeInternal(const std::array<uint8_t, 2>& code) {
99 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->setCountryCode(
100 instances_.size() > 0 ? instances_[0] : ifname_, code);
101 return createWifiStatusFromLegacyError(legacy_status);
102}
103
104std::pair<std::vector<int32_t>, ndk::ScopedAStatus> WifiApIface::getValidFrequenciesForBandInternal(
105 WifiBand band) {
106 static_assert(sizeof(WifiChannelWidthInMhz) == sizeof(int32_t), "Size mismatch");
107 legacy_hal::wifi_error legacy_status;
108 std::vector<uint32_t> valid_frequencies;
109 std::tie(legacy_status, valid_frequencies) = legacy_hal_.lock()->getValidFrequenciesForBand(
110 instances_.size() > 0 ? instances_[0] : ifname_,
111 aidl_struct_util::convertAidlWifiBandToLegacy(band));
112 return {std::vector<int32_t>(valid_frequencies.begin(), valid_frequencies.end()),
113 createWifiStatusFromLegacyError(legacy_status)};
114}
115
116ndk::ScopedAStatus WifiApIface::setMacAddressInternal(const std::array<uint8_t, 6>& mac) {
117 // Support random MAC up to 2 interfaces
118 if (instances_.size() == 2) {
119 int rbyte = 1;
120 for (auto const& intf : instances_) {
121 std::array<uint8_t, 6> rmac = mac;
122 // reverse the bits to avoid collision
123 rmac[rbyte] = 0xff - rmac[rbyte];
124 if (!iface_util_.lock()->setMacAddress(intf, rmac)) {
125 LOG(INFO) << "Failed to set random mac address on " << intf;
126 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
127 }
128 rbyte++;
129 }
130 }
131 // It also needs to set mac address for bridged interface, otherwise the mac
132 // address of bridged interface will be changed after one of instance
133 // down.
134 if (!iface_util_.lock()->setMacAddress(ifname_, mac)) {
135 LOG(ERROR) << "Fail to config MAC for interface " << ifname_;
136 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
137 }
138 return ndk::ScopedAStatus::ok();
139}
140
141std::pair<std::array<uint8_t, 6>, ndk::ScopedAStatus> WifiApIface::getFactoryMacAddressInternal(
142 const std::string& ifaceName) {
143 std::array<uint8_t, 6> mac = iface_util_.lock()->getFactoryMacAddress(ifaceName);
144 if (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 && mac[4] == 0 && mac[5] == 0) {
145 return {mac, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
146 }
147 return {mac, ndk::ScopedAStatus::ok()};
148}
149
150ndk::ScopedAStatus WifiApIface::resetToFactoryMacAddressInternal() {
151 std::pair<std::array<uint8_t, 6>, ndk::ScopedAStatus> getMacResult;
152 if (instances_.size() == 2) {
153 for (auto const& intf : instances_) {
154 getMacResult = getFactoryMacAddressInternal(intf);
155 LOG(DEBUG) << "Reset MAC to factory MAC on " << intf;
156 if (!getMacResult.second.isOk() ||
157 !iface_util_.lock()->setMacAddress(intf, getMacResult.first)) {
158 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
159 }
160 }
161 // We need to set mac address for bridged interface, otherwise the mac
162 // address of the bridged interface will be changed after one of the
163 // instances goes down. Thus we are generating a random MAC address for
164 // the bridged interface even if we got the request to reset the Factory
165 // MAC. This is because the bridged interface is an internal interface
166 // for the operation of bpf and other networking operations.
167 if (!iface_util_.lock()->setMacAddress(ifname_,
168 iface_util_.lock()->createRandomMacAddress())) {
169 LOG(ERROR) << "Fail to config MAC for bridged interface " << ifname_;
170 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
171 }
172 } else {
173 getMacResult = getFactoryMacAddressInternal(ifname_);
174 LOG(DEBUG) << "Reset MAC to factory MAC on " << ifname_;
175 if (!getMacResult.second.isOk() ||
176 !iface_util_.lock()->setMacAddress(ifname_, getMacResult.first)) {
177 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
178 }
179 }
180 return ndk::ScopedAStatus::ok();
181}
182
183std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiApIface::getBridgedInstancesInternal() {
184 return {instances_, ndk::ScopedAStatus::ok()};
185}
186
187} // namespace wifi
188} // namespace hardware
189} // namespace android
190} // namespace aidl