• 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 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]) {
36   std::copy(addr, addr + kLength, data());
37 }
38 
Address(std::initializer_list<uint8_t> l)39 Address::Address(std::initializer_list<uint8_t> l) {
40   std::copy(l.begin(), std::min(l.begin() + kLength, l.end()), data());
41 }
42 
_ToMaskedColonSepHexString(int bytes_to_mask) const43 std::string Address::_ToMaskedColonSepHexString(int bytes_to_mask) const {
44   std::stringstream ss;
45   int count = 0;
46   for (auto it = address.rbegin(); it != address.rend(); it++) {
47     if (count++ < bytes_to_mask) {
48       ss << "xx";
49     } else {
50       ss << std::nouppercase << std::hex << std::setw(2) << std::setfill('0')
51          << +*it;
52     }
53     if (std::next(it) != address.rend()) {
54       ss << ':';
55     }
56   }
57   return ss.str();
58 }
59 
ToString() const60 std::string Address::ToString() const { return _ToMaskedColonSepHexString(0); }
61 
ToColonSepHexString() const62 std::string Address::ToColonSepHexString() const {
63   return _ToMaskedColonSepHexString(0);
64 }
65 
ToStringForLogging() const66 std::string Address::ToStringForLogging() const {
67   return _ToMaskedColonSepHexString(0);
68 }
69 
ToRedactedStringForLogging() const70 std::string Address::ToRedactedStringForLogging() const {
71   return _ToMaskedColonSepHexString(4);
72 }
73 
ToLegacyConfigString() const74 std::string Address::ToLegacyConfigString() const { return ToString(); }
75 
FromLegacyConfigString(const std::string & str)76 std::optional<Address> Address::FromLegacyConfigString(const std::string& str) {
77   return FromString(str);
78 }
79 
FromString(const std::string & from)80 std::optional<Address> Address::FromString(const std::string& from) {
81   if (from.length() != 17) {
82     return std::nullopt;
83   }
84 
85   Address addr{};
86   std::istringstream stream(from);
87   std::string token;
88   int index = 0;
89   while (getline(stream, token, ':')) {
90     if (index >= 6) {
91       return std::nullopt;
92     }
93 
94     if (token.length() != 2) {
95       return std::nullopt;
96     }
97 
98     char* temp = nullptr;
99     addr.address.at(5 - index) = std::strtol(token.c_str(), &temp, 16);
100     if (temp == token.c_str()) {
101       // string token is empty or has wrong format
102       return std::nullopt;
103     }
104     if (temp != (token.c_str() + token.size())) {
105       // cannot parse whole string
106       return std::nullopt;
107     }
108 
109     index++;
110   }
111 
112   if (index != 6) {
113     return std::nullopt;
114   }
115 
116   return addr;
117 }
118 
FromString(const std::string & from,Address & to)119 bool Address::FromString(const std::string& from, Address& to) {
120   auto addr = FromString(from);
121   if (!addr) {
122     to = {};
123     return false;
124   }
125   to = std::move(*addr);
126   return true;
127 }
128 
FromOctets(const uint8_t * from)129 size_t Address::FromOctets(const uint8_t* from) {
130   std::copy(from, from + kLength, data());
131   return kLength;
132 };
133 
IsValidAddress(const std::string & address)134 bool Address::IsValidAddress(const std::string& address) {
135   return Address::FromString(address).has_value();
136 }
137 
138 }  // namespace hci
139 }  // namespace bluetooth
140