aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/bts_shutdown_fsm.c
blob: 50ef652e750a1ee78d6544ba2092db8c0835d885 (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
/* BTS shutdown 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 <osmocom/core/fsm.h>
#include <osmocom/core/tdef.h>

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

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

static const struct osmo_tdef_state_timeout bts_shutdown_fsm_timeouts[32] = {
	[BTS_SHUTDOWN_ST_WAIT_RAMP_DOWN_COMPL] = { .T = -1 },
	[BTS_SHUTDOWN_ST_EXIT] = { .T = -2 },
};

#define bts_shutdown_fsm_state_chg(fi, NEXT_STATE) \
	osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, bts_shutdown_fsm_timeouts, ((struct gsm_bts *)fi->priv)->T_defs, -1)

static void st_none(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	switch(event) {
	case BTS_SHUTDOWN_EV_START:
		bts_shutdown_fsm_state_chg(fi, BTS_SHUTDOWN_ST_WAIT_RAMP_DOWN_COMPL);
		break;
	}
}

static void st_wait_ramp_down_compl_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	/* TODO: here power ramp down will be started on all TRX, prior to changing state */
}

static void st_wait_ramp_down_compl(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	/* TODO: In here once we have ramp down implemented we'll transit to
	   regular exit. For now we simply wait for state timeout
	   bts_shutdown_fsm_state_chg(fi, BTS_SHUTDOWN_ST_EXIT);
	*/
}

static void st_exit_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct gsm_bts *bts = (struct gsm_bts *)fi->priv;
	struct gsm_bts_trx *trx;
	llist_for_each_entry_reverse(trx, &bts->trx_list, list) {
		bts_model_trx_deact_rf(trx);
		bts_model_trx_close(trx);
	}
	/* There's yet no way to get confirmation from lower layers regarding
	   state. Allow a few seconds of select() loop and timeout timer will
	   exit later */
}

static struct osmo_fsm_state bts_shutdown_fsm_states[] = {
	[BTS_SHUTDOWN_ST_NONE] = {
		.in_event_mask =
			X(BTS_SHUTDOWN_EV_START),
		.out_state_mask = X(BTS_SHUTDOWN_ST_WAIT_RAMP_DOWN_COMPL),
		.name = "NONE",
		.action = st_none,
	},
	[BTS_SHUTDOWN_ST_WAIT_RAMP_DOWN_COMPL] = {
		.in_event_mask = 0,
		.out_state_mask =
			X(BTS_SHUTDOWN_ST_EXIT),
		.name = "WAIT_RAMP_DOWN_COMPL",
		.onenter = st_wait_ramp_down_compl_on_enter,
		.action = st_wait_ramp_down_compl,
	},
	[BTS_SHUTDOWN_ST_EXIT] = {
		.name = "EXIT",
		.onenter = st_exit_on_enter,
	}
};

const struct value_string bts_shutdown_fsm_event_names[] = {
	OSMO_VALUE_STRING(BTS_SHUTDOWN_EV_START),
	{ 0, NULL }
};

int bts_shutdown_fsm_timer_cb(struct osmo_fsm_inst *fi)
{
	switch (fi->state) {
	case BTS_SHUTDOWN_ST_WAIT_RAMP_DOWN_COMPL:
		LOGPFSML(fi, LOGL_ERROR, "Timer expired waiting for ramp down complete\n");
		bts_shutdown_fsm_state_chg(fi, BTS_SHUTDOWN_ST_EXIT);
		break;
	case BTS_SHUTDOWN_ST_EXIT:
		LOGPFSML(fi, LOGL_NOTICE, "Shutdown process completed successfuly, exiting process\n");
		exit(0);
		break;
	default:
		OSMO_ASSERT(false);
	}
	return 0;
}

struct osmo_fsm bts_shutdown_fsm = {
	.name = "BTS_SHUTDOWN",
	.states = bts_shutdown_fsm_states,
	.num_states = ARRAY_SIZE(bts_shutdown_fsm_states),
	.event_names = bts_shutdown_fsm_event_names,
	.log_subsys = DOML,
	.timer_cb = bts_shutdown_fsm_timer_cb,
};

static __attribute__((constructor)) void bts_shutdown_fsm_init(void)
{
	OSMO_ASSERT(osmo_fsm_register(&bts_shutdown_fsm) == 0);
}

void bts_shutdown(struct gsm_bts *bts, const char *reason)
{
	struct osmo_fsm_inst *fi = bts->shutdown_fi;
	if (fi->state != BTS_SHUTDOWN_ST_NONE) {
		LOGPFSML(fi, LOGL_NOTICE, "BTS is already being shutdown.\n");
		return;
	}

	LOGPFSML(fi, LOGL_NOTICE, "Shutting down BTS, reason: %s\n", reason);
	osmo_fsm_inst_dispatch(fi, BTS_SHUTDOWN_EV_START, NULL);
}