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 <array>
22 #include <optional>
23 #include <string>
24 
25 #include "packet/custom_field_fixed_size_interface.h"
26 #include "storage/serializable.h"
27 
28 namespace bluetooth {
29 namespace hci {
30 
31 class ClassOfDevice final : public packet::CustomFieldFixedSizeInterface<ClassOfDevice>,
32                             public storage::Serializable<ClassOfDevice> {
33  public:
34   static constexpr size_t kLength = 3;
35 
36   std::array<uint8_t, kLength> cod = {};
37 
38   ClassOfDevice() = default;
39   ClassOfDevice(const uint8_t (&class_of_device)[kLength]);
40 
41   // packet::CustomFieldFixedSizeInterface methods
data()42   inline uint8_t* data() override {
43     return cod.data();
44   }
data()45   inline const uint8_t* data() const override {
46     return cod.data();
47   }
48 
49   // storage::Serializable methods
50   std::string ToString() const;
51   static std::optional<ClassOfDevice> FromString(const std::string& str);
52   std::string ToLegacyConfigString() const override;
53   static std::optional<ClassOfDevice> FromLegacyConfigString(const std::string& str);
54 
55   bool operator<(const ClassOfDevice& rhs) const {
56     return cod < rhs.cod;
57   }
58   bool operator==(const ClassOfDevice& rhs) const {
59     return cod == rhs.cod;
60   }
61   bool operator>(const ClassOfDevice& rhs) const {
62     return (rhs < *this);
63   }
64   bool operator<=(const ClassOfDevice& rhs) const {
65     return !(*this > rhs);
66   }
67   bool operator>=(const ClassOfDevice& rhs) const {
68     return !(*this < rhs);
69   }
70   bool operator!=(const ClassOfDevice& rhs) const {
71     return !(*this == rhs);
72   }
73 
74   // Converts |string| to ClassOfDevice and places it in |to|. If |from| does
75   // not represent a Class of Device, |to| is not modified and this function
76   // returns false. Otherwise, it returns true.
77   static bool FromString(const std::string& from, ClassOfDevice& to);
78 
79   // Converts uint32_t encoded class of device to ClassOfDevice object
80   // uint32_t encoding:
81   //     <high> uint8_t(cod[0]) | uint8_t(cod[1]) | uint8_t(cod[2]) <low>
82   // Only used in legacy stack device config
83   static std::optional<ClassOfDevice> FromUint32Legacy(uint32_t cod_int);
84   uint32_t ToUint32Legacy() const;
85 
86   // Copies |from| raw Class of Device octets to the local object.
87   // Returns the number of copied octets (always ClassOfDevice::kLength)
88   size_t FromOctets(const uint8_t* from);
89 
90   static bool IsValid(const std::string& class_of_device);
91 };
92 
93 inline std::ostream& operator<<(std::ostream& os, const ClassOfDevice& c) {
94   os << c.ToString();
95   return os;
96 }
97 
98 }  // namespace hci
99 }  // namespace bluetooth
100