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