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