Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012-2017 sysmocom - s.f.m.c. GmbH |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include <stdint.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <arpa/inet.h> |
| 25 | |
| 26 | #include <osmocom/core/talloc.h> |
| 27 | #include <osmocom/core/utils.h> |
| 28 | #include <osmocom/core/rate_ctr.h> |
| 29 | |
| 30 | #include <osmocom/vty/command.h> |
| 31 | #include <osmocom/vty/vty.h> |
| 32 | #include <osmocom/vty/misc.h> |
| 33 | |
| 34 | #include "trx_vty.h" |
| 35 | #include "../config.h" |
| 36 | |
| 37 | static struct trx_ctx* g_trx_ctx; |
| 38 | |
| 39 | struct trx_ctx *trx_from_vty(struct vty *v) |
| 40 | { |
| 41 | /* It can't hurt to force callers to continue to pass the vty instance |
| 42 | * to this function, in case we'd like to retrieve the global |
| 43 | * trx instance from the vty at some point in the future. But |
| 44 | * until then, just return the global pointer, which should have been |
| 45 | * initialized by trx_vty_init(). |
| 46 | */ |
| 47 | OSMO_ASSERT(g_trx_ctx); |
| 48 | return g_trx_ctx; |
| 49 | } |
| 50 | |
| 51 | enum trx_vty_node { |
| 52 | TRX_NODE = _LAST_OSMOVTY_NODE + 1, |
| 53 | }; |
| 54 | |
| 55 | static struct cmd_node trx_node = { |
| 56 | TRX_NODE, |
| 57 | "%s(config-trx)# ", |
| 58 | 1, |
| 59 | }; |
| 60 | |
| 61 | DEFUN(cfg_trx, cfg_trx_cmd, |
| 62 | "trx", |
| 63 | "Configure the TRX\n") |
| 64 | { |
| 65 | struct trx_ctx *trx = trx_from_vty(vty); |
| 66 | |
| 67 | if (!trx) |
| 68 | return CMD_WARNING; |
| 69 | |
| 70 | vty->node = TRX_NODE; |
| 71 | |
| 72 | return CMD_SUCCESS; |
| 73 | } |
| 74 | |
| 75 | DEFUN(cfg_bind_ip, cfg_bind_ip_cmd, |
| 76 | "bind-ip A.B.C.D", |
| 77 | "Set the IP address for the local bind\n" |
| 78 | "IPv4 Address\n") |
| 79 | { |
| 80 | struct trx_ctx *trx = trx_from_vty(vty); |
| 81 | |
| 82 | osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, argv[0]); |
| 83 | |
| 84 | return CMD_SUCCESS; |
| 85 | } |
| 86 | |
| 87 | static int config_write_trx(struct vty *vty) |
| 88 | { |
| 89 | struct trx_ctx *trx = trx_from_vty(vty); |
| 90 | |
| 91 | vty_out(vty, "trx%s", VTY_NEWLINE); |
| 92 | if (trx->cfg.bind_addr) |
| 93 | vty_out(vty, " bind-ip %s%s", trx->cfg.bind_addr, VTY_NEWLINE); |
| 94 | |
| 95 | return CMD_SUCCESS; |
| 96 | } |
| 97 | |
| 98 | DEFUN(show_trx, show_trx_cmd, |
| 99 | "show trx", |
| 100 | SHOW_STR "Display information on the TRX\n") |
| 101 | { |
| 102 | struct trx_ctx *trx = trx_from_vty(vty); |
| 103 | |
| 104 | vty_out(vty, "TRX: Bound to %s%s", trx->cfg.bind_addr, VTY_NEWLINE); |
| 105 | |
| 106 | return CMD_SUCCESS; |
| 107 | } |
| 108 | |
| 109 | static int trx_vty_is_config_node(struct vty *vty, int node) |
| 110 | { |
| 111 | switch (node) { |
| 112 | case TRX_NODE: |
| 113 | return 1; |
| 114 | default: |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static int trx_vty_go_parent(struct vty *vty) |
| 120 | { |
| 121 | switch (vty->node) { |
| 122 | case TRX_NODE: |
| 123 | vty->node = CONFIG_NODE; |
| 124 | vty->index = NULL; |
| 125 | vty->index_sub = NULL; |
| 126 | break; |
| 127 | default: |
| 128 | OSMO_ASSERT(0); |
| 129 | } |
| 130 | |
| 131 | return vty->node; |
| 132 | } |
| 133 | |
| 134 | static const char trx_copyright[] = |
| 135 | "Copyright (C) 2007-2014 Free Software Foundation, Inc.\r\n" |
| 136 | "Copyright (C) 2013 Thomas Tsou <tom@tsou.cc>\r\n" |
| 137 | "Copyright (C) 2015 Ettus Research LLC\r\n" |
| 138 | "Copyright (C) 2017-2018 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n" |
| 139 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
| 140 | "This is free software: you are free to change and redistribute it.\r\n" |
| 141 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
| 142 | |
| 143 | struct vty_app_info g_vty_info = { |
| 144 | .name = "OsmoTRX", |
| 145 | .version = PACKAGE_VERSION, |
| 146 | .copyright = trx_copyright, |
| 147 | .go_parent_cb = trx_vty_go_parent, |
| 148 | .is_config_node = trx_vty_is_config_node, |
| 149 | }; |
| 150 | |
| 151 | int trx_vty_init(struct trx_ctx* trx) |
| 152 | { |
| 153 | g_trx_ctx = trx; |
| 154 | install_element_ve(&show_trx_cmd); |
| 155 | |
| 156 | install_element(CONFIG_NODE, &cfg_trx_cmd); |
| 157 | |
| 158 | install_node(&trx_node, config_write_trx); |
| 159 | install_element(TRX_NODE, &cfg_bind_ip_cmd); |
| 160 | |
| 161 | return 0; |
| 162 | } |