Skip to content

Commit cfc64dd

Browse files
committed
shell: Add user data argument to shell_set_bypass
Allow passing some context to the shell bypass callback function by providing a void pointer user data argument. Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
1 parent d02cdc7 commit cfc64dd

File tree

8 files changed

+39
-20
lines changed

8 files changed

+39
-20
lines changed

drivers/can/can_shell.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ static bool can_device_check(const struct device *dev)
7474
}
7575

7676
#ifdef CONFIG_CAN_SHELL_SCRIPTING_FRIENDLY
77-
static void can_shell_dummy_bypass_cb(const struct shell *sh, uint8_t *data, size_t len)
77+
static void can_shell_dummy_bypass_cb(const struct shell *sh, uint8_t *data, size_t len,
78+
void *user_data)
7879
{
7980
ARG_UNUSED(sh);
8081
ARG_UNUSED(data);
8182
ARG_UNUSED(len);
83+
ARG_UNUSED(user_data);
8284
}
8385
#endif /* CONFIG_CAN_SHELL_SCRIPTING_FRIENDLY */
8486

@@ -90,7 +92,7 @@ static void can_shell_print_frame(const struct shell *sh, const struct device *d
9092

9193
#ifdef CONFIG_CAN_SHELL_SCRIPTING_FRIENDLY
9294
/* Bypass the shell to avoid breaking up the line containing the frame */
93-
shell_set_bypass(sh, can_shell_dummy_bypass_cb);
95+
shell_set_bypass(sh, can_shell_dummy_bypass_cb, NULL);
9496
#endif /* CONFIG_CAN_SHELL_SCRIPTING_FRIENDLY */
9597

9698
#ifdef CONFIG_CAN_RX_TIMESTAMP
@@ -132,7 +134,7 @@ static void can_shell_print_frame(const struct shell *sh, const struct device *d
132134
shell_fprintf_normal(sh, "\n");
133135

134136
#ifdef CONFIG_CAN_SHELL_SCRIPTING_FRIENDLY
135-
shell_set_bypass(sh, NULL);
137+
shell_set_bypass(sh, NULL, NULL);
136138
#endif /* CONFIG_CAN_SHELL_SCRIPTING_FRIENDLY */
137139
}
138140

drivers/flash/flash_shell.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,17 +617,19 @@ static int set_bypass(const struct shell *sh, shell_bypass_cb_t bypass)
617617
shell_print(sh, "Loading...");
618618
}
619619

620-
shell_set_bypass(sh, bypass);
620+
shell_set_bypass(sh, bypass, NULL);
621621

622622
return 0;
623623
}
624624

