blob: 1eed41feffb27c64450485ea50d1753ef259946d [file] [log] [blame]
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02001/*! \file gsm23003.c
2 * Utility function implementations related to 3GPP TS 23.003 */
3/*
Harald Weltee08da972017-11-13 01:00:26 +09004 * (C) 2017 sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
5 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02006 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020020 */
21
22#include <ctype.h>
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010023#include <stdio.h>
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +010024#include <stdlib.h>
25#include <errno.h>
26#include <string.h>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020027
28#include <osmocom/gsm/gsm23003.h>
29#include <osmocom/gsm/protocol/gsm_23_003.h>
Oliver Smith894be2d2019-01-11 13:13:37 +010030#include <osmocom/core/utils.h>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020031
32static bool is_n_digits(const char *str, int min_digits, int max_digits)
33{
34 int len;
35 /* Use unsigned char * to avoid a compiler warning of
36 * "error: array subscript has type 'char' [-Werror=char-subscripts]" */
37 const unsigned char *pos = (const unsigned char *)str;
Neels Hofmeyr4b7c7912017-10-07 04:45:01 +020038 if (!pos)
39 return min_digits < 1;
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020040 for (len = 0; *pos && len < max_digits; len++, pos++)
41 if (!isdigit(*pos))
42 return false;
43 if (len < min_digits)
44 return false;
45 /* With not too many digits, we should have reached *str == nul */
46 if (*pos)
47 return false;
48 return true;
49}
50
51/*! Determine whether the given IMSI is valid according to 3GPP TS 23.003.
52 * \param imsi IMSI digits in ASCII string representation.
53 * \returns true when the IMSI is valid, false for invalid characters or number
54 * of digits.
55 */
56bool osmo_imsi_str_valid(const char *imsi)
57{
58 return is_n_digits(imsi, GSM23003_IMSI_MIN_DIGITS, GSM23003_IMSI_MAX_DIGITS);
59}
60
61/*! Determine whether the given MSISDN is valid according to 3GPP TS 23.003.
62 * \param msisdn MSISDN digits in ASCII string representation.
63 * \returns true when the MSISDN is valid, false for invalid characters or number
64 * of digits.
65 */
66bool osmo_msisdn_str_valid(const char *msisdn)
67{
Oliver Smithcdac6202019-05-10 11:28:27 +020068 return is_n_digits(msisdn, GSM23003_MSISDN_MIN_DIGITS, GSM23003_MSISDN_MAX_DIGITS);
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020069}
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010070
Oliver Smith894be2d2019-01-11 13:13:37 +010071/*! Determine whether the given IMEI is valid according to 3GPP TS 23.003,
72 * Section 6.2.1. It consists of 14 digits, the 15th check digit is not
73 * intended for digital transmission.
74 * \param imei IMEI digits in ASCII string representation.
75 * \param with_15th_digit when true, expect the 15th digit to be present and
76 * verify it.
77 * \returns true when the IMEI is valid, false for invalid characters or number
78 * of digits.
79 */
80bool osmo_imei_str_valid(const char *imei, bool with_15th_digit)
81{
82 if (with_15th_digit)
83 return is_n_digits(imei, 15, 15) && osmo_luhn(imei, 14) == imei[14];
84 else
85 return is_n_digits(imei, 14, 14);
86}
87
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010088/*! Return MCC string as standardized 3-digit with leading zeros.
Harald Welte4a62eda2019-03-18 18:27:00 +010089 * \param[out] buf caller-allocated output buffer
90 * \param[in] buf_len size of buf in bytes
91 * \param[in] mcc MCC value.
92 * \returns string in user-supplied output buffer
93 */
94char *osmo_mcc_name_buf(char *buf, size_t buf_len, uint16_t mcc)
95{
96 snprintf(buf, buf_len, "%03u", mcc);
97 return buf;
98}
99
100/*! Return MCC string as standardized 3-digit with leading zeros.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100101 * \param[in] mcc MCC value.
102 * \returns string in static buffer.
103 */
104const char *osmo_mcc_name(uint16_t mcc)
105{
Harald Welte171ef822019-03-28 10:49:05 +0100106 static __thread char buf[8];
Harald Welte4a62eda2019-03-18 18:27:00 +0100107 return osmo_mcc_name_buf(buf, sizeof(buf), mcc);
108}
109
Harald Welte179f3572019-03-18 18:38:47 +0100110/*! Return MCC string as standardized 3-digit with leading zeros, into a talloc-allocated buffer.
111 * \param[in] ctx talloc context from which to allocate output buffer
112 * \param[in] mcc MCC value.
113 * \returns string in dynamically allocated buffer.
114 */
115const char *osmo_mcc_name_c(const void *ctx, uint16_t mcc)
116{
117 char *buf = talloc_size(ctx, 8);
118 if (!buf)
119 return NULL;
120 return osmo_mcc_name_buf(buf, 8, mcc);
121}
122
Harald Welte4a62eda2019-03-18 18:27:00 +0100123/*! Return MNC string as standardized 2- or 3-digit with leading zeros.
124 * \param[out] buf caller-allocated output buffer
125 * \param[in] buf_len size of buf in bytes
126 * \param[in] mnc MNC value.
127 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
128 * \returns string in static buffer.
129 */
130char *osmo_mnc_name_buf(char *buf, size_t buf_len, uint16_t mnc, bool mnc_3_digits)
131{
132 snprintf(buf, buf_len, "%0*u", mnc_3_digits ? 3 : 2, mnc);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100133 return buf;
134}
135
Harald Welte179f3572019-03-18 18:38:47 +0100136/*! Return MNC string as standardized 2- or 3-digit with leading zeros, into a talloc-allocated buffer.
137 * \param[in] ctx talloc context from which to allocate output buffer
138 * \param[in] mnc MNC value.
139 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
140 * \returns string in dynamically allocated buffer.
141 */
142char *osmo_mnc_name_c(const void *ctx, uint16_t mnc, bool mnc_3_digits)
143{
144 char *buf = talloc_size(ctx, 8);
145 if (!buf)
146 return buf;
147 return osmo_mnc_name_buf(buf, 8, mnc, mnc_3_digits);
148}
149
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100150/*! Return MNC string as standardized 2- or 3-digit with leading zeros.
151 * \param[in] mnc MNC value.
152 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
153 * \returns string in static buffer.
154 */
155const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits)
156{
Harald Welte171ef822019-03-28 10:49:05 +0100157 static __thread char buf[8];
Harald Welte4a62eda2019-03-18 18:27:00 +0100158 return osmo_mnc_name_buf(buf, sizeof(buf), mnc, mnc_3_digits);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100159}
160
Harald Welte4a62eda2019-03-18 18:27:00 +0100161/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros.
162 * \param[out] buf caller-allocated output buffer
163 * \param[in] buf_len size of buf in bytes
164 * \param[in] plmn MCC-MNC value.
165 * \returns string in static buffer.
166 */
167char *osmo_plmn_name_buf(char *buf, size_t buf_len, const struct osmo_plmn_id *plmn)
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100168{
Harald Welte4a62eda2019-03-18 18:27:00 +0100169 char mcc[8], mnc[8];
170 snprintf(buf, buf_len, "%s-%s", osmo_mcc_name_buf(mcc, sizeof(mcc), plmn->mcc),
171 osmo_mnc_name_buf(mnc, sizeof(mnc), plmn->mnc, plmn->mnc_3_digits));
172 return buf;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100173}
174
175/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros.
176 * \param[in] plmn MCC-MNC value.
177 * \returns string in static buffer.
178 */
179const char *osmo_plmn_name(const struct osmo_plmn_id *plmn)
180{
Harald Welte171ef822019-03-28 10:49:05 +0100181 static __thread char buf[16];
Harald Welte4a62eda2019-03-18 18:27:00 +0100182 return osmo_plmn_name_buf(buf, sizeof(buf), plmn);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100183}
184
Harald Welte4a62eda2019-03-18 18:27:00 +0100185
Neels Hofmeyr6d57c572019-02-26 21:54:00 +0100186/*! Same as osmo_plmn_name(), but returning in a different static buffer.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100187 * \param[in] plmn MCC-MNC value.
188 * \returns string in static buffer.
189 */
190const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn)
191{
Harald Welte171ef822019-03-28 10:49:05 +0100192 static __thread char buf[16];
Harald Welte4a62eda2019-03-18 18:27:00 +0100193 return osmo_plmn_name_buf(buf, sizeof(buf), plmn);
194}
195
Harald Welte179f3572019-03-18 18:38:47 +0100196/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros, into
197 * a dynamically-allocated output buffer.
198 * \param[in] ctx talloc context from which to allocate output buffer
199 * \param[in] plmn MCC-MNC value.
200 * \returns string in dynamically allocated buffer.
201 */
202char *osmo_plmn_name_c(const void *ctx, const struct osmo_plmn_id *plmn)
203{
204 char *buf = talloc_size(ctx, 16);
205 if (!buf)
206 return NULL;
207 return osmo_plmn_name_buf(buf, 16, plmn);
208}
209
Harald Welte4a62eda2019-03-18 18:27:00 +0100210/*! Return MCC-MNC-LAC as string, in caller-provided output buffer.
211 * \param[out] buf caller-allocated output buffer
212 * \param[in] buf_len size of buf in bytes
213 * \param[in] lai LAI to encode, the rac member is ignored.
214 * \returns buf
215 */
216char *osmo_lai_name_buf(char *buf, size_t buf_len, const struct osmo_location_area_id *lai)
217{
218 char plmn[16];
219 snprintf(buf, buf_len, "%s-%u", osmo_plmn_name_buf(plmn, sizeof(plmn), &lai->plmn), lai->lac);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100220 return buf;
221}
222
223/*! Return MCC-MNC-LAC as string, in a static buffer.
224 * \param[in] lai LAI to encode, the rac member is ignored.
225 * \returns Static string buffer.
226 */
227const char *osmo_lai_name(const struct osmo_location_area_id *lai)
228{
Harald Welte171ef822019-03-28 10:49:05 +0100229 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100230 return osmo_lai_name_buf(buf, sizeof(buf), lai);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100231}
232
Harald Welte179f3572019-03-18 18:38:47 +0100233/*! Return MCC-MNC-LAC as string, in a talloc-allocated output buffer.
234 * \param[in] ctx talloc context from which to allocate output buffer
235 * \param[in] lai LAI to encode, the rac member is ignored.
236 * \returns string representation of lai in dynamically allocated buffer.
237 */
238char *osmo_lai_name_c(const void *ctx, const struct osmo_location_area_id *lai)
239{
240 char *buf = talloc_size(ctx, 32);
241 if (!buf)
242 return NULL;
243 return osmo_lai_name_buf(buf, 32, lai);
244}
245
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100246/*! Return MCC-MNC-LAC-RAC as string, in caller-provided output buffer.
247 * \param[out] buf caller-allocated output buffer
248 * \param[in] buf_len size of buf in bytes
249 * \param[in] rai RAI to encode, the rac member is ignored.
250 * \returns buf
251 */
252char *osmo_rai_name2_buf(char *buf, size_t buf_len, const struct osmo_routing_area_id *rai)
253{
254 char plmn[16];
255 snprintf(buf, buf_len, "%s-%u-%u", osmo_plmn_name_buf(plmn, sizeof(plmn),
256 &rai->lac.plmn), rai->lac.lac, rai->rac);
257 return buf;
258}
259
260/*! Return MCC-MNC-LAC-RAC as string, in a static buffer.
261 * \param[in] rai RAI to encode, the rac member is ignored.
262 * \returns Static string buffer.
263 */
264const char *osmo_rai_name2(const struct osmo_routing_area_id *rai)
265{
266 static __thread char buf[32];
267 return osmo_rai_name2_buf(buf, sizeof(buf), rai);
268}
269
270/*! Return MCC-MNC-LAC-RAC as string, in a talloc-allocated output buffer.
271 * \param[in] ctx talloc context from which to allocate output buffer
272 * \param[in] rai RAI to encode, the rac member is ignored.
273 * \returns string representation of lai in dynamically allocated buffer.
274 */
275char *osmo_rai_name2_c(const void *ctx, const struct osmo_routing_area_id *rai)
276{
277 char *buf = talloc_size(ctx, 32);
278 if (!buf)
279 return NULL;
280 return osmo_rai_name2_buf(buf, 32, rai);
281}
282
Harald Welte4a62eda2019-03-18 18:27:00 +0100283/*! Return MCC-MNC-LAC-CI as string, in caller-provided output buffer.
284 * \param[out] buf caller-allocated output buffer
285 * \param[in] buf_len size of buf in bytes
286 * \param[in] cgi CGI to encode.
287 * \returns buf
288 */
289char *osmo_cgi_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id *cgi)
Neels Hofmeyr43496202018-03-22 14:04:30 +0100290{
Harald Welte4a62eda2019-03-18 18:27:00 +0100291 snprintf(buf, buf_len, "%s-%u", osmo_lai_name(&cgi->lai), cgi->cell_identity);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100292 return buf;
293}
294
295/*! Return MCC-MNC-LAC-CI as string, in a static buffer.
296 * \param[in] cgi CGI to encode.
297 * \returns Static string buffer.
298 */
299const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi)
300{
Harald Welte171ef822019-03-28 10:49:05 +0100301 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100302 return osmo_cgi_name_buf(buf, sizeof(buf), cgi);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100303}
304
305/*! Same as osmo_cgi_name(), but uses a different static buffer.
306 * Useful for printing two distinct CGIs in the same printf format.
307 * \param[in] cgi CGI to encode.
308 * \returns Static string buffer.
309 */
310const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi)
311{
Harald Welte171ef822019-03-28 10:49:05 +0100312 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100313 return osmo_cgi_name_buf(buf, sizeof(buf), cgi);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100314}
315
Harald Welte179f3572019-03-18 18:38:47 +0100316/*! Return MCC-MNC-LAC-CI as string, in a talloc-allocated output buffer.
317 * \param[in] ctx talloc context from which to allocate output buffer
318 * \param[in] cgi CGI to encode.
319 * \returns string representation of CGI in dynamically-allocated buffer.
320 */
321char *osmo_cgi_name_c(const void *ctx, const struct osmo_cell_global_id *cgi)
322{
323 char *buf = talloc_size(ctx, 32);
324 return osmo_cgi_name_buf(buf, 32, cgi);
325}
326
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100327/*! Return MCC-MNC-LAC-RAC-CI as string, in caller-provided output buffer.
328 * \param[out] buf caller-allocated output buffer
329 * \param[in] buf_len size of buf in bytes
330 * \param[in] cgi_ps CGI-PS to encode.
331 * \returns buf
332 */
333char *osmo_cgi_ps_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id_ps *cgi_ps)
334{
335 snprintf(buf, buf_len, "%s-%u", osmo_rai_name2(&cgi_ps->rai), cgi_ps->cell_identity);
336 return buf;
337}
338
339/*! Return MCC-MNC-LAC-RAC-CI as string, in a static buffer.
340 * \param[in] cgi_ps CGI-PS to encode.
341 * \returns Static string buffer.
342 */
343const char *osmo_cgi_ps_name(const struct osmo_cell_global_id_ps *cgi_ps)
344{
345 static __thread char buf[32];
346 return osmo_cgi_ps_name_buf(buf, sizeof(buf), cgi_ps);
347}
348
349/*! Same as osmo_cgi_ps_name(), but uses a different static buffer.
350 * Useful for printing two distinct CGI-PSs in the same printf format.
351 * \param[in] cgi CGI-PS to encode.
352 * \returns Static string buffer.
353 */
354const char *osmo_cgi_ps_name2(const struct osmo_cell_global_id_ps *cgi_ps)
355{
356 static __thread char buf[32];
357 return osmo_cgi_ps_name_buf(buf, sizeof(buf), cgi_ps);
358}
359
360/*! Return MCC-MNC-LAC-RAC-CI as string, in a talloc-allocated output buffer.
361 * \param[in] ctx talloc context from which to allocate output buffer
362 * \param[in] cgi_ps CGI-PS to encode.
363 * \returns string representation of CGI in dynamically-allocated buffer.
364 */
365char *osmo_cgi_ps_name_c(const void *ctx, const struct osmo_cell_global_id_ps *cgi_ps)
366{
367 char *buf = talloc_size(ctx, 32);
368 return osmo_cgi_ps_name_buf(buf, 32, cgi_ps);
369}
370
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100371/*! Return MCC-MNC-LAC-SAC as string, in caller-provided output buffer.
372 * \param[out] buf caller-allocated output buffer
373 * \param[in] buf_len size of buf in bytes
374 * \param[in] sai SAI to encode.
375 * \returns buf
376 */
377char *osmo_sai_name_buf(char *buf, size_t buf_len, const struct osmo_service_area_id *sai)
378{
379 snprintf(buf, buf_len, "%s-%u", osmo_lai_name(&sai->lai), sai->sac);
380 return buf;
381}
382
383/*! Return MCC-MNC-LAC-SAC as string, in a static buffer.
384 * \param[in] sai SAI to encode.
385 * \returns Static string buffer.
386 */
387const char *osmo_sai_name(const struct osmo_service_area_id *sai)
388{
389 static __thread char buf[32];
390 return osmo_sai_name_buf(buf, sizeof(buf), sai);
391}
392
393/*! Same as osmo_cgi_name(), but uses a different static buffer.
394 * Useful for printing two distinct CGIs in the same printf format.
395 * \param[in] sai SAI to encode.
396 * \returns Static string buffer.
397 */
398const char *osmo_sai_name2(const struct osmo_service_area_id *sai)
399{
400 static __thread char buf[32];
401 return osmo_sai_name_buf(buf, sizeof(buf), sai);
402}
403
404/*! Return MCC-MNC-LAC-SAC as string, in a talloc-allocated output buffer.
405 * \param[in] ctx talloc context from which to allocate output buffer
406 * \param[in] sai SAI to encode.
407 * \returns string representation of CGI in dynamically-allocated buffer.
408 */
409char *osmo_sai_name_c(const void *ctx, const struct osmo_service_area_id *sai)
410{
411 char *buf = talloc_size(ctx, 32);
412 return osmo_sai_name_buf(buf, 32, sai);
413}
414
415
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100416static void to_bcd(uint8_t *bcd, uint16_t val)
417{
418 bcd[2] = val % 10;
419 val = val / 10;
420 bcd[1] = val % 10;
421 val = val / 10;
422 bcd[0] = val % 10;
423}
424
Harald Welte4a62eda2019-03-18 18:27:00 +0100425/*! Return string representation of GUMMEI in caller-provided output buffer.
426 * \param[out] buf pointer to caller-provided output buffer
427 * \param[in] buf_len size of buf in bytes
428 * \param[in] gummei GUMMEI to be stringified
429 * \returns buf
430 */
431char *osmo_gummei_name_buf(char *buf, size_t buf_len, const struct osmo_gummei *gummei)
Harald Weltede1da352018-10-08 22:27:04 +0200432{
Harald Welte4a62eda2019-03-18 18:27:00 +0100433 char plmn[16];
434 snprintf(buf, buf_len, "%s-%04x-%02x", osmo_plmn_name_buf(plmn, sizeof(plmn), &gummei->plmn),
Harald Weltede1da352018-10-08 22:27:04 +0200435 gummei->mme.group_id, gummei->mme.code);
436 return buf;
437}
438
Harald Welte4a62eda2019-03-18 18:27:00 +0100439/*! Return string representation of GUMMEI in static output buffer.
440 * \param[in] gummei GUMMEI to be stringified
441 * \returns pointer to static output buffer
442 */
443const char *osmo_gummei_name(const struct osmo_gummei *gummei)
444{
Harald Welte171ef822019-03-28 10:49:05 +0100445 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100446 return osmo_gummei_name_buf(buf, sizeof(buf), gummei);
447}
448
Harald Welte179f3572019-03-18 18:38:47 +0100449/*! Return string representation of GUMMEI in static output buffer.
450 * \param[out] buf pointer to caller-provided output buffer
451 * \param[in] buf_len size of buf in bytes
452 * \param[in] gummei GUMMEI to be stringified
453 * \returns pointer to static output buffer
454 */
455char *osmo_gummei_name_c(const void *ctx, const struct osmo_gummei *gummei)
456{
457 char *buf = talloc_size(ctx, 32);
458 if (!buf)
459 return NULL;
460 return osmo_gummei_name_buf(buf, 32, gummei);
461}
Harald Welte4a62eda2019-03-18 18:27:00 +0100462
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100463/* Convert MCC + MNC to BCD representation
464 * \param[out] bcd_dst caller-allocated memory for output
465 * \param[in] mcc Mobile Country Code
466 * \param[in] mnc Mobile Network Code
467 * \param[in] mnc_3_digits true if the MNC shall have three digits.
468 *
469 * Convert given mcc and mnc to BCD and write to *bcd_dst, which must be an
470 * allocated buffer of (at least) 3 bytes length. Encode the MNC in three
471 * digits if its integer value is > 99, or if mnc_3_digits is passed true.
472 * Encode an MNC < 100 with mnc_3_digits passed as true as a three-digit MNC
473 * with leading zeros in the BCD representation.
474 */
475void osmo_plmn_to_bcd(uint8_t *bcd_dst, const struct osmo_plmn_id *plmn)
476{
477 uint8_t bcd[3];
478
479 to_bcd(bcd, plmn->mcc);
480 bcd_dst[0] = bcd[0] | (bcd[1] << 4);
481 bcd_dst[1] = bcd[2];
482
483 to_bcd(bcd, plmn->mnc);
484 if (plmn->mnc > 99 || plmn->mnc_3_digits) {
485 bcd_dst[1] |= bcd[2] << 4;
486 bcd_dst[2] = bcd[0] | (bcd[1] << 4);
487 } else {
488 bcd_dst[1] |= 0xf << 4;
489 bcd_dst[2] = bcd[1] | (bcd[2] << 4);
490 }
491}
492
Philipp Maierc7c1ff52022-01-14 17:13:29 +0100493/* Convert given 3-byte BCD buffer to integers and write results to plmn->mcc and plmn->mnc. The first three BCD digits
494 * result in the MCC and the remaining ones in the MNC. Set plmn->mnc_3_digits as false if the MNC's most significant
495 * digit is encoded as 0xF, true otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of
496 * 0xF.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100497 * \param[in] bcd_src 3-byte BCD buffer containing MCC+MNC representations.
Philipp Maierc7c1ff52022-01-14 17:13:29 +0100498 * \param[out] plmn user provided memory to store the result.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100499 */
500void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn)
501{
502 plmn->mcc = (bcd_src[0] & 0x0f) * 100
503 + (bcd_src[0] >> 4) * 10
504 + (bcd_src[1] & 0x0f);
505
506 if ((bcd_src[1] & 0xf0) == 0xf0) {
507 plmn->mnc = (bcd_src[2] & 0x0f) * 10
508 + (bcd_src[2] >> 4);
509 plmn->mnc_3_digits = false;
510 } else {
511 plmn->mnc = (bcd_src[2] & 0x0f) * 100
512 + (bcd_src[2] >> 4) * 10
513 + (bcd_src[1] >> 4);
514 plmn->mnc_3_digits = true;
515 }
516}
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100517
518/* Convert string to MNC, detecting 3-digit MNC with leading zeros.
519 * Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
520 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
521 * \param mnc_str[in] String representation of an MNC, with or without leading zeros.
522 * \param mnc[out] MNC result buffer, or NULL.
523 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
524 * \returns zero on success, -EINVAL in case of surplus characters, negative errno in case of conversion
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100525 * errors. In case of error, do not modify the out-arguments.
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100526 */
527int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits)
528{
Neels Hofmeyr34907fe2021-09-05 19:50:34 +0200529 int _mnc = 0;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100530 bool _mnc_3_digits = false;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100531 int rc = 0;
532
Kévin Redon1af2cd52019-05-23 19:00:19 +0200533 if (!mnc_str || !isdigit((unsigned char)mnc_str[0]) || strlen(mnc_str) > 3)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100534 return -EINVAL;
535
Neels Hofmeyr34907fe2021-09-05 19:50:34 +0200536 rc = osmo_str_to_int(&_mnc, mnc_str, 10, 0, 999);
537 /* Heed the API definition to return -EINVAL in case of surplus chars */
538 if (rc == -E2BIG)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100539 return -EINVAL;
Neels Hofmeyr34907fe2021-09-05 19:50:34 +0200540 /* Heed the API definition to always return negative errno */
541 if (rc > 0)
542 return -rc;
543 if (rc < 0)
544 return rc;
545
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100546 _mnc_3_digits = strlen(mnc_str) > 2;
547
548 if (mnc)
549 *mnc = (uint16_t)_mnc;
550 if (mnc_3_digits)
551 *mnc_3_digits = _mnc_3_digits;
552 return rc;
553}
554
555/* Compare two MNC with three-digit flag.
556 * The mnc_3_digits flags passed in only have an effect if the MNC are < 100, i.e. if they would amount
557 * to a change in leading zeros in a BCD representation. An MNC >= 100 implies three digits, and the flag
558 * is actually ignored.
559 * \param a_mnc[in] "Left" side MNC.
560 * \param a_mnc_3_digits[in] "Left" side three-digits flag.
561 * \param b_mnc[in] "Right" side MNC.
562 * \param b_mnc_3_digits[in] "Right" side three-digits flag.
563 * \returns 0 if the MNC are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
564int osmo_mnc_cmp(uint16_t a_mnc, bool a_mnc_3_digits, uint16_t b_mnc, bool b_mnc_3_digits)
565{
566 if (a_mnc < b_mnc)
567 return -1;
568 if (a_mnc > b_mnc)
569 return 1;
570 /* a_mnc == b_mnc, but same amount of leading zeros? */
571 if (a_mnc < 100 && a_mnc_3_digits != b_mnc_3_digits)
572 return a_mnc_3_digits ? 1 : -1;
573 return 0;
574}
575
576/* Compare two PLMN.
577 * \param a[in] "Left" side PLMN.
578 * \param b[in] "Right" side PLMN.
579 * \returns 0 if the PLMN are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
580int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b)
581{
582 if (a == b)
583 return 0;
584 if (a->mcc < b->mcc)
585 return -1;
586 if (a->mcc > b->mcc)
587 return 1;
588 return osmo_mnc_cmp(a->mnc, a->mnc_3_digits, b->mnc, b->mnc_3_digits);
589}
Harald Weltede1da352018-10-08 22:27:04 +0200590
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200591/* Compare two LAI.
592 * The order of comparison is MCC, MNC, LAC. See also osmo_plmn_cmp().
593 * \param a[in] "Left" side LAI.
594 * \param b[in] "Right" side LAI.
595 * \returns 0 if the LAI are equal, -1 if a < b, 1 if a > b. */
596int osmo_lai_cmp(const struct osmo_location_area_id *a, const struct osmo_location_area_id *b)
597{
598 int rc = osmo_plmn_cmp(&a->plmn, &b->plmn);
599 if (rc)
600 return rc;
601 if (a->lac < b->lac)
602 return -1;
603 if (a->lac > b->lac)
604 return 1;
605 return 0;
606}
607
Pau Espin Pedrold426ba62021-01-22 17:44:34 +0100608/* Compare two RAI.
609 * The order of comparison is MCC, MNC, LAC, RAC. See also osmo_lai_cmp().
610 * \param a[in] "Left" side RAI.
611 * \param b[in] "Right" side RAI.
612 * \returns 0 if the RAI are equal, -1 if a < b, 1 if a > b. */
613int osmo_rai_cmp(const struct osmo_routing_area_id *a, const struct osmo_routing_area_id *b)
614{
615 int rc = osmo_lai_cmp(&a->lac, &b->lac);
616 if (rc)
617 return rc;
618 if (a->rac < b->rac)
619 return -1;
620 if (a->rac > b->rac)
621 return 1;
622 return 0;
623}
624
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200625/* Compare two CGI.
626 * The order of comparison is MCC, MNC, LAC, CI. See also osmo_lai_cmp().
627 * \param a[in] "Left" side CGI.
628 * \param b[in] "Right" side CGI.
629 * \returns 0 if the CGI are equal, -1 if a < b, 1 if a > b. */
630int osmo_cgi_cmp(const struct osmo_cell_global_id *a, const struct osmo_cell_global_id *b)
631{
632 int rc = osmo_lai_cmp(&a->lai, &b->lai);
633 if (rc)
634 return rc;
635 if (a->cell_identity < b->cell_identity)
636 return -1;
637 if (a->cell_identity > b->cell_identity)
638 return 1;
639 return 0;
640}
641
Pau Espin Pedrold426ba62021-01-22 17:44:34 +0100642/* Compare two CGI-PS.
643 * The order of comparison is MCC, MNC, LAC, RAC, CI. See also osmo_rai_cmp().
644 * \param a[in] "Left" side CGI-PS.
645 * \param b[in] "Right" side CGI-PS.
646 * \returns 0 if the CGI are equal, -1 if a < b, 1 if a > b. */
647int osmo_cgi_ps_cmp(const struct osmo_cell_global_id_ps *a, const struct osmo_cell_global_id_ps *b)
648{
649 int rc = osmo_rai_cmp(&a->rai, &b->rai);
650 if (rc)
651 return rc;
652 if (a->cell_identity < b->cell_identity)
653 return -1;
654 if (a->cell_identity > b->cell_identity)
655 return 1;
656 return 0;
657}
658
Harald Weltede1da352018-10-08 22:27:04 +0200659/*! Generate TS 23.003 Section 19.2 Home Network Realm/Domain (text form)
660 * \param out[out] caller-provided output buffer, at least 33 bytes long
661 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
662 * \returns number of characters printed (excluding NUL); negative on error */
663int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn)
664{
665 if (plmn->mcc > 999)
666 return -EINVAL;
667 if (plmn->mnc > 999)
668 return -EINVAL;
669 return sprintf(out, "epc.mnc%03u.mcc%03u.3gppnetwork.org", plmn->mnc, plmn->mcc);
670}
671
672/*! Parse a TS 23.003 Section 19.2 Home Network Realm/Domain (text form) into a \ref osmo_plmn_id
673 * \param out[out] caller-allocated output structure
674 * \param in[in] character string representation to be parsed
675 * \returns 0 on success; negative on error */
676int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in)
677{
678 int rc;
679
680 memset(out, 0, sizeof(*out));
681 rc = sscanf(in, "epc.mnc%03hu.mcc%03hu.3gppnetwork.org", &out->mnc, &out->mcc);
682 if (rc < 0)
683 return rc;
684 if (rc != 2)
685 return -EINVAL;
686 return 0;
687}
688
689/*! Generate TS 23.003 Section 19.4.2.4 MME Domain (text form)
690 * \param out[out] caller-provided output buffer, at least 56 bytes long
691 * \param gummei[in] Structure representing the Globally Unique MME Identifier
692 * \returns number of characters printed (excluding NUL); negative on error */
693int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei)
694{
695 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
696 int rc;
697 rc = osmo_gen_home_network_domain(domain, &gummei->plmn);
698 if (rc < 0)
699 return rc;
700 return sprintf(out, "mmec%02x.mmegi%04x.mme.%s", gummei->mme.code, gummei->mme.group_id, domain);
701}
702
703/*! Parse a TS 23.003 Section 19.4.2.4 MME Domain (text form) into a \ref osmo_gummei
704 * \param out[out] caller-allocated output GUMMEI structure
705 * \param in[in] character string representation to be parsed
706 * \returns 0 on success; negative on error */
707int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in)
708{
709 int rc;
710
711 memset(out, 0, sizeof(*out));
712 rc = sscanf(in, "mmec%02hhx.mmegi%04hx.mme.epc.mnc%03hu.mcc%03hu.3gppnetwork.org",
713 &out->mme.code, &out->mme.group_id,
714 &out->plmn.mnc, &out->plmn.mcc);
715 if (rc < 0)
716 return rc;
717 if (rc != 4)
718 return -EINVAL;
719 return 0;
720}
721
722/*! Generate TS 23.003 Section 19.4.2.4 MME Group Domain (text form)
723 * \param out[out] caller-provided output buffer, at least 56 bytes long
724 * \param mmegi[in] MME Group Identifier
725 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
726 * \returns number of characters printed (excluding NUL); negative on error */
727int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn)
728{
729 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
730 int rc;
731 rc = osmo_gen_home_network_domain(domain, plmn);
732 if (rc < 0)
733 return rc;
734 return sprintf(out, "mmegi%04x.mme.%s", mmegi, domain);
735}