esp-nimble-cpp 2.6.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 "NimBLEUtils.h"
26
27# include <vector>
28# include <atomic>
29
30class NimBLEClient;
31class NimBLEL2CAPChannelCallbacks;
32
40class NimBLEL2CAPChannel {
41 public:
49 static NimBLEL2CAPChannel* connect(NimBLEClient* client, uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
50
58 bool write(const std::vector<uint8_t>& bytes);
59
62 bool disconnect();
63
66 uint16_t getConnHandle() const;
67
69 bool isConnected() const { return !!channel; }
70
71 protected:
72 NimBLEL2CAPChannel(uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
73 ~NimBLEL2CAPChannel();
74
75 int handleConnectionEvent(struct ble_l2cap_event* event);
76 int handleAcceptEvent(struct ble_l2cap_event* event);
77 int handleDataReceivedEvent(struct ble_l2cap_event* event);
78 int handleTxUnstalledEvent(struct ble_l2cap_event* event);
79 int handleDisconnectionEvent(struct ble_l2cap_event* event);
80
81 private:
82 friend class NimBLEL2CAPServer;
83 static constexpr const char* LOG_TAG = "NimBLEL2CAPChannel";
84
85 const uint16_t psm; // PSM of the channel
86 const uint16_t mtu; // The requested (local) MTU of the channel, might be larger than negotiated MTU
87 struct ble_l2cap_chan* channel = nullptr;
88 NimBLEL2CAPChannelCallbacks* callbacks;
89 uint8_t* receiveBuffer = nullptr; // buffers a full (local) MTU
90
91 // NimBLE memory pool
92 void* _coc_memory = nullptr;
93 struct os_mempool _coc_mempool;
94 struct os_mbuf_pool _coc_mbuf_pool;
95
96 // Runtime handling
97 std::atomic<bool> stalled{false};
98 NimBLEUtils::TaskData* m_pTaskData{nullptr};
99
100 // Allocate / deallocate NimBLE memory pool
101 bool setupMemPool();
102 void teardownMemPool();
103
104 // Writes data up to the size of the negotiated MTU to the channel.
105 int writeFragment(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end);
106
107 // L2CAP event handler
108 static int handleL2capEvent(struct ble_l2cap_event* event, void* arg);
109};
110
114class NimBLEL2CAPChannelCallbacks {
115 public:
116 NimBLEL2CAPChannelCallbacks() = default;
117 virtual ~NimBLEL2CAPChannelCallbacks() = default;
118
122 virtual bool shouldAcceptConnection(NimBLEL2CAPChannel* channel) { return true; }
125 virtual void onConnect(NimBLEL2CAPChannel* channel, uint16_t negotiatedMTU) {};
128 virtual void onRead(NimBLEL2CAPChannel* channel, std::vector<uint8_t>& data) {};
131 virtual void onDisconnect(NimBLEL2CAPChannel* channel) {};
132};
133
134#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM)
135#endif // NIMBLE_CPP_L2CAPCHANNEL_H_
A structure to hold data for a task that is waiting for a response.
Definition NimBLEUtils.h:78