blob: 0036a590acd185760400ec9ae64e4e49a98fb80d [file] [log] [blame]
Myles Watson0ead5972019-04-01 13:21:25 -07001/******************************************************************************
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#pragma once
20
21#include <string>
22
23namespace bluetooth {
24namespace common {
25
26class Address final {
27 public:
28 static constexpr unsigned int kLength = 6;
29
30 uint8_t address[kLength];
31
32 Address() = default;
33 Address(const uint8_t (&addr)[6]);
34
35 bool operator<(const Address& rhs) const {
36 return (std::memcmp(address, rhs.address, sizeof(address)) < 0);
37 }
38 bool operator==(const Address& rhs) const {
39 return (std::memcmp(address, rhs.address, sizeof(address)) == 0);
40 }
41 bool operator>(const Address& rhs) const {
42 return (rhs < *this);
43 }
44 bool operator<=(const Address& rhs) const {
45 return !(*this > rhs);
46 }
47 bool operator>=(const Address& rhs) const {
48 return !(*this < rhs);
49 }
50 bool operator!=(const Address& rhs) const {
51 return !(*this == rhs);
52 }
53
54 bool IsEmpty() const {
55 return *this == kEmpty;
56 }
57
58 std::string ToString() const;
59
60 // Converts |string| to Address and places it in |to|. If |from| does
61 // not represent a Bluetooth address, |to| is not modified and this function
62 // returns false. Otherwise, it returns true.
63 static bool FromString(const std::string& from, Address& to);
64
65 // Copies |from| raw Bluetooth address octets to the local object.
66 // Returns the number of copied octets - should be always Address::kLength
67 size_t FromOctets(const uint8_t* from);
68
69 static bool IsValidAddress(const std::string& address);
70
71 static const Address kEmpty; // 00:00:00:00:00:00
72 static const Address kAny; // FF:FF:FF:FF:FF:FF
73};
74
75inline std::ostream& operator<<(std::ostream& os, const Address& a) {
76 os << a.ToString();
77 return os;
78}
79
80} // namespace common
81} // namespace bluetooth