aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/nm_channel_fsm.c
blob: 263d2cc9b6d2a0aff9dd43f23a3af49cd79ffff5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* NM Radio Carrier FSM */

/* (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/>.
 *
 */

#include <errno.h>
#include <unistd.h>
#include <inttypes.h>

#include <osmocom/core/fsm.h>
#include <osmocom/core/tdef.h>
#include <osmocom/gsm/protocol/gsm_12_21.h>

#include <osmo-bts/logging.h>
#include <osmo-bts/gsm_data.h>
#include <osmo-bts/bts_model.h>
#include <osmo-bts/bts.h>
#include <osmo-bts/rsl.h>
#include <osmo-bts/nm_common_fsm.h>

#define X(s) (1 << (s))

#define nm_chan_fsm_state_chg(fi, NEXT_STATE) \
	osmo_fsm_inst_state_chg(fi, NEXT_STATE, 0, 0)

//////////////////////////
// FSM STATE ACTIONS
//////////////////////////

static void st_op_disabled_notinstalled_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct gsm_bts_trx_ts *ts = (struct gsm_bts_trx_ts *)fi->priv;
	ts->mo.opstart_success = false;
	oml_mo_state_chg(&ts->mo, NM_OPSTATE_DISABLED, NM_AVSTATE_NOT_INSTALLED);
}

