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 #pragma once 20 21 #include <string> 22 23 namespace bluetooth { 24 namespace hci { 25 26 class ClassOfDevice final { 27 public: 28 static constexpr unsigned int kLength = 3; 29 30 uint8_t cod[kLength]; 31 32 ClassOfDevice() = default; 33 ClassOfDevice(const uint8_t (&class_of_device)[kLength]); 34 35 bool operator==(const ClassOfDevice& rhs) const { 36 return (std::memcmp(cod, rhs.cod, sizeof(cod)) == 0); 37 } 38 39 bool operator!=(const ClassOfDevice& rhs) const { 40 return std::memcmp(cod, rhs.cod, sizeof(cod)) != 0; 41 } 42 43 std::string ToString() const; 44 45 // Converts |string| to ClassOfDevice and places it in |to|. If |from| does 46 // not represent a Class of Device, |to| is not modified and this function 47 // returns false. Otherwise, it returns true. 48 static bool FromString(const std::string& from, ClassOfDevice& to); 49 50 // Copies |from| raw Class of Device octets to the local object. 51 // Returns the number of copied octets (always ClassOfDevice::kLength) 52 size_t FromOctets(const uint8_t* from); 53 54 static bool IsValid(const std::string& class_of_device); 55 }; 56 57 inline std::ostream& operator<<(std::ostream& os, const ClassOfDevice& c) { 58 os << c.ToString(); 59 return os; 60 } 61 62 } // namespace hci 63 } // namespace bluetooth 64