625-
static void bypass_cb(const struct shell *sh, uint8_t *recv, size_t len)
625+
static void bypass_cb(const struct shell *sh, uint8_t *recv, size_t len, void *user_data)
626626
{
627627
uint32_t left_to_read = flash_load_total - flash_load_written - flash_load_boff;
628628
uint32_t to_copy = MIN(len, left_to_read);
629629
uint32_t copied = 0;
630630

631+
ARG_UNUSED(user_data);
632+
631633
while (copied < to_copy) {
632634

633635
uint32_t buf_copy = MIN(to_copy, flash_load_buf_size - flash_load_boff);

include/zephyr/shell/shell.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,8 @@ typedef void (*shell_uninit_cb_t)(const struct shell *sh, int res);
789789
*/
790790
typedef void (*shell_bypass_cb_t)(const struct shell *sh,
791791
uint8_t *data,
792-
size_t len);
792+
size_t len,
793+
void *user_data);
793794

794795
struct shell_transport;
795796

@@ -990,6 +991,9 @@ struct shell_ctx {
990991
/** When bypass is set, all incoming data is passed to the callback. */
991992
shell_bypass_cb_t bypass;
992993

994+
/** When bypass is set, this user data pointer is passed to the callback. */
995+
void *bypass_user_data;
996+
993997
/*!< Logging level for a backend. */
994998
uint32_t log_level;
995999

@@ -1364,8 +1368,9 @@ int shell_set_root_cmd(const char *cmd);
13641368
*
13651369
* @param[in] sh Pointer to the shell instance.
13661370
* @param[in] bypass Bypass callback or null to disable.
1371+
* @param[in] user_data Bypass callback user data.
13671372
*/
1368-
void shell_set_bypass(const struct shell *sh, shell_bypass_cb_t bypass);
1373+
void shell_set_bypass(const struct shell *sh, shell_bypass_cb_t bypass, void *user_data);
13691374

13701375
/** @brief Get shell readiness to execute commands.
13711376
*

samples/subsys/shell/shell_module/src/main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,21 @@ static int set_bypass(const struct shell *sh, shell_bypass_cb_t bypass)
255255
in_use = true;
256256
}
257257

258-
shell_set_bypass(sh, bypass);
258+
shell_set_bypass(sh, bypass, NULL);
259259

260260
return 0;
261261
}
262262

263263
#define CHAR_1 0x18
264264
#define CHAR_2 0x11
265265

266-
static void bypass_cb(const struct shell *sh, uint8_t *data, size_t len)
266+
static void bypass_cb(const struct shell *sh, uint8_t *data, size_t len, void *user_data)
267267
{
268268
static uint8_t tail;
269269
bool escape = false;
270270

271+
ARG_UNUSED(user_data);
272+
271273
/* Check if escape criteria is met. */
272274
if (tail == CHAR_1 && data[0] == CHAR_2) {
273275
escape = true;

subsys/net/lib/shell/ping.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static int parse_arg(size_t *i, size_t argc, char *argv[])
231231
static void ping_cleanup(struct ping_context *ctx)
232232
{
233233
(void)net_icmp_cleanup_ctx(&ctx->icmp);
234-
shell_set_bypass(ctx->sh, NULL);
234+
shell_set_bypass(ctx->sh, NULL, NULL);
235235
}
236236

237237
static void ping_done(struct ping_context *ctx)
@@ -286,9 +286,10 @@ static void ping_work(struct k_work *work)
286286

287287
#define ASCII_CTRL_C 0x03
288288

289-
static void ping_bypass(const struct shell *sh, uint8_t *data, size_t len)
289+
static void ping_bypass(const struct shell *sh, uint8_t *data, size_t len, void *user_data)
290290
{
291291
ARG_UNUSED(sh);
292+
ARG_UNUSED(user_data);
292293

293294
for (size_t i = 0; i < len; i++) {
294295
if (data[i] == ASCII_CTRL_C) {
@@ -481,7 +482,7 @@ static int cmd_net_ping(const struct shell *sh, size_t argc, char *argv[])
481482

482483
PR("PING %s\n", host);
483484

484-
shell_set_bypass(sh, ping_bypass);
485+
shell_set_bypass(sh, ping_bypass, NULL);
485486
k_work_reschedule(&ping_ctx.work, K_NO_WAIT);
486487

487488
return 0;

subsys/net/lib/tls_credentials/tls_credentials_shell.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,15 @@ static int tls_cred_cmd_add(const struct shell *sh, size_t argc, char *argv[])
517517

518518
#define ASCII_CTRL_C 0x03
519519

520-
static void tls_cred_cmd_load_bypass(const struct shell *sh, uint8_t *data, size_t len)
520+
static void tls_cred_cmd_load_bypass(const struct shell *sh, uint8_t *data, size_t len,
521+
void *user_data)
521522
{
522523
bool terminate = false;
523524
int res;
524525
size_t write_len = len;
525526

527+
ARG_UNUSED(user_data);
528+
526529
for (size_t i = 0; i < len; i++) {
527530
if (data[i] == ASCII_CTRL_C) {
528531
write_len = i;
@@ -533,7 +536,7 @@ static void tls_cred_cmd_load_bypass(const struct shell *sh, uint8_t *data, size
533536

534537
res = cred_buf_write(data, write_len);
535538
if (res == -ENOMEM) {
536-
shell_set_bypass(sh, NULL);
539+
shell_set_bypass(sh, NULL, NULL);
537540
shell_fprintf(sh, SHELL_ERROR, "Not enough room in credential buffer for "
538541
"provided data. Increase "
539542
"CONFIG_TLS_CREDENTIALS_SHELL_CRED_BUF_SIZE.\n");
@@ -542,7 +545,7 @@ static void tls_cred_cmd_load_bypass(const struct shell *sh, uint8_t *data, size
542545
}
543546

544547
if (terminate) {
545-
shell_set_bypass(sh, NULL);
548+
shell_set_bypass(sh, NULL, NULL);
546549
shell_fprintf(sh, SHELL_NORMAL, "Stored %d bytes.\n", cred_written);
547550
}
548551
}
@@ -561,7 +564,7 @@ static int tls_cred_cmd_buf_load(const struct shell *sh, size_t argc, char *argv
561564
shell_clear_cred_buf(sh);
562565

563566
shell_fprintf(sh, SHELL_NORMAL, "Input credential, finish with CTRL+C.\n");
564-
shell_set_bypass(sh, tls_cred_cmd_load_bypass);
567+
shell_set_bypass(sh, tls_cred_cmd_load_bypass, NULL);
565568
return 0;
566569
}
567570

subsys/shell/modules/devmem_service.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,19 @@ static int set_bypass(const struct shell *sh, shell_bypass_cb_t bypass)
175175
in_use = true;
176176
}
177177

178-
shell_set_bypass(sh, bypass);
178+
shell_set_bypass(sh, bypass, NULL);
179179

180180
return 0;
181181
}
182182

183-
static void bypass_cb(const struct shell *sh, uint8_t *recv, size_t len)
183+
static void bypass_cb(const struct shell *sh, uint8_t *recv, size_t len, void *user_data)
184184
{
185185
bool escape = false;
186186
static uint8_t tail;
187187
uint8_t byte;
188188

189+
ARG_UNUSED(user_data);
190+
189191
for (size_t i = 0; i < len; i++) {
190192
if (tail == CHAR_CAN && recv[i] == CHAR_DC1) {
191193
escape = true;

subsys/shell/shell.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ static void state_collect(const struct shell *sh)
987987

988988
while (true) {
989989
shell_bypass_cb_t bypass = sh->ctx->bypass;
990+
void *bypass_user_data = sh->ctx->bypass_user_data;
990991

991992
if (bypass) {
992993
#if defined(CONFIG_SHELL_BACKEND_RTT) && defined(CONFIG_SEGGER_RTT_BUFFER_SIZE_DOWN)
@@ -1006,7 +1007,7 @@ static void state_collect(const struct shell *sh)
10061007
* to the bypass function.
10071008
*/
10081009
z_shell_unlock(sh);
1009-
bypass(sh, buf, count);
1010+
bypass(sh, buf, count, bypass_user_data);
10101011
/* After returning, we're back in the shell context — re-acquire
10111012
* the shell mutex on the shell thread.
10121013
*/
@@ -1816,11 +1817,12 @@ int shell_mode_delete_set(const struct shell *sh, bool val)
18161817
return (int)z_flag_mode_delete_set(sh, val);
18171818
}
18181819

1819-
void shell_set_bypass(const struct shell *sh, shell_bypass_cb_t bypass)
1820+
void shell_set_bypass(const struct shell *sh, shell_bypass_cb_t bypass, void *user_data)
18201821
{
18211822
__ASSERT_NO_MSG(sh);
18221823

18231824
sh->ctx->bypass = bypass;
1825+
sh->ctx->bypass_user_data = user_data;
18241826

18251827
if (bypass == NULL) {
18261828
cmd_buffer_clear(sh);

0 commit comments

Comments
 (0)