blob: b506f340d6996df89213fe18157c0845d7dcd36f [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);
Harald Welte97ed1e72009-02-06 13:38:02 +0000174 u_int8_t *data = &foh->data[0];
175
176 DEBUGP(DNM, "STATE CHG: OC=%02x INST=(%02x,%02x,%02x) ",
177 foh->obj_class, foh->obj_inst.bts_nr, foh->obj_inst.trx_nr,
178 foh->obj_inst.ts_nr);
179 if (*data++ == NM_ATT_OPER_STATE)
180 DEBUGPC(DNM, "OP_STATE=%02x ", *data++);
181 if (*data++ == NM_ATT_AVAIL_STATUS) {
182 u_int8_t att_len = *data++;
183 while (att_len--)
184 DEBUGPC(DNM, "AVAIL=%02x ", *data++);
185 }
186 DEBUGPC(DNM, "\n");
187 return 0;
188}
189
190static int abis_nm_rcvmsg_report(struct msgb *mb)
191{
192 struct abis_om_fom_hdr *foh = msgb_l3(mb);
193 u_int8_t mt = foh->msg_type;
194
195 //nmh->cfg->report_cb(mb, foh);
196
197 switch (mt) {
198 case NM_MT_STATECHG_EVENT_REP:
199 return abis_nm_rx_statechg_rep(mb);
200 break;
201 };
202
203 DEBUGP(DNM, "reporting NM MT 0x%02x\n", mt);
204
205 return 0;
206}
207
Harald Welte52b1f982008-12-23 20:25:15 +0000208/* Receive a OML NM Message from BTS */
Harald Welte8470bf22008-12-25 23:28:35 +0000209static int abis_nm_rcvmsg_fom(struct msgb *mb)
Harald Welte52b1f982008-12-23 20:25:15 +0000210{
211 struct abis_om_fom_hdr *foh = msgb_l3(mb);
212 u_int8_t mt = foh->msg_type;
213
214 /* check for unsolicited message */
Harald Welte97ed1e72009-02-06 13:38:02 +0000215 if (is_report(mt))
216 return abis_nm_rcvmsg_report(mb);
Harald Welte52b1f982008-12-23 20:25:15 +0000217
Harald Welte4724f992009-01-18 18:01:49 +0000218 if (is_in_arr(mt, sw_load_msgs, ARRAY_SIZE(sw_load_msgs)))
219 return abis_nm_rcvmsg_sw(mb);
220
Harald Weltead384642008-12-26 10:20:07 +0000221#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000222 /* check if last message is to be acked */
223 if (is_ack_nack(nmh->last_msgtype)) {
224 if (mt == MT_ACK(nmh->last_msgtype)) {
225 fprintf(stderr, "received ACK (0x%x)\n",
226 foh->msg_type);
227 /* we got our ACK, continue sending the next msg */
228 } else if (mt == MT_NACK(nmh->last_msgtype)) {
229 /* we got a NACK, signal this to the caller */
230 fprintf(stderr, "received NACK (0x%x)\n",
231 foh->msg_type);
232 /* FIXME: somehow signal this to the caller */
233 } else {
234 /* really strange things happen */
235 return -EINVAL;
236 }
237 }
Harald Weltead384642008-12-26 10:20:07 +0000238#endif
239
Harald Welte97ed1e72009-02-06 13:38:02 +0000240 switch (mt) {
241 case NM_MT_BS11_LMT_SESSION:
242 DEBUGP(DNM, "LMT Event: \n");
243 break;
244 }
245
Harald Weltead384642008-12-26 10:20:07 +0000246 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000247}
248
249/* High-Level API */
250/* Entry-point where L2 OML from BTS enters the NM code */
Harald Welte8470bf22008-12-25 23:28:35 +0000251int abis_nm_rcvmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000252{
253 int rc;
254 struct abis_om_hdr *oh = msgb_l2(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000255
256 /* Various consistency checks */
257 if (oh->placement != ABIS_OM_PLACEMENT_ONLY) {
258 fprintf(stderr, "ABIS OML placement 0x%x not supported\n",
259 oh->placement);
260 return -EINVAL;
261 }
262 if (oh->sequence != 0) {
263 fprintf(stderr, "ABIS OML sequence 0x%x != 0x00\n",
264 oh->sequence);
265 return -EINVAL;
266 }
Harald Welte702d8702008-12-26 20:25:35 +0000267#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000268 unsigned int l2_len = msg->tail - (u_int8_t *)msgb_l2(msg);
269 unsigned int hlen = sizeof(*oh) + sizeof(struct abis_om_fom_hdr);
Harald Welte702d8702008-12-26 20:25:35 +0000270 if (oh->length + hlen > l2_len) {
Harald Welte52b1f982008-12-23 20:25:15 +0000271 fprintf(stderr, "ABIS OML truncated message (%u > %u)\n",
272 oh->length + sizeof(*oh), l2_len);
273 return -EINVAL;
274 }
Harald Welte702d8702008-12-26 20:25:35 +0000275 if (oh->length + hlen < l2_len)
276 fprintf(stderr, "ABIS OML message with extra trailer?!? (oh->len=%d, sizeof_oh=%d l2_len=%d\n", oh->length, sizeof(*oh), l2_len);
277#endif
Harald Weltead384642008-12-26 10:20:07 +0000278 msg->l3h = (unsigned char *)oh + sizeof(*oh);
Harald Welte52b1f982008-12-23 20:25:15 +0000279
280 switch (oh->mdisc) {
281 case ABIS_OM_MDISC_FOM:
Harald Welte8470bf22008-12-25 23:28:35 +0000282 rc = abis_nm_rcvmsg_fom(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000283 break;
284 case ABIS_OM_MDISC_MMI:
285 case ABIS_OM_MDISC_TRAU:
286 case ABIS_OM_MDISC_MANUF:
287 default:
288 fprintf(stderr, "unknown ABIS OML message discriminator 0x%x\n",
289 oh->mdisc);
290 return -EINVAL;
291 }
292
Harald Weltead384642008-12-26 10:20:07 +0000293 msgb_free(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000294 return rc;
295}
296
297#if 0
298/* initialized all resources */
299struct abis_nm_h *abis_nm_init(struct abis_nm_cfg *cfg)
300{
301 struct abis_nm_h *nmh;
302
303 nmh = malloc(sizeof(*nmh));
304 if (!nmh)
305 return NULL;
306
307 nmh->cfg = cfg;
308
309 return nmh;
310}
311
312/* free all resources */
313void abis_nm_fini(struct abis_nm_h *nmh)
314{
315 free(nmh);
316}
317#endif
318
319/* Here we are trying to define a high-level API that can be used by
320 * the actual BSC implementation. However, the architecture is currently
321 * still under design. Ideally the calls to this API would be synchronous,
322 * while the underlying stack behind the APi runs in a traditional select
323 * based state machine.
324 */
325
Harald Welte4724f992009-01-18 18:01:49 +0000326/* 6.2 Software Load: */
327enum sw_state {
328 SW_STATE_NONE,
329 SW_STATE_WAIT_INITACK,
330 SW_STATE_WAIT_SEGACK,
331 SW_STATE_WAIT_ENDACK,
332 SW_STATE_WAIT_ACTACK,
333 SW_STATE_ERROR,
334};
Harald Welte52b1f982008-12-23 20:25:15 +0000335
Harald Welte52b1f982008-12-23 20:25:15 +0000336struct abis_nm_sw {
Harald Welte4724f992009-01-18 18:01:49 +0000337 struct gsm_bts *bts;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000338 gsm_cbfn *cbfn;
339 void *cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000340 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000341
Harald Welte52b1f982008-12-23 20:25:15 +0000342 /* this will become part of the SW LOAD INITIATE */
343 u_int8_t obj_class;
344 u_int8_t obj_instance[3];
Harald Welte4724f992009-01-18 18:01:49 +0000345
346 u_int8_t file_id[255];
347 u_int8_t file_id_len;
348
349 u_int8_t file_version[255];
350 u_int8_t file_version_len;
351
352 u_int8_t window_size;
353 u_int8_t seg_in_window;
354
355 int fd;
356 FILE *stream;
357 enum sw_state state;
Harald Welte1602ade2009-01-29 21:12:39 +0000358 int last_seg;
Harald Welte52b1f982008-12-23 20:25:15 +0000359};
360
Harald Welte4724f992009-01-18 18:01:49 +0000361static struct abis_nm_sw g_sw;
362
363/* 6.2.1 / 8.3.1: Load Data Initiate */
364static int sw_load_init(struct abis_nm_sw *sw)
Harald Welte52b1f982008-12-23 20:25:15 +0000365{
Harald Welte4724f992009-01-18 18:01:49 +0000366 struct abis_om_hdr *oh;
367 struct msgb *msg = nm_msgb_alloc();
368 u_int8_t len = 3*2 + sw->file_id_len + sw->file_version_len;
369
370 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
371 fill_om_fom_hdr(oh, len, NM_MT_LOAD_INIT, sw->obj_class,
372 sw->obj_instance[0], sw->obj_instance[1],
373 sw->obj_instance[2]);
374
375 /* FIXME: this is BS11 specific format */
376 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
377 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
378 sw->file_version);
379 msgb_tv_put(msg, NM_ATT_WINDOW_SIZE, sw->window_size);
380
381 return abis_nm_sendmsg(sw->bts, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000382}
383
Harald Welte1602ade2009-01-29 21:12:39 +0000384static int is_last_line(FILE *stream)
385{
386 char next_seg_buf[256];
387 long pos;
388
389 /* check if we're sending the last line */
390 pos = ftell(stream);
391 if (!fgets(next_seg_buf, sizeof(next_seg_buf)-2, stream)) {
392 fseek(stream, pos, SEEK_SET);
393 return 1;
394 }
395
396 fseek(stream, pos, SEEK_SET);
397 return 0;
398}
399
Harald Welte4724f992009-01-18 18:01:49 +0000400/* 6.2.2 / 8.3.2 Load Data Segment */
401static int sw_load_segment(struct abis_nm_sw *sw)
402{
403 struct abis_om_hdr *oh;
404 struct msgb *msg = nm_msgb_alloc();
405 char seg_buf[256];
406 char *line_buf = seg_buf+2;
Harald Welte3b8ba212009-01-29 12:27:58 +0000407 unsigned char *tlv;
Harald Welte4724f992009-01-18 18:01:49 +0000408 u_int8_t len;
Harald Welte4724f992009-01-18 18:01:49 +0000409
410 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte3b8ba212009-01-29 12:27:58 +0000411
412 switch (sw->bts->type) {
413 case GSM_BTS_TYPE_BS11:
414 if (fgets(line_buf, sizeof(seg_buf)-2, sw->stream) == NULL) {
415 perror("fgets reading segment");
416 return -EINVAL;
417 }
418 seg_buf[0] = 0x00;
Harald Welte1602ade2009-01-29 21:12:39 +0000419
420 /* check if we're sending the last line */
421 sw->last_seg = is_last_line(sw->stream);
422 if (sw->last_seg)
423 seg_buf[1] = 0;
424 else
425 seg_buf[1] = 1 + sw->seg_in_window++;
Harald Welte3b8ba212009-01-29 12:27:58 +0000426
427 len = strlen(line_buf) + 2;
428 tlv = msgb_put(msg, TLV_GROSS_LEN(len));
429 tlv_put(tlv, NM_ATT_BS11_FILE_DATA, len, (u_int8_t *)seg_buf);
430 /* BS11 wants CR + LF in excess of the TLV length !?! */
431 tlv[1] -= 2;
432
433 /* we only now know the exact length for the OM hdr */
434 len = strlen(line_buf)+2;
435 break;
436 default:
437 /* FIXME: Other BTS types */
438 return -1;
Harald Welte4724f992009-01-18 18:01:49 +0000439 }
Harald Welte4724f992009-01-18 18:01:49 +0000440
Harald Welte4724f992009-01-18 18:01:49 +0000441 fill_om_fom_hdr(oh, len, NM_MT_LOAD_SEG, sw->obj_class,
442 sw->obj_instance[0], sw->obj_instance[1],
443 sw->obj_instance[2]);
444
445 return abis_nm_sendmsg(sw->bts, msg);
446}
447
448/* 6.2.4 / 8.3.4 Load Data End */
449static int sw_load_end(struct abis_nm_sw *sw)
450{
451 struct abis_om_hdr *oh;
452 struct msgb *msg = nm_msgb_alloc();
453 u_int8_t len = 2*2 + sw->file_id_len + sw->file_version_len;
454
455 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
456 fill_om_fom_hdr(oh, len, NM_MT_LOAD_END, sw->obj_class,
457 sw->obj_instance[0], sw->obj_instance[1],
458 sw->obj_instance[2]);
459
460 /* FIXME: this is BS11 specific format */
461 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
462 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
463 sw->file_version);
464
465 return abis_nm_sendmsg(sw->bts, msg);
466}
Harald Welte5e4d1b32009-02-01 13:36:56 +0000467
Harald Welte52b1f982008-12-23 20:25:15 +0000468/* Activate the specified software into the BTS */
Harald Welte4724f992009-01-18 18:01:49 +0000469static int sw_activate(struct abis_nm_sw *sw)
Harald Welte52b1f982008-12-23 20:25:15 +0000470{
Harald Welte4724f992009-01-18 18:01:49 +0000471 struct abis_om_hdr *oh;
472 struct msgb *msg = nm_msgb_alloc();
473 u_int8_t len = 2*2 + sw->file_id_len + sw->file_version_len;
Harald Welte52b1f982008-12-23 20:25:15 +0000474
Harald Welte4724f992009-01-18 18:01:49 +0000475 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
476 fill_om_fom_hdr(oh, len, NM_MT_ACTIVATE_SW, sw->obj_class,
477 sw->obj_instance[0], sw->obj_instance[1],
478 sw->obj_instance[2]);
479
480 /* FIXME: this is BS11 specific format */
481 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
482 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
483 sw->file_version);
484
485 return abis_nm_sendmsg(sw->bts, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000486}
Harald Welte4724f992009-01-18 18:01:49 +0000487
488static int sw_open_file(struct abis_nm_sw *sw, const char *fname)
489{
490 char file_id[12+1];
491 char file_version[80+1];
492 int rc;
493
494 sw->fd = open(fname, O_RDONLY);
495 if (sw->fd < 0)
496 return sw->fd;
497
498 switch (sw->bts->type) {
499 case GSM_BTS_TYPE_BS11:
500 sw->stream = fdopen(sw->fd, "r");
501 if (!sw->stream) {
502 perror("fdopen");
503 return -1;
504 }
505 /* read first line and parse file ID and VERSION */
Harald Welte3b8ba212009-01-29 12:27:58 +0000506 rc = fscanf(sw->stream, "@(#)%12s:%80s\r\n",
Harald Welte4724f992009-01-18 18:01:49 +0000507 file_id, file_version);
508 if (rc != 2) {
509 perror("parsing header line of software file");
510 return -1;
511 }
512 strcpy((char *)sw->file_id, file_id);
513 sw->file_id_len = strlen(file_id);
514 strcpy((char *)sw->file_version, file_version);
515 sw->file_version_len = strlen(file_version);
516 /* rewind to start of file */
Harald Welte3b8ba212009-01-29 12:27:58 +0000517 rewind(sw->stream);
Harald Welte4724f992009-01-18 18:01:49 +0000518 break;
519 default:
520 /* We don't know how to treat them yet */
521 close(sw->fd);
522 return -EINVAL;
523 }
524
525 return 0;
526}
527
528static void sw_close_file(struct abis_nm_sw *sw)
529{
530 switch (sw->bts->type) {
531 case GSM_BTS_TYPE_BS11:
532 fclose(sw->stream);
533 break;
534 default:
535 close(sw->fd);
536 break;
537 }
538}
539
540/* Fill the window */
541static int sw_fill_window(struct abis_nm_sw *sw)
542{
543 int rc;
544
545 while (sw->seg_in_window < sw->window_size) {
546 rc = sw_load_segment(sw);
547 if (rc < 0)
548 return rc;
Harald Welte1602ade2009-01-29 21:12:39 +0000549 if (sw->last_seg)
550 break;
Harald Welte4724f992009-01-18 18:01:49 +0000551 }
552 return 0;
553}
554
555/* callback function from abis_nm_rcvmsg() handler */
556static int abis_nm_rcvmsg_sw(struct msgb *mb)
557{
558 struct abis_om_fom_hdr *foh = msgb_l3(mb);
559 int rc = -1;
560 struct abis_nm_sw *sw = &g_sw;
561 enum sw_state old_state = sw->state;
562
Harald Welte3ffd1372009-02-01 22:15:49 +0000563 //DEBUGP(DNM, "state %u, NM MT 0x%02x\n", sw->state, foh->msg_type);
Harald Welte4724f992009-01-18 18:01:49 +0000564
565 switch (sw->state) {
566 case SW_STATE_WAIT_INITACK:
567 switch (foh->msg_type) {
568 case NM_MT_LOAD_INIT_ACK:
569 /* fill window with segments */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000570 if (sw->cbfn)
571 sw->cbfn(GSM_HOOK_NM_SWLOAD,
572 NM_MT_LOAD_INIT_ACK, mb,
573 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000574 rc = sw_fill_window(sw);
575 sw->state = SW_STATE_WAIT_SEGACK;
576 break;
577 case NM_MT_LOAD_INIT_NACK:
Harald Welte3ffd1372009-02-01 22:15:49 +0000578 if (sw->forced) {
579 DEBUGP(DNM, "FORCED: Ignoring Software Load "
580 "Init NACK\n");
581 if (sw->cbfn)
582 sw->cbfn(GSM_HOOK_NM_SWLOAD,
583 NM_MT_LOAD_INIT_ACK, mb,
584 sw->cb_data, NULL);
585 rc = sw_fill_window(sw);
586 sw->state = SW_STATE_WAIT_SEGACK;
587 } else {
588 DEBUGP(DNM, "Software Load Init NACK\n");
589 if (sw->cbfn)
590 sw->cbfn(GSM_HOOK_NM_SWLOAD,
591 NM_MT_LOAD_INIT_NACK, mb,
592 sw->cb_data, NULL);
593 sw->state = SW_STATE_ERROR;
594 }
Harald Welte4724f992009-01-18 18:01:49 +0000595 break;
596 }
597 break;
598 case SW_STATE_WAIT_SEGACK:
599 switch (foh->msg_type) {
600 case NM_MT_LOAD_SEG_ACK:
Harald Welte3ffd1372009-02-01 22:15:49 +0000601 if (sw->cbfn)
602 sw->cbfn(GSM_HOOK_NM_SWLOAD,
603 NM_MT_LOAD_SEG_ACK, mb,
604 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000605 sw->seg_in_window = 0;
Harald Welte1602ade2009-01-29 21:12:39 +0000606 if (!sw->last_seg) {
607 /* fill window with more segments */
608 rc = sw_fill_window(sw);
609 sw->state = SW_STATE_WAIT_SEGACK;
610 } else {
611 /* end the transfer */
612 sw->state = SW_STATE_WAIT_ENDACK;
613 rc = sw_load_end(sw);
614 }
Harald Welte4724f992009-01-18 18:01:49 +0000615 break;
616 }
617 break;
618 case SW_STATE_WAIT_ENDACK:
619 switch (foh->msg_type) {
620 case NM_MT_LOAD_END_ACK:
621 sw_close_file(sw);
Harald Welte5e4d1b32009-02-01 13:36:56 +0000622 DEBUGP(DNM, "Software Load End (BTS %u)\n",
623 sw->bts->nr);
624 sw->state = SW_STATE_NONE;
625 if (sw->cbfn)
626 sw->cbfn(GSM_HOOK_NM_SWLOAD,
627 NM_MT_LOAD_END_ACK, mb,
628 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000629 break;
630 case NM_MT_LOAD_END_NACK:
Holger Freyther31338a12009-02-06 17:43:50 +0000631 if (sw->forced) {
632 DEBUGP(DNM, "FORCED: Ignoring Software Load"
633 "End NACK\n");
634 sw->state = SW_STATE_NONE;
635 if (sw->cbfn)
636 sw->cbfn(GSM_HOOK_NM_SWLOAD,
637 NM_MT_LOAD_END_ACK, mb,
638 sw->cb_data, NULL);
639 } else {
640 DEBUGP(DNM, "Software Load End NACK\n");
641 sw->state = SW_STATE_ERROR;
642 if (sw->cbfn)
643 sw->cbfn(GSM_HOOK_NM_SWLOAD,
644 NM_MT_LOAD_END_NACK, mb,
645 sw->cb_data, NULL);
646 }
Harald Welte4724f992009-01-18 18:01:49 +0000647 break;
648 }
649 case SW_STATE_WAIT_ACTACK:
650 switch (foh->msg_type) {
651 case NM_MT_ACTIVATE_SW_ACK:
652 /* we're done */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000653 DEBUGP(DNM, "Activate Software DONE!\n");
Harald Welte4724f992009-01-18 18:01:49 +0000654 sw->state = SW_STATE_NONE;
655 rc = 0;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000656 if (sw->cbfn)
657 sw->cbfn(GSM_HOOK_NM_SWLOAD,
658 NM_MT_ACTIVATE_SW_ACK, mb,
659 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000660 break;
661 case NM_MT_ACTIVATE_SW_NACK:
Harald Welte1602ade2009-01-29 21:12:39 +0000662 DEBUGP(DNM, "Activate Software NACK\n");
Harald Welte4724f992009-01-18 18:01:49 +0000663 sw->state = SW_STATE_ERROR;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000664 if (sw->cbfn)
665 sw->cbfn(GSM_HOOK_NM_SWLOAD,
666 NM_MT_ACTIVATE_SW_NACK, mb,
667 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000668 break;
669 }
670 case SW_STATE_NONE:
671 case SW_STATE_ERROR:
672 break;
673 }
674
675 if (rc)
676 fprintf(stderr, "unexpected NM MT 0x%02x in state %u -> %u\n",
677 foh->msg_type, old_state, sw->state);
678
679 return rc;
680}
681
682/* Load the specified software into the BTS */
683int abis_nm_software_load(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +0000684 u_int8_t win_size, int forced,
685 gsm_cbfn *cbfn, void *cb_data)
Harald Welte4724f992009-01-18 18:01:49 +0000686{
687 struct abis_nm_sw *sw = &g_sw;
688 int rc;
689
Harald Welte5e4d1b32009-02-01 13:36:56 +0000690 DEBUGP(DNM, "Software Load (BTS %u, File \"%s\")\n",
691 bts->nr, fname);
692
Harald Welte4724f992009-01-18 18:01:49 +0000693 if (sw->state != SW_STATE_NONE)
694 return -EBUSY;
695
696 sw->bts = bts;
697 sw->obj_class = NM_OC_SITE_MANAGER;
698 sw->obj_instance[0] = 0xff;
699 sw->obj_instance[1] = 0xff;
700 sw->obj_instance[2] = 0xff;
701 sw->window_size = win_size;
702 sw->state = SW_STATE_WAIT_INITACK;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000703 sw->cbfn = cbfn;
704 sw->cb_data = cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000705 sw->forced = forced;
Harald Welte4724f992009-01-18 18:01:49 +0000706
707 rc = sw_open_file(sw, fname);
708 if (rc < 0) {
709 sw->state = SW_STATE_NONE;
710 return rc;
711 }
712
713 return sw_load_init(sw);
714}
Harald Welte52b1f982008-12-23 20:25:15 +0000715
Harald Welte1602ade2009-01-29 21:12:39 +0000716int abis_nm_software_load_status(struct gsm_bts *bts)
717{
718 struct abis_nm_sw *sw = &g_sw;
719 struct stat st;
720 int rc, percent;
721
722 rc = fstat(sw->fd, &st);
723 if (rc < 0) {
724 perror("ERROR during stat");
725 return rc;
726 }
727
728 percent = (ftell(sw->stream) * 100) / st.st_size;
729 return percent;
730}
731
Harald Welte5e4d1b32009-02-01 13:36:56 +0000732/* Activate the specified software into the BTS */
733int abis_nm_software_activate(struct gsm_bts *bts, const char *fname,
734 gsm_cbfn *cbfn, void *cb_data)
735{
736 struct abis_nm_sw *sw = &g_sw;
737 int rc;
738
739 DEBUGP(DNM, "Activating Software (BTS %u, File \"%s\")\n",
740 bts->nr, fname);
741
742 if (sw->state != SW_STATE_NONE)
743 return -EBUSY;
744
745 sw->bts = bts;
746 sw->obj_class = NM_OC_SITE_MANAGER;
747 sw->obj_instance[0] = 0xff;
748 sw->obj_instance[1] = 0xff;
749 sw->obj_instance[2] = 0xff;
750 sw->state = SW_STATE_WAIT_ACTACK;
751 sw->cbfn = cbfn;
752 sw->cb_data = cb_data;
753
754 /* Open the file in order to fill some sw struct members */
755 rc = sw_open_file(sw, fname);
756 if (rc < 0) {
757 sw->state = SW_STATE_NONE;
758 return rc;
759 }
760 sw_close_file(sw);
761
762 return sw_activate(sw);
763}
764
Harald Welte8470bf22008-12-25 23:28:35 +0000765static void fill_nm_channel(struct abis_nm_channel *ch, u_int8_t bts_port,
Harald Welte52b1f982008-12-23 20:25:15 +0000766 u_int8_t ts_nr, u_int8_t subslot_nr)
767{
Harald Welteadaf08b2009-01-18 11:08:10 +0000768 ch->attrib = NM_ATT_ABIS_CHANNEL;
Harald Welte52b1f982008-12-23 20:25:15 +0000769 ch->bts_port = bts_port;
770 ch->timeslot = ts_nr;
771 ch->subslot = subslot_nr;
772}
773
774int abis_nm_establish_tei(struct gsm_bts *bts, u_int8_t trx_nr,
775 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot,
776 u_int8_t tei)
777{
778 struct abis_om_hdr *oh;
779 struct abis_nm_channel *ch;
Harald Welte702d8702008-12-26 20:25:35 +0000780 u_int8_t len = sizeof(*ch) + 2;
Harald Welte8470bf22008-12-25 23:28:35 +0000781 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000782
783 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
784 fill_om_fom_hdr(oh, len, NM_MT_ESTABLISH_TEI, NM_OC_RADIO_CARRIER,
785 bts->bts_nr, trx_nr, 0xff);
786
Harald Welte8470bf22008-12-25 23:28:35 +0000787 msgb_tv_put(msg, NM_ATT_TEI, tei);
Harald Welte52b1f982008-12-23 20:25:15 +0000788
789 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
790 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
791
792 return abis_nm_sendmsg(bts, msg);
793}
794
795/* connect signalling of one (BTS,TRX) to a particular timeslot on the E1 */
796int abis_nm_conn_terr_sign(struct gsm_bts_trx *trx,
797 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot)
798{
Harald Welte8470bf22008-12-25 23:28:35 +0000799 struct gsm_bts *bts = trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000800 struct abis_om_hdr *oh;
801 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000802 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000803
804 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000805 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_SIGN,
Harald Welte52b1f982008-12-23 20:25:15 +0000806 NM_OC_RADIO_CARRIER, bts->bts_nr, trx->nr, 0xff);
807
808 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
809 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
810
811 return abis_nm_sendmsg(bts, msg);
812}
813
814#if 0
815int abis_nm_disc_terr_sign(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
816 struct abis_nm_abis_channel *chan)
817{
818}
819#endif
820
821int abis_nm_conn_terr_traf(struct gsm_bts_trx_ts *ts,
822 u_int8_t e1_port, u_int8_t e1_timeslot,
823 u_int8_t e1_subslot)
824{
825 struct gsm_bts *bts = ts->trx->bts;
826 struct abis_om_hdr *oh;
827 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000828 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000829
830 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
831 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_TRAF,
832 NM_OC_BASEB_TRANSC, bts->bts_nr, ts->trx->nr, ts->nr);
833
834 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
835 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
836
837 return abis_nm_sendmsg(bts, msg);
838}
839
840#if 0
841int abis_nm_disc_terr_traf(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
842 struct abis_nm_abis_channel *chan,
843 u_int8_t subchan)
844{
845}
846#endif
847
848int abis_nm_set_channel_attr(struct gsm_bts_trx_ts *ts, u_int8_t chan_comb)
849{
850 struct gsm_bts *bts = ts->trx->bts;
851 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000852 u_int16_t arfcn = htons(ts->trx->arfcn);
Harald Welte52b1f982008-12-23 20:25:15 +0000853 u_int8_t zero = 0x00;
Harald Welte8470bf22008-12-25 23:28:35 +0000854 struct msgb *msg = nm_msgb_alloc();
Harald Welte702d8702008-12-26 20:25:35 +0000855 u_int8_t len = 4 + 2 + 2 + 2 + 2 +3;
Harald Welte52b1f982008-12-23 20:25:15 +0000856
857 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000858 fill_om_fom_hdr(oh, len, NM_MT_SET_CHAN_ATTR,
Harald Welte52b1f982008-12-23 20:25:15 +0000859 NM_OC_BASEB_TRANSC, bts->bts_nr,
860 ts->trx->nr, ts->nr);
861 /* FIXME: don't send ARFCN list, hopping sequence, mAIO, ...*/
862 msgb_tlv16_put(msg, NM_ATT_ARFCN_LIST, 1, &arfcn);
863 msgb_tv_put(msg, NM_ATT_CHAN_COMB, chan_comb);
864 msgb_tv_put(msg, NM_ATT_HSN, 0x00);
865 msgb_tv_put(msg, NM_ATT_MAIO, 0x00);
866 msgb_tv_put(msg, NM_ATT_TSC, 0x07); /* training sequence */
867 msgb_tlv_put(msg, 0x59, 1, &zero);
868
869 return abis_nm_sendmsg(bts, msg);
870}
871
Harald Welte8470bf22008-12-25 23:28:35 +0000872int abis_nm_raw_msg(struct gsm_bts *bts, int len, u_int8_t *rawmsg)
Harald Welte52b1f982008-12-23 20:25:15 +0000873{
Harald Welte8470bf22008-12-25 23:28:35 +0000874 struct msgb *msg = nm_msgb_alloc();
875 struct abis_om_hdr *oh;
Harald Welte52b1f982008-12-23 20:25:15 +0000876 u_int8_t *data;
877
878 oh = (struct abis_om_hdr *) msgb_put(msg, sizeof(*oh));
879 fill_om_hdr(oh, len);
880 data = msgb_put(msg, len);
Harald Weltead384642008-12-26 10:20:07 +0000881 memcpy(data, rawmsg, len);
Harald Welte52b1f982008-12-23 20:25:15 +0000882
883 return abis_nm_sendmsg(bts, msg);
884}
885
886/* Siemens specific commands */
887static int __simple_cmd(struct gsm_bts *bts, u_int8_t msg_type)
888{
889 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000890 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000891
892 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000893 fill_om_fom_hdr(oh, 0, msg_type, NM_OC_SITE_MANAGER,
Harald Welte52b1f982008-12-23 20:25:15 +0000894 0xff, 0xff, 0xff);
895
896 return abis_nm_sendmsg(bts, msg);
897}
898
899int abis_nm_event_reports(struct gsm_bts *bts, int on)
900{
901 if (on == 0)
Harald Welte227d4072009-01-03 08:16:25 +0000902 return __simple_cmd(bts, NM_MT_STOP_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000903 else
Harald Welte227d4072009-01-03 08:16:25 +0000904 return __simple_cmd(bts, NM_MT_REST_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000905}
906
Harald Welte3ffd1372009-02-01 22:15:49 +0000907
Harald Welte47d88ae2009-01-04 12:02:08 +0000908/* Siemens (or BS-11) specific commands */
909
Harald Welte3ffd1372009-02-01 22:15:49 +0000910int abis_nm_bs11_bsc_disconnect(struct gsm_bts *bts, int reconnect)
911{
912 if (reconnect == 0)
913 return __simple_cmd(bts, NM_MT_BS11_DISCONNECT);
914 else
915 return __simple_cmd(bts, NM_MT_BS11_RECONNECT);
916}
917
Harald Welteb8427972009-02-05 19:27:17 +0000918int abis_nm_bs11_restart(struct gsm_bts *bts)
919{
920 return __simple_cmd(bts, NM_MT_BS11_RESTART);
921}
922
923
Harald Welte268bb402009-02-01 19:11:56 +0000924struct bs11_date_time {
925 u_int16_t year;
926 u_int8_t month;
927 u_int8_t day;
928 u_int8_t hour;
929 u_int8_t min;
930 u_int8_t sec;
931} __attribute__((packed));
932
933
934void get_bs11_date_time(struct bs11_date_time *aet)
935{
936 time_t t;
937 struct tm *tm;
938
939 t = time(NULL);
940 tm = localtime(&t);
941 aet->sec = tm->tm_sec;
942 aet->min = tm->tm_min;
943 aet->hour = tm->tm_hour;
944 aet->day = tm->tm_mday;
945 aet->month = tm->tm_mon;
946 aet->year = htons(1900 + tm->tm_year);
947}
948
Harald Welte05188ee2009-01-18 11:39:08 +0000949int abis_nm_bs11_reset_resource(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000950{
Harald Welte4668fda2009-01-03 08:19:29 +0000951 return __simple_cmd(bts, NM_MT_BS11_RESET_RESOURCE);
Harald Welte52b1f982008-12-23 20:25:15 +0000952}
953
Harald Welte05188ee2009-01-18 11:39:08 +0000954int abis_nm_bs11_db_transmission(struct gsm_bts *bts, int begin)
Harald Welte52b1f982008-12-23 20:25:15 +0000955{
956 if (begin)
Harald Welte4668fda2009-01-03 08:19:29 +0000957 return __simple_cmd(bts, NM_MT_BS11_BEGIN_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000958 else
Harald Welte4668fda2009-01-03 08:19:29 +0000959 return __simple_cmd(bts, NM_MT_BS11_END_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000960}
Harald Welte47d88ae2009-01-04 12:02:08 +0000961
Harald Welte05188ee2009-01-18 11:39:08 +0000962int abis_nm_bs11_create_object(struct gsm_bts *bts,
Harald Welte1bc09062009-01-18 14:17:52 +0000963 enum abis_bs11_objtype type, u_int8_t idx,
964 u_int8_t attr_len, const u_int8_t *attr)
Harald Welte47d88ae2009-01-04 12:02:08 +0000965{
966 struct abis_om_hdr *oh;
967 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000968 u_int8_t *cur;
Harald Welte47d88ae2009-01-04 12:02:08 +0000969
970 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000971 fill_om_fom_hdr(oh, attr_len, NM_MT_BS11_CREATE_OBJ,
Harald Welte268bb402009-02-01 19:11:56 +0000972 NM_OC_BS11, type, 0, idx);
Harald Welte1bc09062009-01-18 14:17:52 +0000973 cur = msgb_put(msg, attr_len);
974 memcpy(cur, attr, attr_len);
Harald Welte47d88ae2009-01-04 12:02:08 +0000975
976 return abis_nm_sendmsg(bts, msg);
977}
978
Harald Welte05188ee2009-01-18 11:39:08 +0000979int abis_nm_bs11_create_envaBTSE(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000980{
981 struct abis_om_hdr *oh;
982 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000983 u_int8_t zero = 0x00;
Harald Welte47d88ae2009-01-04 12:02:08 +0000984
985 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000986 fill_om_fom_hdr(oh, 3, NM_MT_BS11_CREATE_OBJ,
Harald Welte1bc09062009-01-18 14:17:52 +0000987 NM_OC_BS11_ENVABTSE, 0, idx, 0xff);
988 msgb_tlv_put(msg, 0x99, 1, &zero);
Harald Welte47d88ae2009-01-04 12:02:08 +0000989
990 return abis_nm_sendmsg(bts, msg);
991}
992
Harald Welte05188ee2009-01-18 11:39:08 +0000993int abis_nm_bs11_create_bport(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000994{
995 struct abis_om_hdr *oh;
996 struct msgb *msg = nm_msgb_alloc();
997
998 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
999 fill_om_fom_hdr(oh, 0, NM_MT_BS11_CREATE_OBJ, NM_OC_BS11_BPORT,
1000 idx, 0, 0);
1001
1002 return abis_nm_sendmsg(bts, msg);
1003}
Harald Welte05188ee2009-01-18 11:39:08 +00001004
1005int abis_nm_bs11_set_oml_tei(struct gsm_bts *bts, u_int8_t tei)
1006{
1007 struct abis_om_hdr *oh;
1008 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +00001009
1010 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001011 fill_om_fom_hdr(oh, 2, NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
Harald Welte05188ee2009-01-18 11:39:08 +00001012 0xff, 0xff, 0xff);
1013 msgb_tv_put(msg, NM_ATT_TEI, tei);
1014
1015 return abis_nm_sendmsg(bts, msg);
1016}
1017
1018/* like abis_nm_conn_terr_traf */
1019int abis_nm_bs11_conn_oml(struct gsm_bts *bts, u_int8_t e1_port,
1020 u_int8_t e1_timeslot, u_int8_t e1_subslot)
1021{
1022 struct abis_om_hdr *oh;
1023 struct abis_nm_channel *ch;
1024 struct msgb *msg = nm_msgb_alloc();
1025
1026 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte05188ee2009-01-18 11:39:08 +00001027 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_BS11_SET_ATTR,
1028 NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff);
1029
1030 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
1031 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
1032
1033 return abis_nm_sendmsg(bts, msg);
1034}
1035
1036int abis_nm_bs11_set_trx_power(struct gsm_bts_trx *trx, u_int8_t level)
1037{
1038 struct abis_om_hdr *oh;
1039 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +00001040
1041 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001042 fill_om_fom_hdr(oh, 3, NM_MT_BS11_SET_ATTR,
Harald Welte05188ee2009-01-18 11:39:08 +00001043 NM_OC_BS11, BS11_OBJ_PA, 0x00, trx->nr);
1044 msgb_tlv_put(msg, NM_ATT_BS11_TXPWR, 1, &level);
1045
1046 return abis_nm_sendmsg(trx->bts, msg);
1047}
1048
Harald Welte268bb402009-02-01 19:11:56 +00001049//static const u_int8_t bs11_logon_c7[] = { 0x07, 0xd9, 0x01, 0x11, 0x0d, 0x10, 0x20 };
Harald Weltebb151312009-01-28 20:42:07 +00001050static const u_int8_t bs11_logon_c8[] = { 0x02 };
Harald Welte05188ee2009-01-18 11:39:08 +00001051static const u_int8_t bs11_logon_c9[] = "FACTORY";
1052
Harald Welte1bc09062009-01-18 14:17:52 +00001053int abis_nm_bs11_factory_logon(struct gsm_bts *bts, int on)
Harald Welte05188ee2009-01-18 11:39:08 +00001054{
1055 struct abis_om_hdr *oh;
1056 struct msgb *msg = nm_msgb_alloc();
Harald Welte268bb402009-02-01 19:11:56 +00001057 struct bs11_date_time bdt;
1058
1059 get_bs11_date_time(&bdt);
Harald Welte05188ee2009-01-18 11:39:08 +00001060
1061 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte1bc09062009-01-18 14:17:52 +00001062 if (on) {
Harald Welte268bb402009-02-01 19:11:56 +00001063 u_int8_t len = 3*2 + sizeof(bdt)
Harald Welte6f676a32009-01-18 14:27:48 +00001064 + sizeof(bs11_logon_c8) + sizeof(bs11_logon_c9);
Harald Welte043d04a2009-01-29 23:15:30 +00001065 fill_om_fom_hdr(oh, len, NM_MT_BS11_LMT_LOGON,
Harald Welte1bc09062009-01-18 14:17:52 +00001066 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
Harald Welte043d04a2009-01-29 23:15:30 +00001067 msgb_tlv_put(msg, NM_ATT_BS11_LMT_LOGIN_TIME,
Harald Welte5083b0b2009-02-02 19:20:52 +00001068 sizeof(bdt), (u_int8_t *) &bdt);
Harald Welte043d04a2009-01-29 23:15:30 +00001069 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_ACC_LEV,
1070 sizeof(bs11_logon_c8), bs11_logon_c8);
1071 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_NAME,
1072 sizeof(bs11_logon_c9), bs11_logon_c9);
Harald Welte1bc09062009-01-18 14:17:52 +00001073 } else {
Harald Welte5e4d1b32009-02-01 13:36:56 +00001074 fill_om_fom_hdr(oh, 0, NM_MT_BS11_LMT_LOGOFF,
Harald Welte1bc09062009-01-18 14:17:52 +00001075 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
1076 }
Harald Welte05188ee2009-01-18 11:39:08 +00001077
1078 return abis_nm_sendmsg(bts, msg);
1079}
Harald Welte1bc09062009-01-18 14:17:52 +00001080
1081int abis_nm_bs11_set_trx1_pw(struct gsm_bts *bts, const char *password)
1082{
1083 struct abis_om_hdr *oh;
1084 struct msgb *msg;
1085
1086 if (strlen(password) != 10)
1087 return -EINVAL;
1088
1089 msg = nm_msgb_alloc();
1090 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001091 fill_om_fom_hdr(oh, 2+strlen(password), NM_MT_BS11_SET_ATTR,
Harald Welte1bc09062009-01-18 14:17:52 +00001092 NM_OC_BS11, BS11_OBJ_TRX1, 0x00, 0x00);
1093 msgb_tlv_put(msg, NM_ATT_BS11_PASSWORD, 10, (const u_int8_t *)password);
1094
1095 return abis_nm_sendmsg(bts, msg);
1096}
1097
1098int abis_nm_bs11_get_state(struct gsm_bts *bts)
1099{
1100 return __simple_cmd(bts, NM_MT_BS11_GET_STATE);
1101}
Harald Welte5e4d1b32009-02-01 13:36:56 +00001102
1103/* BS11 SWL */
1104
1105struct abis_nm_bs11_sw {
1106 struct gsm_bts *bts;
1107 char swl_fname[PATH_MAX];
1108 u_int8_t win_size;
Harald Welte3ffd1372009-02-01 22:15:49 +00001109 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001110 struct llist_head file_list;
1111 gsm_cbfn *user_cb; /* specified by the user */
1112};
1113static struct abis_nm_bs11_sw _g_bs11_sw, *g_bs11_sw = &_g_bs11_sw;
1114
1115struct file_list_entry {
1116 struct llist_head list;
1117 char fname[PATH_MAX];
1118};
1119
1120struct file_list_entry *fl_dequeue(struct llist_head *queue)
1121{
1122 struct llist_head *lh;
1123
1124 if (llist_empty(queue))
1125 return NULL;
1126
1127 lh = queue->next;
1128 llist_del(lh);
1129
1130 return llist_entry(lh, struct file_list_entry, list);
1131}
1132
1133static int bs11_read_swl_file(struct abis_nm_bs11_sw *bs11_sw)
1134{
1135 char linebuf[255];
1136 struct llist_head *lh, *lh2;
1137 FILE *swl;
1138 int rc = 0;
1139
1140 swl = fopen(bs11_sw->swl_fname, "r");
1141 if (!swl)
1142 return -ENODEV;
1143
1144 /* zero the stale file list, if any */
1145 llist_for_each_safe(lh, lh2, &bs11_sw->file_list) {
1146 llist_del(lh);
1147 free(lh);
1148 }
1149
1150 while (fgets(linebuf, sizeof(linebuf), swl)) {
1151 char file_id[12+1];
1152 char file_version[80+1];
1153 struct file_list_entry *fle;
1154 static char dir[PATH_MAX];
1155
1156 if (strlen(linebuf) < 4)
1157 continue;
Harald Welte3ffd1372009-02-01 22:15:49 +00001158
Harald Welte5e4d1b32009-02-01 13:36:56 +00001159 rc = sscanf(linebuf+4, "%12s:%80s\r\n", file_id, file_version);
1160 if (rc < 0) {
1161 perror("ERR parsing SWL file");
1162 rc = -EINVAL;
1163 goto out;
1164 }
1165 if (rc < 2)
1166 continue;
1167
1168 fle = malloc(sizeof(*fle));
1169 if (!fle) {
1170 rc = -ENOMEM;
1171 goto out;
1172 }
1173 memset(fle, 0, sizeof(*fle));
1174
1175 /* construct new filename */
1176 strncpy(dir, bs11_sw->swl_fname, sizeof(dir));
1177 strncat(fle->fname, dirname(dir), sizeof(fle->fname) - 1);
1178 strcat(fle->fname, "/");
1179 strncat(fle->fname, file_id, sizeof(fle->fname) - 1 -strlen(fle->fname));
Harald Welte5e4d1b32009-02-01 13:36:56 +00001180
1181 llist_add_tail(&fle->list, &bs11_sw->file_list);
1182 }
1183
1184out:
1185 fclose(swl);
1186 return rc;
1187}
1188
1189/* bs11 swload specific callback, passed to abis_nm core swload */
1190static int bs11_swload_cbfn(unsigned int hook, unsigned int event,
1191 struct msgb *msg, void *data, void *param)
1192{
1193 struct abis_nm_bs11_sw *bs11_sw = data;
1194 struct file_list_entry *fle;
1195 int rc = 0;
1196
Harald Welte5e4d1b32009-02-01 13:36:56 +00001197 switch (event) {
1198 case NM_MT_LOAD_END_ACK:
1199 fle = fl_dequeue(&bs11_sw->file_list);
1200 if (fle) {
1201 /* start download the next file of our file list */
1202 rc = abis_nm_software_load(bs11_sw->bts, fle->fname,
1203 bs11_sw->win_size,
Harald Welte3ffd1372009-02-01 22:15:49 +00001204 bs11_sw->forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001205 &bs11_swload_cbfn, bs11_sw);
1206 free(fle);
1207 } else {
1208 /* activate the SWL */
1209 rc = abis_nm_software_activate(bs11_sw->bts,
1210 bs11_sw->swl_fname,
1211 bs11_swload_cbfn,
1212 bs11_sw);
1213 }
1214 break;
Harald Welte3ffd1372009-02-01 22:15:49 +00001215 case NM_MT_LOAD_SEG_ACK:
Harald Welte5e4d1b32009-02-01 13:36:56 +00001216 case NM_MT_LOAD_END_NACK:
1217 case NM_MT_LOAD_INIT_ACK:
1218 case NM_MT_LOAD_INIT_NACK:
1219 case NM_MT_ACTIVATE_SW_NACK:
1220 case NM_MT_ACTIVATE_SW_ACK:
1221 default:
1222 /* fallthrough to the user callback */
Harald Welte97ed1e72009-02-06 13:38:02 +00001223 if (bs11_sw->user_cb)
1224 rc = bs11_sw->user_cb(hook, event, msg, NULL, NULL);
Harald Welte5e4d1b32009-02-01 13:36:56 +00001225 break;
1226 }
1227
1228 return rc;
1229}
1230
1231/* Siemens provides a SWL file that is a mere listing of all the other
1232 * files that are part of a software release. We need to upload first
1233 * the list file, and then each file that is listed in the list file */
1234int abis_nm_bs11_load_swl(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +00001235 u_int8_t win_size, int forced, gsm_cbfn *cbfn)
Harald Welte5e4d1b32009-02-01 13:36:56 +00001236{
1237 struct abis_nm_bs11_sw *bs11_sw = g_bs11_sw;
1238 struct file_list_entry *fle;
1239 int rc = 0;
1240
1241 INIT_LLIST_HEAD(&bs11_sw->file_list);
1242 bs11_sw->bts = bts;
1243 bs11_sw->win_size = win_size;
1244 bs11_sw->user_cb = cbfn;
Harald Welte3ffd1372009-02-01 22:15:49 +00001245 bs11_sw->forced = forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001246
1247 strncpy(bs11_sw->swl_fname, fname, sizeof(bs11_sw->swl_fname));
1248 rc = bs11_read_swl_file(bs11_sw);
1249 if (rc < 0)
1250 return rc;
1251
1252 /* dequeue next item in file list */
1253 fle = fl_dequeue(&bs11_sw->file_list);
1254 if (!fle)
1255 return -EINVAL;
1256
1257 /* start download the next file of our file list */
Harald Welte3ffd1372009-02-01 22:15:49 +00001258 rc = abis_nm_software_load(bts, fle->fname, win_size, forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001259 bs11_swload_cbfn, bs11_sw);
1260 free(fle);
1261 return rc;
1262}
1263
Harald Welte5083b0b2009-02-02 19:20:52 +00001264#if 0
Harald Welte5e4d1b32009-02-01 13:36:56 +00001265static u_int8_t req_attr_btse[] = {
1266 NM_ATT_ADM_STATE, NM_ATT_BS11_LMT_LOGON_SESSION,
1267 NM_ATT_BS11_LMT_LOGIN_TIME, NM_ATT_BS11_LMT_USER_ACC_LEV,
1268 NM_ATT_BS11_LMT_USER_NAME,
1269
1270 0xaf, NM_ATT_BS11_RX_OFFSET, NM_ATT_BS11_VENDOR_NAME,
1271
1272 NM_ATT_BS11_SW_LOAD_INTENDED, NM_ATT_BS11_SW_LOAD_SAFETY,
1273
1274 NM_ATT_BS11_SW_LOAD_STORED };
1275
1276static u_int8_t req_attr_btsm[] = {
1277 NM_ATT_ABIS_CHANNEL, NM_ATT_TEI, NM_ATT_BS11_ABIS_EXT_TIME,
1278 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xce, NM_ATT_FILE_ID,
1279 NM_ATT_FILE_VERSION, NM_ATT_OPER_STATE, 0xe8, NM_ATT_BS11_ALL_TEST_CATG,
1280 NM_ATT_SW_DESCR, NM_ATT_GET_ARI };
Harald Welte5083b0b2009-02-02 19:20:52 +00001281#endif
Harald Welte5e4d1b32009-02-01 13:36:56 +00001282
1283static u_int8_t req_attr[] = {
1284 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xa8, NM_ATT_OPER_STATE,
1285 0xd5, 0xa1, NM_ATT_BS11_ESN_FW_CODE_NO, NM_ATT_BS11_ESN_HW_CODE_NO,
1286 0x42, NM_ATT_BS11_ESN_PCB_SERIAL };
1287
1288int abis_nm_bs11_get_serno(struct gsm_bts *bts)
1289{
1290 struct abis_om_hdr *oh;
1291 struct msgb *msg = nm_msgb_alloc();
1292
1293 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1294 /* SiemensHW CCTRL object */
1295 fill_om_fom_hdr(oh, 2+sizeof(req_attr), NM_MT_GET_ATTR, NM_OC_BS11,
1296 0x03, 0x00, 0x00);
1297 msgb_tlv_put(msg, NM_ATT_LIST_REQ_ATTR, sizeof(req_attr), req_attr);
1298
1299 return abis_nm_sendmsg(bts, msg);
1300}
Harald Welte268bb402009-02-01 19:11:56 +00001301
1302int abis_nm_bs11_set_ext_time(struct gsm_bts *bts)
1303{
1304 struct abis_om_hdr *oh;
1305 struct msgb *msg = nm_msgb_alloc();
1306 struct bs11_date_time aet;
1307
1308 get_bs11_date_time(&aet);
1309 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1310 /* SiemensHW CCTRL object */
1311 fill_om_fom_hdr(oh, 2+sizeof(aet), NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
1312 0xff, 0xff, 0xff);
Harald Welte5083b0b2009-02-02 19:20:52 +00001313 msgb_tlv_put(msg, NM_ATT_BS11_ABIS_EXT_TIME, sizeof(aet), (u_int8_t *) &aet);
Harald Welte268bb402009-02-01 19:11:56 +00001314
1315 return abis_nm_sendmsg(bts, msg);
1316}