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