static void st_op_disabled_notinstalled(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct gsm_bts_trx_ts *ts = (struct gsm_bts_trx_ts *)fi->priv;

	switch (event) {
	case NM_EV_BBTRANSC_INSTALLED:
		oml_mo_tx_sw_act_rep(&ts->mo);
		if (ts->trx->bb_transc.mo.nm_state.operational == NM_OPSTATE_ENABLED)
			nm_chan_fsm_state_chg(fi, NM_CHAN_ST_OP_DISABLED_OFFLINE);
		else
			nm_chan_fsm_state_chg(fi, NM_CHAN_ST_OP_DISABLED_DEPENDENCY);
		return;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_op_disabled_dependency_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct gsm_bts_trx_ts *ts = (struct gsm_bts_trx_ts *)fi->priv;
	ts->mo.opstart_success = false;
	oml_mo_state_chg(&ts->mo, NM_OPSTATE_DISABLED, NM_AVSTATE_DEPENDENCY);
}

static void st_op_disabled_dependency(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct gsm_bts_trx_ts *ts = (struct gsm_bts_trx_ts *)fi->priv;

	switch (event) {
	case NM_EV_OPSTART_ACK:
		 LOGPFSML(fi, LOGL_NOTICE, "BSC trying to activate TS while still in avail=dependency. "
			  "Allowing it to stay backward-compatible with older osmo-bts versions, but BSC is wrong.\n");
		ts->mo.opstart_success = true;
		oml_mo_opstart_ack(&ts->mo);
		nm_chan_fsm_state_chg(fi, NM_CHAN_ST_OP_ENABLED);
		return;
	case NM_EV_OPSTART_NACK:
		ts->mo.opstart_success = false;
		oml_mo_opstart_nack(&ts->mo, (int)(intptr_t)data);
		return;
	case NM_EV_BBTRANSC_ENABLED:
		oml_mo_state_chg(&ts->mo, NM_OPSTATE_DISABLED, NM_AVSTATE_OFF_LINE);
		return;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_op_disabled_offline_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct gsm_bts_trx_ts *ts = (struct gsm_bts_trx_ts *)fi->priv;
	ts->mo.opstart_success = false;
	oml_mo_state_chg(&ts->mo, NM_OPSTATE_DISABLED, NM_AVSTATE_OFF_LINE);
}

static void st_op_disabled_offline(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct gsm_bts_trx_ts *ts = (struct gsm_bts_trx_ts *)fi->priv;

	switch (event) {
	case NM_EV_OPSTART_ACK:
		ts->mo.opstart_success = true;
		oml_mo_opstart_ack(&ts->mo);
		nm_chan_fsm_state_chg(fi, NM_CHAN_ST_OP_ENABLED);
		return;
	case NM_EV_OPSTART_NACK:
		ts->mo.opstart_success = false;
		oml_mo_opstart_nack(&ts->mo, (int)(intptr_t)data);
		return;
	case NM_EV_BBTRANSC_DISABLED:
		oml_mo_state_chg(&ts->mo, NM_OPSTATE_DISABLED, NM_AVSTATE_DEPENDENCY);
		return;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_op_enabled_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct gsm_bts_trx_ts *ts = (struct gsm_bts_trx_ts *)fi->priv;
	oml_mo_state_chg(&ts->mo, NM_OPSTATE_ENABLED, NM_AVSTATE_OK);
}

static void st_op_enabled(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	switch (event) {
	case NM_EV_BBTRANSC_DISABLED:
		nm_chan_fsm_state_chg(fi, NM_CHAN_ST_OP_DISABLED_DEPENDENCY);
		return;
	case NM_EV_DISABLE:
		nm_chan_fsm_state_chg(fi, NM_CHAN_ST_OP_DISABLED_OFFLINE);
		return;
	default:
		OSMO_ASSERT(0);
	}
}

static struct osmo_fsm_state nm_chan_fsm_states[] = {
	[NM_CHAN_ST_OP_DISABLED_NOTINSTALLED] = {
		.in_event_mask =
			X(NM_EV_BBTRANSC_INSTALLED),
		.out_state_mask =
			X(NM_CHAN_ST_OP_DISABLED_OFFLINE) |
			X(NM_CHAN_ST_OP_DISABLED_DEPENDENCY),
		.name = "DISABLED_NOTINSTALLED",
		.onenter = st_op_disabled_notinstalled_on_enter,
		.action = st_op_disabled_notinstalled,
	},
	[NM_CHAN_ST_OP_DISABLED_DEPENDENCY] = {
		.in_event_mask =
			X(NM_EV_OPSTART_ACK) |  /* backward compatibility, buggy BSC */
			X(NM_EV_OPSTART_NACK) |
			X(NM_EV_BBTRANSC_ENABLED),
		.out_state_mask =
			X(NM_CHAN_ST_OP_DISABLED_OFFLINE) |
			X(NM_CHAN_ST_OP_ENABLED), /* backward compatibility, buggy BSC */
		.name = "DISABLED_DEPENDENCY",
		.onenter = st_op_disabled_dependency_on_enter,
		.action = st_op_disabled_dependency,
	},
	[NM_CHAN_ST_OP_DISABLED_OFFLINE] = {
		.in_event_mask =
			X(NM_EV_OPSTART_ACK) |
			X(NM_EV_OPSTART_NACK) |
			X(NM_EV_BBTRANSC_DISABLED),
		.out_state_mask =
			X(NM_CHAN_ST_OP_ENABLED) |
			X(NM_CHAN_ST_OP_DISABLED_DEPENDENCY),
		.name = "DISABLED_OFFLINE",
		.onenter = st_op_disabled_offline_on_enter,
		.action = st_op_disabled_offline,
	},
	[NM_CHAN_ST_OP_ENABLED] = {
		.in_event_mask =
			X(NM_EV_BBTRANSC_DISABLED) |
			X(NM_EV_DISABLE),
		.out_state_mask =
			X(NM_CHAN_ST_OP_DISABLED_OFFLINE) |
			X(NM_CHAN_ST_OP_DISABLED_DEPENDENCY),
		.name = "ENABLED",
		.onenter = st_op_enabled_on_enter,
		.action = st_op_enabled,
	},
};

struct osmo_fsm nm_chan_fsm = {
	.name = "NM_CHAN_OP",
	.states = nm_chan_fsm_states,
	.num_states = ARRAY_SIZE(nm_chan_fsm_states),
	.event_names = nm_fsm_event_names,
	.log_subsys = DOML,
};

static __attribute__((constructor)) void nm_chan_fsm_init(void)
{
	OSMO_ASSERT(osmo_fsm_register(&nm_chan_fsm) == 0);
}