NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
NimBLELocalValueAttribute.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_LOCAL_VALUE_ATTRIBUTE_H_
19#define NIMBLE_LOCAL_VALUE_ATTRIBUTE_H_
20
21#include "nimconfig.h"
22#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
23
24# if defined(CONFIG_NIMBLE_CPP_IDF)
25# include "host/ble_hs.h"
26# else
27# include "nimble/nimble/host/include/host/ble_hs.h"
28# endif
29
30/**** FIX COMPILATION ****/
31# undef min
32# undef max
33/**************************/
34
35typedef enum {
36 READ = BLE_GATT_CHR_F_READ,
37 READ_ENC = BLE_GATT_CHR_F_READ_ENC,
38 READ_AUTHEN = BLE_GATT_CHR_F_READ_AUTHEN,
39 READ_AUTHOR = BLE_GATT_CHR_F_READ_AUTHOR,
40 WRITE = BLE_GATT_CHR_F_WRITE,
41 WRITE_NR = BLE_GATT_CHR_F_WRITE_NO_RSP,
42 WRITE_ENC = BLE_GATT_CHR_F_WRITE_ENC,
43 WRITE_AUTHEN = BLE_GATT_CHR_F_WRITE_AUTHEN,
44 WRITE_AUTHOR = BLE_GATT_CHR_F_WRITE_AUTHOR,
45 BROADCAST = BLE_GATT_CHR_F_BROADCAST,
46 NOTIFY = BLE_GATT_CHR_F_NOTIFY,
47 INDICATE = BLE_GATT_CHR_F_INDICATE
48} NIMBLE_PROPERTY;
49
50# include "NimBLELocalAttribute.h"
51# include "NimBLEAttValue.h"
52# include <vector>
53class NimBLEConnInfo;
54
55class NimBLELocalValueAttribute : public NimBLELocalAttribute {
56 public:
60 uint16_t getProperties() const { return m_properties; }
61
66 size_t getLength() const { return m_value.size(); }
67
73 NimBLEAttValue getValue(time_t* timestamp = nullptr) const { return m_value; }
74
80 void setValue(const uint8_t* data, size_t size) { m_value.setValue(data, size); }
81
86 void setValue(const char* str) { m_value.setValue(str); }
87
92 void setValue(const std::vector<uint8_t>& vec) { m_value.setValue(vec); }
93
98 template <typename T>
99 void setValue(const T& val) {
100 m_value.setValue<T>(val);
101 }
102
111 template <typename T>
112 T getValue(time_t* timestamp = nullptr, bool skipSizeCheck = false) const {
113 return m_value.getValue<T>(timestamp, skipSizeCheck);
114 }
115
116 protected:
117 friend class NimBLEServer;
118
126 NimBLELocalValueAttribute(const NimBLEUUID& uuid,
127 uint16_t handle,
128 uint16_t maxLen,
129 uint16_t initLen = CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH)
130 : NimBLELocalAttribute(uuid, handle), m_value(initLen, maxLen) {}
131
135 virtual ~NimBLELocalValueAttribute() = default;
136
142 virtual void readEvent(NimBLEConnInfo& connInfo) = 0;
143
151 virtual void writeEvent(const uint8_t* val, uint16_t len, NimBLEConnInfo& connInfo) = 0;
152
158 const NimBLEAttValue& getAttVal() const { return m_value; }
159
164 void setProperties(uint16_t properties) { m_properties = properties; }
165
166 NimBLEAttValue m_value{};
167 uint16_t m_properties{0};
168};
169
170#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
171#endif // NIMBLE_LOCAL_VALUE_ATTRIBUTE_H_
A specialized container class to hold BLE attribute values.
Definition NimBLEAttValue.h:72
bool setValue(const uint8_t *value, uint16_t len)
Set the value from a buffer.
Definition NimBLEAttValue.cpp:96
Connection information.
Definition NimBLEConnInfo.h:32
A base class for local BLE attributes.
Definition NimBLELocalAttribute.h:29
The model of a BLE server.
Definition NimBLEServer.h:60
A model of a BLE UUID.
Definition NimBLEUUID.h:41