blob: 695d4c37bc0868bf14831948e658d822532c575b [file] [log] [blame]
Harald Welte5bc61dc2010-05-16 22:02:16 +02001/* OpenBSC VTY common helpers */
2/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
3 * (C) 2009-2010 by Holger Hans Peter Freyther
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Harald Welte5bc61dc2010-05-16 22:02:16 +02009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Harald Welte5bc61dc2010-05-16 22:02:16 +020015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte5bc61dc2010-05-16 22:02:16 +020018 *
19 */
20
21#include <stdlib.h>
22#include <string.h>
23
24#include <osmocore/talloc.h>
25
26#include <openbsc/vty.h>
Harald Welte5bc61dc2010-05-16 22:02:16 +020027#include <openbsc/gsm_data.h>
28#include <openbsc/debug.h>
Harald Welte4b037e42010-05-19 19:45:32 +020029#include <openbsc/gsm_subscriber.h>
Holger Hans Peter Freytherab52c842010-06-15 20:11:16 +080030#include <openbsc/bsc_nat.h>
Harald Welte5bc61dc2010-05-16 22:02:16 +020031
Harald Welte4b037e42010-05-19 19:45:32 +020032#include <osmocom/vty/telnet_interface.h>
33#include <osmocom/vty/command.h>
34#include <osmocom/vty/buffer.h>
35#include <osmocom/vty/vty.h>
Harald Welte5bc61dc2010-05-16 22:02:16 +020036
37
Holger Hans Peter Freyther57da4472010-06-08 16:11:06 +080038enum node_type bsc_vty_go_parent(struct vty *vty)
Harald Welte4b037e42010-05-19 19:45:32 +020039{
40 switch (vty->node) {
41 case GSMNET_NODE:
42 vty->node = CONFIG_NODE;
43 vty->index = NULL;
44 break;
45 case BTS_NODE:
46 vty->node = GSMNET_NODE;
47 {
48 /* set vty->index correctly ! */
49 struct gsm_bts *bts = vty->index;
50 vty->index = bts->network;
51 }
52 break;
53 case TRX_NODE:
54 vty->node = BTS_NODE;
55 {
56 /* set vty->index correctly ! */
57 struct gsm_bts_trx *trx = vty->index;
58 vty->index = trx->bts;
59 }
60 break;
61 case TS_NODE:
62 vty->node = TRX_NODE;
63 {
64 /* set vty->index correctly ! */
65 struct gsm_bts_trx_ts *ts = vty->index;
66 vty->index = ts->trx;
67 }
68 break;
Harald Welte64632322010-05-31 17:17:29 +020069 case OML_NODE:
70 vty->node = ENABLE_NODE;
71 talloc_free(vty->index);
72 vty->index = NULL;
73 break;
Holger Hans Peter Freytherab52c842010-06-15 20:11:16 +080074 case NAT_NODE:
75 vty->node = CONFIG_NODE;
76 vty->index = NULL;
77 break;
Holger Hans Peter Freyther7b4a53d2010-06-17 14:42:20 +080078 case NAT_BSC_NODE:
Holger Hans Peter Freytherab52c842010-06-15 20:11:16 +080079 vty->node = NAT_NODE;
80 {
81 struct bsc_config *bsc_config = vty->index;
82 vty->index = bsc_config->nat;
83 }
84 break;
Holger Hans Peter Freyther47b26012010-09-15 23:28:49 +080085 case MSC_NODE:
86 vty->node = GSMNET_NODE;
87 break;
Harald Welte4b037e42010-05-19 19:45:32 +020088 default:
89 vty->node = CONFIG_NODE;
90 }
91
92 return vty->node;
93}
94
Harald Welte5bc61dc2010-05-16 22:02:16 +020095/* Down vty node level. */
96gDEFUN(ournode_exit,
97 ournode_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
98{
99 switch (vty->node) {
100 case GSMNET_NODE:
101 vty->node = CONFIG_NODE;
102 vty->index = NULL;
103 break;
104 case BTS_NODE:
105 vty->node = GSMNET_NODE;
106 {
107 /* set vty->index correctly ! */
108 struct gsm_bts *bts = vty->index;
109 vty->index = bts->network;
110 vty->index_sub = NULL;
111 }
112 break;
113 case TRX_NODE:
114 vty->node = BTS_NODE;
115 {
116 /* set vty->index correctly ! */
117 struct gsm_bts_trx *trx = vty->index;
118 vty->index = trx->bts;
119 vty->index_sub = &trx->bts->description;
120 }
121 break;
122 case TS_NODE:
123 vty->node = TRX_NODE;
124 {
125 /* set vty->index correctly ! */
126 struct gsm_bts_trx_ts *ts = vty->index;
127 vty->index = ts->trx;
128 vty->index_sub = &ts->trx->description;
129 }
130 break;
Holger Hans Peter Freyther7b4a53d2010-06-17 14:42:20 +0800131 case NAT_BSC_NODE:
Holger Hans Peter Freytherab52c842010-06-15 20:11:16 +0800132 vty->node = NAT_NODE;
133 {
134 struct bsc_config *bsc_config = vty->index;
135 vty->index = bsc_config->nat;
136 }
137 break;
Harald Welte5bc61dc2010-05-16 22:02:16 +0200138 case MGCP_NODE:
139 case GBPROXY_NODE:
140 case SGSN_NODE:
141 case NS_NODE:
Harald Welte5fa2f442010-05-17 23:20:56 +0200142 case BSSGP_NODE:
Holger Hans Peter Freytherab52c842010-06-15 20:11:16 +0800143 case NAT_NODE:
Harald Welte5bc61dc2010-05-16 22:02:16 +0200144 vty->node = CONFIG_NODE;
145 vty->index = NULL;
146 break;
Harald Welte64632322010-05-31 17:17:29 +0200147 case OML_NODE:
148 vty->node = ENABLE_NODE;
149 talloc_free(vty->index);
150 vty->index = NULL;
151 break;
Holger Hans Peter Freyther47b26012010-09-15 23:28:49 +0800152 case MSC_NODE:
153 vty->node = GSMNET_NODE;
154 break;
Harald Welte5bc61dc2010-05-16 22:02:16 +0200155 default:
156 break;
157 }
158 return CMD_SUCCESS;
159}
160
161/* End of configuration. */
162gDEFUN(ournode_end,
163 ournode_end_cmd, "end", "End current mode and change to enable mode.")
164{
165 switch (vty->node) {
166 case VIEW_NODE:
167 case ENABLE_NODE:
168 /* Nothing to do. */
169 break;
170 case CONFIG_NODE:
171 case GSMNET_NODE:
172 case BTS_NODE:
173 case TRX_NODE:
174 case TS_NODE:
175 case MGCP_NODE:
176 case GBPROXY_NODE:
177 case SGSN_NODE:
178 case NS_NODE:
179 case VTY_NODE:
Holger Hans Peter Freyther5654c5b2010-07-31 04:50:21 +0800180 case NAT_NODE:
181 case NAT_BSC_NODE:
Holger Hans Peter Freyther47b26012010-09-15 23:28:49 +0800182 case MSC_NODE:
Harald Welte5bc61dc2010-05-16 22:02:16 +0200183 vty_config_unlock(vty);
184 vty->node = ENABLE_NODE;
185 vty->index = NULL;
186 vty->index_sub = NULL;
187 break;
188 default:
189 break;
190 }
191 return CMD_SUCCESS;
192}
193
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +0800194int bsc_vty_is_config_node(struct vty *vty, int node)
195{
196 switch (node) {
197 /* add items that are not config */
198 case OML_NODE:
199 case SUBSCR_NODE:
Holger Hans Peter Freyther6ffca132010-09-04 11:15:27 +0800200 case CONFIG_NODE:
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +0800201 return 0;
202
203 default:
204 return 1;
205 }
206}
Holger Hans Peter Freyther3e9a7f82010-10-12 23:21:54 +0200207
208/* a talloc string replace routine */
209void bsc_replace_string(void *ctx, char **dst, const char *newstr)
210{
211 if (*dst)
212 talloc_free(*dst);
213 *dst = talloc_strdup(ctx, newstr);
214}