blob: 79af71e6332eeab075cec47afc34d6960872b586 [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:
Harald Welte1602ade2009-01-29 21:12:39 +0000632 DEBUGP(DNM, "Software Load End NACK\n");
Harald Welte4724f992009-01-18 18:01:49 +0000633 sw->state = SW_STATE_ERROR;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000634 if (sw->cbfn)
635 sw->cbfn(GSM_HOOK_NM_SWLOAD,
636 NM_MT_LOAD_END_NACK, mb,
637 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000638 break;
639 }
640 case SW_STATE_WAIT_ACTACK:
641 switch (foh->msg_type) {
642 case NM_MT_ACTIVATE_SW_ACK:
643 /* we're done */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000644 DEBUGP(DNM, "Activate Software DONE!\n");
Harald Welte4724f992009-01-18 18:01:49 +0000645 sw->state = SW_STATE_NONE;
646 rc = 0;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000647 if (sw->cbfn)
648 sw->cbfn(GSM_HOOK_NM_SWLOAD,
649 NM_MT_ACTIVATE_SW_ACK, mb,
650 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000651 break;
652 case NM_MT_ACTIVATE_SW_NACK:
Harald Welte1602ade2009-01-29 21:12:39 +0000653 DEBUGP(DNM, "Activate Software NACK\n");
Harald Welte4724f992009-01-18 18:01:49 +0000654 sw->state = SW_STATE_ERROR;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000655 if (sw->cbfn)
656 sw->cbfn(GSM_HOOK_NM_SWLOAD,
657 NM_MT_ACTIVATE_SW_NACK, mb,
658 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000659 break;
660 }
661 case SW_STATE_NONE:
662 case SW_STATE_ERROR:
663 break;
664 }
665
666 if (rc)
667 fprintf(stderr, "unexpected NM MT 0x%02x in state %u -> %u\n",
668 foh->msg_type, old_state, sw->state);
669
670 return rc;
671}
672
673/* Load the specified software into the BTS */
674int abis_nm_software_load(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +0000675 u_int8_t win_size, int forced,
676 gsm_cbfn *cbfn, void *cb_data)
Harald Welte4724f992009-01-18 18:01:49 +0000677{
678 struct abis_nm_sw *sw = &g_sw;
679 int rc;
680
Harald Welte5e4d1b32009-02-01 13:36:56 +0000681 DEBUGP(DNM, "Software Load (BTS %u, File \"%s\")\n",
682 bts->nr, fname);
683
Harald Welte4724f992009-01-18 18:01:49 +0000684 if (sw->state != SW_STATE_NONE)
685 return -EBUSY;
686
687 sw->bts = bts;
688 sw->obj_class = NM_OC_SITE_MANAGER;
689 sw->obj_instance[0] = 0xff;
690 sw->obj_instance[1] = 0xff;
691 sw->obj_instance[2] = 0xff;
692 sw->window_size = win_size;
693 sw->state = SW_STATE_WAIT_INITACK;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000694 sw->cbfn = cbfn;
695 sw->cb_data = cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000696 sw->forced = forced;
Harald Welte4724f992009-01-18 18:01:49 +0000697
698 rc = sw_open_file(sw, fname);
699 if (rc < 0) {
700 sw->state = SW_STATE_NONE;
701 return rc;
702 }
703
704 return sw_load_init(sw);
705}
Harald Welte52b1f982008-12-23 20:25:15 +0000706
Harald Welte1602ade2009-01-29 21:12:39 +0000707int abis_nm_software_load_status(struct gsm_bts *bts)
708{
709 struct abis_nm_sw *sw = &g_sw;
710 struct stat st;
711 int rc, percent;
712
713 rc = fstat(sw->fd, &st);
714 if (rc < 0) {
715 perror("ERROR during stat");
716 return rc;
717 }
718
719 percent = (ftell(sw->stream) * 100) / st.st_size;
720 return percent;
721}
722
Harald Welte5e4d1b32009-02-01 13:36:56 +0000723/* Activate the specified software into the BTS */
724int abis_nm_software_activate(struct gsm_bts *bts, const char *fname,
725 gsm_cbfn *cbfn, void *cb_data)
726{
727 struct abis_nm_sw *sw = &g_sw;
728 int rc;
729
730 DEBUGP(DNM, "Activating Software (BTS %u, File \"%s\")\n",
731 bts->nr, fname);
732
733 if (sw->state != SW_STATE_NONE)
734 return -EBUSY;
735
736 sw->bts = bts;
737 sw->obj_class = NM_OC_SITE_MANAGER;
738 sw->obj_instance[0] = 0xff;
739 sw->obj_instance[1] = 0xff;
740 sw->obj_instance[2] = 0xff;
741 sw->state = SW_STATE_WAIT_ACTACK;
742 sw->cbfn = cbfn;
743 sw->cb_data = cb_data;
744
745 /* Open the file in order to fill some sw struct members */
746 rc = sw_open_file(sw, fname);
747 if (rc < 0) {
748 sw->state = SW_STATE_NONE;
749 return rc;
750 }
751 sw_close_file(sw);
752
753 return sw_activate(sw);
754}
755
Harald Welte8470bf22008-12-25 23:28:35 +0000756static void fill_nm_channel(struct abis_nm_channel *ch, u_int8_t bts_port,
Harald Welte52b1f982008-12-23 20:25:15 +0000757 u_int8_t ts_nr, u_int8_t subslot_nr)
758{
Harald Welteadaf08b2009-01-18 11:08:10 +0000759 ch->attrib = NM_ATT_ABIS_CHANNEL;
Harald Welte52b1f982008-12-23 20:25:15 +0000760 ch->bts_port = bts_port;
761 ch->timeslot = ts_nr;
762 ch->subslot = subslot_nr;
763}
764
765int abis_nm_establish_tei(struct gsm_bts *bts, u_int8_t trx_nr,
766 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot,
767 u_int8_t tei)
768{
769 struct abis_om_hdr *oh;
770 struct abis_nm_channel *ch;
Harald Welte702d8702008-12-26 20:25:35 +0000771 u_int8_t len = sizeof(*ch) + 2;
Harald Welte8470bf22008-12-25 23:28:35 +0000772 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000773
774 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
775 fill_om_fom_hdr(oh, len, NM_MT_ESTABLISH_TEI, NM_OC_RADIO_CARRIER,
776 bts->bts_nr, trx_nr, 0xff);
777
Harald Welte8470bf22008-12-25 23:28:35 +0000778 msgb_tv_put(msg, NM_ATT_TEI, tei);
Harald Welte52b1f982008-12-23 20:25:15 +0000779
780 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
781 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
782
783 return abis_nm_sendmsg(bts, msg);
784}
785
786/* connect signalling of one (BTS,TRX) to a particular timeslot on the E1 */
787int abis_nm_conn_terr_sign(struct gsm_bts_trx *trx,
788 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot)
789{
Harald Welte8470bf22008-12-25 23:28:35 +0000790 struct gsm_bts *bts = trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000791 struct abis_om_hdr *oh;
792 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000793 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000794
795 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000796 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_SIGN,
Harald Welte52b1f982008-12-23 20:25:15 +0000797 NM_OC_RADIO_CARRIER, bts->bts_nr, trx->nr, 0xff);
798
799 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
800 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
801
802 return abis_nm_sendmsg(bts, msg);
803}
804
805#if 0
806int abis_nm_disc_terr_sign(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
807 struct abis_nm_abis_channel *chan)
808{
809}
810#endif
811
812int abis_nm_conn_terr_traf(struct gsm_bts_trx_ts *ts,
813 u_int8_t e1_port, u_int8_t e1_timeslot,
814 u_int8_t e1_subslot)
815{
816 struct gsm_bts *bts = ts->trx->bts;
817 struct abis_om_hdr *oh;
818 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000819 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000820
821 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
822 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_TRAF,
823 NM_OC_BASEB_TRANSC, bts->bts_nr, ts->trx->nr, ts->nr);
824
825 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
826 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
827
828 return abis_nm_sendmsg(bts, msg);
829}
830
831#if 0
832int abis_nm_disc_terr_traf(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
833 struct abis_nm_abis_channel *chan,
834 u_int8_t subchan)
835{
836}
837#endif
838
839int abis_nm_set_channel_attr(struct gsm_bts_trx_ts *ts, u_int8_t chan_comb)
840{
841 struct gsm_bts *bts = ts->trx->bts;
842 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000843 u_int16_t arfcn = htons(ts->trx->arfcn);
Harald Welte52b1f982008-12-23 20:25:15 +0000844 u_int8_t zero = 0x00;
Harald Welte8470bf22008-12-25 23:28:35 +0000845 struct msgb *msg = nm_msgb_alloc();
Harald Welte702d8702008-12-26 20:25:35 +0000846 u_int8_t len = 4 + 2 + 2 + 2 + 2 +3;
Harald Welte52b1f982008-12-23 20:25:15 +0000847
848 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000849 fill_om_fom_hdr(oh, len, NM_MT_SET_CHAN_ATTR,
Harald Welte52b1f982008-12-23 20:25:15 +0000850 NM_OC_BASEB_TRANSC, bts->bts_nr,
851 ts->trx->nr, ts->nr);
852 /* FIXME: don't send ARFCN list, hopping sequence, mAIO, ...*/
853 msgb_tlv16_put(msg, NM_ATT_ARFCN_LIST, 1, &arfcn);
854 msgb_tv_put(msg, NM_ATT_CHAN_COMB, chan_comb);
855 msgb_tv_put(msg, NM_ATT_HSN, 0x00);
856 msgb_tv_put(msg, NM_ATT_MAIO, 0x00);
857 msgb_tv_put(msg, NM_ATT_TSC, 0x07); /* training sequence */
858 msgb_tlv_put(msg, 0x59, 1, &zero);
859
860 return abis_nm_sendmsg(bts, msg);
861}
862
Harald Welte8470bf22008-12-25 23:28:35 +0000863int abis_nm_raw_msg(struct gsm_bts *bts, int len, u_int8_t *rawmsg)
Harald Welte52b1f982008-12-23 20:25:15 +0000864{
Harald Welte8470bf22008-12-25 23:28:35 +0000865 struct msgb *msg = nm_msgb_alloc();
866 struct abis_om_hdr *oh;
Harald Welte52b1f982008-12-23 20:25:15 +0000867 u_int8_t *data;
868
869 oh = (struct abis_om_hdr *) msgb_put(msg, sizeof(*oh));
870 fill_om_hdr(oh, len);
871 data = msgb_put(msg, len);
Harald Weltead384642008-12-26 10:20:07 +0000872 memcpy(data, rawmsg, len);
Harald Welte52b1f982008-12-23 20:25:15 +0000873
874 return abis_nm_sendmsg(bts, msg);
875}
876
877/* Siemens specific commands */
878static int __simple_cmd(struct gsm_bts *bts, u_int8_t msg_type)
879{
880 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000881 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000882
883 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000884 fill_om_fom_hdr(oh, 0, msg_type, NM_OC_SITE_MANAGER,
Harald Welte52b1f982008-12-23 20:25:15 +0000885 0xff, 0xff, 0xff);
886
887 return abis_nm_sendmsg(bts, msg);
888}
889
890int abis_nm_event_reports(struct gsm_bts *bts, int on)
891{
892 if (on == 0)
Harald Welte227d4072009-01-03 08:16:25 +0000893 return __simple_cmd(bts, NM_MT_STOP_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000894 else
Harald Welte227d4072009-01-03 08:16:25 +0000895 return __simple_cmd(bts, NM_MT_REST_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000896}
897
Harald Welte3ffd1372009-02-01 22:15:49 +0000898
Harald Welte47d88ae2009-01-04 12:02:08 +0000899/* Siemens (or BS-11) specific commands */
900
Harald Welte3ffd1372009-02-01 22:15:49 +0000901int abis_nm_bs11_bsc_disconnect(struct gsm_bts *bts, int reconnect)
902{
903 if (reconnect == 0)
904 return __simple_cmd(bts, NM_MT_BS11_DISCONNECT);
905 else
906 return __simple_cmd(bts, NM_MT_BS11_RECONNECT);
907}
908
Harald Welteb8427972009-02-05 19:27:17 +0000909int abis_nm_bs11_restart(struct gsm_bts *bts)
910{
911 return __simple_cmd(bts, NM_MT_BS11_RESTART);
912}
913
914
Harald Welte268bb402009-02-01 19:11:56 +0000915struct bs11_date_time {
916 u_int16_t year;
917 u_int8_t month;
918 u_int8_t day;
919 u_int8_t hour;
920 u_int8_t min;
921 u_int8_t sec;
922} __attribute__((packed));
923
924
925void get_bs11_date_time(struct bs11_date_time *aet)
926{
927 time_t t;
928 struct tm *tm;
929
930 t = time(NULL);
931 tm = localtime(&t);
932 aet->sec = tm->tm_sec;
933 aet->min = tm->tm_min;
934 aet->hour = tm->tm_hour;
935 aet->day = tm->tm_mday;
936 aet->month = tm->tm_mon;
937 aet->year = htons(1900 + tm->tm_year);
938}
939
Harald Welte05188ee2009-01-18 11:39:08 +0000940int abis_nm_bs11_reset_resource(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000941{
Harald Welte4668fda2009-01-03 08:19:29 +0000942 return __simple_cmd(bts, NM_MT_BS11_RESET_RESOURCE);
Harald Welte52b1f982008-12-23 20:25:15 +0000943}
944
Harald Welte05188ee2009-01-18 11:39:08 +0000945int abis_nm_bs11_db_transmission(struct gsm_bts *bts, int begin)
Harald Welte52b1f982008-12-23 20:25:15 +0000946{
947 if (begin)
Harald Welte4668fda2009-01-03 08:19:29 +0000948 return __simple_cmd(bts, NM_MT_BS11_BEGIN_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000949 else
Harald Welte4668fda2009-01-03 08:19:29 +0000950 return __simple_cmd(bts, NM_MT_BS11_END_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000951}
Harald Welte47d88ae2009-01-04 12:02:08 +0000952
Harald Welte05188ee2009-01-18 11:39:08 +0000953int abis_nm_bs11_create_object(struct gsm_bts *bts,
Harald Welte1bc09062009-01-18 14:17:52 +0000954 enum abis_bs11_objtype type, u_int8_t idx,
955 u_int8_t attr_len, const u_int8_t *attr)
Harald Welte47d88ae2009-01-04 12:02:08 +0000956{
957 struct abis_om_hdr *oh;
958 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000959 u_int8_t *cur;
Harald Welte47d88ae2009-01-04 12:02:08 +0000960
961 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000962 fill_om_fom_hdr(oh, attr_len, NM_MT_BS11_CREATE_OBJ,
Harald Welte268bb402009-02-01 19:11:56 +0000963 NM_OC_BS11, type, 0, idx);
Harald Welte1bc09062009-01-18 14:17:52 +0000964 cur = msgb_put(msg, attr_len);
965 memcpy(cur, attr, attr_len);
Harald Welte47d88ae2009-01-04 12:02:08 +0000966
967 return abis_nm_sendmsg(bts, msg);
968}
969
Harald Welte05188ee2009-01-18 11:39:08 +0000970int abis_nm_bs11_create_envaBTSE(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000971{
972 struct abis_om_hdr *oh;
973 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000974 u_int8_t zero = 0x00;
Harald Welte47d88ae2009-01-04 12:02:08 +0000975
976 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000977 fill_om_fom_hdr(oh, 3, NM_MT_BS11_CREATE_OBJ,
Harald Welte1bc09062009-01-18 14:17:52 +0000978 NM_OC_BS11_ENVABTSE, 0, idx, 0xff);
979 msgb_tlv_put(msg, 0x99, 1, &zero);
Harald Welte47d88ae2009-01-04 12:02:08 +0000980
981 return abis_nm_sendmsg(bts, msg);
982}
983
Harald Welte05188ee2009-01-18 11:39:08 +0000984int abis_nm_bs11_create_bport(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000985{
986 struct abis_om_hdr *oh;
987 struct msgb *msg = nm_msgb_alloc();
988
989 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
990 fill_om_fom_hdr(oh, 0, NM_MT_BS11_CREATE_OBJ, NM_OC_BS11_BPORT,
991 idx, 0, 0);
992
993 return abis_nm_sendmsg(bts, msg);
994}
Harald Welte05188ee2009-01-18 11:39:08 +0000995
996int abis_nm_bs11_set_oml_tei(struct gsm_bts *bts, u_int8_t tei)
997{
998 struct abis_om_hdr *oh;
999 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +00001000
1001 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001002 fill_om_fom_hdr(oh, 2, NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
Harald Welte05188ee2009-01-18 11:39:08 +00001003 0xff, 0xff, 0xff);
1004 msgb_tv_put(msg, NM_ATT_TEI, tei);
1005
1006 return abis_nm_sendmsg(bts, msg);
1007}
1008
1009/* like abis_nm_conn_terr_traf */
1010int abis_nm_bs11_conn_oml(struct gsm_bts *bts, u_int8_t e1_port,
1011 u_int8_t e1_timeslot, u_int8_t e1_subslot)
1012{
1013 struct abis_om_hdr *oh;
1014 struct abis_nm_channel *ch;
1015 struct msgb *msg = nm_msgb_alloc();
1016
1017 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte05188ee2009-01-18 11:39:08 +00001018 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_BS11_SET_ATTR,
1019 NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff);
1020
1021 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
1022 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
1023
1024 return abis_nm_sendmsg(bts, msg);
1025}
1026
1027int abis_nm_bs11_set_trx_power(struct gsm_bts_trx *trx, u_int8_t level)
1028{
1029 struct abis_om_hdr *oh;
1030 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +00001031
1032 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001033 fill_om_fom_hdr(oh, 3, NM_MT_BS11_SET_ATTR,
Harald Welte05188ee2009-01-18 11:39:08 +00001034 NM_OC_BS11, BS11_OBJ_PA, 0x00, trx->nr);
1035 msgb_tlv_put(msg, NM_ATT_BS11_TXPWR, 1, &level);
1036
1037 return abis_nm_sendmsg(trx->bts, msg);
1038}
1039
Harald Welte268bb402009-02-01 19:11:56 +00001040//static const u_int8_t bs11_logon_c7[] = { 0x07, 0xd9, 0x01, 0x11, 0x0d, 0x10, 0x20 };
Harald Weltebb151312009-01-28 20:42:07 +00001041static const u_int8_t bs11_logon_c8[] = { 0x02 };
Harald Welte05188ee2009-01-18 11:39:08 +00001042static const u_int8_t bs11_logon_c9[] = "FACTORY";
1043
Harald Welte1bc09062009-01-18 14:17:52 +00001044int abis_nm_bs11_factory_logon(struct gsm_bts *bts, int on)
Harald Welte05188ee2009-01-18 11:39:08 +00001045{
1046 struct abis_om_hdr *oh;
1047 struct msgb *msg = nm_msgb_alloc();
Harald Welte268bb402009-02-01 19:11:56 +00001048 struct bs11_date_time bdt;
1049
1050 get_bs11_date_time(&bdt);
Harald Welte05188ee2009-01-18 11:39:08 +00001051
1052 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte1bc09062009-01-18 14:17:52 +00001053 if (on) {
Harald Welte268bb402009-02-01 19:11:56 +00001054 u_int8_t len = 3*2 + sizeof(bdt)
Harald Welte6f676a32009-01-18 14:27:48 +00001055 + sizeof(bs11_logon_c8) + sizeof(bs11_logon_c9);
Harald Welte043d04a2009-01-29 23:15:30 +00001056 fill_om_fom_hdr(oh, len, NM_MT_BS11_LMT_LOGON,
Harald Welte1bc09062009-01-18 14:17:52 +00001057 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
Harald Welte043d04a2009-01-29 23:15:30 +00001058 msgb_tlv_put(msg, NM_ATT_BS11_LMT_LOGIN_TIME,
Harald Welte5083b0b2009-02-02 19:20:52 +00001059 sizeof(bdt), (u_int8_t *) &bdt);
Harald Welte043d04a2009-01-29 23:15:30 +00001060 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_ACC_LEV,
1061 sizeof(bs11_logon_c8), bs11_logon_c8);
1062 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_NAME,
1063 sizeof(bs11_logon_c9), bs11_logon_c9);
Harald Welte1bc09062009-01-18 14:17:52 +00001064 } else {
Harald Welte5e4d1b32009-02-01 13:36:56 +00001065 fill_om_fom_hdr(oh, 0, NM_MT_BS11_LMT_LOGOFF,
Harald Welte1bc09062009-01-18 14:17:52 +00001066 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
1067 }
Harald Welte05188ee2009-01-18 11:39:08 +00001068
1069 return abis_nm_sendmsg(bts, msg);
1070}
Harald Welte1bc09062009-01-18 14:17:52 +00001071
1072int abis_nm_bs11_set_trx1_pw(struct gsm_bts *bts, const char *password)
1073{
1074 struct abis_om_hdr *oh;
1075 struct msgb *msg;
1076
1077 if (strlen(password) != 10)
1078 return -EINVAL;
1079
1080 msg = nm_msgb_alloc();
1081 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001082 fill_om_fom_hdr(oh, 2+strlen(password), NM_MT_BS11_SET_ATTR,
Harald Welte1bc09062009-01-18 14:17:52 +00001083 NM_OC_BS11, BS11_OBJ_TRX1, 0x00, 0x00);
1084 msgb_tlv_put(msg, NM_ATT_BS11_PASSWORD, 10, (const u_int8_t *)password);
1085
1086 return abis_nm_sendmsg(bts, msg);
1087}
1088
1089int abis_nm_bs11_get_state(struct gsm_bts *bts)
1090{
1091 return __simple_cmd(bts, NM_MT_BS11_GET_STATE);
1092}
Harald Welte5e4d1b32009-02-01 13:36:56 +00001093
1094/* BS11 SWL */
1095
1096struct abis_nm_bs11_sw {
1097 struct gsm_bts *bts;
1098 char swl_fname[PATH_MAX];
1099 u_int8_t win_size;
Harald Welte3ffd1372009-02-01 22:15:49 +00001100 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001101 struct llist_head file_list;
1102 gsm_cbfn *user_cb; /* specified by the user */
1103};
1104static struct abis_nm_bs11_sw _g_bs11_sw, *g_bs11_sw = &_g_bs11_sw;
1105
1106struct file_list_entry {
1107 struct llist_head list;
1108 char fname[PATH_MAX];
1109};
1110
1111struct file_list_entry *fl_dequeue(struct llist_head *queue)
1112{
1113 struct llist_head *lh;
1114
1115 if (llist_empty(queue))
1116 return NULL;
1117
1118 lh = queue->next;
1119 llist_del(lh);
1120
1121 return llist_entry(lh, struct file_list_entry, list);
1122}
1123
1124static int bs11_read_swl_file(struct abis_nm_bs11_sw *bs11_sw)
1125{
1126 char linebuf[255];
1127 struct llist_head *lh, *lh2;
1128 FILE *swl;
1129 int rc = 0;
1130
1131 swl = fopen(bs11_sw->swl_fname, "r");
1132 if (!swl)
1133 return -ENODEV;
1134
1135 /* zero the stale file list, if any */
1136 llist_for_each_safe(lh, lh2, &bs11_sw->file_list) {
1137 llist_del(lh);
1138 free(lh);
1139 }
1140
1141 while (fgets(linebuf, sizeof(linebuf), swl)) {
1142 char file_id[12+1];
1143 char file_version[80+1];
1144 struct file_list_entry *fle;
1145 static char dir[PATH_MAX];
1146
1147 if (strlen(linebuf) < 4)
1148 continue;
Harald Welte3ffd1372009-02-01 22:15:49 +00001149
Harald Welte5e4d1b32009-02-01 13:36:56 +00001150 rc = sscanf(linebuf+4, "%12s:%80s\r\n", file_id, file_version);
1151 if (rc < 0) {
1152 perror("ERR parsing SWL file");
1153 rc = -EINVAL;
1154 goto out;
1155 }
1156 if (rc < 2)
1157 continue;
1158
1159 fle = malloc(sizeof(*fle));
1160 if (!fle) {
1161 rc = -ENOMEM;
1162 goto out;
1163 }
1164 memset(fle, 0, sizeof(*fle));
1165
1166 /* construct new filename */
1167 strncpy(dir, bs11_sw->swl_fname, sizeof(dir));
1168 strncat(fle->fname, dirname(dir), sizeof(fle->fname) - 1);
1169 strcat(fle->fname, "/");
1170 strncat(fle->fname, file_id, sizeof(fle->fname) - 1 -strlen(fle->fname));
Harald Welte5e4d1b32009-02-01 13:36:56 +00001171
1172 llist_add_tail(&fle->list, &bs11_sw->file_list);
1173 }
1174
1175out:
1176 fclose(swl);
1177 return rc;
1178}
1179
1180/* bs11 swload specific callback, passed to abis_nm core swload */
1181static int bs11_swload_cbfn(unsigned int hook, unsigned int event,
1182 struct msgb *msg, void *data, void *param)
1183{
1184 struct abis_nm_bs11_sw *bs11_sw = data;
1185 struct file_list_entry *fle;
1186 int rc = 0;
1187
Harald Welte97ed1e72009-02-06 13:38:02 +00001188 DEBUGP(DNM, "Event %u\n", event);
1189
Harald Welte5e4d1b32009-02-01 13:36:56 +00001190 switch (event) {
1191 case NM_MT_LOAD_END_ACK:
1192 fle = fl_dequeue(&bs11_sw->file_list);
1193 if (fle) {
1194 /* start download the next file of our file list */
1195 rc = abis_nm_software_load(bs11_sw->bts, fle->fname,
1196 bs11_sw->win_size,
Harald Welte3ffd1372009-02-01 22:15:49 +00001197 bs11_sw->forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001198 &bs11_swload_cbfn, bs11_sw);
1199 free(fle);
1200 } else {
1201 /* activate the SWL */
1202 rc = abis_nm_software_activate(bs11_sw->bts,
1203 bs11_sw->swl_fname,
1204 bs11_swload_cbfn,
1205 bs11_sw);
1206 }
1207 break;
Harald Welte3ffd1372009-02-01 22:15:49 +00001208 case NM_MT_LOAD_SEG_ACK:
Harald Welte5e4d1b32009-02-01 13:36:56 +00001209 case NM_MT_LOAD_END_NACK:
1210 case NM_MT_LOAD_INIT_ACK:
1211 case NM_MT_LOAD_INIT_NACK:
1212 case NM_MT_ACTIVATE_SW_NACK:
1213 case NM_MT_ACTIVATE_SW_ACK:
1214 default:
1215 /* fallthrough to the user callback */
Harald Welte97ed1e72009-02-06 13:38:02 +00001216 if (bs11_sw->user_cb)
1217 rc = bs11_sw->user_cb(hook, event, msg, NULL, NULL);
Harald Welte5e4d1b32009-02-01 13:36:56 +00001218 break;
1219 }
1220
1221 return rc;
1222}
1223
1224/* Siemens provides a SWL file that is a mere listing of all the other
1225 * files that are part of a software release. We need to upload first
1226 * the list file, and then each file that is listed in the list file */
1227int abis_nm_bs11_load_swl(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +00001228 u_int8_t win_size, int forced, gsm_cbfn *cbfn)
Harald Welte5e4d1b32009-02-01 13:36:56 +00001229{
1230 struct abis_nm_bs11_sw *bs11_sw = g_bs11_sw;
1231 struct file_list_entry *fle;
1232 int rc = 0;
1233
1234 INIT_LLIST_HEAD(&bs11_sw->file_list);
1235 bs11_sw->bts = bts;
1236 bs11_sw->win_size = win_size;
1237 bs11_sw->user_cb = cbfn;
Harald Welte3ffd1372009-02-01 22:15:49 +00001238 bs11_sw->forced = forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001239
1240 strncpy(bs11_sw->swl_fname, fname, sizeof(bs11_sw->swl_fname));
1241 rc = bs11_read_swl_file(bs11_sw);
1242 if (rc < 0)
1243 return rc;
1244
1245 /* dequeue next item in file list */
1246 fle = fl_dequeue(&bs11_sw->file_list);
1247 if (!fle)
1248 return -EINVAL;
1249
1250 /* start download the next file of our file list */
Harald Welte3ffd1372009-02-01 22:15:49 +00001251 rc = abis_nm_software_load(bts, fle->fname, win_size, forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001252 bs11_swload_cbfn, bs11_sw);
1253 free(fle);
1254 return rc;
1255}
1256
Harald Welte5083b0b2009-02-02 19:20:52 +00001257#if 0
Harald Welte5e4d1b32009-02-01 13:36:56 +00001258static u_int8_t req_attr_btse[] = {
1259 NM_ATT_ADM_STATE, NM_ATT_BS11_LMT_LOGON_SESSION,
1260 NM_ATT_BS11_LMT_LOGIN_TIME, NM_ATT_BS11_LMT_USER_ACC_LEV,
1261 NM_ATT_BS11_LMT_USER_NAME,
1262
1263 0xaf, NM_ATT_BS11_RX_OFFSET, NM_ATT_BS11_VENDOR_NAME,
1264
1265 NM_ATT_BS11_SW_LOAD_INTENDED, NM_ATT_BS11_SW_LOAD_SAFETY,
1266
1267 NM_ATT_BS11_SW_LOAD_STORED };
1268
1269static u_int8_t req_attr_btsm[] = {
1270 NM_ATT_ABIS_CHANNEL, NM_ATT_TEI, NM_ATT_BS11_ABIS_EXT_TIME,
1271 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xce, NM_ATT_FILE_ID,
1272 NM_ATT_FILE_VERSION, NM_ATT_OPER_STATE, 0xe8, NM_ATT_BS11_ALL_TEST_CATG,
1273 NM_ATT_SW_DESCR, NM_ATT_GET_ARI };
Harald Welte5083b0b2009-02-02 19:20:52 +00001274#endif
Harald Welte5e4d1b32009-02-01 13:36:56 +00001275
1276static u_int8_t req_attr[] = {
1277 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xa8, NM_ATT_OPER_STATE,
1278 0xd5, 0xa1, NM_ATT_BS11_ESN_FW_CODE_NO, NM_ATT_BS11_ESN_HW_CODE_NO,
1279 0x42, NM_ATT_BS11_ESN_PCB_SERIAL };
1280
1281int abis_nm_bs11_get_serno(struct gsm_bts *bts)
1282{
1283 struct abis_om_hdr *oh;
1284 struct msgb *msg = nm_msgb_alloc();
1285
1286 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1287 /* SiemensHW CCTRL object */
1288 fill_om_fom_hdr(oh, 2+sizeof(req_attr), NM_MT_GET_ATTR, NM_OC_BS11,
1289 0x03, 0x00, 0x00);
1290 msgb_tlv_put(msg, NM_ATT_LIST_REQ_ATTR, sizeof(req_attr), req_attr);
1291
1292 return abis_nm_sendmsg(bts, msg);
1293}
Harald Welte268bb402009-02-01 19:11:56 +00001294
1295int abis_nm_bs11_set_ext_time(struct gsm_bts *bts)
1296{
1297 struct abis_om_hdr *oh;
1298 struct msgb *msg = nm_msgb_alloc();
1299 struct bs11_date_time aet;
1300
1301 get_bs11_date_time(&aet);
1302 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1303 /* SiemensHW CCTRL object */
1304 fill_om_fom_hdr(oh, 2+sizeof(aet), NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
1305 0xff, 0xff, 0xff);
Harald Welte5083b0b2009-02-02 19:20:52 +00001306 msgb_tlv_put(msg, NM_ATT_BS11_ABIS_EXT_TIME, sizeof(aet), (u_int8_t *) &aet);
Harald Welte268bb402009-02-01 19:11:56 +00001307
1308 return abis_nm_sendmsg(bts, msg);
1309}