Skip to content

Commit

Permalink
firmware/sys/shell_extended: Adds support for net_tools commands (#354
Browse files Browse the repository at this point in the history
)

* firmware/sys/shell_extended: Add  net_tools cmds
* firmware/sys/shell_extended: Added net_tools functionalities
* added mockup to show info of interface

Co-authored-by: eduazocar <[email protected]>
  • Loading branch information
luisan00 and CW-75 authored Oct 7, 2022
1 parent fd9a88c commit b67d5da
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/shell_extended/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rsource "../../firmware/sys/Kconfig"
2 changes: 2 additions & 0 deletions examples/shell_extended/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ include ../Makefile.common
# Include the shell extended module, shell and shell commands modules
# will be implicitly included.
USEMODULE += shell_extended

# Include desired modules where shell extended is enabled.
USEMODULE += uniqueid
USEMODULE += net_tools

include $(RIOTBASE)/Makefile.include
4 changes: 4 additions & 0 deletions firmware/sys/shell_extended/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ ifneq (,$(filter storage,$(USEMODULE)))
SRC += se_storage.c
endif

ifneq (,$(filter net_tools,$(USEMODULE)))
SRC += se_net_tools.c
endif

include $(RIOTBASE)/Makefile.base
127 changes: 127 additions & 0 deletions firmware/sys/shell_extended/se_net_tools.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2022 Mesh4all <mesh4all.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @brief Extends the shell functionality for uniqueid
* @author Luis A. Ruiz <[email protected]>
* @author Eduardo Azócar <[email protected]>
*/

#include <stdio.h>
#include <string.h>
#include "net_tools.h"
#if MODULE_UNIQUEID
#include "uniqueid.h"
#endif

#define ENABLE_DEBUG 0
#include "debug.h"

void show_iface_info(kernel_pid_t iface_idx);
void check_inp_addr(char *addr, ipv6_addr_t *ip, uint8_t *pfx);

void net_tools_usage(void) {
puts("net-tools usage:");
puts("\tlist-of-commands");
puts("\t if [iface id] [add]");
puts("\t\t\t -[ipv6 unicast addr/network prefix length]");
#if MODULE_UNIQUEID
puts("\t\t\t -[ipuid] [header/prefix] [static|random]");
#endif
puts("");
}

int net_tools_cmd(int argc, char **argv) {
(void)argc;
(void)argv;

if ((strcmp(argv[1], "help") == 0)) {
net_tools_usage();
return 0;
}
if ((strcmp(argv[2], "add") == 0)) {
int if_idx = atoi(argv[1]);
char ip[40] = "";
ipv6_addr_t addr;
uint8_t prefix;
if ((netif_get_by_id(if_idx)) == NULL) {
puts("Interface doesn't exist");
return -1;
}
#if MODULE_UNIQUEID
if (strcmp(argv[3], "ipuid") == 0) {
check_inp_addr(argv[4], &addr, &prefix);
if ((strcmp(argv[5], "static") == 0)) {
set_ipv6_by_uid(if_idx, &addr, prefix, UNIQUEID_STATIC_MODE);
DEBUG("ipv6 address: %s\n", ipv6_addr_to_str(ip, &addr, sizeof(ip)));
return 0;
}
if ((strcmp(argv[5], "random") == 0)) {
set_ipv6_by_uid(if_idx, &addr, prefix, UNIQUEID_RANDOM_MODE);
DEBUG("ipv6 address: %s\n", ipv6_addr_to_str(ip, &addr, sizeof(ip)));
return 0;
} else {
puts("Ipuid (Ip by Unique ID) mode wasn't selected");
return -1;
}
}
#endif
check_inp_addr(argv[3], &addr, &prefix);
set_ipv6_global(if_idx, addr, prefix);
DEBUG("ipv6 address: %s\n", ipv6_addr_to_str(ip, &addr, sizeof(ip)));
return 0;
}
if (strcmp(argv[2], "show") == 0) {
uint8_t if_idx = atoi(argv[1]);
if ((netif_get_by_id(if_idx)) == NULL) {
puts("Interface doesn't exist");
return -1;
}
show_iface_info(if_idx);
return 0;
}

if ((argc < 5) || (argc > 5)) {
puts("net-tools command needs at least one argument");
net_tools_usage();
return 1;
}
return 0;
}

void show_iface_info(kernel_pid_t iface_idx) {
/*ToDo show of an interface its specifications -> Physical info*/
printf("ToDo: Show an interface\n");
if (iface_idx == 0) {
/*ToDo show all interfaces and their physical features.*/
}
}

void check_inp_addr(char *addr, ipv6_addr_t *ip, uint8_t *pfx) {
uint8_t strlen = 0;
char *prefix;
while (*(addr + strlen) != '/') {
strlen++;
if (*(addr + strlen) == '\0') {
printf("Wrong input address. Address must be e.g: 2001:db8::/64\n");
return;
}
}
strlen++;
prefix = addr + strlen;
*(addr + strlen - 1) = '\0';
ipv6_addr_from_str(ip, addr);
*pfx = atoi(prefix);
}
9 changes: 9 additions & 0 deletions firmware/sys/shell_extended/shell_extended.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include "shell_extended.h"
#include "kernel_defines.h"

/* important: please keep the used modules alphabetically sorted */
#if IS_USED(MODULE_NET_TOOLS)
int net_tools_cmd(int argc, char **argv);
#endif

#if IS_USED(MODULE_UNIQUEID)
int uid_cmd(int argc, char **argv);
#endif
Expand All @@ -30,6 +35,10 @@ int storage_cmd(int argc, char **argv);
#endif

const shell_command_t shell_extended_commands[] = {
/* important: please keep the used modules alphabetically sorted */
#if IS_USED(MODULE_NET_TOOLS)
{"if", "net_tools commands", net_tools_cmd},
#endif
#if IS_USED(MODULE_UNIQUEID)
{"uid", "uniqueid commands", uid_cmd},
#endif
Expand Down

0 comments on commit b67d5da

Please sign in to comment.