Adding shell command

How do I add multiple shell commands.
I understand I have to have a rtems_shell_cmd_t for each command. What I can’t figure out is how to have multiple:

#define CONFIGURE_SHELL_USER_COMMANDS &Shell_USERCMD_Command

The CONFIGURE_SHELL_USER_COMMANDS takes a list of commands separated with comas. So you can just use

#define CONFIGURE_SHELL_USER_COMMANDS \
  &first_command, \
  &second_command, \
  &third_command
1 Like

Hey Dave,
You can put a comma-separated list in the #define. The CONFIGURE_SHELL_USER_COMMANDS gets pasted into the end of an argument list, so you can add as many as you’d like.

Thank you both, didn’t realize I needed commas!

Just another note: Depending on the application, it can also be usefull to dynamically register shell commands. You can use something like follows for that:

rtems_shell_cmd_t my_cmd = {
	.name = "my-cmd",
	.usage = "some useage",
	.topic = "my-group",
	.command = my_cmd_fun,
};

/* ... */

rv = rtems_shell_add_cmd_struct(&cmd);
assert(rv != NULL); /* Use better error handling! */

Can be sometimes usefull if you want to write something a bit more modular without a lot of #ifdef.

Hi, I can’t statically add the commands I need, I’m doing everything as written in the manual, but it looks like static addition requires something else. (RTEMS 7 & stm32f4 BSP)

static int shell_test_command(int argc, char **argv)
{
    printf("GO!\n");
    return 0;
}


rtems_shell_cmd_t custom_test_command = {
    .name = "test",
    .usage = "test - Test command",
    .topic = "misc",
    .command = shell_test_command,
    .alias = false,
    .next = NULL
};


#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_RTEMS_INIT_TASKS_ENABLE
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
#define CONFIGURE_POSIX_INIT
#define CONFIGURE_SHELL_USER_COMMANDS &custom_test_command,
#define CONFIGURE_SHELL_COMMANDS_INIT
#undef CONFIGURE_SHELL_COMMANDS_ALL
#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
#define CONFIGURE_MAXIMUM_TASKS                 5
#define CONFIGURE_MAXIMUM_POSIX_THREADS         5
#define CONFIGURE_MAXIMUM_POSIX_KEYS            16
#define CONFIGURE_MAXIMUM_DRIVERS               10
#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS      10
#define CONFIGURE_HEAP_SIZE                     (64 * 1024)
#define CONFIGURE_INIT

#include <rtems/confdefs.h>
#include <rtems/shellconfig.h>

Shell output:

RTEMS Shell on /dev/console. Use 'help' to list commands.
SHLL [/] # test
test: command not found
SHLL [/] #

Nevertheless, I manage to register commands via: rtems_shell_add_cmd().
After examining the code, I see that the changes are made through the configuration macro in shellconfig.h file. Is this supposed to require a BSP rebuild? Or is it possible to statically register user commands only at the BSP build stage?

I add to Init():
rtems_shell_add_cmd_struct(&custom_test_command);

Shell output:

RTEMS Shell on /dev/console. Use 'help' to list commands.
SHLL [/] # test
GO!

I figured it out, the problem was in the connection order of the header files. I will attach a workable code, perhaps it also does not correspond to the RTEMS philosophy, please correct me.

#include <rtems.h>
#include <rtems/shell.h>
#include <stdio.h>


static int shell_test_command(int argc, char **argv)
{
    printf("GO!\n");
    return 0;
}


rtems_shell_cmd_t custom_test_command = {
    .name = "test",
    .usage = "test - Test command",
    .topic = "misc",
    .command = shell_test_command,
    .alias = false,
    .next = NULL
};


#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_RTEMS_INIT_TASKS_ENABLE
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_POSIX_INIT

#define CONFIGURE_SHELL_USER_COMMANDS &custom_test_command
#define CONFIGURE_SHELL_COMMANDS_INIT
#undef  CONFIGURE_SHELL_COMMANDS_ALL
#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK

#define CONFIGURE_MAXIMUM_TASKS                 5
#define CONFIGURE_MAXIMUM_POSIX_THREADS         5
#define CONFIGURE_MAXIMUM_POSIX_KEYS            16

#define CONFIGURE_MAXIMUM_DRIVERS               10
#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS      10
#define CONFIGURE_HEAP_SIZE                     (64 * 1024)

#define CONFIGURE_INIT

#include <rtems/confdefs.h>
#include <rtems/shellconfig.h>

 /**
 * @brief RTEMS init entry point
 *
 * @param ignored
 * @return rtems_task
 */
rtems_task Init(rtems_task_argument ignored)
{
    rtems_status_code sc;
   
    /** Start shell task */
    sc = rtems_shell_init(
        "SHLL",
        4096,
        100,
        "/dev/console",
        false,
        true,
        NULL
    );

    rtems_task_exit();
}