From 745857277cd24fadcd27d54aaa402bd82090cc86 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Sat, 23 Jul 2016 17:38:22 +0200 Subject: code dup: join [rsl_]lchan_lookup() from libbsc and osmo-bts lchan_lookup in abis_rsl.c and rsl_lchan_lookup() from osmo-bts rsl.c are the same code, except for the log context, which is only set in abis_rsl.c. Factor out the common code to rsl_lchan_lookup() in gsm_data_shared.c. Openbsc and osmo-bts each define their own DRSL log constant, so add an int *rc return code argument and keep the logging part in abis_rsl.c's thin lchan_lookup() wrapper. Incidentally, this also removes code dup for logging. To avoid duplicate symbols, the rsl_lchan_lookup() implementation needs to be removed from osmo-bts, so older osmo-bts git revisions will not build with this. Change-Id: Ie89bc5bb9110a0e539d37991dedac6f913211b48 --- openbsc/include/openbsc/gsm_data_shared.h | 2 ++ openbsc/src/libbsc/abis_rsl.c | 52 ++++++------------------------- openbsc/src/libcommon/gsm_data_shared.c | 49 +++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 42 deletions(-) diff --git a/openbsc/include/openbsc/gsm_data_shared.h b/openbsc/include/openbsc/gsm_data_shared.h index 15bc49056..7241ff0f8 100644 --- a/openbsc/include/openbsc/gsm_data_shared.h +++ b/openbsc/include/openbsc/gsm_data_shared.h @@ -844,5 +844,7 @@ static inline uint8_t gsm_ts_tsc(const struct gsm_bts_trx_ts *ts) return ts->trx->bts->bsic & 7; } +struct gsm_lchan *rsl_lchan_lookup(struct gsm_bts_trx *trx, uint8_t chan_nr, + int *rc); #endif diff --git a/openbsc/src/libbsc/abis_rsl.c b/openbsc/src/libbsc/abis_rsl.c index 0a2c114f4..baefd54f6 100644 --- a/openbsc/src/libbsc/abis_rsl.c +++ b/openbsc/src/libbsc/abis_rsl.c @@ -106,53 +106,21 @@ static inline void init_dchan_hdr(struct abis_rsl_dchan_hdr *dh, dh->ie_chan = RSL_IE_CHAN_NR; } -/* determine logical channel based on TRX and channel number IE */ -struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, uint8_t chan_nr) +/* call rsl_lchan_lookup and set the log context */ +static struct gsm_lchan *lchan_lookup(struct gsm_bts_trx *trx, uint8_t chan_nr) { - struct gsm_lchan *lchan; - uint8_t ts_nr = chan_nr & 0x07; - uint8_t cbits = chan_nr >> 3; - uint8_t lch_idx; - struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr]; - - if (cbits == 0x01) { - lch_idx = 0; /* TCH/F */ - if (ts->pchan != GSM_PCHAN_TCH_F && - ts->pchan != GSM_PCHAN_PDCH && - ts->pchan != GSM_PCHAN_TCH_F_PDCH) - LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n", - chan_nr, ts->pchan); - } else if ((cbits & 0x1e) == 0x02) { - lch_idx = cbits & 0x1; /* TCH/H */ - if (ts->pchan != GSM_PCHAN_TCH_H) - LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n", - chan_nr, ts->pchan); - } else if ((cbits & 0x1c) == 0x04) { - lch_idx = cbits & 0x3; /* SDCCH/4 */ - if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4 && - ts->pchan != GSM_PCHAN_CCCH_SDCCH4_CBCH) - LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n", - chan_nr, ts->pchan); - } else if ((cbits & 0x18) == 0x08) { - lch_idx = cbits & 0x7; /* SDCCH/8 */ - if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C && - ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C_CBCH) - LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n", - chan_nr, ts->pchan); - } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) { - lch_idx = 0; - if (ts->pchan != GSM_PCHAN_CCCH && - ts->pchan != GSM_PCHAN_CCCH_SDCCH4 && - ts->pchan != GSM_PCHAN_CCCH_SDCCH4_CBCH) - LOGP(DRSL, LOGL_ERROR, "chan_nr=0x%02x but pchan=%u\n", - chan_nr, ts->pchan); - /* FIXME: we should not return first sdcch4 !!! */ - } else { + int rc; + struct gsm_lchan *lchan = rsl_lchan_lookup(trx, chan_nr, &rc); + + if (!lchan) { LOGP(DRSL, LOGL_ERROR, "unknown chan_nr=0x%02x\n", chan_nr); return NULL; } - lchan = &ts->lchan[lch_idx]; + if (rc < 0) + LOGP(DRSL, LOGL_ERROR, "%s mismatching chan_nr=0x%02x\n", + gsm_ts_and_pchan_name(lchan->ts), chan_nr); + log_set_context(BSC_CTX_LCHAN, lchan); if (lchan->conn) log_set_context(BSC_CTX_SUBSCR, lchan->conn->subscr); diff --git a/openbsc/src/libcommon/gsm_data_shared.c b/openbsc/src/libcommon/gsm_data_shared.c index 07bb64d06..2fc56901a 100644 --- a/openbsc/src/libcommon/gsm_data_shared.c +++ b/openbsc/src/libcommon/gsm_data_shared.c @@ -612,3 +612,52 @@ struct gsm_lchan *gsm_bts_get_cbch(struct gsm_bts *bts) return lchan; } + +/* determine logical channel based on TRX and channel number IE */ +struct gsm_lchan *rsl_lchan_lookup(struct gsm_bts_trx *trx, uint8_t chan_nr, + int *rc) +{ + uint8_t ts_nr = chan_nr & 0x07; + uint8_t cbits = chan_nr >> 3; + uint8_t lch_idx; + struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr]; + bool ok = true; + + if (rc) + *rc = -EINVAL; + + if (cbits == 0x01) { + lch_idx = 0; /* TCH/F */ + if (ts->pchan != GSM_PCHAN_TCH_F && + ts->pchan != GSM_PCHAN_PDCH && + ts->pchan != GSM_PCHAN_TCH_F_PDCH) + ok = false; + } else if ((cbits & 0x1e) == 0x02) { + lch_idx = cbits & 0x1; /* TCH/H */ + if (ts->pchan != GSM_PCHAN_TCH_H) + ok = false; + } else if ((cbits & 0x1c) == 0x04) { + lch_idx = cbits & 0x3; /* SDCCH/4 */ + if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4 && + ts->pchan != GSM_PCHAN_CCCH_SDCCH4_CBCH) + ok = false; + } else if ((cbits & 0x18) == 0x08) { + lch_idx = cbits & 0x7; /* SDCCH/8 */ + if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C && + ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C_CBCH) + ok = false; + } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) { + lch_idx = 0; + if (ts->pchan != GSM_PCHAN_CCCH && + ts->pchan != GSM_PCHAN_CCCH_SDCCH4 && + ts->pchan != GSM_PCHAN_CCCH_SDCCH4_CBCH) + ok = false; + /* FIXME: we should not return first sdcch4 !!! */ + } else + return NULL; + + if (rc && ok) + *rc = 0; + + return &ts->lchan[lch_idx]; +} -- cgit v1.2.3