26#ifndef BT_GATT_HANDLER_HPP_
27#define BT_GATT_HANDLER_HPP_
178 MAX_ATT_MTU = 512 + 1,
183 static constexpr uint16_t
number(
const Defaults d) {
return static_cast<uint16_t
>(d); }
307 Section(uint16_t s, uint16_t e) : start(s), end(e) {}
332 const bool confirmationSent) = 0;
343 [[maybe_unused]]
const BTDeviceRef& clientSource) { }
354 [[maybe_unused]]
const BTDeviceRef& clientDest) { }
367 virtual void mtuResponse([[maybe_unused]]
const uint16_t clientMTU,
368 [[maybe_unused]]
const AttPDUMsg& pduReply,
370 [[maybe_unused]]
const uint16_t serverMTU,
371 [[maybe_unused]]
const uint16_t usedMTU,
373 [[maybe_unused]]
const BTDeviceRef& clientRequester) { }
387 [[maybe_unused]]
const bool with_response,
389 [[maybe_unused]]
const BTDeviceRef& clientSource) { }
402 [[maybe_unused]]
const BTDeviceRef& clientDest) { }
417 [[maybe_unused]]
const uint16_t value_offset,
418 [[maybe_unused]]
const AttPDUMsg& pduReply,
422 [[maybe_unused]]
const BTDeviceRef& clientRequester) { }
438 {
return this == &rhs; }
441 {
return !(*
this == rhs); }
453 std::weak_ptr<BTDevice> wbr_device;
457 const std::string deviceString;
458 mutable std::recursive_mutex mtx_command;
474 struct GattCharListenerPair {
478 std::weak_ptr<BTGattChar> wbr_characteristic;
480 bool match(
const BTGattChar& characteristic)
const noexcept {
482 if(
nullptr != sda ) {
483 return *sda == characteristic;
491 gattCharListenerList_t gattCharListenerList;
498 std::unique_ptr<GattServerHandler> gattServerHandler;
502 std::shared_ptr<GattGenericAccessSvc> genericAccess =
nullptr;
504 bool validateConnected() noexcept;
506 DBGattCharRef findServerGattCharByValueHandle(const uint16_t char_value_handle) noexcept;
513 bool replyAttPDUReq(
std::unique_ptr<const
AttPDUMsg> && pdu) noexcept;
515 void l2capReaderWork(
jau::service_runner& sr) noexcept;
516 void l2capReaderEndLocked(
jau::service_runner& sr) noexcept;
518 bool l2capReaderInterrupted(
int dummy=0) noexcept;
578 bool discoverCompletePrimaryServices(const
std::shared_ptr<
BTGattHandler>& shared_this) noexcept;
620 bool disconnect(const
bool disconnect_device, const
bool ioerr_cause) noexcept;
623 inline uint16_t
getUsedMTU() const noexcept {
return usedMTU; }
624 void setUsedMTU(
const uint16_t mtu)
noexcept { usedMTU = mtu; }
658 bool initClientGatt(
const std::shared_ptr<BTGattHandler>& shared_this,
bool& already_init)
noexcept;
676 inline std::shared_ptr<GattGenericAccessSvc>
getGenericAccess() noexcept {
return genericAccess; }
768 bool writeValue(const uint16_t handle, const
jau::TROOctets & value, const
bool withResponse) noexcept;
817 bool sendNotification(const uint16_t char_value_handle, const
jau::TROOctets & value) noexcept;
830 bool sendIndication(const uint16_t char_value_handle, const
jau::TROOctets & value) noexcept;
945 const uint16_t serverMTU, const uint16_t usedMTU,
1031 bool ping() noexcept;
BT Core Spec v5.2: Vol 3, Part F ATT: 3.4.1.1 ATT_ERROR_RSP.
BT Core Spec v5.2: Vol 3, Part F ATT: 3.4.2.1 ATT_EXCHANGE_MTU_REQ BT Core Spec v5....
BT Core Spec v5.2: Vol 3, Part F ATT: 3.4.3.3 ATT_FIND_BY_TYPE_VALUE_REQ.
BT Core Spec v5.2: Vol 3, Part F ATT: 3.4.3.1 ATT_FIND_INFORMATION_REQ.
Handles the Attribute Protocol (ATT) using Protocol Data Unit (PDU) encoded messages over L2CAP chann...
BT Core Spec v5.2: Vol 3, Part F ATT: 3.4.4.1 ATT_READ_BY_TYPE_REQ.
BTGattChar event listener for notification and indication events.
Representing a Gatt Characteristic object from the GATTRole::Client perspective.
Representing a Gatt Characteristic Descriptor object from the GATTRole::Client perspective.
GATT Singleton runtime environment properties.
const jau::fraction_i64 GATT_READ_COMMAND_REPLY_TIMEOUT
Timeout for GATT read command replies, defaults to 550ms minimum, where 500ms is the minimum supervis...
const int32_t ATTPDU_RING_CAPACITY
Medium ringbuffer capacity, defaults to 128 messages.
const jau::fraction_i64 GATT_INITIAL_COMMAND_REPLY_TIMEOUT
Timeout for l2cap initial command reply, defaults to 2500ms (2000ms minimum).
const bool DEBUG_DATA
Debug all GATT Data communication.
static BTGattEnv & get() noexcept
const jau::fraction_i64 GATT_WRITE_COMMAND_REPLY_TIMEOUT
Timeout for GATT write command replies, defaults to 550ms minimum, where 500ms is the minimum supervi...
Internal handler implementation for given DBGattServer instance matching its DBGattServer::Mode.
virtual bool replyFindInfoReq(const AttFindInfoReq *pdu) noexcept=0
Reply to a find info request.
virtual bool replyWriteReq(const AttPDUMsg *pdu) noexcept=0
Reply to a write request.
virtual DBGattServer::Mode getMode() noexcept=0
virtual bool replyExchangeMTUReq(const AttExchangeMTU *pdu) noexcept=0
Reply to an exchange MTU request.
virtual bool replyReadReq(const AttPDUMsg *pdu) noexcept=0
Reply to a read request.
virtual bool replyFindByTypeValueReq(const AttFindByTypeValueReq *pdu) noexcept=0
Reply to a find by type value request.
virtual ~GattServerHandler()=default
virtual bool replyReadByGroupTypeReq(const AttReadByNTypeReq *pdu) noexcept=0
Reply to a read by group type request.
virtual bool replyReadByTypeReq(const AttReadByNTypeReq *pdu) noexcept=0
Reply to a read by type request.
virtual void close() noexcept
Close and clear this handler, i.e.
Native GATT characteristic event listener for notification and indication events received from a GATT...
bool operator!=(const NativeGattCharListener &rhs) const noexcept
virtual ~NativeGattCharListener() noexcept=default
virtual void replyReceived(const AttPDUMsg &pduReply, const BTDeviceRef &serverSource, const BTDeviceRef &clientDest)
Informal low-level notification of AttPDUMsg responses from this GATTRole::Server,...
virtual bool operator==(const NativeGattCharListener &rhs) const noexcept
Default comparison operator, merely testing for same memory reference.
virtual void notificationReceived(const BTDeviceRef &source, const uint16_t charHandle, const jau::TROOctets &charValue, const uint64_t timestamp)=0
Called from native BLE stack, initiated by a received notification.
virtual void writeResponse(const AttPDUMsg &pduReply, const AttErrorRsp::ErrorCode error_code, const BTDeviceRef &serverSource, const BTDeviceRef &clientDest)
Informal notification about a write response received from this GATTRole::Server, optional.
virtual void mtuResponse(const uint16_t clientMTU, const AttPDUMsg &pduReply, const AttErrorRsp::ErrorCode error_reply, const uint16_t serverMTU, const uint16_t usedMTU, const BTDeviceRef &serverReplier, const BTDeviceRef &clientRequester)
Informal notification about a complete MTU exchange request and response to and from this GATTRole::S...
virtual void readResponse(const uint16_t handle, const uint16_t value_offset, const AttPDUMsg &pduReply, const AttErrorRsp::ErrorCode error_reply, const jau::TROOctets &data_reply, const BTDeviceRef &serverReplier, const BTDeviceRef &clientRequester)
Informal notification about a complete read request and response to and from this GATTRole::Server,...
virtual void indicationReceived(const BTDeviceRef &, const uint16_t charHandle, const jau::TROOctets &charValue, const uint64_t timestamp, const bool confirmationSent)=0
Called from native BLE stack, initiated by a received indication.
virtual void writeRequest(const uint16_t handle, const jau::TROOctets &data, const jau::darray< Section > §ions, const bool with_response, const BTDeviceRef &serverDest, const BTDeviceRef &clientSource)
Informal notification about a completed write request sent to this GATTRole::Server,...
virtual void requestSent(const AttPDUMsg &pduRequest, const BTDeviceRef &serverDest, const BTDeviceRef &clientSource)
Informal low-level notification of AttPDUMsg requests to this GATTRole::Server, optional.
A thread safe GATT handler associated to one device via one L2CAP connection.
bool readDescriptorValue(BTGattDesc &cd, ssize_type expectedLength=-1) noexcept
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.1 Read Characteristic Descriptor.
bool disconnect(const bool disconnect_device, const bool ioerr_cause) noexcept
Disconnect this BTGattHandler and optionally the associated device.
bool send(const AttPDUMsg &msg) noexcept
Sends the given AttPDUMsg to the connected device via l2cap.
BTDeviceRef getDeviceUnchecked() const noexcept
const int32_t supervision_timeout
Supervison timeout of the connection.
bool writeValue(const uint16_t handle, const jau::TROOctets &value, const bool withResponse) noexcept
Generic write GATT value and long value.
void notifyNativeReplyReceived(const AttPDUMsg &pduReply, const BTDeviceRef &clientDest) noexcept
Notify all NativeGattCharListener about a low-level AttPDUMsg reply being received from this GATTRole...
bool addCharListener(const BTGattCharListenerRef &l) noexcept
Add the given listener to the list if not already present.
bool getSendIndicationConfirmation() noexcept
Returns whether sending an immediate confirmation for received indication events from the device is e...
void notifyNativeMTUResponse(const uint16_t clientMTU, const AttPDUMsg &pduReply, const AttErrorRsp::ErrorCode error_reply, const uint16_t serverMTU, const uint16_t usedMTU, const BTDeviceRef &clientRequester) noexcept
Notify all NativeGattCharListener about a completed MTU exchange request and response to and from thi...
uint16_t getUsedMTU() const noexcept
std::string toString() const noexcept
void setUsedMTU(const uint16_t mtu) noexcept
GattServiceList_t & getServices() noexcept
Returns a reference of the internal kept BTGattService list.
bool readValue(const uint16_t handle, jau::POctets &res, ssize_type expectedLength=-1) noexcept
Generic read GATT value and long value.
bool writeCharacteristicValueNoResp(const BTGattChar &c, const jau::TROOctets &value) noexcept
BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.1 Write Characteristic Value Without Response.
void setSendIndicationConfirmation(const bool v) noexcept
Enable or disable sending an immediate confirmation for received indication events from the device.
bool readCharacteristicValue(const BTGattChar &c, jau::POctets &res, ssize_type expectedLength=-1) noexcept
BT Core Spec v5.2: Vol 3, Part G GATT: 4.8.1 Read Characteristic Value.
uint16_t getServerMTU() const noexcept
void notifyNativeWriteResponse(const AttPDUMsg &pduReply, const AttErrorRsp::ErrorCode error_code, const BTDeviceRef &clientDest) noexcept
Notify all NativeGattCharListener about a write response received from this GATTRole::Server.
bool sendIndication(const uint16_t char_value_handle, const jau::TROOctets &value) noexcept
Send an indication event consisting out of the given value representing the given characteristic valu...
static constexpr uint16_t number(const Defaults d)
void notifyNativeReadResponse(const uint16_t handle, const uint16_t value_offset, const AttPDUMsg &pduReply, const AttErrorRsp::ErrorCode error_reply, const jau::TROOctets &data_reply, const BTDeviceRef &clientRequester) noexcept
Notify all NativeGattCharListener about a completed read request and response to and from this GATTRo...
bool configNotificationIndication(BTGattDesc &cd, const bool enableNotification, const bool enableIndication) noexcept
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
jau::darray< BTGattServiceRef, size_type > GattServiceList_t
const BTGattEnv & env
Environment runtime configuration, usually used internally only.
bool initClientGatt(const std::shared_ptr< BTGattHandler > &shared_this, bool &already_init) noexcept
Initialize the connection and internal data set for GATT client operations:
bool writeDescriptorValue(const BTGattDesc &cd) noexcept
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.3 Write Characteristic Descriptors.
std::string getStateString() const noexcept
bool sendNotification(const uint16_t char_value_handle, const jau::TROOctets &value) noexcept
Send a notification event consisting out of the given value representing the given characteristic val...
bool hasIOError() const noexcept
const jau::fraction_i64 read_cmd_reply_timeout
Derived environment runtime configuration, usually used internally only.
size_type removeAllAssociatedCharListener(const BTGattCharRef &associatedChar) noexcept
Remove all BTGattCharListener from the list, which are associated to the given BTGattChar when added ...
bool isConnected() const noexcept
jau::darray< NativeGattCharListener::Section, size_type > NativeGattCharSections_t
jau::nsize_t getCharListenerCount() const noexcept
Return event listener count.
bool removeCharListener(const BTGattCharListenerRef &l) noexcept
Remove the given listener from the list.
bool writeCharacteristicValue(const BTGattChar &c, const jau::TROOctets &value) noexcept
BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.3 Write Characteristic Value.
std::unique_ptr< const AttPDUMsg > sendWithReply(const AttPDUMsg &msg, const jau::fraction_i64 &timeout) noexcept
Sends the given AttPDUMsg to the connected device via l2cap using send().
jau::cow_darray< NativeGattCharListenerRef, size_type > NativeGattCharListenerList_t
std::shared_ptr< GattGenericAccessSvc > getGenericAccess() noexcept
Returns the internal kept shared GattGenericAccessSvc instance.
BTGattCharRef findCharacterisicsByValueHandle(const GattServiceList_t &services_, const uint16_t charValueHandle) noexcept
Find and return the BTGattChar within given list of primary services via given characteristic value h...
bool ping() noexcept
Issues a ping to the device, validating whether it is still reachable.
std::shared_ptr< NativeGattCharListener > NativeGattCharListenerRef
BTDeviceRef getDeviceChecked() const
size_type removeAllCharListener() noexcept
Remove all event listener from the list.
std::shared_ptr< GattDeviceInformationSvc > getDeviceInformation(GattServiceList_t &primServices) noexcept
void printCharListener() noexcept
Print a list of all BTGattCharListener and NativeGattCharListener.
void notifyNativeRequestSent(const AttPDUMsg &pduRequest, const BTDeviceRef &clientSource) noexcept
Notify all NativeGattCharListener about a low-level AttPDUMsg request being sent to this GATTRole::Se...
void notifyNativeWriteRequest(const uint16_t handle, const jau::TROOctets &data, const NativeGattCharSections_t §ions, const bool with_response, const BTDeviceRef &clientSource) noexcept
Notify all NativeGattCharListener about a completed write request sent to this GATTRole::Server.
GATTRole getRole() const noexcept
Return the local GATTRole to the remote BTDevice.
const jau::fraction_i64 write_cmd_reply_timeout
Derived environment runtime configuration, usually used internally only.
Mode
Operating mode of a DBGattServer instance.
Generic Access Service is a mandatory GATT service all peripherals are required to implement.
L2CAP read/write communication channel to remote device.
Persistent endian aware octet data, i.e.
Transient read only and endian aware octet data, i.e.
bool(* equal_comparator)(const value_type &a, const value_type &b)
Generic value_type equal comparator to be user defined for e.g.
Implementation of a dynamic linear array storage, aka vector.
Ring buffer implementation, a.k.a circular buffer, exposing lock-free get*(..) and put*(....
Base jau environment class, merely to tag all environment settings by inheritance and hence documenta...
Service runner, a reusable dedicated thread performing custom user services.
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
std::string to_string(const alphabet &v) noexcept
std::shared_ptr< BTGattHandler > BTGattHandlerRef
std::shared_ptr< BTDevice > BTDeviceRef
GATTRole
Bluetooth GATT roles.
std::shared_ptr< BTGattCharListener > BTGattCharListenerRef
std::shared_ptr< BTGattChar > BTGattCharRef
std::shared_ptr< BTGattService > BTGattServiceRef
std::shared_ptr< DBGattServer > DBGattServerRef
std::shared_ptr< DBGattChar > DBGattCharRef
fraction< int64_t > fraction_i64
fraction using int64_t as integral type
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
int_fast32_t snsize_t
Natural 'ssize_t' alternative using int_fast32_t as its natural sized type.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
uint16_t start
start point, inclusive
uint16_t end
end point, exclusive
Section(uint16_t s, uint16_t e)