Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 1 | /* |
| 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 Pius | c444679 | 2019-02-14 13:20:39 -0800 | [diff] [blame] | 17 | #include <cstddef> |
| 18 | #include <iostream> |
| 19 | #include <limits> |
| 20 | #include <random> |
| 21 | |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/macros.h> |
| 24 | #include <private/android_filesystem_config.h> |
| 25 | |
Roshan Pius | c444679 | 2019-02-14 13:20:39 -0800 | [diff] [blame] | 26 | #undef NAN |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 27 | #include "wifi_iface_util.h" |
| 28 | |
Roshan Pius | c444679 | 2019-02-14 13:20:39 -0800 | [diff] [blame] | 29 | namespace { |
| 30 | // Constants to set the local bit & clear the multicast bit. |
| 31 | constexpr uint8_t kMacAddressMulticastMask = 0x01; |
| 32 | constexpr uint8_t kMacAddressLocallyAssignedMask = 0x02; |
| 33 | } // namespace |
| 34 | |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace hardware { |
| 37 | namespace wifi { |
| 38 | namespace V1_3 { |
| 39 | namespace implementation { |
| 40 | namespace iface_util { |
| 41 | |
Roshan Pius | 356d5a6 | 2019-05-17 15:07:34 -0700 | [diff] [blame^] | 42 | WifiIfaceUtil::WifiIfaceUtil() |
| 43 | : random_mac_address_(nullptr), event_handlers_map_() {} |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 44 | |
| 45 | std::array<uint8_t, 6> WifiIfaceUtil::getFactoryMacAddress( |
| 46 | const std::string& iface_name) { |
| 47 | return iface_tool_.GetFactoryMacAddress(iface_name.c_str()); |
| 48 | } |
| 49 | |
| 50 | bool WifiIfaceUtil::setMacAddress(const std::string& iface_name, |
| 51 | const std::array<uint8_t, 6>& mac) { |
| 52 | if (!iface_tool_.SetUpState(iface_name.c_str(), false)) { |
| 53 | LOG(ERROR) << "SetUpState(false) failed."; |
| 54 | return false; |
| 55 | } |
| 56 | if (!iface_tool_.SetMacAddress(iface_name.c_str(), mac)) { |
| 57 | LOG(ERROR) << "SetMacAddress failed."; |
| 58 | return false; |
| 59 | } |
| 60 | if (!iface_tool_.SetUpState(iface_name.c_str(), true)) { |
| 61 | LOG(ERROR) << "SetUpState(true) failed."; |
| 62 | return false; |
| 63 | } |
Roshan Pius | 356d5a6 | 2019-05-17 15:07:34 -0700 | [diff] [blame^] | 64 | IfaceEventHandlers event_handlers = {}; |
| 65 | const auto it = event_handlers_map_.find(iface_name); |
| 66 | if (it != event_handlers_map_.end()) { |
| 67 | event_handlers = it->second; |
| 68 | } |
| 69 | if (event_handlers.on_state_toggle_off_on != nullptr) { |
| 70 | event_handlers.on_state_toggle_off_on(iface_name); |
| 71 | } |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 72 | LOG(DEBUG) << "Successfully SetMacAddress."; |
| 73 | return true; |
| 74 | } |
| 75 | |
Roshan Pius | 3b6705f | 2019-02-14 09:43:43 -0800 | [diff] [blame] | 76 | std::array<uint8_t, 6> WifiIfaceUtil::getOrCreateRandomMacAddress() { |
| 77 | if (random_mac_address_) { |
| 78 | return *random_mac_address_.get(); |
| 79 | } |
| 80 | random_mac_address_ = |
| 81 | std::make_unique<std::array<uint8_t, 6>>(createRandomMacAddress()); |
| 82 | return *random_mac_address_.get(); |
| 83 | } |
| 84 | |
Roshan Pius | 356d5a6 | 2019-05-17 15:07:34 -0700 | [diff] [blame^] | 85 | void WifiIfaceUtil::registerIfaceEventHandlers(const std::string& iface_name, |
| 86 | IfaceEventHandlers handlers) { |
| 87 | event_handlers_map_[iface_name] = handlers; |
| 88 | } |
| 89 | |
| 90 | void WifiIfaceUtil::unregisterIfaceEventHandlers( |
| 91 | const std::string& iface_name) { |
| 92 | event_handlers_map_.erase(iface_name); |
| 93 | } |
| 94 | |
Roshan Pius | 3b6705f | 2019-02-14 09:43:43 -0800 | [diff] [blame] | 95 | std::array<uint8_t, 6> WifiIfaceUtil::createRandomMacAddress() { |
Roshan Pius | c444679 | 2019-02-14 13:20:39 -0800 | [diff] [blame] | 96 | std::array<uint8_t, 6> address = {}; |
| 97 | std::random_device rd; |
| 98 | std::default_random_engine engine(rd()); |
| 99 | std::uniform_int_distribution<uint8_t> dist( |
| 100 | std::numeric_limits<uint8_t>::min(), |
| 101 | std::numeric_limits<uint8_t>::max()); |
| 102 | for (size_t i = 0; i < address.size(); i++) { |
| 103 | address[i] = dist(engine); |
| 104 | } |
| 105 | // Set the local bit and clear the multicast bit. |
| 106 | address[0] |= kMacAddressLocallyAssignedMask; |
| 107 | address[0] &= ~kMacAddressMulticastMask; |
| 108 | return address; |
Roshan Pius | 3b6705f | 2019-02-14 09:43:43 -0800 | [diff] [blame] | 109 | } |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 110 | } // namespace iface_util |
| 111 | } // namespace implementation |
| 112 | } // namespace V1_3 |
| 113 | } // namespace wifi |
| 114 | } // namespace hardware |
| 115 | } // namespace android |