[PATCH] libosmocore[master]: control_if: Add control interface commands for FSMs

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Sun Apr 16 17:23:22 UTC 2017


Review at  https://gerrit.osmocom.org/2377

control_if: Add control interface commands for FSMs

This allows programmatic access to introspection of FSM instances, which
is quite handy from e.g. external test cases: Send a message to the
code, then use the CTRL interface to check if that message has triggered
the right kind of state transition.

Change-Id: I2822513ac63a3d9b87b3eefc7c4e6b585c555ee2
---
M include/osmocom/ctrl/control_cmd.h
M src/ctrl/Makefile.am
M src/ctrl/control_if.c
A src/ctrl/fsm_ctrl_commands.c
M tests/fsm/fsm_test.c
M tests/fsm/fsm_test.err
6 files changed, 226 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/77/2377/1

diff --git a/include/osmocom/ctrl/control_cmd.h b/include/osmocom/ctrl/control_cmd.h
index d9092f3..3cef9d8 100644
--- a/include/osmocom/ctrl/control_cmd.h
+++ b/include/osmocom/ctrl/control_cmd.h
@@ -18,6 +18,8 @@
 	CTRL_NODE_BTS,	/* BTS specific (net.btsN.) */
 	CTRL_NODE_TRX,	/* TRX specific (net.btsN.trxM.) */
 	CTRL_NODE_TS,	/* TS specific (net.btsN.trxM.tsI.) */
+	CTRL_NODE_FSM,	/* Finite State Machine (description) */
+	CTRL_NODE_FSM_INST,	/* Finite State Machine (instance) */
 	_LAST_CTRL_NODE
 };
 
diff --git a/src/ctrl/Makefile.am b/src/ctrl/Makefile.am
index 1817cac..e8d55e6 100644
--- a/src/ctrl/Makefile.am
+++ b/src/ctrl/Makefile.am
@@ -8,7 +8,7 @@
 if ENABLE_CTRL
 lib_LTLIBRARIES = libosmoctrl.la
 
-libosmoctrl_la_SOURCES = control_cmd.c control_if.c
+libosmoctrl_la_SOURCES = control_cmd.c control_if.c fsm_ctrl_commands.c
 
 libosmoctrl_la_LDFLAGS = $(LTLDFLAGS_OSMOCTRL) $(TALLOC_LIBS) -version-info $(LIBVERSION) -no-undefined
 libosmoctrl_la_LIBADD = \
diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c
index 4f357c5..081c2b5 100644
--- a/src/ctrl/control_if.c
+++ b/src/ctrl/control_if.c
@@ -789,6 +789,10 @@
 	if (ret)
 		goto err_vec;
 
+	ret = osmo_fsm_ctrl_cmds_install();
+	if (ret)
+		goto err_vec;
+
 	LOGP(DLCTRL, LOGL_NOTICE, "CTRL at %s %u\n", bind_addr, port);
 	return ctrl;
 }
