esp-nimble-cpp 2.5.0
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 "syscfg/syscfg.h"
9#if CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM)
10
11# include "inttypes.h"
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"
15# else
16# include "host/ble_l2cap.h"
17# 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
61 bool disconnect();
62
65 uint16_t getConnHandle() const;
66
68 bool isConnected() const { return !!channel; }
69
70 protected:
71 NimBLEL2CAPChannel(uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
72 ~NimBLEL2CAPChannel();
73
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);
79
80 private:
81 friend class NimBLEL2CAPServer;
82 static constexpr const char* LOG_TAG = "NimBLEL2CAPChannel";
83
84 const uint16_t psm; // PSM of the channel
85 const uint16_t mtu; // The requested (local) MTU of the channel, might be larger than negotiated MTU
86 struct ble_l2cap_chan* channel = nullptr;
87 NimBLEL2CAPChannelCallbacks* callbacks;
88 uint8_t* receiveBuffer = nullptr; // buffers a full (local) MTU
89
90 // NimBLE memory pool
91 void* _coc_memory = nullptr;
92 struct os_mempool _coc_mempool;
93 struct os_mbuf_pool _coc_mbuf_pool;
94
95 // Runtime handling
96 std::atomic<bool> stalled{false};
97 NimBLETaskData* m_pTaskData{nullptr};
98
99 // Allocate / deallocate NimBLE memory pool
100 bool setupMemPool();
101 void teardownMemPool();
102
103 // Writes data up to the size of the negotiated MTU to the channel.
104 int writeFragment(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end);
105
106 // L2CAP event handler
107 static int handleL2capEvent(struct ble_l2cap_event* event, void* arg);
108};
109
113class NimBLEL2CAPChannelCallbacks {
114 public:
115 NimBLEL2CAPChannelCallbacks() = default;
116 virtual ~NimBLEL2CAPChannelCallbacks() = default;
117
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) {};
131};
132
133#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM)
134#endif // NIMBLE_CPP_L2CAPCHANNEL_H_
A structure to hold data for a task that is waiting for a response.
Definition NimBLEUtils.h:53