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 uint32_t RetryTimeout = 50 ;
2018constexpr 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 ;
@@ -69,14 +64,18 @@ bool NimBLEL2CAPChannel::setupMemPool() {
6964}
7065
7166void NimBLEL2CAPChannel::teardownMemPool () {
72-
73- if (this ->callbacks ) { delete this ->callbacks ; }
74- if (this ->receiveBuffer ) { free (this ->receiveBuffer ); }
75- if (_coc_memory) { free (_coc_memory); }
67+ if (this ->callbacks ) {
68+ delete this ->callbacks ;
69+ }
70+ if (this ->receiveBuffer ) {
71+ free (this ->receiveBuffer );
72+ }
73+ if (_coc_memory) {
74+ free (_coc_memory);
75+ }
7676}
7777
7878int NimBLEL2CAPChannel::writeFragment (std::vector<uint8_t >::const_iterator begin, std::vector<uint8_t >::const_iterator end) {
79-
8079 auto toSend = end - begin;
8180
8281 if (stalled) {
@@ -101,7 +100,6 @@ int NimBLEL2CAPChannel::writeFragment(std::vector<uint8_t>::const_iterator begin
101100 auto retries = RetryCounter;
102101
103102 while (retries--) {
104-
105103 auto txd = os_mbuf_get_pkthdr (&_coc_mbuf_pool, 0 );
106104 if (!txd) {
107105 NIMBLE_LOGE (LOG_TAG, " Can't os_mbuf_get_pkthdr." );
@@ -115,10 +113,15 @@ int NimBLEL2CAPChannel::writeFragment(std::vector<uint8_t>::const_iterator begin
115113
116114 auto res = ble_l2cap_send (channel, txd);
117115 switch (res) {
116+ case 0 :
117+ NIMBLE_LOGD (LOG_TAG, " L2CAP COC 0x%04X sent %d bytes." , this ->psm , toSend);
118+ return 0 ;
119+
118120 case BLE_HS_ESTALLED:
119121 stalled = true ;
120122 NIMBLE_LOGD (LOG_TAG, " L2CAP COC 0x%04X sent %d bytes." , this ->psm , toSend);
121- NIMBLE_LOGW (LOG_TAG, " ble_l2cap_send returned BLE_HS_ESTALLED. Next send will wait for unstalled event..." );
123+ NIMBLE_LOGW (LOG_TAG,
124+ " ble_l2cap_send returned BLE_HS_ESTALLED. Next send will wait for unstalled event..." );
122125 return 0 ;
123126
124127 case BLE_HS_ENOMEM:
@@ -129,25 +132,24 @@ int NimBLEL2CAPChannel::writeFragment(std::vector<uint8_t>::const_iterator begin
129132 ble_npl_time_delay (ble_npl_time_ms_to_ticks32 (RetryTimeout));
130133 continue ;
131134
132- case ESP_OK:
133- NIMBLE_LOGD (LOG_TAG, " L2CAP COC 0x%04X sent %d bytes." , this ->psm , toSend);
134- return 0 ;
135-
136135 default :
137136 NIMBLE_LOGE (LOG_TAG, " ble_l2cap_send failed: %d" , res);
138137 return res;
139-
140138 }
141139 }
142140 NIMBLE_LOGE (LOG_TAG, " Retries exhausted, dropping %d bytes to send." , toSend);
143141 return -BLE_HS_EREJECT;
144142}
145143
146144#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
147- NimBLEL2CAPChannel* NimBLEL2CAPChannel::connect (NimBLEClient* client, uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks) {
148-
145+ NimBLEL2CAPChannel* NimBLEL2CAPChannel::connect (NimBLEClient* client,
146+ uint16_t psm,
147+ uint16_t mtu,
148+ NimBLEL2CAPChannelCallbacks* callbacks) {
149149 if (!client->isConnected ()) {
150- NIMBLE_LOGE (LOG_TAG, " Client is not connected. Before connecting via L2CAP, a GAP connection must have been established" );
150+ NIMBLE_LOGE (
151+ LOG_TAG,
152+ " Client is not connected. Before connecting via L2CAP, a GAP connection must have been established" );
151153 return nullptr ;
152154 };
153155
@@ -167,7 +169,6 @@ NimBLEL2CAPChannel* NimBLEL2CAPChannel::connect(NimBLEClient* client, uint16_t p
167169#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL
168170
169171bool NimBLEL2CAPChannel::write (const std::vector<uint8_t >& bytes) {
170-
171172 if (!this ->channel ) {
172173 NIMBLE_LOGW (LOG_TAG, " L2CAP Channel not open" );
173174 return false ;
@@ -177,7 +178,6 @@ bool NimBLEL2CAPChannel::write(const std::vector<uint8_t>& bytes) {
177178 ble_l2cap_get_chan_info (channel, &info);
178179 auto mtu = info.peer_coc_mtu < info.our_coc_mtu ? info.peer_coc_mtu : info.our_coc_mtu ;
179180
180-
181181 auto start = bytes.begin ();
182182 while (start != bytes.end ()) {
183183 auto end = start + mtu < bytes.end () ? start + mtu : bytes.end ();
@@ -191,12 +191,16 @@ bool NimBLEL2CAPChannel::write(const std::vector<uint8_t>& bytes) {
191191
192192// private
193193int NimBLEL2CAPChannel::handleConnectionEvent (struct ble_l2cap_event * event) {
194-
195194 channel = event->connect .chan ;
196195 struct ble_l2cap_chan_info info;
197196 ble_l2cap_get_chan_info (channel, &info);
198- NIMBLE_LOGI (LOG_TAG, " L2CAP COC 0x%04X connected. Local MTU = %d [%d], remote MTU = %d [%d]." , psm,
199- info.our_coc_mtu , info.our_l2cap_mtu , info.peer_coc_mtu , info.peer_l2cap_mtu );
197+ NIMBLE_LOGI (LOG_TAG,
198+ " L2CAP COC 0x%04X connected. Local MTU = %d [%d], remote MTU = %d [%d]." ,
199+ psm,
200+ info.our_coc_mtu ,
201+ info.our_l2cap_mtu ,
202+ info.peer_coc_mtu ,
203+ info.peer_l2cap_mtu );
200204 if (info.our_coc_mtu > info.peer_coc_mtu ) {
201205 NIMBLE_LOGW (LOG_TAG, " L2CAP COC 0x%04X connected, but local MTU is bigger than remote MTU." , psm);
202206 }
@@ -212,7 +216,7 @@ int NimBLEL2CAPChannel::handleAcceptEvent(struct ble_l2cap_event* event) {
212216 return -1 ;
213217 }
214218
215- struct os_mbuf * sdu_rx = os_mbuf_get_pkthdr (&_coc_mbuf_pool, 0 );
219+ struct os_mbuf * sdu_rx = os_mbuf_get_pkthdr (&_coc_mbuf_pool, 0 );
216220 assert (sdu_rx != NULL );
217221 ble_l2cap_recv_ready (event->accept .chan , sdu_rx);
218222 return 0 ;
@@ -264,8 +268,7 @@ int NimBLEL2CAPChannel::handleDisconnectionEvent(struct ble_l2cap_event* event)
264268}
265269
266270/* STATIC */
267- int NimBLEL2CAPChannel::handleL2capEvent (struct ble_l2cap_event *event, void *arg) {
268-
271+ int NimBLEL2CAPChannel::handleL2capEvent (struct ble_l2cap_event * event, void * arg) {
269272 NIMBLE_LOGD (LOG_TAG, " handleL2capEvent: handling l2cap event %d" , event->type );
270273 NimBLEL2CAPChannel* self = reinterpret_cast <NimBLEL2CAPChannel*>(arg);
271274
0 commit comments