[PATCH] libosmocore[master]: fsm: Add a function to change the FSM instance ID later

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

daniel gerrit-no-reply at lists.osmocom.org
Wed Feb 7 18:39:06 UTC 2018


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

fsm: Add a function to change the FSM instance ID later

Sometimes we want to create an FSM instance before we know its name. In
that case we should be able to update the id later.

Change-Id: Ic216e5b11d4440f8e106a297714f4f06c1152945
---
M include/osmocom/core/fsm.h
M src/fsm.c
2 files changed, 29 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/16/6316/1

diff --git a/include/osmocom/core/fsm.h b/include/osmocom/core/fsm.h
index 8f550d1..cf063a6 100644
--- a/include/osmocom/core/fsm.h
+++ b/include/osmocom/core/fsm.h
@@ -87,7 +87,7 @@
 	/*! back-pointer to the FSM of which we are an instance */
 	struct osmo_fsm *fsm;
 	/*! human readable identifier */
-	const char *id;
+	char *id;
 	/*! human readable fully-qualified name */
 	const char *name;
 	/*! some private data of this instance */
@@ -156,6 +156,8 @@
 				 uint32_t new_parent_term_event);
 void osmo_fsm_inst_free(struct osmo_fsm_inst *fi);
 
+bool osmo_fsm_inst_update_id(struct osmo_fsm_inst *fi, const char *id);
+
 const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event);
 const char *osmo_fsm_inst_name(struct osmo_fsm_inst *fi);
 const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state);
diff --git a/src/fsm.c b/src/fsm.c
index d8751c9..4a1053d 100644
--- a/src/fsm.c
+++ b/src/fsm.c
@@ -197,11 +197,35 @@
 	osmo_fsm_inst_term(fi, OSMO_FSM_TERM_TIMEOUT, &T);
 }
 
+/*! Change id of the FSM instance
+ * \param[in] fi FSM instance
+ * \param[in] id new ID
+ * \returns true if the ID was updated, otherwise false
+ */
+bool osmo_fsm_inst_update_id(struct osmo_fsm_inst *fi, const char *id)
+{
+	if (id) {
+		if (!osmo_identifier_valid(id)) {
+			LOGP(DLGLOBAL, LOGL_ERROR, "Attempting to allocate FSM instance of type '%s'"
+			     " with illegal identifier '%s'\n", fi->fsm->name, id);
+			return false;
+		}
+		if (fi->id) {
+			talloc_free(fi->id);
+		}
+		fi->id = talloc_strdup(fi, id);
+
+		return true;
+	}
+	return false;
+}
+
 /*! allocate a new instance of a specified FSM
  *  \param[in] fsm Descriptor of the FSM
  *  \param[in] ctx talloc context from which to allocate memory
  *  \param[in] priv private data reference store in fsm instance
  *  \param[in] log_level The log level for events of this FSM
+ *  \param[in] id The name/ID of the FSM instance
  *  \returns newly-allocated, initialized and registered FSM instance
  */
 struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv,
@@ -213,14 +237,10 @@
 	fi->priv = priv;
 	fi->log_level = log_level;
 	osmo_timer_setup(&fi->timer, fsm_tmr_cb, fi);
-	if (id) {
-		if (!osmo_identifier_valid(id)) {
-			LOGP(DLGLOBAL, LOGL_ERROR, "Attempting to allocate FSM instance of type '%s'"
-			     " with illegal identifier '%s'\n", fsm->name, id);
+
+	if (!osmo_fsm_inst_update_id(fi, id)) {
 			talloc_free(fi);
 			return NULL;
-		}
-		fi->id = talloc_strdup(fi, id);
 	}
 
 	if (!fsm_log_addr) {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic216e5b11d4440f8e106a297714f4f06c1152945
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: daniel <dwillmann at sysmocom.de>



More information about the gerrit-log mailing list