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 { return cod.data(); }
data()43   inline const uint8_t* data() const override { return cod.data(); }
44 
45   // storage::Serializable methods
46   std::string ToString() const override;
47   static std::optional<ClassOfDevice> FromString(const std::string& str);
48   std::string ToLegacyConfigString() const override;
49   static std::optional<ClassOfDevice> FromLegacyConfigString(const std::string& str);
50 
51   bool operator<(const ClassOfDevice& rhs) const { return cod < rhs.cod; }
52   bool operator==(const ClassOfDevice& rhs) const { return cod == rhs.cod; }
53   bool operator>(const ClassOfDevice& rhs) const { return rhs < *this; }
54   bool operator<=(const ClassOfDevice& rhs) const { return !(*this > rhs); }
55   bool operator>=(const ClassOfDevice& rhs) const { return !(*this < rhs); }
56   bool operator!=(const ClassOfDevice& rhs) const { return !(*this == rhs); }
57 
58   // Converts |string| to ClassOfDevice and places it in |to|. If |from| does
59   // not represent a Class of Device, |to| is not modified and this function
60   // returns false. Otherwise, it returns true.
61   static bool FromString(const std::string& from, ClassOfDevice& to);
62 
63   // Converts uint32_t encoded class of device to ClassOfDevice object
64   // uint32_t encoding:
65   //     <high> uint8_t(cod[0]) | uint8_t(cod[1]) | uint8_t(cod[2]) <low>
66   // Only used in legacy stack device config
67   static std::optional<ClassOfDevice> FromUint32Legacy(uint32_t cod_int);
68   uint32_t ToUint32Legacy() const;
69 
70   // Copies |from| raw Class of Device octets to the local object.
71   // Returns the number of copied octets (always ClassOfDevice::kLength)
72   size_t FromOctets(const uint8_t* from);
73 
74   static bool IsValid(const std::string& class_of_device);
75 };
76 
77 inline std::ostream& operator<<(std::ostream& os, const ClassOfDevice& c) {
78   os << c.ToString();
79   return os;
80 }
81 
82 }  // namespace hci
83 }  // namespace bluetooth
84