From d04cffd9869b90e04ddf43ea9e4cadfb1f453e7c Mon Sep 17 00:00:00 2001 From: fangpeina Date: Fri, 14 Nov 2025 18:37:18 +0800 Subject: [PATCH] system/nxinit: Add setprop support for action Add the property backend and a setprop builtin so that setting a property can feed action triggers. property_simple.c provides a minimal init_property_*() implementation whose init_property_set() forwards the key/value pair to init_action_trigger_event(), and init.c wires the property poller into the init poll loop. Signed-off-by: fangpeina Signed-off-by: wangjianyu3 --- system/nxinit/CMakeLists.txt | 2 ++ system/nxinit/Makefile | 1 + system/nxinit/action.h | 2 ++ system/nxinit/builtin.c | 16 +++++++++ system/nxinit/init.c | 45 +++++++++++--------------- system/nxinit/init.h | 16 +++++++++ system/nxinit/property.h | 49 ++++++++++++++++++++++++++++ system/nxinit/property_simple.c | 57 +++++++++++++++++++++++++++++++++ 8 files changed, 161 insertions(+), 27 deletions(-) create mode 100644 system/nxinit/property.h create mode 100644 system/nxinit/property_simple.c diff --git a/system/nxinit/CMakeLists.txt b/system/nxinit/CMakeLists.txt index 5aef892e9..92f778657 100644 --- a/system/nxinit/CMakeLists.txt +++ b/system/nxinit/CMakeLists.txt @@ -24,6 +24,8 @@ if(CONFIG_SYSTEM_NXINIT) set(CSRCS init.c action.c builtin.c import.c parser.c service.c) + list(APPEND CSRCS property_simple.c) + nuttx_add_application( MODULE ${CONFIG_SYSTEM_NXINIT} diff --git a/system/nxinit/Makefile b/system/nxinit/Makefile index e1f6ae894..643f4d247 100644 --- a/system/nxinit/Makefile +++ b/system/nxinit/Makefile @@ -30,6 +30,7 @@ CSRCS += parser.c CSRCS += action.c CSRCS += service.c CSRCS += import.c +CSRCS += property_simple.c PROGNAME = $(CONFIG_SYSTEM_NXINIT_PROGNAME) PRIORITY = $(CONFIG_SYSTEM_NXINIT_PRIORITY) diff --git a/system/nxinit/action.h b/system/nxinit/action.h index 908b907b4..f05bff1aa 100644 --- a/system/nxinit/action.h +++ b/system/nxinit/action.h @@ -71,6 +71,8 @@ struct action_manager_s struct timespec time_run; #endif FAR struct service_manager_s *sm; + + FAR struct init_poller_s *prop; }; /**************************************************************************** diff --git a/system/nxinit/builtin.c b/system/nxinit/builtin.c index ba7a41bcf..6da046b69 100644 --- a/system/nxinit/builtin.c +++ b/system/nxinit/builtin.c @@ -33,6 +33,7 @@ #include "builtin.h" #include "init.h" +#include "property.h" #include "service.h" /**************************************************************************** @@ -59,6 +60,10 @@ static int cmd_start(FAR struct action_manager_s *am, int argc, FAR char **argv); static int cmd_stop(FAR struct action_manager_s *am, int argc, FAR char **argv); +#if CONFIG_SYSTEM_NXINIT_ACTION_EVENTS_MAX > 1 +static int cmd_setprop(FAR struct action_manager_s *am, + int argc, FAR char **argv); +#endif static int cmd_exec(FAR struct action_manager_s *am, int argc, FAR char **argv); static int cmd_class_start(FAR struct action_manager_s *am, @@ -76,6 +81,9 @@ static const struct cmd_map_s g_builtin[] = {"class_stop", 2, 2, cmd_class_stop}, {"exec", 3, 99, cmd_exec}, {"exec_start", 2, 2, cmd_exec_start}, +#if CONFIG_SYSTEM_NXINIT_ACTION_EVENTS_MAX > 1 + {"setprop", 3, 3, cmd_setprop}, +#endif {"start", 2, 2, cmd_start}, {"stop", 2, 2, cmd_stop}, {"trigger", 2, 2, cmd_trigger}, @@ -136,6 +144,14 @@ static int cmd_stop(FAR struct action_manager_s *am, return init_service_stop(service); } +#if CONFIG_SYSTEM_NXINIT_ACTION_EVENTS_MAX > 1 +static int cmd_setprop(FAR struct action_manager_s *am, int argc, + FAR char **argv) +{ + return init_property_set(am->prop, argv[1], argv[2]); +} +#endif + static int cmd_trigger(FAR struct action_manager_s *am, int argc, FAR char **argv) { diff --git a/system/nxinit/init.c b/system/nxinit/init.c index 6c7291e5e..0f5c03ab4 100644 --- a/system/nxinit/init.c +++ b/system/nxinit/init.c @@ -38,6 +38,7 @@ #include "builtin.h" #include "init.h" #include "import.h" +#include "property.h" #include "service.h" /**************************************************************************** @@ -49,21 +50,6 @@ : ((ts)->tv_sec = (ms) / 1000, \ (ts)->tv_nsec = ((ms) % 1000) * 1000000, (ts))) -/**************************************************************************** - * Private Types - ****************************************************************************/ - -struct init_event_s -{ - FAR struct pollfd *pfd; - FAR void *priv; - FAR struct service_manager_s *sm; - FAR struct action_manager_s *am; - CODE int (*init) (FAR struct init_event_s *); - CODE void (*handle) (FAR struct init_event_s *); - CODE void (*deinit) (FAR struct init_event_s *); -}; - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -154,11 +140,16 @@ int main(int argc, FAR char *argv[]) {NULL}, }; - struct init_event_s ev[] = + struct init_poller_s poller[] = { + { + .init = init_property_init, + .handle = init_property_handler, + .deinit = init_property_deinit, + }, }; - struct pollfd pfds[nitems(ev)]; + struct pollfd pfds[nitems(poller)]; sigset_t mask; size_t i; int r; @@ -176,12 +167,12 @@ int main(int argc, FAR char *argv[]) usbtrace_enable(TRACE_BITSET); #endif - for (i = 0; i < nitems(ev); i++) + for (i = 0; i < nitems(poller); i++) { - ev[i].sm = &sm; - ev[i].am = &am; - ev[i].pfd = &pfds[i]; - r = ev[i].init(&ev[i]); + poller[i].sm = &sm; + poller[i].am = &am; + poller[i].pfd = &pfds[i]; + r = poller[i].init(&poller[i]); if (r < 0) { init_err("Init event %zu", i); @@ -237,11 +228,11 @@ int main(int argc, FAR char *argv[]) break; } - for (i = 0; i < nitems(ev); i++) + for (i = 0; i < nitems(poller); i++) { - if (ev[i].pfd->revents & ev[i].pfd->events) + if (poller[i].pfd->revents & poller[i].pfd->events) { - ev[i].handle(&ev[i]); + poller[i].handle(&poller[i]); } } @@ -251,9 +242,9 @@ int main(int argc, FAR char *argv[]) out: while (i--) { - if (ev[i].deinit) + if (poller[i].deinit) { - ev[i].deinit(&ev[i]); + poller[i].deinit(&poller[i]); } } diff --git a/system/nxinit/init.h b/system/nxinit/init.h index 16daa2306..e6ce868fb 100644 --- a/system/nxinit/init.h +++ b/system/nxinit/init.h @@ -27,6 +27,7 @@ * Included Files ****************************************************************************/ +#include #include /**************************************************************************** @@ -90,4 +91,19 @@ } \ while (0) +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct init_poller_s +{ + FAR struct pollfd *pfd; + FAR void *priv; + FAR struct service_manager_s *sm; + FAR struct action_manager_s *am; + CODE int (*init) (FAR struct init_poller_s *); + CODE void (*handle) (FAR struct init_poller_s *); + CODE void (*deinit) (FAR struct init_poller_s *); +}; + #endif /* __APPS_SYSTEM_NXINIT_INIT_H */ diff --git a/system/nxinit/property.h b/system/nxinit/property.h new file mode 100644 index 000000000..e533dc26f --- /dev/null +++ b/system/nxinit/property.h @@ -0,0 +1,49 @@ +/**************************************************************************** + * apps/system/nxinit/property.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_PROPERTY_H +#define __APPS_SYSTEM_NXINIT_PROPERTY_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "init.h" + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/* Steps to enable action events + * + * - Define all init_property_*() interfaces declared in this file. + * - Data structures or functions that will likely be used: + * - struct action_event_s + * - init_action_foreach_event() + */ + +void init_property_handler(FAR struct init_poller_s *ctx); +void init_property_deinit(FAR struct init_poller_s *ctx); +int init_property_init(FAR struct init_poller_s *ctx); +int init_property_set(FAR struct init_poller_s *ctx, + FAR const char *key, FAR const char *value); +#endif /* __APPS_SYSTEM_NXINIT_PROPERTY_H */ diff --git a/system/nxinit/property_simple.c b/system/nxinit/property_simple.c new file mode 100644 index 000000000..c8f12d9b9 --- /dev/null +++ b/system/nxinit/property_simple.c @@ -0,0 +1,57 @@ +/**************************************************************************** + * apps/system/nxinit/property_simple.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "action.h" +#include "property.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int init_property_set(FAR struct init_poller_s *ctx, + FAR const char *key, FAR const char *value) +{ + init_debug("Setprop key:%s value:%s", key, value); + init_action_trigger_event(ctx->am, key, value); + return 0; +} + +void init_property_handler(FAR struct init_poller_s *ctx) +{ + UNUSED(ctx); +} + +void init_property_deinit(FAR struct init_poller_s *ctx) +{ + UNUSED(ctx); +} + +int init_property_init(FAR struct init_poller_s *ctx) +{ + ctx->am->prop = ctx; + ctx->pfd->fd = -1; + return 0; +}