1 /******************************************************************************
2 *
3 * Copyright 2019 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include "hci/address.h"
20
21 #include <algorithm>
22 #include <cstdint>
23 #include <cstdio>
24 #include <iomanip>
25 #include <sstream>
26
27 namespace bluetooth {
28 namespace hci {
29
30 const Address Address::kAny{{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
31 const Address Address::kEmpty{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
32
33 // Address cannot initialize member variables as it is a POD type
34 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Address(const uint8_t (& addr)[6])35 Address::Address(const uint8_t (&addr)[6]) { std::copy(addr, addr + kLength, data()); }
36
Address(std::initializer_list<uint8_t> l)37 Address::Address(std::initializer_list<uint8_t> l) {
38 std::copy(l.begin(), std::min(l.begin() + kLength, l.end()), data());
39 }
40
_ToMaskedColonSepHexString(int bytes_to_mask) const41 std::string Address::_ToMaskedColonSepHexString(int bytes_to_mask) const {
42 std::stringstream ss;
43 int count = 0;
44 for (auto it = address.rbegin(); it != address.rend(); it++) {
45 if (count++ < bytes_to_mask) {
46 ss << "xx";
47 } else {
48 ss << std::nouppercase << std::hex << std::setw(2) << std::setfill('0') << +*it;
49 }
50 if (std::next(it) != address.rend()) {
51 ss << ':';
52 }
53 }
54 return ss.str();
55 }
56
ToString() const57 std::string Address::ToString() const { return _ToMaskedColonSepHexString(0); }
58
ToColonSepHexString() const59 std::string Address::ToColonSepHexString() const { return _ToMaskedColonSepHexString(0); }
60
ToStringForLogging() const61 std::string Address::ToStringForLogging() const { return _ToMaskedColonSepHexString(0); }
62
ToRedactedStringForLogging() const63 std::string Address::ToRedactedStringForLogging() const { return _ToMaskedColonSepHexString(4); }
64
ToLegacyConfigString() const65 std::string Address::ToLegacyConfigString() const { return ToString(); }
66
FromLegacyConfigString(const std::string & str)67 std::optional<Address> Address::FromLegacyConfigString(const std::string& str) {
68 return FromString(str);
69 }
70
FromString(const std::string & from)71 std::optional<Address> Address::FromString(const std::string& from) {
72 if (from.length() != 17) {
73 return std::nullopt;
74 }
75
76 Address addr{};
77 std::istringstream stream(from);
78 std::string token;
79 int index = 0;
80 while (getline(stream, token, ':')) {
81 if (index >= 6) {
82 return std::nullopt;
83 }
84
85 if (token.length() != 2) {
86 return std::nullopt;
87 }
88
89 char* temp = nullptr;
90 addr.address.at(5 - index) = std::strtol(token.c_str(), &temp, 16);
91 if (temp == token.c_str()) {
92 // string token is empty or has wrong format
93 return std::nullopt;
94 }
95 if (temp != (token.c_str() + token.size())) {
96 // cannot parse whole string
97 return std::nullopt;
98 }
99
100 index++;
101 }
102
103 if (index != 6) {
104 return std::nullopt;
105 }
106
107 return addr;
108 }
109
FromString(const std::string & from,Address & to)110 bool Address::FromString(const std::string& from, Address& to) {
111 auto addr = FromString(from);
112 if (!addr) {
113 to = {};
114 return false;
115 }
116 to = std::move(*addr);
117 return true;
118 }
119
FromOctets(const uint8_t * from)120 size_t Address::FromOctets(const uint8_t* from) {
121 std::copy(from, from + kLength, data());
122 return kLength;
123 }
124
IsValidAddress(const std::string & address)125 bool Address::IsValidAddress(const std::string& address) {
126 return Address::FromString(address).has_value();
127 }
128
129 } // namespace hci
130 } // namespace bluetooth
131