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