Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gsm23003.h */ |
| 2 | |
Harald Welte | bda26c0 | 2016-05-11 09:13:08 +0200 | [diff] [blame] | 3 | #pragma once |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 4 | |
Harald Welte | bda26c0 | 2016-05-11 09:13:08 +0200 | [diff] [blame] | 5 | #include <stdint.h> |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 6 | #include <stdbool.h> |
Harald Welte | bda26c0 | 2016-05-11 09:13:08 +0200 | [diff] [blame] | 7 | |
| 8 | /* 23.003 Chapter 12.1 */ |
| 9 | struct osmo_plmn_id { |
| 10 | uint16_t mcc; |
| 11 | uint16_t mnc; |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 12 | bool mnc_3_digits; /*< ignored and implied true if mnc > 99, otherwise defines leading zeros. */ |
Harald Welte | bda26c0 | 2016-05-11 09:13:08 +0200 | [diff] [blame] | 13 | }; |
| 14 | |
| 15 | /* 4.1 */ |
| 16 | struct osmo_location_area_id { |
| 17 | struct osmo_plmn_id plmn; |
| 18 | uint16_t lac; |
| 19 | }; |
| 20 | |
| 21 | /* 4.2 */ |
| 22 | struct osmo_routing_area_id { |
| 23 | struct osmo_location_area_id lac; |
| 24 | uint8_t rac; |
| 25 | }; |
| 26 | |
| 27 | /* 4.3.1 */ |
| 28 | struct osmo_cell_global_id { |
| 29 | struct osmo_location_area_id lai; |
| 30 | uint16_t cell_identity; |
| 31 | }; |
| 32 | |
Pau Espin Pedrol | ca33a71 | 2021-01-05 19:36:48 +0100 | [diff] [blame] | 33 | struct osmo_cell_global_id_ps { |
| 34 | struct osmo_routing_area_id rai; |
| 35 | uint16_t cell_identity; |
| 36 | }; |
| 37 | |
Neels Hofmeyr | 3a50453 | 2019-02-10 22:28:27 +0100 | [diff] [blame] | 38 | /*! Bitmask of items contained in a struct osmo_cell_global_id. |
| 39 | * See also gsm0808_cell_id_to_cgi(). |
| 40 | */ |
| 41 | enum osmo_cgi_part { |
| 42 | OSMO_CGI_PART_PLMN = 1, |
| 43 | OSMO_CGI_PART_LAC = 2, |
| 44 | OSMO_CGI_PART_CI = 4, |
Pau Espin Pedrol | ca33a71 | 2021-01-05 19:36:48 +0100 | [diff] [blame] | 45 | OSMO_CGI_PART_RAC = 8, |
Neels Hofmeyr | 3a50453 | 2019-02-10 22:28:27 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 48 | /* Actually defined in 3GPP TS 48.008 3.2.2.27 Cell Identifier List, |
| 49 | * but conceptually belongs with the above structures. */ |
| 50 | struct osmo_lac_and_ci_id { |
| 51 | uint16_t lac; |
| 52 | uint16_t ci; |
| 53 | }; |
| 54 | |
Harald Welte | bda26c0 | 2016-05-11 09:13:08 +0200 | [diff] [blame] | 55 | /* 12.5 */ |
| 56 | struct osmo_service_area_id { |
| 57 | struct osmo_location_area_id lai; |
| 58 | uint16_t sac; |
| 59 | }; |
| 60 | |
| 61 | /* 12.6 */ |
| 62 | struct osmo_shared_network_area_id { |
| 63 | struct osmo_plmn_id plmn; |
| 64 | uint32_t snac; |
| 65 | }; |
| 66 | |
| 67 | /* 5.1 */ |
| 68 | enum osmo_gsn_addr_type { |
| 69 | GSN_ADDR_TYPE_IPV4 = 0, |
| 70 | GSN_ADDR_TYPE_IPV6 = 1, |
| 71 | }; |
| 72 | |
| 73 | /* 5.1 */ |
| 74 | struct osmo_gsn_address { |
| 75 | enum osmo_gsn_addr_type type; |
| 76 | uint8_t length; |
| 77 | uint8_t addr[16]; |
| 78 | }; |
| 79 | |
| 80 | /* 19.4.2.3 */ |
| 81 | struct osmo_tracking_area_id { |
| 82 | struct osmo_plmn_id plmn; |
| 83 | uint16_t tac; |
| 84 | }; |
| 85 | |
| 86 | struct osmo_eutran_cell_global_id { |
| 87 | struct osmo_plmn_id plmn; |
| 88 | uint32_t eci; /* FIXME */ |
| 89 | }; |
| 90 | |
| 91 | /* 2.8.1 */ |
| 92 | struct osmo_mme_id { |
| 93 | uint16_t group_id; |
| 94 | uint8_t code; |
| 95 | }; |
| 96 | |
| 97 | /* 2.8.1 */ |
| 98 | struct osmo_gummei { |
| 99 | struct osmo_plmn_id plmn; |
| 100 | struct osmo_mme_id mme; |
| 101 | }; |
| 102 | |
| 103 | /* 2.8.1 */ |
| 104 | struct osmo_guti { |
| 105 | struct osmo_gummei gummei; |
| 106 | uint32_t mtmsi; |
| 107 | }; |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 108 | |
| 109 | bool osmo_imsi_str_valid(const char *imsi); |
| 110 | bool osmo_msisdn_str_valid(const char *msisdn); |
Oliver Smith | 894be2d | 2019-01-11 13:13:37 +0100 | [diff] [blame] | 111 | bool osmo_imei_str_valid(const char *imei, bool with_15th_digit); |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 112 | |
| 113 | const char *osmo_mcc_name(uint16_t mcc); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 114 | char *osmo_mcc_name_buf(char *buf, size_t buf_len, uint16_t mcc); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 115 | const char *osmo_mcc_name_c(const void *ctx, uint16_t mcc); |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 116 | const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 117 | char *osmo_mnc_name_buf(char *buf, size_t buf_len, uint16_t mnc, bool mnc_3_digits); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 118 | char *osmo_mnc_name_c(const void *ctx, uint16_t mnc, bool mnc_3_digits); |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 119 | const char *osmo_plmn_name(const struct osmo_plmn_id *plmn); |
| 120 | const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 121 | char *osmo_plmn_name_buf(char *buf, size_t buf_len, const struct osmo_plmn_id *plmn); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 122 | char *osmo_plmn_name_c(const void *ctx, const struct osmo_plmn_id *plmn); |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 123 | const char *osmo_lai_name(const struct osmo_location_area_id *lai); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 124 | char *osmo_lai_name_buf(char *buf, size_t buf_len, const struct osmo_location_area_id *lai); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 125 | char *osmo_lai_name_c(const void *ctx, const struct osmo_location_area_id *lai); |
Pau Espin Pedrol | ca33a71 | 2021-01-05 19:36:48 +0100 | [diff] [blame] | 126 | const char *osmo_rai_name2(const struct osmo_routing_area_id *rai); |
| 127 | char *osmo_rai_name2_buf(char *buf, size_t buf_len, const struct osmo_routing_area_id *rai); |
| 128 | char *osmo_rai_name2_c(const void *ctx, const struct osmo_routing_area_id *rai); |
Neels Hofmeyr | 4349620 | 2018-03-22 14:04:30 +0100 | [diff] [blame] | 129 | const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi); |
| 130 | const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 131 | char *osmo_cgi_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id *cgi); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 132 | char *osmo_cgi_name_c(const void *ctx, const struct osmo_cell_global_id *cgi); |
Pau Espin Pedrol | ca33a71 | 2021-01-05 19:36:48 +0100 | [diff] [blame] | 133 | const char *osmo_cgi_ps_name(const struct osmo_cell_global_id_ps *cgi_ps); |
| 134 | const char *osmo_cgi_ps_name2(const struct osmo_cell_global_id_ps *cgi_ps); |
| 135 | char *osmo_cgi_ps_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id_ps *cgi_ps); |
| 136 | char *osmo_cgi_ps_name_c(const void *ctx, const struct osmo_cell_global_id_ps *cgi_ps); |
Harald Welte | de1da35 | 2018-10-08 22:27:04 +0200 | [diff] [blame] | 137 | const char *osmo_gummei_name(const struct osmo_gummei *gummei); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 138 | char *osmo_gummei_name_buf(char *buf, size_t buf_len, const struct osmo_gummei *gummei); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 139 | char *osmo_gummei_name_c(const void *ctx, const struct osmo_gummei *gummei); |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 140 | |
| 141 | void osmo_plmn_to_bcd(uint8_t *bcd_dst, const struct osmo_plmn_id *plmn); |
| 142 | void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn); |
Neels Hofmeyr | 721aa6d | 2018-02-20 21:38:00 +0100 | [diff] [blame] | 143 | |
| 144 | int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits); |
| 145 | |
Neels Hofmeyr | 4d68fd0 | 2018-03-05 04:16:48 +0100 | [diff] [blame] | 146 | /* Convert string to MCC. |
| 147 | * \param mcc_str[in] String representation of an MCC, with or without leading zeros. |
| 148 | * \param mcc[out] MCC result buffer, or NULL. |
| 149 | * \returns zero on success, -EINVAL in case of surplus characters, negative errno in case of conversion |
| 150 | * errors. In case of error, do not modify the out-arguments. |
| 151 | */ |
| 152 | static inline int osmo_mcc_from_str(const char *mcc_str, uint16_t *mcc) |
| 153 | { |
| 154 | return osmo_mnc_from_str(mcc_str, mcc, NULL); |
| 155 | } |
| 156 | |
Neels Hofmeyr | 721aa6d | 2018-02-20 21:38:00 +0100 | [diff] [blame] | 157 | int osmo_mnc_cmp(uint16_t a_mnc, bool a_mnc_3_digits, uint16_t b_mnc, bool b_mnc_3_digits); |
| 158 | int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b); |
Neels Hofmeyr | d01ef75 | 2018-09-21 15:57:26 +0200 | [diff] [blame] | 159 | int osmo_lai_cmp(const struct osmo_location_area_id *a, const struct osmo_location_area_id *b); |
Pau Espin Pedrol | d426ba6 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 160 | int osmo_rai_cmp(const struct osmo_routing_area_id *a, const struct osmo_routing_area_id *b); |
Neels Hofmeyr | d01ef75 | 2018-09-21 15:57:26 +0200 | [diff] [blame] | 161 | int osmo_cgi_cmp(const struct osmo_cell_global_id *a, const struct osmo_cell_global_id *b); |
Pau Espin Pedrol | d426ba6 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 162 | int osmo_cgi_ps_cmp(const struct osmo_cell_global_id_ps *a, const struct osmo_cell_global_id_ps *b); |
Harald Welte | de1da35 | 2018-10-08 22:27:04 +0200 | [diff] [blame] | 163 | |
| 164 | int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn); |
| 165 | int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in); |
| 166 | int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei); |
| 167 | int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn); |
| 168 | int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in); |