Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
Public Types | Public Member Functions | Static Public Member Functions | List of all members
direct_bt::L2CAPClient Class Reference

L2CAP read/write communication channel to remote device. More...

#include <L2CAPComm.hpp>

Inheritance diagram for direct_bt::L2CAPClient:
Collaboration diagram for direct_bt::L2CAPClient:

Public Types

enum class  Defaults : int { L2CAP_CONNECT_MAX_RETRY = 3 }
 
enum class  RWExitCode : jau::snsize_t {
  SUCCESS = 0 , NOT_OPEN = -1 , INTERRUPTED = -2 , INVALID_SOCKET_DD = -3 ,
  POLL_ERROR = -10 , POLL_TIMEOUT = -11 , READ_ERROR = -20 , READ_TIMEOUT = -21 ,
  WRITE_ERROR = -30
}
 Exit code for read() and write() operations. More...
 
- Public Types inherited from direct_bt::L2CAPComm
typedef jau::function< bool(int)> get_boolean_callback_t
 Utilized to query for external interruption, whether device is still connected etc. More...
 

Public Member Functions

 L2CAPClient (const uint16_t adev_id, BDAddressAndType adapterAddressAndType, const L2CAP_PSM psm, const L2CAP_CID cid) noexcept
 Constructing a non connected L2CAP channel instance for the pre-defined PSM and CID. More...
 
 L2CAPClient (const uint16_t adev_id, BDAddressAndType adapterAddressAndType, const L2CAP_PSM psm, const L2CAP_CID cid, BDAddressAndType remoteAddressAndType, int client_socket) noexcept
 Constructing a connected L2CAP channel instance for the pre-defined PSM and CID. More...
 
 ~L2CAPClient () noexcept override
 Destructor closing the L2CAP channel, see close(). More...
 
bool close () noexcept override
 Closing the L2CAP channel, locking mutex_write(). More...
 
BTSecurityLevel getBTSecurityLevel () noexcept
 Fetches the current BlueZ's L2CAP socket BT_SECURITY sec_level. More...
 
const BDAddressAndTypegetRemoteAddressAndType () const noexcept
 
std::string getStateString () const noexcept override
 
bool hasIOError () const noexcept
 
std::recursive_mutex & mutex_write () noexcept
 Return the recursive write mutex for multithreading access. More...
 
bool open (const BTDevice &device, const BTSecurityLevel sec_level=BTSecurityLevel::NONE) noexcept
 Opens and connects the L2CAP channel, locking mutex_write(). More...
 
jau::snsize_t read (uint8_t *buffer, const jau::nsize_t capacity) noexcept
 Generic read, w/o locking suitable for a unique ringbuffer sink. More...
 
bool setBTSecurityLevel (const BTSecurityLevel sec_level) noexcept
 If sec_level > BTSecurityLevel::UNSET, sets the BlueZ's L2CAP socket BT_SECURITY sec_level, determining the SMP security mode per connection. More...
 
std::string toString () const noexcept override
 
jau::snsize_t write (const uint8_t *buffer, const jau::nsize_t length) noexcept
 Generic write, locking mutex_write(). More...
 
- Public Member Functions inherited from direct_bt::L2CAPComm
 L2CAPComm (const L2CAPComm &)=delete
 
 L2CAPComm (const uint16_t adev_id, BDAddressAndType localAddressAndType, const L2CAP_PSM psm, const L2CAP_CID cid) noexcept
 
virtual ~L2CAPComm () noexcept=default
 Destructor specialization shall close the L2CAP socket, see close(). More...
 
virtual bool close () noexcept=0
 Closing the L2CAP socket, see specializations. More...
 
virtual std::string getStateString () const noexcept=0
 
bool interrupted () const noexcept
 Returns true if interrupted by internal or external cause, hence shall stop connecting and reading. More...
 
bool is_open () const noexcept
 
void operator= (const L2CAPComm &)=delete
 
void set_interrupted_query (get_boolean_callback_t is_interrupted_cb) noexcept
 The external is interrupted callback is used until close(), thereafter it is removed. More...
 
int socket () const noexcept
 Return this L2CAP socket descriptor. More...
 
virtual std::string toString () const noexcept=0
 

Static Public Member Functions

static std::string getRWExitCodeString (const jau::snsize_t ecn) noexcept
 
static std::string getRWExitCodeString (const RWExitCode ec) noexcept
 
static constexpr int number (const Defaults d) noexcept
 
static constexpr jau::snsize_t number (const RWExitCode rhs) noexcept
 
static constexpr RWExitCode toRWExitCode (const jau::snsize_t rhs) noexcept
 
