1 // 2 // Copyright 2016 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #pragma once 18 19 #include <cstdint> 20 #include <string> 21 #include <vector> 22 23 namespace test_vendor_lib { 24 25 // Encapsulate handling for Bluetooth Addresses: 26 // - store the address 27 // - convert to/from strings and vectors of bytes 28 // - check strings to see if they represent a valid address 29 class BtAddress { 30 public: 31 // Conversion constants 32 static const size_t kStringLength = 17; // "XX:XX:XX:XX:XX:XX" 33 static const size_t kOctets = 6; // "X0:X1:X2:X3:X4:X5" 34 BtAddress()35 BtAddress() : address_(0){}; 36 virtual ~BtAddress() = default; 37 38 // Returns true if |addr| has the form "XX:XX:XX:XX:XX:XX": 39 // - the length of |addr| is >= kStringLength 40 // - every third character is ':' 41 // - each remaining character is a hexadecimal digit 42 static bool IsValid(const std::string& addr); 43 44 inline bool operator==(const BtAddress& right) { 45 return address_ == right.address_; 46 } 47 inline bool operator!=(const BtAddress& right) { 48 return address_ != right.address_; 49 } 50 inline bool operator<(const BtAddress& right) { 51 return address_ < right.address_; 52 } 53 inline bool operator>(const BtAddress& right) { 54 return address_ > right.address_; 55 } 56 inline bool operator<=(const BtAddress& right) { 57 return address_ <= right.address_; 58 } 59 inline bool operator>=(const BtAddress& right) { 60 return address_ >= right.address_; 61 } 62 63 inline void operator=(const BtAddress& right) { address_ = right.address_; } 64 inline void operator|=(const BtAddress& right) { address_ |= right.address_; } 65 inline void operator&=(const BtAddress& right) { address_ &= right.address_; } 66 67 // Set the address to the address represented by |str|. 68 // returns true if |str| represents a valid address, otherwise false. 69 bool FromString(const std::string& str); 70 71 // Set the address to the address represented by |str|. 72 // returns true if octets.size() >= kOctets, otherwise false. 73 bool FromVector(const std::vector<uint8_t>& octets); 74 75 // Appends the Bluetooth address to the vector |octets|. 76 void ToVector(std::vector<uint8_t>& octets) const; 77 78 // Return a string representation of the Bluetooth address, in this format: 79 // "xx:xx:xx:xx:xx:xx", where x represents a lowercase hexadecimal digit. 80 std::string ToString() const; 81 82 private: 83 // The Bluetooth Address is stored in the lower 48 bits of a 64-bit value 84 uint64_t address_; 85 }; 86 87 } // namespace test_vendor_lib 88