[PATCH] libosmocore[master]: osmo_fsm: Lookup functions to find FSM Instance by name or ID

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 15:28:40 UTC 2017


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

osmo_fsm: Lookup functions to find FSM Instance by name or ID

Introduce two lookup helper functions to resolve a fsm_instance based on
the FSM and name or ID.  Also, add related test cases.

Change-Id: I707f3ed2795c28a924e64adc612d378c21baa815
---
M include/osmocom/core/fsm.h
M src/fsm.c
M tests/fsm/fsm_test.c
M tests/fsm/fsm_test.err
4 files changed, 45 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/72/2372/1

diff --git a/include/osmocom/core/fsm.h b/include/osmocom/core/fsm.h
index 3a1f233..f42dd0c 100644
--- a/include/osmocom/core/fsm.h
+++ b/include/osmocom/core/fsm.h
@@ -141,6 +141,10 @@
 int osmo_fsm_register(struct osmo_fsm *fsm);
 void osmo_fsm_unregister(struct osmo_fsm *fsm);
 struct osmo_fsm *osmo_fsm_find_by_name(const char *name);
+struct osmo_fsm_inst *osmo_fsm_inst_find_by_name(const struct osmo_fsm *fsm,
+						 const char *name);
+struct osmo_fsm_inst *osmo_fsm_inst_find_by_id(const struct osmo_fsm *fsm,
+						const char *id);
 struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv,
 					  int log_level, const char *id);
 struct osmo_fsm_inst *osmo_fsm_inst_alloc_child(struct osmo_fsm *fsm,
diff --git a/src/fsm.c b/src/fsm.c
index 0e2c9be..7b2be70 100644
--- a/src/fsm.c
+++ b/src/fsm.c
@@ -113,6 +113,30 @@
 	return NULL;
 }
 
+struct osmo_fsm_inst *osmo_fsm_inst_find_by_name(const struct osmo_fsm *fsm,
+						 const char *name)
+{
+	struct osmo_fsm_inst *fi;
+
+	llist_for_each_entry(fi, &fsm->instances, list) {
+		if (!strcmp(name, fi->name))
+			return fi;
+	}
+	return NULL;
+}
+
+struct osmo_fsm_inst *osmo_fsm_inst_find_by_id(const struct osmo_fsm *fsm,
+						const char *id)
+{
+	struct osmo_fsm_inst *fi;
+
+	llist_for_each_entry(fi, &fsm->instances, list) {
+		if (!strcmp(id, fi->id))
+			return fi;
+	}
+	return NULL;
+}
+
 /*! \brief register a FSM with the core
  *
  *  A FSM descriptor needs to be registered with the core before any
diff --git a/tests/fsm/fsm_test.c b/tests/fsm/fsm_test.c
index 7a64421..c3ab91c 100644
--- a/tests/fsm/fsm_test.c
+++ b/tests/fsm/fsm_test.c
@@ -94,7 +94,7 @@
 	struct osmo_fsm_inst *fi;
 
 	LOGP(DMAIN, LOGL_INFO, "Checking FSM allocation\n");
-	fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, NULL);
+	fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, "my_id");
 	OSMO_ASSERT(fi);
 	OSMO_ASSERT(fi->fsm == &fsm);
 	OSMO_ASSERT(!strncmp(osmo_fsm_inst_name(fi), fsm.name, strlen(fsm.name)));
@@ -143,10 +143,16 @@
 	log_add_target(stderr_target);
 	log_set_print_filename(stderr_target, 0);
 
-	g_ctx = NULL;
-	osmo_fsm_register(&fsm);
 
+	g_ctx = NULL;
+	OSMO_ASSERT(osmo_fsm_find_by_name(fsm.name) == NULL);
+	osmo_fsm_register(&fsm);
+	OSMO_ASSERT(osmo_fsm_find_by_name(fsm.name) == &fsm);
+
+	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);
 
 	while (1) {
 		osmo_select_main(0);
diff --git a/tests/fsm/fsm_test.err b/tests/fsm/fsm_test.err
index c9021bb..6f031be 100644
--- a/tests/fsm/fsm_test.err
+++ b/tests/fsm/fsm_test.err
@@ -1,11 +1,11 @@
 Checking FSM allocation
-Test FSM{NULL}: Allocated
-Test FSM{NULL}: Received Event 1
-Test FSM{NULL}: Event 1 not permitted
-Test FSM{NULL}: Received Event 0
-Test FSM{NULL}: state_chg to ONE
-Test FSM{ONE}: Received Event 1
-Test FSM{ONE}: state_chg to TWO
-Test FSM{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/2372
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I707f3ed2795c28a924e64adc612d378c21baa815
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