- Static Public Member Functions inherited from direct_bt::L2CAPComm
static std::string getStateString (bool isOpen, bool hasIOError) noexcept
 
static std::string getStateString (bool isOpen, bool irqed_int, bool irqed_ext, bool hasIOError) noexcept
 
static std::string getStateString (bool isOpen, bool isInterrupted, bool hasIOError) noexcept
 

Additional Inherited Members

- Public Attributes inherited from direct_bt::L2CAPComm
const uint16_t adev_id
 Corresponding BTAdapter device id. More...
 
const L2CAP_CID cid
 Corresponding L2CAP_CID for the channel. More...
 
const BDAddressAndType localAddressAndType
 Corresponding BTAdapter local BTAddressAndType. More...
 
const L2CAP_PSM psm
 Corresponding L2CAP_PSM for the channel. More...
 
- Protected Member Functions inherited from direct_bt::L2CAPComm
BTSecurityLevel getBTSecurityLevelImpl (const BDAddressAndType &remoteAddressAndType) noexcept
 
bool interrupted_ext () const noexcept
 Returns true if interrupted by external cause. More...
 
bool interrupted_int () const noexcept
 Returns true if interrupted by internal cause. More...
 
bool setBTSecurityLevelImpl (const BTSecurityLevel sec_level, const BDAddressAndType &remoteAddressAndType) noexcept
 
- Static Protected Member Functions inherited from direct_bt::L2CAPComm
static int l2cap_close_dev (int dd) noexcept
 
static int l2cap_open_dev (const BDAddressAndType &adapterAddressAndType, const L2CAP_PSM psm, const L2CAP_CID cid) noexcept
 
- Protected Attributes inherited from direct_bt::L2CAPComm
const L2CAPEnvenv
 
jau::sc_atomic_bool interrupted_intern
 
get_boolean_callback_t is_interrupted_extern
 
jau::sc_atomic_bool is_open_
 
std::recursive_mutex mtx_open
 
jau::relaxed_atomic_int socket_
 

Detailed Description

L2CAP read/write communication channel to remote device.

Definition at line 195 of file L2CAPComm.hpp.

Member Enumeration Documentation

◆ Defaults

enum class direct_bt::L2CAPClient::Defaults : int
strong
Enumerator
L2CAP_CONNECT_MAX_RETRY 

Definition at line 197 of file L2CAPComm.hpp.

◆ RWExitCode

Exit code for read() and write() operations.

Enumerator
SUCCESS 

SUCCESS.

NOT_OPEN 

NOT_OPEN.

INTERRUPTED 

INTERRUPTED.

INVALID_SOCKET_DD 

INVALID_SOCKET_DD.

POLL_ERROR 

POLL_ERROR.

POLL_TIMEOUT 

POLL_TIMEOUT.

READ_ERROR 

READ_ERROR.

READ_TIMEOUT 

READ_TIMEOUT.

WRITE_ERROR 

WRITE_ERROR.

Definition at line 205 of file L2CAPComm.hpp.

Constructor & Destructor Documentation

◆ L2CAPClient() [1/2]

L2CAPClient::L2CAPClient ( const uint16_t  adev_id,
BDAddressAndType  adapterAddressAndType,
const L2CAP_PSM  psm,
const L2CAP_CID  cid 
)
noexcept

Constructing a non connected L2CAP channel instance for the pre-defined PSM and CID.

Definition at line 253 of file L2CAPComm.cpp.

◆ L2CAPClient() [2/2]

L2CAPClient::L2CAPClient ( const uint16_t  adev_id,
BDAddressAndType  adapterAddressAndType,
const L2CAP_PSM  psm,
const L2CAP_CID  cid,
BDAddressAndType  remoteAddressAndType,
int  client_socket 
)
noexcept

Constructing a connected L2CAP channel instance for the pre-defined PSM and CID.

Definition at line 259 of file L2CAPComm.cpp.

◆ ~L2CAPClient()

direct_bt::L2CAPClient::~L2CAPClient ( )
inlineoverridenoexcept

Destructor closing the L2CAP channel, see close().

Definition at line 249 of file L2CAPComm.hpp.

Member Function Documentation

◆ number() [1/2]

static constexpr int direct_bt::L2CAPClient::number ( const Defaults  d)
inlinestaticconstexprnoexcept

Definition at line 200 of file L2CAPComm.hpp.

Here is the caller graph for this function:

◆ number() [2/2]

static constexpr jau::snsize_t direct_bt::L2CAPClient::number ( const RWExitCode  rhs)
inlinestaticconstexprnoexcept

Definition at line 216 of file L2CAPComm.hpp.

