• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   char* ptr = nullptr;
119   auto num = std::strtoull(str.data(), &ptr, 10);
120   if (num > 0xffffff) {
121     return std::nullopt;
122   }
123   return FromUint32Legacy(static_cast<uint32_t>(num));
124 }
125 
ToUint32Legacy() const126 uint32_t ClassOfDevice::ToUint32Legacy() const {
127   return (cod[2]) | (cod[1] << 8) | (cod[0] << 16);
128 }
129 
FromOctets(const uint8_t * from)130 size_t ClassOfDevice::FromOctets(const uint8_t* from) {
131   std::copy(from, from + kLength, data());
132   return kLength;
133 };
134 
IsValid(const std::string & cod)135 bool ClassOfDevice::IsValid(const std::string& cod) {
136   return ClassOfDevice::FromString(cod).has_value();
137 }
138 }  // namespace hci
139 }  // namespace bluetooth
140