blob: 8ebd96d50ae61d4e405b3861a01ffbfc14cfde06 [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 Welte4724f992009-01-18 18:01:49 +000032
Harald Welte52b1f982008-12-23 20:25:15 +000033#include <sys/types.h>
Harald Welte4724f992009-01-18 18:01:49 +000034#include <sys/stat.h>
Harald Welte8470bf22008-12-25 23:28:35 +000035#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000036
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/gsm_data.h>
38#include <openbsc/debug.h>
39#include <openbsc/msgb.h>
40#include <openbsc/tlv.h>
41#include <openbsc/abis_nm.h>
Holger Freytherca362a62009-01-04 21:05:01 +000042#include <openbsc/misdn.h>
Harald Welte52b1f982008-12-23 20:25:15 +000043
Harald Welte8470bf22008-12-25 23:28:35 +000044#define OM_ALLOC_SIZE 1024
45#define OM_HEADROOM_SIZE 128
Harald Welte52b1f982008-12-23 20:25:15 +000046
47/* unidirectional messages from BTS to BSC */
48static const enum abis_nm_msgtype reports[] = {
49 NM_MT_SW_ACTIVATED_REP,
50 NM_MT_TEST_REP,
51 NM_MT_STATECHG_EVENT_REP,
52 NM_MT_FAILURE_EVENT_REP,
53};
54
55/* messages without ACK/NACK */
56static const enum abis_nm_msgtype no_ack_nack[] = {
57 NM_MT_MEAS_RES_REQ,
58 NM_MT_STOP_MEAS,
59 NM_MT_START_MEAS,
60};
61
Harald Welte4724f992009-01-18 18:01:49 +000062/* Messages related to software load */
63static const enum abis_nm_msgtype sw_load_msgs[] = {
64 NM_MT_LOAD_INIT_ACK,
65 NM_MT_LOAD_INIT_NACK,
66 NM_MT_LOAD_SEG_ACK,
67 NM_MT_LOAD_ABORT,
68 NM_MT_LOAD_END_ACK,
69 NM_MT_LOAD_END_NACK,
70 NM_MT_SW_ACT_REQ,
71 NM_MT_ACTIVATE_SW_ACK,
72 NM_MT_ACTIVATE_SW_NACK,
73 NM_MT_SW_ACTIVATED_REP,
74};
75
Harald Welte52b1f982008-12-23 20:25:15 +000076/* Attributes that the BSC can set, not only get, according to Section 9.4 */
77static const enum abis_nm_attr nm_att_settable[] = {
78 NM_ATT_ADD_INFO,
79 NM_ATT_ADD_TEXT,
80 NM_ATT_DEST,
81 NM_ATT_EVENT_TYPE,
82 NM_ATT_FILE_DATA,
83 NM_ATT_GET_ARI,
84 NM_ATT_HW_CONF_CHG,
85 NM_ATT_LIST_REQ_ATTR,
86 NM_ATT_MDROP_LINK,
87 NM_ATT_MDROP_NEXT,
88 NM_ATT_NACK_CAUSES,
89 NM_ATT_OUTST_ALARM,
90 NM_ATT_PHYS_CONF,
91 NM_ATT_PROB_CAUSE,
92 NM_ATT_RAD_SUBC,
93 NM_ATT_SOURCE,
94 NM_ATT_SPEC_PROB,
95 NM_ATT_START_TIME,
96 NM_ATT_TEST_DUR,
97 NM_ATT_TEST_NO,
98 NM_ATT_TEST_REPORT,
99 NM_ATT_WINDOW_SIZE,
100 NM_ATT_SEVERITY,
101 NM_ATT_MEAS_RES,
102 NM_ATT_MEAS_TYPE,
103};
104
105static int is_in_arr(enum abis_nm_msgtype mt, const enum abis_nm_msgtype *arr, int size)
106{
107 int i;
108
109 for (i = 0; i < size; i++) {
110 if (arr[i] == mt)
111 return 1;
112 }
113
114 return 0;
115}
116
Holger Freytherca362a62009-01-04 21:05:01 +0000117#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000118/* is this msgtype the usual ACK/NACK type ? */
119static int is_ack_nack(enum abis_nm_msgtype mt)
120{
121 return !is_in_arr(mt, no_ack_nack, ARRAY_SIZE(no_ack_nack));
122}
Holger Freytherca362a62009-01-04 21:05:01 +0000123#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000124
125/* is this msgtype a report ? */
126static int is_report(enum abis_nm_msgtype mt)
127{
Harald Welte8470bf22008-12-25 23:28:35 +0000128 return is_in_arr(mt, reports, ARRAY_SIZE(reports));
Harald Welte52b1f982008-12-23 20:25:15 +0000129}
130
131#define MT_ACK(x) (x+1)
132#define MT_NACK(x) (x+2)
133
134static void fill_om_hdr(struct abis_om_hdr *oh, u_int8_t len)
135{
136 oh->mdisc = ABIS_OM_MDISC_FOM;
137 oh->placement = ABIS_OM_PLACEMENT_ONLY;
138 oh->sequence = 0;
139 oh->length = len;
140}
141
142static void fill_om_fom_hdr(struct abis_om_hdr *oh, u_int8_t len,
143 u_int8_t msg_type, u_int8_t obj_class,
144 u_int8_t bts_nr, u_int8_t trx_nr, u_int8_t ts_nr)
145{
146 struct abis_om_fom_hdr *foh =
147 (struct abis_om_fom_hdr *) oh->data;
148
Harald Welte702d8702008-12-26 20:25:35 +0000149 fill_om_hdr(oh, len+sizeof(*foh));
Harald Welte52b1f982008-12-23 20:25:15 +0000150 foh->msg_type = msg_type;
151 foh->obj_class = obj_class;
152 foh->obj_inst.bts_nr = bts_nr;
153 foh->obj_inst.trx_nr = trx_nr;
154 foh->obj_inst.ts_nr = ts_nr;
155}
156
Harald Welte8470bf22008-12-25 23:28:35 +0000157static struct msgb *nm_msgb_alloc(void)
158{
159 return msgb_alloc_headroom(OM_ALLOC_SIZE, OM_HEADROOM_SIZE);
160}
161
Harald Welte52b1f982008-12-23 20:25:15 +0000162/* Send a OML NM Message from BSC to BTS */
163int abis_nm_sendmsg(struct gsm_bts *bts, struct msgb *msg)
164{
Harald Weltead384642008-12-26 10:20:07 +0000165 return _abis_nm_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000166}
167
Harald Welte4724f992009-01-18 18:01:49 +0000168static int abis_nm_rcvmsg_sw(struct msgb *mb);
169
Harald Welte52b1f982008-12-23 20:25:15 +0000170/* Receive a OML NM Message from BTS */
Harald Welte8470bf22008-12-25 23:28:35 +0000171static int abis_nm_rcvmsg_fom(struct msgb *mb)
Harald Welte52b1f982008-12-23 20:25:15 +0000172{
173 struct abis_om_fom_hdr *foh = msgb_l3(mb);
174 u_int8_t mt = foh->msg_type;
175
176 /* check for unsolicited message */
177 if (is_report(mt)) {
Harald Weltead384642008-12-26 10:20:07 +0000178 DEBUGP(DNM, "reporting NM MT 0x%02x\n", mt);
179 //nmh->cfg->report_cb(mb, foh);
Harald Welte52b1f982008-12-23 20:25:15 +0000180 return 0;
181 }
182
Harald Welte4724f992009-01-18 18:01:49 +0000183 if (is_in_arr(mt, sw_load_msgs, ARRAY_SIZE(sw_load_msgs)))
184 return abis_nm_rcvmsg_sw(mb);
185
Harald Weltead384642008-12-26 10:20:07 +0000186#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000187 /* check if last message is to be acked */
188 if (is_ack_nack(nmh->last_msgtype)) {
189 if (mt == MT_ACK(nmh->last_msgtype)) {
190 fprintf(stderr, "received ACK (0x%x)\n",
191 foh->msg_type);
192 /* we got our ACK, continue sending the next msg */
193 } else if (mt == MT_NACK(nmh->last_msgtype)) {
194 /* we got a NACK, signal this to the caller */
195 fprintf(stderr, "received NACK (0x%x)\n",
196 foh->msg_type);
197 /* FIXME: somehow signal this to the caller */
198 } else {
199 /* really strange things happen */
200 return -EINVAL;
201 }
202 }
Harald Weltead384642008-12-26 10:20:07 +0000203#endif
204
205 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000206}
207
208/* High-Level API */
209/* Entry-point where L2 OML from BTS enters the NM code */
Harald Welte8470bf22008-12-25 23:28:35 +0000210int abis_nm_rcvmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000211{
212 int rc;
213 struct abis_om_hdr *oh = msgb_l2(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000214
215 /* Various consistency checks */
216 if (oh->placement != ABIS_OM_PLACEMENT_ONLY) {
217 fprintf(stderr, "ABIS OML placement 0x%x not supported\n",
218 oh->placement);
219 return -EINVAL;
220 }
221 if (oh->sequence != 0) {
222 fprintf(stderr, "ABIS OML sequence 0x%x != 0x00\n",
223 oh->sequence);
224 return -EINVAL;
225 }
Harald Welte702d8702008-12-26 20:25:35 +0000226#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000227 unsigned int l2_len = msg->tail - (u_int8_t *)msgb_l2(msg);
228 unsigned int hlen = sizeof(*oh) + sizeof(struct abis_om_fom_hdr);
Harald Welte702d8702008-12-26 20:25:35 +0000229 if (oh->length + hlen > l2_len) {
Harald Welte52b1f982008-12-23 20:25:15 +0000230 fprintf(stderr, "ABIS OML truncated message (%u > %u)\n",
231 oh->length + sizeof(*oh), l2_len);
232 return -EINVAL;
233 }
Harald Welte702d8702008-12-26 20:25:35 +0000234 if (oh->length + hlen < l2_len)
235 fprintf(stderr, "ABIS OML message with extra trailer?!? (oh->len=%d, sizeof_oh=%d l2_len=%d\n", oh->length, sizeof(*oh), l2_len);
236#endif
Harald Weltead384642008-12-26 10:20:07 +0000237 msg->l3h = (unsigned char *)oh + sizeof(*oh);
Harald Welte52b1f982008-12-23 20:25:15 +0000238
239 switch (oh->mdisc) {
240 case ABIS_OM_MDISC_FOM:
Harald Welte8470bf22008-12-25 23:28:35 +0000241 rc = abis_nm_rcvmsg_fom(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000242 break;
243 case ABIS_OM_MDISC_MMI:
244 case ABIS_OM_MDISC_TRAU:
245 case ABIS_OM_MDISC_MANUF:
246 default:
247 fprintf(stderr, "unknown ABIS OML message discriminator 0x%x\n",
248 oh->mdisc);
249 return -EINVAL;
250 }
251
Harald Weltead384642008-12-26 10:20:07 +0000252 msgb_free(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000253 return rc;
254}
255
256#if 0
257/* initialized all resources */
258struct abis_nm_h *abis_nm_init(struct abis_nm_cfg *cfg)
259{
260 struct abis_nm_h *nmh;
261
262 nmh = malloc(sizeof(*nmh));
263 if (!nmh)
264 return NULL;
265
266 nmh->cfg = cfg;
267
268 return nmh;
269}
270
271/* free all resources */
272void abis_nm_fini(struct abis_nm_h *nmh)
273{
274 free(nmh);
275}
276#endif
277
278/* Here we are trying to define a high-level API that can be used by
279 * the actual BSC implementation. However, the architecture is currently
280 * still under design. Ideally the calls to this API would be synchronous,
281 * while the underlying stack behind the APi runs in a traditional select
282 * based state machine.
283 */
284
Harald Welte4724f992009-01-18 18:01:49 +0000285/* 6.2 Software Load: */
286enum sw_state {
287 SW_STATE_NONE,
288 SW_STATE_WAIT_INITACK,
289 SW_STATE_WAIT_SEGACK,
290 SW_STATE_WAIT_ENDACK,
291 SW_STATE_WAIT_ACTACK,
292 SW_STATE_ERROR,
293};
Harald Welte52b1f982008-12-23 20:25:15 +0000294
Harald Welte52b1f982008-12-23 20:25:15 +0000295struct abis_nm_sw {
Harald Welte4724f992009-01-18 18:01:49 +0000296 struct gsm_bts *bts;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000297 gsm_cbfn *cbfn;
298 void *cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000299 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000300
Harald Welte52b1f982008-12-23 20:25:15 +0000301 /* this will become part of the SW LOAD INITIATE */
302 u_int8_t obj_class;
303 u_int8_t obj_instance[3];
Harald Welte4724f992009-01-18 18:01:49 +0000304
305 u_int8_t file_id[255];
306 u_int8_t file_id_len;
307
308 u_int8_t file_version[255];
309 u_int8_t file_version_len;
310
311 u_int8_t window_size;
312 u_int8_t seg_in_window;
313
314 int fd;
315 FILE *stream;
316 enum sw_state state;
Harald Welte1602ade2009-01-29 21:12:39 +0000317 int last_seg;
Harald Welte52b1f982008-12-23 20:25:15 +0000318};
319
Harald Welte4724f992009-01-18 18:01:49 +0000320static struct abis_nm_sw g_sw;
321
322/* 6.2.1 / 8.3.1: Load Data Initiate */
323static int sw_load_init(struct abis_nm_sw *sw)
Harald Welte52b1f982008-12-23 20:25:15 +0000324{
Harald Welte4724f992009-01-18 18:01:49 +0000325 struct abis_om_hdr *oh;
326 struct msgb *msg = nm_msgb_alloc();
327 u_int8_t len = 3*2 + sw->file_id_len + sw->file_version_len;
328
329 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
330 fill_om_fom_hdr(oh, len, NM_MT_LOAD_INIT, sw->obj_class,
331 sw->obj_instance[0], sw->obj_instance[1],
332 sw->obj_instance[2]);
333
334 /* FIXME: this is BS11 specific format */
335 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
336 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
337 sw->file_version);
338 msgb_tv_put(msg, NM_ATT_WINDOW_SIZE, sw->window_size);
339
340 return abis_nm_sendmsg(sw->bts, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000341}
342
Harald Welte1602ade2009-01-29 21:12:39 +0000343static int is_last_line(FILE *stream)
344{
345 char next_seg_buf[256];
346 long pos;
347
348 /* check if we're sending the last line */
349 pos = ftell(stream);
350 if (!fgets(next_seg_buf, sizeof(next_seg_buf)-2, stream)) {
351 fseek(stream, pos, SEEK_SET);
352 return 1;
353 }
354
355 fseek(stream, pos, SEEK_SET);
356 return 0;
357}
358
Harald Welte4724f992009-01-18 18:01:49 +0000359/* 6.2.2 / 8.3.2 Load Data Segment */
360static int sw_load_segment(struct abis_nm_sw *sw)
361{
362 struct abis_om_hdr *oh;
363 struct msgb *msg = nm_msgb_alloc();
364 char seg_buf[256];
365 char *line_buf = seg_buf+2;
Harald Welte3b8ba212009-01-29 12:27:58 +0000366 unsigned char *tlv;
Harald Welte4724f992009-01-18 18:01:49 +0000367 u_int8_t len;
Harald Welte4724f992009-01-18 18:01:49 +0000368
369 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte3b8ba212009-01-29 12:27:58 +0000370
371 switch (sw->bts->type) {
372 case GSM_BTS_TYPE_BS11:
373 if (fgets(line_buf, sizeof(seg_buf)-2, sw->stream) == NULL) {
374 perror("fgets reading segment");
375 return -EINVAL;
376 }
377 seg_buf[0] = 0x00;
Harald Welte1602ade2009-01-29 21:12:39 +0000378
379 /* check if we're sending the last line */
380 sw->last_seg = is_last_line(sw->stream);
381 if (sw->last_seg)
382 seg_buf[1] = 0;
383 else
384 seg_buf[1] = 1 + sw->seg_in_window++;
Harald Welte3b8ba212009-01-29 12:27:58 +0000385
386 len = strlen(line_buf) + 2;
387 tlv = msgb_put(msg, TLV_GROSS_LEN(len));
388 tlv_put(tlv, NM_ATT_BS11_FILE_DATA, len, (u_int8_t *)seg_buf);
389 /* BS11 wants CR + LF in excess of the TLV length !?! */
390 tlv[1] -= 2;
391
392 /* we only now know the exact length for the OM hdr */
393 len = strlen(line_buf)+2;
394 break;
395 default:
396 /* FIXME: Other BTS types */
397 return -1;
Harald Welte4724f992009-01-18 18:01:49 +0000398 }
Harald Welte4724f992009-01-18 18:01:49 +0000399
Harald Welte4724f992009-01-18 18:01:49 +0000400 fill_om_fom_hdr(oh, len, NM_MT_LOAD_SEG, sw->obj_class,
401 sw->obj_instance[0], sw->obj_instance[1],
402 sw->obj_instance[2]);
403
404 return abis_nm_sendmsg(sw->bts, msg);
405}
406
407/* 6.2.4 / 8.3.4 Load Data End */
408static int sw_load_end(struct abis_nm_sw *sw)
409{
410 struct abis_om_hdr *oh;
411 struct msgb *msg = nm_msgb_alloc();
412 u_int8_t len = 2*2 + sw->file_id_len + sw->file_version_len;
413
414 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
415 fill_om_fom_hdr(oh, len, NM_MT_LOAD_END, sw->obj_class,
416 sw->obj_instance[0], sw->obj_instance[1],
417 sw->obj_instance[2]);
418
419 /* FIXME: this is BS11 specific format */
420 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
421 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
422 sw->file_version);
423
424 return abis_nm_sendmsg(sw->bts, msg);
425}
Harald Welte5e4d1b32009-02-01 13:36:56 +0000426
Harald Welte52b1f982008-12-23 20:25:15 +0000427/* Activate the specified software into the BTS */
Harald Welte4724f992009-01-18 18:01:49 +0000428static int sw_activate(struct abis_nm_sw *sw)
Harald Welte52b1f982008-12-23 20:25:15 +0000429{
Harald Welte4724f992009-01-18 18:01:49 +0000430 struct abis_om_hdr *oh;
431 struct msgb *msg = nm_msgb_alloc();
432 u_int8_t len = 2*2 + sw->file_id_len + sw->file_version_len;
Harald Welte52b1f982008-12-23 20:25:15 +0000433
Harald Welte4724f992009-01-18 18:01:49 +0000434 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
435 fill_om_fom_hdr(oh, len, NM_MT_ACTIVATE_SW, sw->obj_class,
436 sw->obj_instance[0], sw->obj_instance[1],
437 sw->obj_instance[2]);
438
439 /* FIXME: this is BS11 specific format */
440 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
441 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
442 sw->file_version);
443
444 return abis_nm_sendmsg(sw->bts, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000445}
Harald Welte4724f992009-01-18 18:01:49 +0000446
447static int sw_open_file(struct abis_nm_sw *sw, const char *fname)
448{
449 char file_id[12+1];
450 char file_version[80+1];
451 int rc;
452
453 sw->fd = open(fname, O_RDONLY);
454 if (sw->fd < 0)
455 return sw->fd;
456
457 switch (sw->bts->type) {
458 case GSM_BTS_TYPE_BS11:
459 sw->stream = fdopen(sw->fd, "r");
460 if (!sw->stream) {
461 perror("fdopen");
462 return -1;
463 }
464 /* read first line and parse file ID and VERSION */
Harald Welte3b8ba212009-01-29 12:27:58 +0000465 rc = fscanf(sw->stream, "@(#)%12s:%80s\r\n",
Harald Welte4724f992009-01-18 18:01:49 +0000466 file_id, file_version);
467 if (rc != 2) {
468 perror("parsing header line of software file");
469 return -1;
470 }
471 strcpy((char *)sw->file_id, file_id);
472 sw->file_id_len = strlen(file_id);
473 strcpy((char *)sw->file_version, file_version);
474 sw->file_version_len = strlen(file_version);
475 /* rewind to start of file */
Harald Welte3b8ba212009-01-29 12:27:58 +0000476 rewind(sw->stream);
Harald Welte4724f992009-01-18 18:01:49 +0000477 break;
478 default:
479 /* We don't know how to treat them yet */
480 close(sw->fd);
481 return -EINVAL;
482 }
483
484 return 0;
485}
486
487static void sw_close_file(struct abis_nm_sw *sw)
488{
489 switch (sw->bts->type) {
490 case GSM_BTS_TYPE_BS11:
491 fclose(sw->stream);
492 break;
493 default:
494 close(sw->fd);
495 break;
496 }
497}
498
499/* Fill the window */
500static int sw_fill_window(struct abis_nm_sw *sw)
501{
502 int rc;
503
504 while (sw->seg_in_window < sw->window_size) {
505 rc = sw_load_segment(sw);
506 if (rc < 0)
507 return rc;
Harald Welte1602ade2009-01-29 21:12:39 +0000508 if (sw->last_seg)
509 break;
Harald Welte4724f992009-01-18 18:01:49 +0000510 }
511 return 0;
512}
513
514/* callback function from abis_nm_rcvmsg() handler */
515static int abis_nm_rcvmsg_sw(struct msgb *mb)
516{
517 struct abis_om_fom_hdr *foh = msgb_l3(mb);
518 int rc = -1;
519 struct abis_nm_sw *sw = &g_sw;
520 enum sw_state old_state = sw->state;
521
Harald Welte3ffd1372009-02-01 22:15:49 +0000522 //DEBUGP(DNM, "state %u, NM MT 0x%02x\n", sw->state, foh->msg_type);
Harald Welte4724f992009-01-18 18:01:49 +0000523
524 switch (sw->state) {
525 case SW_STATE_WAIT_INITACK:
526 switch (foh->msg_type) {
527 case NM_MT_LOAD_INIT_ACK:
528 /* fill window with segments */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000529 if (sw->cbfn)
530 sw->cbfn(GSM_HOOK_NM_SWLOAD,
531 NM_MT_LOAD_INIT_ACK, mb,
532 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000533 rc = sw_fill_window(sw);
534 sw->state = SW_STATE_WAIT_SEGACK;
535 break;
536 case NM_MT_LOAD_INIT_NACK:
Harald Welte3ffd1372009-02-01 22:15:49 +0000537 if (sw->forced) {
538 DEBUGP(DNM, "FORCED: Ignoring Software Load "
539 "Init NACK\n");
540 if (sw->cbfn)
541 sw->cbfn(GSM_HOOK_NM_SWLOAD,
542 NM_MT_LOAD_INIT_ACK, mb,
543 sw->cb_data, NULL);
544 rc = sw_fill_window(sw);
545 sw->state = SW_STATE_WAIT_SEGACK;
546 } else {
547 DEBUGP(DNM, "Software Load Init NACK\n");
548 if (sw->cbfn)
549 sw->cbfn(GSM_HOOK_NM_SWLOAD,
550 NM_MT_LOAD_INIT_NACK, mb,
551 sw->cb_data, NULL);
552 sw->state = SW_STATE_ERROR;
553 }
Harald Welte4724f992009-01-18 18:01:49 +0000554 break;
555 }
556 break;
557 case SW_STATE_WAIT_SEGACK:
558 switch (foh->msg_type) {
559 case NM_MT_LOAD_SEG_ACK:
Harald Welte3ffd1372009-02-01 22:15:49 +0000560 if (sw->cbfn)
561 sw->cbfn(GSM_HOOK_NM_SWLOAD,
562 NM_MT_LOAD_SEG_ACK, mb,
563 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000564 sw->seg_in_window = 0;
Harald Welte1602ade2009-01-29 21:12:39 +0000565 if (!sw->last_seg) {
566 /* fill window with more segments */
567 rc = sw_fill_window(sw);
568 sw->state = SW_STATE_WAIT_SEGACK;
569 } else {
570 /* end the transfer */
571 sw->state = SW_STATE_WAIT_ENDACK;
572 rc = sw_load_end(sw);
573 }
Harald Welte4724f992009-01-18 18:01:49 +0000574 break;
575 }
576 break;
577 case SW_STATE_WAIT_ENDACK:
578 switch (foh->msg_type) {
579 case NM_MT_LOAD_END_ACK:
580 sw_close_file(sw);
Harald Welte5e4d1b32009-02-01 13:36:56 +0000581 DEBUGP(DNM, "Software Load End (BTS %u)\n",
582 sw->bts->nr);
583 sw->state = SW_STATE_NONE;
584 if (sw->cbfn)
585 sw->cbfn(GSM_HOOK_NM_SWLOAD,
586 NM_MT_LOAD_END_ACK, mb,
587 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000588 break;
589 case NM_MT_LOAD_END_NACK:
Harald Welte1602ade2009-01-29 21:12:39 +0000590 DEBUGP(DNM, "Software Load End NACK\n");
Harald Welte4724f992009-01-18 18:01:49 +0000591 sw->state = SW_STATE_ERROR;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000592 if (sw->cbfn)
593 sw->cbfn(GSM_HOOK_NM_SWLOAD,
594 NM_MT_LOAD_END_NACK, mb,
595 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000596 break;
597 }
598 case SW_STATE_WAIT_ACTACK:
599 switch (foh->msg_type) {
600 case NM_MT_ACTIVATE_SW_ACK:
601 /* we're done */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000602 DEBUGP(DNM, "Activate Software DONE!\n");
Harald Welte4724f992009-01-18 18:01:49 +0000603 sw->state = SW_STATE_NONE;
604 rc = 0;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000605 if (sw->cbfn)
606 sw->cbfn(GSM_HOOK_NM_SWLOAD,
607 NM_MT_ACTIVATE_SW_ACK, mb,
608 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000609 break;
610 case NM_MT_ACTIVATE_SW_NACK:
Harald Welte1602ade2009-01-29 21:12:39 +0000611 DEBUGP(DNM, "Activate Software NACK\n");
Harald Welte4724f992009-01-18 18:01:49 +0000612 sw->state = SW_STATE_ERROR;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000613 if (sw->cbfn)
614 sw->cbfn(GSM_HOOK_NM_SWLOAD,
615 NM_MT_ACTIVATE_SW_NACK, mb,
616 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000617 break;
618 }
619 case SW_STATE_NONE:
620 case SW_STATE_ERROR:
621 break;
622 }
623
624 if (rc)
625 fprintf(stderr, "unexpected NM MT 0x%02x in state %u -> %u\n",
626 foh->msg_type, old_state, sw->state);
627
628 return rc;
629}
630
631/* Load the specified software into the BTS */
632int abis_nm_software_load(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +0000633 u_int8_t win_size, int forced,
634 gsm_cbfn *cbfn, void *cb_data)
Harald Welte4724f992009-01-18 18:01:49 +0000635{
636 struct abis_nm_sw *sw = &g_sw;
637 int rc;
638
Harald Welte5e4d1b32009-02-01 13:36:56 +0000639 DEBUGP(DNM, "Software Load (BTS %u, File \"%s\")\n",
640 bts->nr, fname);
641
Harald Welte4724f992009-01-18 18:01:49 +0000642 if (sw->state != SW_STATE_NONE)
643 return -EBUSY;
644
645 sw->bts = bts;
646 sw->obj_class = NM_OC_SITE_MANAGER;
647 sw->obj_instance[0] = 0xff;
648 sw->obj_instance[1] = 0xff;
649 sw->obj_instance[2] = 0xff;
650 sw->window_size = win_size;
651 sw->state = SW_STATE_WAIT_INITACK;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000652 sw->cbfn = cbfn;
653 sw->cb_data = cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000654 sw->forced = forced;
Harald Welte4724f992009-01-18 18:01:49 +0000655
656 rc = sw_open_file(sw, fname);
657 if (rc < 0) {
658 sw->state = SW_STATE_NONE;
659 return rc;
660 }
661
662 return sw_load_init(sw);
663}
Harald Welte52b1f982008-12-23 20:25:15 +0000664
Harald Welte1602ade2009-01-29 21:12:39 +0000665int abis_nm_software_load_status(struct gsm_bts *bts)
666{
667 struct abis_nm_sw *sw = &g_sw;
668 struct stat st;
669 int rc, percent;
670
671 rc = fstat(sw->fd, &st);
672 if (rc < 0) {
673 perror("ERROR during stat");
674 return rc;
675 }
676
677 percent = (ftell(sw->stream) * 100) / st.st_size;
678 return percent;
679}
680
Harald Welte5e4d1b32009-02-01 13:36:56 +0000681/* Activate the specified software into the BTS */
682int abis_nm_software_activate(struct gsm_bts *bts, const char *fname,
683 gsm_cbfn *cbfn, void *cb_data)
684{
685 struct abis_nm_sw *sw = &g_sw;
686 int rc;
687
688 DEBUGP(DNM, "Activating Software (BTS %u, File \"%s\")\n",
689 bts->nr, fname);
690
691 if (sw->state != SW_STATE_NONE)
692 return -EBUSY;
693
694 sw->bts = bts;
695 sw->obj_class = NM_OC_SITE_MANAGER;
696 sw->obj_instance[0] = 0xff;
697 sw->obj_instance[1] = 0xff;
698 sw->obj_instance[2] = 0xff;
699 sw->state = SW_STATE_WAIT_ACTACK;
700 sw->cbfn = cbfn;
701 sw->cb_data = cb_data;
702
703 /* Open the file in order to fill some sw struct members */
704 rc = sw_open_file(sw, fname);
705 if (rc < 0) {
706 sw->state = SW_STATE_NONE;
707 return rc;
708 }
709 sw_close_file(sw);
710
711 return sw_activate(sw);
712}
713
Harald Welte8470bf22008-12-25 23:28:35 +0000714static void fill_nm_channel(struct abis_nm_channel *ch, u_int8_t bts_port,
Harald Welte52b1f982008-12-23 20:25:15 +0000715 u_int8_t ts_nr, u_int8_t subslot_nr)
716{
Harald Welteadaf08b2009-01-18 11:08:10 +0000717 ch->attrib = NM_ATT_ABIS_CHANNEL;
Harald Welte52b1f982008-12-23 20:25:15 +0000718 ch->bts_port = bts_port;
719 ch->timeslot = ts_nr;
720 ch->subslot = subslot_nr;
721}
722
723int abis_nm_establish_tei(struct gsm_bts *bts, u_int8_t trx_nr,
724 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot,
725 u_int8_t tei)
726{
727 struct abis_om_hdr *oh;
728 struct abis_nm_channel *ch;
Harald Welte702d8702008-12-26 20:25:35 +0000729 u_int8_t len = sizeof(*ch) + 2;
Harald Welte8470bf22008-12-25 23:28:35 +0000730 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000731
732 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
733 fill_om_fom_hdr(oh, len, NM_MT_ESTABLISH_TEI, NM_OC_RADIO_CARRIER,
734 bts->bts_nr, trx_nr, 0xff);
735
Harald Welte8470bf22008-12-25 23:28:35 +0000736 msgb_tv_put(msg, NM_ATT_TEI, tei);
Harald Welte52b1f982008-12-23 20:25:15 +0000737
738 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
739 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
740
741 return abis_nm_sendmsg(bts, msg);
742}
743
744/* connect signalling of one (BTS,TRX) to a particular timeslot on the E1 */
745int abis_nm_conn_terr_sign(struct gsm_bts_trx *trx,
746 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot)
747{
Harald Welte8470bf22008-12-25 23:28:35 +0000748 struct gsm_bts *bts = trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000749 struct abis_om_hdr *oh;
750 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000751 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000752
753 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000754 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_SIGN,
Harald Welte52b1f982008-12-23 20:25:15 +0000755 NM_OC_RADIO_CARRIER, bts->bts_nr, trx->nr, 0xff);
756
757 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
758 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
759
760 return abis_nm_sendmsg(bts, msg);
761}
762
763#if 0
764int abis_nm_disc_terr_sign(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
765 struct abis_nm_abis_channel *chan)
766{
767}
768#endif
769
770int abis_nm_conn_terr_traf(struct gsm_bts_trx_ts *ts,
771 u_int8_t e1_port, u_int8_t e1_timeslot,
772 u_int8_t e1_subslot)
773{
774 struct gsm_bts *bts = ts->trx->bts;
775 struct abis_om_hdr *oh;
776 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000777 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000778
779 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
780 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_TRAF,
781 NM_OC_BASEB_TRANSC, bts->bts_nr, ts->trx->nr, ts->nr);
782
783 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
784 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
785
786 return abis_nm_sendmsg(bts, msg);
787}
788
789#if 0
790int abis_nm_disc_terr_traf(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
791 struct abis_nm_abis_channel *chan,
792 u_int8_t subchan)
793{
794}
795#endif
796
797int abis_nm_set_channel_attr(struct gsm_bts_trx_ts *ts, u_int8_t chan_comb)
798{
799 struct gsm_bts *bts = ts->trx->bts;
800 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000801 u_int16_t arfcn = htons(ts->trx->arfcn);
Harald Welte52b1f982008-12-23 20:25:15 +0000802 u_int8_t zero = 0x00;
Harald Welte8470bf22008-12-25 23:28:35 +0000803 struct msgb *msg = nm_msgb_alloc();
Harald Welte702d8702008-12-26 20:25:35 +0000804 u_int8_t len = 4 + 2 + 2 + 2 + 2 +3;
Harald Welte52b1f982008-12-23 20:25:15 +0000805
806 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000807 fill_om_fom_hdr(oh, len, NM_MT_SET_CHAN_ATTR,
Harald Welte52b1f982008-12-23 20:25:15 +0000808 NM_OC_BASEB_TRANSC, bts->bts_nr,
809 ts->trx->nr, ts->nr);
810 /* FIXME: don't send ARFCN list, hopping sequence, mAIO, ...*/
811 msgb_tlv16_put(msg, NM_ATT_ARFCN_LIST, 1, &arfcn);
812 msgb_tv_put(msg, NM_ATT_CHAN_COMB, chan_comb);
813 msgb_tv_put(msg, NM_ATT_HSN, 0x00);
814 msgb_tv_put(msg, NM_ATT_MAIO, 0x00);
815 msgb_tv_put(msg, NM_ATT_TSC, 0x07); /* training sequence */
816 msgb_tlv_put(msg, 0x59, 1, &zero);
817
818 return abis_nm_sendmsg(bts, msg);
819}
820
Harald Welte8470bf22008-12-25 23:28:35 +0000821int abis_nm_raw_msg(struct gsm_bts *bts, int len, u_int8_t *rawmsg)
Harald Welte52b1f982008-12-23 20:25:15 +0000822{
Harald Welte8470bf22008-12-25 23:28:35 +0000823 struct msgb *msg = nm_msgb_alloc();
824 struct abis_om_hdr *oh;
Harald Welte52b1f982008-12-23 20:25:15 +0000825 u_int8_t *data;
826
827 oh = (struct abis_om_hdr *) msgb_put(msg, sizeof(*oh));
828 fill_om_hdr(oh, len);
829 data = msgb_put(msg, len);
Harald Weltead384642008-12-26 10:20:07 +0000830 memcpy(data, rawmsg, len);
Harald Welte52b1f982008-12-23 20:25:15 +0000831
832 return abis_nm_sendmsg(bts, msg);
833}
834
835/* Siemens specific commands */
836static int __simple_cmd(struct gsm_bts *bts, u_int8_t msg_type)
837{
838 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000839 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000840
841 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000842 fill_om_fom_hdr(oh, 0, msg_type, NM_OC_SITE_MANAGER,
Harald Welte52b1f982008-12-23 20:25:15 +0000843 0xff, 0xff, 0xff);
844
845 return abis_nm_sendmsg(bts, msg);
846}
847
848int abis_nm_event_reports(struct gsm_bts *bts, int on)
849{
850 if (on == 0)
Harald Welte227d4072009-01-03 08:16:25 +0000851 return __simple_cmd(bts, NM_MT_STOP_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000852 else
Harald Welte227d4072009-01-03 08:16:25 +0000853 return __simple_cmd(bts, NM_MT_REST_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000854}
855
Harald Welte3ffd1372009-02-01 22:15:49 +0000856
Harald Welte47d88ae2009-01-04 12:02:08 +0000857/* Siemens (or BS-11) specific commands */
858
Harald Welte3ffd1372009-02-01 22:15:49 +0000859int abis_nm_bs11_bsc_disconnect(struct gsm_bts *bts, int reconnect)
860{
861 if (reconnect == 0)
862 return __simple_cmd(bts, NM_MT_BS11_DISCONNECT);
863 else
864 return __simple_cmd(bts, NM_MT_BS11_RECONNECT);
865}
866
Harald Welte268bb402009-02-01 19:11:56 +0000867struct bs11_date_time {
868 u_int16_t year;
869 u_int8_t month;
870 u_int8_t day;
871 u_int8_t hour;
872 u_int8_t min;
873 u_int8_t sec;
874} __attribute__((packed));
875
876
877void get_bs11_date_time(struct bs11_date_time *aet)
878{
879 time_t t;
880 struct tm *tm;
881
882 t = time(NULL);
883 tm = localtime(&t);
884 aet->sec = tm->tm_sec;
885 aet->min = tm->tm_min;
886 aet->hour = tm->tm_hour;
887 aet->day = tm->tm_mday;
888 aet->month = tm->tm_mon;
889 aet->year = htons(1900 + tm->tm_year);
890}
891
Harald Welte05188ee2009-01-18 11:39:08 +0000892int abis_nm_bs11_reset_resource(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000893{
Harald Welte4668fda2009-01-03 08:19:29 +0000894 return __simple_cmd(bts, NM_MT_BS11_RESET_RESOURCE);
Harald Welte52b1f982008-12-23 20:25:15 +0000895}
896
Harald Welte05188ee2009-01-18 11:39:08 +0000897int abis_nm_bs11_db_transmission(struct gsm_bts *bts, int begin)
Harald Welte52b1f982008-12-23 20:25:15 +0000898{
899 if (begin)
Harald Welte4668fda2009-01-03 08:19:29 +0000900 return __simple_cmd(bts, NM_MT_BS11_BEGIN_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000901 else
Harald Welte4668fda2009-01-03 08:19:29 +0000902 return __simple_cmd(bts, NM_MT_BS11_END_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000903}
Harald Welte47d88ae2009-01-04 12:02:08 +0000904
Harald Welte05188ee2009-01-18 11:39:08 +0000905int abis_nm_bs11_create_object(struct gsm_bts *bts,
Harald Welte1bc09062009-01-18 14:17:52 +0000906 enum abis_bs11_objtype type, u_int8_t idx,
907 u_int8_t attr_len, const u_int8_t *attr)
Harald Welte47d88ae2009-01-04 12:02:08 +0000908{
909 struct abis_om_hdr *oh;
910 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000911 u_int8_t *cur;
Harald Welte47d88ae2009-01-04 12:02:08 +0000912
913 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000914 fill_om_fom_hdr(oh, attr_len, NM_MT_BS11_CREATE_OBJ,
Harald Welte268bb402009-02-01 19:11:56 +0000915 NM_OC_BS11, type, 0, idx);
Harald Welte1bc09062009-01-18 14:17:52 +0000916 cur = msgb_put(msg, attr_len);
917 memcpy(cur, attr, attr_len);
Harald Welte47d88ae2009-01-04 12:02:08 +0000918
919 return abis_nm_sendmsg(bts, msg);
920}
921
Harald Welte05188ee2009-01-18 11:39:08 +0000922int abis_nm_bs11_create_envaBTSE(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000923{
924 struct abis_om_hdr *oh;
925 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000926 u_int8_t zero = 0x00;
Harald Welte47d88ae2009-01-04 12:02:08 +0000927
928 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000929 fill_om_fom_hdr(oh, 3, NM_MT_BS11_CREATE_OBJ,
Harald Welte1bc09062009-01-18 14:17:52 +0000930 NM_OC_BS11_ENVABTSE, 0, idx, 0xff);
931 msgb_tlv_put(msg, 0x99, 1, &zero);
Harald Welte47d88ae2009-01-04 12:02:08 +0000932
933 return abis_nm_sendmsg(bts, msg);
934}
935
Harald Welte05188ee2009-01-18 11:39:08 +0000936int abis_nm_bs11_create_bport(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000937{
938 struct abis_om_hdr *oh;
939 struct msgb *msg = nm_msgb_alloc();
940
941 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
942 fill_om_fom_hdr(oh, 0, NM_MT_BS11_CREATE_OBJ, NM_OC_BS11_BPORT,
943 idx, 0, 0);
944
945 return abis_nm_sendmsg(bts, msg);
946}
Harald Welte05188ee2009-01-18 11:39:08 +0000947
948int abis_nm_bs11_set_oml_tei(struct gsm_bts *bts, u_int8_t tei)
949{
950 struct abis_om_hdr *oh;
951 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +0000952
953 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000954 fill_om_fom_hdr(oh, 2, NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
Harald Welte05188ee2009-01-18 11:39:08 +0000955 0xff, 0xff, 0xff);
956 msgb_tv_put(msg, NM_ATT_TEI, tei);
957
958 return abis_nm_sendmsg(bts, msg);
959}
960
961/* like abis_nm_conn_terr_traf */
962int abis_nm_bs11_conn_oml(struct gsm_bts *bts, u_int8_t e1_port,
963 u_int8_t e1_timeslot, u_int8_t e1_subslot)
964{
965 struct abis_om_hdr *oh;
966 struct abis_nm_channel *ch;
967 struct msgb *msg = nm_msgb_alloc();
968
969 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte05188ee2009-01-18 11:39:08 +0000970 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_BS11_SET_ATTR,
971 NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff);
972
973 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
974 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
975
976 return abis_nm_sendmsg(bts, msg);
977}
978
979int abis_nm_bs11_set_trx_power(struct gsm_bts_trx *trx, u_int8_t level)
980{
981 struct abis_om_hdr *oh;
982 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +0000983
984 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000985 fill_om_fom_hdr(oh, 3, NM_MT_BS11_SET_ATTR,
Harald Welte05188ee2009-01-18 11:39:08 +0000986 NM_OC_BS11, BS11_OBJ_PA, 0x00, trx->nr);
987 msgb_tlv_put(msg, NM_ATT_BS11_TXPWR, 1, &level);
988
989 return abis_nm_sendmsg(trx->bts, msg);
990}
991
Harald Welte268bb402009-02-01 19:11:56 +0000992//static const u_int8_t bs11_logon_c7[] = { 0x07, 0xd9, 0x01, 0x11, 0x0d, 0x10, 0x20 };
Harald Weltebb151312009-01-28 20:42:07 +0000993static const u_int8_t bs11_logon_c8[] = { 0x02 };
Harald Welte05188ee2009-01-18 11:39:08 +0000994static const u_int8_t bs11_logon_c9[] = "FACTORY";
995
Harald Welte1bc09062009-01-18 14:17:52 +0000996int abis_nm_bs11_factory_logon(struct gsm_bts *bts, int on)
Harald Welte05188ee2009-01-18 11:39:08 +0000997{
998 struct abis_om_hdr *oh;
999 struct msgb *msg = nm_msgb_alloc();
Harald Welte268bb402009-02-01 19:11:56 +00001000 struct bs11_date_time bdt;
1001
1002 get_bs11_date_time(&bdt);
Harald Welte05188ee2009-01-18 11:39:08 +00001003
1004 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte1bc09062009-01-18 14:17:52 +00001005 if (on) {
Harald Welte268bb402009-02-01 19:11:56 +00001006 u_int8_t len = 3*2 + sizeof(bdt)
Harald Welte6f676a32009-01-18 14:27:48 +00001007 + sizeof(bs11_logon_c8) + sizeof(bs11_logon_c9);
Harald Welte043d04a2009-01-29 23:15:30 +00001008 fill_om_fom_hdr(oh, len, NM_MT_BS11_LMT_LOGON,
Harald Welte1bc09062009-01-18 14:17:52 +00001009 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
Harald Welte043d04a2009-01-29 23:15:30 +00001010 msgb_tlv_put(msg, NM_ATT_BS11_LMT_LOGIN_TIME,
Harald Welte268bb402009-02-01 19:11:56 +00001011 sizeof(bdt), &bdt);
Harald Welte043d04a2009-01-29 23:15:30 +00001012 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_ACC_LEV,
1013 sizeof(bs11_logon_c8), bs11_logon_c8);
1014 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_NAME,
1015 sizeof(bs11_logon_c9), bs11_logon_c9);
Harald Welte1bc09062009-01-18 14:17:52 +00001016 } else {
Harald Welte5e4d1b32009-02-01 13:36:56 +00001017 fill_om_fom_hdr(oh, 0, NM_MT_BS11_LMT_LOGOFF,
Harald Welte1bc09062009-01-18 14:17:52 +00001018 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
1019 }
Harald Welte05188ee2009-01-18 11:39:08 +00001020
1021 return abis_nm_sendmsg(bts, msg);
1022}
Harald Welte1bc09062009-01-18 14:17:52 +00001023
1024int abis_nm_bs11_set_trx1_pw(struct gsm_bts *bts, const char *password)
1025{
1026 struct abis_om_hdr *oh;
1027 struct msgb *msg;
1028
1029 if (strlen(password) != 10)
1030 return -EINVAL;
1031
1032 msg = nm_msgb_alloc();
1033 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001034 fill_om_fom_hdr(oh, 2+strlen(password), NM_MT_BS11_SET_ATTR,
Harald Welte1bc09062009-01-18 14:17:52 +00001035 NM_OC_BS11, BS11_OBJ_TRX1, 0x00, 0x00);
1036 msgb_tlv_put(msg, NM_ATT_BS11_PASSWORD, 10, (const u_int8_t *)password);
1037
1038 return abis_nm_sendmsg(bts, msg);
1039}
1040
1041int abis_nm_bs11_get_state(struct gsm_bts *bts)
1042{
1043 return __simple_cmd(bts, NM_MT_BS11_GET_STATE);
1044}
Harald Welte5e4d1b32009-02-01 13:36:56 +00001045
1046/* BS11 SWL */
1047
1048struct abis_nm_bs11_sw {
1049 struct gsm_bts *bts;
1050 char swl_fname[PATH_MAX];
1051 u_int8_t win_size;
Harald Welte3ffd1372009-02-01 22:15:49 +00001052 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001053 struct llist_head file_list;
1054 gsm_cbfn *user_cb; /* specified by the user */
1055};
1056static struct abis_nm_bs11_sw _g_bs11_sw, *g_bs11_sw = &_g_bs11_sw;
1057
1058struct file_list_entry {
1059 struct llist_head list;
1060 char fname[PATH_MAX];
1061};
1062
1063struct file_list_entry *fl_dequeue(struct llist_head *queue)
1064{
1065 struct llist_head *lh;
1066
1067 if (llist_empty(queue))
1068 return NULL;
1069
1070 lh = queue->next;
1071 llist_del(lh);
1072
1073 return llist_entry(lh, struct file_list_entry, list);
1074}
1075
1076static int bs11_read_swl_file(struct abis_nm_bs11_sw *bs11_sw)
1077{
1078 char linebuf[255];
1079 struct llist_head *lh, *lh2;
1080 FILE *swl;
1081 int rc = 0;
1082
1083 swl = fopen(bs11_sw->swl_fname, "r");
1084 if (!swl)
1085 return -ENODEV;
1086
1087 /* zero the stale file list, if any */
1088 llist_for_each_safe(lh, lh2, &bs11_sw->file_list) {
1089 llist_del(lh);
1090 free(lh);
1091 }
1092
1093 while (fgets(linebuf, sizeof(linebuf), swl)) {
1094 char file_id[12+1];
1095 char file_version[80+1];
1096 struct file_list_entry *fle;
1097 static char dir[PATH_MAX];
1098
1099 if (strlen(linebuf) < 4)
1100 continue;
Harald Welte3ffd1372009-02-01 22:15:49 +00001101
Harald Welte5e4d1b32009-02-01 13:36:56 +00001102 rc = sscanf(linebuf+4, "%12s:%80s\r\n", file_id, file_version);
1103 if (rc < 0) {
1104 perror("ERR parsing SWL file");
1105 rc = -EINVAL;
1106 goto out;
1107 }
1108 if (rc < 2)
1109 continue;
1110
1111 fle = malloc(sizeof(*fle));
1112 if (!fle) {
1113 rc = -ENOMEM;
1114 goto out;
1115 }
1116 memset(fle, 0, sizeof(*fle));
1117
1118 /* construct new filename */
1119 strncpy(dir, bs11_sw->swl_fname, sizeof(dir));
1120 strncat(fle->fname, dirname(dir), sizeof(fle->fname) - 1);
1121 strcat(fle->fname, "/");
1122 strncat(fle->fname, file_id, sizeof(fle->fname) - 1 -strlen(fle->fname));
Harald Welte5e4d1b32009-02-01 13:36:56 +00001123
1124 llist_add_tail(&fle->list, &bs11_sw->file_list);
1125 }
1126
1127out:
1128 fclose(swl);
1129 return rc;
1130}
1131
1132/* bs11 swload specific callback, passed to abis_nm core swload */
1133static int bs11_swload_cbfn(unsigned int hook, unsigned int event,
1134 struct msgb *msg, void *data, void *param)
1135{
1136 struct abis_nm_bs11_sw *bs11_sw = data;
1137 struct file_list_entry *fle;
1138 int rc = 0;
1139
Harald Welte5e4d1b32009-02-01 13:36:56 +00001140 switch (event) {
1141 case NM_MT_LOAD_END_ACK:
1142 fle = fl_dequeue(&bs11_sw->file_list);
1143 if (fle) {
1144 /* start download the next file of our file list */
1145 rc = abis_nm_software_load(bs11_sw->bts, fle->fname,
1146 bs11_sw->win_size,
Harald Welte3ffd1372009-02-01 22:15:49 +00001147 bs11_sw->forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001148 &bs11_swload_cbfn, bs11_sw);
1149 free(fle);
1150 } else {
1151 /* activate the SWL */
1152 rc = abis_nm_software_activate(bs11_sw->bts,
1153 bs11_sw->swl_fname,
1154 bs11_swload_cbfn,
1155 bs11_sw);
1156 }
1157 break;
Harald Welte3ffd1372009-02-01 22:15:49 +00001158 case NM_MT_LOAD_SEG_ACK:
Harald Welte5e4d1b32009-02-01 13:36:56 +00001159 case NM_MT_LOAD_END_NACK:
1160 case NM_MT_LOAD_INIT_ACK:
1161 case NM_MT_LOAD_INIT_NACK:
1162 case NM_MT_ACTIVATE_SW_NACK:
1163 case NM_MT_ACTIVATE_SW_ACK:
1164 default:
1165 /* fallthrough to the user callback */
1166 rc = bs11_sw->user_cb(hook, event, msg, NULL, NULL);
1167 break;
1168 }
1169
1170 return rc;
1171}
1172
1173/* Siemens provides a SWL file that is a mere listing of all the other
1174 * files that are part of a software release. We need to upload first
1175 * the list file, and then each file that is listed in the list file */
1176int abis_nm_bs11_load_swl(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +00001177 u_int8_t win_size, int forced, gsm_cbfn *cbfn)
Harald Welte5e4d1b32009-02-01 13:36:56 +00001178{
1179 struct abis_nm_bs11_sw *bs11_sw = g_bs11_sw;
1180 struct file_list_entry *fle;
1181 int rc = 0;
1182
1183 INIT_LLIST_HEAD(&bs11_sw->file_list);
1184 bs11_sw->bts = bts;
1185 bs11_sw->win_size = win_size;
1186 bs11_sw->user_cb = cbfn;
Harald Welte3ffd1372009-02-01 22:15:49 +00001187 bs11_sw->forced = forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001188
1189 strncpy(bs11_sw->swl_fname, fname, sizeof(bs11_sw->swl_fname));
1190 rc = bs11_read_swl_file(bs11_sw);
1191 if (rc < 0)
1192 return rc;
1193
1194 /* dequeue next item in file list */
1195 fle = fl_dequeue(&bs11_sw->file_list);
1196 if (!fle)
1197 return -EINVAL;
1198
1199 /* start download the next file of our file list */
Harald Welte3ffd1372009-02-01 22:15:49 +00001200 rc = abis_nm_software_load(bts, fle->fname, win_size, forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001201 bs11_swload_cbfn, bs11_sw);
1202 free(fle);
1203 return rc;
1204}
1205
1206static u_int8_t req_attr_btse[] = {
1207 NM_ATT_ADM_STATE, NM_ATT_BS11_LMT_LOGON_SESSION,
1208 NM_ATT_BS11_LMT_LOGIN_TIME, NM_ATT_BS11_LMT_USER_ACC_LEV,
1209 NM_ATT_BS11_LMT_USER_NAME,
1210
1211 0xaf, NM_ATT_BS11_RX_OFFSET, NM_ATT_BS11_VENDOR_NAME,
1212
1213 NM_ATT_BS11_SW_LOAD_INTENDED, NM_ATT_BS11_SW_LOAD_SAFETY,
1214
1215 NM_ATT_BS11_SW_LOAD_STORED };
1216
1217static u_int8_t req_attr_btsm[] = {
1218 NM_ATT_ABIS_CHANNEL, NM_ATT_TEI, NM_ATT_BS11_ABIS_EXT_TIME,
1219 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xce, NM_ATT_FILE_ID,
1220 NM_ATT_FILE_VERSION, NM_ATT_OPER_STATE, 0xe8, NM_ATT_BS11_ALL_TEST_CATG,
1221 NM_ATT_SW_DESCR, NM_ATT_GET_ARI };
1222
1223static u_int8_t req_attr[] = {
1224 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xa8, NM_ATT_OPER_STATE,
1225 0xd5, 0xa1, NM_ATT_BS11_ESN_FW_CODE_NO, NM_ATT_BS11_ESN_HW_CODE_NO,
1226 0x42, NM_ATT_BS11_ESN_PCB_SERIAL };
1227
1228int abis_nm_bs11_get_serno(struct gsm_bts *bts)
1229{
1230 struct abis_om_hdr *oh;
1231 struct msgb *msg = nm_msgb_alloc();
1232
1233 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1234 /* SiemensHW CCTRL object */
1235 fill_om_fom_hdr(oh, 2+sizeof(req_attr), NM_MT_GET_ATTR, NM_OC_BS11,
1236 0x03, 0x00, 0x00);
1237 msgb_tlv_put(msg, NM_ATT_LIST_REQ_ATTR, sizeof(req_attr), req_attr);
1238
1239 return abis_nm_sendmsg(bts, msg);
1240}
Harald Welte268bb402009-02-01 19:11:56 +00001241
1242int abis_nm_bs11_set_ext_time(struct gsm_bts *bts)
1243{
1244 struct abis_om_hdr *oh;
1245 struct msgb *msg = nm_msgb_alloc();
1246 struct bs11_date_time aet;
1247
1248 get_bs11_date_time(&aet);
1249 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1250 /* SiemensHW CCTRL object */
1251 fill_om_fom_hdr(oh, 2+sizeof(aet), NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
1252 0xff, 0xff, 0xff);
1253 msgb_tlv_put(msg, NM_ATT_BS11_ABIS_EXT_TIME, sizeof(aet), &aet);
1254
1255 return abis_nm_sendmsg(bts, msg);
1256}