blob: 313b9dcf13048bd541832750912aa66e4fa9dd2f [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{
Holger Freyther59639e82009-02-09 23:09:55 +0000166 msg->trx = bts->c0;
167
Harald Weltead384642008-12-26 10:20:07 +0000168 return _abis_nm_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000169}
170
Harald Welte4724f992009-01-18 18:01:49 +0000171static int abis_nm_rcvmsg_sw(struct msgb *mb);
172
Harald Welte97ed1e72009-02-06 13:38:02 +0000173static int abis_nm_rx_statechg_rep(struct msgb *mb)
174{
175 struct abis_om_fom_hdr *foh = msgb_l3(mb);
Harald Welte97ed1e72009-02-06 13:38:02 +0000176 u_int8_t *data = &foh->data[0];
177
178 DEBUGP(DNM, "STATE CHG: OC=%02x INST=(%02x,%02x,%02x) ",
179 foh->obj_class, foh->obj_inst.bts_nr, foh->obj_inst.trx_nr,
180 foh->obj_inst.ts_nr);
181 if (*data++ == NM_ATT_OPER_STATE)
182 DEBUGPC(DNM, "OP_STATE=%02x ", *data++);
183 if (*data++ == NM_ATT_AVAIL_STATUS) {
184 u_int8_t att_len = *data++;
185 while (att_len--)
186 DEBUGPC(DNM, "AVAIL=%02x ", *data++);
187 }
188 DEBUGPC(DNM, "\n");
189 return 0;
190}
191
192static int abis_nm_rcvmsg_report(struct msgb *mb)
193{
194 struct abis_om_fom_hdr *foh = msgb_l3(mb);
195 u_int8_t mt = foh->msg_type;
196
197 //nmh->cfg->report_cb(mb, foh);
198
199 switch (mt) {
200 case NM_MT_STATECHG_EVENT_REP:
201 return abis_nm_rx_statechg_rep(mb);
202 break;
203 };
204
205 DEBUGP(DNM, "reporting NM MT 0x%02x\n", mt);
206
207 return 0;
208}
209
Harald Welte52b1f982008-12-23 20:25:15 +0000210/* Receive a OML NM Message from BTS */
Harald Welte8470bf22008-12-25 23:28:35 +0000211static int abis_nm_rcvmsg_fom(struct msgb *mb)
Harald Welte52b1f982008-12-23 20:25:15 +0000212{
213 struct abis_om_fom_hdr *foh = msgb_l3(mb);
214 u_int8_t mt = foh->msg_type;
215
216 /* check for unsolicited message */
Harald Welte97ed1e72009-02-06 13:38:02 +0000217 if (is_report(mt))
218 return abis_nm_rcvmsg_report(mb);
Harald Welte52b1f982008-12-23 20:25:15 +0000219
Harald Welte4724f992009-01-18 18:01:49 +0000220 if (is_in_arr(mt, sw_load_msgs, ARRAY_SIZE(sw_load_msgs)))
221 return abis_nm_rcvmsg_sw(mb);
222
Harald Weltead384642008-12-26 10:20:07 +0000223#if 0
Harald Welte52b1f982008-12-23 20:25:15 +0000224 /* check if last message is to be acked */
225 if (is_ack_nack(nmh->last_msgtype)) {
226 if (mt == MT_ACK(nmh->last_msgtype)) {
227 fprintf(stderr, "received ACK (0x%x)\n",
228 foh->msg_type);
229 /* we got our ACK, continue sending the next msg */
230 } else if (mt == MT_NACK(nmh->last_msgtype)) {
231 /* we got a NACK, signal this to the caller */
232 fprintf(stderr, "received NACK (0x%x)\n",
233 foh->msg_type);
234 /* FIXME: somehow signal this to the caller */
235 } else {
236 /* really strange things happen */
237 return -EINVAL;
238 }
239 }
Harald Weltead384642008-12-26 10:20:07 +0000240#endif
241
Harald Welte97ed1e72009-02-06 13:38:02 +0000242 switch (mt) {
243 case NM_MT_BS11_LMT_SESSION:
244 DEBUGP(DNM, "LMT Event: \n");
245 break;
246 }
247
Harald Weltead384642008-12-26 10:20:07 +0000248 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000249}
250
251/* High-Level API */
252/* Entry-point where L2 OML from BTS enters the NM code */
Harald Welte8470bf22008-12-25 23:28:35 +0000253int abis_nm_rcvmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000254{
255 int rc;
256 struct abis_om_hdr *oh = msgb_l2(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000257
258 /* Various consistency checks */
259 if (oh->placement != ABIS_OM_PLACEMENT_ONLY) {
260 fprintf(stderr, "ABIS OML placement 0x%x not supported\n",
261 oh->placement);
262 return -EINVAL;
263 }
264 if (oh->sequence != 0) {
265 fprintf(stderr, "ABIS OML sequence 0x%x != 0x00\n",
266 oh->sequence);
267 return -EINVAL;
268 }
Harald Welte702d8702008-12-26 20:25:35 +0000269#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000270 unsigned int l2_len = msg->tail - (u_int8_t *)msgb_l2(msg);
271 unsigned int hlen = sizeof(*oh) + sizeof(struct abis_om_fom_hdr);
Harald Welte702d8702008-12-26 20:25:35 +0000272 if (oh->length + hlen > l2_len) {
Harald Welte52b1f982008-12-23 20:25:15 +0000273 fprintf(stderr, "ABIS OML truncated message (%u > %u)\n",
274 oh->length + sizeof(*oh), l2_len);
275 return -EINVAL;
276 }
Harald Welte702d8702008-12-26 20:25:35 +0000277 if (oh->length + hlen < l2_len)
278 fprintf(stderr, "ABIS OML message with extra trailer?!? (oh->len=%d, sizeof_oh=%d l2_len=%d\n", oh->length, sizeof(*oh), l2_len);
279#endif
Harald Weltead384642008-12-26 10:20:07 +0000280 msg->l3h = (unsigned char *)oh + sizeof(*oh);
Harald Welte52b1f982008-12-23 20:25:15 +0000281
282 switch (oh->mdisc) {
283 case ABIS_OM_MDISC_FOM:
Harald Welte8470bf22008-12-25 23:28:35 +0000284 rc = abis_nm_rcvmsg_fom(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000285 break;
286 case ABIS_OM_MDISC_MMI:
287 case ABIS_OM_MDISC_TRAU:
288 case ABIS_OM_MDISC_MANUF:
289 default:
290 fprintf(stderr, "unknown ABIS OML message discriminator 0x%x\n",
291 oh->mdisc);
292 return -EINVAL;
293 }
294
Harald Weltead384642008-12-26 10:20:07 +0000295 msgb_free(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000296 return rc;
297}
298
299#if 0
300/* initialized all resources */
301struct abis_nm_h *abis_nm_init(struct abis_nm_cfg *cfg)
302{
303 struct abis_nm_h *nmh;
304
305 nmh = malloc(sizeof(*nmh));
306 if (!nmh)
307 return NULL;
308
309 nmh->cfg = cfg;
310
311 return nmh;
312}
313
314/* free all resources */
315void abis_nm_fini(struct abis_nm_h *nmh)
316{
317 free(nmh);
318}
319#endif
320
321/* Here we are trying to define a high-level API that can be used by
322 * the actual BSC implementation. However, the architecture is currently
323 * still under design. Ideally the calls to this API would be synchronous,
324 * while the underlying stack behind the APi runs in a traditional select
325 * based state machine.
326 */
327
Harald Welte4724f992009-01-18 18:01:49 +0000328/* 6.2 Software Load: */
329enum sw_state {
330 SW_STATE_NONE,
331 SW_STATE_WAIT_INITACK,
332 SW_STATE_WAIT_SEGACK,
333 SW_STATE_WAIT_ENDACK,
334 SW_STATE_WAIT_ACTACK,
335 SW_STATE_ERROR,
336};
Harald Welte52b1f982008-12-23 20:25:15 +0000337
Harald Welte52b1f982008-12-23 20:25:15 +0000338struct abis_nm_sw {
Harald Welte4724f992009-01-18 18:01:49 +0000339 struct gsm_bts *bts;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000340 gsm_cbfn *cbfn;
341 void *cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000342 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000343
Harald Welte52b1f982008-12-23 20:25:15 +0000344 /* this will become part of the SW LOAD INITIATE */
345 u_int8_t obj_class;
346 u_int8_t obj_instance[3];
Harald Welte4724f992009-01-18 18:01:49 +0000347
348 u_int8_t file_id[255];
349 u_int8_t file_id_len;
350
351 u_int8_t file_version[255];
352 u_int8_t file_version_len;
353
354 u_int8_t window_size;
355 u_int8_t seg_in_window;
356
357 int fd;
358 FILE *stream;
359 enum sw_state state;
Harald Welte1602ade2009-01-29 21:12:39 +0000360 int last_seg;
Harald Welte52b1f982008-12-23 20:25:15 +0000361};
362
Harald Welte4724f992009-01-18 18:01:49 +0000363static struct abis_nm_sw g_sw;
364
365/* 6.2.1 / 8.3.1: Load Data Initiate */
366static int sw_load_init(struct abis_nm_sw *sw)
Harald Welte52b1f982008-12-23 20:25:15 +0000367{
Harald Welte4724f992009-01-18 18:01:49 +0000368 struct abis_om_hdr *oh;
369 struct msgb *msg = nm_msgb_alloc();
370 u_int8_t len = 3*2 + sw->file_id_len + sw->file_version_len;
371
372 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
373 fill_om_fom_hdr(oh, len, NM_MT_LOAD_INIT, sw->obj_class,
374 sw->obj_instance[0], sw->obj_instance[1],
375 sw->obj_instance[2]);
376
377 /* FIXME: this is BS11 specific format */
378 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
379 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
380 sw->file_version);
381 msgb_tv_put(msg, NM_ATT_WINDOW_SIZE, sw->window_size);
382
383 return abis_nm_sendmsg(sw->bts, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000384}
385
Harald Welte1602ade2009-01-29 21:12:39 +0000386static int is_last_line(FILE *stream)
387{
388 char next_seg_buf[256];
389 long pos;
390
391 /* check if we're sending the last line */
392 pos = ftell(stream);
393 if (!fgets(next_seg_buf, sizeof(next_seg_buf)-2, stream)) {
394 fseek(stream, pos, SEEK_SET);
395 return 1;
396 }
397
398 fseek(stream, pos, SEEK_SET);
399 return 0;
400}
401
Harald Welte4724f992009-01-18 18:01:49 +0000402/* 6.2.2 / 8.3.2 Load Data Segment */
403static int sw_load_segment(struct abis_nm_sw *sw)
404{
405 struct abis_om_hdr *oh;
406 struct msgb *msg = nm_msgb_alloc();
407 char seg_buf[256];
408 char *line_buf = seg_buf+2;
Harald Welte3b8ba212009-01-29 12:27:58 +0000409 unsigned char *tlv;
Harald Welte4724f992009-01-18 18:01:49 +0000410 u_int8_t len;
Harald Welte4724f992009-01-18 18:01:49 +0000411
412 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte3b8ba212009-01-29 12:27:58 +0000413
414 switch (sw->bts->type) {
415 case GSM_BTS_TYPE_BS11:
416 if (fgets(line_buf, sizeof(seg_buf)-2, sw->stream) == NULL) {
417 perror("fgets reading segment");
418 return -EINVAL;
419 }
420 seg_buf[0] = 0x00;
Harald Welte1602ade2009-01-29 21:12:39 +0000421
422 /* check if we're sending the last line */
423 sw->last_seg = is_last_line(sw->stream);
424 if (sw->last_seg)
425 seg_buf[1] = 0;
426 else
427 seg_buf[1] = 1 + sw->seg_in_window++;
Harald Welte3b8ba212009-01-29 12:27:58 +0000428
429 len = strlen(line_buf) + 2;
430 tlv = msgb_put(msg, TLV_GROSS_LEN(len));
431 tlv_put(tlv, NM_ATT_BS11_FILE_DATA, len, (u_int8_t *)seg_buf);
432 /* BS11 wants CR + LF in excess of the TLV length !?! */
433 tlv[1] -= 2;
434
435 /* we only now know the exact length for the OM hdr */
436 len = strlen(line_buf)+2;
437 break;
438 default:
439 /* FIXME: Other BTS types */
440 return -1;
Harald Welte4724f992009-01-18 18:01:49 +0000441 }
Harald Welte4724f992009-01-18 18:01:49 +0000442
Harald Welte4724f992009-01-18 18:01:49 +0000443 fill_om_fom_hdr(oh, len, NM_MT_LOAD_SEG, sw->obj_class,
444 sw->obj_instance[0], sw->obj_instance[1],
445 sw->obj_instance[2]);
446
447 return abis_nm_sendmsg(sw->bts, msg);
448}
449
450/* 6.2.4 / 8.3.4 Load Data End */
451static int sw_load_end(struct abis_nm_sw *sw)
452{
453 struct abis_om_hdr *oh;
454 struct msgb *msg = nm_msgb_alloc();
455 u_int8_t len = 2*2 + sw->file_id_len + sw->file_version_len;
456
457 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
458 fill_om_fom_hdr(oh, len, NM_MT_LOAD_END, sw->obj_class,
459 sw->obj_instance[0], sw->obj_instance[1],
460 sw->obj_instance[2]);
461
462 /* FIXME: this is BS11 specific format */
463 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
464 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
465 sw->file_version);
466
467 return abis_nm_sendmsg(sw->bts, msg);
468}
Harald Welte5e4d1b32009-02-01 13:36:56 +0000469
Harald Welte52b1f982008-12-23 20:25:15 +0000470/* Activate the specified software into the BTS */
Harald Welte4724f992009-01-18 18:01:49 +0000471static int sw_activate(struct abis_nm_sw *sw)
Harald Welte52b1f982008-12-23 20:25:15 +0000472{
Harald Welte4724f992009-01-18 18:01:49 +0000473 struct abis_om_hdr *oh;
474 struct msgb *msg = nm_msgb_alloc();
475 u_int8_t len = 2*2 + sw->file_id_len + sw->file_version_len;
Harald Welte52b1f982008-12-23 20:25:15 +0000476
Harald Welte4724f992009-01-18 18:01:49 +0000477 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
478 fill_om_fom_hdr(oh, len, NM_MT_ACTIVATE_SW, sw->obj_class,
479 sw->obj_instance[0], sw->obj_instance[1],
480 sw->obj_instance[2]);
481
482 /* FIXME: this is BS11 specific format */
483 msgb_tlv_put(msg, NM_ATT_FILE_ID, sw->file_id_len, sw->file_id);
484 msgb_tlv_put(msg, NM_ATT_FILE_VERSION, sw->file_version_len,
485 sw->file_version);
486
487 return abis_nm_sendmsg(sw->bts, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000488}
Harald Welte4724f992009-01-18 18:01:49 +0000489
490static int sw_open_file(struct abis_nm_sw *sw, const char *fname)
491{
492 char file_id[12+1];
493 char file_version[80+1];
494 int rc;
495
496 sw->fd = open(fname, O_RDONLY);
497 if (sw->fd < 0)
498 return sw->fd;
499
500 switch (sw->bts->type) {
501 case GSM_BTS_TYPE_BS11:
502 sw->stream = fdopen(sw->fd, "r");
503 if (!sw->stream) {
504 perror("fdopen");
505 return -1;
506 }
507 /* read first line and parse file ID and VERSION */
Harald Welte3b8ba212009-01-29 12:27:58 +0000508 rc = fscanf(sw->stream, "@(#)%12s:%80s\r\n",
Harald Welte4724f992009-01-18 18:01:49 +0000509 file_id, file_version);
510 if (rc != 2) {
511 perror("parsing header line of software file");
512 return -1;
513 }
514 strcpy((char *)sw->file_id, file_id);
515 sw->file_id_len = strlen(file_id);
516 strcpy((char *)sw->file_version, file_version);
517 sw->file_version_len = strlen(file_version);
518 /* rewind to start of file */
Harald Welte3b8ba212009-01-29 12:27:58 +0000519 rewind(sw->stream);
Harald Welte4724f992009-01-18 18:01:49 +0000520 break;
521 default:
522 /* We don't know how to treat them yet */
523 close(sw->fd);
524 return -EINVAL;
525 }
526
527 return 0;
528}
529
530static void sw_close_file(struct abis_nm_sw *sw)
531{
532 switch (sw->bts->type) {
533 case GSM_BTS_TYPE_BS11:
534 fclose(sw->stream);
535 break;
536 default:
537 close(sw->fd);
538 break;
539 }
540}
541
542/* Fill the window */
543static int sw_fill_window(struct abis_nm_sw *sw)
544{
545 int rc;
546
547 while (sw->seg_in_window < sw->window_size) {
548 rc = sw_load_segment(sw);
549 if (rc < 0)
550 return rc;
Harald Welte1602ade2009-01-29 21:12:39 +0000551 if (sw->last_seg)
552 break;
Harald Welte4724f992009-01-18 18:01:49 +0000553 }
554 return 0;
555}
556
557/* callback function from abis_nm_rcvmsg() handler */
558static int abis_nm_rcvmsg_sw(struct msgb *mb)
559{
560 struct abis_om_fom_hdr *foh = msgb_l3(mb);
561 int rc = -1;
562 struct abis_nm_sw *sw = &g_sw;
563 enum sw_state old_state = sw->state;
564
Harald Welte3ffd1372009-02-01 22:15:49 +0000565 //DEBUGP(DNM, "state %u, NM MT 0x%02x\n", sw->state, foh->msg_type);
Harald Welte4724f992009-01-18 18:01:49 +0000566
567 switch (sw->state) {
568 case SW_STATE_WAIT_INITACK:
569 switch (foh->msg_type) {
570 case NM_MT_LOAD_INIT_ACK:
571 /* fill window with segments */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000572 if (sw->cbfn)
573 sw->cbfn(GSM_HOOK_NM_SWLOAD,
574 NM_MT_LOAD_INIT_ACK, mb,
575 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000576 rc = sw_fill_window(sw);
577 sw->state = SW_STATE_WAIT_SEGACK;
578 break;
579 case NM_MT_LOAD_INIT_NACK:
Harald Welte3ffd1372009-02-01 22:15:49 +0000580 if (sw->forced) {
581 DEBUGP(DNM, "FORCED: Ignoring Software Load "
582 "Init NACK\n");
583 if (sw->cbfn)
584 sw->cbfn(GSM_HOOK_NM_SWLOAD,
585 NM_MT_LOAD_INIT_ACK, mb,
586 sw->cb_data, NULL);
587 rc = sw_fill_window(sw);
588 sw->state = SW_STATE_WAIT_SEGACK;
589 } else {
590 DEBUGP(DNM, "Software Load Init NACK\n");
591 if (sw->cbfn)
592 sw->cbfn(GSM_HOOK_NM_SWLOAD,
593 NM_MT_LOAD_INIT_NACK, mb,
594 sw->cb_data, NULL);
595 sw->state = SW_STATE_ERROR;
596 }
Harald Welte4724f992009-01-18 18:01:49 +0000597 break;
598 }
599 break;
600 case SW_STATE_WAIT_SEGACK:
601 switch (foh->msg_type) {
602 case NM_MT_LOAD_SEG_ACK:
Harald Welte3ffd1372009-02-01 22:15:49 +0000603 if (sw->cbfn)
604 sw->cbfn(GSM_HOOK_NM_SWLOAD,
605 NM_MT_LOAD_SEG_ACK, mb,
606 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000607 sw->seg_in_window = 0;
Harald Welte1602ade2009-01-29 21:12:39 +0000608 if (!sw->last_seg) {
609 /* fill window with more segments */
610 rc = sw_fill_window(sw);
611 sw->state = SW_STATE_WAIT_SEGACK;
612 } else {
613 /* end the transfer */
614 sw->state = SW_STATE_WAIT_ENDACK;
615 rc = sw_load_end(sw);
616 }
Harald Welte4724f992009-01-18 18:01:49 +0000617 break;
618 }
619 break;
620 case SW_STATE_WAIT_ENDACK:
621 switch (foh->msg_type) {
622 case NM_MT_LOAD_END_ACK:
623 sw_close_file(sw);
Harald Welte5e4d1b32009-02-01 13:36:56 +0000624 DEBUGP(DNM, "Software Load End (BTS %u)\n",
625 sw->bts->nr);
626 sw->state = SW_STATE_NONE;
627 if (sw->cbfn)
628 sw->cbfn(GSM_HOOK_NM_SWLOAD,
629 NM_MT_LOAD_END_ACK, mb,
630 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000631 break;
632 case NM_MT_LOAD_END_NACK:
Holger Freyther31338a12009-02-06 17:43:50 +0000633 if (sw->forced) {
634 DEBUGP(DNM, "FORCED: Ignoring Software Load"
635 "End NACK\n");
636 sw->state = SW_STATE_NONE;
637 if (sw->cbfn)
638 sw->cbfn(GSM_HOOK_NM_SWLOAD,
639 NM_MT_LOAD_END_ACK, mb,
640 sw->cb_data, NULL);
641 } else {
642 DEBUGP(DNM, "Software Load End NACK\n");
643 sw->state = SW_STATE_ERROR;
644 if (sw->cbfn)
645 sw->cbfn(GSM_HOOK_NM_SWLOAD,
646 NM_MT_LOAD_END_NACK, mb,
647 sw->cb_data, NULL);
648 }
Harald Welte4724f992009-01-18 18:01:49 +0000649 break;
650 }
651 case SW_STATE_WAIT_ACTACK:
652 switch (foh->msg_type) {
653 case NM_MT_ACTIVATE_SW_ACK:
654 /* we're done */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000655 DEBUGP(DNM, "Activate Software DONE!\n");
Harald Welte4724f992009-01-18 18:01:49 +0000656 sw->state = SW_STATE_NONE;
657 rc = 0;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000658 if (sw->cbfn)
659 sw->cbfn(GSM_HOOK_NM_SWLOAD,
660 NM_MT_ACTIVATE_SW_ACK, mb,
661 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000662 break;
663 case NM_MT_ACTIVATE_SW_NACK:
Harald Welte1602ade2009-01-29 21:12:39 +0000664 DEBUGP(DNM, "Activate Software NACK\n");
Harald Welte4724f992009-01-18 18:01:49 +0000665 sw->state = SW_STATE_ERROR;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000666 if (sw->cbfn)
667 sw->cbfn(GSM_HOOK_NM_SWLOAD,
668 NM_MT_ACTIVATE_SW_NACK, mb,
669 sw->cb_data, NULL);
Harald Welte4724f992009-01-18 18:01:49 +0000670 break;
671 }
672 case SW_STATE_NONE:
673 case SW_STATE_ERROR:
674 break;
675 }
676
677 if (rc)
678 fprintf(stderr, "unexpected NM MT 0x%02x in state %u -> %u\n",
679 foh->msg_type, old_state, sw->state);
680
681 return rc;
682}
683
684/* Load the specified software into the BTS */
685int abis_nm_software_load(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +0000686 u_int8_t win_size, int forced,
687 gsm_cbfn *cbfn, void *cb_data)
Harald Welte4724f992009-01-18 18:01:49 +0000688{
689 struct abis_nm_sw *sw = &g_sw;
690 int rc;
691
Harald Welte5e4d1b32009-02-01 13:36:56 +0000692 DEBUGP(DNM, "Software Load (BTS %u, File \"%s\")\n",
693 bts->nr, fname);
694
Harald Welte4724f992009-01-18 18:01:49 +0000695 if (sw->state != SW_STATE_NONE)
696 return -EBUSY;
697
698 sw->bts = bts;
699 sw->obj_class = NM_OC_SITE_MANAGER;
700 sw->obj_instance[0] = 0xff;
701 sw->obj_instance[1] = 0xff;
702 sw->obj_instance[2] = 0xff;
703 sw->window_size = win_size;
704 sw->state = SW_STATE_WAIT_INITACK;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000705 sw->cbfn = cbfn;
706 sw->cb_data = cb_data;
Harald Welte3ffd1372009-02-01 22:15:49 +0000707 sw->forced = forced;
Harald Welte4724f992009-01-18 18:01:49 +0000708
709 rc = sw_open_file(sw, fname);
710 if (rc < 0) {
711 sw->state = SW_STATE_NONE;
712 return rc;
713 }
714
715 return sw_load_init(sw);
716}
Harald Welte52b1f982008-12-23 20:25:15 +0000717
Harald Welte1602ade2009-01-29 21:12:39 +0000718int abis_nm_software_load_status(struct gsm_bts *bts)
719{
720 struct abis_nm_sw *sw = &g_sw;
721 struct stat st;
722 int rc, percent;
723
724 rc = fstat(sw->fd, &st);
725 if (rc < 0) {
726 perror("ERROR during stat");
727 return rc;
728 }
729
730 percent = (ftell(sw->stream) * 100) / st.st_size;
731 return percent;
732}
733
Harald Welte5e4d1b32009-02-01 13:36:56 +0000734/* Activate the specified software into the BTS */
735int abis_nm_software_activate(struct gsm_bts *bts, const char *fname,
736 gsm_cbfn *cbfn, void *cb_data)
737{
738 struct abis_nm_sw *sw = &g_sw;
739 int rc;
740
741 DEBUGP(DNM, "Activating Software (BTS %u, File \"%s\")\n",
742 bts->nr, fname);
743
744 if (sw->state != SW_STATE_NONE)
745 return -EBUSY;
746
747 sw->bts = bts;
748 sw->obj_class = NM_OC_SITE_MANAGER;
749 sw->obj_instance[0] = 0xff;
750 sw->obj_instance[1] = 0xff;
751 sw->obj_instance[2] = 0xff;
752 sw->state = SW_STATE_WAIT_ACTACK;
753 sw->cbfn = cbfn;
754 sw->cb_data = cb_data;
755
756 /* Open the file in order to fill some sw struct members */
757 rc = sw_open_file(sw, fname);
758 if (rc < 0) {
759 sw->state = SW_STATE_NONE;
760 return rc;
761 }
762 sw_close_file(sw);
763
764 return sw_activate(sw);
765}
766
Harald Welte8470bf22008-12-25 23:28:35 +0000767static void fill_nm_channel(struct abis_nm_channel *ch, u_int8_t bts_port,
Harald Welte52b1f982008-12-23 20:25:15 +0000768 u_int8_t ts_nr, u_int8_t subslot_nr)
769{
Harald Welteadaf08b2009-01-18 11:08:10 +0000770 ch->attrib = NM_ATT_ABIS_CHANNEL;
Harald Welte52b1f982008-12-23 20:25:15 +0000771 ch->bts_port = bts_port;
772 ch->timeslot = ts_nr;
773 ch->subslot = subslot_nr;
774}
775
776int abis_nm_establish_tei(struct gsm_bts *bts, u_int8_t trx_nr,
777 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot,
778 u_int8_t tei)
779{
780 struct abis_om_hdr *oh;
781 struct abis_nm_channel *ch;
Harald Welte702d8702008-12-26 20:25:35 +0000782 u_int8_t len = sizeof(*ch) + 2;
Harald Welte8470bf22008-12-25 23:28:35 +0000783 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000784
785 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
786 fill_om_fom_hdr(oh, len, NM_MT_ESTABLISH_TEI, NM_OC_RADIO_CARRIER,
787 bts->bts_nr, trx_nr, 0xff);
788
Harald Welte8470bf22008-12-25 23:28:35 +0000789 msgb_tv_put(msg, NM_ATT_TEI, tei);
Harald Welte52b1f982008-12-23 20:25:15 +0000790
791 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
792 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
793
794 return abis_nm_sendmsg(bts, msg);
795}
796
797/* connect signalling of one (BTS,TRX) to a particular timeslot on the E1 */
798int abis_nm_conn_terr_sign(struct gsm_bts_trx *trx,
799 u_int8_t e1_port, u_int8_t e1_timeslot, u_int8_t e1_subslot)
800{
Harald Welte8470bf22008-12-25 23:28:35 +0000801 struct gsm_bts *bts = trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000802 struct abis_om_hdr *oh;
803 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000804 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000805
806 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000807 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_SIGN,
Harald Welte52b1f982008-12-23 20:25:15 +0000808 NM_OC_RADIO_CARRIER, bts->bts_nr, trx->nr, 0xff);
809
810 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
811 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
812
813 return abis_nm_sendmsg(bts, msg);
814}
815
816#if 0
817int abis_nm_disc_terr_sign(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
818 struct abis_nm_abis_channel *chan)
819{
820}
821#endif
822
823int abis_nm_conn_terr_traf(struct gsm_bts_trx_ts *ts,
824 u_int8_t e1_port, u_int8_t e1_timeslot,
825 u_int8_t e1_subslot)
826{
827 struct gsm_bts *bts = ts->trx->bts;
828 struct abis_om_hdr *oh;
829 struct abis_nm_channel *ch;
Harald Welte8470bf22008-12-25 23:28:35 +0000830 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000831
832 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
833 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_CONN_TERR_TRAF,
834 NM_OC_BASEB_TRANSC, bts->bts_nr, ts->trx->nr, ts->nr);
835
836 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
837 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
838
839 return abis_nm_sendmsg(bts, msg);
840}
841
842#if 0
843int abis_nm_disc_terr_traf(struct abis_nm_h *h, struct abis_om_obj_inst *inst,
844 struct abis_nm_abis_channel *chan,
845 u_int8_t subchan)
846{
847}
848#endif
849
850int abis_nm_set_channel_attr(struct gsm_bts_trx_ts *ts, u_int8_t chan_comb)
851{
852 struct gsm_bts *bts = ts->trx->bts;
853 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000854 u_int16_t arfcn = htons(ts->trx->arfcn);
Harald Welte52b1f982008-12-23 20:25:15 +0000855 u_int8_t zero = 0x00;
Harald Welte8470bf22008-12-25 23:28:35 +0000856 struct msgb *msg = nm_msgb_alloc();
Harald Welte702d8702008-12-26 20:25:35 +0000857 u_int8_t len = 4 + 2 + 2 + 2 + 2 +3;
Harald Welte52b1f982008-12-23 20:25:15 +0000858
859 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000860 fill_om_fom_hdr(oh, len, NM_MT_SET_CHAN_ATTR,
Harald Welte52b1f982008-12-23 20:25:15 +0000861 NM_OC_BASEB_TRANSC, bts->bts_nr,
862 ts->trx->nr, ts->nr);
863 /* FIXME: don't send ARFCN list, hopping sequence, mAIO, ...*/
864 msgb_tlv16_put(msg, NM_ATT_ARFCN_LIST, 1, &arfcn);
865 msgb_tv_put(msg, NM_ATT_CHAN_COMB, chan_comb);
866 msgb_tv_put(msg, NM_ATT_HSN, 0x00);
867 msgb_tv_put(msg, NM_ATT_MAIO, 0x00);
868 msgb_tv_put(msg, NM_ATT_TSC, 0x07); /* training sequence */
869 msgb_tlv_put(msg, 0x59, 1, &zero);
870
871 return abis_nm_sendmsg(bts, msg);
872}
873
Harald Welte8470bf22008-12-25 23:28:35 +0000874int abis_nm_raw_msg(struct gsm_bts *bts, int len, u_int8_t *rawmsg)
Harald Welte52b1f982008-12-23 20:25:15 +0000875{
Harald Welte8470bf22008-12-25 23:28:35 +0000876 struct msgb *msg = nm_msgb_alloc();
877 struct abis_om_hdr *oh;
Harald Welte52b1f982008-12-23 20:25:15 +0000878 u_int8_t *data;
879
880 oh = (struct abis_om_hdr *) msgb_put(msg, sizeof(*oh));
881 fill_om_hdr(oh, len);
882 data = msgb_put(msg, len);
Harald Weltead384642008-12-26 10:20:07 +0000883 memcpy(data, rawmsg, len);
Harald Welte52b1f982008-12-23 20:25:15 +0000884
885 return abis_nm_sendmsg(bts, msg);
886}
887
888/* Siemens specific commands */
889static int __simple_cmd(struct gsm_bts *bts, u_int8_t msg_type)
890{
891 struct abis_om_hdr *oh;
Harald Welte8470bf22008-12-25 23:28:35 +0000892 struct msgb *msg = nm_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000893
894 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte702d8702008-12-26 20:25:35 +0000895 fill_om_fom_hdr(oh, 0, msg_type, NM_OC_SITE_MANAGER,
Harald Welte52b1f982008-12-23 20:25:15 +0000896 0xff, 0xff, 0xff);
897
898 return abis_nm_sendmsg(bts, msg);
899}
900
901int abis_nm_event_reports(struct gsm_bts *bts, int on)
902{
903 if (on == 0)
Harald Welte227d4072009-01-03 08:16:25 +0000904 return __simple_cmd(bts, NM_MT_STOP_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000905 else
Harald Welte227d4072009-01-03 08:16:25 +0000906 return __simple_cmd(bts, NM_MT_REST_EVENT_REP);
Harald Welte52b1f982008-12-23 20:25:15 +0000907}
908
Harald Welte3ffd1372009-02-01 22:15:49 +0000909
Harald Welte47d88ae2009-01-04 12:02:08 +0000910/* Siemens (or BS-11) specific commands */
911
Harald Welte3ffd1372009-02-01 22:15:49 +0000912int abis_nm_bs11_bsc_disconnect(struct gsm_bts *bts, int reconnect)
913{
914 if (reconnect == 0)
915 return __simple_cmd(bts, NM_MT_BS11_DISCONNECT);
916 else
917 return __simple_cmd(bts, NM_MT_BS11_RECONNECT);
918}
919
Harald Welteb8427972009-02-05 19:27:17 +0000920int abis_nm_bs11_restart(struct gsm_bts *bts)
921{
922 return __simple_cmd(bts, NM_MT_BS11_RESTART);
923}
924
925
Harald Welte268bb402009-02-01 19:11:56 +0000926struct bs11_date_time {
927 u_int16_t year;
928 u_int8_t month;
929 u_int8_t day;
930 u_int8_t hour;
931 u_int8_t min;
932 u_int8_t sec;
933} __attribute__((packed));
934
935
936void get_bs11_date_time(struct bs11_date_time *aet)
937{
938 time_t t;
939 struct tm *tm;
940
941 t = time(NULL);
942 tm = localtime(&t);
943 aet->sec = tm->tm_sec;
944 aet->min = tm->tm_min;
945 aet->hour = tm->tm_hour;
946 aet->day = tm->tm_mday;
947 aet->month = tm->tm_mon;
948 aet->year = htons(1900 + tm->tm_year);
949}
950
Harald Welte05188ee2009-01-18 11:39:08 +0000951int abis_nm_bs11_reset_resource(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000952{
Harald Welte4668fda2009-01-03 08:19:29 +0000953 return __simple_cmd(bts, NM_MT_BS11_RESET_RESOURCE);
Harald Welte52b1f982008-12-23 20:25:15 +0000954}
955
Harald Welte05188ee2009-01-18 11:39:08 +0000956int abis_nm_bs11_db_transmission(struct gsm_bts *bts, int begin)
Harald Welte52b1f982008-12-23 20:25:15 +0000957{
958 if (begin)
Harald Welte4668fda2009-01-03 08:19:29 +0000959 return __simple_cmd(bts, NM_MT_BS11_BEGIN_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000960 else
Harald Welte4668fda2009-01-03 08:19:29 +0000961 return __simple_cmd(bts, NM_MT_BS11_END_DB_TX);
Harald Welte52b1f982008-12-23 20:25:15 +0000962}
Harald Welte47d88ae2009-01-04 12:02:08 +0000963
Harald Welte05188ee2009-01-18 11:39:08 +0000964int abis_nm_bs11_create_object(struct gsm_bts *bts,
Harald Welte1bc09062009-01-18 14:17:52 +0000965 enum abis_bs11_objtype type, u_int8_t idx,
966 u_int8_t attr_len, const u_int8_t *attr)
Harald Welte47d88ae2009-01-04 12:02:08 +0000967{
968 struct abis_om_hdr *oh;
969 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000970 u_int8_t *cur;
Harald Welte47d88ae2009-01-04 12:02:08 +0000971
972 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000973 fill_om_fom_hdr(oh, attr_len, NM_MT_BS11_CREATE_OBJ,
Harald Welte268bb402009-02-01 19:11:56 +0000974 NM_OC_BS11, type, 0, idx);
Harald Welte1bc09062009-01-18 14:17:52 +0000975 cur = msgb_put(msg, attr_len);
976 memcpy(cur, attr, attr_len);
Harald Welte47d88ae2009-01-04 12:02:08 +0000977
978 return abis_nm_sendmsg(bts, msg);
979}
980
Harald Welte05188ee2009-01-18 11:39:08 +0000981int abis_nm_bs11_create_envaBTSE(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000982{
983 struct abis_om_hdr *oh;
984 struct msgb *msg = nm_msgb_alloc();
Harald Welte1bc09062009-01-18 14:17:52 +0000985 u_int8_t zero = 0x00;
Harald Welte47d88ae2009-01-04 12:02:08 +0000986
987 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +0000988 fill_om_fom_hdr(oh, 3, NM_MT_BS11_CREATE_OBJ,
Harald Welte1bc09062009-01-18 14:17:52 +0000989 NM_OC_BS11_ENVABTSE, 0, idx, 0xff);
990 msgb_tlv_put(msg, 0x99, 1, &zero);
Harald Welte47d88ae2009-01-04 12:02:08 +0000991
992 return abis_nm_sendmsg(bts, msg);
993}
994
Harald Welte05188ee2009-01-18 11:39:08 +0000995int abis_nm_bs11_create_bport(struct gsm_bts *bts, u_int8_t idx)
Harald Welte47d88ae2009-01-04 12:02:08 +0000996{
997 struct abis_om_hdr *oh;
998 struct msgb *msg = nm_msgb_alloc();
999
1000 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1001 fill_om_fom_hdr(oh, 0, NM_MT_BS11_CREATE_OBJ, NM_OC_BS11_BPORT,
1002 idx, 0, 0);
1003
1004 return abis_nm_sendmsg(bts, msg);
1005}
Harald Welte05188ee2009-01-18 11:39:08 +00001006
1007int abis_nm_bs11_set_oml_tei(struct gsm_bts *bts, u_int8_t tei)
1008{
1009 struct abis_om_hdr *oh;
1010 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +00001011
1012 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001013 fill_om_fom_hdr(oh, 2, NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
Harald Welte05188ee2009-01-18 11:39:08 +00001014 0xff, 0xff, 0xff);
1015 msgb_tv_put(msg, NM_ATT_TEI, tei);
1016
1017 return abis_nm_sendmsg(bts, msg);
1018}
1019
1020/* like abis_nm_conn_terr_traf */
1021int abis_nm_bs11_conn_oml(struct gsm_bts *bts, u_int8_t e1_port,
1022 u_int8_t e1_timeslot, u_int8_t e1_subslot)
1023{
1024 struct abis_om_hdr *oh;
1025 struct abis_nm_channel *ch;
1026 struct msgb *msg = nm_msgb_alloc();
1027
1028 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte05188ee2009-01-18 11:39:08 +00001029 fill_om_fom_hdr(oh, sizeof(*ch), NM_MT_BS11_SET_ATTR,
1030 NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff);
1031
1032 ch = (struct abis_nm_channel *) msgb_put(msg, sizeof(*ch));
1033 fill_nm_channel(ch, e1_port, e1_timeslot, e1_subslot);
1034
1035 return abis_nm_sendmsg(bts, msg);
1036}
1037
1038int abis_nm_bs11_set_trx_power(struct gsm_bts_trx *trx, u_int8_t level)
1039{
1040 struct abis_om_hdr *oh;
1041 struct msgb *msg = nm_msgb_alloc();
Harald Welte05188ee2009-01-18 11:39:08 +00001042
1043 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001044 fill_om_fom_hdr(oh, 3, NM_MT_BS11_SET_ATTR,
Harald Welte05188ee2009-01-18 11:39:08 +00001045 NM_OC_BS11, BS11_OBJ_PA, 0x00, trx->nr);
1046 msgb_tlv_put(msg, NM_ATT_BS11_TXPWR, 1, &level);
1047
1048 return abis_nm_sendmsg(trx->bts, msg);
1049}
1050
Harald Welte268bb402009-02-01 19:11:56 +00001051//static const u_int8_t bs11_logon_c7[] = { 0x07, 0xd9, 0x01, 0x11, 0x0d, 0x10, 0x20 };
Harald Weltebb151312009-01-28 20:42:07 +00001052static const u_int8_t bs11_logon_c8[] = { 0x02 };
Harald Welte05188ee2009-01-18 11:39:08 +00001053static const u_int8_t bs11_logon_c9[] = "FACTORY";
1054
Harald Welte1bc09062009-01-18 14:17:52 +00001055int abis_nm_bs11_factory_logon(struct gsm_bts *bts, int on)
Harald Welte05188ee2009-01-18 11:39:08 +00001056{
1057 struct abis_om_hdr *oh;
1058 struct msgb *msg = nm_msgb_alloc();
Harald Welte268bb402009-02-01 19:11:56 +00001059 struct bs11_date_time bdt;
1060
1061 get_bs11_date_time(&bdt);
Harald Welte05188ee2009-01-18 11:39:08 +00001062
1063 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte1bc09062009-01-18 14:17:52 +00001064 if (on) {
Harald Welte268bb402009-02-01 19:11:56 +00001065 u_int8_t len = 3*2 + sizeof(bdt)
Harald Welte6f676a32009-01-18 14:27:48 +00001066 + sizeof(bs11_logon_c8) + sizeof(bs11_logon_c9);
Harald Welte043d04a2009-01-29 23:15:30 +00001067 fill_om_fom_hdr(oh, len, NM_MT_BS11_LMT_LOGON,
Harald Welte1bc09062009-01-18 14:17:52 +00001068 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
Harald Welte043d04a2009-01-29 23:15:30 +00001069 msgb_tlv_put(msg, NM_ATT_BS11_LMT_LOGIN_TIME,
Harald Welte5083b0b2009-02-02 19:20:52 +00001070 sizeof(bdt), (u_int8_t *) &bdt);
Harald Welte043d04a2009-01-29 23:15:30 +00001071 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_ACC_LEV,
1072 sizeof(bs11_logon_c8), bs11_logon_c8);
1073 msgb_tlv_put(msg, NM_ATT_BS11_LMT_USER_NAME,
1074 sizeof(bs11_logon_c9), bs11_logon_c9);
Harald Welte1bc09062009-01-18 14:17:52 +00001075 } else {
Harald Welte5e4d1b32009-02-01 13:36:56 +00001076 fill_om_fom_hdr(oh, 0, NM_MT_BS11_LMT_LOGOFF,
Harald Welte1bc09062009-01-18 14:17:52 +00001077 NM_OC_BS11_A3, 0xff, 0xff, 0xff);
1078 }
Harald Welte05188ee2009-01-18 11:39:08 +00001079
1080 return abis_nm_sendmsg(bts, msg);
1081}
Harald Welte1bc09062009-01-18 14:17:52 +00001082
1083int abis_nm_bs11_set_trx1_pw(struct gsm_bts *bts, const char *password)
1084{
1085 struct abis_om_hdr *oh;
1086 struct msgb *msg;
1087
1088 if (strlen(password) != 10)
1089 return -EINVAL;
1090
1091 msg = nm_msgb_alloc();
1092 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
Harald Welte6f676a32009-01-18 14:27:48 +00001093 fill_om_fom_hdr(oh, 2+strlen(password), NM_MT_BS11_SET_ATTR,
Harald Welte1bc09062009-01-18 14:17:52 +00001094 NM_OC_BS11, BS11_OBJ_TRX1, 0x00, 0x00);
1095 msgb_tlv_put(msg, NM_ATT_BS11_PASSWORD, 10, (const u_int8_t *)password);
1096
1097 return abis_nm_sendmsg(bts, msg);
1098}
1099
1100int abis_nm_bs11_get_state(struct gsm_bts *bts)
1101{
1102 return __simple_cmd(bts, NM_MT_BS11_GET_STATE);
1103}
Harald Welte5e4d1b32009-02-01 13:36:56 +00001104
1105/* BS11 SWL */
1106
1107struct abis_nm_bs11_sw {
1108 struct gsm_bts *bts;
1109 char swl_fname[PATH_MAX];
1110 u_int8_t win_size;
Harald Welte3ffd1372009-02-01 22:15:49 +00001111 int forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001112 struct llist_head file_list;
1113 gsm_cbfn *user_cb; /* specified by the user */
1114};
1115static struct abis_nm_bs11_sw _g_bs11_sw, *g_bs11_sw = &_g_bs11_sw;
1116
1117struct file_list_entry {
1118 struct llist_head list;
1119 char fname[PATH_MAX];
1120};
1121
1122struct file_list_entry *fl_dequeue(struct llist_head *queue)
1123{
1124 struct llist_head *lh;
1125
1126 if (llist_empty(queue))
1127 return NULL;
1128
1129 lh = queue->next;
1130 llist_del(lh);
1131
1132 return llist_entry(lh, struct file_list_entry, list);
1133}
1134
1135static int bs11_read_swl_file(struct abis_nm_bs11_sw *bs11_sw)
1136{
1137 char linebuf[255];
1138 struct llist_head *lh, *lh2;
1139 FILE *swl;
1140 int rc = 0;
1141
1142 swl = fopen(bs11_sw->swl_fname, "r");
1143 if (!swl)
1144 return -ENODEV;
1145
1146 /* zero the stale file list, if any */
1147 llist_for_each_safe(lh, lh2, &bs11_sw->file_list) {
1148 llist_del(lh);
1149 free(lh);
1150 }
1151
1152 while (fgets(linebuf, sizeof(linebuf), swl)) {
1153 char file_id[12+1];
1154 char file_version[80+1];
1155 struct file_list_entry *fle;
1156 static char dir[PATH_MAX];
1157
1158 if (strlen(linebuf) < 4)
1159 continue;
Harald Welte3ffd1372009-02-01 22:15:49 +00001160
Harald Welte5e4d1b32009-02-01 13:36:56 +00001161 rc = sscanf(linebuf+4, "%12s:%80s\r\n", file_id, file_version);
1162 if (rc < 0) {
1163 perror("ERR parsing SWL file");
1164 rc = -EINVAL;
1165 goto out;
1166 }
1167 if (rc < 2)
1168 continue;
1169
1170 fle = malloc(sizeof(*fle));
1171 if (!fle) {
1172 rc = -ENOMEM;
1173 goto out;
1174 }
1175 memset(fle, 0, sizeof(*fle));
1176
1177 /* construct new filename */
1178 strncpy(dir, bs11_sw->swl_fname, sizeof(dir));
1179 strncat(fle->fname, dirname(dir), sizeof(fle->fname) - 1);
1180 strcat(fle->fname, "/");
1181 strncat(fle->fname, file_id, sizeof(fle->fname) - 1 -strlen(fle->fname));
Harald Welte5e4d1b32009-02-01 13:36:56 +00001182
1183 llist_add_tail(&fle->list, &bs11_sw->file_list);
1184 }
1185
1186out:
1187 fclose(swl);
1188 return rc;
1189}
1190
1191/* bs11 swload specific callback, passed to abis_nm core swload */
1192static int bs11_swload_cbfn(unsigned int hook, unsigned int event,
1193 struct msgb *msg, void *data, void *param)
1194{
1195 struct abis_nm_bs11_sw *bs11_sw = data;
1196 struct file_list_entry *fle;
1197 int rc = 0;
1198
Harald Welte5e4d1b32009-02-01 13:36:56 +00001199 switch (event) {
1200 case NM_MT_LOAD_END_ACK:
1201 fle = fl_dequeue(&bs11_sw->file_list);
1202 if (fle) {
1203 /* start download the next file of our file list */
1204 rc = abis_nm_software_load(bs11_sw->bts, fle->fname,
1205 bs11_sw->win_size,
Harald Welte3ffd1372009-02-01 22:15:49 +00001206 bs11_sw->forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001207 &bs11_swload_cbfn, bs11_sw);
1208 free(fle);
1209 } else {
1210 /* activate the SWL */
1211 rc = abis_nm_software_activate(bs11_sw->bts,
1212 bs11_sw->swl_fname,
1213 bs11_swload_cbfn,
1214 bs11_sw);
1215 }
1216 break;
Harald Welte3ffd1372009-02-01 22:15:49 +00001217 case NM_MT_LOAD_SEG_ACK:
Harald Welte5e4d1b32009-02-01 13:36:56 +00001218 case NM_MT_LOAD_END_NACK:
1219 case NM_MT_LOAD_INIT_ACK:
1220 case NM_MT_LOAD_INIT_NACK:
1221 case NM_MT_ACTIVATE_SW_NACK:
1222 case NM_MT_ACTIVATE_SW_ACK:
1223 default:
1224 /* fallthrough to the user callback */
Harald Welte97ed1e72009-02-06 13:38:02 +00001225 if (bs11_sw->user_cb)
1226 rc = bs11_sw->user_cb(hook, event, msg, NULL, NULL);
Harald Welte5e4d1b32009-02-01 13:36:56 +00001227 break;
1228 }
1229
1230 return rc;
1231}
1232
1233/* Siemens provides a SWL file that is a mere listing of all the other
1234 * files that are part of a software release. We need to upload first
1235 * the list file, and then each file that is listed in the list file */
1236int abis_nm_bs11_load_swl(struct gsm_bts *bts, const char *fname,
Harald Welte3ffd1372009-02-01 22:15:49 +00001237 u_int8_t win_size, int forced, gsm_cbfn *cbfn)
Harald Welte5e4d1b32009-02-01 13:36:56 +00001238{
1239 struct abis_nm_bs11_sw *bs11_sw = g_bs11_sw;
1240 struct file_list_entry *fle;
1241 int rc = 0;
1242
1243 INIT_LLIST_HEAD(&bs11_sw->file_list);
1244 bs11_sw->bts = bts;
1245 bs11_sw->win_size = win_size;
1246 bs11_sw->user_cb = cbfn;
Harald Welte3ffd1372009-02-01 22:15:49 +00001247 bs11_sw->forced = forced;
Harald Welte5e4d1b32009-02-01 13:36:56 +00001248
1249 strncpy(bs11_sw->swl_fname, fname, sizeof(bs11_sw->swl_fname));
1250 rc = bs11_read_swl_file(bs11_sw);
1251 if (rc < 0)
1252 return rc;
1253
1254 /* dequeue next item in file list */
1255 fle = fl_dequeue(&bs11_sw->file_list);
1256 if (!fle)
1257 return -EINVAL;
1258
1259 /* start download the next file of our file list */
Harald Welte3ffd1372009-02-01 22:15:49 +00001260 rc = abis_nm_software_load(bts, fle->fname, win_size, forced,
Harald Welte5e4d1b32009-02-01 13:36:56 +00001261 bs11_swload_cbfn, bs11_sw);
1262 free(fle);
1263 return rc;
1264}
1265
Harald Welte5083b0b2009-02-02 19:20:52 +00001266#if 0
Harald Welte5e4d1b32009-02-01 13:36:56 +00001267static u_int8_t req_attr_btse[] = {
1268 NM_ATT_ADM_STATE, NM_ATT_BS11_LMT_LOGON_SESSION,
1269 NM_ATT_BS11_LMT_LOGIN_TIME, NM_ATT_BS11_LMT_USER_ACC_LEV,
1270 NM_ATT_BS11_LMT_USER_NAME,
1271
1272 0xaf, NM_ATT_BS11_RX_OFFSET, NM_ATT_BS11_VENDOR_NAME,
1273
1274 NM_ATT_BS11_SW_LOAD_INTENDED, NM_ATT_BS11_SW_LOAD_SAFETY,
1275
1276 NM_ATT_BS11_SW_LOAD_STORED };
1277
1278static u_int8_t req_attr_btsm[] = {
1279 NM_ATT_ABIS_CHANNEL, NM_ATT_TEI, NM_ATT_BS11_ABIS_EXT_TIME,
1280 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xce, NM_ATT_FILE_ID,
1281 NM_ATT_FILE_VERSION, NM_ATT_OPER_STATE, 0xe8, NM_ATT_BS11_ALL_TEST_CATG,
1282 NM_ATT_SW_DESCR, NM_ATT_GET_ARI };
Harald Welte5083b0b2009-02-02 19:20:52 +00001283#endif
Harald Welte5e4d1b32009-02-01 13:36:56 +00001284
1285static u_int8_t req_attr[] = {
1286 NM_ATT_ADM_STATE, NM_ATT_AVAIL_STATUS, 0xa8, NM_ATT_OPER_STATE,
1287 0xd5, 0xa1, NM_ATT_BS11_ESN_FW_CODE_NO, NM_ATT_BS11_ESN_HW_CODE_NO,
1288 0x42, NM_ATT_BS11_ESN_PCB_SERIAL };
1289
1290int abis_nm_bs11_get_serno(struct gsm_bts *bts)
1291{
1292 struct abis_om_hdr *oh;
1293 struct msgb *msg = nm_msgb_alloc();
1294
1295 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1296 /* SiemensHW CCTRL object */
1297 fill_om_fom_hdr(oh, 2+sizeof(req_attr), NM_MT_GET_ATTR, NM_OC_BS11,
1298 0x03, 0x00, 0x00);
1299 msgb_tlv_put(msg, NM_ATT_LIST_REQ_ATTR, sizeof(req_attr), req_attr);
1300
1301 return abis_nm_sendmsg(bts, msg);
1302}
Harald Welte268bb402009-02-01 19:11:56 +00001303
1304int abis_nm_bs11_set_ext_time(struct gsm_bts *bts)
1305{
1306 struct abis_om_hdr *oh;
1307 struct msgb *msg = nm_msgb_alloc();
1308 struct bs11_date_time aet;
1309
1310 get_bs11_date_time(&aet);
1311 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
1312 /* SiemensHW CCTRL object */
1313 fill_om_fom_hdr(oh, 2+sizeof(aet), NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER,
1314 0xff, 0xff, 0xff);
Harald Welte5083b0b2009-02-02 19:20:52 +00001315 msgb_tlv_put(msg, NM_ATT_BS11_ABIS_EXT_TIME, sizeof(aet), (u_int8_t *) &aet);
Harald Welte268bb402009-02-01 19:11:56 +00001316
1317 return abis_nm_sendmsg(bts, msg);
1318}