1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2020 Gothel Software e.K.
* Copyright (c) 2020 ZAFENA AB
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef GATT_DESCRIPTOR_HPP_
#define GATT_DESCRIPTOR_HPP_
#include <cstring>
#include <string>
#include <memory>
#include <cstdint>
#include <vector>
#include <mutex>
#include <atomic>
#include "UUID.hpp"
#include "BTTypes.hpp"
#include "OctetTypes.hpp"
#include "ATTPDUTypes.hpp"
#include "DBTTypes.hpp"
/**
* - - - - - - - - - - - - - - -
*
* Module GATTDescriptor:
*
* - BT Core Spec v5.2: Vol 3, Part G Generic Attribute Protocol (GATT)
* - BT Core Spec v5.2: Vol 3, Part G GATT: 2.6 GATT Profile Hierarchy
*/
namespace direct_bt {
class DBTDevice; // forward
class GATTCharacteristic; // forward
typedef std::shared_ptr<GATTCharacteristic> GATTCharacteristicRef;
/**
* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3 Characteristic Descriptor
*/
class GATTDescriptor : public DBTObject {
private:
/** Descriptor's characteristic weak back-reference */
std::weak_ptr<GATTCharacteristic> wbr_characteristic;
/**
* For an unknown reason, using the virtual function 'toString()'
* while constructing an exception message causes a SIGSEGV.
* <p>
* This method represents a non-virtual variation,
* which also does not call any other virtual function.
* </p>
*/
std::string toSafeString() const noexcept;
public:
static const uuid16_t TYPE_EXT_PROP;
static const uuid16_t TYPE_USER_DESC;
static const uuid16_t TYPE_CCC_DESC;
static std::shared_ptr<const uuid_t> getStaticType(const uuid16_t & type) noexcept {
return std::shared_ptr<const uuid_t>(&type, [](const uuid_t *){});
}
/**
* Following UUID16 GATT profile attribute types are listed under:
* BT Core Spec v5.2: Vol 3, Part G GATT: 3.4 Summary of GATT Profile Attribute Types
*/
enum Type : uint16_t {
CHARACTERISTIC_APPEARANCE = 0x2A01,
CHARACTERISTIC_PERIPHERAL_PRIV_FLAG = 0x2A02,
CHARACTERISTIC_RECONNECTION_ADDRESS = 0x2A03,
CHARACTERISTIC_PERIPHERAL_PREF_CONN = 0x2A04,
CHARACTERISTIC_SERVICE_CHANGED = 0x2A05,
/* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.1 Characteristic Extended Properties */
CHARACTERISTIC_EXTENDED_PROPERTIES = 0x2900,
/* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.2 Characteristic User Description (Characteristic Descriptor, optional, single, string) */
CHARACTERISTIC_USER_DESCRIPTION = 0x2901,
/* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration (Characteristic Descriptor, optional, single, uint16_t bitfield) */
CLIENT_CHARACTERISTIC_CONFIGURATION = 0x2902,
/* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.4 Server Characteristic Configuration (Characteristic Descriptor, optional, single, bitfield) */
SERVER_CHARACTERISTIC_CONFIGURATION = 0x2903,
/* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.5 Characteristic Presentation Format (Characteristic Descriptor, optional, single, complex) */
CHARACTERISTIC_PRESENTATION_FORMAT = 0x2904,
CHARACTERISTIC_AGGREGATE_FORMAT = 0x2905,
/** Our identifier to mark a custom vendor Characteristic Descriptor */
CUSTOM_CHARACTERISTIC_DESCRIPTION = 0x8888
};
/** Type of descriptor */
std::shared_ptr<const uuid_t> type;
/**
* Characteristic Descriptor Handle
* <p>
* Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
* </p>
*/
const uint16_t handle;
/* Characteristics Descriptor's Value */
POctets value;
GATTDescriptor(const GATTCharacteristicRef & characteristic, const std::shared_ptr<const uuid_t> & type,
const uint16_t handle) noexcept
: wbr_characteristic(characteristic), type(type), handle(handle), value(0) {}
std::string get_java_class() const noexcept override {
return java_class();
}
static std::string java_class() noexcept {
return std::string(JAVA_DBT_PACKAGE "DBTGattDescriptor");
}
std::shared_ptr<GATTCharacteristic> getCharacteristicUnchecked() const noexcept { return wbr_characteristic.lock(); }
std::shared_ptr<GATTCharacteristic> getCharacteristicChecked() const;
std::shared_ptr<GATTHandler> getGATTHandlerChecked() const;
std::shared_ptr<DBTDevice> getDeviceChecked() const;
virtual std::string toString() const noexcept override;
/** Value is uint16_t bitfield */
bool isExtendedProperties() const noexcept { return TYPE_EXT_PROP == *type; }
/* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration (Characteristic Descriptor, optional, single, uint16_t bitfield) */
bool isClientCharacteristicConfiguration() const noexcept{ return TYPE_CCC_DESC == *type; }
/**
* BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.1 Read Characteristic Descriptor
* <p>
* BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.2 Read Long Characteristic Descriptor
* </p>
* <p>
* If expectedLength = 0, then only one ATT_READ_REQ/RSP will be used.
* </p>
* <p>
* If expectedLength < 0, then long values using multiple ATT_READ_BLOB_REQ/RSP will be used until
* the response returns zero. This is the default parameter.
* </p>
* <p>
* If expectedLength > 0, then long values using multiple ATT_READ_BLOB_REQ/RSP will be used
* if required until the response returns zero.
* </p>
* <p>
* Convenience delegation call to GATTHandler via DBTDevice
* If the DBTDevice's GATTHandler is null, i.e. not connected, an IllegalStateException is thrown.
* </p>
*/
bool readValue(int expectedLength=-1);
/**
* BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.3 Write Characteristic Descriptors
* <p>
* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3 Characteristic Descriptor
* </p>
* <p>
* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration
* </p>
* <p>
* Convenience delegation call to GATTHandler via DBTDevice
* If the DBTDevice's GATTHandler is null, i.e. not connected, an IllegalStateException is thrown.
* </p>
*/
bool writeValue();
};
typedef std::shared_ptr<GATTDescriptor> GATTDescriptorRef;
inline bool operator==(const GATTDescriptor& lhs, const GATTDescriptor& rhs) noexcept
{ return lhs.handle == rhs.handle; /** unique attribute handles */ }
inline bool operator!=(const GATTDescriptor& lhs, const GATTDescriptor& rhs) noexcept
{ return !(lhs == rhs); }
} // namespace direct_bt
#endif /* GATT_DESCRIPTOR_HPP_ */
|