◆ toRWExitCode()

static constexpr RWExitCode direct_bt::L2CAPClient::toRWExitCode ( const jau::snsize_t  rhs)
inlinestaticconstexprnoexcept

Definition at line 219 of file L2CAPComm.hpp.

◆ getRWExitCodeString() [1/2]

std::string L2CAPClient::getRWExitCodeString ( const RWExitCode  ec)
staticnoexcept

Definition at line 510 of file L2CAPComm.cpp.

Here is the caller graph for this function:

◆ getRWExitCodeString() [2/2]

static std::string direct_bt::L2CAPClient::getRWExitCodeString ( const jau::snsize_t  ecn)
inlinestaticnoexcept

Definition at line 223 of file L2CAPComm.hpp.

◆ open()

bool L2CAPClient::open ( const BTDevice device,
const BTSecurityLevel  sec_level = BTSecurityLevel::NONE 
)
noexcept

Opens and connects the L2CAP channel, locking mutex_write().

BT Core Spec v5.2: Vol 3, Part A: L2CAP_CONNECTION_REQ

Parameters
devicethe remote device to establish this L2CAP connection
sec_levelsec_level < BTSecurityLevel::NONE will not set security level
Returns
true if connection has been established, otherwise false

Definition at line 269 of file L2CAPComm.cpp.

◆ getRemoteAddressAndType()

const BDAddressAndType & direct_bt::L2CAPClient::getRemoteAddressAndType ( ) const
inlinenoexcept

Definition at line 265 of file L2CAPComm.hpp.

◆ close()

bool direct_bt::L2CAPClient::close ( )
inlineoverridevirtualnoexcept

Closing the L2CAP channel, locking mutex_write().

Implements direct_bt::L2CAPComm.

Definition at line 268 of file L2CAPComm.hpp.

◆ hasIOError()

bool direct_bt::L2CAPClient::hasIOError ( ) const
inlinenoexcept

Definition at line 270 of file L2CAPComm.hpp.

◆ getStateString()

std::string direct_bt::L2CAPClient::getStateString ( ) const
inlineoverridevirtualnoexcept

Implements direct_bt::L2CAPComm.

Definition at line 271 of file L2CAPComm.hpp.

Here is the caller graph for this function:

◆ mutex_write()

std::recursive_mutex & direct_bt::L2CAPClient::mutex_write ( )
inlinenoexcept

Return the recursive write mutex for multithreading access.

Definition at line 274 of file L2CAPComm.hpp.

◆ setBTSecurityLevel()

bool L2CAPClient::setBTSecurityLevel ( const BTSecurityLevel  sec_level)
noexcept

If sec_level > BTSecurityLevel::UNSET, sets the BlueZ's L2CAP socket BT_SECURITY sec_level, determining the SMP security mode per connection.

To unset security, the L2CAP socket should be closed and opened again.

If setting the security level fails, close() will be called.

Parameters
sec_levelsec_level == BTSecurityLevel::UNSET will not set security level and returns true.
Returns
true if a security level > BTSecurityLevel::UNSET has been set successfully, false if no security level has been set or if it failed.

Definition at line 462 of file L2CAPComm.cpp.

◆ getBTSecurityLevel()

BTSecurityLevel L2CAPClient::getBTSecurityLevel ( )
noexcept

Fetches the current BlueZ's L2CAP socket BT_SECURITY sec_level.

Returns
BTSecurityLevel sec_level value, BTSecurityLevel::UNSET if failure

Definition at line 484 of file L2CAPComm.cpp.

◆ read()

jau::snsize_t L2CAPClient::read ( uint8_t *  buffer,
const jau::nsize_t  capacity 
)
noexcept

Generic read, w/o locking suitable for a unique ringbuffer sink.

Using L2CAPEnv::L2CAP_READER_POLL_TIMEOUT.

Parameters
buffer
capacity
Returns
number of bytes read if >= 0, otherwise L2CAPComm::ExitCode error code.

Definition at line 521 of file L2CAPComm.cpp.

◆ write()

jau::snsize_t L2CAPClient::write ( const uint8_t *  buffer,
const jau::nsize_t  length 
)
noexcept

Generic write, locking mutex_write().

Parameters
buffer
length
Returns
number of bytes written if >= 0, otherwise L2CAPComm::ExitCode error code.

Definition at line 643 of file L2CAPComm.cpp.

◆ toString()

std::string L2CAPClient::toString ( ) const
overridevirtualnoexcept

Implements direct_bt::L2CAPComm.

Definition at line 713 of file L2CAPComm.cpp.


The documentation for this class was generated from the following files: