You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "FelipeMdeO (via GitHub)" <gi...@apache.org> on 2026/01/02 21:04:04 UTC

[PR] Port SMF .c/.h files to NuttX [nuttx-apps]

FelipeMdeO opened a new pull request, #3296:
URL: https://github.com/apache/nuttx-apps/pull/3296

    Fixed build issues
   
   Added example to demonstate state machine framework
   
   Removed unused file
   
   Improve code quality | Fix thread termination issue
   
   *Note: Please adhere to [Contributing Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
   This change adds the State Machine Framework (SMF) to nuttx-apps as a
   self-contained, reusable library.
   
   The SMF provides a deterministic framework for implementing flat and
   hierarchical state machines, based on explicit entry, run, and exit actions.
   Event handling and scheduling remain fully under application control.
   
   The implementation is a port of the SMF originally introduced in the Zephyr
   RTOS, adapted to NuttX coding conventions and build infrastructure. The library
   does not perform dynamic memory allocation and is intended to be used with
   statically defined state tables.
   
   An example application is included to demonstrate correct usage.
   
   ## Impact
   
   - Adds a new optional library to nuttx-apps, enabled via Kconfig.
   - No impact on existing applications when the feature is disabled.
   - No changes to existing APIs or build configurations.
   - No dynamic memory usage introduced.
   - Architecture-independent and suitable for both hosted and embedded targets.
   
   ## Testing
   
   The change was validated using the provided SMF hierarchical state machine
   (HSM) example application. Example available in:
    | Prompt: State Machine Framework PSICC2 demo (HSM)                                                                                                                                                            │ 
   │   Location:                                                                                                                                                                                                
   │  
   │     -> Application Configuration                                                                                                                                                                            │  
   │       -> Examples 
    |             -> State Machine Framework PSICC2 demo
   
   Testing was performed by enabling the SMF configuration options and building
   the example for the simulator target. The example demonstrates:
   
   - Initialization of the state machine using smf_set_initial()
   - Execution of flat and hierarchical states
   - State transitions triggered from run actions
   - Entry and exit action sequencing
   - Graceful termination using smf_set_terminate()
   
   The example was built and executed using the sim:nsh configuration. Runtime
   output was manually verified to confirm correct state transitions and expected
   execution order.
   
   No regressions were observed, and existing applications build and run
   unchanged when SMF is not enabled.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Port SMF .c/.h files to NuttX [nuttx-apps]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #3296:
URL: https://github.com/apache/nuttx-apps/pull/3296#discussion_r2659002821


##########
system/smf/Kconfig:
##########
@@ -0,0 +1,28 @@
+# SMF (State Machine Framework) configuration options
+#
+# For syntax reference, see kconfig-language.txt in the NuttX tools repository.
+
+config APPS_SMF
+  bool "(SMF) support"
+  default n
+  ---help---
+    Enables the State Machine Framework (SMF) for implementing state machines
+
+if APPS_SMF
+
+config SMF_ANCESTOR_SUPPORT

Review Comment:
   use the same prefix APPS_SMF



##########
include/system/smf.h:
##########
@@ -0,0 +1,265 @@
+/****************************************************************************
+ * apps/include/system/smf.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __APPS_INCLUDE_SYSTEM_SMF_H
+#define __APPS_INCLUDE_SYSTEM_SMF_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_SMF_ANCESTOR_SUPPORT
+#  ifdef CONFIG_SMF_INITIAL_TRANSITION
+#    define SMF_CREATE_STATE(_entry, _run, _exit, _parent, _initial) \
+       { \
+         .entry = (_entry), \
+         .run = (_run), \
+         .exit = (_exit), \
+         .parent = (_parent), \
+         .initial = (_initial), \
+       }
+#  else
+#    define SMF_CREATE_STATE(_entry, _run, _exit, _parent, _initial) \
+       { \
+         .entry = (_entry), \
+         .run = (_run), \
+         .exit = (_exit), \
+         .parent = (_parent), \
+       }
+#  endif
+#else
+#  define SMF_CREATE_STATE(_entry, _run, _exit, _parent, _initial) \
+     { \
+       .entry = (_entry), \
+       .run = (_run), \
+       .exit = (_exit), \
+     }
+#endif
+
+#define SMF_CTX(o) ((struct smf_ctx *)(o))
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum smf_state_result
+{
+  SMF_EVENT_HANDLED,
+  SMF_EVENT_PROPAGATE
+};
+
+typedef void (*state_method)(void *obj);
+
+typedef enum smf_state_result (*state_execution)(void *obj);
+
+struct smf_state
+{
+  /* Optional method that will be run when this state is entered. */
+
+  state_method entry;
+
+  /* Optional method that will be run repeatedly during the state machine
+   * loop.
+   */
+
+  state_execution run;
+
+  /* Optional method that will be run when this state exits. */
+
+  state_method exit;
+
+#ifdef CONFIG_SMF_ANCESTOR_SUPPORT
+  /* Optional parent state that contains common entry/run/exit
+   * implementation among various child states.
+   * entry: Parent function executes BEFORE child function.
+   * run:   Parent function executes AFTER child function.
+   * exit:  Parent function executes AFTER child function.
+   * Note: When transitioning between two child states with a shared
+   * parent, that parent's exit and entry functions do not execute.
+   */
+
+  const struct smf_state *parent;
+
+#  ifdef CONFIG_SMF_INITIAL_TRANSITION
+  /* Optional initial transition state. NULL for leaf states. */
+
+  const struct smf_state *initial;
+#  endif /* CONFIG_SMF_INITIAL_TRANSITION */
+#endif /* CONFIG_SMF_ANCESTOR_SUPPORT */
+};
+
+struct smf_ctx
+{
+  /* Current state the state machine is executing. */
+
+  const struct smf_state *current;
+
+  /* Previous state the state machine executed. */
+
+  const struct smf_state *previous;
+
+#ifdef CONFIG_SMF_ANCESTOR_SUPPORT
+  /* Currently executing state (which may be a parent). */
+
+  const struct smf_state *executing;
+#endif /* CONFIG_SMF_ANCESTOR_SUPPORT */
+  /* This value is set by the set_terminate function and should */
+  /* terminate the state machine when its set to a value other than */

Review Comment:
   change to multiple line comment



##########
examples/smf/main.c:
##########
@@ -0,0 +1,127 @@
+/****************************************************************************
+ * apps/examples/smf/main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Copyright (c) 2024 Glenn Andrews
+ * State Machine example copyright (c) Miro Samek
+ *
+ * Implementation of the statechart in Figure 2.11 of
+ * Practical UML Statecharts in C/C++, 2nd Edition by Miro Samek
+ * https://www.state-machine.com/psicc2
+ * Used with permission of the author.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "hsm_psicc2_thread.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: usage
+ ****************************************************************************/
+
+static void usage(void)
+{
+  printf("Usage:\n");
+  printf("  hsm_psicc2 start\n");
+  printf("  hsm_psicc2 event <A..I>\n");
+  printf("  hsm_psicc2 terminate\n");
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: hsm_psicc2_main
+ ****************************************************************************/
+
+int hsm_psicc2_main(int argc, char *argv[])

Review Comment:
   ```suggestion
   int main(int argc, char *argv[])
   ```



##########
examples/smf/Kconfig:
##########
@@ -0,0 +1,29 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config EXAMPLES_SMF
+  tristate "State Machine Framework PSICC2 demo (HSM)"
+  default n
+  select NSH_BUILTIN_APPS
+  select APPS_SMF
+  select SMF_ANCESTOR_SUPPORT

Review Comment:
   change to depends on



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Port SMF .c/.h files to NuttX [nuttx-apps]

Posted by "FelipeMdeO (via GitHub)" <gi...@apache.org>.
FelipeMdeO commented on PR #3296:
URL: https://github.com/apache/nuttx-apps/pull/3296#issuecomment-3707230864

   Hello All, I solved the issues as requested. Thanks for the review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] system/smf: Port SMF .c/.h files to NuttX [nuttx-apps]

Posted by "cederom (via GitHub)" <gi...@apache.org>.
cederom commented on code in PR #3296:
URL: https://github.com/apache/nuttx-apps/pull/3296#discussion_r2659591524


##########
examples/smf/Kconfig:
##########
@@ -0,0 +1,37 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config EXAMPLES_SMF
+  tristate "State Machine Framework PSICC2 demo (HSM)"
+  default n
+  select NSH_BUILTIN_APPS

Review Comment:
   @FelipeMdeO please update :-)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] system/smf: Port SMF .c/.h files to NuttX [nuttx-apps]

