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, cod[1] & 0x0f, cod[0]);
41 std::string str(buffer);
42 return str;
43 }
44
ToLegacyConfigString() const45 std::string ClassOfDevice::ToLegacyConfigString() const { return std::to_string(ToUint32Legacy()); }
46
FromString(const std::string & str)47 std::optional<ClassOfDevice> ClassOfDevice::FromString(const std::string& str) {
48 if (str.length() != 8) {
49 return std::nullopt;
50 }
51
52 std::istringstream stream(str);
53 std::string token;
54 int index = 0;
55 uint16_t values[3];
56
57 ClassOfDevice new_cod{};
58 while (getline(stream, token, '-')) {
59 if (index >= 3) {
60 return std::nullopt;
61 }
62
63 if (index == 0 && token.length() != 3) {
64 return std::nullopt;
65 } else if (index == 1 && token.length() != 1) {
66 return std::nullopt;
67 } else if (index == 2 && token.length() != 2) {
68 return std::nullopt;
69 }
70 char* temp = nullptr;
71 values[index] = std::strtol(token.c_str(), &temp, 16);
72 if (*temp != '\0') {
73 return std::nullopt;
74 }
75
76 index++;
77 }
78
79 if (index != 3) {
80 return std::nullopt;
81 }
82
83 new_cod.cod[0] = values[2];
84 new_cod.cod[1] = values[1] | ((values[0] & 0xf) << 4);
85 new_cod.cod[2] = values[0] >> 4;
86
87 return new_cod;
88 }
89
FromString(const std::string & from,ClassOfDevice & to)90 bool ClassOfDevice::FromString(const std::string& from, ClassOfDevice& to) {
91 auto new_cod = FromString(from);
92 if (!new_cod) {
93 to = {};
94 return false;
95 }
96 to = std::move(*new_cod);
97 return true;
98 }
99
FromUint32Legacy(uint32_t cod_int)100 std::optional<ClassOfDevice> ClassOfDevice::FromUint32Legacy(uint32_t cod_int) {
101 if (cod_int != 0 && (cod_int >> 24) != 0) {
102 return std::nullopt;
103 }
104 ClassOfDevice result = {};
105 result.cod[2] = static_cast<uint8_t>(cod_int);
106 result.cod[1] = static_cast<uint8_t>(cod_int >> 8);
107 result.cod[0] = static_cast<uint8_t>(cod_int >> 16);
108 return result;
109 }
110
FromLegacyConfigString(const std::string & str)111 std::optional<ClassOfDevice> ClassOfDevice::FromLegacyConfigString(const std::string& str) {
112 char* ptr = nullptr;
113 auto num = std::strtoull(str.data(), &ptr, 10);
114 if (num > 0xffffff) {
115 return std::nullopt;
116 }
117 return FromUint32Legacy(static_cast<uint32_t>(num));
118 }
119
ToUint32Legacy() const120 uint32_t ClassOfDevice::ToUint32Legacy() const { return (cod[2]) | (cod[1] << 8) | (cod[0] << 16); }
121
FromOctets(const uint8_t * from)122 size_t ClassOfDevice::FromOctets(const uint8_t* from) {
123 std::copy(from, from + kLength, data());
124 return kLength;
125 }
126
IsValid(const std::string & cod)127 bool ClassOfDevice::IsValid(const std::string& cod) {
128 return ClassOfDevice::FromString(cod).has_value();
129 }
130 } // namespace hci
131 } // namespace bluetooth
132