blob: 8a46f7697fff137b4e1347df435c6aface7eecbd [file] [log] [blame]
Harald Welte4fb20752010-03-02 23:17:33 +01001/* GSM Mobile Radio Interface Layer 3 messages
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Welte9eb6d882010-03-25 12:00:54 +08004/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte4fb20752010-03-02 23:17:33 +01005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
6 *
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 along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <stdint.h>
26#include <stdio.h>
27#include <string.h>
Maxfe65fa72016-05-10 17:17:05 +020028#include <stdbool.h>
Harald Welte61e2bfc2010-03-04 10:53:03 +010029#include <arpa/inet.h>
30
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010031#include <osmocom/core/utils.h>
32#include <osmocom/gsm/tlv.h>
33#include <osmocom/gsm/gsm48.h>
Harald Welte94df39e2011-06-26 14:33:57 +020034#include <osmocom/gsm/gsm0502.h>
Maxfb348ee2016-03-30 21:14:53 +020035#include <osmocom/gsm/gsm_utils.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010036#include <osmocom/gsm/protocol/gsm_04_08.h>
Harald Welte2aee7b12011-06-26 14:20:04 +020037#include <osmocom/gsm/protocol/gsm_08_58.h>
Harald Welte4fb20752010-03-02 23:17:33 +010038
39const struct tlv_definition gsm48_att_tlvdef = {
40 .def = {
41 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
42 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
43 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
44 [GSM48_IE_UTC] = { TLV_TYPE_TV },
45 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
46 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
47
48 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
49 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
50 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
51 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
52 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
53 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
54 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
55 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
56 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
57 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
58 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
59 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
60 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
61 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
62 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
63 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
64 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
65 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
66 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
67 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
68 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
69 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
70 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
71 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
72 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
73 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
74 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
75 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
76 /* FIXME: more elements */
77 },
78};
79
Andreas Eversberg014cb872010-07-12 09:11:00 +020080/* RR elements */
81const struct tlv_definition gsm48_rr_att_tlvdef = {
82 .def = {
83 /* NOTE: Don't add IE 17 = MOBILE_ID here, it already used. */
84 [GSM48_IE_VGCS_TARGET] = { TLV_TYPE_TLV },
85 [GSM48_IE_FRQSHORT_AFTER] = { TLV_TYPE_FIXED, 9 },
86 [GSM48_IE_MUL_RATE_CFG] = { TLV_TYPE_TLV },
87 [GSM48_IE_FREQ_L_AFTER] = { TLV_TYPE_TLV },
88 [GSM48_IE_MSLOT_DESC] = { TLV_TYPE_TLV },
89 [GSM48_IE_CHANMODE_2] = { TLV_TYPE_TV },
90 [GSM48_IE_FRQSHORT_BEFORE] = { TLV_TYPE_FIXED, 9 },
91 [GSM48_IE_CHANMODE_3] = { TLV_TYPE_TV },
92 [GSM48_IE_CHANMODE_4] = { TLV_TYPE_TV },
93 [GSM48_IE_CHANMODE_5] = { TLV_TYPE_TV },
94 [GSM48_IE_CHANMODE_6] = { TLV_TYPE_TV },
95 [GSM48_IE_CHANMODE_7] = { TLV_TYPE_TV },
96 [GSM48_IE_CHANMODE_8] = { TLV_TYPE_TV },
97 [GSM48_IE_FREQ_L_BEFORE] = { TLV_TYPE_TLV },
98 [GSM48_IE_CH_DESC_1_BEFORE] = { TLV_TYPE_FIXED, 3 },
99 [GSM48_IE_CH_DESC_2_BEFORE] = { TLV_TYPE_FIXED, 3 },
100 [GSM48_IE_F_CH_SEQ_BEFORE] = { TLV_TYPE_FIXED, 9 },
101 [GSM48_IE_CLASSMARK3] = { TLV_TYPE_TLV },
102 [GSM48_IE_MA_BEFORE] = { TLV_TYPE_TLV },
103 [GSM48_IE_RR_PACKET_UL] = { TLV_TYPE_TLV },
104 [GSM48_IE_RR_PACKET_DL] = { TLV_TYPE_TLV },
105 [GSM48_IE_CELL_CH_DESC] = { TLV_TYPE_FIXED, 16 },
106 [GSM48_IE_CHANMODE_1] = { TLV_TYPE_TV },
107 [GSM48_IE_CHDES_2_AFTER] = { TLV_TYPE_FIXED, 3 },
108 [GSM48_IE_MODE_SEC_CH] = { TLV_TYPE_TV },
109 [GSM48_IE_F_CH_SEQ_AFTER] = { TLV_TYPE_FIXED, 9 },
110 [GSM48_IE_MA_AFTER] = { TLV_TYPE_TLV },
111 [GSM48_IE_BA_RANGE] = { TLV_TYPE_TLV },
112 [GSM48_IE_GROUP_CHDES] = { TLV_TYPE_TLV },
113 [GSM48_IE_BA_LIST_PREF] = { TLV_TYPE_TLV },
114 [GSM48_IE_MOB_OVSERV_DIF] = { TLV_TYPE_TLV },
115 [GSM48_IE_REALTIME_DIFF] = { TLV_TYPE_TLV },
116 [GSM48_IE_START_TIME] = { TLV_TYPE_FIXED, 2 },
117 [GSM48_IE_TIMING_ADVANCE] = { TLV_TYPE_TV },
118 [GSM48_IE_GROUP_CIP_SEQ] = { TLV_TYPE_SINGLE_TV },
119 [GSM48_IE_CIP_MODE_SET] = { TLV_TYPE_SINGLE_TV },
120 [GSM48_IE_GPRS_RESUMPT] = { TLV_TYPE_SINGLE_TV },
121 [GSM48_IE_SYNC_IND] = { TLV_TYPE_SINGLE_TV },
122 },
123};
124
125/* MM elements */
126const struct tlv_definition gsm48_mm_att_tlvdef = {
127 .def = {
128 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
129 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
130 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
131 [GSM48_IE_UTC] = { TLV_TYPE_TV },
132 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
133 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
Jacob Erlbeck1c3f0882013-09-16 10:29:57 +0200134 [GSM48_IE_NET_DST] = { TLV_TYPE_TLV },
Andreas Eversberg014cb872010-07-12 09:11:00 +0200135
136 [GSM48_IE_LOCATION_AREA] = { TLV_TYPE_FIXED, 5 },
137 [GSM48_IE_PRIORITY_LEV] = { TLV_TYPE_SINGLE_TV },
138 [GSM48_IE_FOLLOW_ON_PROC] = { TLV_TYPE_T },
139 [GSM48_IE_CTS_PERMISSION] = { TLV_TYPE_T },
140 },
141};
142
Harald Weltee9e190a2010-03-25 11:44:57 +0800143static const struct value_string rr_cause_names[] = {
144 { GSM48_RR_CAUSE_NORMAL, "Normal event" },
145 { GSM48_RR_CAUSE_ABNORMAL_UNSPEC, "Abnormal release, unspecified" },
146 { GSM48_RR_CAUSE_ABNORMAL_UNACCT, "Abnormal release, channel unacceptable" },
147 { GSM48_RR_CAUSE_ABNORMAL_TIMER, "Abnormal release, timer expired" },
148 { GSM48_RR_CAUSE_ABNORMAL_NOACT, "Abnormal release, no activity on radio path" },
149 { GSM48_RR_CAUSE_PREMPTIVE_REL, "Preemptive release" },
150 { GSM48_RR_CAUSE_HNDOVER_IMP, "Handover impossible, timing advance out of range" },
151 { GSM48_RR_CAUSE_CHAN_MODE_UNACCT, "Channel mode unacceptable" },
152 { GSM48_RR_CAUSE_FREQ_NOT_IMPL, "Frequency not implemented" },
153 { GSM48_RR_CAUSE_CALL_CLEARED, "Call already cleared" },
154 { GSM48_RR_CAUSE_SEMANT_INCORR, "Semantically incorrect message" },
155 { GSM48_RR_CAUSE_INVALID_MAND_INF, "Invalid mandatory information" },
156 { GSM48_RR_CAUSE_MSG_TYPE_N, "Message type non-existant or not implemented" },
157 { GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT, "Message type not compatible with protocol state" },
158 { GSM48_RR_CAUSE_COND_IE_ERROR, "Conditional IE error" },
159 { GSM48_RR_CAUSE_NO_CELL_ALLOC_A, "No cell allocation available" },
160 { GSM48_RR_CAUSE_PROT_ERROR_UNSPC, "Protocol error unspecified" },
161 { 0, NULL },
Harald Welte4fb20752010-03-02 23:17:33 +0100162};
163
Harald Welte9eb6d882010-03-25 12:00:54 +0800164/* FIXME: convert to value_string */
Andreas Eversberg014cb872010-07-12 09:11:00 +0200165static const char *cc_state_names[32] = {
Harald Welte4fb20752010-03-02 23:17:33 +0100166 "NULL",
167 "INITIATED",
Andreas Eversberg1ef041f2010-04-09 07:52:12 +0200168 "MM_CONNECTION_PEND",
Harald Welte4fb20752010-03-02 23:17:33 +0100169 "MO_CALL_PROC",
170 "CALL_DELIVERED",
171 "illegal state 5",
172 "CALL_PRESENT",
173 "CALL_RECEIVED",
174 "CONNECT_REQUEST",
175 "MO_TERM_CALL_CONF",
176 "ACTIVE",
177 "DISCONNECT_REQ",
178 "DISCONNECT_IND",
179 "illegal state 13",
180 "illegal state 14",
181 "illegal state 15",
182 "illegal state 16",
183 "illegal state 17",
184 "illegal state 18",
185 "RELEASE_REQ",
186 "illegal state 20",
187 "illegal state 21",
188 "illegal state 22",
189 "illegal state 23",
190 "illegal state 24",
191 "illegal state 25",
192 "MO_ORIG_MODIFY",
193 "MO_TERM_MODIFY",
194 "CONNECT_IND",
195 "illegal state 29",
196 "illegal state 30",
197 "illegal state 31",
198};
199
Harald Welte9eb6d882010-03-25 12:00:54 +0800200const char *gsm48_cc_state_name(uint8_t state)
201{
202 if (state < ARRAY_SIZE(cc_state_names))
203 return cc_state_names[state];
204
205 return "invalid";
206}
207
208static const struct value_string cc_msg_names[] = {
209 { GSM48_MT_CC_ALERTING, "ALERTING" },
210 { GSM48_MT_CC_CALL_PROC, "CALL_PROC" },
211 { GSM48_MT_CC_PROGRESS, "PROGRESS" },
212 { GSM48_MT_CC_ESTAB, "ESTAB" },
213 { GSM48_MT_CC_SETUP, "SETUP" },
214 { GSM48_MT_CC_ESTAB_CONF, "ESTAB_CONF" },
215 { GSM48_MT_CC_CONNECT, "CONNECT" },
216 { GSM48_MT_CC_CALL_CONF, "CALL_CONF" },
217 { GSM48_MT_CC_START_CC, "START_CC" },
218 { GSM48_MT_CC_RECALL, "RECALL" },
219 { GSM48_MT_CC_EMERG_SETUP, "EMERG_SETUP" },
220 { GSM48_MT_CC_CONNECT_ACK, "CONNECT_ACK" },
221 { GSM48_MT_CC_USER_INFO, "USER_INFO" },
222 { GSM48_MT_CC_MODIFY_REJECT, "MODIFY_REJECT" },
223 { GSM48_MT_CC_MODIFY, "MODIFY" },
224 { GSM48_MT_CC_HOLD, "HOLD" },
225 { GSM48_MT_CC_HOLD_ACK, "HOLD_ACK" },
226 { GSM48_MT_CC_HOLD_REJ, "HOLD_REJ" },
227 { GSM48_MT_CC_RETR, "RETR" },
228 { GSM48_MT_CC_RETR_ACK, "RETR_ACK" },
229 { GSM48_MT_CC_RETR_REJ, "RETR_REJ" },
230 { GSM48_MT_CC_MODIFY_COMPL, "MODIFY_COMPL" },
231 { GSM48_MT_CC_DISCONNECT, "DISCONNECT" },
232 { GSM48_MT_CC_RELEASE_COMPL, "RELEASE_COMPL" },
233 { GSM48_MT_CC_RELEASE, "RELEASE" },
234 { GSM48_MT_CC_STOP_DTMF, "STOP_DTMF" },
235 { GSM48_MT_CC_STOP_DTMF_ACK, "STOP_DTMF_ACK" },
236 { GSM48_MT_CC_STATUS_ENQ, "STATUS_ENQ" },
237 { GSM48_MT_CC_START_DTMF, "START_DTMF" },
238 { GSM48_MT_CC_START_DTMF_ACK, "START_DTMF_ACK" },
239 { GSM48_MT_CC_START_DTMF_REJ, "START_DTMF_REJ" },
240 { GSM48_MT_CC_CONG_CTRL, "CONG_CTRL" },
241 { GSM48_MT_CC_FACILITY, "FACILITY" },
242 { GSM48_MT_CC_STATUS, "STATUS" },
243 { GSM48_MT_CC_NOTIFY, "NOTFIY" },
244 { 0, NULL }
Harald Welte61e2bfc2010-03-04 10:53:03 +0100245};
246
Harald Welte9eb6d882010-03-25 12:00:54 +0800247const char *gsm48_cc_msg_name(uint8_t msgtype)
248{
249 return get_value_string(cc_msg_names, msgtype);
250}
Harald Welte4fb20752010-03-02 23:17:33 +0100251
252const char *rr_cause_name(uint8_t cause)
253{
Harald Weltee9e190a2010-03-25 11:44:57 +0800254 return get_value_string(rr_cause_names, cause);
Harald Welte4fb20752010-03-02 23:17:33 +0100255}
256
Maxfb348ee2016-03-30 21:14:53 +0200257const struct value_string gsm48_chan_mode_names[] = {
258 { GSM48_CMODE_SIGN, "SIGNALLING" },
259 { GSM48_CMODE_SPEECH_V1, "SPEECH_V1" },
260 { GSM48_CMODE_SPEECH_EFR, "SPEECH_EFR" },
261 { GSM48_CMODE_SPEECH_AMR, "SPEECH_AMR" },
262 { GSM48_CMODE_DATA_14k5, "DATA_14k5" },
263 { GSM48_CMODE_DATA_12k0, "DATA_12k0" },
264 { GSM48_CMODE_DATA_6k0, "DATA_6k0" },
265 { GSM48_CMODE_DATA_3k6, "DATA_3k6" },
266 { 0, NULL },
267};
268
269const struct value_string gsm_chan_t_names[] = {
270 { GSM_LCHAN_NONE, "NONE" },
271 { GSM_LCHAN_SDCCH, "SDCCH" },
272 { GSM_LCHAN_TCH_F, "TCH_F" },
273 { GSM_LCHAN_TCH_H, "TCH_H" },
274 { GSM_LCHAN_UNKNOWN, "UNKNOWN" },
275 { GSM_LCHAN_CCCH, "CCCH" },
276 { GSM_LCHAN_PDTCH, "PDTCH" },
277 { GSM_LCHAN_CBCH, "CBCH" },
278 { 0, NULL },
279};
280
Harald Welte1a8c4e02015-08-16 17:56:25 +0200281static const struct value_string mi_type_names[] = {
282 { GSM_MI_TYPE_NONE, "NONE" },
283 { GSM_MI_TYPE_IMSI, "IMSI" },
284 { GSM_MI_TYPE_IMEI, "IMEI" },
285 { GSM_MI_TYPE_IMEISV, "IMEI-SV" },
286 { GSM_MI_TYPE_TMSI, "TMSI" },
287 { 0, NULL }
288};
289
290const char *gsm48_mi_type_name(uint8_t mi)
291{
292 return get_value_string(mi_type_names, mi);
293}
294
Harald Welte61e2bfc2010-03-04 10:53:03 +0100295static void to_bcd(uint8_t *bcd, uint16_t val)
296{
297 bcd[2] = val % 10;
298 val = val / 10;
299 bcd[1] = val % 10;
300 val = val / 10;
301 bcd[0] = val % 10;
302 val = val / 10;
303}
Harald Welte4fb20752010-03-02 23:17:33 +0100304
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100305/* Convert given mcc and mnc to BCD and write to *bcd_dst, which must be an
306 * allocated buffer of (at least) 3 bytes length. */
307void gsm48_mcc_mnc_to_bcd(uint8_t *bcd_dst, uint16_t mcc, uint16_t mnc)
Harald Welte61e2bfc2010-03-04 10:53:03 +0100308{
309 uint8_t bcd[3];
310
311 to_bcd(bcd, mcc);
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100312 bcd_dst[0] = bcd[0] | (bcd[1] << 4);
313 bcd_dst[1] = bcd[2];
Harald Welte61e2bfc2010-03-04 10:53:03 +0100314
315 to_bcd(bcd, mnc);
316 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
Andreas Eversberged00fe42010-07-12 09:12:46 +0200317 if (mnc > 99) {
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100318 bcd_dst[1] |= bcd[2] << 4;
319 bcd_dst[2] = bcd[0] | (bcd[1] << 4);
Andreas Eversberged00fe42010-07-12 09:12:46 +0200320 } else {
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100321 bcd_dst[1] |= 0xf << 4;
322 bcd_dst[2] = bcd[1] | (bcd[2] << 4);
Andreas Eversberged00fe42010-07-12 09:12:46 +0200323 }
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100324}
Harald Welte61e2bfc2010-03-04 10:53:03 +0100325
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100326/* Convert given 3-byte BCD buffer to integers and write results to *mcc and
327 * *mnc. The first three BCD digits result in the MCC and the remaining ones in
328 * the MNC. */
329void gsm48_mcc_mnc_from_bcd(uint8_t *bcd_src, uint16_t *mcc, uint16_t *mnc)
330{
331 *mcc = (bcd_src[0] & 0x0f) * 100
332 + (bcd_src[0] >> 4) * 10
333 + (bcd_src[1] & 0x0f);
334
335 if ((bcd_src[1] & 0xf0) == 0xf0) {
336 *mnc = (bcd_src[2] & 0x0f) * 10
337 + (bcd_src[2] >> 4);
338 } else {
339 *mnc = (bcd_src[2] & 0x0f) * 100
340 + (bcd_src[2] >> 4) * 10
341 + (bcd_src[1] >> 4);
342 }
343}
344
345void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc,
346 uint16_t mnc, uint16_t lac)
347{
348 gsm48_mcc_mnc_to_bcd(&lai48->digits[0], mcc, mnc);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100349 lai48->lac = htons(lac);
350}
351
Neels Hofmeyrf20f5fd2016-02-29 09:56:39 +0100352/* Attention: this function returns true integers, not hex! */
Harald Welte774a9de2012-07-13 21:35:13 +0200353int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
354 uint16_t *mnc, uint16_t *lac)
355{
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100356 gsm48_mcc_mnc_from_bcd(&lai->digits[0], mcc, mnc);
Harald Welte774a9de2012-07-13 21:35:13 +0200357 *lac = ntohs(lai->lac);
Harald Welte774a9de2012-07-13 21:35:13 +0200358 return 0;
359}
360
Maxfe65fa72016-05-10 17:17:05 +0200361/*! \brief Set DTX mode in Cell Options IE (3GPP TS 44.018)
362 * \param[in] op Cell Options structure in which DTX parameters will be set
363 * \param[in] full Mode for full-rate channels
364 * \param[in] half Mode for half-rate channels
365 * \param[in] is_bcch Indicates if we should use 10.5.2.3.1 instead of
366 * 10.5.2.3a.2
367 *
368 * There is no space for separate DTX settings for Full and Half rate channels
369 * in BCCH - in this case full setting is used for both and half parameter is
370 * ignored.
371 */
372void gsm48_set_dtx(struct gsm48_cell_options *op, enum gsm48_dtx_mode full,
373 enum gsm48_dtx_mode half, bool is_bcch)
374{
375 if (is_bcch) {
376 switch (full) {
377 case GSM48_DTX_MAY_BE_USED:
378 op->dtx = 0;
379 return;
380 case GSM48_DTX_SHALL_BE_USED:
381 op->dtx = 1;
382 return;
383 case GSM48_DTX_SHALL_NOT_BE_USED:
384 op->dtx = 2;
385 return;
386 }
387 } else {
388 switch (full) {
389 case GSM48_DTX_MAY_BE_USED:
390 op->dtx = (half == GSM48_DTX_SHALL_BE_USED) ? 3 : 0;
391 op->d = (half == GSM48_DTX_SHALL_NOT_BE_USED) ? 0 : 1;
392 return;
393 case GSM48_DTX_SHALL_BE_USED:
394 op->dtx = (half == GSM48_DTX_MAY_BE_USED) ? 3 : 1;
395 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
396 return;
397 case GSM48_DTX_SHALL_NOT_BE_USED:
398 op->dtx = 2;
399 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
400 return;
401 }
402 }
403}
404
Harald Welte61e2bfc2010-03-04 10:53:03 +0100405int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi)
406{
Holger Hans Peter Freythercd252e32013-07-03 09:56:53 +0200407 uint32_t tmsi_be = htonl(tmsi);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100408
409 buf[0] = GSM48_IE_MOBILE_ID;
410 buf[1] = GSM48_TMSI_LEN;
411 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Holger Hans Peter Freythercd252e32013-07-03 09:56:53 +0200412 memcpy(&buf[3], &tmsi_be, sizeof(tmsi_be));
Harald Welte61e2bfc2010-03-04 10:53:03 +0100413
414 return 7;
415}
416
417int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
418{
419 unsigned int length = strlen(imsi), i, off = 0;
420 uint8_t odd = (length & 0x1) == 1;
421
422 buf[0] = GSM48_IE_MOBILE_ID;
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200423 buf[2] = osmo_char2bcd(imsi[0]) << 4 | GSM_MI_TYPE_IMSI | (odd << 3);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100424
425 /* if the length is even we will fill half of the last octet */
426 if (odd)
427 buf[1] = (length + 1) >> 1;
428 else
429 buf[1] = (length + 2) >> 1;
430
431 for (i = 1; i < buf[1]; ++i) {
432 uint8_t lower, upper;
433
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200434 lower = osmo_char2bcd(imsi[++off]);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100435 if (!odd && off + 1 == length)
436 upper = 0x0f;
437 else
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200438 upper = osmo_char2bcd(imsi[++off]) & 0x0f;
Harald Welte61e2bfc2010-03-04 10:53:03 +0100439
440 buf[2 + i] = (upper << 4) | lower;
441 }
442
443 return 2 + buf[1];
444}
Harald Welte9bb553e2010-03-28 18:14:50 +0800445
446/* Convert Mobile Identity (10.5.1.4) to string */
Harald Welte163d0ea2010-04-09 07:57:40 +0200447int gsm48_mi_to_string(char *string, const int str_len, const uint8_t *mi,
448 const int mi_len)
Harald Welte9bb553e2010-03-28 18:14:50 +0800449{
450 int i;
Harald Welte163d0ea2010-04-09 07:57:40 +0200451 uint8_t mi_type;
Harald Welte9bb553e2010-03-28 18:14:50 +0800452 char *str_cur = string;
Harald Welte163d0ea2010-04-09 07:57:40 +0200453 uint32_t tmsi;
Harald Welte9bb553e2010-03-28 18:14:50 +0800454
455 mi_type = mi[0] & GSM_MI_TYPE_MASK;
456
457 switch (mi_type) {
458 case GSM_MI_TYPE_NONE:
459 break;
460 case GSM_MI_TYPE_TMSI:
461 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
462 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
463 memcpy(&tmsi, &mi[1], 4);
464 tmsi = ntohl(tmsi);
Harald Weltec79b3972016-03-17 14:42:45 +0100465 return snprintf(string, str_len, "%u", tmsi);
Harald Welte9bb553e2010-03-28 18:14:50 +0800466 }
467 break;
468 case GSM_MI_TYPE_IMSI:
469 case GSM_MI_TYPE_IMEI:
470 case GSM_MI_TYPE_IMEISV:
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200471 *str_cur++ = osmo_bcd2char(mi[0] >> 4);
Harald Welte9bb553e2010-03-28 18:14:50 +0800472
473 for (i = 1; i < mi_len; i++) {
474 if (str_cur + 2 >= string + str_len)
475 return str_cur - string;
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200476 *str_cur++ = osmo_bcd2char(mi[i] & 0xf);
Harald Welte9bb553e2010-03-28 18:14:50 +0800477 /* skip last nibble in last input byte when GSM_EVEN */
478 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200479 *str_cur++ = osmo_bcd2char(mi[i] >> 4);
Harald Welte9bb553e2010-03-28 18:14:50 +0800480 }
481 break;
482 default:
483 break;
484 }
485 *str_cur++ = '\0';
486
487 return str_cur - string;
488}
Harald Weltea1c4f762010-05-01 11:59:42 +0200489
490void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf)
491{
492 raid->mcc = (buf[0] & 0xf) * 100;
493 raid->mcc += (buf[0] >> 4) * 10;
494 raid->mcc += (buf[1] & 0xf) * 1;
495
496 /* I wonder who came up with the stupidity of encoding the MNC
497 * differently depending on how many digits its decimal number has! */
498 if ((buf[1] >> 4) == 0xf) {
499 raid->mnc = (buf[2] & 0xf) * 10;
500 raid->mnc += (buf[2] >> 4) * 1;
501 } else {
502 raid->mnc = (buf[2] & 0xf) * 100;
503 raid->mnc += (buf[2] >> 4) * 10;
504 raid->mnc += (buf[1] >> 4) * 1;
505 }
506
507 raid->lac = ntohs(*(uint16_t *)(buf + 3));
508 raid->rac = buf[5];
509}
Harald Welte35a93942010-05-01 14:25:22 +0200510
511int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid)
512{
513 uint16_t mcc = raid->mcc;
514 uint16_t mnc = raid->mnc;
Andreas Eversbergd430ef42012-11-05 10:47:59 +0100515 uint16_t _lac;
Harald Welte35a93942010-05-01 14:25:22 +0200516
517 buf[0] = ((mcc / 100) % 10) | (((mcc / 10) % 10) << 4);
518 buf[1] = (mcc % 10);
519
520 /* I wonder who came up with the stupidity of encoding the MNC
521 * differently depending on how many digits its decimal number has! */
522 if (mnc < 100) {
523 buf[1] |= 0xf0;
524 buf[2] = ((mnc / 10) % 10) | ((mnc % 10) << 4);
525 } else {
526 buf[1] |= (mnc % 10) << 4;
Gus Bourg4b6bc3b2011-12-14 23:20:21 +0100527 buf[2] = ((mnc / 100) % 10) | (((mnc / 10) % 10) << 4);
Harald Welte35a93942010-05-01 14:25:22 +0200528 }
529
Andreas Eversbergd430ef42012-11-05 10:47:59 +0100530 _lac = htons(raid->lac);
531 memcpy(buf + 3, &_lac, 2);
Harald Welte35a93942010-05-01 14:25:22 +0200532
533 buf[5] = raid->rac;
534
535 return 6;
536}
Harald Welte2aee7b12011-06-26 14:20:04 +0200537
538/* From Table 10.5.33 of GSM 04.08 */
539int gsm48_number_of_paging_subchannels(struct gsm48_control_channel_descr *chan_desc)
540{
Harald Welte94df39e2011-06-26 14:33:57 +0200541 unsigned int n_pag_blocks = gsm0502_get_n_pag_blocks(chan_desc);
542
543 if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C)
544 return OSMO_MAX(1, n_pag_blocks) * (chan_desc->bs_pa_mfrms + 2);
545 else
546 return n_pag_blocks * (chan_desc->bs_pa_mfrms + 2);
Harald Welte2aee7b12011-06-26 14:20:04 +0200547}