• 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 #pragma once
19 
20 #include <optional>
21 #include <string>
22 
23 #include "bt_types.h"
24 #include "hardware/bt_has.h"
25 
26 namespace le_audio {
27 namespace has {
28 /* Represents preset instance. It stores properties such as preset name,
29  * preset index and if it supports renaming. Also stores all the needed
30  * GATT characteristics and descriptor informations.
31  */
32 class HasPreset {
33  private:
34   mutable std::string name_;
35   mutable uint8_t properties_;
36   uint8_t index_;
37 
38  public:
39   static constexpr size_t kCharValueMinSize = 1 /*index*/ + 1 /*properties*/;
40 
41   static constexpr uint8_t kPropertyWritable = 0x01;
42   static constexpr uint8_t kPropertyAvailable = 0x02;
43 
44   static constexpr uint8_t kPresetNameLengthLimit = 40;
45 
46   HasPreset(uint8_t index, uint8_t props = 0,
47             std::optional<std::string> name = std::nullopt)
properties_(props)48       : properties_(props), index_(index) {
49     name_ = name.value_or("");
50   }
HasPreset()51   HasPreset()
52       : name_(""),
53         properties_(0),
54         index_(bluetooth::has::kHasPresetIndexInvalid) {}
55 
GetName()56   auto& GetName() const { return name_; }
GetIndex()57   decltype(index_) GetIndex() const { return index_; }
GetProperties()58   decltype(properties_) GetProperties() const { return properties_; }
IsWritable()59   bool IsWritable() const { return properties_ & kPropertyWritable; }
IsAvailable()60   bool IsAvailable() const { return properties_ & kPropertyAvailable; }
61 
62   HasPreset& operator=(const HasPreset& other) {
63     LOG_ASSERT(index_ == other.GetIndex())
64         << "Assigning immutable preset index!";
65 
66     if ((this != &other) && (*this != other)) {
67       index_ = other.GetIndex();
68       name_ = other.GetName();
69     }
70     return *this;
71   }
72 
73   bool operator==(const HasPreset& b) const {
74     return (index_ == b.index_) && (properties_ == b.properties_) &&
75            (name_ == b.name_);
76   }
77   bool operator!=(const HasPreset& b) const {
78     return (index_ != b.index_) || (properties_ != b.properties_) ||
79            (name_ != b.name_);
80   }
81   bool operator<(const HasPreset& b) const { return index_ < b.index_; }
82   friend std::ostream& operator<<(std::ostream& os, const HasPreset& b);
83 
84   struct ComparatorDesc {
85     using is_transparent = void;
operatorComparatorDesc86     bool operator()(HasPreset const& a, int index) const {
87       return a.index_ < index;
88     }
operatorComparatorDesc89     bool operator()(int index, HasPreset const& a) const {
90       return index < a.index_;
91     }
operatorComparatorDesc92     bool operator()(HasPreset const& a, HasPreset const& b) const {
93       return a.index_ < b.index_;
94     }
95   };
96 
97   static std::optional<HasPreset> FromCharacteristicValue(uint16_t& len,
98                                                           const uint8_t* value);
99   void ToCharacteristicValue(std::vector<uint8_t>& value) const;
100 
101   /* Calculates buffer space that the preset will use when serialized */
SerializedSize()102   uint8_t SerializedSize() const {
103     return (sizeof(index_) + sizeof(properties_) + 1 /* name length */
104             + name_.length());
105   }
106   /* Serializes into binary blob for the persistent storage */
107   uint8_t* Serialize(uint8_t* p_out, size_t buffer_size) const;
108   /* Deserializes binary blob read from the persistent storage */
109   static const uint8_t* Deserialize(const uint8_t* p_in, size_t len,
110                                     HasPreset& preset);
111 };
112 
113 }  // namespace has
114 }  // namespace le_audio
115