blob: 4070581f95251dbbfce25871be5a8b7c85575fe6 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm23003.h */
2
Harald Weltebda26c02016-05-11 09:13:08 +02003#pragma once
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02004
Harald Weltebda26c02016-05-11 09:13:08 +02005#include <stdint.h>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02006#include <stdbool.h>
Harald Weltebda26c02016-05-11 09:13:08 +02007
8/* 23.003 Chapter 12.1 */
9struct osmo_plmn_id {
10 uint16_t mcc;
11 uint16_t mnc;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010012 bool mnc_3_digits; /*< ignored and implied true if mnc > 99, otherwise defines leading zeros. */
Harald Weltebda26c02016-05-11 09:13:08 +020013};
14
15/* 4.1 */
16struct osmo_location_area_id {
17 struct osmo_plmn_id plmn;
18 uint16_t lac;
19};
20
21/* 4.2 */
22struct osmo_routing_area_id {
23 struct osmo_location_area_id lac;
24 uint8_t rac;
25};
26
27/* 4.3.1 */
28struct osmo_cell_global_id {
29 struct osmo_location_area_id lai;
30 uint16_t cell_identity;
31};
32
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +010033/* 3GPP TS 48.018:
34 * 8c.1.4.1.1 GERAN BSS identification (RIM)
35 * sec 11.3.9 Cell Identifier */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +010036struct osmo_cell_global_id_ps {
37 struct osmo_routing_area_id rai;
38 uint16_t cell_identity;
39};
40
Neels Hofmeyr3a504532019-02-10 22:28:27 +010041/*! Bitmask of items contained in a struct osmo_cell_global_id.
42 * See also gsm0808_cell_id_to_cgi().
43 */
44enum osmo_cgi_part {
45 OSMO_CGI_PART_PLMN = 1,
46 OSMO_CGI_PART_LAC = 2,
47 OSMO_CGI_PART_CI = 4,
Pau Espin Pedrolca33a712021-01-05 19:36:48 +010048 OSMO_CGI_PART_RAC = 8,
Neels Hofmeyr3a504532019-02-10 22:28:27 +010049};
50
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010051/* Actually defined in 3GPP TS 48.008 3.2.2.27 Cell Identifier List,
52 * but conceptually belongs with the above structures. */
53struct osmo_lac_and_ci_id {
54 uint16_t lac;
55 uint16_t ci;
56};
57
Harald Weltebda26c02016-05-11 09:13:08 +020058/* 12.5 */
59struct osmo_service_area_id {
60 struct osmo_location_area_id lai;
61 uint16_t sac;
62};
63
64/* 12.6 */
65struct osmo_shared_network_area_id {
66 struct osmo_plmn_id plmn;
67 uint32_t snac;
68};
69
70/* 5.1 */
71enum osmo_gsn_addr_type {
72 GSN_ADDR_TYPE_IPV4 = 0,
73 GSN_ADDR_TYPE_IPV6 = 1,
74};
75
76/* 5.1 */
77struct osmo_gsn_address {
78 enum osmo_gsn_addr_type type;
79 uint8_t length;
80 uint8_t addr[16];
81};
82
83/* 19.4.2.3 */
84struct osmo_tracking_area_id {
85 struct osmo_plmn_id plmn;
86 uint16_t tac;
87};
88
89struct osmo_eutran_cell_global_id {
90 struct osmo_plmn_id plmn;
91 uint32_t eci; /* FIXME */
92};
93
94/* 2.8.1 */
95struct osmo_mme_id {
96 uint16_t group_id;
97 uint8_t code;
98};
99
100/* 2.8.1 */
101struct osmo_gummei {
102 struct osmo_plmn_id plmn;
103 struct osmo_mme_id mme;
104};
105
106/* 2.8.1 */
107struct osmo_guti {
108 struct osmo_gummei gummei;
109 uint32_t mtmsi;
110};
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +0200111
112bool osmo_imsi_str_valid(const char *imsi);
113bool osmo_msisdn_str_valid(const char *msisdn);
Oliver Smith894be2d2019-01-11 13:13:37 +0100114bool osmo_imei_str_valid(const char *imei, bool with_15th_digit);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100115
116const char *osmo_mcc_name(uint16_t mcc);
Harald Welte4a62eda2019-03-18 18:27:00 +0100117char *osmo_mcc_name_buf(char *buf, size_t buf_len, uint16_t mcc);
Harald Welte179f3572019-03-18 18:38:47 +0100118const char *osmo_mcc_name_c(const void *ctx, uint16_t mcc);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100119const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits);
Harald Welte4a62eda2019-03-18 18:27:00 +0100120char *osmo_mnc_name_buf(char *buf, size_t buf_len, uint16_t mnc, bool mnc_3_digits);
Harald Welte179f3572019-03-18 18:38:47 +0100121char *osmo_mnc_name_c(const void *ctx, uint16_t mnc, bool mnc_3_digits);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100122const char *osmo_plmn_name(const struct osmo_plmn_id *plmn);
123const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn);
Harald Welte4a62eda2019-03-18 18:27:00 +0100124char *osmo_plmn_name_buf(char *buf, size_t buf_len, const struct osmo_plmn_id *plmn);
Harald Welte179f3572019-03-18 18:38:47 +0100125char *osmo_plmn_name_c(const void *ctx, const struct osmo_plmn_id *plmn);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100126const char *osmo_lai_name(const struct osmo_location_area_id *lai);
Harald Welte4a62eda2019-03-18 18:27:00 +0100127char *osmo_lai_name_buf(char *buf, size_t buf_len, const struct osmo_location_area_id *lai);
Harald Welte179f3572019-03-18 18:38:47 +0100128char *osmo_lai_name_c(const void *ctx, const struct osmo_location_area_id *lai);
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100129const char *osmo_rai_name2(const struct osmo_routing_area_id *rai);
130char *osmo_rai_name2_buf(char *buf, size_t buf_len, const struct osmo_routing_area_id *rai);
131char *osmo_rai_name2_c(const void *ctx, const struct osmo_routing_area_id *rai);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100132const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi);
133const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi);
Harald Welte4a62eda2019-03-18 18:27:00 +0100134char *osmo_cgi_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id *cgi);
Harald Welte179f3572019-03-18 18:38:47 +0100135char *osmo_cgi_name_c(const void *ctx, const struct osmo_cell_global_id *cgi);
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100136const char *osmo_cgi_ps_name(const struct osmo_cell_global_id_ps *cgi_ps);
137const char *osmo_cgi_ps_name2(const struct osmo_cell_global_id_ps *cgi_ps);
138char *osmo_cgi_ps_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id_ps *cgi_ps);
139char *osmo_cgi_ps_name_c(const void *ctx, const struct osmo_cell_global_id_ps *cgi_ps);
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100140const char *osmo_sai_name(const struct osmo_service_area_id *sai);
141const char *osmo_sai_name2(const struct osmo_service_area_id *sai);
142char *osmo_sai_name_buf(char *buf, size_t buf_len, const struct osmo_service_area_id *sai);
143char *osmo_sai_name_c(const void *ctx, const struct osmo_service_area_id *sai);
Harald Weltede1da352018-10-08 22:27:04 +0200144const char *osmo_gummei_name(const struct osmo_gummei *gummei);
Harald Welte4a62eda2019-03-18 18:27:00 +0100145char *osmo_gummei_name_buf(char *buf, size_t buf_len, const struct osmo_gummei *gummei);
Harald Welte179f3572019-03-18 18:38:47 +0100146char *osmo_gummei_name_c(const void *ctx, const struct osmo_gummei *gummei);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100147
148void osmo_plmn_to_bcd(uint8_t *bcd_dst, const struct osmo_plmn_id *plmn);
149void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn);
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100150
151int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits);
152
Neels Hofmeyr4d68fd02018-03-05 04:16:48 +0100153/* Convert string to MCC.
154 * \param mcc_str[in] String representation of an MCC, with or without leading zeros.
155 * \param mcc[out] MCC result buffer, or NULL.
156 * \returns zero on success, -EINVAL in case of surplus characters, negative errno in case of conversion
157 * errors. In case of error, do not modify the out-arguments.
158 */
159static inline int osmo_mcc_from_str(const char *mcc_str, uint16_t *mcc)
160{
161 return osmo_mnc_from_str(mcc_str, mcc, NULL);
162}
163
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100164int osmo_mnc_cmp(uint16_t a_mnc, bool a_mnc_3_digits, uint16_t b_mnc, bool b_mnc_3_digits);
165int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b);
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200166int osmo_lai_cmp(const struct osmo_location_area_id *a, const struct osmo_location_area_id *b);
Pau Espin Pedrold426ba62021-01-22 17:44:34 +0100167int osmo_rai_cmp(const struct osmo_routing_area_id *a, const struct osmo_routing_area_id *b);
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200168int osmo_cgi_cmp(const struct osmo_cell_global_id *a, const struct osmo_cell_global_id *b);
Pau Espin Pedrold426ba62021-01-22 17:44:34 +0100169int osmo_cgi_ps_cmp(const struct osmo_cell_global_id_ps *a, const struct osmo_cell_global_id_ps *b);
Harald Weltede1da352018-10-08 22:27:04 +0200170
171int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn);
172int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in);
173int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei);
174int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn);
175int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in);