Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
BTGattHandler.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_HANDLER_HPP_
27 #define BT_GATT_HANDLER_HPP_
28 
29 #include <cstring>
30 #include <string>
31 #include <memory>
32 #include <cstdint>
33 
34 #include <mutex>
35 #include <atomic>
36 #include <thread>
37 
38 #include <jau/environment.hpp>
39 #include <jau/ringbuffer.hpp>
40 #include <jau/cow_darray.hpp>
41 
42 #include "UUID.hpp"
43 #include "BTTypes0.hpp"
44 #include "L2CAPComm.hpp"
45 #include "ATTPDUTypes.hpp"
46 #include "GattTypes.hpp"
47 
48 /**
49  * - - - - - - - - - - - - - - -
50  *
51  * Module BTGattHandler:
52  *
53  * - BT Core Spec v5.2: Vol 3, Part G Generic Attribute Protocol (GATT)
54  * - BT Core Spec v5.2: Vol 3, Part G GATT: 2.6 GATT Profile Hierarchy
55  * - BT Core Spec v5.2: Vol 3, Part G GATT: 3.4 Summary of GATT Profile Attribute Types
56  */
57 namespace direct_bt {
58 
59  class BTDevice; // forward
60 
61  /**
62  * GATT Singleton runtime environment properties
63  * <p>
64  * Also see {@link DBTEnv::getExplodingProperties(const std::string & prefixDomain)}.
65  * </p>
66  */
68  private:
69  BTGattEnv() noexcept;
70 
71  const bool exploding; // just to trigger exploding properties
72 
73  public:
74  /**
75  * Timeout for GATT read command replies, defaults to 500ms.
76  * <p>
77  * Environment variable is 'direct_bt.gatt.cmd.read.timeout'.
78  * </p>
79  */
81 
82  /**
83  * Timeout for GATT write command replies, defaults to 500ms.
84  * <p>
85  * Environment variable is 'direct_bt.gatt.cmd.write.timeout'.
86  * </p>
87  */
89 
90  /**
91  * Timeout for l2cap _initial_ command reply, defaults to 2500ms.
92  * <p>
93  * Environment variable is 'direct_bt.gatt.cmd.init.timeout'.
94  * </p>
95  */
97 
98  /**
99  * Medium ringbuffer capacity, defaults to 128 messages.
100  * <p>
101  * Environment variable is 'direct_bt.gatt.ringsize'.
102  * </p>
103  */
104  const int32_t ATTPDU_RING_CAPACITY;
105 
106  /**
107  * Debug all GATT Data communication
108  * <p>
109  * Environment variable is 'direct_bt.debug.gatt.data'.
110  * </p>
111  */
112  const bool DEBUG_DATA;
113 
114  public:
115  static BTGattEnv& get() noexcept {
116  /**
117  * Thread safe starting with C++11 6.7:
118  *
119  * If control enters the declaration concurrently while the variable is being initialized,
120  * the concurrent execution shall wait for completion of the initialization.
121  *
122  * (Magic Statics)
123  *
124  * Avoiding non-working double checked locking.
125  */
126  static BTGattEnv e;
127  return e;
128  }
129  };
130 
131  /**
132  * A thread safe GATT handler associated to one device via one L2CAP connection.
133  * <p>
134  * Implementation utilizes a lock free ringbuffer receiving data within its separate thread.
135  * </p>
136  * <p>
137  * Controlling Environment variables, see {@link BTGattEnv}.
138  * </p>
139  */
141  public:
142  enum class Defaults : int32_t {
143  /* BT Core Spec v5.2: Vol 3, Part F 3.2.8: Maximum length of an attribute value. */
144  MAX_ATT_MTU = 512,
145 
146  /* BT Core Spec v5.2: Vol 3, Part G GATT: 5.2.1 ATT_MTU */
147  MIN_ATT_MTU = 23
148  };
149  static constexpr int number(const Defaults d) { return static_cast<int>(d); }
150 
151  private:
152  const BTGattEnv & env;
153 
154  /** BTGattHandler's device weak back-reference */
155  std::weak_ptr<BTDevice> wbr_device;
156  L2CAPComm& l2cap;
157 
158  const std::string deviceString;
159  std::recursive_mutex mtx_command;
160  POctets rbuffer;
161 
162  jau::sc_atomic_bool is_connected; // reflects state
163  jau::relaxed_atomic_bool has_ioerror; // reflects state
164 
166  jau::sc_atomic_bool l2capReaderShallStop;
167 
168  std::mutex mtx_l2capReaderLifecycle;
169  std::condition_variable cv_l2capReaderInit;
170  pthread_t l2capReaderThreadId;
171  jau::relaxed_atomic_bool l2capReaderRunning;
172 
173  /** send immediate confirmation of indication events from device, defaults to true. */
174  jau::relaxed_atomic_bool sendIndicationConfirmation = true;
175  typedef jau::cow_darray<std::shared_ptr<BTGattCharListener>> characteristicListenerList_t;
176  characteristicListenerList_t characteristicListenerList;
177 
178  uint16_t serverMTU;
179  std::atomic<uint16_t> usedMTU; // concurrent use in ctor(set), send and l2capReaderThreadImpl
181  std::shared_ptr<GattGenericAccessSvc> genericAccess = nullptr;
182 
183  bool validateConnected() noexcept;
184 
185  void l2capReaderThreadImpl();
186 
187  /**
188  * Sends the given AttPDUMsg to the connected device via l2cap.
189  * <p>
190  * Implementation throws an IllegalStateException if not connected,
191  * a IllegalArgumentException if message size exceeds usedMTU.
192  * </p>
193  * <p>
194  * Implementation disconnect() and throws an BluetoothException
195  * if an l2cap write errors occurs.
196  * </p>
197  * <p>
198  * In case method completes, the message has been send out successfully.
199  * </p>
200  * @param msg the message to be send
201  */
202  void send(const AttPDUMsg & msg);
203 
204  /**
205  * Sends the given AttPDUMsg to the connected device via l2cap using {@link #send()}.
206  * <p>
207  * Implementation waits for timeout milliseconds receiving the response
208  * from the ringbuffer, filled from the reader-thread.
209  * </p>
210  * <p>
211  * Implementation disconnect() and throws an BluetoothException
212  * if no matching reply has been received within timeout milliseconds.
213  * </p>
214  * <p>
215  * In case method completes, the message has been send out successfully
216  * and a reply has also been received and is returned as a result.<br>
217  * Hence this method either throws an exception or returns a matching reply.
218  * </p>
219  *
220  * @param msg the message to be send
221  * @param timeout milliseconds to wait for a reply
222  * @return a valid reply, never nullptrs
223  */
224  std::unique_ptr<const AttPDUMsg> sendWithReply(const AttPDUMsg & msg, const int timeout);
225 
226  /**
227  * BT Core Spec v5.2: Vol 3, Part G GATT: 3.4.2 MTU Exchange
228  * <p>
229  * Returns the server-mtu if successful, otherwise 0.
230  * </p>
231  */
232  uint16_t exchangeMTUImpl(const uint16_t clientMaxMTU, const int32_t timeout);
233 
234  public:
235  /**
236  * Constructing a new BTGattHandler instance with its opened and connected L2CAP channel.
237  * <p>
238  * After successful l2cap connection, the MTU will be exchanged.
239  * See getServerMTU() and getUsedMTU(), the latter is in use.
240  * </p>
241  */
242  BTGattHandler(const std::shared_ptr<BTDevice> & device, L2CAPComm& l2cap_att) noexcept;
243 
244  BTGattHandler(const BTGattHandler&) = delete;
245  void operator=(const BTGattHandler&) = delete;
246 
247  /** Destructor closing this instance including L2CAP channel, see {@link #disconnect()}. */
248  ~BTGattHandler() noexcept;
249 
250  std::shared_ptr<BTDevice> getDeviceUnchecked() const noexcept { return wbr_device.lock(); }
251  std::shared_ptr<BTDevice> getDeviceChecked() const;
252 
253  bool isConnected() const noexcept { return is_connected ; }
254  bool hasIOError() const noexcept { return has_ioerror; }
255  std::string getStateString() const noexcept { return L2CAPComm::getStateString(is_connected, has_ioerror); }
256 
257  /**
258  * Disconnect this BTGattHandler and optionally the associated device
259  * @param disconnectDevice if true, associated device will also be disconnected, otherwise not.
260  * @param ioErrorCause if true, reason for disconnection is an IO error
261  * @return true if successful, otherwise false
262  */
263  bool disconnect(const bool disconnectDevice, const bool ioErrorCause) noexcept;
264 
265  inline uint16_t getServerMTU() const noexcept { return serverMTU; }
266  inline uint16_t getUsedMTU() const noexcept { return usedMTU; }
267 
268  /**
269  * Find and return the BTGattChar within internal primary services
270  * via given characteristic value handle.
271  * <p>
272  * Returns nullptr if not found.
273  * </p>
274  */
275  BTGattCharRef findCharacterisicsByValueHandle(const uint16_t charValueHandle) noexcept;
276 
277  /**
278  * Find and return the BTGattChar within given list of primary services
279  * via given characteristic value handle.
280  * <p>
281  * Returns nullptr if not found.
282  * </p>
283  */
284  BTGattCharRef findCharacterisicsByValueHandle(const uint16_t charValueHandle, jau::darray<BTGattServiceRef> &services) noexcept;
285 
286  /**
287  * Find and return the BTGattChar within given primary service
288  * via given characteristic value handle.
289  * <p>
290  * Returns nullptr if not found.
291  * </p>
292  */
293  BTGattCharRef findCharacterisicsByValueHandle(const uint16_t charValueHandle, BTGattServiceRef service) noexcept;
294 
295  /**
296  * Discover all primary services _and_ all its characteristics declarations
297  * including their client config.
298  * <p>
299  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.4.1 Discover All Primary Services
300  * </p>
301  * Method returns reference to BTGattHandler's internal BTGattService vector of discovered services
302  *
303  * @param shared_this shared pointer of this instance, used to forward a weak_ptr to BTGattService for back-reference. Reference is validated.
304  * @return BTGattHandler's internal BTGattService vector of discovered services
305  */
306  jau::darray<BTGattServiceRef> & discoverCompletePrimaryServices(std::shared_ptr<BTGattHandler> shared_this);
307 
308  /**
309  * Returns a reference of the internal kept BTGattService list.
310  * <p>
311  * The internal list will be populated via {@link #discoverCompletePrimaryServices()}.
312  * </p>
313  */
314  inline jau::darray<BTGattServiceRef> & getServices() noexcept { return services; }
315 
316  /**
317  * Returns the internal kept shared GattGenericAccessSvc instance.
318  * <p>
319  * This instance is created via {@link #discoverCompletePrimaryServices()}.
320  * </p>
321  */
322  inline std::shared_ptr<GattGenericAccessSvc> getGenericAccess() noexcept { return genericAccess; }
323 
324  /**
325  * Discover all primary services _only_.
326  * <p>
327  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.4.1 Discover All Primary Services
328  * </p>
329  * @param shared_this shared pointer of this instance, used to forward a weak_ptr to BTGattService for back-reference. Reference is validated.
330  * @param result vector containing all discovered primary services
331  * @return true on success, otherwise false
332  */
333  bool discoverPrimaryServices(std::shared_ptr<BTGattHandler> shared_this, jau::darray<BTGattServiceRef> & result);
334 
335  /**
336  * Discover all characteristics of a service and declaration attributes _only_.
337  * <p>
338  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.6.1 Discover All Characteristics of a Service
339  * </p>
340  * <p>
341  * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.1 Characterisic Declaration Attribute Value
342  * </p>
343  */
345 
346  /**
347  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.7.1 Discover All Characteristic Descriptors
348  */
349  bool discoverDescriptors(BTGattServiceRef & service);
350 
351  /**
352  * Generic read GATT value and long value
353  * <p>
354  * If expectedLength = 0, then only one ATT_READ_REQ/RSP will be used.
355  * </p>
356  * <p>
357  * If expectedLength < 0, then long values using multiple ATT_READ_BLOB_REQ/RSP will be used until
358  * the response returns zero. This is the default parameter.
359  * </p>
360  * <p>
361  * If expectedLength > 0, then long values using multiple ATT_READ_BLOB_REQ/RSP will be used
362  * if required until the response returns zero.
363  * </p>
364  */
365  bool readValue(const uint16_t handle, POctets & res, int expectedLength=-1);
366 
367  /**
368  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.8.1 Read Characteristic Value
369  * <p>
370  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.8.3 Read Long Characteristic Value
371  * </p>
372  * <p>
373  * If expectedLength = 0, then only one ATT_READ_REQ/RSP will be used.
374  * </p>
375  * <p>
376  * If expectedLength < 0, then long values using multiple ATT_READ_BLOB_REQ/RSP will be used until
377  * the response returns zero. This is the default parameter.
378  * </p>
379  * <p>
380  * If expectedLength > 0, then long values using multiple ATT_READ_BLOB_REQ/RSP will be used
381  * if required until the response returns zero.
382  * </p>
383  */
384  bool readCharacteristicValue(const BTGattChar & c, POctets & res, int expectedLength=-1);
385 
386  /**
387  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.1 Read Characteristic Descriptor
388  * <p>
389  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.2 Read Long Characteristic Descriptor
390  * </p>
391  * <p>
392  * If expectedLength = 0, then only one ATT_READ_REQ/RSP will be used.
393  * </p>
394  * <p>
395  * If expectedLength < 0, then long values using multiple ATT_READ_BLOB_REQ/RSP will be used until
396  * the response returns zero. This is the default parameter.
397  * </p>
398  * <p>
399  * If expectedLength > 0, then long values using multiple ATT_READ_BLOB_REQ/RSP will be used
400  * if required until the response returns zero.
401  * </p>
402  */
403  bool readDescriptorValue(BTGattDesc & cd, int expectedLength=-1);
404 
405  /**
406  * Generic write GATT value and long value
407  */
408  bool writeValue(const uint16_t handle, const TROOctets & value, const bool withResponse);
409 
410  /**
411  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.3 Write Characteristic Descriptors
412  * <p>
413  * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3 Characteristic Descriptor
414  * </p>
415  * <p>
416  * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration
417  * </p>
418  */
419  bool writeDescriptorValue(const BTGattDesc & cd);
420 
421  /**
422  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.3 Write Characteristic Value
423  */
424  bool writeCharacteristicValue(const BTGattChar & c, const TROOctets & value);
425 
426  /**
427  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.1 Write Characteristic Value Without Response
428  */
429  bool writeCharacteristicValueNoResp(const BTGattChar & c, const TROOctets & value);
430 
431  /**
432  * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration
433  * <p>
434  * Method enables notification and/or indication for the corresponding characteristic at BLE level.
435  * </p>
436  * <p>
437  * It is recommended to utilize notification over indication, as its link-layer handshake
438  * and higher potential bandwidth may deliver material higher performance.
439  * </p>
440  * <p>
441  * Throws an IllegalArgumentException if the given BTGattDesc is not a ClientCharacteristicConfiguration.
442  * </p>
443  */
444  bool configNotificationIndication(BTGattDesc & cd, const bool enableNotification, const bool enableIndication);
445 
446  /**
447  * Add the given listener to the list if not already present.
448  * <p>
449  * Returns true if the given listener is not element of the list and has been newly added,
450  * otherwise false.
451  * </p>
452  */
453  bool addCharListener(std::shared_ptr<BTGattCharListener> l);
454 
455  /**
456  * Remove the given listener from the list.
457  * <p>
458  * Returns true if the given listener is an element of the list and has been removed,
459  * otherwise false.
460  * </p>
461  */
462  bool removeCharListener(std::shared_ptr<BTGattCharListener> l) noexcept;
463 
464  /**
465  * Remove the given listener from the list.
466  * <p>
467  * Returns true if the given listener is an element of the list and has been removed,
468  * otherwise false.
469  * </p>
470  */
471  bool removeCharListener(const BTGattCharListener * l) noexcept;
472 
473 
474  /**
475  * Remove all {@link BTGattCharListener} from the list, which are associated to the given {@link BTGattChar}.
476  * <p>
477  * Implementation tests all listener's BTGattCharListener::match(const BTGattChar & characteristic)
478  * to match with the given associated characteristic.
479  * </p>
480  * @param associatedCharacteristic the match criteria to remove any BTGattCharListener from the list
481  * @return number of removed listener.
482  */
483  int removeAllAssociatedCharListener(std::shared_ptr<BTGattChar> associatedChar) noexcept;
484 
485  int removeAllAssociatedCharListener(const BTGattChar * associatedChar) noexcept;
486 
487  /**
488  * Remove all event listener from the list.
489  * <p>
490  * Returns the number of removed event listener.
491  * </p>
492  */
493  int removeAllCharListener() noexcept ;
494 
495  /**
496  * Enable or disable sending an immediate confirmation for received indication events from the device.
497  * <p>
498  * Default value is true.
499  * </p>
500  * <p>
501  * This setting is per BTGattHandler and hence per BTDevice.
502  * </p>
503  */
504  void setSendIndicationConfirmation(const bool v);
505 
506  /**
507  * Returns whether sending an immediate confirmation for received indication events from the device is enabled.
508  * <p>
509  * Default value is true.
510  * </p>
511  * <p>
512  * This setting is per BTGattHandler and hence per BTDevice.
513  * </p>
514  */
515  bool getSendIndicationConfirmation() noexcept;
516 
517  /*****************************************************/
518  /** Higher level semantic functionality **/
519  /*****************************************************/
520 
521  std::shared_ptr<GattGenericAccessSvc> getGenericAccess(jau::darray<BTGattServiceRef> & primServices);
522  std::shared_ptr<GattGenericAccessSvc> getGenericAccess(jau::darray<BTGattCharRef> & genericAccessCharDeclList);
523 
524  std::shared_ptr<GattDeviceInformationSvc> getDeviceInformation(jau::darray<BTGattServiceRef> & primServices);
525  std::shared_ptr<GattDeviceInformationSvc> getDeviceInformation(jau::darray<BTGattCharRef> & deviceInfoCharDeclList);
526 
527  /**
528  * Issues a ping to the device, validating whether it is still reachable.
529  * <p>
530  * This method could be periodically utilized to shorten the underlying OS disconnect period
531  * after turning the device off, which lies within 7-13s.
532  * </p>
533  * <p>
534  * In case the device is no more reachable, disconnect will be initiated due to the occurring IO error.
535  * </p>
536  * @return `true` if successful, otherwise false in case no GATT services exists etc.
537  */
538  bool ping();
539  };
540 
541 } // namespace direct_bt
542 
543 #endif /* BT_GATT_HANDLER_HPP_ */
direct_bt::BTGattEnv::get
static BTGattEnv & get() noexcept
Definition: BTGattHandler.hpp:115
direct_bt::BTGattHandler::writeDescriptorValue
bool writeDescriptorValue(const BTGattDesc &cd)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.3 Write Characteristic Descriptors.
Definition: BTGattHandler.cpp:835
direct_bt::BTGattHandler::removeAllCharListener
int removeAllCharListener() noexcept
Remove all event listener from the list.
Definition: BTGattHandler.cpp:166
direct_bt::BTGattEnv
GATT Singleton runtime environment properties.
Definition: BTGattHandler.hpp:67
direct_bt::BTGattHandler::getDeviceUnchecked
std::shared_ptr< BTDevice > getDeviceUnchecked() const noexcept
Definition: BTGattHandler.hpp:250
L2CAPComm.hpp
direct_bt::BTGattHandler::getDeviceInformation
std::shared_ptr< GattDeviceInformationSvc > getDeviceInformation(jau::darray< BTGattServiceRef > &primServices)
Definition: BTGattHandler.cpp:1096
direct_bt::BTGattHandler::getServices
jau::darray< BTGattServiceRef > & getServices() noexcept
Returns a reference of the internal kept BTGattService list.
Definition: BTGattHandler.hpp:314
direct_bt::BTGattHandler::setSendIndicationConfirmation
void setSendIndicationConfirmation(const bool v)
Enable or disable sending an immediate confirmation for received indication events from the device.
Definition: BTGattHandler.cpp:172
direct_bt::BTGattHandler::hasIOError
bool hasIOError() const noexcept
Definition: BTGattHandler.hpp:254
direct_bt::BTGattHandler::readDescriptorValue
bool readDescriptorValue(BTGattDesc &cd, int expectedLength=-1)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.1 Read Characteristic Descriptor.
Definition: BTGattHandler.cpp:743
jau::root_environment
Base jau environment class, merely to tag all environment settings by inheritance and hence documenta...
Definition: environment.hpp:51
direct_bt::BTGattHandler::findCharacterisicsByValueHandle
BTGattCharRef findCharacterisicsByValueHandle(const uint16_t charValueHandle) noexcept
Find and return the BTGattChar within internal primary services via given characteristic value handle...
Definition: BTGattHandler.cpp:489
direct_bt
Definition: ATTPDUTypes.hpp:171
direct_bt::BTGattChar
Definition: BTGattChar.hpp:75
direct_bt::BTGattEnv::DEBUG_DATA
const bool DEBUG_DATA
Debug all GATT Data communication.
Definition: BTGattHandler.hpp:112
direct_bt::BTGattHandler::Defaults
Defaults
Definition: BTGattHandler.hpp:142
direct_bt::BTGattServiceRef
std::shared_ptr< BTGattService > BTGattServiceRef
Definition: BTGattChar.hpp:60
direct_bt::L2CAPComm
Read/Write L2CAP communication channel.
Definition: L2CAPComm.hpp:113
jau
Definition: basic_algos.hpp:34
direct_bt::BTGattHandler::writeValue
bool writeValue(const uint16_t handle, const TROOctets &value, const bool withResponse)
Generic write GATT value and long value.
Definition: BTGattHandler.cpp:865
direct_bt::BTGattHandler::discoverDescriptors
bool discoverDescriptors(BTGattServiceRef &service)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.7.1 Discover All Characteristic Descriptors.
Definition: BTGattHandler.cpp:657
direct_bt::BTGattHandler::readCharacteristicValue
bool readCharacteristicValue(const BTGattChar &c, POctets &res, int expectedLength=-1)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.8.1 Read Characteristic Value.
Definition: BTGattHandler.cpp:753
direct_bt::L2CAPComm::getStateString
std::string getStateString() const
Definition: L2CAPComm.hpp:178
jau::ordered_atomic< bool, std::memory_order::memory_order_seq_cst >
jau::darray< BTGattServiceRef >
direct_bt::BTGattHandler::readValue
bool readValue(const uint16_t handle, POctets &res, int expectedLength=-1)
Generic read GATT value and long value.
Definition: BTGattHandler.cpp:762
cow_darray.hpp
direct_bt::BTGattHandler::getDeviceChecked
std::shared_ptr< BTDevice > getDeviceChecked() const
Definition: BTGattHandler.cpp:75
direct_bt::BTGattCharRef
std::shared_ptr< BTGattChar > BTGattCharRef
Definition: BTGattChar.hpp:409
direct_bt::AttPDUMsg
Handles the Attribute Protocol (ATT) using Protocol Data Unit (PDU) encoded messages over L2CAP chann...
Definition: ATTPDUTypes.hpp:313
direct_bt::BTGattEnv::GATT_WRITE_COMMAND_REPLY_TIMEOUT
const int32_t GATT_WRITE_COMMAND_REPLY_TIMEOUT
Timeout for GATT write command replies, defaults to 500ms.
Definition: BTGattHandler.hpp:88
direct_bt::BTGattCharListener
BTGattChar event listener for notification and indication events.
Definition: BTGattChar.hpp:435
direct_bt::BTGattHandler::getServerMTU
uint16_t getServerMTU() const noexcept
Definition: BTGattHandler.hpp:265
direct_bt::GattGenericAccessSvc
Generic Access Service is a mandatory GATT service all peripherals are required to implement.
Definition: GattNumbers.hpp:264
direct_bt::BTGattHandler::Defaults::MAX_ATT_MTU
@ MAX_ATT_MTU
direct_bt::BTGattHandler::ping
bool ping()
Issues a ping to the device, validating whether it is still reachable.
Definition: BTGattHandler.cpp:994
direct_bt::BTGattEnv::ATTPDU_RING_CAPACITY
const int32_t ATTPDU_RING_CAPACITY
Medium ringbuffer capacity, defaults to 128 messages.
Definition: BTGattHandler.hpp:104
BTTypes0.hpp
jau::ringbuffer
Ring buffer implementation, a.k.a circular buffer, exposing lock-free get*(..) and put*(....
Definition: ringbuffer.hpp:115
UUID.hpp
direct_bt::BTGattHandler::Defaults::MIN_ATT_MTU
@ MIN_ATT_MTU
jau::cow_darray
Implementation of a Copy-On-Write (CoW) using jau::darray as the underlying storage,...
Definition: cow_darray.hpp:130
direct_bt::BTGattHandler::writeCharacteristicValueNoResp
bool writeCharacteristicValueNoResp(const BTGattChar &c, const TROOctets &value)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.1 Write Characteristic Value Without Response.
Definition: BTGattHandler.cpp:859
direct_bt::BTGattHandler::addCharListener
bool addCharListener(std::shared_ptr< BTGattCharListener > l)
Add the given listener to the list if not already present.
Definition: BTGattHandler.cpp:105
direct_bt::BTGattHandler
A thread safe GATT handler associated to one device via one L2CAP connection.
Definition: BTGattHandler.hpp:140
direct_bt::BTGattHandler::getUsedMTU
uint16_t getUsedMTU() const noexcept
Definition: BTGattHandler.hpp:266
ATTPDUTypes.hpp
GattTypes.hpp
direct_bt::TROOctets
Transient read only octet data, i.e.
Definition: OctetTypes.hpp:59
direct_bt::BTGattHandler::getStateString
std::string getStateString() const noexcept
Definition: BTGattHandler.hpp:255
jau::nsize_t
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
Definition: int_types.hpp:44
ringbuffer.hpp
direct_bt::GattDeviceInformationSvc
Device Information is a GATT service.
Definition: GattNumbers.hpp:315
direct_bt::BTGattHandler::getSendIndicationConfirmation
bool getSendIndicationConfirmation() noexcept
Returns whether sending an immediate confirmation for received indication events from the device is e...
Definition: BTGattHandler.cpp:176
direct_bt::BTGattHandler::getGenericAccess
std::shared_ptr< GattGenericAccessSvc > getGenericAccess() noexcept
Returns the internal kept shared GattGenericAccessSvc instance.
Definition: BTGattHandler.hpp:322
direct_bt::BTGattHandler::isConnected
bool isConnected() const noexcept
Definition: BTGattHandler.hpp:253
direct_bt::BTGattHandler::writeCharacteristicValue
bool writeCharacteristicValue(const BTGattChar &c, const TROOctets &value)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.3 Write Characteristic Value.
Definition: BTGattHandler.cpp:849
direct_bt::BTGattHandler::removeAllAssociatedCharListener
int removeAllAssociatedCharListener(std::shared_ptr< BTGattChar > associatedChar) noexcept
Remove all BTGattCharListener from the list, which are associated to the given BTGattChar.
Definition: BTGattHandler.cpp:137
direct_bt::BTGattHandler::discoverPrimaryServices
bool discoverPrimaryServices(std::shared_ptr< BTGattHandler > shared_this, jau::darray< BTGattServiceRef > &result)
Discover all primary services only.
Definition: BTGattHandler.cpp:528
direct_bt::BTGattHandler::disconnect
bool disconnect(const bool disconnectDevice, const bool ioErrorCause) noexcept
Disconnect this BTGattHandler and optionally the associated device.
Definition: BTGattHandler.cpp:341
direct_bt::POctets
Persistent octet data, i.e.
Definition: OctetTypes.hpp:451
direct_bt::BTGattEnv::GATT_READ_COMMAND_REPLY_TIMEOUT
const int32_t GATT_READ_COMMAND_REPLY_TIMEOUT
Timeout for GATT read command replies, defaults to 500ms.
Definition: BTGattHandler.hpp:80
direct_bt::BTGattHandler::configNotificationIndication
bool configNotificationIndication(BTGattDesc &cd, const bool enableNotification, const bool enableIndication)
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
Definition: BTGattHandler.cpp:908
direct_bt::BTGattEnv::GATT_INITIAL_COMMAND_REPLY_TIMEOUT
const int32_t GATT_INITIAL_COMMAND_REPLY_TIMEOUT
Timeout for l2cap initial command reply, defaults to 2500ms.
Definition: BTGattHandler.hpp:96
direct_bt::BTGattHandler::number
static constexpr int number(const Defaults d)
Definition: BTGattHandler.hpp:149
direct_bt::BTDevice
Definition: BTDevice.hpp:57
direct_bt::BTGattDesc
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3 Characteristic Descriptor.
Definition: BTGattDesc.hpp:62
direct_bt::BTGattHandler::discoverCompletePrimaryServices
jau::darray< BTGattServiceRef > & discoverCompletePrimaryServices(std::shared_ptr< BTGattHandler > shared_this)
Discover all primary services and all its characteristics declarations including their client config.
Definition: BTGattHandler.cpp:513
environment.hpp
direct_bt::BTGattHandler::removeCharListener
bool removeCharListener(std::shared_ptr< BTGattCharListener > l) noexcept
Remove the given listener from the list.
Definition: BTGattHandler.cpp:112
direct_bt::BTGattHandler::discoverCharacteristics
bool discoverCharacteristics(BTGattServiceRef &service)
Discover all characteristics of a service and declaration attributes only.
Definition: BTGattHandler.cpp:592