From fa4da8f98098a9142025716b9867707b997222a8 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 7 Mar 2024 17:56:34 +0100 Subject: [PATCH] Introduce umts_cell_id_from_str() as inverse of umts_cell_id_name() We are about to introduce the stringified UMTS cell identifier to the VTY, and for that we need to not only print but also parse the related string. Related: SYS#6773 Change-Id: I6da947d1f2316241e44e53bb6aaec4221cfaa2c0 --- include/osmocom/hnbgw/hnbgw.h | 1 + src/osmo-hnbgw/hnbgw.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/osmocom/hnbgw/hnbgw.h b/include/osmocom/hnbgw/hnbgw.h index f4f7dc1..62b548c 100644 --- a/include/osmocom/hnbgw/hnbgw.h +++ b/include/osmocom/hnbgw/hnbgw.h @@ -79,6 +79,7 @@ struct umts_cell_id { uint32_t cid; /*!< Cell ID */ }; const char *umts_cell_id_name(const struct umts_cell_id *ucid); +int umts_cell_id_from_str(struct umts_cell_id *ucid, const char *instr); struct hnbgw_context_map; diff --git a/src/osmo-hnbgw/hnbgw.c b/src/osmo-hnbgw/hnbgw.c index 82ae279..366568d 100644 --- a/src/osmo-hnbgw/hnbgw.c +++ b/src/osmo-hnbgw/hnbgw.c @@ -202,6 +202,32 @@ const char *umts_cell_id_name(const struct umts_cell_id *ucid) ucid->sac, ucid->cid); } +/* parse a string representation of an umts_cell_id into its decoded representation */ +int umts_cell_id_from_str(struct umts_cell_id *ucid, const char *instr) +{ + int rc = sscanf(instr, "%hu-%hu-L%hu-R%hu-S%hu-C%u", &ucid->mcc, &ucid->mnc, &ucid->lac, &ucid->rac, &ucid->sac, &ucid->cid); + if (rc < 0) + return -errno; + + if (rc != 6) + return -EINVAL; + + if (ucid->mcc > 999) + return -EINVAL; + + if (ucid->mnc > 999) + return -EINVAL; + + if (ucid->lac == 0 || ucid->lac == 0xffff) + return -EINVAL; + + /* CellIdentity in the ASN.1 syntax is a bit-string of 28 bits length */ + if (ucid->cid >= (1 << 28)) + return -EINVAL; + + return 0; +} + const char *hnb_context_name(struct hnb_context *ctx) { char *result;