Skip to content

Commit

Permalink
feat(common): Console for runtime network interface configuration and…
Browse files Browse the repository at this point in the history
… monitoring
  • Loading branch information
espressif-abhikroy committed Jul 28, 2023
1 parent 5143f5a commit d5fec78
Show file tree
Hide file tree
Showing 9 changed files with 756 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components/console_cmd_ifconfig/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
idf_component_register(SRCS "console_connect.c" "console_ifconfig.c"
INCLUDE_DIRS "include"
REQUIRES "ethernet_init"
PRIV_REQUIRES esp_netif console nvs_flash esp_eth)
18 changes: 18 additions & 0 deletions components/console_cmd_ifconfig/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
menu "Enable console commands"

orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"

config EXAMPLE_CMD_QUIT
bool
prompt "quit"
default y
config EXAMPLE_CMD_IFCONFIG
bool
prompt "ifconfig"
default y
config EXAMPLE_CMD_PING
bool
prompt "ping"
default n

endmenu
1 change: 1 addition & 0 deletions components/console_cmd_ifconfig/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console_cmd_ifconfig/LICENSE
57 changes: 57 additions & 0 deletions components/console_cmd_ifconfig/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Console command ifconfig
The component offers a console that enables runtime network interface configuration and monitoring for any example project.

## API

### Steps to enable console in an example code:
1. Add this component to your project using the command:
```bash
idf.py add-dependency
```
2. In the main file of the example, add the following line:
```c
#include "console_connect.h"
```
3. Ensure esp-netif is initialized and default event loop is created in your app_main():
```c
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
```
4. In your app_main() function, add the following line as the last line:
```c
example_start_networking_console(NULL, NULL);
```
5. Optionally, you can add a user-defined command:
```c
example_start_networking_console("user_cmd", usr_cmd_hndl);
```
In the above line, "user_cmd" is a string representing the user-defined command name, and usr_cmd_hndl is the command callback function with the prototype.
```c
int usr_cmd_hndl(int argc, char **argv)
```


## Suported commands:

### Ifconfig:
* **ifconfig help:** Prints the help text for all ifconfig commands
* **ifconfig netif create/destroy \<ethernet handle id\>/\<iface\>:** Create or destroy a network interface with the specified ethernet handle or interface name
* **ifconfig eth show:** Display a list of available ethernet handle
* **ifconfig:** Display a list of all esp_netif interfaces along with their information.
* **ifconfig \<iface>:** Provide the details of the named interface.
* **ifconfig \<iface> default:** Set the specified interface as the default interface.
* **ifconfig \<iface> ip6:** Enable IPv6 on the specified interface.
* **ifconfig <iface> up:** Enable the specified interface.
* **ifconfig <iface> down:** Disable the specified interface.
* **ifconfig \<iface> link \<up/down>:** Enable or disable the link of the specified interface.
* **ifconfig \<iface> ip \<ipv4 address>:** Set the IPv4 address of the specified interface.
* **ifconfig \<iface> mask \<ipv4 address>:** Set the subnet mask of the specified interface.
* **ifconfig \<iface> gw \<ipv4 address>:** Set the default gateway of the specified interface.
* **ifconfig \<iface> napt \<enable/disable>:** Enable or disable Network Address and Port Translation (NAPT) on the specified interface.
* **ifconfig \<iface> dhcp server \<enable/disable>:** Enable or disable the DHCP server on the specified interface. (Note: DHCP server is not supported yet)
* **ifconfig \<iface> dhcp client \<enable/disable>:** Enable or disable the DHCP client on the specified interface.

Note: Disabling the DHCP server and client enables the use of static IP configuration.

### Quit:
**quit:** Quits the Console application.
96 changes: 96 additions & 0 deletions components/console_cmd_ifconfig/console_connect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "lwip/inet.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "esp_netif.h"
#include "esp_console.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "argtable3/argtable3.h"
#include "esp_log.h"
#include "esp_netif_net_stack.h"
#include "lwip/ip6.h"
#include "lwip/opt.h"
#if IP_NAPT
#include "lwip/lwip_napt.h"
#endif
#include "console_connect.h"
#include "console_ifconfig.h"

static const char *TAG = "console_connect";

static esp_console_repl_t *s_repl = NULL;

/* handle 'quit' command */
static int do_cmd_quit(int argc, char **argv)
{
printf("Bye Bye\n\r\n");
s_repl->del(s_repl);
return 0;
}

static esp_console_cmd_t register_quit(void)
{
esp_console_cmd_t command = {
.command = "quit",
.help = "Quit REPL environment",
.func = &do_cmd_quit
};
return command;
}


esp_err_t example_start_networking_console(char *usr_cmd, int (*usr_cmd_hndl)(int argc, char **argv))
{
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
esp_console_cmd_t command;

// Initialize TCP/IP network interface aka the esp-netif (should be called only once in application)
//ESP_ERROR_CHECK(esp_netif_init());
// Create default event loop that running in background
//ESP_ERROR_CHECK(esp_event_loop_create_default());

// install console REPL environment
#if CONFIG_ESP_CONSOLE_UART
esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &s_repl));
#endif

#if CONFIG_EXAMPLE_CMD_IFCONFIG
/* register command `ifconfig` */
command = register_ifconfig();
if (esp_console_cmd_register(&command)) {
ESP_LOGE(TAG, "Unable to register ifconfig");
}
#endif

#if CONFIG_EXAMPLE_CMD_QUIT
/* register command `quit` */
command = register_quit();
if (esp_console_cmd_register(&command)) {
ESP_LOGE(TAG, "Unable to register quit");
}
#endif

/* Register command from caller */
if ((usr_cmd_hndl != NULL) && (usr_cmd != NULL)) {
esp_console_cmd_t command = {
.command = usr_cmd,
.help = "user command",
.func = usr_cmd_hndl
};
if (esp_console_cmd_register(&command) != ESP_OK) {
ESP_LOGE(TAG, "Unable to register user command");
}
}

// start console REPL
return esp_console_start_repl(s_repl);
}
Loading

0 comments on commit d5fec78

Please sign in to comment.