diff options
-rw-r--r-- | include/osmocom/gsm/gsm23003.h | 7 | ||||
-rw-r--r-- | include/osmocom/gsm/protocol/gsm_23_003.h | 6 | ||||
-rw-r--r-- | src/gsm/gsm23003.c | 86 | ||||
-rw-r--r-- | src/gsm/libosmogsm.map | 6 | ||||
-rw-r--r-- | tests/gsm23003/gsm23003_test.c | 75 | ||||
-rw-r--r-- | tests/gsm23003/gsm23003_test.ok | 5 |
6 files changed, 185 insertions, 0 deletions
diff --git a/include/osmocom/gsm/gsm23003.h b/include/osmocom/gsm/gsm23003.h index fd4f3694..2f380aec 100644 --- a/include/osmocom/gsm/gsm23003.h +++ b/include/osmocom/gsm/gsm23003.h @@ -101,6 +101,7 @@ const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn); const char *osmo_lai_name(const struct osmo_location_area_id *lai); const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi); const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi); +const char *osmo_gummei_name(const struct osmo_gummei *gummei); void osmo_plmn_to_bcd(uint8_t *bcd_dst, const struct osmo_plmn_id *plmn); void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn); @@ -120,3 +121,9 @@ static inline int osmo_mcc_from_str(const char *mcc_str, uint16_t *mcc) int osmo_mnc_cmp(uint16_t a_mnc, bool a_mnc_3_digits, uint16_t b_mnc, bool b_mnc_3_digits); int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b); + +int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn); +int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in); +int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei); +int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn); +int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in); diff --git a/include/osmocom/gsm/protocol/gsm_23_003.h b/include/osmocom/gsm/protocol/gsm_23_003.h index 0e669399..ee697ff4 100644 --- a/include/osmocom/gsm/protocol/gsm_23_003.h +++ b/include/osmocom/gsm/protocol/gsm_23_003.h @@ -24,3 +24,9 @@ GSM23003_IMEI_SNR_NUM_DIGITS + 1) #define GSM23003_IMEISV_NUM_DIGITS (GSM23003_IMEI_TAC_NUM_DIGITS + \ GSM23003_IMEI_SNR_NUM_DIGITS + 2) + +/* Chapter 19.2 "epc.mnc000.mcc000.3gppnetwork.org" */ +#define GSM23003_HOME_NETWORK_DOMAIN_LEN 33 + +/* Chapter 19.4.2.4: "mmec00.mmegi0000.mme.epc.mnc000.mcc000.3gppnetwork.org" */ +#define GSM23003_MME_DOMAIN_LEN 55 diff --git a/src/gsm/gsm23003.c b/src/gsm/gsm23003.c index 2c3b21e6..4fdad48a 100644 --- a/src/gsm/gsm23003.c +++ b/src/gsm/gsm23003.c @@ -169,6 +169,14 @@ static void to_bcd(uint8_t *bcd, uint16_t val) bcd[0] = val % 10; } +const char *osmo_gummei_name(const struct osmo_gummei *gummei) +{ + static char buf[32]; + snprintf(buf, sizeof(buf), "%s-%04x-%02x", osmo_plmn_name(&gummei->plmn), + gummei->mme.group_id, gummei->mme.code); + return buf; +} + /* Convert MCC + MNC to BCD representation * \param[out] bcd_dst caller-allocated memory for output * \param[in] mcc Mobile Country Code @@ -297,3 +305,81 @@ int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b) return 1; return osmo_mnc_cmp(a->mnc, a->mnc_3_digits, b->mnc, b->mnc_3_digits); } + +/*! Generate TS 23.003 Section 19.2 Home Network Realm/Domain (text form) + * \param out[out] caller-provided output buffer, at least 33 bytes long + * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC) + * \returns number of characters printed (excluding NUL); negative on error */ +int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn) +{ + if (plmn->mcc > 999) + return -EINVAL; + if (plmn->mnc > 999) + return -EINVAL; + return sprintf(out, "epc.mnc%03u.mcc%03u.3gppnetwork.org", plmn->mnc, plmn->mcc); +} + +/*! Parse a TS 23.003 Section 19.2 Home Network Realm/Domain (text form) into a \ref osmo_plmn_id + * \param out[out] caller-allocated output structure + * \param in[in] character string representation to be parsed + * \returns 0 on success; negative on error */ +int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in) +{ + int rc; + + memset(out, 0, sizeof(*out)); + rc = sscanf(in, "epc.mnc%03hu.mcc%03hu.3gppnetwork.org", &out->mnc, &out->mcc); + if (rc < 0) + return rc; + if (rc != 2) + return -EINVAL; + return 0; +} + +/*! Generate TS 23.003 Section 19.4.2.4 MME Domain (text form) + * \param out[out] caller-provided output buffer, at least 56 bytes long + * \param gummei[in] Structure representing the Globally Unique MME Identifier + * \returns number of characters printed (excluding NUL); negative on error */ +int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei) +{ + char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1]; + int rc; + rc = osmo_gen_home_network_domain(domain, &gummei->plmn); + if (rc < 0) + return rc; + return sprintf(out, "mmec%02x.mmegi%04x.mme.%s", gummei->mme.code, gummei->mme.group_id, domain); +} + +/*! Parse a TS 23.003 Section 19.4.2.4 MME Domain (text form) into a \ref osmo_gummei + * \param out[out] caller-allocated output GUMMEI structure + * \param in[in] character string representation to be parsed + * \returns 0 on success; negative on error */ +int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in) +{ + int rc; + + memset(out, 0, sizeof(*out)); + rc = sscanf(in, "mmec%02hhx.mmegi%04hx.mme.epc.mnc%03hu.mcc%03hu.3gppnetwork.org", + &out->mme.code, &out->mme.group_id, + &out->plmn.mnc, &out->plmn.mcc); + if (rc < 0) + return rc; + if (rc != 4) + return -EINVAL; + return 0; +} + +/*! Generate TS 23.003 Section 19.4.2.4 MME Group Domain (text form) + * \param out[out] caller-provided output buffer, at least 56 bytes long + * \param mmegi[in] MME Group Identifier + * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC) + * \returns number of characters printed (excluding NUL); negative on error */ +int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn) +{ + char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1]; + int rc; + rc = osmo_gen_home_network_domain(domain, plmn); + if (rc < 0) + return rc; + return sprintf(out, "mmegi%04x.mme.%s", mmegi, domain); +} diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map index 3cc2ec5f..4813e135 100644 --- a/src/gsm/libosmogsm.map +++ b/src/gsm/libosmogsm.map @@ -315,9 +315,15 @@ osmo_lai_name; osmo_rai_name; osmo_cgi_name; osmo_cgi_name2; +osmo_gummei_name; osmo_mnc_from_str; osmo_mnc_cmp; osmo_plmn_cmp; +osmo_gen_home_network_domain; +osmo_parse_home_network_domain; +osmo_gen_mme_domain; +osmo_parse_mme_domain; +osmo_gen_mme_group_domain; gsm48_chan_mode_names; gsm_chan_t_names; gsm48_pdisc_names; diff --git a/tests/gsm23003/gsm23003_test.c b/tests/gsm23003/gsm23003_test.c index 947aa18d..79965cfb 100644 --- a/tests/gsm23003/gsm23003_test.c +++ b/tests/gsm23003/gsm23003_test.c @@ -24,8 +24,10 @@ #include <stdio.h> #include <errno.h> #include <strings.h> +#include <string.h> #include <osmocom/gsm/gsm23003.h> +#include <osmocom/gsm/protocol/gsm_23_003.h> #include <osmocom/core/utils.h> #define BOOL_STR(b) ((b)? "true" : "false") @@ -170,6 +172,76 @@ static bool test_mnc_from_str() return pass; } +static bool test_gummei_name() +{ + static const struct osmo_gummei gummei = { + .plmn = { .mcc = 901, .mnc = 70 }, + .mme = { .group_id = 0xA123, .code = 0xB1 } + }; + const char *out; + bool pass = true; + + out = osmo_gummei_name(&gummei); + printf("%s\n", out); + if (strcmp(out, "901-70-a123-b1")) + pass = false; + + return pass; +} + +static bool test_domain_gen() +{ + static const struct osmo_gummei gummei = { + .plmn = { .mcc = 901, .mnc = 70 }, + .mme = { .group_id = 0xA123, .code = 0xB1 } + }; + char out[GSM23003_MME_DOMAIN_LEN]; + bool pass = true; + int rc; + + rc = osmo_gen_home_network_domain(out, &gummei.plmn); + if (rc < 0) + pass = false; + printf("%s -> %s\n", osmo_plmn_name(&gummei.plmn), out); + if (strcmp(out, "epc.mnc070.mcc901.3gppnetwork.org")) + pass = false; + + rc = osmo_gen_mme_domain(out, &gummei); + printf("%s -> %s\n", osmo_gummei_name(&gummei), out); + if (strcmp(out, "mmecb1.mmegia123.mme.epc.mnc070.mcc901.3gppnetwork.org")) + pass = false; + + return pass; +} + + +static bool test_domain_parse() +{ + static const char *mme_dom_valid = "mmec01.mmegiA001.mme.epc.mnc070.mcc901.3gppnetwork.org"; + static const char *home_dom_valid = "epc.mnc070.mcc901.3gppnetwork.org"; + struct osmo_gummei gummei; + struct osmo_plmn_id plmn; + bool pass = true; + int rc; + + rc = osmo_parse_home_network_domain(&plmn, home_dom_valid); + if (rc < 0) + pass = false; + printf("%s -> %s\n", home_dom_valid, osmo_plmn_name(&plmn)); + if (plmn.mcc != 901 || plmn.mnc != 70) + pass = false; + + rc = osmo_parse_mme_domain(&gummei, mme_dom_valid); + if (rc < 0) + pass = false; + printf("%s -> %s\n", mme_dom_valid, osmo_gummei_name(&gummei)); + if (gummei.plmn.mcc != 901 || gummei.plmn.mnc != 70 || + gummei.mme.group_id != 0xA001 || gummei.mme.code != 1) + pass = false; + + return pass; +} + int main(int argc, char **argv) { bool pass = true; @@ -177,6 +249,9 @@ int main(int argc, char **argv) pass = pass && test_valid_imsi(); pass = pass && test_valid_msisdn(); pass = pass && test_mnc_from_str(); + pass = pass && test_gummei_name(); + pass = pass && test_domain_gen(); + pass = pass && test_domain_parse(); OSMO_ASSERT(pass); diff --git a/tests/gsm23003/gsm23003_test.ok b/tests/gsm23003/gsm23003_test.ok index b435f450..c64f515c 100644 --- a/tests/gsm23003/gsm23003_test.ok +++ b/tests/gsm23003/gsm23003_test.ok @@ -59,3 +59,8 @@ 13: " 023" rc=-22 mnc=0 mnc_3_digits=0 pass 14: "023 " rc=-22 mnc=0 mnc_3_digits=0 pass 15: "023 " rc=-22 mnc=0 mnc_3_digits=0 pass +901-70-a123-b1 +901-70 -> epc.mnc070.mcc901.3gppnetwork.org +901-70-a123-b1 -> mmecb1.mmegia123.mme.epc.mnc070.mcc901.3gppnetwork.org +epc.mnc070.mcc901.3gppnetwork.org -> 901-70 +mmec01.mmegiA001.mme.epc.mnc070.mcc901.3gppnetwork.org -> 901-70-a001-01 |