5#ifndef NIMBLE_CPP_L2CAPCHANNEL_H_
6#define NIMBLE_CPP_L2CAPCHANNEL_H_
8#include "syscfg/syscfg.h"
9#if CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM)
12# ifdef USING_NIMBLE_ARDUINO_HEADERS
13# include "nimble/nimble/host/include/host/ble_l2cap.h"
14# include "nimble/porting/nimble/include/os/os_mbuf.h"
16# include "host/ble_l2cap.h"
17# 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);
65 uint16_t getConnHandle()
const;
68 bool isConnected()
const {
return !!channel; }
71 NimBLEL2CAPChannel(uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
72 ~NimBLEL2CAPChannel();
74 int handleConnectionEvent(
struct ble_l2cap_event* event);
75 int handleAcceptEvent(
struct ble_l2cap_event* event);
76 int handleDataReceivedEvent(
struct ble_l2cap_event* event);
77 int handleTxUnstalledEvent(
struct ble_l2cap_event* event);
78 int handleDisconnectionEvent(
struct ble_l2cap_event* event);
81 friend class NimBLEL2CAPServer;
82 static constexpr const char* LOG_TAG =
"NimBLEL2CAPChannel";
86 struct ble_l2cap_chan* channel =
nullptr;
87 NimBLEL2CAPChannelCallbacks* callbacks;
88 uint8_t* receiveBuffer =
nullptr;
91 void* _coc_memory =
nullptr;
92 struct os_mempool _coc_mempool;
93 struct os_mbuf_pool _coc_mbuf_pool;
96 std::atomic<bool> stalled{
false};
101 void teardownMemPool();
104 int writeFragment(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end);
107 static int handleL2capEvent(
struct ble_l2cap_event* event,
void* arg);
113class NimBLEL2CAPChannelCallbacks {
115 NimBLEL2CAPChannelCallbacks() =
default;
116 virtual ~NimBLEL2CAPChannelCallbacks() =
default;
121 virtual bool shouldAcceptConnection(NimBLEL2CAPChannel* channel) {
return true; }
124 virtual void onConnect(NimBLEL2CAPChannel* channel, uint16_t negotiatedMTU) {};
127 virtual void onRead(NimBLEL2CAPChannel* channel, std::vector<uint8_t>& data) {};
130 virtual void onDisconnect(NimBLEL2CAPChannel* channel) {};
A structure to hold data for a task that is waiting for a response.
Definition NimBLEUtils.h:53