Posted by "FelipeMdeO (via GitHub)" <gi...@apache.org>.
FelipeMdeO commented on code in PR #3296:
URL: https://github.com/apache/nuttx-apps/pull/3296#discussion_r2659606879


##########
examples/smf/Kconfig:
##########
@@ -0,0 +1,37 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config EXAMPLES_SMF
+  tristate "State Machine Framework PSICC2 demo (HSM)"
+  default n
+  select NSH_BUILTIN_APPS

Review Comment:
   Sorry, I forgot to fix it. Fixed now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] system/smf: Port SMF .c/.h files to NuttX [nuttx-apps]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #3296:
URL: https://github.com/apache/nuttx-apps/pull/3296#discussion_r2659299918


##########
examples/smf/Kconfig:
##########
@@ -0,0 +1,37 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config EXAMPLES_SMF
+  tristate "State Machine Framework PSICC2 demo (HSM)"
+  default n
+  select NSH_BUILTIN_APPS

Review Comment:
   but still select here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Port SMF .c/.h files to NuttX [nuttx-apps]

Posted by "linguini1 (via GitHub)" <gi...@apache.org>.
linguini1 commented on code in PR #3296:
URL: https://github.com/apache/nuttx-apps/pull/3296#discussion_r2658412363


##########
system/smf/Kconfig:
##########
@@ -0,0 +1,28 @@
+# SMF (State Machine Framework) configuration options
+#
+# For syntax reference, see kconfig-language.txt in the NuttX tools repository.
+
+config APPS_SMF

