osmo-v5/src/v5x_vty.c

189 lines
4.7 KiB
C

#include <osmocom/vty/command.h>
#include "v5x_internal.h"
#include "v5x_vty.h"
#include "../config.h"
extern struct v5x_instance *v5i;
enum v5_vty_node {
INTERFACE_NODE = _LAST_OSMOVTY_NODE + 1,
};
static struct cmd_node interface_node = {
INTERFACE_NODE,
"%s(config-if)# ",
1,
};
static int v5le_vty_is_config_node(struct vty *vty, int node)
{
switch (node) {
case CONFIG_NODE:
return 0;
default:
return 1;
}
}
static int v5le_vty_go_parent(struct vty *vty)
{
switch (vty->node) {
default:
if (v5le_vty_is_config_node(vty, vty->node))
vty->node = CONFIG_NODE;
else
vty->node = ENABLE_NODE;
vty->index = NULL;
}
return vty->node;
}
struct vty_app_info vty_info = {
.name = "OsmoV5LE",
.version = PACKAGE_VERSION,
.go_parent_cb = v5le_vty_go_parent,
.is_config_node = v5le_vty_is_config_node,
};
DEFUN(cfg_interface, cfg_interface_cmd,
"interface", "Configure the V5 interface")
{
struct v5x_interface *v5if = (struct v5x_interface *)v5i->interfaces.next;
vty->node = INTERFACE_NODE;
vty->index = v5if;
return CMD_SUCCESS;
}
DEFUN(cfg_interface_id, cfg_interface_id_cmd,
"id <0-16777215>",
"Set interface ID\n" "Interface ID")
{
struct v5x_interface *v5if = vty->index;
v5if->id = atoi(argv[0]);
return CMD_SUCCESS;
}
DEFUN(cfg_interface_variant, cfg_interface_variant_cmd,
"variant <0-127>",
"Set interface provisioning variant\n" "Variant value")
{
struct v5x_interface *v5if = vty->index;
v5if->variant = atoi(argv[0]);
return CMD_SUCCESS;
}
DEFUN(cfg_port_pstn, cfg_port_pstn_cmd,
"port pstn <0-32767> <1-31>",
"Create V5 user port\n" "PSTN user port\n" "L3 address\n" "Time slot")
{
struct v5x_interface *v5if = vty->index;
struct v5x_user_port *v5up;
v5up = v5x_user_port_find(v5if, atoi(argv[0]), false);
if (v5up) {
vty_out(vty, "%%Given PSTN user port already exists, remove first.%s", VTY_NEWLINE);
return CMD_WARNING;
}
v5up = v5x_user_port_create(v5if, atoi(argv[0]), V5X_USER_TYPE_PSTN, atoi(argv[1]), 0);
if (!v5up) {
vty_out(vty, "%%Failed to create PSTN user port.%s", VTY_NEWLINE);
return CMD_WARNING;
}
return CMD_SUCCESS;
}
DEFUN(cfg_no_port_pstn, cfg_no_port_pstn_cmd,
"no port pstn <0-32767>",
NO_STR "Delete V5 user port\n" "PSTN user port\n" "L3 address\n")
{
struct v5x_interface *v5if = vty->index;
struct v5x_user_port *v5up;
v5up = v5x_user_port_find(v5if, atoi(argv[0]), false);
if (!v5up) {
vty_out(vty, "%%Given PSTN user port does not exist.%s", VTY_NEWLINE);
return CMD_WARNING;
}
v5x_user_port_destroy(v5up);
return CMD_SUCCESS;
}
DEFUN(cfg_port_isdn, cfg_port_isdn_cmd,
"port isdn <0-8175> <1-31> <1-31>",
"Create V5 user port\n" "ISDN user port\n" "L3 address\n" "Time slot 1\n" "Time slot 2\n")
{
struct v5x_interface *v5if = vty->index;
struct v5x_user_port *v5up;
v5up = v5x_user_port_find(v5if, atoi(argv[0]), true);
if (v5up) {
vty_out(vty, "%%Given ISDN user port already exists, remove first.%s", VTY_NEWLINE);
return CMD_WARNING;
}
v5up = v5x_user_port_create(v5if, atoi(argv[0]), V5X_USER_TYPE_ISDN, atoi(argv[1]), atoi(argv[2]));
if (!v5up) {
vty_out(vty, "%%Failed to create ISDN user port.%s", VTY_NEWLINE);
return CMD_WARNING;
}
return CMD_SUCCESS;
}
DEFUN(cfg_no_port_isdn, cfg_no_port_isdn_cmd,
"no port isdn <0-8175>",
NO_STR "Delete V5 user port\n" "ISDN user port\n" "L3 address\n")
{
struct v5x_interface *v5if = vty->index;
struct v5x_user_port *v5up;
v5up = v5x_user_port_find(v5if, atoi(argv[0]), true);
if (!v5up) {
vty_out(vty, "%%Given ISDN user port does not exist.%s", VTY_NEWLINE);
return CMD_WARNING;
}
v5x_user_port_destroy(v5up);
return CMD_SUCCESS;
}
static int config_write_interface(struct vty *vty)
{
struct v5x_interface *v5if = (struct v5x_interface *)v5i->interfaces.next;
struct v5x_user_port *v5up;
vty_out(vty, "!%s", VTY_NEWLINE);
vty_out(vty, "interface%s", VTY_NEWLINE);
vty_out(vty, " id %d%s", v5if->id, VTY_NEWLINE);
vty_out(vty, " variant %d%s", v5if->variant, VTY_NEWLINE);
llist_for_each_entry(v5up, &v5if->user_ports, list) {
switch (v5up->type) {
case V5X_USER_TYPE_PSTN:
vty_out(vty, " port pstn %d %d%s", v5up->nr, v5up->ts_nr[0], VTY_NEWLINE);
break;
case V5X_USER_TYPE_ISDN:
vty_out(vty, " port isdn %d %d %d%s", v5up->nr, v5up->ts_nr[0], v5up->ts_nr[1], VTY_NEWLINE);
break;
}
}
return CMD_SUCCESS;
}
int v5x_vty_init(void)
{
install_element(CONFIG_NODE, &cfg_interface_cmd);
install_node(&interface_node, config_write_interface);
install_element(INTERFACE_NODE, &cfg_interface_id_cmd);
install_element(INTERFACE_NODE, &cfg_interface_variant_cmd);
install_element(INTERFACE_NODE, &cfg_port_pstn_cmd);
install_element(INTERFACE_NODE, &cfg_no_port_pstn_cmd);
install_element(INTERFACE_NODE, &cfg_port_isdn_cmd);
install_element(INTERFACE_NODE, &cfg_no_port_isdn_cmd);
return 0;
}