1 /******************************************************************************
3 * Copyright (C) 2017 The Android Open Source Project
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
23 /** Bluetooth Address */
24 class RawAddress final {
26 static constexpr unsigned int kLength = 6;
28 uint8_t address[kLength];
30 RawAddress() = default;
31 RawAddress(const uint8_t (&addr)[6]);
33 bool operator<(const RawAddress& rhs) const {
34 return (std::memcmp(address, rhs.address, sizeof(address)) < 0);
36 bool operator==(const RawAddress& rhs) const {
37 return (std::memcmp(address, rhs.address, sizeof(address)) == 0);
39 bool operator>(const RawAddress& rhs) const { return (rhs < *this); }
40 bool operator<=(const RawAddress& rhs) const { return !(*this > rhs); }
41 bool operator>=(const RawAddress& rhs) const { return !(*this < rhs); }
42 bool operator!=(const RawAddress& rhs) const { return !(*this == rhs); }
44 bool IsEmpty() const { return *this == kEmpty; }
46 std::string ToString() const;
48 // Converts |string| to RawAddress and places it in |to|. If |from| does
49 // not represent a Bluetooth address, |to| is not modified and this function
50 // returns false. Otherwise, it returns true.
51 static bool FromString(const std::string& from, RawAddress& to);
53 static bool IsValidAddress(const std::string& address);
55 static const RawAddress kEmpty; // 00:00:00:00:00:00
56 static const RawAddress kAny; // FF:FF:FF:FF:FF:FF
59 inline std::ostream& operator<<(std::ostream& os, const RawAddress& a) {