• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "common/strings.h"
28 
29 namespace bluetooth {
30 namespace hci {
31 
32 const Address Address::kAny{{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
33 const Address Address::kEmpty{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
34 
35 // Address cannot initialize member variables as it is a POD type
36 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Address(const uint8_t (& addr)[6])37 Address::Address(const uint8_t (&addr)[6]) {
38   std::copy(addr, addr + kLength, data());
39 }
40 
Address(std::initializer_list<uint8_t> l)41 Address::Address(std::initializer_list<uint8_t> l) {
42   std::copy(l.begin(), std::min(l.begin() + kLength, l.end()), data());
43 }
44 
_ToMaskedColonSepHexString(int bytes_to_mask) const45 std::string Address::_ToMaskedColonSepHexString(int bytes_to_mask) const {
46   std::stringstream ss;
47   int count = 0;
48   for (auto it = address.rbegin(); it != address.rend(); it++) {
49     if (count++ < bytes_to_mask) {
50       ss << "xx";
51     } else {
52       ss << std::nouppercase << std::hex << std::setw(2) << std::setfill('0') << +*it;
53     }
54     if (std::next(it) != address.rend()) {
55       ss << ':';
56     }
57   }
58   return ss.str();
59 }
60 
ToString() const61 std::string Address::ToString() const {
62   return _ToMaskedColonSepHexString(0);
63 }
64 
ToColonSepHexString() const65 std::string Address::ToColonSepHexString() const {
66   return _ToMaskedColonSepHexString(0);
67 }
68 
ToStringForLogging() const69 std::string Address::ToStringForLogging() const {
70   return _ToMaskedColonSepHexString(0);
71 }
72 
ToRedactedStringForLogging() const73 std::string Address::ToRedactedStringForLogging() const {
74   return _ToMaskedColonSepHexString(4);
75 }
76 
ToLegacyConfigString() const77 std::string Address::ToLegacyConfigString() const {
78   return ToString();
79 }
80 
FromLegacyConfigString(const std::string & str)81 std::optional<Address> Address::FromLegacyConfigString(const std::string& str) {
82   return FromString(str);
83 }
84 
FromString(const std::string & from)85 std::optional<Address> Address::FromString(const std::string& from) {
86   if (from.length() != 17) {
87     return std::nullopt;
88   }
89 
90   Address addr{};
91   std::istringstream stream(from);
92   std::string token;
93   int index = 0;
94   while (getline(stream, token, ':')) {
95     if (index >= 6) {
96       return std::nullopt;
97     }
98 
99     if (token.length() != 2) {
100       return std::nullopt;
101     }
102 
103     char* temp = nullptr;
104     addr.address.at(5 - index) = std::strtol(token.c_str(), &temp, 16);
105     if (temp == token.c_str()) {
106       // string token is empty or has wrong format
107       return std::nullopt;
108     }
109     if (temp != (token.c_str() + token.size())) {
110       // cannot parse whole string
111       return std::nullopt;
112     }
113 
114     index++;
115   }
116 
117   if (index != 6) {
118     return std::nullopt;
119   }
120 
121   return addr;
122 }
123 
FromString(const std::string & from,Address & to)124 bool Address::FromString(const std::string& from, Address& to) {
125   auto addr = FromString(from);
126   if (!addr) {
127     to = {};
128     return false;
129   }
130   to = std::move(*addr);
131   return true;
132 }
133 
FromOctets(const uint8_t * from)134 size_t Address::FromOctets(const uint8_t* from) {
135   std::copy(from, from + kLength, data());
136   return kLength;
137 };
138 
IsValidAddress(const std::string & address)139 bool Address::IsValidAddress(const std::string& address) {
140   return Address::FromString(address).has_value();
141 }
142 
143 }  // namespace hci
144 }  // namespace bluetooth
145