large refactoring: use FSMs for lchans; add inter-BSC HO

Add FSMs:

- timeslot_fsm: handle dynamic timeslots and OML+RSL availability.
- lchan_fsm: handle an individual lchan activation, RTP stream and release,
  signal the appropriate calling FSMs on success, failure, release.
- mgw_endpoint_fsm: handle one entire endpoint with several CI.
- assignment_fsm: BSSMAP Assignment Request.
- handover_fsm: all of intra, inter-MO and inter-MT handover.

Above FSMs absorb large parts of the gscon FSM. The gscon FSM was surpassing
the maximum amount events (32), and it is more logical to treat assignment,
handover and MGW procedures in separate FSMs.

- Add logging macros for each FSM type:
  - LOG_TS()
  - LOG_LCHAN()
  - LOG_MGWEP(), LOG_CI()
  - LOG_ASSIGNMENT()
  - LOG_HO()
  These log with the osmo_fsm_inst where present.
  New style decision: logging without a final newline char is awkward,
  especially for gsmtap logging and when other logs interleave LOGPC() calls;
  we have various cases where the final \n goes missing, and also this invokes
  the log category checking N times instead of once.
  So I decided to make these macros *always* append a newline, but only if
  there is no final newline yet. I hope that the compiler optimizes the
  strlen() of the constant format strings away. Thus I can log with or without
  typing "\n" and always get an \n termination anyway.

General:

- replace osmo_timers, state enums and program-wide osmo_signal_dispatch()
  with dedicated FSM timeouts, states and events.

- introduce a common way to handle Tnnn timers: gsm_timers.h/.c: struct T_def.
  These can be used (with some macro magic) to define a state's timeout once,
  and not make mistakes for each osmo_fsm_inst_state_chg().

Details:

bsc_subscr_conn_fsm.c:

- move most states of this FSM to lchan_fsm, assignment_fsm, handover_fsm and
  mgw_endpoint_fsm.

- There is exactly one state for an ongoing Assignment, with all details
  handled in conn->assignment.fi. The state relies on the assignment_fsm's
  timeout.

- There is one state for an ongoing Handover; except for an incoming Handover
  from a remote BSS, the gscon remains in ST_INIT until the new lchan and conn
  are both established.

- move bssmap_add_lcls_status() to osmo_bsc_lcls.c

abis_rsl.c:

- move all dynamic timeslot logic away into timeslot_fsm. Only keep plain send/receive functions in
  abis_rsl.c

- reduce some rsl functions to merely send a message, rename to "_tx_".
  - rsl_ipacc_mdcx(): add '_tx_' in the name; move parts that change the lchan state out into the
    lchan_fsm, the lchan->abis_ip.* are now set there prior to invoking this function.

- move all timers and error/release handling away into various FSMs.

- tweak ipa_smod_s_for_lchan() and ipa_rtp_pt_for_lchan() to not require an
  lchan passed, but just mode,type that they require. Rename to
  ipacc_speech_mode*() and ipacc_payload_type().

- add rsl_forward_layer3_info, used for inter-BSC HO MO, to just send the RR
  message received during BSSMAP Handover Command.

- move various logging to LOG_LCHAN() in order to log with the lchan FSM instance.
  One drawback is that the lchan FSM is limited to one logging category, i.e. this moves some logging
  from DRR to DRSL. It might actually make sense to combine those categories.

- lose LOGP...LOGPC logging cascades: they are bad for gsmtap logging and for performance.

- handle_classmark_chg(): change logging, move cm2 len check out of the cm3 condition (I hope that's
  correct).

- gsm48_send_ho_cmd(): split off gsm48_make_ho_cmd() which doesn't send right away, so that during
  inter-bsc HO we can make an RR Handover Command to send via the MSC to the remote BSS.

assignment_fsm.c:

