blob: 5959d278da7dc2ddc28b3f55061a5161b72a181d [file] [log] [blame]
Holger Hans Peter Freytherf8c42192013-01-09 17:03:27 +01001/*
2 * (C) 2013 by Holger Hans Peter Freyther
3 * (C) 2013 by sysmocom s.f.m.c. GmbH
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <openbsc/control_cmd.h>
Holger Hans Peter Freyther6ca9f4f2013-01-09 17:30:11 +010023#include <openbsc/ipaccess.h>
Holger Hans Peter Freytherf8c42192013-01-09 17:03:27 +010024#include <openbsc/gsm_data.h>
25
26#define CTRL_CMD_VTY_STRING(cmdname, cmdstr, dtype, element) \
27 CTRL_HELPER_GET_STRING(cmdname, dtype, element) \
28 CTRL_HELPER_SET_STRING(cmdname, dtype, element) \
29static struct ctrl_cmd_element cmd_##cmdname = { \
30 .name = cmdstr, \
31 .param = NULL, \
32 .get = get_##cmdname, \
33 .set = set_##cmdname, \
34 .verify = verify_vty_description_string, \
35}
36
37/**
38 * Check that there are no newlines or comments or other things
39 * that could make the VTY configuration unparsable.
40 */
41static int verify_vty_description_string(struct ctrl_cmd *cmd,
42 const char *value, void *data)
43{
44 int i;
45 const size_t len = strlen(value);
46
47 for (i = 0; i < len; ++i) {
48 switch(value[i]) {
49 case '#':
50 case '\n':
51 case '\r':
52 cmd->reply = "String includes illegal character";
53 return -1;
54 default:
55 break;
56 }
57 }
58
59 return 0;
60}
61
62CTRL_CMD_DEFINE_RANGE(net_mnc, "mnc", struct gsm_network, network_code, 0, 999);
63CTRL_CMD_DEFINE_RANGE(net_mcc, "mcc", struct gsm_network, country_code, 1, 999);
64CTRL_CMD_VTY_STRING(net_short_name, "short-name", struct gsm_network, name_short);
65CTRL_CMD_VTY_STRING(net_long_name, "long-name", struct gsm_network, name_long);
66
Holger Hans Peter Freyther6ca9f4f2013-01-09 17:30:11 +010067static int verify_net_apply_config(struct ctrl_cmd *cmd, const char *v, void *d)
68{
69 return 0;
70}
71
72static int get_net_apply_config(struct ctrl_cmd *cmd, void *data)
73{
74 cmd->reply = "Write only attribute";
75 return CTRL_CMD_ERROR;
76}
77
78static int set_net_apply_config(struct ctrl_cmd *cmd, void *data)
79{
80 struct gsm_network *net = cmd->node;
81 struct gsm_bts *bts;
82
83 llist_for_each_entry(bts, &net->bts_list, list) {
84 if (!is_ipaccess_bts(bts))
85 continue;
86
87 ipaccess_drop_oml(bts);
88 }
89
90 cmd->reply = "Tried to drop the BTS";
91 return CTRL_CMD_REPLY;
92}
93
94CTRL_CMD_DEFINE(net_apply_config, "apply-configuration");
95
Holger Hans Peter Freyther3adb7722014-03-04 17:16:58 +010096static int verify_net_mcc_mnc_apply(struct ctrl_cmd *cmd, const char *value, void *d)
97{
98 char *tmp, *saveptr, *mcc, *mnc;
99
100 tmp = talloc_strdup(cmd, value);
101 if (!tmp)
102 return 1;
103
104 mcc = strtok_r(tmp, ",", &saveptr);
105 mnc = strtok_r(NULL, ",", &saveptr);
106 talloc_free(tmp);
107
108 if (!mcc || !mnc)
109 return 1;
110 return 0;
111}
112
113static int get_net_mcc_mnc_apply(struct ctrl_cmd *cmd, void *data)
114{
115 cmd->reply = "Write only attribute";
116 return CTRL_CMD_ERROR;
117}
118
119static int set_net_mcc_mnc_apply(struct ctrl_cmd *cmd, void *data)
120{
121 struct gsm_network *net = cmd->node;
122 char *tmp, *saveptr, *mcc_str, *mnc_str;
123 int mcc, mnc;
124
125 tmp = talloc_strdup(cmd, cmd->value);
126 if (!tmp)
127 goto oom;
128
129
130 mcc_str = strtok_r(tmp, ",", &saveptr);
131 mnc_str = strtok_r(NULL, ",", &saveptr);
132
133 mcc = atoi(mcc_str);
134 mnc = atoi(mnc_str);
135
136 talloc_free(tmp);
137
138 if (net->network_code == mnc && net->country_code == mcc) {
139 cmd->reply = "Nothing changed";
140 return CTRL_CMD_REPLY;
141 }
142
143 net->network_code = mnc;
144 net->country_code = mcc;
145
146 return set_net_apply_config(cmd, data);
147
148oom:
149 cmd->reply = "OOM";
150 return CTRL_CMD_ERROR;
151}
152CTRL_CMD_DEFINE(net_mcc_mnc_apply, "mcc-mnc-apply");
153
Holger Hans Peter Freytherd092f482014-03-23 11:17:27 +0100154/* TRX related commands below here */
155CTRL_HELPER_GET_INT(trx_max_power, struct gsm_bts_trx, max_power_red);
156CTRL_HELPER_SET_INT(trx_max_power, struct gsm_bts_trx, max_power_red);
157static int verify_trx_max_power(struct ctrl_cmd *cmd, const char *value, void *_data)
158{
159 int tmp = atoi(value);
160
161 if (tmp < 0 || tmp > 22) {
162 cmd->reply = "Value must be between 0 and 22";
163 return -1;
164 }
165
166 if (tmp & 1) {
167 cmd->reply = "Value must be even";
168 return -1;
169 }
170
171 return 0;
172}
173CTRL_CMD_DEFINE(trx_max_power, "max-power-reduction");
174
Holger Hans Peter Freytherf8c42192013-01-09 17:03:27 +0100175int bsc_base_ctrl_cmds_install(void)
176{
177 int rc = 0;
178 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_mnc);
179 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_mcc);
180 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_short_name);
181 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_long_name);
Holger Hans Peter Freyther6ca9f4f2013-01-09 17:30:11 +0100182 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_apply_config);
Holger Hans Peter Freyther3adb7722014-03-04 17:16:58 +0100183 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_mcc_mnc_apply);
Holger Hans Peter Freytherf8c42192013-01-09 17:03:27 +0100184
Holger Hans Peter Freytherd092f482014-03-23 11:17:27 +0100185 rc |= ctrl_cmd_install(CTRL_NODE_TRX, &cmd_trx_max_power);
Holger Hans Peter Freytherf8c42192013-01-09 17:03:27 +0100186 return rc;
187}