esp-nimble-cpp 2.3.0
Loading...
Searching...
No Matches
NimBLEL2CAPChannel.h
1//
2// (C) Dr. Michael 'Mickey' Lauer <mickey@vanille-media.de>
3//
4#pragma once
5#ifndef NIMBLEL2CAPCHANNEL_H
6# define NIMBLEL2CAPCHANNEL_H
7
8# include "nimconfig.h"
9
10# include "inttypes.h"
11# if defined(CONFIG_NIMBLE_CPP_IDF)
12# include "host/ble_l2cap.h"
13# include "os/os_mbuf.h"
14# else
15# include "nimble/nimble/host/include/host/ble_l2cap.h"
16# include "nimble/porting/nimble/include/os/os_mbuf.h"
17# endif
18
19/**** FIX COMPILATION ****/
20# undef min
21# undef max
22/**************************/
23
24# include <vector>
25# include <atomic>
26
27class NimBLEClient;
29struct NimBLETaskData;
30
39 public:
47 static NimBLEL2CAPChannel* connect(NimBLEClient* client, uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
48
56 bool write(const std::vector<uint8_t>& bytes);
57
59 bool isConnected() const { return !!channel; }
60
61 protected:
62 NimBLEL2CAPChannel(uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
64
65 int handleConnectionEvent(struct ble_l2cap_event* event);
66 int handleAcceptEvent(struct ble_l2cap_event* event);
67 int handleDataReceivedEvent(struct ble_l2cap_event* event);
68 int handleTxUnstalledEvent(struct ble_l2cap_event* event);
69 int handleDisconnectionEvent(struct ble_l2cap_event* event);
70
71 private:
72 friend class NimBLEL2CAPServer;
73 static constexpr const char* LOG_TAG = "NimBLEL2CAPChannel";
74
75 const uint16_t psm; // PSM of the channel
76 const uint16_t mtu; // The requested (local) MTU of the channel, might be larger than negotiated MTU
77 struct ble_l2cap_chan* channel = nullptr;
79 uint8_t* receiveBuffer = nullptr; // buffers a full (local) MTU
80
81 // NimBLE memory pool
82 void* _coc_memory = nullptr;
83 struct os_mempool _coc_mempool;
84 struct os_mbuf_pool _coc_mbuf_pool;
85
86 // Runtime handling
87 std::atomic<bool> stalled{false};
88 NimBLETaskData* m_pTaskData{nullptr};
89
90 // Allocate / deallocate NimBLE memory pool
91 bool setupMemPool();
92 void teardownMemPool();
93
94 // Writes data up to the size of the negotiated MTU to the channel.
95 int writeFragment(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end);
96
97 // L2CAP event handler
98 static int handleL2capEvent(struct ble_l2cap_event* event, void* arg);
99};
100
105 public:
106 NimBLEL2CAPChannelCallbacks() = default;
107 virtual ~NimBLEL2CAPChannelCallbacks() = default;
108
112 virtual bool shouldAcceptConnection(NimBLEL2CAPChannel* channel) { return true; }
115 virtual void onConnect(NimBLEL2CAPChannel* channel, uint16_t negotiatedMTU) {};
118 virtual void onRead(NimBLEL2CAPChannel* channel, std::vector<uint8_t>& data) {};
121 virtual void onDisconnect(NimBLEL2CAPChannel* channel) {};
122};
123
124#endif
A model of a BLE client.
Definition NimBLEClient.h:49
Callbacks base class for the L2CAP channel.
Definition NimBLEL2CAPChannel.h:104
virtual void onRead(NimBLEL2CAPChannel *channel, std::vector< uint8_t > &data)
Definition NimBLEL2CAPChannel.h:118
virtual void onDisconnect(NimBLEL2CAPChannel *channel)
Definition NimBLEL2CAPChannel.h:121
virtual bool shouldAcceptConnection(NimBLEL2CAPChannel *channel)
Definition NimBLEL2CAPChannel.h:112
virtual void onConnect(NimBLEL2CAPChannel *channel, uint16_t negotiatedMTU)
Definition NimBLEL2CAPChannel.h:115
Encapsulates a L2CAP channel.
Definition NimBLEL2CAPChannel.h:38
static NimBLEL2CAPChannel * connect(NimBLEClient *client, uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks *callbacks)
Open an L2CAP channel via the specified PSM and MTU.
Definition NimBLEL2CAPChannel.cpp:145
bool write(const std::vector< uint8_t > &bytes)
Write data to the channel.
Definition NimBLEL2CAPChannel.cpp:171
bool isConnected() const
Definition NimBLEL2CAPChannel.h:59
L2CAP server class.
Definition NimBLEL2CAPServer.h:20
A structure to hold data for a task that is waiting for a response.
Definition NimBLEUtils.h:32