Skip to content

Commit

Permalink
Bluetooth: rfcomm: Support dynamic channel allocation
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
lylezhu2012 authored and kartben committed Jan 17, 2025
1 parent fe92367 commit 87b2a30
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
12 changes: 11 additions & 1 deletion include/zephyr/bluetooth/classic/rfcomm.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum {
BT_RFCOMM_CHAN_HSP_AG,
BT_RFCOMM_CHAN_HSP_HS,
BT_RFCOMM_CHAN_SPP,
BT_RFCOMM_CHAN_DYNAMIC_START,
};

struct bt_rfcomm_dlc;
Expand Down Expand Up @@ -125,7 +126,16 @@ struct bt_rfcomm_dlc {
};

struct bt_rfcomm_server {
/** Server Channel */
/** Server Channel
*
* Possible values:
* 0 A dynamic value will be auto-allocated when bt_rfcomm_server_register() is
* called.
*
* 0x01 - 0x1e Dynamically allocated. May be pre-set by the application before server
* registration (not recommended however), or auto-allocated by the stack
* if the 0 is passed.
*/
uint8_t channel;

/** Server accept callback
Expand Down
29 changes: 23 additions & 6 deletions subsys/bluetooth/host/classic/rfcomm.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,32 @@ rfcomm_sessions_lookup_bt_conn(struct bt_conn *conn)

int bt_rfcomm_server_register(struct bt_rfcomm_server *server)
{
if (server->channel < RFCOMM_CHANNEL_START ||
server->channel > RFCOMM_CHANNEL_END || !server->accept) {
if (server->channel > RFCOMM_CHANNEL_END || !server->accept) {
return -EINVAL;
}

/* Check if given channel is already in use */
if (rfcomm_server_lookup_channel(server->channel)) {
LOG_DBG("Channel already registered");
return -EADDRINUSE;
if (!server->channel) {
uint8_t chan = (uint8_t)BT_RFCOMM_CHAN_DYNAMIC_START;

for (; chan <= RFCOMM_CHANNEL_END; chan++) {
/* Check if given channel is already in use */
if (!rfcomm_server_lookup_channel(chan)) {
server->channel = chan;
LOG_DBG("Allocated channel 0x%02x for new server", chan);
break;
}
}

if (!server->channel) {
LOG_WRN("No free dynamic rfcomm channels available");
return -EADDRNOTAVAIL;
}
} else {
/* Check if given channel is already in use */
if (rfcomm_server_lookup_channel(server->channel)) {
LOG_WRN("Channel already registered");
return -EADDRINUSE;
}
}

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

0 comments on commit 87b2a30

Please sign in to comment.