blob: df8e0fd3d0d1901d193556c575d5b0b0f24a3ffb [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* GSM Network Management (OML) messages on the A-bis interface
2 * 3GPP TS 12.21 version 8.0.0 Release 1999 / ETSI TS 100 623 V8.0.0 */
3
Harald Welte4724f992009-01-18 18:01:49 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte8470bf22008-12-25 23:28:35 +00005 *
Harald Welte52b1f982008-12-23 20:25:15 +00006 * 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
24
25#include <errno.h>
Harald Welte4724f992009-01-18 18:01:49 +000026#include <unistd.h>
Harald Welte52b1f982008-12-23 20:25:15 +000027#include <stdio.h>
Harald Welte4724f992009-01-18 18:01:49 +000028#include <fcntl.h>
Harald Welte5e4d1b32009-02-01 13:36:56 +000029#include <malloc.h>
30#include <libgen.h>
Harald Welte268bb402009-02-01 19:11:56 +000031#include <time.h>
Harald Welte5f6f1492009-02-02 14:50:29 +000032#include <limits.h>
Harald Welte4724f992009-01-18 18:01:49 +000033
Harald Welte52b1f982008-12-23 20:25:15 +000034#include <sys/types.h>
Harald Welte4724f992009-01-18 18:01:49 +000035#include <sys/stat.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000037
Harald Welte8470bf22008-12-25 23:28:35 +000038#include <openbsc/gsm_data.h>
39#include <openbsc/debug.h>
40#include <openbsc/msgb.h>
41#include <openbsc/tlv.h>
42#include <openbsc/abis_nm.h>
Holger Freytherca362a62009-01-04 21:05:01 +000043#include <openbsc/misdn.h>
Harald Welte52b1f982008-12-23 20:25:15 +000044
Harald Welte8470bf22008-12-25 23:28:35 +000045#define OM_ALLOC_SIZE 1024
46#define OM_HEADROOM_SIZE 128
Harald Welte52b1f982008-12-23 20:25:15 +000047
48/* unidirectional messages from BTS to BSC */
49static const enum abis_nm_msgtype reports[] = {
50 NM_MT_SW_ACTIVATED_REP,
51 NM_MT_TEST_REP,
52 NM_MT_STATECHG_EVENT_REP,
53 NM_MT_FAILURE_EVENT_REP,
54};
55
56/* messages without ACK/NACK */
57static const enum abis_nm_msgtype no_ack_nack[] = {
58 NM_MT_MEAS_RES_REQ,
59 NM_MT_STOP_MEAS,
60 NM_MT_START_MEAS,
61};
62
Harald Welte4724f992009-01-18 18:01:49 +000063/* Messages related to software load */
64static const enum abis_nm_msgtype sw_load_msgs[] = {
65 NM_MT_LOAD_INIT_ACK,
66 NM_MT_LOAD_INIT_NACK,
67 NM_MT_LOAD_SEG_ACK,
68 NM_MT_LOAD_ABORT,
69 NM_MT_LOAD_END_ACK,
70 NM_MT_LOAD_END_NACK,
71 NM_MT_SW_ACT_REQ,
72 NM_MT_ACTIVATE_SW_ACK,
73 NM_MT_ACTIVATE_SW_NACK,
74 NM_MT_SW_ACTIVATED_REP,
75};
76
Harald Welte52b1f982008-12-23 20:25:15 +000077/* Attributes that the BSC can set, not only get, according to Section 9.4 */
78static const enum abis_nm_attr nm_att_settable[] = {
79 NM_ATT_ADD_INFO,
80 NM_ATT_ADD_TEXT,
81 NM_ATT_DEST,
82 NM_ATT_EVENT_TYPE,
83 NM_ATT_FILE_DATA,
84 NM_ATT_GET_ARI,
85 NM_ATT_HW_CONF_CHG,
86 NM_ATT_LIST_REQ_ATTR,
87 NM_ATT_MDROP_LINK,
88 NM_ATT_MDROP_NEXT,
89 NM_ATT_NACK_CAUSES,
90 NM_ATT_OUTST_ALARM,
91 NM_ATT_PHYS_CONF,
92 NM_ATT_PROB_CAUSE,
93 NM_ATT_RAD_SUBC,
94 NM_ATT_SOURCE,
95 NM_ATT_SPEC_PROB,
96 NM_ATT_START_TIME,
97 NM_ATT_TEST_DUR,
98 NM_ATT_TEST_NO,
99 NM_ATT_TEST_REPORT,
100 NM_ATT_WINDOW_SIZE,
101 NM_ATT_SEVERITY,
102 NM_ATT_MEAS_RES,
103 NM_ATT_MEAS_TYPE,
104};
105
106static int is_in_arr(enum abis_nm_msgtype mt, const enum abis_nm_msgtype *arr, int size)
107{
108 int i;
109
110 for (i = 0; i < size; i++) {
111 if (arr[i] == mt)
112 return 1;
113 }
114
115 return 0;
116}
117
Holger Freytherca362a62009-01-04 21:05:01 +0000118#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000119/* is this msgtype the usual ACK/NACK type ? */
120static int is_ack_nack(enum abis_nm_msgtype mt)
121{
122 return !is_in_arr(mt, no_ack_nack, ARRAY_SIZE(no_ack_nack));
123}
Holger Freytherca362a62009-01-04 21:05:01 +0000124#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000125
126/* is this msgtype a report ? */
127static int is_report(enum abis_nm_msgtype mt)
128{
Harald Welte8470bf22008-12-25 23:28:35 +0000129 return is_in_arr(mt, reports, ARRAY_SIZE(reports));
Harald Welte52b1f982008-12-23 20:25:15 +0000130}
131
132#define MT_ACK(x) (x+1)
133#define MT_NACK(x) (x+2)
134
135static void fill_om_hdr(struct abis_om_hdr *oh, u_int8_t len)
136{
137 oh->mdisc = ABIS_OM_MDISC_FOM;
138 oh->placement = ABIS_OM_PLACEMENT_ONLY;
139 oh->sequence = 0;
140 oh->length = len;
141}
142
143static void fill_om_fom_hdr(struct abis_om_hdr *oh, u_int8_t len,
144 u_int8_t msg_type, u_int8_t obj_class,
145 u_int8_t bts_nr, u_int8_t trx_nr, u_int8_t ts_nr)
146{
147 struct abis_om_fom_hdr *foh =
148 (struct abis_om_fom_hdr *) oh->data;
149
Harald Welte702d8702008-12-26 20:25:35 +0000150 fill_om_hdr(oh, len+sizeof(*foh));
Harald Welte52b1f982008-12-23 20:25:15 +0000151 foh->msg_type = msg_type;
152 foh->obj_class = obj_class;
153 foh->obj_inst.bts_nr = bts_nr;
154 foh->obj_inst.trx_nr = trx_nr;
155 foh->obj_inst.ts_nr = ts_nr;
156}
157
Harald Welte8470bf22008-12-25 23:28:35 +0000158static struct msgb *nm_msgb_alloc(void)
159{
160 return msgb_alloc_headroom(OM_ALLOC_SIZE, OM_HEADROOM_SIZE);
161}
162
Harald Welte52b1f982008-12-23 20:25:15 +0000163/* Send a OML NM Message from BSC to BTS */
164int abis_nm_sendmsg(struct gsm_bts *bts, struct msgb *msg)
165{
Harald Weltead384642008-12-26 10:20:07 +0000166 return _abis_nm_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000167}
168
Harald Welte4724f992009-01-18 18:01:49 +0000169static int abis_nm_rcvmsg_sw(struct msgb *mb);
170
Harald Welte97ed1e72009-02-06 13:38:02 +0000171static int abis_nm_rx_statechg_rep(struct msgb *mb)
172{
173 struct abis_om_fom_hdr *foh = msgb_l3(mb);
174 u_int8_t mt = foh->msg_type;
175 u_int8_t *data = &foh->data[0];
176
177 DEBUGP(DNM, "STATE CHG: OC=%02x INST=(%02x,%02x,%02x) ",
178 foh->obj_class, foh->obj_inst.bts_nr, foh->obj_inst.trx_nr,
179 foh->obj_inst.ts_nr);
180 if (*data++ == NM_ATT_OPER_STATE)
181 DEBUGPC(DNM, "OP_STATE=%02x ", *data++);
182 if (*data++ == NM_ATT_AVAIL_STATUS) {
183 u_int8_t att_len = *data++;
184 while (att_len--)
185 DEBUGPC(DNM, "AVAIL=%02x ", *data++);
186 }
187 DEBUGPC(DNM, "\n");
188 return 0;
189}
190
191static int abis_nm_rcvmsg_report(struct msgb *mb)
192{
193 struct abis_om_fom_hdr *foh = msgb_l3(mb);
194 u_int8_t mt = foh->msg_type;
195
196 //nmh->cfg->report_cb(mb, foh);
197
198 switch (mt) {
199 case NM_MT_STATECHG_EVENT_REP:
200 return abis_nm_rx_statechg_rep(mb);
201 break;
202 };
203
204 DEBUGP(DNM, "reporting NM MT 0x%02x\n", mt);
205
206 return 0;
207}
208
Harald Welte52b1f982008-12-23 20:25:15 +0000209/* Receive a OML NM Message from BTS */
Harald Welte8470bf22008-12-25 23:28:35 +0000210static int abis_nm_rcvmsg_fom(struct msgb *mb)
Harald Welte52b1f982008-12-23 20:25:15 +0000211{
212 struct abis_om_fom_hdr *foh = msgb_l3(mb);
213 u_int8_t mt = foh->msg_type;
214
215 /* check for unsolicited message */
Harald Welte97ed1e72009-02-06 13:38:02 +0000216 if (is_report(mt))
217 return abis_nm_rcvmsg_report(mb);
Harald Welte52b1f982008-12-23 20:25:15 +0000218
Harald Welte4724f992009-01-18 18:01:49 +0000219 if (is_in_arr(mt, sw_load_msgs, ARRAY_SIZE(sw_load_msgs)))
220 return abis_nm_rcvmsg_sw(mb);
221
Harald Weltead384642008-12-26 10:20:07 +0000222#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000223 /* check if last message is to be acked */
224 if (is_ack_nack(nmh->last_msgtype)) {
225 if (mt == MT_ACK(nmh->last_msgtype)) {
226 fprintf(stderr, "received ACK (0x%x)\n",
227 foh->msg_type);
228 /* we got our ACK, continue sending the next msg */
229 } else if (mt == MT_NACK(nmh->last_msgtype)) {
230 /* we got a NACK, signal this to the caller */
231 fprintf(stderr, "received NACK (0x%x)\n",
232 foh->msg_type);
233 /* FIXME: somehow signal this to the caller */
234 } else {
235 /* really strange things happen */
236 return -EINVAL;
237 }
238 }
Harald Weltead384642008-12-26 10:20:07 +0000239#endif
240
Harald Welte97ed1e72009-02-06 13:38:02 +0000241 switch (mt) {
242 case NM_MT_BS11_LMT_SESSION:
243 DEBUGP(DNM, "LMT Event: \n");
244 break;
245 }
246
Harald Weltead384642008-12-26 10:20:07 +0000247 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000248}
249
250/* High-Level API */
251/* Entry-point where L2 OML from BTS enters the NM code */
Harald Welte8470bf22008-12-25 23:28:35 +0000252int abis_nm_rcvmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000253{
254 int rc;
255 struct abis_om_hdr *oh = msgb_l2(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000256
257 /* Various consistency checks */
258 if (oh->placement != ABIS_OM_PLACEMENT_ONLY) {
259 fprintf(stderr, "ABIS OML placement 0x%x not supported\n",
260 oh->placement);
261 return -EINVAL;
262 }
263 if (oh->sequence != 0) {
264 fprintf(stderr, "ABIS OML sequence 0x%x != 0x00\n",
265 oh->sequence);
266 return -EINVAL;
267 }
Harald Welte702d8702008-12-26 20:25:35 +0000268#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000269 unsigned int l2_len = msg->tail - (u_int8_t *)msgb_l2(msg);
270 unsigned int hlen = sizeof(*oh) + sizeof(struct abis_om_fom_hdr);
Harald Welte702d8702008-12-26 20:25:35 +0000271 if (oh->length + hlen > l2_len) {
Harald Welte52b1f982008-12-23 20:25:15 +0000272 fprintf(stderr, "ABIS OML truncated message (%u > %u)\n",
273 oh->length + sizeof(*oh), l2_len);
274 return -EINVAL;
275 }
Harald Welte702d8702008-12-26 20:25:35 +0000276 if (oh->length + hlen < l2_len)
277 fprintf(stderr, "ABIS OML message with extra trailer?!? (oh->len=%d, sizeof_oh=%d l2_len=%d\n", oh->length, sizeof(*oh), l2_len);
278#endif
Harald Weltead384642008-12-26 10:20:07 +0000279 msg->l3h = (unsigned char *)oh + sizeof(*oh);
Harald Welte52b1f982008-12-23 20:25:15 +0000280
281 switch (oh->mdisc) {
282 case ABIS_OM_MDISC_FOM:
Harald Welte8470bf22008-12-25 23:28:35 +0000283 rc = abis_nm_rcvmsg_fom(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000284 break;
285 case ABIS_OM_MDISC_MMI:
286 case ABIS_OM_MDISC_TRAU:
287 case ABIS_OM_MDISC_MANUF:
288 default:
289 fprintf(stderr, "unknown ABIS OML message discriminator 0x%x\n",
290 oh->mdisc);
291 return -EINVAL;
292 }
293
Harald Weltead384642008-12-26 10:20:07 +0000294 msgb_free(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000295 return rc;
296}
297
298#if 0
299/* initialized all resources */
300struct abis_nm_h *abis_nm_init(struct abis_nm_cfg *cfg)
301{
302 struct abis_nm_h *nmh;
303
304 nmh = malloc(sizeof(*nmh));
305 if (!nmh)
306 return NULL;
307
308 nmh->cfg = cfg;
309
310 return nmh;
311}
312
313/* free all resources */
314void abis_nm_fini(struct abis_nm_h *nmh)
315{
316 free(nmh);
317}
318#endif
319
320/* Here we are trying to define a high-level API that can be used by
321 * the actual BSC implementation. However, the architecture is currently
322 * still under design. Ideally the calls to this API would be synchronous,
323 * while the underlying stack behind the APi runs in a traditional select
324 * based state machine.
325 */
326
Harald Welte4724f992009-01-18 18:01:49 +0000327/* 6.2 Software Load: */
328enum sw_state {
329 SW_STATE_NONE,
330 SW_STATE_WAIT_INITACK,
331 SW_STATE_WAIT_SEGACK,
332 SW_STATE_WAIT_ENDACK,
333 SW_STATE_WAIT_ACTACK,
334 SW_STATE_ERROR,
335};
Harald Welte52b1f982008-12-23 20:25:15 +0000336
Harald Welte52b1f982008-12-23 20:25:15 +0000337struct abis_nm_sw {
Harald Welte4724f992009-01-18 18:01:49 +0000338 struct gsm_bts *bts;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000339 gsm_cbfn *cbfn;
340 void *cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000341 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000342
Harald Welte52b1f982008-12-23 20:25:15 +0000343 /* this will become part of the SW LOAD INITIATE */
344 u_int8_t obj_class;
345 u_int8_t obj_instance[3];
Harald Welte4724f992009-01-18 18:01:49 +0000346
347 u_int8_t file_id[255];
348 u_int8_t file_id_len;
349
350 u_int8_t file_version[255];
351 u_int8_t file_version_len;
352
353 u_int8_t window_size;
354 u_int8_t seg_in_window;
355
356 int fd;
357 FILE *stream;
358 enum sw_state state;
Harald Welte1602ade2009-01-29 21:12:39 +0000359 int last_seg;
Harald Welte52b1f982008-12-23 20:25:15 +0000360};
361
Harald Welte4724f992009-01-18 18:01:49 +0000362static struct abis_nm_sw g_sw;
363
364/* 6.2.1 / 8.3.1: Load Data Initiate */
365static int sw_load_init(struct abis_nm_sw *sw)
Harald Welte52b1f982008-12-23 20:25:15 +0000366{
Harald Welte4724f992009-01-18 18:01:49 +0000367 struct abis_om_hdr *oh;
368 struct msgb *msg = nm_msgb_alloc();
369 u_int8_t len = 3*2 + sw->file_id_len + sw->file_version_len;
370
371 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
372 fill_om_fom_hdr(oh, len, NM_MT_LOAD_INIT, sw->obj_class,
373 sw->obj_instance[0], sw->obj_instance[1],
374 sw->obj_instance[2]);
375
376 /* FIXME: this is BS11 specific format */
377 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
378 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
379 sw->file_version);
380 msgb_tv_put(msg, NM_ATT_WINDOW_SIZE, sw->window_size);
381
382 return abis_nm_sendmsg(sw->bts, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000383}
384
Harald Welte1602ade2009-01-29 21:12:39 +0000385static int is_last_line(FILE *stream)
386{
387 char next_seg_buf[256];
388 long pos;
389
390 /* check if we're sending the last line */
391 pos = ftell(stream);
392 if (!fgets(next_seg_buf, sizeof(next_seg_buf)-2, stream)) {
393 fseek(stream, pos, SEEK_SET);
394 return 1;
395 }
396
397 fseek(stream, pos, SEEK_SET);
398 return 0;
399}
400
Harald Welte4724f992009-01-18 18:01:49 +0000401/* 6.2.2 / 8.3.2 Load Data Segment */
402static int sw_load_segment(struct abis_nm_sw *sw)
403{
404 struct abis_om_hdr *oh;
405 struct msgb *msg = nm_msgb_alloc();
406 char seg_buf[256];
407 char *line_buf = seg_buf+2;
Harald Welte3b8ba212009-01-29 12:27:58 +0000408 unsigned char *tlv;
Harald Welte4724f992009-01-18 18:01:49 +0000409 u_int8_t len;
Harald Welte4724f992009-01-18 18:01:49 +0000410
411 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte3b8ba212009-01-29 12:27:58 +0000412
413 switch (sw->bts->type) {
414 case GSM_BTS_TYPE_BS11:
415 if (fgets(line_buf, sizeof(seg_buf)-2, sw->stream) == NULL) {
416 perror("fgets reading segment");
417 return -EINVAL;
418 }
419 seg_buf[0] = 0x00;
Harald Welte1602ade2009-01-29 21:12:39 +0000420
421 /* check if we're sending the last line */
422 sw->last_seg = is_last_line(sw->stream);
423 if (sw->last_seg)
424 seg_buf[1] = 0;
425 else
426 seg_buf[1] = 1 + sw->seg_in_window++;
Harald Welte3b8ba212009-01-29 12:27:58 +0000427
428 len = strlen(line_buf) + 2;
429 tlv = msgb_put(msg, TLV_GROSS_LEN(len));
430 tlv_put(tlv, NM_ATT_BS11_FILE_DATA, len, (u_int8_t *)seg_buf);
431 /* BS11 wants CR + LF in excess of the TLV length !?! */
432 tlv[1] -= 2;
433
434 /* we only now know the exact length for the OM hdr */
435 len = strlen(line_buf)+2;
436 break;
437 default:
438 /* FIXME: Other BTS types */
439 return -1;
Harald Welte4724f992009-01-18 18:01:49 +0000440 }
Harald Welte4724f992009-01-18 18:01:49 +0000441
Harald Welte4724f992009-01-18 18:01:49 +0000442 fill_om_fom_hdr(oh, len, NM_MT_LOAD_SEG, sw->obj_class,
443 sw->obj_instance[0], sw->obj_instance[1],
444 sw->obj_instance[2]);
445
446 return abis_nm_sendmsg(sw->bts, msg);
447}
448
449/* 6.2.4 / 8.3.4 Load Data End */
450static int sw_load_end(struct abis_nm_sw *sw)
451{
452 struct abis_om_hdr *oh;
453 struct msgb *msg = nm_msgb_alloc();
454 u_int8_t len = 2*2 + sw->file_id_len + sw->file_version_len;
455
456 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
457 fill_om_fom_hdr(oh, len, NM_MT_LOAD_END, sw->obj_class,
458 sw->obj_instance[0], sw->obj_instance[1],
459 sw->obj_instance[2]);
460
461 /* FIXME: this is BS11 specific format */
462 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
463 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
464 sw->file_version);
465
466 return abis_nm_sendmsg(sw->bts, msg);
467}
Harald Welte5e4d1b32009-02-01 13:36:56 +0000468
Harald Welte52b1f982008-12-23 20:25:15 +0000469/* Activate the specified software into the BTS */
Harald Welte4724f992009-01-18 18:01:49 +0000470static int sw_activate(struct abis_nm_sw *sw)
Harald Welte52b1f982008-12-23 20:25:15 +0000471{
Harald Welte4724f992009-01-18 18:01:49 +0000472 struct abis_om_hdr *oh;
473 struct msgb *msg = nm_msgb_alloc();
474 u_int8_t len = 2*2 + sw->file_id_len + sw->file_version_len;
Harald Welte52b1f982008-12-23 20:25:15 +0000475
Harald Welte4724f992009-01-18 18:01:49 +0000476 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
477 fill_om_fom_hdr(oh, len, NM_MT_ACTIVATE_SW, sw->obj_class,
478 sw->obj_instance[0], sw->obj_instance[1],
479 sw->obj_instance[2]);
480
481 /* FIXME: this is BS11 specific format */
482 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
483 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
484 sw->file_version);
485
486 return abis_nm_sendmsg(sw->bts, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000487}
Harald Welte4724f992009-01-18 18:01:49 +0000488
489static int sw_open_file(struct abis_nm_sw *sw, const char *fname)
490{
491 char file_id[12+1];
492 char file_version[80+1];
493 int rc;
494
495 sw->fd = open(fname, O_RDONLY);
496 if (sw->fd < 0)
497 return sw->fd;
498
499 switch (sw->bts->type) {
500 case GSM_BTS_TYPE_BS11:
501 sw->stream = fdopen(sw->fd, "r");
502 if (!sw->stream) {
503 perror("fdopen");
504 return -1;
505 }
506 /* read first line and parse file ID and VERSION */
Harald Welte3b8ba212009-01-29 12:27:58 +0000507 rc = fscanf(sw->stream, "@(#)%12s:%80s\r\n",
Harald Welte4724f992009-01-18 18:01:49 +0000508 file_id, file_version);
509 if (rc != 2) {
510 perror("parsing header line of software file");
511 return -1;
512 }
513 strcpy((char *)sw->file_id, file_id);
514 sw->file_id_len = strlen(file_id);
515 strcpy((char *)sw->file_version, file_version);
516 sw->file_version_len = strlen(file_version);
517 /* rewind to start of file */
Harald Welte3b8ba212009-01-29 12:27:58 +0000518 rewind(sw->stream);
Harald Welte4724f992009-01-18 18:01:49 +0000519 break;
520 default:
521 /* We don't know how to treat them yet */
522 close(sw->fd);
523 return -EINVAL;
524 }
525
526 return 0;
527}
528
529static void sw_close_file(struct abis_nm_sw *sw)
530{
531 switch (sw->bts->type) {
532 case GSM_BTS_TYPE_BS11:
533 fclose(sw->stream);
534 break;
535 default:
536 close(sw->fd);
537 break;
538 }
539}
540
541/* Fill the window */
542static int sw_fill_window(struct abis_nm_sw *sw)
543{
544 int rc;
545
546 while (sw->seg_in_window < sw->window_size) {
547 rc = sw_load_segment(sw);
548 if (rc < 0)
549 return rc;
Harald Welte1602ade2009-01-29 21:12:39 +0000550 if (sw->last_seg)
551 break;
Harald Welte4724f992009-01-18 18:01:49 +0000552 }
553 return 0;
554}
555
556/* callback function from abis_nm_rcvmsg() handler */
557static int abis_nm_rcvmsg_sw(struct msgb *mb)
558{
559 struct abis_om_fom_hdr *foh = msgb_l3(mb);
560 int rc = -1;
561 struct abis_nm_sw *sw = &g_sw;
562 enum sw_state old_state = sw->state;
563
Harald Welte3ffd1372009-02-01 22:15:49 +0000564 //DEBUGP(DNM, "state %u, NM MT 0x%02x\n", sw->state, foh->msg_type);
Harald Welte4724f992009-01-18 18:01:49 +0000565
566 switch (sw->state) {
567 case SW_STATE_WAIT_INITACK:
568 switch (foh->msg_type) {
569 case NM_MT_LOAD_INIT_ACK:
570 /* fill window with segments */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000571 if (sw->cbfn)
572 sw->cbfn(GSM_HOOK_NM_SWLOAD,
573 NM_MT_LOAD_INIT_ACK, mb,
574 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000575 rc = sw_fill_window(sw);
576 sw->state = SW_STATE_WAIT_SEGACK;
577 break;
578 case NM_MT_LOAD_INIT_NACK:
Harald Welte3ffd1372009-02-01 22:15:49 +0000579 if (sw->forced) {
580 DEBUGP(DNM, "FORCED: Ignoring Software Load "
581 "Init NACK\n");
582 if (sw->cbfn)
583 sw->cbfn(GSM_HOOK_NM_SWLOAD,
584 NM_MT_LOAD_INIT_ACK, mb,
585 sw->cb_data, NULL);
586 rc = sw_fill_window(sw);
587 sw->state = SW_STATE_WAIT_SEGACK;
588 } else {
589 DEBUGP(DNM, "Software Load Init NACK\n");
590 if (sw->cbfn)
591 sw->cbfn(GSM_HOOK_NM_SWLOAD,
592 NM_MT_LOAD_INIT_NACK, mb,
593 sw->cb_data, NULL);
594 sw->state = SW_STATE_ERROR;
595 }
Harald Welte4724f992009-01-18 18:01:49 +0000596 break;
597 }
598 break;
599 case SW_STATE_WAIT_SEGACK:
600 switch (foh->msg_type) {
601 case NM_MT_LOAD_SEG_ACK:
Harald Welte3ffd1372009-02-01 22:15:49 +0000602 if (sw->cbfn)
603 sw->cbfn(GSM_HOOK_NM_SWLOAD,
604 NM_MT_LOAD_SEG_ACK, mb,
605 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000606 sw->seg_in_window = 0;
Harald Welte1602ade2009-01-29 21:12:39 +0000607 if (!sw->last_seg) {
608 /* fill window with more segments */
609 rc = sw_fill_window(sw);
610 sw->state = SW_STATE_WAIT_SEGACK;
611 } else {
612 /* end the transfer */
613 sw->state = SW_STATE_WAIT_ENDACK;
614 rc = sw_load_end(sw);
615 }
Harald Welte4724f992009-01-18 18:01:49 +0000616 break;
617 }
618 break;
619 case SW_STATE_WAIT_ENDACK:
620 switch (foh->msg_type) {
621 case NM_MT_LOAD_END_ACK:
622 sw_close_file(sw);
Harald Welte5e4d1b32009-02-01 13:36:56 +0000623 DEBUGP(DNM, "Software Load End (BTS %u)\n",
624 sw->bts->nr);
625 sw->state = SW_STATE_NONE;
626 if (sw->cbfn)
627 sw->cbfn(GSM_HOOK_NM_SWLOAD,
628 NM_MT_LOAD_END_ACK, mb,
629 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000630 break;
631 case NM_MT_LOAD_END_NACK:
Holger Freyther31338a12009-02-06 17:43:50 +0000632 if (sw->forced) {
633 DEBUGP(DNM, "FORCED: Ignoring Software Load"
634 "End NACK\n");
635 sw->state = SW_STATE_NONE;
636 if (sw->cbfn)
637 sw->cbfn(GSM_HOOK_NM_SWLOAD,
638 NM_MT_LOAD_END_ACK, mb,
639 sw->cb_data, NULL);
640 } else {
641 DEBUGP(DNM, "Software Load End NACK\n");
642 sw->state = SW_STATE_ERROR;
643 if (sw->cbfn)
644 sw->cbfn(GSM_HOOK_NM_SWLOAD,
645 NM_MT_LOAD_END_NACK, mb,
646 sw->cb_data, NULL);
647 }
Harald Welte4724f992009-01-18 18:01:49 +0000648 break;
649 }
650 case SW_STATE_WAIT_ACTACK:
651 switch (foh->msg_type) {
652 case NM_MT_ACTIVATE_SW_ACK:
653 /* we're done */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000654 DEBUGP(DNM, "Activate Software DONE!\n");
Harald Welte4724f992009-01-18 18:01:49 +0000655 sw->state = SW_STATE_NONE;
656 rc = 0;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000657 if (sw->cbfn)
658 sw->cbfn(GSM_HOOK_NM_SWLOAD,
659 NM_MT_ACTIVATE_SW_ACK, mb,
660 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000661 break;
662 case NM_MT_ACTIVATE_SW_NACK:
Harald Welte1602ade2009-01-29 21:12:39 +0000663 DEBUGP(DNM, "Activate Software NACK\n");
Harald Welte4724f992009-01-18 18:01:49 +0000664 sw->state = SW_STATE_ERROR;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000665 if (sw->cbfn)
666 sw->cbfn(GSM_HOOK_NM_SWLOAD,
667 NM_MT_ACTIVATE_SW_NACK, mb,
668 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000669 break;
670 }
671 case SW_STATE_NONE:
672 case SW_STATE_ERROR:
673 break;
674 }
675
676 if (rc)
677 fprintf(stderr, "unexpected NM MT 0x%02x in state %u -> %u\n",
678 foh->msg_type, old_state, sw->state);
679
680 return rc;
681}
682
683/* Load the specified software into the BTS */
684int abis_nm_software_load(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +0000685 u_int8_t win_size, int forced,
686 gsm_cbfn *cbfn, void *cb_data)
Harald Welte4724f992009-01-18 18:01:49 +0000687{
688 struct abis_nm_sw *sw = &g_sw;
689 int rc;
690
Harald Welte5e4d1b32009-02-01 13:36:56 +0000691 DEBUGP(DNM, "Software Load (BTS %u, File \"%s\")\n",
692 bts->nr, fname);
693
Harald Welte4724f992009-01-18 18:01:49 +0000694 if (sw->state != SW_STATE_NONE)
695 return -EBUSY;
696
697 sw->bts = bts;
698 sw->obj_class = NM_OC_SITE_MANAGER;
699 sw->obj_instance[0] = 0xff;
700 sw->obj_instance[1] = 0xff;
701 sw->obj_instance[2] = 0xff;
702 sw->window_size = win_size;
703 sw->state = SW_STATE_WAIT_INITACK;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000704 sw->cbfn = cbfn;
705 sw->cb_data = cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000706 sw->forced = forced;
Harald Welte4724f992009-01-18 18:01:49 +0000707
708 rc = sw_open_file(sw, fname);
709 if (rc < 0) {
710 sw->state = SW_STATE_NONE;
711 return rc;
712 }
713
714 return sw_load_init(sw);
715}
Harald Welte52b1f982008-12-23 20:25:15 +0000716
Harald Welte1602ade2009-01-29 21:12:39 +0000717int abis_nm_software_load_status(struct gsm_bts *bts)
718{
719 struct abis_nm_sw *sw = &g_sw;
720 struct stat st;
721 int rc, percent;
722
723 rc = fstat(sw->fd, &st);
724 if (rc < 0) {
725 perror("ERROR during stat");
726 return rc;
727 }
728
729 percent = (ftell(sw->stream) * 100) / st.st_size;
730 return percent;
731}
732
Harald Welte5e4d1b32009-02-01 13:36:56 +0000733/* Activate the specified software into the BTS */
734int abis_nm_software_activate(struct gsm_bts *bts, const char *fname,
735 gsm_cbfn *cbfn, void *cb_data)
736{
737 struct abis_nm_sw *sw = &g_sw;
738 int rc;
739
740 DEBUGP(DNM, "Activating Software (BTS %u, File \"%s\")\n",
741 bts->nr, fname);
742
743 if (sw->state != SW_STATE_NONE)
744 return -EBUSY;
745
746 sw->bts = bts;
747 sw->obj_class = NM_OC_SITE_MANAGER;
748 sw->obj_instance[0] = 0xff;
749 sw->obj_instance[1] = 0xff;
750 sw->obj_instance[2] = 0xff;
751 sw->state = SW_STATE_WAIT_ACTACK;
752 sw->cbfn = cbfn;
753 sw->cb_data = cb_data;
754
755 /* Open the file in order to fill some sw struct members */
756 rc = sw_open_file(sw, fname);
757 if (rc < 0) {
758 sw->state = SW_STATE_NONE;
759 return rc;
760 }
761 sw_close_file(sw);
762
763 return sw_activate(sw);
764}
765
Harald Welte8470bf22008-12-25 23:28:35 +0000766static void fill_nm_channel(struct abis_nm_channel *ch, u_int8_t bts_port,
Harald Welte52b1f982008-12-23 20:25:15 +0000767 u_int8_t ts_nr, u_int8_t subslot_nr)
768{
Harald Welteadaf08b2009-01-18 11:08:10 +0000769 ch->attrib = NM_ATT_ABIS_CHANNEL;
Harald Welte52b1f982008-12-23 20:25:15 +0000770 ch->bts_port = bts_port;
771 ch->timeslot = ts_nr;
772 ch->subslot = subslot_nr;
773}
774
775int abis_nm_establish_tei(struct gsm_bts *bts, u_int8_t trx_nr,
776 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot,
777 u_int8_t tei)
778{
779 struct abis_om_hdr *oh;
780 struct abis_nm_channel *ch;
Harald Welte702d8702008-12-26 20:25:35 +0000781 u_int8_t len = sizeof(*ch) + 2;
Harald Welte8470bf22008-12-25 23:28:35 +0000782 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000783
784 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
785 fill_om_fom_hdr(oh, len, NM_MT_ESTABLISH_TEI, NM_OC_RADIO_CARRIER,
786 bts->bts_nr, trx_nr, 0xff);
787
Harald Welte8470bf22008-12-25 23:28:35 +0000788 msgb_tv_put(msg, NM_ATT_TEI, tei);
Harald Welte52b1f982008-12-23 20:25:15 +0000789
790 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
791 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
792
793 return abis_nm_sendmsg(bts, msg);
794}
795
796/* connect signalling of one (BTS,TRX) to a particular timeslot on the E1 */
797int abis_nm_conn_terr_sign(struct gsm_bts_trx *trx,
798 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot)
799{
Harald Welte8470bf22008-12-25 23:28:35 +0000800 struct gsm_bts *bts = trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000801 struct abis_om_hdr *oh;
802 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000803 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000804
805 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000806 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_SIGN,
Harald Welte52b1f982008-12-23 20:25:15 +0000807 NM_OC_RADIO_CARRIER, bts->bts_nr, trx->nr, 0xff);
808
809 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
810 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
811
812 return abis_nm_sendmsg(bts, msg);
813}
814
815#if 0
816int abis_nm_disc_terr_sign(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
817 struct abis_nm_abis_channel *chan)
818{
819}
820#endif
821
822int abis_nm_conn_terr_traf(struct gsm_bts_trx_ts *ts,
823 u_int8_t e1_port, u_int8_t e1_timeslot,
824 u_int8_t e1_subslot)
825{
826 struct gsm_bts *bts = ts->trx->bts;
827 struct abis_om_hdr *oh;
828 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000829 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000830
831 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
832 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_TRAF,
833 NM_OC_BASEB_TRANSC, bts->bts_nr, ts->trx->nr, ts->nr);
834
835 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
836 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
837
838 return abis_nm_sendmsg(bts, msg);
839}
840
841#if 0
842int abis_nm_disc_terr_traf(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
843 struct abis_nm_abis_channel *chan,
844 u_int8_t subchan)
845{
846}
847#endif
848
849int abis_nm_set_channel_attr(struct gsm_bts_trx_ts *ts, u_int8_t chan_comb)
850{
851 struct gsm_bts *bts = ts->trx->bts;
852 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000853 u_int16_t arfcn = htons(ts->trx->arfcn);
Harald Welte52b1f982008-12-23 20:25:15 +0000854 u_int8_t zero = 0x00;
Harald Welte8470bf22008-12-25 23:28:35 +0000855 struct msgb *msg = nm_msgb_alloc();
Harald Welte702d8702008-12-26 20:25:35 +0000856 u_int8_t len = 4 + 2 + 2 + 2 + 2 +3;
Harald Welte52b1f982008-12-23 20:25:15 +0000857
858 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000859 fill_om_fom_hdr(oh, len, NM_MT_SET_CHAN_ATTR,
Harald Welte52b1f982008-12-23 20:25:15 +0000860 NM_OC_BASEB_TRANSC, bts->bts_nr,
861 ts->trx->nr, ts->nr);
862 /* FIXME: don't send ARFCN list, hopping sequence, mAIO, ...*/
863 msgb_tlv16_put(msg, NM_ATT_ARFCN_LIST, 1, &arfcn);
864 msgb_tv_put(msg, NM_ATT_CHAN_COMB, chan_comb);
865 msgb_tv_put(msg, NM_ATT_HSN, 0x00);
866 msgb_tv_put(msg, NM_ATT_MAIO, 0x00);
867 msgb_tv_put(msg, NM_ATT_TSC, 0x07); /* training sequence */
868 msgb_tlv_put(msg, 0x59, 1, &zero);
869
870 return abis_nm_sendmsg(bts, msg);
871}
872
Harald Welte8470bf22008-12-25 23:28:35 +0000873int abis_nm_raw_msg(struct gsm_bts *bts, int len, u_int8_t *rawmsg)
Harald Welte52b1f982008-12-23 20:25:15 +0000874{
Harald Welte8470bf22008-12-25 23:28:35 +0000875 struct msgb *msg = nm_msgb_alloc();
876 struct abis_om_hdr *oh;
Harald Welte52b1f982008-12-23 20:25:15 +0000877 u_int8_t *data;
878
879 oh = (struct abis_om_hdr *) msgb_put(msg, sizeof(*oh));
880 fill_om_hdr(oh, len);
881 data = msgb_put(msg, len);
Harald Weltead384642008-12-26 10:20:07 +0000882 memcpy(data, rawmsg, len);
Harald Welte52b1f982008-12-23 20:25:15 +0000883
884 return abis_nm_sendmsg(bts, msg);
885}
886
887/* Siemens specific commands */
888static int __simple_cmd(struct gsm_bts *bts, u_int8_t msg_type)
889{
890 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000891 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000892
893 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000894 fill_om_fom_hdr(oh, 0, msg_type, NM_OC_SITE_MANAGER,
Harald Welte52b1f982008-12-23 20:25:15 +0000895 0xff, 0xff, 0xff);
896
897 return abis_nm_sendmsg(bts, msg);
898}
899
900int abis_nm_event_reports(struct gsm_bts *bts, int on)
901{
902 if (on == 0)
Harald Welte227d4072009-01-03 08:16:25 +0000903 return __simple_cmd(bts, NM_MT_STOP_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000904 else
Harald Welte227d4072009-01-03 08:16:25 +0000905 return __simple_cmd(bts, NM_MT_REST_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000906}
907
Harald Welte3ffd1372009-02-01 22:15:49 +0000908
Harald Welte47d88ae2009-01-04 12:02:08 +0000909/* Siemens (or BS-11) specific commands */
910
Harald Welte3ffd1372009-02-01 22:15:49 +0000911int abis_nm_bs11_bsc_disconnect(struct gsm_bts *bts, int reconnect)
912{
913 if (reconnect == 0)
914 return __simple_cmd(bts, NM_MT_BS11_DISCONNECT);
915 else
916 return __simple_cmd(bts, NM_MT_BS11_RECONNECT);
917}
918
Harald Welteb8427972009-02-05 19:27:17 +0000919int abis_nm_bs11_restart(struct gsm_bts *bts)
920{
921 return __simple_cmd(bts, NM_MT_BS11_RESTART);
922}
923
924
Harald Welte268bb402009-02-01 19:11:56 +0000925struct bs11_date_time {
926 u_int16_t year;
927 u_int8_t month;
928 u_int8_t day;
929 u_int8_t hour;
930 u_int8_t min;
931 u_int8_t sec;
932} __attribute__((packed));
933
934
935void get_bs11_date_time(struct bs11_date_time *aet)
936{
937 time_t t;
938 struct tm *tm;
939
940 t = time(NULL);
941 tm = localtime(&t);
942 aet->sec = tm->tm_sec;
943 aet->min = tm->tm_min;
944 aet->hour = tm->tm_hour;
945 aet->day = tm->tm_mday;
946 aet->month = tm->tm_mon;
947 aet->year = htons(1900 + tm->tm_year);
948}
949
Harald Welte05188ee2009-01-18 11:39:08 +0000950int abis_nm_bs11_reset_resource(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000951{
Harald Welte4668fda2009-01-03 08:19:29 +0000952 return __simple_cmd(bts, NM_MT_BS11_RESET_RESOURCE);
Harald Welte52b1f982008-12-23 20:25:15 +0000953}
954
Harald Welte05188ee2009-01-18 11:39:08 +0000955int abis_nm_bs11_db_transmission(struct gsm_bts *bts, int begin)
Harald Welte52b1f982008-12-23 20:25:15 +0000956{
957 if (begin)
Harald Welte4668fda2009-01-03 08:19:29 +0000958 return __simple_cmd(bts, NM_MT_BS11_BEGIN_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000959 else
Harald Welte4668fda2009-01-03 08:19:29 +0000960 return __simple_cmd(bts, NM_MT_BS11_END_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000961}
Harald Welte47d88ae2009-01-04 12:02:08 +0000962
Harald Welte05188ee2009-01-18 11:39:08 +0000963int abis_nm_bs11_create_object(struct gsm_bts *bts,
Harald Welte1bc09062009-01-18 14:17:52 +0000964 enum abis_bs11_objtype type, u_int8_t idx,
965 u_int8_t attr_len, const u_int8_t *attr)
Harald Welte47d88ae2009-01-04 12:02:08 +0000966{
967 struct abis_om_hdr *oh;
968 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000969 u_int8_t *cur;
Harald Welte47d88ae2009-01-04 12:02:08 +0000970
971 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000972 fill_om_fom_hdr(oh, attr_len, NM_MT_BS11_CREATE_OBJ,
Harald Welte268bb402009-02-01 19:11:56 +0000973 NM_OC_BS11, type, 0, idx);
Harald Welte1bc09062009-01-18 14:17:52 +0000974 cur = msgb_put(msg, attr_len);
975 memcpy(cur, attr, attr_len);
Harald Welte47d88ae2009-01-04 12:02:08 +0000976
977 return abis_nm_sendmsg(bts, msg);
978}
979
Harald Welte05188ee2009-01-18 11:39:08 +0000980int abis_nm_bs11_create_envaBTSE(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000981{
982 struct abis_om_hdr *oh;
983 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000984 u_int8_t zero = 0x00;
Harald Welte47d88ae2009-01-04 12:02:08 +0000985
986 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000987 fill_om_fom_hdr(oh, 3, NM_MT_BS11_CREATE_OBJ,
Harald Welte1bc09062009-01-18 14:17:52 +0000988 NM_OC_BS11_ENVABTSE, 0, idx, 0xff);
989 msgb_tlv_put(msg, 0x99, 1, &zero);
Harald Welte47d88ae2009-01-04 12:02:08 +0000990
991 return abis_nm_sendmsg(bts, msg);
992}
993
Harald Welte05188ee2009-01-18 11:39:08 +0000994int abis_nm_bs11_create_bport(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000995{
996 struct abis_om_hdr *oh;
997 struct msgb *msg = nm_msgb_alloc();
998
999 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1000 fill_om_fom_hdr(oh, 0, NM_MT_BS11_CREATE_OBJ, NM_OC_BS11_BPORT,
1001 idx, 0, 0);
1002
1003 return abis_nm_sendmsg(bts, msg);
1004}
Harald Welte05188ee2009-01-18 11:39:08 +00001005
1006int abis_nm_bs11_set_oml_tei(struct gsm_bts *bts, u_int8_t tei)
1007{
1008 struct abis_om_hdr *oh;
1009 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +00001010
1011 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001012 fill_om_fom_hdr(oh, 2, NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
Harald Welte05188ee2009-01-18 11:39:08 +00001013 0xff, 0xff, 0xff);
1014 msgb_tv_put(msg, NM_ATT_TEI, tei);
1015
1016 return abis_nm_sendmsg(bts, msg);
1017}
1018
1019/* like abis_nm_conn_terr_traf */
1020int abis_nm_bs11_conn_oml(struct gsm_bts *bts, u_int8_t e1_port,
1021 u_int8_t e1_timeslot, u_int8_t e1_subslot)
1022{
1023 struct abis_om_hdr *oh;
1024 struct abis_nm_channel *ch;
1025 struct msgb *msg = nm_msgb_alloc();
1026
1027 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte05188ee2009-01-18 11:39:08 +00001028 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_BS11_SET_ATTR,
1029 NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff);
1030
1031 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
1032 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
1033
1034 return abis_nm_sendmsg(bts, msg);
1035}
1036
1037int abis_nm_bs11_set_trx_power(struct gsm_bts_trx *trx, u_int8_t level)
1038{
1039 struct abis_om_hdr *oh;
1040 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +00001041
1042 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001043 fill_om_fom_hdr(oh, 3, NM_MT_BS11_SET_ATTR,
Harald Welte05188ee2009-01-18 11:39:08 +00001044 NM_OC_BS11, BS11_OBJ_PA, 0x00, trx->nr);
1045 msgb_tlv_put(msg, NM_ATT_BS11_TXPWR, 1, &level);
1046
1047 return abis_nm_sendmsg(trx->bts, msg);
1048}
1049
Harald Welte268bb402009-02-01 19:11:56 +00001050//static const u_int8_t bs11_logon_c7[] = { 0x07, 0xd9, 0x01, 0x11, 0x0d, 0x10, 0x20 };
Harald Weltebb151312009-01-28 20:42:07 +00001051static const u_int8_t bs11_logon_c8[] = { 0x02 };
Harald Welte05188ee2009-01-18 11:39:08 +00001052static const u_int8_t bs11_logon_c9[] = "FACTORY";
1053
Harald Welte1bc09062009-01-18 14:17:52 +00001054int abis_nm_bs11_factory_logon(struct gsm_bts *bts, int on)
Harald Welte05188ee2009-01-18 11:39:08 +00001055{
1056 struct abis_om_hdr *oh;
1057 struct msgb *msg = nm_msgb_alloc();
Harald Welte268bb402009-02-01 19:11:56 +00001058 struct bs11_date_time bdt;
1059
1060 get_bs11_date_time(&bdt);
Harald Welte05188ee2009-01-18 11:39:08 +00001061
1062 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte1bc09062009-01-18 14:17:52 +00001063 if (on) {
Harald Welte268bb402009-02-01 19:11:56 +00001064 u_int8_t len = 3*2 + sizeof(bdt)
Harald Welte6f676a32009-01-18 14:27:48 +00001065 + sizeof(bs11_logon_c8) + sizeof(bs11_logon_c9);
Harald Welte043d04a2009-01-29 23:15:30 +00001066 fill_om_fom_hdr(oh, len, NM_MT_BS11_LMT_LOGON,
Harald Welte1bc09062009-01-18 14:17:52 +00001067 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
Harald Welte043d04a2009-01-29 23:15:30 +00001068 msgb_tlv_put(msg, NM_ATT_BS11_LMT_LOGIN_TIME,
Harald Welte5083b0b2009-02-02 19:20:52 +00001069 sizeof(bdt), (u_int8_t *) &bdt);
Harald Welte043d04a2009-01-29 23:15:30 +00001070 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_ACC_LEV,
1071 sizeof(bs11_logon_c8), bs11_logon_c8);
1072 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_NAME,
1073 sizeof(bs11_logon_c9), bs11_logon_c9);
Harald Welte1bc09062009-01-18 14:17:52 +00001074 } else {
Harald Welte5e4d1b32009-02-01 13:36:56 +00001075 fill_om_fom_hdr(oh, 0, NM_MT_BS11_LMT_LOGOFF,
Harald Welte1bc09062009-01-18 14:17:52 +00001076 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
1077 }
Harald Welte05188ee2009-01-18 11:39:08 +00001078
1079 return abis_nm_sendmsg(bts, msg);
1080}
Harald Welte1bc09062009-01-18 14:17:52 +00001081
1082int abis_nm_bs11_set_trx1_pw(struct gsm_bts *bts, const char *password)
1083{
1084 struct abis_om_hdr *oh;
1085 struct msgb *msg;
1086
1087 if (strlen(password) != 10)
1088 return -EINVAL;
1089
1090 msg = nm_msgb_alloc();
1091 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001092 fill_om_fom_hdr(oh, 2+strlen(password), NM_MT_BS11_SET_ATTR,
Harald Welte1bc09062009-01-18 14:17:52 +00001093 NM_OC_BS11, BS11_OBJ_TRX1, 0x00, 0x00);
1094 msgb_tlv_put(msg, NM_ATT_BS11_PASSWORD, 10, (const u_int8_t *)password);
1095
1096 return abis_nm_sendmsg(bts, msg);
1097}
1098
1099int abis_nm_bs11_get_state(struct gsm_bts *bts)
1100{
1101 return __simple_cmd(bts, NM_MT_BS11_GET_STATE);
1102}
Harald Welte5e4d1b32009-02-01 13:36:56 +00001103
1104/* BS11 SWL */
1105
1106struct abis_nm_bs11_sw {
1107 struct gsm_bts *bts;
1108 char swl_fname[PATH_MAX];
1109 u_int8_t win_size;
Harald Welte3ffd1372009-02-01 22:15:49 +00001110 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001111 struct llist_head file_list;
1112 gsm_cbfn *user_cb; /* specified by the user */
1113};
1114static struct abis_nm_bs11_sw _g_bs11_sw, *g_bs11_sw = &_g_bs11_sw;
1115
1116struct file_list_entry {
1117 struct llist_head list;
1118 char fname[PATH_MAX];
1119};
1120
1121struct file_list_entry *fl_dequeue(struct llist_head *queue)
1122{
1123 struct llist_head *lh;
1124
1125 if (llist_empty(queue))
1126 return NULL;
1127
1128 lh = queue->next;
1129 llist_del(lh);
1130
1131 return llist_entry(lh, struct file_list_entry, list);
1132}
1133
1134static int bs11_read_swl_file(struct abis_nm_bs11_sw *bs11_sw)
1135{
1136 char linebuf[255];
1137 struct llist_head *lh, *lh2;
1138 FILE *swl;
1139 int rc = 0;
1140
1141 swl = fopen(bs11_sw->swl_fname, "r");
1142 if (!swl)
1143 return -ENODEV;
1144
1145 /* zero the stale file list, if any */
1146 llist_for_each_safe(lh, lh2, &bs11_sw->file_list) {
1147 llist_del(lh);
1148 free(lh);
1149 }
1150
1151 while (fgets(linebuf, sizeof(linebuf), swl)) {
1152 char file_id[12+1];
1153 char file_version[80+1];
1154 struct file_list_entry *fle;
1155 static char dir[PATH_MAX];
1156
1157 if (strlen(linebuf) < 4)
1158 continue;
Harald Welte3ffd1372009-02-01 22:15:49 +00001159
Harald Welte5e4d1b32009-02-01 13:36:56 +00001160 rc = sscanf(linebuf+4, "%12s:%80s\r\n", file_id, file_version);
1161 if (rc < 0) {
1162 perror("ERR parsing SWL file");
1163 rc = -EINVAL;
1164 goto out;
1165 }
1166 if (rc < 2)
1167 continue;
1168
1169 fle = malloc(sizeof(*fle));
1170 if (!fle) {
1171 rc = -ENOMEM;
1172 goto out;
1173 }
1174 memset(fle, 0, sizeof(*fle));
1175
1176 /* construct new filename */
1177 strncpy(dir, bs11_sw->swl_fname, sizeof(dir));
1178 strncat(fle->fname, dirname(dir), sizeof(fle->fname) - 1);
1179 strcat(fle->fname, "/");
1180 strncat(fle->fname, file_id, sizeof(fle->fname) - 1 -strlen(fle->fname));
Harald Welte5e4d1b32009-02-01 13:36:56 +00001181
1182 llist_add_tail(&fle->list, &bs11_sw->file_list);
1183 }
1184
1185out:
1186 fclose(swl);
1187 return rc;
1188}
1189
1190/* bs11 swload specific callback, passed to abis_nm core swload */
1191static int bs11_swload_cbfn(unsigned int hook, unsigned int event,
1192 struct msgb *msg, void *data, void *param)
1193{
1194 struct abis_nm_bs11_sw *bs11_sw = data;
1195 struct file_list_entry *fle;
1196 int rc = 0;
1197
Harald Welte97ed1e72009-02-06 13:38:02 +00001198 DEBUGP(DNM, "Event %u\n", event);
1199
Harald Welte5e4d1b32009-02-01 13:36:56 +00001200 switch (event) {
1201 case NM_MT_LOAD_END_ACK:
1202 fle = fl_dequeue(&bs11_sw->file_list);
1203 if (fle) {
1204 /* start download the next file of our file list */
1205 rc = abis_nm_software_load(bs11_sw->bts, fle->fname,
1206 bs11_sw->win_size,
Harald Welte3ffd1372009-02-01 22:15:49 +00001207 bs11_sw->forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001208 &bs11_swload_cbfn, bs11_sw);
1209 free(fle);
1210 } else {
1211 /* activate the SWL */
1212 rc = abis_nm_software_activate(bs11_sw->bts,
1213 bs11_sw->swl_fname,
1214 bs11_swload_cbfn,
1215 bs11_sw);
1216 }
1217 break;
Harald Welte3ffd1372009-02-01 22:15:49 +00001218 case NM_MT_LOAD_SEG_ACK:
Harald Welte5e4d1b32009-02-01 13:36:56 +00001219 case NM_MT_LOAD_END_NACK:
1220 case NM_MT_LOAD_INIT_ACK:
1221 case NM_MT_LOAD_INIT_NACK:
1222 case NM_MT_ACTIVATE_SW_NACK:
1223 case NM_MT_ACTIVATE_SW_ACK:
1224 default:
1225 /* fallthrough to the user callback */
Harald Welte97ed1e72009-02-06 13:38:02 +00001226 if (bs11_sw->user_cb)
1227 rc = bs11_sw->user_cb(hook, event, msg, NULL, NULL);
Harald Welte5e4d1b32009-02-01 13:36:56 +00001228 break;
1229 }
1230
1231 return rc;
1232}
1233
1234/* Siemens provides a SWL file that is a mere listing of all the other
1235 * files that are part of a software release. We need to upload first
1236 * the list file, and then each file that is listed in the list file */
1237int abis_nm_bs11_load_swl(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +00001238 u_int8_t win_size, int forced, gsm_cbfn *cbfn)
Harald Welte5e4d1b32009-02-01 13:36:56 +00001239{
1240 struct abis_nm_bs11_sw *bs11_sw = g_bs11_sw;
1241 struct file_list_entry *fle;
1242 int rc = 0;
1243
1244 INIT_LLIST_HEAD(&bs11_sw->file_list);
1245 bs11_sw->bts = bts;
1246 bs11_sw->win_size = win_size;
1247 bs11_sw->user_cb = cbfn;
Harald Welte3ffd1372009-02-01 22:15:49 +00001248 bs11_sw->forced = forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001249
1250 strncpy(bs11_sw->swl_fname, fname, sizeof(bs11_sw->swl_fname));
1251 rc = bs11_read_swl_file(bs11_sw);
1252 if (rc < 0)
1253 return rc;
1254
1255 /* dequeue next item in file list */
1256 fle = fl_dequeue(&bs11_sw->file_list);
1257 if (!fle)
1258 return -EINVAL;
1259
1260 /* start download the next file of our file list */
Harald Welte3ffd1372009-02-01 22:15:49 +00001261 rc = abis_nm_software_load(bts, fle->fname, win_size, forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001262 bs11_swload_cbfn, bs11_sw);
1263 free(fle);
1264 return rc;
1265}
1266
Harald Welte5083b0b2009-02-02 19:20:52 +00001267#if 0
Harald Welte5e4d1b32009-02-01 13:36:56 +00001268static u_int8_t req_attr_btse[] = {
1269 NM_ATT_ADM_STATE, NM_ATT_BS11_LMT_LOGON_SESSION,
1270 NM_ATT_BS11_LMT_LOGIN_TIME, NM_ATT_BS11_LMT_USER_ACC_LEV,
1271 NM_ATT_BS11_LMT_USER_NAME,
1272
1273 0xaf, NM_ATT_BS11_RX_OFFSET, NM_ATT_BS11_VENDOR_NAME,
1274
1275 NM_ATT_BS11_SW_LOAD_INTENDED, NM_ATT_BS11_SW_LOAD_SAFETY,
1276
1277 NM_ATT_BS11_SW_LOAD_STORED };
1278
1279static u_int8_t req_attr_btsm[] = {
1280 NM_ATT_ABIS_CHANNEL, NM_ATT_TEI, NM_ATT_BS11_ABIS_EXT_TIME,
1281 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xce, NM_ATT_FILE_ID,
1282 NM_ATT_FILE_VERSION, NM_ATT_OPER_STATE, 0xe8, NM_ATT_BS11_ALL_TEST_CATG,
1283 NM_ATT_SW_DESCR, NM_ATT_GET_ARI };
Harald Welte5083b0b2009-02-02 19:20:52 +00001284#endif
Harald Welte5e4d1b32009-02-01 13:36:56 +00001285
1286static u_int8_t req_attr[] = {
1287 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xa8, NM_ATT_OPER_STATE,
1288 0xd5, 0xa1, NM_ATT_BS11_ESN_FW_CODE_NO, NM_ATT_BS11_ESN_HW_CODE_NO,
1289 0x42, NM_ATT_BS11_ESN_PCB_SERIAL };
1290
1291int abis_nm_bs11_get_serno(struct gsm_bts *bts)
1292{
1293 struct abis_om_hdr *oh;
1294 struct msgb *msg = nm_msgb_alloc();
1295
1296 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1297 /* SiemensHW CCTRL object */
1298 fill_om_fom_hdr(oh, 2+sizeof(req_attr), NM_MT_GET_ATTR, NM_OC_BS11,
1299 0x03, 0x00, 0x00);
1300 msgb_tlv_put(msg, NM_ATT_LIST_REQ_ATTR, sizeof(req_attr), req_attr);
1301
1302 return abis_nm_sendmsg(bts, msg);
1303}
Harald Welte268bb402009-02-01 19:11:56 +00001304
1305int abis_nm_bs11_set_ext_time(struct gsm_bts *bts)
1306{
1307 struct abis_om_hdr *oh;
1308 struct msgb *msg = nm_msgb_alloc();
1309 struct bs11_date_time aet;
1310
1311 get_bs11_date_time(&aet);
1312 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1313 /* SiemensHW CCTRL object */
1314 fill_om_fom_hdr(oh, 2+sizeof(aet), NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
1315 0xff, 0xff, 0xff);
Harald Welte5083b0b2009-02-02 19:20:52 +00001316 msgb_tlv_put(msg, NM_ATT_BS11_ABIS_EXT_TIME, sizeof(aet), (u_int8_t *) &aet);
Harald Welte268bb402009-02-01 19:11:56 +00001317
1318 return abis_nm_sendmsg(bts, msg);
1319}