Skip to content

Commit bbaf576

Browse files
committed
Refactor L2Cap (style)
1 parent dadbc0d commit bbaf576

File tree

4 files changed

+72
-68
lines changed

4 files changed

+72
-68
lines changed

src/NimBLEL2CAPChannel.cpp

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
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
1917
constexpr TickType_t RetryTimeout = pdMS_TO_TICKS(50);
20-
constexpr int RetryCounter = 3;
18+
constexpr int RetryCounter = 3;
2119

2220
NimBLEL2CAPChannel::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

3229
NimBLEL2CAPChannel::~NimBLEL2CAPChannel() {
33-
3430
teardownMemPool();
3531

3632
NIMBLE_LOGI(LOG_TAG, "L2CAP COC 0x%04X shutdown and freed.", this->psm);
3733
}
3834

3935
bool 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

7368
void 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

8080
int 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

168170
bool 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
192192
int 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

src/NimBLEL2CAPChannel.h

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,39 @@
33
//
44
#pragma once
55
#ifndef NIMBLEL2CAPCHANNEL_H
6-
#define NIMBLEL2CAPCHANNEL_H
6+
# define NIMBLEL2CAPCHANNEL_H
77

8-
#include "inttypes.h"
9-
#include "host/ble_l2cap.h"
10-
#include "os/os_mbuf.h"
11-
#include "os/os_mempool.h"
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
1218

1319
/**** FIX COMPILATION ****/
1420
# undef min
1521
# undef max
1622
/**************************/
1723

18-
#include <vector>
19-
#include <atomic>
24+
# include <vector>
25+
# include <atomic>
2026

2127
class NimBLEClient;
2228
class NimBLEL2CAPChannelCallbacks;
2329

2430
/**
2531
* @brief Encapsulates a L2CAP channel.
26-
*
32+
*
2733
* This class is used to encapsulate a L2CAP connection oriented channel, both
2834
* from the "server" (which waits for the connection to be opened) and the "client"
2935
* (which opens the connection) point of view.
3036
*/
3137
class NimBLEL2CAPChannel {
32-
33-
public:
38+
public:
3439
/// @brief Open an L2CAP channel via the specified PSM and MTU.
3540
/// @param[in] psm The PSM to use.
3641
/// @param[in] mtu The MTU to use. Note that this is the local MTU. Upon opening the channel,
@@ -52,8 +57,7 @@ class NimBLEL2CAPChannel {
5257
/// @return True, if the channel is connected. False, otherwise.
5358
bool isConnected() const { return !!channel; }
5459

55-
protected:
56-
60+
protected:
5761
NimBLEL2CAPChannel(uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
5862
~NimBLEL2CAPChannel();
5963

@@ -63,19 +67,19 @@ class NimBLEL2CAPChannel {
6367
int handleTxUnstalledEvent(struct ble_l2cap_event* event);
6468
int handleDisconnectionEvent(struct ble_l2cap_event* event);
6569

66-
private:
70+
private:
6771
friend class NimBLEL2CAPServer;
6872
static constexpr const char* LOG_TAG = "NimBLEL2CAPChannel";
6973

70-
const uint16_t psm; // PSM of the channel
71-
const uint16_t mtu; // The requested (local) MTU of the channel, might be larger than negotiated MTU
72-
struct ble_l2cap_chan* channel = nullptr;
74+
const uint16_t psm; // PSM of the channel
75+
const uint16_t mtu; // The requested (local) MTU of the channel, might be larger than negotiated MTU
76+
struct ble_l2cap_chan* channel = nullptr;
7377
NimBLEL2CAPChannelCallbacks* callbacks;
74-
uint8_t* receiveBuffer = nullptr; // buffers a full (local) MTU
78+
uint8_t* receiveBuffer = nullptr; // buffers a full (local) MTU
7579

7680
// NimBLE memory pool
77-
void* _coc_memory = nullptr;
78-
struct os_mempool _coc_mempool;
81+
void* _coc_memory = nullptr;
82+
struct os_mempool _coc_mempool;
7983
struct os_mbuf_pool _coc_mbuf_pool;
8084

8185
// Runtime handling
@@ -90,16 +94,15 @@ class NimBLEL2CAPChannel {
9094
int writeFragment(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end);
9195

9296
// L2CAP event handler
93-
static int handleL2capEvent(struct ble_l2cap_event* event, void *arg);
97+
static int handleL2capEvent(struct ble_l2cap_event* event, void* arg);
9498
};
9599

96100
/**
97101
* @brief Callbacks base class for the L2CAP channel.
98102
*/
99103
class NimBLEL2CAPChannelCallbacks {
100-
101-
public:
102-
NimBLEL2CAPChannelCallbacks() = default;
104+
public:
105+
NimBLEL2CAPChannelCallbacks() = default;
103106
virtual ~NimBLEL2CAPChannelCallbacks() = default;
104107

105108
/// Called when the client attempts to open a channel on the server.

src/NimBLEL2CAPServer.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@
99
static const char* LOG_TAG = "NimBLEL2CAPServer";
1010

1111
NimBLEL2CAPServer::NimBLEL2CAPServer() {
12-
1312
// Nothing to do here...
1413
}
1514

1615
NimBLEL2CAPServer::~NimBLEL2CAPServer() {
17-
1816
// Delete all services
19-
for (auto service: this->services) {
17+
for (auto service : this->services) {
2018
delete service;
2119
}
2220
}
2321

24-
NimBLEL2CAPChannel* NimBLEL2CAPServer::createService(const uint16_t psm, const uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks) {
25-
22+
NimBLEL2CAPChannel* NimBLEL2CAPServer::createService(const uint16_t psm,
23+
const uint16_t mtu,
24+
NimBLEL2CAPChannelCallbacks* callbacks) {
2625
auto service = new NimBLEL2CAPChannel(psm, mtu, callbacks);
27-
auto rc = ble_l2cap_create_server(psm, mtu, NimBLEL2CAPChannel::handleL2capEvent, service);
26+
auto rc = ble_l2cap_create_server(psm, mtu, NimBLEL2CAPChannel::handleL2capEvent, service);
2827

2928
if (rc != 0) {
3029
NIMBLE_LOGE(LOG_TAG, "Could not ble_l2cap_create_server: %d", rc);

src/NimBLEL2CAPServer.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@
1111
class NimBLEL2CAPChannel;
1212
class NimBLEL2CAPChannelCallbacks;
1313

14-
1514
/**
1615
* @brief L2CAP server class.
17-
*
16+
*
1817
* Encapsulates a L2CAP server that can hold multiple services. Every service is represented by a channel object
1918
* and an assorted set of callbacks.
2019
*/
2120
class NimBLEL2CAPServer {
22-
public:
21+
public:
2322
/// @brief Register a new L2CAP service instance.
2423
/// @param psm The port multiplexor service number.
2524
/// @param mtu The maximum transmission unit.
2625
/// @param callbacks The callbacks for this service.
2726
/// @return the newly created object, if the server registration was successful.
2827
NimBLEL2CAPChannel* createService(const uint16_t psm, const uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
2928

30-
private:
29+
private:
3130
NimBLEL2CAPServer();
3231
~NimBLEL2CAPServer();
3332
std::vector<NimBLEL2CAPChannel*> services;

0 commit comments

Comments
 (0)