Skip to content

Commit e29d1ce

Browse files
committed
Refactor L2Cap (style)
1 parent a570274 commit e29d1ce

File tree

4 files changed

+69
-65
lines changed

4 files changed

+69
-65
lines changed

src/NimBLEL2CAPChannel.cpp

Lines changed: 36 additions & 33 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 uint32_t RetryTimeout = 50;
2018
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;
@@ -69,14 +64,18 @@ bool NimBLEL2CAPChannel::setupMemPool() {
6964
}
7065

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

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

169171
bool 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
193193
int 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

src/NimBLEL2CAPChannel.h

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@
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;
@@ -30,8 +36,7 @@ struct NimBLETaskData;
3036
* (which opens the connection) point of view.
3137
*/
3238
class NimBLEL2CAPChannel {
33-
34-
public:
39+
public:
3540
/// @brief Open an L2CAP channel via the specified PSM and MTU.
3641
/// @param[in] psm The PSM to use.
3742
/// @param[in] mtu The MTU to use. Note that this is the local MTU. Upon opening the channel,
@@ -53,8 +58,7 @@ class NimBLEL2CAPChannel {
5358
/// @return True, if the channel is connected. False, otherwise.
5459
bool isConnected() const { return !!channel; }
5560

56-
protected:
57-
61+
protected:
5862
NimBLEL2CAPChannel(uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks);
5963
~NimBLEL2CAPChannel();
6064

@@ -64,19 +68,19 @@ class NimBLEL2CAPChannel {
6468
int handleTxUnstalledEvent(struct ble_l2cap_event* event);
6569
int handleDisconnectionEvent(struct ble_l2cap_event* event);
6670

67-
private:
71+
private:
6872
friend class NimBLEL2CAPServer;
6973
static constexpr const char* LOG_TAG = "NimBLEL2CAPChannel";
7074

71-
const uint16_t psm; // PSM of the channel
72-
const uint16_t mtu; // The requested (local) MTU of the channel, might be larger than negotiated MTU
73-
struct ble_l2cap_chan* channel = nullptr;
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;
7478
NimBLEL2CAPChannelCallbacks* callbacks;
75-
uint8_t* receiveBuffer = nullptr; // buffers a full (local) MTU
79+
uint8_t* receiveBuffer = nullptr; // buffers a full (local) MTU
7680

7781
// NimBLE memory pool
78-
void* _coc_memory = nullptr;
79-
struct os_mempool _coc_mempool;
82+
void* _coc_memory = nullptr;
83+
struct os_mempool _coc_mempool;
8084
struct os_mbuf_pool _coc_mbuf_pool;
8185

8286
// Runtime handling
@@ -91,16 +95,15 @@ class NimBLEL2CAPChannel {
9195
int writeFragment(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end);
9296

9397
// L2CAP event handler
94-
static int handleL2capEvent(struct ble_l2cap_event* event, void *arg);
98+
static int handleL2capEvent(struct ble_l2cap_event* event, void* arg);
9599
};
96100

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

106109
/// 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)