laforge has submitted this change. ( https://gerrit.osmocom.org/c/osmo-hnbgw/+/36204?usp=email )
Change subject: Introduce umts_cell_id_from_str() as inverse of umts_cell_id_name() ......................................................................
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 --- M include/osmocom/hnbgw/hnbgw.h M src/osmo-hnbgw/hnbgw.c 2 files changed, 41 insertions(+), 0 deletions(-)
Approvals: osmith: Looks good to me, but someone else must approve pespin: Looks good to me, but someone else must approve Jenkins Builder: Verified laforge: Looks good to me, approved
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 @@ 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 @@ 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;