- the Chan Mode Modify in case of re-using the same lchan is not implemented
  yet, because this was also missing in the previous implementation (OS#3357).

osmo_bsc_api.c:

- simplify bsc_mr_config() and move to lchan_fsm.c, the only caller; rename to
  lchan_mr_config(). (bsc_mr_config() used to copy the values to mr_bts_lv
  twice, once by member assignment and then again with a memcpy.)

- During handover, we used to copy the MR config from the old lchan. Since we
  may handover between FR and HR, rather set the MR Config anew every time, so
  that FR rates are always available on FR lchans, and never on HR lchans.

Depends: I03ee7ce840ecfa0b6a33358e7385528aabd4873f (libosmocore),
         I1f2918418c38918c5ac70acaa51a47adfca12b5e (libosmocore)
Change-Id: I82e3f918295daa83274a4cf803f046979f284366
diff --git a/include/osmocom/bsc/bsc_subscr_conn_fsm.h b/include/osmocom/bsc/bsc_subscr_conn_fsm.h
index 70fce32..d4de1c9 100644
--- a/include/osmocom/bsc/bsc_subscr_conn_fsm.h
+++ b/include/osmocom/bsc/bsc_subscr_conn_fsm.h
@@ -8,28 +8,20 @@
 	GSCON_EV_A_CONN_REQ,
 	/* MSC confirms the SCCP connection */
 	GSCON_EV_A_CONN_CFM,
-	/* MSC requests assignment */
-	GSCON_EV_A_ASSIGNMENT_CMD,
 	/* MSC has sent BSSMAP CLEAR CMD */
 	GSCON_EV_A_CLEAR_CMD,
 	/* MSC SCCP disconnect indication */
 	GSCON_EV_A_DISC_IND,
-	/* MSC sends Handover Request (in CR) */
-	GSCON_EV_A_HO_REQ,
 
-	/* RR ASSIGNMENT COMPLETE received */
-	GSCON_EV_RR_ASS_COMPL,
-	/* RR ASSIGNMENT FAIL received */
-	GSCON_EV_RR_ASS_FAIL,
+	GSCON_EV_ASSIGNMENT_START,
+	GSCON_EV_ASSIGNMENT_END,
 
-	/* RSL RLL Release Indication */
-	GSCON_EV_RLL_REL_IND,
+	GSCON_EV_HANDOVER_START,
+	GSCON_EV_HANDOVER_END,
+
 	/* RSL CONNection FAILure Indication */
 	GSCON_EV_RSL_CONN_FAIL,
 
-	/* RSL/lchan tells us clearing is complete */
-	GSCON_EV_RSL_CLEAR_COMPL,
-
 	/* Mobile-originated DTAP (from MS) */
 	GSCON_EV_MO_DTAP,
 	/* Mobile-terminated DTAP (from MSC) */
@@ -38,42 +30,53 @@
 	/* Transmit custom SCCP message */
 	GSCON_EV_TX_SCCP,
 
-	/* MGW is indicating failure (BTS) */
-	GSCON_EV_MGW_FAIL_BTS,
-	/* MGW is indicating failure (MSC) */
-	GSCON_EV_MGW_FAIL_MSC,
-	/* CRCX response received (BTS) */
-	GSCON_EV_MGW_CRCX_RESP_BTS,
-	/* MDCX response received (BTS) */
-	GSCON_EV_MGW_MDCX_RESP_BTS,
-	/* CRCX response received (MSC) */
-	GSCON_EV_MGW_CRCX_RESP_MSC,
 	/* MDCX response received (MSC) - triggered by LCLS */
 	GSCON_EV_MGW_MDCX_RESP_MSC,
 
-	/* Internal handover request (intra-BSC handover) */
-	GSCON_EV_HO_START,
-	/* Handover timed out (T3103 in handover_logic.c) */
-	GSCON_EV_HO_TIMEOUT,
-	/* Handover failed (handover_logic.c) */
-	GSCON_EV_HO_FAIL,
-	/* Handover completed successfully (handover_logic.c) */
-	GSCON_EV_HO_COMPL,
-
 	/* LCLS child FSM has terminated due to hard failure */
 	GSCON_EV_LCLS_FAIL,
+
+	GSCON_EV_FORGET_LCHAN,
+	GSCON_EV_FORGET_MGW_ENDPOINT,
 };
 
 struct gsm_subscriber_connection;
 struct gsm_network;
-struct mgcp_conn_peer;
 struct msgb;
+struct mgwep_ci;
+struct assignment_request;
+struct gsm_lchan;
+
+void bsc_subscr_conn_fsm_init();
 
 /* Allocate a subscriber connection and its associated FSM */
 struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *net);
-
-void bsc_subscr_pick_codec(struct mgcp_conn_peer *conn_peer, struct gsm_subscriber_connection *conn);
+void gscon_update_id(struct gsm_subscriber_connection *conn);
 
 void gscon_submit_rsl_dtap(struct gsm_subscriber_connection *conn,
 			   struct msgb *msg, int link_id, int allow_sacch);
-void gscon_dtap_queue_flush(struct gsm_subscriber_connection *conn, int send);
+int gscon_sigtran_send(struct gsm_subscriber_connection *conn, struct msgb *msg);
+
+struct mgw_endpoint *gscon_ensure_mgw_endpoint(struct gsm_subscriber_connection *conn,
+					       uint16_t msc_assigned_cic);
+bool gscon_connect_mgw_to_msc(struct gsm_subscriber_connection *conn,
+			      struct gsm_lchan *for_lchan,
+			      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);
+
+void gscon_start_assignment(struct gsm_subscriber_connection *conn,
+			    struct assignment_request *req);
+
+void gscon_change_primary_lchan(struct gsm_subscriber_connection *conn, struct gsm_lchan *new_lchan);
+void gscon_release_lchans(struct gsm_subscriber_connection *conn, bool do_sacch_deact);
+
+void gscon_lchan_releasing(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan);
+void gscon_forget_lchan(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan);
+
+void gscon_forget_mgw_endpoint_ci(struct gsm_subscriber_connection *conn, struct mgwep_ci *ci);
+
+bool gscon_is_aoip(struct gsm_subscriber_connection *conn);
+bool gscon_is_sccplite(struct gsm_subscriber_connection *conn);