Skip to content

Commit fe67510

Browse files
lylezhu2012m-alperen-sener
authored andcommitted
[nrf fromtree] Bluetooth: rfcomm: Support dynamic channel allocation
In the function `bt_rfcomm_server_register()`, the channel cannot be dynamic allocated. And only pre-set channel is supported. Improve the function `bt_rfcomm_server_register()` to support the dynamic channel allocation if the passed channel is zero. Signed-off-by: Lyle Zhu <[email protected]> (cherry picked from commit 87b2a30)
1 parent 8ac71ed commit fe67510

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

include/zephyr/bluetooth/classic/rfcomm.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ enum {
4747
BT_RFCOMM_CHAN_HSP_AG,
4848
BT_RFCOMM_CHAN_HSP_HS,
4949
BT_RFCOMM_CHAN_SPP,
50+
BT_RFCOMM_CHAN_DYNAMIC_START,
5051
};
5152

5253
struct bt_rfcomm_dlc;
@@ -125,7 +126,16 @@ struct bt_rfcomm_dlc {
125126
};
126127

127128
struct bt_rfcomm_server {
128-
/** Server Channel */
129+
/** Server Channel
130+
*
131+
* Possible values:
132+
* 0 A dynamic value will be auto-allocated when bt_rfcomm_server_register() is
133+
* called.
134+
*
135+
* 0x01 - 0x1e Dynamically allocated. May be pre-set by the application before server
136+
* registration (not recommended however), or auto-allocated by the stack
137+
* if the 0 is passed.
138+
*/
129139
uint8_t channel;
130140

131141
/** Server accept callback

subsys/bluetooth/host/classic/rfcomm.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,32 @@ rfcomm_sessions_lookup_bt_conn(struct bt_conn *conn)
199199

200200
int bt_rfcomm_server_register(struct bt_rfcomm_server *server)
201201
{
202-
if (server->channel < RFCOMM_CHANNEL_START ||
203-
server->channel > RFCOMM_CHANNEL_END || !server->accept) {
202+
if (server->channel > RFCOMM_CHANNEL_END || !server->accept) {
204203
return -EINVAL;
205204
}
206205

207-
/* Check if given channel is already in use */
208-
if (rfcomm_server_lookup_channel(server->channel)) {
209-
LOG_DBG("Channel already registered");
210-
return -EADDRINUSE;
206+
if (!server->channel) {
207+
uint8_t chan = (uint8_t)BT_RFCOMM_CHAN_DYNAMIC_START;
208+
209+
for (; chan <= RFCOMM_CHANNEL_END; chan++) {
210+
/* Check if given channel is already in use */
211+
if (!rfcomm_server_lookup_channel(chan)) {
212+
server->channel = chan;
213+
LOG_DBG("Allocated channel 0x%02x for new server", chan);
214+
break;
215+
}
216+
}
217+
218+
if (!server->channel) {
219+
LOG_WRN("No free dynamic rfcomm channels available");
220+
return -EADDRNOTAVAIL;
221+
}
222+
} else {
223+
/* Check if given channel is already in use */
224+
if (rfcomm_server_lookup_channel(server->channel)) {
225+
LOG_WRN("Channel already registered");
226+
return -EADDRINUSE;
227+
}
211228
}
212229

213230
LOG_DBG("Channel 0x%02x", server->channel);

0 commit comments

Comments
 (0)