esp-nimble-cpp 2.3.0
Loading...
Searching...
No Matches
NimBLEValueAttribute.h
1/*
2 * Copyright 2020-2025 Ryan Powell <ryan@nable-embedded.io> and
3 * esp-nimble-cpp, NimBLE-Arduino contributors.
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#ifndef NIMBLE_CPP_VALUE_ATTRIBUTE_H_
19#define NIMBLE_CPP_VALUE_ATTRIBUTE_H_
20
21#include "nimconfig.h"
22#if defined(CONFIG_BT_ENABLED) && (defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) || defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL))
23
24# include "NimBLEAttribute.h"
25# include "NimBLEAttValue.h"
26
27class NimBLEValueAttribute {
28 public:
29 NimBLEValueAttribute(uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN, uint16_t initLen = CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH)
30 : m_value(initLen, maxLen) {}
31
37 NimBLEAttValue getValue(time_t* timestamp) const { return m_value.getValue(timestamp); }
38
43 NimBLEAttValue getValue() const { return m_value; }
44
49 size_t getLength() const { return m_value.size(); }
50
60 template <typename T>
61 typename std::enable_if<std::is_trivially_copyable<T>::value, T>::type
62 getValue(time_t* timestamp = nullptr, bool skipSizeCheck = false) const {
63 return m_value.getValue<T>(timestamp, skipSizeCheck);
64 }
65
75 template <typename T>
76 typename std::enable_if<!std::is_trivially_copyable<T>::value && std::is_convertible<T, NimBLEAttValue>::value, T>::type
77 getValue(time_t* timestamp = nullptr, bool skipSizeCheck = false) const {
78 return m_value;
79 }
80
81 protected:
82 NimBLEAttValue m_value{};
83};
84
85#endif // CONFIG_BT_ENABLED && (CONFIG_BT_NIMBLE_ROLE_PERIPHERAL || CONFIG_BT_NIMBLE_ROLE_CENTRAL)
86#endif // NIMBLE_CPP_ATTRIBUTE_H_
A specialized container class to hold BLE attribute values.
Definition NimBLEAttValue.h:71
uint16_t size() const
Returns the current size of the value in bytes.
Definition NimBLEAttValue.h:158
#define CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH
Uncomment to set the default allocation size (bytes) for each attribute if not specified when the con...
Definition nimconfig.h:68