Skip to content

Commit 9658b7d

Browse files
Thalleyaescolar
authored andcommitted
samples: Bluetooth: Audio: Avoid uisng K_FOREVER in syswq
Several samples used K_FOREVER when allocating buffers in the system workqueue thread, which is prohibited. The samples should rather retry later if TX fails. This is not the ideal solution for the samples, but fixes a bug. Ideally the samples would use a dedicated thread to handle TX, instead of the system workqueue. Signed-off-by: Emil Gydesen <[email protected]>
1 parent bce51b7 commit 9658b7d

File tree

7 files changed

+57
-18
lines changed

7 files changed

+57
-18
lines changed

samples/bluetooth/bap_unicast_server/src/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,12 @@ static void audio_timer_timeout(struct k_work *work)
229229
for (size_t i = 0; i < configured_source_stream_count; i++) {
230230
struct bt_bap_stream *stream = &source_streams[i].stream;
231231

232-
buf = net_buf_alloc(&tx_pool, K_FOREVER);
232+
buf = net_buf_alloc(&tx_pool, K_NO_WAIT);
233+
if (buf == NULL) {
234+
printk("Failed to allocate TX buffer\n");
235+
/* Break and retry later */
236+
break;
237+
}
233238
net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
234239

235240
net_buf_add_mem(buf, buf_data, ++source_streams[i].len_to_send);

samples/bluetooth/hap_ha/src/bap_unicast_sr.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ static void audio_timer_timeout(struct k_work *work)
157157
for (size_t i = 0; i < configured_source_stream_count; i++) {
158158
struct bt_bap_stream *stream = source_streams[i].stream;
159159

160-
buf = net_buf_alloc(&tx_pool, K_FOREVER);
160+
buf = net_buf_alloc(&tx_pool, K_NO_WAIT);
161+
if (buf == NULL) {
162+
printk("Failed to allocate TX buffer\n");
163+
/* Break and retry later */
164+
break;
165+
}
161166
net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
162167

163168
net_buf_add_mem(buf, buf_data, len_to_send);

samples/bluetooth/iso_broadcast_benchmark/src/broadcaster.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ static void iso_timer_timeout(struct k_work *work)
589589
k_work_reschedule(&iso_send_work, K_USEC(big_create_param.interval - 100));
590590

591591
for (int i = 0; i < big_create_param.num_bis; i++) {
592-
buf = net_buf_alloc(&bis_tx_pool, K_FOREVER);
592+
buf = net_buf_alloc(&bis_tx_pool, K_NO_WAIT);
593593
if (buf == NULL) {
594594
LOG_ERR("Could not allocate buffer");
595595
return;

samples/bluetooth/iso_connected_benchmark/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static void iso_send(struct bt_iso_chan *chan)
202202
interval = (role == ROLE_CENTRAL) ?
203203
cig_create_param.c_to_p_interval : cig_create_param.p_to_c_interval;
204204

205-
buf = net_buf_alloc(&tx_pool, K_FOREVER);
205+
buf = net_buf_alloc(&tx_pool, K_NO_WAIT);
206206
if (buf == NULL) {
207207
LOG_ERR("Could not allocate buffer");
208208
k_work_reschedule(&chan_work->send_work, K_USEC(interval));

samples/bluetooth/pbp_public_broadcast_source/src/main.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct bt_cap_initiator_broadcast_stream_param stream_params;
6868
struct bt_cap_initiator_broadcast_subgroup_param subgroup_param;
6969
struct bt_cap_initiator_broadcast_create_param create_param;
7070
struct bt_cap_broadcast_source *broadcast_source;
71+
static struct k_work_delayable audio_send_work;
7172
struct bt_le_ext_adv *ext_adv;
7273

7374
static void broadcast_started_cb(struct bt_bap_stream *stream)
@@ -110,24 +111,33 @@ static void broadcast_sent_cb(struct bt_bap_stream *stream)
110111
mock_data_initialized = true;
111112
}
112113

113-
buf = net_buf_alloc(&tx_pool, K_FOREVER);
114+
buf = net_buf_alloc(&tx_pool, K_NO_WAIT);
114115
if (buf == NULL) {
115116
printk("Could not allocate buffer when sending on %p\n", stream);
116117

118+
/* Retry next SDU interval */
119+
k_work_schedule(&audio_send_work, K_USEC(broadcast_preset_48_2_1.qos.interval));
117120
return;
118121
}
119122

120123
net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
121124
net_buf_add_mem(buf, mock_data, broadcast_preset_48_2_1.qos.sdu);
122125
ret = bt_bap_stream_send(stream, buf, seq_num++);
123126
if (ret < 0) {
124-
/* This will end broadcasting on this stream. */
127+
printk("Could not send on %p: %d\n", stream, ret);
125128
net_buf_unref(buf);
126129

130+
/* Retry next SDU interval */
131+
k_work_schedule(&audio_send_work, K_USEC(broadcast_preset_48_2_1.qos.interval));
127132
return;
128133
}
129134
}
130135

136+
static void audio_timer_timeout(struct k_work *work)
137+
{
138+
broadcast_sent_cb(&broadcast_stream->bap_stream);
139+
}
140+
131141
static struct bt_bap_stream_ops broadcast_stream_ops = {
132142
.started = broadcast_started_cb,
133143
.stopped = broadcast_stopped_cb,
@@ -319,6 +329,8 @@ int cap_initiator_init(void)
319329
bt_bap_stream_cb_register(&broadcast_stream->bap_stream, &broadcast_stream_ops);
320330
}
321331

332+
k_work_init_delayable(&audio_send_work, audio_timer_timeout);
333+
322334
return 0;
323335
}
324336

samples/bluetooth/tmap_bms/src/cap_initiator.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct bt_cap_initiator_broadcast_stream_param stream_params;
5252
struct bt_cap_initiator_broadcast_subgroup_param subgroup_param;
5353
struct bt_cap_initiator_broadcast_create_param create_param;
5454
struct bt_cap_broadcast_source *broadcast_source;
55+
static struct k_work_delayable audio_send_work;
5556
struct bt_le_ext_adv *ext_adv;
5657

5758
static uint8_t tmap_addata[] = {
@@ -94,22 +95,32 @@ static void broadcast_sent_cb(struct bt_bap_stream *stream)
9495
mock_data_initialized = true;
9596
}
9697

97-
buf = net_buf_alloc(&tx_pool, K_FOREVER);
98+
buf = net_buf_alloc(&tx_pool, K_NO_WAIT);
9899
if (buf == NULL) {
99100
printk("Could not allocate buffer when sending on %p\n", stream);
101+
102+
/* Retry next SDU interval */
103+
k_work_schedule(&audio_send_work, K_USEC(broadcast_preset_48_2_1.qos.interval));
100104
return;
101105
}
102106

103107
net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
104108
net_buf_add_mem(buf, mock_data, broadcast_preset_48_2_1.qos.sdu);
105109
ret = bt_bap_stream_send(stream, buf, seq_num++);
106110
if (ret < 0) {
107-
/* This will end broadcasting on this stream. */
108111
net_buf_unref(buf);
112+
113+
/* Retry next SDU interval */
114+
k_work_schedule(&audio_send_work, K_USEC(broadcast_preset_48_2_1.qos.interval));
109115
return;
110116
}
111117
}
112118

119+
static void audio_timer_timeout(struct k_work *work)
120+
{
121+
broadcast_sent_cb(&broadcast_stream->bap_stream);
122+
}
123+
113124
static struct bt_bap_stream_ops broadcast_stream_ops = {
114125
.started = broadcast_started_cb,
115126
.stopped = broadcast_stopped_cb,
@@ -262,6 +273,7 @@ int cap_initiator_init(void)
262273
{
263274
broadcast_stream = &broadcast_source_stream;
264275
bt_bap_stream_cb_register(&broadcast_stream->bap_stream, &broadcast_stream_ops);
276+
k_work_init_delayable(&audio_send_work, audio_timer_timeout);
265277

266278
return 0;
267279
}

samples/bluetooth/tmap_central/src/cap_initiator.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,17 +393,22 @@ static void audio_timer_timeout(struct k_work *work)
393393
data_initialized = true;
394394
}
395395

396-
buf = net_buf_alloc(&tx_pool, K_FOREVER);
397-
net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
398-
net_buf_add_mem(buf, buf_data, len_to_send);
399-
buf_to_send = buf;
400-
401-
ret = bt_bap_stream_send(stream, buf_to_send, 0);
402-
if (ret < 0) {
403-
printk("Failed to send audio data on streams: (%d)\n", ret);
404-
net_buf_unref(buf_to_send);
396+
buf = net_buf_alloc(&tx_pool, K_NO_WAIT);
397+
if (buf != NULL) {
398+
net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
399+
net_buf_add_mem(buf, buf_data, len_to_send);
400+
buf_to_send = buf;
401+
402+
ret = bt_bap_stream_send(stream, buf_to_send, 0);
403+
if (ret < 0) {
404+
printk("Failed to send audio data on streams: (%d)\n", ret);
405+
net_buf_unref(buf_to_send);
406+
} else {
407+
printk("Sending mock data with len %zu\n", len_to_send);
408+
}
405409
} else {
406-
printk("Sending mock data with len %zu\n", len_to_send);
410+
printk("Failed to allocate TX buffer\n");
411+
/* Retry later */
407412
}
408413

409414
k_work_schedule(&audio_send_work, K_MSEC(1000));

0 commit comments

Comments
 (0)