blob: 8c52093aeff6a4bd39fe8cd9a7db1851855643d0 [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
148int main(int argc, char **argv)
149{
150 struct gsm_bts *bts;
151 struct sockaddr_in sin;
152 int rc, option_index = 0;
153
154 printf("ipaccess-config (C) 2009 by Harald Welte\n");
155 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
156
157 while (1) {
158 int c;
Harald Welte684b1a82009-07-03 11:26:45 +0200159 unsigned long ul;
160 char *slash;
Harald Welte25de9912009-04-30 15:53:07 +0000161 static struct option long_options[] = {
162 { "unit-id", 1, 0, 'u' },
163 { "oml-ip", 1, 0, 'o' },
164 { "restart", 0, 0, 'r' },
165 };
166
Harald Welte684b1a82009-07-03 11:26:45 +0200167 c = getopt_long(argc, argv, "u:o:rn:", long_options,
Harald Welte25de9912009-04-30 15:53:07 +0000168 &option_index);
169
170 if (c == -1)
171 break;
172
173 switch (c) {
174 case 'u':
175 unit_id = optarg;
176 break;
177 case 'o':
178 prim_oml_ip = optarg;
179 break;
180 case 'r':
181 restart = 1;
182 break;
Harald Welte684b1a82009-07-03 11:26:45 +0200183 case 'n':
184 slash = strchr(optarg, '/');
185 if (!slash)
186 exit(2);
187 ul = strtoul(optarg, NULL, 16);
188 nv_flags = ul & 0xffff;
189 ul = strtoul(slash+1, NULL, 16);
190 nv_mask = ul & 0xffff;
191 break;
Harald Welte25de9912009-04-30 15:53:07 +0000192 }
193 };
194
195 if (optind >= argc) {
196 fprintf(stderr, "you have to specify the IP address of the BTS\n");
197 exit(2);
198 }
199
Harald Weltee441d9c2009-06-21 16:17:15 +0200200 gsmnet = gsm_network_init(1, 1, NULL);
Harald Welte25de9912009-04-30 15:53:07 +0000201 if (!gsmnet)
202 exit(1);
203
Harald Weltee441d9c2009-06-21 16:17:15 +0200204 bts = gsm_bts_alloc(gsmnet, GSM_BTS_TYPE_NANOBTS_900, HARDCODED_TSC,
205 HARDCODED_BSIC);
Harald Welte25de9912009-04-30 15:53:07 +0000206
207 printf("Trying to connect to ip.access BTS ...\n");
208
209 memset(&sin, 0, sizeof(sin));
210 sin.sin_family = AF_INET;
211 inet_aton(argv[optind], &sin.sin_addr);
212 rc = ia_config_connect(bts, &sin);
213 if (rc < 0) {
214 perror("Error connecting to the BTS");
215 exit(1);
216 }
217
218 while (1) {
Harald Welte04d3c922009-05-23 06:07:04 +0000219 rc = bsc_select_main(0);
Harald Welte25de9912009-04-30 15:53:07 +0000220 if (rc < 0)
221 exit(3);
222 }
223
224 exit(0);
225}
226