esp-nimble-cpp 2.0.2
Loading...
Searching...
No Matches
NimBLERemoteValueAttribute.h
1/*
2 * Copyright 2020-2024 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_REMOTE_VALUE_ATTRIBUTE_H_
19#define NIMBLE_CPP_REMOTE_VALUE_ATTRIBUTE_H_
20
21#include "nimconfig.h"
22#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
23
24# if defined(CONFIG_NIMBLE_CPP_IDF)
25# include <host/ble_gatt.h>
26# else
27# include <nimble/nimble/host/include/host/ble_gatt.h>
28# endif
29
30/**** FIX COMPILATION ****/
31# undef min
32# undef max
33/**************************/
34
35# include "NimBLEAttribute.h"
36# include "NimBLEAttValue.h"
37
38class NimBLEClient;
39
40class NimBLERemoteValueAttribute : public NimBLEAttribute {
41 public:
47 NimBLEAttValue readValue(time_t* timestamp = nullptr) const;
48
53 size_t getLength() const { return m_value.size(); }
54
60 NimBLEAttValue getValue() const { return m_value; }
61
65 virtual NimBLEClient* getClient() const = 0;
66
74 bool writeValue(const uint8_t* data, size_t length, bool response = false) const;
75
83 bool writeValue(const char* str, size_t length = 0, bool response = false) const {
84 return writeValue(reinterpret_cast<const uint8_t*>(str), length ? length : strlen(str), response);
85 }
86
87# if __cplusplus < 201703L
95 template <typename T>
96# ifdef _DOXYGEN_
97 bool
98# else
99 typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
100# endif
101 writeValue(const T& v, bool response = false) const {
102 return writeValue(reinterpret_cast<const uint8_t*>(&v), sizeof(T), response);
103 }
104
111 template <typename T>
112# ifdef _DOXYGEN_
113 bool
114# else
115 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
116# endif
117 writeValue(const T& s, bool response = false) const {
118 return writeValue(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), response);
119 }
120
127 template <typename T>
128# ifdef _DOXYGEN_
129 bool
130# else
131 typename std::enable_if<Has_data_size<T>::value, bool>::type
132# endif
133 writeValue(const T& v, bool response = false) const {
134 return writeValue(reinterpret_cast<const uint8_t*>(v.data()), v.size(), response);
135 }
136
137# else
144 template <typename T>
145 typename std::enable_if<!std::is_pointer<T>::value, bool>::type writeValue(const T& v, bool response = false) const {
146 if constexpr (Has_data_size<T>::value) {
147 return writeValue(reinterpret_cast<const uint8_t*>(v.data()), v.size(), response);
148 } else if constexpr (Has_c_str_length<T>::value) {
149 return writeValue(reinterpret_cast<const uint8_t*>(v.c_str()), v.length(), response);
150 } else {
151 return writeValue(reinterpret_cast<const uint8_t*>(&v), sizeof(v), response);
152 }
153 }
154# endif
155
165 template <typename T>
166 T getValue(time_t* timestamp = nullptr, bool skipSizeCheck = false) const {
167 return m_value.getValue<T>(timestamp, skipSizeCheck);
168 }
169
179 template <typename T>
180 T readValue(time_t* timestamp = nullptr, bool skipSizeCheck = false) const {
181 readValue();
182 return m_value.getValue<T>(timestamp, skipSizeCheck);
183 }
184
185 protected:
189 NimBLERemoteValueAttribute(const ble_uuid_any_t& uuid, uint16_t handle) : NimBLEAttribute(uuid, handle) {}
190
194 virtual ~NimBLERemoteValueAttribute() = default;
195
196 static int onReadCB(uint16_t conn_handle, const ble_gatt_error* error, ble_gatt_attr* attr, void* arg);
197 static int onWriteCB(uint16_t conn_handle, const ble_gatt_error* error, ble_gatt_attr* attr, void* arg);
198
199 mutable NimBLEAttValue m_value{};
200};
201
202#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
203#endif // NIMBLE_CPP_REMOTE_VALUE_ATTRIBUTE_H_
A specialized container class to hold BLE attribute values.
Definition NimBLEAttValue.h:72
A base class for BLE attributes.
Definition NimBLEAttribute.h:29
A model of a BLE client.
Definition NimBLEClient.h:49