77#include " NimBLELog.h"
88#include " NimBLEUtils.h"
99
10- #include " nimble/nimble_port.h"
11-
1210// L2CAP buffer block size
13- #define L2CAP_BUF_BLOCK_SIZE (250 )
11+ #define L2CAP_BUF_BLOCK_SIZE (250 )
1412#define L2CAP_BUF_SIZE_MTUS_PER_CHANNEL (3 )
1513// Round-up integer division
16- #define CEIL_DIVIDE (a, b ) (((a) + (b) - 1 ) / (b))
17- #define ROUND_DIVIDE (a, b ) (((a) + (b) / 2 ) / (b))
14+ #define CEIL_DIVIDE (a, b ) (((a) + (b) - 1 ) / (b))
15+ #define ROUND_DIVIDE (a, b ) (((a) + (b) / 2 ) / (b))
1816// Retry
1917constexpr TickType_t RetryTimeout = pdMS_TO_TICKS(50 );
20- constexpr int RetryCounter = 3 ;
18+ constexpr int RetryCounter = 3 ;
2119
2220NimBLEL2CAPChannel::NimBLEL2CAPChannel (uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks)
23- :psm(psm), mtu(mtu), callbacks(callbacks) {
24-
21+ : psm(psm), mtu(mtu), callbacks(callbacks) {
2522 assert (mtu); // fail here, if MTU is too little
2623 assert (callbacks); // fail here, if no callbacks are given
2724 assert (setupMemPool ()); // fail here, if the memory pool could not be setup
@@ -30,14 +27,12 @@ NimBLEL2CAPChannel::NimBLEL2CAPChannel(uint16_t psm, uint16_t mtu, NimBLEL2CAPCh
3027};
3128
3229NimBLEL2CAPChannel::~NimBLEL2CAPChannel () {
33-
3430 teardownMemPool ();
3531
3632 NIMBLE_LOGI (LOG_TAG, " L2CAP COC 0x%04X shutdown and freed." , this ->psm );
3733}
3834
3935bool NimBLEL2CAPChannel::setupMemPool () {
40-
4136 const size_t buf_blocks = CEIL_DIVIDE (mtu, L2CAP_BUF_BLOCK_SIZE) * L2CAP_BUF_SIZE_MTUS_PER_CHANNEL;
4237 NIMBLE_LOGD (LOG_TAG, " Computed number of buf_blocks = %d" , buf_blocks);
4338
@@ -59,7 +54,7 @@ bool NimBLEL2CAPChannel::setupMemPool() {
5954 return false ;
6055 }
6156
62- this ->receiveBuffer = (uint8_t *) malloc (mtu);
57+ this ->receiveBuffer = (uint8_t *)malloc (mtu);
6358 if (!this ->receiveBuffer ) {
6459 NIMBLE_LOGE (LOG_TAG, " Can't malloc receive buffer: %d, %s" , errno, strerror (errno));
6560 return false ;
@@ -71,14 +66,18 @@ bool NimBLEL2CAPChannel::setupMemPool() {
7166}
7267
7368void NimBLEL2CAPChannel::teardownMemPool () {
74-
75- if (this ->callbacks ) { delete this ->callbacks ; }
76- if (this ->receiveBuffer ) { free (this ->receiveBuffer ); }
77- if (_coc_memory) { free (_coc_memory); }
69+ if (this ->callbacks ) {
70+ delete this ->callbacks ;
71+ }
72+ if (this ->receiveBuffer ) {
73+ free (this ->receiveBuffer );
74+ }
75+ if (_coc_memory) {
76+ free (_coc_memory);
77+ }
7878}
7979
8080int NimBLEL2CAPChannel::writeFragment (std::vector<uint8_t >::const_iterator begin, std::vector<uint8_t >::const_iterator end) {
81-
8281 auto toSend = end - begin;
8382
8483 if (stalled) {
@@ -100,7 +99,6 @@ int NimBLEL2CAPChannel::writeFragment(std::vector<uint8_t>::const_iterator begin
10099 auto retries = RetryCounter;
101100
102101 while (retries--) {
103-
104102 auto txd = os_mbuf_get_pkthdr (&_coc_mbuf_pool, 0 );
105103 if (!txd) {
106104 NIMBLE_LOGE (LOG_TAG, " Can't os_mbuf_get_pkthdr." );
@@ -114,10 +112,15 @@ int NimBLEL2CAPChannel::writeFragment(std::vector<uint8_t>::const_iterator begin
114112
115113 auto res = ble_l2cap_send (channel, txd);
116114 switch (res) {
115+ case 0 :
116+ NIMBLE_LOGD (LOG_TAG, " L2CAP COC 0x%04X sent %d bytes." , this ->psm , toSend);
117+ return 0 ;
118+
117119 case BLE_HS_ESTALLED:
118120 stalled = true ;
119121 NIMBLE_LOGD (LOG_TAG, " L2CAP COC 0x%04X sent %d bytes." , this ->psm , toSend);
120- NIMBLE_LOGW (LOG_TAG, " ble_l2cap_send returned BLE_HS_ESTALLED. Next send will wait for unstalled event..." );
122+ NIMBLE_LOGW (LOG_TAG,
123+ " ble_l2cap_send returned BLE_HS_ESTALLED. Next send will wait for unstalled event..." );
121124 return 0 ;
122125
123126 case BLE_HS_ENOMEM:
@@ -128,25 +131,24 @@ int NimBLEL2CAPChannel::writeFragment(std::vector<uint8_t>::const_iterator begin
128131 vTaskDelay (RetryTimeout);
129132 continue ;
130133
131- case ESP_OK:
132- NIMBLE_LOGD (LOG_TAG, " L2CAP COC 0x%04X sent %d bytes." , this ->psm , toSend);
133- return 0 ;
134-
135134 default :
136135 NIMBLE_LOGE (LOG_TAG, " ble_l2cap_send failed: %d" , res);
137136 return res;
138-
139137 }
140138 }
141139 NIMBLE_LOGE (LOG_TAG, " Retries exhausted, dropping %d bytes to send." , toSend);
142140 return -BLE_HS_EREJECT;
143141}
144142
145143#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
146- NimBLEL2CAPChannel* NimBLEL2CAPChannel::connect (NimBLEClient* client, uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks) {
147-
144+ NimBLEL2CAPChannel* NimBLEL2CAPChannel::connect (NimBLEClient* client,
145+ uint16_t psm,
146+ uint16_t mtu,
147+ NimBLEL2CAPChannelCallbacks* callbacks) {
148148 if (!client->isConnected ()) {
149- NIMBLE_LOGE (LOG_TAG, " Client is not connected. Before connecting via L2CAP, a GAP connection must have been established" );
149+ NIMBLE_LOGE (
150+ LOG_TAG,
151+ " Client is not connected. Before connecting via L2CAP, a GAP connection must have been established" );
150152 return nullptr ;
151153 };
152154
@@ -166,7 +168,6 @@ NimBLEL2CAPChannel* NimBLEL2CAPChannel::connect(NimBLEClient* client, uint16_t p
166168#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL
167169
168170bool NimBLEL2CAPChannel::write (const std::vector<uint8_t >& bytes) {
169-
170171 if (!this ->channel ) {
171172 NIMBLE_LOGW (LOG_TAG, " L2CAP Channel not open" );
172173 return false ;
@@ -176,7 +177,6 @@ bool NimBLEL2CAPChannel::write(const std::vector<uint8_t>& bytes) {
176177 ble_l2cap_get_chan_info (channel, &info);
177178 auto mtu = info.peer_coc_mtu < info.our_coc_mtu ? info.peer_coc_mtu : info.our_coc_mtu ;
178179
179-
180180 auto start = bytes.begin ();
181181 while (start != bytes.end ()) {
182182 auto end = start + mtu < bytes.end () ? start + mtu : bytes.end ();
@@ -190,12 +190,16 @@ bool NimBLEL2CAPChannel::write(const std::vector<uint8_t>& bytes) {
190190
191191// private
192192int NimBLEL2CAPChannel::handleConnectionEvent (struct ble_l2cap_event * event) {
193-
194193 channel = event->connect .chan ;
195194 struct ble_l2cap_chan_info info;
196195 ble_l2cap_get_chan_info (channel, &info);
197- NIMBLE_LOGI (LOG_TAG, " L2CAP COC 0x%04X connected. Local MTU = %d [%d], remote MTU = %d [%d]." , psm,
198- info.our_coc_mtu , info.our_l2cap_mtu , info.peer_coc_mtu , info.peer_l2cap_mtu );
196+ NIMBLE_LOGI (LOG_TAG,
197+ " L2CAP COC 0x%04X connected. Local MTU = %d [%d], remote MTU = %d [%d]." ,
198+ psm,
199+ info.our_coc_mtu ,
200+ info.our_l2cap_mtu ,
201+ info.peer_coc_mtu ,
202+ info.peer_l2cap_mtu );
199203 if (info.our_coc_mtu > info.peer_coc_mtu ) {
200204 NIMBLE_LOGW (LOG_TAG, " L2CAP COC 0x%04X connected, but local MTU is bigger than remote MTU." , psm);
201205 }
@@ -211,7 +215,7 @@ int NimBLEL2CAPChannel::handleAcceptEvent(struct ble_l2cap_event* event) {
211215 return -1 ;
212216 }
213217
214- struct os_mbuf * sdu_rx = os_mbuf_get_pkthdr (&_coc_mbuf_pool, 0 );
218+ struct os_mbuf * sdu_rx = os_mbuf_get_pkthdr (&_coc_mbuf_pool, 0 );
215219 assert (sdu_rx != NULL );
216220 ble_l2cap_recv_ready (event->accept .chan , sdu_rx);
217221 return 0 ;
@@ -260,15 +264,14 @@ int NimBLEL2CAPChannel::handleDisconnectionEvent(struct ble_l2cap_event* event)
260264}
261265
262266/* STATIC */
263- int NimBLEL2CAPChannel::handleL2capEvent (struct ble_l2cap_event *event, void *arg) {
264-
267+ int NimBLEL2CAPChannel::handleL2capEvent (struct ble_l2cap_event * event, void * arg) {
265268 NIMBLE_LOGD (LOG_TAG, " handleL2capEvent: handling l2cap event %d" , event->type );
266269 NimBLEL2CAPChannel* self = reinterpret_cast <NimBLEL2CAPChannel*>(arg);
267270
268271 int returnValue = 0 ;
269272
270273 switch (event->type ) {
271- case BLE_L2CAP_EVENT_COC_CONNECTED:
274+ case BLE_L2CAP_EVENT_COC_CONNECTED:
272275 returnValue = self->handleConnectionEvent (event);
273276 break ;
274277
0 commit comments