NimBLE-Arduino 2.3.2
Loading...
Searching...
No Matches
NimBLEL2CAPChannel.h
1//
2// (C) Dr. Michael 'Mickey' Lauer <mickey@vanille-media.de>
3//
4
5#ifndef NIMBLE_CPP_L2CAPCHANNEL_H_
6#define NIMBLE_CPP_L2CAPCHANNEL_H_
7
8#include "nimconfig.h"
9#if CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM
10
11# include "inttypes.h"
12# if defined(CONFIG_NIMBLE_CPP_IDF)
13# include "host/ble_l2cap.h"
14# include "os/os_mbuf.h"
15# else
16# include "nimble/nimble/host/include/host/ble_l2cap.h"
17# include "nimble/porting/nimble/include/os/os_mbuf.h"
18# endif
19
20/**** FIX COMPILATION ****/
21# undef min
22# undef max
23/**************************/
24
25# include <vector>
26# include <atomic>
27
28class NimBLEClient;
29class NimBLEL2CAPChannelCallbacks;
30struct NimBLETaskData;
31
39class NimBLEL2CAPChannel {
40 public:
48 static NimBLEL2CAPChannel* connect(NimBLEClient* client, uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
49
57 bool write(const std::vector<uint8_t>& bytes);
58
60 bool isConnected() const { return !!channel; }
61
62 protected:
63 NimBLEL2CAPChannel(uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
64 ~NimBLEL2CAPChannel();
65
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);
71
72 private:
73 friend class NimBLEL2CAPServer;
74 static constexpr const char* LOG_TAG = "NimBLEL2CAPChannel";
75
76 const uint16_t psm; // PSM of the channel
77 const uint16_t mtu; // The requested (local) MTU of the channel, might be larger than negotiated MTU
78 struct ble_l2cap_chan* channel = nullptr;
79 NimBLEL2CAPChannelCallbacks* callbacks;
80 uint8_t* receiveBuffer = nullptr; // buffers a full (local) MTU
81
82 // NimBLE memory pool
83 void* _coc_memory = nullptr;
84 struct os_mempool _coc_mempool;
85 struct os_mbuf_pool _coc_mbuf_pool;
86
87 // Runtime handling
88 std::atomic<bool> stalled{false};
89 NimBLETaskData* m_pTaskData{nullptr};
90
91 // Allocate / deallocate NimBLE memory pool
92 bool setupMemPool();
93 void teardownMemPool();
94
95 // Writes data up to the size of the negotiated MTU to the channel.
96 int writeFragment(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end);
97
98 // L2CAP event handler
99 static int handleL2capEvent(struct ble_l2cap_event* event, void* arg);
100};
101
105class NimBLEL2CAPChannelCallbacks {
106 public:
107 NimBLEL2CAPChannelCallbacks() = default;
108 virtual ~NimBLEL2CAPChannelCallbacks() = default;
109
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) {};
123};
124
125#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM
126#endif // NIMBLE_CPP_L2CAPCHANNEL_H_
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