# Adding "Reboot" command

### Create New Application

**Create New Application**

<figure><img src="/files/knTIwMI794cfkNXaoSxi" alt="" width="271"><figcaption></figcaption></figure>

#### Select Board

<figure><img src="/files/6J2eIPdX9WHSsPxtURyG" 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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/biil_psoc6/sensor-interfacing-and-hmi/bdh-shell/adding-reboot-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
