Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
BTDevice.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_DEVICE_HPP_
27 #define BT_DEVICE_HPP_
28 
29 #include <cstring>
30 #include <string>
31 #include <memory>
32 #include <cstdint>
33 
34 #include <mutex>
35 
36 #include <jau/darray.hpp>
37 
38 #include "BTTypes1.hpp"
39 
40 #include "HCIIoctl.hpp"
41 #include "HCIComm.hpp"
42 
43 #include "MgmtTypes.hpp"
44 #include "SMPHandler.hpp"
45 #include "BTGattHandler.hpp"
46 
47 namespace direct_bt {
48 
49  // *************************************************
50  // *************************************************
51  // *************************************************
52 
53  class BTAdapter; // forward
54  class AdapterStatusListener; // forward
55 
56  class BTDevice : public BTObject
57  {
58  friend BTAdapter; // managing us: ctor and update(..) during discovery
59  friend BTGattHandler; // may issue detailed disconnect(..)
60 
61  private:
62  BTAdapter & adapter;
63  L2CAPComm l2cap_att;
64  uint64_t ts_last_discovery;
65  uint64_t ts_last_update;
66  std::string name;
67  int8_t rssi = 127; // The core spec defines 127 as the "not available" value
68  int8_t tx_power = 127; // The core spec defines 127 as the "not available" value
70  jau::relaxed_atomic_uint16 hciConnHandle;
72  std::shared_ptr<ManufactureSpecificData> advMSD = nullptr;
74 #if SMP_SUPPORTED_BY_OS
75  std::shared_ptr<SMPHandler> smpHandler = nullptr;
76  std::recursive_mutex mtx_smpHandler;
77 #endif
78  std::shared_ptr<BTGattHandler> gattHandler = nullptr;
79  mutable std::recursive_mutex mtx_gattHandler;
80  mutable std::recursive_mutex mtx_connect;
81  mutable std::mutex mtx_data;
82  std::atomic<bool> isConnected;
83  std::atomic<bool> allowDisconnect; // allowDisconnect = isConnected || 'isConnectIssued'
84 
85  struct PairingData {
88  BTSecurityLevel sec_level_conn = BTSecurityLevel::UNSET;
89  BTSecurityLevel sec_level_user = BTSecurityLevel::UNSET;
90  SMPIOCapability ioCap_auto = SMPIOCapability::UNSET; // not cleared by clearSMPStates()
91 
92  SMPPairingState state;
93  PairingMode mode;
94  bool res_requested_sec;
95  bool use_sc;
96 
97  SMPAuthReqs authReqs_init, authReqs_resp;
98  SMPIOCapability ioCap_init, ioCap_resp;
99  SMPOOBDataFlag oobFlag_init, oobFlag_resp;
100  uint8_t maxEncsz_init, maxEncsz_resp;
101  SMPKeyType keys_init_exp, keys_resp_exp;
102  SMPKeyType keys_init_has, keys_resp_has;
103 
104  // LTK: Set of Long Term Key data: ltk, ediv + rand
105  SMPLongTermKeyInfo ltk_init, ltk_resp;
106 
107  // IRK
108  jau::uint128_t irk_init, irk_resp;
109  EUI48 address;
110  bool is_static_random_address;
111 
112  // CSRK
113  SMPSignatureResolvingKeyInfo csrk_init, csrk_resp;
114  };
115  PairingData pairing_data;
116  mutable std::mutex mtx_pairing;
117  std::condition_variable cv_pairing_state_changed;
118  mutable jau::sc_atomic_bool sync_data;
119 
120  /** Private class only for private make_shared(). */
121  class ctor_cookie { friend BTDevice; ctor_cookie(const uint16_t secret) { (void)secret; } };
122 
123  /** Private std::make_shared<BTDevice>(..) vehicle for friends. */
124  static std::shared_ptr<BTDevice> make_shared(BTAdapter & adapter, EInfoReport const & r) {
125  return std::make_shared<BTDevice>(BTDevice::ctor_cookie(0), adapter, r);
126  }
127 
128  /** Add advertised service (GAP discovery) */
129  bool addAdvService(std::shared_ptr<uuid_t> const &uuid) noexcept;
130  /** Add advertised service (GAP discovery) */
131  bool addAdvServices(jau::darray<std::shared_ptr<uuid_t>> const & services) noexcept;
132  /**
133  * Find advertised service (GAP discovery) index
134  * @return index >= 0 if found, otherwise -1
135  */
136  int findAdvService(std::shared_ptr<uuid_t> const &uuid) const noexcept;
137 
138  EIRDataType update(EInfoReport const & data) noexcept;
139  EIRDataType update(GattGenericAccessSvc const &data, const uint64_t timestamp) noexcept;
140 
141  void notifyDisconnected() noexcept;
142  void notifyConnected(std::shared_ptr<BTDevice> sthis, const uint16_t handle, const SMPIOCapability io_cap) noexcept;
143  void notifyLEFeatures(std::shared_ptr<BTDevice> sthis, const LE_Features features) noexcept;
144 
145  /**
146  * Setup L2CAP channel connection to device incl. optional security encryption level off-thread.
147  * <p>
148  * Will be performed after connectLE(..), i.e. notifyConnected() and notifyLEFeatures(),
149  * initiated by the latter.
150  * </p>
151  */
152  void processL2CAPSetup(std::shared_ptr<BTDevice> sthis);
153 
154  /**
155  * Established SMP host connection and security for L2CAP connection if sec_level > BTSecurityLevel::NONE.
156  * <p>
157  * Will be performed after connectLE(..), i.e. notifyConnected() and notifyLEFeatures().<br>
158  * Called from processL2CAPSetup, if supported.
159  * </p>
160  * <p>
161  * If sec_level > BTSecurityLevel::NONE, sets the BlueZ's L2CAP socket BT_SECURITY sec_level, determining the SMP security mode per connection.
162  * </p>
163  * <p>
164  * The SMPHandler is managed by this device instance and closed via disconnectSMP().
165  * </p>
166  *
167  * @param sec_level sec_level <= BTSecurityLevel::NONE will not set security level and returns false.
168  * @return true if a security level > BTSecurityLevel::NONE has been set successfully, false if no security level has been set or if it failed.
169  */
170  bool connectSMP(std::shared_ptr<BTDevice> sthis, const BTSecurityLevel sec_level) noexcept;
171 
172  bool checkPairingKeyDistributionComplete(const std::string& timestamp) const noexcept;
173 
174  bool updatePairingState(std::shared_ptr<BTDevice> sthis, const MgmtEvent& evt, const HCIStatusCode evtStatus, SMPPairingState claimed_state) noexcept;
175 
176  /**
177  * Forwarded from HCIHandler -> BTAdapter -> this BTDevice
178  * <p>
179  * Will be initiated by processL2CAPSetup()'s security_level setup after connectLE(..), i.e. notifyConnected() and notifyLEFeatures().
180  * </p>
181  */
182  void hciSMPMsgCallback(std::shared_ptr<BTDevice> sthis, const SMPPDUMsg& msg, const HCIACLData::l2cap_frame& source) noexcept;
183 
184  /**
185  * Setup GATT via connectGATT() off-thread.
186  * <p>
187  * <p>
188  * Will be performed after connectLE(..), i.e. notifyConnected() and notifyLEFeatures().<br>
189  * Called from either processL2CAPSetup() w/o security or with SMP security readiness from hciSMPMsgCallback().
190  * </p>
191  */
192  void processDeviceReady(std::shared_ptr<BTDevice> sthis, const uint64_t timestamp);
193 
194  /**
195  * Returns a newly established GATT connection.
196  * <p>
197  * Will be performed after connectLE(..) via notifyConnected(), processNotifyConnectedOffThread().
198  * </p>
199  * <p>
200  * The GATTHandler is managed by this device instance and closed via disconnectGATT().
201  * </p>
202  */
203  bool connectGATT(std::shared_ptr<BTDevice> sthis) noexcept;
204 
205  /**
206  * Will be performed within disconnect() and notifyDisconnected().
207  */
208  void disconnectGATT(const int caller) noexcept;
209 
210  /**
211  * Will be performed within disconnect() and notifyDisconnected().
212  */
213  void disconnectSMP(const int caller) noexcept;
214 
215  void clearSMPStates(const bool connected) noexcept;
216 
217  void sendMgmtEvDeviceDisconnected(std::unique_ptr<MgmtEvent> evt) noexcept;
218 
219  public:
220  const uint64_t ts_creation;
221  /** Device's unique mac address and type tuple. */
222  const BDAddressAndType addressAndType; // FIXME: Mutable for resolvable -> identity during pairing?
223 
224  /** Private ctor for private BTDevice::make_shared() intended for friends. */
225  BTDevice(const BTDevice::ctor_cookie& cc, BTAdapter & adapter, EInfoReport const & r);
226 
227  BTDevice(const BTDevice&) = delete;
228  void operator=(const BTDevice&) = delete;
229 
230  /**
231  * Releases this instance after calling {@link #remove()}.
232  */
233  ~BTDevice() noexcept;
234 
235  std::string get_java_class() const noexcept override {
236  return java_class();
237  }
238  static std::string java_class() noexcept {
239  return std::string(JAVA_DBT_PACKAGE "DBTDevice");
240  }
241 
242  /** Returns the managing adapter */
243  BTAdapter & getAdapter() const { return adapter; }
244 
245  /** Returns the shared pointer of this instance managed by the adapter. */
246  std::shared_ptr<BTDevice> getSharedInstance() const noexcept;
247 
248  /**
249  * Returns the timestamp in monotonic milliseconds when this device instance has been created,
250  * either via its initial discovery or its initial direct connection.
251  * @see BasicTypes::getCurrentMilliseconds()
252  */
253  uint64_t getCreationTimestamp() const noexcept { return ts_creation; }
254 
255  /**
256  * Returns the timestamp in monotonic milliseconds when this device instance has
257  * discovered or connected directly the last time.
258  * @see BasicTypes::getCurrentMilliseconds()
259  */
260  uint64_t getLastDiscoveryTimestamp() const noexcept { return ts_last_discovery; }
261 
262  /**
263  * Returns the timestamp in monotonic milliseconds when this device instance underlying data
264  * has been updated the last time.
265  * @see BasicTypes::getCurrentMilliseconds()
266  */
267  uint64_t getLastUpdateTimestamp() const noexcept { return ts_last_update; }
268 
269  /**
270  * @see getLastUpdateTimestamp()
271  */
272  uint64_t getLastUpdateAge(const uint64_t ts_now) const noexcept { return ts_now - ts_last_update; }
273 
274  /**
275  * Returns the unique device EUI48 address and BDAddressType type.
276  * @since 2.2.0
277  */
278  constexpr BDAddressAndType const & getAddressAndType() const noexcept { return addressAndType; }
279 
280  /** Return RSSI of device as recognized at discovery and connect. */
281  int8_t getRSSI() const noexcept { return rssi; }
282 
283  /** Return Tx Power of device as recognized at discovery and connect. */
284  int8_t getTxPower() const noexcept { return tx_power; }
285 
286  /** Return AppearanceCat of device as recognized at discovery, connect and GATT discovery. */
287  AppearanceCat getAppearance() const noexcept { return appearance; }
288 
289  std::string const getName() const noexcept;
290 
291  /** Return shared ManufactureSpecificData as recognized at discovery, pre GATT discovery. */
292  std::shared_ptr<ManufactureSpecificData> const getManufactureSpecificData() const noexcept;
293 
294  /**
295  * Return a list of advertised services as recognized at discovery, pre GATT discovery.
296  * <p>
297  * To receive a complete list of GATT services including characteristics etc,
298  * use {@link #getGattService()}.
299  * </p>
300  */
301  jau::darray<std::shared_ptr<uuid_t>> getAdvertisedServices() const noexcept;
302 
303  std::string toString() const noexcept override { return toString(false); }
304 
305  std::string toString(bool includeDiscoveredServices) const noexcept;
306 
307  /**
308  * Add the given AdapterStatusListener to the list if not already present,
309  * intended to listen only for events matching this device.
310  *
311  * User needs to implement AdapterStatusListener::matchDevice() for the latter.
312  *
313  * The AdapterStatusListener is released at remove() or this device's destruction.
314  * <p>
315  * Returns true if the given listener is not element of the list and has been newly added,
316  * otherwise false.
317  * </p>
318  * <p>
319  * The newly added AdapterStatusListener will receive an initial
320  * AdapterStatusListener::adapterSettingsChanged(..) event,
321  * passing an empty AdapterSetting::NONE oldMask and changedMask, as well as current AdapterSetting newMask. <br>
322  * This allows the receiver to be aware of this adapter's current settings.
323  * </p>
324  * @since 2.3.0
325  * @see BTAdapter::addStatusListener()
326  * @see BTAdapter::removeStatusListener()
327  * @see BTAdapter::removeAllStatusListener()
328  * @see removeStatusListener()
329  */
330  bool addStatusListener(std::shared_ptr<AdapterStatusListener> l);
331 
332  /**
333  * Remove the given listener from the list.
334  * <p>
335  * Returns true if the given listener is an element of the list and has been removed,
336  * otherwise false.
337  * </p>
338  * @since 2.3.0
339  * @see BTAdapter::removeStatusListener()
340  * @see BTAdapter::removeStatusListener()
341  * @see BTAdapter::removeAllStatusListener()
342  * @see addStatusListener()
343  */
344  bool removeStatusListener(std::shared_ptr<AdapterStatusListener> l);
345 
346  /**
347  * Retrieves the current connection info for this device and returns the ConnectionInfo reference if successful,
348  * otherwise returns nullptr.
349  * <p>
350  * Before this method returns, the internal rssi and tx_power will be updated if any changed
351  * and therefore all BTAdapterStatusListener's deviceUpdated(..) method called for notification.
352  * </p>
353  */
354  std::shared_ptr<ConnectionInfo> getConnectionInfo() noexcept;
355 
356  /**
357  * Return true if the device has been successfully connected, otherwise false.
358  */
359  bool getConnected() noexcept { return isConnected.load(); }
360 
361  /**
362  * Request and return LE_PHYs bit for the given connection.
363  * <pre>
364  * BT Core Spec v5.2: Vol 4, Part E, 7.8.47 LE Read PHY command (we transfer the sequential value to this bitmask for unification)
365  * </pre>
366  * @param resRx reference for the resulting receiver LE_PHYs bit
367  * @param resTx reference for the resulting transmitter LE_PHYs bit
368  * @return HCIStatusCode
369  */
370  HCIStatusCode getConnectedLE_PHY(LE_PHYs& resRx, LE_PHYs& resTx) noexcept;
371 
372  /**
373  * Establish a HCI BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM connection to this device.
374  * <p>
375  * BT Core Spec v5.2: Vol 4, Part E HCI: 7.8.12 LE Create Connection command
376  * </p>
377  * <p>
378  * If this device's addressType is not BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM,
379  * HCIStatusCode::UNACCEPTABLE_CONNECTION_PARAM is being returned.
380  * </p>
381  * <p>
382  * The actual new connection handle will be delivered asynchronous and
383  * the connection event can be caught via AdapterStatusListener::deviceConnected(..),
384  * or if failed via AdapterStatusListener::deviceDisconnected(..).
385  * </p>
386  * <p>
387  * The device is tracked by the managing adapter.
388  * </p>
389  * <p>
390  * Default parameter values are chosen for using public address resolution
391  * and usual connection latency, interval etc.
392  * </p>
393  * <p>
394  * Set window to the same value as the interval, enables continuous scanning.
395  * </p>
396  * <p>
397  * The associated BTAdapter's HCIHandler instance is used to connect,
398  * see HCIHandler::le_create_conn().
399  * </p>
400  *
401  * @param le_scan_interval in units of 0.625ms, default value 24 for 15ms; Value range [4 .. 0x4000] for [2.5ms .. 10.24s]
402  * @param le_scan_window in units of 0.625ms, default value 24 for 15ms; Value range [4 .. 0x4000] for [2.5ms .. 10.24s]. Shall be <= le_scan_interval
403  * @param conn_interval_min in units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]
404  * @param conn_interval_max in units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]
405  * @param conn_latency slave latency in units of connection events, default value 0; Value range [0 .. 0x01F3].
406  * @param supervision_timeout in units of 10ms, default value >= 10 x conn_interval_max, we use HCIConstInt::LE_CONN_MIN_TIMEOUT_MS minimum; Value range [0xA-0x0C80] for [100ms - 32s].
407  * @return HCIStatusCode::SUCCESS if the command has been accepted, otherwise HCIStatusCode may disclose reason for rejection.
408  */
409  HCIStatusCode connectLE(const uint16_t le_scan_interval=24, const uint16_t le_scan_window=24,
410  const uint16_t conn_interval_min=12, const uint16_t conn_interval_max=12,
411  const uint16_t conn_latency=0, const uint16_t supervision_timeout=getHCIConnSupervisorTimeout(0, 15)) noexcept;
412 
413  /**
414  * Establish a HCI BDADDR_BREDR connection to this device.
415  * <p>
416  * BT Core Spec v5.2: Vol 4, Part E HCI: 7.1.5 Create Connection command
417  * </p>
418  * <p>
419  * If this device's addressType is not BDADDR_BREDR,
420  * HCIStatusCode::UNACCEPTABLE_CONNECTION_PARAM is being returned.
421  * </p>
422  * <p>
423  * The actual new connection handle will be delivered asynchronous and
424  * the connection event can be caught via AdapterStatusListener::deviceConnected(..),
425  * or if failed via AdapterStatusListener::deviceDisconnected(..).
426  * </p>
427  * <p>
428  * The device is tracked by the managing adapter.
429  * </p>
430  * <p>
431  * The associated BTAdapter's HCIHandler instance is used to connect,
432  * see HCIHandler::create_conn().
433  * </p>
434  * @return HCIStatusCode::SUCCESS if the command has been accepted, otherwise HCIStatusCode may disclose reason for rejection.
435  */
436  HCIStatusCode connectBREDR(const uint16_t pkt_type=HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5,
437  const uint16_t clock_offset=0x0000, const uint8_t role_switch=0x01) noexcept;
438 
439  /**
440  * Establish a default HCI connection to this device, using certain default parameter.
441  * <p>
442  * BT Core Spec v5.2: Vol 4, Part E HCI: 7.8.12 LE Create Connection command <br>
443  * BT Core Spec v5.2: Vol 4, Part E HCI: 7.1.5 Create Connection command
444  * </p>
445  * <p>
446  * Depending on this device's addressType,
447  * either a BREDR (BDADDR_BREDR) or LE (BDADDR_LE_PUBLIC, BDADDR_LE_RANDOM) connection is attempted.<br>
448  * If unacceptable, HCIStatusCode::UNACCEPTABLE_CONNECTION_PARAM is being returned.
449  * </p>
450  * <p>
451  * The actual new connection handle will be delivered asynchronous and
452  * the connection event can be caught via AdapterStatusListener::deviceConnected(..),
453  * or if failed via AdapterStatusListener::deviceDisconnected(..).
454  * <p>
455  * The device is tracked by the managing adapter.
456  * </p>
457  * <p>
458  * See connectLE() and connectBREDR() for more details.
459  * </p>
460  * @return HCIStatusCode::SUCCESS if the command has been accepted, otherwise HCIStatusCode may disclose reason for rejection.
461  */
462  HCIStatusCode connectDefault() noexcept;
463 
464 
465  /** Return the HCI connection handle to the LE or BREDR peer, zero if not connected. */
466  uint16_t getConnectionHandle() const noexcept { return hciConnHandle; }
467 
468  /**
469  * Disconnect the LE or BREDR peer's GATT and HCI connection.
470  * <p>
471  * BT Core Spec v5.2: Vol 4, Part E HCI: 7.1.6 Disconnect command
472  * </p>
473  * <p>
474  * The actual disconnect event will be delivered asynchronous and
475  * the connection event can be caught via AdapterStatusListener::deviceDisconnected(..).
476  * </p>
477  * <p>
478  * The device will be removed from the managing adapter's connected devices
479  * when AdapterStatusListener::deviceDisconnected(..) has been received.
480  * </p>
481  * <p>
482  * An open GATTHandler will also be closed.<br>
483  * The connection to this device is removed, removing all connected profiles.
484  * </p>
485  * <p>
486  * An application using one thread per device and rapid connect, should either use disconnect() or remove(),
487  * but never issue remove() after disconnect(). Doing so would eventually delete the device being already
488  * in use by another thread due to discovery post disconnect!
489  * </p>
490  * <p>
491  * The associated BTAdapter's HCIHandler instance is used to disconnect,
492  * see HCIHandler::disconnect().
493  * </p>
494  * @return HCIStatusCode::SUCCESS if the command has been accepted, otherwise HCIStatusCode may disclose reason for rejection.
495  */
497 
498  /**
499  * Returns the available ::SMPKeyType mask for the responder (LL slave) or initiator (LL master).
500  * @param responder if true, queries the responder (LL slave) key, otherwise the initiator (LL master) key.
501  * @return ::SMPKeyType mask result
502  */
503  SMPKeyType getAvailableSMPKeys(const bool responder) const noexcept;
504 
505  /**
506  * Returns a copy of the Long Term Key (LTK) info, valid after connection and SMP pairing has been completed.
507  * @param responder true will return the responder's LTK info (remote device, LL slave), otherwise the initiator's (the LL master).
508  * @return the resulting key. SMPLongTermKeyInfo::enc_size will be zero if invalid.
509  * @see ::SMPPairingState::COMPLETED
510  * @see AdapterStatusListener::deviceReady()
511  */
512  SMPLongTermKeyInfo getLongTermKeyInfo(const bool responder) const noexcept;
513 
514  /**
515  * Sets the long term ket (LTK) info of this device to reuse pre-paired encryption.
516  * <p>
517  * Must be called before connecting to this device, otherwise HCIStatusCode::CONNECTION_ALREADY_EXISTS will be returned.
518  * </p>
519  * @param ltk the pre-paired encryption LTK
520  * @return ::HCIStatusCode::SUCCESS if successful, otherwise the appropriate error code.
521  */
523 
524  /**
525  * Returns a copy of the Signature Resolving Key (LTK) info, valid after connection and SMP pairing has been completed.
526  * @param responder true will return the responder's LTK info (remote device, LL slave), otherwise the initiator's (the LL master).
527  * @return the resulting key
528  * @see ::SMPPairingState::COMPLETED
529  * @see AdapterStatusListener::deviceReady()
530  */
531  SMPSignatureResolvingKeyInfo getSignatureResolvingKeyInfo(const bool responder) const noexcept;
532 
533  /**
534  * Unpairs this device from the adapter while staying connected.
535  * <p>
536  * All keys will be cleared within the adapter and host implementation.<br>
537  * Should rarely being used by user.<br>
538  * Internally being used to re-start pairing if GATT connection fails
539  * in PairingMode::PRE_PAIRED mode.
540  * </p>
541  * @return HCIStatusCode::SUCCESS or an appropriate error status.
542  */
543  HCIStatusCode unpair() noexcept;
544 
545  /**
546  * Experimental only.
547  * <pre>
548  * adapter.stopDiscovery(): Renders pairDevice(..) to fail: Busy!
549  * pairDevice(..) behaves quite instable within our connected workflow: Not used!
550  * </pre>
551  */
552  HCIStatusCode pair(const SMPIOCapability io_cap) noexcept;
553 
554  /**
555  * Set the ::BTSecurityLevel used to connect to this device on the upcoming connection.
556  * <p>
557  * Method returns false if ::BTSecurityLevel::UNSET has been given,
558  * operation fails, this device has already being connected,
559  * or BTDevice::connectLE() or BTDevice::connectBREDR() has been issued already.
560  * </p>
561  * <p>
562  * To ensure a consistent authentication setup,
563  * it is advised to set ::SMPIOCapability::NO_INPUT_NO_OUTPUT for sec_level <= ::BTSecurityLevel::ENC_ONLY
564  * using setConnSecurity() as well as an IO capable ::SMPIOCapability value
565  * for ::BTSecurityLevel::ENC_AUTH or ::BTSecurityLevel::ENC_AUTH_FIPS.<br>
566  * You may like to consider using setConnSecurityBest().
567  * </p>
568  * @param sec_level ::BTSecurityLevel to be applied, ::BTSecurityLevel::UNSET will be ignored and method fails.
569  * @see ::BTSecurityLevel
570  * @see ::SMPIOCapability
571  * @see getConnSecurityLevel()
572  * @see setConnIOCapability()
573  * @see getConnIOCapability()
574  * @see setConnSecurity()
575  * @see setConnSecurityBest()
576  */
577  bool setConnSecurityLevel(const BTSecurityLevel sec_level) noexcept;
578 
579  /**
580  * Return the ::BTSecurityLevel, determined when the connection is established.
581  * @see ::BTSecurityLevel
582  * @see ::SMPIOCapability
583  * @see setConnSecurityLevel()
584  * @see setConnIOCapability()
585  * @see getConnIOCapability()
586  * @see setConnSecurity()
587  * @see setConnSecurityBest()
588  */
589  BTSecurityLevel getConnSecurityLevel() const noexcept;
590 
591  /**
592  * Sets the given ::SMPIOCapability used to connect to this device on the upcoming connection.
593  * <p>
594  * Method returns false if ::SMPIOCapability::UNSET has been given,
595  * operation fails, this device has already being connected,
596  * or BTDevice::connectLE() or BTDevice::connectBREDR() has been issued already.
597  * </p>
598  * @param[in] io_cap ::SMPIOCapability to be applied, ::SMPIOCapability::UNSET will be ignored and method fails.
599  * @see ::BTSecurityLevel
600  * @see ::SMPIOCapability
601  * @see setConnSecurityLevel()
602  * @see getConnSecurityLevel()
603  * @see getConnIOCapability()
604  * @see setConnSecurity()
605  * @see setConnSecurityBest()
606  */
607  bool setConnIOCapability(const SMPIOCapability io_cap) noexcept;
608 
609  /**
610  * Return the set ::SMPIOCapability value, determined when the connection is established.
611  * @see ::BTSecurityLevel
612  * @see ::SMPIOCapability
613  * @see setConnSecurityLevel()
614  * @see getConnSecurityLevel()
615  * @see setConnIOCapability()
616  * @see setConnSecurity()
617  * @see setConnSecurityBest()
618  */
619  SMPIOCapability getConnIOCapability() const noexcept;
620 
621  /**
622  * Sets the given ::BTSecurityLevel and ::SMPIOCapability used to connect to this device on the upcoming connection.
623  * <p>
624  * Method returns false if this device has already being connected,
625  * or BTDevice::connectLE() or BTDevice::connectBREDR() has been issued already.
626  * </p>
627  * <p>
628  * Method either changes both parameter for the upcoming connection or none at all.
629  * </p>
630  * @param[in] sec_level ::BTSecurityLevel to be applied.
631  * @param[in] io_cap ::SMPIOCapability to be applied.
632  * @see ::BTSecurityLevel
633  * @see ::SMPIOCapability
634  * @see setConnSecurityLevel()
635  * @see getConnSecurityLevel()
636  * @see setConnIOCapability()
637  * @see getConnIOCapability()
638  * @see setConnSecurityBest()
639  */
640  bool setConnSecurity(const BTSecurityLevel sec_level, const SMPIOCapability io_cap) noexcept;
641 
642  /**
643  * Convenience method to determine the best practice ::BTSecurityLevel and ::SMPIOCapability
644  * based on the given arguments, used to connect to this device on the upcoming connection.
645  * <pre>
646  * if( BTSecurityLevel::UNSET < sec_level && SMPIOCapability::UNSET != io_cap ) {
647  * return setConnSecurity(sec_level, io_cap);
648  * } else if( BTSecurityLevel::UNSET < sec_level ) {
649  * if( BTSecurityLevel::ENC_ONLY >= sec_level ) {
650  * return setConnSecurity(sec_level, SMPIOCapability::NO_INPUT_NO_OUTPUT);
651  * } else {
652  * return setConnSecurityLevel(sec_level);
653  * }
654  * } else if( SMPIOCapability::UNSET != io_cap ) {
655  * return setConnIOCapability(io_cap);
656  * } else {
657  * return false;
658  * }
659  * </pre>
660  * <p>
661  * Method returns false if ::BTSecurityLevel::UNSET and ::SMPIOCapability::UNSET has been given,
662  * operation fails, this device has already being connected,
663  * or BTDevice::connectLE() or BTDevice::connectBREDR() has been issued already.
664  * </p>
665  * <p>
666  * Method either changes both parameter for the upcoming connection or none at all.
667  * </p>
668  * @param[in] sec_level ::BTSecurityLevel to be applied.
669  * @param[in] io_cap ::SMPIOCapability to be applied.
670  * @see ::BTSecurityLevel
671  * @see ::SMPIOCapability
672  * @see setConnSecurityLevel()
673  * @see getConnSecurityLevel()
674  * @see setConnIOCapability()
675  * @see getConnIOCapability()
676  * @see setConnSecurityBest()
677  * @see setConnSecurityAuto()
678  */
679  bool setConnSecurityBest(const BTSecurityLevel sec_level, const SMPIOCapability io_cap) noexcept;
680 
681  /**
682  * Set automatic security negotiation of BTSecurityLevel and SMPIOCapability pairing mode.
683  * <p>
684  * Disabled by default and if set to ::SMPIOCapability::UNSET
685  * </p>
686  * Implementation iterates through below setup from highest security to lowest,
687  * while performing a full connection attempt for each.
688  * <pre>
689  * BTSecurityLevel::ENC_AUTH_FIPS, iocap_auto*
690  * BTSecurityLevel::ENC_AUTH, iocap_auto*
691  * BTSecurityLevel::ENC_ONLY, SMPIOCapability::NO_INPUT_NO_OUTPUT
692  * BTSecurityLevel::NONE, SMPIOCapability::NO_INPUT_NO_OUTPUT
693  *
694  * (*): user SMPIOCapability choice of for authentication IO, skipped if ::SMPIOCapability::NO_INPUT_NO_OUTPUT
695  * </pre>
696  * <p>
697  * Implementation may perform multiple connection and disconnect actions
698  * until successful pairing or failure.
699  * </p>
700  * <p>
701  * Intermediate AdapterStatusListener::deviceConnected() and AdapterStatusListener::deviceDisconnected()
702  * callbacks are not delivered while negotiating. This avoids any interference by the user application.
703  * </p>
704  * @param auth_io_cap user SMPIOCapability choice for negotiation
705  * @see isConnSecurityAutoEnabled()
706  * @see ::BTSecurityLevel
707  * @see ::SMPIOCapability
708  */
709  bool setConnSecurityAuto(const SMPIOCapability iocap_auto) noexcept;
710 
711  /**
712  * Returns true if automatic security negotiation has been enabled via setConnSecurityAuto(),
713  * otherwise false.
714  * @see setConnSecurityAuto()
715  */
716  bool isConnSecurityAutoEnabled() const noexcept;
717 
718  /**
719  * Method sets the given passkey entry, see ::PairingMode::PASSKEY_ENTRY_ini.
720  * <p>
721  * Call this method if the device shall be securely paired with ::PairingMode::PASSKEY_ENTRY_ini,
722  * i.e. when notified via AdapterStatusListener::devicePairingState() in state ::SMPPairingState::PASSKEY_EXPECTED.
723  * </p>
724  *
725  * @param passkey used for ::PairingMode::PASSKEY_ENTRY_ini method.
726  * Will be encrypted before sending to counter-party.
727  *
728  * @return HCIStatusCode::SUCCESS if the command has been accepted, otherwise ::HCIStatusCode may disclose reason for rejection.
729  * @see PairingMode
730  * @see SMPPairingState
731  * @see AdapterStatusListener::devicePairingState()
732  * @see setPairingPasskey()
733  * @see setPairingNumericComparison()
734  * @see getPairingMode()
735  * @see getPairingState()
736  */
737  HCIStatusCode setPairingPasskey(const uint32_t passkey) noexcept;
738 
739  /**
740  * Method replies with a negative passkey response, i.e. rejection, see ::PairingMode::PASSKEY_ENTRY_ini.
741  * <p>
742  * You may call this method if the device shall be securely paired with ::PairingMode::PASSKEY_ENTRY_ini,
743  * i.e. when notified via AdapterStatusListener::devicePairingState() in state ::SMPPairingState::PASSKEY_EXPECTED.
744  * </p>
745  * <p>
746  * Current experience exposed a roughly 3s immediate disconnect handshake with certain devices and/or Kernel BlueZ code.
747  *
748  * Hence using setPairingPasskey() with `passkey = 0` is recommended, especially when utilizing
749  * automatic security negotiation via setConnSecurityAuto()!
750  * </p>
751  *
752  * @return HCIStatusCode::SUCCESS if the command has been accepted, otherwise ::HCIStatusCode may disclose reason for rejection.
753  * @see PairingMode
754  * @see SMPPairingState
755  * @see AdapterStatusListener::devicePairingState()
756  * @see setPairingPasskey()
757  * @see setPairingNumericComparison()
758  * @see getPairingMode()
759  * @see getPairingState()
760  */
762 
763  /**
764  * Method sets the numeric comparison result, see ::PairingMode::NUMERIC_COMPARE_ini.
765  * <p>
766  * Call this method if the device shall be securely paired with ::PairingMode::NUMERIC_COMPARE_ini,
767  * i.e. when notified via AdapterStatusListener::devicePairingState() in state ::SMPPairingState::NUMERIC_COMPARE_EXPECTED.
768  * </p>
769  *
770  * @param equal used for ::PairingMode::NUMERIC_COMPARE_ini method.
771  * Will be encrypted before sending to counter-party.
772  *
773  * @return HCIStatusCode::SUCCESS if the command has been accepted, otherwise ::HCIStatusCode may disclose reason for rejection.
774  * @see PairingMode
775  * @see SMPPairingState
776  * @see AdapterStatusListener::devicePairingState()
777  * @see setPairingPasskey()
778  * @see setPairingNumericComparison()
779  * @see getPairingMode()
780  * @see getPairingState()
781  */
782  HCIStatusCode setPairingNumericComparison(const bool equal) noexcept;
783 
784  /**
785  * Returns the current ::PairingMode used by the device.
786  * <p>
787  * If the device is not paired, the current mode is ::PairingMode::NONE.
788  * </p>
789  * <p>
790  * If the Pairing Feature Exchange is completed, i.e. ::SMPPairingState::FEATURE_EXCHANGE_COMPLETED,
791  * as notified by AdapterStatusListener::devicePairingState(),
792  * the current mode reflects the currently used PairingMode.
793  * </p>
794  * <p>
795  * In case the Pairing Feature Exchange is in progress, the current mode is ::PairingMode::NEGOTIATING.
796  * </p>
797  * @return current ::PairingMode.
798  * @see PairingMode
799  * @see SMPPairingState
800  * @see AdapterStatusListener::devicePairingState()
801  * @see setPairingPasskey()
802  * @see setPairingNumericComparison()
803  * @see getPairingMode()
804  * @see getPairingState()
805  */
806  PairingMode getPairingMode() const noexcept;
807 
808  /**
809  * Returns the current ::SMPPairingState.
810  * <p>
811  * If the device is not paired, the current state is ::SMPPairingState::NONE.
812  * </p>
813  * @see PairingMode
814  * @see SMPPairingState
815  * @see AdapterStatusListener::devicePairingState()
816  * @see setPairingPasskey()
817  * @see setPairingNumericComparison()
818  * @see getPairingMode()
819  * @see getPairingState()
820  */
821  SMPPairingState getPairingState() const noexcept;
822 
823  /**
824  * Disconnects this device via disconnect(..) if getConnected()==true
825  * and explicitly removes its shared references from the Adapter:
826  * connected-devices, discovered-devices and shared-devices.
827  * <p>
828  * All added AdapterStatusListener associated with this instance
829  * will be removed via BTAdapter::removeAllStatusListener().
830  * </p>
831  * <p>
832  * This method shall be issued to ensure no device reference will
833  * be leaked in a long lived adapter,
834  * as only its reference within connected-devices and discovered-devices are removed at disconnect.
835  * </p>
836  * <p>
837  * After calling this method, this instance is destroyed and shall not be used anymore!
838  * </p>
839  * <p>
840  * This method is an atomic operation.
841  * </p>
842  * <p>
843  * An application using one thread per device and rapid connect, should either use disconnect() or remove(),
844  * but never issue remove() after disconnect() if the device is in use.
845  * </p>
846  * @see BTAdapter::removeAllStatusListener()
847  */
848  void remove() noexcept;
849 
850  /** Returns the connected GATTHandler or nullptr, see connectGATT(), getGattService() and disconnect(). */
851  std::shared_ptr<BTGattHandler> getGattHandler() noexcept;
852 
853  /**
854  * Returns a list of shared GATTService available on this device if successful,
855  * otherwise returns an empty list if an error occurred.
856  * <p>
857  * The HCI connectLE(..) or connectBREDR(..) must be performed first, see {@link #connectDefault()}.
858  * </p>
859  * <p>
860  * If this method has been called for the first time or no services has been detected yet,
861  * a list of GATTService will be discovered.
862  * <br>
863  * In case no GATT connection has been established it will be created via connectGATT().
864  * </p>
865  */
866  jau::darray<std::shared_ptr<BTGattService>> getGattServices() noexcept;
867 
868  /**
869  * Returns the matching GATTService for the given uuid.
870  * <p>
871  * Implementation calls getGattService().
872  * </p>
873  */
874  std::shared_ptr<BTGattService> findGattService(std::shared_ptr<uuid_t> const &uuid);
875 
876  /** Returns the shared GenericAccess instance, retrieved by {@link #getGattService()} or nullptr if not available. */
877  std::shared_ptr<GattGenericAccessSvc> getGattGenericAccess();
878 
879  /**
880  * Issues a GATT ping to the device, validating whether it is still reachable.
881  * <p>
882  * This method could be periodically utilized to shorten the underlying OS disconnect period
883  * after turning the device off, which lies within 7-13s.
884  * </p>
885  * <p>
886  * In case the device is no more reachable, the GATTHandler will initiate disconnect due to the occurring IO error.
887  * A disconnect will finally being issued.
888  * </p>
889  * <p>
890  * GATT services must have been initialized via {@link #getGattService()}, otherwise `false` is being returned.
891  * </p>
892  * @return `true` if successful, otherwise false in case no GATT services exists or is not connected .. etc.
893  */
894  bool pingGATT() noexcept;
895 
896  /**
897  * Add the given BTGattCharListener to the listener list if not already present.
898  * <p>
899  * Convenience delegation call to GATTHandler
900  * </p>
901  * <p>
902  * To enable the actual BLE notification and/or indication, one needs to call
903  * BTGattChar::configNotificationIndication(bool, bool, bool[])
904  * or BTGattChar::enableNotificationOrIndication(bool enabledState[2]).
905  * </p>
906  * @param listener A BTGattCharListener instance, listening to all BluetoothGattCharacteristic events of this device
907  * @return true if the given listener is not element of the list and has been newly added, otherwise false.
908  * @throws IllegalStateException if the GATTHandler is null, i.e. not connected
909  */
910  bool addCharListener(std::shared_ptr<BTGattCharListener> l);
911 
912  /**
913  * Remove the given {@link BTGattCharListener} from the listener list.
914  * <p>
915  * If the GATTHandler is null, i.e. not connected, `false` is being returned.
916  * </p>
917  * @param listener A {@link BTGattCharListener} instance
918  * @return true if the given listener is an element of the list and has been removed, otherwise false.
919  */
920  bool removeCharListener(std::shared_ptr<BTGattCharListener> l) noexcept;
921 
922  /**
923  * Remove all {@link BTGattCharListener} from the list, which are associated to the given {@link BTGattChar}.
924  * <p>
925  * Implementation tests all listener's BTGattCharListener::match(const BTGattChar & characteristic)
926  * to match with the given associated characteristic.
927  * </p>
928  * @param associatedCharacteristic the match criteria to remove any BTGattCharListener from the list
929  * @return number of removed listener.
930  */
931  int removeAllAssociatedCharListener(std::shared_ptr<BTGattChar> associatedCharacteristic) noexcept;
932 
933  int removeAllAssociatedCharListener(const BTGattChar * associatedCharacteristic) noexcept;
934 
935  /**
936  * Remove all {@link BTGattCharListener} from the list.
937  * @return number of removed listener.
938  */
939  int removeAllCharListener() noexcept;
940  };
941 
942  inline bool operator==(const BTDevice& lhs, const BTDevice& rhs) noexcept
943  { return lhs.getAddressAndType() == rhs.getAddressAndType(); }
944 
945  inline bool operator!=(const BTDevice& lhs, const BTDevice& rhs) noexcept
946  { return !(lhs == rhs); }
947 
948 } // namespace direct_bt
949 
950 #endif /* BT_DEVICE_HPP_ */
direct_bt::SMPIOCapability::UNSET
@ UNSET
Denoting unset value, i.e.
direct_bt::BTDevice::connectDefault
HCIStatusCode connectDefault() noexcept
Establish a default HCI connection to this device, using certain default parameter.
Definition: BTDevice.cpp:532
direct_bt::BTDevice::getPairingMode
PairingMode getPairingMode() const noexcept
Returns the current PairingMode used by the device.
Definition: BTDevice.cpp:1373
BTGattHandler.hpp
direct_bt::BTDevice::remove
void remove() noexcept
Disconnects this device via disconnect(..) if getConnected()==true and explicitly removes its shared ...
Definition: BTDevice.cpp:1784
direct_bt::BTDevice::getPairingState
SMPPairingState getPairingState() const noexcept
Returns the current SMPPairingState.
Definition: BTDevice.cpp:1378
direct_bt::BTDevice::setConnSecurityAuto
bool setConnSecurityAuto(const SMPIOCapability iocap_auto) noexcept
Set automatic security negotiation of BTSecurityLevel and SMPIOCapability pairing mode.
Definition: BTDevice.cpp:1274
darray.hpp
direct_bt::BTDevice::getSignatureResolvingKeyInfo
SMPSignatureResolvingKeyInfo getSignatureResolvingKeyInfo(const bool responder) const noexcept
Returns a copy of the Signature Resolving Key (LTK) info, valid after connection and SMP pairing has ...
Definition: BTDevice.cpp:1144
le_scan_interval
static const uint16_t le_scan_interval
Definition: dbt_scanner10.cpp:633
direct_bt::HCIACLData::l2cap_frame
Representing ACL Datas' L2CAP Frame.
Definition: HCITypes.hpp:672
direct_bt::EIRDataType
EIRDataType
Bit mask of 'Extended Inquiry Response' (EIR) data fields, indicating a set of related data.
Definition: BTTypes0.hpp:752
direct_bt::BTDevice::getTxPower
int8_t getTxPower() const noexcept
Return Tx Power of device as recognized at discovery and connect.
Definition: BTDevice.hpp:284
direct_bt::BTDevice::getLongTermKeyInfo
SMPLongTermKeyInfo getLongTermKeyInfo(const bool responder) const noexcept
Returns a copy of the Long Term Key (LTK) info, valid after connection and SMP pairing has been compl...
Definition: BTDevice.cpp:1116
direct_bt::SMPKeyType
SMPKeyType
SMP Key Type for Distribution, indicates keys distributed in the Transport Specific Key Distribution ...
Definition: SMPTypes.hpp:385
direct_bt::EInfoReport
Collection of 'Extended Advertising Data' (EAD), 'Advertising Data' (AD) or 'Extended Inquiry Respons...
Definition: BTTypes0.hpp:803
direct_bt::BTDevice::~BTDevice
~BTDevice() noexcept
Releases this instance after calling remove().
Definition: BTDevice.cpp:82
direct_bt::BTDevice::addCharListener
bool addCharListener(std::shared_ptr< BTGattCharListener > l)
Add the given BTGattCharListener to the listener list if not already present.
Definition: BTDevice.cpp:1599
direct_bt
Definition: ATTPDUTypes.hpp:171
direct_bt::BTGattChar
Definition: BTGattChar.hpp:75
direct_bt::BTDevice::setConnSecurity
bool setConnSecurity(const BTSecurityLevel sec_level, const SMPIOCapability io_cap) noexcept
Sets the given BTSecurityLevel and SMPIOCapability used to connect to this device on the upcoming con...
Definition: BTDevice.cpp:1237
direct_bt::BTDevice::getConnectedLE_PHY
HCIStatusCode getConnectedLE_PHY(LE_PHYs &resRx, LE_PHYs &resTx) noexcept
Request and return LE_PHYs bit for the given connection.
Definition: BTDevice.cpp:1648
direct_bt::BTDevice::setPairingNumericComparison
HCIStatusCode setPairingNumericComparison(const bool equal) noexcept
Method sets the numeric comparison result, see PairingMode::NUMERIC_COMPARE_ini.
Definition: BTDevice.cpp:1350
direct_bt::BTDevice::getLastDiscoveryTimestamp
uint64_t getLastDiscoveryTimestamp() const noexcept
Returns the timestamp in monotonic milliseconds when this device instance has discovered or connected...
Definition: BTDevice.hpp:260
direct_bt::BTDevice::pingGATT
bool pingGATT() noexcept
Issues a GATT ping to the device, validating whether it is still reachable.
Definition: BTDevice.cpp:1575
direct_bt::BTDevice::connectLE
HCIStatusCode connectLE(const uint16_t le_scan_interval=24, const uint16_t le_scan_window=24, const uint16_t conn_interval_min=12, const uint16_t conn_interval_max=12, const uint16_t conn_latency=0, const uint16_t supervision_timeout=getHCIConnSupervisorTimeout(0, 15)) noexcept
Establish a HCI BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM connection to this device.
Definition: BTDevice.cpp:283
BTTypes1.hpp
direct_bt::L2CAPComm
Read/Write L2CAP communication channel.
Definition: L2CAPComm.hpp:113
direct_bt::BTDevice::getManufactureSpecificData
std::shared_ptr< ManufactureSpecificData > const getManufactureSpecificData() const noexcept
Return shared ManufactureSpecificData as recognized at discovery, pre GATT discovery.
Definition: BTDevice.cpp:130
jau
Definition: basic_algos.hpp:34
direct_bt::BTDevice::getConnIOCapability
SMPIOCapability getConnIOCapability() const noexcept
Return the set SMPIOCapability value, determined when the connection is established.
Definition: BTDevice.cpp:1232
direct_bt::BTDevice::getLastUpdateAge
uint64_t getLastUpdateAge(const uint64_t ts_now) const noexcept
Definition: BTDevice.hpp:272
direct_bt::BTDevice::getCreationTimestamp
uint64_t getCreationTimestamp() const noexcept
Returns the timestamp in monotonic milliseconds when this device instance has been created,...
Definition: BTDevice.hpp:253
direct_bt::BTGattService
Representing a complete [Primary] Service Declaration including its list of Characteristic Declaratio...
Definition: BTGattService.hpp:67
direct_bt::BTDevice::connectBREDR
HCIStatusCode connectBREDR(const uint16_t pkt_type=HCI_DM1|HCI_DM3|HCI_DM5|HCI_DH1|HCI_DH3|HCI_DH5, const uint16_t clock_offset=0x0000, const uint8_t role_switch=0x01) noexcept
Establish a HCI BDADDR_BREDR connection to this device.
Definition: BTDevice.cpp:492
direct_bt::BTDevice::toString
std::string toString() const noexcept override
Definition: BTDevice.hpp:303
direct_bt::BTDevice::getAddressAndType
constexpr BDAddressAndType const & getAddressAndType() const noexcept
Returns the unique device EUI48 address and BDAddressType type.
Definition: BTDevice.hpp:278
direct_bt::SMPOOBDataFlag
SMPOOBDataFlag
Vol 3, Part H, 2.3.3 OOB authentication data.
Definition: SMPTypes.hpp:223
direct_bt::BTSecurityLevel
BTSecurityLevel
Bluetooth Security Level.
Definition: BTTypes0.hpp:211
direct_bt::MgmtEvent
uint16_t opcode, uint16_t dev-id, uint16_t param_size
Definition: MgmtTypes.hpp:1083
direct_bt::BTDevice::getAppearance
AppearanceCat getAppearance() const noexcept
Return AppearanceCat of device as recognized at discovery, connect and GATT discovery.
Definition: BTDevice.hpp:287
direct_bt::HCIStatusCode
HCIStatusCode
BT Core Spec v5.2: Vol 1, Part F Controller Error Codes: 1.3 List of Error Codes.
Definition: HCITypes.hpp:124
direct_bt::BTDevice::getAvailableSMPKeys
SMPKeyType getAvailableSMPKeys(const bool responder) const noexcept
Returns the available SMPKeyType mask for the responder (LL slave) or initiator (LL master).
Definition: BTDevice.cpp:1107
jau::ordered_atomic
std::atomic<T> type with predefined fixed std::memory_order, not allowing changing the memory model o...
Definition: ordered_atomic.hpp:51
jau::darray
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:102
direct_bt::BTDevice::removeCharListener
bool removeCharListener(std::shared_ptr< BTGattCharListener > l) noexcept
Remove the given BTGattCharListener from the listener list.
Definition: BTDevice.cpp:1608
SMPHandler.hpp
direct_bt::BTDevice::setConnSecurityLevel
bool setConnSecurityLevel(const BTSecurityLevel sec_level) noexcept
Set the BTSecurityLevel used to connect to this device on the upcoming connection.
Definition: BTDevice.cpp:1182
direct_bt::BTDevice::getGattGenericAccess
std::shared_ptr< GattGenericAccessSvc > getGattGenericAccess()
Returns the shared GenericAccess instance, retrieved by getGattService() or nullptr if not available.
Definition: BTDevice.cpp:1590
direct_bt::BTDevice::removeAllAssociatedCharListener
int removeAllAssociatedCharListener(std::shared_ptr< BTGattChar > associatedCharacteristic) noexcept
Remove all BTGattCharListener from the list, which are associated to the given BTGattChar.
Definition: BTDevice.cpp:1618
direct_bt::LE_Features
LE_Features
HCI Supported Commands.
Definition: BTTypes0.hpp:106
direct_bt::BTGattCharListener
BTGattChar event listener for notification and indication events.
Definition: BTGattChar.hpp:435
le_scan_window
static const uint16_t le_scan_window
Definition: dbt_scanner10.cpp:634
direct_bt::BTDevice::setPairingPasskey
HCIStatusCode setPairingPasskey(const uint32_t passkey) noexcept
Method sets the given passkey entry, see PairingMode::PASSKEY_ENTRY_ini.
Definition: BTDevice.cpp:1304
direct_bt::GattGenericAccessSvc
Generic Access Service is a mandatory GATT service all peripherals are required to implement.
Definition: GattNumbers.hpp:264
direct_bt::BTDevice::operator=
void operator=(const BTDevice &)=delete
JAVA_DBT_PACKAGE
#define JAVA_DBT_PACKAGE
Definition: BTTypes1.hpp:39
direct_bt::BTObject
Definition: BTTypes1.hpp:48
direct_bt::BTDevice::disconnect
HCIStatusCode disconnect(const HCIStatusCode reason=HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION) noexcept
Disconnect the LE or BREDR peer's GATT and HCI connection.
Definition: BTDevice.cpp:1701
direct_bt::BTDevice::removeStatusListener
bool removeStatusListener(std::shared_ptr< AdapterStatusListener > l)
Remove the given listener from the list.
Definition: BTDevice.cpp:252
direct_bt::ManufactureSpecificData
Definition: BTTypes0.hpp:705
direct_bt::SMPPDUMsg
Handles the Security Manager Protocol (SMP) using Protocol Data Unit (PDU) encoded messages over L2CA...
Definition: SMPTypes.hpp:644
direct_bt::BTDevice::ts_creation
const uint64_t ts_creation
Definition: BTDevice.hpp:220
direct_bt::SMPSignatureResolvingKeyInfo
SMP Signature Resolving Key Info, used for platform agnostic persistence.
Definition: SMPTypes.hpp:598
direct_bt::BTSecurityLevel::UNSET
@ UNSET
Security Level not set, value 0.
direct_bt::PairingMode
PairingMode
Bluetooth secure pairing mode.
Definition: BTTypes0.hpp:261
direct_bt::BTDevice::removeAllCharListener
int removeAllCharListener() noexcept
Remove all BTGattCharListener from the list.
Definition: BTDevice.cpp:1638
direct_bt::BTDevice::setConnSecurityBest
bool setConnSecurityBest(const BTSecurityLevel sec_level, const SMPIOCapability io_cap) noexcept
Convenience method to determine the best practice BTSecurityLevel and SMPIOCapability based on the gi...
Definition: BTDevice.cpp:1258
direct_bt::BTDevice::getConnectionInfo
std::shared_ptr< ConnectionInfo > getConnectionInfo() noexcept
Retrieves the current connection info for this device and returns the ConnectionInfo reference if suc...
Definition: BTDevice.cpp:256
direct_bt::BTDevice::addressAndType
const BDAddressAndType addressAndType
Device's unique mac address and type tuple.
Definition: BTDevice.hpp:222
direct_bt::AppearanceCat
AppearanceCat
Definition: BTTypes0.hpp:637
direct_bt::BTGattHandler
A thread safe GATT handler associated to one device via one L2CAP connection.
Definition: BTGattHandler.hpp:140
direct_bt::BTDevice::isConnSecurityAutoEnabled
bool isConnSecurityAutoEnabled() const noexcept
Returns true if automatic security negotiation has been enabled via setConnSecurityAuto(),...
Definition: BTDevice.cpp:1299
direct_bt::SMPAuthReqs
SMPAuthReqs
SMP Authentication Requirements Bits, denotes specific bits or whole protocol uint8_t bit-mask.
Definition: SMPTypes.hpp:253
direct_bt::BTDevice::java_class
static std::string java_class() noexcept
Definition: BTDevice.hpp:238
direct_bt::getHCIConnSupervisorTimeout
constexpr int32_t getHCIConnSupervisorTimeout(const uint16_t conn_latency, const uint16_t conn_interval_max_ms, const uint16_t min_result_ms=number(HCIConstInt::LE_CONN_MIN_TIMEOUT_MS), const uint16_t multiplier=10) noexcept
Defining the supervising timeout for LE connections to be a multiple of the maximum connection interv...
Definition: HCITypes.hpp:97
direct_bt::BTDevice::getAdvertisedServices
jau::darray< std::shared_ptr< uuid_t > > getAdvertisedServices() const noexcept
Return a list of advertised services as recognized at discovery, pre GATT discovery.
Definition: BTDevice.cpp:136
direct_bt::BTAdapter
BTAdapter represents one Bluetooth Controller.
Definition: BTAdapter.hpp:250
direct_bt::BTDevice::getSharedInstance
std::shared_ptr< BTDevice > getSharedInstance() const noexcept
Returns the shared pointer of this instance managed by the adapter.
Definition: BTDevice.cpp:90
direct_bt::BTDevice::get_java_class
std::string get_java_class() const noexcept override
Definition: BTDevice.hpp:235
MgmtTypes.hpp
direct_bt::EUI48
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
Definition: BTAddress.hpp:388
direct_bt::BTDevice::setConnIOCapability
bool setConnIOCapability(const SMPIOCapability io_cap) noexcept
Sets the given SMPIOCapability used to connect to this device on the upcoming connection.
Definition: BTDevice.cpp:1209
direct_bt::SMPIOCapability
SMPIOCapability
Vol 3, Part H, 2.3.2 IO capabilities.
Definition: SMPTypes.hpp:185
direct_bt::BTDevice::BTDevice
BTDevice(const BTDevice::ctor_cookie &cc, BTAdapter &adapter, EInfoReport const &r)
Private ctor for private BTDevice::make_shared() intended for friends.
Definition: BTDevice.cpp:47
direct_bt::BTDevice::getGattServices
jau::darray< std::shared_ptr< BTGattService > > getGattServices() noexcept
Returns a list of shared GATTService available on this device if successful, otherwise returns an emp...
Definition: BTDevice.cpp:1525
direct_bt::BTDevice::setPairingPasskeyNegative
HCIStatusCode setPairingPasskeyNegative() noexcept
Method replies with a negative passkey response, i.e.
Definition: BTDevice.cpp:1327
direct_bt::BTDevice::addStatusListener
bool addStatusListener(std::shared_ptr< AdapterStatusListener > l)
Add the given AdapterStatusListener to the list if not already present, intended to listen only for e...
Definition: BTDevice.cpp:248
direct_bt::uuid_t
Definition: UUID.hpp:52
direct_bt::AppearanceCat::UNKNOWN
@ UNKNOWN
jau::uint128_t
Definition: int_types.hpp:83
direct_bt::BTDevice::getGattHandler
std::shared_ptr< BTGattHandler > getGattHandler() noexcept
Returns the connected GATTHandler or nullptr, see connectGATT(), getGattService() and disconnect().
Definition: BTDevice.cpp:1520
direct_bt::BTDevice::getConnected
bool getConnected() noexcept
Return true if the device has been successfully connected, otherwise false.
Definition: BTDevice.hpp:359
direct_bt::BTDevice::getRSSI
int8_t getRSSI() const noexcept
Return RSSI of device as recognized at discovery and connect.
Definition: BTDevice.hpp:281
direct_bt::BTDevice::getName
std::string const getName() const noexcept
Definition: BTDevice.cpp:124
HCIComm.hpp
direct_bt::BTDevice::getLastUpdateTimestamp
uint64_t getLastUpdateTimestamp() const noexcept
Returns the timestamp in monotonic milliseconds when this device instance underlying data has been up...
Definition: BTDevice.hpp:267
direct_bt::BTDevice::pair
HCIStatusCode pair(const SMPIOCapability io_cap) noexcept
Experimental only.
Definition: BTDevice.cpp:1149
direct_bt::operator!=
bool operator!=(const EUI48Sub &lhs, const EUI48Sub &rhs) noexcept
Definition: BTAddress.hpp:275
direct_bt::BTDevice::unpair
HCIStatusCode unpair() noexcept
Unpairs this device from the adapter while staying connected.
Definition: BTDevice.cpp:1772
direct_bt::SMPPairingState
SMPPairingState
SMP Pairing Process state definition.
Definition: SMPTypes.hpp:101
direct_bt::BTDevice::getConnSecurityLevel
BTSecurityLevel getConnSecurityLevel() const noexcept
Return the BTSecurityLevel, determined when the connection is established.
Definition: BTDevice.cpp:1204
direct_bt::HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION
@ REMOTE_USER_TERMINATED_CONNECTION
direct_bt::BTDevice::setLongTermKeyInfo
HCIStatusCode setLongTermKeyInfo(const SMPLongTermKeyInfo &ltk) noexcept
Sets the long term ket (LTK) info of this device to reuse pre-paired encryption.
Definition: BTDevice.cpp:1121
direct_bt::LE_PHYs
LE_PHYs
LE Transport PHY bit values.
Definition: BTTypes0.hpp:175
direct_bt::BDAddressAndType
Unique Bluetooth EUI48 address and BDAddressType tuple.
Definition: BTAddress.hpp:417
direct_bt::BTDevice
Definition: BTDevice.hpp:57
direct_bt::BTDevice::BTDevice
BTDevice(const BTDevice &)=delete
direct_bt::BTDevice::getAdapter
BTAdapter & getAdapter() const
Returns the managing adapter.
Definition: BTDevice.hpp:243
direct_bt::BTDevice::getConnectionHandle
uint16_t getConnectionHandle() const noexcept
Return the HCI connection handle to the LE or BREDR peer, zero if not connected.
Definition: BTDevice.hpp:466
direct_bt::BTDevice::findGattService
std::shared_ptr< BTGattService > findGattService(std::shared_ptr< uuid_t > const &uuid)
Returns the matching GATTService for the given uuid.
Definition: BTDevice.cpp:1563
direct_bt::SMPLongTermKeyInfo
SMP Long Term Key Info, used for platform agnostic persistence.
Definition: SMPTypes.hpp:522