Skip to content

Commit e5e5e63

Browse files
committed
subsys/shell: update to use new ring_buffer api
Move to new ring_buffer API. Signed-off-by: Måns Ansgariusson <[email protected]>
1 parent 3b2d943 commit e5e5e63

File tree

5 files changed

+27
-37
lines changed

5 files changed

+27
-37
lines changed

include/zephyr/shell/shell_mqtt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct shell_mqtt {
4747
/** Handler function registered by shell. */
4848
shell_transport_handler_t shell_handler;
4949

50-
struct ring_buf rx_rb;
50+
struct ring_buffer rx_rb;
5151
uint8_t rx_rb_buf[RX_RB_SIZE];
5252
uint8_t *rx_rb_ptr;
5353

include/zephyr/shell/shell_uart.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ struct shell_uart_common {
4949

5050
struct shell_uart_int_driven {
5151
struct shell_uart_common common;
52-
struct ring_buf tx_ringbuf;
53-
struct ring_buf rx_ringbuf;
52+
struct ring_buffer tx_ringbuf;
53+
struct ring_buffer rx_ringbuf;
5454
uint8_t tx_buf[CONFIG_SHELL_BACKEND_SERIAL_TX_RING_BUFFER_SIZE];
5555
uint8_t rx_buf[CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE];
5656
struct k_timer dtr_timer;
@@ -68,7 +68,7 @@ struct shell_uart_async {
6868

6969
struct shell_uart_polling {
7070
struct shell_uart_common common;
71-
struct ring_buf rx_ringbuf;
71+
struct ring_buffer rx_ringbuf;
7272
uint8_t rx_buf[CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE];
7373
struct k_timer rx_timer;
7474
};

subsys/shell/backends/Kconfig.backends

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ config SHELL_BACKEND_SERIAL
1818
bool "Serial backend"
1919
default "$(dt_chosen_enabled,$(DT_CHOSEN_Z_SHELL_UART))"
2020
select SERIAL
21-
select RING_BUFFER
2221
help
2322
Enable serial backend.
2423

subsys/shell/backends/shell_mqtt.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -573,20 +573,20 @@ static void mqtt_evt_handler(struct mqtt_client *const client, const struct mqtt
573573

574574
while (payload_left > 0) {
575575
/* Attempt to claim `payload_left` bytes of buffer in rb */
576-
size = (size_t)ring_buf_put_claim(&sh->rx_rb, &sh->rx_rb_ptr,
577-
payload_left);
576+
size = (size_t)MIN(ring_buffer_write_ptr(&sh->rx_rb, &sh->rx_rb_ptr),
577+
payload_left);
578578
/* Read `size` bytes of payload from mqtt */
579579
rc = mqtt_read_publish_payload_blocking(client, sh->rx_rb_ptr, size);
580580

581581
/* errno value, return */
582582
if (rc < 0) {
583-
ring_buf_reset(&sh->rx_rb);
583+
ring_buffer_reset(&sh->rx_rb);
584584
return;
585585
}
586586

587587
size = (size_t)rc;
588588
/* Indicate that `size` bytes of payload has been written into rb */
589-
(void)ring_buf_put_finish(&sh->rx_rb, size);
589+
ring_buffer_commit(&sh->rx_rb, size);
590590
/* Update `payload_left` */
591591
payload_left -= size;
592592
/* Tells the shell that we have new data for it */
@@ -598,9 +598,9 @@ static void mqtt_evt_handler(struct mqtt_client *const client, const struct mqtt
598598
/* Shell won't execute the cmds without \r\n */
599599
while (true) {
600600
/* Check if rb's free space is enough to fit in \r\n */
601-
size = ring_buf_space_get(&sh->rx_rb);
601+
size = ring_buffer_space(&sh->rx_rb);
602602
if (size >= sizeof("\r\n")) {
603-
(void)ring_buf_put(&sh->rx_rb, "\r\n", sizeof("\r\n"));
603+
(void)ring_buffer_write(&sh->rx_rb, "\r\n", sizeof("\r\n"));
604604
break;
605605
}
606606
/* Arbitrary sleep for the shell to do its thing */
@@ -652,7 +652,7 @@ static int init(const struct shell_transport *transport, const void *config,
652652
(void)snprintf(sh->sub_topic, SH_MQTT_TOPIC_RX_MAX_SIZE, "%s" CONFIG_SHELL_MQTT_TOPIC_RX_ID,
653653
sh->device_id);
654654

655-
ring_buf_init(&sh->rx_rb, RX_RB_SIZE, sh->rx_rb_buf);
655+
ring_buffer_init(&sh->rx_rb, sh->rx_rb_buf, RX_RB_SIZE);
656656

657657
LOG_DBG("Initializing shell MQTT backend");
658658

@@ -795,10 +795,10 @@ static int read_data(const struct shell_transport *transport, void *data, size_t
795795
return 0;
796796
}
797797

798-
*cnt = ring_buf_get(&sh->rx_rb, data, length);
798+
*cnt = ring_buffer_read(&sh->rx_rb, data, length);
799799

800800
/* Inform the shell if there are still data in the rb */
801-
if (ring_buf_size_get(&sh->rx_rb) > 0) {
801+
if (ring_buffer_size(&sh->rx_rb) > 0) {
802802
sh->shell_handler(SHELL_TRANSPORT_EVT_RX_RDY, sh->shell_context);
803803
}
804804

subsys/shell/backends/shell_uart.c

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ static void uart_rx_handle(const struct device *dev, struct shell_uart_int_drive
7878
#endif
7979

8080
do {
81-
len = ring_buf_put_claim(&sh_uart->rx_ringbuf, &data,
82-
sh_uart->rx_ringbuf.size);
83-
81+
len = ring_buffer_write_ptr(&sh_uart->rx_ringbuf, &data);
8482
if (len > 0) {
8583
rd_len = uart_fifo_read(dev, data, len);
8684

@@ -105,9 +103,7 @@ static void uart_rx_handle(const struct device *dev, struct shell_uart_int_drive
105103
}
106104
}
107105
#endif /* CONFIG_MCUMGR_TRANSPORT_SHELL */
108-
int err = ring_buf_put_finish(&sh_uart->rx_ringbuf, rd_len);
109-
(void)err;
110-
__ASSERT_NO_MSG(err == 0);
106+
ring_buffer_commit(&sh_uart->rx_ringbuf, rd_len);
111107
} else {
112108
uint8_t dummy;
113109

@@ -176,15 +172,10 @@ static void uart_tx_handle(const struct device *dev, struct shell_uart_int_drive
176172
return;
177173
}
178174

179-
len = ring_buf_get_claim(&sh_uart->tx_ringbuf, (uint8_t **)&data,
180-
sh_uart->tx_ringbuf.size);
175+
len = ring_buffer_read_ptr(&sh_uart->tx_ringbuf, (uint8_t **)&data);
181176
if (len) {
182-
int err;
183-
184177
len = uart_fifo_fill(dev, data, len);
185-
err = ring_buf_get_finish(&sh_uart->tx_ringbuf, len);
186-
__ASSERT_NO_MSG(err == 0);
187-
ARG_UNUSED(err);
178+
ring_buffer_consume(&sh_uart->tx_ringbuf, len);
188179
} else {
189180
uart_irq_tx_disable(dev);
190181
sh_uart->tx_busy = 0;
@@ -212,10 +203,10 @@ static void irq_init(struct shell_uart_int_driven *sh_uart)
212203
{
213204
const struct device *dev = sh_uart->common.dev;
214205

215-
ring_buf_init(&sh_uart->rx_ringbuf, CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE,
216-
sh_uart->rx_buf);
217-
ring_buf_init(&sh_uart->tx_ringbuf, CONFIG_SHELL_BACKEND_SERIAL_TX_RING_BUFFER_SIZE,
218-
sh_uart->tx_buf);
206+
ring_buffer_init(&sh_uart->rx_ringbuf, sh_uart->rx_buf,
207+
CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE);
208+
ring_buffer_init(&sh_uart->tx_ringbuf, sh_uart->tx_buf,
209+
CONFIG_SHELL_BACKEND_SERIAL_TX_RING_BUFFER_SIZE);
219210
sh_uart->tx_busy = 0;
220211
uart_irq_callback_user_data_set(dev, uart_callback, (void *)sh_uart);
221212
uart_irq_rx_enable(dev);
@@ -266,7 +257,7 @@ static void polling_rx_timeout_handler(struct k_timer *timer)
266257
struct shell_uart_polling *sh_uart = k_timer_user_data_get(timer);
267258

268259
while (uart_poll_in(sh_uart->common.dev, &c) == 0) {
269-
if (ring_buf_put(&sh_uart->rx_ringbuf, &c, 1) == 0U) {
260+
if (ring_buffer_write(&sh_uart->rx_ringbuf, &c, 1) == 0U) {
270261
/* ring buffer full. */
271262
LOG_WRN("RX ring buffer full.");
272263
}
@@ -280,8 +271,8 @@ static void polling_init(struct shell_uart_polling *sh_uart)
280271
k_timer_user_data_set(&sh_uart->rx_timer, (void *)sh_uart);
281272
k_timer_start(&sh_uart->rx_timer, RX_POLL_PERIOD, RX_POLL_PERIOD);
282273

283-
ring_buf_init(&sh_uart->rx_ringbuf, CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE,
284-
sh_uart->rx_buf);
274+
ring_buffer_init(&sh_uart->rx_ringbuf, sh_uart->rx_buf,
275+
CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE);
285276
}
286277

287278
static int init(const struct shell_transport *transport,
@@ -375,7 +366,7 @@ static int polling_write(struct shell_uart_common *sh_uart,
375366
static int irq_write(struct shell_uart_int_driven *sh_uart,
376367
const void *data, size_t length, size_t *cnt)
377368
{
378-
*cnt = ring_buf_put(&sh_uart->tx_ringbuf, data, length);
369+
*cnt = ring_buffer_write(&sh_uart->tx_ringbuf, data, length);
379370

380371
if (atomic_set(&sh_uart->tx_busy, 1) == 0) {
381372
uart_irq_tx_enable(sh_uart->common.dev);
@@ -420,15 +411,15 @@ static int write_uart(const struct shell_transport *transport,
420411
static int irq_read(struct shell_uart_int_driven *sh_uart,
421412
void *data, size_t length, size_t *cnt)
422413
{
423-
*cnt = ring_buf_get(&sh_uart->rx_ringbuf, data, length);
414+
*cnt = ring_buffer_read(&sh_uart->rx_ringbuf, data, length);
424415

425416
return 0;
426417
}
427418

428419
static int polling_read(struct shell_uart_polling *sh_uart,
429420
void *data, size_t length, size_t *cnt)
430421
{
431-
*cnt = ring_buf_get(&sh_uart->rx_ringbuf, data, length);
422+
*cnt = ring_buffer_read(&sh_uart->rx_ringbuf, data, length);
432423

433424
return 0;
434425
}

0 commit comments

Comments
 (0)