nuttx-apps/system/nxinit/action.h
wangjianyu3 3328bbb0be system/nxinit: fix on-event actions re-running on every trigger
The `on <event>` action re-executed on every property poll because the
event pending flag was sticky: event_callback returned the same non-zero
pending value whether the event had just changed or had stayed satisfied
from an earlier change. init_action_foreach_event could not distinguish
an edge from a steady state and re-enqueued the action each round
(board_netinit ran 262 times per boot).

Introduce a three-state result (EVENT_STATE_UNSATISFIED / SATISFIED /
TRIGGERED). event_callback now returns TRIGGERED only on the edge where
pending flips false -> true. foreach folds per-event states into a
product clamped to TRIGGERED, enqueuing the action only when every event
is satisfied AND at least one fired this round.

Assisted-by: GitHubCopilot:claude-4.8-opus
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2026-08-15 18:51:45 +08:00

119 lines
4.1 KiB
C

/****************************************************************************
* apps/system/nxinit/action.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_SYSTEM_NXINIT_ACTION_H
#define __APPS_SYSTEM_NXINIT_ACTION_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/list.h>
#include <time.h>
#include "parser.h"
/****************************************************************************
* Public Types
****************************************************************************/
struct action_cmd_s
{
struct list_node node; /* Command list node */
int argc;
FAR char *argv[CONFIG_SYSTEM_NXINIT_ACTION_CMD_ARGS_MAX];
};
struct action_event_s
{
FAR const char *key;
FAR char *value;
bool invert;
bool pending;
};
struct action_s
{
struct list_node node; /* Action list node */
struct list_node ready_node; /* Ready list node */
struct action_event_s events[CONFIG_SYSTEM_NXINIT_ACTION_EVENTS_MAX];
struct list_node cmds; /* Command header, struct action_cmd_s */
};
struct action_manager_s
{
struct list_node actions; /* Action header, struct action_s */
struct list_node ready_actions; /* Ready header, struct action_s */
FAR struct action_s *current;
FAR struct action_cmd_s *running;
int pid_running;
#if defined(CONFIG_SYSTEM_NXINIT_ACTION_WARN_SLOW) && \
CONFIG_SYSTEM_NXINIT_ACTION_WARN_SLOW > 0
struct timespec time_run;
#endif
FAR struct service_manager_s *sm;
FAR struct init_poller_s *prop;
};
/* Event evaluation result reported by init_action_event_cb.
* TRIGGERED means the event is satisfied and its key is the one that just
* changed, SATISFIED means it stays satisfied from an earlier change.
*/
enum action_event_state_e
{
EVENT_STATE_UNSATISFIED = 0,
EVENT_STATE_SATISFIED,
EVENT_STATE_TRIGGERED,
};
typedef CODE int (*init_action_event_cb)(FAR struct action_manager_s *,
FAR struct action_s *,
FAR struct action_event_s *,
FAR void *arg);
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
int init_action_add_event(FAR struct action_manager_s *am,
FAR const char *name);
int init_action_run_command(FAR struct action_manager_s *am);
void init_action_reap_command(FAR struct action_manager_s *am);
int init_action_parse(FAR const struct parser_s *parser,
bool create, FAR char *buf);
int init_action_foreach_event(FAR struct action_manager_s *am,
init_action_event_cb cb,
FAR void *arg);
void init_action_trigger_event(FAR struct action_manager_s *am,
FAR const char *key,
FAR const char *value);
#ifdef CONFIG_SYSTEM_NXINIT_DEBUG
void init_dump_actions(FAR struct list_node *head);
#else
# define init_dump_actions(h)
#endif
#endif /* __APPS_SYSTEM_NXINIT_ACTION_H */