blob: b73bb0c8a45c10f6ce3844fbd18cdd9bf06dbf0e [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 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <errno.h>
25#include <string.h>
26#include <stdio.h>
Harald Welteb5503132011-05-24 15:01:53 +020027
28#include <osmocom/core/bitvec.h>
29#include <osmocom/core/utils.h>
30#include <osmocom/gsm/sysinfo.h>
31#include <osmocom/gsm/protocol/gsm_04_08.h>
32#include <osmocom/gsm/protocol/gsm_08_58.h>
33
34/* verify the sizes of the system information type structs */
35
36/* rest octets are not part of the struct */
37osmo_static_assert(sizeof(struct gsm48_system_information_type_header) == 3, _si_header_size);
38osmo_static_assert(sizeof(struct gsm48_rach_control) == 3, _si_rach_control);
39osmo_static_assert(sizeof(struct gsm48_system_information_type_1) == 22, _si1_size);
40osmo_static_assert(sizeof(struct gsm48_system_information_type_2) == 23, _si2_size);
41osmo_static_assert(sizeof(struct gsm48_system_information_type_3) == 19, _si3_size);
42osmo_static_assert(sizeof(struct gsm48_system_information_type_4) == 13, _si4_size);
43
44/* bs11 forgot the l2 len, 0-6 rest octets */
45osmo_static_assert(sizeof(struct gsm48_system_information_type_5) == 18, _si5_size);
46osmo_static_assert(sizeof(struct gsm48_system_information_type_6) == 11, _si6_size);
47
48osmo_static_assert(sizeof(struct gsm48_system_information_type_13) == 3, _si13_size);
49
50static const uint8_t sitype2rsl[_MAX_SYSINFO_TYPE] = {
51 [SYSINFO_TYPE_1] = RSL_SYSTEM_INFO_1,
52 [SYSINFO_TYPE_2] = RSL_SYSTEM_INFO_2,
53 [SYSINFO_TYPE_3] = RSL_SYSTEM_INFO_3,
54 [SYSINFO_TYPE_4] = RSL_SYSTEM_INFO_4,
55 [SYSINFO_TYPE_5] = RSL_SYSTEM_INFO_5,
56 [SYSINFO_TYPE_6] = RSL_SYSTEM_INFO_6,
57 [SYSINFO_TYPE_7] = RSL_SYSTEM_INFO_7,
58 [SYSINFO_TYPE_8] = RSL_SYSTEM_INFO_8,
59 [SYSINFO_TYPE_9] = RSL_SYSTEM_INFO_9,
60 [SYSINFO_TYPE_10] = RSL_SYSTEM_INFO_10,
61 [SYSINFO_TYPE_13] = RSL_SYSTEM_INFO_13,
62 [SYSINFO_TYPE_16] = RSL_SYSTEM_INFO_16,
63 [SYSINFO_TYPE_17] = RSL_SYSTEM_INFO_17,
64 [SYSINFO_TYPE_18] = RSL_SYSTEM_INFO_18,
65 [SYSINFO_TYPE_19] = RSL_SYSTEM_INFO_19,
66 [SYSINFO_TYPE_20] = RSL_SYSTEM_INFO_20,
67 [SYSINFO_TYPE_2bis] = RSL_SYSTEM_INFO_2bis,
68 [SYSINFO_TYPE_2ter] = RSL_SYSTEM_INFO_2ter,
69 [SYSINFO_TYPE_2quater] = RSL_SYSTEM_INFO_2quater,
70 [SYSINFO_TYPE_5bis] = RSL_SYSTEM_INFO_5bis,
71 [SYSINFO_TYPE_5ter] = RSL_SYSTEM_INFO_5ter,
Harald Welte620f7ab2011-06-25 21:39:18 +020072 [SYSINFO_TYPE_EMO] = RSL_EXT_MEAS_ORDER,
73 [SYSINFO_TYPE_MEAS_INFO]= RSL_MEAS_INFO,
Harald Welteb5503132011-05-24 15:01:53 +020074};
75
Sylvain Munautf2699502011-05-29 15:39:04 +020076static const uint8_t rsl2sitype[256] = {
Harald Welteb5503132011-05-24 15:01:53 +020077 [RSL_SYSTEM_INFO_1] = SYSINFO_TYPE_1,
78 [RSL_SYSTEM_INFO_2] = SYSINFO_TYPE_2,
79 [RSL_SYSTEM_INFO_3] = SYSINFO_TYPE_3,
80 [RSL_SYSTEM_INFO_4] = SYSINFO_TYPE_4,
81 [RSL_SYSTEM_INFO_5] = SYSINFO_TYPE_5,
82 [RSL_SYSTEM_INFO_6] = SYSINFO_TYPE_6,
83 [RSL_SYSTEM_INFO_7] = SYSINFO_TYPE_7,
84 [RSL_SYSTEM_INFO_8] = SYSINFO_TYPE_8,
85 [RSL_SYSTEM_INFO_9] = SYSINFO_TYPE_9,
86 [RSL_SYSTEM_INFO_10] = SYSINFO_TYPE_10,
87 [RSL_SYSTEM_INFO_13] = SYSINFO_TYPE_13,
88 [RSL_SYSTEM_INFO_16] = SYSINFO_TYPE_16,
89 [RSL_SYSTEM_INFO_17] = SYSINFO_TYPE_17,
90 [RSL_SYSTEM_INFO_18] = SYSINFO_TYPE_18,
91 [RSL_SYSTEM_INFO_19] = SYSINFO_TYPE_19,
92 [RSL_SYSTEM_INFO_20] = SYSINFO_TYPE_20,
93 [RSL_SYSTEM_INFO_2bis] = SYSINFO_TYPE_2bis,
94 [RSL_SYSTEM_INFO_2ter] = SYSINFO_TYPE_2ter,
95 [RSL_SYSTEM_INFO_2quater] = SYSINFO_TYPE_2quater,
96 [RSL_SYSTEM_INFO_5bis] = SYSINFO_TYPE_5bis,
97 [RSL_SYSTEM_INFO_5ter] = SYSINFO_TYPE_5ter,
Harald Welte620f7ab2011-06-25 21:39:18 +020098 [RSL_EXT_MEAS_ORDER] = SYSINFO_TYPE_EMO,
99 [RSL_MEAS_INFO] = SYSINFO_TYPE_MEAS_INFO,
Harald Welteb5503132011-05-24 15:01:53 +0200100};
101
102const struct value_string osmo_sitype_strs[_MAX_SYSINFO_TYPE] = {
103 { SYSINFO_TYPE_1, "1" },
104 { SYSINFO_TYPE_2, "2" },
105 { SYSINFO_TYPE_3, "3" },
106 { SYSINFO_TYPE_4, "4" },
107 { SYSINFO_TYPE_5, "5" },
108 { SYSINFO_TYPE_6, "6" },
109 { SYSINFO_TYPE_7, "7" },
110 { SYSINFO_TYPE_8, "8" },
111 { SYSINFO_TYPE_9, "9" },
112 { SYSINFO_TYPE_10, "10" },
113 { SYSINFO_TYPE_13, "13" },
114 { SYSINFO_TYPE_16, "16" },
115 { SYSINFO_TYPE_17, "17" },
116 { SYSINFO_TYPE_18, "18" },
117 { SYSINFO_TYPE_19, "19" },
118 { SYSINFO_TYPE_20, "20" },
119 { SYSINFO_TYPE_2bis, "2bis" },
120 { SYSINFO_TYPE_2ter, "2ter" },
121 { SYSINFO_TYPE_2quater, "2quater" },
122 { SYSINFO_TYPE_5bis, "5bis" },
123 { SYSINFO_TYPE_5ter, "5ter" },
Harald Welte620f7ab2011-06-25 21:39:18 +0200124 { SYSINFO_TYPE_EMO, "EMO" },
125 { SYSINFO_TYPE_MEAS_INFO, "MI" },
Harald Welteb5503132011-05-24 15:01:53 +0200126 { 0, NULL }
127};
128
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129/*! Add pair of arfcn and measurement bandwith value to earfcn struct
Max03309b52016-03-17 11:51:09 +0100130 * \param[in,out] e earfcn struct
131 * \param[in] arfcn EARFCN value, 16 bits
132 * \param[in] meas_bw measurement bandwith value
133 * \returns 0 on success, error otherwise
134 */
Maxea345cd2016-03-17 14:46:19 +0100135int osmo_earfcn_add(struct osmo_earfcn_si2q *e, uint16_t arfcn, uint8_t meas_bw)
Max03309b52016-03-17 11:51:09 +0100136{
137 size_t i;
138 for (i = 0; i < e->length; i++) {
139 if (OSMO_EARFCN_INVALID == e->arfcn[i]) {
140 e->arfcn[i] = arfcn;
141 e->meas_bw[i] = meas_bw;
142 return 0;
143 }
144 }
145 return -ENOMEM;
146}
147
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200148/*! Return number of bits necessary to represent earfcn struct as
Maxfbb8bfa2016-04-15 16:04:04 +0200149 * Repeated E-UTRAN Neighbour Cells IE from 3GPP TS 44.018 Table 10.5.2.33b.1
150 * \param[in,out] e earfcn struct
151 * \returns number of bits
152 */
153size_t osmo_earfcn_bit_size(const struct osmo_earfcn_si2q *e)
154{
Max91dd2192017-05-11 15:38:10 +0200155 return osmo_earfcn_bit_size_ext(e, 0);
156}
157
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200158/*! Return number of bits necessary to represent earfcn struct as
Max91dd2192017-05-11 15:38:10 +0200159 * Repeated E-UTRAN Neighbour Cells IE from 3GPP TS 44.018 Table 10.5.2.33b.1
160 * \param[in,out] e earfcn struct
161 * \param[in] offset into earfcn struct: how many EARFCNs to skip while estimating size
162 * \returns number of bits
163 */
164size_t osmo_earfcn_bit_size_ext(const struct osmo_earfcn_si2q *e, size_t offset)
165{
Maxfbb8bfa2016-04-15 16:04:04 +0200166 /* 1 stop bit + 5 bits for THRESH_E-UTRAN_high */
Max91dd2192017-05-11 15:38:10 +0200167 size_t i, bits = 6, skip = 0;
Maxfbb8bfa2016-04-15 16:04:04 +0200168 for (i = 0; i < e->length; i++) {
169 if (e->arfcn[i] != OSMO_EARFCN_INVALID) {
Max91dd2192017-05-11 15:38:10 +0200170 if (skip < offset)
171 skip++;
172 else {
173 bits += 17;
174 if (OSMO_EARFCN_MEAS_INVALID == e->meas_bw[i])
175 bits++;
176 else
177 bits += 4;
178 }
Maxfbb8bfa2016-04-15 16:04:04 +0200179 }
180 }
181 bits += (e->prio_valid) ? 4 : 1;
182 bits += (e->thresh_lo_valid) ? 6 : 1;
183 bits += (e->qrxlm_valid) ? 6 : 1;
184 return bits;
185}
186
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200187/*! Delete arfcn (and corresponding measurement bandwith) from earfcn
Max03309b52016-03-17 11:51:09 +0100188 * struct
189 * \param[in,out] e earfcn struct
190 * \param[in] arfcn EARFCN value, 16 bits
191 * \returns 0 on success, error otherwise
192 */
Maxea345cd2016-03-17 14:46:19 +0100193int osmo_earfcn_del(struct osmo_earfcn_si2q *e, uint16_t arfcn)
Max03309b52016-03-17 11:51:09 +0100194{
195 size_t i;
196 for (i = 0; i < e->length; i++) {
197 if (arfcn == e->arfcn[i]) {
198 e->arfcn[i] = OSMO_EARFCN_INVALID;
199 e->meas_bw[i] = OSMO_EARFCN_MEAS_INVALID;
200 return 0;
201 }
202 }
203 return -ENOENT;
204}
205
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200206/*! Initialize earfcn struct
Max03309b52016-03-17 11:51:09 +0100207 * \param[in,out] e earfcn struct
208 */
Maxea345cd2016-03-17 14:46:19 +0100209void osmo_earfcn_init(struct osmo_earfcn_si2q *e)
Max03309b52016-03-17 11:51:09 +0100210{
211 size_t i;
212 for (i = 0; i < e->length; i++) {
213 e->arfcn[i] = OSMO_EARFCN_INVALID;
214 e->meas_bw[i] = OSMO_EARFCN_MEAS_INVALID;
215 }
216}
217
Harald Welteb5503132011-05-24 15:01:53 +0200218uint8_t osmo_sitype2rsl(enum osmo_sysinfo_type si_type)
219{
220 return sitype2rsl[si_type];
221}
222
223enum osmo_sysinfo_type osmo_rsl2sitype(uint8_t rsl_si)
224{
225 return rsl2sitype[rsl_si];
226}