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