Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 1 | /* OpenBSC E1 vty interface */ |
| 2 | /* (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 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 Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 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 Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <unistd.h> |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 22 | |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 23 | #include <osmocom/core/linuxlist.h> |
| 24 | #include <osmocom/core/talloc.h> |
| 25 | #include <osmocom/core/utils.h> |
| 26 | #include <osmocom/gsm/gsm_utils.h> |
| 27 | |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 28 | #include <osmocom/vty/command.h> |
| 29 | #include <osmocom/vty/buffer.h> |
| 30 | #include <osmocom/vty/vty.h> |
| 31 | #include <osmocom/vty/logging.h> |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 32 | #include <osmocom/vty/misc.h> |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 33 | #include <osmocom/vty/telnet_interface.h> |
| 34 | |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 35 | #include <openbsc/gsm_data.h> |
| 36 | #include <openbsc/e1_input.h> |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 37 | #include <openbsc/vty.h> |
| 38 | #include <openbsc/debug.h> |
| 39 | |
Harald Welte | d9dea59 | 2011-03-04 13:53:51 +0100 | [diff] [blame] | 40 | #include "../../bscconfig.h" |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 41 | |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 42 | /* CONFIG */ |
| 43 | |
Harald Welte | 890fe4a | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 44 | #define E1_DRIVER_NAMES "(misdn|dahdi)" |
| 45 | #define E1_DRIVER_HELP "mISDN supported E1 Card\n" \ |
| 46 | "DAHDI supported E1/T1/J1 Card\n" |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 47 | |
Harald Welte | 7b61265 | 2011-08-11 12:40:07 +0200 | [diff] [blame] | 48 | #define E1_LINE_HELP "Configure E1/T1/J1 Line\n" "Line Number\n" |
| 49 | |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 50 | DEFUN(cfg_e1line_driver, cfg_e1_line_driver_cmd, |
| 51 | "e1_line <0-255> driver " E1_DRIVER_NAMES, |
Harald Welte | 7b61265 | 2011-08-11 12:40:07 +0200 | [diff] [blame] | 52 | E1_LINE_HELP "Set driver for this line\n" |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 53 | E1_DRIVER_HELP) |
| 54 | { |
| 55 | struct e1inp_line *line; |
| 56 | int e1_nr = atoi(argv[0]); |
| 57 | |
| 58 | line = e1inp_line_get(e1_nr); |
| 59 | if (line) { |
| 60 | vty_out(vty, "%% Line %d already exists%s", e1_nr, VTY_NEWLINE); |
| 61 | return CMD_WARNING; |
| 62 | } |
| 63 | line = e1inp_line_create(e1_nr, argv[1]); |
| 64 | if (!line) { |
| 65 | vty_out(vty, "%% Error creating line %d%s", e1_nr, VTY_NEWLINE); |
| 66 | return CMD_WARNING; |
| 67 | } |
| 68 | |
| 69 | return CMD_SUCCESS; |
| 70 | } |
| 71 | |
Harald Welte | 7b61265 | 2011-08-11 12:40:07 +0200 | [diff] [blame] | 72 | DEFUN(cfg_e1line_name, cfg_e1_line_name_cmd, |
| 73 | "e1_line <0-255> name .LINE", |
| 74 | E1_LINE_HELP "Set name for this line\n" "Human readable name\n") |
| 75 | { |
| 76 | struct e1inp_line *line; |
| 77 | int e1_nr = atoi(argv[0]); |
| 78 | |
| 79 | line = e1inp_line_get(e1_nr); |
| 80 | if (!line) { |
| 81 | vty_out(vty, "%% Line %d doesn't exist%s", e1_nr, VTY_NEWLINE); |
| 82 | return CMD_WARNING; |
| 83 | } |
| 84 | if (line->name) { |
| 85 | talloc_free((void *)line->name); |
| 86 | line->name = NULL; |
| 87 | } |
| 88 | line->name = talloc_strdup(line, argv[1]); |
| 89 | |
| 90 | return CMD_SUCCESS; |
| 91 | } |
| 92 | |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 93 | DEFUN(cfg_e1inp, cfg_e1inp_cmd, |
| 94 | "e1_input", |
| 95 | "Configure E1/T1/J1 TDM input\n") |
| 96 | { |
| 97 | vty->node = E1INP_NODE; |
| 98 | |
| 99 | return CMD_SUCCESS; |
| 100 | } |
| 101 | |
| 102 | static int e1inp_config_write(struct vty *vty) |
| 103 | { |
| 104 | struct e1inp_line *line; |
| 105 | |
Pablo Neira Ayuso | eaca15c | 2011-05-14 11:32:45 +0200 | [diff] [blame] | 106 | if (llist_empty(&e1inp_line_list)) |
| 107 | return CMD_SUCCESS; |
| 108 | |
Harald Welte | cc5e9f8 | 2011-02-05 15:57:42 +0100 | [diff] [blame] | 109 | vty_out(vty, "e1_input%s", VTY_NEWLINE); |
| 110 | |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 111 | llist_for_each_entry(line, &e1inp_line_list, list) { |
| 112 | vty_out(vty, " e1_line %u driver %s%s", line->num, |
| 113 | line->driver->name, VTY_NEWLINE); |
Harald Welte | 7b61265 | 2011-08-11 12:40:07 +0200 | [diff] [blame] | 114 | if (line->name) |
| 115 | vty_out(vty, " e1_line %u name %s%s", line->num, |
| 116 | line->name, VTY_NEWLINE); |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 117 | } |
| 118 | return CMD_SUCCESS; |
| 119 | } |
| 120 | |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 121 | /* SHOW */ |
| 122 | |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 123 | static void e1drv_dump_vty(struct vty *vty, struct e1inp_driver *drv) |
| 124 | { |
| 125 | vty_out(vty, "E1 Input Driver %s%s", drv->name, VTY_NEWLINE); |
| 126 | } |
| 127 | |
| 128 | DEFUN(show_e1drv, |
| 129 | show_e1drv_cmd, |
| 130 | "show e1_driver", |
| 131 | SHOW_STR "Display information about available E1 drivers\n") |
| 132 | { |
| 133 | struct e1inp_driver *drv; |
| 134 | |
| 135 | llist_for_each_entry(drv, &e1inp_driver_list, list) |
| 136 | e1drv_dump_vty(vty, drv); |
| 137 | |
| 138 | return CMD_SUCCESS; |
| 139 | } |
| 140 | |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 141 | static void e1line_dump_vty(struct vty *vty, struct e1inp_line *line, |
| 142 | int stats) |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 143 | { |
| 144 | vty_out(vty, "E1 Line Number %u, Name %s, Driver %s%s", |
| 145 | line->num, line->name ? line->name : "", |
| 146 | line->driver->name, VTY_NEWLINE); |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 147 | if (stats) |
| 148 | vty_out_rate_ctr_group(vty, " ", line->rate_ctr); |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | DEFUN(show_e1line, |
| 152 | show_e1line_cmd, |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 153 | "show e1_line [line_nr] [stats]", |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 154 | SHOW_STR "Display information about a E1 line\n" |
| 155 | "E1 Line Number\n") |
| 156 | { |
| 157 | struct e1inp_line *line; |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 158 | int stats = 0; |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 159 | |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 160 | if (argc >= 1 && strcmp(argv[0], "stats")) { |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 161 | int num = atoi(argv[0]); |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 162 | if (argc >= 2) |
| 163 | stats = 1; |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 164 | llist_for_each_entry(line, &e1inp_line_list, list) { |
| 165 | if (line->num == num) { |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 166 | e1line_dump_vty(vty, line, stats); |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 167 | return CMD_SUCCESS; |
| 168 | } |
| 169 | } |
| 170 | return CMD_WARNING; |
| 171 | } |
| 172 | |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 173 | if (argc >= 1 && !strcmp(argv[0], "stats")) |
| 174 | stats = 1; |
| 175 | |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 176 | llist_for_each_entry(line, &e1inp_line_list, list) |
Harald Welte | 2f55f15 | 2011-08-11 12:50:13 +0200 | [diff] [blame^] | 177 | e1line_dump_vty(vty, line, stats); |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 178 | |
| 179 | return CMD_SUCCESS; |
| 180 | } |
| 181 | |
| 182 | static void e1ts_dump_vty(struct vty *vty, struct e1inp_ts *ts) |
| 183 | { |
| 184 | if (ts->type == E1INP_TS_TYPE_NONE) |
| 185 | return; |
| 186 | vty_out(vty, "E1 Timeslot %2u of Line %u is Type %s%s", |
| 187 | ts->num, ts->line->num, e1inp_tstype_name(ts->type), |
| 188 | VTY_NEWLINE); |
| 189 | } |
| 190 | |
| 191 | DEFUN(show_e1ts, |
| 192 | show_e1ts_cmd, |
| 193 | "show e1_timeslot [line_nr] [ts_nr]", |
| 194 | SHOW_STR "Display information about a E1 timeslot\n" |
| 195 | "E1 Line Number\n" "E1 Timeslot Number\n") |
| 196 | { |
| 197 | struct e1inp_line *line = NULL; |
| 198 | struct e1inp_ts *ts; |
| 199 | int ts_nr; |
| 200 | |
| 201 | if (argc == 0) { |
| 202 | llist_for_each_entry(line, &e1inp_line_list, list) { |
| 203 | for (ts_nr = 0; ts_nr < NUM_E1_TS; ts_nr++) { |
| 204 | ts = &line->ts[ts_nr]; |
| 205 | e1ts_dump_vty(vty, ts); |
| 206 | } |
| 207 | } |
| 208 | return CMD_SUCCESS; |
| 209 | } |
| 210 | if (argc >= 1) { |
| 211 | int num = atoi(argv[0]); |
| 212 | struct e1inp_line *l; |
| 213 | llist_for_each_entry(l, &e1inp_line_list, list) { |
| 214 | if (l->num == num) { |
| 215 | line = l; |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | if (!line) { |
| 220 | vty_out(vty, "E1 line %s is invalid%s", |
| 221 | argv[0], VTY_NEWLINE); |
| 222 | return CMD_WARNING; |
| 223 | } |
| 224 | } |
| 225 | if (argc >= 2) { |
| 226 | ts_nr = atoi(argv[1]); |
| 227 | if (ts_nr >= NUM_E1_TS) { |
| 228 | vty_out(vty, "E1 timeslot %s is invalid%s", |
| 229 | argv[1], VTY_NEWLINE); |
| 230 | return CMD_WARNING; |
| 231 | } |
| 232 | ts = &line->ts[ts_nr]; |
| 233 | e1ts_dump_vty(vty, ts); |
| 234 | return CMD_SUCCESS; |
| 235 | } else { |
| 236 | for (ts_nr = 0; ts_nr < NUM_E1_TS; ts_nr++) { |
| 237 | ts = &line->ts[ts_nr]; |
| 238 | e1ts_dump_vty(vty, ts); |
| 239 | } |
| 240 | return CMD_SUCCESS; |
| 241 | } |
| 242 | return CMD_SUCCESS; |
| 243 | } |
| 244 | |
| 245 | |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 246 | struct cmd_node e1inp_node = { |
| 247 | E1INP_NODE, |
| 248 | "%s(e1_input)#", |
| 249 | 1, |
| 250 | }; |
| 251 | |
| 252 | int e1inp_vty_init(void) |
| 253 | { |
| 254 | install_element(CONFIG_NODE, &cfg_e1inp_cmd); |
| 255 | install_node(&e1inp_node, e1inp_config_write); |
| 256 | install_element(E1INP_NODE, &cfg_e1_line_driver_cmd); |
Harald Welte | 7b61265 | 2011-08-11 12:40:07 +0200 | [diff] [blame] | 257 | install_element(E1INP_NODE, &cfg_e1_line_name_cmd); |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 258 | |
Harald Welte | 008be6a | 2011-08-11 12:43:31 +0200 | [diff] [blame] | 259 | install_element_ve(&show_e1drv_cmd); |
| 260 | install_element_ve(&show_e1line_cmd); |
| 261 | install_element_ve(&show_e1ts_cmd); |
| 262 | |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 263 | return 0; |
| 264 | } |