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 #include "hci/class_of_device.h"
20 
21 #include <algorithm>
22 #include <cstdint>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <sstream>
26 #include <vector>
27 
28 namespace bluetooth {
29 namespace hci {
30 
31 // ClassOfDevice cannot initialize member variables as it is a POD type
32 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
ClassOfDevice(const uint8_t (& class_of_device)[kLength])33 ClassOfDevice::ClassOfDevice(const uint8_t (&class_of_device)[kLength]) {
34   std::copy(class_of_device, class_of_device + kLength, cod.data());
35 };
36 
ToString() const37 std::string ClassOfDevice::ToString() const {
38   char buffer[] = "000-0-00";
39   std::snprintf(&buffer[0], sizeof(buffer), "%03x-%01x-%02x",
40                 (static_cast<uint16_t>(cod[2]) << 4) | cod[1] >> 4,
41                 cod[1] & 0x0f, cod[0]);
42   std::string str(buffer);
43   return str;
44 }
45 
ToLegacyConfigString() const46 std::string ClassOfDevice::ToLegacyConfigString() const {
47   return std::to_string(ToUint32Legacy());
48 }
49 
FromString(const std::string & str)50 std::optional<ClassOfDevice> ClassOfDevice::FromString(const std::string& str) {
51   if (str.length() != 8) {
52     return std::nullopt;
53   }
54 
55   std::istringstream stream(str);
56   std::string token;
57   int index = 0;
58   uint16_t values[3];
59 
60   ClassOfDevice new_cod{};
61   while (getline(stream, token, '-')) {
62     if (index >= 3) {
63       return std::nullopt;
64     }
65 
66     if (index == 0 && token.length() != 3) {
67       return std::nullopt;
68     } else if (index == 1 && token.length() != 1) {
69       return std::nullopt;
70     } else if (index == 2 && token.length() != 2) {
71       return std::nullopt;
72     }
73     char* temp = nullptr;
74     values[index] = std::strtol(token.c_str(), &temp, 16);
75     if (*temp != '\0') {
76       return std::nullopt;
77     }
78 
79     index++;
80   }
81 
82   if (index != 3) {
83     return std::nullopt;
84   }
85 
86   new_cod.cod[0] = values[2];
87   new_cod.cod[1] = values[1] | ((values[0] & 0xf) << 4);
88   new_cod.cod[2] = values[0] >> 4;
89 
90   return new_cod;
91 }
92 
FromString(const std::string & from,ClassOfDevice & to)93 bool ClassOfDevice::FromString(const std::string& from, ClassOfDevice& to) {
94   auto new_cod = FromString(from);
95   if (!new_cod) {
96     to = {};
97     return false;
98   }
99   to = std::move(*new_cod);
100   return true;
101 }
102 
FromUint32Legacy(uint32_t cod_int)103 std::optional<ClassOfDevice> ClassOfDevice::FromUint32Legacy(uint32_t cod_int) {
104   if (cod_int != 0 && (cod_int >> 24) != 0) {
105     return std::nullopt;
106   }
107   ClassOfDevice result = {};
108   result.cod[2] = static_cast<uint8_t>(cod_int);
109   result.cod[1] = static_cast<uint8_t>(cod_int >> 8);
110   result.cod[0] = static_cast<uint8_t>(cod_int >> 16);
111   return result;
112 }
113 
FromLegacyConfigString(const std::string & str)114 std::optional<ClassOfDevice> ClassOfDevice::FromLegacyConfigString(
115     const std::string& str) {
116   char* ptr = nullptr;
117   auto num = std::strtoull(str.data(), &ptr, 10);
118   if (num > 0xffffff) {
119     return std::nullopt;
120   }
121   return FromUint32Legacy(static_cast<uint32_t>(num));
122 }
123 
ToUint32Legacy() const124 uint32_t ClassOfDevice::ToUint32Legacy() const {
125   return (cod[2]) | (cod[1] << 8) | (cod[0] << 16);
126 }
127 
FromOctets(const uint8_t * from)128 size_t ClassOfDevice::FromOctets(const uint8_t* from) {
129   std::copy(from, from + kLength, data());
130   return kLength;
131 };
132 
IsValid(const std::string & cod)133 bool ClassOfDevice::IsValid(const std::string& cod) {
134   return ClassOfDevice::FromString(cod).has_value();
135 }
136 }  // namespace hci
137 }  // namespace bluetooth
138