# Adding "Reboot" command

### Create New Application

**Create New Application**

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FcxeWco13yZsAp5Wvxa0l%2Fimage.png?alt=media&#x26;token=a074f32b-1b29-4853-97f4-e403c2e295e0" alt="" width="271"><figcaption></figcaption></figure>

#### Select Board

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FZLE7boEjZWem58GdEsVX%2Fimage.png?alt=media&#x26;token=c3f745cb-e245-45e7-9497-993c1461c53a" alt="" width="563"><figcaption></figcaption></figure>

**Select Project**

### Coding Explained

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

<figure><img src="https://github.com/Advance-Innovation-Centre-AIC/BDH_Shell_over_Serial_Terminal/assets/88732241/84ed1c1c-967f-4812-8b64-393b272e4c5d" alt=""><figcaption></figcaption></figure>

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:

```c
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.

```c
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:

```c
{ "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`:

```c
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.

```c
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[]`

```c
{ "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.

<br>
