Introduce NM BTS Site Manager FSM

Change-Id: Ic001ce6ebeff6f51470ef58140b0235f4a30265e
diff --git a/include/osmocom/bsc/Makefile.am b/include/osmocom/bsc/Makefile.am
index 05d71bb..1f066b6 100644
--- a/include/osmocom/bsc/Makefile.am
+++ b/include/osmocom/bsc/Makefile.am
@@ -38,6 +38,7 @@
 	misdn.h \
 	neighbor_ident.h \
 	network_listen.h \
+	nm_common_fsm.h \
 	openbscdefines.h \
 	osmo_bsc.h \
 	osmo_bsc_grace.h \
diff --git a/include/osmocom/bsc/bts.h b/include/osmocom/bsc/bts.h
index 7f36904..a9d57a3 100644
--- a/include/osmocom/bsc/bts.h
+++ b/include/osmocom/bsc/bts.h
@@ -361,6 +361,11 @@
 	uint8_t _features_data[MAX_BTS_FEATURES/8];
 };
 
+/* BTS Site Manager */
+struct gsm_bts_sm {
+	struct gsm_abis_mo mo;
+};
+
 /* One BTS */
 struct gsm_bts {
 	/* list header in net->bts_list */
@@ -425,9 +430,7 @@
 	/* CCCH is on C0 */
 	struct gsm_bts_trx *c0;
 
-	struct {
-		struct gsm_abis_mo mo;
-	} site_mgr;
+	struct gsm_bts_sm site_mgr;
 
 	/* bitmask of all SI that are present/valid in si_buf */
 	uint32_t si_valid;
@@ -725,6 +728,10 @@
 	return &lai;
 }
 
+static inline struct gsm_bts *gsm_bts_sm_get_bts(struct gsm_bts_sm *site_mgr) {
+	return (struct gsm_bts *)container_of(site_mgr, struct gsm_bts, site_mgr);
+}
+
 struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, uint8_t bts_num);
 
 char *gsm_bts_name(const struct gsm_bts *bts);
diff --git a/include/osmocom/bsc/gsm_data.h b/include/osmocom/bsc/gsm_data.h
index 050cd7a..06348be 100644
--- a/include/osmocom/bsc/gsm_data.h
+++ b/include/osmocom/bsc/gsm_data.h
@@ -393,6 +393,8 @@
 	struct gsm_nm_state nm_state;
 	struct tlv_parsed *nm_attr;
 	struct gsm_bts *bts;
+	struct osmo_fsm_inst *fi;
+	bool opstart_sent;
 };
 
 /* Ericsson OM2000 Managed Object */
diff --git a/include/osmocom/bsc/nm_common_fsm.h b/include/osmocom/bsc/nm_common_fsm.h
new file mode 100644
index 0000000..a76c198
--- /dev/null
+++ b/include/osmocom/bsc/nm_common_fsm.h
@@ -0,0 +1,46 @@
+/* Header for all NM FSM. Following 3GPP TS 12.21 Figure 2/GSM 12.21:
+  GSM 12.21 Objects' Operational state and availability status behaviour during initialization */
+
+/* (C) 2020 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
+ * Author: Pau Espin Pedrol <pespin@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 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/>.
+ *
+ */
+
+#pragma once
+
+#include <osmocom/core/fsm.h>
+#include <osmocom/core/utils.h>
+
+/* Common */
+enum nm_fsm_events {
+	NM_EV_SW_ACT_REP,
+	NM_EV_STATE_CHG_REP,
+	NM_EV_OPSTART_ACK,
+	NM_EV_OPSTART_NACK,
+	NM_EV_OML_DOWN,
+};
+extern const struct value_string nm_fsm_event_names[];
+
+/* BTS SiteManager */
+enum nm_bts_sm_op_fsm_states {
+	NM_BTS_SM_ST_OP_DISABLED_NOTINSTALLED,
+	NM_BTS_SM_ST_OP_DISABLED_DEPENDENCY,
+	NM_BTS_SM_ST_OP_DISABLED_OFFLINE,
+	NM_BTS_SM_ST_OP_ENABLED,
+};
+extern struct osmo_fsm nm_bts_sm_fsm;
diff --git a/include/osmocom/bsc/signal.h b/include/osmocom/bsc/signal.h
index c7d7fe1..abda67e 100644
--- a/include/osmocom/bsc/signal.h
+++ b/include/osmocom/bsc/signal.h
@@ -72,6 +72,7 @@
 	S_NM_STATECHG_ADM,	/* Administrative State changed */
 	S_NM_OM2K_CONF_RES,	/* OM2K Configuration Result */
 	S_NM_OPSTART_ACK,	/* Received OPSTART ACK, arg is struct msgb *oml_msg */
+	S_NM_OPSTART_NACK,	/* Received OPSTART NACK, arg is struct msgb *oml_msg */
 	S_NM_GET_ATTR_REP,	/* Received Get Attributes Response, arg is struct msgb *oml_msg */
 	S_NM_SET_RADIO_ATTR_ACK, /* Received Set Radio Carrier Attributes Ack, arg is struct msgb *oml_msg */
 };