aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2018-06-11 02:40:30 +0200
committerNeels Hofmeyr <neels@hofmeyr.de>2018-06-16 15:56:14 +0200
commitc10a77af68c231a026b4f0aa2faf86c584e08eb6 (patch)
tree75e1b15068d2f25054c5a84ee16cdae957508059 /src
parent73e028416e82f5f6b21d7f323fd907110ae02d74 (diff)
code bomb: refactor just about everything for inter-bsc HOneels/inter_bsc_ho_saved
This is way too large, yet the typical tangle of modification onto modifications needs to be squashed to make sensible bits of them later for code review... Change-Id: I82e3f918295daa83274a4cf803f046979f284366
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am4
-rw-r--r--src/ipaccess/Makefile.am4
-rw-r--r--src/osmo-bsc/Makefile.am10
-rw-r--r--src/osmo-bsc/abis_om2000.c25
-rw-r--r--src/osmo-bsc/abis_rsl.c1739
-rw-r--r--src/osmo-bsc/assignment_fsm.c619
-rw-r--r--src/osmo-bsc/bsc_api.c676
-rw-r--r--src/osmo-bsc/bsc_dyn_ts.c60
-rw-r--r--src/osmo-bsc/bsc_init.c1
-rw-r--r--src/osmo-bsc/bsc_subscr_conn_fsm.c1340
-rw-r--r--src/osmo-bsc/bsc_subscriber.c14
-rw-r--r--src/osmo-bsc/bsc_vty.c392
-rw-r--r--src/osmo-bsc/bts_ericsson_rbs2000.c3
-rw-r--r--src/osmo-bsc/bts_ipaccess_nanobts.c11
-rw-r--r--src/osmo-bsc/bts_ipaccess_nanobts_omlattr.c2
-rw-r--r--src/osmo-bsc/bts_nokia_site.c35
-rw-r--r--src/osmo-bsc/bts_siemens_bs11.c7
-rw-r--r--src/osmo-bsc/chan_alloc.c579
-rw-r--r--src/osmo-bsc/gsm_04_08_utils.c257
-rw-r--r--src/osmo-bsc/gsm_data.c650
-rw-r--r--src/osmo-bsc/gsm_timers.c206
-rw-r--r--src/osmo-bsc/gsm_timers_vty.c117
-rw-r--r--src/osmo-bsc/handover_decision.c75
-rw-r--r--src/osmo-bsc/handover_decision_2.c189
-rw-r--r--src/osmo-bsc/handover_fsm.c1070
-rw-r--r--src/osmo-bsc/handover_logic.c504
-rw-r--r--src/osmo-bsc/lchan_fsm.c1466
-rw-r--r--src/osmo-bsc/lchan_select.c260
-rw-r--r--src/osmo-bsc/meas_feed.c2
-rw-r--r--src/osmo-bsc/mgw_endpoint_fsm.c728
-rw-r--r--src/osmo-bsc/neighbor_ident_vty.c20
-rw-r--r--src/osmo-bsc/net_init.c48
-rw-r--r--src/osmo-bsc/osmo_bsc_api.c230
-rw-r--r--src/osmo-bsc/osmo_bsc_audio.c98
-rw-r--r--src/osmo-bsc/osmo_bsc_bssap.c345
-rw-r--r--src/osmo-bsc/osmo_bsc_filter.c165
-rw-r--r--src/osmo-bsc/osmo_bsc_grace.c3
-rw-r--r--src/osmo-bsc/osmo_bsc_lcls.c80
-rw-r--r--src/osmo-bsc/osmo_bsc_main.c26
-rw-r--r--src/osmo-bsc/osmo_bsc_sigtran.c58
-rw-r--r--src/osmo-bsc/paging.c5
-rw-r--r--src/osmo-bsc/pcu_sock.c32
-rw-r--r--src/osmo-bsc/system_information.c12
-rw-r--r--src/osmo-bsc/timeslot_fsm.c859
-rw-r--r--src/stubs/lchan_fsm_stubs.c3
-rw-r--r--src/stubs/system_information_stubs.c7
-rw-r--r--src/stubs/ts_fsm_stubs.c3
-rw-r--r--src/utils/Makefile.am4
48 files changed, 8202 insertions, 4841 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 6c63eead8..e75863abd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,6 +19,10 @@ AM_LDFLAGS = \
$(COVERAGE_LDFLAGS) \
$(NULL)
+EXTRA_DIST = \
+ stubs/ts_fsm_stubs.c \
+ $(NULL)
+
SUBDIRS = \
libfilter \
osmo-bsc \
diff --git a/src/ipaccess/Makefile.am b/src/ipaccess/Makefile.am
index 2c6282d11..eb39c993c 100644
--- a/src/ipaccess/Makefile.am
+++ b/src/ipaccess/Makefile.am
@@ -46,6 +46,7 @@ ipaccess_config_SOURCES = \
ipaccess-firmware.c \
network_listen.c \
stubs.c \
+ $(top_srcdir)/src/stubs/ts_fsm_stubs.c \
$(NULL)
# FIXME: resolve the bogus dependencies patched around here:
@@ -54,6 +55,7 @@ ipaccess_config_LDADD = \
$(top_builddir)/src/osmo-bsc/bts_ipaccess_nanobts.o \
$(top_builddir)/src/osmo-bsc/bts_ipaccess_nanobts_omlattr.o \
$(top_builddir)/src/osmo-bsc/gsm_data.o \
+ $(top_builddir)/src/osmo-bsc/gsm_timers.o \
$(top_builddir)/src/osmo-bsc/net_init.o \
$(OSMO_LIBS) \
$(NULL)
@@ -62,6 +64,8 @@ ipaccess_proxy_SOURCES = \
ipaccess-proxy.c \
stubs.c \
$(top_srcdir)/src/osmo-bsc/gsm_data.c \
+ $(top_srcdir)/src/osmo-bsc/gsm_timers.c \
+ $(top_srcdir)/src/stubs/ts_fsm_stubs.c \
$(NULL)
ipaccess_proxy_LDADD = \
diff --git a/src/osmo-bsc/Makefile.am b/src/osmo-bsc/Makefile.am
index afae0b66b..0fe83ba65 100644
--- a/src/osmo-bsc/Makefile.am
+++ b/src/osmo-bsc/Makefile.am
@@ -34,10 +34,10 @@ osmo_bsc_SOURCES = \
abis_rsl.c \
acc_ramp.c \
arfcn_range_encode.c \
+ assignment_fsm.c \
bsc_api.c \
bsc_ctrl_commands.c \
bsc_ctrl_lookup.c \
- bsc_dyn_ts.c \
bsc_init.c \
bsc_rf_ctrl.c \
bsc_rll.c \
@@ -57,18 +57,23 @@ osmo_bsc_SOURCES = \
gsm_04_08_utils.c \
gsm_04_80_utils.c \
gsm_data.c \
+ gsm_timers.c \
+ gsm_timers_vty.c \
handover_cfg.c \
handover_decision.c \
handover_decision_2.c \
+ handover_fsm.c \
handover_logic.c \
handover_vty.c \
+ lchan_fsm.c \
+ lchan_select.c \
meas_feed.c \
meas_rep.c \
+ mgw_endpoint_fsm.c \
neighbor_ident.c \
neighbor_ident_vty.c \
net_init.c \
osmo_bsc_api.c \
- osmo_bsc_audio.c \
osmo_bsc_bssap.c \
osmo_bsc_ctrl.c \
osmo_bsc_filter.c \
@@ -83,6 +88,7 @@ osmo_bsc_SOURCES = \
penalty_timers.c \
rest_octets.c \
system_information.c \
+ timeslot_fsm.c \
$(NULL)
osmo_bsc_LDADD = \
diff --git a/src/osmo-bsc/abis_om2000.c b/src/osmo-bsc/abis_om2000.c
index d533ea198..0c50edd2b 100644
--- a/src/osmo-bsc/abis_om2000.c
+++ b/src/osmo-bsc/abis_om2000.c
@@ -42,6 +42,7 @@
#include <osmocom/bsc/abis_rsl.h>
#include <osmocom/bsc/abis_om2000.h>
#include <osmocom/bsc/signal.h>
+#include <osmocom/bsc/timeslot_fsm.h>
#include <osmocom/abis/e1_input.h>
/* FIXME: move to libosmocore */
@@ -1295,8 +1296,7 @@ static uint8_t pchan2comb(enum gsm_phys_chan_config pchan)
static uint8_t ts2comb(struct gsm_bts_trx_ts *ts)
{
- switch (ts->pchan) {
- case GSM_PCHAN_TCH_F_PDCH:
+ if (ts->pchan_on_init == GSM_PCHAN_TCH_F_PDCH) {
LOGP(DNM, LOGL_ERROR, "%s pchan %s not intended for use"
" with OM2000, use %s instead\n",
gsm_ts_and_pchan_name(ts),
@@ -1306,11 +1306,8 @@ static uint8_t ts2comb(struct gsm_bts_trx_ts *ts)
* when we try to send the ip.access specific RSL PDCH Act
* message for it. Rather fail completely right now: */
return 0;
- case GSM_PCHAN_TCH_F_TCH_H_PDCH:
- return pchan2comb(GSM_PCHAN_TCH_F);
- default:
- return pchan2comb(ts->pchan);
}
+ return pchan2comb(ts->pchan_is);
}
static int put_freq_list(uint8_t *buf, uint16_t arfcn)
@@ -1373,7 +1370,7 @@ int abis_om2k_tx_ts_conf_req(struct gsm_bts_trx_ts *ts)
msgb_tv_put(msg, OM2K_DEI_EXT_RANGE, 0); /* Off */
/* Optional: Interference Rejection Combining */
msgb_tv_put(msg, OM2K_DEI_INTERF_REJ_COMB, 0x00);
- switch (ts->pchan) {
+ switch (ts->pchan_is) {
case GSM_PCHAN_CCCH:
msgb_tv_put(msg, OM2K_DEI_BA_PA_MFRMS, 0x06);
msgb_tv_put(msg, OM2K_DEI_BS_AG_BKS_RES, 0x01);
@@ -1382,7 +1379,8 @@ int abis_om2k_tx_ts_conf_req(struct gsm_bts_trx_ts *ts)
msgb_tv_put(msg, OM2K_DEI_CCCH_OPTIONS, 0x01);
break;
case GSM_PCHAN_CCCH_SDCCH4:
- msgb_tv_put(msg, OM2K_DEI_T3105, ts->trx->bts->network->T3105 / 10);
+ msgb_tv_put(msg, OM2K_DEI_T3105,
+ T_def_get(ts->trx->bts->network->T_defs, 3105, T_MS, -1) / 10);
msgb_tv_put(msg, OM2K_DEI_NY1, 35);
msgb_tv_put(msg, OM2K_DEI_BA_PA_MFRMS, 0x06);
msgb_tv_put(msg, OM2K_DEI_CBCH_INDICATOR, 0);
@@ -1396,7 +1394,8 @@ int abis_om2k_tx_ts_conf_req(struct gsm_bts_trx_ts *ts)
sizeof(icm_bound_params), icm_bound_params);
break;
case GSM_PCHAN_SDCCH8_SACCH8C:
- msgb_tv_put(msg, OM2K_DEI_T3105, ts->trx->bts->network->T3105 / 10);
+ msgb_tv_put(msg, OM2K_DEI_T3105,
+ T_def_get(ts->trx->bts->network->T_defs, 3105, T_MS, -1) / 10);
msgb_tv_put(msg, OM2K_DEI_NY1, 35);
msgb_tv_put(msg, OM2K_DEI_CBCH_INDICATOR, 0);
msgb_tv_put(msg, OM2K_DEI_TSC, gsm_ts_tsc(ts));
@@ -1406,7 +1405,8 @@ int abis_om2k_tx_ts_conf_req(struct gsm_bts_trx_ts *ts)
sizeof(icm_bound_params), icm_bound_params);
break;
default:
- msgb_tv_put(msg, OM2K_DEI_T3105, ts->trx->bts->network->T3105 / 10);
+ msgb_tv_put(msg, OM2K_DEI_T3105,
+ T_def_get(ts->trx->bts->network->T_defs, 3105, T_MS, -1) / 10);
msgb_tv_put(msg, OM2K_DEI_NY1, 35);
msgb_tv_put(msg, OM2K_DEI_TSC, gsm_ts_tsc(ts));
/* Disable RF RESOURCE INDICATION on idle channels */
@@ -1414,7 +1414,7 @@ int abis_om2k_tx_ts_conf_req(struct gsm_bts_trx_ts *ts)
msgb_tv_fixed_put(msg, OM2K_DEI_ICM_BOUND_PARAMS,
sizeof(icm_bound_params), icm_bound_params);
msgb_tv_put(msg, OM2K_DEI_TTA, 10); /* Timer for Time Alignment */
- if (ts->pchan == GSM_PCHAN_TCH_H)
+ if (ts->pchan_is == GSM_PCHAN_TCH_H)
msgb_tv_put(msg, OM2K_DEI_ICM_CHAN_RATE, 1); /* TCH/H */
else
msgb_tv_put(msg, OM2K_DEI_ICM_CHAN_RATE, 0); /* TCH/F */
@@ -2740,7 +2740,8 @@ void abis_om2k_trx_init(struct gsm_bts_trx *trx)
struct gsm_bts_trx_ts *ts = &trx->ts[i];
om2k_mo_init(&ts->rbs2000.om2k_mo, OM2K_MO_CLS_TS,
bts->nr, trx->nr, i);
- gsm_ts_check_init(ts);
+ OSMO_ASSERT(ts->fi);
+ osmo_fsm_inst_dispatch(ts->fi, TS_EV_OML_READY, NULL);
}
}
diff --git a/src/osmo-bsc/abis_rsl.c b/src/osmo-bsc/abis_rsl.c
index ab3658d53..5f4a03690 100644
--- a/src/osmo-bsc/abis_rsl.c
+++ b/src/osmo-bsc/abis_rsl.c
@@ -28,10 +28,13 @@
#include <arpa/inet.h>
#include <osmocom/bsc/gsm_data.h>
+#include <osmocom/bsc/timeslot_fsm.h>
+#include <osmocom/bsc/lchan_fsm.h>
+#include <osmocom/bsc/lchan_select.h>
#include <osmocom/bsc/gsm_04_08_utils.h>
#include <osmocom/gsm/gsm_utils.h>
#include <osmocom/bsc/abis_rsl.h>
-#include <osmocom/bsc/chan_alloc.h>
+#include <osmocom/bsc/lchan_fsm.h>
#include <osmocom/bsc/bsc_rll.h>
#include <osmocom/bsc/debug.h>
#include <osmocom/gsm/tlv.h>
@@ -46,6 +49,8 @@
#include <osmocom/bsc/pcu_if.h>
#include <osmocom/bsc/bsc_api.h>
#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
+#include <osmocom/bsc/assignment_fsm.h>
+#include <osmocom/bsc/handover_fsm.h>
#define RSL_ALLOC_SIZE 1024
#define RSL_ALLOC_HEADROOM 128
@@ -60,12 +65,6 @@ enum sacch_deact {
SACCH_DEACTIVATE,
};
-static int rsl_send_imm_assignment(struct gsm_lchan *lchan);
-static void error_timeout_cb(void *data);
-static int dyn_ts_switchover_continue(struct gsm_bts_trx_ts *ts);
-static int dyn_ts_switchover_failed(struct gsm_bts_trx_ts *ts, int rc);
-static void dyn_ts_switchover_complete(struct gsm_lchan *lchan);
-
static void send_lchan_signal(int sig_no, struct gsm_lchan *lchan,
struct gsm_meas_rep *resp)
{
@@ -75,19 +74,6 @@ static void send_lchan_signal(int sig_no, struct gsm_lchan *lchan,
osmo_signal_dispatch(SS_LCHAN, sig_no, &sig);
}
-static void do_lchan_free(struct gsm_lchan *lchan)
-{
- /* We start the error timer to make the channel available again */
- if (lchan->state == LCHAN_S_REL_ERR) {
- osmo_timer_setup(&lchan->error_timer, error_timeout_cb, lchan);
- osmo_timer_schedule(&lchan->error_timer,
- lchan->ts->trx->bts->network->T3111 + 2, 0);
- } else {
- rsl_lchan_set_state(lchan, LCHAN_S_NONE);
- }
- lchan_free(lchan);
-}
-
static void count_codecs(struct gsm_bts *bts, struct gsm_lchan *lchan)
{
OSMO_ASSERT(bts);
@@ -200,23 +186,6 @@ static void print_rsl_cause(int lvl, const uint8_t *cause_v, uint8_t cause_len)
LOGPC(DRSL, lvl, "%02x ", cause_v[i]);
}
-static void lchan_act_tmr_cb(void *data)
-{
- struct gsm_lchan *lchan = data;
-
- rsl_lchan_mark_broken(lchan, "activation timeout");
- lchan_free(lchan);
-}
-
-static void lchan_deact_tmr_cb(void *data)
-{
- struct gsm_lchan *lchan = data;
-
- rsl_lchan_mark_broken(lchan, "de-activation timeout");
- lchan_free(lchan);
-}
-
-
/* Send a BCCH_INFO message as per Chapter 8.5.1 */
int rsl_bcch_info(const struct gsm_bts_trx *trx, enum osmo_sysinfo_type si_type, const uint8_t *data, int len)
{
@@ -471,59 +440,8 @@ static void mr_config_for_bts(struct gsm_lchan *lchan, struct msgb *msg)
lchan->mr_bts_lv + 1);
}
-static enum gsm_phys_chan_config pchan_for_lchant(enum gsm_chan_t type)
-{
- switch (type) {
- case GSM_LCHAN_TCH_F:
- return GSM_PCHAN_TCH_F;
- case GSM_LCHAN_TCH_H:
- return GSM_PCHAN_TCH_H;
- case GSM_LCHAN_NONE:
- case GSM_LCHAN_PDTCH:
- /* TODO: so far lchan->type is NONE in PDCH mode. PDTCH is only
- * used in osmo-bts. Maybe set PDTCH and drop the NONE case
- * here. */
- return GSM_PCHAN_PDCH;
- default:
- return GSM_PCHAN_UNKNOWN;
- }
-}
-
-/*! Tx simplified channel activation message for non-standard PDCH type. */
-static int rsl_chan_activate_lchan_as_pdch(struct gsm_lchan *lchan)
-{
- struct msgb *msg;
- struct abis_rsl_dchan_hdr *dh;
-
- /* This might be called after release of the second lchan of a TCH/H,
- * but PDCH activation must always happen on the first lchan. Make sure
- * the calling code passes the correct lchan. */
- OSMO_ASSERT(lchan == lchan->ts->lchan);
-
- rsl_lchan_set_state(lchan, LCHAN_S_ACT_REQ);
-
- msg = rsl_msgb_alloc();
- dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
- init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
- dh->chan_nr = gsm_lchan_as_pchan2chan_nr(lchan, GSM_PCHAN_PDCH);
-
- msgb_tv_put(msg, RSL_IE_ACT_TYPE, RSL_ACT_OSMO_PDCH);
-
- if (lchan->ts->trx->bts->type == GSM_BTS_TYPE_RBS2000 &&
- lchan->ts->trx->bts->rbs2000.use_superchannel) {
- const uint8_t eric_pgsl_tmr[] = { 30, 1 };
- msgb_tv_fixed_put(msg, RSL_IE_ERIC_PGSL_TIMERS,
- sizeof(eric_pgsl_tmr), eric_pgsl_tmr);
- }
-
- msg->dst = lchan->ts->trx->rsl_link;
-
- return abis_rsl_sendmsg(msg);
-}
-
/* Chapter 8.4.1 */
-int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type,
- uint8_t ho_ref)
+int rsl_tx_chan_activ(struct gsm_lchan *lchan, uint8_t act_type, uint8_t ho_ref)
{
struct abis_rsl_dchan_hdr *dh;
struct msgb *msg;
@@ -534,75 +452,12 @@ int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type,
struct rsl_ie_chan_mode cm;
struct gsm48_chan_desc cd;
- /* If a TCH_F/PDCH TS is in PDCH mode, deactivate PDCH first. */
- if (lchan->ts->pchan == GSM_PCHAN_TCH_F_PDCH
- && (lchan->ts->flags & TS_F_PDCH_ACTIVE)) {
- /* store activation type and handover reference */
- lchan->dyn.act_type = act_type;
- lchan->dyn.ho_ref = ho_ref;
- return rsl_ipacc_pdch_activate(lchan->ts, 0);
- }
-
- /*
- * If necessary, release PDCH on dynamic TS. Note that sending a
- * release here is only necessary when in PDCH mode; for TCH types, an
- * RSL RF Chan Release is initiated by the BTS when a voice call ends,
- * so when we reach this, it will already be released. If a dyn TS is
- * in PDCH mode, it is still active and we need to initiate a release
- * from the BSC side here.
- *
- * If pchan_is != pchan_want, the PDCH has already been taken down and
- * the switchover now needs to enable the TCH lchan.
- *
- * To switch a dyn TS between TCH/H and TCH/F, it is sufficient to send
- * a chan activ with the new lchan type, because it will already be
- * released.
- */
- if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
- && lchan->ts->dyn.pchan_is == lchan->ts->dyn.pchan_want) {
- enum gsm_phys_chan_config pchan_want;
- pchan_want = pchan_for_lchant(lchan->type);
- if (lchan->ts->dyn.pchan_is != pchan_want) {
- /*
- * Make sure to record on lchan[0] so that we'll find
- * it after the PDCH release.
- */
- struct gsm_lchan *lchan0 = lchan->ts->lchan;
- lchan0->dyn.act_type = act_type,
- lchan0->dyn.ho_ref = ho_ref;
- lchan0->dyn.rqd_ref = lchan->rqd_ref;
- lchan0->dyn.rqd_ta = lchan->rqd_ta;
- lchan->rqd_ref = NULL;
- lchan->rqd_ta = 0;
- DEBUGP(DRSL, "%s saved rqd_ref=%p ta=%u\n",
- gsm_lchan_name(lchan0), lchan0->rqd_ref,
- lchan0->rqd_ta);
- return dyn_ts_switchover_start(lchan->ts, pchan_want);
- }
- }
-
DEBUGP(DRSL, "%s Tx RSL Channel Activate with act_type=%s\n",
gsm_ts_and_pchan_name(lchan->ts),
rsl_act_type_name(act_type));
- if (act_type == RSL_ACT_OSMO_PDCH) {
- if (lchan->ts->pchan != GSM_PCHAN_TCH_F_TCH_H_PDCH) {
- LOGP(DRSL, LOGL_ERROR,
- "%s PDCH channel activation only allowed on %s\n",
- gsm_ts_and_pchan_name(lchan->ts),
- gsm_pchan_name(GSM_PCHAN_TCH_F_TCH_H_PDCH));
- return -EINVAL;
- }
- return rsl_chan_activate_lchan_as_pdch(lchan);
- }
-
- if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
- && lchan->ts->dyn.pchan_want == GSM_PCHAN_PDCH) {
- LOGP(DRSL, LOGL_ERROR,
- "%s Expected PDCH activation kind\n",
- gsm_ts_and_pchan_name(lchan->ts));
- return -EINVAL;
- }
+ /* PDCH activation is a job for rsl_tx_dyn_ts_pdch_act_deact(); */
+ OSMO_ASSERT(act_type != RSL_ACT_OSMO_PDCH);
rc = channel_mode_from_lchan(&cm, lchan);
if (rc < 0) {
@@ -612,8 +467,6 @@ int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type,
return rc;
}
- rsl_lchan_set_state(lchan, LCHAN_S_ACT_REQ);
-
ta = lchan->rqd_ta;
/* BS11 requires TA shifted by 2 bits */
@@ -627,11 +480,7 @@ int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type,
dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV);
- if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH)
- dh->chan_nr = gsm_lchan_as_pchan2chan_nr(
- lchan, lchan->ts->dyn.pchan_want);
- else
- dh->chan_nr = gsm_lchan2chan_nr(lchan);
+ dh->chan_nr = gsm_lchan2chan_nr(lchan);
msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type);
msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm),
@@ -683,10 +532,7 @@ int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type,
rate_ctr_inc(&lchan->ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_ACT_TOTAL]);
- rc = abis_rsl_sendmsg(msg);
- if (!rc)
- rsl_lchan_set_state(lchan, LCHAN_S_ACT_REQ);
- return rc;
+ return abis_rsl_sendmsg(msg);
}
/* Chapter 8.4.9: Modify channel mode on BTS side */
@@ -775,80 +621,11 @@ int rsl_deact_sacch(struct gsm_lchan *lchan)
return abis_rsl_sendmsg(msg);
}
-static bool dyn_ts_should_switch_to_pdch(struct gsm_bts_trx_ts *ts)
-{
- int ss;
-
- if (ts->pchan != GSM_PCHAN_TCH_F_TCH_H_PDCH)
- return false;
-
- if (ts->trx->bts->gprs.mode == BTS_GPRS_NONE)
- return false;
-
- /* Already in PDCH mode? */
- if (ts->dyn.pchan_is == GSM_PCHAN_PDCH)
- return false;
-
- /* See if all lchans are released. */
- for (ss = 0; ss < ts_subslots(ts); ss++) {
- struct gsm_lchan *lc = &ts->lchan[ss];
- if (lc->state != LCHAN_S_NONE) {
- DEBUGP(DRSL, "%s lchan %u still in use"
- " (type=%s,state=%s)\n",
- gsm_ts_and_pchan_name(ts), lc->nr,
- gsm_lchant_name(lc->type),
- gsm_lchans_name(lc->state));
- /* An lchan is still used. */
- return false;
- }
- }
-
- /* All channels are released, go to PDCH mode. */
- DEBUGP(DRSL, "%s back to PDCH\n",
- gsm_ts_and_pchan_name(ts));
- return true;
-}
-
-static void error_timeout_cb(void *data)
-{
- struct gsm_lchan *lchan = data;
- if (lchan->state != LCHAN_S_REL_ERR) {
- LOGP(DRSL, LOGL_ERROR, "%s error timeout but not in error state: %d\n",
- gsm_lchan_name(lchan), lchan->state);
- return;
- }
-
- /* go back to the none state */
- LOGP(DRSL, LOGL_INFO, "%s is back in operation.\n", gsm_lchan_name(lchan));
- rsl_lchan_set_state(lchan, LCHAN_S_NONE);
-
- /* Put PDCH channel back into PDCH mode, if GPRS is enabled */
- if (lchan->ts->pchan == GSM_PCHAN_TCH_F_PDCH
- && lchan->ts->trx->bts->gprs.mode != BTS_GPRS_NONE)
- rsl_ipacc_pdch_activate(lchan->ts, 1);
-
- if (dyn_ts_should_switch_to_pdch(lchan->ts))
- dyn_ts_switchover_start(lchan->ts, GSM_PCHAN_PDCH);
-}
-
-static int rsl_rx_rf_chan_rel_ack(struct gsm_lchan *lchan);
-
/* Chapter 8.4.14 / 4.7: Tell BTS to release the radio channel */
-static int rsl_rf_chan_release(struct gsm_lchan *lchan, int error,
- enum sacch_deact deact_sacch)
+int rsl_tx_rf_chan_release(struct gsm_lchan *lchan)
{
struct abis_rsl_dchan_hdr *dh;
struct msgb *msg;
- int rc;
-
- /* Stop timers that should lead to a channel release */
- osmo_timer_del(&lchan->T3109);
-
- if (lchan->state == LCHAN_S_REL_ERR) {
- LOGP(DRSL, LOGL_NOTICE, "%s is in error state, not sending release.\n",
- gsm_lchan_name(lchan));
- return -1;
- }
msg = rsl_msgb_alloc();
dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
@@ -858,156 +635,23 @@ static int rsl_rf_chan_release(struct gsm_lchan *lchan, int error,
msg->lchan = lchan;
msg->dst = lchan->ts->trx->rsl_link;
- if (error)
- DEBUGP(DRSL, "%s RF Channel Release due to error: %d\n",
- gsm_lchan_name(lchan), error);
- else
- DEBUGP(DRSL, "%s RF Channel Release\n", gsm_lchan_name(lchan));
-
- if (error) {
- /*
- * FIXME: GSM 04.08 gives us two options for the abnormal
- * chanel release. This can be either like in the non-existent
- * sub-lcuase 3.5.1 or for the main signalling link deactivate
- * the SACCH, start timer T3109 and consider the channel as
- * released.
- *
- * This code is doing the later for all raido links and not
- * only the main link. Right now all SAPIs are released on the
- * local end, the SACCH will be de-activated and right now the
- * T3111 will be started. First T3109 should be started and then
- * the T3111.
- *
- * TODO: Move this out of the function.
- */
-
- /*
- * sacch de-activate and "local end release"
- */
- if (deact_sacch == SACCH_DEACTIVATE)
- rsl_deact_sacch(lchan);
- rsl_release_sapis_from(lchan, 0, RSL_REL_LOCAL_END);
-
- /*
- * TODO: start T3109 now.
- */
- rsl_lchan_set_state(lchan, LCHAN_S_REL_ERR);
- }
-
- /* Start another timer or assume the BTS sends a ACK/NACK? */
- osmo_timer_setup(&lchan->act_timer, lchan_deact_tmr_cb, lchan);
- osmo_timer_schedule(&lchan->act_timer, 4, 0);
-
- rc = abis_rsl_sendmsg(msg);
-
- /* BTS will respond by RF CHAN REL ACK */
- return rc;
-}
-
-/*
- * Special handling for channel releases in the error case.
- */
-static int rsl_rf_chan_release_err(struct gsm_lchan *lchan)
-{
- enum sacch_deact sacch_deact;
- if (lchan->state != LCHAN_S_ACTIVE)
- return 0;
- switch (ts_pchan(lchan->ts)) {
- case GSM_PCHAN_TCH_F:
- case GSM_PCHAN_TCH_H:
- case GSM_PCHAN_CCCH_SDCCH4:
- case GSM_PCHAN_CCCH_SDCCH4_CBCH:
- case GSM_PCHAN_SDCCH8_SACCH8C:
- case GSM_PCHAN_SDCCH8_SACCH8C_CBCH:
- sacch_deact = SACCH_DEACTIVATE;
- break;
- default:
- sacch_deact = SACCH_NONE;
- break;
- }
- return rsl_rf_chan_release(lchan, 1, sacch_deact);
+ return abis_rsl_sendmsg(msg);
}
static int rsl_rx_rf_chan_rel_ack(struct gsm_lchan *lchan)
{
struct gsm_bts_trx_ts *ts = lchan->ts;
- DEBUGP(DRSL, "%s RF CHANNEL RELEASE ACK\n", gsm_lchan_name(lchan));
-
- /* Stop all pending timers */
- osmo_timer_del(&lchan->act_timer);
- osmo_timer_del(&lchan->T3111);
-
- /*
- * The BTS didn't respond within the timeout to our channel
- * release request and we have marked the channel as broken.
- * Now we do receive an ACK and let's be conservative. If it
- * is a sysmoBTS we know that only one RF Channel Release ACK
- * will be sent. So let's "repair" the channel.
- */
- if (lchan->state == LCHAN_S_BROKEN) {
- int do_free = is_sysmobts_v2(ts->trx->bts);
- LOGP(DRSL, LOGL_NOTICE,
- "%s CHAN REL ACK for broken channel. %s.\n",
- gsm_lchan_name(lchan),
- do_free ? "Releasing it" : "Keeping it broken");
- if (do_free)
- do_lchan_free(lchan);
- if (dyn_ts_should_switch_to_pdch(lchan->ts))
- dyn_ts_switchover_start(lchan->ts, GSM_PCHAN_PDCH);
+ /* Catch Osmocom style dyn TS PDCH release ACK: */
+ if (ts->pchan_on_init == GSM_PCHAN_TCH_F_TCH_H_PDCH
+ && lchan->nr == 0
+ && ts->fi->state == TS_ST_WAIT_PDCH_DEACT) {
+ osmo_fsm_inst_dispatch(ts->fi, TS_EV_PDCH_DEACT_ACK, NULL);
return 0;
}
- if (lchan->state != LCHAN_S_REL_REQ && lchan->state != LCHAN_S_REL_ERR)
- LOGP(DRSL, LOGL_NOTICE, "%s CHAN REL ACK but state %s\n",
- gsm_lchan_name(lchan),
- gsm_lchans_name(lchan->state));
-
- do_lchan_free(lchan);
-
- /*
- * Check Osmocom RSL CHAN ACT style dynamic TCH/F_TCH/H_PDCH TS for pending
- * transitions in these cases:
- *
- * a) after PDCH was released due to switchover request, activate TCH.
- * BSC initiated this switchover, so dyn.pchan_is != pchan_want and
- * lchan->type has been set to the desired GSM_LCHAN_*.
- *
- * b) Voice call ended and a TCH is released. If the TS is now unused,
- * switch to PDCH. Here still dyn.pchan_is == dyn.pchan_want because
- * we're only just notified and may decide to switch to PDCH now.
- */
- if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) {
- DEBUGP(DRSL, "%s Rx RSL Channel Release ack for lchan %u\n",
- gsm_ts_and_pchan_name(ts), lchan->nr);
-
- /* (a) */
- if (ts->dyn.pchan_is != ts->dyn.pchan_want)
- return dyn_ts_switchover_continue(ts);
-
- /* (b) */
- if (dyn_ts_should_switch_to_pdch(ts))
- return dyn_ts_switchover_start(ts, GSM_PCHAN_PDCH);
- }
-
- /*
- * Put a dynamic TCH/F_PDCH channel back to PDCH mode iff it was
- * released successfully. If in error, the PDCH ACT will follow after
- * T3111 in error_timeout_cb().
- *
- * Any state other than LCHAN_S_REL_ERR became LCHAN_S_NONE after above
- * do_lchan_free(). Assert this, because that's what ensures a PDCH ACT
- * on a TCH/F_PDCH TS in all cases.
- *
- * If GPRS is disabled, always skip the PDCH ACT.
- */
- OSMO_ASSERT(lchan->state == LCHAN_S_NONE
- || lchan->state == LCHAN_S_REL_ERR);
- if (ts->trx->bts->gprs.mode == BTS_GPRS_NONE)
- return 0;
- if (ts->pchan == GSM_PCHAN_TCH_F_PDCH
- && lchan->state == LCHAN_S_NONE)
- return rsl_ipacc_pdch_activate(ts, 1);
+ /* Otherwise, it is a regular lchan release. */
+ osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_RSL_RF_CHAN_REL_ACK, NULL);
return 0;
}
@@ -1035,6 +679,22 @@ int rsl_paging_cmd(struct gsm_bts *bts, uint8_t paging_group, uint8_t len,
return abis_rsl_sendmsg(msg);
}
+int rsl_forward_layer3_info(struct gsm_lchan *lchan, const uint8_t *l3_info, uint8_t l3_info_len)
+{
+ struct msgb *msg;
+ uint8_t *dst;
+
+ if (!l3_info || !l3_info_len)
+ return -EINVAL;
+
+ msg = rsl_msgb_alloc();
+ dst = msgb_put(msg, l3_info_len);
+ memcpy(dst, l3_info, l3_info_len);
+
+ msg->lchan = lchan;
+ return rsl_data_request(msg, 0);
+}
+
int imsi_str2bcd(uint8_t *bcd_out, const char *str_in)
{
int i, len = strlen(str_in);
@@ -1157,22 +817,6 @@ int rsl_establish_request(struct gsm_lchan *lchan, uint8_t link_id)
return abis_rsl_sendmsg(msg);
}
-static void rsl_handle_release(struct gsm_lchan *lchan);
-
-/* Special work handler to handle missing RSL_MT_REL_CONF message from
- * Nokia InSite BTS */
-static void lchan_rel_work_cb(void *data)
-{
- struct gsm_lchan *lchan = data;
- int sapi;
-
- for (sapi = 0; sapi < ARRAY_SIZE(lchan->sapis); ++sapi) {
- if (lchan->sapis[sapi] == LCHAN_SAPI_REL)
- lchan->sapis[sapi] = LCHAN_SAPI_UNUSED;
- }
- rsl_handle_release(lchan);
-}
-
/* Chapter 8.3.7 Request the release of multiframe mode of RLL connection.
This is what higher layers should call. The BTS then responds with
RELEASE CONFIRM, which we in turn use to trigger RSL CHANNEL RELEASE,
@@ -1189,8 +833,6 @@ int rsl_release_request(struct gsm_lchan *lchan, uint8_t link_id,
/* 0 is normal release, 1 is local end */
msgb_tv_put(msg, RSL_IE_RELEASE_MODE, release_mode);
- /* FIXME: start some timer in case we don't receive a REL ACK ? */
-
msg->dst = lchan->ts->trx->rsl_link;
DEBUGP(DRLL, "%s RSL RLL RELEASE REQ (link_id=0x%02x, reason=%u)\n",
@@ -1198,105 +840,26 @@ int rsl_release_request(struct gsm_lchan *lchan, uint8_t link_id,
abis_rsl_sendmsg(msg);
- /* Do not wait for Nokia BTS to send the confirm. */
- if (is_nokia_bts(lchan->ts->trx->bts)
- && lchan->ts->trx->bts->nokia.no_loc_rel_cnf
- && release_mode == RSL_REL_LOCAL_END) {
- DEBUGP(DRLL, "Scheduling release, becasuse Nokia InSite BTS does not send a RELease CONFirm.\n");
- lchan->sapis[link_id & 0x7] = LCHAN_SAPI_REL;
- osmo_timer_setup(&lchan->rel_work, lchan_rel_work_cb, lchan);
- osmo_timer_schedule(&lchan->rel_work, 0, 0);
- }
-
- return 0;
-}
-
-int rsl_lchan_mark_broken(struct gsm_lchan *lchan, const char *reason)
-{
- LOGP(DRSL, LOGL_ERROR, "%s %s lchan broken: %s\n",
- gsm_lchan_name(lchan), gsm_lchant_name(lchan->type), reason);
- rsl_lchan_set_state(lchan, LCHAN_S_BROKEN);
- lchan->broken_reason = reason;
- return 0;
-}
-
-int rsl_lchan_set_state_with_log(struct gsm_lchan *lchan, enum gsm_lchan_state state, const char *file, unsigned line)
-{
- if (lchan->state != state)
- LOGPSRC(DRSL, LOGL_DEBUG, file, line, "%s state %s -> %s\n",
- gsm_lchan_name(lchan), gsm_lchans_name(lchan->state), gsm_lchans_name(state));
-
- lchan->state = state;
return 0;
}
/* Chapter 8.4.2: Channel Activate Acknowledge */
static int rsl_rx_chan_act_ack(struct msgb *msg)
{
- struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
struct gsm_lchan *lchan = msg->lchan;
struct gsm_bts_trx_ts *ts = lchan->ts;
+ struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
- /* BTS has confirmed channel activation, we now need
- * to assign the activated channel to the MS */
- if (rslh->ie_chan != RSL_IE_CHAN_NR)
- return -EINVAL;
-
- osmo_timer_del(&lchan->act_timer);
-
- if (lchan->state == LCHAN_S_BROKEN) {
- int do_release = is_sysmobts_v2(ts->trx->bts);
- LOGP(DRSL, LOGL_NOTICE, "%s CHAN ACT ACK for broken channel. %s\n",
- gsm_lchan_name(lchan),
- do_release ? "Releasing it" : "Keeping it broken");
- if (do_release) {
- talloc_free(lchan->rqd_ref);
- lchan->rqd_ref = NULL;
- lchan->rqd_ta = 0;
- rsl_lchan_set_state(msg->lchan, LCHAN_S_ACTIVE);
- if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) {
- /*
- * lchan_act_tmr_cb() already called
- * lchan_free() and cleared the lchan->type, so
- * calling dyn_ts_switchover_complete() here
- * would not have the desired effect of
- * mimicking an activated lchan that we can
- * release. Instead hack the dyn ts state to
- * make sure that rsl_rx_rf_chan_rel_ack() will
- * switch back to PDCH, i.e. have pchan_is ==
- * pchan_want, both != GSM_PCHAN_PDCH:
- */
- ts->dyn.pchan_is = GSM_PCHAN_NONE;
- ts->dyn.pchan_want = GSM_PCHAN_NONE;
- }
- rsl_rf_chan_release(msg->lchan, 0, SACCH_NONE);
- }
+ /* Catch Osmocom style dyn TS PDCH Activ ACK: */
+ if (ts->pchan_on_init == GSM_PCHAN_TCH_F_TCH_H_PDCH
+ && lchan->nr == 0
+ && (rslh->chan_nr & RSL_CHAN_OSMO_PDCH) == RSL_CHAN_OSMO_PDCH) {
+ osmo_fsm_inst_dispatch(ts->fi, TS_EV_PDCH_ACT_ACK, NULL);
return 0;
}
- if (lchan->state != LCHAN_S_ACT_REQ)
- LOGP(DRSL, LOGL_NOTICE, "%s CHAN ACT ACK, but state %s\n",
- gsm_lchan_name(lchan),
- gsm_lchans_name(lchan->state));
- rsl_lchan_set_state(lchan, LCHAN_S_ACTIVE);
-
- if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH)
- dyn_ts_switchover_complete(lchan);
-
- if (lchan->rqd_ref) {
- rsl_send_imm_assignment(lchan);
- talloc_free(lchan->rqd_ref);
- lchan->rqd_ref = NULL;
- lchan->rqd_ta = 0;
- }
-
- send_lchan_signal(S_LCHAN_ACTIVATE_ACK, lchan, NULL);
-
- /* Update bts attributes inside the PCU */
- if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH ||
- ts->pchan == GSM_PCHAN_TCH_F_PDCH ||
- ts->pchan == GSM_PCHAN_PDCH)
- pcu_info_update(ts->trx->bts);
+ /* Otherwise, it is a regular lchan act. */
+ osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_RSL_CHAN_ACTIV_ACK, NULL);
return 0;
}
@@ -1306,46 +869,32 @@ static int rsl_rx_chan_act_nack(struct msgb *msg)
{
struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
struct tlv_parsed tp;
-
- osmo_timer_del(&msg->lchan->act_timer);
+ struct gsm_lchan *lchan = msg->lchan;
rate_ctr_inc(&msg->lchan->ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_ACT_NACK]);
- if (msg->lchan->state == LCHAN_S_BROKEN) {
+ if (!msg->lchan->fi) {
LOGP(DRSL, LOGL_ERROR,
- "%s CHANNEL ACTIVATE NACK for broken channel.\n",
- gsm_lchan_name(msg->lchan));
- return -1;
+ "%s CHANNEL ACTIVATE NACK on uninitialized lchan (chan_nr=0x%x)\n",
+ gsm_lchan_name(lchan), dh->chan_nr);
}
- /* BTS has rejected channel activation ?!? */
if (dh->ie_chan != RSL_IE_CHAN_NR) {
- LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK, and chan nr mismatches\n",
- gsm_lchan_name(msg->lchan));
+ LOG_LCHAN(msg->lchan, LOGL_ERROR, "Invalid IE: expected CHAN_NR IE (0x%x), got 0x%x",
+ RSL_IE_CHAN_NR, dh->ie_chan);
return -EINVAL;
}
rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
if (TLVP_PRESENT(&tp, RSL_IE_CAUSE)) {
const uint8_t *cause = TLVP_VAL(&tp, RSL_IE_CAUSE);
- LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK: ",
- gsm_lchan_name(msg->lchan));
- print_rsl_cause(LOGL_ERROR, cause,
- TLVP_LEN(&tp, RSL_IE_CAUSE));
- LOGPC(DRSL, LOGL_ERROR, "\n");
- msg->lchan->error_cause = *cause;
- if (*cause != RSL_ERR_RCH_ALR_ACTV_ALLOC) {
- rsl_lchan_mark_broken(msg->lchan, "NACK on activation");
- } else
- rsl_rf_chan_release(msg->lchan, 1, SACCH_DEACTIVATE);
-
+ LOG_LCHAN(lchan, LOGL_ERROR, "CHANNEL ACTIVATE NACK: %s (%s)",
+ rsl_err_name(*cause), osmo_hexdump(cause, TLVP_LEN(&tp, RSL_IE_CAUSE)));
+ osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_RSL_CHAN_ACTIV_NACK, (void*)cause);
} else {
- LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK, no cause IE\n",
- gsm_lchan_name(msg->lchan));
- rsl_lchan_mark_broken(msg->lchan, "NACK on activation no IE");
+ LOG_LCHAN(lchan, LOGL_ERROR, "CHANNEL ACTIVATE NACK lacks a cause IE");
+ osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_RSL_CHAN_ACTIV_NACK, 0);
}
-
- send_lchan_signal(S_LCHAN_ACTIVATE_NACK, msg->lchan, NULL);
return 0;
}
@@ -1357,9 +906,7 @@ static int rsl_rx_conn_fail(struct msgb *msg)
struct tlv_parsed tp;
uint8_t cause = 0;
- LOGP(DRSL, LOGL_NOTICE, "%s CONNECTION FAIL in state %s ",
- gsm_lchan_name(msg->lchan),
- gsm_lchans_name(msg->lchan->state));
+ LOG_LCHAN(lchan, LOGL_NOTICE, "CONNECTION FAIL");
rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
@@ -1372,7 +919,9 @@ static int rsl_rx_conn_fail(struct msgb *msg)
LOGPC(DRSL, LOGL_NOTICE, "\n");
rate_ctr_inc(&lchan->ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_RF_FAIL]);
- if (lchan->conn)
+ if (!lchan->conn) {
+ lchan_release(lchan, false, true, cause);
+ } else
osmo_fsm_inst_dispatch(lchan->conn->fi, GSCON_EV_RSL_CONN_FAIL, &cause);
return 0;
@@ -1459,11 +1008,8 @@ static int rsl_rx_meas_res(struct msgb *msg)
const uint8_t *val;
int rc;
- /* check if this channel is actually active */
- /* FIXME: maybe this check should be way more generic/centralized */
- if (msg->lchan->state != LCHAN_S_ACTIVE) {
- LOGP(DRSL, LOGL_DEBUG, "%s: MEAS RES for inactive channel\n",
- gsm_lchan_name(msg->lchan));
+ if (!lchan_may_receive_data(msg->lchan)) {
+ LOG_LCHAN(msg->lchan, LOGL_DEBUG, "MEAS RES for inactive channel");
return 0;
}
@@ -1543,76 +1089,37 @@ static int rsl_rx_hando_det(struct msgb *msg)
{
struct abis_rsl_dchan_hdr *dh = msgb_l2(msg);
struct tlv_parsed tp;
-
- DEBUGP(DRSL, "%s HANDOVER DETECT ", gsm_lchan_name(msg->lchan));
+ struct handover_rr_detect_data d = {
+ .msg = msg,
+ };
rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh));
if (TLVP_PRESENT(&tp, RSL_IE_ACCESS_DELAY))
- DEBUGPC(DRSL, "access delay = %u\n",
- *TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY));
- else
- DEBUGPC(DRSL, "\n");
-
- send_lchan_signal(S_LCHAN_HANDOVER_DETECT, msg->lchan, NULL);
-
- return 0;
-}
+ d.access_delay = TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY);
-static bool lchan_may_change_pdch(struct gsm_lchan *lchan, bool pdch_act)
-{
- struct gsm_bts_trx_ts *ts;
-
- OSMO_ASSERT(lchan);
-
- ts = lchan->ts;
- OSMO_ASSERT(ts);
- OSMO_ASSERT(ts->trx);
- OSMO_ASSERT(ts->trx->bts);
-
- if (lchan->ts->pchan != GSM_PCHAN_TCH_F_PDCH) {
- LOGP(DRSL, LOGL_ERROR, "%s pchan=%s Rx PDCH %s ACK"
- " for channel that is no TCH/F_PDCH\n",
- gsm_lchan_name(lchan),
- gsm_pchan_name(ts->pchan),
- pdch_act? "ACT" : "DEACT");
- return false;
+ if (!msg->lchan->conn || !msg->lchan->conn->ho.fi) {
+ LOGP(DRSL, LOGL_ERROR, "%s HANDOVER DETECT but no handover is ongoing\n",
+ gsm_lchan_name(msg->lchan));
+ return 0;
}
- if (lchan->state != LCHAN_S_NONE) {
- LOGP(DRSL, LOGL_ERROR, "%s pchan=%s Rx PDCH %s ACK"
- " in unexpected state: %s\n",
- gsm_lchan_name(lchan),
- gsm_pchan_name(ts->pchan),
- pdch_act? "ACT" : "DEACT",
- gsm_lchans_name(lchan->state));
- return false;
- }
- return true;
-}
-
-static int rsl_rx_pdch_act_ack(struct msgb *msg)
-{
- if (!lchan_may_change_pdch(msg->lchan, true))
- return -EINVAL;
-
- msg->lchan->ts->flags |= TS_F_PDCH_ACTIVE;
- msg->lchan->ts->flags &= ~TS_F_PDCH_ACT_PENDING;
+ osmo_fsm_inst_dispatch(msg->lchan->conn->ho.fi, HO_EV_RR_HO_DETECT, &d);
return 0;
}
-static int rsl_rx_pdch_deact_ack(struct msgb *msg)
+static int rsl_rx_ipacc_pdch(struct msgb *msg, char *name, uint32_t ts_ev)
{
- if (!lchan_may_change_pdch(msg->lchan, false))
- return -EINVAL;
-
- msg->lchan->ts->flags &= ~TS_F_PDCH_ACTIVE;
- msg->lchan->ts->flags &= ~TS_F_PDCH_DEACT_PENDING;
+ struct gsm_bts_trx_ts *ts = msg->lchan->ts;
- rsl_chan_activate_lchan(msg->lchan, msg->lchan->dyn.act_type,
- msg->lchan->dyn.ho_ref);
+ if (ts->pchan_on_init != GSM_PCHAN_TCH_F_PDCH) {
+ LOG_TS(ts, LOGL_ERROR, "Rx RSL ip.access PDCH %s acceptable only for %s",
+ name, gsm_pchan_name(GSM_PCHAN_TCH_F_PDCH));
+ return -EINVAL;
+ }
+ osmo_fsm_inst_dispatch(ts->fi, ts_ev, NULL);
return 0;
}
@@ -1620,18 +1127,39 @@ static int abis_rsl_rx_dchan(struct msgb *msg)
{
struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg);
int rc = 0;
- char *ts_name;
+ char *lchan_name;
struct e1inp_sign_link *sign_link = msg->dst;
+ if (rslh->ie_chan != RSL_IE_CHAN_NR) {
+ LOGP(DRSL, LOGL_ERROR,
+ "Rx RSL DCHAN: invalid RSL header, expecting Channel Number IE tag, got 0x%x\n",
+ rslh->ie_chan);
+ return -EINVAL;
+ }
+
msg->lchan = lchan_lookup(sign_link->trx, rslh->chan_nr,
"Abis RSL rx DCHAN: ");
- if (!msg->lchan)
- return -1;
- ts_name = gsm_lchan_name(msg->lchan);
+ if (!msg->lchan) {
+ LOGP(DRSL, LOGL_ERROR,
+ "Rx RSL DCHAN: unable to match RSL message to an lchan: chan_nr=0x%x\n",
+ rslh->chan_nr);
+ return -EINVAL;
+ }
+
+ lchan_name = gsm_lchan_name(msg->lchan);
+ DEBUGP(DRSL, "%s %s\n", gsm_lchan_name(msg->lchan), rsl_or_ipac_msg_name(rslh->c.msg_type));
+
+ if (!msg->lchan->fi) {
+ LOGP(DRSL, LOGL_ERROR,
+ "Rx RSL DCHAN: RSL message for unconfigured lchan: chan_nr=0x%x %s trx=%p ts=%p=%p"
+ " lchan=%p=%p\n",
+ rslh->chan_nr, gsm_lchan_name(msg->lchan), sign_link->trx, msg->lchan->ts,
+ msg->lchan->ts->fi, msg->lchan, msg->lchan->fi);
+ return -EINVAL;
+ }
switch (rslh->c.msg_type) {
case RSL_MT_CHAN_ACTIV_ACK:
- DEBUGP(DRSL, "%s CHANNEL ACTIVATE ACK\n", ts_name);
rc = rsl_rx_chan_act_ack(msg);
count_codecs(sign_link->trx->bts, msg->lchan);
break;
@@ -1652,27 +1180,22 @@ static int abis_rsl_rx_dchan(struct msgb *msg)
break;
case RSL_MT_MODE_MODIFY_ACK:
count_codecs(sign_link->trx->bts, msg->lchan);
- DEBUGP(DRSL, "%s CHANNEL MODE MODIFY ACK\n", ts_name);
break;
case RSL_MT_MODE_MODIFY_NACK:
- LOGP(DRSL, LOGL_ERROR, "%s CHANNEL MODE MODIFY NACK\n", ts_name);
+ LOGP(DRSL, LOGL_ERROR, "%s CHANNEL MODE MODIFY NACK\n", lchan_name);
rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_MODE_MODIFY_NACK]);
break;
case RSL_MT_IPAC_PDCH_ACT_ACK:
- DEBUGP(DRSL, "%s IPAC PDCH ACT ACK\n", ts_name);
- rc = rsl_rx_pdch_act_ack(msg);
+ rc = rsl_rx_ipacc_pdch(msg, "ACT ACK", TS_EV_PDCH_ACT_ACK);
break;
case RSL_MT_IPAC_PDCH_ACT_NACK:
- LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH ACT NACK\n", ts_name);
- rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]);
+ rc = rsl_rx_ipacc_pdch(msg, "ACT NACK", TS_EV_PDCH_ACT_NACK);
break;
case RSL_MT_IPAC_PDCH_DEACT_ACK:
- DEBUGP(DRSL, "%s IPAC PDCH DEACT ACK\n", ts_name);
- rc = rsl_rx_pdch_deact_ack(msg);
+ rc = rsl_rx_ipacc_pdch(msg, "DEACT ACK", TS_EV_PDCH_DEACT_ACK);
break;
case RSL_MT_IPAC_PDCH_DEACT_NACK:
- LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH DEACT NACK\n", ts_name);
- rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]);
+ rc = rsl_rx_ipacc_pdch(msg, "DEACT NACK", TS_EV_PDCH_DEACT_NACK);
break;
case RSL_MT_PHY_CONTEXT_CONF:
case RSL_MT_PREPROC_MEAS_RES:
@@ -1683,12 +1206,12 @@ static int abis_rsl_rx_dchan(struct msgb *msg)
case RSL_MT_MR_CODEC_MOD_NACK:
case RSL_MT_MR_CODEC_MOD_PER:
LOGP(DRSL, LOGL_NOTICE, "%s Unimplemented Abis RSL DChan "
- "msg 0x%02x\n", ts_name, rslh->c.msg_type);
+ "msg 0x%02x\n", lchan_name, rslh->c.msg_type);
rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_UNKNOWN]);
break;
default:
LOGP(DRSL, LOGL_NOTICE, "%s unknown Abis RSL DChan msg 0x%02x\n",
- ts_name, rslh->c.msg_type);
+ lchan_name, rslh->c.msg_type);
rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_UNKNOWN]);
return -EINVAL;
}
@@ -1749,36 +1272,6 @@ static int abis_rsl_rx_trx(struct msgb *msg)
return rc;
}
-/* If T3101 expires, we never received a response to IMMEDIATE ASSIGN */
-static void t3101_expired(void *data)
-{
- struct gsm_lchan *lchan = data;
- LOGP(DRSL, LOGL_NOTICE,
- "%s T3101 expired: no response to IMMEDIATE ASSIGN\n",
- gsm_lchan_name(lchan));
- rsl_rf_chan_release(lchan, 1, SACCH_DEACTIVATE);
-}
-
-/* If T3111 expires, we will send the RF Channel Request */
-static void t3111_expired(void *data)
-{
- struct gsm_lchan *lchan = data;
- LOGP(DRSL, LOGL_NOTICE,
- "%s T3111 expired: releasing RF Channel\n",
- gsm_lchan_name(lchan));
- rsl_rf_chan_release(lchan, 0, SACCH_NONE);
-}
-
-/* If T3109 expires the MS has not send a UA/UM do the error release */
-static void t3109_expired(void *data)
-{
- struct gsm_lchan *lchan = data;
-
- LOGP(DRSL, LOGL_ERROR,
- "%s SACCH deactivation timeout.\n", gsm_lchan_name(lchan));
- rsl_rf_chan_release(lchan, 1, SACCH_NONE);
-}
-
/* Format an IMM ASS REJ according to 04.08 Chapter 9.1.20 */
static int rsl_send_imm_ass_rej(struct gsm_bts *bts,
struct gsm48_req_ref *rqd_ref,
@@ -1814,6 +1307,19 @@ static int rsl_send_imm_ass_rej(struct gsm_bts *bts,
return rsl_imm_assign_cmd(bts, sizeof(*iar), (uint8_t *) iar);
}
+int rsl_tx_imm_ass_rej(struct gsm_lchan *lchan)
+{
+ uint8_t wait_ind;
+ struct gsm_bts *bts = lchan->ts->trx->bts;
+ if (bts->T3122)
+ wait_ind = bts->T3122;
+ else {
+ wait_ind = T_def_get(bts->network->T_defs, 3122, T_S, -1);
+ }
+ /* The BTS will gather multiple CHAN RQD and reject up to 4 MS at the same time. */
+ return rsl_send_imm_ass_rej(bts, lchan->rqd_ref, wait_ind);
+}
+
/* Handle packet channel rach requests */
static int rsl_rx_pchan_rqd(struct msgb *msg, struct gsm_bts *bts)
{
@@ -1859,9 +1365,6 @@ static int rsl_rx_chan_rqd(struct msgb *msg)
struct gsm_lchan *lchan;
uint8_t rqd_ta;
- uint16_t arfcn;
- uint8_t subch;
-
/* parse request reference to be used in immediate assign */
if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE)
return -EINVAL;
@@ -1901,79 +1404,44 @@ static int rsl_rx_chan_rqd(struct msgb *msg)
* Note: If the MS requests not TCH/H, we don't know if the phone
* supports TCH/H, so we must assign TCH/F or SDCCH.
*/
- lchan = lchan_alloc(bts, GSM_LCHAN_SDCCH, 0);
+ lchan = lchan_select_by_type(bts, GSM_LCHAN_SDCCH);
if (!lchan && lctype != GSM_LCHAN_SDCCH) {
LOGP(DRSL, LOGL_NOTICE, "(bts=%d) CHAN RQD: no resources for %s "
"0x%x, retrying with %s\n",
msg->lchan->ts->trx->bts->nr,
gsm_lchant_name(GSM_LCHAN_SDCCH), rqd_ref->ra,
gsm_lchant_name(lctype));
- lchan = lchan_alloc(bts, lctype, 0);
+ lchan = lchan_select_by_type(bts, lctype);
}
if (!lchan) {
- uint8_t wait_ind;
LOGP(DRSL, LOGL_NOTICE, "(bts=%d) CHAN RQD: no resources for %s 0x%x\n",
msg->lchan->ts->trx->bts->nr, gsm_lchant_name(lctype), rqd_ref->ra);
rate_ctr_inc(&bts->bts_ctrs->ctr[BTS_CTR_CHREQ_NO_CHANNEL]);
- if (bts->T3122)
- wait_ind = bts->T3122;
- else if (bts->network->T3122)
- wait_ind = bts->network->T3122 & 0xff;
- else
- wait_ind = GSM_T3122_DEFAULT;
- /* The BTS will gather multiple CHAN RQD and reject up to 4 MS at the same time. */
- rsl_send_imm_ass_rej(bts, rqd_ref, wait_ind);
+ rsl_tx_imm_ass_rej(msg->lchan);
return 0;
}
- /*
- * Expecting lchan state to be NONE, except for dyn TS in PDCH mode.
- * Those are expected to be ACTIVE: the PDCH release will be sent from
- * rsl_chan_activate_lchan() below.
- */
- if (lchan->state != LCHAN_S_NONE
- && !(lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
- && lchan->ts->dyn.pchan_is == GSM_PCHAN_PDCH
- && lchan->state == LCHAN_S_ACTIVE))
- LOGP(DRSL, LOGL_NOTICE, "%s lchan_alloc() returned channel "
- "in state %s\n", gsm_lchan_name(lchan),
- gsm_lchans_name(lchan->state));
-
/* save the RACH data as we need it after the CHAN ACT ACK */
lchan->rqd_ref = talloc_zero(bts, struct gsm48_req_ref);
- if (!lchan->rqd_ref) {
- LOGP(DRSL, LOGL_ERROR, "Failed to allocate gsm48_req_ref.\n");
- lchan_free(lchan);
- return -ENOMEM;
- }
+ OSMO_ASSERT(lchan->rqd_ref);
- memcpy(lchan->rqd_ref, rqd_ref, sizeof(*rqd_ref));
+ *(lchan->rqd_ref) = *rqd_ref;
lchan->rqd_ta = rqd_ta;
- arfcn = lchan->ts->trx->arfcn;
- subch = lchan->nr;
-
- lchan->encr.alg_id = RSL_ENC_ALG_A5(0); /* no encryption */
- lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
- lchan->bs_power = 0; /* 0dB reduction, output power = Pn */
- lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
- lchan->tch_mode = GSM48_CMODE_SIGN;
-
- /* Start another timer or assume the BTS sends a ACK/NACK? */
- osmo_timer_setup(&lchan->act_timer, lchan_act_tmr_cb, lchan);
- osmo_timer_schedule(&lchan->act_timer, 4, 0);
-
- DEBUGP(DRSL, "%s Activating ARFCN(%u) SS(%u) lctype %s "
- "r=%s ra=0x%02x ta=%d\n", gsm_lchan_name(lchan), arfcn, subch,
- gsm_lchant_name(lchan->type), gsm_chreq_name(chreq_reason),
- rqd_ref->ra, rqd_ta);
-
- rsl_chan_activate_lchan(lchan, RSL_ACT_INTRA_IMM_ASS, 0);
+ LOG_LCHAN(lchan, LOGL_DEBUG, "MS: Channel Request: reason=%s ra=0x%02x ta=%d\n",
+ gsm_chreq_name(chreq_reason), rqd_ref->ra, rqd_ta);
+ {
+ struct lchan_activate_info info = {
+ .activ_for = FOR_MS_CHANNEL_REQUEST,
+ .chan_mode = GSM48_CMODE_SIGN,
+ };
+ lchan_activate(lchan, &info);
+ }
return 0;
}
-static int rsl_send_imm_assignment(struct gsm_lchan *lchan)
+int rsl_tx_imm_assignment(struct gsm_lchan *lchan)
{
struct gsm_bts *bts = lchan->ts->trx->bts;
uint8_t buf[GSM_MACBLOCK_LEN];
@@ -1999,10 +1467,6 @@ static int rsl_send_imm_assignment(struct gsm_lchan *lchan)
/* we need to subtract 1 byte from sizeof(*ia) since ia includes the l2_plen field */
ia->l2_plen = GSM48_LEN2PLEN((sizeof(*ia)-1) + ia->mob_alloc_len);
- /* Start timer T3101 to wait for GSM48_MT_RR_PAG_RESP */
- osmo_timer_setup(&lchan->T3101, t3101_expired, lchan);
- osmo_timer_schedule(&lchan->T3101, bts->network->T3101, 0);
-
/* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */
return rsl_imm_assign_cmd(bts, sizeof(*ia)+ia->mob_alloc_len, (uint8_t *) ia);
}
@@ -2113,55 +1577,20 @@ static int rsl_rx_rll_err_ind(struct msgb *msg)
rsl_tlv_parse(&tp, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
if (!TLVP_PRESENT(&tp, RSL_IE_RLM_CAUSE)) {
- LOGP(DRLL, LOGL_ERROR,
- "%s ERROR INDICATION without mandantory cause.\n",
- gsm_lchan_name(msg->lchan));
+ LOG_LCHAN(msg->lchan, LOGL_ERROR, "ERROR INDICATION without mandantory cause.");
return -1;
}
rlm_cause = *TLVP_VAL(&tp, RSL_IE_RLM_CAUSE);
- LOGP(DRLL, LOGL_ERROR, "%s ERROR INDICATION cause=%s in state=%s\n",
- gsm_lchan_name(msg->lchan),
- rsl_rlm_cause_name(rlm_cause),
- gsm_lchans_name(msg->lchan->state));
+ LOG_LCHAN(msg->lchan, LOGL_ERROR, "ERROR INDICATION cause=%s\n", rsl_rlm_cause_name(rlm_cause));
rll_indication(msg->lchan, rllh->link_id, BSC_RLLR_IND_ERR_IND);
- if (rlm_cause == RLL_CAUSE_T200_EXPIRED) {
- rate_ctr_inc(&msg->lchan->ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_RLL_ERR]);
- return rsl_rf_chan_release_err(msg->lchan);
- }
+ rate_ctr_inc(&msg->lchan->ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_RLL_ERR]);
- return 0;
-}
+ osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_RLL_ERR_IND, &rlm_cause);
-static void rsl_handle_release(struct gsm_lchan *lchan)
-{
- int sapi;
- struct gsm_bts *bts;
-
- /*
- * Maybe only one link/SAPI was releasd or the error handling
- * was activated. Just return now and let the other code handle
- * it.
- */
- if (lchan->state != LCHAN_S_REL_REQ)
- return;
-
- for (sapi = 0; sapi < ARRAY_SIZE(lchan->sapis); ++sapi) {
- if (lchan->sapis[sapi] == LCHAN_SAPI_UNUSED)
- continue;
- LOGP(DRSL, LOGL_DEBUG, "%s waiting for SAPI=%d to be released.\n",
- gsm_lchan_name(lchan), sapi);
- return;
- }
-
-
- /* Stop T3109 and wait for T3111 before re-using the channel */
- osmo_timer_del(&lchan->T3109);
- osmo_timer_setup(&lchan->T3111, t3111_expired, lchan);
- bts = lchan->ts->trx->bts;
- osmo_timer_schedule(&lchan->T3111, bts->network->T3111, 0);
+ return 0;
}
/* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST
@@ -2175,13 +1604,13 @@ static int abis_rsl_rx_rll(struct msgb *msg)
struct e1inp_sign_link *sign_link = msg->dst;
struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
int rc = 0;
- char *ts_name;
+ char *lchan_name;
uint8_t sapi = rllh->link_id & 7;
msg->lchan = lchan_lookup(sign_link->trx, rllh->chan_nr,
"Abis RSL rx RLL: ");
- ts_name = gsm_lchan_name(msg->lchan);
- DEBUGP(DRLL, "%s SAPI=%u ", ts_name, sapi);
+ lchan_name = gsm_lchan_name(msg->lchan);
+ DEBUGP(DRLL, "%s SAPI=%u ", lchan_name, sapi);
switch (rllh->c.msg_type) {
case RSL_MT_DATA_IND:
@@ -2226,7 +1655,8 @@ static int abis_rsl_rx_rll(struct msgb *msg)
}
msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_MS;
- osmo_timer_del(&msg->lchan->T3101);
+ osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_RLL_ESTABLISH_IND, msg);
+
if (msgb_l2len(msg) >
sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) &&
rllh->data[0] == RSL_IE_L3_INFO) {
@@ -2242,21 +1672,12 @@ static int abis_rsl_rx_rll(struct msgb *msg)
break;
case RSL_MT_REL_IND:
/* BTS informs us of having received DISC from MS */
- DEBUGPC(DRLL, "RELEASE INDICATION\n");
- msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
- rll_indication(msg->lchan, rllh->link_id,
- BSC_RLLR_IND_REL_IND);
- rsl_handle_release(msg->lchan);
- /* if it was the main signalling link, let the subscr_conn_fsm know */
- if (msg->lchan->conn && sapi == 0 && (rllh->link_id >> 6) == 0)
- osmo_fsm_inst_dispatch(msg->lchan->conn->fi, GSCON_EV_RLL_REL_IND, msg);
+ osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_RLL_REL_IND, &rllh->link_id);
break;
case RSL_MT_REL_CONF:
/* BTS informs us of having received UA from MS,
* in response to DISC that we've sent earlier */
- DEBUGPC(DRLL, "RELEASE CONFIRMATION\n");
- msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED;
- rsl_handle_release(msg->lchan);
+ osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_RLL_REL_CONF, &rllh->link_id);
break;
case RSL_MT_ERROR_IND:
DEBUGPC(DRLL, "ERROR INDICATION\n");
@@ -2276,11 +1697,12 @@ static int abis_rsl_rx_rll(struct msgb *msg)
return rc;
}
-static uint8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
+/* Return an ip.access BTS speech mode value (uint8_t) or negative on error. */
+int ipacc_speech_mode(enum gsm48_chan_mode tch_mode, enum gsm_chan_t type)
{
- switch (lchan->tch_mode) {
+ switch (tch_mode) {
case GSM48_CMODE_SPEECH_V1:
- switch (lchan->type) {
+ switch (type) {
case GSM_LCHAN_TCH_F:
return 0x00;
case GSM_LCHAN_TCH_H:
@@ -2290,7 +1712,7 @@ static uint8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
}
break;
case GSM48_CMODE_SPEECH_EFR:
- switch (lchan->type) {
+ switch (type) {
case GSM_LCHAN_TCH_F:
return 0x01;
/* there's no half-rate EFR */
@@ -2299,7 +1721,7 @@ static uint8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
}
break;
case GSM48_CMODE_SPEECH_AMR:
- switch (lchan->type) {
+ switch (type) {
case GSM_LCHAN_TCH_F:
return 0x02;
case GSM_LCHAN_TCH_H:
@@ -2311,16 +1733,24 @@ static uint8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan)
default:
break;
}
- LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access speech mode for "
- "tch_mode == 0x%02x\n", lchan->tch_mode);
- return 0;
+ return -EINVAL;
}
-static uint8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan)
+void ipacc_speech_mode_set_direction(uint8_t *speech_mode, bool send)
{
- switch (lchan->tch_mode) {
+ const uint8_t recv_only_flag = 0x10;
+ if (send)
+ *speech_mode = *speech_mode & ~recv_only_flag;
+ else
+ *speech_mode = *speech_mode | recv_only_flag;
+}
+
+/* Return an ip.access BTS payload type value (uint8_t) or negative on error. */
+int ipacc_payload_type(enum gsm48_chan_mode tch_mode, enum gsm_chan_t type)
+{
+ switch (tch_mode) {
case GSM48_CMODE_SPEECH_V1:
- switch (lchan->type) {
+ switch (type) {
case GSM_LCHAN_TCH_F:
return RTP_PT_GSM_FULL;
case GSM_LCHAN_TCH_H:
@@ -2330,7 +1760,7 @@ static uint8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan)
}
break;
case GSM48_CMODE_SPEECH_EFR:
- switch (lchan->type) {
+ switch (type) {
case GSM_LCHAN_TCH_F:
return RTP_PT_GSM_EFR;
/* there's no half-rate EFR */
@@ -2339,7 +1769,7 @@ static uint8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan)
}
break;
case GSM48_CMODE_SPEECH_AMR:
- switch (lchan->type) {
+ switch (type) {
case GSM_LCHAN_TCH_F:
case GSM_LCHAN_TCH_H:
return RTP_PT_AMR;
@@ -2350,72 +1780,70 @@ static uint8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan)
default:
break;
}
- LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access rtp payload type for "
- "tch_mode == 0x%02x\n & lchan_type == %d",
- lchan->tch_mode, lchan->type);
- return 0;
+ return -EINVAL;
+}
+
+static const char *ip_to_a(uint32_t ip)
+{
+ struct in_addr ia;
+ ia.s_addr = htonl(ip);
+ return inet_ntoa(ia);
}
/* ip.access specific RSL extensions */
-static void ipac_parse_rtp(struct gsm_lchan *lchan, struct tlv_parsed *tv)
+static void ipac_parse_rtp(struct gsm_lchan *lchan, struct tlv_parsed *tv, const char *label)
{
struct in_addr ip;
uint16_t port, conn_id;
if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_IP)) {
ip.s_addr = tlvp_val32_unal(tv, RSL_IE_IPAC_LOCAL_IP);
- DEBUGPC(DRSL, "LOCAL_IP=%s ", inet_ntoa(ip));
lchan->abis_ip.bound_ip = ntohl(ip.s_addr);
}
if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_PORT)) {
port = tlvp_val16_unal(tv, RSL_IE_IPAC_LOCAL_PORT);
port = ntohs(port);
- DEBUGPC(DRSL, "LOCAL_PORT=%u ", port);
lchan->abis_ip.bound_port = port;
}
if (TLVP_PRESENT(tv, RSL_IE_IPAC_CONN_ID)) {
conn_id = tlvp_val16_unal(tv, RSL_IE_IPAC_CONN_ID);
conn_id = ntohs(conn_id);
- DEBUGPC(DRSL, "CON_ID=%u ", conn_id);
lchan->abis_ip.conn_id = conn_id;
}
if (TLVP_PRESENT(tv, RSL_IE_IPAC_RTP_PAYLOAD2)) {
lchan->abis_ip.rtp_payload2 =
*TLVP_VAL(tv, RSL_IE_IPAC_RTP_PAYLOAD2);
- DEBUGPC(DRSL, "RTP_PAYLOAD2=0x%02x ",
- lchan->abis_ip.rtp_payload2);
}
if (TLVP_PRESENT(tv, RSL_IE_IPAC_SPEECH_MODE)) {
lchan->abis_ip.speech_mode =
*TLVP_VAL(tv, RSL_IE_IPAC_SPEECH_MODE);
- DEBUGPC(DRSL, "speech_mode=0x%02x ",
- lchan->abis_ip.speech_mode);
}
+ /* Why would we receive the MGW IP and port back from the BTS, and why would we care?? */
if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_IP)) {
ip.s_addr = tlvp_val32_unal(tv, RSL_IE_IPAC_REMOTE_IP);
- DEBUGPC(DRSL, "REMOTE_IP=%s ", inet_ntoa(ip));
lchan->abis_ip.connect_ip = ntohl(ip.s_addr);
}
-
if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_PORT)) {
port = tlvp_val16_unal(tv, RSL_IE_IPAC_REMOTE_PORT);
port = ntohs(port);
- DEBUGPC(DRSL, "REMOTE_PORT=%u ", port);
lchan->abis_ip.connect_port = port;
}
- DEBUGPC(DRSL, "\n");
+ LOG_LCHAN(lchan, LOGL_DEBUG, "Rx IPACC %s ACK:"
+ " BTS=%s:%u conn_id=%u rtp_payload2=0x%02x speech_mode=0x%02x",
+ label, ip_to_a(lchan->abis_ip.bound_ip), lchan->abis_ip.bound_port,
+ lchan->abis_ip.conn_id, lchan->abis_ip.rtp_payload2, lchan->abis_ip.speech_mode);
}
-/*! \brief Issue IPA RSL CRCX to configure RTP on BTS side
- * \param[in] lchan Logical Channel for which we issue CRCX
+/*! Send Issue IPA RSL CRCX to configure the RTP port of the BTS.
+ * \param[in] lchan Logical Channel for which we issue CRCX
*/
-int rsl_ipacc_crcx(struct gsm_lchan *lchan)
+int rsl_tx_ipacc_crcx(struct gsm_lchan *lchan)
{
struct msgb *msg = rsl_msgb_alloc();
struct abis_rsl_dchan_hdr *dh;
@@ -2426,116 +1854,52 @@ int rsl_ipacc_crcx(struct gsm_lchan *lchan)
dh->chan_nr = gsm_lchan2chan_nr(lchan);
/* 0x1- == receive-only, 0x-1 == EFR codec */
- lchan->abis_ip.speech_mode = 0x10 | ipa_smod_s_for_lchan(lchan);
- lchan->abis_ip.rtp_payload = ipa_rtp_pt_for_lchan(lchan);
msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD, lchan->abis_ip.rtp_payload);
- DEBUGP(DRSL, "%s IPAC_BIND speech_mode=0x%02x RTP_PAYLOAD=%d\n",
- gsm_lchan_name(lchan), lchan->abis_ip.speech_mode,
- lchan->abis_ip.rtp_payload);
+ LOG_LCHAN(lchan, LOGL_DEBUG, "Sending IPACC CRCX to BTS: speech_mode=0x%02x RTP_PAYLOAD=%d\n",
+ lchan->abis_ip.speech_mode, lchan->abis_ip.rtp_payload);
msg->dst = lchan->ts->trx->rsl_link;
return abis_rsl_sendmsg(msg);
}
-/*! \brief Issue IPA RSL MDCX to configure MGW-side of RTP
- * \param[in] lchan Logical Channel for which we issue MDCX
- * \param[in] ip Remote (MGW) IP address for RTP
- * \param[in] port Remote (MGW) UDP port number for RTP
- * \param[in] rtp_payload2 Contents of RTP PAYLOAD 2 IE
+/*! Send IPA RSL MDCX to configure the RTP port the BTS sends to (MGW).
+ * \param[in] lchan Logical Channel for which we issue MDCX
+ * Remote (MGW) IP address, port and payload types for RTP are determined from lchan->abis_ip.
*/
-int rsl_ipacc_mdcx(struct gsm_lchan *lchan, uint32_t ip, uint16_t port,
- uint8_t rtp_payload2)
+int rsl_tx_ipacc_mdcx(struct gsm_lchan *lchan)
{
struct msgb *msg = rsl_msgb_alloc();
struct abis_rsl_dchan_hdr *dh;
uint32_t *att_ip;
- struct in_addr ia;
dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
init_dchan_hdr(dh, RSL_MT_IPAC_MDCX);
dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS;
dh->chan_nr = gsm_lchan2chan_nr(lchan);
- /* we need to store these now as MDCX_ACK does not return them :( */
- lchan->abis_ip.rtp_payload2 = rtp_payload2;
- lchan->abis_ip.connect_port = port;
- lchan->abis_ip.connect_ip = ip;
-
- /* 0x0- == both directions, 0x-1 == EFR codec */
- lchan->abis_ip.speech_mode = 0x00 | ipa_smod_s_for_lchan(lchan);
- lchan->abis_ip.rtp_payload = ipa_rtp_pt_for_lchan(lchan);
-
- ia.s_addr = htonl(ip);
- DEBUGP(DRSL, "%s IPAC_MDCX IP=%s PORT=%d RTP_PAYLOAD=%d RTP_PAYLOAD2=%d "
- "CONN_ID=%d speech_mode=0x%02x\n", gsm_lchan_name(lchan),
- inet_ntoa(ia), port, lchan->abis_ip.rtp_payload, rtp_payload2,
- lchan->abis_ip.conn_id, lchan->abis_ip.speech_mode);
-
msgb_tv16_put(msg, RSL_IE_IPAC_CONN_ID, lchan->abis_ip.conn_id);
msgb_v_put(msg, RSL_IE_IPAC_REMOTE_IP);
- att_ip = (uint32_t *) msgb_put(msg, sizeof(ip));
- *att_ip = ia.s_addr;
- msgb_tv16_put(msg, RSL_IE_IPAC_REMOTE_PORT, port);
+ att_ip = (uint32_t *)msgb_put(msg, sizeof(uint32_t));
+ *att_ip = htonl(lchan->abis_ip.connect_ip);
+ msgb_tv16_put(msg, RSL_IE_IPAC_REMOTE_PORT, lchan->abis_ip.connect_port);
msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode);
msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD, lchan->abis_ip.rtp_payload);
- if (rtp_payload2)
- msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, rtp_payload2);
+ if (lchan->abis_ip.rtp_payload2)
+ msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, lchan->abis_ip.rtp_payload2);
msg->dst = lchan->ts->trx->rsl_link;
- return abis_rsl_sendmsg(msg);
-}
-
-static bool check_gprs_enabled(struct gsm_bts_trx_ts *ts)
-{
- if (ts->trx->bts->gprs.mode == BTS_GPRS_NONE) {
- LOGP(DRSL, LOGL_NOTICE, "%s: GPRS mode is 'none': not activating PDCH.\n",
- gsm_ts_and_pchan_name(ts));
- return false;
- }
- return true;
-}
-
-int rsl_ipacc_pdch_activate(struct gsm_bts_trx_ts *ts, int act)
-{
- struct msgb *msg = rsl_msgb_alloc();
- struct abis_rsl_dchan_hdr *dh;
- uint8_t msg_type;
-
- if (ts->flags & TS_F_PDCH_PENDING_MASK) {
- LOGP(DRSL, LOGL_ERROR,
- "%s PDCH %s requested, but a PDCH%s%s is still pending\n",
- gsm_ts_name(ts),
- act ? "ACT" : "DEACT",
- ts->flags & TS_F_PDCH_ACT_PENDING? " ACT" : "",
- ts->flags & TS_F_PDCH_DEACT_PENDING? " DEACT" : "");
- return -EINVAL;
- }
-
- if (act){
- if (!check_gprs_enabled(ts))
- return -ENOTSUP;
-
- msg_type = RSL_MT_IPAC_PDCH_ACT;
- ts->flags |= TS_F_PDCH_ACT_PENDING;
- } else {
- msg_type = RSL_MT_IPAC_PDCH_DEACT;
- ts->flags |= TS_F_PDCH_DEACT_PENDING;
- }
- /* TODO add timeout to cancel PDCH DE/ACT */
-
- dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
- init_dchan_hdr(dh, msg_type);
- dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
- dh->chan_nr = gsm_pchan2chan_nr(GSM_PCHAN_TCH_F, ts->nr, 0);
-
- DEBUGP(DRSL, "%s IPAC PDCH %sACT\n", gsm_ts_name(ts),
- act ? "" : "DE");
-
- msg->dst = ts->trx->rsl_link;
+ LOG_LCHAN(lchan, LOGL_DEBUG, "Sending IPACC MDCX to BTS:"
+ " %s:%u rtp_payload=%u rtp_payload2=%u conn_id=%u speech_mode=0x%02x",
+ ip_to_a(lchan->abis_ip.connect_ip),
+ lchan->abis_ip.connect_port,
+ lchan->abis_ip.rtp_payload,
+ lchan->abis_ip.rtp_payload2,
+ lchan->abis_ip.conn_id,
+ lchan->abis_ip.speech_mode);
return abis_rsl_sendmsg(msg);
}
@@ -2558,9 +1922,9 @@ static int abis_rsl_rx_ipacc_crcx_ack(struct msgb *msg)
return -EINVAL;
}
- ipac_parse_rtp(lchan, &tv);
+ ipac_parse_rtp(lchan, &tv, "CRCX");
- osmo_signal_dispatch(SS_ABISIP, S_ABISIP_CRCX_ACK, msg->lchan);
+ osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_IPACC_CRCX_ACK, 0);
return 0;
}
@@ -2576,8 +1940,9 @@ static int abis_rsl_rx_ipacc_mdcx_ack(struct msgb *msg)
* connected the given logical channel */
rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh));
- ipac_parse_rtp(lchan, &tv);
- osmo_signal_dispatch(SS_ABISIP, S_ABISIP_MDCX_ACK, msg->lchan);
+ ipac_parse_rtp(lchan, &tv, "MDCX");
+
+ osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_IPACC_MDCX_ACK, 0);
return 0;
}
@@ -2593,7 +1958,7 @@ static int abis_rsl_rx_ipacc_dlcx_ind(struct msgb *msg)
print_rsl_cause(LOGL_DEBUG, TLVP_VAL(&tv, RSL_IE_CAUSE),
TLVP_LEN(&tv, RSL_IE_CAUSE));
- osmo_signal_dispatch(SS_ABISIP, S_ABISIP_DLCX_IND, msg->lchan);
+ LOG_LCHAN(msg->lchan, LOGL_NOTICE, "Rx IPACC DLCX IND");
return 0;
}
@@ -2602,37 +1967,36 @@ static int abis_rsl_rx_ipacc(struct msgb *msg)
{
struct e1inp_sign_link *sign_link = msg->dst;
struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
- char *ts_name;
+ char *lchan_name;
int rc = 0;
msg->lchan = lchan_lookup(sign_link->trx, rllh->chan_nr,
"Abis RSL rx IPACC: ");
- ts_name = gsm_lchan_name(msg->lchan);
+ lchan_name = gsm_lchan_name(msg->lchan);
switch (rllh->c.msg_type) {
case RSL_MT_IPAC_CRCX_ACK:
- DEBUGP(DRSL, "%s IPAC_CRCX_ACK ", ts_name);
rc = abis_rsl_rx_ipacc_crcx_ack(msg);
break;
case RSL_MT_IPAC_CRCX_NACK:
/* somehow the BTS was unable to bind the lchan to its local
* port?!? */
- LOGP(DRSL, LOGL_ERROR, "%s IPAC_CRCX_NACK\n", ts_name);
+ LOGP(DRSL, LOGL_ERROR, "%s IPAC_CRCX_NACK\n", lchan_name);
rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]);
+ osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_IPACC_CRCX_NACK, 0);
break;
case RSL_MT_IPAC_MDCX_ACK:
/* the BTS tells us that a connect operation was successful */
- DEBUGP(DRSL, "%s IPAC_MDCX_ACK ", ts_name);
rc = abis_rsl_rx_ipacc_mdcx_ack(msg);
break;
case RSL_MT_IPAC_MDCX_NACK:
/* somehow the BTS was unable to connect the lchan to a remote
* port */
- LOGP(DRSL, LOGL_ERROR, "%s IPAC_MDCX_NACK\n", ts_name);
+ LOGP(DRSL, LOGL_ERROR, "%s IPAC_MDCX_NACK\n", lchan_name);
rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]);
+ osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_IPACC_MDCX_NACK, 0);
break;
case RSL_MT_IPAC_DLCX_IND:
- DEBUGP(DRSL, "%s IPAC_DLCX_IND ", ts_name);
rc = abis_rsl_rx_ipacc_dlcx_ind(msg);
break;
default:
@@ -2645,207 +2009,84 @@ static int abis_rsl_rx_ipacc(struct msgb *msg)
return rc;
}
-int dyn_ts_switchover_start(struct gsm_bts_trx_ts *ts,
- enum gsm_phys_chan_config to_pchan)
+/*! Tx simplified channel (de-)activation message for non-standard Osmocom dyn TS PDCH type. */
+static int send_osmocom_style_pdch_chan_act(struct gsm_bts_trx_ts *ts, bool activate)
{
- int ss;
- int rc = -EIO;
+ struct msgb *msg;
+ struct abis_rsl_dchan_hdr *dh;
- OSMO_ASSERT(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH);
+ msg = rsl_msgb_alloc();
+ dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
+ init_dchan_hdr(dh, activate ? RSL_MT_CHAN_ACTIV : RSL_MT_RF_CHAN_REL);
- if (ts->dyn.pchan_is != ts->dyn.pchan_want) {
- LOGP(DRSL, LOGL_ERROR,
- "%s: Attempt to switch dynamic channel to %s,"
- " but is already in switchover.\n",
- gsm_ts_and_pchan_name(ts),
- gsm_pchan_name(to_pchan));
- return ts->dyn.pchan_want == to_pchan? 0 : -EAGAIN;
- }
-
- if (ts->dyn.pchan_is == to_pchan) {
- LOGP(DRSL, LOGL_INFO,
- "%s %s Already is in %s mode, cannot switchover.\n",
- gsm_ts_name(ts), gsm_pchan_name(ts->pchan),
- gsm_pchan_name(to_pchan));
- return -EINVAL;
- }
+ dh->chan_nr = RSL_CHAN_OSMO_PDCH | (ts->nr & ~RSL_CHAN_NR_MASK);
- /* Paranoia: let's make sure all is indeed released. */
- for (ss = 0; ss < ts_subslots(ts); ss++) {
- struct gsm_lchan *lc = &ts->lchan[ss];
- if (lc->state != LCHAN_S_NONE) {
- LOGP(DRSL, LOGL_ERROR,
- "%s Attempt to switch dynamic channel to %s,"
- " but is not fully released.\n",
- gsm_ts_and_pchan_name(ts),
- gsm_pchan_name(to_pchan));
- return -EAGAIN;
+ if (activate) {
+ msgb_tv_put(msg, RSL_IE_ACT_TYPE, RSL_ACT_OSMO_PDCH);
+
+ if (ts->trx->bts->type == GSM_BTS_TYPE_RBS2000
+ && ts->trx->bts->rbs2000.use_superchannel) {
+ const uint8_t eric_pgsl_tmr[] = { 30, 1 };
+ msgb_tv_fixed_put(msg, RSL_IE_ERIC_PGSL_TIMERS,
+ sizeof(eric_pgsl_tmr), eric_pgsl_tmr);
}
}
- if (to_pchan == GSM_PCHAN_PDCH && !check_gprs_enabled(ts))
- return -ENOTSUP;
-
- DEBUGP(DRSL, "%s starting switchover to %s\n",
- gsm_ts_and_pchan_name(ts), gsm_pchan_name(to_pchan));
+ msg->dst = ts->trx->rsl_link;
+ return abis_rsl_sendmsg(msg);
+}
- /* Record that we're busy switching. */
- ts->dyn.pchan_want = to_pchan;
+/*! Tx simplified channel (de-)activation message for non-standard ip.access dyn TS PDCH type. */
+static int send_ipacc_style_pdch_act(struct gsm_bts_trx_ts *ts, bool activate)
+{
+ struct msgb *msg = rsl_msgb_alloc();
+ struct abis_rsl_dchan_hdr *dh;
- /*
- * To switch from PDCH, we need to initiate the release from the BSC
- * side. dyn_ts_switchover_continue() will be called from
- * rsl_rx_rf_chan_rel_ack(). PDCH is always on lchan[0].
- */
- if (ts->dyn.pchan_is == GSM_PCHAN_PDCH) {
- rsl_lchan_set_state(ts->lchan, LCHAN_S_REL_REQ);
- rc = rsl_rf_chan_release(ts->lchan, 0, SACCH_NONE);
- if (rc) {
- LOGP(DRSL, LOGL_ERROR,
- "%s RSL RF Chan Release failed\n",
- gsm_ts_and_pchan_name(ts));
- return dyn_ts_switchover_failed(ts, rc);
- }
- return 0;
- }
+ dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh));
+ init_dchan_hdr(dh, activate ? RSL_MT_IPAC_PDCH_ACT : RSL_MT_IPAC_PDCH_DEACT);
+ dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN;
+ dh->chan_nr = gsm_pchan2chan_nr(GSM_PCHAN_TCH_F, ts->nr, 0);
- /*
- * To switch from TCH/F and TCH/H pchans, this has been called from
- * rsl_rx_rf_chan_rel_ack(), i.e. release is complete. Go ahead and
- * activate as new type. This will always be PDCH.
- */
- return dyn_ts_switchover_continue(ts);
+ msg->dst = ts->trx->rsl_link;
+ return abis_rsl_sendmsg(msg);
}
-static int dyn_ts_switchover_continue(struct gsm_bts_trx_ts *ts)
+int rsl_tx_dyn_ts_pdch_act_deact(struct gsm_bts_trx_ts *ts, bool activate)
{
int rc;
- uint8_t act_type;
- uint8_t ho_ref;
- int ss;
- struct gsm_lchan *lchan;
+ const char *what;
+ const char *act;
- OSMO_ASSERT(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH);
- DEBUGP(DRSL, "%s switchover: release complete,"
- " activating new pchan type\n",
- gsm_ts_and_pchan_name(ts));
-
- if (ts->dyn.pchan_is == ts->dyn.pchan_want) {
- LOGP(DRSL, LOGL_ERROR,
- "%s Requested to switchover dynamic channel to the"
- " same type it is already in.\n",
- gsm_ts_and_pchan_name(ts));
- return 0;
- }
+ switch (ts->pchan_on_init) {
+ case GSM_PCHAN_TCH_F_TCH_H_PDCH:
+ what = "Osmocom dyn TS";
+ act = activate? "PDCH Chan Activ" : "PDCH Chan RF Release";
- for (ss = 0; ss < ts_subslots(ts); ss++) {
- lchan = &ts->lchan[ss];
- if (lchan->rqd_ref) {
- LOGP(DRSL, LOGL_ERROR,
- "%s During dyn TS switchover, expecting no"
- " Request Reference to be pending. Discarding!\n",
- gsm_lchan_name(lchan));
- talloc_free(lchan->rqd_ref);
- lchan->rqd_ref = NULL;
- }
- }
+ rc = send_osmocom_style_pdch_chan_act(ts, activate);
+ break;
- /*
- * When switching pchan modes, all lchans are unused. So always
- * activate whatever wants to be activated on the first lchan. (We
- * wouldn't remember to use lchan[1] across e.g. a PDCH deact anyway)
- */
- lchan = ts->lchan;
+ case GSM_PCHAN_TCH_F_PDCH:
+ what = "ip.access dyn TS";
+ act = activate? "PDCH ACT" : "PDCH DEACT";
- /*
- * For TCH/x, the lchan->type has been set in lchan_alloc(), but it may
- * have been lost during channel release due to dynamic switchover.
- *
- * For PDCH, the lchan->type will actually remain NONE.
- * TODO: set GSM_LCHAN_PDTCH?
- */
- switch (ts->dyn.pchan_want) {
- case GSM_PCHAN_TCH_F:
- lchan->type = GSM_LCHAN_TCH_F;
- break;
- case GSM_PCHAN_TCH_H:
- lchan->type = GSM_LCHAN_TCH_H;
- break;
- case GSM_PCHAN_PDCH:
- lchan->type = GSM_LCHAN_NONE;
+ rc = send_ipacc_style_pdch_act(ts, activate);
break;
+
default:
- LOGP(DRSL, LOGL_ERROR,
- "%s Invalid target pchan for dynamic TS\n",
- gsm_ts_and_pchan_name(ts));
- }
-
- act_type = (ts->dyn.pchan_want == GSM_PCHAN_PDCH)
- ? RSL_ACT_OSMO_PDCH
- : lchan->dyn.act_type;
- ho_ref = (ts->dyn.pchan_want == GSM_PCHAN_PDCH)
- ? 0
- : lchan->dyn.ho_ref;
-
- /* Fetch the rqd_ref back from before switchover started. */
- lchan->rqd_ref = lchan->dyn.rqd_ref;
- lchan->rqd_ta = lchan->dyn.rqd_ta;
- lchan->dyn.rqd_ref = NULL;
- lchan->dyn.rqd_ta = 0;
-
- /* During switchover, we have received a release ack, which means that
- * the act_timer has been stopped. Start the timer again so we mark
- * this channel broken if the activation ack comes too late. */
- osmo_timer_setup(&lchan->act_timer, lchan_act_tmr_cb, lchan);
- osmo_timer_schedule(&lchan->act_timer, 4, 0);
-
- rc = rsl_chan_activate_lchan(lchan, act_type, ho_ref);
- if (rc) {
- LOGP(DRSL, LOGL_ERROR,
- "%s RSL Chan Activate failed\n",
- gsm_ts_and_pchan_name(ts));
- return dyn_ts_switchover_failed(ts, rc);
+ what = "static timeslot";
+ act = activate? "dynamic PDCH activation" : "dynamic PDCH deactivation";
+ rc = -EINVAL;
+ break;
}
- return 0;
-}
-static int dyn_ts_switchover_failed(struct gsm_bts_trx_ts *ts, int rc)
-{
- ts->dyn.pchan_want = ts->dyn.pchan_is;
- LOGP(DRSL, LOGL_ERROR, "%s Error %d during dynamic channel switchover."
- " Going back to previous pchan.\n", gsm_ts_and_pchan_name(ts),
- rc);
+ if (rc)
+ LOG_TS(ts, LOGL_ERROR, "Tx FAILED: %s: %s: %d (%s)",
+ what, act, rc, strerror(-rc));
+ else
+ LOG_TS(ts, LOGL_DEBUG, "Tx: %s: %s", what, act);
return rc;
}
-static void dyn_ts_switchover_complete(struct gsm_lchan *lchan)
-{
- enum gsm_phys_chan_config pchan_act;
- enum gsm_phys_chan_config pchan_was;
- struct gsm_bts_trx_ts *ts = lchan->ts;
-
- OSMO_ASSERT(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH);
-
- pchan_act = pchan_for_lchant(lchan->type);
- /*
- * Paranoia: do the types match?
- * In case of errors: we've received an act ack already, so what to do
- * about it? Logging the error should suffice for now.
- */
- if (pchan_act != ts->dyn.pchan_want)
- LOGP(DRSL, LOGL_ERROR,
- "%s Requested transition does not match lchan type %s\n",
- gsm_ts_and_pchan_name(ts),
- gsm_lchant_name(lchan->type));
-
- pchan_was = ts->dyn.pchan_is;
- ts->dyn.pchan_is = ts->dyn.pchan_want = pchan_act;
-
- if (pchan_was != ts->dyn.pchan_is)
- LOGP(DRSL, LOGL_INFO, "%s switchover from %s complete.\n",
- gsm_ts_and_pchan_name(ts), gsm_pchan_name(pchan_was));
-}
-
/* Entry-point where L2 RSL from BTS enters */
int abis_rsl_rcvmsg(struct msgb *msg)
{
@@ -2968,67 +2209,411 @@ int rsl_bs_power_control(struct gsm_bts_trx *trx, uint8_t channel, uint8_t reduc
return abis_rsl_sendmsg(msg);
}
-/**
- * Release all allocated SAPIs starting from @param start and
- * release them with the given release mode. Once the release
- * confirmation arrives it will be attempted to release the
- * the RF channel.
- */
-int rsl_release_sapis_from(struct gsm_lchan *lchan, int start,
- enum rsl_rel_mode release_mode)
+static void handle_classmark_chg(struct gsm_subscriber_connection *conn,
+ struct msgb *msg)
{
- int no_sapi = 1;
- int sapi;
+ struct gsm48_hdr *gh = msgb_l3(msg);
+ unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
+ uint8_t cm2_len, cm3_len = 0;
+ uint8_t *cm2, *cm3 = NULL;
+
+ LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, "CLASSMARK CHANGE ");
- for (sapi = start; sapi < ARRAY_SIZE(lchan->sapis); ++sapi) {
- uint8_t link_id;
- if (lchan->sapis[sapi] == LCHAN_SAPI_UNUSED)
- continue;
+ /* classmark 2 */
+ cm2_len = gh->data[0];
+ cm2 = &gh->data[1];
+ DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
- link_id = sapi;
- if (lchan->type == GSM_LCHAN_TCH_F || lchan->type == GSM_LCHAN_TCH_H)
- link_id |= 0x40;
- rsl_release_request(lchan, link_id, release_mode);
- no_sapi = 0;
+ if (payload_len > cm2_len + 1) {
+ /* we must have a classmark3 */
+ if (gh->data[cm2_len+1] != 0x20) {
+ DEBUGPC(DRR, "ERR CM3 TAG\n");
+ return;
+ }
+ if (cm2_len > 3) {
+ DEBUGPC(DRR, "CM2 too long!\n");
+ return;
+ }
+
+ cm3_len = gh->data[cm2_len+2];
+ cm3 = &gh->data[cm2_len+3];
+ if (cm3_len > 14) {
+ DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
+ return;
+ }
+ DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
}
+ bsc_cm_update(conn, cm2, cm2_len, cm3, cm3_len);
+}
+
+/* 9.1.29 RR Status */
+struct msgb *gsm48_create_rr_status(uint8_t cause)
+{
+ struct msgb *msg;
+ struct gsm48_hdr *gh;
- return no_sapi;
+ msg = gsm48_msgb_alloc_name("GSM 04.08 RR STATUS");
+ if (!msg)
+ return NULL;
+
+ gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
+ gh->proto_discr = GSM48_PDISC_RR;
+ gh->msg_type = GSM48_MT_RR_STATUS;
+ gh->data[0] = cause;
+
+ return msg;
}
-int rsl_start_t3109(struct gsm_lchan *lchan)
+/* 9.1.29 RR Status */
+int gsm48_tx_rr_status(struct gsm_subscriber_connection *conn, uint8_t cause)
{
- struct gsm_bts *bts = lchan->ts->trx->bts;
+ struct msgb *msg = gsm48_create_rr_status(cause);
+ if (!msg)
+ return -1;
+ return gsm0808_submit_dtap(conn, msg, 0, 0);
+}
- osmo_timer_setup(&lchan->T3109, t3109_expired, lchan);
- osmo_timer_schedule(&lchan->T3109, bts->network->T3109, 0);
- return 0;
+static void dispatch_dtap(struct gsm_subscriber_connection *conn,
+ uint8_t link_id, struct msgb *msg)
+{
+ struct gsm48_hdr *gh;
+ uint8_t pdisc;
+ uint8_t msg_type;
+ int rc;
+
+ if (msgb_l3len(msg) < sizeof(*gh)) {
+ LOGP(DMSC, LOGL_ERROR, "(%s) Message too short for a GSM48 header.\n",
+ bsc_subscr_name(conn->bsub));
+ return;
+ }
+
+ gh = msgb_l3(msg);
+ pdisc = gsm48_hdr_pdisc(gh);
+ msg_type = gsm48_hdr_msg_type(gh);
+
+ /* the idea is to handle all RR messages here, and only hand
+ * MM/CC/SMS-CP/LCS up to the MSC. Some messages like PAGING
+ * RESPONSE or CM SERVICE REQUEST will not be covered here, as
+ * they are only possible in the first L3 message of each L2
+ * channel, i.e. 'conn' will not exist and gsm0408_rcvmsg()
+ * will call api->compl_l3() for it */
+ switch (pdisc) {
+ case GSM48_PDISC_RR:
+ switch (msg_type) {
+ case GSM48_MT_RR_GPRS_SUSP_REQ:
+ LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG,
+ "%s\n", gsm48_rr_msg_name(GSM48_MT_RR_GPRS_SUSP_REQ));
+ break;
+ case GSM48_MT_RR_STATUS:
+ LOGPLCHAN(msg->lchan, DRR, LOGL_NOTICE,
+ "%s (cause: %s)\n", gsm48_rr_msg_name(GSM48_MT_RR_STATUS),
+ rr_cause_name(gh->data[0]));
+ break;
+ case GSM48_MT_RR_MEAS_REP:
+ /* This shouldn't actually end up here, as RSL treats
+ * L3 Info of 08.58 MEASUREMENT REPORT different by calling
+ * directly into gsm48_parse_meas_rep */
+ LOGPLCHAN(msg->lchan, DMEAS, LOGL_ERROR,
+ "DIRECT GSM48 MEASUREMENT REPORT ?!?\n");
+ gsm48_tx_rr_status(conn, GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT);
+ break;
+ case GSM48_MT_RR_HANDO_COMPL:
+ /* Chapter 9.1.16 Handover complete */
+ if (!conn->ho.fi)
+ LOG_LCHAN(msg->lchan, LOGL_ERROR,
+ "Rx RR Handover Complete, but no handover is ongoing\n");
+ else
+ osmo_fsm_inst_dispatch(conn->ho.fi, HO_EV_RR_HO_COMPLETE, msg);
+ break;
+ case GSM48_MT_RR_HANDO_FAIL:
+ /* Chapter 9.1.17 Handover Failure */
+ if (!conn->ho.fi)
+ LOG_LCHAN(msg->lchan, LOGL_ERROR,
+ "Rx RR Handover Fail, but no handover is ongoing\n");
+ else
+ osmo_fsm_inst_dispatch(conn->ho.fi, HO_EV_RR_HO_FAIL, msg);
+ break;
+ case GSM48_MT_RR_CIPH_M_COMPL:
+ bsc_cipher_mode_compl(conn, msg, conn->lchan->encr.alg_id);
+ break;
+ case GSM48_MT_RR_ASS_COMPL:
+ if (conn->assignment.fi)
+ osmo_fsm_inst_dispatch(conn->assignment.fi,
+ ASSIGNMENT_EV_RR_ASSIGNMENT_COMPLETE, msg);
+ else
+ LOGPLCHAN(msg->lchan, DRR, LOGL_ERROR,
+ "Rx RR Assignment Complete, but no assignment is ongoing\n");
+ break;
+ case GSM48_MT_RR_ASS_FAIL:
+ if (conn->assignment.fi)
+ osmo_fsm_inst_dispatch(conn->assignment.fi,
+ ASSIGNMENT_EV_RR_ASSIGNMENT_FAIL, msg);
+ else
+ LOGPLCHAN(msg->lchan, DRR, LOGL_ERROR,
+ "Rx RR Assignment Failure, but no assignment is ongoing\n");
+ break;
+ case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
+ rc = gsm48_rx_rr_modif_ack(msg);
+ if (rc < 0)
+ osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_CHAN_MODE_MODIF_ERROR, &rc);
+ else
+ osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_CHAN_MODE_MODIF_ACK, msg);
+ break;
+ case GSM48_MT_RR_CLSM_CHG:
+ handle_classmark_chg(conn, msg);
+ break;
+ case GSM48_MT_RR_APP_INFO:
+ /* Passing RR APP INFO to MSC, not quite
+ * according to spec */
+ bsc_dtap(conn, link_id, msg);
+ break;
+ default:
+ /* Drop unknown RR message */
+ LOGPLCHAN(msg->lchan, DRR, LOGL_NOTICE,
+ "Dropping %s 04.08 RR message\n", gsm48_rr_msg_name(msg_type));
+ gsm48_tx_rr_status(conn, GSM48_RR_CAUSE_MSG_TYPE_N);
+ break;
+ }
+ break;
+ default:
+ bsc_dtap(conn, link_id, msg);
+ break;
+ }
}
-/**
- * \brief directly RF Channel Release the lchan
- *
- * When no SAPI was allocated, directly release the logical channel. This
- * should only be called from chan_alloc.c on channel release handling. In
- * case no SAPI was established the RF Channel can be directly released,
- */
-int rsl_direct_rf_release(struct gsm_lchan *lchan)
+/*! RSL has received a DATA INDICATION with L3 from MS */
+int gsm0408_rcvmsg(struct msgb *msg, uint8_t link_id)
{
- int i;
- for (i = 0; i < ARRAY_SIZE(lchan->sapis); ++i) {
- if (lchan->sapis[i] != LCHAN_SAPI_UNUSED) {
- LOGP(DRSL, LOGL_ERROR, "%s SAPI(%d) still allocated.\n",
- gsm_lchan_name(lchan), i);
+ struct gsm_lchan *lchan;
+
+ lchan = msg->lchan;
+ if (!lchan->fi) {
+ LOGP(DRSL, LOGL_INFO, "%s: Got data for uninitialized lchan, discarding.\n",
+ gsm_lchan_name(lchan));
+ return -1;
+ }
+
+ if (!lchan_may_receive_data(lchan)) {
+ LOG_LCHAN(msg->lchan, LOGL_INFO, "Got data in non active state, discarding.\n");
+ return -1;
+ }
+
+ if (lchan->conn) {
+ /* if we already have a connection, forward via DTAP to
+ * MSC */
+ dispatch_dtap(lchan->conn, link_id, msg);
+ } else {
+ /* allocate a new connection */
+ lchan->conn = bsc_subscr_con_allocate(msg->lchan->ts->trx->bts->network);
+ if (!lchan->conn) {
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
return -1;
}
+ lchan->conn->lchan = lchan;
+
+ /* fwd via bsc_api to send COMPLETE L3 INFO to MSC */
+ bsc_compl_l3(lchan->conn, msg, 0);
+ /* conn shall release lchan on teardown, also if this Layer 3 Complete is rejected. */
+ }
+
+ return 0;
+}
+
+int send_siemens_mrpci(struct gsm_lchan *lchan,
+ uint8_t *classmark2_lv)
+{
+ struct rsl_mrpci mrpci;
+
+ if (classmark2_lv[0] < 2)
+ return -EINVAL;
+
+ mrpci.power_class = classmark2_lv[1] & 0x7;
+ mrpci.vgcs_capable = classmark2_lv[2] & (1 << 1);
+ mrpci.vbs_capable = classmark2_lv[2] & (1 <<2);
+ mrpci.gsm_phase = (classmark2_lv[1]) >> 5 & 0x3;
+
+ return rsl_siemens_mrpci(lchan, &mrpci);
+}
+
+int gsm48_rx_rr_modif_ack(struct msgb *msg)
+{
+ int rc;
+ struct gsm48_hdr *gh = msgb_l3(msg);
+ struct gsm48_chan_mode_modify *mod =
+ (struct gsm48_chan_mode_modify *) gh->data;
+
+ DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
+
+ if (mod->mode != msg->lchan->tch_mode) {
+ LOGP(DRR, LOGL_ERROR, "CHANNEL MODE change failed. Wanted: %d Got: %d\n",
+ msg->lchan->tch_mode, mod->mode);
+ return -1;
+ }
+
+ /* update the channel type */
+ switch (mod->mode) {
+ case GSM48_CMODE_SIGN:
+ msg->lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
+ break;
+ case GSM48_CMODE_SPEECH_V1:
+ case GSM48_CMODE_SPEECH_EFR:
+ case GSM48_CMODE_SPEECH_AMR:
+ msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
+ break;
+ case GSM48_CMODE_DATA_14k5:
+ case GSM48_CMODE_DATA_12k0:
+ case GSM48_CMODE_DATA_6k0:
+ case GSM48_CMODE_DATA_3k6:
+ msg->lchan->rsl_cmode = RSL_CMOD_SPD_DATA;
+ break;
}
- /* Now release it */
- return rsl_rf_chan_release(lchan, 0, SACCH_NONE);
+ /* We've successfully modified the MS side of the channel,
+ * now go on to modify the BTS side of the channel */
+ rc = rsl_chan_mode_modify_req(msg->lchan);
+
+ /* FIXME: we not only need to do this after mode modify, but
+ * also after channel activation */
+ if (is_ipaccess_bts(msg->lchan->ts->trx->bts) && mod->mode != GSM48_CMODE_SIGN)
+ rsl_tx_ipacc_crcx(msg->lchan);
+ return rc;
+}
+
+int gsm48_sendmsg(struct msgb *msg)
+{
+ if (msg->lchan)
+ msg->dst = msg->lchan->ts->trx->rsl_link;
+
+ msg->l3h = msg->data;
+ return rsl_data_request(msg, 0);
+}
+
+/* 7.1.7 and 9.1.7: RR CHANnel RELease */
+int gsm48_send_rr_release(struct gsm_lchan *lchan)
+{
+ struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 RR REL");
+ struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
+ uint8_t *cause;
+
+ msg->lchan = lchan;
+ gh->proto_discr = GSM48_PDISC_RR;
+ gh->msg_type = GSM48_MT_RR_CHAN_REL;
+
+ cause = msgb_put(msg, 1);
+ cause[0] = GSM48_RR_CAUSE_NORMAL;
+
+ DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
+ lchan->nr, lchan->type);
+
+ /* Send actual release request to MS */
+ return gsm48_sendmsg(msg);
}
-/* Initial timeslot actions when a timeslot first comes into operation. */
-bool on_gsm_ts_init(struct gsm_bts_trx_ts *ts)
+/* Chapter 9.1.9: Ciphering Mode Command */
+int gsm48_send_rr_ciph_mode(struct gsm_lchan *lchan, int want_imeisv)
{
- dyn_ts_init(ts);
- return true;
+ struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CIPH");
+ struct gsm48_hdr *gh;
+ uint8_t ciph_mod_set;
+
+ msg->lchan = lchan;
+
+ DEBUGP(DRR, "TX CIPHERING MODE CMD\n");
+
+ if (lchan->encr.alg_id <= RSL_ENC_ALG_A5(0))
+ ciph_mod_set = 0;
+ else
+ ciph_mod_set = (lchan->encr.alg_id-2)<<1 | 1;
+
+ gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
+ gh->proto_discr = GSM48_PDISC_RR;
+ gh->msg_type = GSM48_MT_RR_CIPH_M_CMD;
+ gh->data[0] = (want_imeisv & 0x1) << 4 | (ciph_mod_set & 0xf);
+
+ return rsl_encryption_cmd(msg);
}
+
+int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan,
+ uint8_t power_command, uint8_t ho_ref)
+{
+ struct msgb *msg = gsm48_make_ho_cmd(new_lchan, power_command, ho_ref);
+ if (!msg)
+ return -EINVAL;
+ msg->lchan = old_lchan;
+ return gsm48_sendmsg(msg);
+}
+
+static void mr_config_for_ms(struct gsm_lchan *lchan, struct msgb *msg)
+{
+ if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR)
+ msgb_tlv_put(msg, GSM48_IE_MUL_RATE_CFG, lchan->mr_ms_lv[0],
+ lchan->mr_ms_lv + 1);
+}
+
+/* Chapter 9.1.2: Assignment Command */
+int gsm48_send_rr_ass_cmd(struct gsm_lchan *dest_lchan, struct gsm_lchan *lchan, uint8_t power_command)
+{
+ struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ASS CMD");
+ struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
+ struct gsm48_ass_cmd *ass =
+ (struct gsm48_ass_cmd *) msgb_put(msg, sizeof(*ass));
+
+ DEBUGP(DRR, "-> ASSIGNMENT COMMAND tch_mode=0x%02x\n", lchan->tch_mode);
+
+ msg->lchan = dest_lchan;
+ gh->proto_discr = GSM48_PDISC_RR;
+ gh->msg_type = GSM48_MT_RR_ASS_CMD;
+
+ /*
+ * fill the channel information element, this code
+ * should probably be shared with rsl_rx_chan_rqd(),
+ * gsm48_lchan_modify(). But beware that 10.5.2.5
+ * 10.5.2.5.a have slightly different semantic for
+ * the chan_desc. But as long as multi-slot configurations
+ * are not used we seem to be fine.
+ */
+ gsm48_lchan2chan_desc(&ass->chan_desc, lchan);
+ ass->power_command = power_command;
+
+ /* optional: cell channel description */
+
+ msgb_tv_put(msg, GSM48_IE_CHANMODE_1, lchan->tch_mode);
+
+ /* mobile allocation in case of hopping */
+ if (lchan->ts->hopping.enabled) {
+ msgb_tlv_put(msg, GSM48_IE_MA_BEFORE, lchan->ts->hopping.ma_len,
+ lchan->ts->hopping.ma_data);
+ }
+
+ /* in case of multi rate we need to attach a config */
+ mr_config_for_ms(lchan, msg);
+
+ return gsm48_sendmsg(msg);
+}
+
+/* 9.1.5 Channel mode modify: Modify the mode on the MS side */
+int gsm48_lchan_modify(struct gsm_lchan *lchan, uint8_t mode)
+{
+ struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CHN MOD");
+ struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
+ struct gsm48_chan_mode_modify *cmm =
+ (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
+
+ DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
+
+ lchan->tch_mode = mode;
+ msg->lchan = lchan;
+ gh->proto_discr = GSM48_PDISC_RR;
+ gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
+
+ /* fill the channel information element, this code
+ * should probably be shared with rsl_rx_chan_rqd() */
+ gsm48_lchan2chan_desc(&cmm->chan_desc, lchan);
+ cmm->mode = mode;
+
+ /* in case of multi rate we need to attach a config */
+ mr_config_for_ms(lchan, msg);
+
+ return gsm48_sendmsg(msg);
+}
+
diff --git a/src/osmo-bsc/assignment_fsm.c b/src/osmo-bsc/assignment_fsm.c
new file mode 100644
index 000000000..64d23bb54
--- /dev/null
+++ b/src/osmo-bsc/assignment_fsm.c
@@ -0,0 +1,619 @@
+/* osmo-bsc BSSMAP Assignment procedure implementation.
+ *
+ * (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * Author: Neels Hofmeyr <neels@hofmeyr.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/gsm/gsm0808.h>
+
+#include <osmocom/mgcp_client/mgcp_client_fsm.h>
+
+#include <osmocom/bsc/debug.h>
+#include <osmocom/bsc/gsm_data.h>
+#include <osmocom/bsc/gsm_timers.h>
+#include <osmocom/bsc/lchan_fsm.h>
+#include <osmocom/bsc/mgw_endpoint_fsm.h>
+#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
+#include <osmocom/bsc/osmo_bsc_lcls.h>
+#include <osmocom/bsc/bsc_msc_data.h>
+#include <osmocom/bsc/bsc_api.h>
+#include <osmocom/bsc/lchan_select.h>
+#include <osmocom/bsc/abis_rsl.h>
+
+#include <osmocom/bsc/assignment_fsm.h>
+
+static struct osmo_fsm assignment_fsm;
+
+/* From local var fi->priv, define local var conn. */
+#define GET_CONN() \
+ struct gsm_subscriber_connection *conn = fi->priv; \
+ OSMO_ASSERT((fi)->fsm == &assignment_fsm && (fi)->priv)
+
+/*
+assignment_st_requested#ASSIGNMENT_ST_REQUESTED
+assignment_st_wait_lchan#ASSIGNMENT_ST_WAIT_LCHAN_ACTIVE
+assignment_st_wait_mgw_endpoint_to_msc#ASSIGNMENT_ST_WAIT_MGW_ENDPOINT_TO_MSC
+assignment_st_wait_ass_complete#ASSIGNMENT_ST_WAIT_RR_ASS_COMPLETE
+assignment_st_success#ASSIGNMENT_ST_SUCCESS
+assignment_st_failure#ASSIGNMENT_ST_FAILURE
+
+:s/assignment_st_\(.*\)#\(ASSIGNMENT_ST_\(.*\)\)/static void assignment_fsm_\1_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)\r{\r\tGET_CONN();\r}\r\rstatic void assignment_fsm_\1(struct osmo_fsm_inst *fi, uint32_t event, void *data)\r{\r\tGET_CONN();\r\tswitch (event) {\r\tdefault:\r\t\tOSMO_ASSERT(false);\r\t}\r}\r
+
+
+
+:s/assignment_st_\(.*\)#\(ASSIGNMENT_ST_\(.*\)\)/\t[\2] = {\r\t\t.name = "\3",\r\t\t.onenter = assignment_fsm_\1_onenter,\r\t\t.action = assignment_fsm_\1,\r\t\t.in_event_mask = 0\r\t\t\t| S(ASSIGNMENT_EV_CONN_RELEASING)\r\t\t\t,\r\t\t.out_state_mask = 0\r\t\t\t| S(ASSIGNMENT_ST_FAILURE)\r\t\t\t,\r\t},
+
+
+*/
+
+
+struct state_timeout assignment_fsm_timeouts[32] = {
+ [ASSIGNMENT_ST_WAIT_LCHAN_ACTIVE] = { .T=10 },
+ [ASSIGNMENT_ST_WAIT_RR_ASS_COMPLETE] = { .keep_timer=true },
+ [ASSIGNMENT_ST_WAIT_LCHAN_ESTABLISHED] = { .keep_timer=true },
+ [ASSIGNMENT_ST_WAIT_MGW_ENDPOINT_TO_MSC] = { .T=23042 },
+};
+
+/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
+ * The actual timeout value is in turn obtained from network->T_defs.
+ * Assumes local variable fi exists. */
+#define assignment_fsm_state_chg(state) \
+ fsm_inst_state_chg_T(fi, state, \
+ assignment_fsm_timeouts, \
+ ((struct gsm_subscriber_connection*)(fi->priv))->network->T_defs, \
+ 5)
+
+/* Log failure and transition to ASSIGNMENT_ST_FAILURE, which triggers the appropriate actions. */
+#define assignment_fail(cause, fmt, args...) do { \
+ struct gsm_subscriber_connection *_conn = fi->priv; \
+ _conn->assignment.failure_cause = cause; \
+ LOG_ASSIGNMENT(_conn, LOGL_ERROR, "Assignment failed in state %s, cause %s: " fmt, \
+ osmo_fsm_inst_state_name(fi), gsm0808_cause_name(cause), ## args); \
+ on_assignment_failure(_conn); \
+ } while(0)
+
+void assignment_reset(struct gsm_subscriber_connection *conn)
+{
+ if (conn->assignment.new_lchan) {
+ struct gsm_lchan *lchan = conn->assignment.new_lchan;
+ conn->assignment.new_lchan = NULL;
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ }
+
+ if (conn->assignment.created_ci_for_msc)
+ mgw_endpoint_ci_dlcx(conn->assignment.created_ci_for_msc);
+
+ conn->assignment = (struct assignment_fsm_data){
+ .fi = conn->assignment.fi, /* The FSM shall clear itself when it's done. */
+ };
+}
+
+static void on_assignment_failure(struct gsm_subscriber_connection *conn)
+{
+ struct msgb *resp = gsm0808_create_assignment_failure(conn->assignment.failure_cause, NULL);
+
+ if (!resp)
+ LOG_ASSIGNMENT(conn, LOGL_ERROR, "Unable to compose BSSMAP Assignment Failure message");
+ else
+ gscon_sigtran_send(conn, resp);
+
+ /* If assignment failed as early as in assignment_fsm_start(), there may not be an fi yet. */
+ if (conn->assignment.fi) {
+ LOG_ASSIGNMENT(conn, LOGL_ERROR, "Assignment failed");
+ osmo_fsm_inst_term(conn->assignment.fi, OSMO_FSM_TERM_ERROR, 0);
+ }
+}
+
+static void send_assignment_complete(struct gsm_subscriber_connection *conn, struct gsm_lchan *new_lchan)
+{
+ int rc;
+ struct gsm0808_speech_codec sc;
+ struct gsm0808_speech_codec *sc_ptr = NULL;
+ struct sockaddr_storage addr_local;
+ struct sockaddr_storage *addr_local_p = NULL;
+ int perm_spch = 0;
+ uint8_t chosen_channel;
+ struct msgb *resp;
+ struct gsm_lchan *lchan = new_lchan;
+ struct osmo_fsm_inst *fi = conn->fi;
+
+ chosen_channel = gsm0808_chosen_channel(lchan->type, lchan->tch_mode);
+ if (!chosen_channel) {
+ assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE,
+ "Unable to compose Chosen Channel for mode=%s type=%s",
+ get_value_string(gsm48_chan_mode_names, lchan->tch_mode),
+ gsm_lchant_name(lchan->type));
+ return;
+ }
+
+ /* Generate voice related fields */
+ if (conn->assignment.requires_voice_stream) {
+ perm_spch = gsm0808_permitted_speech(lchan->type, lchan->tch_mode);
+
+ switch (conn->sccp.msc->a.asp_proto) {
+ case OSMO_SS7_ASP_PROT_IPA:
+ /* don't add any AoIP specific fields. CIC allocated by MSC */
+ break;
+ default:
+ if (!mgwep_ci_get_crcx_info_to_sockaddr(conn->user_plane.mgw_endpoint_ci_msc,
+ &addr_local)) {
+ assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE,
+ "Unable to compose RTP address of MGW -> MSC");
+ return;
+ }
+ addr_local_p = &addr_local;
+
+ /* Extrapolate speech codec from speech mode */
+ gsm0808_speech_codec_from_chan_type(&sc, perm_spch);
+ sc_ptr = &sc;
+ break;
+ }
+ /* FIXME: AMR codec configuration must be derived from lchan1! */
+ }
+
+ gsm0808_speech_codec_from_chan_type(&sc, perm_spch);
+
+ resp = gsm0808_create_ass_compl(lchan->abis_ip.ass_compl.rr_cause,
+ chosen_channel,
+ lchan->encr.alg_id, perm_spch,
+ addr_local_p, sc_ptr, NULL);
+
+ if (!resp) {
+ assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE,
+ "Unable to compose Assignment Complete message");
+ return;
+ }
+
+ /* Add LCLS BSS-Status IE in case there is any LCLS status for this connection */
+ bssmap_add_lcls_status_if_needed(conn, resp);
+
+ rc = gscon_sigtran_send(conn, resp);
+ if (rc) {
+ assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE,
+ "Unable send Assignment Complete message: rc=%d %s",
+ rc, strerror(-rc));
+ return;
+ }
+}
+
+static void assignment_success(struct gsm_subscriber_connection *conn)
+{
+ send_assignment_complete(conn, conn->assignment.new_lchan);
+ /* If something went wrong during send_assignment_complete(), the fi will be gone from
+ * error handling in there. Almost a success, but then again the whole thing failed. */
+ if (!conn->assignment.fi)
+ return;
+
+ /* Take on the new lchan */
+ gscon_change_primary_lchan(conn, &conn->assignment.new_lchan);
+
+ /* Only remembered this for error handling */
+ conn->assignment.created_ci_for_msc = NULL;
+
+ /* New RTP information is now accepted */
+ osmo_strlcpy(conn->user_plane.msc_assigned_rtp_addr, conn->assignment.req.msc_rtp_addr,
+ sizeof(conn->user_plane.msc_assigned_rtp_addr));
+ conn->user_plane.msc_assigned_rtp_port = conn->assignment.req.msc_rtp_port;
+
+ /* apply LCLS configuration (if any) */
+ lcls_apply_config(conn);
+
+ LOG_ASSIGNMENT(conn, LOGL_DEBUG, "Assignment successful");
+ osmo_fsm_inst_term(conn->assignment.fi, OSMO_FSM_TERM_REGULAR, 0);
+}
+
+static void assignment_fsm_update_id(struct gsm_subscriber_connection *conn)
+{
+ struct gsm_lchan *new_lchan = conn->assignment.new_lchan;
+ if (!new_lchan) {
+ osmo_fsm_inst_update_id(conn->assignment.fi, conn->fi->id);
+ return;
+ }
+
+ osmo_fsm_inst_update_id_f(conn->assignment.fi, "%s_%u-%u-%u-%s%s%s-%u",
+ conn->fi->id,
+ new_lchan->ts->trx->bts->nr, new_lchan->ts->trx->nr, new_lchan->ts->nr,
+ gsm_pchan_id(new_lchan->ts->pchan_on_init),
+ (new_lchan->ts->pchan_on_init == new_lchan->ts->pchan_is)? "" : "as",
+ (new_lchan->ts->pchan_on_init == new_lchan->ts->pchan_is)? ""
+ : gsm_pchan_id(new_lchan->ts->pchan_is),
+ new_lchan->nr);
+}
+
+static bool lchan_type_compat_with_mode(enum gsm_chan_t type,
+ enum gsm48_chan_mode chan_mode, int full_rate)
+{
+ switch (chan_mode) {
+ case GSM48_CMODE_SIGN:
+ switch (type) {
+ case GSM_LCHAN_TCH_F:
+ case GSM_LCHAN_TCH_H:
+ case GSM_LCHAN_SDCCH:
+ return true;
+ default:
+ return false;
+ }
+
+ case GSM48_CMODE_SPEECH_V1:
+ case GSM48_CMODE_SPEECH_AMR:
+ case GSM48_CMODE_DATA_3k6:
+ case GSM48_CMODE_DATA_6k0:
+ /* these services can all run on TCH/H, but we may have
+ * an explicit override by the 'full_rate' argument */
+ switch (type) {
+ case GSM_LCHAN_TCH_F:
+ return full_rate;
+ case GSM_LCHAN_TCH_H:
+ return !full_rate;
+ default:
+ return false;
+ }
+
+ case GSM48_CMODE_DATA_12k0:
+ case GSM48_CMODE_DATA_14k5:
+ case GSM48_CMODE_SPEECH_EFR:
+ /* these services all explicitly require a TCH/F */
+ return type == GSM_LCHAN_TCH_F;
+
+ default:
+ return false;
+ }
+}
+
+void assignment_fsm_start(struct gsm_subscriber_connection *conn, struct gsm_bts *bts,
+ struct assignment_request *req)
+{
+ struct osmo_fsm_inst *fi;
+
+ static bool g_initialized = false;
+ if (!g_initialized) {
+ OSMO_ASSERT(osmo_fsm_register(&assignment_fsm) == 0);
+ g_initialized = true;
+ }
+ OSMO_ASSERT(conn);
+ OSMO_ASSERT(conn->fi);
+ OSMO_ASSERT(!conn->assignment.fi);
+ OSMO_ASSERT(!conn->assignment.new_lchan);
+
+ fi = osmo_fsm_inst_alloc_child(&assignment_fsm, conn->fi, GSCON_EV_ASSIGNMENT_END);
+ OSMO_ASSERT(fi);
+ conn->assignment.fi = fi;
+ fi->priv = conn;
+
+ conn->assignment.req = *req;
+
+ switch (req->chan_mode) {
+
+ case GSM48_CMODE_SPEECH_V1:
+ case GSM48_CMODE_SPEECH_EFR:
+ case GSM48_CMODE_SPEECH_AMR:
+ conn->assignment.requires_voice_stream = true;
+ /* Select an lchan below. */
+ break;
+
+ case GSM48_CMODE_SIGN:
+ conn->assignment.requires_voice_stream = false;
+ /* Select an lchan below. */
+ break;
+
+ default:
+ assignment_fail(GSM0808_CAUSE_REQ_CODEC_TYPE_OR_CONFIG_NOT_SUPP,
+ "Channel mode not supported: %s",
+ gsm48_chan_mode_name(req->chan_mode));
+ return;
+ }
+
+ if (conn->lchan
+ && lchan_type_compat_with_mode(conn->lchan->type, req->chan_mode, req->full_rate)) {
+
+ if (conn->lchan->tch_mode == req->chan_mode) {
+ /* current lchan suffices and already is in the right mode. We're done. */
+ LOG_ASSIGNMENT(conn, LOGL_DEBUG,
+ "Current lchan is compatible with requested chan_mode,"
+ " sending BSSMAP Assignment Complete directly."
+ " requested chan_mode=%s; current lchan is %s",
+ gsm48_chan_mode_name(req->chan_mode),
+ gsm_lchan_name(conn->lchan));
+ send_assignment_complete(conn, conn->lchan);
+ return;
+ }
+
+ /* FIXME */
+ LOG_ASSIGNMENT(conn, LOGL_ERROR,
+ "NOT IMPLEMENTED:"
+ " Current lchan would be compatible, we should send Channel Mode Modify");
+#if 0
+ case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
+ rc = gsm48_rx_rr_modif_ack(msg);
+ if (rc < 0)
+ bsc_assign_fail(conn, GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, NULL);
+ else
+ bsc_assign_compl(conn, 0);
+ break;
+#endif
+ }
+
+ conn->assignment.new_lchan = lchan_select_by_chan_mode(bts, req->chan_mode, req->full_rate);
+
+ if (!conn->assignment.new_lchan) {
+ assignment_fail(GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE,
+ "BSSMAP Assignment Command:"
+ " No lchan available for: chan_mode=%s, full_rate=%i\n",
+ get_value_string(gsm48_chan_mode_names, req->chan_mode), req->full_rate);
+ return;
+ }
+
+ assignment_fsm_update_id(conn);
+ LOG_ASSIGNMENT(conn, LOGL_INFO, "Starting Assignment: chan_mode=%s, full_rate=%d,"
+ " aoip=%s MSC-rtp=%s:%u",
+ gsm48_chan_mode_name(req->chan_mode), req->full_rate,
+ req->aoip ? "yes" : "no", req->msc_rtp_addr, req->msc_rtp_port);
+
+ assignment_fsm_state_chg(ASSIGNMENT_ST_WAIT_LCHAN_ACTIVE);
+ {
+ struct lchan_activate_info info = {
+ .activ_for = FOR_ASSIGNMENT,
+ .for_conn = conn,
+ .chan_mode = req->chan_mode,
+ .requires_voice_stream = conn->assignment.requires_voice_stream,
+ .old_lchan = conn->lchan,
+ };
+ lchan_activate(conn->assignment.new_lchan, &info);
+ }
+}
+
+static void assignment_fsm_wait_lchan(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_CONN();
+ switch (event) {
+
+ case ASSIGNMENT_EV_LCHAN_ACTIVE:
+ if (data != conn->assignment.new_lchan)
+ return;
+
+ assignment_fsm_state_chg(ASSIGNMENT_ST_WAIT_RR_ASS_COMPLETE);
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void assignment_fsm_wait_rr_ass_complete_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int rc;
+ GET_CONN();
+
+ rc = gsm48_send_rr_ass_cmd(conn->lchan, conn->assignment.new_lchan,
+ conn->lchan->ms_power);
+
+ if (rc)
+ assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, "Unable to send RR Assignment Command");
+}
+
+static uint8_t get_cause(void *data)
+{
+ if (data)
+ return *(uint8_t*)data;
+ return GSM0808_CAUSE_EQUIPMENT_FAILURE;
+}
+
+static void assignment_fsm_wait_rr_ass_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ switch (event) {
+
+ case ASSIGNMENT_EV_RR_ASSIGNMENT_COMPLETE:
+ assignment_fsm_state_chg(ASSIGNMENT_ST_WAIT_LCHAN_ESTABLISHED);
+ return;
+
+ case ASSIGNMENT_EV_RR_ASSIGNMENT_FAIL:
+ assignment_fail(get_cause(data), "Rx RR Assignment Failure");
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void assignment_fsm_wait_lchan_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ GET_CONN();
+ /* The RR Assignment Complete counts as RLL Establish event */
+ osmo_fsm_inst_dispatch(conn->assignment.new_lchan->fi, LCHAN_EV_RLL_ESTABLISH_IND, 0);
+}
+
+static void assignment_fsm_wait_lchan_established(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_CONN();
+ switch (event) {
+
+ case ASSIGNMENT_EV_LCHAN_ESTABLISHED:
+ if (conn->assignment.requires_voice_stream)
+ assignment_fsm_state_chg(ASSIGNMENT_ST_WAIT_MGW_ENDPOINT_TO_MSC);
+ else
+ assignment_success(conn);
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void assignment_fsm_wait_mgw_endpoint_to_msc_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ GET_CONN();
+
+ OSMO_ASSERT(conn->assignment.requires_voice_stream);
+
+ LOG_ASSIGNMENT(conn, LOGL_DEBUG,
+ "Connecting MGW endpoint to the MSC's RTP port: %s:%u",
+ conn->assignment.req.msc_rtp_addr,
+ conn->assignment.req.msc_rtp_port);
+
+ if (!gscon_connect_mgw_to_msc(conn,
+ conn->assignment.req.msc_rtp_addr,
+ conn->assignment.req.msc_rtp_port,
+ fi,
+ ASSIGNMENT_EV_MSC_MGW_OK,
+ ASSIGNMENT_EV_MSC_MGW_FAIL,
+ NULL,
+ &conn->assignment.created_ci_for_msc)) {
+ assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE,
+ "Unable to connect MGW endpoint to the MSC side");
+ return;
+ }
+}
+
+static void assignment_fsm_wait_mgw_endpoint_to_msc(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ const struct mgcp_conn_peer *mgw_info;
+ GET_CONN();
+ switch (event) {
+
+ case ASSIGNMENT_EV_MSC_MGW_OK:
+ mgw_info = mgwep_ci_get_rtp_info(conn->user_plane.mgw_endpoint_ci_msc);
+ if (!mgw_info) {
+ assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE,
+ "Unable to retrieve RTP port info allocated by MGW for the MSC side.");
+ return;
+ }
+ LOG_ASSIGNMENT(conn, LOGL_DEBUG, "MGW's MSC side CI: %s:%u",
+ mgw_info->addr, mgw_info->port);
+ assignment_success(conn);
+ return;
+
+ case ASSIGNMENT_EV_MSC_MGW_FAIL:
+ assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE,
+ "Unable to connect MGW endpoint to the MSC side");
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+#define S(x) (1 << (x))
+
+static const struct osmo_fsm_state assignment_fsm_states[] = {
+ [ASSIGNMENT_ST_WAIT_LCHAN_ACTIVE] = {
+ .name = "WAIT_LCHAN_ACTIVE",
+ .action = assignment_fsm_wait_lchan,
+ .in_event_mask = 0
+ | S(ASSIGNMENT_EV_LCHAN_ACTIVE)
+ ,
+ .out_state_mask = 0
+ | S(ASSIGNMENT_ST_WAIT_LCHAN_ACTIVE)
+ | S(ASSIGNMENT_ST_WAIT_RR_ASS_COMPLETE)
+ ,
+ },
+ [ASSIGNMENT_ST_WAIT_RR_ASS_COMPLETE] = {
+ .name = "WAIT_RR_ASS_COMPLETE",
+ .onenter = assignment_fsm_wait_rr_ass_complete_onenter,
+ .action = assignment_fsm_wait_rr_ass_complete,
+ .in_event_mask = 0
+ | S(ASSIGNMENT_EV_RR_ASSIGNMENT_COMPLETE)
+ | S(ASSIGNMENT_EV_RR_ASSIGNMENT_FAIL)
+ ,
+ .out_state_mask = 0
+ | S(ASSIGNMENT_ST_WAIT_LCHAN_ESTABLISHED)
+ ,
+ },
+ [ASSIGNMENT_ST_WAIT_LCHAN_ESTABLISHED] = {
+ .name = "WAIT_LCHAN_ESTABLISHED",
+ .onenter = assignment_fsm_wait_lchan_established_onenter,
+ .action = assignment_fsm_wait_lchan_established,
+ .in_event_mask = 0
+ | S(ASSIGNMENT_EV_LCHAN_ESTABLISHED)
+ ,
+ .out_state_mask = 0
+ | S(ASSIGNMENT_ST_WAIT_MGW_ENDPOINT_TO_MSC)
+ ,
+ },
+ [ASSIGNMENT_ST_WAIT_MGW_ENDPOINT_TO_MSC] = {
+ .name = "WAIT_MGW_ENDPOINT_TO_MSC",
+ .onenter = assignment_fsm_wait_mgw_endpoint_to_msc_onenter,
+ .action = assignment_fsm_wait_mgw_endpoint_to_msc,
+ .in_event_mask = 0
+ | S(ASSIGNMENT_EV_MSC_MGW_OK)
+ | S(ASSIGNMENT_EV_MSC_MGW_FAIL)
+ ,
+ },
+};
+
+static const struct value_string assignment_fsm_event_names[] = {
+ OSMO_VALUE_STRING(ASSIGNMENT_EV_LCHAN_ACTIVE),
+ OSMO_VALUE_STRING(ASSIGNMENT_EV_LCHAN_ESTABLISHED),
+ OSMO_VALUE_STRING(ASSIGNMENT_EV_LCHAN_ERROR),
+ OSMO_VALUE_STRING(ASSIGNMENT_EV_MSC_MGW_OK),
+ OSMO_VALUE_STRING(ASSIGNMENT_EV_MSC_MGW_FAIL),
+ OSMO_VALUE_STRING(ASSIGNMENT_EV_RR_ASSIGNMENT_COMPLETE),
+ OSMO_VALUE_STRING(ASSIGNMENT_EV_RR_ASSIGNMENT_FAIL),
+ OSMO_VALUE_STRING(ASSIGNMENT_EV_CONN_RELEASING),
+ {}
+};
+
+void assignment_fsm_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_CONN();
+ switch (event) {
+
+ case ASSIGNMENT_EV_CONN_RELEASING:
+ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, 0);
+ return;
+
+ case ASSIGNMENT_EV_LCHAN_ERROR:
+ if (data != conn->assignment.new_lchan)
+ return;
+ assignment_fail(conn->assignment.new_lchan->activate.gsm0808_error_cause,
+ "Failed to activate lchan %s",
+ gsm_lchan_name(conn->assignment.new_lchan));
+ return;
+
+ default:
+ return;
+ }
+}
+
+int assignment_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+ assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, "Timeout");
+ return 0;
+}
+
+void assignment_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
+{
+ GET_CONN();
+ assignment_reset(conn);
+ conn->assignment.fi = NULL;
+}
+
+static struct osmo_fsm assignment_fsm = {
+ .name = "assignment",
+ .states = assignment_fsm_states,
+ .num_states = ARRAY_SIZE(assignment_fsm_states),
+ .log_subsys = DRSL,
+ .event_names = assignment_fsm_event_names,
+ .allstate_action = assignment_fsm_allstate_action,
+ .allstate_event_mask = 0
+ | S(ASSIGNMENT_EV_CONN_RELEASING)
+ | S(ASSIGNMENT_EV_LCHAN_ERROR)
+ ,
+ .timer_cb = assignment_fsm_timer_cb,
+ .cleanup = assignment_fsm_cleanup,
+};
diff --git a/src/osmo-bsc/bsc_api.c b/src/osmo-bsc/bsc_api.c
index 4cf11a466..e03093481 100644
--- a/src/osmo-bsc/bsc_api.c
+++ b/src/osmo-bsc/bsc_api.c
@@ -22,7 +22,6 @@
*/
#include <osmocom/bsc/bsc_api.h>
-#include <osmocom/bsc/bsc_rll.h>
#include <osmocom/bsc/gsm_data.h>
#include <osmocom/bsc/signal.h>
#include <osmocom/bsc/abis_rsl.h>
@@ -33,568 +32,20 @@
#include <osmocom/bsc/bsc_subscriber.h>
#include <osmocom/bsc/penalty_timers.h>
#include <osmocom/bsc/osmo_bsc_sigtran.h>
+#include <osmocom/bsc/lchan_fsm.h>
+#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
#include <osmocom/gsm/protocol/gsm_08_08.h>
#include <osmocom/gsm/gsm48.h>
#include <osmocom/core/talloc.h>
-#define GSM0808_T10_VALUE 6, 0
-
-#define HO_DTAP_CACHE_MSGB_CB_LINK_ID 0
-#define HO_DTAP_CACHE_MSGB_CB_ALLOW_SACCH 1
-
-static void rll_ind_cb(struct gsm_lchan *, uint8_t, void *, enum bsc_rllr_ind);
-static void handle_release(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan);
-static void handle_chan_ack(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan);
-static void handle_chan_nack(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan);
-
-/*
- * Start a new assignment and make sure that it is completed within T10 either
- * positively, negatively or by the timeout.
- *
- * 1.) allocate a new lchan
- * 2.) copy the encryption key and other data from the
- * old to the new channel.
- * 3.) RSL Channel Activate this channel and wait
- *
- * -> Signal handler for the LCHAN
- * 4.) Send GSM 04.08 assignment command to the MS
- *
- * -> Assignment Complete/Assignment Failure
- * 5.) Release the SDCCH, continue signalling on the new link
- */
-static int handle_new_assignment(struct gsm_subscriber_connection *conn, int chan_mode, int full_rate)
-{
- struct gsm_lchan *new_lchan;
- enum gsm_chan_t chan_type;
-
- chan_type = full_rate ? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H;
-
- new_lchan = lchan_alloc(conn_get_bts(conn), chan_type, 0);
-
- if (!new_lchan) {
- LOGP(DMSC, LOGL_NOTICE, "%s No free channel for %s\n",
- bsc_subscr_name(conn->bsub), gsm_lchant_name(chan_type));
- return -1;
- }
-
- /* check if we are on TCH/F and requested TCH/H, but got TCH/F */
- if (conn->lchan->type == new_lchan->type
- && chan_type != new_lchan->type) {
- LOGPLCHAN(conn->lchan, DHO, LOGL_NOTICE,
- "-> %s Will not re-assign to identical channel type, %s was requested\n",
- gsm_lchan_name(new_lchan), gsm_lchant_name(chan_type));
- lchan_free(new_lchan);
- return -1;
- }
-
- /* copy old data to the new channel */
- memcpy(&new_lchan->encr, &conn->lchan->encr, sizeof(new_lchan->encr));
- new_lchan->ms_power = conn->lchan->ms_power;
- new_lchan->bs_power = conn->lchan->bs_power;
- new_lchan->rqd_ta = conn->lchan->rqd_ta;
-
- /* copy new data to it */
- new_lchan->tch_mode = chan_mode;
- new_lchan->rsl_cmode = (chan_mode == GSM48_CMODE_SIGN) ?
- RSL_CMOD_SPD_SIGN : RSL_CMOD_SPD_SPEECH;
-
- /* handle AMR correctly */
- if (chan_mode == GSM48_CMODE_SPEECH_AMR)
- bsc_mr_config(conn, new_lchan, full_rate);
-
- if (rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTRA_NORM_ASS, 0) < 0) {
- LOGPLCHAN(new_lchan, DHO, LOGL_ERROR, "could not activate channel\n");
- lchan_free(new_lchan);
- return -1;
- }
-
- /* remember that we have the channel */
- conn->secondary_lchan = new_lchan;
- new_lchan->conn = conn;
- return 0;
-}
-
-static void ho_dtap_cache_add(struct gsm_subscriber_connection *conn, struct msgb *msg,
- int link_id, bool allow_sacch)
-{
- if (conn->ho_dtap_cache_len >= 23) {
- LOGP(DHO, LOGL_ERROR, "%s: Cannot cache more DTAP messages,"
- " already reached sane maximum of %u cached messages\n",
- bsc_subscr_name(conn->bsub), conn->ho_dtap_cache_len);
- msgb_free(msg);
- return;
- }
- conn->ho_dtap_cache_len ++;
- LOGP(DHO, LOGL_DEBUG, "%s: Caching DTAP message during ho/ass (%u)\n",
- bsc_subscr_name(conn->bsub), conn->ho_dtap_cache_len);
- msg->cb[HO_DTAP_CACHE_MSGB_CB_LINK_ID] = (unsigned long)link_id;
- msg->cb[HO_DTAP_CACHE_MSGB_CB_ALLOW_SACCH] = allow_sacch ? 1 : 0;
- msgb_enqueue(&conn->ho_dtap_cache, msg);
-}
-
-void ho_dtap_cache_flush(struct gsm_subscriber_connection *conn, int send)
-{
- struct msgb *msg;
- unsigned int flushed_count = 0;
-
- if (conn->secondary_lchan || conn->ho) {
- LOGP(DHO, LOGL_ERROR, "%s: Cannot send cached DTAP messages, handover/assignment is still ongoing\n",
- bsc_subscr_name(conn->bsub));
- send = 0;
- }
-
- while ((msg = msgb_dequeue(&conn->ho_dtap_cache))) {
- conn->ho_dtap_cache_len --;
- flushed_count ++;
- if (send) {
- int link_id = (int)msg->cb[HO_DTAP_CACHE_MSGB_CB_LINK_ID];
- bool allow_sacch = !!msg->cb[HO_DTAP_CACHE_MSGB_CB_ALLOW_SACCH];
- LOGP(DHO, LOGL_DEBUG, "%s: Sending cached DTAP message after handover/assignment (%u/%u)\n",
- bsc_subscr_name(conn->bsub), flushed_count, conn->ho_dtap_cache_len);
- gsm0808_submit_dtap(conn, msg, link_id, allow_sacch);
- } else
- msgb_free(msg);
- }
-}
-
-/*! \brief process incoming 08.08 DTAP from MSC (send via BTS to MS) */
-int gsm0808_submit_dtap(struct gsm_subscriber_connection *conn,
- struct msgb *msg, int link_id, int allow_sacch)
-{
- uint8_t sapi;
-
-
- if (!conn->lchan) {
- LOGP(DMSC, LOGL_ERROR,
- "%s Called submit dtap without an lchan.\n",
- bsc_subscr_name(conn->bsub));
- msgb_free(msg);
- return -1;
- }
-
- /* buffer message during assignment / handover */
- if (conn->secondary_lchan || conn->ho) {
- ho_dtap_cache_add(conn, msg, link_id, !! allow_sacch);
- return 0;
- }
-
- sapi = link_id & 0x7;
- msg->lchan = conn->lchan;
- msg->dst = msg->lchan->ts->trx->rsl_link;
-
- /* If we are on a TCH and need to submit a SMS (on SAPI=3) we need to use the SACH */
- if (allow_sacch && sapi != 0) {
- if (conn->lchan->type == GSM_LCHAN_TCH_F || conn->lchan->type == GSM_LCHAN_TCH_H)
- link_id |= 0x40;
- }
-
- msg->l3h = msg->data;
- /* is requested SAPI already up? */
- if (conn->lchan->sapis[sapi] == LCHAN_SAPI_UNUSED) {
- /* Establish L2 for additional SAPI */
- OBSC_LINKID_CB(msg) = link_id;
- if (rll_establish(msg->lchan, sapi, rll_ind_cb, msg) != 0) {
- msgb_free(msg);
- bsc_sapi_n_reject(conn, link_id);
- return -1;
- }
- return 0;
- } else {
- /* Directly forward via RLL/RSL to BTS */
- return rsl_data_request(msg, link_id);
- }
-}
-
-/*
- * \brief Check if the given channel is compatible with the mode/fullrate
- */
-static int chan_compat_with_mode(struct gsm_lchan *lchan, int chan_mode, int full_rate)
-{
- switch (chan_mode) {
- case GSM48_CMODE_SIGN:
- switch (lchan->type) {
- case GSM_LCHAN_TCH_F:
- case GSM_LCHAN_TCH_H:
- case GSM_LCHAN_SDCCH:
- return 1;
- default:
- return 0;
- }
- case GSM48_CMODE_SPEECH_V1:
- case GSM48_CMODE_SPEECH_AMR:
- case GSM48_CMODE_DATA_3k6:
- case GSM48_CMODE_DATA_6k0:
- /* these services can all run on TCH/H, but we may have
- * an explicit override by the 'full_rate' argument */
- switch (lchan->type) {
- case GSM_LCHAN_TCH_F:
- return full_rate ? 1 : 0;
- case GSM_LCHAN_TCH_H:
- return full_rate ? 0 : 1;
- default:
- return 0;
- }
- case GSM48_CMODE_DATA_12k0:
- case GSM48_CMODE_DATA_14k5:
- case GSM48_CMODE_SPEECH_EFR:
- /* these services all explicitly require a TCH/F */
- return (lchan->type == GSM_LCHAN_TCH_F) ? 1 : 0;
- default:
- return 0;
- }
-}
-
-/*! Send a GSM08.08 Assignment Request. Right now this does not contain the
- * audio codec type or the allowed rates for the config. In case the current
- * channel does not allow the selected mode a new one will be allocated.
- * \param[out] conn related subscriber connection
- * \param[in] chan_mode mode of the channel (see enum gsm48_chan_mode)
- * \param[in] full_rate select full rate or half rate channel
- * \returns 0 on success, 1 when no operation is neccessary, -1 on failure */
-int gsm0808_assign_req(struct gsm_subscriber_connection *conn, int chan_mode, int full_rate)
-{
- /* TODO: Add multirate configuration, make it work for more than audio. */
-
- if (!chan_compat_with_mode(conn->lchan, chan_mode, full_rate)) {
- if (handle_new_assignment(conn, chan_mode, full_rate) != 0)
- goto error;
- } else {
- /* Check if the channel is already in the requested mode, if
- * yes, we skip unnecessary channel mode modify operations. */
- if (conn->lchan->tch_mode == chan_mode)
- return 1;
-
- if (chan_mode == GSM48_CMODE_SPEECH_AMR)
- bsc_mr_config(conn, conn->lchan, full_rate);
-
- LOGPLCHAN(conn->lchan, DMSC, LOGL_NOTICE,
- "Sending ChanModify for speech: %s\n",
- get_value_string(gsm48_chan_mode_names, chan_mode));
- gsm48_lchan_modify(conn->lchan, chan_mode);
- }
-
- /* we expect the caller will manage T10 */
- return 0;
-
-error:
- bsc_assign_fail(conn, 0, NULL);
- return -1;
-}
-
int gsm0808_page(struct gsm_bts *bts, unsigned int page_group, unsigned int mi_len,
uint8_t *mi, int chan_type)
{
return rsl_paging_cmd(bts, page_group, mi_len, mi, chan_type, false);
}
-static void handle_ass_compl(struct gsm_subscriber_connection *conn,
- struct msgb *msg)
-{
- struct gsm48_hdr *gh = msgb_l3(msg);
- enum gsm48_rr_cause cause;
-
- /* Expecting gsm48_hdr + cause value */
- if (msgb_l3len(msg) != sizeof(*gh) + 1) {
- LOGPLCHAN(msg->lchan, DRR, LOGL_ERROR,
- "RR Assignment Complete: length invalid: %u, expected %zu\n",
- msgb_l3len(msg), sizeof(*gh) + 1);
- return;
- }
-
- cause = gh->data[0];
-
- LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, "ASSIGNMENT COMPLETE cause = %s\n",
- rr_cause_name(cause));
-
- if (conn->ho) {
- struct lchan_signal_data sig = {
- .lchan = msg->lchan,
- };
- osmo_signal_dispatch(SS_LCHAN, S_LCHAN_ASSIGNMENT_COMPL, &sig);
- /* FIXME: release old channel */
-
- /* send pending messages, if any */
- ho_dtap_cache_flush(conn, 1);
-
- return;
- }
-
- if (conn->secondary_lchan != msg->lchan) {
- LOGPLCHAN(msg->lchan, DRR, LOGL_ERROR,
- "RR Assignment Complete does not match conn's secondary lchan.\n");
- return;
- }
-
- lchan_release(conn->lchan, 0, RSL_REL_LOCAL_END);
- conn->lchan = conn->secondary_lchan;
- conn->secondary_lchan = NULL;
-
- /* send pending messages, if any */
- ho_dtap_cache_flush(conn, 1);
-
- if (is_ipaccess_bts(conn_get_bts(conn)) && conn->lchan->tch_mode != GSM48_CMODE_SIGN)
- rsl_ipacc_crcx(conn->lchan);
-
- bsc_assign_compl(conn, cause);
-}
-
-static void handle_ass_fail(struct gsm_subscriber_connection *conn,
- struct msgb *msg)
-{
- uint8_t *rr_failure;
- struct gsm48_hdr *gh;
-
- if (conn->ho) {
- struct lchan_signal_data sig;
- struct gsm48_hdr *gh = msgb_l3(msg);
-
- LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, "ASSIGNMENT FAILED cause = %s\n",
- rr_cause_name(gh->data[0]));
-
- sig.lchan = msg->lchan;
- sig.mr = NULL;
- osmo_signal_dispatch(SS_LCHAN, S_LCHAN_ASSIGNMENT_FAIL, &sig);
- /* FIXME: release allocated new channel */
-
- /* send pending messages, if any */
- ho_dtap_cache_flush(conn, 1);
-
- return;
- }
-
- if (conn->lchan != msg->lchan) {
- LOGPLCHAN(msg->lchan, DMSC, LOGL_ERROR,
- "Assignment failure should occur on primary lchan.\n");
- return;
- }
-
- /* stop the timer and release it */
- if (conn->secondary_lchan) {
- lchan_release(conn->secondary_lchan, 0, RSL_REL_LOCAL_END);
- conn->secondary_lchan = NULL;
- }
-
- /* send pending messages, if any */
- ho_dtap_cache_flush(conn, 1);
-
- gh = msgb_l3(msg);
- if (msgb_l3len(msg) - sizeof(*gh) != 1) {
- LOGPLCHAN(conn->lchan, DMSC, LOGL_ERROR, "assignment failure unhandled: %zu\n",
- msgb_l3len(msg) - sizeof(*gh));
- rr_failure = NULL;
- } else {
- rr_failure = &gh->data[0];
- }
-
- bsc_assign_fail(conn, GSM0808_CAUSE_RADIO_INTERFACE_MESSAGE_FAILURE, rr_failure);
-}
-
-static void handle_classmark_chg(struct gsm_subscriber_connection *conn,
- struct msgb *msg)
-{
- struct gsm48_hdr *gh = msgb_l3(msg);
- unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
- uint8_t cm2_len, cm3_len = 0;
- uint8_t *cm2, *cm3 = NULL;
-
- LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, "CLASSMARK CHANGE ");
-
- /* classmark 2 */
- cm2_len = gh->data[0];
- cm2 = &gh->data[1];
- DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
-
- if (payload_len > cm2_len + 1) {
- /* we must have a classmark3 */
- if (gh->data[cm2_len+1] != 0x20) {
- DEBUGPC(DRR, "ERR CM3 TAG\n");
- return;
- }
- if (cm2_len > 3) {
- DEBUGPC(DRR, "CM2 too long!\n");
- return;
- }
-
- cm3_len = gh->data[cm2_len+2];
- cm3 = &gh->data[cm2_len+3];
- if (cm3_len > 14) {
- DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
- return;
- }
- DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
- }
- bsc_cm_update(conn, cm2, cm2_len, cm3, cm3_len);
-}
-
-/* Chapter 9.1.16 Handover complete */
-static void handle_rr_ho_compl(struct msgb *msg)
-{
- struct lchan_signal_data sig;
- struct gsm48_hdr *gh = msgb_l3(msg);
-
- LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG,
- "HANDOVER COMPLETE cause = %s\n", rr_cause_name(gh->data[0]));
-
- sig.lchan = msg->lchan;
- sig.mr = NULL;
- osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig);
- /* FIXME: release old channel */
-
- /* send pending messages, if any */
- ho_dtap_cache_flush(msg->lchan->conn, 1);
-}
-
-/* Chapter 9.1.17 Handover Failure */
-static void handle_rr_ho_fail(struct msgb *msg)
-{
- struct lchan_signal_data sig;
- struct gsm48_hdr *gh = msgb_l3(msg);
-
- /* Log on both RR and HO categories: it is an RR message, but is still quite important when
- * filtering on HO. */
- LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG,
- "HANDOVER FAILED cause = %s\n", rr_cause_name(gh->data[0]));
- LOGPLCHAN(msg->lchan, DHO, LOGL_DEBUG,
- "HANDOVER FAILED cause = %s\n", rr_cause_name(gh->data[0]));
-
- sig.lchan = msg->lchan;
- sig.mr = NULL;
- osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
- /* FIXME: release allocated new channel */
-
- /* send pending messages, if any */
- ho_dtap_cache_flush(msg->lchan->conn, 1);
-}
-
-
-static void dispatch_dtap(struct gsm_subscriber_connection *conn,
- uint8_t link_id, struct msgb *msg)
-{
- struct gsm48_hdr *gh;
- uint8_t pdisc;
- uint8_t msg_type;
- int rc;
-
- if (msgb_l3len(msg) < sizeof(*gh)) {
- LOGP(DMSC, LOGL_ERROR, "(%s) Message too short for a GSM48 header.\n",
- bsc_subscr_name(conn->bsub));
- return;
- }
-
- gh = msgb_l3(msg);
- pdisc = gsm48_hdr_pdisc(gh);
- msg_type = gsm48_hdr_msg_type(gh);
-
- /* the idea is to handle all RR messages here, and only hand
- * MM/CC/SMS-CP/LCS up to the MSC. Some messages like PAGING
- * RESPONSE or CM SERVICE REQUEST will not be covered here, as
- * they are only possible in the first L3 message of each L2
- * channel, i.e. 'conn' will not exist and gsm0408_rcvmsg()
- * will call api->compl_l3() for it */
- switch (pdisc) {
- case GSM48_PDISC_RR:
- switch (msg_type) {
- case GSM48_MT_RR_GPRS_SUSP_REQ:
- LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG,
- "%s\n", gsm48_rr_msg_name(GSM48_MT_RR_GPRS_SUSP_REQ));
- break;
- case GSM48_MT_RR_STATUS:
- LOGPLCHAN(msg->lchan, DRR, LOGL_NOTICE,
- "%s (cause: %s)\n", gsm48_rr_msg_name(GSM48_MT_RR_STATUS),
- rr_cause_name(gh->data[0]));
- break;
- case GSM48_MT_RR_MEAS_REP:
- /* This shouldn't actually end up here, as RSL treats
- * L3 Info of 08.58 MEASUREMENT REPORT different by calling
- * directly into gsm48_parse_meas_rep */
- LOGPLCHAN(msg->lchan, DMEAS, LOGL_ERROR,
- "DIRECT GSM48 MEASUREMENT REPORT ?!?\n");
- gsm48_tx_rr_status(conn, GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT);
- break;
- case GSM48_MT_RR_HANDO_COMPL:
- handle_rr_ho_compl(msg);
- break;
- case GSM48_MT_RR_HANDO_FAIL:
- handle_rr_ho_fail(msg);
- break;
- case GSM48_MT_RR_CIPH_M_COMPL:
- bsc_cipher_mode_compl(conn, msg, conn->lchan->encr.alg_id);
- break;
- case GSM48_MT_RR_ASS_COMPL:
- handle_ass_compl(conn, msg);
- break;
- case GSM48_MT_RR_ASS_FAIL:
- handle_ass_fail(conn, msg);
- break;
- case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
- rc = gsm48_rx_rr_modif_ack(msg);
- if (rc < 0)
- bsc_assign_fail(conn, GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, NULL);
- else
- bsc_assign_compl(conn, 0);
- break;
- case GSM48_MT_RR_CLSM_CHG:
- handle_classmark_chg(conn, msg);
- break;
- case GSM48_MT_RR_APP_INFO:
- /* Passing RR APP INFO to MSC, not quite
- * according to spec */
- bsc_dtap(conn, link_id, msg);
- break;
- default:
- /* Drop unknown RR message */
- LOGPLCHAN(msg->lchan, DRR, LOGL_NOTICE,
- "Dropping %s 04.08 RR message\n", gsm48_rr_msg_name(msg_type));
- gsm48_tx_rr_status(conn, GSM48_RR_CAUSE_MSG_TYPE_N);
- break;
- }
- break;
- default:
- bsc_dtap(conn, link_id, msg);
- break;
- }
-}
-
-/*! \brief RSL has received a DATA INDICATION with L3 from MS */
-int gsm0408_rcvmsg(struct msgb *msg, uint8_t link_id)
-{
- int rc;
- struct gsm_lchan *lchan;
-
- lchan = msg->lchan;
- if (lchan->state != LCHAN_S_ACTIVE) {
- LOGPLCHAN(msg->lchan, DRSL, LOGL_INFO, "Got data in non active state, discarding.\n");
- return -1;
- }
-
-
- if (lchan->conn) {
- /* if we already have a connection, forward via DTAP to
- * MSC */
- dispatch_dtap(lchan->conn, link_id, msg);
- } else {
- /* allocate a new connection */
- rc = BSC_API_CONN_POL_REJECT;
- lchan->conn = bsc_subscr_con_allocate(msg->lchan->ts->trx->bts->network);
- if (!lchan->conn) {
- lchan_release(lchan, 1, RSL_REL_NORMAL);
- return -1;
- }
- lchan->conn->lchan = lchan;
-
- /* fwd via bsc_api to send COMPLETE L3 INFO to MSC */
- rc = bsc_compl_l3(lchan->conn, msg, 0);
-
- if (rc != BSC_API_CONN_POL_ACCEPT) {
- //osmo_fsm_inst_dispatch(lchan->conn->fi, FIXME, NULL);
- }
- }
-
- return 0;
-}
-
/*! \brief We received a GSM 08.08 CIPHER MODE from the MSC */
int gsm0808_cipher_mode(struct gsm_subscriber_connection *conn, int cipher,
const uint8_t *key, int len, int include_imeisv)
@@ -622,126 +73,3 @@ int gsm0808_cipher_mode(struct gsm_subscriber_connection *conn, int cipher,
return gsm48_send_rr_ciph_mode(conn->lchan, include_imeisv);
}
-
-/*
- * Release all occupied RF Channels but stay around for more.
- */
-int gsm0808_clear(struct gsm_subscriber_connection *conn)
-{
- if (conn->ho)
- bsc_clear_handover(conn, 1);
-
- if (conn->secondary_lchan)
- lchan_release(conn->secondary_lchan, 0, RSL_REL_LOCAL_END);
-
- if (conn->lchan)
- lchan_release(conn->lchan, 1, RSL_REL_NORMAL);
-
- conn->lchan = NULL;
- conn->secondary_lchan = NULL;
-
- return 0;
-}
-
-static void rll_ind_cb(struct gsm_lchan *lchan, uint8_t link_id, void *_data, enum bsc_rllr_ind rllr_ind)
-{
- struct msgb *msg = _data;
-
- /*
- * There seems to be a small window that the RLL timer can
- * fire after a lchan_release call and before the S_CHALLOC_FREED
- * is called. Check if a conn is set before proceeding.
- */
- if (!lchan->conn)
- return;
-
- switch (rllr_ind) {
- case BSC_RLLR_IND_EST_CONF:
- rsl_data_request(msg, OBSC_LINKID_CB(msg));
- break;
- case BSC_RLLR_IND_REL_IND:
- case BSC_RLLR_IND_ERR_IND:
- case BSC_RLLR_IND_TIMEOUT:
- bsc_sapi_n_reject(lchan->conn, OBSC_LINKID_CB(msg));
- msgb_free(msg);
- break;
- }
-}
-
-static int bsc_handle_lchan_signal(unsigned int subsys, unsigned int signal,
- void *handler_data, void *signal_data)
-{
- struct gsm_lchan *lchan;
- struct lchan_signal_data *lchan_data;
-
- if (subsys != SS_LCHAN)
- return 0;
-
-
- lchan_data = signal_data;
- if (!lchan_data->lchan || !lchan_data->lchan->conn)
- return 0;
-
- lchan = lchan_data->lchan;
-
- switch (signal) {
- case S_LCHAN_UNEXPECTED_RELEASE:
- LOGPLCHAN(lchan, DMSC, LOGL_NOTICE, "S_LCHAN_UNEXPECTED_RELEASE\n");
- handle_release(lchan->conn, lchan);
- break;
- case S_LCHAN_ACTIVATE_ACK:
- handle_chan_ack(lchan->conn, lchan);
- break;
- case S_LCHAN_ACTIVATE_NACK:
- handle_chan_nack(lchan->conn, lchan);
- break;
- }
-
- return 0;
-}
-
-static void handle_release(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan)
-{
- if (conn->secondary_lchan == lchan) {
- LOGPLCHAN(lchan, DMSC, LOGL_NOTICE,
- "lchan release on new lchan, Assignment failed\n");
- conn->secondary_lchan = NULL;
-
- bsc_assign_fail(conn, GSM0808_CAUSE_RADIO_INTERFACE_FAILURE, NULL);
- }
-
- /* clear the connection now */
- bsc_clear_request(conn, 0);
-
- /* now give up all channels */
- if (conn->lchan == lchan)
- conn->lchan = NULL;
- if (conn->ho && conn->ho->new_lchan == lchan)
- bsc_clear_handover(conn, 0);
- lchan->conn = NULL;
-}
-
-static void handle_chan_ack(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan)
-{
- if (conn->secondary_lchan != lchan)
- return;
-
- LOGPLCHAN(lchan, DMSC, LOGL_NOTICE, "Sending RR Assignment\n");
- gsm48_send_rr_ass_cmd(conn->lchan, lchan, lchan->ms_power);
-}
-
-static void handle_chan_nack(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan)
-{
- if (conn->secondary_lchan != lchan)
- return;
-
- LOGPLCHAN(lchan, DMSC, LOGL_ERROR, "Channel activation failed.\n");
- conn->secondary_lchan->conn = NULL;
- conn->secondary_lchan = NULL;
- bsc_assign_fail(conn, GSM0808_CAUSE_RADIO_INTERFACE_FAILURE, NULL);
-}
-
-static __attribute__((constructor)) void on_dso_load_bsc(void)
-{
- osmo_signal_register_handler(SS_LCHAN, bsc_handle_lchan_signal, NULL);
-}
diff --git a/src/osmo-bsc/bsc_dyn_ts.c b/src/osmo-bsc/bsc_dyn_ts.c
deleted file mode 100644
index ed7caed7f..000000000
--- a/src/osmo-bsc/bsc_dyn_ts.c
+++ /dev/null
@@ -1,60 +0,0 @@
-/* Dynamic PDCH initialisation implementation shared across NM and RSL */
-
-/* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
- * All Rights Reserved
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-#include <osmocom/core/logging.h>
-#include <osmocom/bsc/debug.h>
-#include <osmocom/bsc/gsm_data.h>
-#include <osmocom/bsc/abis_rsl.h>
-
-static void tchf_pdch_ts_init(struct gsm_bts_trx_ts *ts)
-{
- int rc;
-
- rc = rsl_ipacc_pdch_activate(ts, 1);
- if (rc != 0 && rc != -ENOTSUP)
- LOGP(DRSL, LOGL_ERROR, "%s %s: PDCH ACT failed\n",
- gsm_ts_name(ts), gsm_pchan_name(ts->pchan));
-}
-
-static void tchf_tchh_pdch_ts_init(struct gsm_bts_trx_ts *ts)
-{
- dyn_ts_switchover_start(ts, GSM_PCHAN_PDCH);
-}
-
-void dyn_ts_init(struct gsm_bts_trx_ts *ts)
-{
- /* Clear all TCH/F_PDCH flags */
- ts->flags &= ~(TS_F_PDCH_PENDING_MASK | TS_F_PDCH_ACTIVE);
-
- /* Clear TCH/F_TCH/H_PDCH state */
- ts->dyn.pchan_is = ts->dyn.pchan_want = GSM_PCHAN_NONE;
- ts->dyn.pending_chan_activ = NULL;
-
- switch (ts->pchan) {
- case GSM_PCHAN_TCH_F_PDCH:
- tchf_pdch_ts_init(ts);
- break;
- case GSM_PCHAN_TCH_F_TCH_H_PDCH:
- tchf_tchh_pdch_ts_init(ts);
- break;
- default:
- break;
- }
-}
diff --git a/src/osmo-bsc/bsc_init.c b/src/osmo-bsc/bsc_init.c
index 1fe484761..908d6cb4e 100644
--- a/src/osmo-bsc/bsc_init.c
+++ b/src/osmo-bsc/bsc_init.c
@@ -34,6 +34,7 @@
#include <osmocom/bsc/pcu_if.h>
#include <osmocom/bsc/bsc_msc_data.h>
#include <osmocom/bsc/handover_cfg.h>
+#include <osmocom/bsc/neighbor_ident.h>
#include <osmocom/bsc/gsm_04_08_utils.h>
#include <time.h>
diff --git a/src/osmo-bsc/bsc_subscr_conn_fsm.c b/src/osmo-bsc/bsc_subscr_conn_fsm.c
index 4f09ae6bc..9bb6d0eba 100644
--- a/src/osmo-bsc/bsc_subscr_conn_fsm.c
+++ b/src/osmo-bsc/bsc_subscr_conn_fsm.c
@@ -26,14 +26,19 @@
#include <osmocom/bsc/a_reset.h>
#include <osmocom/bsc/bsc_api.h>
#include <osmocom/bsc/gsm_data.h>
-#include <osmocom/bsc/handover.h>
-#include <osmocom/bsc/chan_alloc.h>
+#include <osmocom/bsc/handover_fsm.h>
+#include <osmocom/bsc/lchan_fsm.h>
#include <osmocom/bsc/bsc_subscriber.h>
#include <osmocom/bsc/osmo_bsc_sigtran.h>
#include <osmocom/bsc/osmo_bsc_lcls.h>
#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
#include <osmocom/bsc/osmo_bsc.h>
#include <osmocom/bsc/penalty_timers.h>
+#include <osmocom/bsc/gsm_04_08_utils.h>
+#include <osmocom/bsc/abis_rsl.h>
+#include <osmocom/bsc/bsc_rll.h>
+#include <osmocom/bsc/mgw_endpoint_fsm.h>
+#include <osmocom/bsc/assignment_fsm.h>
#include <osmocom/mgcp_client/mgcp_client_fsm.h>
#include <osmocom/core/byteswap.h>
@@ -45,210 +50,225 @@
#define MGCP_MGW_HO_TIMEOUT 4 /* in seconds */
#define MGCP_MGW_HO_TIMEOUT_TIMER_NR 2
-#define ENDPOINT_ID "rtpbridge/*@mgw"
-
enum gscon_fsm_states {
ST_INIT,
/* waiting for CC from MSC */
ST_WAIT_CC,
/* active connection */
ST_ACTIVE,
- /* during assignment; waiting for ASS_CMPL */
- ST_WAIT_ASS_CMPL,
+ ST_ASSIGNMENT,
+ ST_HANDOVER,
/* BSSMAP CLEAR has been received */
ST_CLEARING,
-
-/* MGW handling */
- /* during assignment; waiting for MGW response to CRCX for BTS */
- ST_WAIT_CRCX_BTS,
- /* during assignment; waiting for MGW response to MDCX for BTS */
- ST_WAIT_MDCX_BTS,
- /* during assignment; waiting for MGW response to CRCX for MSC */
- ST_WAIT_CRCX_MSC,
-
-/* MT (inbound) handover */
- /* Wait for Handover Access from MS/BTS */
- ST_WAIT_MT_HO_ACC,
- /* Wait for RR Handover Complete from MS/BTS */
- ST_WAIT_MT_HO_COMPL,
-
-/* MO (outbound) handover */
- /* Wait for Handover Command / Handover Required Reject from MSC */
- ST_WAIT_MO_HO_CMD,
- /* Wait for Clear Command from MSC */
- ST_MO_HO_PROCEEDING,
-
-/* Internal HO handling */
- /* Wait for the handover logic to complete the handover */
- ST_WAIT_HO_COMPL,
- /* during handover; waiting for MGW response to MDCX for BTS */
- ST_WAIT_MDCX_BTS_HO,
};
static const struct value_string gscon_fsm_event_names[] = {
{GSCON_EV_A_CONN_IND, "MT-CONNECT.ind"},
{GSCON_EV_A_CONN_REQ, "MO-CONNECT.req"},
{GSCON_EV_A_CONN_CFM, "MO-CONNECT.cfm"},
- {GSCON_EV_A_ASSIGNMENT_CMD, "ASSIGNMENT_CMD"},
{GSCON_EV_A_CLEAR_CMD, "CLEAR_CMD"},
{GSCON_EV_A_DISC_IND, "DISCONNET.ind"},
- {GSCON_EV_A_HO_REQ, "HANDOVER_REQUEST"},
-
- {GSCON_EV_RR_ASS_COMPL, "RR_ASSIGN_COMPL"},
- {GSCON_EV_RR_ASS_FAIL, "RR_ASSIGN_FAIL"},
- {GSCON_EV_RLL_REL_IND, "RLL_RELEASE.ind"},
- {GSCON_EV_RSL_CONN_FAIL, "RSL_CONN_FAIL.ind"},
- {GSCON_EV_RSL_CLEAR_COMPL, "RSL_CLEAR_COMPLETE"},
-
- {GSCON_EV_MO_DTAP, "MO-DTAP"},
- {GSCON_EV_MT_DTAP, "MT-DTAP"},
+ {GSCON_EV_ASSIGNMENT_START, "ASSIGNMENT_START"},
+ {GSCON_EV_ASSIGNMENT_END, "ASSIGNMENT_END"},
+ {GSCON_EV_HANDOVER_START, "HANDOVER_START"},
+ {GSCON_EV_HANDOVER_END, "HANDOVER_END"},
+ {GSCON_EV_RSL_CONN_FAIL, "RSL_CONN_FAIL"},
+ {GSCON_EV_MO_DTAP, "MO_DTAP"},
+ {GSCON_EV_MT_DTAP, "MT_DTAP"},
{GSCON_EV_TX_SCCP, "TX_SCCP"},
-
- {GSCON_EV_MGW_FAIL_BTS, "MGW_FAILURE_BTS"},
- {GSCON_EV_MGW_FAIL_MSC, "MGW_FAILURE_MSC"},
- {GSCON_EV_MGW_CRCX_RESP_BTS, "MGW_CRCX_RESPONSE_BTS"},
- {GSCON_EV_MGW_MDCX_RESP_BTS, "MGW_MDCX_RESPONSE_BTS"},
- {GSCON_EV_MGW_CRCX_RESP_MSC, "MGW_CRCX_RESPONSE_MSC"},
- {GSCON_EV_MGW_MDCX_RESP_MSC, "MGW_MDCX_RESPONSE_MSC"},
-
- {GSCON_EV_HO_START, "HO_START"},
- {GSCON_EV_HO_TIMEOUT, "HO_TIMEOUT"},
- {GSCON_EV_HO_FAIL, "HO_FAIL"},
- {GSCON_EV_HO_COMPL, "HO_COMPL"},
+ {GSCON_EV_MGW_MDCX_RESP_MSC, "MGW_MDCX_RESP_MSC"},
{GSCON_EV_LCLS_FAIL, "LCLS_FAIL"},
+ {GSCON_EV_FORGET_LCHAN, "FORGET_LCHAN"},
+ {GSCON_EV_FORGET_MGW_ENDPOINT, "FORGET_MGW_ENDPOINT"},
+ {}
+};
- {0, NULL}
+struct state_timeout conn_fsm_timeouts[32] = {
+ [ST_WAIT_CC] = { .T = 993210 },
+ [ST_CLEARING] = { .T = 999 },
};
-/* Send data SCCP message through SCCP connection. All sigtran messages
- * that are send from this FSM must use this function. Never use
- * osmo_bsc_sigtran_send() directly since this would defeat the checks
- * provided by this function. */
-static void sigtran_send(struct gsm_subscriber_connection *conn, struct msgb *msg, struct osmo_fsm_inst *fi)
+/* Transition to a state, using the T timer defined in conn_fsm_timeouts.
+ * The actual timeout value is in turn obtained from network->T_defs.
+ * Assumes local variable fi exists. */
+#define conn_fsm_state_chg(state) \
+ fsm_inst_state_chg_T(conn->fi, state, \
+ conn_fsm_timeouts, \
+ conn->network->T_defs, \
+ 1)
+
+
+int gscon_sigtran_send(struct gsm_subscriber_connection *conn, struct msgb *msg)
{
int rc;
+ if (!msg)
+ return -ENOMEM;
+
/* Make sure that we only attempt to send SCCP messages if we have
* a life SCCP connection. Otherwise drop the message. */
- if (fi->state == ST_INIT || fi->state == ST_WAIT_CC) {
- LOGPFSML(fi, LOGL_ERROR, "No active SCCP connection, dropping message!\n");
+ if (conn->fi->state == ST_INIT || conn->fi->state == ST_WAIT_CC) {
+ LOGPFSML(conn->fi, LOGL_ERROR, "No active SCCP connection, dropping message\n");
msgb_free(msg);
- return;
+ return -ENODEV;
}
rc = osmo_bsc_sigtran_send(conn, msg);
if (rc < 0)
- LOGPFSML(fi, LOGL_ERROR, "Unable to deliver SCCP message!\n");
+ LOGPFSML(conn->fi, LOGL_ERROR, "Unable to deliver SCCP message\n");
+ return rc;
}
+
+#define GSCON_DTAP_CACHE_MSGB_CB_LINK_ID 0
+#define GSCON_DTAP_CACHE_MSGB_CB_ALLOW_SACCH 1
-/* Add the LCLS BSS Status IE to a BSSMAP message. We assume this is
- * called on a msgb that was returned by gsm0808_create_ass_compl() */
-static void bssmap_add_lcls_status(struct msgb *msg, enum gsm0808_lcls_status status)
+static void gscon_dtap_cache_add(struct gsm_subscriber_connection *conn, struct msgb *msg,
+ int link_id, bool allow_sacch)
{
- OSMO_ASSERT(msg->l3h[0] == BSSAP_MSG_BSS_MANAGEMENT);
- OSMO_ASSERT(msg->l3h[2] == BSS_MAP_MSG_ASSIGMENT_COMPLETE ||
- msg->l3h[2] == BSS_MAP_MSG_HANDOVER_RQST_ACKNOWLEDGE ||
- msg->l3h[2] == BSS_MAP_MSG_HANDOVER_COMPLETE ||
- msg->l3h[2] == BSS_MAP_MSG_HANDOVER_PERFORMED);
- OSMO_ASSERT(msgb_tailroom(msg) >= 2);
-
- /* append IE to end of message */
- msgb_tv_put(msg, GSM0808_IE_LCLS_BSS_STATUS, status);
- /* increment the "length" byte in the BSSAP header */
- msg->l3h[1] += 2;
+ if (conn->gscon_dtap_cache_len >= 23) {
+ LOGP(DHO, LOGL_ERROR, "%s: Cannot cache more DTAP messages,"
+ " already reached sane maximum of %u cached messages\n",
+ bsc_subscr_name(conn->bsub), conn->gscon_dtap_cache_len);
+ msgb_free(msg);
+ return;
+ }
+ conn->gscon_dtap_cache_len ++;
+ LOGP(DHO, LOGL_DEBUG, "%s: Caching DTAP message during ho/ass (%u)\n",
+ bsc_subscr_name(conn->bsub), conn->gscon_dtap_cache_len);
+ msg->cb[GSCON_DTAP_CACHE_MSGB_CB_LINK_ID] = (unsigned long)link_id;
+ msg->cb[GSCON_DTAP_CACHE_MSGB_CB_ALLOW_SACCH] = allow_sacch ? 1 : 0;
+ msgb_enqueue(&conn->gscon_dtap_cache, msg);
}
-/* Add (append) the LCLS BSS Status IE to a BSSMAP message, if there is any LCLS
- * active on the given \a conn */
-static void bssmap_add_lcls_status_if_needed(struct gsm_subscriber_connection *conn,
- struct msgb *msg)
+void gscon_dtap_cache_flush(struct gsm_subscriber_connection *conn, int send)
{
- enum gsm0808_lcls_status status = lcls_get_status(conn);
- if (status != 0xff) {
- LOGPFSM(conn->fi, "Adding LCLS BSS-Status (%s) to %s\n",
- gsm0808_lcls_status_name(status),
- gsm0808_bssmap_name(msg->l3h[2]));
- bssmap_add_lcls_status(msg, status);
+ struct msgb *msg;
+ unsigned int flushed_count = 0;
+
+ while ((msg = msgb_dequeue(&conn->gscon_dtap_cache))) {
+ conn->gscon_dtap_cache_len --;
+ flushed_count ++;
+ if (send) {
+ int link_id = (int)msg->cb[GSCON_DTAP_CACHE_MSGB_CB_LINK_ID];
+ bool allow_sacch = !!msg->cb[GSCON_DTAP_CACHE_MSGB_CB_ALLOW_SACCH];
+ LOGP(DHO, LOGL_DEBUG, "%s: Sending cached DTAP message after handover/assignment (%u/%u)\n",
+ bsc_subscr_name(conn->bsub), flushed_count, conn->gscon_dtap_cache_len);
+ gsm0808_submit_dtap(conn, msg, link_id, allow_sacch);
+ } else
+ msgb_free(msg);
}
}
-/* Generate and send assignment complete message */
-static void send_ass_compl(struct gsm_lchan *lchan, struct osmo_fsm_inst *fi, bool voice)
+static void rll_ind_cb(struct gsm_lchan *lchan, uint8_t link_id, void *_data, enum bsc_rllr_ind rllr_ind)
{
- struct msgb *resp;
- struct gsm0808_speech_codec sc;
- struct gsm0808_speech_codec *sc_ptr = NULL;
- struct gsm_subscriber_connection *conn;
- struct sockaddr_storage *addr_local = NULL;
- int perm_spch = 0;
- uint8_t chosen_channel;
-
- conn = lchan->conn;
- OSMO_ASSERT(conn);
+ struct msgb *msg = _data;
+
+ /*
+ * There seems to be a small window that the RLL timer can
+ * fire after a lchan_release call and before the S_CHALLOC_FREED
+ * is called. Check if a conn is set before proceeding.
+ */
+ if (!lchan->conn)
+ return;
- /* apply LCLS configuration (if any) */
- lcls_apply_config(conn);
-
- LOGPFSML(fi, LOGL_DEBUG, "Sending assignment complete message... (id=%i)\n", conn->sccp.conn_id);
-
- /* Generate voice related fields */
- if (voice) {
- perm_spch = gsm0808_permitted_speech(lchan->type, lchan->tch_mode);
- switch (conn->sccp.msc->a.asp_proto) {
- case OSMO_SS7_ASP_PROT_IPA:
- /* don't add any AoIP specific fields. CIC allocated by MSC */
- break;
- default:
- OSMO_ASSERT(lchan->abis_ip.ass_compl.valid);
- addr_local = &conn->user_plane.aoip_rtp_addr_local;
-
- /* Extrapolate speech codec from speech mode */
- gsm0808_speech_codec_from_chan_type(&sc, perm_spch);
- sc_ptr = &sc;
- break;
- }
- /* FIXME: AMR codec configuration must be derived from lchan1! */
+ switch (rllr_ind) {
+ case BSC_RLLR_IND_EST_CONF:
+ rsl_data_request(msg, OBSC_LINKID_CB(msg));
+ break;
+ case BSC_RLLR_IND_REL_IND:
+ case BSC_RLLR_IND_ERR_IND:
+ case BSC_RLLR_IND_TIMEOUT:
+ bsc_sapi_n_reject(lchan->conn, OBSC_LINKID_CB(msg));
+ msgb_free(msg);
+ break;
}
+}
+
+/*! \brief process incoming 08.08 DTAP from MSC (send via BTS to MS) */
+int gsm0808_submit_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, uint8_t link_id,
+ bool allow_sacch)
+{
+ uint8_t sapi;
- chosen_channel = gsm0808_chosen_channel(lchan->tch_mode, lchan->type);
- if (!chosen_channel)
- LOGP(DMSC, LOGL_ERROR, "Unknown lchan type or TCH mode: %s\n", gsm_lchan_name(lchan));
+ if (!conn->lchan) {
+ LOGP(DMSC, LOGL_ERROR,
+ "%s Called submit dtap without an lchan.\n",
+ bsc_subscr_name(conn->bsub));
+ msgb_free(msg);
+ return -1;
+ }
- /* Generate message */
- resp = gsm0808_create_ass_compl(lchan->abis_ip.ass_compl.rr_cause,
- chosen_channel,
- lchan->encr.alg_id, perm_spch,
- addr_local, sc_ptr, NULL);
+ sapi = link_id & 0x7;
+ msg->lchan = conn->lchan;
+ msg->dst = msg->lchan->ts->trx->rsl_link;
- if (!resp) {
- LOGPFSML(fi, LOGL_ERROR, "Failed to generate assignment completed message! (id=%i)\n",
- conn->sccp.conn_id);
+ /* If we are on a TCH and need to submit a SMS (on SAPI=3) we need to use the SACH */
+ if (allow_sacch && sapi != 0) {
+ if (conn->lchan->type == GSM_LCHAN_TCH_F || conn->lchan->type == GSM_LCHAN_TCH_H)
+ link_id |= 0x40;
}
- /* Add LCLS BSS-Status IE in case there is any LCLS status for this connection */
- bssmap_add_lcls_status_if_needed(conn, resp);
+ msg->l3h = msg->data;
+ /* is requested SAPI already up? */
+ if (conn->lchan->sapis[sapi] == LCHAN_SAPI_UNUSED) {
+ /* Establish L2 for additional SAPI */
+ OBSC_LINKID_CB(msg) = link_id;
+ if (rll_establish(msg->lchan, sapi, rll_ind_cb, msg) != 0) {
+ msgb_free(msg);
+ bsc_sapi_n_reject(conn, link_id);
+ return -1;
+ }
+ return 0;
+ } else {
+ /* Directly forward via RLL/RSL to BTS */
+ return rsl_data_request(msg, link_id);
+ }
+}
- sigtran_send(conn, resp, fi);
+static void gscon_bssmap_clear(struct gsm_subscriber_connection *conn,
+ enum gsm0808_cause cause)
+{
+ struct msgb *resp = gsm0808_create_clear_rqst(cause);
+ gscon_sigtran_send(conn, resp);
}
/* forward MT DTAP from BSSAP side to RSL side */
-static void submit_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, struct osmo_fsm_inst *fi)
+static void _submit_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg,
+ bool to_cache)
{
int rc;
- struct msgb *resp = NULL;
+ uint8_t link_id;
+ bool allow_sacch;
- OSMO_ASSERT(fi);
OSMO_ASSERT(msg);
OSMO_ASSERT(conn);
+ OSMO_ASSERT(conn->fi);
- rc = gsm0808_submit_dtap(conn, msg, OBSC_LINKID_CB(msg), 1);
- if (rc != 0) {
- LOGPFSML(fi, LOGL_ERROR, "Tx BSSMAP CLEAR REQUEST to MSC\n");
- resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_EQUIPMENT_FAILURE);
- sigtran_send(conn, resp, fi);
- osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
+ link_id = OBSC_LINKID_CB(msg);
+ allow_sacch = true;
+
+ if (to_cache) {
+ gscon_dtap_cache_add(conn, msg, link_id, allow_sacch);
return;
}
+
+ rc = gsm0808_submit_dtap(conn, msg, link_id, allow_sacch);
+ if (rc != 0) {
+ LOGPFSML(conn->fi, LOGL_ERROR,
+ "Failed to send DTAP to MS, Tx BSSMAP CLEAR REQUEST to MSC\n");
+ gscon_bssmap_clear(conn, GSM0808_CAUSE_EQUIPMENT_FAILURE);
+ conn_fsm_state_chg(ST_ACTIVE);
+ }
+}
+
+static void submit_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg)
+{
+ _submit_dtap(conn, msg, false);
+}
+
+static void cache_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg)
+{
+ _submit_dtap(conn, msg, true);
}
/* forward MO DTAP from RSL side to BSSAP side */
@@ -260,29 +280,76 @@ static void forward_dtap(struct gsm_subscriber_connection *conn, struct msgb *ms
OSMO_ASSERT(conn);
resp = gsm0808_create_dtap(msg, OBSC_LINKID_CB(msg));
- sigtran_send(conn, resp, fi);
+ gscon_sigtran_send(conn, resp);
+}
+
+void gscon_release_lchans(struct gsm_subscriber_connection *conn, bool do_sacch_deact)
+{
+ if (conn->ho.fi)
+ handover_end(conn, HO_RESULT_CONN_RELEASE);
+
+ assignment_reset(conn);
+
+ lchan_release(conn->lchan, do_sacch_deact, false, 0);
}
-/* In case there are open MGCP connections, toss
- * those connections */
-static void toss_mgcp_conn(struct gsm_subscriber_connection *conn, struct osmo_fsm_inst *fi)
+static void handle_bssap_n_connect(struct osmo_fsm_inst *fi, struct osmo_scu_prim *scu_prim)
{
- LOGPFSML(fi, LOGL_ERROR, "tossing all MGCP connections...\n");
+ struct gsm_subscriber_connection *conn = fi->priv;
+ struct msgb *msg = scu_prim->oph.msg;
+ struct bssmap_header *bs;
+ uint8_t bssmap_type;
+
+ msg->l3h = msgb_l2(msg);
+ if (!msgb_l3(msg)) {
+ LOGPFSML(fi, LOGL_ERROR, "internal error: no l3 in msg\n");
+ goto refuse;
+ }
+
+ if (msgb_l3len(msg) < sizeof(*bs)) {
+ LOGPFSML(fi, LOGL_NOTICE, "message too short for BSSMAP header (%u < %zu)\n",
+ msgb_l3len(msg), sizeof(*bs));
+ goto refuse;
+ }
- if (conn->user_plane.fi_bts) {
- mgcp_conn_delete(conn->user_plane.fi_bts);
- conn->user_plane.fi_bts = NULL;
+ bs = (struct bssmap_header*)msgb_l3(msg);
+ if (msgb_l3len(msg) < (bs->length + sizeof(*bs))) {
+ LOGPFSML(fi, LOGL_NOTICE,
+ "message too short for length indicated in BSSMAP header (%u < %u)\n",
+ msgb_l3len(msg), bs->length);
+ goto refuse;
}
- if (conn->user_plane.fi_msc) {
- mgcp_conn_delete(conn->user_plane.fi_msc);
- conn->user_plane.fi_msc = NULL;
+ switch (bs->type) {
+ case BSSAP_MSG_BSS_MANAGEMENT:
+ break;
+ default:
+ LOGPFSML(fi, LOGL_NOTICE,
+ "message type not allowed for N-CONNECT: %s\n", gsm0808_bssap_name(bs->type));
+ goto refuse;
}
- if (conn->user_plane.mgw_endpoint) {
- talloc_free(conn->user_plane.mgw_endpoint);
- conn->user_plane.mgw_endpoint = NULL;
+ msg->l4h = &msg->l3h[sizeof(*bs)];
+ bssmap_type = msg->l4h[0];
+
+ LOGPFSML(fi, LOGL_DEBUG, "Rx N-CONNECT: %s: %s\n", gsm0808_bssap_name(bs->type),
+ gsm0808_bssmap_name(bssmap_type));
+
+ switch (bssmap_type) {
+ case BSS_MAP_MSG_HANDOVER_RQST:
+ /* Inter-BSC MT Handover Request, another BSS is handovering to us. */
+ handover_start_inter_bsc_mt(conn, msg);
+ return;
+ default:
+ break;
}
+
+ LOGPFSML(fi, LOGL_NOTICE, "No support for N-CONNECT: %s: %s\n",
+ gsm0808_bssap_name(bs->type), gsm0808_bssmap_name(bssmap_type));
+refuse:
+ osmo_sccp_tx_disconn(conn->sccp.msc->a.sccp_user, scu_prim->u.connect.conn_id,
+ &scu_prim->u.connect.called_addr, 0);
+ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
}
static void gscon_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
@@ -291,6 +358,7 @@ static void gscon_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
struct osmo_scu_prim *scu_prim = NULL;
struct msgb *msg = NULL;
int rc;
+ enum handover_result ho_result;
switch (event) {
case GSCON_EV_A_CONN_REQ:
@@ -307,8 +375,9 @@ static void gscon_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
} else {
/* SCCP T(conn est) is 1-2 minutes, way too long. The MS will timeout
* using T3210 (20s), T3220 (5s) or T3230 (10s) */
- osmo_fsm_inst_state_chg(fi, ST_WAIT_CC, 20, 993210);
+ conn_fsm_state_chg(ST_WAIT_CC);
}
+ gscon_update_id(conn);
break;
case GSCON_EV_A_CONN_IND:
scu_prim = data;
@@ -322,498 +391,269 @@ static void gscon_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
/* FIXME: Extract optional IMSI and update FSM using osmo_fsm_inst_set_id()
* related: OS2969 (same as above) */
- LOGPFSML(fi, LOGL_NOTICE, "No support for MSC-originated SCCP Connections yet\n");
- osmo_sccp_tx_disconn(conn->sccp.msc->a.sccp_user, scu_prim->u.connect.conn_id,
- &scu_prim->u.connect.called_addr, 0);
- osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
+ handle_bssap_n_connect(fi, scu_prim);
+ gscon_update_id(conn);
break;
+ case GSCON_EV_HANDOVER_END:
+ ho_result = HO_RESULT_ERROR;
+ if (data)
+ ho_result = *(enum handover_result*)data;
+ if (ho_result == HO_RESULT_OK) {
+ /* In this case the ho struct should still be populated. */
+ if (conn->ho.scope & HO_INTER_BSC_MT) {
+ /* Done with establishing a conn where we accept another BSC's MS via
+ * inter-BSC handover */
+
+ osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
+ gscon_dtap_cache_flush(conn, 1);
+ return;
+ }
+ LOG_HO(conn, LOGL_ERROR,
+ "Conn is in state %s, the only accepted handover kind is inter-BSC MT",
+ osmo_fsm_inst_state_name(conn->fi));
+ }
+ gscon_bssmap_clear(conn, GSM0808_CAUSE_EQUIPMENT_FAILURE);
+ osmo_fsm_inst_state_chg(fi, ST_CLEARING, 60, 999);
+ return;
default:
OSMO_ASSERT(false);
- break;
}
}
/* We've sent the CONNECTION.req to the SCCP provider and are waiting for CC from MSC */
static void gscon_fsm_wait_cc(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
+ struct gsm_subscriber_connection *conn = fi->priv;
switch (event) {
case GSCON_EV_A_CONN_CFM:
/* MSC has confirmed the connection, we now change into the
* active state and wait there for further operations */
- osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
+ conn_fsm_state_chg(ST_ACTIVE);
/* if there's user payload, forward it just like EV_MT_DTAP */
/* FIXME: Question: if there's user payload attached to the CC, forward it like EV_MT_DTAP? */
break;
default:
OSMO_ASSERT(false);
- break;
- }
-}
-
-static const char *get_mgw_ep_name(struct gsm_subscriber_connection *conn)
-{
- static char ep_name[256];
- struct bsc_msc_data *msc = conn->sccp.msc;
-
- switch (conn->sccp.msc->a.asp_proto) {
- case OSMO_SS7_ASP_PROT_IPA:
- /* derive endpoint name from CIC on A interface side */
- snprintf(ep_name, sizeof(ep_name), "%x@mgw",
- mgcp_port_to_cic(conn->user_plane.rtp_port, msc->rtp_base));
- break;
- default:
- /* use dynamic RTPBRIDGE endpoint allocation in MGW */
- osmo_strlcpy(ep_name, ENDPOINT_ID, sizeof(ep_name));
- break;
}
- return ep_name;
}
-#define assignment_failed(fi, cause) \
- _assignment_failed(fi, cause, __FILE__, __LINE__)
-static void _assignment_failed(struct osmo_fsm_inst *fi, enum gsm0808_cause cause,
- const char *file, int line)
+static void gscon_fsm_active_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
struct gsm_subscriber_connection *conn = fi->priv;
- struct msgb *resp = NULL;
-
- LOGPFSMLSRC(fi, LOGL_ERROR, file, line, "Assignment failed: %s\n", gsm0808_cause_name(cause));
-
- resp = gsm0808_create_assignment_failure(cause, NULL);
- sigtran_send(conn, resp, fi);
- if (fi->state != ST_ACTIVE)
- osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
+ if (!conn->lchan)
+ gscon_bssmap_clear(conn, GSM0808_CAUSE_EQUIPMENT_FAILURE);
}
/* We're on an active subscriber connection, passing DTAP back and forth */
static void gscon_fsm_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
struct gsm_subscriber_connection *conn = fi->priv;
- struct msgb *resp = NULL;
- struct mgcp_conn_peer conn_peer;
- int rc;
+ struct gsm_bts *bts;
switch (event) {
- case GSCON_EV_A_ASSIGNMENT_CMD:
- /* MSC requests us to perform assignment, this code section is
- * triggered via signal GSCON_EV_A_ASSIGNMENT_CMD from
- * bssmap_handle_assignm_req() in osmo_bsc_bssap.c, which does
- * the parsing of incoming assignment requests. */
-
- LOGPFSML(fi, LOGL_NOTICE, "Channel assignment: chan_mode=%s, full_rate=%i\n",
- get_value_string(gsm48_chan_mode_names, conn->user_plane.chan_mode),
- conn->user_plane.full_rate);
-
- /* FIXME: We need to check if current channel is sufficient. If
- * yes, do MODIFY. If not, do assignment (see commented lines below) */
-
- switch (conn->user_plane.chan_mode) {
- case GSM48_CMODE_SPEECH_V1:
- case GSM48_CMODE_SPEECH_EFR:
- case GSM48_CMODE_SPEECH_AMR:
- /* A voice channel is requested, so we run down the
- * mgcp-ass-mgcp state-chain (see FIXME above) */
- memset(&conn_peer, 0, sizeof(conn_peer));
- conn_peer.call_id = conn->sccp.conn_id;
- osmo_strlcpy(conn_peer.endpoint, get_mgw_ep_name(conn), sizeof(conn_peer.endpoint));
-
- /* (Pre)Change state and create the connection */
- osmo_fsm_inst_state_chg(fi, ST_WAIT_CRCX_BTS, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
- conn->user_plane.fi_bts =
- mgcp_conn_create(conn->network->mgw.client, fi, GSCON_EV_MGW_FAIL_BTS,
- GSCON_EV_MGW_CRCX_RESP_BTS, &conn_peer);
- if (!conn->user_plane.fi_bts) {
- assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE);
- return;
- }
- break;
- case GSM48_CMODE_SIGN:
- /* A signalling channel is requested, so we perform the
- * channel assignment directly without performing any
- * MGCP actions. ST_WAIT_ASS_CMPL will see by the
- * conn->user_plane.chan_mode parameter that this
- * assignment is for a signalling channel and will then
- * change back to ST_ACTIVE (here) immediately. */
- rc = gsm0808_assign_req(conn, conn->user_plane.chan_mode,
- conn->user_plane.full_rate);
-
- if (rc == 1) {
- send_ass_compl(conn->lchan, fi, false);
- return;
- } else if (rc != 0) {
- assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE);
- return;
- }
- osmo_fsm_inst_state_chg(fi, ST_WAIT_ASS_CMPL, conn->network->T10, 10);
- break;
- default:
- /* An unsupported channel is requested, so we have to
- * reject this request by sending an assignment failure
- * message immediately */
- LOGPFSML(fi, LOGL_ERROR, "Requested channel mode is not supported! chan_mode=%s full_rate=%d\n",
- get_value_string(gsm48_chan_mode_names, conn->user_plane.chan_mode),
- conn->user_plane.full_rate);
-
- /* The requested channel mode is not supported */
- assignment_failed(fi, GSM0808_CAUSE_REQ_CODEC_TYPE_OR_CONFIG_NOT_SUPP);
- break;
- }
- break;
- case GSCON_EV_HO_START:
- rc = bsc_handover_start_gscon(conn);
- if (rc) {
- resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_EQUIPMENT_FAILURE);
- sigtran_send(conn, resp, fi);
- osmo_fsm_inst_state_chg(fi, ST_CLEARING, 0, 0);
- return;
- }
+ case GSCON_EV_ASSIGNMENT_START:
+ bts = conn->lchan? conn->lchan->ts->trx->bts : NULL;
- /* Note: No timeout is set here, T3103 in handover_logic.c
- * will generate a GSCON_EV_HO_TIMEOUT event should the
- * handover time out, so we do not need another timeout
- * here (maybe its worth to think about giving GSCON
- * more power over the actual handover process). */
- osmo_fsm_inst_state_chg(fi, ST_WAIT_HO_COMPL, 0, 0);
- break;
- case GSCON_EV_A_HO_REQ:
- /* FIXME: reject any handover requests with HO FAIL until implemented */
- break;
- case GSCON_EV_MO_DTAP:
- forward_dtap(conn, (struct msgb *)data, fi);
- break;
- case GSCON_EV_MT_DTAP:
- submit_dtap(conn, (struct msgb *)data, fi);
- break;
- case GSCON_EV_TX_SCCP:
- sigtran_send(conn, (struct msgb *)data, fi);
- break;
- default:
- OSMO_ASSERT(false);
- break;
- }
-}
-
-/* Before we may start the channel assignment we need to get an IP/Port for the
- * RTP connection from the MGW */
-static void gscon_fsm_wait_crcx_bts(struct osmo_fsm_inst *fi, uint32_t event, void *data)
-{
- struct gsm_subscriber_connection *conn = fi->priv;
- struct mgcp_conn_peer *conn_peer = NULL;
- int rc;
-
- switch (event) {
- case GSCON_EV_MGW_CRCX_RESP_BTS:
- conn_peer = data;
-
- /* Check if the MGW has assigned an enpoint to us, otherwise we
- * can not proceed. */
- if (strlen(conn_peer->endpoint) <= 0) {
- assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE);
+ if (!bts) {
+ LOGPFSML(fi, LOGL_ERROR, "Cannot do assignment, no active BTS\n");
return;
}
- /* Memorize the endpoint name we got assigned from the MGW.
- * When the BTS sided connection is done, we need to create
- * a second connection on that same endpoint, so we need
- * to know its ID */
- if (!conn->user_plane.mgw_endpoint)
- conn->user_plane.mgw_endpoint = talloc_zero_size(conn, MGCP_ENDPOINT_MAXLEN);
- OSMO_ASSERT(conn->user_plane.mgw_endpoint);
- osmo_strlcpy(conn->user_plane.mgw_endpoint, conn_peer->endpoint, MGCP_ENDPOINT_MAXLEN);
-
- /* Store the IP-Address and the port the MGW assigned to us,
- * then start the channel assignment. */
- conn->user_plane.rtp_port = conn_peer->port;
- conn->user_plane.rtp_ip = osmo_ntohl(inet_addr(conn_peer->addr));
- rc = gsm0808_assign_req(conn, conn->user_plane.chan_mode, conn->user_plane.full_rate);
- if (rc != 0) {
- assignment_failed(fi, GSM0808_CAUSE_RQSTED_SPEECH_VERSION_UNAVAILABLE);
- return;
- }
+ /* Rely on assignment_fsm timeout */
+ osmo_fsm_inst_state_chg(fi, ST_ASSIGNMENT, 0, 0);
+ assignment_fsm_start(conn, bts, data);
+ return;
- osmo_fsm_inst_state_chg(fi, ST_WAIT_ASS_CMPL, conn->network->T10, 10);
+ case GSCON_EV_HANDOVER_START:
+ rate_ctr_inc(&conn->network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_ATTEMPTED]);
+ /* Rely on handover_fsm timeout */
+ osmo_fsm_inst_state_chg(fi, ST_HANDOVER, 0, 0);
break;
+
case GSCON_EV_MO_DTAP:
forward_dtap(conn, (struct msgb *)data, fi);
break;
case GSCON_EV_MT_DTAP:
- submit_dtap(conn, (struct msgb *)data, fi);
+ submit_dtap(conn, (struct msgb *)data);
break;
case GSCON_EV_TX_SCCP:
- sigtran_send(conn, (struct msgb *)data, fi);
+ gscon_sigtran_send(conn, (struct msgb *)data);
break;
default:
OSMO_ASSERT(false);
- break;
}
}
-/* We're waiting for an ASSIGNMENT COMPLETE from MS */
-static void gscon_fsm_wait_ass_cmpl(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+static void gscon_fsm_assignment(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
struct gsm_subscriber_connection *conn = fi->priv;
- struct gsm_lchan *lchan = conn->lchan;
- struct mgcp_conn_peer conn_peer;
- struct in_addr addr;
- int rc;
switch (event) {
- case GSCON_EV_RR_ASS_COMPL:
- switch (conn->user_plane.chan_mode) {
- case GSM48_CMODE_SPEECH_V1:
- case GSM48_CMODE_SPEECH_EFR:
- case GSM48_CMODE_SPEECH_AMR:
- /* FIXME: What if we are using SCCP-Lite? */
-
- /* We are dealing with a voice channel, so we can not
- * confirm the assignment directly. We must first do
- * some final steps on the MGCP side. */
-
- /* Prepare parameters with the information we got during the assignment */
- memset(&conn_peer, 0, sizeof(conn_peer));
- addr.s_addr = osmo_ntohl(lchan->abis_ip.bound_ip);
- osmo_strlcpy(conn_peer.addr, inet_ntoa(addr), sizeof(conn_peer.addr));
- conn_peer.port = lchan->abis_ip.bound_port;
-
- /* (Pre)Change state and modify the connection */
- osmo_fsm_inst_state_chg(fi, ST_WAIT_MDCX_BTS, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
- rc = mgcp_conn_modify(conn->user_plane.fi_bts, GSCON_EV_MGW_MDCX_RESP_BTS, &conn_peer);
- if (rc != 0) {
- assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE);
- return;
- }
- break;
- case GSM48_CMODE_SIGN:
- /* Confirm the successful assignment on BSSMAP and
- * change back into active state */
- send_ass_compl(lchan, fi, false);
- osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
- break;
- default:
- /* Unsupported modes should have been already filtered
- * by gscon_fsm_active(). If we reach the default
- * section here anyway than some unsupported mode must
- * have made it into the FSM, this would be a bug, so
- * we fire an assertion here */
- OSMO_ASSERT(false);
- break;
- }
+ case GSCON_EV_ASSIGNMENT_END:
+ osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
+ gscon_dtap_cache_flush(conn, 1);
+ return;
- break;
- case GSCON_EV_RR_ASS_FAIL:
- {
- enum gsm0808_cause cause = GSM0808_CAUSE_RQSTED_TERRESTRIAL_RESOURCE_UNAVAILABLE;
- if (data)
- cause = *((enum gsm0808_cause*)data);
- assignment_failed(fi, cause);
- }
- break;
case GSCON_EV_MO_DTAP:
forward_dtap(conn, (struct msgb *)data, fi);
break;
case GSCON_EV_MT_DTAP:
- submit_dtap(conn, (struct msgb *)data, fi);
+ /* cache until assignment is done */
+ cache_dtap(conn, (struct msgb *)data);
break;
case GSCON_EV_TX_SCCP:
- sigtran_send(conn, (struct msgb *)data, fi);
+ gscon_sigtran_send(conn, (struct msgb *)data);
break;
default:
OSMO_ASSERT(false);
- break;
}
}
-/* We are waiting for the MGW response to the MDCX */
-static void gscon_fsm_wait_mdcx_bts(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+static void gscon_fsm_handover(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
struct gsm_subscriber_connection *conn = fi->priv;
- struct mgcp_conn_peer conn_peer;
- struct sockaddr_in *sin = NULL;
switch (event) {
- case GSCON_EV_MGW_MDCX_RESP_BTS:
-
- /* Prepare parameters with the connection information we got
- * with the assignment command */
- memset(&conn_peer, 0, sizeof(conn_peer));
- conn_peer.call_id = conn->sccp.conn_id;
- sin = (struct sockaddr_in *)&conn->user_plane.aoip_rtp_addr_remote;
- conn_peer.port = osmo_ntohs(sin->sin_port);
- osmo_strlcpy(conn_peer.addr, inet_ntoa(sin->sin_addr), sizeof(conn_peer.addr));
-
- /* Make sure we use the same endpoint where we created the
- * BTS connection. */
- osmo_strlcpy(conn_peer.endpoint, conn->user_plane.mgw_endpoint, sizeof(conn_peer.endpoint));
-
- switch (conn->sccp.msc->a.asp_proto) {
- case OSMO_SS7_ASP_PROT_IPA:
- /* Send assignment complete message to the MSC */
- send_ass_compl(conn->lchan, fi, true);
- osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
- break;
- default:
- /* (Pre)Change state and create the connection */
- osmo_fsm_inst_state_chg(fi, ST_WAIT_CRCX_MSC, MGCP_MGW_TIMEOUT,
- MGCP_MGW_TIMEOUT_TIMER_NR);
- conn->user_plane.fi_msc = mgcp_conn_create(conn->network->mgw.client, fi,
- GSCON_EV_MGW_FAIL_MSC,
- GSCON_EV_MGW_CRCX_RESP_MSC, &conn_peer);
- if (!conn->user_plane.fi_msc) {
- assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE);
- return;
- }
- break;
- }
+ case GSCON_EV_HANDOVER_END:
+ osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
+ gscon_dtap_cache_flush(conn, 1);
+ return;
- break;
case GSCON_EV_MO_DTAP:
forward_dtap(conn, (struct msgb *)data, fi);
break;
case GSCON_EV_MT_DTAP:
- submit_dtap(conn, (struct msgb *)data, fi);
+ /* cache until handover is done */
+ cache_dtap(conn, (struct msgb *)data);
break;
case GSCON_EV_TX_SCCP:
- sigtran_send(conn, (struct msgb *)data, fi);
+ gscon_sigtran_send(conn, (struct msgb *)data);
break;
default:
OSMO_ASSERT(false);
- break;
}
}
-static void gscon_fsm_wait_crcx_msc(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+static bool same_mgw_info(const struct mgcp_conn_peer *a, const struct mgcp_conn_peer *b)
{
- struct gsm_subscriber_connection *conn = fi->priv;
- struct mgcp_conn_peer *conn_peer = NULL;
- struct gsm_lchan *lchan = conn->lchan;
- struct sockaddr_in *sin = NULL;
-
- switch (event) {
- case GSCON_EV_MGW_CRCX_RESP_MSC:
- conn_peer = data;
+ if (!a || !b)
+ return false;
+ if (a == b)
+ return true;
+ if (strcmp(a->addr, b->addr))
+ return false;
+ if (a->port != b->port)
+ return false;
+ if (a->call_id != b->call_id)
+ return false;
+ return true;
+}
- /* Store address information we got in response from the CRCX command. */
- sin = (struct sockaddr_in *)&conn->user_plane.aoip_rtp_addr_local;
- sin->sin_family = AF_INET;
- sin->sin_addr.s_addr = inet_addr(conn_peer->addr);
- sin->sin_port = osmo_ntohs(conn_peer->port);
+/* Make sure a conn->user_plane.mgw_endpoint is allocated with the proper mgw endpoint name. */
+struct mgw_endpoint *gscon_ensure_mgw_endpoint(struct gsm_subscriber_connection *conn)
+{
+ struct bsc_msc_data *msc;
+ if (!conn || !conn->sccp.msc)
+ return NULL;
+ if (conn->user_plane.mgw_endpoint)
+ return conn->user_plane.mgw_endpoint;
- /* Send assignment complete message to the MSC */
- send_ass_compl(lchan, fi, true);
+ msc = conn->sccp.msc;
- osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
+ switch (msc->a.asp_proto) {
+ case OSMO_SS7_ASP_PROT_IPA:
+ /* derive endpoint name from CIC on A interface side */
+ conn->user_plane.mgw_endpoint =
+ mgw_endpoint_alloc(conn->fi, GSCON_EV_FORGET_MGW_ENDPOINT,
+ conn->network->mgw.client, conn->fi->id,
+ "%x@mgw",
+ mgcp_port_to_cic(conn->user_plane.rtp_port, msc->rtp_base));
break;
- case GSCON_EV_MO_DTAP:
- forward_dtap(conn, (struct msgb *)data, fi);
- break;
- case GSCON_EV_MT_DTAP:
- submit_dtap(conn, (struct msgb *)data, fi);
- break;
- case GSCON_EV_TX_SCCP:
- sigtran_send(conn, (struct msgb *)data, fi);
- break;
default:
- OSMO_ASSERT(false);
+ /* use dynamic RTPBRIDGE endpoint allocation in MGW */
+ conn->user_plane.mgw_endpoint =
+ mgw_endpoint_alloc(conn->fi, GSCON_EV_FORGET_MGW_ENDPOINT,
+ conn->network->mgw.client, conn->fi->id,
+ "rtpbridge/*@mgw");
break;
}
-}
-static void gscon_fsm_clearing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
-{
- struct gsm_subscriber_connection *conn = fi->priv;
- struct msgb *resp;
-
- switch (event) {
- case GSCON_EV_RSL_CLEAR_COMPL:
- resp = gsm0808_create_clear_complete();
- sigtran_send(conn, resp, fi);
- /* we cannot terminate the FSM here, as that would send N-DISCCONNET.req
- * and 3GPP TS 48.006 Section 9.2 clearly states that SCCP connections must
- * always be released from the MSC side*/
- break;
- default:
- OSMO_ASSERT(false);
- break;
- }
+ return conn->user_plane.mgw_endpoint;
}
-/* Wait for the handover logic to tell us whether the handover completed,
- * failed or has timed out */
-static void gscon_fsm_wait_ho_compl(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+bool gscon_connect_mgw_to_msc(struct gsm_subscriber_connection *conn,
+ const char *addr, uint16_t port,
+ struct osmo_fsm_inst *notify,
+ uint32_t event_success, uint32_t event_failure,
+ void *notify_data,
+ struct mgwep_ci **created_ci)
{
- struct gsm_subscriber_connection *conn = fi->priv;
- struct mgcp_conn_peer conn_peer;
- struct gsm_lchan *lchan = conn->lchan;
- struct in_addr addr;
- struct msgb *resp;
int rc;
+ struct mgwep_ci *ci;
+ struct mgcp_conn_peer mgw_info = {
+ .port = port,
+ .call_id = conn->sccp.conn_id,
+ };
+ enum mgcp_verb verb;
+
+ if (created_ci)
+ *created_ci = NULL;
+
+ rc = osmo_strlcpy(mgw_info.addr, addr, sizeof(mgw_info.addr));
+ if (rc <= 0 || rc >= sizeof(mgw_info.addr)) {
+ LOGPFSML(conn->fi, LOGL_ERROR, "Failed to compose MGW endpoint address for MGW -> MSC\n");
+ return false;
+ }
- switch (event) {
- case GSCON_EV_HO_COMPL:
- /* The handover logic informs us that the handover has been
- * completet. Now we have to tell the MGW the IP/Port on the
- * new BTS so that the uplink RTP traffic can be redirected
- * there. */
-
- /* Prepare parameters with the information we got during the
- * handover procedure (via IPACC) */
- memset(&conn_peer, 0, sizeof(conn_peer));
- addr.s_addr = osmo_ntohl(lchan->abis_ip.bound_ip);
- osmo_strlcpy(conn_peer.addr, inet_ntoa(addr), sizeof(conn_peer.addr));
- conn_peer.port = lchan->abis_ip.bound_port;
-
- /* (Pre)Change state and modify the connection */
- osmo_fsm_inst_state_chg(fi, ST_WAIT_MDCX_BTS_HO, MGCP_MGW_TIMEOUT, MGCP_MGW_HO_TIMEOUT_TIMER_NR);
- rc = mgcp_conn_modify(conn->user_plane.fi_bts, GSCON_EV_MGW_MDCX_RESP_BTS, &conn_peer);
- if (rc != 0) {
- resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_EQUIPMENT_FAILURE);
- sigtran_send(conn, resp, fi);
- osmo_fsm_inst_state_chg(fi, ST_CLEARING, 0, 0);
- return;
+ ci = conn->user_plane.mgw_endpoint_ci_msc;
+ if (ci) {
+ const struct mgcp_conn_peer *prev_crcx_info = mgwep_ci_get_rtp_info(ci);
+
+ if (!conn->user_plane.mgw_endpoint) {
+ LOGPFSML(conn->fi, LOGL_ERROR, "Internal error: conn has a CI but no endoint\n");
+ return false;
}
- break;
- case GSCON_EV_HO_TIMEOUT:
- case GSCON_EV_HO_FAIL:
- /* The handover logic informs us that the handover failed for
- * some reason. This means the phone stays on the TS/BTS on
- * which it currently is. We will change back to the active
- * state again as there are no further operations needed */
- osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
- break;
- default:
- OSMO_ASSERT(false);
- break;
- }
-}
-/* Wait for the MGW to confirm handover related modification of the connection
- * parameters */
-static void gscon_fsm_wait_mdcx_bts_ho(struct osmo_fsm_inst *fi, uint32_t event, void *data)
-{
- struct gsm_subscriber_connection *conn = fi->priv;
+ if (!prev_crcx_info) {
+ LOGPFSML(conn->fi, LOGL_ERROR, "There already is an MGW connection for the MSC side,"
+ " but it seems to be broken. Will not CRCX another one (%s)\n",
+ mgwep_ci_name(ci));
+ return false;
+ }
- switch (event) {
- case GSCON_EV_MGW_MDCX_RESP_BTS:
- /* The MGW has confirmed the handover MDCX, and the handover
- * is now also done on the RTP side. We may now change back
- * to the active state. */
- osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
- break;
- case GSCON_EV_MO_DTAP:
- forward_dtap(conn, (struct msgb *)data, fi);
- break;
- case GSCON_EV_MT_DTAP:
- submit_dtap(conn, (struct msgb *)data, fi);
- break;
- case GSCON_EV_TX_SCCP:
- sigtran_send(conn, (struct msgb *)data, fi);
- break;
- default:
- OSMO_ASSERT(false);
- break;
+ if (same_mgw_info(&mgw_info, prev_crcx_info)) {
+ LOGPFSML(conn->fi, LOGL_DEBUG,
+ "MSC side MGW endpoint ci is already configured to %s",
+ mgwep_ci_name(ci));
+ return true;
+ }
+
+ verb = MGCP_VERB_MDCX;
+ } else
+ verb = MGCP_VERB_CRCX;
+
+ gscon_ensure_mgw_endpoint(conn);
+
+ if (!conn->user_plane.mgw_endpoint) {
+ LOGPFSML(conn->fi, LOGL_ERROR, "Unable to allocate endpoint info\n");
+ return false;
+ }
+
+ if (!ci) {
+ ci = mgw_endpoint_ci_add(conn->user_plane.mgw_endpoint, "to-MSC");
+ if (created_ci)
+ *created_ci = ci;
+ conn->user_plane.mgw_endpoint_ci_msc = ci;
}
+ if (!ci) {
+ LOGPFSML(conn->fi, LOGL_ERROR, "Unable to allocate endpoint CI info\n");
+ return false;
+ }
+
+ mgw_endpoint_ci_request(ci, verb, &mgw_info, notify, event_success, event_failure, notify_data);
+ return true;
}
#define EV_TRANSPARENT_SCCP S(GSCON_EV_TX_SCCP) | S(GSCON_EV_MO_DTAP) | S(GSCON_EV_MT_DTAP)
@@ -821,8 +661,9 @@ static void gscon_fsm_wait_mdcx_bts_ho(struct osmo_fsm_inst *fi, uint32_t event,
static const struct osmo_fsm_state gscon_fsm_states[] = {
[ST_INIT] = {
.name = "INIT",
- .in_event_mask = S(GSCON_EV_A_CONN_REQ) | S(GSCON_EV_A_CONN_IND),
- .out_state_mask = S(ST_WAIT_CC),
+ .in_event_mask = S(GSCON_EV_A_CONN_REQ) | S(GSCON_EV_A_CONN_IND)
+ | S(GSCON_EV_HANDOVER_END),
+ .out_state_mask = S(ST_WAIT_CC) | S(ST_ACTIVE) | S(ST_CLEARING),
.action = gscon_fsm_init,
},
[ST_WAIT_CC] = {
@@ -833,133 +674,126 @@ static const struct osmo_fsm_state gscon_fsm_states[] = {
},
[ST_ACTIVE] = {
.name = "ACTIVE",
- .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_A_ASSIGNMENT_CMD) |
- S(GSCON_EV_A_HO_REQ) | S(GSCON_EV_HO_START),
- .out_state_mask = S(ST_CLEARING) | S(ST_WAIT_CRCX_BTS) | S(ST_WAIT_ASS_CMPL) |
- S(ST_WAIT_MO_HO_CMD) | S(ST_WAIT_HO_COMPL),
+ .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_ASSIGNMENT_START) |
+ S(GSCON_EV_HANDOVER_START),
+ .out_state_mask = S(ST_CLEARING) | S(ST_ASSIGNMENT) |
+ S(ST_HANDOVER),
+ .onenter = gscon_fsm_active_onenter,
.action = gscon_fsm_active,
},
- [ST_WAIT_CRCX_BTS] = {
- .name = "WAIT_CRCX_BTS",
- .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_MGW_CRCX_RESP_BTS),
- .out_state_mask = S(ST_ACTIVE) | S(ST_WAIT_ASS_CMPL),
- .action = gscon_fsm_wait_crcx_bts,
+ [ST_ASSIGNMENT] = {
+ .name = "ASSIGNMENT",
+ .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_ASSIGNMENT_END),
+ .out_state_mask = S(ST_ACTIVE) | S(ST_CLEARING),
+ .action = gscon_fsm_assignment,
},
- [ST_WAIT_ASS_CMPL] = {
- .name = "WAIT_ASS_CMPL",
- .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_RR_ASS_COMPL) | S(GSCON_EV_RR_ASS_FAIL),
- .out_state_mask = S(ST_ACTIVE) | S(ST_WAIT_MDCX_BTS),
- .action = gscon_fsm_wait_ass_cmpl,
- },
- [ST_WAIT_MDCX_BTS] = {
- .name = "WAIT_MDCX_BTS",
- .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_MGW_MDCX_RESP_BTS),
- .out_state_mask = S(ST_ACTIVE) | S(ST_WAIT_CRCX_MSC),
- .action = gscon_fsm_wait_mdcx_bts,
- },
- [ST_WAIT_CRCX_MSC] = {
- .name = "WAIT_CRCX_MSC",
- .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_MGW_CRCX_RESP_MSC),
- .out_state_mask = S(ST_ACTIVE),
- .action = gscon_fsm_wait_crcx_msc,
+ [ST_HANDOVER] = {
+ .name = "HANDOVER",
+ .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_HANDOVER_END),
+ .out_state_mask = S(ST_ACTIVE) | S(ST_CLEARING),
+ .action = gscon_fsm_handover,
},
[ST_CLEARING] = {
.name = "CLEARING",
- .in_event_mask = S(GSCON_EV_RSL_CLEAR_COMPL),
- .action = gscon_fsm_clearing,
+ /* dead end state */
},
+};
- /* TODO: external handover, probably it makes sense to break up the
- * program flow in handover_logic.c a bit and handle some of the logic
- * here? */
- [ST_WAIT_MT_HO_ACC] = {
- .name = "WAIT_MT_HO_ACC",
- },
- [ST_WAIT_MT_HO_COMPL] = {
- .name = "WAIT_MT_HO_COMPL",
- },
- [ST_WAIT_MO_HO_CMD] = {
- .name = "WAIT_MO_HO_CMD",
- },
- [ST_MO_HO_PROCEEDING] = {
- .name = "MO_HO_PROCEEDING",
- },
+void gscon_change_primary_lchan(struct gsm_subscriber_connection *conn, struct gsm_lchan **new_lchan)
+{
+ /* On release, do not receive release events that look like the primary lchan is gone. */
+ struct gsm_lchan *old_lchan = conn->lchan;
+ if (old_lchan)
+ old_lchan->conn = NULL;
- /* Internal handover */
- [ST_WAIT_HO_COMPL] = {
- .name = "WAIT_HO_COMPL",
- .in_event_mask = S(GSCON_EV_HO_COMPL) | S(GSCON_EV_HO_FAIL) | S(GSCON_EV_HO_TIMEOUT),
- .out_state_mask = S(ST_ACTIVE) | S(ST_WAIT_MDCX_BTS_HO) | S(ST_CLEARING),
- .action = gscon_fsm_wait_ho_compl,
- },
- [ST_WAIT_MDCX_BTS_HO] = {
- .name = "WAIT_MDCX_BTS_HO",
- .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_MGW_MDCX_RESP_BTS),
- .action = gscon_fsm_wait_mdcx_bts_ho,
- .out_state_mask = S(ST_ACTIVE),
- },
-};
+ conn->lchan = *new_lchan;
+ conn->lchan->conn = conn;
+ *new_lchan = NULL;
+
+ if (old_lchan)
+ lchan_release(old_lchan, false, false, 0);
+}
+
+void gscon_lchan_releasing(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan)
+{
+ if (!lchan)
+ return;
+ if (conn->assignment.new_lchan == lchan) {
+ if (conn->assignment.fi)
+ osmo_fsm_inst_dispatch(conn->assignment.fi, ASSIGNMENT_EV_LCHAN_ERROR, lchan);
+ conn->assignment.new_lchan = NULL;
+ }
+ if (conn->ho.mt.new_lchan == lchan) {
+ if (conn->ho.fi)
+ osmo_fsm_inst_dispatch(conn->ho.fi, HO_EV_LCHAN_ERROR, lchan);
+ conn->ho.mt.new_lchan = NULL;
+ }
+ if (conn->lchan == lchan)
+ conn->lchan = NULL;
+ if (!conn->lchan) {
+ osmo_fsm_inst_state_chg(conn->fi, ST_CLEARING, 60, 999);
+ gscon_bssmap_clear(conn, GSM0808_CAUSE_EQUIPMENT_FAILURE);
+ }
+}
+
+/* An lchan was deallocated. */
+void gscon_forgetx_lchan(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan)
+{
+ if (!lchan)
+ return;
+ if (conn->assignment.new_lchan == lchan)
+ conn->assignment.new_lchan = NULL;
+ if (conn->ho.mt.new_lchan == lchan)
+ conn->ho.mt.new_lchan = NULL;
+ if (conn->lchan == lchan)
+ conn->lchan = NULL;
+ if (!conn->lchan)
+ gscon_bssmap_clear(conn, GSM0808_CAUSE_EQUIPMENT_FAILURE);
+}
+
+static void gscon_forget_mgw_endpoint(struct gsm_subscriber_connection *conn)
+{
+ conn->user_plane.mgw_endpoint = NULL;
+ conn->user_plane.mgw_endpoint_ci_msc = NULL;
+ lchan_forgetx_mgw_endpoint(conn->lchan);
+ lchan_forgetx_mgw_endpoint(conn->assignment.new_lchan);
+ lchan_forgetx_mgw_endpoint(conn->ho.mt.new_lchan);
+}
static void gscon_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
struct gsm_subscriber_connection *conn = fi->priv;
- struct msgb *resp = NULL;
-
- /* When a connection on the MGW fails, make sure that the reference
- * in our book-keeping is erased. */
- switch (event) {
- case GSCON_EV_MGW_FAIL_BTS:
- conn->user_plane.fi_bts = NULL;
- break;
- case GSCON_EV_MGW_FAIL_MSC:
- conn->user_plane.fi_msc = NULL;
- break;
- }
/* Regular allstate event processing */
switch (event) {
- case GSCON_EV_MGW_FAIL_BTS:
- case GSCON_EV_MGW_FAIL_MSC:
- /* Note: An MGW connection die per definition at any time.
- * However, if it dies during the assignment we must return
- * with an assignment failure */
- OSMO_ASSERT(fi->state != ST_INIT && fi->state != ST_WAIT_CC);
- if (fi->state == ST_WAIT_CRCX_BTS || fi->state == ST_WAIT_ASS_CMPL || fi->state == ST_WAIT_MDCX_BTS
- || fi->state == ST_WAIT_CRCX_MSC)
- assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE);
- break;
case GSCON_EV_A_CLEAR_CMD:
/* MSC tells us to cleanly shut down */
- osmo_fsm_inst_state_chg(fi, ST_CLEARING, 0, 0);
- gsm0808_clear(conn);
+ osmo_fsm_inst_state_chg(fi, ST_CLEARING, 60, 999);
+ gscon_release_lchans(conn, true);
/* FIXME: Release all terestrial resources in ST_CLEARING */
/* According to 3GPP 48.008 3.1.9.1. "The BSS need not wait for the radio channel
* release to be completed or for the guard timer to expire before returning the
* CLEAR COMPLETE message" */
/* Close MGCP connections */
- toss_mgcp_conn(conn, fi);
+ mgw_endpoint_clear(conn->user_plane.mgw_endpoint);
- /* FIXME: Question: Is this a hack to force a clear complete from internel?
- * nobody seems to send the event from outside? */
- osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_RSL_CLEAR_COMPL, NULL);
+ gscon_sigtran_send(conn, gsm0808_create_clear_complete());
break;
case GSCON_EV_A_DISC_IND:
/* MSC or SIGTRAN network has hard-released SCCP connection,
* terminate the FSM now. */
osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, data);
break;
- case GSCON_EV_RLL_REL_IND:
- /* BTS reports that one of the LAPDm data links was released */
- /* send proper clear request to MSC */
- LOGPFSML(fi, LOGL_DEBUG, "Tx BSSMAP CLEAR REQUEST to MSC\n");
- resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_RADIO_INTERFACE_MESSAGE_FAILURE);
- sigtran_send(conn, resp, fi);
+ case GSCON_EV_FORGET_MGW_ENDPOINT:
+ gscon_forget_mgw_endpoint(conn);
break;
case GSCON_EV_RSL_CONN_FAIL:
- LOGPFSML(fi, LOGL_DEBUG, "Tx BSSMAP CLEAR REQUEST to MSC\n");
- resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_RADIO_INTERFACE_FAILURE);
- sigtran_send(conn, resp, fi);
+ if (conn->lchan) {
+ conn->lchan->release_in_error = true;
+ conn->lchan->error_cause = data ? *(uint8_t*)data : RSL_ERR_IE_ERROR;
+ }
+ gscon_bssmap_clear(conn, GSM0808_CAUSE_RADIO_INTERFACE_FAILURE);
break;
case GSCON_EV_MGW_MDCX_RESP_MSC:
LOGPFSML(fi, LOGL_DEBUG, "Rx MDCX of MSC side (LCLS?)\n");
@@ -972,29 +806,10 @@ static void gscon_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *d
}
}
-void ho_dtap_cache_flush(struct gsm_subscriber_connection *conn, int send);
-
static void gscon_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
{
struct gsm_subscriber_connection *conn = fi->priv;
- if (conn->ho) {
- LOGPFSML(fi, LOGL_DEBUG, "Releasing handover state\n");
- bsc_clear_handover(conn, 1);
- conn->ho = NULL;
- }
-
- if (conn->secondary_lchan) {
- LOGPFSML(fi, LOGL_DEBUG, "Releasing secondary_lchan\n");
- lchan_release(conn->secondary_lchan, 0, RSL_REL_LOCAL_END);
- conn->secondary_lchan = NULL;
- }
- if (conn->lchan) {
- LOGPFSML(fi, LOGL_DEBUG, "Releasing lchan\n");
- lchan_release(conn->lchan, 0, RSL_REL_LOCAL_END);
- conn->lchan = NULL;
- }
-
if (conn->sccp.state != SUBSCR_SCCP_ST_NONE) {
LOGPFSML(fi, LOGL_DEBUG, "Disconnecting SCCP\n");
struct bsc_msc_data *msc = conn->sccp.msc;
@@ -1003,11 +818,6 @@ static void gscon_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cau
conn->sccp.state = SUBSCR_SCCP_ST_NONE;
}
- /* drop pending messages */
- ho_dtap_cache_flush(conn, 0);
-
- penalty_timers_free(&conn->hodec2.penalty_timers);
-
if (conn->bsub) {
LOGPFSML(fi, LOGL_DEBUG, "Putting bsc_subscr\n");
bsc_subscr_put(conn->bsub);
@@ -1016,21 +826,26 @@ static void gscon_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cau
llist_del(&conn->entry);
talloc_free(conn);
- fi->priv = NULL;
}
static void gscon_pre_term(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
{
struct gsm_subscriber_connection *conn = fi->priv;
-
- /* Make sure all possibly still open MGCP connections get closed */
- toss_mgcp_conn(conn, fi);
+
+ mgw_endpoint_clear(conn->user_plane.mgw_endpoint);
if (conn->lcls.fi) {
/* request termination of LCLS FSM */
osmo_fsm_inst_term(conn->lcls.fi, cause, NULL);
conn->lcls.fi = NULL;
}
+
+ gscon_release_lchans(conn, false);
+
+ /* drop pending messages */
+ gscon_dtap_cache_flush(conn, 0);
+
+ penalty_timers_free(&conn->hodec2.penalty_timers);
}
static int gscon_timer_cb(struct osmo_fsm_inst *fi)
@@ -1039,6 +854,8 @@ static int gscon_timer_cb(struct osmo_fsm_inst *fi)
switch (fi->T) {
case 993210:
+ lchan_release(conn->lchan, false, true, RSL_ERR_INTERWORKING);
+
/* MSC has not responded/confirmed connection with CC, this
* could indicate a bad SCCP connection. We now inform the the
* FSM that controls the BSSMAP reset about the event. Maybe
@@ -1050,16 +867,16 @@ static int gscon_timer_cb(struct osmo_fsm_inst *fi)
* gscon_cleanup() above) */
osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
break;
- case 10: /* Assignment Failed */
- assignment_failed(fi, GSM0808_CAUSE_RADIO_INTERFACE_FAILURE);
- break;
- case MGCP_MGW_TIMEOUT_TIMER_NR: /* Assignment failed (no response from MGW) */
- assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE);
- break;
- case MGCP_MGW_HO_TIMEOUT_TIMER_NR: /* Handover failed (no response from MGW) */
- osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0);
+ case 999:
+ /* The MSC has sent a BSSMAP Clear Command, we acknowledged that, but the conn was never
+ * disconnected. */
+ LOGPFSML(fi, LOGL_ERROR, "Long after a BSSMAP Clear Command, the conn is still not"
+ " released. For sanity, discarding this conn now.\n");
+ a_reset_conn_fail(conn->sccp.msc->a.reset_fsm);
+ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
break;
default:
+ LOGPFSML(fi, LOGL_ERROR, "Unknown timer %d expired\n", fi->T);
OSMO_ASSERT(false);
}
return 0;
@@ -1070,8 +887,9 @@ static struct osmo_fsm gscon_fsm = {
.states = gscon_fsm_states,
.num_states = ARRAY_SIZE(gscon_fsm_states),
.allstate_event_mask = S(GSCON_EV_A_DISC_IND) | S(GSCON_EV_A_CLEAR_CMD) | S(GSCON_EV_RSL_CONN_FAIL) |
- S(GSCON_EV_RLL_REL_IND) | S(GSCON_EV_MGW_FAIL_BTS) | S(GSCON_EV_MGW_FAIL_MSC) |
- S(GSCON_EV_MGW_MDCX_RESP_MSC) | S(GSCON_EV_LCLS_FAIL),
+ S(GSCON_EV_LCLS_FAIL) |
+ S(GSCON_EV_FORGET_LCHAN) |
+ S(GSCON_EV_FORGET_MGW_ENDPOINT),
.allstate_action = gscon_fsm_allstate,
.cleanup = gscon_cleanup,
.pre_term = gscon_pre_term,
@@ -1097,7 +915,7 @@ struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *ne
return NULL;
conn->network = net;
- INIT_LLIST_HEAD(&conn->ho_dtap_cache);
+ INIT_LLIST_HEAD(&conn->gscon_dtap_cache);
/* BTW, penalty timers will be initialized on-demand. */
conn->sccp.conn_id = -1;
@@ -1122,3 +940,155 @@ struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *ne
llist_add_tail(&conn->entry, &net->subscr_conns);
return conn;
}
+
+/* Compose an FSM ID, if possible from the current subscriber information */
+void gscon_update_id(struct gsm_subscriber_connection *conn)
+{
+ osmo_fsm_inst_update_id_f(conn->fi, "conn%x%s%s",
+ conn->sccp.conn_id,
+ conn->bsub? "_" : "",
+ conn->bsub? bsc_subscr_id(conn->bsub) : "");
+}
+
+#if 0
+static void handle_ass_fail(struct gsm_subscriber_connection *conn,
+ struct msgb *msg)
+{
+ uint8_t *rr_failure;
+ struct gsm48_hdr *gh;
+
+ if (conn->ho) {
+ struct lchan_signal_data sig;
+ struct gsm48_hdr *gh = msgb_l3(msg);
+
+ LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, "ASSIGNMENT FAILED cause = %s\n",
+ rr_cause_name(gh->data[0]));
+
+ sig.lchan = msg->lchan;
+ sig.mr = NULL;
+ osmo_signal_dispatch(SS_LCHAN, S_LCHAN_ASSIGNMENT_FAIL, &sig);
+ /* FIXME: release allocated new channel */
+
+ /* send pending messages, if any */
+ gscon_dtap_cache_flush(conn, 1);
+
+ return;
+ }
+
+ if (conn->lchan != msg->lchan) {
+ LOGPLCHAN(msg->lchan, DMSC, LOGL_ERROR,
+ "Assignment failure should occur on primary lchan.\n");
+ return;
+ }
+
+ /* stop the timer and release it */
+ if (conn->secondary_lchan) {
+ lchan_release(conn->secondary_lchan, 0, RSL_REL_LOCAL_END);
+ conn->secondary_lchan = NULL;
+ }
+
+ /* send pending messages, if any */
+ gscon_dtap_cache_flush(conn, 1);
+
+ gh = msgb_l3(msg);
+ if (msgb_l3len(msg) - sizeof(*gh) != 1) {
+ LOGPLCHAN(conn->lchan, DMSC, LOGL_ERROR, "assignment failure unhandled: %zu\n",
+ msgb_l3len(msg) - sizeof(*gh));
+ rr_failure = NULL;
+ } else {
+ rr_failure = &gh->data[0];
+ }
+
+ bsc_assign_fail(conn, GSM0808_CAUSE_RADIO_INTERFACE_MESSAGE_FAILURE, rr_failure);
+}
+
+static void handle_rr_ho_compl(struct msgb *msg)
+{
+ struct lchan_signal_data sig;
+ struct gsm48_hdr *gh = msgb_l3(msg);
+
+ LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG,
+ "HANDOVER COMPLETE cause = %s\n", rr_cause_name(gh->data[0]));
+
+ sig.lchan = msg->lchan;
+ sig.mr = NULL;
+ osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig);
+ /* FIXME: release old channel */
+
+ /* send pending messages, if any */
+ gscon_dtap_cache_flush(msg->lchan->conn, 1);
+}
+
+static void handle_rr_ho_fail(struct msgb *msg)
+{
+ struct lchan_signal_data sig;
+ struct gsm48_hdr *gh = msgb_l3(msg);
+
+ /* Log on both RR and HO categories: it is an RR message, but is still quite important when
+ * filtering on HO. */
+ LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG,
+ "HANDOVER FAILED cause = %s\n", rr_cause_name(gh->data[0]));
+ LOGPLCHAN(msg->lchan, DHO, LOGL_DEBUG,
+ "HANDOVER FAILED cause = %s\n", rr_cause_name(gh->data[0]));
+
+ sig.lchan = msg->lchan;
+ sig.mr = NULL;
+ osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
+ /* FIXME: release allocated new channel */
+
+ /* send pending messages, if any */
+ gscon_dtap_cache_flush(msg->lchan->conn, 1);
+}
+
+
+static void handle_ass_compl(struct gsm_subscriber_connection *conn,
+ struct msgb *msg)
+{
+ struct gsm48_hdr *gh = msgb_l3(msg);
+ enum gsm48_rr_cause cause;
+
+ /* Expecting gsm48_hdr + cause value */
+ if (msgb_l3len(msg) != sizeof(*gh) + 1) {
+ LOGPLCHAN(msg->lchan, DRR, LOGL_ERROR,
+ "RR Assignment Complete: length invalid: %u, expected %zu\n",
+ msgb_l3len(msg), sizeof(*gh) + 1);
+ return;
+ }
+
+ cause = gh->data[0];
+
+ LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, "ASSIGNMENT COMPLETE cause = %s\n",
+ rr_cause_name(cause));
+
+ if (conn->ho) {
+ struct lchan_signal_data sig = {
+ .lchan = msg->lchan,
+ };
+ osmo_signal_dispatch(SS_LCHAN, S_LCHAN_ASSIGNMENT_COMPL, &sig);
+ /* FIXME: release old channel */
+
+ /* send pending messages, if any */
+ gscon_dtap_cache_flush(conn, 1);
+
+ return;
+ }
+
+ if (conn->secondary_lchan != msg->lchan) {
+ LOGPLCHAN(msg->lchan, DRR, LOGL_ERROR,
+ "RR Assignment Complete does not match conn's secondary lchan.\n");
+ return;
+ }
+
+ lchan_release(conn->lchan, 0, RSL_REL_LOCAL_END);
+ conn->lchan = conn->secondary_lchan;
+ conn->secondary_lchan = NULL;
+
+ /* send pending messages, if any */
+ gscon_dtap_cache_flush(conn, 1);
+
+ if (is_ipaccess_bts(conn_get_bts(conn)) && conn->lchan->tch_mode != GSM48_CMODE_SIGN)
+ rsl_ipacc_crcx(conn->lchan);
+
+ bsc_assign_compl(conn, cause);
+}
+#endif
diff --git a/src/osmo-bsc/bsc_subscriber.c b/src/osmo-bsc/bsc_subscriber.c
index 65a660b40..2541883d5 100644
--- a/src/osmo-bsc/bsc_subscriber.c
+++ b/src/osmo-bsc/bsc_subscriber.c
@@ -118,6 +118,20 @@ const char *bsc_subscr_name(struct bsc_subscr *bsub)
return buf;
}
+/* Like bsc_subscr_name() but returns only characters approved by osmo_identifier_valid(), useful for
+ * osmo_fsm_inst IDs. */
+const char *bsc_subscr_id(struct bsc_subscr *bsub)
+{
+ static char buf[32];
+ if (!bsub)
+ return "unknown";
+ if (bsub->imsi[0])
+ snprintf(buf, sizeof(buf), "IMSI%s", bsub->imsi);
+ else
+ snprintf(buf, sizeof(buf), "TMSI%08x", bsub->tmsi);
+ return buf;
+}
+
static void bsc_subscr_free(struct bsc_subscr *bsub)
{
llist_del(&bsub->entry);
diff --git a/src/osmo-bsc/bsc_vty.c b/src/osmo-bsc/bsc_vty.c
index 5298644e6..4ef1a60ee 100644
--- a/src/osmo-bsc/bsc_vty.c
+++ b/src/osmo-bsc/bsc_vty.c
@@ -33,6 +33,7 @@
#include <osmocom/gsm/gsm0502.h>
#include <osmocom/ctrl/control_if.h>
#include <osmocom/gsm/gsm48.h>
+#include <osmocom/gsm/gsm0808.h>
#include <arpa/inet.h>
@@ -56,13 +57,17 @@
#include <osmocom/bsc/bsc_msc_data.h>
#include <osmocom/bsc/osmo_bsc_rf.h>
#include <osmocom/bsc/pcu_if.h>
-#include <osmocom/bsc/handover.h>
+#include <osmocom/bsc/handover_fsm.h>
#include <osmocom/bsc/handover_cfg.h>
#include <osmocom/bsc/handover_vty.h>
#include <osmocom/bsc/gsm_04_08_utils.h>
#include <osmocom/bsc/acc_ramp.h>
#include <osmocom/bsc/meas_feed.h>
#include <osmocom/bsc/neighbor_ident.h>
+#include <osmocom/bsc/timeslot_fsm.h>
+#include <osmocom/bsc/lchan_fsm.h>
+#include <osmocom/bsc/gsm_timers.h>
+#include <osmocom/bsc/mgw_endpoint_fsm.h>
#include <inttypes.h>
@@ -550,9 +555,9 @@ static void config_write_ts_single(struct vty *vty, struct gsm_bts_trx_ts *ts)
vty_out(vty, " timeslot %u%s", ts->nr, VTY_NEWLINE);
if (ts->tsc != -1)
vty_out(vty, " training_sequence_code %u%s", ts->tsc, VTY_NEWLINE);
- if (ts->pchan != GSM_PCHAN_NONE)
+ if (ts->pchan_from_config != GSM_PCHAN_NONE)
vty_out(vty, " phys_chan_config %s%s",
- gsm_pchan_name(ts->pchan), VTY_NEWLINE);
+ gsm_pchan_name(ts->pchan_from_config), VTY_NEWLINE);
vty_out(vty, " hopping enabled %u%s",
ts->hopping.enabled, VTY_NEWLINE);
if (ts->hopping.enabled) {
@@ -967,11 +972,6 @@ static int config_write_bts(struct vty *v)
return CMD_SUCCESS;
}
-/* small helper macro for conditional dumping of timer */
-#define VTY_OUT_TIMER(number) \
- if (gsmnet->T##number != GSM_T##number##_DEFAULT) \
- vty_out(vty, " timer t"#number" %u%s", gsmnet->T##number, VTY_NEWLINE)
-
static int config_write_net(struct vty *vty)
{
struct gsm_network *gsmnet = gsmnet_from_vty(vty);
@@ -992,22 +992,7 @@ static int config_write_net(struct vty *vty)
ho_vty_write_net(vty, gsmnet);
- VTY_OUT_TIMER(3101);
- VTY_OUT_TIMER(3103);
- VTY_OUT_TIMER(3105);
- VTY_OUT_TIMER(3107);
- VTY_OUT_TIMER(3109);
- VTY_OUT_TIMER(3111);
- VTY_OUT_TIMER(3113);
- VTY_OUT_TIMER(3115);
- VTY_OUT_TIMER(3117);
- VTY_OUT_TIMER(3119);
- VTY_OUT_TIMER(3122);
- VTY_OUT_TIMER(3141);
- VTY_OUT_TIMER(10);
- VTY_OUT_TIMER(7);
- VTY_OUT_TIMER(8);
- VTY_OUT_TIMER(101);
+ T_defs_vty_write(vty, " ");
if (!gsmnet->dyn_ts_allow_tch_f)
vty_out(vty, " dyn_ts_allow_tch_f 0%s", VTY_NEWLINE);
@@ -1020,11 +1005,8 @@ static int config_write_net(struct vty *vty)
vty_out(vty, " timezone %d %d%s",
gsmnet->tz.hr, gsmnet->tz.mn, VTY_NEWLINE);
}
- if (gsmnet->t3212 == 0)
- vty_out(vty, " no periodic location update%s", VTY_NEWLINE);
- else
- vty_out(vty, " periodic location update %u%s",
- gsmnet->t3212 * 6, VTY_NEWLINE);
+
+ /* writing T3212 from the common T_defs_vty_write() instead. */
{
uint16_t meas_port;
@@ -1121,17 +1103,12 @@ DEFUN(show_trx,
static void ts_dump_vty(struct vty *vty, struct gsm_bts_trx_ts *ts)
{
- vty_out(vty, "BTS %u, TRX %u, Timeslot %u, phys cfg %s, TSC %u",
+ vty_out(vty, "BTS %u, TRX %u, Timeslot %u, phys cfg %s",
ts->trx->bts->nr, ts->trx->nr, ts->nr,
- gsm_pchan_name(ts->pchan), gsm_ts_tsc(ts));
- if (ts->pchan == GSM_PCHAN_TCH_F_PDCH) {
- vty_out(vty, " (%s mode)",
- ts->flags & TS_F_PDCH_ACTIVE ? "PDCH" : "TCH/F");
- } else if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) {
- vty_out(vty, " (%s mode)", gsm_pchan_name(ts->dyn.pchan_is));
- }
- vty_out(vty, "%s", VTY_NEWLINE);
- vty_out(vty, " NM State: ");
+ gsm_pchan_name(ts->pchan_on_init));
+ if (ts->pchan_is != ts->pchan_on_init)
+ vty_out(vty, " (%s mode)", gsm_pchan_name(ts->pchan_is));
+ vty_out(vty, ", TSC %u%s NM State: ", gsm_ts_tsc(ts), VTY_NEWLINE);
net_dump_nmstate(vty, &ts->mo.nm_state);
if (!is_ipaccess_bts(ts->trx->bts))
vty_out(vty, " E1 Line %u, Timeslot %u, Subslot %u%s",
@@ -1258,48 +1235,17 @@ static void meas_rep_dump_vty(struct vty *vty, struct gsm_meas_rep *mr,
meas_rep_dump_uni_vty(vty, &mr->ul, prefix, "ul");
}
-/* FIXME: move this to libosmogsm */
-static const struct value_string gsm48_cmode_names[] = {
- { GSM48_CMODE_SIGN, "signalling" },
- { GSM48_CMODE_SPEECH_V1, "FR or HR" },
- { GSM48_CMODE_SPEECH_EFR, "EFR" },
- { GSM48_CMODE_SPEECH_AMR, "AMR" },
- { GSM48_CMODE_DATA_14k5, "CSD(14k5)" },
- { GSM48_CMODE_DATA_12k0, "CSD(12k0)" },
- { GSM48_CMODE_DATA_6k0, "CSD(6k0)" },
- { GSM48_CMODE_DATA_3k6, "CSD(3k6)" },
- { 0, NULL }
-};
/* call vty_out() to print a string like " as TCH/H" for dynamic timeslots.
* Don't do anything if the ts is not dynamic. */
static void vty_out_dyn_ts_status(struct vty *vty, struct gsm_bts_trx_ts *ts)
{
- switch (ts->pchan) {
- case GSM_PCHAN_TCH_F_TCH_H_PDCH:
- if (ts->dyn.pchan_is == ts->dyn.pchan_want)
- vty_out(vty, " as %s",
- gsm_pchan_name(ts->dyn.pchan_is));
- else
- vty_out(vty, " switching %s -> %s",
- gsm_pchan_name(ts->dyn.pchan_is),
- gsm_pchan_name(ts->dyn.pchan_want));
- break;
- case GSM_PCHAN_TCH_F_PDCH:
- if ((ts->flags & TS_F_PDCH_PENDING_MASK) == 0)
- vty_out(vty, " as %s",
- (ts->flags & TS_F_PDCH_ACTIVE)? "PDCH"
- : "TCH/F");
- else
- vty_out(vty, " switching %s -> %s",
- (ts->flags & TS_F_PDCH_ACTIVE)? "PDCH"
- : "TCH/F",
- (ts->flags & TS_F_PDCH_ACT_PENDING)? "PDCH"
- : "TCH/F");
- break;
- default:
- /* no dyn ts */
- break;
+ enum gsm_phys_chan_config target;
+ if (ts_is_pchan_switching(ts, &target)) {
+ vty_out(vty, " switching %s -> %s", gsm_pchan_name(ts->pchan_is),
+ gsm_pchan_name(target));
+ } else if (ts->pchan_is != ts->pchan_on_init) {
+ vty_out(vty, " as %s", gsm_pchan_name(ts->pchan_is));
}
}
@@ -1311,7 +1257,7 @@ static void lchan_dump_full_vty(struct vty *vty, struct gsm_lchan *lchan)
lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
lchan->nr, gsm_lchant_name(lchan->type), VTY_NEWLINE);
/* show dyn TS details, if applicable */
- switch (lchan->ts->pchan) {
+ switch (lchan->ts->pchan_on_init) {
case GSM_PCHAN_TCH_F_TCH_H_PDCH:
vty_out(vty, " Osmocom Dyn TS:");
vty_out_dyn_ts_status(vty, lchan->ts);
@@ -1327,10 +1273,9 @@ static void lchan_dump_full_vty(struct vty *vty, struct gsm_lchan *lchan)
break;
}
vty_out(vty, " Connection: %u, State: %s%s%s%s",
- lchan->conn ? 1: 0,
- gsm_lchans_name(lchan->state),
- lchan->state == LCHAN_S_BROKEN ? " Error reason: " : "",
- lchan->state == LCHAN_S_BROKEN ? lchan->broken_reason : "",
+ lchan->conn ? 1: 0, lchan_state_name(lchan),
+ lchan->fi && lchan->fi->state == LCHAN_ST_BORKEN ? " Error reason: " : "",
+ lchan->fi && lchan->fi->state == LCHAN_ST_BORKEN ? lchan->last_error : "",
VTY_NEWLINE);
vty_out(vty, " BS Power: %u dBm, MS Power: %u dBm%s",
lchan->ts->trx->nominal_power - lchan->ts->trx->max_power_red
@@ -1338,7 +1283,7 @@ static void lchan_dump_full_vty(struct vty *vty, struct gsm_lchan *lchan)
ms_pwr_dbm(lchan->ts->trx->bts->band, lchan->ms_power),
VTY_NEWLINE);
vty_out(vty, " Channel Mode / Codec: %s%s",
- get_value_string(gsm48_cmode_names, lchan->tch_mode),
+ gsm48_chan_mode_name(lchan->tch_mode),
VTY_NEWLINE);
if (lchan->conn && lchan->conn->bsub) {
vty_out(vty, " Subscriber:%s", VTY_NEWLINE);
@@ -1382,12 +1327,12 @@ static void lchan_dump_short_vty(struct vty *vty, struct gsm_lchan *lchan)
vty_out(vty, "BTS %u, TRX %u, Timeslot %u %s",
lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
- gsm_pchan_name(lchan->ts->pchan));
+ gsm_pchan_name(lchan->ts->pchan_on_init));
vty_out_dyn_ts_status(vty, lchan->ts);
vty_out(vty, ", Lchan %u, Type %s, State %s - "
"L1 MS Power: %u dBm RXL-FULL-dl: %4d dBm RXL-FULL-ul: %4d dBm%s",
lchan->nr,
- gsm_lchant_name(lchan->type), gsm_lchans_name(lchan->state),
+ gsm_lchant_name(lchan->type), lchan_state_name(lchan),
mr->ms_l1.pwr,
rxlev2dbm(mr->dl.full.rx_lev),
rxlev2dbm(mr->ul.full.rx_lev),
@@ -1401,7 +1346,7 @@ static int dump_lchan_trx_ts(struct gsm_bts_trx_ts *ts, struct vty *vty,
int lchan_nr;
for (lchan_nr = 0; lchan_nr < TS_MAX_LCHAN; lchan_nr++) {
struct gsm_lchan *lchan = &ts->lchan[lchan_nr];
- if ((lchan->type == GSM_LCHAN_NONE) && (lchan->state == LCHAN_S_NONE))
+ if ((lchan->type == GSM_LCHAN_NONE) && lchan_state_is(lchan, LCHAN_ST_UNUSED))
continue;
dump_cb(vty, lchan);
}
@@ -1528,7 +1473,7 @@ static void dump_one_subscr_conn(struct vty *vty, const struct gsm_subscriber_co
vty_out(vty, "conn ID=%u, MSC=%u, hodec2_fail=%d, mode=%s, mgw_ep=%s%s",
conn->sccp.conn_id, conn->sccp.msc->nr, conn->hodec2.failures,
get_value_string(gsm48_chan_mode_names, conn->user_plane.chan_mode),
- conn->user_plane.mgw_endpoint, VTY_NEWLINE);
+ mgw_endpoint_name(conn->user_plane.mgw_endpoint), VTY_NEWLINE);
if (conn->lcls.global_call_ref_len) {
vty_out(vty, " LCLS GCR: %s%s",
osmo_hexdump_nospc(conn->lcls.global_call_ref, conn->lcls.global_call_ref_len),
@@ -1539,8 +1484,8 @@ static void dump_one_subscr_conn(struct vty *vty, const struct gsm_subscriber_co
}
if (conn->lchan)
lchan_dump_full_vty(vty, conn->lchan);
- if (conn->secondary_lchan)
- lchan_dump_full_vty(vty, conn->secondary_lchan);
+ if (conn->assignment.new_lchan)
+ lchan_dump_full_vty(vty, conn->assignment.new_lchan);
}
DEFUN(show_subscr_conn,
@@ -1569,8 +1514,6 @@ DEFUN(show_subscr_conn,
static int trigger_ho_or_as(struct vty *vty, struct gsm_lchan *from_lchan, struct gsm_bts *to_bts)
{
- int rc;
-
if (!to_bts || from_lchan->ts->trx->bts == to_bts) {
LOGP(DHO, LOGL_NOTICE, "%s Manually triggering Assignment from VTY\n",
gsm_lchan_name(from_lchan));
@@ -1578,11 +1521,13 @@ static int trigger_ho_or_as(struct vty *vty, struct gsm_lchan *from_lchan, struc
} else
LOGP(DHO, LOGL_NOTICE, "%s (ARFCN %u) --> BTS %u Manually triggering Handover from VTY\n",
gsm_lchan_name(from_lchan), from_lchan->ts->trx->arfcn, to_bts->nr);
- rc = bsc_handover_start(HODEC_NONE, from_lchan, to_bts, from_lchan->type);
- if (rc) {
- vty_out(vty, "bsc_handover_start() returned %d=%s%s", rc,
- strerror(-rc), VTY_NEWLINE);
- return CMD_WARNING;
+ {
+ struct handover_mo_req req = {
+ .from_hodec_id = HODEC_USER,
+ .old_lchan = from_lchan,
+ .target_nik = *bts_ident_key(to_bts),
+ };
+ handover_start(&req);
}
return CMD_SUCCESS;
}
@@ -1671,18 +1616,13 @@ static struct gsm_lchan *find_used_voice_lchan(struct vty *vty)
int i;
for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
struct gsm_bts_trx_ts *ts = &trx->ts[i];
- int j;
- int subslots;
+ struct gsm_lchan *lchan;
- /* skip administratively deactivated timeslots */
- if (!nm_is_running(&ts->mo.nm_state))
+ if (ts->fi->state != TS_ST_IN_USE)
continue;
- subslots = ts_subslots(ts);
- for (j = 0; j < subslots; j++) {
- struct gsm_lchan *lchan = &ts->lchan[j];
-
- if (lchan->state == LCHAN_S_ACTIVE
+ ts_for_each_lchan(lchan, ts) {
+ if (lchan_state_is(lchan, LCHAN_ST_ACTIVE)
&& (lchan->type == GSM_LCHAN_TCH_F
|| lchan->type == GSM_LCHAN_TCH_H)) {
@@ -1714,30 +1654,27 @@ static struct gsm_bts *find_other_bts_with_free_slots(struct vty *vty, struct gs
llist_for_each_entry(trx, &bts->trx_list, list) {
int i;
+ /* FIXME: use lchan_select_by_type() instead */
for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
struct gsm_bts_trx_ts *ts = &trx->ts[i];
- int j;
- int subslots;
+ struct gsm_lchan *lchan;
/* skip administratively deactivated timeslots */
if (!nm_is_running(&ts->mo.nm_state))
continue;
- if (ts->pchan != free_type)
+ if (ts->pchan_is != free_type)
continue;
- subslots = ts_subslots(ts);
- for (j = 0; j < subslots; j++) {
- struct gsm_lchan *lchan = &ts->lchan[j];
-
- if (lchan->state == LCHAN_S_NONE) {
- vty_out(vty, "Found unused %s slot: %s%s",
- gsm_pchan_name(free_type),
- gsm_lchan_name(lchan),
- VTY_NEWLINE);
- lchan_dump_full_vty(vty, lchan);
- return bts;
- }
+ ts_for_each_lchan(lchan, ts) {
+ if (lchan->fi->state != LCHAN_ST_UNUSED)
+ continue;
+ vty_out(vty, "Found unused %s slot: %s%s",
+ gsm_pchan_name(free_type),
+ gsm_lchan_name(lchan),
+ VTY_NEWLINE);
+ lchan_dump_full_vty(vty, lchan);
+ return bts;
}
}
}
@@ -1761,7 +1698,7 @@ DEFUN(handover_any, handover_any_cmd,
return CMD_WARNING;
to_bts = find_other_bts_with_free_slots(vty, from_lchan->ts->trx->bts,
- ts_pchan(from_lchan->ts));
+ from_lchan->ts->pchan_is);
if (!to_bts)
return CMD_WARNING;
@@ -1783,6 +1720,61 @@ DEFUN(assignment_any, assignment_any_cmd,
return trigger_ho_or_as(vty, from_lchan, NULL);
}
+#if 0
+
+DEFUN(handover_ext_cgi,
+ handover_ext_cgi_cmd,
+ "handover external (cgi|lac+ci|ci|lai|lac|all|none) [VAL1] [VAL2] [VAL3] [VAL4]",
+ MANUAL_HANDOVER_EXTERNAL_STR
+ "Identify handover target cell by Cell Global Identifier; supply MCC MNC LAC CI arguments\n"
+ "Identify handover target cell by Location Area Code and Cell Identify\n"
+ "Identify handover target cell by Cell Identify\n"
+ "Identify handover target cell by Location Area Identifcation\n"
+ "Identify handover target cell by Location Area Code\n"
+ "Identify handover target as all and any available cell\n"
+ "Do not associate a cell with the Handover Required message\n"
+ "Value to specify target cell, format depends\n"
+ "Value to specify target cell, format depends\n"
+ "Value to specify target cell, format depends\n"
+ "Value to specify target cell, format depends\n")
+{
+ return ho_or_as(vty, argv, argc);
+}
+#endif
+
+DEFUN(handover_any_to_arfcn_bsic, handover_any_to_arfcn_bsic_cmd,
+ "handover any to " NEIGHBOR_IDENT_VTY_KEY_PARAMS,
+ MANUAL_HANDOVER_STR
+ "Pick any actively used TCH/F or TCH/H lchan to handover to another cell."
+ " This is likely to fail outside of a lab setup where you are certain that"
+ " all MS are able to see the target cell.\n"
+ "'to'\n"
+ NEIGHBOR_IDENT_VTY_KEY_DOC)
+{
+ struct gsm_lchan *from_lchan;
+
+ from_lchan = find_used_voice_lchan(vty);
+ if (!from_lchan)
+ return CMD_WARNING;
+
+ {
+ struct handover_mo_req req = {
+ .from_hodec_id = HODEC_USER,
+ .old_lchan = from_lchan,
+ };
+
+ if (!neighbor_ident_bts_parse_key_params(vty, from_lchan->ts->trx->bts,
+ argv, &req.target_nik)) {
+ vty_out(vty, "%% BTS %u does not know about this neighbor%s",
+ from_lchan->ts->trx->bts->nr, VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ handover_start(&req);
+ }
+ return CMD_SUCCESS;
+}
+
static void paging_dump_vty(struct vty *vty, struct gsm_paging_request *pag)
{
vty_out(vty, "Paging on BTS %u%s", pag->bts->nr, VTY_NEWLINE);
@@ -1888,48 +1880,6 @@ DEFUN(cfg_net_pag_any_tch,
return CMD_SUCCESS;
}
-#define DEFAULT_TIMER(number) GSM_T##number##_DEFAULT
-/* Add another expansion so that DEFAULT_TIMER() becomes its value */
-#define EXPAND_AND_STRINGIFY(x) OSMO_STRINGIFY(x)
-
-#define DECLARE_TIMER(number, doc) \
- DEFUN(cfg_net_T##number, \
- cfg_net_T##number##_cmd, \
- "timer t" #number " (default|<1-65535>)", \
- "Configure GSM Timers\n" \
- doc " (default: " EXPAND_AND_STRINGIFY(DEFAULT_TIMER(number)) " seconds)\n" \
- "Set to default timer value" \
- " (" EXPAND_AND_STRINGIFY(DEFAULT_TIMER(number)) " seconds)\n" \
- "Timer Value in seconds\n") \
-{ \
- struct gsm_network *gsmnet = gsmnet_from_vty(vty); \
- int value; \
- if (strcmp(argv[0], "default") == 0) \
- value = DEFAULT_TIMER(number); \
- else \
- value = atoi(argv[0]); \
- \
- gsmnet->T##number = value; \
- return CMD_SUCCESS; \
-}
-
-DECLARE_TIMER(3101, "Set the timeout value for IMMEDIATE ASSIGNMENT")
-DECLARE_TIMER(3103, "Set the timeout value for HANDOVER")
-DECLARE_TIMER(3105, "Set the timer for repetition of PHYSICAL INFORMATION")
-DECLARE_TIMER(3107, "Currently not used")
-DECLARE_TIMER(3109, "Set the RSL SACCH deactivation timeout")
-DECLARE_TIMER(3111, "Set the RSL timeout to wait before releasing the RF Channel")
-DECLARE_TIMER(3113, "Set the time to try paging a subscriber")
-DECLARE_TIMER(3115, "Currently not used")
-DECLARE_TIMER(3117, "Currently not used")
-DECLARE_TIMER(3119, "Currently not used")
-DECLARE_TIMER(3122, "Default waiting time (seconds) after IMM ASS REJECT")
-DECLARE_TIMER(3141, "Currently not used")
-DECLARE_TIMER(10, "Assignment Command timeout in seconds")
-DECLARE_TIMER(7, "Set the outgoing inter-BSC Handover timeout, from Handover Required to Handover Command")
-DECLARE_TIMER(8, "Set the outgoing inter-BSC Handover timeout, from Handover Command to final Clear")
-DECLARE_TIMER(101, "Set the incoming inter-BSC Handover timeout, from Handover Request to Accept")
-
DEFUN_DEPRECATED(cfg_net_dtx,
cfg_net_dtx_cmd,
"dtx-used (0|1)",
@@ -4049,7 +3999,7 @@ DEFUN(cfg_ts_pchan,
if (pchanc < 0)
return CMD_WARNING;
- ts->pchan = pchanc;
+ ts->pchan_from_config = pchanc;
return CMD_SUCCESS;
}
@@ -4068,7 +4018,7 @@ DEFUN_HIDDEN(cfg_ts_pchan_compat,
if (pchanc < 0)
return CMD_WARNING;
- ts->pchan = pchanc;
+ ts->pchan_from_config = pchanc;
return CMD_SUCCESS;
}
@@ -4416,8 +4366,10 @@ DEFUN(pdch_act, pdch_act_cmd,
int activate;
ts = vty_get_ts(vty, argv[0], argv[1], argv[2]);
- if (!ts)
+ if (!ts || !ts->fi || ts->fi->state == TS_ST_NOT_INITIALIZED || ts->fi->state == TS_ST_BORKEN) {
+ vty_out(vty, "%% Timeslot is not usable%s", VTY_NEWLINE);
return CMD_WARNING;
+ }
if (!is_ipaccess_bts(ts->trx->bts)) {
vty_out(vty, "%% This command only works for ipaccess BTS%s",
@@ -4425,9 +4377,10 @@ DEFUN(pdch_act, pdch_act_cmd,
return CMD_WARNING;
}
- if (ts->pchan != GSM_PCHAN_TCH_F_PDCH) {
- vty_out(vty, "%% Timeslot %u is not in dynamic TCH_F/PDCH "
- "mode%s", ts->nr, VTY_NEWLINE);
+ if (ts->pchan_on_init != GSM_PCHAN_TCH_F_TCH_H_PDCH
+ && ts->pchan_on_init != GSM_PCHAN_TCH_F_PDCH) {
+ vty_out(vty, "%% Timeslot %u is not dynamic TCH/F_TCH/H_PDCH or TCH/F_PDCH%s",
+ ts->nr, VTY_NEWLINE);
return CMD_WARNING;
}
@@ -4436,30 +4389,24 @@ DEFUN(pdch_act, pdch_act_cmd,
else
activate = 0;
- rsl_ipacc_pdch_activate(ts, activate);
+ if (activate && ts->fi->state != TS_ST_UNUSED) {
+ vty_out(vty, "%% Timeslot %u is still in use%s",
+ ts->nr, VTY_NEWLINE);
+ return CMD_WARNING;
+ } else if (!activate && ts->fi->state != TS_ST_PDCH) {
+ vty_out(vty, "%% Timeslot %u is not in PDCH mode%s",
+ ts->nr, VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ LOG_TS(ts, LOGL_NOTICE, "telnet VTY user asks to %s", activate ? "PDCH ACT" : "PDCH DEACT");
+ ts->pdch_act_allowed = activate;
+ osmo_fsm_inst_state_chg(ts->fi, activate ? TS_ST_WAIT_PDCH_ACT : TS_ST_WAIT_PDCH_DEACT, 4, 0);
return CMD_SUCCESS;
}
-/* determine the logical channel type based on the physical channel type */
-static int lchan_type_by_pchan(enum gsm_phys_chan_config pchan)
-{
- switch (pchan) {
- case GSM_PCHAN_TCH_F:
- return GSM_LCHAN_TCH_F;
- case GSM_PCHAN_TCH_H:
- return GSM_LCHAN_TCH_H;
- case GSM_PCHAN_SDCCH8_SACCH8C:
- case GSM_PCHAN_SDCCH8_SACCH8C_CBCH:
- case GSM_PCHAN_CCCH_SDCCH4:
- case GSM_PCHAN_CCCH_SDCCH4_CBCH:
- return GSM_LCHAN_SDCCH;
- default:
- return -1;
- }
-}
-
/* configure the lchan for a single AMR mode (as specified) */
static int lchan_set_single_amr_mode(struct gsm_lchan *lchan, uint8_t amr_mode)
{
@@ -4516,19 +4463,21 @@ DEFUN(lchan_act, lchan_act_cmd,
else
activate = 0;
- if (ss_nr >= ts_subslots(ts)) {
- vty_out(vty, "%% subslot %d >= permitted %d for physical channel %s%s",
- ss_nr, ts_subslots(ts), gsm_pchan_name(ts->pchan), VTY_NEWLINE);
+ /* FIXME: allow dynamic channels with switchover, lchan_activate(lchan, FOR_VTY) */
+ if (ss_nr >= pchan_subslots(ts->pchan_is)) {
+ vty_out(vty, "%% subslot index %d too large for physical channel %s (%u slots)%s",
+ ss_nr, gsm_pchan_name(ts->pchan_is), pchan_subslots(ts->pchan_is),
+ VTY_NEWLINE);
return CMD_WARNING;
}
if (activate) {
int lchan_t;
- if (lchan->state != LCHAN_S_NONE) {
+ if (lchan->fi->state != LCHAN_ST_UNUSED) {
vty_out(vty, "%% Cannot activate: Channel busy!%s", VTY_NEWLINE);
return CMD_WARNING;
}
- lchan_t = lchan_type_by_pchan(ts->pchan);
+ lchan_t = gsm_lchan_type_by_pchan(ts->pchan_is);
if (lchan_t < 0)
return CMD_WARNING;
/* configure the lchan */
@@ -4549,10 +4498,16 @@ DEFUN(lchan_act, lchan_act_cmd,
lchan_set_single_amr_mode(lchan, amr_mode);
}
vty_out(vty, "%% activating lchan %s%s", gsm_lchan_name(lchan), VTY_NEWLINE);
- rsl_chan_activate_lchan(lchan, RSL_ACT_TYPE_INITIAL, 0);
- rsl_ipacc_crcx(lchan);
+ rsl_tx_chan_activ(lchan, RSL_ACT_TYPE_INITIAL, 0);
+ rsl_tx_ipacc_crcx(lchan);
} else {
- rsl_direct_rf_release(lchan);
+ if (!lchan->fi) {
+ vty_out(vty, "%% Cannot release: Channel not initialized%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ vty_out(vty, "%% Asking for release of %s in state %s%s", gsm_lchan_name(lchan),
+ osmo_fsm_inst_state_name(lchan->fi), VTY_NEWLINE);
+ lchan_release(lchan, false, false, 0);
}
return CMD_SUCCESS;
@@ -4576,15 +4531,18 @@ DEFUN(lchan_mdcx, lchan_mdcx_cmd,
lchan = &ts->lchan[ss_nr];
- if (ss_nr >= ts_subslots(ts)) {
- vty_out(vty, "%% subslot %d >= permitted %d for physical channel %s%s",
- ss_nr, ts_subslots(ts), gsm_pchan_name(ts->pchan), VTY_NEWLINE);
+ if (ss_nr >= pchan_subslots(ts->pchan_is)) {
+ vty_out(vty, "%% subslot index %d too large for physical channel %s (%u slots)%s",
+ ss_nr, gsm_pchan_name(ts->pchan_is), pchan_subslots(ts->pchan_is),
+ VTY_NEWLINE);
return CMD_WARNING;
}
vty_out(vty, "%% connecting RTP of %s to %s:%u%s", gsm_lchan_name(lchan),
inet_ntoa(ia), port, VTY_NEWLINE);
- rsl_ipacc_mdcx(lchan, ntohl(ia.s_addr), port, 0);
+ lchan->abis_ip.connect_ip = ia.s_addr;
+ lchan->abis_ip.connect_port = port;
+ rsl_tx_ipacc_mdcx(lchan);
return CMD_SUCCESS;
}
@@ -4763,9 +4721,11 @@ DEFUN(cfg_net_per_loc_upd, cfg_net_per_loc_upd_cmd,
"Periodic Location Updating Interval in Minutes\n")
{
struct gsm_network *net = vty->index;
+ struct T_def *d = T_def_get_entry(net->T_defs, 3212);
- net->t3212 = atoi(argv[0]) / 6;
-
+ OSMO_ASSERT(d);
+ d->val = atoi(argv[0]) / 6;
+ vty_out(vty, "T%d = %u %s (%s)%s", d->T, d->val, "* 6min", d->desc, VTY_NEWLINE);
return CMD_SUCCESS;
}
@@ -4777,9 +4737,11 @@ DEFUN(cfg_net_no_per_loc_upd, cfg_net_no_per_loc_upd_cmd,
"Periodic Location Updating Interval\n")
{
struct gsm_network *net = vty->index;
+ struct T_def *d = T_def_get_entry(net->T_defs, 3212);
- net->t3212 = 0;
-
+ OSMO_ASSERT(d);
+ d->val = 0;
+ vty_out(vty, "T%d = %u %s (%s)%s", d->T, d->val, "* 6min", d->desc, VTY_NEWLINE);
return CMD_SUCCESS;
}
@@ -4862,32 +4824,20 @@ int bsc_vty_init(struct gsm_network *network)
install_element_ve(&show_lchan_summary_cmd);
install_element_ve(&show_subscr_conn_cmd);
- install_element_ve(&handover_any_cmd);
- install_element_ve(&assignment_any_cmd);
install_element_ve(&show_paging_cmd);
install_element_ve(&show_paging_group_cmd);
+ install_element(ENABLE_NODE, &handover_any_cmd);
+ install_element(ENABLE_NODE, &assignment_any_cmd);
+ install_element(ENABLE_NODE, &handover_any_to_arfcn_bsic_cmd);
+
logging_vty_add_cmds(NULL);
osmo_talloc_vty_add_cmds();
+ T_defs_vty_init(network->T_defs, GSMNET_NODE);
+
install_element(GSMNET_NODE, &cfg_net_neci_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3101_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3103_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3105_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3107_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3109_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3111_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3113_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3115_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3117_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3119_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3122_cmd);
- install_element(GSMNET_NODE, &cfg_net_T3141_cmd);
- install_element(GSMNET_NODE, &cfg_net_T10_cmd);
- install_element(GSMNET_NODE, &cfg_net_T7_cmd);
- install_element(GSMNET_NODE, &cfg_net_T8_cmd);
- install_element(GSMNET_NODE, &cfg_net_T101_cmd);
install_element(GSMNET_NODE, &cfg_net_dtx_cmd);
install_element(GSMNET_NODE, &cfg_net_pag_any_tch_cmd);
/* See also handover commands added on net level from handover_vty.c */
diff --git a/src/osmo-bsc/bts_ericsson_rbs2000.c b/src/osmo-bsc/bts_ericsson_rbs2000.c
index 9c8b90ee2..c2975f4c6 100644
--- a/src/osmo-bsc/bts_ericsson_rbs2000.c
+++ b/src/osmo-bsc/bts_ericsson_rbs2000.c
@@ -28,6 +28,7 @@
#include <osmocom/bsc/abis_nm.h>
#include <osmocom/abis/e1_input.h>
#include <osmocom/bsc/signal.h>
+#include <osmocom/bsc/timeslot_fsm.h>
#include <osmocom/abis/lapd.h>
@@ -52,7 +53,7 @@ static void bootstrap_om_trx(struct gsm_bts_trx *trx)
static int shutdown_om(struct gsm_bts *bts)
{
- gsm_bts_mark_all_ts_uninitialized(bts);
+ gsm_bts_all_ts_dispatch(bts, TS_EV_OML_DOWN, NULL);
/* FIXME */
return 0;
diff --git a/src/osmo-bsc/bts_ipaccess_nanobts.c b/src/osmo-bsc/bts_ipaccess_nanobts.c
index 0e23955c8..412bba877 100644
--- a/src/osmo-bsc/bts_ipaccess_nanobts.c
+++ b/src/osmo-bsc/bts_ipaccess_nanobts.c
@@ -42,6 +42,7 @@
#include <osmocom/bsc/ipaccess.h>
#include <osmocom/bsc/bts_ipaccess_nanobts_omlattr.h>
#include <osmocom/bsc/paging.h>
+#include <osmocom/bsc/timeslot_fsm.h>
static int bts_model_nanobts_start(struct gsm_network *net);
static void bts_model_nanobts_e1line_bind_ops(struct e1inp_line *line);
@@ -169,7 +170,7 @@ static int nm_statechg_event(int evt, struct nm_statechg_signal_data *nsd)
if (new_state->operational == NM_OPSTATE_DISABLED &&
new_state->availability == NM_AVSTATE_DEPENDENCY) {
enum abis_nm_chan_comb ccomb =
- abis_nm_chcomb4pchan(ts->pchan);
+ abis_nm_chcomb4pchan(ts->pchan_from_config);
if (abis_nm_set_channel_attr(ts, ccomb) == -EINVAL) {
ipaccess_drop_oml(trx->bts);
return -1;
@@ -305,8 +306,12 @@ static void nm_rx_opstart_ack_chan(struct msgb *oml_msg)
if (!ts)
/* error already logged in abis_nm_get_ts() */
return;
+ if (!ts->fi) {
+ LOG_TS(ts, LOGL_ERROR, "Channel OPSTART ACK for uninitialized TS");
+ return;
+ }
- gsm_ts_check_init(ts);
+ osmo_fsm_inst_dispatch(ts->fi, TS_EV_OML_READY, NULL);
}
static void nm_rx_opstart_ack(struct msgb *oml_msg)
@@ -413,7 +418,7 @@ void ipaccess_drop_oml(struct gsm_bts *bts)
llist_for_each_entry(trx, &bts->trx_list, list)
ipaccess_drop_rsl(trx);
- gsm_bts_mark_all_ts_uninitialized(bts);
+ gsm_bts_all_ts_dispatch(bts, TS_EV_OML_DOWN, NULL);
bts->ip_access.flags = 0;
diff --git a/src/osmo-bsc/bts_ipaccess_nanobts_omlattr.c b/src/osmo-bsc/bts_ipaccess_nanobts_omlattr.c
index 1a8d9b07b..15c11860d 100644
--- a/src/osmo-bsc/bts_ipaccess_nanobts_omlattr.c
+++ b/src/osmo-bsc/bts_ipaccess_nanobts_omlattr.c
@@ -90,7 +90,7 @@ struct msgb *nanobts_attr_bts_get(struct gsm_bts *bts)
msgb_tv_fixed_put(msgb, NM_ATT_LDAVG_SLOTS, 2, buf);
/* 10 milliseconds */
- msgb_tv_put(msgb, NM_ATT_BTS_AIR_TIMER, bts->network->T3105 > 0? bts->network->T3105 : 13);
+ msgb_tv_put(msgb, NM_ATT_BTS_AIR_TIMER, T_def_get(bts->network->T_defs, 3105, T_MS, -1));
/* 10 retransmissions of physical config */
msgb_tv_put(msgb, NM_ATT_NY1, 10);
diff --git a/src/osmo-bsc/bts_nokia_site.c b/src/osmo-bsc/bts_nokia_site.c
index 4a24c3931..052741ed1 100644
--- a/src/osmo-bsc/bts_nokia_site.c
+++ b/src/osmo-bsc/bts_nokia_site.c
@@ -35,6 +35,7 @@
#include <osmocom/bsc/abis_nm.h>
#include <osmocom/abis/e1_input.h>
#include <osmocom/bsc/signal.h>
+#include <osmocom/bsc/timeslot_fsm.h>
#include <osmocom/core/timer.h>
@@ -55,13 +56,13 @@ static void bootstrap_om_bts(struct gsm_bts *bts)
{
LOGP(DNM, LOGL_NOTICE, "bootstrapping OML for BTS %u\n", bts->nr);
- gsm_bts_mark_all_ts_uninitialized(bts);
-
if (!bts->nokia.skip_reset) {
if (!bts->nokia.did_reset)
abis_nm_reset(bts, 1);
} else
bts->nokia.did_reset = 1;
+
+ gsm_bts_all_ts_dispatch(bts, TS_EV_OML_READY, NULL);
}
static void bootstrap_om_trx(struct gsm_bts_trx *trx)
@@ -69,7 +70,7 @@ static void bootstrap_om_trx(struct gsm_bts_trx *trx)
LOGP(DNM, LOGL_NOTICE, "bootstrapping OML for TRX %u/%u\n",
trx->bts->nr, trx->nr);
- gsm_trx_mark_all_ts_uninitialized(trx);
+ gsm_trx_all_ts_dispatch(trx, TS_EV_OML_READY, NULL);
}
static int shutdown_om(struct gsm_bts *bts)
@@ -779,24 +780,32 @@ static int make_fu_config(struct gsm_bts_trx *trx, uint8_t id,
0xFF = spare TS
*/
- if (ts->pchan == GSM_PCHAN_NONE)
+ switch (ts->pchan_from_config) {
+ case GSM_PCHAN_NONE:
chan_config = 0xFF;
- else if (ts->pchan == GSM_PCHAN_CCCH)
+ break;
+ case GSM_PCHAN_CCCH:
chan_config = 0;
- else if (ts->pchan == GSM_PCHAN_CCCH_SDCCH4)
+ break;
+ case GSM_PCHAN_CCCH_SDCCH4:
chan_config = 1;
- else if (ts->pchan == GSM_PCHAN_TCH_F)
+ break;
+ case GSM_PCHAN_TCH_F:
chan_config = 6; /* 9 should work too */
- else if (ts->pchan == GSM_PCHAN_TCH_H)
+ break;
+ case GSM_PCHAN_TCH_H:
chan_config = 9;
- else if (ts->pchan == GSM_PCHAN_SDCCH8_SACCH8C)
+ break;
+ case GSM_PCHAN_SDCCH8_SACCH8C:
chan_config = 4;
- else if (ts->pchan == GSM_PCHAN_PDCH)
+ break;
+ case GSM_PCHAN_PDCH:
chan_config = 11;
- else {
+ break;
+ default:
fprintf(stderr,
- "unsupported channel config %d for timeslot %d\n",
- ts->pchan, i);
+ "unsupported channel config %s for timeslot %d\n",
+ gsm_pchan_name(ts->pchan_from_config), i);
return 0;
}
diff --git a/src/osmo-bsc/bts_siemens_bs11.c b/src/osmo-bsc/bts_siemens_bs11.c
index 2d2351702..cf90575e2 100644
--- a/src/osmo-bsc/bts_siemens_bs11.c
+++ b/src/osmo-bsc/bts_siemens_bs11.c
@@ -27,6 +27,7 @@
#include <osmocom/bsc/abis_nm.h>
#include <osmocom/abis/e1_input.h>
#include <osmocom/bsc/signal.h>
+#include <osmocom/bsc/timeslot_fsm.h>
static int bts_model_bs11_start(struct gsm_network *net);
@@ -358,7 +359,7 @@ static void patch_nm_tables(struct gsm_bts *bts)
uint8_t arfcn_high = (bts->c0->arfcn >> 8) & 0x0f;
/* T3105 attribute in units of 10ms */
- bs11_attr_bts[2] = bts->network->T3105 / 10;
+ bs11_attr_bts[2] = T_def_get(bts->network->T_defs, 3105, T_MS, -1) / 10;
/* patch ARFCN into BTS Attributes */
bs11_attr_bts[69] &= 0xf0;
@@ -392,7 +393,7 @@ static void patch_nm_tables(struct gsm_bts *bts)
static void nm_reconfig_ts(struct gsm_bts_trx_ts *ts)
{
- enum abis_nm_chan_comb ccomb = abis_nm_chcomb4pchan(ts->pchan);
+ enum abis_nm_chan_comb ccomb = abis_nm_chcomb4pchan(ts->pchan_from_config);
struct gsm_e1_subslot *e1l = &ts->e1_link;
abis_nm_set_channel_attr(ts, ccomb);
@@ -536,7 +537,7 @@ static int shutdown_om(struct gsm_bts *bts)
/* Reset BTS Site manager resource */
abis_nm_bs11_reset_resource(bts);
- gsm_bts_mark_all_ts_uninitialized(bts);
+ gsm_bts_all_ts_dispatch(bts, TS_EV_OML_DOWN, NULL);
return 0;
}
diff --git a/src/osmo-bsc/chan_alloc.c b/src/osmo-bsc/chan_alloc.c
index a24fbea94..de8f63183 100644
--- a/src/osmo-bsc/chan_alloc.c
+++ b/src/osmo-bsc/chan_alloc.c
@@ -31,573 +31,12 @@
#include <osmocom/bsc/abis_rsl.h>
#include <osmocom/bsc/debug.h>
#include <osmocom/bsc/signal.h>
+#include <osmocom/bsc/timeslot_fsm.h>
+#include <osmocom/bsc/lchan_fsm.h>
#include <osmocom/bsc/gsm_04_08_utils.h>
#include <osmocom/core/talloc.h>
-bool ts_is_usable(const struct gsm_bts_trx_ts *ts)
-{
- if (!trx_is_usable(ts->trx)) {
- LOGP(DRLL, LOGL_DEBUG, "%s not usable\n", gsm_trx_name(ts->trx));
- return false;
- }
-
- /* If a TCH/F_PDCH TS is busy changing, it is already taken or not
- * yet available. */
- if (ts->pchan == GSM_PCHAN_TCH_F_PDCH) {
- if (ts->flags & TS_F_PDCH_PENDING_MASK) {
- LOGP(DRLL, LOGL_DEBUG, "%s in switchover, not available\n",
- gsm_ts_and_pchan_name(ts));
- return false;
- }
- }
-
- /* If a dynamic channel is busy changing, it is already taken or not
- * yet available. */
- if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) {
- if (ts->dyn.pchan_is != ts->dyn.pchan_want) {
- LOGP(DRLL, LOGL_DEBUG, "%s in switchover, not available\n",
- gsm_ts_and_pchan_name(ts));
- return false;
- }
- }
-
- return true;
-}
-
-static int trx_count_free_ts(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan)
-{
- struct gsm_bts_trx_ts *ts;
- int j, ss;
- int count = 0;
-
- if (!trx_is_usable(trx))
- return 0;
-
- for (j = 0; j < ARRAY_SIZE(trx->ts); j++) {
- enum gsm_phys_chan_config ts_pchan_is;
- ts = &trx->ts[j];
- if (!ts_is_usable(ts))
- continue;
-
- ts_pchan_is = ts_pchan(ts);
-
- if (ts_pchan_is == GSM_PCHAN_PDCH) {
- /* Dynamic timeslots in PDCH mode will become TCH if needed. */
- switch (ts->pchan) {
- case GSM_PCHAN_TCH_F_PDCH:
- if (pchan == GSM_PCHAN_TCH_F)
- count++;
- continue;
-
- case GSM_PCHAN_TCH_F_TCH_H_PDCH:
- if (pchan == GSM_PCHAN_TCH_F)
- count++;
- else if (pchan == GSM_PCHAN_TCH_H)
- count += 2;
- continue;
-
- default:
- /* Not dynamic, not applicable. */
- continue;
- }
- }
-
- if (ts_pchan_is != pchan)
- continue;
- /* check if all sub-slots are allocated yet */
- for (ss = 0; ss < ts_subslots(ts); ss++) {
- struct gsm_lchan *lc = &ts->lchan[ss];
- if (lc->type == GSM_LCHAN_NONE &&
- lc->state == LCHAN_S_NONE)
- count++;
- }
- }
-
- return count;
-}
-
-/* Count number of free TS of given pchan type */
-int bts_count_free_ts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
-{
- struct gsm_bts_trx *trx;
- int count = 0;
-
- llist_for_each_entry(trx, &bts->trx_list, list)
- count += trx_count_free_ts(trx, pchan);
-
- return count;
-}
-
-static bool ts_usable_as_pchan(struct gsm_bts_trx_ts *ts,
- enum gsm_phys_chan_config as_pchan)
-{
- switch (ts->pchan) {
- case GSM_PCHAN_TCH_F_PDCH:
- if (ts->flags & TS_F_PDCH_PENDING_MASK) {
- /* currently being switched over. Not usable. */
- return false;
- }
- switch (as_pchan) {
- case GSM_PCHAN_TCH_F:
- case GSM_PCHAN_PDCH:
- /* continue to check below. */
- break;
- default:
- return false;
- }
- break;
-
- case GSM_PCHAN_TCH_F_TCH_H_PDCH:
- if (ts->dyn.pchan_is != ts->dyn.pchan_want) {
- /* currently being switched over. Not usable. */
- return false;
- }
- switch (as_pchan) {
- case GSM_PCHAN_TCH_F:
- case GSM_PCHAN_TCH_H:
- case GSM_PCHAN_PDCH:
- /* continue to check below. */
- break;
- default:
- return false;
- }
- break;
-
- default:
- /* static timeslots never switch. */
- return ts->pchan == as_pchan;
- }
-
- /* Dynamic timeslots -- Checks depending on the current actual pchan mode: */
- switch (ts_pchan(ts)) {
- case GSM_PCHAN_NONE:
- /* Not initialized, possibly because GPRS was disabled. We may switch. */
- return true;
-
- case GSM_PCHAN_PDCH:
- /* This slot is in PDCH mode and available to switch pchan mode. But check for
- * error states: */
- if (ts->lchan->state != LCHAN_S_NONE && ts->lchan->state != LCHAN_S_ACTIVE)
- return false;
- return true;
-
- case GSM_PCHAN_TCH_F:
- case GSM_PCHAN_TCH_H:
- /* No need to switch at all? */
- if (ts_pchan(ts) == as_pchan)
- return true;
-
- /* If any lchan is in use, we can't change the pchan kind */
- {
- int ss;
- int subslots = ts_subslots(ts);
- for (ss = 0; ss < subslots; ss++) {
- struct gsm_lchan *lc = &ts->lchan[ss];
- if (lc->type != GSM_LCHAN_NONE || lc->state != LCHAN_S_NONE)
- return false;
- }
- }
- return true;
-
- default:
- /* Not implemented. */
- return false;
- }
-}
-
-static struct gsm_lchan *
-_lc_find_trx(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan,
- enum gsm_phys_chan_config as_pchan)
-{
- struct gsm_bts_trx_ts *ts;
- int j, start, stop, dir, ss;
- int check_subslots;
-
-#define LOGPLCHANALLOC(fmt, args...) \
- LOGP(DRLL, LOGL_DEBUG, "looking for lchan %s as %s: " fmt, \
- gsm_pchan_name(pchan), gsm_pchan_name(as_pchan), ## args)
-
- if (!trx_is_usable(trx)) {
- LOGPLCHANALLOC("%s trx not usable\n", gsm_trx_name(trx));
- return NULL;
- }
-
- if (trx->bts->chan_alloc_reverse) {
- /* check TS 7..0 */
- start = 7;
- stop = -1;
- dir = -1;
- } else {
- /* check TS 0..7 */
- start = 0;
- stop = 8;
- dir = 1;
- }
-
- for (j = start; j != stop; j += dir) {
- ts = &trx->ts[j];
- if (!ts_is_usable(ts))
- continue;
- /* The caller first selects what kind of TS to search in, e.g. looking for exact
- * GSM_PCHAN_TCH_F, or maybe among dynamic GSM_PCHAN_TCH_F_TCH_H_PDCH... */
- if (ts->pchan != pchan) {
- LOGPLCHANALLOC("%s is != %s\n", gsm_ts_and_pchan_name(ts),
- gsm_pchan_name(pchan));
- continue;
- }
- /* Next, is this timeslot in or can it be switched to the pchan we want to use it for? */
- if (!ts_usable_as_pchan(ts, as_pchan)) {
- LOGPLCHANALLOC("%s is not usable as %s\n", gsm_ts_and_pchan_name(ts),
- gsm_pchan_name(as_pchan));
- continue;
- }
- /* If we need to switch it, after above check we are also allowed to switch it, and we
- * will always use the first lchan after the switch. Return that lchan and rely on the
- * caller to perform the pchan switchover. */
- if (ts_pchan(ts) != as_pchan) {
- LOGPLCHANALLOC("%s is a match, will switch to %s\n", gsm_ts_and_pchan_name(ts),
- gsm_pchan_name(as_pchan));
- return ts->lchan;
- }
-
- /* TS is in desired pchan mode. Go ahead and check for an available lchan. */
- check_subslots = ts_subslots(ts);
- for (ss = 0; ss < check_subslots; ss++) {
- struct gsm_lchan *lc = &ts->lchan[ss];
- if (lc->type == GSM_LCHAN_NONE &&
- lc->state == LCHAN_S_NONE) {
- LOGPLCHANALLOC("%s ss=%d is available\n", gsm_ts_and_pchan_name(ts),
- lc->nr);
- return lc;
- }
- LOGPLCHANALLOC("%s ss=%d in type=%s,state=%s not suitable\n",
- gsm_ts_and_pchan_name(ts), lc->nr, gsm_lchant_name(lc->type),
- gsm_lchans_name(lc->state));
- }
- }
-
- return NULL;
-#undef LOGPLCHANALLOC
-}
-
-static struct gsm_lchan *
-_lc_dyn_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan,
- enum gsm_phys_chan_config dyn_as_pchan)
-{
- struct gsm_bts_trx *trx;
- struct gsm_lchan *lc;
-
- if (bts->chan_alloc_reverse) {
- llist_for_each_entry_reverse(trx, &bts->trx_list, list) {
- lc = _lc_find_trx(trx, pchan, dyn_as_pchan);
- if (lc)
- return lc;
- }
- } else {
- llist_for_each_entry(trx, &bts->trx_list, list) {
- lc = _lc_find_trx(trx, pchan, dyn_as_pchan);
- if (lc)
- return lc;
- }
- }
-
- return NULL;
-}
-
-static struct gsm_lchan *
-_lc_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
-{
- return _lc_dyn_find_bts(bts, pchan, pchan);
-}
-
-/* Allocate a logical channel.
- *
- * Dynamic channel types: we always prefer a dedicated TS, and only pick +
- * switch a dynamic TS if no pure TS of the requested PCHAN is available.
- *
- * TCH_F/PDCH: if we pick a PDCH ACT style dynamic TS as TCH/F channel, PDCH
- * will be disabled in rsl_chan_activate_lchan(); there is no need to check
- * whether PDCH mode is currently active, here.
- */
-struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type,
- int allow_bigger)
-{
- struct gsm_lchan *lchan = NULL;
- enum gsm_phys_chan_config first, first_cbch, second, second_cbch;
-
- LOGP(DRLL, LOGL_DEBUG, "(bts=%d) lchan_alloc(%s)\n", bts->nr, gsm_lchant_name(type));
-
- switch (type) {
- case GSM_LCHAN_SDCCH:
- if (bts->chan_alloc_reverse) {
- first = GSM_PCHAN_SDCCH8_SACCH8C;
- first_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH;
- second = GSM_PCHAN_CCCH_SDCCH4;
- second_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH;
- } else {
- first = GSM_PCHAN_CCCH_SDCCH4;
- first_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH;
- second = GSM_PCHAN_SDCCH8_SACCH8C;
- second_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH;
- }
-
- lchan = _lc_find_bts(bts, first);
- if (lchan == NULL)
- lchan = _lc_find_bts(bts, first_cbch);
- if (lchan == NULL)
- lchan = _lc_find_bts(bts, second);
- if (lchan == NULL)
- lchan = _lc_find_bts(bts, second_cbch);
-
- /* allow to assign bigger channels */
- if (allow_bigger) {
- if (lchan == NULL) {
- lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H);
- if (lchan)
- type = GSM_LCHAN_TCH_H;
- }
-
- if (lchan == NULL) {
- lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F);
- if (lchan)
- type = GSM_LCHAN_TCH_F;
- }
-
- /* try dynamic TCH/F_PDCH */
- if (lchan == NULL) {
- lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH,
- GSM_PCHAN_TCH_F);
- /* TCH/F_PDCH will be used as TCH/F */
- if (lchan)
- type = GSM_LCHAN_TCH_F;
- }
-
- /* try fully dynamic TCH/F_TCH/H_PDCH */
- if (lchan == NULL) {
- lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_TCH_H_PDCH,
- GSM_PCHAN_TCH_H);
- if (lchan)
- type = GSM_LCHAN_TCH_H;
- }
- /*
- * No need to check fully dynamic channels for TCH/F:
- * if no TCH/H was available, neither will be TCH/F.
- */
- }
- break;
- case GSM_LCHAN_TCH_F:
- lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F);
- /* If we don't have TCH/F available, fall-back to TCH/H */
- if (!lchan) {
- lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H);
- if (lchan)
- type = GSM_LCHAN_TCH_H;
- }
- /* If we don't have TCH/H either, try dynamic TCH/F_PDCH */
- if (!lchan) {
- lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH,
- GSM_PCHAN_TCH_F);
- /* TCH/F_PDCH used as TCH/F -- here, type is already
- * set to GSM_LCHAN_TCH_F, but for clarity's sake... */
- if (lchan)
- type = GSM_LCHAN_TCH_F;
- }
-
- /* Try fully dynamic TCH/F_TCH/H_PDCH as TCH/F... */
- if (!lchan && bts->network->dyn_ts_allow_tch_f) {
- lchan = _lc_dyn_find_bts(bts,
- GSM_PCHAN_TCH_F_TCH_H_PDCH,
- GSM_PCHAN_TCH_F);
- if (lchan)
- type = GSM_LCHAN_TCH_F;
- }
- /* ...and as TCH/H. */
- if (!lchan) {
- lchan = _lc_dyn_find_bts(bts,
- GSM_PCHAN_TCH_F_TCH_H_PDCH,
- GSM_PCHAN_TCH_H);
- if (lchan)
- type = GSM_LCHAN_TCH_H;
- }
- break;
- case GSM_LCHAN_TCH_H:
- lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H);
- /* If we don't have TCH/H available, fall-back to TCH/F */
- if (!lchan) {
- lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F);
- if (lchan)
- type = GSM_LCHAN_TCH_F;
- }
- /* No dedicated TCH/x available -- try fully dynamic
- * TCH/F_TCH/H_PDCH */
- if (!lchan) {
- lchan = _lc_dyn_find_bts(bts,
- GSM_PCHAN_TCH_F_TCH_H_PDCH,
- GSM_PCHAN_TCH_H);
- if (lchan)
- type = GSM_LCHAN_TCH_H;
- }
- /*
- * No need to check TCH/F_TCH/H_PDCH channels for TCH/F:
- * if no TCH/H was available, neither will be TCH/F.
- */
- /* If we don't have TCH/F either, try dynamic TCH/F_PDCH */
- if (!lchan) {
- lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH,
- GSM_PCHAN_TCH_F);
- if (lchan)
- type = GSM_LCHAN_TCH_F;
- }
- break;
- default:
- LOGP(DRLL, LOGL_ERROR, "Unknown gsm_chan_t %u\n", type);
- }
-
- if (lchan) {
- lchan->type = type;
-
- LOGP(DRLL, LOGL_INFO, "%s Allocating lchan=%u as %s\n",
- gsm_ts_and_pchan_name(lchan->ts),
- lchan->nr, gsm_lchant_name(lchan->type));
-
- /* reset measurement report counter and index */
- lchan->meas_rep_count = 0;
- lchan->meas_rep_idx = 0;
- lchan->meas_rep_last_seen_nr = 255;
-
- /* clear sapis */
- memset(lchan->sapis, 0, ARRAY_SIZE(lchan->sapis));
-
- /* clear multi rate config */
- memset(&lchan->mr_ms_lv, 0, sizeof(lchan->mr_ms_lv));
- memset(&lchan->mr_bts_lv, 0, sizeof(lchan->mr_bts_lv));
- lchan->broken_reason = "";
- } else {
- struct challoc_signal_data sig;
-
- LOGP(DRLL, LOGL_ERROR, "(bts=%d) Failed to allocate %s channel\n",
- bts->nr, gsm_lchant_name(type));
-
- sig.bts = bts;
- sig.type = type;
- osmo_signal_dispatch(SS_CHALLOC, S_CHALLOC_ALLOC_FAIL, &sig);
- }
-
- return lchan;
-}
-
-/* Free a logical channel */
-void lchan_free(struct gsm_lchan *lchan)
-{
- struct challoc_signal_data sig;
- int i;
-
- sig.type = lchan->type;
- lchan->type = GSM_LCHAN_NONE;
-
-
- if (lchan->conn
- && !(lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
- && lchan->ts->dyn.pchan_is != lchan->ts->dyn.pchan_want)) {
- struct lchan_signal_data sig;
-
- /* We might kill an active channel... */
- sig.lchan = lchan;
- sig.mr = NULL;
- osmo_signal_dispatch(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, &sig);
- }
-
- /* stop the timer */
- osmo_timer_del(&lchan->T3101);
-
- /* clear cached measuement reports */
- lchan->meas_rep_idx = 0;
- for (i = 0; i < ARRAY_SIZE(lchan->meas_rep); i++) {
- lchan->meas_rep[i].flags = 0;
- lchan->meas_rep[i].nr = 0;
- }
- for (i = 0; i < ARRAY_SIZE(lchan->neigh_meas); i++)
- lchan->neigh_meas[i].arfcn = 0;
-
- if (lchan->rqd_ref) {
- talloc_free(lchan->rqd_ref);
- lchan->rqd_ref = NULL;
- lchan->rqd_ta = 0;
- }
-
- sig.lchan = lchan;
- sig.bts = lchan->ts->trx->bts;
- osmo_signal_dispatch(SS_CHALLOC, S_CHALLOC_FREED, &sig);
-
- if (lchan->conn
- && !(lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
- && lchan->ts->dyn.pchan_is != lchan->ts->dyn.pchan_want)) {
- LOGP(DRLL, LOGL_ERROR, "the subscriber connection should be gone.\n");
- lchan->conn = NULL;
- }
-
- /* FIXME: ts_free() the timeslot, if we're the last logical
- * channel using it */
-}
-
-/*
- * There was an error with the TRX and we need to forget
- * any state so that a lchan can be allocated again after
- * the trx is fully usable.
- *
- * This should be called after lchan_free to force a channel
- * be available for allocation again. This means that this
- * method will stop the "delay after error"-timer and set the
- * state to LCHAN_S_NONE.
- */
-void lchan_reset(struct gsm_lchan *lchan)
-{
- osmo_timer_del(&lchan->T3101);
- osmo_timer_del(&lchan->T3109);
- osmo_timer_del(&lchan->T3111);
- osmo_timer_del(&lchan->error_timer);
-
- lchan->type = GSM_LCHAN_NONE;
- rsl_lchan_set_state(lchan, LCHAN_S_NONE);
-}
-
-/* Drive the release process of the lchan */
-static void _lchan_handle_release(struct gsm_lchan *lchan,
- int sacch_deact, int mode)
-{
- /* Release all SAPIs on the local end and continue */
- rsl_release_sapis_from(lchan, 1, RSL_REL_LOCAL_END);
-
- /*
- * Shall we send a RR Release, start T3109 and wait for the
- * release indication from the BTS or just take it down (e.g.
- * on assignment requests)
- */
- if (sacch_deact) {
- gsm48_send_rr_release(lchan);
-
- /* Deactivate the SACCH on the BTS side */
- rsl_deact_sacch(lchan);
- rsl_start_t3109(lchan);
- } else if (lchan->sapis[0] == LCHAN_SAPI_UNUSED) {
- rsl_direct_rf_release(lchan);
- } else {
- rsl_release_request(lchan, 0, mode);
- }
-}
-
-/* Consider releasing the channel now */
-int lchan_release(struct gsm_lchan *lchan, int sacch_deact, enum rsl_rel_mode mode)
-{
- DEBUGP(DRLL, "%s starting release sequence\n", gsm_lchan_name(lchan));
- rsl_lchan_set_state(lchan, LCHAN_S_REL_REQ);
-
- lchan->conn = NULL;
- _lchan_handle_release(lchan, sacch_deact, mode);
- return 1;
-}
-
void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts)
{
struct gsm_bts_trx *trx;
@@ -612,22 +51,18 @@ void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts)
for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
struct gsm_bts_trx_ts *ts = &trx->ts[i];
- struct load_counter *pl = &cl->pchan[ts->pchan];
- int j;
- int subslots;
+ struct load_counter *pl = &cl->pchan[ts->pchan_on_init];
+ struct gsm_lchan *lchan;
/* skip administratively deactivated timeslots */
if (!nm_is_running(&ts->mo.nm_state))
continue;
- subslots = ts_subslots(ts);
- for (j = 0; j < subslots; j++) {
- struct gsm_lchan *lchan = &ts->lchan[j];
-
+ ts_for_each_lchan(lchan, ts) {
pl->total++;
- switch (lchan->state) {
- case LCHAN_S_NONE:
+ switch (lchan->fi->state) {
+ case LCHAN_ST_UNUSED:
break;
default:
pl->used++;
diff --git a/src/osmo-bsc/gsm_04_08_utils.c b/src/osmo-bsc/gsm_04_08_utils.c
index e9aeb550f..942252a70 100644
--- a/src/osmo-bsc/gsm_04_08_utils.c
+++ b/src/osmo-bsc/gsm_04_08_utils.c
@@ -41,15 +41,6 @@
* or should OpenBSC always act as RTP relay/proxy in between (0) ? */
int ipacc_rtp_direct = 1;
-static int gsm48_sendmsg(struct msgb *msg)
-{
- if (msg->lchan)
- msg->dst = msg->lchan->ts->trx->rsl_link;
-
- msg->l3h = msg->data;
- return rsl_data_request(msg, 0);
-}
-
/* Section 9.1.8 / Table 9.9 */
struct chreq {
uint8_t val;
@@ -225,76 +216,7 @@ int get_reason_by_chreq(uint8_t ra, int neci)
return GSM_CHREQ_REASON_OTHER;
}
-static void mr_config_for_ms(struct gsm_lchan *lchan, struct msgb *msg)
-{
- if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR)
- msgb_tlv_put(msg, GSM48_IE_MUL_RATE_CFG, lchan->mr_ms_lv[0],
- lchan->mr_ms_lv + 1);
-}
-
-/* 7.1.7 and 9.1.7: RR CHANnel RELease */
-int gsm48_send_rr_release(struct gsm_lchan *lchan)
-{
- struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 RR REL");
- struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
- uint8_t *cause;
-
- msg->lchan = lchan;
- gh->proto_discr = GSM48_PDISC_RR;
- gh->msg_type = GSM48_MT_RR_CHAN_REL;
-
- cause = msgb_put(msg, 1);
- cause[0] = GSM48_RR_CAUSE_NORMAL;
-
- DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
- lchan->nr, lchan->type);
-
- /* Send actual release request to MS */
- return gsm48_sendmsg(msg);
-}
-
-int send_siemens_mrpci(struct gsm_lchan *lchan,
- uint8_t *classmark2_lv)
-{
- struct rsl_mrpci mrpci;
-
- if (classmark2_lv[0] < 2)
- return -EINVAL;
-
- mrpci.power_class = classmark2_lv[1] & 0x7;
- mrpci.vgcs_capable = classmark2_lv[2] & (1 << 1);
- mrpci.vbs_capable = classmark2_lv[2] & (1 <<2);
- mrpci.gsm_phase = (classmark2_lv[1]) >> 5 & 0x3;
-
- return rsl_siemens_mrpci(lchan, &mrpci);
-}
-
-/* Chapter 9.1.9: Ciphering Mode Command */
-int gsm48_send_rr_ciph_mode(struct gsm_lchan *lchan, int want_imeisv)
-{
- struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CIPH");
- struct gsm48_hdr *gh;
- uint8_t ciph_mod_set;
-
- msg->lchan = lchan;
-
- DEBUGP(DRR, "TX CIPHERING MODE CMD\n");
-
- if (lchan->encr.alg_id <= RSL_ENC_ALG_A5(0))
- ciph_mod_set = 0;
- else
- ciph_mod_set = (lchan->encr.alg_id-2)<<1 | 1;
-
- gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
- gh->proto_discr = GSM48_PDISC_RR;
- gh->msg_type = GSM48_MT_RR_CIPH_M_CMD;
- gh->data[0] = (want_imeisv & 0x1) << 4 | (ciph_mod_set & 0xf);
-
- return rsl_encryption_cmd(msg);
-}
-
-static void gsm48_cell_desc(struct gsm48_cell_desc *cd,
- const struct gsm_bts *bts)
+void gsm48_cell_desc(struct gsm48_cell_desc *cd, const struct gsm_bts *bts)
{
cd->ncc = (bts->bsic >> 3 & 0x7);
cd->bcc = (bts->bsic & 0x7);
@@ -350,15 +272,13 @@ int gsm48_multirate_config(uint8_t *lv, const struct amr_multirate_conf *mr, con
#define GSM48_HOCMD_CCHDESC_LEN 16
/* Chapter 9.1.15: Handover Command */
-int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan,
- uint8_t power_command, uint8_t ho_ref)
+struct msgb *gsm48_make_ho_cmd(struct gsm_lchan *new_lchan, uint8_t power_command, uint8_t ho_ref)
{
struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 HO CMD");
struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
struct gsm48_ho_cmd *ho =
(struct gsm48_ho_cmd *) msgb_put(msg, sizeof(*ho));
- msg->lchan = old_lchan;
gh->proto_discr = GSM48_PDISC_RR;
gh->msg_type = GSM48_MT_RR_HANDO_CMD;
@@ -393,118 +313,7 @@ int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan,
msgb_tlv_put(msg, GSM48_IE_MUL_RATE_CFG, new_lchan->mr_ms_lv[0],
new_lchan->mr_ms_lv + 1);
- return gsm48_sendmsg(msg);
-}
-
-/* Chapter 9.1.2: Assignment Command */
-int gsm48_send_rr_ass_cmd(struct gsm_lchan *dest_lchan, struct gsm_lchan *lchan, uint8_t power_command)
-{
- struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ASS CMD");
- struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
- struct gsm48_ass_cmd *ass =
- (struct gsm48_ass_cmd *) msgb_put(msg, sizeof(*ass));
-
- DEBUGP(DRR, "-> ASSIGNMENT COMMAND tch_mode=0x%02x\n", lchan->tch_mode);
-
- msg->lchan = dest_lchan;
- gh->proto_discr = GSM48_PDISC_RR;
- gh->msg_type = GSM48_MT_RR_ASS_CMD;
-
- /*
- * fill the channel information element, this code
- * should probably be shared with rsl_rx_chan_rqd(),
- * gsm48_lchan_modify(). But beware that 10.5.2.5
- * 10.5.2.5.a have slightly different semantic for
- * the chan_desc. But as long as multi-slot configurations
- * are not used we seem to be fine.
- */
- gsm48_lchan2chan_desc(&ass->chan_desc, lchan);
- ass->power_command = power_command;
-
- /* optional: cell channel description */
-
- msgb_tv_put(msg, GSM48_IE_CHANMODE_1, lchan->tch_mode);
-
- /* mobile allocation in case of hopping */
- if (lchan->ts->hopping.enabled) {
- msgb_tlv_put(msg, GSM48_IE_MA_BEFORE, lchan->ts->hopping.ma_len,
- lchan->ts->hopping.ma_data);
- }
-
- /* in case of multi rate we need to attach a config */
- mr_config_for_ms(lchan, msg);
-
- return gsm48_sendmsg(msg);
-}
-
-/* 9.1.5 Channel mode modify: Modify the mode on the MS side */
-int gsm48_lchan_modify(struct gsm_lchan *lchan, uint8_t mode)
-{
- struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CHN MOD");
- struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
- struct gsm48_chan_mode_modify *cmm =
- (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
-
- DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
-
- lchan->tch_mode = mode;
- msg->lchan = lchan;
- gh->proto_discr = GSM48_PDISC_RR;
- gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
-
- /* fill the channel information element, this code
- * should probably be shared with rsl_rx_chan_rqd() */
- gsm48_lchan2chan_desc(&cmm->chan_desc, lchan);
- cmm->mode = mode;
-
- /* in case of multi rate we need to attach a config */
- mr_config_for_ms(lchan, msg);
-
- return gsm48_sendmsg(msg);
-}
-
-int gsm48_rx_rr_modif_ack(struct msgb *msg)
-{
- int rc;
- struct gsm48_hdr *gh = msgb_l3(msg);
- struct gsm48_chan_mode_modify *mod =
- (struct gsm48_chan_mode_modify *) gh->data;
-
- DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
-
- if (mod->mode != msg->lchan->tch_mode) {
- LOGP(DRR, LOGL_ERROR, "CHANNEL MODE change failed. Wanted: %d Got: %d\n",
- msg->lchan->tch_mode, mod->mode);
- return -1;
- }
-
- /* update the channel type */
- switch (mod->mode) {
- case GSM48_CMODE_SIGN:
- msg->lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
- break;
- case GSM48_CMODE_SPEECH_V1:
- case GSM48_CMODE_SPEECH_EFR:
- case GSM48_CMODE_SPEECH_AMR:
- msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
- break;
- case GSM48_CMODE_DATA_14k5:
- case GSM48_CMODE_DATA_12k0:
- case GSM48_CMODE_DATA_6k0:
- case GSM48_CMODE_DATA_3k6:
- msg->lchan->rsl_cmode = RSL_CMOD_SPD_DATA;
- break;
- }
-
- /* We've successfully modified the MS side of the channel,
- * now go on to modify the BTS side of the channel */
- rc = rsl_chan_mode_modify_req(msg->lchan);
-
- /* FIXME: we not only need to do this after mode modify, but
- * also after channel activation */
- if (is_ipaccess_bts(msg->lchan->ts->trx->bts) && mod->mode != GSM48_CMODE_SIGN)
- rsl_ipacc_crcx(msg->lchan);
- return rc;
+ return msg;
}
int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
@@ -587,66 +396,6 @@ int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
return 0;
}
-/* 9.2.5 CM service accept */
-int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
-{
- struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 SERV ACK");
- struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
-
- msg->lchan = conn->lchan;
-
- gh->proto_discr = GSM48_PDISC_MM;
- gh->msg_type = GSM48_MT_MM_CM_SERV_ACC;
-
- DEBUGP(DMM, "-> CM SERVICE ACK\n");
-
- return gsm0808_submit_dtap(conn, msg, 0, 0);
-}
-
-/* 9.2.6 CM service reject */
-int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
- enum gsm48_reject_value value)
-{
- struct msgb *msg;
-
- msg = gsm48_create_mm_serv_rej(value);
- if (!msg) {
- LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
- return -1;
- }
-
- DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
-
- return gsm0808_submit_dtap(conn, msg, 0, 0);
-}
-
-/* 9.1.29 RR Status */
-struct msgb *gsm48_create_rr_status(uint8_t cause)
-{
- struct msgb *msg;
- struct gsm48_hdr *gh;
-
- msg = gsm48_msgb_alloc_name("GSM 04.08 RR STATUS");
- if (!msg)
- return NULL;
-
- gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
- gh->proto_discr = GSM48_PDISC_RR;
- gh->msg_type = GSM48_MT_RR_STATUS;
- gh->data[0] = cause;
-
- return msg;
-}
-
-/* 9.1.29 RR Status */
-int gsm48_tx_rr_status(struct gsm_subscriber_connection *conn, uint8_t cause)
-{
- struct msgb *msg = gsm48_create_rr_status(cause);
- if (!msg)
- return -1;
- return gsm0808_submit_dtap(conn, msg, 0, 0);
-}
-
struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
{
struct msgb *msg;
diff --git a/src/osmo-bsc/gsm_data.c b/src/osmo-bsc/gsm_data.c
index 734e2fb6f..a5e64cfd6 100644
--- a/src/osmo-bsc/gsm_data.c
+++ b/src/osmo-bsc/gsm_data.c
@@ -39,6 +39,9 @@
#include <osmocom/bsc/bsc_msc_data.h>
#include <osmocom/bsc/abis_nm.h>
#include <osmocom/bsc/handover_cfg.h>
+#include <osmocom/bsc/timeslot_fsm.h>
+#include <osmocom/bsc/lchan_fsm.h>
+#include <osmocom/bsc/mgw_endpoint_fsm.h>
void *tall_bsc_ctx = NULL;
@@ -470,7 +473,7 @@ const struct value_string gsm_chreq_descs[] = {
{ 0, NULL }
};
-const struct value_string gsm_pchant_names[13] = {
+const struct value_string gsm_pchant_names[] = {
{ GSM_PCHAN_NONE, "NONE" },
{ GSM_PCHAN_CCCH, "CCCH" },
{ GSM_PCHAN_CCCH_SDCCH4,"CCCH+SDCCH4" },
@@ -486,6 +489,22 @@ const struct value_string gsm_pchant_names[13] = {
{ 0, NULL }
};
+const struct value_string gsm_pchan_ids[] = {
+ { GSM_PCHAN_NONE, "NONE" },
+ { GSM_PCHAN_CCCH, "CCCH" },
+ { GSM_PCHAN_CCCH_SDCCH4,"CCCH_SDCCH4" },
+ { GSM_PCHAN_TCH_F, "TCH_F" },
+ { GSM_PCHAN_TCH_H, "TCH_H" },
+ { GSM_PCHAN_SDCCH8_SACCH8C, "SDCCH8" },
+ { GSM_PCHAN_PDCH, "PDCH" },
+ { GSM_PCHAN_TCH_F_PDCH, "TCH_F_PDCH" },
+ { GSM_PCHAN_UNKNOWN, "UNKNOWN" },
+ { GSM_PCHAN_CCCH_SDCCH4_CBCH, "CCCH_SDCCH4_CBCH" },
+ { GSM_PCHAN_SDCCH8_SACCH8C_CBCH, "SDCCH8_CBCH" },
+ { GSM_PCHAN_TCH_F_TCH_H_PDCH, "TCH_F_TCH_H_PDCH" },
+ { 0, NULL }
+};
+
const struct value_string gsm_pchant_descs[13] = {
{ GSM_PCHAN_NONE, "Physical Channel not configured" },
{ GSM_PCHAN_CCCH, "FCCH + SCH + BCCH + CCCH (Comb. IV)" },
@@ -519,22 +538,6 @@ const char *gsm_lchant_name(enum gsm_chan_t c)
return get_value_string(gsm_chan_t_names, c);
}
-static const struct value_string lchan_s_names[] = {
- { LCHAN_S_NONE, "NONE" },
- { LCHAN_S_ACT_REQ, "ACTIVATION REQUESTED" },
- { LCHAN_S_ACTIVE, "ACTIVE" },
- { LCHAN_S_INACTIVE, "INACTIVE" },
- { LCHAN_S_REL_REQ, "RELEASE REQUESTED" },
- { LCHAN_S_REL_ERR, "RELEASE DUE ERROR" },
- { LCHAN_S_BROKEN, "BROKEN UNUSABLE" },
- { 0, NULL }
-};
-
-const char *gsm_lchans_name(enum gsm_lchan_state s)
-{
- return get_value_string(lchan_s_names, s);
-}
-
static const struct value_string chreq_names[] = {
{ GSM_CHREQ_REASON_EMERG, "EMERGENCY" },
{ GSM_CHREQ_REASON_PAG, "PAGING" },
@@ -549,7 +552,7 @@ const char *gsm_chreq_name(enum gsm_chreq_reason_t c)
return get_value_string(chreq_names, c);
}
-struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num)
+struct gsm_bts *gsm_bts_num(const struct gsm_network *net, int num)
{
struct gsm_bts *bts;
@@ -564,61 +567,63 @@ struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num)
return NULL;
}
-bool gsm_bts_matches_cell_id(struct gsm_bts *bts, const struct gsm0808_cell_id *ci)
+bool gsm_bts_matches_lai(const struct gsm_bts *bts,
+ const struct osmo_location_area_id *lai)
{
- if (!bts || !ci)
+ return osmo_plmn_cmp(&lai->plmn, &bts->network->plmn) == 0
+ && lai->lac == bts->location_area_code;
+}
+
+bool gsm_bts_matches_cell_id(const struct gsm_bts *bts, const struct gsm0808_cell_id *cell_id)
+{
+ const union gsm0808_cell_id_u *id = &cell_id->id;
+ if (!bts || !cell_id)
return false;
- switch (ci->id_discr) {
+
+ switch (cell_id->id_discr) {
case CELL_IDENT_WHOLE_GLOBAL:
- if (osmo_plmn_cmp(&bts->network->plmn, &ci->id.global.lai.plmn))
- return false;
- if (bts->location_area_code != ci->id.global.lai.lac)
- return false;
- if (bts->cell_identity != ci->id.global.cell_identity)
- return false;
- return true;
+ return gsm_bts_matches_lai(bts, &id->global.lai)
+ && id->global.cell_identity == bts->cell_identity;
case CELL_IDENT_LAC_AND_CI:
- if (bts->location_area_code != ci->id.lac_and_ci.lac)
- return false;
- if (bts->cell_identity != ci->id.lac_and_ci.ci)
- return false;
- return true;
+ return id->lac_and_ci.lac == bts->location_area_code
+ && id->lac_and_ci.ci == bts->cell_identity;
case CELL_IDENT_CI:
- if (bts->cell_identity != ci->id.ci)
- return false;
- return true;
+ return id->ci == bts->cell_identity;
case CELL_IDENT_NO_CELL:
return false;
case CELL_IDENT_LAI_AND_LAC:
- if (osmo_plmn_cmp(&bts->network->plmn, &ci->id.lai_and_lac.plmn))
- return false;
- if (bts->location_area_code != ci->id.lai_and_lac.lac)
- return false;
- return true;
+ return gsm_bts_matches_lai(bts, &id->lai_and_lac);
case CELL_IDENT_LAC:
- if (bts->location_area_code != ci->id.lac)
- return false;
- return true;
+ return id->lac == bts->location_area_code;
case CELL_IDENT_BSS:
return true;
case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
case CELL_IDENT_UTRAN_RNC:
case CELL_IDENT_UTRAN_LAC_RNC:
- /* Not implemented */
- default:
return false;
+ default:
+ OSMO_ASSERT(false);
}
}
-struct gsm_bts *gsm_bts_by_cell_id(struct gsm_network *net, const struct gsm0808_cell_id *ci)
+/* From a list of local BTSes that match the cell_id, return the Nth one, or NULL if there is no such
+ * match. */
+struct gsm_bts *gsm_bts_by_cell_id(const struct gsm_network *net,
+ const struct gsm0808_cell_id *cell_id,
+ int match_idx)
{
struct gsm_bts *bts;
-
+ int i = 0;
llist_for_each_entry(bts, &net->bts_list, list) {
- if (gsm_bts_matches_cell_id(bts, ci))
- return bts;
+ if (!gsm_bts_matches_cell_id(bts, cell_id))
+ continue;
+ if (i < match_idx) {
+ /* this is only the i'th match, we're looking for a later one... */
+ i++;
+ continue;
+ }
+ return bts;
}
-
return NULL;
}
@@ -696,13 +701,16 @@ struct gsm_bts_trx *gsm_bts_trx_alloc(struct gsm_bts *bts)
struct gsm_bts_trx_ts *ts = &trx->ts[k];
int l;
+
ts->trx = trx;
ts->nr = k;
- ts->pchan = GSM_PCHAN_NONE;
- ts->dyn.pchan_is = GSM_PCHAN_NONE;
- ts->dyn.pchan_want = GSM_PCHAN_NONE;
+ ts->pchan_from_config = ts->pchan_on_init = ts->pchan_is = GSM_PCHAN_NONE;
ts->tsc = -1;
+ ts_fsm_alloc(ts);
+ LOGP(DRSL, LOGL_NOTICE, "gsm_bts_trx_alloc() = %p, ts[%d] %p fi=%p\n",
+ trx, k, &(trx->ts[k]), ts->fi);
+
gsm_mo_init(&ts->mo, bts, NM_OC_CHANNEL,
bts->nr, trx->nr, ts->nr);
@@ -817,7 +825,7 @@ struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, uint8_t bts_num)
talloc_free(bts);
return NULL;
}
- bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4;
+ bts->c0->ts[0].pchan_from_config = GSM_PCHAN_CCCH_SDCCH4; /* TODO: really?? */
bts->rach_b_thresh = -1;
bts->rach_ldavg_slots = -1;
@@ -863,7 +871,7 @@ struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, uint8_t bts_num)
bts->si_common.chan_desc.att = 1; /* attachment required */
bts->si_common.chan_desc.bs_pa_mfrms = RSL_BS_PA_MFRMS_5; /* paging frames */
bts->si_common.chan_desc.bs_ag_blks_res = 1; /* reserved AGCH blocks */
- bts->si_common.chan_desc.t3212 = net->t3212; /* Use network's current value */
+ bts->si_common.chan_desc.t3212 = T_def_get(net->T_defs, 3212, T_CUSTOM, -1);
gsm_bts_set_radio_link_timeout(bts, 32); /* Use RADIO LINK TIMEOUT of 32 */
INIT_LLIST_HEAD(&bts->abis_queue);
@@ -937,49 +945,30 @@ char *gsm_ts_name(const struct gsm_bts_trx_ts *ts)
/*! Log timeslot number with full pchan information */
char *gsm_ts_and_pchan_name(const struct gsm_bts_trx_ts *ts)
{
- switch (ts->pchan) {
- case GSM_PCHAN_TCH_F_TCH_H_PDCH:
- if (ts->dyn.pchan_is == ts->dyn.pchan_want)
- snprintf(ts2str, sizeof(ts2str),
- "(bts=%d,trx=%d,ts=%d,pchan=%s as %s)",
- ts->trx->bts->nr, ts->trx->nr, ts->nr,
- gsm_pchan_name(ts->pchan),
- gsm_pchan_name(ts->dyn.pchan_is));
- else
- snprintf(ts2str, sizeof(ts2str),
- "(bts=%d,trx=%d,ts=%d,pchan=%s"
- " switching %s -> %s)",
- ts->trx->bts->nr, ts->trx->nr, ts->nr,
- gsm_pchan_name(ts->pchan),
- gsm_pchan_name(ts->dyn.pchan_is),
- gsm_pchan_name(ts->dyn.pchan_want));
- break;
- case GSM_PCHAN_TCH_F_PDCH:
- if ((ts->flags & TS_F_PDCH_PENDING_MASK) == 0)
- snprintf(ts2str, sizeof(ts2str),
- "(bts=%d,trx=%d,ts=%d,pchan=%s as %s)",
- ts->trx->bts->nr, ts->trx->nr, ts->nr,
- gsm_pchan_name(ts->pchan),
- (ts->flags & TS_F_PDCH_ACTIVE)? "PDCH"
- : "TCH/F");
- else
- snprintf(ts2str, sizeof(ts2str),
- "(bts=%d,trx=%d,ts=%d,pchan=%s"
- " switching %s -> %s)",
- ts->trx->bts->nr, ts->trx->nr, ts->nr,
- gsm_pchan_name(ts->pchan),
- (ts->flags & TS_F_PDCH_ACTIVE)? "PDCH"
- : "TCH/F",
- (ts->flags & TS_F_PDCH_ACT_PENDING)? "PDCH"
- : "TCH/F");
- break;
- default:
- snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d,pchan=%s)",
+ if (!ts->fi)
+ snprintf(ts2str, sizeof(ts2str),
+ "(bts=%d,trx=%d,ts=%d,pchan_from_config=%s, not allocated)",
ts->trx->bts->nr, ts->trx->nr, ts->nr,
- gsm_pchan_name(ts->pchan));
- break;
- }
-
+ gsm_pchan_name(ts->pchan_from_config));
+ else if (ts->fi->state == TS_ST_NOT_INITIALIZED)
+ snprintf(ts2str, sizeof(ts2str),
+ "(bts=%d,trx=%d,ts=%d,pchan_from_config=%s,state=%s)",
+ ts->trx->bts->nr, ts->trx->nr, ts->nr,
+ gsm_pchan_name(ts->pchan_from_config),
+ osmo_fsm_inst_state_name(ts->fi));
+ else if (ts->pchan_is == ts->pchan_on_init)
+ snprintf(ts2str, sizeof(ts2str),
+ "(bts=%d,trx=%d,ts=%d,pchan=%s,state=%s)",
+ ts->trx->bts->nr, ts->trx->nr, ts->nr,
+ gsm_pchan_name(ts->pchan_is),
+ osmo_fsm_inst_state_name(ts->fi));
+ else
+ snprintf(ts2str, sizeof(ts2str),
+ "(bts=%d,trx=%d,ts=%d,pchan_on_init=%s,pchan=%s,state=%s)",
+ ts->trx->bts->nr, ts->trx->nr, ts->nr,
+ gsm_pchan_name(ts->pchan_on_init),
+ gsm_pchan_name(ts->pchan_is),
+ osmo_fsm_inst_state_name(ts->fi));
return ts2str;
}
@@ -1197,20 +1186,9 @@ uint8_t gsm_pchan2chan_nr(enum gsm_phys_chan_config pchan,
uint8_t gsm_lchan2chan_nr(const struct gsm_lchan *lchan)
{
- enum gsm_phys_chan_config pchan = lchan->ts->pchan;
- if (pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH)
- return gsm_lchan_as_pchan2chan_nr(lchan,
- lchan->ts->dyn.pchan_is);
- return gsm_pchan2chan_nr(lchan->ts->pchan, lchan->ts->nr, lchan->nr);
-}
-
-uint8_t gsm_lchan_as_pchan2chan_nr(const struct gsm_lchan *lchan,
- enum gsm_phys_chan_config as_pchan)
-{
- if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
- && as_pchan == GSM_PCHAN_PDCH)
- return RSL_CHAN_OSMO_PDCH | (lchan->ts->nr & ~RSL_CHAN_NR_MASK);
- return gsm_pchan2chan_nr(as_pchan, lchan->ts->nr, lchan->nr);
+ /* Note: non-standard Osmocom style dyn TS PDCH mode chan_nr is only used within
+ * rsl_tx_dyn_ts_pdch_act_deact(). */
+ return gsm_pchan2chan_nr(lchan->ts->pchan_is, lchan->ts->nr, lchan->nr);
}
/* return the gsm_lchan for the CBCH (if it exists at all) */
@@ -1219,12 +1197,12 @@ struct gsm_lchan *gsm_bts_get_cbch(struct gsm_bts *bts)
struct gsm_lchan *lchan = NULL;
struct gsm_bts_trx *trx = bts->c0;
- if (trx->ts[0].pchan == GSM_PCHAN_CCCH_SDCCH4_CBCH)
+ if (trx->ts[0].pchan_is == GSM_PCHAN_CCCH_SDCCH4_CBCH)
lchan = &trx->ts[0].lchan[2];
else {
int i;
for (i = 0; i < 8; i++) {
- if (trx->ts[i].pchan == GSM_PCHAN_SDCCH8_SACCH8C_CBCH) {
+ if (trx->ts[i].pchan_is == GSM_PCHAN_SDCCH8_SACCH8C_CBCH) {
lchan = &trx->ts[i].lchan[2];
break;
}
@@ -1242,48 +1220,31 @@ struct gsm_lchan *rsl_lchan_lookup(struct gsm_bts_trx *trx, uint8_t chan_nr,
uint8_t cbits = chan_nr >> 3;
uint8_t lch_idx;
struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
- bool ok = true;
+ bool ok;
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
- && !(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
- && (ts->dyn.pchan_is == GSM_PCHAN_TCH_F
- || ts->dyn.pchan_want == GSM_PCHAN_TCH_F)))
- ok = false;
+ ok = ts_is_capable_of_pchan(ts, GSM_PCHAN_TCH_F)
+ || ts->pchan_on_init == GSM_PCHAN_PDCH; /* PDCH? really? */
} else if ((cbits & 0x1e) == 0x02) {
lch_idx = cbits & 0x1; /* TCH/H */
- if (ts->pchan != GSM_PCHAN_TCH_H
- && !(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH
- && (ts->dyn.pchan_is == GSM_PCHAN_TCH_H
- || ts->dyn.pchan_want == GSM_PCHAN_TCH_H)))
- ok = false;
+ ok = ts_is_capable_of_pchan(ts, GSM_PCHAN_TCH_H);
} 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;
+ ok = ts_is_capable_of_pchan(ts, GSM_PCHAN_CCCH_SDCCH4);
} 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;
+ ok = ts_is_capable_of_pchan(ts, GSM_PCHAN_SDCCH8_SACCH8C);
} 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;
+ lch_idx = 0; /* CCCH? */
+ ok = ts_is_capable_of_pchan(ts, GSM_PCHAN_CCCH);
/* FIXME: we should not return first sdcch4 !!! */
} else if ((chan_nr & RSL_CHAN_NR_MASK) == RSL_CHAN_OSMO_PDCH) {
lch_idx = 0;
- if (ts->pchan != GSM_PCHAN_TCH_F_TCH_H_PDCH)
- ok = false;
+ ok = (ts->pchan_on_init == GSM_PCHAN_TCH_F_TCH_H_PDCH);
} else
return NULL;
@@ -1303,33 +1264,18 @@ static const uint8_t subslots_per_pchan[] = {
[GSM_PCHAN_SDCCH8_SACCH8C] = 8,
[GSM_PCHAN_CCCH_SDCCH4_CBCH] = 4,
[GSM_PCHAN_SDCCH8_SACCH8C_CBCH] = 8,
- /*
- * GSM_PCHAN_TCH_F_PDCH and GSM_PCHAN_TCH_F_TCH_H_PDCH should not be
- * part of this, those TS are handled according to their dynamic state.
- */
+ /* Dyn TS: maximum allowed subslots */
+ [GSM_PCHAN_TCH_F_TCH_H_PDCH] = 2,
+ [GSM_PCHAN_TCH_F_PDCH] = 1,
};
-/*! Return the actual pchan type, also heeding dynamic TS. */
-enum gsm_phys_chan_config ts_pchan(struct gsm_bts_trx_ts *ts)
-{
- switch (ts->pchan) {
- case GSM_PCHAN_TCH_F_TCH_H_PDCH:
- return ts->dyn.pchan_is;
- case GSM_PCHAN_TCH_F_PDCH:
- if (ts->flags & TS_F_PDCH_ACTIVE)
- return GSM_PCHAN_PDCH;
- else
- return GSM_PCHAN_TCH_F;
- default:
- return ts->pchan;
- }
-}
-
/*! According to ts->pchan and possibly ts->dyn_pchan, return the number of
* logical channels available in the timeslot. */
-uint8_t ts_subslots(struct gsm_bts_trx_ts *ts)
+uint8_t pchan_subslots(enum gsm_phys_chan_config pchan)
{
- return subslots_per_pchan[ts_pchan(ts)];
+ if (pchan < 0 || pchan >= ARRAY_SIZE(subslots_per_pchan))
+ return 0;
+ return subslots_per_pchan[pchan];
}
static bool pchan_is_tch(enum gsm_phys_chan_config pchan)
@@ -1345,7 +1291,7 @@ static bool pchan_is_tch(enum gsm_phys_chan_config pchan)
bool ts_is_tch(struct gsm_bts_trx_ts *ts)
{
- return pchan_is_tch(ts_pchan(ts));
+ return pchan_is_tch(ts->pchan_is);
}
bool trx_is_usable(const struct gsm_bts_trx *trx)
@@ -1360,34 +1306,20 @@ bool trx_is_usable(const struct gsm_bts_trx *trx)
return true;
}
-void gsm_trx_mark_all_ts_uninitialized(struct gsm_bts_trx *trx)
+void gsm_trx_all_ts_dispatch(struct gsm_bts_trx *trx, uint32_t ts_ev, void *data)
{
int i;
for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
struct gsm_bts_trx_ts *ts = &trx->ts[i];
- ts->initialized = false;
+ osmo_fsm_inst_dispatch(ts->fi, ts_ev, data);
}
}
-void gsm_bts_mark_all_ts_uninitialized(struct gsm_bts *bts)
+void gsm_bts_all_ts_dispatch(struct gsm_bts *bts, uint32_t ts_ev, void *data)
{
struct gsm_bts_trx *trx;
llist_for_each_entry(trx, &bts->trx_list, list)
- gsm_trx_mark_all_ts_uninitialized(trx);
-}
-
-/* Trigger initial timeslot actions iff both OML and RSL are setup. */
-void gsm_ts_check_init(struct gsm_bts_trx_ts *ts)
-{
- struct gsm_bts *bts = ts->trx->bts;
- if (bts->model->oml_is_ts_ready
- && !bts->model->oml_is_ts_ready(ts))
- return;
- if (!ts->trx->rsl_link)
- return;
- if (ts->initialized)
- return;
- ts->initialized = on_gsm_ts_init(ts);
+ gsm_trx_all_ts_dispatch(trx, ts_ev, data);
}
void gsm48_lchan2chan_desc(struct gsm48_chan_desc *cd,
@@ -1416,3 +1348,359 @@ bool nm_is_running(const struct gsm_nm_state *s) {
(s->availability == 0xff)
);
}
+
+/* determine the logical channel type based on the physical channel type */
+int gsm_lchan_type_by_pchan(enum gsm_phys_chan_config pchan)
+{
+ switch (pchan) {
+ case GSM_PCHAN_TCH_F:
+ return GSM_LCHAN_TCH_F;
+ case GSM_PCHAN_TCH_H:
+ return GSM_LCHAN_TCH_H;
+ case GSM_PCHAN_SDCCH8_SACCH8C:
+ case GSM_PCHAN_SDCCH8_SACCH8C_CBCH:
+ case GSM_PCHAN_CCCH_SDCCH4:
+ case GSM_PCHAN_CCCH_SDCCH4_CBCH:
+ return GSM_LCHAN_SDCCH;
+ default:
+ return -1;
+ }
+}
+
+enum gsm_phys_chan_config gsm_pchan_by_lchan_type(enum gsm_chan_t type)
+{
+ switch (type) {
+ case GSM_LCHAN_TCH_F:
+ return GSM_PCHAN_TCH_F;
+ case GSM_LCHAN_TCH_H:
+ return GSM_PCHAN_TCH_H;
+ case GSM_LCHAN_NONE:
+ case GSM_LCHAN_PDTCH:
+ /* TODO: so far lchan->type is NONE in PDCH mode. PDTCH is only
+ * used in osmo-bts. Maybe set PDTCH and drop the NONE case
+ * here. */
+ return GSM_PCHAN_PDCH;
+ default:
+ return GSM_PCHAN_UNKNOWN;
+ }
+}
+
+/* Can the timeslot in principle be used as this PCHAN kind? */
+bool ts_is_capable_of_pchan(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config pchan)
+{
+ switch (ts->pchan_on_init) {
+ case GSM_PCHAN_TCH_F_PDCH:
+ switch (pchan) {
+ case GSM_PCHAN_TCH_F:
+ case GSM_PCHAN_PDCH:
+ return true;
+ default:
+ return false;
+ }
+
+ case GSM_PCHAN_TCH_F_TCH_H_PDCH:
+ switch (pchan) {
+ case GSM_PCHAN_TCH_F:
+ case GSM_PCHAN_TCH_H:
+ case GSM_PCHAN_PDCH:
+ return true;
+ default:
+ return false;
+ }
+
+ case GSM_PCHAN_CCCH_SDCCH4_CBCH:
+ switch (pchan) {
+ case GSM_PCHAN_CCCH_SDCCH4_CBCH:
+ case GSM_PCHAN_CCCH_SDCCH4:
+ case GSM_PCHAN_CCCH:
+ return true;
+ default:
+ return false;
+ }
+
+ case GSM_PCHAN_CCCH_SDCCH4:
+ switch (pchan) {
+ case GSM_PCHAN_CCCH_SDCCH4:
+ case GSM_PCHAN_CCCH:
+ return true;
+ default:
+ return false;
+ }
+
+ case GSM_PCHAN_SDCCH8_SACCH8C_CBCH:
+ switch (pchan) {
+ case GSM_PCHAN_SDCCH8_SACCH8C_CBCH:
+ case GSM_PCHAN_SDCCH8_SACCH8C:
+ return true;
+ default:
+ return false;
+ }
+
+ default:
+ return ts->pchan_on_init == pchan;
+ }
+}
+
+static int trx_count_free_ts(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan)
+{
+ struct gsm_bts_trx_ts *ts;
+ struct gsm_lchan *lchan;
+ int j;
+ int count = 0;
+
+ if (!trx_is_usable(trx))
+ return 0;
+
+ for (j = 0; j < ARRAY_SIZE(trx->ts); j++) {
+ ts = &trx->ts[j];
+ if (!ts_is_usable(ts))
+ continue;
+
+ if (ts->pchan_is == GSM_PCHAN_PDCH) {
+ /* Dynamic timeslots in PDCH mode will become TCH if needed. */
+ switch (ts->pchan_on_init) {
+ case GSM_PCHAN_TCH_F_PDCH:
+ if (pchan == GSM_PCHAN_TCH_F)
+ count++;
+ continue;
+
+ case GSM_PCHAN_TCH_F_TCH_H_PDCH:
+ if (pchan == GSM_PCHAN_TCH_F)
+ count++;
+ else if (pchan == GSM_PCHAN_TCH_H)
+ count += 2;
+ continue;
+
+ default:
+ /* Not dynamic, not applicable. */
+ continue;
+ }
+ }
+
+ if (ts->pchan_is != pchan)
+ continue;
+
+ ts_for_each_lchan(lchan, ts) {
+ if (lchan_state_is(lchan, LCHAN_ST_UNUSED))
+ count++;
+ }
+ }
+
+ return count;
+}
+
+/* Count number of free TS of given pchan type */
+int bts_count_free_ts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
+{
+ struct gsm_bts_trx *trx;
+ int count = 0;
+
+ llist_for_each_entry(trx, &bts->trx_list, list)
+ count += trx_count_free_ts(trx, pchan);
+
+ return count;
+}
+
+bool ts_is_usable(const struct gsm_bts_trx_ts *ts)
+{
+ if (!trx_is_usable(ts->trx)) {
+ LOGP(DRLL, LOGL_DEBUG, "%s not usable\n", gsm_trx_name(ts->trx));
+ return false;
+ }
+
+ if (!ts->fi)
+ return false;
+
+ switch (ts->fi->state) {
+ case TS_ST_NOT_INITIALIZED:
+ case TS_ST_BORKEN:
+ return false;
+ default:
+ break;
+ }
+
+ return true;
+}
+
+const struct value_string lchan_activate_mode_names[] = {
+ OSMO_VALUE_STRING(FOR_NONE),
+ OSMO_VALUE_STRING(FOR_MS_CHANNEL_REQUEST),
+ OSMO_VALUE_STRING(FOR_ASSIGNMENT),
+ OSMO_VALUE_STRING(FOR_HANDOVER),
+ OSMO_VALUE_STRING(FOR_VTY),
+ {}
+};
+
+/* Helper function for bsc_match_codec_pref(), looks up a matching permitted speech
+ * value for a given msc audio codec pref */
+static enum gsm0808_permitted_speech audio_support_to_gsm88(const struct gsm_audio_support *audio)
+{
+ if (audio->hr) {
+ switch (audio->ver) {
+ case 1:
+ return GSM0808_PERM_HR1;
+ break;
+ case 2:
+ return GSM0808_PERM_HR2;
+ break;
+ case 3:
+ return GSM0808_PERM_HR3;
+ break;
+ default:
+ LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: hr%d, using hr1 instead\n",
+ audio->ver);
+ return GSM0808_PERM_HR1;
+ }
+ } else {
+ switch (audio->ver) {
+ case 1:
+ return GSM0808_PERM_FR1;
+ break;
+ case 2:
+ return GSM0808_PERM_FR2;
+ break;
+ case 3:
+ return GSM0808_PERM_FR3;
+ break;
+ default:
+ LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: fr%d, using fr1 instead\n",
+ audio->ver);
+ return GSM0808_PERM_FR1;
+ }
+ }
+}
+
+/* Helper function for bsc_match_codec_pref(), looks up a matching chan mode for
+ * a given permitted speech value */
+static enum gsm48_chan_mode gsm88_to_chan_mode(enum gsm0808_permitted_speech speech)
+{
+ switch (speech) {
+ case GSM0808_PERM_HR1:
+ case GSM0808_PERM_FR1:
+ return GSM48_CMODE_SPEECH_V1;
+ break;
+ case GSM0808_PERM_HR2:
+ case GSM0808_PERM_FR2:
+ return GSM48_CMODE_SPEECH_EFR;
+ break;
+ case GSM0808_PERM_HR3:
+ case GSM0808_PERM_FR3:
+ return GSM48_CMODE_SPEECH_AMR;
+ break;
+ default:
+ LOGP(DMSC, LOGL_FATAL,
+ "Unsupported permitted speech selected, assuming AMR as channel mode...\n");
+ return GSM48_CMODE_SPEECH_AMR;
+ }
+}
+
+/* Helper function for bsc_match_codec_pref(), tests if a given audio support
+ * matches one of the permitted speech settings of the channel type element.
+ * The matched permitted speech value is then also compared against the
+ * speech codec list. (optional, only relevant for AoIP) */
+static bool test_codec_pref(const struct gsm0808_channel_type *ct,
+ const struct gsm0808_speech_codec_list *scl,
+ uint8_t perm_spch)
+{
+ unsigned int i;
+ bool match = false;
+ struct gsm0808_speech_codec sc;
+ int rc;
+
+ /* Try to finde the given permitted speech value in the
+ * codec list of the channel type element */
+ for (i = 0; i < ct->perm_spch_len; i++) {
+ if (ct->perm_spch[i] == perm_spch) {
+ match = true;
+ break;
+ }
+ }
+
+ /* If we do not have a speech codec list to test against,
+ * we just exit early (will be always the case in non-AoIP networks) */
+ if (!scl || !scl->len)
+ return match;
+
+ /* If we failed to match until here, there is no
+ * point in testing further */
+ if (match == false)
+ return false;
+
+ /* Extrapolate speech codec data */
+ rc = gsm0808_speech_codec_from_chan_type(&sc, perm_spch);
+ if (rc < 0)
+ return false;
+
+ /* Try to find extrapolated speech codec data in
+ * the speech codec list */
+ for (i = 0; i < scl->len; i++) {
+ if (sc.type == scl->codec[i].type)
+ return true;
+ }
+
+ return false;
+}
+
+/*! Match the codec preferences from local config with a received codec preferences IEs received from the
+ * MSC.
+ * \param[out] chan_mode GSM 04.08 channel mode.
+ * \param[out] full_rate true iff full-rate.
+ * \param[in] ct GSM 08.08 channel type received from MSC.
+ * \param[in] scl GSM 08.08 speech codec list received from MSC (optional).
+ * \param[in] audio_support List of allowed codecs as from local config.
+ * \param[in] audio_length Number of items in audio_support.
+ * \returns 0 on success, -1 in case no match was found */
+int bsc_match_codec_pref(enum gsm48_chan_mode *chan_mode,
+ bool *full_rate,
+ const struct gsm0808_channel_type *ct,
+ const struct gsm0808_speech_codec_list *scl,
+ struct gsm_audio_support * const *audio_support,
+ int audio_length)
+{
+ unsigned int i;
+ uint8_t perm_spch;
+ bool match = false;
+
+ for (i = 0; i < audio_length; i++) {
+ perm_spch = audio_support_to_gsm88(audio_support[i]);
+ if (test_codec_pref(ct, scl, perm_spch)) {
+ match = true;
+ break;
+ }
+ }
+
+ /* Exit without result, in case no match can be deteched */
+ if (!match) {
+ *full_rate = false;
+ *chan_mode = GSM48_CMODE_SIGN;
+ return -1;
+ }
+
+ /* Check if the result is a half or full rate codec */
+ switch (perm_spch) {
+ case GSM0808_PERM_HR1:
+ case GSM0808_PERM_HR2:
+ case GSM0808_PERM_HR3:
+ case GSM0808_PERM_HR4:
+ case GSM0808_PERM_HR6:
+ *full_rate = false;
+ break;
+
+ case GSM0808_PERM_FR1:
+ case GSM0808_PERM_FR2:
+ case GSM0808_PERM_FR3:
+ case GSM0808_PERM_FR4:
+ case GSM0808_PERM_FR5:
+ *full_rate = true;
+ break;
+
+ default:
+ LOGP(DMSC, LOGL_ERROR, "Invalid permitted-speech value: %u\n", perm_spch);
+ return -EINVAL;
+ }
+
+ /* Lookup a channel mode for the selected codec */
+ *chan_mode = gsm88_to_chan_mode(perm_spch);
+
+ return 0;
+}
diff --git a/src/osmo-bsc/gsm_timers.c b/src/osmo-bsc/gsm_timers.c
new file mode 100644
index 000000000..2f737934c
--- /dev/null
+++ b/src/osmo-bsc/gsm_timers.c
@@ -0,0 +1,206 @@
+/* Implementation to define Tnnn timers globally and use for FSM state changes. */
+/* (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ *
+ * Author: Neels Hofmeyr <neels@hofmeyr.de>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/core/fsm.h>
+
+#include <osmocom/bsc/gsm_timers.h>
+
+/* a = return_val * b. Return 0 if factor is below 1. */
+static int T_factor(enum T_unit a, enum T_unit b)
+{
+ if (b == a
+ || b == T_CUSTOM || a == T_CUSTOM)
+ return 1;
+
+ switch (b) {
+ case T_MS:
+ switch (a) {
+ case T_S:
+ return 1000;
+ case T_M:
+ return 60*1000;
+ default:
+ return 0;
+ }
+ case T_S:
+ switch (a) {
+ case T_M:
+ return 60;
+ default:
+ return 0;
+ }
+ default:
+ return 0;
+ }
+}
+
+static int T_round(int val, enum T_unit from_unit, enum T_unit to_unit)
+{
+ int f;
+ if (!val)
+ return 0;
+
+ f = T_factor(from_unit, to_unit);
+ if (f < 1) {
+ f = T_factor(to_unit, from_unit);
+ return (val / f) + (val % f? 1 : 0);
+ }
+ return val * f;
+}
+
+/* Return the value of a T timer from a list of T_defs.
+ * Any value is rounded up to match as_unit: 1100 ms as T_S becomes 2 seconds, as T_M becomes one minute.
+ * If no such timer is defined, return the default value passed, or abort the program if default < 0.
+ *
+ * Usage examples:
+ *
+ * - Initialization:
+ *
+ * struct T_def global_T_defs[] = {
+ * { .T=7, .default_val=60, .desc="Water Boiling Timeout" }, // default is .unit=T_S == 0
+ * { .T=8, .default_val=300, .desc="Tea brewing" },
+ * { .T=9, .default_val=5, .unit=T_M, .desc="Let tea cool down before drinking" },
+ * { .T=10, .default_val=20, .unit=T_M, .desc="Forgot to drink tea while it's warm" },
+ * {} // <-- important! last entry shall be zero
+ * };
+ * T_defs_reset(global_T_defs); // make all values the default
+ * T_defs_vty_init(global_T_defs, CONFIG_NODE);
+ *
+ * val = T_def_get(global_T_defs, 7, T_S, -1); // -> 10
+ * sleep(val);
+ *
+ * val = T_def_get(global_T_defs, 7, T_M, -1); // 10 seconds becomes 1 minute -> 1
+ * sleep_minutes(val);
+ *
+ * val = T_def_get(global_T_defs, 99, T_S, -1); // not defined, program aborts!
+ *
+ * val = T_def_get(global_T_defs, 99, T_S, 3); // not defined, returns 3
+ */
+int T_def_get(struct T_def *T_defs, int T, enum T_unit as_unit, int val_if_not_present)
+{
+ struct T_def *d = T_def_get_entry(T_defs, T);
+ if (!d) {
+ OSMO_ASSERT(val_if_not_present >= 0);
+ return val_if_not_present;
+ }
+ return T_round(d->val, d->unit, as_unit);
+}
+
+/* Set all T_def values to the default_val. */
+void T_defs_reset(struct T_def *T_defs)
+{
+ struct T_def *d;
+ for_each_T_def(d, T_defs)
+ d->val = d->default_val;
+}
+
+/* Return a pointer to a T_def from an array, or NULL. */
+struct T_def *T_def_get_entry(struct T_def *T_defs, int T)
+{
+ struct T_def *d;
+ for_each_T_def(d, T_defs) {
+ if (d->T == T)
+ return d;
+ }
+ return NULL;
+}
+
+/* Return a state_timeout entry from an array, or return NULL if the entry is zero.
+ *
+ * The timeouts_array shall contain exactly 32 elements, which corresponds to the number of states
+ * allowed by osmo_fsm_*. Lookup is by array index.
+ *
+ * For example:
+ * struct state_timeout my_fsm_timeouts[32] = {
+ * [MY_FSM_STATE_3] = { .T = 423 },
+ * [MY_FSM_STATE_7] = { .T = 235 },
+ * [MY_FSM_STATE_8] = { .keep_timer = true },
+ * // any state that is omitted will remain zero == no timeout
+ * };
+ * get_state_timeout(MY_FSM_STATE_0, &my_fsm_timeouts) -> NULL,
+ * get_state_timeout(MY_FSM_STATE_7, &my_fsm_timeouts) -> { .T = 235 }
+ *
+ * The intention is then to obtain the timer like T_def_get(global_T_defs, T=235); see also
+ * fsm_inst_state_chg_T() below.
+ */
+struct state_timeout *get_state_timeout(uint32_t state, struct state_timeout *timeouts_array)
+{
+ struct state_timeout *t;
+ OSMO_ASSERT(state < 32);
+ t = &timeouts_array[state];
+ if (!t->keep_timer && !t->T)
+ return NULL;
+ return t;
+}
+
+/* Call osmo_fsm_inst_state_chg() or osmo_fsm_inst_state_chg_keep_timer(), depending on the T value
+ * defined for this state in the timeouts_array, and obtaining the actual timeout value from T_defs.
+ * A T timer configured in sub-second precision is rounded up to the next full second.
+ *
+ * See get_state_timeout() and T_def_get().
+ *
+ * Should a T number be defined in timeouts_array that is not defined in T_defs, use default_timeout.
+ * This is best used by wrapping this function call in a macro suitable for a specific FSM
+ * implementation, which can become as short as: my_fsm_state_chg(fi, NEXT_STATE):
+ *
+ * #define my_fsm_state_chg(fi, NEXT_STATE) \
+ * fsm_inst_state_chg_T(fi, NEXT_STATE, my_fsm_timeouts, global_T_defs, 5)
+ *
+ * my_fsm_state_chg(fi, MY_FSM_STATE_1);
+ * // -> No timeout configured, will enter state without timeout.
+ *
+ * my_fsm_state_chg(fi, MY_FSM_STATE_3);
+ * // T423 configured for this state, will look up T423 in T_defs, or use 5 seconds if unset.
+ *
+ * my_fsm_state_chg(fi, MY_FSM_STATE_8);
+ * // keep_timer configured for this state, will invoke osmo_fsm_inst_state_chg_keep_timer().
+ *
+ */
+int _fsm_inst_state_chg_T(struct osmo_fsm_inst *fi, uint32_t state,
+ struct state_timeout *timeouts_array,
+ struct T_def *T_defs, int default_timeout,
+ const char *file, int line)
+{
+ struct state_timeout *t = get_state_timeout(state, timeouts_array);
+ int val;
+
+ /* No timeout defined for this state? */
+ if (!t)
+ return _osmo_fsm_inst_state_chg(fi, state, 0, 0, file, line);
+
+ if (t->keep_timer) {
+ int rc = _osmo_fsm_inst_state_chg_keep_timer(fi, state, file, line);
+ if (t->T && !rc)
+ fi->T = t->T;
+ return rc;
+ }
+
+ val = T_def_get(T_defs, t->T, T_S, default_timeout);
+ return _osmo_fsm_inst_state_chg(fi, state, val, t->T, file, line);
+}
+
+const struct value_string T_unit_names[] = {
+ { T_S, "s" },
+ { T_MS, "ms" },
+ { T_CUSTOM, "(custom)" },
+ { 0, NULL }
+};
diff --git a/src/osmo-bsc/gsm_timers_vty.c b/src/osmo-bsc/gsm_timers_vty.c
new file mode 100644
index 000000000..8a13259ff
--- /dev/null
+++ b/src/osmo-bsc/gsm_timers_vty.c
@@ -0,0 +1,117 @@
+/* Implementation to configure Tnnn timers in VTY */
+/* (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ *
+ * Author: Neels Hofmeyr <neels@hofmeyr.de>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <string.h>
+
+#include <osmocom/vty/vty.h>
+#include <osmocom/vty/command.h>
+
+#include <osmocom/bsc/gsm_timers.h>
+
+/* Global singleton list used for the VTY configuration. See T_defs_vty_init(). */
+static struct T_def *g_vty_T_defs = NULL;
+
+/* Parse an argument like "T1234", "t1234" or "1234" and return the corresponding T_def entry from
+ * g_vty_T_defs, if any. */
+static struct T_def *parse_T_arg(struct vty *vty, const char *T_str)
+{
+ int T;
+ struct T_def *d;
+
+ if (T_str[0] == 't' || T_str[0] == 'T')
+ T_str++;
+ T = atoi(T_str);
+
+ d = T_def_get_entry(g_vty_T_defs, T);
+ if (!d)
+ vty_out(vty, "No such timer: T%d%s", T, VTY_NEWLINE);
+ return d;
+}
+
+/* Installed in the VTY on T_defs_vty_init(). */
+DEFUN(cfg_timer, cfg_timer_cmd,
+ "timer TNNNN (default|<1-65535>)",
+ "Configure GSM Timers\n"
+ "T-number, optionally preceded by 't' or 'T'."
+ "See also 'show timer' for a list of available timers.\n"
+ "Set to default timer value\n" "Timer value\n")
+{
+ const char *val_str = argv[1];
+ struct T_def *d;
+
+ d = parse_T_arg(vty, argv[0]);
+ if (!d)
+ return CMD_WARNING;
+
+ if (!strcmp(val_str, "default"))
+ d->val = d->default_val;
+ else
+ d->val = atoi(val_str);
+ vty_out(vty, "T%d = %u %s (%s)%s", d->T, d->val, T_unit_name(d->unit), d->desc, VTY_NEWLINE);
+ return CMD_SUCCESS;
+}
+
+/* Print a T_def to the VTY. */
+static void show_one_timer(struct vty *vty, struct T_def *d)
+{
+ vty_out(vty, "T%d = %u %s (default = %u %s) \t%s%s",
+ d->T, d->val, T_unit_name(d->unit),
+ d->default_val, T_unit_name(d->unit), d->desc, VTY_NEWLINE);
+}
+
+/* Installed in the VTY on T_defs_vty_init(). */
+DEFUN(show_timer, show_timer_cmd,
+ "show timer [TNNNN]",
+ SHOW_STR "GSM Timers\n"
+ "Specific timer to show, or all timers if omitted.\n")
+{
+ struct T_def *d;
+
+ if (argc) {
+ d = parse_T_arg(vty, argv[0]);
+ if (!d)
+ return CMD_WARNING;
+ show_one_timer(vty, d);
+ return CMD_SUCCESS;
+ }
+
+ for_each_T_def(d, g_vty_T_defs)
+ show_one_timer(vty, d);
+ return CMD_SUCCESS;
+}
+
+/* Install GSM timer configuration commands in the VTY. */
+void T_defs_vty_init(struct T_def *T_defs, int cfg_parent_node)
+{
+ install_element_ve(&show_timer_cmd);
+ install_element(cfg_parent_node, &cfg_timer_cmd);
+}
+
+/* Write GSM timer configuration to the vty. */
+void T_defs_vty_write(struct vty *vty, const char *indent)
+{
+ struct T_def *d;
+ for_each_T_def(d, g_vty_T_defs) {
+ if (d->val != d->default_val)
+ vty_out(vty, "%stimer t%d %u%s", indent, d->T, d->val, VTY_NEWLINE);
+ }
+}
diff --git a/src/osmo-bsc/handover_decision.c b/src/osmo-bsc/handover_decision.c
index 887c2993f..072ba8cc9 100644
--- a/src/osmo-bsc/handover_decision.c
+++ b/src/osmo-bsc/handover_decision.c
@@ -32,47 +32,9 @@
#include <osmocom/core/talloc.h>
#include <osmocom/gsm/gsm_utils.h>
-#include <osmocom/bsc/handover.h>
+#include <osmocom/bsc/handover_fsm.h>
#include <osmocom/bsc/handover_cfg.h>
-/* Find BTS by ARFCN and BSIC */
-struct gsm_bts *bts_by_arfcn_bsic(const struct gsm_network *net,
- uint16_t arfcn, uint8_t bsic)
-{
- struct gsm_bts *bts;
-
- llist_for_each_entry(bts, &net->bts_list, list) {
- if (bts->c0->arfcn == arfcn &&
- bts->bsic == bsic)
- return bts;
- }
-
- return NULL;
-}
-
-
-/* issue handover to a cell identified by ARFCN and BSIC */
-static int handover_to_arfcn_bsic(struct gsm_lchan *lchan,
- uint16_t arfcn, uint8_t bsic)
-{
- struct gsm_bts *new_bts;
-
- /* resolve the gsm_bts structure for the best neighbor */
- /* FIXME: use some better heuristics here to determine which cell
- * using this ARFCN really is closest to the target cell. For
- * now we simply assume that each ARFCN will only be used by one
- * cell */
- new_bts = bts_by_arfcn_bsic(lchan->ts->trx->bts->network, arfcn, bsic);
- if (!new_bts) {
- LOGP(DHODEC, LOGL_NOTICE, "unable to determine neighbor BTS "
- "for ARFCN %u BSIC %u ?!?\n", arfcn, bsic);
- return -EINVAL;
- }
-
- /* and actually try to handover to that cell */
- return bsc_handover_start(HODEC1, lchan, new_bts, lchan->type);
-}
-
/* did we get a RXLEV for a given cell in the given report? */
static int rxlev_for_cell_in_rep(struct gsm_meas_rep *mr,
uint16_t arfcn, uint8_t bsic)
@@ -196,12 +158,12 @@ static void process_meas_neigh(struct gsm_meas_rep *mr)
}
/* attempt to do a handover */
-static int attempt_handover(struct gsm_meas_rep *mr)
+static void attempt_handover(struct gsm_meas_rep *mr)
{
struct gsm_bts *bts = mr->lchan->ts->trx->bts;
struct neigh_meas_proc *best_cell = NULL;
unsigned int best_better_db = 0;
- int i, rc;
+ int i;
/* find the best cell in this report that is at least RXLEV_HYST
* better than the current serving cell */
@@ -229,30 +191,29 @@ static int attempt_handover(struct gsm_meas_rep *mr)
}
if (!best_cell)
- return 0;
+ return;
LOGP(DHODEC, LOGL_INFO, "%s: Cell on ARFCN %u is better: ",
gsm_ts_name(mr->lchan->ts), best_cell->arfcn);
if (!ho_get_ho_active(bts->ho)) {
LOGPC(DHODEC, LOGL_INFO, "Skipping, Handover disabled\n");
- return 0;
+ return;
}
- rc = handover_to_arfcn_bsic(mr->lchan, best_cell->arfcn, best_cell->bsic);
- switch (rc) {
- case 0:
- LOGPC(DHODEC, LOGL_INFO, "Starting handover: meas report number %d \n", mr->nr);
- break;
- case -ENOSPC:
- LOGPC(DHODEC, LOGL_INFO, "No channel available\n");
- break;
- case -EBUSY:
- LOGPC(DHODEC, LOGL_INFO, "Handover already active\n");
- break;
- default:
- LOGPC(DHODEC, LOGL_ERROR, "Unknown error\n");
+ {
+ struct handover_mo_req req = {
+ .from_hodec_id = HODEC1,
+ .old_lchan = mr->lchan,
+ .target_nik = {
+ .from_bts = bts->nr,
+ .arfcn = best_cell->arfcn,
+ .bsic_kind = BSIC_6BIT,
+ .bsic = best_cell->bsic,
+ },
+ };
+
+ handover_start(&req);
}
- return rc;
}
/* process an already parsed measurement report and decide if we want to
diff --git a/src/osmo-bsc/handover_decision_2.c b/src/osmo-bsc/handover_decision_2.c
index 7ac54df95..ff1b20898 100644
--- a/src/osmo-bsc/handover_decision_2.c
+++ b/src/osmo-bsc/handover_decision_2.c
@@ -27,35 +27,41 @@
#include <osmocom/bsc/debug.h>
#include <osmocom/bsc/gsm_data.h>
-#include <osmocom/bsc/handover.h>
+#include <osmocom/bsc/handover_fsm.h>
#include <osmocom/bsc/handover_decision.h>
#include <osmocom/bsc/handover_decision_2.h>
#include <osmocom/bsc/handover_cfg.h>
#include <osmocom/bsc/bsc_subscriber.h>
-#include <osmocom/bsc/chan_alloc.h>
+#include <osmocom/bsc/lchan_fsm.h>
#include <osmocom/bsc/signal.h>
#include <osmocom/bsc/penalty_timers.h>
+#include <osmocom/bsc/neighbor_ident.h>
+#include <osmocom/bsc/timeslot_fsm.h>
#define LOGPHOBTS(bts, level, fmt, args...) \
LOGP(DHODEC, level, "(BTS %u) " fmt, bts->nr, ## args)
#define LOGPHOLCHAN(lchan, level, fmt, args...) \
- LOGP(DHODEC, level, "(lchan %u.%u%u%u %s) (subscr %s) " fmt, \
+ LOGP(DHODEC, level, "(lchan %u.%u%u%u %s%s%s) (subscr %s) " fmt, \
lchan->ts->trx->bts->nr, \
lchan->ts->trx->nr, \
lchan->ts->nr, \
lchan->nr, \
- gsm_pchan_name(lchan->ts->pchan), \
+ gsm_pchan_name(lchan->ts->pchan_on_init), \
+ lchan->ts->pchan_on_init == lchan->ts->pchan_is? "" : " as ", \
+ lchan->ts->pchan_on_init == lchan->ts->pchan_is? "" : gsm_pchan_name(lchan->ts->pchan_is), \
bsc_subscr_name(lchan->conn? lchan->conn->bsub : NULL), \
## args)
#define LOGPHOLCHANTOBTS(lchan, new_bts, level, fmt, args...) \
- LOGP(DHODEC, level, "(lchan %u.%u%u%u %s)->(BTS %u) (subscr %s) " fmt, \
+ LOGP(DHODEC, level, "(lchan %u.%u%u%u %s%s%s)->(BTS %u) (subscr %s) " fmt, \
lchan->ts->trx->bts->nr, \
lchan->ts->trx->nr, \
lchan->ts->nr, \
lchan->nr, \
- gsm_pchan_name(lchan->ts->pchan), \
+ gsm_pchan_name(lchan->ts->pchan_on_init), \
+ lchan->ts->pchan_on_init == lchan->ts->pchan_is? "" : " as ", \
+ lchan->ts->pchan_on_init == lchan->ts->pchan_is? "" : gsm_pchan_name(lchan->ts->pchan_is), \
new_bts->nr, \
bsc_subscr_name(lchan->conn? lchan->conn->bsub : NULL), \
## args)
@@ -74,7 +80,8 @@
struct ho_candidate {
struct gsm_lchan *lchan; /* candidate for whom */
- struct gsm_bts *bts; /* target BTS */
+ struct gsm_bts *bts; /* target BTS in local BSS */
+ struct gsm0808_cell_id_list2 *cil; /* target cells in remote BSS */
uint8_t requirements; /* what is fulfilled */
int avg; /* average RX level */
};
@@ -147,22 +154,47 @@ void hodec2_on_change_congestion_check_interval(struct gsm_network *net, unsigne
reinit_congestion_timer(net);
}
-static void conn_penalty_time_add(struct gsm_subscriber_connection *conn, struct gsm_bts *bts,
+static void _conn_penalty_time_add(struct gsm_subscriber_connection *conn,
+ const void *for_object,
int penalty_time)
{
+ if (!for_object) {
+ LOGP(DHODEC, LOGL_ERROR, "%s Unable to set Handover-2 penalty timer:"
+ " no target cell pointer\n",
+ bsc_subscr_name(conn->bsub));
+ return;
+ }
+
if (!conn->hodec2.penalty_timers) {
conn->hodec2.penalty_timers = penalty_timers_init(conn);
OSMO_ASSERT(conn->hodec2.penalty_timers);
}
- penalty_timers_add(conn->hodec2.penalty_timers, bts, penalty_time);
+
+ penalty_timers_add(conn->hodec2.penalty_timers, for_object, penalty_time);
+}
+
+static void nik_penalty_time_add(struct gsm_subscriber_connection *conn,
+ struct neighbor_ident_key *nik,
+ int penalty_time)
+{
+ _conn_penalty_time_add(conn,
+ neighbor_ident_get(conn->network->neighbor_bss_cells, nik),
+ penalty_time);
+}
+
+static void bts_penalty_time_add(struct gsm_subscriber_connection *conn,
+ struct gsm_bts *bts,
+ int penalty_time)
+{
+ _conn_penalty_time_add(conn, bts, penalty_time);
}
static unsigned int conn_penalty_time_remaining(struct gsm_subscriber_connection *conn,
- struct gsm_bts *bts)
+ const void *for_object)
{
if (!conn->hodec2.penalty_timers)
return 0;
- return penalty_timers_remaining(conn->hodec2.penalty_timers, bts);
+ return penalty_timers_remaining(conn->hodec2.penalty_timers, for_object);
}
/* did we get a RXLEV for a given cell in the given report? Mark matches as MRC_F_PROCESSED. */
@@ -300,7 +332,7 @@ static bool codec_type_is_supported(struct gsm_subscriber_connection *conn,
int i;
struct gsm0808_speech_codec_list *clist = &conn->codec_list;
- if (!conn->codec_list_present) {
+ if (!conn->codec_list.len) {
/* We don't have a list of supported codecs. This should never happen. */
LOGPHOLCHAN(conn->lchan, LOGL_ERROR,
"No Speech Codec List present, accepting all codecs\n");
@@ -546,7 +578,7 @@ static uint8_t check_requirements(struct gsm_lchan *lchan, struct gsm_bts *bts,
/* the maximum number of unsynchonized handovers must no be exceeded */
if (current_bts != bts
- && bsc_ho_count(bts, true) >= ho_get_hodec2_ho_max(bts->ho)) {
+ && handover_count(bts, HO_SCOPE_ALL) >= ho_get_hodec2_ho_max(bts->ho)) {
LOGPHOLCHANTOBTS(lchan, bts, LOGL_DEBUG,
"not a candidate, number of allowed handovers (%d) would be exceeded\n",
ho_get_hodec2_ho_max(bts->ho));
@@ -687,8 +719,16 @@ static int trigger_handover_or_assignment(struct gsm_lchan *lchan, struct gsm_bt
full_rate ? "TCH/F" : "TCH/H",
ho_reason_name(global_ho_reason));
- return bsc_handover_start(HODEC2, lchan, current_bts == new_bts? NULL : new_bts,
- full_rate? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H);
+ {
+ struct handover_mo_req req = {
+ .from_hodec_id = HODEC2,
+ .old_lchan = lchan,
+ .target_nik = *bts_ident_key(new_bts),
+ .new_lchan_type = full_rate? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H,
+ };
+ handover_start(&req);
+ }
+ return 0;
}
/* debug collected candidates */
@@ -788,6 +828,13 @@ static void collect_handover_candidate(struct gsm_lchan *lchan, struct neigh_mea
struct gsm_bts *bts = lchan->ts->trx->bts;
int tchf_count, tchh_count;
struct gsm_bts *neighbor_bts;
+ const struct gsm0808_cell_id_list2 *neighbor_cil;
+ struct neighbor_ident_key ni = {
+ .from_bts = bts->nr,
+ .arfcn = nmp->arfcn,
+ .bsic_kind = BSIC_6BIT,
+ .bsic = nmp->bsic,
+ };
int avg;
struct ho_candidate *c;
int min_rxlev;
@@ -801,16 +848,24 @@ static void collect_handover_candidate(struct gsm_lchan *lchan, struct neigh_mea
/* skip if measurement report is old */
if (nmp->last_seen_nr != lchan->meas_rep_last_seen_nr) {
- LOGPHOLCHAN(lchan, LOGL_DEBUG, "neighbor ARFCN %u measurement report is old"
+ LOGPHOLCHAN(lchan, LOGL_DEBUG, "neighbor ARFCN %u BSIC %u measurement report is old"
" (nmp->last_seen_nr=%u lchan->meas_rep_last_seen_nr=%u)\n",
- nmp->arfcn, nmp->last_seen_nr, lchan->meas_rep_last_seen_nr);
+ nmp->arfcn, nmp->bsic, nmp->last_seen_nr, lchan->meas_rep_last_seen_nr);
return;
}
- neighbor_bts = bts_by_arfcn_bsic(bts->network, nmp->arfcn, nmp->bsic);
+ neighbor_bts = bts_by_neighbor_ident(bts->network, &ni);
if (!neighbor_bts) {
- LOGPHOBTS(bts, LOGL_DEBUG, "neighbor ARFCN %u does not belong to this network\n",
- nmp->arfcn);
+ neighbor_cil = neighbor_ident_get(bts->network->neighbor_bss_cells, &ni);
+ if (neighbor_cil) {
+ LOGPHOBTS(bts, LOGL_ERROR, "would inter-BSC handover to ARFCN %u BSIC %u,"
+ " but inter-BSC handover not implemented for ho decision 2\n",
+ nmp->arfcn, nmp->bsic);
+ return;
+ }
+
+ LOGPHOBTS(bts, LOGL_DEBUG, "no neighbor ARFCN %u BSIC %u configured for this cell\n",
+ nmp->arfcn, nmp->bsic);
return;
}
@@ -820,7 +875,7 @@ static void collect_handover_candidate(struct gsm_lchan *lchan, struct neigh_mea
return;
}
- /* caculate average rxlev for this cell over the window */
+ /* calculate average rxlev for this cell over the window */
avg = neigh_meas_avg(nmp, ho_get_hodec2_rxlev_neigh_avg_win(bts->ho));
/* Heed rxlev hysteresis only if the RXLEV/RXQUAL/TA levels of the MS aren't critically bad and
@@ -1146,12 +1201,12 @@ static void on_measurement_report(struct gsm_meas_rep *mr)
LOGPHOLCHAN(lchan, LOGL_ERROR, "Skipping, No subscriber connection???\n");
return;
}
- if (lchan->conn->secondary_lchan) {
+ if (lchan->conn->assignment.new_lchan) {
LOGPHOLCHAN(lchan, LOGL_INFO, "Skipping, Initial Assignment is still ongoing\n");
return;
}
- if (lchan->conn->ho) {
- LOGPHOLCHAN(lchan, LOGL_INFO, "Skipping, Handover already triggered\n");
+ if (lchan->conn->ho.fi) {
+ LOGPHOLCHAN(lchan, LOGL_INFO, "Skipping, Handover still ongoing\n");
return;
}
@@ -1227,11 +1282,11 @@ static void on_measurement_report(struct gsm_meas_rep *mr)
&& lchan->rqd_ta > ho_get_hodec2_max_distance(bts->ho)) {
global_ho_reason = HO_REASON_MAX_DISTANCE;
LOGPHOLCHAN(lchan, LOGL_INFO, "Attempting handover due to high TA\n");
- /* start penalty timer to prevent comming back too
+ /* start penalty timer to prevent coming back too
* early. it must be started before selecting a better cell,
* so there is no assignment selected, due to running
* penalty timer. */
- conn_penalty_time_add(lchan->conn, bts, ho_get_hodec2_penalty_max_dist(bts->ho));
+ bts_penalty_time_add(lchan->conn, bts, ho_get_hodec2_penalty_max_dist(bts->ho));
find_alternative_lchan(lchan, true);
return;
}
@@ -1340,16 +1395,16 @@ static int bts_resolve_congestion(struct gsm_bts *bts, int tchf_congestion, int
continue;
/* (Do not consider dynamic TS that are in PDCH mode) */
- switch (ts_pchan(ts)) {
+ switch (ts->pchan_is) {
case GSM_PCHAN_TCH_F:
lc = &ts->lchan[0];
/* omit if channel not active */
if (lc->type != GSM_LCHAN_TCH_F
- || lc->state != LCHAN_S_ACTIVE)
+ || !lchan_state_is(lc, LCHAN_ST_ACTIVE))
break;
/* omit if there is an ongoing ho/as */
- if (!lc->conn || lc->conn->secondary_lchan
- || lc->conn->ho)
+ if (!lc->conn || lc->conn->assignment.new_lchan
+ || lc->conn->ho.fi)
break;
/* We desperately want to resolve congestion, ignore rxlev when
* collecting candidates by passing include_weaker_rxlev=true. */
@@ -1360,12 +1415,12 @@ static int bts_resolve_congestion(struct gsm_bts *bts, int tchf_congestion, int
lc = &ts->lchan[j];
/* omit if channel not active */
if (lc->type != GSM_LCHAN_TCH_H
- || lc->state != LCHAN_S_ACTIVE)
+ || !lchan_state_is(lc, LCHAN_ST_ACTIVE))
continue;
/* omit of there is an ongoing ho/as */
if (!lc->conn
- || lc->conn->secondary_lchan
- || lc->conn->ho)
+ || lc->conn->assignment.new_lchan
+ || lc->conn->ho.fi)
continue;
/* We desperately want to resolve congestion, ignore rxlev when
* collecting candidates by passing include_weaker_rxlev=true. */
@@ -1776,50 +1831,58 @@ static void congestion_check_cb(void *arg)
reinit_congestion_timer(net);
}
-void on_ho_chan_activ_nack(struct bsc_handover *ho)
+static void on_handover_end(struct gsm_subscriber_connection *conn, enum handover_result result)
{
- struct gsm_bts *new_bts = ho->new_lchan->ts->trx->bts;
+ struct gsm_bts *old_bts = NULL;
+ struct gsm_bts *new_bts = NULL;
+ int penalty;
+ struct handover *ho = &conn->ho;
- LOGPHO(ho, LOGL_ERROR, "Channel Activate Nack for %s, starting penalty timer\n", ho->inter_cell? "Handover" : "Assignment");
-
- /* if channel failed, wait 10 seconds before allowing to retry handover */
- conn_penalty_time_add(ho->old_lchan->conn, new_bts, 10); /* FIXME configurable */
-}
+ /* If all went fine, then there are no penalty timers to set. */
+ if (result == HO_RESULT_OK)
+ return;
-void on_ho_failure(struct bsc_handover *ho)
-{
- struct gsm_bts *old_bts = ho->old_lchan->ts->trx->bts;
- struct gsm_bts *new_bts = ho->new_lchan->ts->trx->bts;
- struct gsm_subscriber_connection *conn = ho->old_lchan->conn;
+ if (conn->lchan)
+ old_bts = conn->lchan->ts->trx->bts;
+ if (ho->mt.new_lchan)
+ new_bts = ho->mt.new_lchan->ts->trx->bts;
- if (!conn) {
- LOGPHO(ho, LOGL_ERROR, "HO failure, but no conn");
+ /* Only interested in handovers within this BSS or going out into another BSS. Incoming handovers
+ * from another BSS are accounted for in the other BSS. */
+ if (!old_bts)
return;
- }
- if (conn->hodec2.failures >= ho_get_hodec2_retries(old_bts->ho)) {
- int penalty = ho->inter_cell
- ? ho_get_hodec2_penalty_failed_ho(old_bts->ho)
- : ho_get_hodec2_penalty_failed_as(old_bts->ho);
- LOGPHO(ho, LOGL_NOTICE, "%s failed, starting penalty timer (%d s)\n",
- ho->inter_cell ? "Handover" : "Assignment",
- penalty);
- conn->hodec2.failures = 0;
- conn_penalty_time_add(conn, new_bts, penalty);
- } else {
+ if (conn->hodec2.failures < ho_get_hodec2_retries(old_bts->ho)) {
conn->hodec2.failures++;
- LOGPHO(ho, LOGL_NOTICE, "%s failed, allowing handover decision to try again"
+ LOG_HO(conn, LOGL_NOTICE, "Failed, allowing handover decision to try again"
" (%d/%d attempts)\n",
- ho->inter_cell ? "Handover" : "Assignment",
conn->hodec2.failures, ho_get_hodec2_retries(old_bts->ho));
+ return;
}
+
+ switch (ho->scope) {
+ case HO_INTRA_CELL:
+ penalty = ho_get_hodec2_penalty_failed_as(old_bts->ho);
+ break;
+ default:
+ /* TODO: separate penalty for inter-BSC HO? */
+ penalty = ho_get_hodec2_penalty_failed_ho(old_bts->ho);
+ break;
+ }
+
+ LOG_HO(conn, LOGL_NOTICE, "Failed, starting penalty timer (%d s)\n", penalty);
+ conn->hodec2.failures = 0;
+
+ if (new_bts)
+ bts_penalty_time_add(conn, new_bts, penalty);
+ else
+ nik_penalty_time_add(conn, &ho->target_cell, penalty);
}
-struct handover_decision_callbacks hodec2_callbacks = {
+static struct handover_decision_callbacks hodec2_callbacks = {
.hodec_id = 2,
.on_measurement_report = on_measurement_report,
- .on_ho_chan_activ_nack = on_ho_chan_activ_nack,
- .on_ho_failure = on_ho_failure,
+ .on_handover_end = on_handover_end,
};
void hodec2_init(struct gsm_network *net)
diff --git a/src/osmo-bsc/handover_fsm.c b/src/osmo-bsc/handover_fsm.c
new file mode 100644
index 000000000..fa9f25b63
--- /dev/null
+++ b/src/osmo-bsc/handover_fsm.c
@@ -0,0 +1,1070 @@
+/* Handover FSM implementation for intra-BSC and inter-BSC MT (to this BSC) Handover.
+ * (For inter-BSC MO, from this BSC, see handover_inter_mo_fsm.c)
+ *
+ * (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * Author: Neels Hofmeyr <neels@hofmeyr.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/gsm/rsl.h>
+
+#include <osmocom/bsc/debug.h>
+#include <osmocom/bsc/bsc_subscriber.h>
+
+#include <osmocom/bsc/handover_fsm.h>
+#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
+#include <osmocom/bsc/lchan_select.h>
+#include <osmocom/bsc/lchan_fsm.h>
+#include <osmocom/bsc/gsm_04_08_utils.h>
+#include <osmocom/bsc/abis_rsl.h>
+#include <osmocom/bsc/bsc_msc_data.h>
+
+#define LOG_FMT_BTS "bts %u lac-ci %u-%u arfcn-bsic %d-%d"
+#define LOG_ARGS_BTS(bts) \
+ (bts) ? (bts)->nr : 0, \
+ (bts) ? (bts)->location_area_code : 0, \
+ (bts) ? (bts)->cell_identity : 0, \
+ (bts) ? (bts)->c0->arfcn : 0, \
+ (bts) ? (bts)->bsic : 0
+
+#define LOG_FMT_LCHAN "%u-%u-%u-%s%s%s-%u"
+#define LOG_ARGS_LCHAN(lchan) \
+ lchan ? lchan->ts->trx->bts->nr : 0, \
+ lchan ? lchan->ts->trx->nr : 0, \
+ lchan ? lchan->ts->nr : 0, \
+ lchan ? gsm_pchan_name(lchan->ts->pchan_on_init) : "-", \
+ (!lchan || lchan->ts->pchan_on_init == lchan->ts->pchan_is)? "" : ":", \
+ (!lchan || lchan->ts->pchan_on_init == lchan->ts->pchan_is)? "" \
+ : gsm_pchan_name(lchan->ts->pchan_is), \
+ lchan ? lchan->nr : 0 \
+
+#define LOG_FMT_HO_SCOPE "(subscr %s) %s"
+#define LOG_ARGS_HO_SCOPE(conn) \
+ bsc_subscr_name(conn->bsub), \
+ handover_scope_name(conn->ho.scope)
+
+static uint8_t g_next_ho_ref = 1;
+
+const char *handover_status(struct gsm_subscriber_connection *conn)
+{
+ static char buf[256];
+ struct handover *ho = &conn->ho;
+
+ if (!conn)
+ return "";
+
+ if (ho->scope & (HO_INTRA_CELL | HO_INTRA_BSC)) {
+ if (ho->mt.new_lchan)
+ snprintf(buf, sizeof(buf),
+ "("LOG_FMT_LCHAN") --HO-> (" LOG_FMT_LCHAN ") " LOG_FMT_HO_SCOPE,
+ LOG_ARGS_LCHAN(conn->lchan),
+ LOG_ARGS_LCHAN(ho->mt.new_lchan),
+ LOG_ARGS_HO_SCOPE(conn));
+ else if (ho->mt.new_bts)
+ snprintf(buf, sizeof(buf),
+ "("LOG_FMT_LCHAN") --HO-> ("LOG_FMT_BTS",%s) " LOG_FMT_HO_SCOPE,
+ LOG_ARGS_LCHAN(conn->lchan),
+ LOG_ARGS_BTS(ho->mt.new_bts),
+ gsm_lchant_name(ho->new_lchan_type),
+ LOG_ARGS_HO_SCOPE(conn));
+ else
+ snprintf(buf, sizeof(buf),
+ "("LOG_FMT_LCHAN") --HO->(?) " LOG_FMT_HO_SCOPE,
+ LOG_ARGS_LCHAN(conn->lchan),
+ LOG_ARGS_HO_SCOPE(conn));
+
+ } else if (ho->scope & HO_INTER_BSC_MO)
+ snprintf(buf, sizeof(buf),
+ "("LOG_FMT_LCHAN") --HO-> (%s) " LOG_FMT_HO_SCOPE,
+ LOG_ARGS_LCHAN(conn->lchan),
+ neighbor_ident_key_name(&ho->target_cell),
+ LOG_ARGS_HO_SCOPE(conn));
+
+ else if (ho->scope & HO_INTER_BSC_MT) {
+ if (ho->mt.new_lchan)
+ snprintf(buf, sizeof(buf),
+ "(remote:%s) --HO-> (local:%s|"LOG_FMT_LCHAN") " LOG_FMT_HO_SCOPE,
+ gsm0808_cell_id_name(&ho->mt.inter_bsc.cell_id_serving),
+ gsm0808_cell_id_name2(&ho->mt.inter_bsc.cell_id_target),
+ LOG_ARGS_LCHAN(ho->mt.new_lchan),
+ LOG_ARGS_HO_SCOPE(conn));
+ else if (ho->mt.new_bts)
+ snprintf(buf, sizeof(buf),
+ "(remote:%s) --HO-> (local:%s|"LOG_FMT_BTS",%s) " LOG_FMT_HO_SCOPE,
+ gsm0808_cell_id_name(&ho->mt.inter_bsc.cell_id_serving),
+ gsm0808_cell_id_name2(&ho->mt.inter_bsc.cell_id_target),
+ LOG_ARGS_BTS(ho->mt.new_bts),
+ gsm_lchant_name(ho->new_lchan_type),
+ LOG_ARGS_HO_SCOPE(conn));
+ else
+ snprintf(buf, sizeof(buf),
+ "(remote:%s) --HO-> (local:%s,%s) " LOG_FMT_HO_SCOPE,
+ gsm0808_cell_id_name(&ho->mt.inter_bsc.cell_id_serving),
+ gsm0808_cell_id_name2(&ho->mt.inter_bsc.cell_id_target),
+ gsm_lchant_name(ho->new_lchan_type),
+ LOG_ARGS_HO_SCOPE(conn));
+ } else
+ snprintf(buf, sizeof(buf), LOG_FMT_HO_SCOPE, LOG_ARGS_HO_SCOPE(conn));
+ return buf;
+}
+
+static struct osmo_fsm ho_fsm;
+
+/* From local var fi->priv, define local var conn. */
+#define GET_CONN() \
+ struct gsm_subscriber_connection *conn = fi->priv; \
+ OSMO_ASSERT((fi)->fsm == &ho_fsm && (fi)->priv) \
+
+#define GET_HO() \
+ GET_CONN(); \
+ struct handover *ho = &conn->ho
+
+/*
+ho_st_wait_lchan_active#HO_ST_WAIT_LCHAN_ACTIVE
+ho_st_wait_rr_ho_detect#HO_ST_WAIT_RR_HO_DETECT
+ho_st_wait_rr_ho_complete#HO_ST_WAIT_RR_HO_COMPLETE
+ho_st_wait_lchan_established#HO_ST_WAIT_LCHAN_ESTABLISHED
+ho_st_wait_mgw_endpoint_to_msc#HO_ST_WAIT_MGW_ENDPOINT_TO_MSC
+
+
+:s/ho_st_\(.*\)#\(HO_ST_\(.*\)\)/static void ho_fsm_\1_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)\r{\r\tGET_CONN();\r}\r\rstatic void ho_fsm_\1(struct osmo_fsm_inst *fi, uint32_t event, void *data)\r{\r\tGET_CONN();\r\tswitch (event) {\r\tdefault:\r\t\tOSMO_ASSERT(false);\r\t}\r}\r
+
+
+
+:s/ho_st_\(.*\)#\(HO_ST_\(.*\)\)/\t[\2] = {\r\t\t.name = "\3",\r\t\t.onenter = ho_fsm_\1_onenter,\r\t\t.action = ho_fsm_\1,\r\t\t.in_event_mask = 0\r\t\t\t| S(HO_EV_CONN_RELEASING)\r\t\t\t,\r\t\t.out_state_mask = 0\r\t\t\t| S(HO_ST_FAILURE)\r\t\t\t,\r\t},
+
+
+*/
+
+
+struct state_timeout ho_fsm_timeouts[32] = {
+ [HO_ST_WAIT_LCHAN_ACTIVE] = { .T = 23042 },
+ [HO_ST_WAIT_RR_HO_DETECT] = { .T = 23042 },
+ [HO_ST_WAIT_RR_HO_COMPLETE] = { .T = 23042 },
+ [HO_ST_WAIT_LCHAN_ESTABLISHED] = { .T = 23042 },
+ [HO_ST_WAIT_MGW_ENDPOINT_TO_MSC] = { .T = 23042 },
+ [MOHO_ST_WAIT_HO_COMMAND] = { .T = 7 },
+ [MOHO_ST_WAIT_CLEAR] = { .T = 8 },
+};
+
+/* Transition to a state, using the T timer defined in ho_fsm_timeouts.
+ * The actual timeout value is in turn obtained from network->T_defs.
+ * Assumes local variable fi exists. */
+#define ho_fsm_state_chg(state) \
+ fsm_inst_state_chg_T(fi, state, \
+ ho_fsm_timeouts, \
+ ((struct gsm_subscriber_connection*)(fi->priv))->network->T_defs, \
+ 5)
+
+/* Log failure and transition to HO_ST_FAILURE, which triggers the appropriate actions. */
+#define ho_fail(result, fmt, args...) do { \
+ LOG_HO(conn, LOGL_ERROR, "Handover failed in state %s, %s: " fmt, \
+ osmo_fsm_inst_state_name(conn->fi), handover_result_name(result), ## args); \
+ handover_end(conn, result); \
+ } while(0)
+
+#define ho_success() do { \
+ LOG_HO(conn, LOGL_DEBUG, "Handover succeeded"); \
+ handover_end(conn, HO_RESULT_OK); \
+ } while(0)
+
+/* issue handover to a cell identified by ARFCN and BSIC */
+void handover_request(struct handover_mo_req *req)
+{
+ struct gsm_subscriber_connection *conn;
+ OSMO_ASSERT(req->old_lchan);
+
+ conn = req->old_lchan->conn;
+ OSMO_ASSERT(conn && conn->fi);
+
+ /* To make sure we're allowed to start a handover, go through a gscon event dispatch. */
+ osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_HANDOVER_START, req);
+}
+
+/* Check that ho has old_lchan and/or new_lchan and conn pointers match.
+ * If old_lchan and/or new_lchan are NULL, omit those checks.
+ * On error, return false, log an error and call handover_end() with HO_RESULT_ERROR. */
+bool handover_is_sane(struct gsm_subscriber_connection *conn, struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan)
+{
+ if (!conn->ho.fi) {
+ LOG_HO(conn, LOGL_ERROR, "No handover ongoing");
+ return false;
+ }
+
+ if (old_lchan
+ && (conn != old_lchan->conn || conn->lchan != old_lchan))
+ goto insane;
+ if (new_lchan
+ && (conn != new_lchan->conn || conn->ho.mt.new_lchan != new_lchan))
+ goto insane;
+ if (conn->lchan && conn->lchan == conn->ho.mt.new_lchan)
+ goto insane;
+
+ return true;
+insane:
+ LOG_HO(conn, LOGL_ERROR, "Handover state is corrupted");
+ handover_end(conn, HO_RESULT_ERROR);
+ return false;
+}
+
+static void ho_fsm_update_id(struct osmo_fsm_inst *fi, const char *label)
+{
+ GET_CONN();
+ if (conn->fi->id)
+ osmo_fsm_inst_update_id_f(fi, "%s_%s", label, conn->fi->id);
+ else
+ osmo_fsm_inst_update_id_f(fi, "%s_conn_%x", label, conn->sccp.conn_id);
+}
+
+static void handover_start_intra_bsc(struct gsm_subscriber_connection *conn, struct gsm_bts *new_bts);
+static void handover_start_inter_bsc_mo(struct gsm_subscriber_connection *conn,
+ const struct gsm0808_cell_id_list2 *target_cells);
+
+void handover_fsm_alloc(struct gsm_subscriber_connection *conn) {
+ static bool g_initialized = false;
+ if (!g_initialized) {
+ OSMO_ASSERT(osmo_fsm_register(&ho_fsm) == 0);
+ g_initialized = true;
+ }
+
+ OSMO_ASSERT(conn->fi);
+ OSMO_ASSERT(!conn->ho.fi);
+
+ conn->ho.fi = osmo_fsm_inst_alloc_child(&ho_fsm, conn->fi, GSCON_EV_HANDOVER_END);
+ OSMO_ASSERT(conn->ho.fi);
+ conn->ho.fi->priv = conn;
+}
+
+
+/* Invoked by gscon if a handover was accepted to start now. */
+void handover_start(struct handover_mo_req *req)
+{
+
+ OSMO_ASSERT(req && req->old_lchan && req->old_lchan->conn);
+ struct gsm_subscriber_connection *conn = req->old_lchan->conn;
+ struct handover *ho = &conn->ho;
+ struct gsm_bts *bts;
+ const struct gsm0808_cell_id_list2 *cil;
+
+ handover_fsm_alloc(conn);
+
+ ho->from_hodec_id = req->from_hodec_id;
+ ho->new_lchan_type = req->new_lchan_type == GSM_LCHAN_NONE ?
+ req->old_lchan->type : req->new_lchan_type;
+ ho->target_cell = req->target_nik;
+
+ bts = bts_by_neighbor_ident(conn->network, &req->target_nik);
+ if (bts) {
+ handover_start_intra_bsc(conn, bts);
+ return;
+ }
+
+ cil = neighbor_ident_get(conn->network->neighbor_bss_cells, &req->target_nik);
+ if (cil) {
+ handover_start_inter_bsc_mo(conn, cil);
+ return;
+ }
+
+ LOG_HO(conn, LOGL_ERROR, "Cannot handover %s: neighbor unknown\n",
+ neighbor_ident_key_name(&req->target_nik));
+ handover_end(conn, HO_RESULT_FAIL_NO_CHANNEL);
+}
+
+/*! Hand over the specified logical channel to the specified new BTS and possibly change the lchan type.
+ * This is the main entry point for the actual handover algorithm, after the decision whether to initiate
+ * HO to a specific BTS. To not change the lchan type, pass old_lchan->type. */
+static void handover_start_intra_bsc(struct gsm_subscriber_connection *conn,
+ struct gsm_bts *new_bts)
+{
+ struct handover *ho = &conn->ho;
+ struct osmo_fsm_inst *fi = conn->ho.fi;
+
+ OSMO_ASSERT(new_bts);
+
+ *ho = (struct handover){
+ .fi = ho->fi,
+ .scope = (new_bts == conn->lchan->ts->trx->bts) ? HO_INTRA_CELL : HO_INTRA_BSC,
+ .mt = {
+ .new_bts = new_bts,
+ .ho_ref = g_next_ho_ref++,
+ .async = true,
+ .new_lchan = lchan_select_by_type(ho->mt.new_bts, ho->new_lchan_type),
+ },
+ };
+
+ if (!ho->mt.new_lchan) {
+ ho_fail(HO_RESULT_FAIL_NO_CHANNEL,
+ "No %s lchan available on BTS %u",
+ gsm_lchant_name(ho->new_lchan_type), ho->mt.new_bts->nr);
+ return;
+ }
+ LOG_HO(conn, LOGL_DEBUG, "Selected lchan %s", gsm_lchan_name(ho->mt.new_lchan));
+
+ if (ho->scope & HO_INTRA_CELL)
+ ho_fsm_update_id(fi, "intraCell");
+ else
+ ho_fsm_update_id(fi, "intraBSC");
+
+ ho_fsm_state_chg(HO_ST_WAIT_LCHAN_ACTIVE);
+
+ {
+ struct lchan_activate_info info = {
+ .activ_for = FOR_HANDOVER,
+ .for_conn = conn,
+ .chan_mode = conn->lchan->tch_mode,
+ .requires_voice_stream = conn->lchan->mgw_endpoint_ci_bts ? true : false,
+ .old_lchan = conn->lchan,
+ };
+
+ lchan_activate(ho->mt.new_lchan, &info);
+ }
+}
+
+/* 3GPP TS 48.008 § 3.2.1.8 Handover Request */
+static bool parse_ho_request(struct gsm_subscriber_connection *conn, const struct msgb *msg,
+ struct handover_mt_req *req)
+{
+ struct tlv_parsed tp_arr[2];
+ struct tlv_parsed *tp = &tp_arr[0];
+ struct tlv_parsed *tp2 = &tp_arr[1];
+ struct tlv_p_entry *e;
+
+ int payload_length = msg->tail - msg->l4h;
+ if (tlv_parse2(tp_arr, 2, gsm0808_att_tlvdef(), msg->l4h + 1, payload_length - 1, 0, 0) <= 0) {
+ LOG_HO(conn, LOGL_ERROR, "Failed to parse IEs\n");
+ return false;
+ }
+
+ if (!(e = TLVP_GET(tp, GSM0808_IE_CHANNEL_TYPE))) {
+ LOG_HO(conn, LOGL_ERROR, "Missing Channel Type IE\n");
+ return false;
+ }
+ if (gsm0808_dec_channel_type(&req->ct, e->val, e->len) <= 0) {
+ LOG_HO(conn, LOGL_ERROR, "Failed to parse Channel Type IE\n");
+ return false;
+ }
+
+ if (!(e = TLVP_GET(tp, GSM0808_IE_ENCRYPTION_INFORMATION))) {
+ LOG_HO(conn, LOGL_ERROR, "Missing Encryption Information IE\n");
+ return false;
+ }
+ if (gsm0808_dec_encrypt_info(&req->ei, e->val, e->len) <= 0) {
+ LOG_HO(conn, LOGL_ERROR, "Failed to parse Encryption Information IE\n");
+ return false;
+ }
+
+ if ((e = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_TYPE_1))) {
+ if (e->len != sizeof(req->classmark.classmark1)) {
+ LOG_HO(conn, LOGL_ERROR, "Classmark Information 1 has wrong size\n");
+ return false;
+ }
+ req->classmark.classmark1 = *(struct gsm48_classmark1*)e->val;
+ req->classmark.classmark1_set = true;
+ } else if ((e = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T2))) {
+ uint8_t len = OSMO_MIN(sizeof(req->classmark.classmark2),
+ e->len);
+ if (!len) {
+ LOG_HO(conn, LOGL_ERROR, "Classmark Information 2 has zero size\n");
+ return false;
+ }
+ memcpy(&req->classmark.classmark2, e->val, len);
+ req->classmark.classmark2_len = len;
+ } else {
+ LOG_HO(conn, LOGL_ERROR, "Missing IE: either Classmark Information 1 or 2 required\n");
+ return false;
+ }
+
+ /* The Cell Identifier (Serving) and Cell Identifier (Target) are both 3.2.2.17 and are
+ * identified by the same tag. So get one from tp and the other from tp2. */
+ if (!(e = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER))) {
+ LOG_HO(conn, LOGL_ERROR, "Missing IE: Cell Identifier (Serving)\n");
+ return false;
+ }
+ if (gsm0808_dec_cell_id(&req->cell_id_serving, e->val, e->len) < 0) {
+ LOG_HO(conn, LOGL_ERROR, "Invalid IE: Cell Identifier (Serving)\n");
+ return false;
+ }
+
+ if (!(e = TLVP_GET(tp2, GSM0808_IE_CELL_IDENTIFIER))) {
+ LOG_HO(conn, LOGL_ERROR, "Missing IE: Cell Identifier (Target)\n");
+ return false;
+ }
+ if (gsm0808_dec_cell_id(&req->cell_id_target, e->val, e->len) < 0) {
+ LOG_HO(conn, LOGL_ERROR, "Invalid IE: Cell Identifier (Target)\n");
+ return false;
+ }
+
+ /* A lot of IEs remain ignored... */
+
+ return true;
+}
+
+static bool chan_mode_is_tch(enum gsm48_chan_mode mode)
+{
+ switch (mode) {
+ case GSM48_CMODE_SPEECH_V1:
+ case GSM48_CMODE_SPEECH_EFR:
+ case GSM48_CMODE_SPEECH_AMR:
+ return true;
+ default:
+ return false;
+ }
+}
+
+void handover_start_inter_bsc_mt(struct gsm_subscriber_connection *conn,
+ struct msgb *ho_request_msg)
+{
+ struct handover *ho = &conn->ho;
+ struct bsc_msc_data *msc = conn->sccp.msc;
+ struct handover_mt_req *req;
+ int match_idx;
+ enum gsm48_chan_mode mode;
+ bool full_rate;
+ struct osmo_fsm_inst *fi;
+
+ handover_fsm_alloc(conn);
+
+ *ho = (struct handover){
+ .fi = ho->fi,
+ .from_hodec_id = HODEC_REMOTE,
+ .scope = HO_INTER_BSC_MT,
+ .mt = {
+ .ho_ref = g_next_ho_ref++,
+ .async = true,
+ },
+ };
+
+ fi = ho->fi;
+
+ rate_ctr_inc(&conn->network->bsc_ctrs->ctr[BSC_CTR_INTER_BSC_HO_MT_ATTEMPTED]);
+
+ if (!parse_ho_request(conn, ho_request_msg, &ho->mt.inter_bsc)) {
+ ho_fail(HO_RESULT_ERROR, "Invalid Handover Request message from MSC\n");
+ return;
+ }
+
+ req = &ho->mt.inter_bsc;
+
+ /* Figure out channel type */
+ if (bsc_match_codec_pref(&mode,
+ &full_rate,
+ &req->ct,
+ &req->scl,
+ msc->audio_support, msc->audio_length)) {
+ ho_fail(HO_RESULT_FAIL_NO_CHANNEL,
+ "Could not find an allowed channel codec (%s, speech codec list len = %u)",
+ gsm0808_channel_type_name(&req->ct),
+ req->scl.len);
+ return;
+ }
+
+ LOG_HO(conn, LOGL_DEBUG, "Found matching audio type: %s %s for channel_type ="
+ " { ch_indctr=0x%x, ch_rate_type=0x%x, perm_spch=[ %s] }\n",
+ gsm48_chan_mode_name(mode), full_rate? "full-rate" : "half-rate",
+ req->ct.ch_indctr, req->ct.ch_rate_type,
+ osmo_hexdump(req->ct.perm_spch, req->ct.perm_spch_len));
+
+ /* Figure out which cell to handover to. */
+ for (match_idx = 0; ; match_idx++) {
+ struct gsm_bts *bts;
+
+ bts = gsm_bts_by_cell_id(conn->network, &req->cell_id_target,
+ match_idx);
+
+ /* Did we iterate all matches? */
+ if (!bts)
+ break;
+
+ ho->mt.new_bts = bts;
+ LOG_HO(conn, LOGL_DEBUG, "BTS %u matches cell id %s",
+ bts->nr, gsm0808_cell_id_name(&req->cell_id_target));
+
+ ho->mt.new_lchan = lchan_select_by_chan_mode(bts, mode, full_rate);
+ if (!ho->mt.new_lchan) {
+ LOG_HO(conn, LOGL_DEBUG, "BTS %u has no matching free channels",
+ bts->nr);
+ ho->mt.new_bts = NULL;
+ continue;
+ }
+
+ /* Found a match. */
+ break;
+ }
+
+ if (!ho->mt.new_lchan) {
+ ho_fail(HO_RESULT_ERROR, "No free/matching lchan found for %s %s %s\n",
+ gsm0808_cell_id_name(&req->cell_id_target),
+ gsm48_chan_mode_name(mode), full_rate ? "full-rate" : "half-rate");
+ return;
+ }
+
+ /* Just for completeness' sake, maybe some logging uses it? */
+ ho->new_lchan_type = ho->mt.new_lchan->type;
+
+ ho_fsm_state_chg(HO_ST_WAIT_LCHAN_ACTIVE);
+
+ {
+ struct lchan_activate_info info = {
+ .activ_for = FOR_HANDOVER,
+ .for_conn = conn,
+ .chan_mode = mode,
+ .requires_voice_stream = chan_mode_is_tch(mode),
+ };
+
+ lchan_activate(ho->mt.new_lchan, &info);
+ }
+}
+
+
+#define FUNC_RESULT_COUNTER(name) \
+static int result_counter_##name(enum handover_result result) \
+{ \
+ switch (result) { \
+ case HO_RESULT_OK: \
+ return BSC_CTR_##name##_COMPLETED; \
+ case HO_RESULT_FAIL_NO_CHANNEL: \
+ return BSC_CTR_##name##_NO_CHANNEL; \
+ case HO_RESULT_FAIL_RR_HO_FAIL: \
+ return BSC_CTR_##name##_FAILED; \
+ case HO_RESULT_FAIL_TIMEOUT: \
+ return BSC_CTR_##name##_TIMEOUT; \
+ case HO_RESULT_CONN_RELEASE: \
+ return BSC_CTR_##name##_STOPPED; \
+ default: \
+ case HO_RESULT_ERROR: \
+ return BSC_CTR_##name##_ERROR; \
+ } \
+}
+
+FUNC_RESULT_COUNTER(ASSIGNMENT)
+FUNC_RESULT_COUNTER(HANDOVER)
+FUNC_RESULT_COUNTER(INTER_BSC_HO_MT)
+
+static int result_counter_INTER_BSC_HO_MO(enum handover_result result) {
+ switch (result) {
+ case HO_RESULT_OK:
+ return BSC_CTR_INTER_BSC_HO_MT_COMPLETED;
+ case HO_RESULT_FAIL_TIMEOUT:
+ return BSC_CTR_INTER_BSC_HO_MT_TIMEOUT;
+ case HO_RESULT_CONN_RELEASE:
+ return BSC_CTR_INTER_BSC_HO_MT_STOPPED;
+ default:
+ case HO_RESULT_ERROR:
+ return BSC_CTR_INTER_BSC_HO_MT_ERROR;
+ }
+}
+
+static int result_counter(enum handover_scope scope, enum handover_result result)
+{
+ switch (scope) {
+ case HO_INTRA_CELL:
+ return result_counter_ASSIGNMENT(result);
+ default:
+ LOGP(DHO, LOGL_ERROR, "invalid enum handover_scope value: %s\n",
+ handover_scope_name(scope));
+ /* use "normal" HO_INTRA_BSC counter... */
+ case HO_INTRA_BSC:
+ return result_counter_HANDOVER(result);
+ case HO_INTER_BSC_MO:
+ return result_counter_INTER_BSC_HO_MO(result);
+ case HO_INTER_BSC_MT:
+ return result_counter_INTER_BSC_HO_MT(result);
+ return result_counter_HANDOVER(result);
+ }
+}
+
+static void handover_reset(struct gsm_subscriber_connection *conn)
+{
+ if (conn->ho.mt.new_lchan)
+ /* New lchan was activated but never passed to a conn */
+ lchan_release(conn->ho.mt.new_lchan, true, true, RSL_ERR_EQUIPMENT_FAIL);
+ conn->ho = (struct handover){
+ .fi = conn->ho.fi,
+ };
+}
+
+/* Notify the handover decision algorithm of failure and clear out any handover state. */
+void handover_end(struct gsm_subscriber_connection *conn, enum handover_result result)
+{
+ struct handover_decision_callbacks *hdc;
+ struct handover *ho = &conn->ho;
+
+ /* Sanity -- an error result ensures beyond doubt that we don't handover_mt_use_new_lchan() below
+ * when the handover isn't actually allowed to change this conn. */
+ if (result == HO_RESULT_OK && ho->mt.new_lchan) {
+ if (!(ho->scope & (HO_INTRA_CELL | HO_INTRA_BSC | HO_INTER_BSC_MT))) {
+ LOG_HO(conn, LOGL_ERROR, "Got new lchan, but this is not an MT HO\n");
+ result = HO_RESULT_ERROR;
+ }
+ if (ho->mt.new_lchan->conn != conn) {
+ LOG_HO(conn, LOGL_ERROR, "Got new lchan, but it is for another conn\n");
+ result = HO_RESULT_ERROR;
+ }
+ }
+
+ hdc = handover_decision_callbacks_get(ho->from_hodec_id);
+ if (hdc && hdc->on_handover_end)
+ hdc->on_handover_end(conn, result);
+
+ rate_ctr_inc(&conn->network->bsc_ctrs->ctr[result_counter(ho->scope, result)]);
+
+ if (ho->mt.new_lchan) {
+ if (result == HO_RESULT_OK)
+ gscon_change_primary_lchan(conn, &conn->ho.mt.new_lchan);
+ else {
+ /* Release new lchan, it didn't work out */
+ lchan_release(ho->mt.new_lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ ho->mt.new_lchan = NULL;
+ }
+ }
+
+ LOG_HO(conn, LOGL_INFO, "Result: %s\n", handover_result_name(result));
+
+ osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_HANDOVER_END, &result);
+ handover_reset(conn);
+ osmo_fsm_inst_term(conn->ho.fi, OSMO_FSM_TERM_REGULAR, 0);
+}
+
+
+static void ho_fsm_wait_lchan_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_CONN();
+ switch (event) {
+
+ case HO_EV_LCHAN_ACTIVE:
+ ho_fsm_state_chg(HO_ST_WAIT_RR_HO_DETECT);
+ return;
+
+ case HO_EV_LCHAN_ERROR:
+ ho_fail(HO_RESULT_ERROR, "error while activating lchan %s",
+ gsm_lchan_name(conn->ho.mt.new_lchan));
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void ho_fsm_wait_rr_ho_detect_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int rc;
+ GET_CONN();
+ struct handover *ho = &conn->ho;
+
+ struct msgb *rr_ho_cmd = gsm48_make_ho_cmd(ho->mt.new_lchan,
+ ho->mt.new_lchan->ms_power,
+ ho->mt.ho_ref);
+ if (!rr_ho_cmd) {
+ ho_fail(HO_RESULT_ERROR, "Unable to compose RR Handover Command");
+ return;
+ }
+
+
+ if (ho->scope & (HO_INTRA_CELL | HO_INTRA_BSC)) {
+ /* conn->lchan is the old lchan being handovered from */
+ rr_ho_cmd->lchan = conn->lchan;
+ rc = gsm48_sendmsg(rr_ho_cmd);
+ if (rc)
+ ho_fail(HO_RESULT_ERROR, "Unable to Tx RR Handover Command (rc=%d %s)",
+ rc, strerror(-rc));
+ return;
+ }
+
+ if (ho->scope & HO_INTER_BSC_MT) {
+ rc = bsc_send_handover_request_ack(conn, rr_ho_cmd);
+ if (rc)
+ ho_fail(HO_RESULT_ERROR, "Unable to Tx BSSMAP Handover Request Ack (rc=%d %s)",
+ rc, strerror(-rc));
+ return;
+ }
+
+ ho_fail(HO_RESULT_ERROR, "Invalid situation, no target for RR Handover Command");
+}
+
+static void ho_fsm_wait_rr_ho_detect(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_CONN();
+ switch (event) {
+
+ case HO_EV_RR_HO_DETECT:
+ {
+ OSMO_ASSERT(data);
+ struct handover_rr_detect_data *d = data;
+ if (d->access_delay) {
+ LOG_HO(conn, LOGL_DEBUG, "RR Handover Detect (Access Delay=%u)",
+ *(d->access_delay));
+ } else
+ LOG_HO(conn, LOGL_DEBUG, "RR Handover Detect (no Access Delay IE)");
+ }
+
+ ho_fsm_state_chg(HO_ST_WAIT_RR_HO_COMPLETE);
+ /* The lchan FSM will already start to redirect the RTP stream */
+ return;
+
+ case HO_EV_RR_HO_COMPLETE:
+ ho_fail(HO_RESULT_ERROR,
+ "Received RR Handover Complete, but haven't even seen a Handover Detect yet");
+ return;
+
+ case HO_EV_RR_HO_FAIL:
+ ho_fail(HO_RESULT_FAIL_RR_HO_FAIL, "Received RR Handover Fail message");
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void ho_fsm_wait_rr_ho_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_CONN();
+
+ switch (event) {
+
+ case HO_EV_RR_HO_COMPLETE:
+ ho_fsm_state_chg(HO_ST_WAIT_LCHAN_ESTABLISHED);
+ return;
+
+ case HO_EV_RR_HO_FAIL:
+ ho_fail(HO_RESULT_FAIL_RR_HO_FAIL, "Received RR Handover Fail message");
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void ho_fsm_post_lchan_established(struct osmo_fsm_inst *fi);
+
+static void ho_fsm_wait_lchan_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ GET_HO();
+
+ /* The RR Assignment Complete counts as RLL Establish event */
+ osmo_fsm_inst_dispatch(ho->mt.new_lchan->fi, LCHAN_EV_RLL_ESTABLISH_IND, 0);
+
+ if (lchan_state_is(ho->mt.new_lchan, LCHAN_ST_ACTIVE))
+ ho_fsm_post_lchan_established(fi);
+}
+
+static void ho_fsm_wait_lchan_established(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ switch (event) {
+
+ case HO_EV_LCHAN_ESTABLISHED:
+ ho_fsm_post_lchan_established(fi);
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void ho_fsm_post_lchan_established(struct osmo_fsm_inst *fi)
+{
+ GET_HO();
+
+ if (ho->mt.new_lchan->activate.requires_voice_stream
+ && (ho->scope & HO_INTER_BSC_MT))
+ ho_fsm_state_chg(HO_ST_WAIT_MGW_ENDPOINT_TO_MSC);
+ else
+ ho_success();
+}
+
+static void ho_fsm_wait_mgw_endpoint_to_msc_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+
+ OSMO_ASSERT(false);
+#if 0
+ GET_CONN();
+
+ if (!gscon_connect_mgw_to_msc(conn,
+ ho->mt.msc_rtp_addr,
+ ho->mt.msc_rtp_port,
+ fi,
+ HO_EV_MSC_MGW_OK,
+ HO_EV_MSC_MGW_FAIL,
+ NULL,
+ &ho->mt.created_ci_for_msc)) {
+ ho_fail(HO_RESULT_ERROR,
+ "Unable to connect MGW endpoint to the MSC side");
+ }
+#endif
+}
+
+static void ho_fsm_wait_mgw_endpoint_to_msc(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_CONN();
+ switch (event) {
+
+ case HO_EV_MSC_MGW_OK:
+ ho_success();
+ return;
+
+ case HO_EV_MSC_MGW_FAIL:
+ ho_fail(HO_RESULT_ERROR,
+ "Unable to connect MGW endpoint to the MSC side");
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void handover_start_inter_bsc_mo(struct gsm_subscriber_connection *conn,
+ const struct gsm0808_cell_id_list2 *target_cells)
+{
+ int rc;
+ struct msgb *msg;
+ struct handover *ho = &conn->ho;
+ struct osmo_fsm_inst *fi = conn->ho.fi;
+ struct gsm0808_handover_required params = {
+ .cause = GSM0808_CAUSE_BETTER_CELL,
+ .cil = *target_cells,
+ .current_channel_type_1_present = true,
+ .current_channel_type_1 = gsm0808_current_channel_type_1(conn->lchan->type),
+ };
+
+ ho->scope = HO_INTER_BSC_MO;
+
+ ho_fsm_update_id(fi, "interMO");
+
+ switch (conn->lchan->type) {
+ case GSM_LCHAN_TCH_F:
+ case GSM_LCHAN_TCH_H:
+ params.speech_version_used_present = true;
+ params.speech_version_used = gsm0808_permitted_speech(conn->lchan->type,
+ conn->lchan->tch_mode);
+ if (!params.speech_version_used) {
+ ho_fail(HO_RESULT_ERROR,
+ "Cannot encode Speech Version (Used) for HANDOVER REQUIRED message\n");
+ return;
+ }
+ break;
+ default:
+ break;
+ }
+
+ msg = gsm0808_create_handover_required(&params);
+ if (!msg) {
+ ho_fail(HO_RESULT_ERROR, "Cannot compose Handover Required message");
+ return;
+ }
+
+ rc = gscon_sigtran_send(conn, msg);
+ if (rc) {
+ ho_fail(HO_RESULT_ERROR, "Cannot send Handover Required message");
+ return;
+ }
+
+ ho_fsm_state_chg(MOHO_ST_WAIT_HO_COMMAND);
+}
+
+static void moho_fsm_wait_ho_command(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ int rc;
+ struct moho_rx_bssmap_ho_command *rx;
+ GET_CONN();
+ switch (event) {
+
+ case MOHO_EV_BSSMAP_HO_COMMAND:
+ rx = data;
+ if (!rx) {
+ ho_fail(HO_RESULT_ERROR,
+ "Rx BSSMAP Handover Command: no L3 info passed with event");
+ return;
+ }
+
+ LOG_HO(conn, LOGL_DEBUG, "Rx BSSMAP Handover Command: forwarding Layer 3 Info: %s",
+ osmo_hexdump(rx->l3_info, rx->l3_info_len));
+
+ rc = rsl_forward_layer3_info(conn->lchan, rx->l3_info, rx->l3_info_len);
+ if (rc) {
+ ho_fail(HO_RESULT_ERROR,
+ "Rx BSSMAP Handover Command: Failed to forward Layer 3 Info (rc=%d %s)",
+ rc, strerror(-rc));
+ return;
+ }
+
+ ho_fsm_state_chg(MOHO_ST_WAIT_CLEAR);
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+
+#define S(x) (1 << (x))
+
+static const struct osmo_fsm_state ho_fsm_states[] = {
+ [HO_ST_NOT_STARTED] = {
+ .name = "NOT_STARTED",
+ .out_state_mask = 0
+ | S(HO_ST_WAIT_LCHAN_ACTIVE)
+ | S(MOHO_ST_WAIT_HO_COMMAND)
+ ,
+ },
+ [HO_ST_WAIT_LCHAN_ACTIVE] = {
+ .name = "WAIT_LCHAN_ACTIVE",
+ .action = ho_fsm_wait_lchan_active,
+ .in_event_mask = 0
+ | S(HO_EV_LCHAN_ACTIVE)
+ | S(HO_EV_LCHAN_ERROR)
+ ,
+ .out_state_mask = 0
+ | S(HO_ST_WAIT_LCHAN_ACTIVE)
+ | S(HO_ST_WAIT_RR_HO_DETECT)
+ ,
+ },
+ [HO_ST_WAIT_RR_HO_DETECT] = {
+ .name = "WAIT_RR_HO_DETECT",
+ .onenter = ho_fsm_wait_rr_ho_detect_onenter,
+ .action = ho_fsm_wait_rr_ho_detect,
+ .in_event_mask = 0
+ | S(HO_EV_RR_HO_DETECT)
+ | S(HO_EV_RR_HO_COMPLETE) /* actually as error */
+ | S(HO_EV_RR_HO_FAIL)
+ ,
+ .out_state_mask = 0
+ | S(HO_ST_WAIT_RR_HO_COMPLETE)
+ ,
+ },
+ [HO_ST_WAIT_RR_HO_COMPLETE] = {
+ .name = "WAIT_RR_HO_COMPLETE",
+ .action = ho_fsm_wait_rr_ho_complete,
+ .in_event_mask = 0
+ | S(HO_EV_RR_HO_COMPLETE)
+ | S(HO_EV_RR_HO_FAIL)
+ ,
+ .out_state_mask = 0
+ | S(HO_ST_WAIT_LCHAN_ESTABLISHED)
+ ,
+ },
+ [HO_ST_WAIT_LCHAN_ESTABLISHED] = {
+ .name = "WAIT_LCHAN_ESTABLISHED",
+ .onenter = ho_fsm_wait_lchan_established_onenter,
+ .action = ho_fsm_wait_lchan_established,
+ .in_event_mask = 0
+ | S(HO_EV_LCHAN_ESTABLISHED)
+ ,
+ .out_state_mask = 0
+ | S(HO_ST_WAIT_MGW_ENDPOINT_TO_MSC)
+ ,
+ },
+ [HO_ST_WAIT_MGW_ENDPOINT_TO_MSC] = {
+ .name = "WAIT_MGW_ENDPOINT_TO_MSC",
+ .onenter = ho_fsm_wait_mgw_endpoint_to_msc_onenter,
+ .action = ho_fsm_wait_mgw_endpoint_to_msc,
+ .in_event_mask = 0
+ | S(HO_EV_MSC_MGW_OK)
+ | S(HO_EV_MSC_MGW_FAIL)
+ ,
+ },
+
+ [MOHO_ST_WAIT_HO_COMMAND] = {
+ .name = "inter-BSC WAIT_HO_COMMAND",
+ .action = moho_fsm_wait_ho_command,
+ .in_event_mask = 0
+ | S(MOHO_EV_BSSMAP_HO_COMMAND)
+ ,
+ .out_state_mask = 0
+ | S(MOHO_ST_WAIT_CLEAR)
+ ,
+ },
+ [MOHO_ST_WAIT_CLEAR] = {
+ .name = "inter-BSC WAIT_CLEAR",
+ },
+};
+
+static const struct value_string ho_fsm_event_names[] = {
+ OSMO_VALUE_STRING(HO_EV_LCHAN_ACTIVE),
+ OSMO_VALUE_STRING(HO_EV_LCHAN_ESTABLISHED),
+ OSMO_VALUE_STRING(HO_EV_LCHAN_ERROR),
+ OSMO_VALUE_STRING(HO_EV_RR_HO_DETECT),
+ OSMO_VALUE_STRING(HO_EV_RR_HO_COMPLETE),
+ OSMO_VALUE_STRING(HO_EV_RR_HO_FAIL),
+ OSMO_VALUE_STRING(HO_EV_MSC_MGW_OK),
+ OSMO_VALUE_STRING(HO_EV_MSC_MGW_FAIL),
+ OSMO_VALUE_STRING(HO_EV_CONN_RELEASING),
+ OSMO_VALUE_STRING(MOHO_EV_BSSMAP_HO_COMMAND),
+ {}
+};
+
+void ho_fsm_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_CONN();
+ switch (event) {
+
+ case HO_EV_CONN_RELEASING:
+ switch (fi->state) {
+ case MOHO_ST_WAIT_CLEAR:
+ ho_success();
+ return;
+ default:
+ ho_fail(HO_RESULT_CONN_RELEASE,
+ "Connection releasing in the middle of handover");
+ return;
+ }
+
+ case HO_EV_LCHAN_ERROR:
+ switch (fi->state) {
+ case MOHO_ST_WAIT_HO_COMMAND:
+ case MOHO_ST_WAIT_CLEAR:
+ LOG_HO(conn, LOGL_ERROR, "Event not permitted: %s",
+ osmo_fsm_event_name(fi->fsm, event));
+ return;
+
+ default:
+ ho_fail(HO_RESULT_ERROR, "Error while establishing lchan %s",
+ gsm_lchan_name(data));
+ return;
+ }
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+int ho_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+ GET_CONN();
+ ho_fail(HO_RESULT_FAIL_TIMEOUT, "Timeout");
+ return 0;
+}
+
+void ho_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
+{
+ GET_CONN();
+ conn->ho.fi = NULL;
+}
+
+static struct osmo_fsm ho_fsm = {
+ .name = "handover",
+ .states = ho_fsm_states,
+ .num_states = ARRAY_SIZE(ho_fsm_states),
+ .log_subsys = DRSL,
+ .event_names = ho_fsm_event_names,
+ .allstate_action = ho_fsm_allstate_action,
+ .allstate_event_mask = 0
+ | S(HO_EV_CONN_RELEASING)
+ | S(HO_EV_LCHAN_ERROR)
+ ,
+ .timer_cb = ho_fsm_timer_cb,
+ .cleanup = ho_fsm_cleanup,
+};
diff --git a/src/osmo-bsc/handover_logic.c b/src/osmo-bsc/handover_logic.c
index 960bf6993..bdb73f520 100644
--- a/src/osmo-bsc/handover_logic.c
+++ b/src/osmo-bsc/handover_logic.c
@@ -33,7 +33,7 @@
#include <osmocom/bsc/gsm_data.h>
#include <osmocom/gsm/gsm_utils.h>
#include <osmocom/bsc/abis_rsl.h>
-#include <osmocom/bsc/chan_alloc.h>
+#include <osmocom/bsc/lchan_select.h>
#include <osmocom/bsc/signal.h>
#include <osmocom/core/talloc.h>
#include <osmocom/bsc/bsc_subscriber.h>
@@ -41,355 +41,183 @@
#include <osmocom/bsc/handover.h>
#include <osmocom/bsc/handover_cfg.h>
#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
+#include <osmocom/bsc/neighbor_ident.h>
+#include <osmocom/bsc/abis_nm.h>
+#include <osmocom/gsm/gsm0808.h>
+#include <osmocom/gsm/gsm0808_utils.h>
+
+const struct value_string handover_scope_names[] = {
+ { HO_NO_HANDOVER, "No Handover" },
+ { HO_INTRA_CELL, "Assignment" },
+ { HO_INTRA_BSC, "Handover" },
+ { HO_INTER_BSC_MO, "Inter-BSC-Handover (MO)" },
+ { HO_INTER_BSC_MT, "Inter-BSC-Handover (MT)" },
+ { HO_SCOPE_ALL, "Any Handover" },
+ {}
+};
+
+const struct value_string handover_result_names[] = {
+ { HO_RESULT_OK, "Complete" },
+ { HO_RESULT_FAIL_NO_CHANNEL, "Failure (no channel could be allocated)" },
+ { HO_RESULT_FAIL_RR_HO_FAIL, "Failure (MS sent RR Handover Failure)" },
+ { HO_RESULT_FAIL_TIMEOUT, "Failure (timeout)" },
+ { HO_RESULT_CONN_RELEASE, "Connection released" },
+ { HO_RESULT_ERROR, "Failure" },
+ {}
+};
-static LLIST_HEAD(bsc_handovers);
static LLIST_HEAD(handover_decision_callbacks);
-static void handover_free(struct bsc_handover *ho)
-{
- osmo_timer_del(&ho->T3103);
- llist_del(&ho->list);
- talloc_free(ho);
-}
-
-static struct bsc_handover *bsc_ho_by_new_lchan(struct gsm_lchan *new_lchan)
+void handover_decision_callbacks_register(struct handover_decision_callbacks *hdc)
{
- struct bsc_handover *ho;
-
- llist_for_each_entry(ho, &bsc_handovers, list) {
- if (ho->new_lchan == new_lchan)
- return ho;
- }
-
- return NULL;
+ llist_add_tail(&hdc->entry, &handover_decision_callbacks);
}
-static struct bsc_handover *bsc_ho_by_old_lchan(struct gsm_lchan *old_lchan)
+struct handover_decision_callbacks *handover_decision_callbacks_get(int hodec_id)
{
- struct bsc_handover *ho;
-
- llist_for_each_entry(ho, &bsc_handovers, list) {
- if (ho->old_lchan == old_lchan)
- return ho;
+ struct handover_decision_callbacks *hdc;
+ llist_for_each_entry(hdc, &handover_decision_callbacks, entry) {
+ if (hdc->hodec_id == hodec_id)
+ return hdc;
}
-
return NULL;
}
-/*! Hand over the specified logical channel to the specified new BTS and possibly change the lchan type.
- * This is the main entry point for the actual handover algorithm, after the decision whether to initiate
- * HO to a specific BTS. To not change the lchan type, pass old_lchan->type. */
-int bsc_handover_start(enum hodec_id from_hodec_id, struct gsm_lchan *old_lchan, struct gsm_bts *new_bts,
- enum gsm_chan_t new_lchan_type)
-{
- struct gsm_subscriber_connection *conn;
- struct bsc_handover *ho;
- static uint8_t ho_ref = 0;
- bool do_assignment;
-
- OSMO_ASSERT(old_lchan);
-
- /* don't attempt multiple handovers for the same lchan at
- * the same time */
- if (bsc_ho_by_old_lchan(old_lchan))
- return -EBUSY;
-
- conn = old_lchan->conn;
- if (!conn) {
- LOGP(DHO, LOGL_ERROR, "Old lchan lacks connection data.\n");
- return -ENOSPC;
- }
-
- if (!new_bts)
- new_bts = old_lchan->ts->trx->bts;
- OSMO_ASSERT(new_bts);
-
- do_assignment = (new_bts == old_lchan->ts->trx->bts);
-
- ho = talloc_zero(conn, struct bsc_handover);
- if (!ho) {
- LOGP(DHO, LOGL_FATAL, "Out of Memory\n");
- return -ENOMEM;
- }
- ho->from_hodec_id = from_hodec_id;
- ho->old_lchan = old_lchan;
- ho->new_bts = new_bts;
- ho->new_lchan_type = new_lchan_type;
- ho->ho_ref = ho_ref++;
- ho->inter_cell = !do_assignment;
- ho->async = true;
- llist_add(&ho->list, &bsc_handovers);
-
- conn->ho = ho;
-
- DEBUGP(DHO, "(BTS %u trx %u ts %u lchan %u %s)->(BTS %u lchan %s) Initiating %s...\n",
- old_lchan->ts->trx->bts->nr,
- old_lchan->ts->trx->nr,
- old_lchan->ts->nr,
- old_lchan->nr,
- gsm_pchan_name(old_lchan->ts->pchan),
- new_bts->nr,
- gsm_lchant_name(new_lchan_type),
- do_assignment ? "Assignment" : "Handover");
-
- return osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_HO_START, NULL);
-}
-
-/*! Start actual handover. Call bsc_handover_start() instead; The only legal caller is the GSCON FSM in
- * bsc_subscr_conn_fsm.c. */
-int bsc_handover_start_gscon(struct gsm_subscriber_connection *conn)
+#if 0
+static void handover_start_inter_bsc_mo(struct handover *ho,
+ const struct gsm0808_cell_id_list2 *target_cells,
+ enum gsm_chan_t new_lchan_type)
{
int rc;
- struct gsm_network *network = conn->network;
- struct bsc_handover *ho = conn->ho;
- struct gsm_lchan *old_lchan;
- struct gsm_lchan *new_lchan;
-
- if (!ho) {
- LOGP(DHO, LOGL_ERROR, "%s: Requested to start handover, but conn->ho is NULL\n",
- bsc_subscr_name(conn->bsub));
- return -EINVAL;
- }
+ struct gsm_lchan *old_lchan = ho->mo.old_lchan;
+ struct gsm0808_handover_required ho_required_params = {
+ .cause = GSM0808_CAUSE_BETTER_CELL,
+ .cil = *target_cells,
+ .current_channel_type_1_present = true,
+ .current_channel_type_1 = gsm0808_current_channel_type_1(old_lchan->type),
+ };
- OSMO_ASSERT(ho->old_lchan && ho->new_bts);
+ ho->scope = HO_INTER_BSC_MO;
- if (ho->old_lchan->conn != conn) {
- LOGP(DHO, LOGL_ERROR,
- "%s: Requested to start handover, but the lchan does not belong to this conn\n",
- bsc_subscr_name(conn->bsub));
- return -EINVAL;
- }
-
- rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_ATTEMPTED]);
+ LOGPHO(ho, LOGL_INFO, "Starting\n");
- ho->new_lchan = lchan_alloc(ho->new_bts, ho->new_lchan_type, 0);
- if (!ho->new_lchan) {
- LOGP(DHO, LOGL_NOTICE, "No free channel for %s\n", gsm_lchant_name(ho->new_lchan_type));
- rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_NO_CHANNEL]);
- return -ENOSPC;
+ if (osmo_fsm_inst_dispatch(ho->conn->fi, GSCON_EV_INTER_BSC_HO_MO_START, NULL)) {
+ handover_end(ho, HO_RESULT_ERROR);
+ return;
}
- LOGPHO(ho, LOGL_INFO, "Triggering %s\n", ho->inter_cell? "Handover" : "Assignment");
-
- /* copy some parameters from old lchan */
- old_lchan = ho->old_lchan;
- new_lchan = ho->new_lchan;
- memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr));
- if (!ho->inter_cell) {
- new_lchan->ms_power = old_lchan->ms_power;
- new_lchan->rqd_ta = old_lchan->rqd_ta;
- } else {
- new_lchan->ms_power =
- ms_pwr_ctl_lvl(ho->new_bts->band, ho->new_bts->ms_max_power);
- /* FIXME: do we have a better idea of the timing advance? */
- //new_lchan->rqd_ta = old_lchan->rqd_ta;
- }
- new_lchan->bs_power = old_lchan->bs_power;
- new_lchan->rsl_cmode = old_lchan->rsl_cmode;
- new_lchan->tch_mode = old_lchan->tch_mode;
- memcpy(&new_lchan->mr_ms_lv, &old_lchan->mr_ms_lv, sizeof(new_lchan->mr_ms_lv));
- memcpy(&new_lchan->mr_bts_lv, &old_lchan->mr_bts_lv, sizeof(new_lchan->mr_bts_lv));
-
- new_lchan->conn = conn;
-
- rc = rsl_chan_activate_lchan(new_lchan,
- ho->async ? RSL_ACT_INTER_ASYNC : RSL_ACT_INTER_SYNC,
- ho->ho_ref);
- if (rc < 0) {
- LOGPHO(ho, LOGL_INFO, "%s Failure: activate lchan rc = %d\n",
- ho->inter_cell? "Handover" : "Assignment", rc);
- lchan_free(new_lchan);
- ho->new_lchan = NULL;
- bsc_clear_handover(conn, 0);
- return rc;
+ switch (old_lchan->type) {
+ case GSM_LCHAN_TCH_F:
+ case GSM_LCHAN_TCH_H:
+ ho_required_params.speech_version_used_present = true;
+ ho_required_params.speech_version_used = gsm0808_permitted_speech(old_lchan->type,
+ old_lchan->tch_mode);
+ if (!ho_required_params.speech_version_used) {
+ LOGPHO(ho, LOGL_ERROR,
+ "Cannot encode Speech Version (Used) for HANDOVER REQUIRED message\n");
+ handover_end(ho, HO_RESULT_ERROR);
+ return;
+ }
+ break;
+ default:
+ break;
}
- rsl_lchan_set_state(new_lchan, LCHAN_S_ACT_REQ);
- /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */
-
- return 0;
-}
-
-/* clear any operation for this connection */
-void bsc_clear_handover(struct gsm_subscriber_connection *conn, int free_lchan)
-{
- struct bsc_handover *ho = conn->ho;
-
- if (!ho)
- return;
-
- if (ho->new_lchan) {
- ho->new_lchan->conn = NULL;
- if (free_lchan)
- lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
- ho->new_lchan = NULL;
+ rc = bsc_send_handover_required(ho->mo.old_lchan, &ho_required_params);
+ if (rc) {
+ LOGPHO(ho, LOGL_ERROR, "Failed to send Handover Required (rc=%d)\n", rc);
+ handover_end(ho, HO_RESULT_ERROR);
}
-
- handover_free(ho);
- conn->ho = NULL;
}
-/* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */
-static void ho_T3103_cb(void *_ho)
-{
- struct bsc_handover *ho = _ho;
- struct gsm_network *net = ho->new_lchan->ts->trx->bts->network;
-
- DEBUGP(DHO, "HO T3103 expired\n");
- rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_TIMEOUT]);
+#endif
- /* Inform the GSCON FSM about the timed out handover */
- osmo_fsm_inst_dispatch(ho->old_lchan->conn->fi, GSCON_EV_HO_TIMEOUT, NULL);
- bsc_clear_handover(ho->old_lchan->conn, 1);
-}
-/* RSL has acknowledged activation of the new lchan */
-static int ho_chan_activ_ack(struct gsm_lchan *new_lchan)
+static void ho_meas_rep(struct gsm_meas_rep *mr)
{
- struct bsc_handover *ho;
-
- /* we need to check if this channel activation is related to
- * a handover at all (and if, which particular handover) */
- ho = bsc_ho_by_new_lchan(new_lchan);
- if (!ho)
- return -ENODEV;
-
- LOGPHO(ho, LOGL_INFO, "Channel Activate Ack, send %s COMMAND\n", ho->inter_cell? "HANDOVER" : "ASSIGNMENT");
-
- /* we can now send the 04.08 HANDOVER COMMAND to the MS
- * using the old lchan */
-
- gsm48_send_ho_cmd(ho->old_lchan, new_lchan, new_lchan->ms_power, ho->ho_ref);
-
- /* start T3103. We can continue either with T3103 expiration,
- * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */
- osmo_timer_setup(&ho->T3103, ho_T3103_cb, ho);
- osmo_timer_schedule(&ho->T3103, 10, 0);
-
- /* create a RTP connection */
- if (is_ipaccess_bts(new_lchan->ts->trx->bts))
- rsl_ipacc_crcx(new_lchan);
-
- return 0;
-}
-
-/* RSL has not acknowledged activation of the new lchan */
-static int ho_chan_activ_nack(struct gsm_lchan *new_lchan)
-{
- struct bsc_handover *ho;
struct handover_decision_callbacks *hdc;
+ enum hodec_id hodec_id = ho_get_algorithm(mr->lchan->ts->trx->bts->ho);
- ho = bsc_ho_by_new_lchan(new_lchan);
- if (!ho) {
- /* This lchan is not involved in a handover. */
- return 0;
- }
-
- hdc = handover_decision_callbacks_get(ho->from_hodec_id);
- if (hdc && hdc->on_ho_chan_activ_nack)
- hdc->on_ho_chan_activ_nack(ho);
-
- bsc_clear_handover(new_lchan->conn, 0);
- return 0;
-}
-
-/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */
-static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan)
-{
- struct gsm_network *net;
- struct bsc_handover *ho;
-
- ho = bsc_ho_by_new_lchan(new_lchan);
- if (!ho) {
- LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
- return -ENODEV;
- }
-
- net = new_lchan->ts->trx->bts->network;
-
- LOGPHO(ho, LOGL_INFO, "%s Complete\n", ho->inter_cell ? "Handover" : "Assignment");
-
- rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_COMPLETED]);
-
- osmo_timer_del(&ho->T3103);
-
- /* Replace the ho lchan with the primary one */
- if (ho->old_lchan != new_lchan->conn->lchan)
- LOGPHO(ho, LOGL_ERROR, "Primary lchan changed during handover.\n");
-
- if (new_lchan->conn->ho != ho)
- LOGPHO(ho, LOGL_ERROR, "Handover channel changed during this handover.\n");
-
- new_lchan->conn->lchan = new_lchan;
- ho->old_lchan->conn = NULL;
-
- lchan_release(ho->old_lchan, 0, RSL_REL_LOCAL_END);
-
- handover_free(ho);
- new_lchan->conn->ho = NULL;
-
- /* Inform the GSCON FSM that the handover is complete */
- osmo_fsm_inst_dispatch(new_lchan->conn->fi, GSCON_EV_HO_COMPL, NULL);
- return 0;
+ hdc = handover_decision_callbacks_get(hodec_id);
+ if (!hdc || !hdc->on_measurement_report)
+ return;
+ hdc->on_measurement_report(mr);
}
-/* GSM 04.08 HANDOVER FAIL has been received */
-static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
+/* Count ongoing handovers within the given BTS.
+ * ho_scopes is an OR'd combination of enum handover_scope values to include in the count. */
+int handover_count(struct gsm_bts *bts, int ho_scopes)
{
- struct gsm_network *net = old_lchan->ts->trx->bts->network;
- struct bsc_handover *ho;
- struct handover_decision_callbacks *hdc;
+ struct gsm_bts_trx *trx;
+ int count = 0;
- ho = bsc_ho_by_old_lchan(old_lchan);
- if (!ho) {
- LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
- return -ENODEV;
+ llist_for_each_entry(trx, &bts->trx_list, list) {
+ int i;
+ for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
+ struct gsm_bts_trx_ts *ts = &trx->ts[i];
+ struct gsm_lchan *lchan;
+
+ /* skip administratively deactivated timeslots */
+ if (!nm_is_running(&ts->mo.nm_state))
+ continue;
+
+ ts_for_each_lchan(lchan, ts) {
+ if (!lchan->conn)
+ continue;
+ if (!lchan->conn->ho.fi)
+ continue;
+ if (lchan->conn->ho.scope & ho_scopes)
+ count++;
+ }
+ }
}
- hdc = handover_decision_callbacks_get(ho->from_hodec_id);
- if (hdc && hdc->on_ho_failure)
- hdc->on_ho_failure(ho);
-
- rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_FAILED]);
-
- bsc_clear_handover(ho->new_lchan->conn, 1);
-
- /* Inform the GSCON FSM that the handover failed */
- osmo_fsm_inst_dispatch(old_lchan->conn->fi, GSCON_EV_HO_FAIL, NULL);
- return 0;
+ return count;
}
-/* GSM 08.58 HANDOVER DETECT has been received */
-static int ho_rsl_detect(struct gsm_lchan *new_lchan)
-{
- struct bsc_handover *ho;
-
- ho = bsc_ho_by_new_lchan(new_lchan);
- if (!ho) {
- LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
- return -ENODEV;
+struct gsm_bts *bts_by_neighbor_ident(const struct gsm_network *net,
+ const struct neighbor_ident_key *search_for)
+{
+ struct gsm_bts *found = NULL;
+ struct gsm_bts *bts;
+ struct gsm_bts *wildcard_match = NULL;
+
+ llist_for_each_entry(bts, &net->bts_list, list) {
+ struct neighbor_ident_key entry = {
+ .from_bts = NEIGHBOR_IDENT_KEY_ANY_BTS,
+ .arfcn = bts->c0->arfcn,
+ .bsic_kind = BSIC_6BIT,
+ .bsic = bts->bsic,
+ };
+ if (neighbor_ident_key_match(&entry, search_for, true)) {
+ if (found) {
+ LOGP(DHO, LOGL_ERROR, "CONFIG ERROR: Multiple BTS match %s: %d and %d\n",
+ neighbor_ident_key_name(search_for),
+ found->nr, bts->nr);
+ return found;
+ }
+ found = bts;
+ }
+ if (neighbor_ident_key_match(&entry, search_for, false))
+ wildcard_match = bts;
}
- LOGPHO(ho, LOGL_DEBUG, "Handover RACH detected\n");
-
- /* This is just for logging on the DHO category. The actual MGCP switchover happens in
- * osmo_bsc_mgcp.c by receiving the same S_LCHAN_HANDOVER_DETECT signal.
- * (Calling mgcp_handover() directly currently breaks linking in utils/...) */
+ if (found)
+ return found;
- return 0;
+ return wildcard_match;
}
-static int ho_meas_rep(struct gsm_meas_rep *mr)
+struct neighbor_ident_key *bts_ident_key(const struct gsm_bts *bts)
{
- struct handover_decision_callbacks *hdc;
- enum hodec_id hodec_id = ho_get_algorithm(mr->lchan->ts->trx->bts->ho);
-
- hdc = handover_decision_callbacks_get(hodec_id);
- if (!hdc || !hdc->on_measurement_report)
- return 0;
- hdc->on_measurement_report(mr);
- return 0;
+ static struct neighbor_ident_key key;
+ key = (struct neighbor_ident_key){
+ .arfcn = bts->c0->arfcn,
+ .bsic_kind = BSIC_6BIT,
+ .bsic = bts->bsic,
+ };
+ return &key;
}
static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
@@ -401,73 +229,23 @@ static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
lchan_data = signal_data;
switch (subsys) {
case SS_LCHAN:
+ OSMO_ASSERT(lchan_data);
lchan = lchan_data->lchan;
+ OSMO_ASSERT(lchan);
+
switch (signal) {
- case S_LCHAN_ACTIVATE_ACK:
- return ho_chan_activ_ack(lchan);
- case S_LCHAN_ACTIVATE_NACK:
- return ho_chan_activ_nack(lchan);
- case S_LCHAN_HANDOVER_DETECT:
- return ho_rsl_detect(lchan);
- case S_LCHAN_HANDOVER_COMPL:
- return ho_gsm48_ho_compl(lchan);
- case S_LCHAN_HANDOVER_FAIL:
- return ho_gsm48_ho_fail(lchan);
case S_LCHAN_MEAS_REP:
- return ho_meas_rep(lchan_data->mr);
+ ho_meas_rep(lchan_data->mr);
+ break;
}
- break;
+
default:
break;
}
-
return 0;
}
-/* Return the old lchan or NULL. This is meant for audio handling */
-struct gsm_lchan *bsc_handover_pending(struct gsm_lchan *new_lchan)
-{
- struct bsc_handover *ho;
- ho = bsc_ho_by_new_lchan(new_lchan);
- if (!ho)
- return NULL;
- return ho->old_lchan;
-}
-
static __attribute__((constructor)) void on_dso_load_ho_logic(void)
{
osmo_signal_register_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
}
-
-/* Count number of currently ongoing handovers
- * inter_cell: if true, count only handovers between two cells. If false, count only handovers within one
- * cell. */
-int bsc_ho_count(struct gsm_bts *bts, bool inter_cell)
-{
- struct bsc_handover *ho;
- int count = 0;
-
- llist_for_each_entry(ho, &bsc_handovers, list) {
- if (ho->inter_cell != inter_cell)
- continue;
- if (ho->new_lchan->ts->trx->bts == bts)
- count++;
- }
-
- return count;
-}
-
-void handover_decision_callbacks_register(struct handover_decision_callbacks *hdc)
-{
- llist_add_tail(&hdc->entry, &handover_decision_callbacks);
-}
-
-struct handover_decision_callbacks *handover_decision_callbacks_get(int hodec_id)
-{
- struct handover_decision_callbacks *hdc;
- llist_for_each_entry(hdc, &handover_decision_callbacks, entry) {
- if (hdc->hodec_id == hodec_id)
- return hdc;
- }
- return NULL;
-}
diff --git a/src/osmo-bsc/lchan_fsm.c b/src/osmo-bsc/lchan_fsm.c
new file mode 100644
index 000000000..015429893
--- /dev/null
+++ b/src/osmo-bsc/lchan_fsm.c
@@ -0,0 +1,1466 @@
+/* osmo-bsc API to allocate an lchan, complete with dyn TS switchover and MGCP communication to allocate
+ * RTP endpoints.
+ *
+ * (C) 2017 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * Author: Neels Hofmeyr <neels@hofmeyr.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/gsm/rsl.h>
+#include <osmocom/core/byteswap.h>
+
+#include <osmocom/bsc/debug.h>
+#include <osmocom/bsc/lchan_fsm.h>
+#include <osmocom/bsc/timeslot_fsm.h>
+#include <osmocom/bsc/mgw_endpoint_fsm.h>
+#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
+#include <osmocom/bsc/handover.h>
+#include <osmocom/bsc/abis_rsl.h>
+#include <osmocom/bsc/bsc_rll.h>
+#include <osmocom/bsc/gsm_04_08_utils.h>
+#include <osmocom/bsc/assignment_fsm.h>
+#include <osmocom/bsc/handover_fsm.h>
+
+static struct osmo_fsm lchan_fsm;
+
+#define GET_LCHAN(fi, LCHAN) \
+ struct gsm_lchan *LCHAN = fi->priv; \
+ OSMO_ASSERT((fi)->fsm == &lchan_fsm); \
+ OSMO_ASSERT((fi)->priv); \
+ OSMO_ASSERT(LCHAN->fi == (fi))
+
+
+bool lchan_may_receive_data(struct gsm_lchan *lchan)
+{
+ if (!lchan->fi)
+ return false;
+ switch (lchan->fi->state) {
+ case LCHAN_ST_WAIT_RLL_ESTABLISH:
+ case LCHAN_ST_ACTIVE:
+ return true;
+ default:
+ return false;
+ }
+}
+
+
+/*
+lchan_st_unused#LCHAN_ST_UNUSED
+lchan_st_wait_ts_ready#LCHAN_ST_WAIT_TS_READY
+lchan_st_wait_activ_ack#LCHAN_ST_WAIT_ACTIV_ACK
+lchan_st_wait_rll_establish#LCHAN_ST_WAIT_RLL_ESTABLISH
+lchan_st_wait_mgw_endpoint_available#LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE
+lchan_st_wait_ipacc_crcx_ack#LCHAN_ST_WAIT_IPACC_CRCX_ACK
+lchan_st_wait_ipacc_mdcx_ack#LCHAN_ST_WAIT_IPACC_MDCX_ACK
+lchan_st_active#LCHAN_ST_ACTIVE
+lchan_st_wait_sapis_released#LCHAN_ST_WAIT_SAPIS_RELEASED
+lchan_st_wait_before_rf_release#LCHAN_ST_WAIT_BEFORE_RF_RELEASE
+lchan_st_wait_rf_release_ack#LCHAN_ST_WAIT_RF_RELEASE_ACK
+lchan_st_wait_after_error#LCHAN_ST_WAIT_AFTER_ERROR
+lchan_st_borken#LCHAN_ST_BORKEN
+
+
+:s/lchan_st_\(.*\)#\(LCHAN_ST_\(.*\)\)/static void lchan_fsm_\1_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)\r{\r\tGET_LCHAN(fi, lchan);\r}\r\rstatic void lchan_fsm_\1(struct osmo_fsm_inst *fi, uint32_t event, void *data)\r{\r\tGET_LCHAN(fi, lchan);\r\tswitch (event) {\r\tdefault:\r\t\tOSMO_ASSERT(false);\r\t}\r}\r
+
+
+:s/lchan_st_\(.*\)#\(LCHAN_ST_\(.*\)\)/\t[\2] = {\r\t\t.name = "\3",\r\t\t.onenter = lchan_fsm_\1_onenter,\r\t\t.action = lchan_fsm_\1,\r\t\t.in_event_mask = 0\r\t\t\t| S(LCHAN_EV_RELEASE)\r\t\t\t,\r\t\t.out_state_mask = 0\r\t\t\t| S(LCHAN_ST_UNUSED)\r\t\t\t,\r\t},
+
+*/
+
+static void lchan_set_last_error(struct gsm_lchan *lchan, const char *fmt, ...)
+{
+ va_list ap;
+ /* This dance allows using an existing error reason in above fmt */
+ char *last_error_was = lchan->last_error;
+ lchan->last_error = NULL;
+
+ if (fmt) {
+ va_start(ap, fmt);
+ lchan->last_error = talloc_vasprintf(lchan->ts->trx, fmt, ap);
+ va_end(ap);
+
+ LOG_LCHAN(lchan, LOGL_ERROR, "%s", lchan->last_error);
+ }
+
+ if (last_error_was)
+ talloc_free(last_error_was);
+}
+
+/* The idea here is that we must not require to change any lchan state in order to deny a request. */
+#define lchan_on_activation_failure(lchan, for_conn, activ_for) \
+ _lchan_on_activation_failure(lchan, for_conn, activ_for, \
+ __FILE__, __LINE__)
+static void _lchan_on_activation_failure(struct gsm_lchan *lchan, enum lchan_activate_mode activ_for,
+ struct gsm_subscriber_connection *for_conn,
+ const char *file, int line)
+{
+ switch (activ_for) {
+
+ case FOR_MS_CHANNEL_REQUEST:
+ rsl_tx_imm_ass_rej(lchan);
+ break;
+
+ case FOR_ASSIGNMENT:
+ _osmo_fsm_inst_dispatch(for_conn->assignment.fi, ASSIGNMENT_EV_LCHAN_ERROR, lchan,
+ file, line);
+ return;
+
+ case FOR_HANDOVER:
+ if (!for_conn) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "lchan activation for Handover failed, but activation request has"
+ " no conn");
+ break;
+ }
+ if (!for_conn->ho.fi) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "lchan activation for Handover failed, but conn has no ongoing"
+ " handover procedure");
+ break;
+ }
+ _osmo_fsm_inst_dispatch(for_conn->ho.fi, HO_EV_LCHAN_ERROR, lchan, file, line);
+ break;
+
+ case FOR_VTY:
+ LOG_LCHAN(lchan, LOGL_ERROR, "VTY user invoked lchan activation failed");
+ break;
+
+ default:
+ LOG_LCHAN(lchan, LOGL_ERROR, "lchan activation failed");
+ break;
+ }
+}
+
+static void lchan_on_activation_success(struct gsm_lchan *lchan)
+{
+ switch (lchan->activate.activ_for) {
+ case FOR_MS_CHANNEL_REQUEST:
+ /* Nothing to do here, MS is free to use the channel. */
+ break;
+
+ case FOR_ASSIGNMENT:
+ if (!lchan->conn) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "lchan activation for assignment succeeded, but lchan has no conn:"
+ " cannot trigger appropriate actions. Release.");
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ break;
+ }
+ if (!lchan->conn->assignment.fi) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "lchan activation for assignment succeeded, but lchan has no"
+ " assignment ongoing: cannot trigger appropriate actions. Release.");
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ break;
+ }
+ osmo_fsm_inst_dispatch(lchan->conn->assignment.fi, ASSIGNMENT_EV_LCHAN_ESTABLISHED,
+ lchan);
+ break;
+
+ case FOR_HANDOVER:
+ if (!lchan->conn) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "lchan activation for handover succeeded, but lchan has no conn");
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ break;
+ }
+ if (!lchan->conn->ho.fi) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "lchan activation for handover succeeded, but lchan has no"
+ " handover ongoing");
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ break;
+ }
+ osmo_fsm_inst_dispatch(lchan->conn->ho.fi, HO_EV_LCHAN_ESTABLISHED, lchan);
+ break;
+
+ default:
+ LOG_LCHAN(lchan, LOGL_NOTICE, "lchan %s fully established",
+ lchan_activate_mode_name(lchan->activate.activ_for));
+ break;
+ }
+}
+
+struct state_timeout lchan_fsm_timeouts[32] = {
+ [LCHAN_ST_WAIT_TS_READY] = { .T=23001 },
+ [LCHAN_ST_WAIT_ACTIV_ACK] = { .T=23002 },
+ [LCHAN_ST_WAIT_RLL_ESTABLISH] = { .T=3101 },
+ [LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE] = { .T=23004 },
+ [LCHAN_ST_WAIT_IPACC_CRCX_ACK] = { .T=23005 },
+ [LCHAN_ST_WAIT_IPACC_MDCX_ACK] = { .T=23006 },
+ [LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED] = { .T=23004 },
+ [LCHAN_ST_WAIT_SAPIS_RELEASED] = { .T=3109 },
+ [LCHAN_ST_WAIT_BEFORE_RF_RELEASE] = { .T=3111 },
+ [LCHAN_ST_WAIT_RF_RELEASE_ACK] = { .T=3111 },
+ [LCHAN_ST_WAIT_AFTER_ERROR] = { .T=993111 },
+};
+
+/* Transition to a state, using the T timer defined in lchan_fsm_timeouts.
+ * The actual timeout value is in turn obtained from network->T_defs.
+ * Assumes local variable fi exists. */
+#define lchan_fsm_state_chg(state) \
+ fsm_inst_state_chg_T(fi, state, \
+ lchan_fsm_timeouts, \
+ ((struct gsm_lchan*)(fi->priv))->ts->trx->bts->network->T_defs, \
+ 5)
+
+uint32_t lchan_fsm_on_error[32] = {
+ [LCHAN_ST_UNUSED] = LCHAN_ST_UNUSED,
+ [LCHAN_ST_WAIT_TS_READY] = LCHAN_ST_UNUSED,
+ [LCHAN_ST_WAIT_ACTIV_ACK] = LCHAN_ST_BORKEN,
+ [LCHAN_ST_WAIT_RLL_ESTABLISH] = LCHAN_ST_WAIT_RF_RELEASE_ACK,
+ [LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE] = LCHAN_ST_WAIT_SAPIS_RELEASED,
+ [LCHAN_ST_WAIT_IPACC_CRCX_ACK] = LCHAN_ST_WAIT_SAPIS_RELEASED,
+ [LCHAN_ST_WAIT_IPACC_MDCX_ACK] = LCHAN_ST_WAIT_SAPIS_RELEASED,
+ [LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED] = LCHAN_ST_WAIT_SAPIS_RELEASED,
+ [LCHAN_ST_ACTIVE] = LCHAN_ST_WAIT_SAPIS_RELEASED,
+ [LCHAN_ST_WAIT_SAPIS_RELEASED] = LCHAN_ST_WAIT_RF_RELEASE_ACK,
+ [LCHAN_ST_WAIT_BEFORE_RF_RELEASE] = LCHAN_ST_WAIT_RF_RELEASE_ACK,
+ [LCHAN_ST_WAIT_RF_RELEASE_ACK] = LCHAN_ST_BORKEN,
+ [LCHAN_ST_WAIT_AFTER_ERROR] = LCHAN_ST_UNUSED,
+ [LCHAN_ST_BORKEN] = LCHAN_ST_BORKEN,
+};
+
+/* Set a failure message, trigger the common actions to take on failure, transition to a state to
+ * continue with (using state timeouts from lchan_fsm_timeouts[]). Assumes local variable fi exists. */
+#define lchan_fail_to(state_chg, fmt, args...) do { \
+ struct gsm_lchan *_lchan = fi->priv; \
+ uint32_t state_was = fi->state; \
+ lchan_set_last_error(fi->priv, "lchan %s in state %s: " fmt, \
+ _lchan->activate.concluded ? "failure" : "allocation failed", \
+ osmo_fsm_state_name(fi->fsm, state_was), ## args); \
+ if (!_lchan->activate.concluded) \
+ lchan_on_activation_failure(_lchan, _lchan->activate.activ_for, _lchan->conn); \
+ _lchan->activate.concluded = true; \
+ lchan_fsm_state_chg(state_chg); \
+ } while(0)
+
+#define lchan_fail(fmt, args...) lchan_fail_to(lchan_fsm_on_error[fi->state], fmt, ## args)
+
+void lchan_activate(struct gsm_lchan *lchan, struct lchan_activate_info *info)
+{
+ int rc;
+
+ OSMO_ASSERT(lchan && info);
+
+ if (!lchan_state_is(lchan, LCHAN_ST_UNUSED))
+ goto abort;
+
+ /* ensure some basic sanity up first, before we enter the machine. */
+ OSMO_ASSERT(lchan->ts && lchan->ts->fi && lchan->fi);
+
+ switch (info->activ_for) {
+
+ case FOR_ASSIGNMENT:
+ if (!info->for_conn
+ || !info->for_conn->fi) {
+ LOG_LCHAN(lchan, LOGL_ERROR, "Activation requested, but no conn");
+ goto abort;
+ }
+ if (info->for_conn->assignment.new_lchan != lchan) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "Activation for Assignment requested, but conn's state does"
+ " not reflect this lchan to be activated (instead: %s)",
+ info->for_conn->assignment.new_lchan?
+ gsm_lchan_name(info->for_conn->assignment.new_lchan)
+ : "NULL");
+ goto abort;
+ }
+ break;
+
+ case FOR_HANDOVER:
+ if (!info->for_conn
+ || !info->for_conn->fi) {
+ LOG_LCHAN(lchan, LOGL_ERROR, "Activation requested, but no conn");
+ goto abort;
+ }
+ if (!info->for_conn->ho.fi) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "Activation for Handover requested, but conn has no HO pending.");
+ goto abort;
+ }
+ if (info->for_conn->ho.mt.new_lchan != lchan) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "Activation for Handover requested, but conn's HO state does"
+ " not reflect this lchan to be activated (instead: %s)",
+ info->for_conn->ho.mt.new_lchan?
+ gsm_lchan_name(info->for_conn->ho.mt.new_lchan)
+ : "NULL");
+ goto abort;
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ /* To make sure that the lchan is actually allowed to initiate an activation, feed through an FSM
+ * event. */
+ rc = osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_ACTIVATE, info);
+
+ if (rc) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "Activation requested, but cannot dispatch LCHAN_EV_ACTIVATE event");
+ goto abort;
+ }
+ return;
+
+abort:
+ lchan_on_activation_failure(lchan, info->activ_for, info->for_conn);
+ /* Remain in state UNUSED */
+}
+
+static void lchan_fsm_update_id(struct gsm_lchan *lchan)
+{
+ osmo_fsm_inst_update_id_f(lchan->fi, "%u-%u-%u-%s-%u",
+ lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
+ gsm_pchan_id(lchan->ts->pchan_on_init), lchan->nr);
+}
+
+void lchan_fsm_alloc(struct gsm_lchan *lchan)
+{
+ static bool g_initialized = false;
+ if (!g_initialized) {
+ OSMO_ASSERT(osmo_fsm_register(&lchan_fsm) == 0);
+ g_initialized = true;
+ }
+ OSMO_ASSERT(lchan->ts);
+ OSMO_ASSERT(lchan->ts->fi);
+ OSMO_ASSERT(!lchan->fi);
+
+ lchan->fi = osmo_fsm_inst_alloc_child(&lchan_fsm, lchan->ts->fi, TS_EV_LCHAN_UNUSED);
+ OSMO_ASSERT(lchan->fi);
+ lchan->fi->priv = lchan;
+ lchan_fsm_update_id(lchan);
+ LOGPFSML(lchan->fi, LOGL_DEBUG, "new lchan\n");
+}
+
+/* Clear volatile state of the lchan. Clear all except
+ * - the ts backpointer,
+ * - the nr,
+ * - name,
+ * - the FSM instance including its current state,
+ * - last_error string.
+ */
+static void lchan_reset(struct gsm_lchan *lchan)
+{
+ if (lchan->rqd_ref) {
+ talloc_free(lchan->rqd_ref);
+ lchan->rqd_ref = NULL;
+ }
+ if (lchan->mgw_endpoint_ci_bts) {
+ mgw_endpoint_ci_dlcx(lchan->mgw_endpoint_ci_bts);
+ lchan->mgw_endpoint_ci_bts = NULL;
+ }
+
+ /* NUL all volatile state */
+ *lchan = (struct gsm_lchan){
+ .ts = lchan->ts,
+ .nr = lchan->nr,
+ .fi = lchan->fi,
+ .name = lchan->name,
+
+ .meas_rep_last_seen_nr = 255,
+
+ .last_error = lchan->last_error,
+ };
+}
+
+static void lchan_fsm_unused_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ GET_LCHAN(fi, lchan);
+ lchan_reset(lchan);
+ osmo_fsm_inst_dispatch(lchan->ts->fi, TS_EV_LCHAN_UNUSED, lchan);
+}
+
+static void lchan_fsm_unused(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ struct lchan_activate_info *info = data;
+ GET_LCHAN(fi, lchan);
+ switch (event) {
+
+ case LCHAN_EV_ACTIVATE:
+ OSMO_ASSERT(info);
+ OSMO_ASSERT(!lchan->conn);
+ OSMO_ASSERT(!lchan->mgw_endpoint_ci_bts);
+ lchan_set_last_error(lchan, NULL);
+ lchan->release_requested = false;
+
+ lchan->conn = info->for_conn;
+ lchan->activate.activ_for = info->activ_for;
+ lchan->activate.requires_voice_stream = info->requires_voice_stream;
+ lchan->activate.concluded = false;
+
+ if (info->old_lchan) {
+ lchan->mgw_endpoint_ci_bts = info->old_lchan->mgw_endpoint_ci_bts;
+ /* TODO: rather take info->for_conn->encr? */
+ lchan->encr = info->old_lchan->encr;
+ lchan->ms_power = info->old_lchan->ms_power;
+ lchan->bs_power = info->old_lchan->bs_power;
+ lchan->rqd_ta = info->old_lchan->rqd_ta;
+ } else {
+ struct gsm_bts *bts = lchan->ts->trx->bts;
+ /* TODO: rather take info->for_conn->encr? */
+ lchan->encr = (struct gsm_encr){
+ .alg_id = RSL_ENC_ALG_A5(0), /* no encryption */
+ };
+ lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
+ lchan->bs_power = 0; /* 0dB reduction, output power = Pn */
+ }
+
+ switch (info->chan_mode) {
+
+ case GSM48_CMODE_SIGN:
+ lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
+ lchan->tch_mode = GSM48_CMODE_SIGN;
+ break;
+
+ case GSM48_CMODE_SPEECH_V1:
+ case GSM48_CMODE_SPEECH_EFR:
+ case GSM48_CMODE_SPEECH_AMR:
+ lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
+ lchan->tch_mode = info->chan_mode;
+ break;
+
+ default:
+ lchan_fail("Not implemented: cannot activate for chan mode %s",
+ gsm48_chan_mode_name(info->chan_mode));
+ return;
+ }
+
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_TS_READY);
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void lchan_fsm_wait_ts_ready_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ struct mgw_endpoint *mgwep;
+ struct mgcp_conn_peer crcx_info = {};
+ GET_LCHAN(fi, lchan);
+
+ if (lchan->release_requested) {
+ lchan_fail("Release requested while activating");
+ return;
+ }
+
+ LOG_LCHAN(lchan, LOGL_INFO,
+ "Activation requested: %s voice=%s MGW-ci=%s type=%s tch-mode=%s",
+ lchan_activate_mode_name(lchan->activate.activ_for),
+ lchan->activate.requires_voice_stream ? "yes" : "no",
+ lchan->activate.requires_voice_stream ?
+ (lchan->mgw_endpoint_ci_bts ?
+ mgwep_ci_name(lchan->mgw_endpoint_ci_bts) : "new")
+ : "none",
+ gsm_lchant_name(lchan->type),
+ gsm48_chan_mode_name(lchan->tch_mode));
+
+ /* Ask for the timeslot to make ready for this lchan->type.
+ * We'll receive LCHAN_EV_TS_READY or LCHAN_EV_TS_ERROR in response. */
+ osmo_fsm_inst_dispatch(lchan->ts->fi, TS_EV_LCHAN_REQUESTED, lchan);
+
+ /* Prepare an MGW endpoint CI if appropriate. */
+ if (!lchan->activate.requires_voice_stream)
+ return;
+
+ if (lchan->mgw_endpoint_ci_bts) {
+ lchan->activate.mgw_endpoint_available = true;
+ return;
+ }
+
+ mgwep = gscon_ensure_mgw_endpoint(lchan->conn);
+ if (!mgwep) {
+ lchan_fail("Internal error: cannot obtain MGW endpoint handle for conn");
+ return;
+ }
+
+ lchan->mgw_endpoint_ci_bts = mgw_endpoint_ci_add(mgwep, "to-BTS");
+
+ mgw_endpoint_ci_request(lchan->mgw_endpoint_ci_bts,
+ MGCP_VERB_CRCX, &crcx_info,
+ lchan->fi,
+ LCHAN_EV_MGW_ENDPOINT_AVAILABLE,
+ LCHAN_EV_MGW_ENDPOINT_ERROR, 0);
+}
+
+static void lchan_fsm_wait_ts_ready(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_LCHAN(fi, lchan);
+ switch (event) {
+
+ case LCHAN_EV_TS_READY:
+ /* timeslot agrees that we may Chan Activ now. Sending it in onenter. */
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_ACTIV_ACK);
+ break;
+
+ case LCHAN_EV_MGW_ENDPOINT_AVAILABLE:
+ /* conn FSM is already done preparing an MGW endpoint. Remember that. */
+ lchan->activate.mgw_endpoint_available = true;
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void lchan_fsm_wait_activ_ack_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int rc;
+ uint8_t act_type;
+ uint8_t ho_ref = 0;
+ GET_LCHAN(fi, lchan);
+
+ if (lchan->release_requested) {
+ lchan_fail_to(LCHAN_ST_UNUSED, "Release requested while activating");
+ return;
+ }
+
+ switch (lchan->activate.activ_for) {
+ case FOR_MS_CHANNEL_REQUEST:
+ act_type = RSL_ACT_INTRA_IMM_ASS;
+ break;
+ case FOR_HANDOVER:
+ act_type = lchan->conn->ho.mt.async ? RSL_ACT_INTER_ASYNC : RSL_ACT_INTER_SYNC;
+ ho_ref = lchan->conn->ho.mt.ho_ref;
+ break;
+ default:
+ case FOR_ASSIGNMENT:
+ act_type = RSL_ACT_INTRA_NORM_ASS;
+ break;
+ }
+
+ rc = rsl_tx_chan_activ(lchan, act_type, ho_ref);
+ if (rc)
+ lchan_fail_to(LCHAN_ST_UNUSED, "Tx Chan Activ failed: %s (%d)", strerror(-rc), rc);
+}
+
+static void lchan_fsm_wait_activ_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_LCHAN(fi, lchan);
+ switch (event) {
+
+ case LCHAN_EV_MGW_ENDPOINT_AVAILABLE:
+ lchan->activate.mgw_endpoint_available = true;
+ break;
+
+ case LCHAN_EV_RSL_CHAN_ACTIV_ACK:
+ /* Chan Activ was ack'd, but we need an RLL Establish to be sure it's working out. */
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_RLL_ESTABLISH);
+ break;
+
+ case LCHAN_EV_RSL_CHAN_ACTIV_NACK:
+ if (data) {
+ uint32_t next_state;
+ lchan->error_cause = *(uint8_t*)data;
+ lchan->release_in_error = true;
+ if (lchan->error_cause != RSL_ERR_RCH_ALR_ACTV_ALLOC)
+ next_state = LCHAN_ST_BORKEN;
+ else
+ /* Taking this over from legacy code: send an RF Chan Release even though
+ * the Activ was NACKed. Is this really correct? */
+ next_state = LCHAN_ST_WAIT_RF_RELEASE_ACK;
+
+ lchan_fail_to(next_state, "Chan Activ NACK: %s (0x%x)",
+ rsl_err_name(lchan->error_cause), lchan->error_cause);
+ } else {
+ lchan->error_cause = RSL_ERR_IE_NONEXIST;
+ lchan->release_in_error = true;
+ lchan_fail_to(LCHAN_ST_BORKEN, "Chan Activ NACK without cause IE");
+ }
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void lchan_fsm_wait_rll_establish_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int rc;
+ GET_LCHAN(fi, lchan);
+ if (lchan->release_requested)
+ lchan_fail_to(LCHAN_ST_WAIT_SAPIS_RELEASED, "Release requested while activating");
+
+ switch (lchan->activate.activ_for) {
+
+ case FOR_MS_CHANNEL_REQUEST:
+ rc = rsl_tx_imm_assignment(lchan);
+ if (rc) {
+ lchan_fail("Failed to Tx RR Immediate Assignment message (rc=%d %s)",
+ rc, strerror(-rc));
+ return;
+ }
+ LOG_LCHAN(lchan, LOGL_DEBUG, "Tx RR Immediate Assignment");
+ break;
+
+ case FOR_ASSIGNMENT:
+ if (!lchan->conn) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "lchan activation for assignment succeeded, but lchan has no conn:"
+ " cannot trigger appropriate actions. Release.");
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ break;
+ }
+ if (!lchan->conn->assignment.fi) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "lchan activation for assignment succeeded, but lchan has no"
+ " assignment ongoing: cannot trigger appropriate actions. Release.");
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ break;
+ }
+ /* After the Chan Activ Ack, the MS expects to receive an RR Assignment Command.
+ * Let the assignment_fsm handle that. */
+ osmo_fsm_inst_dispatch(lchan->conn->assignment.fi, ASSIGNMENT_EV_LCHAN_ACTIVE, lchan);
+ break;
+
+ case FOR_HANDOVER:
+ if (!lchan->conn) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "lchan activation for handover succeeded, but lchan has no conn:"
+ " cannot trigger appropriate actions. Release.");
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ break;
+ }
+ if (!lchan->conn->ho.fi) {
+ LOG_LCHAN(lchan, LOGL_ERROR,
+ "lchan activation for handover succeeded, but lchan has no"
+ " handover ongoing: cannot trigger appropriate actions. Release.");
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ break;
+ }
+ /* After the Chan Activ Ack of the new lchan, send the MS an RR Handover Command on the
+ * old channel. The handover_fsm handles that. */
+ osmo_fsm_inst_dispatch(lchan->conn->ho.fi, HO_EV_LCHAN_ACTIVE, lchan);
+ break;
+
+ default:
+ LOG_LCHAN(lchan, LOGL_NOTICE, "lchan %s is now active",
+ lchan_activate_mode_name(lchan->activate.activ_for));
+ break;
+ }
+}
+
+static void lchan_fsm_wait_rll_establish(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_LCHAN(fi, lchan);
+ switch (event) {
+
+ case LCHAN_EV_MGW_ENDPOINT_AVAILABLE:
+ lchan->activate.mgw_endpoint_available = true;
+ break;
+
+ case LCHAN_EV_RLL_ESTABLISH_IND:
+ lchan->sapis[0] = LCHAN_SAPI_MS;
+ if (lchan->activate.requires_voice_stream) {
+ /* For Abis/IP, we would technically only need the MGW endpoint one step later,
+ * on IPACC MDCX. But usually the MGW endpoint is anyway done by now, so keep one
+ * common endpoint wait state for all BTS types. */
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE);
+ } else
+ lchan_fsm_state_chg(LCHAN_ST_ACTIVE);
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void lchan_fsm_tch_post_endpoint_available(struct osmo_fsm_inst *fi);
+
+static void lchan_fsm_wait_mgw_endpoint_available_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ GET_LCHAN(fi, lchan);
+
+ if (lchan->release_requested) {
+ lchan_fail("Release requested while activating");
+ return;
+ }
+
+ if (lchan->activate.mgw_endpoint_available) {
+ LOG_LCHAN(lchan, LOGL_DEBUG, "MGW endpoint already available");
+ lchan_fsm_tch_post_endpoint_available(fi);
+ }
+}
+
+static void lchan_fsm_wait_mgw_endpoint_available(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_LCHAN(fi, lchan);
+ switch (event) {
+
+ case LCHAN_EV_MGW_ENDPOINT_AVAILABLE:
+ lchan->activate.mgw_endpoint_available = true;
+ lchan_fsm_tch_post_endpoint_available(fi);
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void lchan_fsm_tch_post_endpoint_available(struct osmo_fsm_inst *fi)
+{
+ GET_LCHAN(fi, lchan);
+
+ LOG_LCHAN(lchan, LOGL_DEBUG, "MGW endpoint: %s",
+ mgcp_conn_peer_name(mgwep_ci_get_rtp_info(lchan->mgw_endpoint_ci_bts)));
+
+ if (is_ipaccess_bts(lchan->ts->trx->bts))
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_IPACC_CRCX_ACK);
+ else
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED);
+}
+
+static void lchan_fsm_wait_ipacc_crcx_ack_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int rc;
+ int val;
+ GET_LCHAN(fi, lchan);
+
+ if (lchan->release_requested) {
+ lchan_fail("Release requested while activating");
+ return;
+ }
+
+ val = ipacc_speech_mode(lchan->tch_mode, lchan->type);
+ if (val < 0) {
+ lchan_fail("Cannot determine Abis/IP speech mode for tch_mode=%s type=%s\n",
+ get_value_string(gsm48_chan_mode_names, lchan->tch_mode),
+ gsm_lchant_name(lchan->type));
+ return;
+ }
+ lchan->abis_ip.speech_mode = val;
+
+ val = ipacc_payload_type(lchan->tch_mode, lchan->type);
+ if (val < 0) {
+ lchan_fail("Cannot determine Abis/IP payload type for tch_mode=%s type=%s\n",
+ get_value_string(gsm48_chan_mode_names, lchan->tch_mode),
+ gsm_lchant_name(lchan->type));
+ return;
+ }
+ lchan->abis_ip.rtp_payload = val;
+
+ /* recv-only */
+ ipacc_speech_mode_set_direction(&lchan->abis_ip.speech_mode, false);
+
+ rc = rsl_tx_ipacc_crcx(lchan);
+ if (rc)
+ lchan_fail("Failure to transmit IPACC CRCX to BTS (rc=%d, %s)",
+ rc, strerror(-rc));
+}
+
+static void lchan_fsm_wait_ipacc_crcx_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ switch (event) {
+
+ case LCHAN_EV_IPACC_CRCX_ACK:
+ /* the CRCX ACK parsing has already noted the RTP port information at
+ * lchan->abis_ip.bound_*, see ipac_parse_rtp(). We'll use that in
+ * lchan_fsm_wait_mgw_endpoint_configured_onenter(). */
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_IPACC_MDCX_ACK);
+ return;
+
+ case LCHAN_EV_IPACC_CRCX_NACK:
+ lchan_fail("Received NACK on IPACC CRCX");
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void lchan_fsm_wait_ipacc_mdcx_ack_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int rc;
+ GET_LCHAN(fi, lchan);
+
+ uint32_t ip = lchan->abis_ip.bound_ip;
+ int port = lchan->abis_ip.bound_port;
+
+ if (lchan->release_requested) {
+ lchan_fail("Release requested while activating");
+ return;
+ }
+
+ if (!ip) {
+ lchan_fail("Cannot send IPACC MDCX to BTS:"
+ " there is no RTP IP set that the BTS should send RTP to.");
+ return;
+ }
+
+ if (port <= 0 || port > 65535) {
+ lchan_fail("Cannot send IPACC MDCX to BTS:"
+ " invalid port number that the BTS should send RTP to: %d", port);
+ return;
+ }
+
+ /* Other RTP settings were already setup in lchan_fsm_wait_ipacc_crcx_ack_onenter() */
+ lchan->abis_ip.connect_ip = ip;
+ lchan->abis_ip.connect_port = port;
+
+ /* send-recv */
+ ipacc_speech_mode_set_direction(&lchan->abis_ip.speech_mode, true);
+
+ rc = rsl_tx_ipacc_mdcx(lchan);
+ if (rc)
+ lchan_fail("Failure to transmit IPACC MDCX to BTS (rc=%d, %s)",
+ rc, strerror(-rc));
+
+}
+
+static void lchan_fsm_wait_ipacc_mdcx_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ switch (event) {
+
+ case LCHAN_EV_IPACC_MDCX_ACK:
+ /* Finally, the lchan and its RTP are established. */
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED);
+ return;
+
+ case LCHAN_EV_IPACC_MDCX_NACK:
+ lchan_fail("Received NACK on IPACC MDCX");
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+/* Tell the MGW endpoint about the RTP port allocated on BTS side. */
+static void lchan_fsm_wait_mgw_endpoint_configured_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int rc;
+ struct mgcp_conn_peer mdcx_info;
+ struct in_addr addr;
+ const char *addr_str;
+ GET_LCHAN(fi, lchan);
+
+ if (lchan->release_requested) {
+ lchan_fail("Release requested while activating");
+ return;
+ }
+
+ mdcx_info = (struct mgcp_conn_peer){
+ .port = lchan->abis_ip.bound_port,
+ };
+
+ addr.s_addr = osmo_ntohl(lchan->abis_ip.bound_ip);
+ addr_str = inet_ntoa(addr);
+ rc = osmo_strlcpy(mdcx_info.addr, addr_str, sizeof(mdcx_info.addr));
+ if (rc <= 0 || rc >= sizeof(mdcx_info.addr)) {
+ lchan_fail("Cannot compose BTS side RTP IP address to send to MGW: '%s'",
+ addr_str);
+ return;
+ }
+
+ if (!lchan->mgw_endpoint_ci_bts) {
+ lchan_fail("No MGW endpoint ci configured");
+ return;
+ }
+
+ LOG_LCHAN(lchan, LOGL_DEBUG, "Sending BTS side RTP port info %s:%u to MGW %s",
+ mdcx_info.addr, mdcx_info.port, mgwep_ci_name(lchan->mgw_endpoint_ci_bts));
+ mgw_endpoint_ci_request(lchan->mgw_endpoint_ci_bts, MGCP_VERB_MDCX,
+ &mdcx_info, fi, LCHAN_EV_MGW_ENDPOINT_CONFIGURED,
+ LCHAN_EV_MGW_ENDPOINT_ERROR, 0);
+}
+
+static void lchan_fsm_wait_mgw_endpoint_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ switch (event) {
+
+ case LCHAN_EV_MGW_ENDPOINT_CONFIGURED:
+ lchan_fsm_state_chg(LCHAN_ST_ACTIVE);
+ return;
+
+ case LCHAN_EV_MGW_ENDPOINT_ERROR:
+ lchan_fail("Error while redirecting the MGW to the BTS' RTP port");
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+
+static void lchan_fsm_active_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ GET_LCHAN(fi, lchan);
+
+ if (lchan->release_requested) {
+ lchan_fail("Release requested while activating");
+ return;
+ }
+
+ /* This flag ensures that when an lchan activation has succeeded, and we have already sent ACKs
+ * like Immediate Assignment or BSSMAP Assignment Complete, and if then, way later, some other
+ * error occurs, e.g. during release, that we don't send a NACK out of context. */
+ lchan->activate.concluded = true;
+ lchan_on_activation_success(lchan);
+}
+
+#define for_each_sapi(sapi, start, lchan) \
+ for (sapi = start; sapi < ARRAY_SIZE(lchan->sapis); sapi++)
+
+static int next_active_sapi(struct gsm_lchan *lchan, int from_sapi)
+{
+ int sapi;
+ for_each_sapi(sapi, from_sapi, lchan) {
+ if (lchan->sapis[sapi] == LCHAN_SAPI_UNUSED)
+ continue;
+ return sapi;
+ }
+ return sapi;
+}
+
+#define for_each_active_sapi(sapi, start, lchan) \
+ for (sapi = next_active_sapi(lchan, start); \
+ sapi < ARRAY_SIZE(lchan->sapis); sapi=next_active_sapi(lchan, sapi+1))
+
+static int lchan_active_sapis(struct gsm_lchan *lchan, int start)
+{
+ int sapis = 0;
+ int sapi;
+ for_each_active_sapi(sapi, start, lchan) {
+ LOG_LCHAN(lchan, LOGL_DEBUG,
+ "Still active: SAPI[%d] (%d)", sapi, lchan->sapis[sapi]);
+ sapis ++;
+ }
+ LOG_LCHAN(lchan, LOGL_DEBUG, "Still active SAPIs: %d", sapis);
+ return sapis;
+}
+
+static void handle_rll_rel_ind_or_conf(struct osmo_fsm_inst *fi, uint32_t event, void *data,
+ bool wait_for_sapi0_rel)
+{
+ uint8_t link_id;
+ uint8_t sapi;
+ GET_LCHAN(fi, lchan);
+
+ OSMO_ASSERT(data);
+ link_id = *(uint8_t*)data;
+ sapi = link_id & 7;
+
+ LOG_LCHAN(lchan, LOGL_DEBUG, "Rx RLL Release %s: SAPI=%u link_id=0x%x",
+ event == LCHAN_EV_RLL_REL_CONF ? "CONF" : "IND", sapi, link_id);
+
+ /* TODO this reflects the code state before the lchan FSM. However, it would make more sense to
+ * me that a Release IND is indeed a cue for us to send a Release Request, and not count it as an
+ * equal to Release CONF. */
+
+ lchan->sapis[sapi] = LCHAN_SAPI_UNUSED;
+ rll_indication(lchan, link_id, BSC_RLLR_IND_REL_IND);
+
+ /* Releasing SAPI 0 means the conn becomes invalid; but not if the link_id contains a TCH flag.
+ * (TODO: is this the correct interpretation?) */
+ if (lchan->conn && sapi == 0 && !(link_id & 0xc0)) {
+ LOG_LCHAN(lchan, LOGL_DEBUG, "lchan is releasing");
+ gscon_lchan_releasing(lchan->conn, lchan);
+ }
+
+ if (!lchan_active_sapis(lchan, wait_for_sapi0_rel? 0 : 1))
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_BEFORE_RF_RELEASE);
+}
+
+static void lchan_fsm_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ handle_rll_rel_ind_or_conf(fi, event, data, true);
+}
+
+static bool should_sacch_deact(struct gsm_lchan *lchan)
+{
+ switch (lchan->ts->pchan_is) {
+ case GSM_PCHAN_TCH_F:
+ case GSM_PCHAN_TCH_H:
+ case GSM_PCHAN_CCCH_SDCCH4:
+ case GSM_PCHAN_CCCH_SDCCH4_CBCH:
+ case GSM_PCHAN_SDCCH8_SACCH8C:
+ case GSM_PCHAN_SDCCH8_SACCH8C_CBCH:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static void lchan_fsm_wait_sapis_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int sapis;
+ int sapi;
+ GET_LCHAN(fi, lchan);
+
+ for (sapi=0; sapi < ARRAY_SIZE(lchan->sapis); sapi++)
+ LOG_LCHAN(lchan, LOGL_DEBUG, "SAPI[%d] = %d", sapi, lchan->sapis[sapi]);
+
+ if (lchan->conn)
+ gsm48_send_rr_release(lchan);
+
+ if (lchan->deact_sacch && should_sacch_deact(lchan))
+ rsl_deact_sacch(lchan);
+
+ sapis = 0;
+ for_each_active_sapi(sapi, 1, lchan) {
+ uint8_t link_id = sapi;
+
+ if (lchan->type == GSM_LCHAN_TCH_F || lchan->type == GSM_LCHAN_TCH_H)
+ link_id |= 0x40;
+ LOG_LCHAN(lchan, LOGL_DEBUG, "Tx: Release SAPI %u link_id 0x%x", sapi, link_id);
+ rsl_release_request(lchan, link_id, RSL_REL_LOCAL_END);
+ sapis ++;
+ }
+
+ /* Do not wait for Nokia BTS to send the confirm. */
+ if (is_nokia_bts(lchan->ts->trx->bts)
+ && lchan->ts->trx->bts->nokia.no_loc_rel_cnf) {
+
+ LOG_LCHAN(lchan, LOGL_DEBUG, "Nokia InSite BTS: not waiting for RELease CONFirm");
+
+ for_each_active_sapi(sapi, 1, lchan)
+ lchan->sapis[sapi] = LCHAN_SAPI_UNUSED;
+ sapis = 0;
+ }
+
+ if (!sapis)
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_BEFORE_RF_RELEASE);
+}
+
+static void lchan_fsm_wait_sapis_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ /* When we're telling the MS to release, we're fine to carry on with RF Channel Release when SAPI
+ * 0 release is not confirmed yet.
+ * TODO: that's how the code was before lchan FSM, is this correct/useful? */
+ handle_rll_rel_ind_or_conf(fi, event, data, false);
+}
+
+static void lchan_fsm_wait_rf_release_ack_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int rc;
+ GET_LCHAN(fi, lchan);
+ rc = rsl_tx_rf_chan_release(lchan);
+ if (rc)
+ LOG_LCHAN(lchan, LOGL_ERROR, "Failed to Tx RSL RF Channel Release: rc=%d %s\n",
+ rc, strerror(-rc));
+}
+
+static void lchan_fsm_wait_rf_release_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_LCHAN(fi, lchan);
+ switch (event) {
+
+ case LCHAN_EV_RSL_RF_CHAN_REL_ACK:
+ if (lchan->error_cause)
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_AFTER_ERROR);
+ else
+ lchan_fsm_state_chg(LCHAN_ST_UNUSED);
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void lchan_fsm_borken_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ GET_LCHAN(fi, lchan);
+ lchan_reset(lchan);
+}
+
+static void lchan_fsm_borken(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_LCHAN(fi, lchan);
+ switch (event) {
+
+ case LCHAN_EV_RSL_CHAN_ACTIV_ACK:
+ /* A late Chan Activ ACK? Release. */
+ lchan->release_in_error = true;
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_RF_RELEASE_ACK);
+ return;
+
+ case LCHAN_EV_RSL_CHAN_ACTIV_NACK:
+ /* A late Chan Activ NACK? Ok then, unused. */
+ lchan_fsm_state_chg(LCHAN_ST_UNUSED);
+ return;
+
+ case LCHAN_ST_WAIT_RF_RELEASE_ACK:
+ /* A late Release ACK? */
+ lchan->release_in_error = true;
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_AFTER_ERROR);
+ /* TODO: we used to do this only for sysmobts:
+ int do_free = is_sysmobts_v2(ts->trx->bts);
+ LOGP(DRSL, LOGL_NOTICE,
+ "%s CHAN REL ACK for broken channel. %s.\n",
+ gsm_lchan_name(lchan),
+ do_free ? "Releasing it" : "Keeping it broken");
+ if (do_free)
+ do_lchan_free(lchan);
+ * Clarify the reason. If a BTS sends a RF Chan Rel ACK, we can consider it released,
+ * independently from the BTS model, right?? */
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+#define S(x) (1 << (x))
+
+static const struct osmo_fsm_state lchan_fsm_states[] = {
+ [LCHAN_ST_UNUSED] = {
+ .name = "UNUSED",
+ .onenter = lchan_fsm_unused_onenter,
+ .action = lchan_fsm_unused,
+ .in_event_mask = 0
+ | S(LCHAN_EV_ACTIVATE)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_WAIT_TS_READY)
+ ,
+ },
+ [LCHAN_ST_WAIT_TS_READY] = {
+ .name = "WAIT_TS_READY",
+ .onenter = lchan_fsm_wait_ts_ready_onenter,
+ .action = lchan_fsm_wait_ts_ready,
+ .in_event_mask = 0
+ | S(LCHAN_EV_TS_READY)
+ | S(LCHAN_EV_MGW_ENDPOINT_AVAILABLE)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_WAIT_ACTIV_ACK)
+ ,
+ },
+ [LCHAN_ST_WAIT_ACTIV_ACK] = {
+ .name = "WAIT_ACTIV_ACK",
+ .onenter = lchan_fsm_wait_activ_ack_onenter,
+ .action = lchan_fsm_wait_activ_ack,
+ .in_event_mask = 0
+ | S(LCHAN_EV_MGW_ENDPOINT_AVAILABLE)
+ | S(LCHAN_EV_RSL_CHAN_ACTIV_ACK)
+ | S(LCHAN_EV_RSL_CHAN_ACTIV_NACK)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_WAIT_RLL_ESTABLISH)
+ | S(LCHAN_ST_BORKEN)
+ | S(LCHAN_ST_WAIT_RF_RELEASE_ACK)
+ ,
+ },
+ [LCHAN_ST_WAIT_RLL_ESTABLISH] = {
+ .name = "WAIT_RLL_ESTABLISH",
+ .onenter = lchan_fsm_wait_rll_establish_onenter,
+ .action = lchan_fsm_wait_rll_establish,
+ .in_event_mask = 0
+ | S(LCHAN_EV_MGW_ENDPOINT_AVAILABLE)
+ | S(LCHAN_EV_RLL_ESTABLISH_IND)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE)
+ | S(LCHAN_ST_ACTIVE)
+ | S(LCHAN_ST_WAIT_RF_RELEASE_ACK)
+ ,
+ },
+ [LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE] = {
+ .name = "WAIT_MGW_ENDPOINT_AVAILABLE",
+ .onenter = lchan_fsm_wait_mgw_endpoint_available_onenter,
+ .action = lchan_fsm_wait_mgw_endpoint_available,
+ .in_event_mask = 0
+ | S(LCHAN_EV_MGW_ENDPOINT_AVAILABLE)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_WAIT_IPACC_CRCX_ACK)
+ | S(LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED)
+ | S(LCHAN_ST_WAIT_SAPIS_RELEASED)
+ | S(LCHAN_ST_WAIT_RF_RELEASE_ACK)
+ ,
+ },
+ [LCHAN_ST_WAIT_IPACC_CRCX_ACK] = {
+ .name = "WAIT_IPACC_CRCX_ACK",
+ .onenter = lchan_fsm_wait_ipacc_crcx_ack_onenter,
+ .action = lchan_fsm_wait_ipacc_crcx_ack,
+ .in_event_mask = 0
+ | S(LCHAN_EV_IPACC_CRCX_ACK)
+ | S(LCHAN_EV_IPACC_CRCX_NACK)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_WAIT_IPACC_MDCX_ACK)
+ | S(LCHAN_ST_WAIT_SAPIS_RELEASED)
+ | S(LCHAN_ST_WAIT_RF_RELEASE_ACK)
+ ,
+ },
+ [LCHAN_ST_WAIT_IPACC_MDCX_ACK] = {
+ .name = "WAIT_IPACC_MDCX_ACK",
+ .onenter = lchan_fsm_wait_ipacc_mdcx_ack_onenter,
+ .action = lchan_fsm_wait_ipacc_mdcx_ack,
+ .in_event_mask = 0
+ | S(LCHAN_EV_IPACC_MDCX_ACK)
+ | S(LCHAN_EV_IPACC_MDCX_NACK)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED)
+ | S(LCHAN_ST_WAIT_SAPIS_RELEASED)
+ | S(LCHAN_ST_WAIT_RF_RELEASE_ACK)
+ ,
+ },
+ [LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED] = {
+ .name = "WAIT_MGW_ENDPOINT_CONFIGURED",
+ .onenter = lchan_fsm_wait_mgw_endpoint_configured_onenter,
+ .action = lchan_fsm_wait_mgw_endpoint_configured,
+ .in_event_mask = 0
+ | S(LCHAN_EV_MGW_ENDPOINT_CONFIGURED)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_ACTIVE)
+ | S(LCHAN_ST_WAIT_SAPIS_RELEASED)
+ | S(LCHAN_ST_WAIT_RF_RELEASE_ACK)
+ ,
+ },
+ [LCHAN_ST_ACTIVE] = {
+ .name = "ACTIVE",
+ .onenter = lchan_fsm_active_onenter,
+ .action = lchan_fsm_active,
+ .in_event_mask = 0
+ | S(LCHAN_EV_RLL_REL_IND)
+ | S(LCHAN_EV_RLL_REL_CONF)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_WAIT_SAPIS_RELEASED)
+ | S(LCHAN_ST_WAIT_BEFORE_RF_RELEASE)
+ | S(LCHAN_ST_WAIT_RF_RELEASE_ACK)
+ ,
+ },
+ [LCHAN_ST_WAIT_SAPIS_RELEASED] = {
+ .name = "WAIT_SAPIS_RELEASED",
+ .onenter = lchan_fsm_wait_sapis_released_onenter,
+ .action = lchan_fsm_wait_sapis_released,
+ .in_event_mask = 0
+ | S(LCHAN_EV_RLL_REL_IND)
+ | S(LCHAN_EV_RLL_REL_CONF)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_WAIT_BEFORE_RF_RELEASE)
+ | S(LCHAN_ST_WAIT_RF_RELEASE_ACK)
+ ,
+ },
+ [LCHAN_ST_WAIT_BEFORE_RF_RELEASE] = {
+ .name = "WAIT_BEFORE_RF_RELEASE",
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_WAIT_RF_RELEASE_ACK)
+ ,
+ },
+ [LCHAN_ST_WAIT_RF_RELEASE_ACK] = {
+ .name = "WAIT_RF_RELEASE_ACK",
+ .onenter = lchan_fsm_wait_rf_release_ack_onenter,
+ .action = lchan_fsm_wait_rf_release_ack,
+ .in_event_mask = 0
+ | S(LCHAN_EV_RSL_RF_CHAN_REL_ACK)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ | S(LCHAN_ST_WAIT_AFTER_ERROR)
+ | S(LCHAN_ST_BORKEN)
+ ,
+ },
+ [LCHAN_ST_WAIT_AFTER_ERROR] = {
+ .name = "WAIT_AFTER_ERROR",
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ ,
+ },
+ [LCHAN_ST_BORKEN] = {
+ .name = "BORKEN",
+ .onenter = lchan_fsm_borken_onenter,
+ .action = lchan_fsm_borken,
+ .in_event_mask = 0
+ | S(LCHAN_EV_RSL_CHAN_ACTIV_ACK)
+ | S(LCHAN_EV_RSL_CHAN_ACTIV_NACK)
+ | S(LCHAN_EV_RSL_RF_CHAN_REL_ACK)
+ ,
+ .out_state_mask = 0
+ | S(LCHAN_ST_UNUSED)
+ ,
+ },
+};
+
+static const struct value_string lchan_fsm_event_names[] = {
+ OSMO_VALUE_STRING(LCHAN_EV_ACTIVATE),
+ OSMO_VALUE_STRING(LCHAN_EV_TS_READY),
+ OSMO_VALUE_STRING(LCHAN_EV_TS_ERROR),
+ OSMO_VALUE_STRING(LCHAN_EV_RSL_CHAN_ACTIV_ACK),
+ OSMO_VALUE_STRING(LCHAN_EV_RSL_CHAN_ACTIV_NACK),
+ OSMO_VALUE_STRING(LCHAN_EV_RLL_ESTABLISH_IND),
+ OSMO_VALUE_STRING(LCHAN_EV_MGW_ENDPOINT_AVAILABLE),
+ OSMO_VALUE_STRING(LCHAN_EV_MGW_ENDPOINT_CONFIGURED),
+ OSMO_VALUE_STRING(LCHAN_EV_MGW_ENDPOINT_ERROR),
+ OSMO_VALUE_STRING(LCHAN_EV_IPACC_CRCX_ACK),
+ OSMO_VALUE_STRING(LCHAN_EV_IPACC_CRCX_NACK),
+ OSMO_VALUE_STRING(LCHAN_EV_IPACC_MDCX_ACK),
+ OSMO_VALUE_STRING(LCHAN_EV_IPACC_MDCX_NACK),
+ OSMO_VALUE_STRING(LCHAN_EV_RLL_REL_IND),
+ OSMO_VALUE_STRING(LCHAN_EV_RLL_REL_CONF),
+ OSMO_VALUE_STRING(LCHAN_EV_RSL_RF_CHAN_REL_ACK),
+ OSMO_VALUE_STRING(LCHAN_EV_RLL_ERR_IND),
+ OSMO_VALUE_STRING(LCHAN_EV_CHAN_MODE_MODIF_ACK),
+ OSMO_VALUE_STRING(LCHAN_EV_CHAN_MODE_MODIF_ERROR),
+ {}
+};
+
+void lchan_fsm_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_LCHAN(fi, lchan);
+
+ switch (event) {
+
+ case LCHAN_EV_TS_ERROR:
+ lchan_fail_to(LCHAN_ST_UNUSED, "LCHAN_EV_TS_ERROR");
+ return;
+
+ case LCHAN_EV_MGW_ENDPOINT_ERROR:
+ /* This event during activation means that it was not possible to establish an endpoint.
+ * After activation was successful, it could also come in at any point to signal that the
+ * MGW side has become unavailable, which should lead to graceful release. */
+ if (fi->state == LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE) {
+ /* This state is actually waiting for availability. Fail it immediately. */
+ lchan_fail("LCHAN_EV_MGW_ENDPOINT_ERROR");
+ return;
+ }
+ lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL);
+ return;
+
+ default:
+ return;
+ }
+}
+
+int lchan_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+ GET_LCHAN(fi, lchan);
+ switch (fi->state) {
+
+ case LCHAN_ST_WAIT_BEFORE_RF_RELEASE:
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_RF_RELEASE_ACK);
+ return 0;
+
+ case LCHAN_ST_WAIT_AFTER_ERROR:
+ lchan_fsm_state_chg(LCHAN_ST_UNUSED);
+ return 0;
+
+ default:
+ lchan->release_in_error = true;
+ lchan_fail("Timeout");
+ return 0;
+ }
+}
+
+void lchan_release(struct gsm_lchan *lchan, bool sacch_deact,
+ bool err, enum gsm48_rr_cause cause_rr)
+{
+ if (!lchan || !lchan->fi)
+ return;
+ struct osmo_fsm_inst *fi = lchan->fi;
+ lchan->release_in_error = err;
+ lchan->error_cause = cause_rr;
+ lchan->deact_sacch = sacch_deact;
+
+ /* This would also happen later, but better to do this a sooner. */
+ if (lchan->mgw_endpoint_ci_bts) {
+ mgw_endpoint_ci_dlcx(lchan->mgw_endpoint_ci_bts);
+ lchan->mgw_endpoint_ci_bts = NULL;
+ }
+
+ /* States waiting for events will notice the desire to release when done waiting, so it is enough
+ * to mark for release. */
+ lchan->release_requested = true;
+
+ /* But when in error, shortcut that. */
+ if (lchan->release_in_error) {
+ switch (lchan->fi->state) {
+ default:
+ /* Normally we deact SACCH in lchan_fsm_wait_sapis_released_onenter(). When
+ * skipping that, but asked to SACCH deact, do it now. */
+ if (lchan->deact_sacch)
+ rsl_deact_sacch(lchan);
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_RF_RELEASE_ACK);
+ return;
+ case LCHAN_ST_WAIT_TS_READY:
+ lchan_fsm_state_chg(LCHAN_ST_UNUSED);
+ return;
+ case LCHAN_ST_WAIT_RF_RELEASE_ACK:
+ case LCHAN_ST_BORKEN:
+ return;
+ }
+ }
+
+ /* The only non-broken state that would stay stuck without noticing the release_requested flag
+ * is: */
+ if (fi->state == LCHAN_ST_ACTIVE)
+ lchan_fsm_state_chg(LCHAN_ST_WAIT_SAPIS_RELEASED);
+}
+
+void lchan_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
+{
+ GET_LCHAN(fi, lchan);
+ if (lchan->conn)
+ gscon_forgetx_lchan(lchan->conn, lchan);
+ lchan_reset(lchan);
+ if (lchan->last_error) {
+ talloc_free(lchan->last_error);
+ lchan->last_error = NULL;
+ }
+ lchan->fi = NULL;
+}
+
+/* The mgw_endpoint was invalidated, just and simply forget the pointer without cleanup. */
+void lchan_forgetx_mgw_endpoint(struct gsm_lchan *lchan)
+{
+ if (!lchan)
+ return;
+ lchan->mgw_endpoint_ci_bts = NULL;
+}
+
+/* The conn is deallocating, just forget all about it */
+void lchan_forgetx_conn(struct gsm_lchan *lchan)
+{
+ if (!lchan)
+ return;
+ lchan_forgetx_mgw_endpoint(lchan);
+ lchan->conn = NULL;
+}
+
+static struct osmo_fsm lchan_fsm = {
+ .name = "lchan",
+ .states = lchan_fsm_states,
+ .num_states = ARRAY_SIZE(lchan_fsm_states),
+ .log_subsys = DRSL,
+ .event_names = lchan_fsm_event_names,
+ .allstate_action = lchan_fsm_allstate_action,
+ .allstate_event_mask = 0
+ | S(LCHAN_EV_TS_ERROR)
+ | S(LCHAN_EV_MGW_ENDPOINT_ERROR)
+ ,
+ .timer_cb = lchan_fsm_timer_cb,
+ .cleanup = lchan_fsm_cleanup,
+};
diff --git a/src/osmo-bsc/lchan_select.c b/src/osmo-bsc/lchan_select.c
new file mode 100644
index 000000000..29a6a2fc7
--- /dev/null
+++ b/src/osmo-bsc/lchan_select.c
@@ -0,0 +1,260 @@
+/* Select a suitable lchan from a given cell.
+ *
+ * (C) 2008 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/bsc/debug.h>
+
+#include <osmocom/bsc/gsm_data.h>
+#include <osmocom/bsc/timeslot_fsm.h>
+#include <osmocom/bsc/lchan_fsm.h>
+
+#include <osmocom/bsc/lchan_select.h>
+
+static struct gsm_lchan *
+_lc_find_trx(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan,
+ enum gsm_phys_chan_config as_pchan)
+{
+ struct gsm_lchan *lchan;
+ struct gsm_bts_trx_ts *ts;
+ int j, start, stop, dir;
+
+#define LOGPLCHANALLOC(fmt, args...) \
+ LOGP(DRLL, LOGL_DEBUG, "looking for lchan %s%s%s: " fmt, \
+ gsm_pchan_name(pchan), \
+ pchan == as_pchan ? "" : " as ", \
+ pchan == as_pchan ? "" : gsm_pchan_name(as_pchan), ## args)
+
+ if (!trx_is_usable(trx)) {
+ LOGPLCHANALLOC("%s trx not usable\n", gsm_trx_name(trx));
+ return NULL;
+ }
+
+ if (trx->bts->chan_alloc_reverse) {
+ /* check TS 7..0 */
+ start = 7;
+ stop = -1;
+ dir = -1;
+ } else {
+ /* check TS 0..7 */
+ start = 0;
+ stop = 8;
+ dir = 1;
+ }
+
+ for (j = start; j != stop; j += dir) {
+ ts = &trx->ts[j];
+ if (!ts_is_usable(ts))
+ continue;
+ /* The caller first selects what kind of TS to search in, e.g. looking for exact
+ * GSM_PCHAN_TCH_F, or maybe among dynamic GSM_PCHAN_TCH_F_TCH_H_PDCH... */
+ if (ts->pchan_on_init != pchan) {
+ LOGPLCHANALLOC("%s is != %s\n", gsm_ts_and_pchan_name(ts),
+ gsm_pchan_name(pchan));
+ continue;
+ }
+ /* Next, is this timeslot in or can it be switched to the pchan we want to use it for? */
+ if (!ts_usable_as_pchan(ts, as_pchan)) {
+ LOGPLCHANALLOC("%s is not usable as %s\n", gsm_ts_and_pchan_name(ts),
+ gsm_pchan_name(as_pchan));
+ continue;
+ }
+
+ /* TS is (going to be) in desired pchan mode. Go ahead and check for an available lchan. */
+ ts_as_pchan_for_each_lchan(lchan, ts, as_pchan) {
+ if (lchan->fi->state == LCHAN_ST_UNUSED) {
+ LOGPLCHANALLOC("%s ss=%d is available%s\n",
+ gsm_ts_and_pchan_name(ts), lchan->nr,
+ ts->pchan_is != as_pchan ? " after dyn PCHAN change" : "");
+ return lchan;
+ }
+ LOGPLCHANALLOC("%s ss=%d in type=%s,state=%s not suitable\n",
+ gsm_ts_and_pchan_name(ts), lchan->nr,
+ gsm_lchant_name(lchan->type),
+ osmo_fsm_inst_state_name(lchan->fi));
+ }
+ }
+
+ return NULL;
+#undef LOGPLCHANALLOC
+}
+
+static struct gsm_lchan *
+_lc_dyn_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan,
+ enum gsm_phys_chan_config dyn_as_pchan)
+{
+ struct gsm_bts_trx *trx;
+ struct gsm_lchan *lc;
+
+ if (bts->chan_alloc_reverse) {
+ llist_for_each_entry_reverse(trx, &bts->trx_list, list) {
+ lc = _lc_find_trx(trx, pchan, dyn_as_pchan);
+ if (lc)
+ return lc;
+ }
+ } else {
+ llist_for_each_entry(trx, &bts->trx_list, list) {
+ lc = _lc_find_trx(trx, pchan, dyn_as_pchan);
+ if (lc)
+ return lc;
+ }
+ }
+
+ return NULL;
+}
+
+static struct gsm_lchan *
+_lc_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
+{
+ return _lc_dyn_find_bts(bts, pchan, pchan);
+}
+
+struct gsm_lchan *lchan_select_by_chan_mode(struct gsm_bts *bts,
+ enum gsm48_chan_mode chan_mode, bool full_rate)
+{
+ enum gsm_chan_t type;
+
+ switch (chan_mode) {
+ case GSM48_CMODE_SIGN:
+ type = GSM_LCHAN_SDCCH;
+ break;
+ case GSM48_CMODE_SPEECH_V1:
+ case GSM48_CMODE_SPEECH_EFR:
+ case GSM48_CMODE_SPEECH_AMR:
+ type = full_rate ? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H;
+ break;
+ default:
+ return NULL;
+ }
+
+ return lchan_select_by_type(bts, type);
+}
+
+/* Return a matching lchan from a specific BTS that is currently available. The next logical step is
+ * lchan_activate() on it, which would possibly cause dynamic timeslot pchan switching, taken care of by
+ * the lchan and timeslot FSMs. */
+struct gsm_lchan *lchan_select_by_type(struct gsm_bts *bts, enum gsm_chan_t type)
+{
+ struct gsm_lchan *lchan = NULL;
+ enum gsm_phys_chan_config first, first_cbch, second, second_cbch;
+
+ LOGP(DRLL, LOGL_DEBUG, "(bts=%d) lchan_select_by_type(%s)\n", bts->nr, gsm_lchant_name(type));
+
+ switch (type) {
+ case GSM_LCHAN_SDCCH:
+ if (bts->chan_alloc_reverse) {
+ first = GSM_PCHAN_SDCCH8_SACCH8C;
+ first_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH;
+ second = GSM_PCHAN_CCCH_SDCCH4;
+ second_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH;
+ } else {
+ first = GSM_PCHAN_CCCH_SDCCH4;
+ first_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH;
+ second = GSM_PCHAN_SDCCH8_SACCH8C;
+ second_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH;
+ }
+
+ lchan = _lc_find_bts(bts, first);
+ if (lchan == NULL)
+ lchan = _lc_find_bts(bts, first_cbch);
+ if (lchan == NULL)
+ lchan = _lc_find_bts(bts, second);
+ if (lchan == NULL)
+ lchan = _lc_find_bts(bts, second_cbch);
+ break;
+ case GSM_LCHAN_TCH_F:
+ lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F);
+ /* If we don't have TCH/F available, fall-back to TCH/H */
+ if (!lchan) {
+ lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H);
+ if (lchan)
+ type = GSM_LCHAN_TCH_H;
+ }
+ /* If we don't have TCH/H either, try dynamic TCH/F_PDCH */
+ if (!lchan) {
+ lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH,
+ GSM_PCHAN_TCH_F);
+ /* TCH/F_PDCH used as TCH/F -- here, type is already
+ * set to GSM_LCHAN_TCH_F, but for clarity's sake... */
+ if (lchan)
+ type = GSM_LCHAN_TCH_F;
+ }
+
+ /* Try fully dynamic TCH/F_TCH/H_PDCH as TCH/F... */
+ if (!lchan && bts->network->dyn_ts_allow_tch_f) {
+ lchan = _lc_dyn_find_bts(bts,
+ GSM_PCHAN_TCH_F_TCH_H_PDCH,
+ GSM_PCHAN_TCH_F);
+ if (lchan)
+ type = GSM_LCHAN_TCH_F;
+ }
+ /* ...and as TCH/H. */
+ if (!lchan) {
+ lchan = _lc_dyn_find_bts(bts,
+ GSM_PCHAN_TCH_F_TCH_H_PDCH,
+ GSM_PCHAN_TCH_H);
+ if (lchan)
+ type = GSM_LCHAN_TCH_H;
+ }
+ break;
+ case GSM_LCHAN_TCH_H:
+ lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H);
+ /* If we don't have TCH/H available, fall-back to TCH/F */
+ if (!lchan) {
+ lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F);
+ if (lchan)
+ type = GSM_LCHAN_TCH_F;
+ }
+ /* No dedicated TCH/x available -- try fully dynamic
+ * TCH/F_TCH/H_PDCH */
+ if (!lchan) {
+ lchan = _lc_dyn_find_bts(bts,
+ GSM_PCHAN_TCH_F_TCH_H_PDCH,
+ GSM_PCHAN_TCH_H);
+ if (lchan)
+ type = GSM_LCHAN_TCH_H;
+ }
+ /*
+ * No need to check TCH/F_TCH/H_PDCH channels for TCH/F:
+ * if no TCH/H was available, neither will be TCH/F.
+ */
+ /* If we don't have TCH/F either, try dynamic TCH/F_PDCH */
+ if (!lchan) {
+ lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH,
+ GSM_PCHAN_TCH_F);
+ if (lchan)
+ type = GSM_LCHAN_TCH_F;
+ }
+ break;
+ default:
+ LOGP(DRLL, LOGL_ERROR, "Unknown gsm_chan_t %u\n", type);
+ }
+
+ if (lchan) {
+ lchan->type = type;
+ LOG_LCHAN(lchan, LOGL_INFO, "Selected");
+ } else
+ LOGP(DRLL, LOGL_ERROR, "(bts=%d) Failed to select %s channel\n",
+ bts->nr, gsm_lchant_name(type));
+
+ return lchan;
+}
+
diff --git a/src/osmo-bsc/meas_feed.c b/src/osmo-bsc/meas_feed.c
index 2e80754d4..8450f69ca 100644
--- a/src/osmo-bsc/meas_feed.c
+++ b/src/osmo-bsc/meas_feed.c
@@ -75,7 +75,7 @@ static int process_meas_rep(struct gsm_meas_rep *mr)
/* copy channel information */
/* we assume that the measurement report always belong to some timeslot */
mfm->lchan_type = (uint8_t)mr->lchan->type;
- mfm->pchan_type = (uint8_t)mr->lchan->ts->pchan;
+ mfm->pchan_type = (uint8_t)mr->lchan->ts->pchan_is;
mfm->bts_nr = mr->lchan->ts->trx->bts->nr;
mfm->trx_nr = mr->lchan->ts->trx->nr;
mfm->ts_nr = mr->lchan->ts->nr;
diff --git a/src/osmo-bsc/mgw_endpoint_fsm.c b/src/osmo-bsc/mgw_endpoint_fsm.c
new file mode 100644
index 000000000..c3b3f40fb
--- /dev/null
+++ b/src/osmo-bsc/mgw_endpoint_fsm.c
@@ -0,0 +1,728 @@
+/* osmo-bsc API to manage all sides of an MGW endpoint
+ *
+ * (C) 2017 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * Author: Neels Hofmeyr <neels@hofmeyr.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <osmocom/core/fsm.h>
+#include <osmocom/core/byteswap.h>
+
+#include <osmocom/bsc/debug.h>
+#include <osmocom/bsc/gsm_timers.h>
+
+#include <osmocom/bsc/mgw_endpoint_fsm.h>
+
+#define LOG_CI(ci, level, fmt, args...) do { \
+ if (!ci || !ci->mgwep) \
+ LOGP(DLGLOBAL, level, "(unknown MGW endpoint) " fmt, ## args); \
+ else \
+ LOG_MGWEP(ci->mgwep, level, "CI[%d] %s%s%s: " fmt, \
+ (int)(ci - ci->mgwep->ci), \
+ ci->label ? : "-", \
+ ci->mgcp_ci_str[0] ? " CI=" : "", \
+ ci->mgcp_ci_str[0] ? ci->mgcp_ci_str : "", \
+ ## args); \
+ } while(0)
+
+#define FIRST_CI_EVENT (_MGWEP_EV_LAST + (_MGWEP_EV_LAST & 1)) /* rounded up to even nr */
+#define USABLE_CI ((32 - FIRST_CI_EVENT)/2)
+#define EV_TO_CI_IDX(event) ((event - FIRST_CI_EVENT) / 2)
+
+#define CI_EV_SUCCESS(ci) (FIRST_CI_EVENT + (((ci) - ci->mgwep->ci) * 2))
+#define CI_EV_FAILURE(ci) (CI_EV_SUCCESS(ci) + 1)
+
+static struct osmo_fsm mgwep_fsm;
+
+struct mgwep_ci {
+ struct mgw_endpoint *mgwep;
+
+ bool occupied;
+ char label[64];
+ struct osmo_fsm_inst *mgcp_client_fi;
+
+ bool pending;
+ bool sent;
+ enum mgcp_verb verb;
+ struct mgcp_conn_peer verb_info;
+ struct osmo_fsm_inst *notify;
+ uint32_t notify_success;
+ uint32_t notify_failure;
+ void *notify_data;
+
+ bool got_port_info;
+ struct mgcp_conn_peer rtp_info;
+ char mgcp_ci_str[MGCP_CONN_ID_LENGTH];
+};
+
+struct mgw_endpoint {
+ struct mgcp_client *mgcp_client;
+ struct osmo_fsm_inst *fi;
+ char endpoint[MGCP_ENDPOINT_MAXLEN];
+
+ struct mgwep_ci ci[USABLE_CI];
+};
+
+static const struct value_string mgcp_verb_names[] = {
+ { MGCP_VERB_CRCX, "CRCX" },
+ { MGCP_VERB_MDCX, "MDCX" },
+ { MGCP_VERB_DLCX, "DLCX" },
+ { MGCP_VERB_AUEP, "AUEP" },
+ { MGCP_VERB_RSIP, "RSIP" },
+ {}
+};
+
+static inline const char *mgcp_verb_name(enum mgcp_verb val)
+{ return get_value_string(mgcp_verb_names, val); }
+
+static struct mgwep_ci *mgwep_check_ci(struct mgwep_ci *ci)
+{
+ if (!ci)
+ return NULL;
+ if (!ci->mgwep)
+ return NULL;
+ if (ci < ci->mgwep->ci || ci >= &ci->mgwep->ci[USABLE_CI])
+ return NULL;
+ return ci;
+}
+
+static struct mgwep_ci *mgwep_ci_for_event(struct mgw_endpoint *mgwep, uint32_t event)
+{
+ int idx;
+ if (event < FIRST_CI_EVENT)
+ return NULL;
+ idx = EV_TO_CI_IDX(event);
+ if (idx >= sizeof(mgwep->ci))
+ return NULL;
+ return mgwep_check_ci(&mgwep->ci[idx]);
+}
+
+const char *mgw_endpoint_name(const struct mgw_endpoint *mgwep)
+{
+ if (!mgwep)
+ return "NULL";
+ if (mgwep->endpoint[0])
+ return mgwep->endpoint;
+ return osmo_fsm_inst_name(mgwep->fi);
+}
+
+const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info)
+{
+ static char buf[128];
+
+ if (!info)
+ return "NULL";
+
+ if (info->endpoint[0]
+ && info->addr[0])
+ snprintf(buf, sizeof(buf), "%s:%s:%u",
+ info->endpoint, info->addr, info->port);
+ else if (info->endpoint[0])
+ snprintf(buf, sizeof(buf), "%s", info->endpoint);
+ else if (info->addr[0])
+ snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
+ else
+ return "empty";
+ return buf;
+}
+
+const char *mgwep_ci_name(const struct mgwep_ci *ci)
+{
+ const struct mgcp_conn_peer *rtp_info;
+
+ if (!ci)
+ return "NULL";
+
+ rtp_info = mgwep_ci_get_rtp_info(ci);
+
+ if (rtp_info)
+ return mgcp_conn_peer_name(rtp_info);
+ return mgw_endpoint_name(ci->mgwep);
+}
+
+static struct value_string mgwep_fsm_event_names[33] = {};
+
+static char mgwep_fsm_event_name_bufs[32][32] = {};
+
+static void fill_event_names()
+{
+ int i;
+ for (i = 0; i < (ARRAY_SIZE(mgwep_fsm_event_names) - 1); i++) {
+ if (i < _MGWEP_EV_LAST)
+ continue;
+ if (i < FIRST_CI_EVENT || EV_TO_CI_IDX(i) > USABLE_CI) {
+ mgwep_fsm_event_names[i] = (struct value_string){i, "Unused"};
+ continue;
+ }
+ snprintf(mgwep_fsm_event_name_bufs[i], sizeof(mgwep_fsm_event_name_bufs[i]),
+ "MGW Response for CI #%d", EV_TO_CI_IDX(i));
+ mgwep_fsm_event_names[i] = (struct value_string){i, mgwep_fsm_event_name_bufs[i]};
+ }
+}
+
+static struct T_def *g_T_defs = NULL;
+
+void mgw_endpoint_fsm_init(struct T_def *T_defs)
+{
+ static bool g_initialized = false;
+ g_T_defs = T_defs;
+ if (!g_initialized) {
+ OSMO_ASSERT(osmo_fsm_register(&mgwep_fsm) == 0);
+ fill_event_names();
+ g_initialized = true;
+ }
+}
+
+/* From local var fi->priv, define local var mgewp. */
+#define GET_MGWEP() \
+ struct mgw_endpoint *mgwep = fi->priv; \
+ OSMO_ASSERT((fi)->fsm == &mgwep_fsm && (fi)->priv)
+
+struct mgw_endpoint *mgw_endpoint_alloc(struct osmo_fsm_inst *parent, uint32_t parent_term_event,
+ struct mgcp_client *mgcp_client,
+ const char *fsm_id,
+ const char *endpoint_str_fmt, ...)
+{
+ va_list ap;
+ struct osmo_fsm_inst *fi;
+ struct mgw_endpoint *mgwep;
+ int rc;
+
+ if (!mgcp_client)
+ return NULL;
+
+ /* use mgcp_client as talloc ctx, so that the conn, lchan, ts can deallocate while MGCP DLCX are
+ * still going on. */
+ fi = osmo_fsm_inst_alloc_child(&mgwep_fsm, parent, parent_term_event);
+ OSMO_ASSERT(fi);
+
+ osmo_fsm_inst_update_id(fi, fsm_id);
+
+ mgwep = talloc_zero(fi, struct mgw_endpoint);
+ OSMO_ASSERT(mgwep);
+
+ va_start(ap, endpoint_str_fmt);
+ rc = snprintf(mgwep->endpoint, sizeof(mgwep->endpoint), endpoint_str_fmt, ap);
+ va_end(ap);
+
+ if (rc <= 0 || rc >= sizeof(mgwep->endpoint)) {
+ LOG_MGWEP(mgwep, LOGL_ERROR, "Endpoint name too long, truncated: %s",
+ mgwep->endpoint);
+ osmo_fsm_inst_term(mgwep->fi, OSMO_FSM_TERM_ERROR, 0);
+ return NULL;
+ }
+
+ mgwep->mgcp_client = mgcp_client;
+ mgwep->fi = fi;
+ mgwep->fi->priv = mgwep;
+ return mgwep;
+}
+
+struct mgwep_ci *mgw_endpoint_ci_add(struct mgw_endpoint *mgwep,
+ const char *label_fmt, ...)
+{
+ va_list ap;
+ int i;
+ struct mgwep_ci *ci;
+
+ for (i = 0; i < USABLE_CI; i++) {
+ ci = &mgwep->ci[i];
+
+ if (ci->occupied || ci->mgcp_client_fi)
+ continue;
+
+ *ci = (struct mgwep_ci){
+ .mgwep = mgwep,
+ .occupied = true,
+ };
+ if (label_fmt) {
+ va_start(ap, label_fmt);
+ vsnprintf(ci->label, sizeof(ci->label), label_fmt, ap);
+ va_end(ap);
+ }
+ return ci;
+ }
+
+ LOG_MGWEP(mgwep, LOGL_ERROR,
+ "Cannot allocate another endpoint, all "
+ OSMO_STRINGIFY_VAL(USABLE_CI) " are in use");
+
+ return NULL;
+}
+
+static void mgwep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi);
+
+static void on_failure(struct mgwep_ci *ci)
+{
+ if (!ci->occupied)
+ return;
+
+ if (ci->notify)
+ osmo_fsm_inst_dispatch(ci->notify, ci->notify_failure, ci->notify_data);
+
+ *ci = (struct mgwep_ci){
+ .mgwep = ci->mgwep,
+ };
+
+
+ mgwep_fsm_check_state_chg_after_response(ci->mgwep->fi);
+}
+
+static void on_success(struct mgwep_ci *ci, void *data)
+{
+ struct mgcp_conn_peer *rtp_info;
+
+ if (!ci->occupied)
+ return;
+
+ ci->pending = false;
+
+ switch (ci->verb) {
+ case MGCP_VERB_CRCX:
+ /* If we sent a wildcarded endpoint name on CRCX, we need to store the resulting endpoint
+ * name here. Also, we receive the MGW's RTP port information. */
+ rtp_info = data;
+ OSMO_ASSERT(rtp_info);
+ ci->got_port_info = true;
+ ci->rtp_info = *rtp_info;
+ osmo_strlcpy(ci->mgcp_ci_str, mgcp_conn_get_ci(ci->mgcp_client_fi),
+ sizeof(ci->mgcp_ci_str));
+ if (rtp_info->endpoint[0]) {
+ int rc;
+ rc = osmo_strlcpy(ci->mgwep->endpoint, rtp_info->endpoint,
+ sizeof(ci->mgwep->endpoint));
+ if (rc <= 0 || rc >= sizeof(ci->mgwep->endpoint)) {
+ LOG_CI(ci, LOGL_ERROR, "Unable to copy endpoint name '%s'",
+ rtp_info->endpoint);
+ mgw_endpoint_ci_dlcx(ci);
+ on_failure(ci);
+ return;
+ }
+ }
+ break;
+
+ case MGCP_VERB_MDCX:
+ /* If an MDCX succeeded, remember the new port information */
+ ci->got_port_info = true;
+ ci->rtp_info = ci->verb_info;
+ break;
+
+ default:
+ break;
+ }
+
+ LOG_CI(ci, LOGL_DEBUG, "received successful response to %s RTP=%s%s",
+ mgcp_verb_name(ci->verb),
+ mgcp_conn_peer_name(ci->got_port_info? &ci->rtp_info : NULL),
+ ci->notify ? "" : " (not sending a notification)");
+
+ if (ci->notify)
+ osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data);
+
+ mgwep_fsm_check_state_chg_after_response(ci->mgwep->fi);
+}
+
+const struct mgcp_conn_peer *mgwep_ci_get_rtp_info(const struct mgwep_ci *ci)
+{
+ ci = mgwep_check_ci((struct mgwep_ci*)ci);
+ if (!ci)
+ return NULL;
+ if (!ci->got_port_info)
+ return NULL;
+ return &ci->rtp_info;
+}
+
+bool mgwep_ci_get_crcx_info_to_sockaddr(const struct mgwep_ci *ci, struct sockaddr_storage *dest)
+{
+ const struct mgcp_conn_peer *rtp_info;
+ struct sockaddr_in *sin;
+
+ rtp_info = mgwep_ci_get_rtp_info(ci);
+ if (!rtp_info)
+ return false;
+
+ sin = (struct sockaddr_in *)dest;
+
+ sin->sin_family = AF_INET;
+ sin->sin_addr.s_addr = inet_addr(rtp_info->addr);
+ sin->sin_port = osmo_ntohs(rtp_info->port);
+ return true;
+}
+
+
+struct state_timeout mgwep_fsm_timeouts[32] = {
+ [MGWEP_ST_WAIT_MGW_RESPONSE] = { .T=23042 },
+};
+
+/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
+ * The actual timeout value is in turn obtained from g_T_defs.
+ * Assumes local variable fi exists. */
+#define mgwep_fsm_state_chg(state) \
+ fsm_inst_state_chg_T(fi, state, mgwep_fsm_timeouts, g_T_defs, 5)
+
+void mgw_endpoint_ci_request(struct mgwep_ci *ci,
+ enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info,
+ struct osmo_fsm_inst *notify,
+ uint32_t event_success, uint32_t event_failure,
+ void *notify_data)
+{
+ struct mgw_endpoint *mgwep;
+ struct osmo_fsm_inst *fi;
+ struct mgwep_ci cleared_ci;
+ ci = mgwep_check_ci(ci);
+
+ if (!ci) {
+ LOGP(DLGLOBAL, LOGL_ERROR, "Invalid MGW endpoint request: no ci\n");
+ goto dispatch_error;
+ }
+ if (!verb_info && verb != MGCP_VERB_DLCX) {
+ LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: missing verb details for %s",
+ mgcp_verb_name(verb));
+ goto dispatch_error;
+ }
+ if ((verb < 0) || (verb > MGCP_VERB_RSIP)) {
+ LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: unknown verb: %s",
+ mgcp_verb_name(verb));
+ goto dispatch_error;
+ }
+
+ mgwep = ci->mgwep;
+ fi = mgwep->fi;
+
+ /* Clear volatile state by explicitly keeping those that should remain. Because we can't assign
+ * the char[] directly, dance through cleared_ci and copy back. */
+ cleared_ci = (struct mgwep_ci){
+ .mgwep = mgwep,
+ .mgcp_client_fi = ci->mgcp_client_fi,
+ .got_port_info = ci->got_port_info,
+ .rtp_info = ci->rtp_info,
+
+ .occupied = true,
+ /* .pending = true follows below */
+ .verb = verb,
+ .notify = notify,
+ .notify_success = event_success,
+ .notify_failure = event_failure,
+ .notify_data = notify_data,
+ };
+ osmo_strlcpy(cleared_ci.label, ci->label, sizeof(cleared_ci.label));
+ osmo_strlcpy(cleared_ci.mgcp_ci_str, ci->mgcp_ci_str, sizeof(cleared_ci.mgcp_ci_str));
+ *ci = cleared_ci;
+
+ LOG_CI(ci, LOGL_ERROR, "%s, notify=%p %s", mgcp_verb_name(ci->verb), ci->notify, ci->notify ?
+ ci->notify->id : "");
+
+ if (verb_info)
+ ci->verb_info = *verb_info;
+
+ if (mgwep->endpoint[0]) {
+ if (ci->verb_info.endpoint[0] && strcmp(ci->verb_info.endpoint, mgwep->endpoint))
+ LOG_CI(ci, LOGL_ERROR,
+ "Warning: Requested %s on endpoint %s, but this CI is on endpoint %s."
+ " Using the proper endpoint instead.",
+ mgcp_verb_name(verb), ci->verb_info.endpoint, mgwep->endpoint);
+ osmo_strlcpy(ci->verb_info.endpoint, mgwep->endpoint, sizeof(ci->verb_info.endpoint));
+ }
+
+ switch (ci->verb) {
+ case MGCP_VERB_CRCX:
+ if (ci->mgcp_client_fi) {
+ LOG_CI(ci, LOGL_ERROR, "CRCX can be called only once per MGW endpoint CI");
+ on_failure(ci);
+ return;
+ }
+ break;
+
+ case MGCP_VERB_MDCX:
+ case MGCP_VERB_DLCX:
+ if (!ci->mgcp_client_fi) {
+ LOG_CI(ci, LOGL_ERROR, "The first verb on an unused MGW endpoint CI must be CRCX, not %s",
+ mgcp_verb_name(ci->verb));
+ on_failure(ci);
+ return;
+ }
+ break;
+
+ default:
+ LOG_CI(ci, LOGL_ERROR, "This verb is not supported: %s", mgcp_verb_name(ci->verb));
+ on_failure(ci);
+ return;
+ }
+
+ ci->pending = true;
+
+ LOG_CI(ci, LOGL_DEBUG, "Scheduling MGCP: %s (%s:%u)",
+ mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port);
+
+ if (mgwep->fi->state != MGWEP_ST_WAIT_MGW_RESPONSE)
+ mgwep_fsm_state_chg(MGWEP_ST_WAIT_MGW_RESPONSE);
+
+ return;
+dispatch_error:
+ if (notify)
+ osmo_fsm_inst_dispatch(notify, event_failure, notify_data);
+}
+
+static int send_verb(struct mgwep_ci *ci)
+{
+ int rc;
+ struct mgw_endpoint *mgwep = ci->mgwep;
+
+ if (!ci->occupied || !ci->pending || ci->sent)
+ return 0;
+
+ switch (ci->verb) {
+
+ case MGCP_VERB_CRCX:
+ OSMO_ASSERT(!ci->mgcp_client_fi);
+ LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s:%u",
+ mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port);
+ ci->mgcp_client_fi = mgcp_conn_create(mgwep->mgcp_client, mgwep->fi,
+ CI_EV_FAILURE(ci), CI_EV_SUCCESS(ci),
+ &ci->verb_info);
+ ci->sent = true;
+ if (!ci->mgcp_client_fi){
+ LOG_CI(ci, LOGL_ERROR, "Error sending MGCP: %s %s:%u (rc=%d %s)",
+ mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port,
+ rc, strerror(-rc));
+ on_failure(ci);
+ }
+ osmo_fsm_inst_update_id(ci->mgcp_client_fi, ci->label);
+ break;
+
+ case MGCP_VERB_MDCX:
+ OSMO_ASSERT(ci->mgcp_client_fi);
+ LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s %s:%u",
+ mgcp_verb_name(ci->verb), ci->mgcp_ci_str, ci->verb_info.addr, ci->verb_info.port);
+ rc = mgcp_conn_modify(ci->mgcp_client_fi, CI_EV_SUCCESS(ci), &ci->verb_info);
+ ci->sent = true;
+ if (rc) {
+ LOG_CI(ci, LOGL_ERROR, "Error sending MGCP: %s %s:%u (rc=%d %s)",
+ mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port,
+ rc, strerror(-rc));
+ on_failure(ci);
+ }
+ break;
+
+ case MGCP_VERB_DLCX:
+ LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s",
+ mgcp_verb_name(ci->verb), ci->mgcp_ci_str);
+ /* The way this is designed, we actually need to forget all about the ci right away. */
+ mgcp_conn_delete(ci->mgcp_client_fi);
+ if (ci->notify)
+ osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data);
+ *ci = (struct mgwep_ci){
+ .mgwep = mgwep,
+ };
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+
+ return 1;
+}
+
+void mgw_endpoint_clear(struct mgw_endpoint *mgwep)
+{
+ if (!mgwep)
+ return;
+ osmo_fsm_inst_term(mgwep->fi, OSMO_FSM_TERM_REGULAR, 0);
+}
+
+/*
+
+mgwep_st_unused#MGWEP_ST_UNUSED
+mgwep_st_wait_mgw_response#MGWEP_ST_WAIT_MGW_RESPONSE
+mgwep_st_in_use#MGWEP_ST_IN_USE
+
+
+:s/\(.*\)_st_\(.*\)#\(.*_ST_\(.*\)\)/static void \1_fsm_\2_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)\r{\r\tGET_MGWEP();\r}\r\rstatic void \1_fsm_\2(struct osmo_fsm_inst *fi, uint32_t event, void *data)\r{\r\tGET_MGWEP();\r\tswitch (event) {\r\tdefault:\r\t\tOSMO_ASSERT(false);\r\t}\r}\r
+
+
+
+
+:s/\(.*\)_st_\(.*\)#\(MGWEP_ST_\(.*\)\)/\t[\3] = {\r\t\t.name = "\4",\r\t\t.onenter = \1_fsm_\2_onenter,\r\t\t.action = \1_fsm_\2,\r\t\t.in_event_mask = 0\r\t\t\t| S()\r\t\t\t,\r\t\t.out_state_mask = 0\r\t\t\t| S()\r\t\t\t,\r\t},
+
+
+*/
+
+static void mgwep_count(struct mgw_endpoint *mgwep, int *occupied, int *pending_not_sent,
+ int *waiting_for_response)
+{
+ int i;
+
+ if (occupied)
+ *occupied = 0;
+
+ if (pending_not_sent)
+ *pending_not_sent = 0;
+
+ if (waiting_for_response)
+ *waiting_for_response = 0;
+
+ for (i = 0; i < ARRAY_SIZE(mgwep->ci); i++) {
+ struct mgwep_ci *ci = &mgwep->ci[i];
+ if (ci->occupied) {
+ if (occupied)
+ (*occupied)++;
+ } else
+ continue;
+
+ if (ci->pending)
+ LOG_CI(ci, LOGL_DEBUG, "verb=%s, %s",
+ mgcp_verb_name(ci->verb),
+ ci->sent ? "waiting for response" : "waiting to be sent");
+ else
+ LOG_CI(ci, LOGL_DEBUG, "%s",
+ mgcp_conn_peer_name(mgwep_ci_get_rtp_info(ci)));
+
+ if (ci->pending && ci->sent)
+ if (waiting_for_response)
+ (*waiting_for_response)++;
+ if (ci->pending && !ci->sent)
+ if (pending_not_sent)
+ (*pending_not_sent)++;
+ }
+}
+
+static void mgwep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi)
+{
+ int waiting_for_response;
+ int occupied;
+ GET_MGWEP();
+
+ mgwep_count(mgwep, &occupied, NULL, &waiting_for_response);
+ LOG_MGWEP(mgwep, LOGL_DEBUG, "CI in use: %d, waiting for response: %d", occupied, waiting_for_response);
+
+ if (!occupied) {
+ mgwep_fsm_state_chg(MGWEP_ST_UNUSED);
+ return;
+ }
+
+ if (!waiting_for_response) {
+ if (fi->state != MGWEP_ST_IN_USE)
+ mgwep_fsm_state_chg(MGWEP_ST_IN_USE);
+ return;
+ }
+
+}
+
+static void mgwep_fsm_wait_mgw_response_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int count = 0;
+ int i;
+ GET_MGWEP();
+
+ for (i = 0; i < ARRAY_SIZE(mgwep->ci); i++) {
+ count += send_verb(&mgwep->ci[i]);
+ }
+
+ LOG_MGWEP(mgwep, LOGL_DEBUG, "Sent messages: %d", count);
+ mgwep_fsm_check_state_chg_after_response(fi);
+
+}
+
+static void mgwep_fsm_handle_ci_events(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ struct mgwep_ci *ci;
+ GET_MGWEP();
+ ci = mgwep_ci_for_event(mgwep, event);
+ if (ci) {
+ if (event == CI_EV_SUCCESS(ci))
+ on_success(ci, data);
+ else
+ on_failure(ci);
+ }
+}
+
+static void mgwep_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int pending_not_sent;
+ GET_MGWEP();
+
+ mgwep_count(mgwep, NULL, &pending_not_sent, NULL);
+ if (pending_not_sent)
+ mgwep_fsm_state_chg(MGWEP_ST_WAIT_MGW_RESPONSE);
+}
+
+#define S(x) (1 << (x))
+
+static const struct osmo_fsm_state mgwep_fsm_states[] = {
+ [MGWEP_ST_UNUSED] = {
+ .name = "UNUSED",
+ .in_event_mask = 0,
+ .out_state_mask = 0
+ | S(MGWEP_ST_WAIT_MGW_RESPONSE)
+ ,
+ },
+ [MGWEP_ST_WAIT_MGW_RESPONSE] = {
+ .name = "WAIT_MGW_RESPONSE",
+ .onenter = mgwep_fsm_wait_mgw_response_onenter,
+ .action = mgwep_fsm_handle_ci_events,
+ .in_event_mask = 0xffffffff,
+ .out_state_mask = 0
+ | S(MGWEP_ST_IN_USE)
+ | S(MGWEP_ST_UNUSED)
+ ,
+ },
+ [MGWEP_ST_IN_USE] = {
+ .name = "IN_USE",
+ .onenter = mgwep_fsm_in_use_onenter,
+ .action = mgwep_fsm_handle_ci_events,
+ .in_event_mask = 0xffffffff, /* mgcp_client_fsm may send parent term anytime */
+ .out_state_mask = 0
+ | S(MGWEP_ST_WAIT_MGW_RESPONSE)
+ ,
+ },
+};
+
+int mgwep_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+ int i;
+ GET_MGWEP();
+
+ switch (fi->T) {
+ default:
+ for (i = 0; i < ARRAY_SIZE(mgwep->ci); i++) {
+ struct mgwep_ci *ci = &mgwep->ci[i];
+ if (!ci->occupied)
+ continue;
+ if (!(ci->pending && ci->sent))
+ continue;
+ on_failure(ci);
+ }
+ return 0;
+ }
+
+ return 0;
+}
+
+static struct osmo_fsm mgwep_fsm = {
+ .name = "mgw-endpoint",
+ .states = mgwep_fsm_states,
+ .num_states = ARRAY_SIZE(mgwep_fsm_states),
+ .log_subsys = DRSL,
+ .event_names = mgwep_fsm_event_names,
+ .timer_cb = mgwep_fsm_timer_cb,
+ /* The FSM termination will automatically trigger any mgcp_client_fsm instances to DLCX. */
+};
diff --git a/src/osmo-bsc/neighbor_ident_vty.c b/src/osmo-bsc/neighbor_ident_vty.c
index 5b659fdb1..73d701c44 100644
--- a/src/osmo-bsc/neighbor_ident_vty.c
+++ b/src/osmo-bsc/neighbor_ident_vty.c
@@ -36,14 +36,26 @@ static struct neighbor_ident_list *g_neighbor_cells = NULL;
/* Parse VTY parameters matching NEIGHBOR_IDENT_VTY_KEY_PARAMS. Pass a pointer so that argv[0] is the
* ARFCN value followed by the BSIC keyword and value. vty *must* reference a BTS_NODE. */
-bool neighbor_ident_vty_parse_key_params(struct vty *vty, const char **argv, struct neighbor_ident_key *key)
+bool neighbor_ident_vty_parse_key_params(struct vty *vty, const char **argv,
+ struct neighbor_ident_key *key)
{
struct gsm_bts *bts = vty->index;
+
+ OSMO_ASSERT(vty->node == BTS_NODE);
+ OSMO_ASSERT(bts);
+
+ return neighbor_ident_bts_parse_key_params(vty, bts, argv, key);
+}
+
+/* same as neighbor_ident_vty_parse_key_params() but pass an explicit bts, so it works on any node. */
+bool neighbor_ident_bts_parse_key_params(struct vty *vty, struct gsm_bts *bts, const char **argv,
+ struct neighbor_ident_key *key)
+{
const char *arfcn_str = argv[0];
const char *bsic_kind = argv[1];
const char *bsic_str = argv[2];
- OSMO_ASSERT(vty->node == BTS_NODE && bts);
+ OSMO_ASSERT(bts);
*key = (struct neighbor_ident_key){
.from_bts = bts->nr,
@@ -81,7 +93,7 @@ static struct gsm_bts *neighbor_ident_vty_parse_bts_nr(struct vty *vty, const ch
static struct gsm_bts *bts_by_cell_id(struct vty *vty, struct gsm0808_cell_id *cell_id)
{
- struct gsm_bts *bts = gsm_bts_by_cell_id(g_net, cell_id);
+ struct gsm_bts *bts = gsm_bts_by_cell_id(g_net, cell_id, 0);
if (!bts)
vty_out(vty, "%% No such BTS: %s%s\n", gsm0808_cell_id_name(cell_id), VTY_NEWLINE);
return bts;
@@ -250,7 +262,7 @@ static int add_remote_or_local_bts(struct vty *vty, const struct gsm0808_cell_id
}
/* Is there a local BTS that matches the cell_id? */
- local_neigh = gsm_bts_by_cell_id(g_net, cell_id);
+ local_neigh = gsm_bts_by_cell_id(g_net, cell_id, 0);
if (local_neigh) {
/* But do the advertised ARFCN and BSIC match as intended?
* The user may omit ARFCN and BSIC for local cells, but if they are provided,
diff --git a/src/osmo-bsc/net_init.c b/src/osmo-bsc/net_init.c
index db3d01c1c..d16281344 100644
--- a/src/osmo-bsc/net_init.c
+++ b/src/osmo-bsc/net_init.c
@@ -24,6 +24,32 @@
#include <osmocom/bsc/chan_alloc.h>
#include <osmocom/bsc/neighbor_ident.h>
+static struct T_def gsm_network_T_defs[] = {
+ { .T=7, .default_val=10, .desc="inter-BSC Handover MO, HO Required to HO Command" },
+ { .T=8, .default_val=10, .desc="inter-BSC Handover MO, HO Command to final Clear" },
+ { .T=10, .default_val=6, .desc="RR Assignment" },
+ { .T=101, .default_val=10, .desc="inter-BSC Handover MT, HO Request to HO Accept" },
+ { .T=3101, .default_val=3, .desc="RR Immediate Assignment" },
+ { .T=3103, .default_val=5, .desc="Handover" },
+ { .T=3105, .default_val=100, .unit=T_MS, .desc="Physical Information" },
+ { .T=3107, .default_val=5, .desc="(unused)" },
+ { .T=3109, .default_val=5, .desc="RSL SACCH deactivation" },
+ { .T=3111, .default_val=2, .desc="Wait time before RSL RF Channel Release" },
+ { .T=993111, .default_val=4, .desc="Wait time after lchan was released in error"
+ "(should be T3111 + 2s)" },
+ { .T=3113, .default_val=10, .desc="Paging"},
+ { .T=3115, .default_val=10, .desc="(unused)" },
+ { .T=3117, .default_val=10, .desc="(unused)" },
+ { .T=3119, .default_val=10, .desc="(unused)" },
+ { .T=3122, .default_val=GSM_T3122_DEFAULT, .desc="Wait time after RR Immediate Assignment Reject" },
+ { .T=3141, .default_val=10, .desc="(unused)" },
+ { .T=3212, .default_val=5, .unit=T_CUSTOM,
+ .desc="Periodic Location Update timer, sent to MS (1 = 6 minutes)" },
+ { .T=993210, .default_val=20, .desc="After L3 Complete, wait for MSC to confirm" },
+ { .T=999, .default_val=60, .desc="After Clear Request, wait for MSC to Clear Command (sanity)" },
+ {}
+};
+
/* Initialize the bare minimum of struct gsm_network, minimizing required dependencies.
* This part is shared among the thin programs in osmo-bsc/src/utils/.
* osmo-bsc requires further initialization that pulls in more dependencies (see bsc_network_init()). */
@@ -43,9 +69,6 @@ struct gsm_network *gsm_network_init(void *ctx)
/* Permit a compile-time default of A5/3 and A5/1 */
net->a5_encryption_mask = (1 << 3) | (1 << 1);
- /* Use 30 min periodic update interval as sane default */
- net->t3212 = 5;
-
INIT_LLIST_HEAD(&net->subscr_conns);
net->bsc_subscribers = talloc_zero(net, struct llist_head);
@@ -53,22 +76,9 @@ struct gsm_network *gsm_network_init(void *ctx)
INIT_LLIST_HEAD(&net->bts_list);
net->num_bts = 0;
- net->T3101 = GSM_T3101_DEFAULT;
- net->T3103 = GSM_T3103_DEFAULT;
- net->T3105 = GSM_T3105_DEFAULT;
- net->T3107 = GSM_T3107_DEFAULT;
- net->T3109 = GSM_T3109_DEFAULT;
- net->T3111 = GSM_T3111_DEFAULT;
- net->T3113 = GSM_T3113_DEFAULT;
- net->T3115 = GSM_T3115_DEFAULT;
- net->T3117 = GSM_T3117_DEFAULT;
- net->T3119 = GSM_T3119_DEFAULT;
- net->T3122 = GSM_T3122_DEFAULT;
- net->T3141 = GSM_T3141_DEFAULT;
- net->T10 = GSM_T10_DEFAULT;
- net->T7 = GSM_T7_DEFAULT;
- net->T8 = GSM_T8_DEFAULT;
- net->T101 = GSM_T101_DEFAULT;
+
+ net->T_defs = gsm_network_T_defs;
+ T_defs_reset(net->T_defs);
return net;
}
diff --git a/src/osmo-bsc/osmo_bsc_api.c b/src/osmo-bsc/osmo_bsc_api.c
index a86b4f99e..1fa7803aa 100644
--- a/src/osmo-bsc/osmo_bsc_api.c
+++ b/src/osmo-bsc/osmo_bsc_api.c
@@ -22,6 +22,7 @@
#include <osmocom/bsc/bsc_msc_data.h>
#include <osmocom/bsc/bsc_subscriber.h>
#include <osmocom/bsc/debug.h>
+#include <osmocom/bsc/paging.h>
#include <osmocom/bsc/gsm_04_80.h>
#include <osmocom/bsc/gsm_04_08_utils.h>
@@ -35,7 +36,7 @@
#include <osmocom/bsc/osmo_bsc_sigtran.h>
/* Check if we have a proper connection to the MSC */
-static bool msc_connected(struct gsm_subscriber_connection *conn)
+bool msc_connected(struct gsm_subscriber_connection *conn)
{
/* No subscriber conn at all */
if (!conn)
@@ -167,6 +168,42 @@ void bsc_cipher_mode_compl(struct gsm_subscriber_connection *conn, struct msgb *
msgb_free(resp);
}
+/* 9.2.5 CM service accept */
+int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
+{
+ struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 SERV ACK");
+ struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
+
+ msg->lchan = conn->lchan;
+
+ gh->proto_discr = GSM48_PDISC_MM;
+ gh->msg_type = GSM48_MT_MM_CM_SERV_ACC;
+
+ DEBUGP(DMM, "-> CM SERVICE ACK\n");
+
+ return gsm0808_submit_dtap(conn, msg, 0, 0);
+}
+
+#if 0
+no-one is using this
+/* 9.2.6 CM service reject */
+int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
+ enum gsm48_reject_value value)
+{
+ struct msgb *msg;
+
+ msg = gsm48_create_mm_serv_rej(value);
+ if (!msg) {
+ LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
+ return -1;
+ }
+
+ DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
+
+ return gsm0808_submit_dtap(conn, msg, 0, 0);
+}
+#endif
+
static void bsc_send_ussd_no_srv(struct gsm_subscriber_connection *conn,
struct msgb *msg, const char *text)
{
@@ -211,6 +248,101 @@ static void bsc_send_ussd_no_srv(struct gsm_subscriber_connection *conn,
bsc_send_ussd_release_complete(conn);
}
+static int is_cm_service_for_emerg(struct msgb *msg)
+{
+ struct gsm48_service_request *cm;
+ struct gsm48_hdr *gh = msgb_l3(msg);
+
+ if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*cm)) {
+ LOGP(DMSC, LOGL_ERROR, "CM ServiceRequest does not fit.\n");
+ return 0;
+ }
+
+ cm = (struct gsm48_service_request *) &gh->data[0];
+ return cm->cm_service_type == GSM48_CMSERV_EMERGENCY;
+}
+
+struct bsc_msc_data *bsc_find_msc(struct gsm_subscriber_connection *conn,
+ struct msgb *msg)
+{
+ struct gsm48_hdr *gh;
+ int8_t pdisc;
+ uint8_t mtype;
+ struct osmo_bsc_data *bsc;
+ struct bsc_msc_data *msc, *pag_msc;
+ struct bsc_subscr *subscr;
+ int is_emerg = 0;
+
+ bsc = conn->network->bsc_data;
+
+ if (msgb_l3len(msg) < sizeof(*gh)) {
+ LOGP(DMSC, LOGL_ERROR, "There is no GSM48 header here.\n");
+ return NULL;
+ }
+
+ gh = msgb_l3(msg);
+ pdisc = gsm48_hdr_pdisc(gh);
+ mtype = gsm48_hdr_msg_type(gh);
+
+ /*
+ * We are asked to select a MSC here but they are not equal. We
+ * want to respond to a paging request on the MSC where we got the
+ * request from. This is where we need to decide where this connection
+ * will go.
+ */
+ if (pdisc == GSM48_PDISC_RR && mtype == GSM48_MT_RR_PAG_RESP)
+ goto paging;
+ else if (pdisc == GSM48_PDISC_MM && mtype == GSM48_MT_MM_CM_SERV_REQ) {
+ is_emerg = is_cm_service_for_emerg(msg);
+ goto round_robin;
+ } else
+ goto round_robin;
+
+round_robin:
+ llist_for_each_entry(msc, &bsc->mscs, entry) {
+ if (!msc->is_authenticated)
+ continue;
+ if (!is_emerg && msc->type != MSC_CON_TYPE_NORMAL)
+ continue;
+ if (is_emerg && !msc->allow_emerg)
+ continue;
+
+ /* force round robin by moving it to the end */
+ llist_move_tail(&msc->entry, &bsc->mscs);
+ return msc;
+ }
+
+ return NULL;
+
+paging:
+ subscr = extract_sub(conn, msg);
+
+ if (!subscr) {
+ LOGP(DMSC, LOGL_ERROR, "Got paged but no subscriber found.\n");
+ return NULL;
+ }
+
+ pag_msc = paging_get_msc(conn_get_bts(conn), subscr);
+ bsc_subscr_put(subscr);
+
+ llist_for_each_entry(msc, &bsc->mscs, entry) {
+ if (msc != pag_msc)
+ continue;
+
+ /*
+ * We don't check if the MSC is connected. In case it
+ * is not the connection will be dropped.
+ */
+
+ /* force round robin by moving it to the end */
+ llist_move_tail(&msc->entry, &bsc->mscs);
+ return msc;
+ }
+
+ LOGP(DMSC, LOGL_ERROR, "Got paged but no request found.\n");
+ return NULL;
+}
+
/*! MS->MSC: New MM context with L3 payload. */
int bsc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg, uint16_t chosen_channel)
{
@@ -230,6 +362,61 @@ int bsc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg, uint1
return complete_layer3(conn, msg, msc);
}
+static int handle_page_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
+{
+ struct bsc_subscr *subscr = extract_sub(conn, msg);
+
+ if (!subscr) {
+ LOGP(DMSC, LOGL_ERROR, "Non active subscriber got paged.\n");
+ return -1;
+ }
+
+ paging_request_stop(&conn->network->bts_list, conn_get_bts(conn), subscr, conn,
+ msg);
+ bsc_subscr_put(subscr);
+ return 0;
+}
+
+static void handle_lu_request(struct gsm_subscriber_connection *conn,
+ struct msgb *msg)
+{
+ struct gsm48_hdr *gh;
+ struct gsm48_loc_upd_req *lu;
+ struct gsm48_loc_area_id lai;
+
+ if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*lu)) {
+ LOGP(DMSC, LOGL_ERROR, "LU too small to look at: %u\n", msgb_l3len(msg));
+ return;
+ }
+
+ gh = msgb_l3(msg);
+ lu = (struct gsm48_loc_upd_req *) gh->data;
+
+ gsm48_generate_lai2(&lai, bts_lai(conn_get_bts(conn)));
+
+ if (memcmp(&lai, &lu->lai, sizeof(lai)) != 0) {
+ LOGP(DMSC, LOGL_DEBUG, "Marking con for welcome USSD.\n");
+ conn->new_subscriber = 1;
+ }
+}
+
+int bsc_scan_bts_msg(struct gsm_subscriber_connection *conn, struct msgb *msg)
+{
+ struct gsm48_hdr *gh = msgb_l3(msg);
+ uint8_t pdisc = gsm48_hdr_pdisc(gh);
+ uint8_t mtype = gsm48_hdr_msg_type(gh);
+
+ if (pdisc == GSM48_PDISC_MM) {
+ if (mtype == GSM48_MT_MM_LOC_UPD_REQUEST)
+ handle_lu_request(conn, msg);
+ } else if (pdisc == GSM48_PDISC_RR) {
+ if (mtype == GSM48_MT_RR_PAG_RESP)
+ handle_page_resp(conn, msg);
+ }
+
+ return 0;
+}
+
static int complete_layer3(struct gsm_subscriber_connection *conn,
struct msgb *msg, struct bsc_msc_data *msc)
{
@@ -274,6 +461,7 @@ static int complete_layer3(struct gsm_subscriber_connection *conn,
} else
conn->bsub = bsc_subscr_find_or_create_by_imsi(msc->network->bsc_subscribers,
imsi);
+ gscon_update_id(conn);
}
conn->filter_state.con_type = con_type;
@@ -311,8 +499,10 @@ static int move_to_msc(struct gsm_subscriber_connection *_conn,
* properly.
*/
if (complete_layer3(_conn, msg, msc) != BSC_API_CONN_POL_ACCEPT) {
- gsm0808_clear(_conn);
- //bsc_subscr_con_free(_conn);
+ /* FIXME: I have not the slightest idea what move_to_msc() intends to do; during lchan
+ * FSM introduction, I changed this and hope it is the appropriate action. I actually
+ * assume this is unused legacy code for osmo-bsc_nat?? */
+ gscon_release_lchans(_conn, false);
return 1;
}
@@ -411,40 +601,6 @@ void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct ms
osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_MO_DTAP, msg);
}
-/*! BSC->MSC: Assignment of lchan successful. */
-void bsc_assign_compl(struct gsm_subscriber_connection *conn, uint8_t rr_cause)
-{
- if (!msc_connected(conn))
- return;
-
- conn->lchan->abis_ip.ass_compl.rr_cause = rr_cause;
-
- if (is_ipaccess_bts(conn_get_bts(conn)) && conn->user_plane.rtp_ip) {
- /* NOTE: In a network that makes use of an IPA base station
- * and AoIP, we have to wait until the BTS reports its RTP
- * IP/Port combination back to BSC via RSL. Unfortunately, the
- * IPA protocol sends its Abis assignment complete message
- * before it sends its RTP IP/Port via IPACC. So we will now
- * postpone the AoIP assignment completed message until we
- * know the RTP IP/Port combination. */
- LOGP(DMSC, LOGL_INFO, "POSTPONE MSC ASSIGN COMPL\n");
- conn->lchan->abis_ip.ass_compl.valid = true;
-
- } else {
- /* NOTE: Send the A assignment complete message immediately. */
- LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN COMPL\n");
- osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_RR_ASS_COMPL, NULL);
- }
-}
-
-/*! BSC->MSC: Assignment of lchan failed. */
-void bsc_assign_fail(struct gsm_subscriber_connection *conn, uint8_t cause, uint8_t *rr_cause)
-{
- LOGPFSML(conn->fi, LOGL_ERROR, "Assignment failure: BSSMAP: '%s' from RR: '%s'\n",
- gsm0808_cause_name(cause), rr_cause ? rr_cause_name(*rr_cause) : "(none)");
- osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_RR_ASS_FAIL, &cause);
-}
-
/*! BSC->MSC: RR conn has been cleared. */
int bsc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
{
diff --git a/src/osmo-bsc/osmo_bsc_audio.c b/src/osmo-bsc/osmo_bsc_audio.c
deleted file mode 100644
index 8eef8d2da..000000000
--- a/src/osmo-bsc/osmo_bsc_audio.c
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * ipaccess audio handling
- *
- * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
- * (C) 2009-2010 by On-Waves
- * All Rights Reserved
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-#include <osmocom/bsc/bsc_msc_data.h>
-#include <osmocom/bsc/osmo_bsc.h>
-#include <osmocom/bsc/abis_rsl.h>
-#include <osmocom/bsc/gsm_data.h>
-#include <osmocom/bsc/debug.h>
-#include <osmocom/bsc/signal.h>
-#include <osmocom/gsm/gsm0808.h>
-#include <osmocom/gsm/gsm0808_utils.h>
-#include <osmocom/bsc/osmo_bsc_sigtran.h>
-#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
-#include <osmocom/bsc/bsc_subscriber.h>
-
-#include <arpa/inet.h>
-
-static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
- void *handler_data, void *signal_data)
-{
- struct gsm_subscriber_connection *con;
- struct gsm_lchan *lchan = signal_data;
- int rc;
- uint32_t rtp_ip;
-
- if (subsys != SS_ABISIP)
- return 0;
-
- con = lchan->conn;
- if (!con)
- return 0;
-
- switch (signal) {
- case S_ABISIP_CRCX_ACK:
- /* we can ask it to connect now */
- LOGP(DMSC, LOGL_DEBUG, "Connecting BTS to port: %d conn: %d\n",
- con->user_plane.rtp_port, lchan->abis_ip.conn_id);
-
- /* If AoIP is in use, the rtp_ip, which has been communicated
- * via the A interface as connect_ip */
- if(con->user_plane.rtp_ip)
- rtp_ip = con->user_plane.rtp_ip;
- else
- rtp_ip = ntohl(INADDR_ANY);
-
- rc = rsl_ipacc_mdcx(lchan, rtp_ip,
- con->user_plane.rtp_port,
- lchan->abis_ip.rtp_payload2);
- if (rc < 0) {
- LOGP(DMSC, LOGL_ERROR, "Failed to send MDCX: %d\n", rc);
- return rc;
- }
- break;
-
- case S_ABISIP_MDCX_ACK:
- if (con->ho) {
- LOGPHO(con->ho, LOGL_DEBUG, "BTS sent MDCX ACK\n");
- /* No need to do anything for handover here. As soon as a HANDOVER DETECT
- * happens, handover_logic.c and bsc_subscr_conn_fsm.c will trigger the
- * MGCP MDCX towards MGW by receiving an S_LCHAN_HANDOVER_DETECT signal. */
- } else if (is_ipaccess_bts(conn_get_bts(con)) && con->user_plane.rtp_ip) {
- /* NOTE: This is only relevant on AoIP networks with
- * IPA based base stations. See also osmo_bsc_api.c,
- * function bsc_assign_compl() */
- LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN COMPL (POSTPONED)\n");
- osmo_fsm_inst_dispatch(con->fi, GSCON_EV_RR_ASS_COMPL, NULL);
-
- }
- break;
- }
-
- return 0;
-}
-
-int osmo_bsc_audio_init(struct gsm_network *net)
-{
- osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, net);
- return 0;
-}
diff --git a/src/osmo-bsc/osmo_bsc_bssap.c b/src/osmo-bsc/osmo_bsc_bssap.c
index c2d51b5cb..269fd7180 100644
--- a/src/osmo-bsc/osmo_bsc_bssap.c
+++ b/src/osmo-bsc/osmo_bsc_bssap.c
@@ -19,6 +19,8 @@
*
*/
+#include <osmocom/mgcp_client/mgcp_client_fsm.h>
+
#include <osmocom/bsc/osmo_bsc.h>
#include <osmocom/bsc/osmo_bsc_grace.h>
#include <osmocom/bsc/osmo_bsc_rf.h>
@@ -27,7 +29,10 @@
#include <osmocom/bsc/bsc_subscriber.h>
#include <osmocom/bsc/paging.h>
#include <osmocom/bsc/gsm_04_08_utils.h>
+#include <osmocom/bsc/gsm_04_80.h>
#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
+#include <osmocom/bsc/abis_rsl.h>
+#include <osmocom/bsc/handover_fsm.h>
#include <osmocom/gsm/protocol/gsm_08_08.h>
#include <osmocom/gsm/gsm0808.h>
@@ -45,161 +50,6 @@
* helpers for the assignment command
*/
-/* Helper function for match_codec_pref(), looks up a matching permitted speech
- * value for a given msc audio codec pref */
-enum gsm0808_permitted_speech audio_support_to_gsm88(struct gsm_audio_support *audio)
-{
- if (audio->hr) {
- switch (audio->ver) {
- case 1:
- return GSM0808_PERM_HR1;
- break;
- case 2:
- return GSM0808_PERM_HR2;
- break;
- case 3:
- return GSM0808_PERM_HR3;
- break;
- default:
- LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: hr%d, using hr1 instead\n",
- audio->ver);
- return GSM0808_PERM_HR1;
- }
- } else {
- switch (audio->ver) {
- case 1:
- return GSM0808_PERM_FR1;
- break;
- case 2:
- return GSM0808_PERM_FR2;
- break;
- case 3:
- return GSM0808_PERM_FR3;
- break;
- default:
- LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: fr%d, using fr1 instead\n",
- audio->ver);
- return GSM0808_PERM_FR1;
- }
- }
-}
-
-/* Helper function for match_codec_pref(), looks up a matching chan mode for
- * a given permitted speech value */
-enum gsm48_chan_mode gsm88_to_chan_mode(enum gsm0808_permitted_speech speech)
-{
- switch (speech) {
- case GSM0808_PERM_HR1:
- case GSM0808_PERM_FR1:
- return GSM48_CMODE_SPEECH_V1;
- break;
- case GSM0808_PERM_HR2:
- case GSM0808_PERM_FR2:
- return GSM48_CMODE_SPEECH_EFR;
- break;
- case GSM0808_PERM_HR3:
- case GSM0808_PERM_FR3:
- return GSM48_CMODE_SPEECH_AMR;
- break;
- default:
- LOGP(DMSC, LOGL_FATAL,
- "Unsupported permitted speech selected, assuming AMR as channel mode...\n");
- return GSM48_CMODE_SPEECH_AMR;
- }
-}
-
-/* Helper function for match_codec_pref(), tests if a given audio support
- * matches one of the permitted speech settings of the channel type element.
- * The matched permitted speech value is then also compared against the
- * speech codec list. (optional, only relevant for AoIP) */
-static bool test_codec_pref(const struct gsm0808_channel_type *ct,
- const struct gsm0808_speech_codec_list *scl,
- uint8_t perm_spch)
-{
- unsigned int i;
- bool match = false;
- struct gsm0808_speech_codec sc;
- int rc;
-
- /* Try to finde the given permitted speech value in the
- * codec list of the channel type element */
- for (i = 0; i < ct->perm_spch_len; i++) {
- if (ct->perm_spch[i] == perm_spch) {
- match = true;
- break;
- }
- }
-
- /* If we do not have a speech codec list to test against,
- * we just exit early (will be always the case in non-AoIP networks) */
- if (!scl)
- return match;
-
- /* If we failed to match until here, there is no
- * point in testing further */
- if (match == false)
- return false;
-
- /* Extrapolate speech codec data */
- rc = gsm0808_speech_codec_from_chan_type(&sc, perm_spch);
- if (rc < 0)
- return false;
-
- /* Try to find extrapolated speech codec data in
- * the speech codec list */
- for (i = 0; i < scl->len; i++) {
- if (sc.type == scl->codec[i].type)
- return true;
- }
-
- return false;
-}
-
-/*! Helper function for bssmap_handle_assignm_req(), matches the codec
- * preferences from the MSC with the codec preferences
- * \param[out] full_rate '1' if full-rate, '0' if half-rate, '-1' if no match
- * \param[out] chan_mode GSM 04.08 channel mode
- * \param[in] ct GSM 08.08 channel type
- * \param[in] scl GSM 08.08 speech codec list
- * \param[in] msc MSC data [for configuration]
- * \returns 0 on success, -1 in case no match was found */
-static int match_codec_pref(int *full_rate, enum gsm48_chan_mode *chan_mode,
- const struct gsm0808_channel_type *ct,
- const struct gsm0808_speech_codec_list *scl,
- const struct bsc_msc_data *msc)
-{
- unsigned int i;
- uint8_t perm_spch;
- bool match = false;
-
- for (i = 0; i < msc->audio_length; i++) {
- perm_spch = audio_support_to_gsm88(msc->audio_support[i]);
- if (test_codec_pref(ct, scl, perm_spch)) {
- match = true;
- break;
- }
- }
-
- /* Exit without result, in case no match can be deteched */
- if (!match) {
- *full_rate = -1;
- *chan_mode = GSM48_CMODE_SIGN;
- return -1;
- }
-
- /* Check if the result is a half or full rate codec */
- if (perm_spch == GSM0808_PERM_HR1 || perm_spch == GSM0808_PERM_HR2
- || perm_spch == GSM0808_PERM_HR3 || perm_spch == GSM0808_PERM_HR4
- || perm_spch == GSM0808_PERM_HR6)
- *full_rate = 0;
- else
- *full_rate = 1;
-
- /* Lookup a channel mode for the selected codec */
- *chan_mode = gsm88_to_chan_mode(perm_spch);
-
- return 0;
-}
static int bssmap_handle_reset_ack(struct bsc_msc_data *msc,
struct msgb *msg, unsigned int length)
@@ -712,6 +562,22 @@ static int bssmap_handle_lcls_connect_ctrl(struct gsm_subscriber_connection *con
return 0;
}
+static bool sockaddr_to_assignment_req(struct assignment_request *req,
+ const struct sockaddr_storage *sa)
+{
+ int rc;
+ const struct sockaddr_in *sin;
+
+ sin = (const struct sockaddr_in *)sa;
+ req->msc_rtp_port = osmo_ntohs(sin->sin_port);
+
+ rc = osmo_strlcpy(req->msc_rtp_addr, inet_ntoa(sin->sin_addr), sizeof(req->msc_rtp_addr));
+ if (rc <= 0 || rc >= sizeof(req->msc_rtp_addr))
+ return false;
+
+ return true;
+}
+
/*
* Handle the assignment request message.
@@ -727,17 +593,17 @@ static int bssmap_handle_assignm_req(struct gsm_subscriber_connection *conn,
uint8_t timeslot = 0;
uint8_t multiplex = 0;
enum gsm48_chan_mode chan_mode = GSM48_CMODE_SIGN;
- int full_rate = -1;
+ bool full_rate = false;
bool aoip = false;
struct sockaddr_storage rtp_addr;
struct gsm0808_channel_type ct;
- struct gsm0808_speech_codec_list *scl_ptr = NULL;
uint8_t cause;
int rc;
+ struct assignment_request req = {};
if (!conn) {
LOGP(DMSC, LOGL_ERROR,
- "No lchan/msc_data in cipher mode command.\n");
+ "No lchan/msc_data in Assignment Request\n");
return -1;
}
@@ -803,27 +669,18 @@ static int bssmap_handle_assignm_req(struct gsm_subscriber_connection *conn,
cause = GSM0808_CAUSE_INCORRECT_VALUE;
goto reject;
}
- } else {
- if (aoip) {
- /* no AoIP transport level address but AoIP transport: illegal */
- LOGP(DMSC, LOGL_ERROR, "AoIP transport address missing in ASSIGN REQ, "
- "audio would not work; rejecting\n");
- cause = GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING;
- goto reject;
- }
+ } else if (aoip) {
+ /* no AoIP transport level address but AoIP transport: illegal */
+ LOGP(DMSC, LOGL_ERROR, "AoIP transport address missing in ASSIGN REQ, "
+ "audio would not work; rejecting\n");
+ cause = GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING;
+ goto reject;
}
- /* Decode speech codec list (AoIP) */
- conn->codec_list_present = false;
- if (aoip) {
-
- /* Check for speech codec list element */
- if (!TLVP_PRESENT(&tp, GSM0808_IE_SPEECH_CODEC_LIST)) {
- LOGP(DMSC, LOGL_ERROR, "Mandatory speech codec list not present.\n");
- cause = GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING;
- goto reject;
- }
-
+ /* Decode speech codec list. First set len = 0. */
+ conn->codec_list = (struct gsm0808_speech_codec_list){};
+ /* Check for speech codec list element */
+ if (TLVP_PRESENT(&tp, GSM0808_IE_SPEECH_CODEC_LIST)) {
/* Decode Speech Codec list */
rc = gsm0808_dec_speech_codec_list(&conn->codec_list,
TLVP_VAL(&tp, GSM0808_IE_SPEECH_CODEC_LIST),
@@ -833,13 +690,19 @@ static int bssmap_handle_assignm_req(struct gsm_subscriber_connection *conn,
cause = GSM0808_CAUSE_INCORRECT_VALUE;
goto reject;
}
- conn->codec_list_present = true;
- scl_ptr = &conn->codec_list;
+ }
+
+ if (aoip && !conn->codec_list.len) {
+ LOGP(DMSC, LOGL_ERROR, "%s: AoIP speech mode Assignment Request:"
+ " Missing or empty Speech Codec List IE\n", bsc_subscr_name(conn->bsub));
+ cause = GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING;
+ goto reject;
}
/* Match codec information from the assignment command against the
* local preferences of the BSC */
- rc = match_codec_pref(&full_rate, &chan_mode, &ct, scl_ptr, msc);
+ rc = bsc_match_codec_pref(&chan_mode, &full_rate, &ct, &conn->codec_list,
+ msc->audio_support, msc->audio_length);
if (rc < 0) {
LOGP(DMSC, LOGL_ERROR, "No supported audio type found for channel_type ="
" { ch_indctr=0x%x, ch_rate_type=0x%x, perm_spch=[ %s] }\n",
@@ -856,37 +719,36 @@ static int bssmap_handle_assignm_req(struct gsm_subscriber_connection *conn,
get_value_string(gsm48_chan_mode_names, chan_mode),
ct.ch_indctr, ct.ch_rate_type, osmo_hexdump(ct.perm_spch, ct.perm_spch_len));
- /* Forward the assignment request to lower layers */
+ req = (struct assignment_request){
+ .aoip = aoip,
+ .chan_mode = chan_mode,
+ .full_rate = full_rate,
+ };
if (aoip) {
- /* Store network side RTP connection information, we will
- * process this address later after we have established an RTP
- * connection to the BTS. This is just for organizational
- * reasons, functional wise it would not matter when exactly
- * the network side RTP connection is made, as long it is made
- * before we return with the assignment complete message. */
- memcpy(&conn->user_plane.aoip_rtp_addr_remote, &rtp_addr, sizeof(rtp_addr));
+ if (!sockaddr_to_assignment_req(&req, &rtp_addr)) {
+ LOGP(DMSC, LOGL_ERROR, "Assignment request: Invalid RTP address (too long?)\n");
+ cause = GSM0808_CAUSE_REQ_CODEC_TYPE_OR_CONFIG_UNAVAIL;
+ goto reject;
+ }
} else {
/* Note: In the sccp-lite case we to not perform any mgcp operation,
- * (the MSC does that for us). We set conn->rtp_ip to 0 and check
- * on this later. By this we know that we have to behave accordingly
- * to sccp-lite. */
- conn->user_plane.rtp_port = mgcp_timeslot_to_port(multiplex, timeslot, msc->rtp_base);
- conn->user_plane.rtp_ip = 0;
+ * (the MSC does that for us). */
+ req.msc_rtp_port = mgcp_timeslot_to_port(multiplex, timeslot, msc->rtp_base);
}
- conn->user_plane.chan_mode = chan_mode;
- conn->user_plane.full_rate = full_rate;
- osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_A_ASSIGNMENT_CMD, NULL);
break;
case GSM0808_CHAN_SIGN:
- conn->user_plane.chan_mode = GSM48_CMODE_SIGN;
- osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_A_ASSIGNMENT_CMD, NULL);
+ req = (struct assignment_request){
+ .aoip = aoip,
+ .chan_mode = chan_mode,
+ };
break;
default:
cause = GSM0808_CAUSE_INVALID_MESSAGE_CONTENTS;
goto reject;
}
- return 0;
+ return osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_ASSIGNMENT_START, &req);
+
reject:
resp = gsm0808_create_assignment_failure(cause, NULL);
OSMO_ASSERT(resp);
@@ -895,6 +757,50 @@ reject:
return -1;
}
+/* Handle Handover Command message, part of inter-BSC handover:
+ * This BSS sent a Handover Required message.
+ * The MSC contacts the remote BSS and receives from it an RR Handover Command; this BSSMAP Handover
+ * Command passes the RR Handover Command over to us and it's our job to forward to the MS.
+ *
+ * See 3GPP TS 48.008 §3.2.1.11
+ */
+static int bssmap_handle_handover_cmd(struct gsm_subscriber_connection *conn,
+ struct msgb *msg, unsigned int length)
+{
+ struct tlv_parsed tp;
+
+ if (!conn->ho.fi) {
+ LOGP(DMSC, LOGL_ERROR, "Received Handover Command, but no handover was requested");
+ /* Should we actually allow the MSC to make us handover without us having requested it
+ * first? Doesn't make any practical sense AFAICT. */
+ return -EINVAL;
+ }
+
+ tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h + 1, length - 1, 0, 0);
+
+ /* Check for channel type element, if its missing, immediately reject */
+ if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
+ LOGP(DMSC, LOGL_ERROR, "Mandatory IE not present: Layer 3 Information\n");
+ goto reject;
+ }
+
+
+ {
+ struct moho_rx_bssmap_ho_command rx = {
+ .l3_info = TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION),
+ .l3_info_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION),
+ };
+
+ osmo_fsm_inst_dispatch(conn->ho.fi, MOHO_EV_BSSMAP_HO_COMMAND, &rx);
+ }
+ return 0;
+reject:
+ /* No "Handover Command Reject" message or similar is specified, so we cannot reply in case of
+ * failure. Or is there?? */
+ handover_end(conn, HO_RESULT_ERROR);
+ return -EINVAL;
+}
+
static int bssmap_rcvmsg_udt(struct bsc_msc_data *msc,
struct msgb *msg, unsigned int length)
{
@@ -953,6 +859,9 @@ static int bssmap_rcvmsg_dt1(struct gsm_subscriber_connection *conn,
case BSS_MAP_MSG_LCLS_CONNECT_CTRL:
ret = bssmap_handle_lcls_connect_ctrl(conn, msg, length);
break;
+ case BSS_MAP_MSG_HANDOVER_CMD:
+ ret = bssmap_handle_handover_cmd(conn, msg, length);
+ break;
default:
LOGP(DMSC, LOGL_NOTICE, "Unimplemented msg type: %s\n",
gsm0808_bssmap_name(msg->l4h[0]));
@@ -962,6 +871,14 @@ static int bssmap_rcvmsg_dt1(struct gsm_subscriber_connection *conn,
return ret;
}
+int bsc_send_welcome_ussd(struct gsm_subscriber_connection *conn)
+{
+ bsc_send_ussd_notify(conn, 1, conn->sccp.msc->ussd_welcome_txt);
+ bsc_send_ussd_release_complete(conn);
+
+ return 0;
+}
+
static int dtap_rcvmsg(struct gsm_subscriber_connection *conn,
struct msgb *msg, unsigned int length)
{
@@ -1066,3 +983,33 @@ int bsc_handle_dt(struct gsm_subscriber_connection *conn,
return -1;
}
+
+/* Inter-BSC MT HO, new BSS has allocated a channel and sends the RR Handover Command via MSC to the old
+ * BSS, encapsulated in a BSSMAP Handover Request Acknowledge. */
+int bsc_send_handover_request_ack(struct gsm_subscriber_connection *conn, struct msgb *rr_ho_command)
+{
+ struct msgb *msg;
+ struct gsm_lchan *new_lchan = conn->ho.mt.new_lchan;
+
+ msg = gsm0808_create_handover_request_ack(rr_ho_command->data, rr_ho_command->len,
+ gsm0808_chosen_channel(new_lchan->type,
+ new_lchan->tch_mode),
+ new_lchan->encr.alg_id,
+ gsm0808_permitted_speech(new_lchan->type,
+ new_lchan->tch_mode));
+ msgb_free(rr_ho_command);
+ if (!msg)
+ return -ENOMEM;
+ return osmo_bsc_sigtran_send(conn, msg);
+}
+
+int bsc_send_handover_detected(struct gsm_subscriber_connection *conn)
+{
+ struct msgb *msg;
+ struct gsm0808_handover_detected params = {};
+ msg = gsm0808_create_handover_detected(&params);
+ if (!msg)
+ return -ENOMEM;
+
+ return osmo_bsc_sigtran_send(conn, msg);
+}
diff --git a/src/osmo-bsc/osmo_bsc_filter.c b/src/osmo-bsc/osmo_bsc_filter.c
index 9eddfbbd9..830d25a02 100644
--- a/src/osmo-bsc/osmo_bsc_filter.c
+++ b/src/osmo-bsc/osmo_bsc_filter.c
@@ -30,29 +30,6 @@
#include <stdlib.h>
-static void handle_lu_request(struct gsm_subscriber_connection *conn,
- struct msgb *msg)
-{
- struct gsm48_hdr *gh;
- struct gsm48_loc_upd_req *lu;
- struct gsm48_loc_area_id lai;
-
- if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*lu)) {
- LOGP(DMSC, LOGL_ERROR, "LU too small to look at: %u\n", msgb_l3len(msg));
- return;
- }
-
- gh = msgb_l3(msg);
- lu = (struct gsm48_loc_upd_req *) gh->data;
-
- gsm48_generate_lai2(&lai, bts_lai(conn_get_bts(conn)));
-
- if (memcmp(&lai, &lu->lai, sizeof(lai)) != 0) {
- LOGP(DMSC, LOGL_DEBUG, "Marking con for welcome USSD.\n");
- conn->new_subscriber = 1;
- }
-}
-
/* extract a subscriber from the paging response */
struct bsc_subscr *extract_sub(struct gsm_subscriber_connection *conn, struct msgb *msg)
{
@@ -92,140 +69,6 @@ struct bsc_subscr *extract_sub(struct gsm_subscriber_connection *conn, struct ms
return subscr;
}
-/* we will need to stop the paging request */
-static int handle_page_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
-{
- struct bsc_subscr *subscr = extract_sub(conn, msg);
-
- if (!subscr) {
- LOGP(DMSC, LOGL_ERROR, "Non active subscriber got paged.\n");
- return -1;
- }
-
- paging_request_stop(&conn->network->bts_list, conn_get_bts(conn), subscr, conn,
- msg);
- bsc_subscr_put(subscr);
- return 0;
-}
-
-static int is_cm_service_for_emerg(struct msgb *msg)
-{
- struct gsm48_service_request *cm;
- struct gsm48_hdr *gh = msgb_l3(msg);
-
- if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*cm)) {
- LOGP(DMSC, LOGL_ERROR, "CM ServiceRequest does not fit.\n");
- return 0;
- }
-
- cm = (struct gsm48_service_request *) &gh->data[0];
- return cm->cm_service_type == GSM48_CMSERV_EMERGENCY;
-}
-
-struct bsc_msc_data *bsc_find_msc(struct gsm_subscriber_connection *conn,
- struct msgb *msg)
-{
- struct gsm48_hdr *gh;
- int8_t pdisc;
- uint8_t mtype;
- struct osmo_bsc_data *bsc;
- struct bsc_msc_data *msc, *pag_msc;
- struct bsc_subscr *subscr;
- int is_emerg = 0;
-
- bsc = conn->network->bsc_data;
-
- if (msgb_l3len(msg) < sizeof(*gh)) {
- LOGP(DMSC, LOGL_ERROR, "There is no GSM48 header here.\n");
- return NULL;
- }
-
- gh = msgb_l3(msg);
- pdisc = gsm48_hdr_pdisc(gh);
- mtype = gsm48_hdr_msg_type(gh);
-
- /*
- * We are asked to select a MSC here but they are not equal. We
- * want to respond to a paging request on the MSC where we got the
- * request from. This is where we need to decide where this connection
- * will go.
- */
- if (pdisc == GSM48_PDISC_RR && mtype == GSM48_MT_RR_PAG_RESP)
- goto paging;
- else if (pdisc == GSM48_PDISC_MM && mtype == GSM48_MT_MM_CM_SERV_REQ) {
- is_emerg = is_cm_service_for_emerg(msg);
- goto round_robin;
- } else
- goto round_robin;
-
-round_robin:
- llist_for_each_entry(msc, &bsc->mscs, entry) {
- if (!msc->is_authenticated)
- continue;
- if (!is_emerg && msc->type != MSC_CON_TYPE_NORMAL)
- continue;
- if (is_emerg && !msc->allow_emerg)
- continue;
-
- /* force round robin by moving it to the end */
- llist_move_tail(&msc->entry, &bsc->mscs);
- return msc;
- }
-
- return NULL;
-
-paging:
- subscr = extract_sub(conn, msg);
-
- if (!subscr) {
- LOGP(DMSC, LOGL_ERROR, "Got paged but no subscriber found.\n");
- return NULL;
- }
-
- pag_msc = paging_get_msc(conn_get_bts(conn), subscr);
- bsc_subscr_put(subscr);
-
- llist_for_each_entry(msc, &bsc->mscs, entry) {
- if (msc != pag_msc)
- continue;
-
- /*
- * We don't check if the MSC is connected. In case it
- * is not the connection will be dropped.
- */
-
- /* force round robin by moving it to the end */
- llist_move_tail(&msc->entry, &bsc->mscs);
- return msc;
- }
-
- LOGP(DMSC, LOGL_ERROR, "Got paged but no request found.\n");
- return NULL;
-}
-
-
-/**
- * This is used to scan a message for extra functionality of the BSC. This
- * includes scanning for location updating requests/acceptd and then send
- * a welcome USSD message to the subscriber.
- */
-int bsc_scan_bts_msg(struct gsm_subscriber_connection *conn, struct msgb *msg)
-{
- struct gsm48_hdr *gh = msgb_l3(msg);
- uint8_t pdisc = gsm48_hdr_pdisc(gh);
- uint8_t mtype = gsm48_hdr_msg_type(gh);
-
- if (pdisc == GSM48_PDISC_MM) {
- if (mtype == GSM48_MT_MM_LOC_UPD_REQUEST)
- handle_lu_request(conn, msg);
- } else if (pdisc == GSM48_PDISC_RR) {
- if (mtype == GSM48_MT_RR_PAG_RESP)
- handle_page_resp(conn, msg);
- }
-
- return 0;
-}
-
static int send_welcome_ussd(struct gsm_subscriber_connection *conn)
{
if (!conn->sccp.msc->ussd_welcome_txt) {
@@ -236,14 +79,6 @@ static int send_welcome_ussd(struct gsm_subscriber_connection *conn)
return BSS_SEND_USSD;
}
-int bsc_send_welcome_ussd(struct gsm_subscriber_connection *conn)
-{
- bsc_send_ussd_notify(conn, 1, conn->sccp.msc->ussd_welcome_txt);
- bsc_send_ussd_release_complete(conn);
-
- return 0;
-}
-
static int bsc_patch_mm_info(struct gsm_subscriber_connection *conn,
uint8_t *data, unsigned int length)
{
diff --git a/src/osmo-bsc/osmo_bsc_grace.c b/src/osmo-bsc/osmo_bsc_grace.c
index 705933cb4..d096611be 100644
--- a/src/osmo-bsc/osmo_bsc_grace.c
+++ b/src/osmo-bsc/osmo_bsc_grace.c
@@ -25,6 +25,7 @@
#include <osmocom/bsc/bsc_subscriber.h>
#include <osmocom/bsc/paging.h>
#include <osmocom/bsc/signal.h>
+#include <osmocom/bsc/lchan_fsm.h>
int bsc_grace_allow_new_connection(struct gsm_network *network, struct gsm_bts *bts)
{
@@ -89,7 +90,7 @@ static int handle_sub(struct gsm_lchan *lchan, const char *text)
return -1;
/* only when active */
- if (lchan->state != LCHAN_S_ACTIVE)
+ if (lchan->fi->state != LCHAN_ST_ACTIVE)
return -1;
bsc_send_ussd_notify(conn, 0, text);
diff --git a/src/osmo-bsc/osmo_bsc_lcls.c b/src/osmo-bsc/osmo_bsc_lcls.c
index c2b076090..14a043769 100644
--- a/src/osmo-bsc/osmo_bsc_lcls.c
+++ b/src/osmo-bsc/osmo_bsc_lcls.c
@@ -28,6 +28,7 @@
#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
#include <osmocom/bsc/gsm_data.h>
#include <osmocom/bsc/osmo_bsc_lcls.h>
+#include <osmocom/bsc/mgw_endpoint_fsm.h>
#include <osmocom/mgcp_client/mgcp_client_fsm.h>
struct value_string lcls_event_names[] = {
@@ -228,23 +229,23 @@ void lcls_apply_config(struct gsm_subscriber_connection *conn)
static void lcls_break_local_switching(struct gsm_subscriber_connection *conn)
{
- struct mgcp_conn_peer peer;
- struct sockaddr_in *sin;
+ struct mgcp_conn_peer mdcx_info;
LOGPFSM(conn->lcls.fi, "=== HERE IS WHERE WE DISABLE LCLS\n");
- if (!conn->user_plane.fi_msc) {
+ if (!conn->user_plane.mgw_endpoint_ci_msc) {
/* the MGCP FSM has died, e.g. due to some MGCP/SDP parsing error */
LOGPFSML(conn->lcls.fi, LOGL_NOTICE, "Cannot disable LCLS without MSC-side MGCP FSM\n");
return;
}
- sin = (struct sockaddr_in *)&conn->user_plane.aoip_rtp_addr_remote;
- OSMO_ASSERT(sin->sin_family == AF_INET);
+ mdcx_info = (struct mgcp_conn_peer){
+ .port = conn->user_plane.msc_assigned_rtp_port,
+ };
+ osmo_strlcpy(mdcx_info.addr, conn->user_plane.msc_assigned_rtp_addr, sizeof(mdcx_info.addr));
- memset(&peer, 0, sizeof(peer));
- peer.port = htons(sin->sin_port);
- osmo_strlcpy(peer.addr, inet_ntoa(sin->sin_addr), sizeof(peer.addr));
- mgcp_conn_modify(conn->user_plane.fi_msc, 0, &peer);
+ mgw_endpoint_ci_request(conn->user_plane.mgw_endpoint_ci_msc,
+ MGCP_VERB_MDCX, &mdcx_info,
+ NULL, 0, 0, NULL);
}
static bool lcls_enable_possible(struct gsm_subscriber_connection *conn)
@@ -546,25 +547,34 @@ static void lcls_locally_switched_onenter(struct osmo_fsm_inst *fi, uint32_t pre
{
struct gsm_subscriber_connection *conn = fi->priv;
struct gsm_subscriber_connection *conn_other = conn->lcls.other;
- struct mgcp_conn_peer peer;
- struct sockaddr_in *sin;
+ const struct mgcp_conn_peer *other_mgw_info;
+ struct mgcp_conn_peer mdcx_info;
OSMO_ASSERT(conn_other);
LOGPFSM(fi, "=== HERE IS WHERE WE ENABLE LCLS\n");
- if (!conn->user_plane.fi_msc) {
+ if (!conn->user_plane.mgw_endpoint_ci_msc) {
+ LOGPFSML(fi, LOGL_ERROR, "Cannot enable LCLS without MSC-side MGCP FSM. FIXME\n");
+ return;
+ }
+ if (!conn_other->user_plane.mgw_endpoint_ci_msc) {
LOGPFSML(fi, LOGL_ERROR, "Cannot enable LCLS without MSC-side MGCP FSM. FIXME\n");
return;
}
- sin = (struct sockaddr_in *)&conn_other->user_plane.aoip_rtp_addr_local;
- OSMO_ASSERT(sin->sin_family == AF_INET);
-
- memset(&peer, 0, sizeof(peer));
- peer.port = htons(sin->sin_port);
- osmo_strlcpy(peer.addr, inet_ntoa(sin->sin_addr), sizeof(peer.addr));
- mgcp_conn_modify(conn->user_plane.fi_msc, 0, &peer);
+ other_mgw_info = mgwep_ci_get_rtp_info(conn_other->user_plane.mgw_endpoint_ci_msc);
+ if (!other_mgw_info) {
+ LOGPFSML(fi, LOGL_ERROR, "Cannot enable LCLS without RTP port info of MSC-side"
+ " -- missing CRCX?\n");
+ return;
+ }
+ mdcx_info = *other_mgw_info;
+ /* Make sure the request doesn't want to use the other side's endpoint string. */
+ mdcx_info.endpoint[0] = 0;
+ mgw_endpoint_ci_request(conn->user_plane.mgw_endpoint_ci_msc,
+ MGCP_VERB_MDCX, &mdcx_info,
+ NULL, 0, 0, NULL);
}
static void lcls_locally_switched_wait_break_fn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
@@ -764,3 +774,35 @@ struct osmo_fsm lcls_fsm = {
.log_subsys = DLCLS,
.event_names = lcls_event_names,
};
+
+/* Add the LCLS BSS Status IE to a BSSMAP message. We assume this is
+ * called on a msgb that was returned by gsm0808_create_ass_compl() */
+static void bssmap_add_lcls_status(struct msgb *msg, enum gsm0808_lcls_status status)
+{
+ OSMO_ASSERT(msg->l3h[0] == BSSAP_MSG_BSS_MANAGEMENT);
+ OSMO_ASSERT(msg->l3h[2] == BSS_MAP_MSG_ASSIGMENT_COMPLETE ||
+ msg->l3h[2] == BSS_MAP_MSG_HANDOVER_RQST_ACKNOWLEDGE ||
+ msg->l3h[2] == BSS_MAP_MSG_HANDOVER_COMPLETE ||
+ msg->l3h[2] == BSS_MAP_MSG_HANDOVER_PERFORMED);
+ OSMO_ASSERT(msgb_tailroom(msg) >= 2);
+
+ /* append IE to end of message */
+ msgb_tv_put(msg, GSM0808_IE_LCLS_BSS_STATUS, status);
+ /* increment the "length" byte in the BSSAP header */
+ msg->l3h[1] += 2;
+}
+
+/* Add (append) the LCLS BSS Status IE to a BSSMAP message, if there is any LCLS
+ * active on the given \a conn */
+void bssmap_add_lcls_status_if_needed(struct gsm_subscriber_connection *conn, struct msgb *msg)
+{
+ enum gsm0808_lcls_status status = lcls_get_status(conn);
+ if (status != 0xff) {
+ LOGPFSM(conn->fi, "Adding LCLS BSS-Status (%s) to %s\n",
+ gsm0808_lcls_status_name(status),
+ gsm0808_bssmap_name(msg->l3h[2]));
+ bssmap_add_lcls_status(msg, status);
+ }
+}
+
+
diff --git a/src/osmo-bsc/osmo_bsc_main.c b/src/osmo-bsc/osmo_bsc_main.c
index c46d2fa9e..67bd1d479 100644
--- a/src/osmo-bsc/osmo_bsc_main.c
+++ b/src/osmo-bsc/osmo_bsc_main.c
@@ -31,6 +31,8 @@
#include <osmocom/bsc/osmo_bsc_sigtran.h>
#include <osmocom/bsc/handover_decision.h>
#include <osmocom/bsc/handover_decision_2.h>
+#include <osmocom/bsc/timeslot_fsm.h>
+#include <osmocom/bsc/mgw_endpoint_fsm.h>
#include <osmocom/ctrl/control_cmd.h>
#include <osmocom/ctrl/control_if.h>
@@ -296,7 +298,8 @@ static void bootstrap_rsl(struct gsm_bts_trx *trx)
for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
struct gsm_bts_trx_ts *ts = &trx->ts[i];
generate_ma_for_ts(ts);
- gsm_ts_check_init(ts);
+ OSMO_ASSERT(ts->fi);
+ osmo_fsm_inst_dispatch(ts->fi, TS_EV_RSL_READY, NULL);
}
}
@@ -306,7 +309,7 @@ static int inp_sig_cb(unsigned int subsys, unsigned int signal,
{
struct input_signal_data *isd = signal_data;
struct gsm_bts_trx *trx = isd->trx;
- int ts_no, lchan_no;
+ int ts_no;
/* N. B: we rely on attribute order when parsing response in abis_nm_rx_get_attr_resp() */
const uint8_t bts_attr[] = { NM_ATT_MANUF_ID, NM_ATT_SW_CONFIG, };
const uint8_t trx_attr[] = { NM_ATT_MANUF_STATE, NM_ATT_SW_CONFIG, };
@@ -365,12 +368,8 @@ static int inp_sig_cb(unsigned int subsys, unsigned int signal,
*/
for (ts_no = 0; ts_no < ARRAY_SIZE(trx->ts); ++ts_no) {
struct gsm_bts_trx_ts *ts = &trx->ts[ts_no];
-
- for (lchan_no = 0; lchan_no < ARRAY_SIZE(ts->lchan); ++lchan_no) {
- if (ts->lchan[lchan_no].state != LCHAN_S_NONE)
- lchan_free(&ts->lchan[lchan_no]);
- lchan_reset(&ts->lchan[lchan_no]);
- }
+ if (ts->fi)
+ osmo_fsm_inst_dispatch(ts->fi, TS_EV_OML_DOWN, 0);
}
gsm_bts_mo_reset(trx->bts);
@@ -434,11 +433,9 @@ static int bootstrap_bts(struct gsm_bts *bts)
/* Control Channel Description is set from vty/config */
- /* T3212 is set from vty/config */
-
/* Set ccch config by looking at ts config */
for (n=0, i=0; i<8; i++)
- n += bts->c0->ts[i].pchan == GSM_PCHAN_CCCH ? 1 : 0;
+ n += bts->c0->ts[i].pchan_is == GSM_PCHAN_CCCH ? 1 : 0;
/* Indicate R99 MSC in SI3 */
bts->si_common.chan_desc.mscr = 1;
@@ -822,6 +819,8 @@ int main(int argc, char **argv)
/* seed the PRNG */
srand(time(NULL));
+ mgw_endpoint_fsm_init(bsc_gsmnet->T_defs);
+
/* Read the config */
rc = bsc_network_configure(config_file);
if (rc < 0) {
@@ -878,11 +877,6 @@ int main(int argc, char **argv)
exit(1);
}
- if (osmo_bsc_audio_init(bsc_gsmnet) != 0) {
- LOGP(DMSC, LOGL_ERROR, "Failed to register audio support.\n");
- exit(1);
- }
-
handover_decision_1_init();
hodec2_init(bsc_gsmnet);
diff --git a/src/osmo-bsc/osmo_bsc_sigtran.c b/src/osmo-bsc/osmo_bsc_sigtran.c
index 72ee50f4c..f5ad9519d 100644
--- a/src/osmo-bsc/osmo_bsc_sigtran.c
+++ b/src/osmo-bsc/osmo_bsc_sigtran.c
@@ -153,6 +153,54 @@ static int handle_unitdata_from_msc(const struct osmo_sccp_addr *msc_addr, struc
return rc;
}
+static int handle_n_connect_from_msc(struct osmo_sccp_user *scu, struct osmo_scu_prim *scu_prim)
+{
+ struct bsc_msc_data *msc = get_msc_by_addr(&scu_prim->u.connect.calling_addr);
+ struct gsm_subscriber_connection *conn;
+ int rc = 0;
+
+ conn = get_bsc_conn_by_conn_id(scu_prim->u.connect.conn_id);
+ if (conn) {
+ LOGP(DMSC, LOGL_NOTICE,
+ "(calling_addr=%s conn_id=%u) N-CONNECT.ind with already used conn_id, ignoring\n",
+ osmo_sccp_addr_dump(&scu_prim->u.connect.calling_addr),
+ scu_prim->u.connect.conn_id);
+ /* The situation is illogical. A conn was already established with this conn id, if we
+ * would like to reply with a disconn onto this conn id, we would close the existing
+ * conn. So just ignore this impossible N-CONNECT completely (including the BSSMAP PDU). */
+ return -EINVAL;
+ }
+
+ if (!msc) {
+ LOGP(DMSC, LOGL_NOTICE, "(calling_addr=%s conn_id=%u) N-CONNECT.ind from unknown MSC\n",
+ osmo_sccp_addr_dump(&scu_prim->u.connect.calling_addr),
+ scu_prim->u.connect.conn_id);
+ rc = -ENOENT;
+ goto refuse;
+ }
+
+ conn = bsc_subscr_con_allocate(bsc_gsmnet);
+ if (!conn)
+ return -ENOMEM;
+ conn->sccp.msc = msc;
+ conn->sccp.conn_id = scu_prim->u.connect.conn_id;
+
+ /* First off, accept the new conn. */
+ osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id,
+ &scu_prim->u.connect.called_addr, NULL, 0);
+
+ /* Make sure the conn FSM will osmo_sccp_tx_disconn() on term */
+ conn->sccp.state = SUBSCR_SCCP_ST_CONNECTED;
+
+ /* Take actions asked for by the enclosed PDU */
+ osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_A_CONN_IND, scu_prim);
+
+ return 0;
+refuse:
+ osmo_sccp_tx_disconn(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, 0);
+ return rc;
+}
+
/* Callback function, called by the SSCP stack when data arrives */
static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
{
@@ -171,13 +219,7 @@ static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
/* Handle inbound connections */
DEBUGP(DMSC, "N-CONNECT.ind(X->%u)\n", scu_prim->u.connect.conn_id);
- conn = bsc_subscr_con_allocate(bsc_gsmnet);
- if (conn) {
- conn->sccp.msc = get_msc_by_addr(&scu_prim->u.connect.calling_addr);
- /* MSC may be NULL, let the FSM deal with it */
- osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_A_CONN_IND, scu_prim);
- } else
- LOGP(DMSC, LOGL_ERROR, "Unable to alloc subscr_conn for inbound N-CONNECT.ind\n");
+ rc = handle_n_connect_from_msc(scu, scu_prim);
break;
case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
@@ -191,7 +233,7 @@ static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
if (msgb_l2len(oph->msg) > 0)
handle_data_from_msc(conn, oph->msg);
} else {
- LOGP(DMSC, LOGL_ERROR, "N-CONNET.cfm(%u, %s) for unknown conn?!?\n",
+ LOGP(DMSC, LOGL_ERROR, "N-CONNECT.cfm(%u, %s) for unknown conn?!?\n",
scu_prim->u.connect.conn_id, osmo_hexdump(msgb_l2(oph->msg),
msgb_l2len(oph->msg)));
}
diff --git a/src/osmo-bsc/paging.c b/src/osmo-bsc/paging.c
index 886d28b8b..ea9cdaae7 100644
--- a/src/osmo-bsc/paging.c
+++ b/src/osmo-bsc/paging.c
@@ -50,6 +50,8 @@
#include <osmocom/bsc/gsm_data.h>
#include <osmocom/bsc/chan_alloc.h>
#include <osmocom/bsc/bsc_api.h>
+#include <osmocom/bsc/gsm_04_08_utils.h>
+#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
void *tall_paging_ctx = NULL;
@@ -314,7 +316,7 @@ static int _paging_request(struct gsm_bts *bts, struct bsc_subscr *bsub, int typ
req->chan_type = type;
req->msc = msc;
osmo_timer_setup(&req->T3113, paging_T3113_expired, req);
- osmo_timer_schedule(&req->T3113, bts->network->T3113, 0);
+ osmo_timer_schedule(&req->T3113, T_def_get(bts->network->T_defs, 3113, T_S, -1), 0);
llist_add_tail(&req->entry, &bts_entry->pending_requests);
paging_schedule_if_needed(bts_entry);
@@ -392,6 +394,7 @@ void paging_request_stop(struct llist_head *bts_list,
log_set_context(LOG_CTX_BSC_SUBSCR, bsub);
conn->bsub = bsc_subscr_get(bsub);
+ gscon_update_id(conn);
/* Stop this first and dispatch the request */
if (_bts) {
diff --git a/src/osmo-bsc/pcu_sock.c b/src/osmo-bsc/pcu_sock.c
index 5a3fefadf..7617b333d 100644
--- a/src/osmo-bsc/pcu_sock.c
+++ b/src/osmo-bsc/pcu_sock.c
@@ -43,6 +43,7 @@
#include <osmocom/bsc/signal.h>
#include <osmocom/bsc/debug.h>
#include <osmocom/bsc/abis_rsl.h>
+#include <osmocom/bsc/gsm_04_08_utils.h>
static int pcu_sock_send(struct gsm_bts *bts, struct msgb *msg);
uint32_t trx_get_hlayer1(struct gsm_bts_trx *trx);
@@ -93,33 +94,6 @@ struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr)
return msg;
}
-/* Helper function exclusivly used by pcu_if_signal_cb() */
-static bool ts_should_be_pdch(struct gsm_bts_trx_ts *ts) {
- if (ts->pchan == GSM_PCHAN_PDCH)
- return true;
- if (ts->pchan == GSM_PCHAN_TCH_F_PDCH) {
- /* When we're busy deactivating the PDCH, we first set
- * DEACT_PENDING, tell the PCU about it and wait for a
- * response. So DEACT_PENDING means "no PDCH" to the PCU.
- * Similarly, when we're activating PDCH, we set the
- * ACT_PENDING and wait for an activation response from the
- * PCU, so ACT_PENDING means "is PDCH". */
- if (ts->flags & TS_F_PDCH_ACTIVE)
- return !(ts->flags & TS_F_PDCH_DEACT_PENDING);
- else
- return (ts->flags & TS_F_PDCH_ACT_PENDING);
- }
- if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) {
- /*
- * When we're busy de-/activating the PDCH, we first set
- * ts->dyn.pchan_want, tell the PCU about it and wait for a
- * response. So only care about dyn.pchan_want here.
- */
- return ts->dyn.pchan_want == GSM_PCHAN_PDCH;
- }
- return false;
-}
-
/* Send BTS properties to the PCU */
static int pcu_tx_info_ind(struct gsm_bts *bts)
{
@@ -231,7 +205,7 @@ static int pcu_tx_info_ind(struct gsm_bts *bts)
for (j = 0; j < ARRAY_SIZE(trx->ts); j++) {
ts = &trx->ts[j];
if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
- && ts_should_be_pdch(ts)) {
+ && ts->pchan_is == GSM_PCHAN_PDCH) {
info_ind->trx[i].pdch_mask |= (1 << j);
info_ind->trx[i].tsc[j] =
(ts->tsc >= 0) ? ts->tsc : bts->bsic & 7;
@@ -520,7 +494,7 @@ static void pcu_sock_close(struct pcu_sock_state *state)
for (j = 0; j < 8; j++) {
ts = &trx->ts[j];
if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
- && ts->pchan == GSM_PCHAN_PDCH) {
+ && ts->pchan_is == GSM_PCHAN_PDCH) {
printf("l1sap_chan_rel(trx,gsm_lchan2chan_nr(ts->lchan));\n");
}
}
diff --git a/src/osmo-bsc/system_information.c b/src/osmo-bsc/system_information.c
index 071babab6..0852ef571 100644
--- a/src/osmo-bsc/system_information.c
+++ b/src/osmo-bsc/system_information.c
@@ -1224,6 +1224,7 @@ static const gen_si_fn_t gen_si_fn[_MAX_SYSINFO_TYPE] = {
int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type)
{
+ int rc;
gen_si_fn_t gen_si;
switch (bts->gprs.mode) {
@@ -1244,8 +1245,15 @@ int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type)
sizeof(struct gsm48_si_selection_params));
gen_si = gen_si_fn[si_type];
- if (!gen_si)
+ if (!gen_si) {
+ LOGP(DRR, LOGL_ERROR, "bts %u: no gen_si_fn() for SI%s\n",
+ bts->nr, get_value_string(osmo_sitype_strs, si_type));
return -EINVAL;
+ }
- return gen_si(si_type, bts);
+ rc = gen_si(si_type, bts);
+ if (rc < 0)
+ LOGP(DRR, LOGL_ERROR, "bts %u: Error while generating SI%s: %s (%d)\n",
+ bts->nr, get_value_string(osmo_sitype_strs, si_type), strerror(-rc), rc);
+ return rc;
}
diff --git a/src/osmo-bsc/timeslot_fsm.c b/src/osmo-bsc/timeslot_fsm.c
new file mode 100644
index 000000000..8356f1adb
--- /dev/null
+++ b/src/osmo-bsc/timeslot_fsm.c
@@ -0,0 +1,859 @@
+/* osmo-bsc API to manage timeslot status: init and switch of dynamic PDCH.
+ *
+ * (C) 2017 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * Author: Neels Hofmeyr <neels@hofmeyr.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/core/logging.h>
+
+#include <osmocom/bsc/debug.h>
+
+#include <osmocom/bsc/timeslot_fsm.h>
+#include <osmocom/bsc/lchan_fsm.h>
+#include <osmocom/bsc/abis_rsl.h>
+#include <osmocom/bsc/pcu_if.h>
+
+static struct osmo_fsm ts_fsm;
+
+#define CHAN_ACT_DEACT_TIMEOUT 4 /* TODO: proper T number? */
+
+enum ts_fsm_T {
+ T_CHAN_ACT_DEACT=23001,
+};
+
+#define GET_TS(fi, TS) \
+ struct gsm_bts_trx_ts *TS = fi->priv; \
+ OSMO_ASSERT((fi)->fsm == &ts_fsm && (fi)->priv)
+
+#define GET_BTS_TS(fi, BTS, TS) \
+ GET_TS(fi, TS); \
+ struct gsm_bts *BTS = TS->trx->bts
+
+static void ts_fsm_update_id(struct gsm_bts_trx_ts *ts)
+{
+ osmo_fsm_inst_update_id_f(ts->fi, "%u-%u-%u-%s", ts->trx->bts->nr, ts->trx->nr, ts->nr,
+ gsm_pchan_id(ts->pchan_on_init));
+}
+
+void ts_fsm_alloc(struct gsm_bts_trx_ts *ts)
+{
+ static bool g_initialized = false;
+ if (!g_initialized) {
+ OSMO_ASSERT(osmo_fsm_register(&ts_fsm) == 0);
+ g_initialized = true;
+ }
+ OSMO_ASSERT(!ts->fi);
+ OSMO_ASSERT(ts->trx);
+ ts->fi = osmo_fsm_inst_alloc(&ts_fsm, ts->trx, ts, LOGL_DEBUG, NULL);
+ OSMO_ASSERT(ts->fi);
+}
+
+enum lchan_sanity {
+ LCHAN_IS_INSANE = -1,
+ LCHAN_IS_READY_TO_GO,
+ LCHAN_NEEDS_PCHAN_CHANGE,
+};
+
+static enum lchan_sanity is_lchan_sane(struct gsm_bts_trx_ts *ts, struct gsm_lchan *lchan)
+{
+ OSMO_ASSERT(ts);
+ OSMO_ASSERT(lchan);
+ if (lchan->ts != ts)
+ return LCHAN_IS_INSANE;
+ if (!lchan->fi)
+ return LCHAN_IS_INSANE;
+
+ if (lchan->type == gsm_lchan_type_by_pchan(ts->pchan_is))
+ return LCHAN_IS_READY_TO_GO;
+
+ switch (ts->pchan_on_init) {
+ case GSM_PCHAN_TCH_F_TCH_H_PDCH:
+ if (lchan->type == GSM_LCHAN_TCH_H)
+ return LCHAN_NEEDS_PCHAN_CHANGE;
+ /* fall thru */
+ case GSM_PCHAN_TCH_F_PDCH:
+ if (lchan->type == GSM_LCHAN_TCH_F)
+ return LCHAN_NEEDS_PCHAN_CHANGE;
+ /* fall thru */
+ default:
+ return LCHAN_IS_INSANE;
+ }
+
+}
+
+static void lchan_dispatch(struct gsm_lchan *lchan, uint32_t lchan_ev)
+{
+ if (!lchan->fi)
+ return;
+ osmo_fsm_inst_dispatch(lchan->fi, lchan_ev, NULL);
+ OSMO_ASSERT(lchan->fi->state != LCHAN_ST_WAIT_TS_READY);
+}
+
+static int ts_count_active_lchans(struct gsm_bts_trx_ts *ts)
+{
+ struct gsm_lchan *lchan;
+ int count = 0;
+
+ ts_for_each_lchan(lchan, ts) {
+ if (lchan->fi->state == LCHAN_ST_UNUSED)
+ continue;
+ count++;
+ }
+
+ return count;
+}
+
+static void ts_lchans_dispatch(struct gsm_bts_trx_ts *ts, int lchan_state, uint32_t lchan_ev)
+{
+ struct gsm_lchan *lchan;
+
+ ts_for_each_lchan(lchan, ts) {
+ if (lchan_state >= 0
+ && !lchan_state_is(lchan, lchan_state))
+ continue;
+ lchan_dispatch(lchan, lchan_ev);
+ }
+}
+
+static void ts_terminate_lchan_fsms(struct gsm_bts_trx_ts *ts)
+{
+ struct gsm_lchan *lchan;
+
+ ts_for_each_lchan(lchan, ts) {
+ osmo_fsm_inst_term(lchan->fi, OSMO_FSM_TERM_REQUEST, NULL);
+ }
+}
+
+static int ts_lchans_waiting(struct gsm_bts_trx_ts *ts)
+{
+ struct gsm_lchan *lchan;
+ int count = 0;
+ ts_for_each_lchan(lchan, ts)
+ if (lchan->fi->state == LCHAN_ST_WAIT_TS_READY)
+ count++;
+ return count;
+}
+
+static void ts_fsm_error(struct osmo_fsm_inst *fi, uint32_t state_chg, const char *fmt, ...)
+{
+ GET_TS(fi, ts);
+
+ char *errmsg = NULL;
+
+ if (fmt) {
+ va_list ap;
+
+ va_start(ap, fmt);
+ errmsg = talloc_vasprintf(ts->trx, fmt, ap);
+ va_end(ap);
+ }
+
+ if (ts->last_errmsg)
+ talloc_free(ts->last_errmsg);
+ ts->last_errmsg = errmsg;
+
+ if (errmsg)
+ LOG_TS(ts, LOGL_ERROR, "%s", errmsg);
+
+ ts_lchans_dispatch(ts, LCHAN_ST_WAIT_TS_READY, LCHAN_EV_TS_ERROR);
+ if (fi->state != state_chg)
+ osmo_fsm_inst_state_chg(fi, state_chg, 0, 0);
+}
+
+static void ts_fsm_err_ready_to_go_in_pdch(struct osmo_fsm_inst *fi, struct gsm_lchan *lchan)
+{
+ /* This shouldn't ever happen, so aggressively mark it. */
+ ts_fsm_error(fi, TS_ST_BORKEN,
+ "Internal error: lchan marked as 'ready to go', but activating"
+ " any lchan should need PCHAN switchover in state %s (lchan: %s)",
+ osmo_fsm_inst_state_name(fi), gsm_lchan_name(lchan));
+}
+
+static void ts_setup_lchans(struct gsm_bts_trx_ts *ts)
+{
+ int i, max_lchans;
+
+ ts->pchan_on_init = ts->pchan_from_config;
+ ts_fsm_update_id(ts);
+
+ max_lchans = pchan_subslots(ts->pchan_on_init);
+ LOG_TS(ts, LOGL_DEBUG, "max lchans: %d", max_lchans);
+
+ for (i = 0; i < max_lchans; i++) {
+ /* We sometimes receive more than one Channel OPSTART ACK. Not quite sure why, just
+ * seeing it happen with bts_ipaccess_nanobts. Don't fail on the second init. */
+ if (ts->lchan[i].fi)
+ continue;
+ lchan_fsm_alloc(&ts->lchan[i]);
+ }
+
+ switch (ts->pchan_on_init) {
+ case GSM_PCHAN_TCH_F_TCH_H_PDCH:
+ ts->pchan_is = GSM_PCHAN_NONE;
+ break;
+ case GSM_PCHAN_TCH_F_PDCH:
+ ts->pchan_is = GSM_PCHAN_TCH_F;
+ break;
+ default:
+ ts->pchan_is = ts->pchan_on_init;
+ break;
+ }
+
+ LOG_TS(ts, LOGL_DEBUG, "lchans initialized: %d", max_lchans);
+}
+
+static void ts_fsm_not_initialized(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_BTS_TS(fi, bts, ts);
+ switch (event) {
+
+ case TS_EV_OML_READY:
+ ts->pdch_act_allowed = true;
+ ts_setup_lchans(ts);
+ if (!ts->trx->rsl_link) {
+ LOG_TS(ts, LOGL_DEBUG, "No RSL link yet");
+ return;
+ }
+ /* -> UNUSED below */
+ break;
+
+ case TS_EV_RSL_READY:
+ ts->pdch_act_allowed = true;
+ if (bts->model->oml_is_ts_ready
+ && !bts->model->oml_is_ts_ready(ts)) {
+ LOG_TS(ts, LOGL_DEBUG, "OML not ready yet");
+ return;
+ }
+ /* -> UNUSED below */
+ break;
+
+ case TS_EV_LCHAN_REQUESTED:
+ {
+ /* TS is not initialized, no lchan can be requested. */
+ struct gsm_lchan *lchan = data;
+ if (lchan && lchan->fi)
+ osmo_fsm_inst_dispatch(fi, LCHAN_EV_TS_ERROR, NULL);
+ }
+ return;
+
+ case TS_EV_LCHAN_UNUSED:
+ /* ignored. */
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+
+ osmo_fsm_inst_state_chg(fi, TS_ST_UNUSED, 0, 0);
+}
+
+static void ts_fsm_unused_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ GET_BTS_TS(fi, bts, ts);
+
+ /* We are entering the unused state. There must by definition not be any lchans waiting to be
+ * activated. */
+ if (ts_lchans_waiting(ts)) {
+ ts_fsm_error(fi, TS_ST_BORKEN,
+ "Internal error: entering UNUSED state, but there are lchans waiting to be"
+ " activated. Not activating them to prevent infinite loops.");
+ return;
+ }
+
+ switch (ts->pchan_on_init) {
+ case GSM_PCHAN_TCH_F_TCH_H_PDCH:
+ case GSM_PCHAN_TCH_F_PDCH:
+ if (bts->gprs.mode == BTS_GPRS_NONE) {
+ LOG_TS(ts, LOGL_DEBUG, "GPRS mode is 'none': not activating PDCH.");
+ return;
+ }
+ if (!ts->pdch_act_allowed) {
+ LOG_TS(ts, LOGL_DEBUG, "PDCH is disabled for this timeslot,"
+ " either due to a PDCH ACT NACK, or from manual VTY command:"
+ " not activating PDCH. (last error: %s)",
+ ts->last_errmsg ? : "-");
+ return;
+ }
+ osmo_fsm_inst_state_chg(fi, TS_ST_WAIT_PDCH_ACT, CHAN_ACT_DEACT_TIMEOUT,
+ T_CHAN_ACT_DEACT);
+ break;
+
+ default:
+ /* nothing to do */
+ break;
+ }
+}
+
+static void ts_fsm_unused(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_TS(fi, ts);
+
+ switch (event) {
+
+ case TS_EV_LCHAN_REQUESTED:
+ {
+ struct gsm_lchan *lchan = data;
+ switch (is_lchan_sane(ts, lchan)) {
+ case LCHAN_NEEDS_PCHAN_CHANGE:
+ /* Osmocom style dyn TS: in UNUSED state, PDCH is already switched off,
+ * we merely need to RSL Chan Activ the new lchan. For ip.access style
+ * dyn TS this is already TCH/F, and we should never hit this. */
+ case LCHAN_IS_READY_TO_GO:
+ osmo_fsm_inst_state_chg(fi, TS_ST_IN_USE, 0, 0);
+ return;
+ default:
+ osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_TS_ERROR, NULL);
+ return;
+ }
+ }
+
+ case TS_EV_LCHAN_UNUSED:
+ /* ignored. */
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static inline void ts_fsm_pdch_deact(struct osmo_fsm_inst *fi)
+{
+ osmo_fsm_inst_state_chg(fi, TS_ST_WAIT_PDCH_DEACT, CHAN_ACT_DEACT_TIMEOUT, T_CHAN_ACT_DEACT);
+}
+
+static void ts_fsm_wait_pdch_act_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int rc;
+ GET_TS(fi, ts);
+
+ rc = rsl_tx_dyn_ts_pdch_act_deact(ts, true);
+
+ /* On error, we couldn't send the activation message and remain unused. */
+ if (rc)
+ ts_fsm_error(fi, TS_ST_UNUSED, "Unable to send PDCH activation");
+}
+
+static void ts_fsm_wait_pdch_act(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_TS(fi, ts);
+ switch (event) {
+
+ case TS_EV_PDCH_ACT_ACK:
+ osmo_fsm_inst_state_chg(fi, TS_ST_PDCH, 0, 0);
+ return;
+
+ case TS_EV_PDCH_ACT_NACK:
+ if (ts->pchan_on_init == GSM_PCHAN_TCH_F_PDCH)
+ rate_ctr_inc(&ts->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]);
+ else
+ rate_ctr_inc(&ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_ACT_NACK]);
+ ts->pdch_act_allowed = false;
+ ts_fsm_error(fi, TS_ST_UNUSED, "Received PDCH activation NACK");
+ return;
+
+ case TS_EV_LCHAN_REQUESTED:
+ {
+ struct gsm_lchan *lchan = data;
+ switch (is_lchan_sane(ts, lchan)) {
+ case LCHAN_IS_READY_TO_GO:
+ /* PDCH activation has not been acked, the previous pchan kind may still
+ * linger in ts->pchan and make it look like the ts is usable right away.
+ * But we've started the switchover and must finish that first. */
+ case LCHAN_NEEDS_PCHAN_CHANGE:
+ /* PDCH onenter will see that the lchan is waiting and continue to switch
+ * off PDCH right away. */
+ return;
+
+ default:
+ lchan_dispatch(lchan, LCHAN_EV_TS_ERROR);
+ return;
+ }
+ }
+
+ case TS_EV_LCHAN_UNUSED:
+ /* ignored. */
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void ts_fsm_pdch_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int count;
+ GET_TS(fi, ts);
+
+ /* Set pchan = PDCH status, but double check. */
+ switch (ts->pchan_on_init) {
+ case GSM_PCHAN_TCH_F_TCH_H_PDCH:
+ case GSM_PCHAN_TCH_F_PDCH:
+ case GSM_PCHAN_PDCH:
+ ts->pchan_is = GSM_PCHAN_PDCH;
+ break;
+ default:
+ ts_fsm_error(fi, TS_ST_BORKEN, "pchan %s is incapable of activating PDCH",
+ gsm_pchan_name(ts->pchan_on_init));
+ return;
+ }
+
+ /* PDCH use has changed, tell the PCU about it. */
+ pcu_info_update(ts->trx->bts);
+
+ /* If we received TS_EV_LCHAN_REQUESTED in the meantime, go right out of PDCH again. */
+ if ((count = ts_lchans_waiting(ts))) {
+ LOG_TS(ts, LOGL_DEBUG, "%d lchan(s) waiting for usable timeslot", count);
+ ts_fsm_pdch_deact(fi);
+ }
+}
+
+static void ts_fsm_pdch(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_TS(fi, ts);
+ switch (event) {
+
+ case TS_EV_LCHAN_REQUESTED:
+ {
+ struct gsm_lchan *lchan = data;
+ switch (is_lchan_sane(ts, lchan)) {
+ case LCHAN_NEEDS_PCHAN_CHANGE:
+ ts_fsm_pdch_deact(fi);
+ return;
+
+ case LCHAN_IS_READY_TO_GO:
+ ts_fsm_err_ready_to_go_in_pdch(fi, lchan);
+ return;
+
+ default:
+ /* Reject just this lchan. */
+ lchan_dispatch(lchan, LCHAN_EV_TS_ERROR);
+ return;
+ }
+ }
+
+ case TS_EV_LCHAN_UNUSED:
+ /* ignored */
+ return;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void ts_fsm_wait_pdch_deact_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+ int rc;
+ GET_TS(fi, ts);
+
+ rc = rsl_tx_dyn_ts_pdch_act_deact(ts, false);
+
+ /* On error, we couldn't send the deactivation message and remain in PDCH. */
+ if (rc)
+ ts_fsm_error(fi, TS_ST_PDCH, "Unable to send PDCH deactivation");
+}
+
+static void ts_fsm_wait_pdch_deact(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_TS(fi, ts);
+ switch (event) {
+
+ case TS_EV_PDCH_DEACT_ACK:
+ /* Remove pchan = PDCH status, but double check. */
+ switch (ts->pchan_on_init) {
+ case GSM_PCHAN_TCH_F_TCH_H_PDCH:
+ ts->pchan_is = GSM_PCHAN_NONE;
+ break;
+ case GSM_PCHAN_TCH_F_PDCH:
+ ts->pchan_is = GSM_PCHAN_TCH_F;
+ break;
+ default:
+ ts_fsm_error(fi, TS_ST_BORKEN, "pchan %s is incapable of deactivating PDCH",
+ gsm_pchan_name(ts->pchan_on_init));
+ return;
+ }
+ osmo_fsm_inst_state_chg(fi, TS_ST_IN_USE, 0, 0);
+ /* IN_USE onenter will signal all waiting lchans. */
+
+ /* PDCH use has changed, tell the PCU about it. */
+ pcu_info_update(ts->trx->bts);
+ return;
+
+ case TS_EV_PDCH_DEACT_NACK:
+ if (ts->pchan_on_init == GSM_PCHAN_TCH_F_PDCH)
+ rate_ctr_inc(&ts->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]);
+ /* For Osmocom style dyn TS, there actually is no NACK, since there is no RF Channel
+ * Release NACK message in RSL. */
+ ts_fsm_error(fi, TS_ST_BORKEN, "Received PDCH deactivation NACK");
+ return;
+
+ case TS_EV_LCHAN_REQUESTED:
+ {
+ struct gsm_lchan *lchan = data;
+ switch (is_lchan_sane(ts, lchan)) {
+ case LCHAN_NEEDS_PCHAN_CHANGE:
+ /* IN_USE onenter will see that the lchan is waiting and signal it. */
+ return;
+
+ case LCHAN_IS_READY_TO_GO:
+ ts_fsm_err_ready_to_go_in_pdch(fi, lchan);
+ return;
+
+ default:
+ /* Reject just this lchan. */
+ lchan_dispatch(lchan, LCHAN_EV_TS_ERROR);
+ return;
+ }
+ }
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void ts_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_event)
+{
+ int in_use;
+ struct gsm_lchan *lchan;
+ GET_TS(fi, ts);
+
+ /* After being in use, allow PDCH act again, if appropriate. */
+ ts->pdch_act_allowed = true;
+
+ ts_lchans_dispatch(ts, LCHAN_ST_WAIT_TS_READY, LCHAN_EV_TS_READY);
+
+ in_use = 0;
+ ts_for_each_lchan(lchan, ts) {
+ if (lchan_state_is(lchan, LCHAN_ST_UNUSED))
+ continue;
+ in_use ++;
+ }
+ if (!in_use) {
+ LOG_TS(ts, LOGL_DEBUG, "Entered IN_USE state but no lchans are actually in use now.");
+ osmo_fsm_inst_state_chg(fi, TS_ST_UNUSED, 0, 0);
+ }
+}
+
+static void ts_fsm_in_use(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_TS(fi, ts);
+ switch (event) {
+ case TS_EV_LCHAN_UNUSED:
+ if (!ts_count_active_lchans(ts))
+ osmo_fsm_inst_state_chg(fi, TS_ST_UNUSED, 0, 0);
+ return;
+
+ case TS_EV_LCHAN_REQUESTED:
+ {
+ struct gsm_lchan *lchan = data;
+ switch (is_lchan_sane(ts, lchan)) {
+ case LCHAN_IS_READY_TO_GO:
+ osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_TS_READY, NULL);
+ return;
+
+ case LCHAN_NEEDS_PCHAN_CHANGE:
+ LOG_TS(ts, LOGL_ERROR,
+ "cannot activate lchan of mismatching pchan type"
+ " when the TS is already in use: %s",
+ gsm_lchan_name(lchan));
+ /* fall thru */
+ default:
+ /* Reject just this lchan. */
+ lchan_dispatch(lchan, LCHAN_EV_TS_ERROR);
+ return;
+ }
+ }
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static void ts_fsm_borken(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ switch (event) {
+ case TS_EV_LCHAN_UNUSED:
+ /* ignored */
+ return;
+
+ case TS_EV_LCHAN_REQUESTED:
+ {
+ struct gsm_lchan *lchan = data;
+ lchan_dispatch(lchan, LCHAN_EV_TS_ERROR);
+ return;
+ }
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+static int ts_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+ switch (fi->state) {
+ case TS_ST_WAIT_PDCH_ACT:
+ ts_fsm_error(fi, TS_ST_BORKEN, "PDCH activation timeout");
+ return 0;
+
+ case TS_ST_WAIT_PDCH_DEACT:
+ ts_fsm_error(fi, TS_ST_BORKEN, "PDCH deactivation timeout");
+ return 0;
+
+ default:
+ ts_fsm_error(fi, TS_ST_BORKEN, "Unknown timeout in state %s",
+ osmo_fsm_inst_state_name(fi));
+ return 0;
+ }
+}
+
+static void ts_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ GET_TS(fi, ts);
+ switch (event) {
+ case TS_EV_OML_DOWN:
+ if (fi->state != TS_ST_NOT_INITIALIZED)
+ osmo_fsm_inst_state_chg(fi, TS_ST_NOT_INITIALIZED, 0, 0);
+ OSMO_ASSERT(fi->state == TS_ST_NOT_INITIALIZED);
+ ts_terminate_lchan_fsms(ts);
+ ts->pchan_is = ts->pchan_on_init = GSM_PCHAN_NONE;
+ ts_fsm_update_id(ts);
+ break;
+
+ case TS_EV_RSL_DOWN:
+ if (fi->state != TS_ST_NOT_INITIALIZED)
+ osmo_fsm_inst_state_chg(fi, TS_ST_NOT_INITIALIZED, 0, 0);
+ OSMO_ASSERT(fi->state == TS_ST_NOT_INITIALIZED);
+ ts->pchan_is = GSM_PCHAN_NONE;
+ ts_lchans_dispatch(ts, -1, LCHAN_EV_TS_ERROR);
+ break;
+
+ default:
+ OSMO_ASSERT(false);
+ }
+}
+
+#define S(x) (1 << (x))
+
+static const struct osmo_fsm_state ts_fsm_states[] = {
+ [TS_ST_NOT_INITIALIZED] = {
+ .name = "NOT_INITIALIZED",
+ .action = ts_fsm_not_initialized,
+ .in_event_mask = 0
+ | S(TS_EV_OML_READY)
+ | S(TS_EV_RSL_READY)
+ | S(TS_EV_LCHAN_REQUESTED)
+ | S(TS_EV_LCHAN_UNUSED)
+ ,
+ .out_state_mask = 0
+ | S(TS_ST_UNUSED)
+ ,
+ },
+ [TS_ST_UNUSED] = {
+ .name = "UNUSED",
+ .onenter = ts_fsm_unused_onenter,
+ .action = ts_fsm_unused,
+ .in_event_mask = 0
+ | S(TS_EV_LCHAN_REQUESTED)
+ | S(TS_EV_LCHAN_UNUSED)
+ ,
+ .out_state_mask = 0
+ | S(TS_ST_WAIT_PDCH_ACT)
+ | S(TS_ST_IN_USE)
+ | S(TS_ST_NOT_INITIALIZED)
+ ,
+ },
+ [TS_ST_WAIT_PDCH_ACT] = {
+ .name = "WAIT_PDCH_ACT",
+ .onenter = ts_fsm_wait_pdch_act_onenter,
+ .action = ts_fsm_wait_pdch_act,
+ .in_event_mask = 0
+ | S(TS_EV_PDCH_ACT_ACK)
+ | S(TS_EV_PDCH_ACT_NACK)
+ | S(TS_EV_LCHAN_REQUESTED)
+ | S(TS_EV_LCHAN_UNUSED)
+ ,
+ .out_state_mask = 0
+ | S(TS_ST_PDCH)
+ | S(TS_ST_UNUSED)
+ | S(TS_ST_BORKEN)
+ | S(TS_ST_NOT_INITIALIZED)
+ ,
+ },
+ [TS_ST_PDCH] = {
+ .name = "PDCH",
+ .onenter = ts_fsm_pdch_onenter,
+ .action = ts_fsm_pdch,
+ .in_event_mask = 0
+ | S(TS_EV_LCHAN_REQUESTED)
+ | S(TS_EV_LCHAN_UNUSED)
+ ,
+ .out_state_mask = 0
+ | S(TS_ST_WAIT_PDCH_DEACT)
+ | S(TS_ST_NOT_INITIALIZED)
+ ,
+ },
+ [TS_ST_WAIT_PDCH_DEACT] = {
+ .name = "WAIT_PDCH_ACT",
+ .onenter = ts_fsm_wait_pdch_deact_onenter,
+ .action = ts_fsm_wait_pdch_deact,
+ .in_event_mask = 0
+ | S(TS_EV_PDCH_DEACT_ACK)
+ | S(TS_EV_PDCH_DEACT_NACK)
+ | S(TS_EV_LCHAN_REQUESTED)
+ | S(TS_EV_LCHAN_UNUSED)
+ ,
+ .out_state_mask = 0
+ | S(TS_ST_IN_USE)
+ | S(TS_ST_UNUSED)
+ | S(TS_ST_BORKEN)
+ | S(TS_ST_NOT_INITIALIZED)
+ ,
+ },
+ [TS_ST_IN_USE] = {
+ .name = "IN_USE",
+ .onenter = ts_fsm_in_use_onenter,
+ .action = ts_fsm_in_use,
+ .in_event_mask = 0
+ | S(TS_EV_LCHAN_REQUESTED)
+ | S(TS_EV_LCHAN_UNUSED)
+ ,
+ .out_state_mask = 0
+ | S(TS_ST_UNUSED)
+ | S(TS_ST_NOT_INITIALIZED)
+ ,
+ },
+ [TS_ST_BORKEN] = {
+ .name = "BORKEN",
+ .action = ts_fsm_borken,
+ .in_event_mask = 0
+ | S(TS_EV_LCHAN_REQUESTED)
+ | S(TS_EV_LCHAN_UNUSED)
+ ,
+ .out_state_mask = 0
+ | S(TS_ST_NOT_INITIALIZED)
+ ,
+ },
+
+};
+
+static const struct value_string ts_fsm_event_names[] = {
+ OSMO_VALUE_STRING(TS_EV_OML_READY),
+ OSMO_VALUE_STRING(TS_EV_OML_DOWN),
+ OSMO_VALUE_STRING(TS_EV_RSL_READY),
+ OSMO_VALUE_STRING(TS_EV_RSL_DOWN),
+ OSMO_VALUE_STRING(TS_EV_LCHAN_REQUESTED),
+ OSMO_VALUE_STRING(TS_EV_LCHAN_UNUSED),
+ OSMO_VALUE_STRING(TS_EV_PDCH_ACT_ACK),
+ OSMO_VALUE_STRING(TS_EV_PDCH_ACT_NACK),
+ OSMO_VALUE_STRING(TS_EV_PDCH_DEACT_ACK),
+ OSMO_VALUE_STRING(TS_EV_PDCH_DEACT_NACK),
+ {}
+};
+
+static struct osmo_fsm ts_fsm = {
+ .name = "timeslot",
+ .states = ts_fsm_states,
+ .num_states = ARRAY_SIZE(ts_fsm_states),
+ .timer_cb = ts_fsm_timer_cb,
+ .log_subsys = DRSL,
+ .event_names = ts_fsm_event_names,
+ .allstate_event_mask = 0
+ | S(TS_EV_OML_DOWN)
+ | S(TS_EV_RSL_DOWN)
+ ,
+ .allstate_action = ts_fsm_allstate,
+};
+
+/* Return true if any lchans are waiting for this timeslot to become a specific PCHAN. If target_pchan is
+ * not NULL, also return the PCHAN being waited for. */
+bool ts_is_lchan_waiting_for_pchan(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config *target_pchan)
+{
+ struct gsm_lchan *lchan;
+ ts_for_each_lchan(lchan, ts) {
+ if (lchan->fi->state == LCHAN_ST_WAIT_TS_READY) {
+ if (target_pchan)
+ *target_pchan = gsm_pchan_by_lchan_type(lchan->type);
+ return true;
+ }
+ }
+
+ if (target_pchan)
+ *target_pchan = ts->pchan_is;
+ return false;
+}
+
+/* Return true if we are busy changing the PCHAN kind. If target_pchan is not NULL, also return the PCHAN
+ * (ultimately) being switched to. */
+bool ts_is_pchan_switching(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config *target_pchan)
+{
+ switch (ts->fi->state) {
+ case TS_ST_NOT_INITIALIZED:
+ case TS_ST_BORKEN:
+ return false;
+ default:
+ break;
+ }
+
+ /* If an lchan is waiting, return the final pchan after all switching is done. */
+ if (ts_is_lchan_waiting_for_pchan(ts, target_pchan))
+ return true;
+
+ /* No lchans waiting. Return any ongoing switching. */
+
+ switch (ts->fi->state) {
+ case TS_ST_WAIT_PDCH_ACT:
+ if (target_pchan)
+ *target_pchan = GSM_PCHAN_PDCH;
+ return true;
+
+ case TS_ST_WAIT_PDCH_DEACT:
+ /* If we were switching to a specific pchan kind, an lchan would be waiting. So this must
+ * be NONE then. */
+ if (target_pchan)
+ *target_pchan = GSM_PCHAN_NONE;
+ return true;
+
+ default:
+ return false;
+ }
+}
+
+/* Does the timeslot's *current* state allow use as this PCHAN kind? If the ts is in switchover, return
+ * true if the switchover's target PCHAN matches, i.e. an lchan for this pchan kind could be requested
+ * and will be served after the switch. (Do not check whether any lchans are actually available.) */
+bool ts_usable_as_pchan(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config as_pchan)
+{
+ if (!ts_is_usable(ts))
+ return false;
+
+ switch (ts->fi->state) {
+ case TS_ST_IN_USE:
+ return ts->pchan_is == as_pchan;
+
+ default:
+ break;
+ }
+
+ {
+ enum gsm_phys_chan_config target_pchan;
+ if (ts_is_lchan_waiting_for_pchan(ts, &target_pchan))
+ return target_pchan == as_pchan;
+ }
+
+ return ts_is_capable_of_pchan(ts, as_pchan);
+}
diff --git a/src/stubs/lchan_fsm_stubs.c b/src/stubs/lchan_fsm_stubs.c
new file mode 100644
index 000000000..a0129c92b
--- /dev/null
+++ b/src/stubs/lchan_fsm_stubs.c
@@ -0,0 +1,3 @@
+#include <osmocom/bsc/lchan_fsm.h>
+
+void lchan_activate(struct gsm_lchan *lchan, struct lchan_activate_info *info) {}
diff --git a/src/stubs/system_information_stubs.c b/src/stubs/system_information_stubs.c
new file mode 100644
index 000000000..bcb65902b
--- /dev/null
+++ b/src/stubs/system_information_stubs.c
@@ -0,0 +1,7 @@
+#include <osmocom/bsc/neighbor_ident.h>
+
+void neighbor_ident_iter(const struct neighbor_ident_list *nil,
+ bool (* iter_cb )(const struct neighbor_ident_key *key,
+ const struct gsm0808_cell_id_list2 *val,
+ void *cb_data),
+ void *cb_data) {}
diff --git a/src/stubs/ts_fsm_stubs.c b/src/stubs/ts_fsm_stubs.c
new file mode 100644
index 000000000..012f6164c
--- /dev/null
+++ b/src/stubs/ts_fsm_stubs.c
@@ -0,0 +1,3 @@
+#include <osmocom/bsc/timeslot_fsm.h>
+
+void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {}
diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am
index 543344b53..485ec9e69 100644
--- a/src/utils/Makefile.am
+++ b/src/utils/Makefile.am
@@ -46,6 +46,7 @@ endif
bs11_config_SOURCES = \
bs11_config.c \
+ $(top_srcdir)/src/stubs/ts_fsm_stubs.c \
stubs.c \
$(NULL)
@@ -54,6 +55,7 @@ bs11_config_LDADD = \
$(top_builddir)/src/osmo-bsc/bts_siemens_bs11.o \
$(top_builddir)/src/osmo-bsc/e1_config.o \
$(top_builddir)/src/osmo-bsc/gsm_data.o \
+ $(top_builddir)/src/osmo-bsc/gsm_timers.o \
$(top_builddir)/src/osmo-bsc/net_init.o \
$(LIBOSMOCORE_LIBS) \
$(LIBOSMOGSM_LIBS) \
@@ -117,11 +119,13 @@ osmo_meas_udp2db_CFLAGS = \
meas_json_SOURCES = \
meas_json.c \
+ $(top_srcdir)/src/stubs/ts_fsm_stubs.c \
stubs.c \
$(NULL)
meas_json_LDADD = \
$(top_builddir)/src/osmo-bsc/gsm_data.o \
+ $(top_builddir)/src/osmo-bsc/gsm_timers.o \
$(LIBOSMOCORE_LIBS) \
$(LIBOSMOGSM_LIBS) \
$(LIBOSMOABIS_LIBS) \