blob: ec145e50d6ca84d0114e3fa5f3aee9ede4b06039 [file] [log] [blame]
Harald Welte25de9912009-04-30 15:53:07 +00001/* ip.access nanoBTS configuration tool */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
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 General Public License as published by
8 * the Free Software Foundation; either version 2 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <unistd.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <getopt.h>
27#include <sys/types.h>
28
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32
33
34#include <openbsc/select.h>
35#include <openbsc/timer.h>
36#include <openbsc/ipaccess.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/e1_input.h>
39#include <openbsc/abis_nm.h>
40
41static struct gsm_network *gsmnet;
42
43static int restart;
44static char *prim_oml_ip;
45static char *unit_id;
Harald Welte684b1a82009-07-03 11:26:45 +020046static u_int16_t nv_flags;
47static u_int16_t nv_mask;
Harald Welte25de9912009-04-30 15:53:07 +000048
49/*
50static u_int8_t prim_oml_attr[] = { 0x95, 0x00, 7, 0x88, 192, 168, 100, 11, 0x00, 0x00 };
51static u_int8_t unit_id_attr[] = { 0x91, 0x00, 9, '2', '3', '4', '2', '/' , '0', '/', '0', 0x00 };
52*/
53
54static void bootstrap_om(struct gsm_bts *bts)
55{
56 int len;
57 static u_int8_t buf[1024];
Harald Welte684b1a82009-07-03 11:26:45 +020058 u_int8_t *cur = buf;
Harald Welte25de9912009-04-30 15:53:07 +000059
60 printf("OML link established\n");
61
62 if (unit_id) {
63 len = strlen(unit_id);
64 if (len > sizeof(buf)-10)
65 return;
66 buf[0] = NM_ATT_IPACC_UNIT_ID;
67 buf[1] = (len+1) >> 8;
68 buf[2] = (len+1) & 0xff;
69 memcpy(buf+3, unit_id, len);
70 buf[3+len] = 0;
71 printf("setting Unit ID to '%s'\n", unit_id);
72 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len+1);
73 }
74 if (prim_oml_ip) {
75 struct in_addr ia;
Harald Welte25de9912009-04-30 15:53:07 +000076
77 if (!inet_aton(prim_oml_ip, &ia)) {
78 fprintf(stderr, "invalid IP address: %s\n",
79 prim_oml_ip);
80 return;
81 }
82
83 /* 0x88 + IP + port */
84 len = 1 + sizeof(ia) + 2;
85
86 *cur++ = NM_ATT_IPACC_PRIM_OML_IP;
87 *cur++ = (len) >> 8;
88 *cur++ = (len) & 0xff;
89 *cur++ = 0x88;
90 memcpy(cur, &ia, sizeof(ia));
91 cur += sizeof(ia);
92 *cur++ = 0;
93 *cur++ = 0;
Harald Welte4802b882009-04-30 16:23:45 +000094 printf("setting primary OML link IP to '%s'\n", inet_ntoa(ia));
Harald Welte25de9912009-04-30 15:53:07 +000095 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
96 }
Harald Welte684b1a82009-07-03 11:26:45 +020097 if (nv_mask) {
98 len = 4;
99
100 *cur++ = NM_ATT_IPACC_NV_FLAGS;
101 *cur++ = (len) >> 8;
102 *cur++ = (len) & 0xff;
103 *cur++ = nv_flags & 0xff;
104 *cur++ = nv_mask & 0xff;
105 *cur++ = nv_flags >> 8;
106 *cur++ = nv_mask >> 8;
107 printf("setting NV Flags/Mask to 0x%04x/0x%04x\n",
108 nv_flags, nv_mask);
109 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
110 }
Harald Welte25de9912009-04-30 15:53:07 +0000111
112 if (restart) {
113 printf("restarting BTS\n");
114 abis_nm_ipaccess_restart(bts);
115 }
116}
117
118void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
119{
120 switch (event) {
121 case EVT_E1_TEI_UP:
122 switch (type) {
123 case E1INP_SIGN_OML:
124 bootstrap_om(trx->bts);
125 break;
126 case E1INP_SIGN_RSL:
127 /* FIXME */
128 break;
129 default:
130 break;
131 }
132 break;
133 case EVT_E1_TEI_DN:
134 fprintf(stderr, "Lost some E1 TEI link\n");
135 /* FIXME: deal with TEI or L1 link loss */
136 break;
137 default:
138 break;
139 }
140}
141
142int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
143 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
144{
145 return 0;
146}
147
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200148static void print_usage(void)
149{
150 printf("Usage: ipaccess-config\n");
151}
152
153static void print_help(void)
154{
155 printf(" -u --unit-id UNIT_ID\n");
156 printf(" -o --oml-ip ip\n");
157 printf(" -r --restart\n");
158 printf(" -n flags/mask Set NVRAM attributes.\n");
159 printf(" -h --help this text\n");
160}
161
Harald Welte25de9912009-04-30 15:53:07 +0000162int main(int argc, char **argv)
163{
164 struct gsm_bts *bts;
165 struct sockaddr_in sin;
166 int rc, option_index = 0;
167
168 printf("ipaccess-config (C) 2009 by Harald Welte\n");
169 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
170
171 while (1) {
172 int c;
Harald Welte684b1a82009-07-03 11:26:45 +0200173 unsigned long ul;
174 char *slash;
Harald Welte25de9912009-04-30 15:53:07 +0000175 static struct option long_options[] = {
176 { "unit-id", 1, 0, 'u' },
177 { "oml-ip", 1, 0, 'o' },
178 { "restart", 0, 0, 'r' },
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200179 { "help", 0, 0, 'h' },
Harald Welte25de9912009-04-30 15:53:07 +0000180 };
181
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200182 c = getopt_long(argc, argv, "u:o:rn:h", long_options,
Harald Welte25de9912009-04-30 15:53:07 +0000183 &option_index);
184
185 if (c == -1)
186 break;
187
188 switch (c) {
189 case 'u':
190 unit_id = optarg;
191 break;
192 case 'o':
193 prim_oml_ip = optarg;
194 break;
195 case 'r':
196 restart = 1;
197 break;
Harald Welte684b1a82009-07-03 11:26:45 +0200198 case 'n':
199 slash = strchr(optarg, '/');
200 if (!slash)
201 exit(2);
202 ul = strtoul(optarg, NULL, 16);
203 nv_flags = ul & 0xffff;
204 ul = strtoul(slash+1, NULL, 16);
205 nv_mask = ul & 0xffff;
206 break;
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200207 case 'h':
208 print_usage();
209 print_help();
210 exit(0);
Harald Welte25de9912009-04-30 15:53:07 +0000211 }
212 };
213
214 if (optind >= argc) {
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200215 fprintf(stderr, "you have to specify the IP address of the BTS. Use --help for more information\n");
Harald Welte25de9912009-04-30 15:53:07 +0000216 exit(2);
217 }
218
Harald Weltee441d9c2009-06-21 16:17:15 +0200219 gsmnet = gsm_network_init(1, 1, NULL);
Harald Welte25de9912009-04-30 15:53:07 +0000220 if (!gsmnet)
221 exit(1);
222
Harald Weltee441d9c2009-06-21 16:17:15 +0200223 bts = gsm_bts_alloc(gsmnet, GSM_BTS_TYPE_NANOBTS_900, HARDCODED_TSC,
224 HARDCODED_BSIC);
Harald Welte25de9912009-04-30 15:53:07 +0000225
226 printf("Trying to connect to ip.access BTS ...\n");
227
228 memset(&sin, 0, sizeof(sin));
229 sin.sin_family = AF_INET;
230 inet_aton(argv[optind], &sin.sin_addr);
231 rc = ia_config_connect(bts, &sin);
232 if (rc < 0) {
233 perror("Error connecting to the BTS");
234 exit(1);
235 }
236
237 while (1) {
Harald Welte04d3c922009-05-23 06:07:04 +0000238 rc = bsc_select_main(0);
Harald Welte25de9912009-04-30 15:53:07 +0000239 if (rc < 0)
240 exit(3);
241 }
242
243 exit(0);
244}
245