Review Comment:
   All of these config options should be prefixed with `SYSTEM_SMF`. i.e. `SYSTEM_SMF_ANCESTOR_SUPPORT`



##########
system/smf/Make.defs:
##########
@@ -0,0 +1,25 @@
+############################################################################
+# apps/smf/Make.defs
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+ifeq ($(CONFIG_APPS_SMF),y)

Review Comment:
   Same here, `CONFIG_SYSTEM_SMF`



##########
examples/smf/hsm_psicc2_thread.h:
##########
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2024 Glenn Andrews
+ * State Machine example copyright (c) Miro Samek
+ *
+ * Implementation of the statechart in Figure 2.11 of
+ * Practical UML Statecharts in C/C++, 2nd Edition by Miro Samek
+ * https://www.state-machine.com/psicc2
+ * Used with permission of the author.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#ifndef _HSM_PSICC2_THREAD_H
+#define _HSM_PSICC2_THREAD_H
+
+#include <stdint.h>
+
+#define HSM_PSICC2_THREAD_STACK_SIZE       2048
+#define HSM_PSICC2_THREAD_PRIORITY         120
+#define HSM_PSICC2_THREAD_EVENT_QUEUE_SIZE 10

Review Comment:
   These should be Kconfig options if possible.



##########
examples/smf/hsm_psicc2_thread.c:
##########
@@ -0,0 +1,401 @@
+/*

Review Comment:
   As the check failures indicate, you'll need to follow the coding standard guideline. NuttX also has a top-of-file comment string you should use here (check another source file to see the format), and be sure to copy in the attributions from the original file as well. @jerpelea is more familiar with the licensing attribution requirements/SPDX stuff.



##########
system/smf/smf.c:
##########
@@ -0,0 +1,436 @@
+/*

Review Comment:
   It looks like you'll have to re-format this file to pass the CI formatting check. Use `tools/checkpatch.sh` to see what needs to be changed.



##########
examples/smf/Makefile:
##########
@@ -0,0 +1,37 @@
+############################################################################
+# apps/examples/hsm_psicc2/Makefile
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+include $(APPDIR)/Make.defs
+
+# HSM PSICC2 (SMF demo) built-in application info
+
+PROGNAME  = $(CONFIG_EXAMPLES_SMF_PROGNAME)
+PRIORITY  = $(CONFIG_EXAMPLES_SMF_PRIORITY)
+STACKSIZE = $(CONFIG_EXAMPLES_SMF_STACKSIZE)
+MODULE    = $(CONFIG_EXAMPLES_SMF)
+
+# HSM PSICC2 Example
+
+MAINSRC = main.c

Review Comment:
   Use `smf_main.c`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] system/smf: Port SMF .c/.h files to NuttX [nuttx-apps]

Posted by "cederom (via GitHub)" <gi...@apache.org>.
cederom commented on PR #3296:
URL: https://github.com/apache/nuttx-apps/pull/3296#issuecomment-3708020087

   Very cool, thanks @FelipeMdeO, lets just wait for the CI build verificaion :-)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] system/smf: Port SMF .c/.h files to NuttX [nuttx-apps]

Posted by "linguini1 (via GitHub)" <gi...@apache.org>.
linguini1 merged PR #3296:
URL: https://github.com/apache/nuttx-apps/pull/3296


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org