State Machine Framework (SMF) unexpected exit #79612
-
Hi, I'm starting to play around with the State Machine Framework. Playin around with the example I discovered something that I find a bit counterintuitive. Take two states S0 and S1 In general, if this is desired behaviour, the state should really only changed as the very last thing, which I guess would be reasonable to document? /* State S0 */
static void s0_entry(void *o){
printk("S0 entry\n");
}
static void s0_run(void *o){
printk("S0 run\n");
smf_set_state(SMF_CTX(&s_obj), &demo_states[S1]);
printk("S0 run finished\n");
}
static void s0_exit(void *o){
printk("S0 exit\n");
}
/* State S1 */
static void s1_entry(void *o){
printk("S1 entry\n");
}
static void s1_run(void *o){
printk("S1 run\n");
smf_set_state(SMF_CTX(&s_obj), &demo_states[S0]);
printk("S1 run finished\n");
}
static void s1_exit(void *o){
printk("S1 exit\n");
}
/* Populate state table */
static const struct smf_state demo_states[] = {
[S0] = SMF_CREATE_STATE(s0_entry, s0_run, s0_exit, NULL, NULL),
[S1] = SMF_CREATE_STATE(s1_entry, s1_run, s1_exit, NULL, NULL),
};
int main(void)
{
int32_t ret;
/* Set initial state */
smf_set_initial(SMF_CTX(&s_obj), &demo_states[S0]);
/* Run the state machine */
while(1) {
/* State machine terminates if a non-zero value is returned */
ret = smf_run_state(SMF_CTX(&s_obj));
if (ret) {
/* handle return code and terminate state machine */
break;
}
k_msleep(1000);
}
}
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Sorry for the delay in getting back to you. You are correct that as soon as There's no way to avoid this with the current architecture, and it is expected that To fix it we'd need to get #83854 is a proposal to modify SMF to return a value from the run action. Once it is accepted, we could expand it with an The downside is if anyone has put code after the |
Beta Was this translation helpful? Give feedback.
-
Thanks, I'll follow the PR! |
Beta Was this translation helpful? Give feedback.
Note the current PR does NOT fix this issue. I'd make a subsequent PR to return a transition flag to run the entry/exit actions.
For now just make sure
smf_set_state()
is the last thing in your code path for the run action.