1+ /*
2+ * Copyright 2020-2025 Ryan Powell <[email protected] > 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+
27+ class 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+
32+ /* *
33+ * @brief Get a copy of the value of the attribute value.
34+ * @param [in] timestamp (Optional) A pointer to a time_t struct to get the time the value set.
35+ * @return A copy of the attribute value.
36+ */
37+ NimBLEAttValue getValue (time_t * timestamp) { return m_value.getValue (timestamp); }
38+
39+ /* *
40+ * @brief Get a copy of the value of the attribute value.
41+ * @return A copy of the attribute value.
42+ */
43+ NimBLEAttValue getValue () const { return m_value; }
44+
45+ /* *
46+ * @brief Get the length of the attribute value.
47+ * @return The length of the attribute value.
48+ */
49+ size_t getLength () const { return m_value.size (); }
50+
51+ /* *
52+ * @brief Template to convert the data to <type\>.
53+ * @tparam T The type to convert the data to.
54+ * @param [in] timestamp (Optional) A pointer to a time_t struct to get the time the value set.
55+ * @param [in] skipSizeCheck (Optional) If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>.
56+ * @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is less than <tt>sizeof(<type\>)</tt>.
57+ * @details <b>Use:</b> <tt>getValue<type>(×tamp, skipSizeCheck);</tt>
58+ * Used for types that are trivially copyable and convertible to NimBLEAttValue.
59+ */
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+
66+ /* *
67+ * @brief Template to convert the data to <type\>.
68+ * @tparam T The type to convert the data to.
69+ * @param [in] timestamp (Optional) A pointer to a time_t struct to get the time the value set.
70+ * @param [in] skipSizeCheck (Optional) If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>.
71+ * @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is less than <tt>sizeof(<type\>)</tt>.
72+ * @details <b>Use:</b> <tt>getValue<type>(×tamp, skipSizeCheck);</tt>
73+ * Used for types that are not trivially copyable and convertible to NimBLEAttValue via it's operators.
74+ */
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+ mutable 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_
0 commit comments