Adding "Reboot" command

based on BDH Shell

Create New Application

Create New Application

Select Board

Select Project

Coding Explained

You can check the cmd functions in the `usrcmd.c` file.

The CLI (Command Line Interface) provides a way for users to interact with the system through text-based commands. Here is the detail of each function:

  • cmd_table_t: This is a struct that includes a command name, a description, and a function that is executed when the command is entered. The CLI (Command Line Interface) provides a way for users to interact with the system through text-based commands. Here is the detail of each function:

  • cmd_table_t: This is a struct that includes a command name, a description, and a function that is executed when the command is entered.

  • cmdlist[]: This is a list of the custom commands available in the shell. Each command corresponds to a specific function that is executed when the command is entered.

  • usrcmd_execute(): This function executes a given command by looking it up in the cmdlist and calling its corresponding function.

  • usrcmd_ntopt_callback(): This is the callback function for the command parsing library. It is invoked when a command is entered, and it executes the appropriate function based on the entered command.

  • Command Functions: Functions like usrcmd_help(), usrcmd_info(), usrcmd_clear(), etc. are the implementations of the custom commands. They are executed when the corresponding command is entered into the shell.

  • usrcmd_task(): This function is where the command line interface starts. It initializes the shell, sets the prompt, and then starts executing commands.

The new commands that we added are usrcmd_history(), usrcmd_reboot(), and usrcmd_kill().

✅ Create the commands of usrcmd_reboot

usrcmd_reboot reboots the system by using the NVIC_SystemReset function which resets the processor and all peripheral devices to their default state.

Step-by-step guide on how to create the usrcmd_reboot

1. Function Prototype:

You have to declare the function prototype for usrcmd_reboot. The prototype of the function is as follows:

static int usrcmd_reboot(int argc, char **argv);

2. Function Definition:

Now let's define the usrcmd_reboot function. Here, it will print a message and then call NVIC_SystemReset() to perform a system reboot.

static int usrcmd_reboot(int argc, char **argv)
{
    printf("Rebooting the system...\n");
    NVIC_SystemReset(); 
    return 0;
}

3. Command Registration:

In the cmdlist[] structure, register the reboot command to map it to the usrcmd_reboot function. Add the following line in the cmdlist[] structure:

{ "reboot", " Reboot the system of board", usrcmd_reboot },

✅ Create the commands of usrcmd_kill

usrcmd_reboot reboots the system by using the NVIC_SystemReset function which resets the processor and all peripheral devices to their default state.

Step-by-step guide on how to create the usrcmd_reboot

1. Function Prototype:

Declare the function prototype for usrcmd_kill:

static int usrcmd_kill(int argc, char **argv);

2. Function Definition:

Define the usrcmd_kill function. This function will typically take the process ID as a parameter from the command line arguments and terminate that process.

static int usrcmd_kill(int argc, char **argv)
{
    if (argc != 2) {
        printf("Usage: kill <pid>\n");
        return 1;
    }
    
    int pid = atoi(argv[1]);
    
    if (kill(pid) != 0) {
        printf("Failed to kill process %d\n", pid);
        return 1;
    }
    
    printf("Process %d killed\n", pid);
    return 0;
}

3. Command Registration:

Add the kill command to the cmdlist[] structure to map it to the usrcmd_kill function. Add the following line in the cmdlist[]

{ "kill", "Kill a process with a specific PID", usrcmd_kill },

This registers the kill command such that it will call the usrcmd_kill function when invoked.

Last updated

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University