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
ToString() const45 std::string Address::ToString() const {
46 std::stringstream ss;
47 for (auto it = address.rbegin(); it != address.rend(); it++) {
48 ss << std::nouppercase << std::hex << std::setw(2) << std::setfill('0') << +*it;
49 if (std::next(it) != address.rend()) {
50 ss << ':';
51 }
52 }
53 return ss.str();
54 }
55
ToLegacyConfigString() const56 std::string Address::ToLegacyConfigString() const {
57 return ToString();
58 }
59
FromLegacyConfigString(const std::string & str)60 std::optional<Address> Address::FromLegacyConfigString(const std::string& str) {
61 return FromString(str);
62 }
63
FromString(const std::string & from)64 std::optional<Address> Address::FromString(const std::string& from) {
65 if (from.length() != 17) {
66 return std::nullopt;
67 }
68
69 Address addr{};
70 std::istringstream stream(from);
71 std::string token;
72 int index = 0;
73 while (getline(stream, token, ':')) {
74 if (index >= 6) {
75 return std::nullopt;
76 }
77
78 if (token.length() != 2) {
79 return std::nullopt;
80 }
81
82 char* temp = nullptr;
83 addr.address.at(5 - index) = std::strtol(token.c_str(), &temp, 16);
84 if (temp == token.c_str()) {
85 // string token is empty or has wrong format
86 return std::nullopt;
87 }
88 if (temp != (token.c_str() + token.size())) {
89 // cannot parse whole string
90 return std::nullopt;
91 }
92
93 index++;
94 }
95
96 if (index != 6) {
97 return std::nullopt;
98 }
99
100 return addr;
101 }
102
FromString(const std::string & from,Address & to)103 bool Address::FromString(const std::string& from, Address& to) {
104 auto addr = FromString(from);
105 if (!addr) {
106 to = {};
107 return false;
108 }
109 to = std::move(*addr);
110 return true;
111 }
112
FromOctets(const uint8_t * from)113 size_t Address::FromOctets(const uint8_t* from) {
114 std::copy(from, from + kLength, data());
115 return kLength;
116 };
117
IsValidAddress(const std::string & address)118 bool Address::IsValidAddress(const std::string& address) {
119 return Address::FromString(address).has_value();
120 }
121
122 } // namespace hci
123 } // namespace bluetooth
124