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