blob: 13ba0225d106bd347519859644f13f494fa878c5 [file] [log] [blame]
Roshan Pius99dab382019-02-14 07:57:10 -08001/*
2 * Copyright (C) 2019 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 Piusc4446792019-02-14 13:20:39 -080017#include <cstddef>
18#include <iostream>
19#include <limits>
20#include <random>
21
Roshan Pius99dab382019-02-14 07:57:10 -080022#include <android-base/logging.h>
23#include <android-base/macros.h>
24#include <private/android_filesystem_config.h>
25
Roshan Piusc4446792019-02-14 13:20:39 -080026#undef NAN
Roshan Pius99dab382019-02-14 07:57:10 -080027#include "wifi_iface_util.h"
28
Roshan Piusc4446792019-02-14 13:20:39 -080029namespace {
30// Constants to set the local bit & clear the multicast bit.
31constexpr uint8_t kMacAddressMulticastMask = 0x01;
32constexpr uint8_t kMacAddressLocallyAssignedMask = 0x02;
33} // namespace
34
Roshan Pius99dab382019-02-14 07:57:10 -080035namespace android {
36namespace hardware {
37namespace wifi {
Ahmed ElArabawyf501a982019-07-23 15:02:22 -070038namespace V1_4 {
Roshan Pius99dab382019-02-14 07:57:10 -080039namespace implementation {
40namespace iface_util {
41
Roshan Piusc885df02019-05-21 14:49:05 -070042WifiIfaceUtil::WifiIfaceUtil(
43 const std::weak_ptr<wifi_system::InterfaceTool> iface_tool)
44 : iface_tool_(iface_tool),
45 random_mac_address_(nullptr),
46 event_handlers_map_() {}
Roshan Pius99dab382019-02-14 07:57:10 -080047
48std::array<uint8_t, 6> WifiIfaceUtil::getFactoryMacAddress(
49 const std::string& iface_name) {
Roshan Piusc885df02019-05-21 14:49:05 -070050 return iface_tool_.lock()->GetFactoryMacAddress(iface_name.c_str());
Roshan Pius99dab382019-02-14 07:57:10 -080051}
52
53bool WifiIfaceUtil::setMacAddress(const std::string& iface_name,
54 const std::array<uint8_t, 6>& mac) {
Ahmed ElArabawy3ee519b2020-04-20 16:28:49 +080055#ifndef WIFI_AVOID_IFACE_RESET_MAC_CHANGE
Roshan Piusc885df02019-05-21 14:49:05 -070056 if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), false)) {
Roshan Pius99dab382019-02-14 07:57:10 -080057 LOG(ERROR) << "SetUpState(false) failed.";
58 return false;
59 }
Ahmed ElArabawy3ee519b2020-04-20 16:28:49 +080060#endif
Roshan Piusc885df02019-05-21 14:49:05 -070061 if (!iface_tool_.lock()->SetMacAddress(iface_name.c_str(), mac)) {
Roshan Pius99dab382019-02-14 07:57:10 -080062 LOG(ERROR) << "SetMacAddress failed.";
63 return false;
64 }
Ahmed ElArabawy3ee519b2020-04-20 16:28:49 +080065#ifndef WIFI_AVOID_IFACE_RESET_MAC_CHANGE
Roshan Piusc885df02019-05-21 14:49:05 -070066 if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), true)) {
Roshan Pius99dab382019-02-14 07:57:10 -080067 LOG(ERROR) << "SetUpState(true) failed.";
68 return false;
69 }
Ahmed ElArabawy3ee519b2020-04-20 16:28:49 +080070#endif
Roshan Pius356d5a62019-05-17 15:07:34 -070071 IfaceEventHandlers event_handlers = {};
72 const auto it = event_handlers_map_.find(iface_name);
73 if (it != event_handlers_map_.end()) {
74 event_handlers = it->second;
75 }
76 if (event_handlers.on_state_toggle_off_on != nullptr) {
77 event_handlers.on_state_toggle_off_on(iface_name);
78 }
Roshan Pius99dab382019-02-14 07:57:10 -080079 LOG(DEBUG) << "Successfully SetMacAddress.";
80 return true;
81}
82
Roshan Pius3b6705f2019-02-14 09:43:43 -080083std::array<uint8_t, 6> WifiIfaceUtil::getOrCreateRandomMacAddress() {
84 if (random_mac_address_) {
85 return *random_mac_address_.get();
86 }
87 random_mac_address_ =
88 std::make_unique<std::array<uint8_t, 6>>(createRandomMacAddress());
89 return *random_mac_address_.get();
90}
91
Roshan Pius356d5a62019-05-17 15:07:34 -070092void WifiIfaceUtil::registerIfaceEventHandlers(const std::string& iface_name,
93 IfaceEventHandlers handlers) {
94 event_handlers_map_[iface_name] = handlers;
95}
96
97void WifiIfaceUtil::unregisterIfaceEventHandlers(
98 const std::string& iface_name) {
99 event_handlers_map_.erase(iface_name);
100}
101
Roshan Pius3b6705f2019-02-14 09:43:43 -0800102std::array<uint8_t, 6> WifiIfaceUtil::createRandomMacAddress() {
Roshan Piusc4446792019-02-14 13:20:39 -0800103 std::array<uint8_t, 6> address = {};
104 std::random_device rd;
105 std::default_random_engine engine(rd());
106 std::uniform_int_distribution<uint8_t> dist(
107 std::numeric_limits<uint8_t>::min(),
108 std::numeric_limits<uint8_t>::max());
109 for (size_t i = 0; i < address.size(); i++) {
110 address[i] = dist(engine);
111 }
112 // Set the local bit and clear the multicast bit.
113 address[0] |= kMacAddressLocallyAssignedMask;
114 address[0] &= ~kMacAddressMulticastMask;
115 return address;
Roshan Pius3b6705f2019-02-14 09:43:43 -0800116}
Roshan Pius5ba0a902020-04-14 11:55:42 -0700117
118bool WifiIfaceUtil::setUpState(const std::string& iface_name, bool request_up) {
119 if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), request_up)) {
120 LOG(ERROR) << "SetUpState to " << request_up << " failed";
121 return false;
122 }
123 return true;
124}
Roshan Pius99dab382019-02-14 07:57:10 -0800125} // namespace iface_util
126} // namespace implementation
Ahmed ElArabawyf501a982019-07-23 15:02:22 -0700127} // namespace V1_4
Roshan Pius99dab382019-02-14 07:57:10 -0800128} // namespace wifi
129} // namespace hardware
130} // namespace android