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