blob: 3bc507fb3f5fbf18d7ebbe8ee190ae4b13a6b03c [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
Jack Hef2d14602019-11-20 17:24:16 -080021#include <cstring>
Myles Watson0ead5972019-04-01 13:21:25 -070022#include <string>
23
24namespace bluetooth {
Martin Brabham94db40c2019-03-29 10:24:52 -070025namespace hci {
Myles Watson0ead5972019-04-01 13:21:25 -070026
27class Address final {
28 public:
29 static constexpr unsigned int kLength = 6;
30
31 uint8_t address[kLength];
32
33 Address() = default;
34 Address(const uint8_t (&addr)[6]);
35
36 bool operator<(const Address& rhs) const {
37 return (std::memcmp(address, rhs.address, sizeof(address)) < 0);
38 }
39 bool operator==(const Address& rhs) const {
40 return (std::memcmp(address, rhs.address, sizeof(address)) == 0);
41 }
42 bool operator>(const Address& rhs) const {
43 return (rhs < *this);
44 }
45 bool operator<=(const Address& rhs) const {
46 return !(*this > rhs);
47 }
48 bool operator>=(const Address& rhs) const {
49 return !(*this < rhs);
50 }
51 bool operator!=(const Address& rhs) const {
52 return !(*this == rhs);
53 }
54
55 bool IsEmpty() const {
56 return *this == kEmpty;
57 }
58
59 std::string ToString() const;
60
61 // Converts |string| to Address and places it in |to|. If |from| does
62 // not represent a Bluetooth address, |to| is not modified and this function
63 // returns false. Otherwise, it returns true.
64 static bool FromString(const std::string& from, Address& to);
65
66 // Copies |from| raw Bluetooth address octets to the local object.
67 // Returns the number of copied octets - should be always Address::kLength
68 size_t FromOctets(const uint8_t* from);
69
70 static bool IsValidAddress(const std::string& address);
71
72 static const Address kEmpty; // 00:00:00:00:00:00
73 static const Address kAny; // FF:FF:FF:FF:FF:FF
74};
75
76inline std::ostream& operator<<(std::ostream& os, const Address& a) {
77 os << a.ToString();
78 return os;
79}
80
Martin Brabham94db40c2019-03-29 10:24:52 -070081} // namespace hci
Myles Watson0ead5972019-04-01 13:21:25 -070082} // namespace bluetooth
Jack He17727d72019-08-19 17:47:10 -070083
84namespace std {
85template <>
Martin Brabham94db40c2019-03-29 10:24:52 -070086struct hash<bluetooth::hci::Address> {
87 std::size_t operator()(const bluetooth::hci::Address& val) const {
88 static_assert(sizeof(uint64_t) >= bluetooth::hci::Address::kLength);
Jack He17727d72019-08-19 17:47:10 -070089 uint64_t int_addr = 0;
Martin Brabham94db40c2019-03-29 10:24:52 -070090 memcpy(reinterpret_cast<uint8_t*>(&int_addr), val.address, bluetooth::hci::Address::kLength);
Jack He17727d72019-08-19 17:47:10 -070091 return std::hash<uint64_t>{}(int_addr);
92 }
93};
94} // namespace std