5#ifndef NIMBLE_CPP_L2CAPCHANNEL_H_
6#define NIMBLE_CPP_L2CAPCHANNEL_H_
9#if CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM
12# if defined(CONFIG_NIMBLE_CPP_IDF)
13# include "host/ble_l2cap.h"
14# include "os/os_mbuf.h"
16# include "nimble/nimble/host/include/host/ble_l2cap.h"
17# include "nimble/porting/nimble/include/os/os_mbuf.h"
29class NimBLEL2CAPChannelCallbacks;
39class NimBLEL2CAPChannel {
48 static NimBLEL2CAPChannel* connect(
NimBLEClient* client, uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
57 bool write(
const std::vector<uint8_t>& bytes);
60 bool isConnected()
const {
return !!channel; }
63 NimBLEL2CAPChannel(uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
64 ~NimBLEL2CAPChannel();
66 int handleConnectionEvent(
struct ble_l2cap_event* event);
67 int handleAcceptEvent(
struct ble_l2cap_event* event);
68 int handleDataReceivedEvent(
struct ble_l2cap_event* event);
69 int handleTxUnstalledEvent(
struct ble_l2cap_event* event);
70 int handleDisconnectionEvent(
struct ble_l2cap_event* event);
73 friend class NimBLEL2CAPServer;
74 static constexpr const char* LOG_TAG =
"NimBLEL2CAPChannel";
78 struct ble_l2cap_chan* channel =
nullptr;
79 NimBLEL2CAPChannelCallbacks* callbacks;
80 uint8_t* receiveBuffer =
nullptr;
83 void* _coc_memory =
nullptr;
84 struct os_mempool _coc_mempool;
85 struct os_mbuf_pool _coc_mbuf_pool;
88 std::atomic<bool> stalled{
false};
93 void teardownMemPool();
96 int writeFragment(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end);
99 static int handleL2capEvent(
struct ble_l2cap_event* event,
void* arg);
105class NimBLEL2CAPChannelCallbacks {
107 NimBLEL2CAPChannelCallbacks() =
default;
108 virtual ~NimBLEL2CAPChannelCallbacks() =
default;
113 virtual bool shouldAcceptConnection(NimBLEL2CAPChannel* channel) {
return true; }
116 virtual void onConnect(NimBLEL2CAPChannel* channel, uint16_t negotiatedMTU) {};
119 virtual void onRead(NimBLEL2CAPChannel* channel, std::vector<uint8_t>& data) {};
122 virtual void onDisconnect(NimBLEL2CAPChannel* channel) {};
A model of a BLE client.
Definition NimBLEClient.h:49
A structure to hold data for a task that is waiting for a response.
Definition NimBLEUtils.h:33