diff --git a/src/ctrl/fsm_ctrl_commands.c b/src/ctrl/fsm_ctrl_commands.c
new file mode 100644
index 0000000..0dfc396
--- /dev/null
+++ b/src/ctrl/fsm_ctrl_commands.c
@@ -0,0 +1,175 @@
+#include <string.h>
+#include <errno.h>
+
+#include <osmocom/core/fsm.h>
+
+#include <osmocom/ctrl/control_cmd.h>
+#include <osmocom/ctrl/control_if.h>
+
+/*! \brief control interface lookup function for FSM's
+ * \param[in] data Private data passed to controlif_setup()
+ * \param[in] vline Vector of the line holding the command string
+ * \param[out] node_type type (CTRL_NODE_) that was determined
+ * \param[out] node_data private data of node that was determined
+ * \param i Current index into vline, up to which it is parsed
+ */
+static int fsm_ctrl_node_lookup(void *data, vector vline, int *node_type,
+				void **node_data, int *i)
+{
+	struct osmo_fsm *fsm = NULL;
+	struct osmo_fsm_inst *fi = NULL;;
+	const char *token = vector_slot(vline, *i);
+
+	switch (*node_type) {
+	case CTRL_NODE_ROOT:
+		if (!strcmp(token, "fsm")) {
+			const char *fsm_name;
+			(*i)++;
+			fsm_name = vector_lookup(vline, *i);
+			if (!fsm_name)
+				goto err_index;
+			fsm = osmo_fsm_find_by_name(fsm_name);
+			if (!fsm)
+				goto err_missing;
+			*node_data = fsm;
+			*node_type = CTRL_NODE_FSM;
+		}
+		break;
+	case CTRL_NODE_FSM:
+		fsm = *node_data;
+		if (!strcmp(token, "name")) {
+			const char *inst_name;
+			(*i)++;
+			inst_name = vector_lookup(vline, *i);
+			if (!inst_name)
+				goto err_index;
+			fi = osmo_fsm_inst_find_by_name(fsm, inst_name);
+			if (!fi)
+				goto err_missing;
+			*node_data = fi;
+			*node_type = CTRL_NODE_FSM_INST;
+		} else if (!strcmp(token, "id")) {
+			const char *inst_id;
+			(*i)++;
+			inst_id = vector_lookup(vline, *i);
+			if (!inst_id)
+				goto err_index;
+			fi = osmo_fsm_inst_find_by_id(fsm, inst_id);
+			if (!fi)
+				goto err_missing;
+			*node_data = fi;
+			*node_type = CTRL_NODE_FSM_INST;
+		}
+		break;
+	default:
+		return 0;
+	}
+
+	return 1;
+
+err_index:
+	return -ERANGE;
+err_missing:
+	return -ENODEV;
+}
+
+static int get_fsm_inst_state(struct ctrl_cmd *cmd, void *data)
+{
+	struct osmo_fsm_inst *fi = cmd->node;
+
+	if (!fi) {
+		cmd->reply = "No such FSM found";
+		return CTRL_CMD_ERROR;
+	}
+
+	cmd->reply = talloc_strdup(cmd, osmo_fsm_state_name(fi->fsm, fi->state));
+	return CTRL_CMD_REPLY;
+}
+CTRL_CMD_DEFINE_RO(fsm_inst_state, "state");
+
+static int get_fsm_inst_parent_name(struct ctrl_cmd *cmd, void *data)
+{
+	struct osmo_fsm_inst *fi = cmd->node;
+
+	if (!fi) {
+		cmd->reply = "No such FSM found";
+		return CTRL_CMD_ERROR;
+	}
+	if (!fi->proc.parent) {
+		cmd->reply = "No parent";
+		return CTRL_CMD_ERROR;
+	}
+	cmd->reply = talloc_strdup(cmd, fi->proc.parent->name);
+	return CTRL_CMD_REPLY;
+}
+CTRL_CMD_DEFINE_RO(fsm_inst_parent_name, "parent-name");
+
+static int get_fsm_inst_timer(struct ctrl_cmd *cmd, void *data)
+{
+	struct osmo_fsm_inst *fi = cmd->node;
+	struct timeval remaining;
+
+	if (!fi) {
+		cmd->reply = "No such FSM found";
+		return CTRL_CMD_ERROR;
+	}
+	if (osmo_timer_remaining(&fi->timer, NULL, &remaining) < 0)
+		cmd->reply = "0,0,0";
+	else
+		cmd->reply = talloc_asprintf(cmd, "%u,%ld,%ld", fi->T, remaining.tv_sec, remaining.tv_usec);
+
+	return CTRL_CMD_REPLY;
+}
+CTRL_CMD_DEFINE_RO(fsm_inst_timer, "timer");
+
+
+static int get_fsm_inst_dump(struct ctrl_cmd *cmd, void *data)
+{
+	struct osmo_fsm_inst *fi = cmd->node;
+	struct osmo_fsm_inst *child;
+
+	if (!fi) {
+		cmd->reply = "No such FSM found";
+		return CTRL_CMD_ERROR;
+	}
+
+	/* Fixed Part: Name, ID, log_level, state, timer number */
+	cmd->reply = talloc_asprintf(cmd, "'%s','%s','%s','%s',%u", fi->name, fi->id,
+				log_level_str(fi->log_level),
+				osmo_fsm_state_name(fi->fsm, fi->state), fi->T);
+
+	/* Variable Parts below */
+	if (fi->T) {
+		struct timeval remaining;
+		int rc;
+		rc = osmo_timer_remaining(&fi->timer, NULL, &remaining);
+		if (rc == 0) {
+			cmd->reply = talloc_asprintf_append(cmd->reply, ",timeout_sec=%ld,timeout_usec=%ld",
+							    remaining.tv_sec, remaining.tv_usec);
+		}
+	}
+
+	if (fi->proc.parent)
+		cmd->reply = talloc_asprintf_append(cmd->reply, ",parent='%s'", fi->proc.parent->name);
+
+	llist_for_each_entry(child, &fi->proc.children, list) {
+		cmd->reply = talloc_asprintf_append(cmd->reply, ",child='%s'", child->name);
+	}
+
+	return CTRL_CMD_REPLY;
+}
+
+CTRL_CMD_DEFINE_RO(fsm_inst_dump, "dump");
+
+int osmo_fsm_ctrl_cmds_install(void)
+{
+	int rc = 0;
+
+	rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_dump);
+	rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_state);
+	rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_parent_name);
+	rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_timer);
+	rc |= ctrl_lookup_register(fsm_ctrl_node_lookup);
+
+	return rc;
+}
diff --git a/tests/fsm/fsm_test.c b/tests/fsm/fsm_test.c
index 7aade98..eea8b22 100644
--- a/tests/fsm/fsm_test.c
+++ b/tests/fsm/fsm_test.c
@@ -84,15 +84,38 @@
 };
 
 static struct osmo_fsm fsm = {
-	.name = "Test FSM",
+	.name = "Test_FSM",
 	.states = test_fsm_states,
 	.num_states = ARRAY_SIZE(test_fsm_states),
 	.log_subsys = DMAIN,
 };
 
