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