1 /*
2  * Copyright 2021 HIMSA II K/S - www.himsa.com.
3  * Represented by EHIMA - www.ehima.com
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 #include "has_preset.h"
19 
20 #include <bluetooth/log.h>
21 
22 #include <cstddef>
23 #include <cstdint>
24 #include <optional>
25 #include <ostream>
26 #include <vector>
27 
28 #include "stack/include/bt_types.h"
29 
30 using namespace bluetooth;
31 
32 namespace bluetooth::le_audio {
33 namespace has {
34 
FromCharacteristicValue(uint16_t & len,const uint8_t * value)35 std::optional<HasPreset> HasPreset::FromCharacteristicValue(uint16_t& len, const uint8_t* value) {
36   if ((len < kCharValueMinSize) || (len > kCharValueMinSize + kPresetNameLengthLimit)) {
37     log::error("Preset record to long: {}", len);
38     return std::nullopt;
39   }
40 
41   HasPreset preset;
42   STREAM_TO_UINT8(preset.index_, value);
43   --len;
44   STREAM_TO_UINT8(preset.properties_, value);
45   --len;
46   preset.name_ = std::string(value, value + len);
47 
48   return preset;
49 }
50 
ToCharacteristicValue(std::vector<uint8_t> & value) const51 void HasPreset::ToCharacteristicValue(std::vector<uint8_t>& value) const {
52   auto initial_offset = value.size();
53 
54   value.resize(value.size() + kCharValueMinSize + name_.size());
55   auto pp = value.data() + initial_offset;
56 
57   UINT8_TO_STREAM(pp, index_);
58   UINT8_TO_STREAM(pp, properties_);
59   ARRAY_TO_STREAM(pp, name_.c_str(), (int)name_.size());
60 }
61 
Serialize(uint8_t * p_out,size_t buffer_size) const62 uint8_t* HasPreset::Serialize(uint8_t* p_out, size_t buffer_size) const {
63   if (buffer_size < SerializedSize()) {
64     log::error("Invalid output buffer size!");
65     return p_out;
66   }
67 
68   uint8_t name_len = name_.length();
69   if (name_len > kPresetNameLengthLimit) {
70     log::error("Invalid preset name length. Cannot be serialized!");
71     return p_out;
72   }
73 
74   /* Serialized data length */
75   UINT8_TO_STREAM(p_out, name_len + 2);
76 
77   UINT8_TO_STREAM(p_out, index_);
78   UINT8_TO_STREAM(p_out, properties_);
79   ARRAY_TO_STREAM(p_out, name_.c_str(), (int)name_.size());
80   return p_out;
81 }
82 
Deserialize(const uint8_t * p_in,size_t len,HasPreset & preset)83 const uint8_t* HasPreset::Deserialize(const uint8_t* p_in, size_t len, HasPreset& preset) {
84   const uint8_t nonamed_size = HasPreset(0, 0).SerializedSize();
85   auto* p_curr = p_in;
86 
87   if (len < nonamed_size) {
88     log::error("Invalid buffer size {}. Cannot deserialize.", len);
89     return p_in;
90   }
91 
92   uint8_t serialized_data_len;
93   STREAM_TO_UINT8(serialized_data_len, p_curr);
94   if (serialized_data_len < 2) {
95     log::error("Invalid data size. Cannot be deserialized!");
96     return p_in;
97   }
98 
99   auto name_len = serialized_data_len - 2;
100   if ((name_len > kPresetNameLengthLimit) || ((size_t)nonamed_size + name_len > len)) {
101     log::error("Invalid preset name length. Cannot be deserialized!");
102     return p_in;
103   }
104 
105   STREAM_TO_UINT8(preset.index_, p_curr);
106   STREAM_TO_UINT8(preset.properties_, p_curr);
107   if (name_len) {
108     preset.name_ = std::string((const char*)p_curr, name_len);
109   }
110 
111   return p_curr + name_len;
112 }
113 
operator <<(std::ostream & os,const HasPreset & b)114 std::ostream& operator<<(std::ostream& os, const HasPreset& b) {
115   os << "{\"index\": " << +b.GetIndex();
116   os << ", \"name\": \"" << b.GetName() << "\"";
117   os << ", \"is_available\": " << (b.IsAvailable() ? "\"True\"" : "\"False\"");
118   os << ", \"is_writable\": " << (b.IsWritable() ? "\"True\"" : "\"False\"");
119   os << "}";
120   return os;
121 }
122 
123 }  // namespace has
124 }  // namespace bluetooth::le_audio
125