blob: 5b4b296cb167fee1f26dccdc133e4520a853919e [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* 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
7 * 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
9 * (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
14 * GNU Affero General Public License for more details.
15 *
16 * 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/>.
18 *
19 */
20
21#include <stdlib.h>
22#include <string.h>
23
Jonathan Santos5a45b152011-08-17 15:33:57 -040024#include <osmocom/core/talloc.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040025
26#include <openbsc/vty.h>
27#include <openbsc/gsm_data.h>
28#include <openbsc/debug.h>
29#include <openbsc/gsm_subscriber.h>
30#include <openbsc/bsc_nat.h>
31
32#include <osmocom/vty/telnet_interface.h>
33#include <osmocom/vty/command.h>
34#include <osmocom/vty/buffer.h>
35#include <osmocom/vty/vty.h>
36
37
38enum node_type bsc_vty_go_parent(struct vty *vty)
39{
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;
69 case OML_NODE:
70 case OM2K_NODE:
71 vty->node = ENABLE_NODE;
72 talloc_free(vty->index);
73 vty->index = NULL;
74 break;
75 case NAT_NODE:
76 vty->node = CONFIG_NODE;
77 vty->index = NULL;
78 break;
79 case NAT_BSC_NODE:
80 vty->node = NAT_NODE;
81 {
82 struct bsc_config *bsc_config = vty->index;
83 vty->index = bsc_config->nat;
84 }
85 break;
Jonathan Santos5a45b152011-08-17 15:33:57 -040086 case PGROUP_NODE:
87 vty->node = NAT_NODE;
88 break;
Jonathan Santos03fd8d02011-05-25 13:54:02 -040089 case MSC_NODE:
Jonathan Santos5a45b152011-08-17 15:33:57 -040090 vty->node = CONFIG_NODE;
Jonathan Santos03fd8d02011-05-25 13:54:02 -040091 break;
92 case TRUNK_NODE:
93 vty->node = MGCP_NODE;
94 break;
95 default:
96 vty->node = CONFIG_NODE;
97 }
98
99 return vty->node;
100}
101
102/* Down vty node level. */
103gDEFUN(ournode_exit,
104 ournode_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
105{
106 switch (vty->node) {
107 case GSMNET_NODE:
108 vty->node = CONFIG_NODE;
109 vty->index = NULL;
110 break;
111 case BTS_NODE:
112 vty->node = GSMNET_NODE;
113 {
114 /* set vty->index correctly ! */
115 struct gsm_bts *bts = vty->index;
116 vty->index = bts->network;
117 vty->index_sub = NULL;
118 }
119 break;
120 case TRX_NODE:
121 vty->node = BTS_NODE;
122 {
123 /* set vty->index correctly ! */
124 struct gsm_bts_trx *trx = vty->index;
125 vty->index = trx->bts;
126 vty->index_sub = &trx->bts->description;
127 }
128 break;
129 case TS_NODE:
130 vty->node = TRX_NODE;
131 {
132 /* set vty->index correctly ! */
133 struct gsm_bts_trx_ts *ts = vty->index;
134 vty->index = ts->trx;
135 vty->index_sub = &ts->trx->description;
136 }
137 break;
138 case NAT_BSC_NODE:
139 vty->node = NAT_NODE;
140 {
141 struct bsc_config *bsc_config = vty->index;
142 vty->index = bsc_config->nat;
143 }
144 break;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400145 case PGROUP_NODE:
146 vty->node = NAT_NODE;
147 break;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400148 case MGCP_NODE:
149 case GBPROXY_NODE:
150 case SGSN_NODE:
151 case NS_NODE:
152 case BSSGP_NODE:
153 case NAT_NODE:
154 vty->node = CONFIG_NODE;
155 vty->index = NULL;
156 break;
157 case OML_NODE:
158 case OM2K_NODE:
159 vty->node = ENABLE_NODE;
160 talloc_free(vty->index);
161 vty->index = NULL;
162 break;
163 case MSC_NODE:
Jonathan Santos5a45b152011-08-17 15:33:57 -0400164 vty->node = CONFIG_NODE;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400165 break;
166 case TRUNK_NODE:
167 vty->node = MGCP_NODE;
168 vty->index = NULL;
169 break;
170 default:
171 break;
172 }
173 return CMD_SUCCESS;
174}
175
176/* End of configuration. */
177gDEFUN(ournode_end,
178 ournode_end_cmd, "end", "End current mode and change to enable mode.")
179{
180 switch (vty->node) {
181 case VIEW_NODE:
182 case ENABLE_NODE:
183 /* Nothing to do. */
184 break;
185 case CONFIG_NODE:
186 case GSMNET_NODE:
187 case BTS_NODE:
188 case TRX_NODE:
189 case TS_NODE:
190 case MGCP_NODE:
191 case TRUNK_NODE:
192 case GBPROXY_NODE:
193 case SGSN_NODE:
194 case NS_NODE:
195 case VTY_NODE:
196 case NAT_NODE:
197 case NAT_BSC_NODE:
Jonathan Santos5a45b152011-08-17 15:33:57 -0400198 case PGROUP_NODE:
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400199 case MSC_NODE:
200 vty_config_unlock(vty);
201 vty->node = ENABLE_NODE;
202 vty->index = NULL;
203 vty->index_sub = NULL;
204 break;
205 default:
206 break;
207 }
208 return CMD_SUCCESS;
209}
210
211int bsc_vty_is_config_node(struct vty *vty, int node)
212{
213 switch (node) {
214 /* add items that are not config */
215 case OML_NODE:
216 case OM2K_NODE:
217 case SUBSCR_NODE:
218 case CONFIG_NODE:
219 return 0;
220
221 default:
222 return 1;
223 }
224}
225
226/* a talloc string replace routine */
227void bsc_replace_string(void *ctx, char **dst, const char *newstr)
228{
229 if (*dst)
230 talloc_free(*dst);
231 *dst = talloc_strdup(ctx, newstr);
232}