blob: 032345929f5b8c3c8eb4acbabd741e4397b102f2 [file] [log] [blame]
Holger Hans Peter Freyther49f9e5b2014-03-23 16:25:16 +01001/* SNMP-like status interface. Look-up of BTS/TRX
2 *
3 * (C) 2010-2011 by Daniel Willmann <daniel@totalueberwachung.de>
4 * (C) 2010-2011 by On-Waves
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Harald Welteba874b82014-08-20 23:47:15 +020024#include <errno.h>
25
26#include <osmocom/vty/command.h>
27#include <osmocom/ctrl/control_if.h>
Holger Hans Peter Freyther49f9e5b2014-03-23 16:25:16 +010028#include <openbsc/debug.h>
Harald Welteba874b82014-08-20 23:47:15 +020029#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther49f9e5b2014-03-23 16:25:16 +010030
31extern vector ctrl_node_vec;
32
33static int get_num(vector vline, int i, long *num)
34{
35 char *token, *tmp;
36
37 if (i >= vector_active(vline))
38 return 0;
39 token = vector_slot(vline, i);
40
41 errno = 0;
42 if (token[0] == '\0')
43 return 0;
44
45 *num = strtol(token, &tmp, 10);
46 if (tmp[0] != '\0' || errno != 0)
47 return 0;
48
49 return 1;
50}
51
52int bsc_ctrl_cmd_handle(struct ctrl_cmd *cmd, void *data)
53{
54 char *token, *request;
55 long num;
56 int i, j, ret, node;
57
58 struct gsm_network *net = data;
59 struct gsm_bts *bts = NULL;
60 struct gsm_bts_trx *trx = NULL;
61 struct gsm_bts_trx_ts *ts = NULL;
62 vector vline, cmdvec, cmds_vec;
63
64 ret = CTRL_CMD_ERROR;
65 cmd->reply = NULL;
66 node = CTRL_NODE_ROOT;
67 cmd->node = net;
68
69 request = talloc_strdup(tall_bsc_ctx, cmd->variable);
70 if (!request)
71 goto err;
72
73 for (i=0;i<strlen(request);i++) {
74 if (request[i] == '.')
75 request[i] = ' ';
76 }
77
78 vline = cmd_make_strvec(request);
79 talloc_free(request);
80 if (!vline) {
81 cmd->reply = "cmd_make_strvec failed.";
82 goto err;
83 }
84
85 for (i=0;i<vector_active(vline);i++) {
86 token = vector_slot(vline, i);
87 /* TODO: We need to make sure that the following chars are digits
88 * and/or use strtol to check if number conversion was successful
89 * Right now something like net.bts_stats will not work */
90 if (!strcmp(token, "bts")) {
91 if (!net)
92 goto err_missing;
93 i++;
94 if (!get_num(vline, i, &num))
95 goto err_index;
96
97 bts = gsm_bts_num(net, num);
98 if (!bts)
99 goto err_missing;
100 cmd->node = bts;
101 node = CTRL_NODE_BTS;
102 } else if (!strcmp(token, "trx")) {
103 if (!bts)
104 goto err_missing;
105 i++;
106 if (!get_num(vline, i, &num))
107 goto err_index;
108
109 trx = gsm_bts_trx_num(bts, num);
110 if (!trx)
111 goto err_missing;
112 cmd->node = trx;
113 node = CTRL_NODE_TRX;
114 } else if (!strcmp(token, "ts")) {
115 if (!trx)
116 goto err_missing;
117 i++;
118 if (!get_num(vline, i, &num))
119 goto err_index;
120
121 if ((num >= 0) && (num < TRX_NR_TS))
122 ts = &trx->ts[num];
123 if (!ts)
124 goto err_missing;
125 cmd->node = ts;
126 node = CTRL_NODE_TS;
127 } else {
128 /* If we're here the rest must be the command */
129 cmdvec = vector_init(vector_active(vline)-i);
130 for (j=i; j<vector_active(vline); j++) {
131 vector_set(cmdvec, vector_slot(vline, j));
132 }
133
134 /* Get the command vector of the right node */
135 cmds_vec = vector_lookup(ctrl_node_vec, node);
136
137 if (!cmds_vec) {
138 cmd->reply = "Command not found.";
139 vector_free(cmdvec);
140 break;
141 }
142
143 ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data);
144
145 vector_free(cmdvec);
146 break;
147 }
148
149 if (i+1 == vector_active(vline))
150 cmd->reply = "Command not present.";
151 }
152
153 cmd_free_strvec(vline);
154
155err:
156 if (!cmd->reply) {
Jacob Erlbeck268b2e62014-05-15 13:04:14 +0200157 if (ret == CTRL_CMD_ERROR) {
Holger Hans Peter Freyther49f9e5b2014-03-23 16:25:16 +0100158 cmd->reply = "An error has occured.";
Jacob Erlbeck268b2e62014-05-15 13:04:14 +0200159 LOGP(DCTRL, LOGL_NOTICE,
160 "%s: cmd->reply has not been set (ERROR).\n",
161 cmd->variable);
162 } else if (ret == CTRL_CMD_REPLY) {
163 LOGP(DCTRL, LOGL_NOTICE,
164 "%s: cmd->reply has not been set (type = %d).\n",
165 cmd->variable, cmd->type);
166 cmd->reply = "";
167 } else {
Holger Hans Peter Freyther49f9e5b2014-03-23 16:25:16 +0100168 cmd->reply = "Command has been handled.";
Jacob Erlbeck268b2e62014-05-15 13:04:14 +0200169 }
Holger Hans Peter Freyther49f9e5b2014-03-23 16:25:16 +0100170 }
171
172 if (ret == CTRL_CMD_ERROR)
173 cmd->type = CTRL_TYPE_ERROR;
174 return ret;
175
176err_missing:
177 cmd_free_strvec(vline);
178 cmd->type = CTRL_TYPE_ERROR;
179 cmd->reply = "Error while resolving object";
180 return ret;
181err_index:
182 cmd_free_strvec(vline);
183 cmd->type = CTRL_TYPE_ERROR;
184 cmd->reply = "Error while parsing the index.";
185 return ret;
186}
187
188struct ctrl_handle *bsc_controlif_setup(struct gsm_network *net, uint16_t port)
189{
190 return controlif_setup(net, port, bsc_ctrl_cmd_handle);
191}