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