add gsm_timers, for Tnnn definitions usable by FSMs

Change-Id: If212fcd042051b6fa53484254223614c5b93a9c6
diff --git a/include/osmocom/bsc/Makefile.am b/include/osmocom/bsc/Makefile.am
index 1e26c34..0f134c8 100644
--- a/include/osmocom/bsc/Makefile.am
+++ b/include/osmocom/bsc/Makefile.am
@@ -19,6 +19,7 @@
 	gsm_04_08_rr.h \
 	gsm_04_80.h \
 	gsm_data.h \
+	gsm_timers.h \
 	handover.h \
 	handover_cfg.h \
 	handover_decision.h \
diff --git a/include/osmocom/bsc/gsm_data.h b/include/osmocom/bsc/gsm_data.h
index 2872493..272b192 100644
--- a/include/osmocom/bsc/gsm_data.h
+++ b/include/osmocom/bsc/gsm_data.h
@@ -32,6 +32,8 @@
 #include <osmocom/bsc/bsc_msg_filter.h>
 #include <osmocom/bsc/acc_ramp.h>
 
+#define GSM_T3122_DEFAULT 10
+
 struct mgcp_client_conf;
 struct mgcp_client;
 struct mgcp_ctx;
@@ -1192,23 +1194,6 @@
 	bsc_ctr_description,
 };
 
-#define GSM_T3101_DEFAULT 3	/* s */
-#define GSM_T3103_DEFAULT 5	/* s */
-#define GSM_T3105_DEFAULT 100	/* ms */
-#define GSM_T3107_DEFAULT 5	/* s */
-#define GSM_T3109_DEFAULT 5	/* s, must be 2s + radio_link_timeout*0.48 */
-#define GSM_T3111_DEFAULT 2	/* s */
-#define GSM_T3113_DEFAULT 10	/* s */
-#define GSM_T3115_DEFAULT 10
-#define GSM_T3117_DEFAULT 10
-#define GSM_T3119_DEFAULT 10
-#define GSM_T3122_DEFAULT 10
-#define GSM_T3141_DEFAULT 10
-#define GSM_T10_DEFAULT 6	/* RR Assignment timeout, in seconds */
-#define GSM_T7_DEFAULT 10	/* inter-BSC MO Handover first timeout, in seconds */
-#define GSM_T8_DEFAULT 10	/* inter-BSC MO Handover second timeout, in seconds */
-#define GSM_T101_DEFAULT 10	/* inter-BSC MT Handover timeout, in seconds */
-
 struct gsm_tz {
 	int override; /* if 0, use system's time zone instead. */
 	int hr; /* hour */
@@ -1240,23 +1225,8 @@
 	unsigned int num_bts;
 	struct llist_head bts_list;
 
-	/* timer values */
-	int T3101;
-	int T3103; /*< Handover timeout */
-	int T3105;
-	int T3107;
-	int T3109;
-	int T3111;
-	int T3113;
-	int T3115;
-	int T3117;
-	int T3119;
-	int T3122;
-	int T3141;
-	int T10; /*< RR Assignment timeout, in seconds */
-	int T7; /*< inter-BSC handover MO timeout from Handover Required to Handover Command */
-	int T8; /*< inter-BSC handover MO timeout from Handover Command to final Clear*/
-	int T101; /*< inter-BSC handover MT timeout from Handover Request to Handover Accept */
+	/* shall reference gsm_network_T[] */
+	struct T_def *T_defs;
 
 	enum gsm_chan_t ctype_by_chreq[_NUM_CHREQ_T];
 
@@ -1290,9 +1260,6 @@
 	 * pointer is NULL to indicate absence of a bsc_subscribers list. */
 	struct llist_head *bsc_subscribers;
 
-	/* Periodic location update default value */
-	uint8_t t3212;
-
 	/* Timer for periodic channel load measurements to maintain each BTS's T3122. */
 	struct osmo_timer_list t3122_chan_load_timer;
 
diff --git a/include/osmocom/bsc/gsm_timers.h b/include/osmocom/bsc/gsm_timers.h
new file mode 100644
index 0000000..fde8c93
--- /dev/null
+++ b/include/osmocom/bsc/gsm_timers.h
@@ -0,0 +1,55 @@
+/* API to define Tnnn timers globally, configure in VTY and use for FSM state changes. */
+#pragma once
+
+#include <stdint.h>
+#include <osmocom/core/utils.h>
+
+struct osmo_fsm_inst;
+struct vty;
+
+enum T_unit {
+	T_S = 0,	/*< most T are in seconds, keep 0 as default. */
+	T_MS,		/*< milliseconds */
+	T_M,		/*< minutes */
+	T_CUSTOM,
+};
+
+extern const struct value_string T_unit_names[];
+static inline const char *T_unit_name(enum T_unit val)
+{ return get_value_string(T_unit_names, val); }
+
+/* Define a GSM timer of the form Tnnn, with unit, default value and doc string. */
+struct T_def {
+	const int T; /*< T1234 number */
+	const int default_val; /*< timeout duration (according to unit), default value. */
+	const enum T_unit unit;
+	const char *desc;
+	int val; /*< currently active value, e.g. set by user config. */
+};
+
+/* Iterate an array of struct T_def, the last item should be fully zero, i.e. "{}" */
+#define for_each_T_def(d, T_defs) \
+	for (d = T_defs; d && (d->T || d->default_val || d->desc); d++)
+
+int T_def_get(struct T_def *T_defs, int T, enum T_unit as_unit, int val_if_not_present);
+void T_defs_reset(struct T_def *T_defs);
+struct T_def *T_def_get_entry(struct T_def *T_defs, int T);
+
+void T_defs_vty_init(struct T_def *T_defs, int cfg_parent_node);
+void T_defs_vty_write(struct vty *vty, const char *indent);
+
+
+struct state_timeout {
+	int T;
+	bool keep_timer;
+};
+
+struct state_timeout *get_state_timeout(uint32_t state, struct state_timeout *timeouts_array);
+
+#define fsm_inst_state_chg_T(fi, state, timeouts_array, T_defs, default_timeout) \
+	_fsm_inst_state_chg_T(fi, state, timeouts_array, T_defs, default_timeout, \
+			      __FILE__, __LINE__)
+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);