+static struct ctrl_handle *g_ctrl;
+
+static struct ctrl_cmd *exec_ctrl_cmd(const char *cmdstr)
+{
+	struct ctrl_cmd *cmd;
+	return ctrl_cmd_exec_from_string(g_ctrl, cmdstr);
+	OSMO_ASSERT(cmd);
+	return cmd;
+}
+
+static void assert_cmd_reply(const char *cmdstr, const char *expres)
+{
+	struct ctrl_cmd *cmd;
+
+	cmd = exec_ctrl_cmd(cmdstr);
+	if (strcmp(cmd->reply, expres)) {
+		fprintf(stderr, "Reply '%s' doesn't match expected '%s'\n", cmd->reply, expres);
+		OSMO_ASSERT(0);
+	}
+	talloc_free(cmd);
+}
+
 static struct osmo_fsm_inst *foo(void)
 {
 	struct osmo_fsm_inst *fi;
+	struct ctrl_cmd *cmd;
 
 	LOGP(DMAIN, LOGL_INFO, "Checking FSM allocation\n");
 	fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, "my_id");
@@ -101,20 +124,30 @@
 	OSMO_ASSERT(!strncmp(osmo_fsm_inst_name(fi), fsm.name, strlen(fsm.name)));
 	OSMO_ASSERT(fi->state == ST_NULL);
 	OSMO_ASSERT(fi->log_level == LOGL_DEBUG);
+	assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "NULL");
+	assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.timer", "0,0,0");
 
 	/* Try invalid state transition */
 	osmo_fsm_inst_dispatch(fi, EV_B, (void *) 42);
 	OSMO_ASSERT(fi->state == ST_NULL);
+	assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "NULL");
+
 
 	/* Legitimate state transition */
 	osmo_fsm_inst_dispatch(fi, EV_A, (void *) 23);
 	OSMO_ASSERT(fi->state == ST_ONE);
+	assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "ONE");
 
 	/* Legitimate transition with timer */
 	fsm.timer_cb = test_fsm_tmr_cb;
 	osmo_fsm_inst_dispatch(fi, EV_B, (void *) 42);
 	OSMO_ASSERT(fi->state == ST_TWO);
+	assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "TWO");
 
+	cmd = exec_ctrl_cmd("GET 2 fsm.Test_FSM.id.my_id.dump");
+	const char *exp = "'Test_FSM(my_id)','my_id','DEBUG','TWO',2342,timeout_sec=";
+	OSMO_ASSERT(!strncmp(cmd->reply, exp, strlen(exp)));
+	talloc_free(cmd);
 
 	return fi;
 }
@@ -143,7 +176,7 @@
 	stderr_target = log_target_create_stderr();
 	log_add_target(stderr_target);
 	log_set_print_filename(stderr_target, 0);
-
+	g_ctrl = ctrl_handle_alloc(NULL, NULL, NULL);
 
 	g_ctx = NULL;
 	OSMO_ASSERT(osmo_fsm_find_by_name(fsm.name) == NULL);
@@ -153,7 +186,7 @@
 	OSMO_ASSERT(osmo_fsm_inst_find_by_name(&fsm, "my_id") == NULL);
 	finst = foo();
 	OSMO_ASSERT(osmo_fsm_inst_find_by_id(&fsm, "my_id") == finst);
-	OSMO_ASSERT(osmo_fsm_inst_find_by_name(&fsm, "Test FSM(my_id)") == finst);
+	OSMO_ASSERT(osmo_fsm_inst_find_by_name(&fsm, "Test_FSM(my_id)") == finst);
 
 	while (1) {
 		osmo_select_main(0);
diff --git a/tests/fsm/fsm_test.err b/tests/fsm/fsm_test.err
index 6f031be..17a2f2e 100644
--- a/tests/fsm/fsm_test.err
+++ b/tests/fsm/fsm_test.err
@@ -1,11 +1,11 @@
 Checking FSM allocation
-Test FSM(my_id){NULL}: Allocated
-Test FSM(my_id){NULL}: Received Event 1
-Test FSM(my_id){NULL}: Event 1 not permitted
-Test FSM(my_id){NULL}: Received Event 0
-Test FSM(my_id){NULL}: state_chg to ONE
-Test FSM(my_id){ONE}: Received Event 1
-Test FSM(my_id){ONE}: state_chg to TWO
-Test FSM(my_id){TWO}: Timeout of T2342
+Test_FSM(my_id){NULL}: Allocated
+Test_FSM(my_id){NULL}: Received Event 1
+Test_FSM(my_id){NULL}: Event 1 not permitted
+Test_FSM(my_id){NULL}: Received Event 0
+Test_FSM(my_id){NULL}: state_chg to ONE
+Test_FSM(my_id){ONE}: Received Event 1
+Test_FSM(my_id){ONE}: state_chg to TWO
+Test_FSM(my_id){TWO}: Timeout of T2342
 Timer
 
\ No newline at end of file

-- 
To view, visit https://gerrit.osmocom.org/2377
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2822513ac63a3d9b87b3eefc7c4e6b585c555ee2
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>



More information about the gerrit-log mailing list