blob: 28057607de5375414da775919a3527d503cab6ee [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 Welte52b1f982008-12-23 20:25:15 +0000171/* Receive a OML NM Message from BTS */
Harald Welte8470bf22008-12-25 23:28:35 +0000172static int abis_nm_rcvmsg_fom(struct msgb *mb)
Harald Welte52b1f982008-12-23 20:25:15 +0000173{
174 struct abis_om_fom_hdr *foh = msgb_l3(mb);
175 u_int8_t mt = foh->msg_type;
176
177 /* check for unsolicited message */
178 if (is_report(mt)) {
Harald Weltead384642008-12-26 10:20:07 +0000179 DEBUGP(DNM, "reporting NM MT 0x%02x\n", mt);
180 //nmh->cfg->report_cb(mb, foh);
Harald Welte52b1f982008-12-23 20:25:15 +0000181 return 0;
182 }
183
Harald Welte4724f992009-01-18 18:01:49 +0000184 if (is_in_arr(mt, sw_load_msgs, ARRAY_SIZE(sw_load_msgs)))
185 return abis_nm_rcvmsg_sw(mb);
186
Harald Weltead384642008-12-26 10:20:07 +0000187#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000188 /* check if last message is to be acked */
189 if (is_ack_nack(nmh->last_msgtype)) {
190 if (mt == MT_ACK(nmh->last_msgtype)) {
191 fprintf(stderr, "received ACK (0x%x)\n",
192 foh->msg_type);
193 /* we got our ACK, continue sending the next msg */
194 } else if (mt == MT_NACK(nmh->last_msgtype)) {
195 /* we got a NACK, signal this to the caller */
196 fprintf(stderr, "received NACK (0x%x)\n",
197 foh->msg_type);
198 /* FIXME: somehow signal this to the caller */
199 } else {
200 /* really strange things happen */
201 return -EINVAL;
202 }
203 }
Harald Weltead384642008-12-26 10:20:07 +0000204#endif
205
206 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000207}
208
209/* High-Level API */
210/* Entry-point where L2 OML from BTS enters the NM code */
Harald Welte8470bf22008-12-25 23:28:35 +0000211int abis_nm_rcvmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000212{
213 int rc;
214 struct abis_om_hdr *oh = msgb_l2(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000215
216 /* Various consistency checks */
217 if (oh->placement != ABIS_OM_PLACEMENT_ONLY) {
218 fprintf(stderr, "ABIS OML placement 0x%x not supported\n",
219 oh->placement);
220 return -EINVAL;
221 }
222 if (oh->sequence != 0) {
223 fprintf(stderr, "ABIS OML sequence 0x%x != 0x00\n",
224 oh->sequence);
225 return -EINVAL;
226 }
Harald Welte702d8702008-12-26 20:25:35 +0000227#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000228 unsigned int l2_len = msg->tail - (u_int8_t *)msgb_l2(msg);
229 unsigned int hlen = sizeof(*oh) + sizeof(struct abis_om_fom_hdr);
Harald Welte702d8702008-12-26 20:25:35 +0000230 if (oh->length + hlen > l2_len) {
Harald Welte52b1f982008-12-23 20:25:15 +0000231 fprintf(stderr, "ABIS OML truncated message (%u > %u)\n",
232 oh->length + sizeof(*oh), l2_len);
233 return -EINVAL;
234 }
Harald Welte702d8702008-12-26 20:25:35 +0000235 if (oh->length + hlen < l2_len)
236 fprintf(stderr, "ABIS OML message with extra trailer?!? (oh->len=%d, sizeof_oh=%d l2_len=%d\n", oh->length, sizeof(*oh), l2_len);
237#endif
Harald Weltead384642008-12-26 10:20:07 +0000238 msg->l3h = (unsigned char *)oh + sizeof(*oh);
Harald Welte52b1f982008-12-23 20:25:15 +0000239
240 switch (oh->mdisc) {
241 case ABIS_OM_MDISC_FOM:
Harald Welte8470bf22008-12-25 23:28:35 +0000242 rc = abis_nm_rcvmsg_fom(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000243 break;
244 case ABIS_OM_MDISC_MMI:
245 case ABIS_OM_MDISC_TRAU:
246 case ABIS_OM_MDISC_MANUF:
247 default:
248 fprintf(stderr, "unknown ABIS OML message discriminator 0x%x\n",
249 oh->mdisc);
250 return -EINVAL;
251 }
252
Harald Weltead384642008-12-26 10:20:07 +0000253 msgb_free(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000254 return rc;
255}
256
257#if 0
258/* initialized all resources */
259struct abis_nm_h *abis_nm_init(struct abis_nm_cfg *cfg)
260{
261 struct abis_nm_h *nmh;
262
263 nmh = malloc(sizeof(*nmh));
264 if (!nmh)
265 return NULL;
266
267 nmh->cfg = cfg;
268
269 return nmh;
270}
271
272/* free all resources */
273void abis_nm_fini(struct abis_nm_h *nmh)
274{
275 free(nmh);
276}
277#endif
278
279/* Here we are trying to define a high-level API that can be used by
280 * the actual BSC implementation. However, the architecture is currently
281 * still under design. Ideally the calls to this API would be synchronous,
282 * while the underlying stack behind the APi runs in a traditional select
283 * based state machine.
284 */
285
Harald Welte4724f992009-01-18 18:01:49 +0000286/* 6.2 Software Load: */
287enum sw_state {
288 SW_STATE_NONE,
289 SW_STATE_WAIT_INITACK,
290 SW_STATE_WAIT_SEGACK,
291 SW_STATE_WAIT_ENDACK,
292 SW_STATE_WAIT_ACTACK,
293 SW_STATE_ERROR,
294};
Harald Welte52b1f982008-12-23 20:25:15 +0000295
Harald Welte52b1f982008-12-23 20:25:15 +0000296struct abis_nm_sw {
Harald Welte4724f992009-01-18 18:01:49 +0000297 struct gsm_bts *bts;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000298 gsm_cbfn *cbfn;
299 void *cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000300 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000301
Harald Welte52b1f982008-12-23 20:25:15 +0000302 /* this will become part of the SW LOAD INITIATE */
303 u_int8_t obj_class;
304 u_int8_t obj_instance[3];
Harald Welte4724f992009-01-18 18:01:49 +0000305
306 u_int8_t file_id[255];
307 u_int8_t file_id_len;
308
309 u_int8_t file_version[255];
310 u_int8_t file_version_len;
311
312 u_int8_t window_size;
313 u_int8_t seg_in_window;
314
315 int fd;
316 FILE *stream;
317 enum sw_state state;
Harald Welte1602ade2009-01-29 21:12:39 +0000318 int last_seg;
Harald Welte52b1f982008-12-23 20:25:15 +0000319};
320
Harald Welte4724f992009-01-18 18:01:49 +0000321static struct abis_nm_sw g_sw;
322
323/* 6.2.1 / 8.3.1: Load Data Initiate */
324static int sw_load_init(struct abis_nm_sw *sw)
Harald Welte52b1f982008-12-23 20:25:15 +0000325{
Harald Welte4724f992009-01-18 18:01:49 +0000326 struct abis_om_hdr *oh;
327 struct msgb *msg = nm_msgb_alloc();
328 u_int8_t len = 3*2 + sw->file_id_len + sw->file_version_len;
329
330 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
331 fill_om_fom_hdr(oh, len, NM_MT_LOAD_INIT, sw->obj_class,
332 sw->obj_instance[0], sw->obj_instance[1],
333 sw->obj_instance[2]);
334
335 /* FIXME: this is BS11 specific format */
336 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
337 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
338 sw->file_version);
339 msgb_tv_put(msg, NM_ATT_WINDOW_SIZE, sw->window_size);
340
341 return abis_nm_sendmsg(sw->bts, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000342}
343
Harald Welte1602ade2009-01-29 21:12:39 +0000344static int is_last_line(FILE *stream)
345{
346 char next_seg_buf[256];
347 long pos;
348
349 /* check if we're sending the last line */
350 pos = ftell(stream);
351 if (!fgets(next_seg_buf, sizeof(next_seg_buf)-2, stream)) {
352 fseek(stream, pos, SEEK_SET);
353 return 1;
354 }
355
356 fseek(stream, pos, SEEK_SET);
357 return 0;
358}
359
Harald Welte4724f992009-01-18 18:01:49 +0000360/* 6.2.2 / 8.3.2 Load Data Segment */
361static int sw_load_segment(struct abis_nm_sw *sw)
362{
363 struct abis_om_hdr *oh;
364 struct msgb *msg = nm_msgb_alloc();
365 char seg_buf[256];
366 char *line_buf = seg_buf+2;
Harald Welte3b8ba212009-01-29 12:27:58 +0000367 unsigned char *tlv;
Harald Welte4724f992009-01-18 18:01:49 +0000368 u_int8_t len;
Harald Welte4724f992009-01-18 18:01:49 +0000369
370 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte3b8ba212009-01-29 12:27:58 +0000371
372 switch (sw->bts->type) {
373 case GSM_BTS_TYPE_BS11:
374 if (fgets(line_buf, sizeof(seg_buf)-2, sw->stream) == NULL) {
375 perror("fgets reading segment");
376 return -EINVAL;
377 }
378 seg_buf[0] = 0x00;
Harald Welte1602ade2009-01-29 21:12:39 +0000379
380 /* check if we're sending the last line */
381 sw->last_seg = is_last_line(sw->stream);
382 if (sw->last_seg)
383 seg_buf[1] = 0;
384 else
385 seg_buf[1] = 1 + sw->seg_in_window++;
Harald Welte3b8ba212009-01-29 12:27:58 +0000386
387 len = strlen(line_buf) + 2;
388 tlv = msgb_put(msg, TLV_GROSS_LEN(len));
389 tlv_put(tlv, NM_ATT_BS11_FILE_DATA, len, (u_int8_t *)seg_buf);
390 /* BS11 wants CR + LF in excess of the TLV length !?! */
391 tlv[1] -= 2;
392
393 /* we only now know the exact length for the OM hdr */
394 len = strlen(line_buf)+2;
395 break;
396 default:
397 /* FIXME: Other BTS types */
398 return -1;
Harald Welte4724f992009-01-18 18:01:49 +0000399 }
Harald Welte4724f992009-01-18 18:01:49 +0000400
Harald Welte4724f992009-01-18 18:01:49 +0000401 fill_om_fom_hdr(oh, len, NM_MT_LOAD_SEG, sw->obj_class,
402 sw->obj_instance[0], sw->obj_instance[1],
403 sw->obj_instance[2]);
404
405 return abis_nm_sendmsg(sw->bts, msg);
406}
407
408/* 6.2.4 / 8.3.4 Load Data End */
409static int sw_load_end(struct abis_nm_sw *sw)
410{
411 struct abis_om_hdr *oh;
412 struct msgb *msg = nm_msgb_alloc();
413 u_int8_t len = 2*2 + sw->file_id_len + sw->file_version_len;
414
415 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
416 fill_om_fom_hdr(oh, len, NM_MT_LOAD_END, sw->obj_class,
417 sw->obj_instance[0], sw->obj_instance[1],
418 sw->obj_instance[2]);
419
420 /* FIXME: this is BS11 specific format */
421 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
422 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
423 sw->file_version);
424
425 return abis_nm_sendmsg(sw->bts, msg);
426}
Harald Welte5e4d1b32009-02-01 13:36:56 +0000427
Harald Welte52b1f982008-12-23 20:25:15 +0000428/* Activate the specified software into the BTS */
Harald Welte4724f992009-01-18 18:01:49 +0000429static int sw_activate(struct abis_nm_sw *sw)
Harald Welte52b1f982008-12-23 20:25:15 +0000430{
Harald Welte4724f992009-01-18 18:01:49 +0000431 struct abis_om_hdr *oh;
432 struct msgb *msg = nm_msgb_alloc();
433 u_int8_t len = 2*2 + sw->file_id_len + sw->file_version_len;
Harald Welte52b1f982008-12-23 20:25:15 +0000434
Harald Welte4724f992009-01-18 18:01:49 +0000435 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
436 fill_om_fom_hdr(oh, len, NM_MT_ACTIVATE_SW, sw->obj_class,
437 sw->obj_instance[0], sw->obj_instance[1],
438 sw->obj_instance[2]);
439
440 /* FIXME: this is BS11 specific format */
441 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
442 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
443 sw->file_version);
444
445 return abis_nm_sendmsg(sw->bts, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000446}
Harald Welte4724f992009-01-18 18:01:49 +0000447
448static int sw_open_file(struct abis_nm_sw *sw, const char *fname)
449{
450 char file_id[12+1];
451 char file_version[80+1];
452 int rc;
453
454 sw->fd = open(fname, O_RDONLY);
455 if (sw->fd < 0)
456 return sw->fd;
457
458 switch (sw->bts->type) {
459 case GSM_BTS_TYPE_BS11:
460 sw->stream = fdopen(sw->fd, "r");
461 if (!sw->stream) {
462 perror("fdopen");
463 return -1;
464 }
465 /* read first line and parse file ID and VERSION */
Harald Welte3b8ba212009-01-29 12:27:58 +0000466 rc = fscanf(sw->stream, "@(#)%12s:%80s\r\n",
Harald Welte4724f992009-01-18 18:01:49 +0000467 file_id, file_version);
468 if (rc != 2) {
469 perror("parsing header line of software file");
470 return -1;
471 }
472 strcpy((char *)sw->file_id, file_id);
473 sw->file_id_len = strlen(file_id);
474 strcpy((char *)sw->file_version, file_version);
475 sw->file_version_len = strlen(file_version);
476 /* rewind to start of file */
Harald Welte3b8ba212009-01-29 12:27:58 +0000477 rewind(sw->stream);
Harald Welte4724f992009-01-18 18:01:49 +0000478 break;
479 default:
480 /* We don't know how to treat them yet */
481 close(sw->fd);
482 return -EINVAL;
483 }
484
485 return 0;
486}
487
488static void sw_close_file(struct abis_nm_sw *sw)
489{
490 switch (sw->bts->type) {
491 case GSM_BTS_TYPE_BS11:
492 fclose(sw->stream);
493 break;
494 default:
495 close(sw->fd);
496 break;
497 }
498}
499
500/* Fill the window */
501static int sw_fill_window(struct abis_nm_sw *sw)
502{
503 int rc;
504
505 while (sw->seg_in_window < sw->window_size) {
506 rc = sw_load_segment(sw);
507 if (rc < 0)
508 return rc;
Harald Welte1602ade2009-01-29 21:12:39 +0000509 if (sw->last_seg)
510 break;
Harald Welte4724f992009-01-18 18:01:49 +0000511 }
512 return 0;
513}
514
515/* callback function from abis_nm_rcvmsg() handler */
516static int abis_nm_rcvmsg_sw(struct msgb *mb)
517{
518 struct abis_om_fom_hdr *foh = msgb_l3(mb);
519 int rc = -1;
520 struct abis_nm_sw *sw = &g_sw;
521 enum sw_state old_state = sw->state;
522
Harald Welte3ffd1372009-02-01 22:15:49 +0000523 //DEBUGP(DNM, "state %u, NM MT 0x%02x\n", sw->state, foh->msg_type);
Harald Welte4724f992009-01-18 18:01:49 +0000524
525 switch (sw->state) {
526 case SW_STATE_WAIT_INITACK:
527 switch (foh->msg_type) {
528 case NM_MT_LOAD_INIT_ACK:
529 /* fill window with segments */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000530 if (sw->cbfn)
531 sw->cbfn(GSM_HOOK_NM_SWLOAD,
532 NM_MT_LOAD_INIT_ACK, mb,
533 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000534 rc = sw_fill_window(sw);
535 sw->state = SW_STATE_WAIT_SEGACK;
536 break;
537 case NM_MT_LOAD_INIT_NACK:
Harald Welte3ffd1372009-02-01 22:15:49 +0000538 if (sw->forced) {
539 DEBUGP(DNM, "FORCED: Ignoring Software Load "
540 "Init NACK\n");
541 if (sw->cbfn)
542 sw->cbfn(GSM_HOOK_NM_SWLOAD,
543 NM_MT_LOAD_INIT_ACK, mb,
544 sw->cb_data, NULL);
545 rc = sw_fill_window(sw);
546 sw->state = SW_STATE_WAIT_SEGACK;
547 } else {
548 DEBUGP(DNM, "Software Load Init NACK\n");
549 if (sw->cbfn)
550 sw->cbfn(GSM_HOOK_NM_SWLOAD,
551 NM_MT_LOAD_INIT_NACK, mb,
552 sw->cb_data, NULL);
553 sw->state = SW_STATE_ERROR;
554 }
Harald Welte4724f992009-01-18 18:01:49 +0000555 break;
556 }
557 break;
558 case SW_STATE_WAIT_SEGACK:
559 switch (foh->msg_type) {
560 case NM_MT_LOAD_SEG_ACK:
Harald Welte3ffd1372009-02-01 22:15:49 +0000561 if (sw->cbfn)
562 sw->cbfn(GSM_HOOK_NM_SWLOAD,
563 NM_MT_LOAD_SEG_ACK, mb,
564 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000565 sw->seg_in_window = 0;
Harald Welte1602ade2009-01-29 21:12:39 +0000566 if (!sw->last_seg) {
567 /* fill window with more segments */
568 rc = sw_fill_window(sw);
569 sw->state = SW_STATE_WAIT_SEGACK;
570 } else {
571 /* end the transfer */
572 sw->state = SW_STATE_WAIT_ENDACK;
573 rc = sw_load_end(sw);
574 }
Harald Welte4724f992009-01-18 18:01:49 +0000575 break;
576 }
577 break;
578 case SW_STATE_WAIT_ENDACK:
579 switch (foh->msg_type) {
580 case NM_MT_LOAD_END_ACK:
581 sw_close_file(sw);
Harald Welte5e4d1b32009-02-01 13:36:56 +0000582 DEBUGP(DNM, "Software Load End (BTS %u)\n",
583 sw->bts->nr);
584 sw->state = SW_STATE_NONE;
585 if (sw->cbfn)
586 sw->cbfn(GSM_HOOK_NM_SWLOAD,
587 NM_MT_LOAD_END_ACK, mb,
588 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000589 break;
590 case NM_MT_LOAD_END_NACK:
Harald Welte1602ade2009-01-29 21:12:39 +0000591 DEBUGP(DNM, "Software Load End NACK\n");
Harald Welte4724f992009-01-18 18:01:49 +0000592 sw->state = SW_STATE_ERROR;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000593 if (sw->cbfn)
594 sw->cbfn(GSM_HOOK_NM_SWLOAD,
595 NM_MT_LOAD_END_NACK, mb,
596 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000597 break;
598 }
599 case SW_STATE_WAIT_ACTACK:
600 switch (foh->msg_type) {
601 case NM_MT_ACTIVATE_SW_ACK:
602 /* we're done */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000603 DEBUGP(DNM, "Activate Software DONE!\n");
Harald Welte4724f992009-01-18 18:01:49 +0000604 sw->state = SW_STATE_NONE;
605 rc = 0;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000606 if (sw->cbfn)
607 sw->cbfn(GSM_HOOK_NM_SWLOAD,
608 NM_MT_ACTIVATE_SW_ACK, mb,
609 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000610 break;
611 case NM_MT_ACTIVATE_SW_NACK:
Harald Welte1602ade2009-01-29 21:12:39 +0000612 DEBUGP(DNM, "Activate Software NACK\n");
Harald Welte4724f992009-01-18 18:01:49 +0000613 sw->state = SW_STATE_ERROR;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000614 if (sw->cbfn)
615 sw->cbfn(GSM_HOOK_NM_SWLOAD,
616 NM_MT_ACTIVATE_SW_NACK, mb,
617 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000618 break;
619 }
620 case SW_STATE_NONE:
621 case SW_STATE_ERROR:
622 break;
623 }
624
625 if (rc)
626 fprintf(stderr, "unexpected NM MT 0x%02x in state %u -> %u\n",
627 foh->msg_type, old_state, sw->state);
628
629 return rc;
630}
631
632/* Load the specified software into the BTS */
633int abis_nm_software_load(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +0000634 u_int8_t win_size, int forced,
635 gsm_cbfn *cbfn, void *cb_data)
Harald Welte4724f992009-01-18 18:01:49 +0000636{
637 struct abis_nm_sw *sw = &g_sw;
638 int rc;
639
Harald Welte5e4d1b32009-02-01 13:36:56 +0000640 DEBUGP(DNM, "Software Load (BTS %u, File \"%s\")\n",
641 bts->nr, fname);
642
Harald Welte4724f992009-01-18 18:01:49 +0000643 if (sw->state != SW_STATE_NONE)
644 return -EBUSY;
645
646 sw->bts = bts;
647 sw->obj_class = NM_OC_SITE_MANAGER;
648 sw->obj_instance[0] = 0xff;
649 sw->obj_instance[1] = 0xff;
650 sw->obj_instance[2] = 0xff;
651 sw->window_size = win_size;
652 sw->state = SW_STATE_WAIT_INITACK;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000653 sw->cbfn = cbfn;
654 sw->cb_data = cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000655 sw->forced = forced;
Harald Welte4724f992009-01-18 18:01:49 +0000656
657 rc = sw_open_file(sw, fname);
658 if (rc < 0) {
659 sw->state = SW_STATE_NONE;
660 return rc;
661 }
662
663 return sw_load_init(sw);
664}
Harald Welte52b1f982008-12-23 20:25:15 +0000665
Harald Welte1602ade2009-01-29 21:12:39 +0000666int abis_nm_software_load_status(struct gsm_bts *bts)
667{
668 struct abis_nm_sw *sw = &g_sw;
669 struct stat st;
670 int rc, percent;
671
672 rc = fstat(sw->fd, &st);
673 if (rc < 0) {
674 perror("ERROR during stat");
675 return rc;
676 }
677
678 percent = (ftell(sw->stream) * 100) / st.st_size;
679 return percent;
680}
681
Harald Welte5e4d1b32009-02-01 13:36:56 +0000682/* Activate the specified software into the BTS */
683int abis_nm_software_activate(struct gsm_bts *bts, const char *fname,
684 gsm_cbfn *cbfn, void *cb_data)
685{
686 struct abis_nm_sw *sw = &g_sw;
687 int rc;
688
689 DEBUGP(DNM, "Activating Software (BTS %u, File \"%s\")\n",
690 bts->nr, fname);
691
692 if (sw->state != SW_STATE_NONE)
693 return -EBUSY;
694
695 sw->bts = bts;
696 sw->obj_class = NM_OC_SITE_MANAGER;
697 sw->obj_instance[0] = 0xff;
698 sw->obj_instance[1] = 0xff;
699 sw->obj_instance[2] = 0xff;
700 sw->state = SW_STATE_WAIT_ACTACK;
701 sw->cbfn = cbfn;
702 sw->cb_data = cb_data;
703
704 /* Open the file in order to fill some sw struct members */
705 rc = sw_open_file(sw, fname);
706 if (rc < 0) {
707 sw->state = SW_STATE_NONE;
708 return rc;
709 }
710 sw_close_file(sw);
711
712 return sw_activate(sw);
713}
714
Harald Welte8470bf22008-12-25 23:28:35 +0000715static void fill_nm_channel(struct abis_nm_channel *ch, u_int8_t bts_port,
Harald Welte52b1f982008-12-23 20:25:15 +0000716 u_int8_t ts_nr, u_int8_t subslot_nr)
717{
Harald Welteadaf08b2009-01-18 11:08:10 +0000718 ch->attrib = NM_ATT_ABIS_CHANNEL;
Harald Welte52b1f982008-12-23 20:25:15 +0000719 ch->bts_port = bts_port;
720 ch->timeslot = ts_nr;
721 ch->subslot = subslot_nr;
722}
723
724int abis_nm_establish_tei(struct gsm_bts *bts, u_int8_t trx_nr,
725 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot,
726 u_int8_t tei)
727{
728 struct abis_om_hdr *oh;
729 struct abis_nm_channel *ch;
Harald Welte702d8702008-12-26 20:25:35 +0000730 u_int8_t len = sizeof(*ch) + 2;
Harald Welte8470bf22008-12-25 23:28:35 +0000731 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000732
733 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
734 fill_om_fom_hdr(oh, len, NM_MT_ESTABLISH_TEI, NM_OC_RADIO_CARRIER,
735 bts->bts_nr, trx_nr, 0xff);
736
Harald Welte8470bf22008-12-25 23:28:35 +0000737 msgb_tv_put(msg, NM_ATT_TEI, tei);
Harald Welte52b1f982008-12-23 20:25:15 +0000738
739 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
740 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
741
742 return abis_nm_sendmsg(bts, msg);
743}
744
745/* connect signalling of one (BTS,TRX) to a particular timeslot on the E1 */
746int abis_nm_conn_terr_sign(struct gsm_bts_trx *trx,
747 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot)
748{
Harald Welte8470bf22008-12-25 23:28:35 +0000749 struct gsm_bts *bts = trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000750 struct abis_om_hdr *oh;
751 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000752 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000753
754 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000755 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_SIGN,
Harald Welte52b1f982008-12-23 20:25:15 +0000756 NM_OC_RADIO_CARRIER, bts->bts_nr, trx->nr, 0xff);
757
758 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
759 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
760
761 return abis_nm_sendmsg(bts, msg);
762}
763
764#if 0
765int abis_nm_disc_terr_sign(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
766 struct abis_nm_abis_channel *chan)
767{
768}
769#endif
770
771int abis_nm_conn_terr_traf(struct gsm_bts_trx_ts *ts,
772 u_int8_t e1_port, u_int8_t e1_timeslot,
773 u_int8_t e1_subslot)
774{
775 struct gsm_bts *bts = ts->trx->bts;
776 struct abis_om_hdr *oh;
777 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000778 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000779
780 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
781 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_TRAF,
782 NM_OC_BASEB_TRANSC, bts->bts_nr, ts->trx->nr, ts->nr);
783
784 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
785 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
786
787 return abis_nm_sendmsg(bts, msg);
788}
789
790#if 0
791int abis_nm_disc_terr_traf(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
792 struct abis_nm_abis_channel *chan,
793 u_int8_t subchan)
794{
795}
796#endif
797
798int abis_nm_set_channel_attr(struct gsm_bts_trx_ts *ts, u_int8_t chan_comb)
799{
800 struct gsm_bts *bts = ts->trx->bts;
801 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000802 u_int16_t arfcn = htons(ts->trx->arfcn);
Harald Welte52b1f982008-12-23 20:25:15 +0000803 u_int8_t zero = 0x00;
Harald Welte8470bf22008-12-25 23:28:35 +0000804 struct msgb *msg = nm_msgb_alloc();
Harald Welte702d8702008-12-26 20:25:35 +0000805 u_int8_t len = 4 + 2 + 2 + 2 + 2 +3;
Harald Welte52b1f982008-12-23 20:25:15 +0000806
807 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000808 fill_om_fom_hdr(oh, len, NM_MT_SET_CHAN_ATTR,
Harald Welte52b1f982008-12-23 20:25:15 +0000809 NM_OC_BASEB_TRANSC, bts->bts_nr,
810 ts->trx->nr, ts->nr);
811 /* FIXME: don't send ARFCN list, hopping sequence, mAIO, ...*/
812 msgb_tlv16_put(msg, NM_ATT_ARFCN_LIST, 1, &arfcn);
813 msgb_tv_put(msg, NM_ATT_CHAN_COMB, chan_comb);
814 msgb_tv_put(msg, NM_ATT_HSN, 0x00);
815 msgb_tv_put(msg, NM_ATT_MAIO, 0x00);
816 msgb_tv_put(msg, NM_ATT_TSC, 0x07); /* training sequence */
817 msgb_tlv_put(msg, 0x59, 1, &zero);
818
819 return abis_nm_sendmsg(bts, msg);
820}
821
Harald Welte8470bf22008-12-25 23:28:35 +0000822int abis_nm_raw_msg(struct gsm_bts *bts, int len, u_int8_t *rawmsg)
Harald Welte52b1f982008-12-23 20:25:15 +0000823{
Harald Welte8470bf22008-12-25 23:28:35 +0000824 struct msgb *msg = nm_msgb_alloc();
825 struct abis_om_hdr *oh;
Harald Welte52b1f982008-12-23 20:25:15 +0000826 u_int8_t *data;
827
828 oh = (struct abis_om_hdr *) msgb_put(msg, sizeof(*oh));
829 fill_om_hdr(oh, len);
830 data = msgb_put(msg, len);
Harald Weltead384642008-12-26 10:20:07 +0000831 memcpy(data, rawmsg, len);
Harald Welte52b1f982008-12-23 20:25:15 +0000832
833 return abis_nm_sendmsg(bts, msg);
834}
835
836/* Siemens specific commands */
837static int __simple_cmd(struct gsm_bts *bts, u_int8_t msg_type)
838{
839 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000840 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000841
842 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000843 fill_om_fom_hdr(oh, 0, msg_type, NM_OC_SITE_MANAGER,
Harald Welte52b1f982008-12-23 20:25:15 +0000844 0xff, 0xff, 0xff);
845
846 return abis_nm_sendmsg(bts, msg);
847}
848
849int abis_nm_event_reports(struct gsm_bts *bts, int on)
850{
851 if (on == 0)
Harald Welte227d4072009-01-03 08:16:25 +0000852 return __simple_cmd(bts, NM_MT_STOP_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000853 else
Harald Welte227d4072009-01-03 08:16:25 +0000854 return __simple_cmd(bts, NM_MT_REST_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000855}
856
Harald Welte3ffd1372009-02-01 22:15:49 +0000857
Harald Welte47d88ae2009-01-04 12:02:08 +0000858/* Siemens (or BS-11) specific commands */
859
Harald Welte3ffd1372009-02-01 22:15:49 +0000860int abis_nm_bs11_bsc_disconnect(struct gsm_bts *bts, int reconnect)
861{
862 if (reconnect == 0)
863 return __simple_cmd(bts, NM_MT_BS11_DISCONNECT);
864 else
865 return __simple_cmd(bts, NM_MT_BS11_RECONNECT);
866}
867
Harald Welte268bb402009-02-01 19:11:56 +0000868struct bs11_date_time {
869 u_int16_t year;
870 u_int8_t month;
871 u_int8_t day;
872 u_int8_t hour;
873 u_int8_t min;
874 u_int8_t sec;
875} __attribute__((packed));
876
877
878void get_bs11_date_time(struct bs11_date_time *aet)
879{
880 time_t t;
881 struct tm *tm;
882
883 t = time(NULL);
884 tm = localtime(&t);
885 aet->sec = tm->tm_sec;
886 aet->min = tm->tm_min;
887 aet->hour = tm->tm_hour;
888 aet->day = tm->tm_mday;
889 aet->month = tm->tm_mon;
890 aet->year = htons(1900 + tm->tm_year);
891}
892
Harald Welte05188ee2009-01-18 11:39:08 +0000893int abis_nm_bs11_reset_resource(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000894{
Harald Welte4668fda2009-01-03 08:19:29 +0000895 return __simple_cmd(bts, NM_MT_BS11_RESET_RESOURCE);
Harald Welte52b1f982008-12-23 20:25:15 +0000896}
897
Harald Welte05188ee2009-01-18 11:39:08 +0000898int abis_nm_bs11_db_transmission(struct gsm_bts *bts, int begin)
Harald Welte52b1f982008-12-23 20:25:15 +0000899{
900 if (begin)
Harald Welte4668fda2009-01-03 08:19:29 +0000901 return __simple_cmd(bts, NM_MT_BS11_BEGIN_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000902 else
Harald Welte4668fda2009-01-03 08:19:29 +0000903 return __simple_cmd(bts, NM_MT_BS11_END_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000904}
Harald Welte47d88ae2009-01-04 12:02:08 +0000905
Harald Welte05188ee2009-01-18 11:39:08 +0000906int abis_nm_bs11_create_object(struct gsm_bts *bts,
Harald Welte1bc09062009-01-18 14:17:52 +0000907 enum abis_bs11_objtype type, u_int8_t idx,
908 u_int8_t attr_len, const u_int8_t *attr)
Harald Welte47d88ae2009-01-04 12:02:08 +0000909{
910 struct abis_om_hdr *oh;
911 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000912 u_int8_t *cur;
Harald Welte47d88ae2009-01-04 12:02:08 +0000913
914 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000915 fill_om_fom_hdr(oh, attr_len, NM_MT_BS11_CREATE_OBJ,
Harald Welte268bb402009-02-01 19:11:56 +0000916 NM_OC_BS11, type, 0, idx);
Harald Welte1bc09062009-01-18 14:17:52 +0000917 cur = msgb_put(msg, attr_len);
918 memcpy(cur, attr, attr_len);
Harald Welte47d88ae2009-01-04 12:02:08 +0000919
920 return abis_nm_sendmsg(bts, msg);
921}
922
Harald Welte05188ee2009-01-18 11:39:08 +0000923int abis_nm_bs11_create_envaBTSE(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000924{
925 struct abis_om_hdr *oh;
926 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000927 u_int8_t zero = 0x00;
Harald Welte47d88ae2009-01-04 12:02:08 +0000928
929 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000930 fill_om_fom_hdr(oh, 3, NM_MT_BS11_CREATE_OBJ,
Harald Welte1bc09062009-01-18 14:17:52 +0000931 NM_OC_BS11_ENVABTSE, 0, idx, 0xff);
932 msgb_tlv_put(msg, 0x99, 1, &zero);
Harald Welte47d88ae2009-01-04 12:02:08 +0000933
934 return abis_nm_sendmsg(bts, msg);
935}
936
Harald Welte05188ee2009-01-18 11:39:08 +0000937int abis_nm_bs11_create_bport(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000938{
939 struct abis_om_hdr *oh;
940 struct msgb *msg = nm_msgb_alloc();
941
942 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
943 fill_om_fom_hdr(oh, 0, NM_MT_BS11_CREATE_OBJ, NM_OC_BS11_BPORT,
944 idx, 0, 0);
945
946 return abis_nm_sendmsg(bts, msg);
947}
Harald Welte05188ee2009-01-18 11:39:08 +0000948
949int abis_nm_bs11_set_oml_tei(struct gsm_bts *bts, u_int8_t tei)
950{
951 struct abis_om_hdr *oh;
952 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +0000953
954 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000955 fill_om_fom_hdr(oh, 2, NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
Harald Welte05188ee2009-01-18 11:39:08 +0000956 0xff, 0xff, 0xff);
957 msgb_tv_put(msg, NM_ATT_TEI, tei);
958
959 return abis_nm_sendmsg(bts, msg);
960}
961
962/* like abis_nm_conn_terr_traf */
963int abis_nm_bs11_conn_oml(struct gsm_bts *bts, u_int8_t e1_port,
964 u_int8_t e1_timeslot, u_int8_t e1_subslot)
965{
966 struct abis_om_hdr *oh;
967 struct abis_nm_channel *ch;
968 struct msgb *msg = nm_msgb_alloc();
969
970 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte05188ee2009-01-18 11:39:08 +0000971 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_BS11_SET_ATTR,
972 NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff);
973
974 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
975 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
976
977 return abis_nm_sendmsg(bts, msg);
978}
979
980int abis_nm_bs11_set_trx_power(struct gsm_bts_trx *trx, u_int8_t level)
981{
982 struct abis_om_hdr *oh;
983 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39: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_SET_ATTR,
Harald Welte05188ee2009-01-18 11:39:08 +0000987 NM_OC_BS11, BS11_OBJ_PA, 0x00, trx->nr);
988 msgb_tlv_put(msg, NM_ATT_BS11_TXPWR, 1, &level);
989
990 return abis_nm_sendmsg(trx->bts, msg);
991}
992
Harald Welte268bb402009-02-01 19:11:56 +0000993//static const u_int8_t bs11_logon_c7[] = { 0x07, 0xd9, 0x01, 0x11, 0x0d, 0x10, 0x20 };
Harald Weltebb151312009-01-28 20:42:07 +0000994static const u_int8_t bs11_logon_c8[] = { 0x02 };
Harald Welte05188ee2009-01-18 11:39:08 +0000995static const u_int8_t bs11_logon_c9[] = "FACTORY";
996
Harald Welte1bc09062009-01-18 14:17:52 +0000997int abis_nm_bs11_factory_logon(struct gsm_bts *bts, int on)
Harald Welte05188ee2009-01-18 11:39:08 +0000998{
999 struct abis_om_hdr *oh;
1000 struct msgb *msg = nm_msgb_alloc();
Harald Welte268bb402009-02-01 19:11:56 +00001001 struct bs11_date_time bdt;
1002
1003 get_bs11_date_time(&bdt);
Harald Welte05188ee2009-01-18 11:39:08 +00001004
1005 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte1bc09062009-01-18 14:17:52 +00001006 if (on) {
Harald Welte268bb402009-02-01 19:11:56 +00001007 u_int8_t len = 3*2 + sizeof(bdt)
Harald Welte6f676a32009-01-18 14:27:48 +00001008 + sizeof(bs11_logon_c8) + sizeof(bs11_logon_c9);
Harald Welte043d04a2009-01-29 23:15:30 +00001009 fill_om_fom_hdr(oh, len, NM_MT_BS11_LMT_LOGON,
Harald Welte1bc09062009-01-18 14:17:52 +00001010 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
Harald Welte043d04a2009-01-29 23:15:30 +00001011 msgb_tlv_put(msg, NM_ATT_BS11_LMT_LOGIN_TIME,
Harald Welte5083b0b2009-02-02 19:20:52 +00001012 sizeof(bdt), (u_int8_t *) &bdt);
Harald Welte043d04a2009-01-29 23:15:30 +00001013 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_ACC_LEV,
1014 sizeof(bs11_logon_c8), bs11_logon_c8);
1015 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_NAME,
1016 sizeof(bs11_logon_c9), bs11_logon_c9);
Harald Welte1bc09062009-01-18 14:17:52 +00001017 } else {
Harald Welte5e4d1b32009-02-01 13:36:56 +00001018 fill_om_fom_hdr(oh, 0, NM_MT_BS11_LMT_LOGOFF,
Harald Welte1bc09062009-01-18 14:17:52 +00001019 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
1020 }
Harald Welte05188ee2009-01-18 11:39:08 +00001021
1022 return abis_nm_sendmsg(bts, msg);
1023}
Harald Welte1bc09062009-01-18 14:17:52 +00001024
1025int abis_nm_bs11_set_trx1_pw(struct gsm_bts *bts, const char *password)
1026{
1027 struct abis_om_hdr *oh;
1028 struct msgb *msg;
1029
1030 if (strlen(password) != 10)
1031 return -EINVAL;
1032
1033 msg = nm_msgb_alloc();
1034 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001035 fill_om_fom_hdr(oh, 2+strlen(password), NM_MT_BS11_SET_ATTR,
Harald Welte1bc09062009-01-18 14:17:52 +00001036 NM_OC_BS11, BS11_OBJ_TRX1, 0x00, 0x00);
1037 msgb_tlv_put(msg, NM_ATT_BS11_PASSWORD, 10, (const u_int8_t *)password);
1038
1039 return abis_nm_sendmsg(bts, msg);
1040}
1041
1042int abis_nm_bs11_get_state(struct gsm_bts *bts)
1043{
1044 return __simple_cmd(bts, NM_MT_BS11_GET_STATE);
1045}
Harald Welte5e4d1b32009-02-01 13:36:56 +00001046
1047/* BS11 SWL */
1048
1049struct abis_nm_bs11_sw {
1050 struct gsm_bts *bts;
1051 char swl_fname[PATH_MAX];
1052 u_int8_t win_size;
Harald Welte3ffd1372009-02-01 22:15:49 +00001053 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001054 struct llist_head file_list;
1055 gsm_cbfn *user_cb; /* specified by the user */
1056};
1057static struct abis_nm_bs11_sw _g_bs11_sw, *g_bs11_sw = &_g_bs11_sw;
1058
1059struct file_list_entry {
1060 struct llist_head list;
1061 char fname[PATH_MAX];
1062};
1063
1064struct file_list_entry *fl_dequeue(struct llist_head *queue)
1065{
1066 struct llist_head *lh;
1067
1068 if (llist_empty(queue))
1069 return NULL;
1070
1071 lh = queue->next;
1072 llist_del(lh);
1073
1074 return llist_entry(lh, struct file_list_entry, list);
1075}
1076
1077static int bs11_read_swl_file(struct abis_nm_bs11_sw *bs11_sw)
1078{
1079 char linebuf[255];
1080 struct llist_head *lh, *lh2;
1081 FILE *swl;
1082 int rc = 0;
1083
1084 swl = fopen(bs11_sw->swl_fname, "r");
1085 if (!swl)
1086 return -ENODEV;
1087
1088 /* zero the stale file list, if any */
1089 llist_for_each_safe(lh, lh2, &bs11_sw->file_list) {
1090 llist_del(lh);
1091 free(lh);
1092 }
1093
1094 while (fgets(linebuf, sizeof(linebuf), swl)) {
1095 char file_id[12+1];
1096 char file_version[80+1];
1097 struct file_list_entry *fle;
1098 static char dir[PATH_MAX];
1099
1100 if (strlen(linebuf) < 4)
1101 continue;
Harald Welte3ffd1372009-02-01 22:15:49 +00001102
Harald Welte5e4d1b32009-02-01 13:36:56 +00001103 rc = sscanf(linebuf+4, "%12s:%80s\r\n", file_id, file_version);
1104 if (rc < 0) {
1105 perror("ERR parsing SWL file");
1106 rc = -EINVAL;
1107 goto out;
1108 }
1109 if (rc < 2)
1110 continue;
1111
1112 fle = malloc(sizeof(*fle));
1113 if (!fle) {
1114 rc = -ENOMEM;
1115 goto out;
1116 }
1117 memset(fle, 0, sizeof(*fle));
1118
1119 /* construct new filename */
1120 strncpy(dir, bs11_sw->swl_fname, sizeof(dir));
1121 strncat(fle->fname, dirname(dir), sizeof(fle->fname) - 1);
1122 strcat(fle->fname, "/");
1123 strncat(fle->fname, file_id, sizeof(fle->fname) - 1 -strlen(fle->fname));
Harald Welte5e4d1b32009-02-01 13:36:56 +00001124
1125 llist_add_tail(&fle->list, &bs11_sw->file_list);
1126 }
1127
1128out:
1129 fclose(swl);
1130 return rc;
1131}
1132
1133/* bs11 swload specific callback, passed to abis_nm core swload */
1134static int bs11_swload_cbfn(unsigned int hook, unsigned int event,
1135 struct msgb *msg, void *data, void *param)
1136{
1137 struct abis_nm_bs11_sw *bs11_sw = data;
1138 struct file_list_entry *fle;
1139 int rc = 0;
1140
Harald Welte5e4d1b32009-02-01 13:36:56 +00001141 switch (event) {
1142 case NM_MT_LOAD_END_ACK:
1143 fle = fl_dequeue(&bs11_sw->file_list);
1144 if (fle) {
1145 /* start download the next file of our file list */
1146 rc = abis_nm_software_load(bs11_sw->bts, fle->fname,
1147 bs11_sw->win_size,
Harald Welte3ffd1372009-02-01 22:15:49 +00001148 bs11_sw->forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001149 &bs11_swload_cbfn, bs11_sw);
1150 free(fle);
1151 } else {
1152 /* activate the SWL */
1153 rc = abis_nm_software_activate(bs11_sw->bts,
1154 bs11_sw->swl_fname,
1155 bs11_swload_cbfn,
1156 bs11_sw);
1157 }
1158 break;
Harald Welte3ffd1372009-02-01 22:15:49 +00001159 case NM_MT_LOAD_SEG_ACK:
Harald Welte5e4d1b32009-02-01 13:36:56 +00001160 case NM_MT_LOAD_END_NACK:
1161 case NM_MT_LOAD_INIT_ACK:
1162 case NM_MT_LOAD_INIT_NACK:
1163 case NM_MT_ACTIVATE_SW_NACK:
1164 case NM_MT_ACTIVATE_SW_ACK:
1165 default:
1166 /* fallthrough to the user callback */
1167 rc = bs11_sw->user_cb(hook, event, msg, NULL, NULL);
1168 break;
1169 }
1170
1171 return rc;
1172}
1173
1174/* Siemens provides a SWL file that is a mere listing of all the other
1175 * files that are part of a software release. We need to upload first
1176 * the list file, and then each file that is listed in the list file */
1177int abis_nm_bs11_load_swl(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +00001178 u_int8_t win_size, int forced, gsm_cbfn *cbfn)
Harald Welte5e4d1b32009-02-01 13:36:56 +00001179{
1180 struct abis_nm_bs11_sw *bs11_sw = g_bs11_sw;
1181 struct file_list_entry *fle;
1182 int rc = 0;
1183
1184 INIT_LLIST_HEAD(&bs11_sw->file_list);
1185 bs11_sw->bts = bts;
1186 bs11_sw->win_size = win_size;
1187 bs11_sw->user_cb = cbfn;
Harald Welte3ffd1372009-02-01 22:15:49 +00001188 bs11_sw->forced = forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001189
1190 strncpy(bs11_sw->swl_fname, fname, sizeof(bs11_sw->swl_fname));
1191 rc = bs11_read_swl_file(bs11_sw);
1192 if (rc < 0)
1193 return rc;
1194
1195 /* dequeue next item in file list */
1196 fle = fl_dequeue(&bs11_sw->file_list);
1197 if (!fle)
1198 return -EINVAL;
1199
1200 /* start download the next file of our file list */
Harald Welte3ffd1372009-02-01 22:15:49 +00001201 rc = abis_nm_software_load(bts, fle->fname, win_size, forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001202 bs11_swload_cbfn, bs11_sw);
1203 free(fle);
1204 return rc;
1205}
1206
Harald Welte5083b0b2009-02-02 19:20:52 +00001207#if 0
Harald Welte5e4d1b32009-02-01 13:36:56 +00001208static u_int8_t req_attr_btse[] = {
1209 NM_ATT_ADM_STATE, NM_ATT_BS11_LMT_LOGON_SESSION,
1210 NM_ATT_BS11_LMT_LOGIN_TIME, NM_ATT_BS11_LMT_USER_ACC_LEV,
1211 NM_ATT_BS11_LMT_USER_NAME,
1212
1213 0xaf, NM_ATT_BS11_RX_OFFSET, NM_ATT_BS11_VENDOR_NAME,
1214
1215 NM_ATT_BS11_SW_LOAD_INTENDED, NM_ATT_BS11_SW_LOAD_SAFETY,
1216
1217 NM_ATT_BS11_SW_LOAD_STORED };
1218
1219static u_int8_t req_attr_btsm[] = {
1220 NM_ATT_ABIS_CHANNEL, NM_ATT_TEI, NM_ATT_BS11_ABIS_EXT_TIME,
1221 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xce, NM_ATT_FILE_ID,
1222 NM_ATT_FILE_VERSION, NM_ATT_OPER_STATE, 0xe8, NM_ATT_BS11_ALL_TEST_CATG,
1223 NM_ATT_SW_DESCR, NM_ATT_GET_ARI };
Harald Welte5083b0b2009-02-02 19:20:52 +00001224#endif
Harald Welte5e4d1b32009-02-01 13:36:56 +00001225
1226static u_int8_t req_attr[] = {
1227 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xa8, NM_ATT_OPER_STATE,
1228 0xd5, 0xa1, NM_ATT_BS11_ESN_FW_CODE_NO, NM_ATT_BS11_ESN_HW_CODE_NO,
1229 0x42, NM_ATT_BS11_ESN_PCB_SERIAL };
1230
1231int abis_nm_bs11_get_serno(struct gsm_bts *bts)
1232{
1233 struct abis_om_hdr *oh;
1234 struct msgb *msg = nm_msgb_alloc();
1235
1236 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1237 /* SiemensHW CCTRL object */
1238 fill_om_fom_hdr(oh, 2+sizeof(req_attr), NM_MT_GET_ATTR, NM_OC_BS11,
1239 0x03, 0x00, 0x00);
1240 msgb_tlv_put(msg, NM_ATT_LIST_REQ_ATTR, sizeof(req_attr), req_attr);
1241
1242 return abis_nm_sendmsg(bts, msg);
1243}
Harald Welte268bb402009-02-01 19:11:56 +00001244
1245int abis_nm_bs11_set_ext_time(struct gsm_bts *bts)
1246{
1247 struct abis_om_hdr *oh;
1248 struct msgb *msg = nm_msgb_alloc();
1249 struct bs11_date_time aet;
1250
1251 get_bs11_date_time(&aet);
1252 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1253 /* SiemensHW CCTRL object */
1254 fill_om_fom_hdr(oh, 2+sizeof(aet), NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
1255 0xff, 0xff, 0xff);
Harald Welte5083b0b2009-02-02 19:20:52 +00001256 msgb_tlv_put(msg, NM_ATT_BS11_ABIS_EXT_TIME, sizeof(aet), (u_int8_t *) &aet);
Harald Welte268bb402009-02-01 19:11:56 +00001257
1258 return abis_nm_sendmsg(bts, msg);
1259}