OpenBSC API change.
[lcr.git] / bootstrap.c
1 /* Bootstrapping GSM - taken from bsc_hack.c */
2
3 /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4  * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <time.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32
33 #ifndef _GNU_SOURCE
34 #define _GNU_SOURCE
35 #endif
36 #include <getopt.h>
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 #include <openbsc/gsm_data.h>
42 #include <openbsc/gsm_04_08.h>
43 #include <openbsc/db.h>
44 #include <openbsc/timer.h>
45 #include <openbsc/select.h>
46 #include <openbsc/abis_rsl.h>
47 #include <openbsc/abis_nm.h>
48 #include <openbsc/debug.h>
49 #include <openbsc/misdn.h>
50 #include <openbsc/telnet_interface.h>
51 #include <openbsc/paging.h>
52 #include <openbsc/e1_input.h>
53
54 /* The following definitions are for OM and NM packets that we cannot yet
55  * generate by code but we just pass on */
56
57 // BTS Site Manager, SET ATTRIBUTES
58
59 /*
60   Object Class: BTS Site Manager
61   Instance 1: FF
62   Instance 2: FF
63   Instance 3: FF
64 SET ATTRIBUTES
65   sAbisExternalTime: 2007/09/08   14:36:11
66   omLAPDRelTimer: 30sec
67   shortLAPDIntTimer: 5sec
68   emergencyTimer1: 10 minutes
69   emergencyTimer2: 0 minutes
70 */
71
72 unsigned char msg_1[] = 
73 {
74         0xD0, 0x00, 0xFF, 0xFF, 0xFF, 
75                 NM_ATT_BS11_ABIS_EXT_TIME, 0x07, 0xD7, 0x09, 0x08, 0x0E, 0x24, 0x0B, 0xCE, 
76                 0x02, 0x00, 0x1E, 
77                 0xE8, 0x01, 0x05,
78                 0x42, 0x02, 0x00, 0x0A, 
79                 0x44, 0x02, 0x00, 0x00
80 };
81
82 // BTS, SET BTS ATTRIBUTES
83
84 /*
85   Object Class: BTS
86   BTS relat. Number: 0 
87   Instance 2: FF
88   Instance 3: FF
89 SET BTS ATTRIBUTES
90   bsIdentityCode / BSIC:
91     PLMN_colour_code: 7h
92     BS_colour_code:   7h
93   BTS Air Timer T3105: 4  ,unit 10 ms
94   btsIsHopping: FALSE
95   periodCCCHLoadIndication: 1sec
96   thresholdCCCHLoadIndication: 0%
97   cellAllocationNumber: 00h = GSM 900
98   enableInterferenceClass: 00h =  Disabled
99   fACCHQual: 6 (FACCH stealing flags minus 1)
100   intaveParameter: 31 SACCH multiframes
101   interferenceLevelBoundaries:
102     Interference Boundary 1: 0Ah 
103     Interference Boundary 2: 0Fh
104     Interference Boundary 3: 14h
105     Interference Boundary 4: 19h
106     Interference Boundary 5: 1Eh
107   mSTxPwrMax: 11
108       GSM range:     2=39dBm, 15=13dBm, stepsize 2 dBm 
109       DCS1800 range: 0=30dBm, 15=0dBm, stepsize 2 dBm 
110       PCS1900 range: 0=30dBm, 15=0dBm, stepsize 2 dBm 
111                     30=33dBm, 31=32dBm 
112   ny1:
113     Maximum number of repetitions for PHYSICAL INFORMATION message (GSM 04.08): 20
114   powerOutputThresholds:
115     Out Power Fault Threshold:     -10 dB
116     Red Out Power Threshold:       - 6 dB
117     Excessive Out Power Threshold:   5 dB
118   rACHBusyThreshold: -127 dBm 
119   rACHLoadAveragingSlots: 250 ,number of RACH burst periods
120   rfResourceIndicationPeriod: 125  SACCH multiframes 
121   T200:
122     SDCCH:                044 in  5 ms
123     FACCH/Full rate:      031 in  5 ms
124     FACCH/Half rate:      041 in  5 ms
125     SACCH with TCH SAPI0: 090 in 10 ms
126     SACCH with SDCCH:     090 in 10 ms
127     SDCCH with SAPI3:     090 in  5 ms
128     SACCH with TCH SAPI3: 135 in 10 ms
129   tSync: 9000 units of 10 msec
130   tTrau: 9000 units of 10 msec
131   enableUmLoopTest: 00h =  disabled
132   enableExcessiveDistance: 00h =  Disabled
133   excessiveDistance: 64km
134   hoppingMode: 00h = baseband hopping
135   cellType: 00h =  Standard Cell
136   BCCH ARFCN / bCCHFrequency: 1
137 */
138
139 unsigned char msg_2[] = 
140 {
141         0x41, 0x01, 0x00, 0xFF, 0xFF,
142                 NM_ATT_BSIC, 0x3F,
143                 NM_ATT_BTS_AIR_TIMER, 0x04,
144                 NM_ATT_BS11_BTSLS_HOPPING, 0x00,
145                 NM_ATT_CCCH_L_I_P, 0x01,
146                 NM_ATT_CCCH_L_T, 0x00,
147                 NM_ATT_BS11_CELL_ALLOC_NR, 0x00,
148                 NM_ATT_BS11_ENA_INTERF_CLASS, 0x00,
149                 NM_ATT_BS11_FACCH_QUAL, 0x06,
150                 NM_ATT_INTAVE_PARAM, 0x1F, 
151                 NM_ATT_INTERF_BOUND, 0x0A, 0x0F, 0x14, 0x19, 0x1E, 0x7B,
152                 NM_ATT_CCCH_L_T, 0x23,
153                 NM_ATT_GSM_TIME, 0x28, 0x00,
154                 NM_ATT_ADM_STATE, 0x03,
155                 NM_ATT_RACH_B_THRESH, 0x7F,
156                 NM_ATT_LDAVG_SLOTS, 0x00, 0xFA,
157                 NM_ATT_BS11_RF_RES_IND_PER, 0x7D,
158                 NM_ATT_T200, 0x2C, 0x1F, 0x29, 0x5A, 0x5A, 0x5A, 0x87,
159                 NM_ATT_BS11_TSYNC, 0x23, 0x28,
160                 NM_ATT_BS11_TTRAU, 0x23, 0x28, 
161                 NM_ATT_TEST_DUR, 0x01, 0x00,
162                 NM_ATT_OUTST_ALARM, 0x01, 0x00,
163                 NM_ATT_BS11_EXCESSIVE_DISTANCE, 0x01, 0x40,
164                 NM_ATT_BS11_HOPPING_MODE, 0x01, 0x00,
165                 NM_ATT_BS11_PLL, 0x01, 0x00, 
166                 NM_ATT_BCCH_ARFCN, 0x00, HARDCODED_ARFCN/*0x01*/, 
167 };
168
169 // Handover Recognition, SET ATTRIBUTES
170
171 /*
172 Illegal Contents GSM Formatted O&M Msg 
173   Object Class: Handover Recognition
174   BTS relat. Number: 0 
175   Instance 2: FF
176   Instance 3: FF
177 SET ATTRIBUTES
178   enableDelayPowerBudgetHO: 00h = Disabled
179   enableDistanceHO: 00h =  Disabled
180   enableInternalInterCellHandover: 00h = Disabled
181   enableInternalIntraCellHandover: 00h =  Disabled
182   enablePowerBudgetHO: 00h = Disabled
183   enableRXLEVHO: 00h =  Disabled
184   enableRXQUALHO: 00h =  Disabled
185   hoAveragingDistance: 8  SACCH multiframes 
186   hoAveragingLev:
187     A_LEV_HO: 8  SACCH multiframes 
188     W_LEV_HO: 1  SACCH multiframes 
189   hoAveragingPowerBudget:  16  SACCH multiframes 
190   hoAveragingQual:
191     A_QUAL_HO: 8  SACCH multiframes 
192     W_QUAL_HO: 2  SACCH multiframes 
193   hoLowerThresholdLevDL: (10 - 110) dBm
194   hoLowerThresholdLevUL: (5 - 110) dBm
195   hoLowerThresholdQualDL: 06h =   6.4% < BER < 12.8%
196   hoLowerThresholdQualUL: 06h =   6.4% < BER < 12.8%
197   hoThresholdLevDLintra : (20 - 110) dBm
198   hoThresholdLevULintra: (20 - 110) dBm
199   hoThresholdMsRangeMax: 20 km 
200   nCell: 06h
201   timerHORequest: 3  ,unit 2 SACCH multiframes 
202 */
203
204 unsigned char msg_3[] = 
205 {
206         0xD0, 0xA1, 0x00, 0xFF, 0xFF, 
207                 0xD0, 0x00,
208                 0x64, 0x00,
209                 0x67, 0x00,
210                 0x68, 0x00,
211                 0x6A, 0x00,
212                 0x6C, 0x00,
213                 0x6D, 0x00,
214                 0x6F, 0x08,
215                 0x70, 0x08, 0x01,
216                 0x71, 0x10, 0x10, 0x10,
217                 0x72, 0x08, 0x02,
218                 0x73, 0x0A,
219                 0x74, 0x05,
220                 0x75, 0x06,
221                 0x76, 0x06,
222                 0x78, 0x14,
223                 0x79, 0x14,
224                 0x7A, 0x14,
225                 0x7D, 0x06,
226                 0x92, 0x03, 0x20, 0x01, 0x00,
227                 0x45, 0x01, 0x00,
228                 0x48, 0x01, 0x00,
229                 0x5A, 0x01, 0x00,
230                 0x5B, 0x01, 0x05,
231                 0x5E, 0x01, 0x1A,
232                 0x5F, 0x01, 0x20,
233                 0x9D, 0x01, 0x00,
234                 0x47, 0x01, 0x00,
235                 0x5C, 0x01, 0x64,
236                 0x5D, 0x01, 0x1E,
237                 0x97, 0x01, 0x20,
238                 0xF7, 0x01, 0x3C,
239 };
240
241 // Power Control, SET ATTRIBUTES
242
243 /*
244   Object Class: Power Control
245   BTS relat. Number: 0 
246   Instance 2: FF
247   Instance 3: FF
248 SET ATTRIBUTES
249   enableMsPowerControl: 00h =  Disabled
250   enablePowerControlRLFW: 00h =  Disabled
251   pcAveragingLev:
252     A_LEV_PC: 4  SACCH multiframes 
253     W_LEV_PC: 1  SACCH multiframes 
254   pcAveragingQual:
255     A_QUAL_PC: 4  SACCH multiframes 
256     W_QUAL_PC: 2  SACCH multiframes 
257   pcLowerThresholdLevDL: 0Fh
258   pcLowerThresholdLevUL: 0Ah
259   pcLowerThresholdQualDL: 05h =   3.2% < BER <  6.4%
260   pcLowerThresholdQualUL: 05h =   3.2% < BER <  6.4%
261   pcRLFThreshold: 0Ch
262   pcUpperThresholdLevDL: 14h
263   pcUpperThresholdLevUL: 0Fh
264   pcUpperThresholdQualDL: 04h =   1.6% < BER <  3.2%
265   pcUpperThresholdQualUL: 04h =   1.6% < BER <  3.2%
266   powerConfirm: 2  ,unit 2 SACCH multiframes 
267   powerControlInterval: 2  ,unit 2 SACCH multiframes 
268   powerIncrStepSize: 02h = 4 dB
269   powerRedStepSize: 01h = 2 dB
270   radioLinkTimeoutBs: 64  SACCH multiframes 
271   enableBSPowerControl: 00h =  disabled
272 */
273
274 unsigned char msg_4[] = 
275 {
276         0xD0, 0xA2, 0x00, 0xFF, 0xFF, 
277                 NM_ATT_BS11_ENA_MS_PWR_CTRL, 0x00,
278                 NM_ATT_BS11_ENA_PWR_CTRL_RLFW, 0x00,
279                 0x7E, 0x04, 0x01,
280                 0x7F, 0x04, 0x02,
281                 0x80, 0x0F,
282                 0x81, 0x0A,
283                 0x82, 0x05,
284                 0x83, 0x05,
285                 0x84, 0x0C, 
286                 0x85, 0x14, 
287                 0x86, 0x0F, 
288                 0x87, 0x04,
289                 0x88, 0x04,
290                 0x89, 0x02,
291                 0x8A, 0x02,
292                 0x8B, 0x02,
293                 0x8C, 0x01,
294                 0x8D, 0x40,
295                 0x65, 0x01, 0x00 // set to 0x01 to enable BSPowerControl
296 };
297
298
299 // Transceiver, SET TRX ATTRIBUTES (TRX 0)
300
301 /*
302   Object Class: Transceiver
303   BTS relat. Number: 0 
304   Tranceiver number: 0 
305   Instance 3: FF
306 SET TRX ATTRIBUTES
307   aRFCNList (HEX):  0001
308   txPwrMaxReduction: 00h =   30dB
309   radioMeasGran: 254  SACCH multiframes 
310   radioMeasRep: 01h =  enabled
311   memberOfEmergencyConfig: 01h =  TRUE
312   trxArea: 00h = TRX doesn't belong to a concentric cell
313 */
314
315 unsigned char msg_6[] = 
316 {
317         0x44, 0x02, 0x00, 0x00, 0xFF, 
318                 NM_ATT_ARFCN_LIST, 0x01, 0x00, HARDCODED_ARFCN /*0x01*/,
319                 NM_ATT_RF_MAXPOWR_R, 0x00,
320                 NM_ATT_BS11_RADIO_MEAS_GRAN, 0x01, 0xFE, 
321                 NM_ATT_BS11_RADIO_MEAS_REP, 0x01, 0x01,
322                 NM_ATT_BS11_EMRG_CFG_MEMBER, 0x01, 0x01,
323                 NM_ATT_BS11_TRX_AREA, 0x01, 0x00, 
324 };
325
326 static unsigned char nanobts_attr_bts[] = {
327         NM_ATT_INTERF_BOUND, 0x55, 0x5b, 0x61, 0x67, 0x6d, 0x73,
328         NM_ATT_INTAVE_PARAM, 0x06,
329         NM_ATT_CONN_FAIL_CRIT, 0x00, 0x02, 0x01, 0x10, 
330         NM_ATT_T200, 0x1e, 0x24, 0x24, 0xa8, 0x34, 0x21, 0xa8,
331         NM_ATT_MAX_TA, 0x3f,
332         NM_ATT_OVERL_PERIOD, 0x00, 0x01, 10, /* seconds */
333         NM_ATT_CCCH_L_T, 10, /* percent */
334         NM_ATT_CCCH_L_I_P, 1, /* seconds */
335         NM_ATT_RACH_B_THRESH, 0x0a,
336         NM_ATT_LDAVG_SLOTS, 0x03, 0xe8,
337         NM_ATT_BTS_AIR_TIMER, 0x80,
338         NM_ATT_NY1, 0x0a,
339         NM_ATT_BCCH_ARFCN, HARDCODED_ARFCN >> 8, HARDCODED_ARFCN & 0xff,
340         NM_ATT_BSIC, 0x20,
341 };
342
343 static unsigned char nanobts_attr_radio[] = {
344         NM_ATT_RF_MAXPOWR_R, 0x0c,
345         NM_ATT_ARFCN_LIST, 0x00, 0x02, HARDCODED_ARFCN >> 8, HARDCODED_ARFCN & 0xff,
346 };
347
348 static unsigned char nanobts_attr_e0[] = {
349         0x85, 0x00,
350         0x81, 0x0b, 0xbb,       /* TCP PORT for RSL */
351 };
352
353 int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
354                    struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
355 {
356         struct gsm_bts *bts;
357         struct gsm_bts_trx *trx;
358         struct gsm_bts_trx_ts *ts;
359
360         /* This is currently only required on nanoBTS */
361
362         switch (evt) {
363         case EVT_STATECHG_OPER:
364                 switch (obj_class) {
365                 case NM_OC_SITE_MANAGER:
366                         bts = container_of(obj, struct gsm_bts, site_mgr);
367                         if (old_state->operational != 2 && new_state->operational == 2) {
368                                 abis_nm_opstart(bts, NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff);
369                         }
370                         break;
371                 case NM_OC_BTS:
372                         bts = (struct gsm_bts *)obj;
373                         if (new_state->availability == 5) {
374                                 abis_nm_set_bts_attr(bts, nanobts_attr_bts,
375                                                         sizeof(nanobts_attr_bts));
376                                 abis_nm_opstart(bts, NM_OC_BTS,
377                                                 bts->nr, 0xff, 0xff);
378                                 abis_nm_chg_adm_state(bts, NM_OC_BTS,
379                                                       bts->nr, 0xff, 0xff,
380                                                       NM_STATE_UNLOCKED);
381                         }
382                         break;
383                 case NM_OC_RADIO_CARRIER:
384                         trx = (struct gsm_bts_trx *)obj;
385                         if (new_state->availability == 3) {
386                                 abis_nm_set_radio_attr(trx, nanobts_attr_radio,
387                                                         sizeof(nanobts_attr_radio));
388                                 abis_nm_opstart(trx->bts, NM_OC_RADIO_CARRIER,
389                                                 trx->bts->nr, trx->nr, 0xff);
390                                 abis_nm_chg_adm_state(trx->bts, NM_OC_RADIO_CARRIER,
391                                                       trx->bts->nr, trx->nr, 0xff,
392                                                       NM_STATE_UNLOCKED);
393                         }
394                         break;
395                 case NM_OC_CHANNEL:
396                         ts = (struct gsm_bts_trx_ts *)obj;
397                         trx = (struct gsm_bts_trx *)ts->trx;
398                         if (new_state->availability == 5) {
399                                 if (ts->nr == 0 && trx == trx->bts->c0)
400                                         abis_nm_set_channel_attr(ts, NM_CHANC_BCCH_CBCH);
401                                 else
402                                         abis_nm_set_channel_attr(ts, NM_CHANC_TCHFull);
403                                 abis_nm_opstart(trx->bts, NM_OC_CHANNEL,
404                                                 trx->bts->nr, trx->nr, ts->nr);
405                                 abis_nm_chg_adm_state(trx->bts, NM_OC_CHANNEL,
406                                                       trx->bts->nr, trx->nr, ts->nr,
407                                                       NM_STATE_UNLOCKED);
408                         }
409                         break;
410                 case NM_OC_BASEB_TRANSC:
411                         trx = container_of(obj, struct gsm_bts_trx, bb_transc);
412                         if (new_state->availability == 5) {
413                                 abis_nm_ipaccess_msg(trx->bts, 0xe0, NM_OC_BASEB_TRANSC,
414                                                      trx->bts->nr, trx->nr, 0xff,
415                                                      nanobts_attr_e0, sizeof(nanobts_attr_e0));
416                                 abis_nm_opstart(trx->bts, NM_OC_BASEB_TRANSC, 
417                                                 trx->bts->nr, trx->nr, 0xff);
418                                 abis_nm_chg_adm_state(trx->bts, NM_OC_BASEB_TRANSC, 
419                                                         trx->bts->nr, trx->nr, 0xff,
420                                                         NM_STATE_UNLOCKED);
421                         }
422                         break;
423                 }
424                 break;
425         case EVT_STATECHG_ADM:
426                 DEBUGP(DMM, "Unhandled state change in %s:%d\n", __func__, __LINE__);
427                 break;
428         }
429         return 0;
430 }
431
432 static void bootstrap_om_nanobts(struct gsm_bts *bts)
433 {
434         /* We don't do callback based bootstrapping, but event driven (see above) */
435 }
436
437 static void bootstrap_om_bs11(struct gsm_bts *bts)
438 {
439         struct gsm_bts_trx *trx = &bts->trx[0];
440
441         /* stop sending event reports */
442         abis_nm_event_reports(bts, 0);
443
444         /* begin DB transmission */
445         abis_nm_bs11_db_transmission(bts, 1);
446
447         /* end DB transmission */
448         abis_nm_bs11_db_transmission(bts, 0);
449
450         /* Reset BTS Site manager resource */
451         abis_nm_bs11_reset_resource(bts);
452
453         /* begin DB transmission */
454         abis_nm_bs11_db_transmission(bts, 1);
455
456         abis_nm_raw_msg(bts, sizeof(msg_1), msg_1); /* set BTS SiteMgr attr*/
457         abis_nm_raw_msg(bts, sizeof(msg_2), msg_2); /* set BTS attr */
458         abis_nm_raw_msg(bts, sizeof(msg_3), msg_3); /* set BTS handover attr */
459         abis_nm_raw_msg(bts, sizeof(msg_4), msg_4); /* set BTS power control attr */
460
461         /* Connect signalling of bts0/trx0 to e1_0/ts1/64kbps */
462         abis_nm_conn_terr_sign(trx, 0, 1, 0xff);
463         set_ts_e1link(&trx->ts[0], 0, 1, 0xff);
464         abis_nm_raw_msg(bts, sizeof(msg_6), msg_6); /* SET TRX ATTRIBUTES */
465
466         /* Use TEI 1 for signalling */
467         abis_nm_establish_tei(bts, 0, 0, 1, 0xff, 0x01);
468         abis_nm_set_channel_attr(&trx->ts[0], NM_CHANC_SDCCH_CBCH);
469
470 #ifdef HAVE_TRX1
471         /* TRX 1 */
472         abis_nm_conn_terr_sign(&bts->trx[1], 0, 1, 0xff);
473         /* FIXME: TRX ATTRIBUTE */
474         abis_nm_establish_tei(bts, 0, 0, 1, 0xff, 0x02);
475 #endif
476
477         /* SET CHANNEL ATTRIBUTE TS1 */
478         abis_nm_set_channel_attr(&trx->ts[1], NM_CHANC_TCHFull);
479         /* Connect traffic of bts0/trx0/ts1 to e1_0/ts2/b */
480         set_ts_e1link(&trx->ts[1], 0, 2, 1);
481         abis_nm_conn_terr_traf(&trx->ts[1], 0, 2, 1);
482         
483         /* SET CHANNEL ATTRIBUTE TS2 */
484         abis_nm_set_channel_attr(&trx->ts[2], NM_CHANC_TCHFull);
485         /* Connect traffic of bts0/trx0/ts2 to e1_0/ts2/c */
486         set_ts_e1link(&trx->ts[2], 0, 2, 2);
487         abis_nm_conn_terr_traf(&trx->ts[2], 0, 2, 2);
488
489         /* SET CHANNEL ATTRIBUTE TS3 */
490         abis_nm_set_channel_attr(&trx->ts[3], NM_CHANC_TCHFull);
491         /* Connect traffic of bts0/trx0/ts3 to e1_0/ts2/d */
492         set_ts_e1link(&trx->ts[3], 0, 2, 3);
493         abis_nm_conn_terr_traf(&trx->ts[3], 0, 2, 3);
494
495         /* SET CHANNEL ATTRIBUTE TS4 */
496         abis_nm_set_channel_attr(&trx->ts[4], NM_CHANC_TCHFull);
497         /* Connect traffic of bts0/trx0/ts4 to e1_0/ts3/a */
498         set_ts_e1link(&trx->ts[4], 0, 3, 0);
499         abis_nm_conn_terr_traf(&trx->ts[4], 0, 3, 0);
500
501         /* SET CHANNEL ATTRIBUTE TS5 */
502         abis_nm_set_channel_attr(&trx->ts[5], NM_CHANC_TCHFull);
503         /* Connect traffic of bts0/trx0/ts5 to e1_0/ts3/b */
504         set_ts_e1link(&trx->ts[5], 0, 3, 1);
505         abis_nm_conn_terr_traf(&trx->ts[5], 0, 3, 1);
506
507         /* SET CHANNEL ATTRIBUTE TS6 */
508         abis_nm_set_channel_attr(&trx->ts[6], NM_CHANC_TCHFull);
509         /* Connect traffic of bts0/trx0/ts6 to e1_0/ts3/c */
510         set_ts_e1link(&trx->ts[6], 0, 3, 2);
511         abis_nm_conn_terr_traf(&trx->ts[6], 0, 3, 2);
512
513         /* SET CHANNEL ATTRIBUTE TS7 */
514         abis_nm_set_channel_attr(&trx->ts[7], NM_CHANC_TCHFull);
515         /* Connect traffic of bts0/trx0/ts7 to e1_0/ts3/d */
516         set_ts_e1link(&trx->ts[7], 0, 3, 3);
517         abis_nm_conn_terr_traf(&trx->ts[7], 0, 3, 3);
518
519         /* end DB transmission */
520         abis_nm_bs11_db_transmission(bts, 0);
521
522         /* Reset BTS Site manager resource */
523         abis_nm_bs11_reset_resource(bts);
524
525         /* restart sending event reports */
526         abis_nm_event_reports(bts, 1);
527 }
528
529 static void bootstrap_om(struct gsm_bts *bts)
530 {
531         fprintf(stdout, "bootstrapping OML\n");
532
533         switch (bts->type) {
534         case GSM_BTS_TYPE_BS11:
535                 bootstrap_om_bs11(bts);
536                 break;
537         case GSM_BTS_TYPE_NANOBTS_900:
538         case GSM_BTS_TYPE_NANOBTS_1800:
539                 bootstrap_om_nanobts(bts);
540                 break;
541         default:
542                 fprintf(stderr, "Unable to bootstrap OML: Unknown BTS type %d\n", bts->type);
543         }
544 }
545
546 static int shutdown_om(struct gsm_bts *bts)
547 {
548         /* stop sending event reports */
549         abis_nm_event_reports(bts, 0);
550
551         /* begin DB transmission */
552         abis_nm_bs11_db_transmission(bts, 1);
553
554         /* end DB transmission */
555         abis_nm_bs11_db_transmission(bts, 0);
556
557         /* Reset BTS Site manager resource */
558         abis_nm_bs11_reset_resource(bts);
559
560         return 0;
561 }
562
563 struct bcch_info {
564         u_int8_t type;
565         u_int8_t len;
566         const u_int8_t *data;
567 };
568
569 /*
570 SYSTEM INFORMATION TYPE 1
571   Cell channel description
572     Format-ID bit map 0
573     CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01
574   RACH Control Parameters
575     maximum 7 retransmissions
576     8 slots used to spread transmission
577     cell not barred for access
578     call reestablishment not allowed
579     Access Control Class = 0000
580 */
581 static u_int8_t si1[] = {
582         /* header */0x55, 0x06, 0x19,
583         /* ccdesc */0x04 /*0x00*/, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
584         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /*0x01*/,
585         /* rach */0xD5, 0x00, 0x00,
586         /* s1 reset*/0x2B
587 };
588
589 /*
590  SYSTEM INFORMATION TYPE 2
591   Neighbour Cells Description
592     EXT-IND: Carries the complete BA
593     BA-IND = 0
594     Format-ID bit map 0
595     CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
596   NCC permitted (NCC) = FF
597   RACH Control Parameters
598     maximum 7 retransmissions
599     8 slots used to spread transmission
600     cell not barred for access
601     call reestablishment not allowed
602     Access Control Class = 0000
603 */
604 static u_int8_t si2[] = {
605         /* header */0x59, 0x06, 0x1A,
606         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
607         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
608         /* ncc */0xFF,
609         /* rach*/0xD5, 0x00, 0x00
610 };
611
612 /*
613 SYSTEM INFORMATION TYPE 3
614   Cell identity = 00001 (1h)
615   Location area identification
616     Mobile Country Code (MCC): 001
617     Mobile Network Code (MNC): 01
618     Location Area Code  (LAC): 00001 (1h)
619   Control Channel Description
620     Attach-detach: MSs in the cell are not allowed to apply IMSI attach /detach
621     0 blocks reserved for access grant
622     1 channel used for CCCH, with SDCCH
623     5 multiframes period for PAGING REQUEST
624     Time-out T3212 = 0
625   Cell Options BCCH
626     Power control indicator: not set
627     MSs shall not use uplink DTX
628     Radio link timeout = 36
629   Cell Selection Parameters
630     Cell reselect hysteresis = 6 dB RXLEV hysteresis for LA re-selection
631     max.TX power level MS may use for CCH = 2 <- according to GSM05.05 39dBm (max)
632     Additional Reselect Parameter Indication (ACS) = only SYSTEM INFO 4: The SI rest octets, if present, shall be used to derive the value of PI and possibly C2 parameters
633     Half rate support (NECI): New establishment causes are not supported
634     min.RX signal level for MS = 0
635   RACH Control Parameters
636     maximum 7 retransmissions
637     8 slots used to spread transmission
638     cell not barred for access
639     call reestablishment not allowed
640     Access Control Class = 0000
641   SI 3 Rest Octets
642     Cell Bar Qualify (CBQ): 0
643     Cell Reselect Offset = 0 dB
644     Temporary Offset = 0 dB
645     Penalty Time = 20 s
646     System Information 2ter Indicator (2TI): 0 = not available
647     Early Classmark Sending Control (ECSC):  0 = forbidden
648     Scheduling Information is not sent in SYSTEM INFORMATION TYPE 9 on the BCCH
649 */
650 static u_int8_t si3[] = {
651         /* header */0x49, 0x06, 0x1B,
652         /* cell */0x00, 0x01,
653         /* lai  */0x00, 0xF1, 0x10, 0x00, 0x01,
654         /* desc */0x01, 0x03, 0x00,
655         /* option*/0x28,
656         /* selection*/0x62, 0x00,
657         /* rach */0xD5, 0x00, 0x00,
658         /* reset*/0x80, 0x00, 0x00, 0x2B
659 };
660
661 /*
662 SYSTEM INFORMATION TYPE 4
663   Location area identification
664     Mobile Country Code (MCC): 001
665     Mobile Network Code (MNC): 01
666     Location Area Code  (LAC): 00001 (1h)
667   Cell Selection Parameters
668     Cell reselect hysteresis = 6 dB RXLEV hysteresis for LA re-selection
669     max.TX power level MS may use for CCH = 2
670     Additional Reselect Parameter Indication (ACS) = only SYSTEM INFO 4: The SI rest octets, if present, shall be used to derive the value of PI and possibly C2 parameters
671     Half rate support (NECI): New establishment causes are not supported
672     min.RX signal level for MS = 0
673   RACH Control Parameters
674     maximum 7 retransmissions
675     8 slots used to spread transmission
676     cell not barred for access
677     call reestablishment not allowed
678     Access Control Class = 0000
679   Channel Description
680     Type = SDCCH/4[2]
681     Timeslot Number: 0
682     Training Sequence Code: 7h
683     ARFCN: 1
684   SI Rest Octets
685     Cell Bar Qualify (CBQ): 0
686     Cell Reselect Offset = 0 dB
687     Temporary Offset = 0 dB
688     Penalty Time = 20 s
689 */
690 static u_int8_t si4[] = {
691         /* header */0x41, 0x06, 0x1C,
692         /* lai */0x00, 0xF1, 0x10, 0x00, 0x01,
693         /* sel */0x62, 0x00,
694         /* rach*/0xD5, 0x00, 0x00,
695         /* var */0x64, 0x30, 0xE0, HARDCODED_ARFCN/*0x01*/, 0x80, 0x00, 0x00,
696         0x2B, 0x2B, 0x2B
697 };
698
699 /*
700  SYSTEM INFORMATION TYPE 5
701   Neighbour Cells Description
702     EXT-IND: Carries the complete BA
703     BA-IND = 0
704     Format-ID bit map 0
705     CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
706 */
707
708 static u_int8_t si5[] = {
709         /* header without l2 len*/0x06, 0x1D,
710         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
711         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
712 };
713
714 // SYSTEM INFORMATION TYPE 6
715
716 /*
717 SACCH FILLING
718   System Info Type: SYSTEM INFORMATION 6
719   L3 Information (Hex): 06 1E 00 01 xx xx 10 00 01 28 FF
720
721 SYSTEM INFORMATION TYPE 6
722   Cell identity = 00001 (1h)
723   Location area identification
724     Mobile Country Code (MCC): 001
725     Mobile Network Code (MNC): 01
726     Location Area Code  (LAC): 00001 (1h)
727   Cell Options SACCH
728     Power control indicator: not set
729     MSs shall not use uplink DTX on a TCH-F. MS shall not use uplink DTX on TCH-H.
730     Radio link timeout = 36
731   NCC permitted (NCC) = FF
732 */
733
734 static u_int8_t si6[] = {
735         /* header */0x06, 0x1E,
736         /* cell id*/ 0x00, 0x01,
737         /* lai */ 0x00, 0xF1, 0x10, 0x00, 0x01,
738         /* options */ 0x28,
739         /* ncc */ 0xFF,
740 };
741
742
743
744 static const struct bcch_info bcch_infos[] = {
745         {
746                 RSL_SYSTEM_INFO_1,
747                 sizeof(si1),
748                 si1,
749         }, {
750                 RSL_SYSTEM_INFO_2,
751                 sizeof(si2),
752                 si2,
753         }, {
754                 RSL_SYSTEM_INFO_3,
755                 sizeof(si3),
756                 si3,
757         }, {
758                 RSL_SYSTEM_INFO_4,
759                 sizeof(si4),
760                 si4,
761         },
762 };
763
764 static_assert(sizeof(si1) == sizeof(struct gsm48_system_information_type_1), type1)
765 static_assert(sizeof(si2) == sizeof(struct gsm48_system_information_type_2), type2)
766 static_assert(sizeof(si3) == sizeof(struct gsm48_system_information_type_3), type3)
767 static_assert(sizeof(si4) >= sizeof(struct gsm48_system_information_type_4), type4)
768 static_assert(sizeof(si5) == sizeof(struct gsm48_system_information_type_5), type5)
769 static_assert(sizeof(si6) >= sizeof(struct gsm48_system_information_type_6), type6)
770
771 /* set all system information types */
772 static int set_system_infos(struct gsm_bts_trx *trx)
773 {
774         unsigned int i;
775
776         for (i = 0; i < ARRAY_SIZE(bcch_infos); i++) {
777                 rsl_bcch_info(trx, bcch_infos[i].type,
778                               bcch_infos[i].data,
779                               bcch_infos[i].len);
780         }
781         rsl_sacch_filling(trx, RSL_SYSTEM_INFO_5, si5, sizeof(si5));
782         rsl_sacch_filling(trx, RSL_SYSTEM_INFO_6, si6, sizeof(si6));
783
784         return 0;
785 }
786
787 /*
788  * Patch the various SYSTEM INFORMATION tables to update
789  * the LAI
790  */
791 static void patch_tables(struct gsm_bts *bts)
792 {
793         u_int8_t arfcn_low = bts->trx[0].arfcn & 0xff;
794         u_int8_t arfcn_high = (bts->trx[0].arfcn >> 8) & 0x0f;
795         /* covert the raw packet to the struct */
796         struct gsm48_system_information_type_3 *type_3 =
797                 (struct gsm48_system_information_type_3*)&si3;
798         struct gsm48_system_information_type_4 *type_4 =
799                 (struct gsm48_system_information_type_4*)&si4;
800         struct gsm48_system_information_type_6 *type_6 =
801                 (struct gsm48_system_information_type_6*)&si6;
802         struct gsm48_loc_area_id lai;
803
804         gsm0408_generate_lai(&lai, bts->network->country_code,
805                                 bts->network->network_code, bts->location_area_code);
806
807         /* assign the MCC and MNC */
808         type_3->lai = lai;
809         type_4->lai = lai;
810         type_6->lai = lai;
811
812         /* patch ARFCN into BTS Attributes */
813         msg_2[74] &= 0xf0;
814         msg_2[74] |= arfcn_high;
815         msg_2[75] = arfcn_low;
816         nanobts_attr_bts[42] &= 0xf0;
817         nanobts_attr_bts[42] |= arfcn_high;
818         nanobts_attr_bts[43] = arfcn_low;
819
820         /* patch ARFCN into TRX Attributes */
821         msg_6[7] &= 0xf0;
822         msg_6[7] |= arfcn_high;
823         msg_6[8] = arfcn_low;
824         nanobts_attr_radio[5] &= 0xf0;
825         nanobts_attr_radio[5] |= arfcn_high;
826         nanobts_attr_radio[6] = arfcn_low;
827
828         type_4->data[2] &= 0xf0;
829         type_4->data[2] |= arfcn_high;
830         type_4->data[3] = arfcn_low;
831
832         /* patch Control Channel Description 10.5.2.11 */
833         type_3->control_channel_desc = bts->chan_desc;
834 }
835
836
837 static void bootstrap_rsl(struct gsm_bts_trx *trx)
838 {
839         fprintf(stdout, "bootstrapping RSL MCC=%u MNC=%u\n", trx->bts->network->country_code, trx->bts->network->network_code);
840         set_system_infos(trx);
841 }
842
843 void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
844 {
845         switch (event) {
846         case EVT_E1_TEI_UP:
847                 switch (type) {
848                 case E1INP_SIGN_OML:
849                         bootstrap_om(trx->bts);
850                         break;
851                 case E1INP_SIGN_RSL:
852                         bootstrap_rsl(trx);
853                         break;
854                 default:
855                         break;
856                 }
857                 break;
858         case EVT_E1_TEI_DN:
859                 fprintf(stderr, "Lost some E1 TEI link\n");
860                 /* FIXME: deal with TEI or L1 link loss */
861                 break;
862         default:
863                 break;
864         }
865 }
866
867 static int bootstrap_bts(struct gsm_bts *bts, int lac, int arfcn)
868 {
869         bts->location_area_code = lac;
870         bts->trx[0].arfcn = arfcn;
871
872         /* Control Channel Description */
873         memset(&bts->chan_desc, 0, sizeof(struct gsm48_control_channel_descr));
874         bts->chan_desc.att = 1;
875         bts->chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_1_C;
876         bts->chan_desc.bs_pa_mfrms = RSL_BS_PA_MFRMS_5;
877         bts->chan_desc.t3212 = 0;
878
879         patch_tables(bts);
880
881         paging_init(bts);
882
883         return 0;
884 }
885
886
887 struct gsm_network *bootstrap_network(int (*mncc_recv)(struct gsm_network *, int, void *), int bts_type, int mcc, int mnc, int lac, int arfcn, int cardnr, int release_l2, char *name_short, char *name_long, char *hlr, int allow_all)
888 {
889         struct gsm_bts *bts;
890         struct gsm_network *gsmnet;
891
892         /* seed the PRNG for TMSI */
893         srand(time(NULL));
894
895         /* initialize our data structures */
896         gsmnet = gsm_network_init(2, (gsm_bts_type)bts_type, mcc, mnc, mncc_recv);
897         if (!gsmnet)
898                 return 0;
899
900         /* open database */
901         if (db_init(hlr, gsmnet)) {
902                 fprintf(stderr, "DB: Failed to init HLR database '%s'. Please check the option settings.\n", hlr);
903                 return NULL;
904         }        
905         if (db_prepare()) {
906                 fprintf(stderr, "DB: Failed to prepare database.\n");
907                 return NULL;
908         }
909
910         gsmnet->name_long = name_long;
911         gsmnet->name_short = name_short;
912         bts = &gsmnet->bts[0];
913         bootstrap_bts(bts, lac, arfcn);
914
915         /* Control Channel Description */
916         memset(&bts->chan_desc, 0, sizeof(struct gsm48_control_channel_descr));
917         bts->chan_desc.att = 1;
918         bts->chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_1_C;
919         bts->chan_desc.bs_pa_mfrms = RSL_BS_PA_MFRMS_5;
920         bts->chan_desc.t3212 = 0;
921
922         patch_tables(bts);
923
924         paging_init(bts);
925
926         telnet_init(gsmnet, 4242);
927
928         /* E1 mISDN input setup */
929         if (bts_type == GSM_BTS_TYPE_BS11) {
930                 gsmnet->num_bts = 1;
931                 if (e1_config(bts, cardnr, release_l2))
932                         return NULL;
933         } else {
934                 bts->ip_access.site_id = 1801;
935                 bts->ip_access.bts_id = 0;
936                 bts = &gsmnet->bts[1];
937                 bootstrap_bts(bts, lac, arfcn);
938                 bts->ip_access.site_id = 1800;
939                 bts->ip_access.bts_id = 0;
940                 if (ipaccess_setup(gsmnet))
941                         return NULL;
942
943         }
944
945         if (allow_all)
946                 gsm0408_allow_everyone(1);
947
948         return gsmnet;
949 }
950
951 int shutdown_net(struct gsm_network *network)
952 {
953         struct gsm_network *net = (struct gsm_network *)network;
954         unsigned int i;
955         for (i = 0; i < net->num_bts; i++) {
956                 int rc;
957                 rc = shutdown_om(&net->bts[i]);
958                 if (rc < 0)
959                         return rc;
960         }
961
962         return 0;
963 }
964
965 #ifdef __cplusplus
966 }
967 #endif