Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
BTManager.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 DBT_MANAGER_HPP_
27 #define DBT_MANAGER_HPP_
28 
29 #include <cstring>
30 #include <string>
31 #include <cstdint>
32 
33 #include <mutex>
34 #include <atomic>
35 #include <thread>
36 
37 #include <jau/environment.hpp>
38 #include <jau/ringbuffer.hpp>
39 #include <jau/java_uplink.hpp>
40 #include <jau/darray.hpp>
41 #include <jau/cow_darray.hpp>
42 
43 #include "BTTypes0.hpp"
44 #include "BTIoctl.hpp"
45 #include "OctetTypes.hpp"
46 #include "HCIComm.hpp"
47 #include "MgmtTypes.hpp"
48 #include "BTAdapter.hpp"
49 
50 namespace direct_bt {
51 
52  class BTManager; // forward
53 
54  /**
55  * Managment Singleton runtime environment properties
56  * <p>
57  * Also see {@link DBTEnv::getExplodingProperties(const std::string & prefixDomain)}.
58  * </p>
59  */
60  class MgmtEnv : public jau::root_environment {
61  friend class BTManager;
62 
63  private:
64  MgmtEnv() noexcept;
65 
66  public:
67  /** Global Debug flag, retrieved first to triggers DBTEnv initialization. */
68  const bool DEBUG_GLOBAL;
69 
70  private:
71  const bool exploding; // just to trigger exploding properties
72  static BTMode getEnvBTMode();
73 
74  public:
75  /**
76  * Poll timeout for mgmt reader thread, defaults to 10s.
77  * <p>
78  * Environment variable is 'direct_bt.mgmt.reader.timeout'.
79  * </p>
80  */
82 
83  /**
84  * Timeout for mgmt command replies, defaults to 3s.
85  * <p>
86  * Environment variable is 'direct_bt.mgmt.cmd.timeout'.
87  * </p>
88  */
90 
91  /**
92  * Small ringbuffer capacity for synchronized commands, defaults to 64 messages.
93  * <p>
94  * Environment variable is 'direct_bt.mgmt.ringsize'.
95  * </p>
96  */
97  const int32_t MGMT_EVT_RING_CAPACITY;
98 
99  /**
100  * Debug all Mgmt event communication
101  * <p>
102  * Environment variable is 'direct_bt.debug.mgmt.event'.
103  * </p>
104  */
105  const bool DEBUG_EVENT;
106 
107  /**
108  * Default {@link BTMode} when initializing new adapter
109  * <p>
110  * Environment variable is 'direct_bt.mgmt.btmode' first, then try 'org.tinyb.btmode'.
111  * </p>
112  * <p>
113  * Default is BTMode::DUAL, if non of the above environment variable is set.
114  * </p>
115  */
117 
118  private:
119  /** Maximum number of packets to wait for until matching a sequential command. Won't block as timeout will limit. */
120  const int32_t MGMT_READ_PACKET_MAX_RETRY;
121 
122  public:
123  static MgmtEnv& get() noexcept {
124  /**
125  * Thread safe starting with C++11 6.7:
126  *
127  * If control enters the declaration concurrently while the variable is being initialized,
128  * the concurrent execution shall wait for completion of the initialization.
129  *
130  * (Magic Statics)
131  *
132  * Avoiding non-working double checked locking.
133  */
134  static MgmtEnv e;
135  return e;
136  }
137  };
138 
139  /**
140  * Callback function to receive change events regarding the system's adapter set,
141  * e.g. a removed or added adapter due to user interaction or 'cold reset'.
142  * <p>
143  * When a new callback is added, all available adapter's will be reported as added,
144  * this allows a fully event driven workflow.
145  * </p>
146  * <p>
147  * The callback is performed on a dedicated thread,
148  * allowing the user to perform complex operations.
149  * </p>
150  * <p>
151  * If an adapter is being removed from the system,
152  * DBTAdapter::close() is being called by BTManager after issuing all
153  * ChangedAdapterSetFunc calls.
154  * </p>
155  *
156  * @param added true if adapter was newly added, otherwise removed from system
157  * @param adapter the shared DBTAdapter reference
158  * @return ignored
159  * @see ChangedAdapterSetCallback
160  * @see BTManager::addChangedAdapterSetCallback()
161  * @see BTManager::removeChangedAdapterSetCallback()
162  */
163  typedef bool (*ChangedAdapterSetFunc)(bool added, std::shared_ptr<BTAdapter>& adapter);
164 
165  /**
166  * Callback jau::FunctionDef to receive change events regarding the system's adapter set,
167  * e.g. a removed or added adapter due to user interaction or 'cold reset'.
168  * <p>
169  * When a new callback is added, all available adapter's will be reported as added,
170  * this allows a fully event driven workflow.
171  * </p>
172  * <p>
173  * The callback is performed on a dedicated thread,
174  * allowing the user to perform complex operations.
175  * </p>
176  * <p>
177  * If an adapter is being removed from the system,
178  * DBTAdapter::close() is being called by BTManager after issuing all
179  * ChangedAdapterSetFunc calls.
180  * </p>
181  *
182  * @param added true if adapter was newly added, otherwise removed from system
183  * @param adapter the shared DBTAdapter reference
184  * @return ignored
185  * @see ChangedAdapterSetFunc
186  * @see BTManager::addChangedAdapterSetCallback()
187  * @see BTManager::removeChangedAdapterSetCallback()
188  */
191 
192  /**
193  * A thread safe singleton handler of the Linux Kernel's BlueZ manager control channel.
194  * <p>
195  * Implementation utilizes a lock free ringbuffer receiving data within its separate thread.
196  * </p>
197  * <p>
198  * Controlling Environment variables, see {@link MgmtEnv}.
199  * </p>
200  */
201  class BTManager : public jau::JavaUplink {
202  public:
203  enum Defaults : int32_t {
204  /* BT Core Spec v5.2: Vol 3, Part F 3.2.8: Maximum length of an attribute value. */
205  ClientMaxMTU = 512
206  };
207 
208  static const pid_t pidSelf;
209 
210  private:
211  friend BTAdapter::~BTAdapter() noexcept;
212 
213 #if USE_LINUX_BT_SECURITY
214  /** Default initialization with ::SMPIOCapability::NO_INPUT_NO_OUTPUT for PairingMode::JUST_WORKS. */
215  constexpr static const SMPIOCapability defaultIOCapability = SMPIOCapability::NO_INPUT_NO_OUTPUT;
216 #else
217  /** Default initialization with ::SMPIOCapability::NO_INPUT_NO_OUTPUT for PairingMode::JUST_WORKS. */
218  constexpr static const SMPIOCapability defaultIOCapability = SMPIOCapability::UNSET;
219 #endif
220  static std::mutex mtx_singleton;
221 
222  struct WhitelistElem {
223  uint16_t dev_id;
224  BDAddressAndType address_and_type;
226 
227  WhitelistElem(uint16_t dev_id_, BDAddressAndType address_and_type_, HCIWhitelistConnectType ctype_)
228  : dev_id(dev_id_), address_and_type(address_and_type_), ctype(ctype_) { }
229  };
231 
232  const MgmtEnv & env;
233  const BTMode defaultBTMode;
234 
235  POctets rbuffer;
236  HCIComm comm;
237 
238  jau::ringbuffer<std::unique_ptr<MgmtEvent>, std::nullptr_t, jau::nsize_t> mgmtEventRing;
239  jau::sc_atomic_bool mgmtReaderShallStop;
240 
241  std::mutex mtx_mgmtReaderLifecycle;
242  std::condition_variable cv_mgmtReaderInit;
243  pthread_t mgmtReaderThreadId;
244  jau::relaxed_atomic_bool mgmtReaderRunning;
245 
246  std::recursive_mutex mtx_sendReply; // for send() and sendWithReply()
247 
248  jau::sc_atomic_bool allowClose;
249 
250  /** One MgmtAdapterEventCallbackList per event type, allowing multiple callbacks to be invoked for each event */
251  std::array<MgmtAdapterEventCallbackList, static_cast<uint16_t>(MgmtEvent::Opcode::MGMT_EVENT_TYPE_COUNT)> mgmtAdapterEventCallbackLists;
252  inline bool isValidMgmtEventCallbackListsIndex(const MgmtEvent::Opcode opc) const noexcept {
253  return static_cast<uint16_t>(opc) < mgmtAdapterEventCallbackLists.size();
254  }
255 
256  ChangedAdapterSetCallbackList mgmtChangedAdapterSetCallbackList;
257 
258  typedef jau::cow_darray<std::shared_ptr<BTAdapter>> adapters_t;
259  adapters_t adapters;
260 
261  /**
262  * Using defaultIOCapability on added AdapterInfo.
263  * Sharing same dev_id <-> index mapping of adapterInfos using findAdapterInfoIndex().
264  * Piggy back reusing adapterInfos.get_write_mutex().
265  */
266  jau::darray<SMPIOCapability> adapterIOCapability;
267 
268  void mgmtReaderThreadImpl() noexcept;
269 
270  /**
271  * In case response size check or devID and optional opcode validation fails,
272  * function returns NULL.
273  */
274  std::unique_ptr<MgmtEvent> sendWithReply(MgmtCommand &req) noexcept;
275 
276  bool send(MgmtCommand &req) noexcept;
277 
278  /**
279  * Instantiate singleton.
280  * @param btMode default {@link BTMode} when initializing new adapter. If BTMode::NONE given, MgmtEnv::DEFAULT_BTMODE is being used.
281  */
282  BTManager(const BTMode defaultBTMode) noexcept;
283 
284  BTManager(const BTManager&) = delete;
285  void operator=(const BTManager&) = delete;
286 
287  void setAdapterMode(const uint16_t dev_id, const uint8_t ssp, const uint8_t bredr, const uint8_t le) noexcept;
288  std::unique_ptr<AdapterInfo> initAdapter(const uint16_t dev_id, const BTMode btMode) noexcept;
289  void shutdownAdapter(BTAdapter& adapter) noexcept;
290 
291  void processAdapterAdded(std::unique_ptr<MgmtEvent> e) noexcept;
292  void processAdapterRemoved(std::unique_ptr<MgmtEvent> e) noexcept;
293  bool mgmtEvNewSettingsCB(const MgmtEvent& e) noexcept;
294  bool mgmtEventAnyCB(const MgmtEvent& e) noexcept;
295 
296  std::shared_ptr<BTAdapter> addAdapter(const AdapterInfo& ai) noexcept;
297 
298  /**
299  * Removes the AdapterInfo with the given dev_id
300  * @return the removed instance or nullptr if not found.
301  */
302  std::shared_ptr<BTAdapter> removeAdapter(const uint16_t dev_id) noexcept;
303 
304  /**
305  * Removal entry for DBTAdapter::~DBTAdapter
306  * @param adapter pointer to the dtor'ed adapter
307  * @return true if contained and removed, otherwise false
308  */
309  bool removeAdapter(BTAdapter* adapter) noexcept;
310 
311  public:
312  /**
313  * Retrieves the singleton instance.
314  * <p>
315  * First call will open and initialize the bluetooth kernel.
316  * </p>
317  * @param btMode default {@link BTMode} when initializing new adapter. If BTMode::NONE given (default), MgmtEnv::DEFAULT_BTMODE is being used.
318  * @return singleton instance.
319  */
320  static BTManager& get(const BTMode defaultBTMode=BTMode::NONE) {
321  /**
322  * Thread safe starting with C++11 6.7:
323  *
324  * If control enters the declaration concurrently while the variable is being initialized,
325  * the concurrent execution shall wait for completion of the initialization.
326  *
327  * (Magic Statics)
328  *
329  * Avoiding non-working double checked locking.
330  */
331  static BTManager s(defaultBTMode);
332  return s;
333  }
334  ~BTManager() noexcept { close(); }
335 
336  void close() noexcept;
337 
338  std::string get_java_class() const noexcept override {
339  return java_class();
340  }
341  static std::string java_class() noexcept {
342  return std::string(JAVA_DBT_PACKAGE "DBTManager");
343  }
344 
345  /** Returns the default {@link BTMode}, adapters are tried to be initialized. */
346  BTMode getDefaultBTMode() noexcept { return defaultBTMode; }
347 
348  /** Returns true if this mgmt instance is open and hence valid, otherwise false */
349  bool isOpen() const noexcept {
350  return comm.isOpen();
351  }
352 
353  std::string toString() const noexcept override {
354  return "MgmtHandler[BTMode "+to_string(defaultBTMode)+", "+std::to_string(adapters.size())+" adapter, "+javaObjectToString()+"]";
355  }
356 
357  /** retrieve information gathered at startup */
358 
359  /**
360  * Returns AdapterInfo count in list
361  */
362  int getAdapterCount() const noexcept { return adapters.size(); }
363 
364  /**
365  * Returns a list of currently added DBTAdapter.
366  */
367  jau::darray<std::shared_ptr<BTAdapter>> getAdapters() { return *adapters.snapshot(); }
368 
369  /**
370  * Returns the DBTAdapter with the given dev_id, or nullptr if not found.
371  */
372  std::shared_ptr<BTAdapter> getAdapter(const uint16_t dev_id) const noexcept;
373 
374  /**
375  * Returns the default AdapterInfo.
376  * <p>
377  * The default adapter is either the first AdapterSetting::POWERED adapter,
378  * or function returns nullptr if none is AdapterSetting::POWERED.
379  * </p>
380  */
381  std::shared_ptr<BTAdapter> getDefaultAdapter() const noexcept;
382 
383  bool setIOCapability(const uint16_t dev_id, const SMPIOCapability io_cap, SMPIOCapability& pre_io_cap) noexcept;
384  SMPIOCapability getIOCapability(const uint16_t dev_id) const noexcept;
385 
386  bool setMode(const uint16_t dev_id, const MgmtCommand::Opcode opc, const uint8_t mode, AdapterSetting& current_settings) noexcept;
387  MgmtStatus setDiscoverable(const uint16_t dev_id, const uint8_t state, const uint16_t timeout, AdapterSetting& current_settings) noexcept;
388 
389  /** Start discovery on given adapter dev_id with a ScanType matching the given BTMode. Returns set ScanType. */
390  ScanType startDiscovery(const uint16_t dev_id, const BTMode btMode) noexcept;
391  /** Start discovery on given adapter dev_id with given ScanType. Returns set ScanType. */
392  ScanType startDiscovery(const uint16_t dev_id, const ScanType type) noexcept;
393  /** Stop discovery on given adapter dev_id. */
394  bool stopDiscovery(const uint16_t dev_id, const ScanType type) noexcept;
395 
396  /**
397  * Uploads given connection parameter for given device to the kernel.
398  *
399  * @param dev_id
400  * @param address
401  * @param address_type
402  * @param conn_interval_min in units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]
403  * @param conn_interval_max in units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]
404  * @param conn_latency slave latency in units of connection events, default value 0; Value range [0 .. 0x01F3].
405  * @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].
406  * @return
407  */
408  bool uploadConnParam(const uint16_t dev_id, const BDAddressAndType & addressAndType,
409  const uint16_t conn_interval_min=12, const uint16_t conn_interval_max=12,
410  const uint16_t conn_latency=0, const uint16_t supervision_timeout=getHCIConnSupervisorTimeout(0, 15)) noexcept;
411 
412  /**
413  * Returns true, if the adapter's device is already whitelisted.
414  */
415  bool isDeviceWhitelisted(const uint16_t dev_id, const BDAddressAndType & addressAndType) noexcept;
416 
417  /**
418  * Add the given device to the adapter's autoconnect whitelist.
419  * <p>
420  * Make sure {@link uploadConnParam(..)} is invoked first, otherwise performance will lack.
421  * </p>
422  * <p>
423  * Method will reject duplicate devices, in which case it should be removed first.
424  * </p>
425  */
426  bool addDeviceToWhitelist(const uint16_t dev_id, const BDAddressAndType & addressAndType, const HCIWhitelistConnectType ctype) noexcept;
427 
428  /** Remove the given device from the adapter's autoconnect whitelist. */
429  bool removeDeviceFromWhitelist(const uint16_t dev_id, const BDAddressAndType & addressAndType) noexcept;
430 
431  /** Remove all previously added devices from the autoconnect whitelist. Returns number of removed devices. */
432  int removeAllDevicesFromWhitelist() noexcept;
433 
434  bool disconnect(const bool ioErrorCause,
435  const uint16_t dev_id, const BDAddressAndType & addressAndType,
436  const HCIStatusCode reason=HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION ) noexcept;
437 
438  std::shared_ptr<ConnectionInfo> getConnectionInfo(const uint16_t dev_id, const BDAddressAndType& addressAndType) noexcept;
439  std::shared_ptr<NameAndShortName> setLocalName(const uint16_t dev_id, const std::string & name, const std::string & short_name) noexcept;
440 
441  /** Security commands */
442 
443  MgmtStatus uploadLinkKey(const uint16_t dev_id, const bool debug_keys, const MgmtLinkKeyInfo &key) noexcept;
444 
445  HCIStatusCode uploadLongTermKey(const uint16_t dev_id, const MgmtLongTermKeyInfo &key) noexcept;
446  HCIStatusCode uploadLongTermKeyInfo(const uint16_t dev_id, const BDAddressAndType & addressAndType, const SMPLongTermKeyInfo& ltk) noexcept;
447 
448  MgmtStatus userPasskeyReply(const uint16_t dev_id, const BDAddressAndType & addressAndType, const uint32_t passkey) noexcept;
449  MgmtStatus userPasskeyNegativeReply(const uint16_t dev_id, const BDAddressAndType & addressAndType) noexcept;
450  MgmtStatus userConfirmReply(const uint16_t dev_id, const BDAddressAndType & addressAndType, const bool positive) noexcept;
451 
452  bool pairDevice(const uint16_t dev_id, const BDAddressAndType & addressAndType, const SMPIOCapability iocap) noexcept;
453  MgmtStatus unpairDevice(const uint16_t dev_id, const BDAddressAndType & addressAndType, const bool disconnect) noexcept;
454 
455  /** MgmtEventCallback handling */
456 
457  /**
458  * Appends the given MgmtEventCallback for the given adapter dev_id to the named MgmtEvent::Opcode list,
459  * if it is not present already (dev_id + opcode + callback).
460  * <p>
461  * The adapter dev_id allows filtering the events only directed to the given adapter.
462  * Use dev_id <code>-1</code> to receive the event for all adapter.
463  * </p>
464  * @param dev_id the associated adapter dev_id
465  * @param opc opcode index for callback list, the callback shall be added to
466  * @param cb the to be added callback
467  * @return true if newly added or already existing, false if given MgmtEvent::Opcode is out of supported range.
468  */
469  bool addMgmtEventCallback(const int dev_id, const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept;
470  /** Returns count of removed given MgmtEventCallback from the named MgmtEvent::Opcode list. */
471  int removeMgmtEventCallback(const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept;
472  /** Returns count of removed MgmtEventCallback from the named MgmtEvent::Opcode list matching the given adapter dev_id . */
473  int removeMgmtEventCallback(const int dev_id) noexcept;
474  /** Removes all MgmtEventCallbacks from the to the named MgmtEvent::Opcode list. */
475  void clearMgmtEventCallbacks(const MgmtEvent::Opcode opc) noexcept;
476  /** Removes all MgmtEventCallbacks from all MgmtEvent::Opcode lists. */
477  void clearAllCallbacks() noexcept;
478 
479  /** Manually send a MgmtEvent to all of its listeners. */
480  void sendMgmtEvent(const MgmtEvent& event) noexcept;
481 
482  /** ChangedAdapterSetCallback handling */
483 
484  /**
485  * Adds the given ChangedAdapterSetCallback to this manager.
486  * <p>
487  * When a new callback is added, all available adapter's will be reported as added,
488  * this allows a fully event driven workflow.
489  * </p>
490  * <p>
491  * The callback is performed on a dedicated thread,
492  * allowing the user to perform complex operations.
493  * </p>
494  */
496 
497  /**
498  * Remove the given ChangedAdapterSetCallback from this manager.
499  * @param l the to be removed element
500  * @return the number of removed elements
501  */
503 
504  /**
505  * Adds the given ChangedAdapterSetFunc to this manager.
506  * <p>
507  * When a new callback is added, all available adapter's will be reported as added,
508  * this allows a fully event driven workflow.
509  * </p>
510  * <p>
511  * The callback is performed on a dedicated thread,
512  * allowing the user to perform complex operations.
513  * </p>
514  */
516 
517  /**
518  * Remove the given ChangedAdapterSetFunc from this manager.
519  * @param l the to be removed element
520  * @return the number of removed elements
521  */
523  };
524 
525 } // namespace direct_bt
526 
527 #endif /* DBT_MANAGER_HPP_ */
528 
direct_bt::SMPIOCapability::UNSET
@ UNSET
Denoting unset value, i.e.
direct_bt::AdapterSetting
AdapterSetting
Adapter Setting Bits.
Definition: BTTypes1.hpp:136
darray.hpp
direct_bt::BTManager::getAdapterCount
int getAdapterCount() const noexcept
retrieve information gathered at startup
Definition: BTManager.hpp:362
direct_bt::BTManager::~BTManager
~BTManager() noexcept
Definition: BTManager.hpp:334
direct_bt::BTManager::startDiscovery
ScanType startDiscovery(const uint16_t dev_id, const BTMode btMode) noexcept
Start discovery on given adapter dev_id with a ScanType matching the given BTMode.
Definition: BTManager.cpp:775
direct_bt::MgmtEnv::MGMT_COMMAND_REPLY_TIMEOUT
const int32_t MGMT_COMMAND_REPLY_TIMEOUT
Timeout for mgmt command replies, defaults to 3s.
Definition: BTManager.hpp:89
direct_bt::MgmtCommand
Definition: MgmtTypes.hpp:371
direct_bt::MgmtEnv::get
static MgmtEnv & get() noexcept
Definition: BTManager.hpp:123
jau::root_environment
Base jau environment class, merely to tag all environment settings by inheritance and hence documenta...
Definition: environment.hpp:51
direct_bt::BTManager::setDiscoverable
MgmtStatus setDiscoverable(const uint16_t dev_id, const uint8_t state, const uint16_t timeout, AdapterSetting &current_settings) noexcept
Definition: BTManager.cpp:750
direct_bt::BTManager::disconnect
bool disconnect(const bool ioErrorCause, const uint16_t dev_id, const BDAddressAndType &addressAndType, const HCIStatusCode reason=HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION) noexcept
Definition: BTManager.cpp:1045
direct_bt
Definition: ATTPDUTypes.hpp:171
direct_bt::MgmtEvent::Opcode
Opcode
Definition: MgmtTypes.hpp:1085
direct_bt::HCIWhitelistConnectType
HCIWhitelistConnectType
HCI Whitelist connection type.
Definition: BTTypes0.hpp:415
direct_bt::BTManager::removeMgmtEventCallback
int removeMgmtEventCallback(const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept
Returns count of removed given MgmtEventCallback from the named MgmtEvent::Opcode list.
Definition: BTManager.cpp:1126
OctetTypes.hpp
direct_bt::ScanType
ScanType
Meta ScanType as derived from BTMode, with defined value mask consisting of BDAddressType bits.
Definition: BTTypes0.hpp:294
direct_bt::BTManager::close
void close() noexcept
Definition: BTManager.cpp:538
direct_bt::BTManager::toString
std::string toString() const noexcept override
Definition: BTManager.hpp:353
direct_bt::HCIComm::isOpen
bool isOpen() const noexcept
Definition: HCIComm.hpp:81
direct_bt::ConnectionInfo
mgmt_addr_info { EUI48, uint8_t type }, int8_t rssi, int8_t tx_power, int8_t max_tx_power;
Definition: BTTypes1.hpp:75
jau::to_string
PRAGMA_DISABLE_WARNING_POP constexpr_cxx20 std::string to_string(const endian &v) noexcept
Return std::string representation of the given jau::endian.
Definition: byte_util.hpp:198
direct_bt::SMPIOCapability::NO_INPUT_NO_OUTPUT
@ NO_INPUT_NO_OUTPUT
No input not output, value 3.
direct_bt::BTManager::pairDevice
bool pairDevice(const uint16_t dev_id, const BDAddressAndType &addressAndType, const SMPIOCapability iocap) noexcept
Definition: BTManager.cpp:937
jau::FunctionDef
Definition: function_def.hpp:309
direct_bt::MgmtEvent
uint16_t opcode, uint16_t dev-id, uint16_t param_size
Definition: MgmtTypes.hpp:1083
direct_bt::BTManager::removeAllDevicesFromWhitelist
int removeAllDevicesFromWhitelist() noexcept
Remove all previously added devices from the autoconnect whitelist.
Definition: BTManager.cpp:992
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
jau::ordered_atomic< bool, std::memory_order::memory_order_seq_cst >
jau::darray
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:102
cow_darray.hpp
direct_bt::BTManager::getDefaultBTMode
BTMode getDefaultBTMode() noexcept
Returns the default BTMode, adapters are tried to be initialized.
Definition: BTManager.hpp:346
direct_bt::MgmtStatus
MgmtStatus
Definition: MgmtTypes.hpp:77
direct_bt::BTManager::isDeviceWhitelisted
bool isDeviceWhitelisted(const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
Returns true, if the adapter's device is already whitelisted.
Definition: BTManager.cpp:962
direct_bt::MgmtLinkKeyInfo
Used for MgmtLoadLinkKeyCmd and MgmtEvtNewLinkKey.
Definition: MgmtTypes.hpp:193
BTAdapter.hpp
direct_bt::MgmtEnv::DEBUG_EVENT
const bool DEBUG_EVENT
Debug all Mgmt event communication.
Definition: BTManager.hpp:105
direct_bt::BTManager::getAdapters
jau::darray< std::shared_ptr< BTAdapter > > getAdapters()
Returns a list of currently added DBTAdapter.
Definition: BTManager.hpp:367
direct_bt::BTManager::addDeviceToWhitelist
bool addDeviceToWhitelist(const uint16_t dev_id, const BDAddressAndType &addressAndType, const HCIWhitelistConnectType ctype) noexcept
Add the given device to the adapter's autoconnect whitelist.
Definition: BTManager.cpp:973
direct_bt::BTManager::setIOCapability
bool setIOCapability(const uint16_t dev_id, const SMPIOCapability io_cap, SMPIOCapability &pre_io_cap) noexcept
Definition: BTManager.cpp:691
direct_bt::BTManager::java_class
static std::string java_class() noexcept
Definition: BTManager.hpp:341
direct_bt::NameAndShortName
Definition: BTTypes1.hpp:103
direct_bt::BTManager::removeDeviceFromWhitelist
bool removeDeviceFromWhitelist(const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
Remove the given device from the adapter's autoconnect whitelist.
Definition: BTManager.cpp:1019
JAVA_DBT_PACKAGE
#define JAVA_DBT_PACKAGE
Definition: BTTypes1.hpp:39
direct_bt::BTManager::ClientMaxMTU
@ ClientMaxMTU
Definition: BTManager.hpp:205
direct_bt::MgmtEnv::MGMT_EVT_RING_CAPACITY
const int32_t MGMT_EVT_RING_CAPACITY
Small ringbuffer capacity for synchronized commands, defaults to 64 messages.
Definition: BTManager.hpp:97
direct_bt::BTManager::isOpen
bool isOpen() const noexcept
Returns true if this mgmt instance is open and hence valid, otherwise false.
Definition: BTManager.hpp:349
direct_bt::BTManager::removeChangedAdapterSetCallback
int removeChangedAdapterSetCallback(const ChangedAdapterSetCallback &l)
Remove the given ChangedAdapterSetCallback from this manager.
Definition: BTManager.cpp:1238
direct_bt::BTManager::get
static BTManager & get(const BTMode defaultBTMode=BTMode::NONE)
Retrieves the singleton instance.
Definition: BTManager.hpp:320
BTTypes0.hpp
jau::ringbuffer
Ring buffer implementation, a.k.a circular buffer, exposing lock-free get*(..) and put*(....
Definition: ringbuffer.hpp:115
direct_bt::BTManager
A thread safe singleton handler of the Linux Kernel's BlueZ manager control channel.
Definition: BTManager.hpp:201
direct_bt::BTManager::clearAllCallbacks
void clearAllCallbacks() noexcept
Removes all MgmtEventCallbacks from all MgmtEvent::Opcode lists.
Definition: BTManager.cpp:1155
jau::cow_darray< ChangedAdapterSetCallback >
direct_bt::HCIComm
Read/Write HCI communication channel.
Definition: HCIComm.hpp:52
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::BTManager::getIOCapability
SMPIOCapability getIOCapability(const uint16_t dev_id) const noexcept
Definition: BTManager.cpp:714
direct_bt::to_string
std::string to_string(const BDAddressType type) noexcept
Definition: BTTypes0.cpp:129
direct_bt::BTManager::addChangedAdapterSetCallback
void addChangedAdapterSetCallback(const ChangedAdapterSetCallback &l)
ChangedAdapterSetCallback handling.
Definition: BTManager.cpp:1230
direct_bt::BTAdapter::~BTAdapter
~BTAdapter() noexcept
Releases this instance.
Definition: BTAdapter.cpp:238
direct_bt::MgmtEvent::Opcode::MGMT_EVENT_TYPE_COUNT
@ MGMT_EVENT_TYPE_COUNT
direct_bt::BTManager::sendMgmtEvent
void sendMgmtEvent(const MgmtEvent &event) noexcept
Manually send a MgmtEvent to all of its listeners.
Definition: BTManager.cpp:154
direct_bt::BTManager::clearMgmtEventCallbacks
void clearMgmtEventCallbacks(const MgmtEvent::Opcode opc) noexcept
Removes all MgmtEventCallbacks from the to the named MgmtEvent::Opcode list.
Definition: BTManager.cpp:1148
direct_bt::MgmtEnv::DEBUG_GLOBAL
const bool DEBUG_GLOBAL
Global Debug flag, retrieved first to triggers DBTEnv initialization.
Definition: BTManager.hpp:68
MgmtTypes.hpp
direct_bt::BTManager::get_java_class
std::string get_java_class() const noexcept override
Definition: BTManager.hpp:338
direct_bt::ChangedAdapterSetFunc
bool(* ChangedAdapterSetFunc)(bool added, std::shared_ptr< BTAdapter > &adapter)
Callback function to receive change events regarding the system's adapter set, e.g.
Definition: BTManager.hpp:163
direct_bt::BTManager::getConnectionInfo
std::shared_ptr< ConnectionInfo > getConnectionInfo(const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
Definition: BTManager.cpp:1072
direct_bt::BTManager::uploadLongTermKey
HCIStatusCode uploadLongTermKey(const uint16_t dev_id, const MgmtLongTermKeyInfo &key) noexcept
Definition: BTManager.cpp:834
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
direct_bt::BTManager::pidSelf
static const pid_t pidSelf
Definition: BTManager.hpp:208
direct_bt::BTManager::setMode
bool setMode(const uint16_t dev_id, const MgmtCommand::Opcode opc, const uint8_t mode, AdapterSetting &current_settings) noexcept
Definition: BTManager.cpp:724
direct_bt::SMPIOCapability
SMPIOCapability
Vol 3, Part H, 2.3.2 IO capabilities.
Definition: SMPTypes.hpp:185
ringbuffer.hpp
direct_bt::BTManager::stopDiscovery
bool stopDiscovery(const uint16_t dev_id, const ScanType type) noexcept
Stop discovery on given adapter dev_id.
Definition: BTManager.cpp:796
direct_bt::BTManager::uploadLinkKey
MgmtStatus uploadLinkKey(const uint16_t dev_id, const bool debug_keys, const MgmtLinkKeyInfo &key) noexcept
Security commands.
Definition: BTManager.cpp:819
direct_bt::BTManager::uploadLongTermKeyInfo
HCIStatusCode uploadLongTermKeyInfo(const uint16_t dev_id, const BDAddressAndType &addressAndType, const SMPLongTermKeyInfo &ltk) noexcept
Definition: BTManager.cpp:858
direct_bt::BTManager::uploadConnParam
bool uploadConnParam(const uint16_t dev_id, const BDAddressAndType &addressAndType, 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
Uploads given connection parameter for given device to the kernel.
Definition: BTManager.cpp:806
direct_bt::BTManager::getDefaultAdapter
std::shared_ptr< BTAdapter > getDefaultAdapter() const noexcept
Returns the default AdapterInfo.
Definition: BTManager.cpp:614
direct_bt::MgmtEnv::DEFAULT_BTMODE
const BTMode DEFAULT_BTMODE
Default BTMode when initializing new adapter.
Definition: BTManager.hpp:116
direct_bt::BTManager::unpairDevice
MgmtStatus unpairDevice(const uint16_t dev_id, const BDAddressAndType &addressAndType, const bool disconnect) noexcept
Definition: BTManager.cpp:946
HCIComm.hpp
direct_bt::MgmtEnv::MGMT_READER_THREAD_POLL_TIMEOUT
const int32_t MGMT_READER_THREAD_POLL_TIMEOUT
Poll timeout for mgmt reader thread, defaults to 10s.
Definition: BTManager.hpp:81
direct_bt::ChangedAdapterSetCallbackList
jau::cow_darray< ChangedAdapterSetCallback > ChangedAdapterSetCallbackList
Definition: BTManager.hpp:190
direct_bt::BTManager::addMgmtEventCallback
bool addMgmtEventCallback(const int dev_id, const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept
MgmtEventCallback handling
Definition: BTManager.cpp:1117
direct_bt::POctets
Persistent octet data, i.e.
Definition: OctetTypes.hpp:451
direct_bt::MgmtEnv
Managment Singleton runtime environment properties.
Definition: BTManager.hpp:60
direct_bt::BTManager::setLocalName
std::shared_ptr< NameAndShortName > setLocalName(const uint16_t dev_id, const std::string &name, const std::string &short_name) noexcept
Definition: BTManager.cpp:1085
direct_bt::BDAddressAndType
Unique Bluetooth EUI48 address and BDAddressType tuple.
Definition: BTAddress.hpp:417
direct_bt::BTManager::Defaults
Defaults
Definition: BTManager.hpp:203
direct_bt::BTManager::getAdapter
std::shared_ptr< BTAdapter > getAdapter(const uint16_t dev_id) const noexcept
Returns the DBTAdapter with the given dev_id, or nullptr if not found.
Definition: BTManager.cpp:624
direct_bt::BTManager::userConfirmReply
MgmtStatus userConfirmReply(const uint16_t dev_id, const BDAddressAndType &addressAndType, const bool positive) noexcept
Definition: BTManager.cpp:916
direct_bt::BTManager::userPasskeyReply
MgmtStatus userPasskeyReply(const uint16_t dev_id, const BDAddressAndType &addressAndType, const uint32_t passkey) noexcept
Definition: BTManager.cpp:886
direct_bt::ChangedAdapterSetCallback
jau::FunctionDef< bool, bool, std::shared_ptr< BTAdapter > & > ChangedAdapterSetCallback
Callback jau::FunctionDef to receive change events regarding the system's adapter set,...
Definition: BTManager.hpp:189
direct_bt::MgmtLongTermKeyInfo
Used for MgmtLoadLongTermKeyCmd and MgmtEvtNewLongTermKey.
Definition: MgmtTypes.hpp:267
direct_bt::BTMode
BTMode
Bluetooth adapter operating mode.
Definition: BTTypes0.hpp:56
direct_bt::BTManager::userPasskeyNegativeReply
MgmtStatus userPasskeyNegativeReply(const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
Definition: BTManager.cpp:901
environment.hpp
direct_bt::SMPLongTermKeyInfo
SMP Long Term Key Info, used for platform agnostic persistence.
Definition: SMPTypes.hpp:522