blob: 40e2652496aa70fdf8c5a906937160e28d7d328c [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file sysinfo.c
2 * GSM 04.08 System Information (SI) encoding and decoding.
Harald Welteb5503132011-05-24 15:01:53 +02003 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02004/*
5 * (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welteb5503132011-05-24 15:01:53 +02006 *
7 * All Rights Reserved
8 *
Harald Weltee08da972017-11-13 01:00:26 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Harald Welteb5503132011-05-24 15:01:53 +020011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26#include <errno.h>
27#include <string.h>
28#include <stdio.h>
Harald Welteb5503132011-05-24 15:01:53 +020029
30#include <osmocom/core/bitvec.h>
31#include <osmocom/core/utils.h>
32#include <osmocom/gsm/sysinfo.h>
33#include <osmocom/gsm/protocol/gsm_04_08.h>
34#include <osmocom/gsm/protocol/gsm_08_58.h>
35
36/* verify the sizes of the system information type structs */
37
38/* rest octets are not part of the struct */
39osmo_static_assert(sizeof(struct gsm48_system_information_type_header) == 3, _si_header_size);
40osmo_static_assert(sizeof(struct gsm48_rach_control) == 3, _si_rach_control);
41osmo_static_assert(sizeof(struct gsm48_system_information_type_1) == 22, _si1_size);
42osmo_static_assert(sizeof(struct gsm48_system_information_type_2) == 23, _si2_size);
43osmo_static_assert(sizeof(struct gsm48_system_information_type_3) == 19, _si3_size);
44osmo_static_assert(sizeof(struct gsm48_system_information_type_4) == 13, _si4_size);
45
46/* bs11 forgot the l2 len, 0-6 rest octets */
47osmo_static_assert(sizeof(struct gsm48_system_information_type_5) == 18, _si5_size);
48osmo_static_assert(sizeof(struct gsm48_system_information_type_6) == 11, _si6_size);
Maxb31a2b52023-01-26 21:31:53 +030049osmo_static_assert(sizeof(struct gsm48_system_information_type_10) == 1, _si10_size);
Harald Welteb5503132011-05-24 15:01:53 +020050osmo_static_assert(sizeof(struct gsm48_system_information_type_13) == 3, _si13_size);
51
52static const uint8_t sitype2rsl[_MAX_SYSINFO_TYPE] = {
53 [SYSINFO_TYPE_1] = RSL_SYSTEM_INFO_1,
54 [SYSINFO_TYPE_2] = RSL_SYSTEM_INFO_2,
55 [SYSINFO_TYPE_3] = RSL_SYSTEM_INFO_3,
56 [SYSINFO_TYPE_4] = RSL_SYSTEM_INFO_4,
57 [SYSINFO_TYPE_5] = RSL_SYSTEM_INFO_5,
58 [SYSINFO_TYPE_6] = RSL_SYSTEM_INFO_6,
59 [SYSINFO_TYPE_7] = RSL_SYSTEM_INFO_7,
60 [SYSINFO_TYPE_8] = RSL_SYSTEM_INFO_8,
61 [SYSINFO_TYPE_9] = RSL_SYSTEM_INFO_9,
62 [SYSINFO_TYPE_10] = RSL_SYSTEM_INFO_10,
63 [SYSINFO_TYPE_13] = RSL_SYSTEM_INFO_13,
64 [SYSINFO_TYPE_16] = RSL_SYSTEM_INFO_16,
65 [SYSINFO_TYPE_17] = RSL_SYSTEM_INFO_17,
66 [SYSINFO_TYPE_18] = RSL_SYSTEM_INFO_18,
67 [SYSINFO_TYPE_19] = RSL_SYSTEM_INFO_19,
68 [SYSINFO_TYPE_20] = RSL_SYSTEM_INFO_20,
69 [SYSINFO_TYPE_2bis] = RSL_SYSTEM_INFO_2bis,
70 [SYSINFO_TYPE_2ter] = RSL_SYSTEM_INFO_2ter,
71 [SYSINFO_TYPE_2quater] = RSL_SYSTEM_INFO_2quater,
72 [SYSINFO_TYPE_5bis] = RSL_SYSTEM_INFO_5bis,
73 [SYSINFO_TYPE_5ter] = RSL_SYSTEM_INFO_5ter,
Harald Welte620f7ab2011-06-25 21:39:18 +020074 [SYSINFO_TYPE_EMO] = RSL_EXT_MEAS_ORDER,
75 [SYSINFO_TYPE_MEAS_INFO]= RSL_MEAS_INFO,
Harald Welteb5503132011-05-24 15:01:53 +020076};
77
Sylvain Munautf2699502011-05-29 15:39:04 +020078static const uint8_t rsl2sitype[256] = {
Harald Welteb5503132011-05-24 15:01:53 +020079 [RSL_SYSTEM_INFO_1] = SYSINFO_TYPE_1,
80 [RSL_SYSTEM_INFO_2] = SYSINFO_TYPE_2,
81 [RSL_SYSTEM_INFO_3] = SYSINFO_TYPE_3,
82 [RSL_SYSTEM_INFO_4] = SYSINFO_TYPE_4,
83 [RSL_SYSTEM_INFO_5] = SYSINFO_TYPE_5,
84 [RSL_SYSTEM_INFO_6] = SYSINFO_TYPE_6,
85 [RSL_SYSTEM_INFO_7] = SYSINFO_TYPE_7,
86 [RSL_SYSTEM_INFO_8] = SYSINFO_TYPE_8,
87 [RSL_SYSTEM_INFO_9] = SYSINFO_TYPE_9,
88 [RSL_SYSTEM_INFO_10] = SYSINFO_TYPE_10,
89 [RSL_SYSTEM_INFO_13] = SYSINFO_TYPE_13,
90 [RSL_SYSTEM_INFO_16] = SYSINFO_TYPE_16,
91 [RSL_SYSTEM_INFO_17] = SYSINFO_TYPE_17,
92 [RSL_SYSTEM_INFO_18] = SYSINFO_TYPE_18,
93 [RSL_SYSTEM_INFO_19] = SYSINFO_TYPE_19,
94 [RSL_SYSTEM_INFO_20] = SYSINFO_TYPE_20,
95 [RSL_SYSTEM_INFO_2bis] = SYSINFO_TYPE_2bis,
96 [RSL_SYSTEM_INFO_2ter] = SYSINFO_TYPE_2ter,
97 [RSL_SYSTEM_INFO_2quater] = SYSINFO_TYPE_2quater,
98 [RSL_SYSTEM_INFO_5bis] = SYSINFO_TYPE_5bis,
99 [RSL_SYSTEM_INFO_5ter] = SYSINFO_TYPE_5ter,
Harald Welte620f7ab2011-06-25 21:39:18 +0200100 [RSL_EXT_MEAS_ORDER] = SYSINFO_TYPE_EMO,
101 [RSL_MEAS_INFO] = SYSINFO_TYPE_MEAS_INFO,
Harald Welteb5503132011-05-24 15:01:53 +0200102};
103
104const struct value_string osmo_sitype_strs[_MAX_SYSINFO_TYPE] = {
105 { SYSINFO_TYPE_1, "1" },
106 { SYSINFO_TYPE_2, "2" },
107 { SYSINFO_TYPE_3, "3" },
108 { SYSINFO_TYPE_4, "4" },
109 { SYSINFO_TYPE_5, "5" },
110 { SYSINFO_TYPE_6, "6" },
111 { SYSINFO_TYPE_7, "7" },
112 { SYSINFO_TYPE_8, "8" },
113 { SYSINFO_TYPE_9, "9" },
114 { SYSINFO_TYPE_10, "10" },
115 { SYSINFO_TYPE_13, "13" },
116 { SYSINFO_TYPE_16, "16" },
117 { SYSINFO_TYPE_17, "17" },
118 { SYSINFO_TYPE_18, "18" },
119 { SYSINFO_TYPE_19, "19" },
120 { SYSINFO_TYPE_20, "20" },
121 { SYSINFO_TYPE_2bis, "2bis" },
122 { SYSINFO_TYPE_2ter, "2ter" },
123 { SYSINFO_TYPE_2quater, "2quater" },
124 { SYSINFO_TYPE_5bis, "5bis" },
125 { SYSINFO_TYPE_5ter, "5ter" },
Harald Welte620f7ab2011-06-25 21:39:18 +0200126 { SYSINFO_TYPE_EMO, "EMO" },
127 { SYSINFO_TYPE_MEAS_INFO, "MI" },
Harald Welteb5503132011-05-24 15:01:53 +0200128 { 0, NULL }
129};
130
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200131/*! Add pair of arfcn and measurement bandwith value to earfcn struct
Max03309b52016-03-17 11:51:09 +0100132 * \param[in,out] e earfcn struct
133 * \param[in] arfcn EARFCN value, 16 bits
134 * \param[in] meas_bw measurement bandwith value
135 * \returns 0 on success, error otherwise
136 */
Maxea345cd2016-03-17 14:46:19 +0100137int osmo_earfcn_add(struct osmo_earfcn_si2q *e, uint16_t arfcn, uint8_t meas_bw)
Max03309b52016-03-17 11:51:09 +0100138{
139 size_t i;
140 for (i = 0; i < e->length; i++) {
141 if (OSMO_EARFCN_INVALID == e->arfcn[i]) {
142 e->arfcn[i] = arfcn;
143 e->meas_bw[i] = meas_bw;
144 return 0;
145 }
146 }
147 return -ENOMEM;
148}
149
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200150/*! Return number of bits necessary to represent earfcn struct as
Maxfbb8bfa2016-04-15 16:04:04 +0200151 * Repeated E-UTRAN Neighbour Cells IE from 3GPP TS 44.018 Table 10.5.2.33b.1
152 * \param[in,out] e earfcn struct
153 * \returns number of bits
154 */
155size_t osmo_earfcn_bit_size(const struct osmo_earfcn_si2q *e)
156{
Max91dd2192017-05-11 15:38:10 +0200157 return osmo_earfcn_bit_size_ext(e, 0);
158}
159
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200160/*! Return number of bits necessary to represent earfcn struct as
Max91dd2192017-05-11 15:38:10 +0200161 * Repeated E-UTRAN Neighbour Cells IE from 3GPP TS 44.018 Table 10.5.2.33b.1
162 * \param[in,out] e earfcn struct
163 * \param[in] offset into earfcn struct: how many EARFCNs to skip while estimating size
164 * \returns number of bits
165 */
166size_t osmo_earfcn_bit_size_ext(const struct osmo_earfcn_si2q *e, size_t offset)
167{
Maxfbb8bfa2016-04-15 16:04:04 +0200168 /* 1 stop bit + 5 bits for THRESH_E-UTRAN_high */
Max91dd2192017-05-11 15:38:10 +0200169 size_t i, bits = 6, skip = 0;
Maxfbb8bfa2016-04-15 16:04:04 +0200170 for (i = 0; i < e->length; i++) {
171 if (e->arfcn[i] != OSMO_EARFCN_INVALID) {
Max91dd2192017-05-11 15:38:10 +0200172 if (skip < offset)
173 skip++;
174 else {
175 bits += 17;
176 if (OSMO_EARFCN_MEAS_INVALID == e->meas_bw[i])
177 bits++;
178 else
179 bits += 4;
180 }
Maxfbb8bfa2016-04-15 16:04:04 +0200181 }
182 }
183 bits += (e->prio_valid) ? 4 : 1;
184 bits += (e->thresh_lo_valid) ? 6 : 1;
185 bits += (e->qrxlm_valid) ? 6 : 1;
186 return bits;
187}
188
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200189/*! Delete arfcn (and corresponding measurement bandwith) from earfcn
Max03309b52016-03-17 11:51:09 +0100190 * struct
191 * \param[in,out] e earfcn struct
192 * \param[in] arfcn EARFCN value, 16 bits
193 * \returns 0 on success, error otherwise
194 */
Maxea345cd2016-03-17 14:46:19 +0100195int osmo_earfcn_del(struct osmo_earfcn_si2q *e, uint16_t arfcn)
Max03309b52016-03-17 11:51:09 +0100196{
197 size_t i;
198 for (i = 0; i < e->length; i++) {
199 if (arfcn == e->arfcn[i]) {
200 e->arfcn[i] = OSMO_EARFCN_INVALID;
201 e->meas_bw[i] = OSMO_EARFCN_MEAS_INVALID;
202 return 0;
203 }
204 }
205 return -ENOENT;
206}
207
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200208/*! Initialize earfcn struct
Max03309b52016-03-17 11:51:09 +0100209 * \param[in,out] e earfcn struct
210 */
Maxea345cd2016-03-17 14:46:19 +0100211void osmo_earfcn_init(struct osmo_earfcn_si2q *e)
Max03309b52016-03-17 11:51:09 +0100212{
213 size_t i;
214 for (i = 0; i < e->length; i++) {
215 e->arfcn[i] = OSMO_EARFCN_INVALID;
216 e->meas_bw[i] = OSMO_EARFCN_MEAS_INVALID;
217 }
218}
219
Harald Welteb5503132011-05-24 15:01:53 +0200220uint8_t osmo_sitype2rsl(enum osmo_sysinfo_type si_type)
221{
222 return sitype2rsl[si_type];
223}
224
225enum osmo_sysinfo_type osmo_rsl2sitype(uint8_t rsl_si)
226{
227 return rsl2sitype[rsl_si];
228}