This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new d65c262df system/nxinit: add the pmconfig command
d65c262df is described below

commit d65c262df9061bf9066295711d404851a675b4c7
Author: wangjianyu3 <[email protected]>
AuthorDate: Sun Nov 22 22:08:42 2026 +0800

    system/nxinit: add the pmconfig command
    
    A "pmconfig" line in init.rc could only run the NSH command through
    "sh -c", so a system without NSH could not request PM states.  Add
    the stay and relax form.  A line of one or two arguments, the NSH
    query form, now fails with -EINVAL instead of falling back to NSH.
    
    Assisted-by: OpenCode:mimo-v2.6-pro
    Co-authored-by: multica-agent <[email protected]>
    Signed-off-by: wangjianyu3 <[email protected]>
---
 system/nxinit/builtin.c                 |  87 ++++++++++++++++++++++
 system/nxinit/test/test_nxinit.c        |   1 +
 system/nxinit/test/test_nxinit.h        |   1 +
 system/nxinit/test/test_nxinit_action.c | 123 ++++++++++++++++++++++++++++++++
 4 files changed, 212 insertions(+)

diff --git a/system/nxinit/builtin.c b/system/nxinit/builtin.c
index a0a40a05d..70fcccb5e 100644
--- a/system/nxinit/builtin.c
+++ b/system/nxinit/builtin.c
@@ -39,6 +39,10 @@
 
 #include <netutils/netinit.h>
 
+#if defined(CONFIG_BOARDCTL) && defined(CONFIG_PM)
+#include <nuttx/power/pm.h>
+#endif
+
 #ifdef CONFIG_RPTUN
 #include <nuttx/rptun/rptun.h>
 #endif
@@ -111,6 +115,10 @@ static int cmd_netinit(FAR struct action_manager_s *am,
 static int cmd_board_finalinit(FAR struct action_manager_s *am,
                                int argc, FAR char **argv);
 #endif
+#if defined(CONFIG_BOARDCTL) && defined(CONFIG_PM)
+static int cmd_pmconfig(FAR struct action_manager_s *am,
+                        int argc, FAR char **argv);
+#endif
 #ifdef CONFIG_RPTUN
 static int cmd_rptun(FAR struct action_manager_s *am,
                      int argc, FAR char **argv);
@@ -164,6 +172,9 @@ static const struct cmd_map_s g_builtin[] =
 #ifdef CONFIG_NETUTILS_NETINIT
   {"netinit", 1, 1, cmd_netinit},
 #endif
+#if defined(CONFIG_BOARDCTL) && defined(CONFIG_PM)
+  {"pmconfig", 3, 4, cmd_pmconfig},
+#endif
 #ifdef CONFIG_RPTUN
   {"rptun", 3, 3, cmd_rptun},
 #endif
@@ -382,6 +393,82 @@ static int cmd_rptun(FAR struct action_manager_s *am,
 }
 #endif
 
+#if defined(CONFIG_BOARDCTL) && defined(CONFIG_PM)
+static int cmd_pmconfig(FAR struct action_manager_s *am,
+                        int argc, FAR char **argv)
+{
+  struct boardioc_pm_ctrl_s ctrl;
+  int domain = 0;
+  int ret;
+
+  UNUSED(am);
+  memset(&ctrl, 0, sizeof(ctrl));
+
+  if (argc > 3)
+    {
+      FAR char *end;
+      long val;
+
+      val = strtol(argv[3], &end, 10);
+      if (end == argv[3] || *end != '\0' || val < 0 ||
+          val >= CONFIG_PM_NDOMAINS)
+        {
+          init_err("pmconfig: bad domain '%s'", argv[3]);
+          return -EINVAL;
+        }
+
+      domain = val;
+    }
+
+  ctrl.domain = domain;
+
+  if (!strcmp(argv[1], "stay"))
+    {
+      ctrl.action = BOARDIOC_PM_STAY;
+    }
+  else if (!strcmp(argv[1], "relax"))
+    {
+      ctrl.action = BOARDIOC_PM_RELAX;
+    }
+  else
+    {
+      init_err("pmconfig: bad action '%s'", argv[1]);
+      return -EINVAL;
+    }
+
+  if (!strcmp(argv[2], "normal"))
+    {
+      ctrl.state = PM_NORMAL;
+    }
+  else if (!strcmp(argv[2], "idle"))
+    {
+      ctrl.state = PM_IDLE;
+    }
+  else if (!strcmp(argv[2], "standby"))
+    {
+      ctrl.state = PM_STANDBY;
+    }
+  else if (!strcmp(argv[2], "sleep"))
+    {
+      ctrl.state = PM_SLEEP;
+    }
+  else
+    {
+      init_err("pmconfig: bad state '%s'", argv[2]);
+      return -EINVAL;
+    }
+
+  ret = boardctl(BOARDIOC_PM_CONTROL, (uintptr_t)&ctrl);
+  if (ret < 0)
+    {
+      ret = -errno;
+      init_err("pmconfig %s '%s' failed: %d", argv[1], argv[2], -ret);
+    }
+
+  return ret;
+}
+#endif
+
 static int cmd_unlink(FAR struct action_manager_s *am,
                       int argc, FAR char **argv)
 {
diff --git a/system/nxinit/test/test_nxinit.c b/system/nxinit/test/test_nxinit.c
index b486bf96a..8b383787e 100644
--- a/system/nxinit/test/test_nxinit.c
+++ b/system/nxinit/test/test_nxinit.c
@@ -58,6 +58,7 @@ int main(int argc, FAR char *argv[])
       cmocka_unit_test(test_nxinit_action_event_and_semantics),
       cmocka_unit_test(test_nxinit_action_empty_skipped),
       cmocka_unit_test(test_nxinit_action_builtin_mount),
+      cmocka_unit_test(test_nxinit_action_builtin_pmconfig),
       cmocka_unit_test(test_nxinit_service_duplicate_conflict),
       cmocka_unit_test(test_nxinit_service_override_replaces_duplicate),
       cmocka_unit_test(test_nxinit_service_args_max_boundary),
diff --git a/system/nxinit/test/test_nxinit.h b/system/nxinit/test/test_nxinit.h
index e9202d125..479aeaba3 100644
--- a/system/nxinit/test/test_nxinit.h
+++ b/system/nxinit/test/test_nxinit.h
@@ -72,6 +72,7 @@ void test_nxinit_action_event_match_fnmatch(FAR void **state);
 void test_nxinit_action_event_and_semantics(FAR void **state);
 void test_nxinit_action_empty_skipped(FAR void **state);
 void test_nxinit_action_builtin_mount(FAR void **state);
+void test_nxinit_action_builtin_pmconfig(FAR void **state);
 
 /****************************************************************************
  * Name: test_nxinit_service_*
diff --git a/system/nxinit/test/test_nxinit_action.c 
b/system/nxinit/test/test_nxinit_action.c
index c077f0900..b032f2c27 100644
--- a/system/nxinit/test/test_nxinit_action.c
+++ b/system/nxinit/test/test_nxinit_action.c
@@ -26,6 +26,7 @@
 
 #include <nuttx/config.h>
 #include <nuttx/list.h>
+#include <nuttx/power/pm.h>
 
 #include <errno.h>
 #include <fcntl.h>
@@ -34,9 +35,11 @@
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/boardctl.h>
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/statfs.h>
@@ -409,3 +412,123 @@ void test_nxinit_action_builtin_mount(FAR void **state)
   skip();
 #endif
 }
+
+/****************************************************************************
+ * Name: test_nxinit_action_builtin_pmconfig
+ *
+ * Description:
+ *   "pmconfig stay" raises the stay count of the state and "pmconfig
+ *   relax" lowers it.  The optional fourth argument selects the domain.
+ *   An unknown action, state or domain fails with -EINVAL.
+ ****************************************************************************/
+
+#if defined(CONFIG_BOARDCTL) && defined(CONFIG_PM)
+static uint32_t pm_count(int domain, int state)
+{
+  struct boardioc_pm_ctrl_s ctrl;
+
+  memset(&ctrl, 0, sizeof(ctrl));
+  ctrl.action = BOARDIOC_PM_STAYCOUNT;
+  ctrl.domain = domain;
+  ctrl.state = state;
+  boardctl(BOARDIOC_PM_CONTROL, (uintptr_t)&ctrl);
+  return ctrl.count;
+}
+#endif
+
+void test_nxinit_action_builtin_pmconfig(FAR void **state)
+{
+#if defined(CONFIG_BOARDCTL) && defined(CONFIG_PM)
+  FAR char *stay[] =
+    {
+      "pmconfig", "stay", "normal", NULL
+    };
+
+  FAR char *staydom[] =
+    {
+      "pmconfig", "stay", "normal", "0", NULL
+    };
+
+  FAR char *relax[] =
+    {
+      "pmconfig", "relax", "normal", NULL
+    };
+
+  FAR char *relaxdom[] =
+    {
+      "pmconfig", "relax", "normal", "0", NULL
+    };
+
+  FAR char *badaction[] =
+    {
+      "pmconfig", "bogus", "normal", NULL
+    };
+
+  FAR char *badstate[] =
+    {
+      "pmconfig", "stay", "bogus", NULL
+    };
+
+  char domain[16];
+  FAR char *baddomain[] =
+    {
+      "pmconfig", "stay", "normal", domain, NULL
+    };
+
+  FAR char *badnum[] =
+    {
+      "pmconfig", "stay", "normal", "1x", NULL
+    };
+
+#if CONFIG_PM_NDOMAINS > 1
+  FAR char *staydom1[] =
+    {
+      "pmconfig", "stay", "normal", "1", NULL
+    };
+
+  FAR char *relaxdom1[] =
+    {
+      "pmconfig", "relax", "normal", "1", NULL
+    };
+
+  uint32_t count1;
+#endif
+
+  struct action_manager_s am;
+  uint32_t count;
+
+  snprintf(domain, sizeof(domain), "%d", CONFIG_PM_NDOMAINS);
+
+  action_manager_init(&am);
+
+  count = pm_count(0, PM_NORMAL);
+  assert_int_equal(init_builtin_run(&am, 3, stay), 0);
+  assert_int_equal(pm_count(0, PM_NORMAL), count + 1);
+  assert_int_equal(init_builtin_run(&am, 4, staydom), 0);
+  assert_int_equal(pm_count(0, PM_NORMAL), count + 2);
+  assert_int_equal(init_builtin_run(&am, 4, relaxdom), 0);
+  assert_int_equal(pm_count(0, PM_NORMAL), count + 1);
+  assert_int_equal(init_builtin_run(&am, 3, relax), 0);
+  assert_int_equal(pm_count(0, PM_NORMAL), count);
+
+  assert_int_equal(init_builtin_run(&am, 3, badaction), -EINVAL);
+  assert_int_equal(init_builtin_run(&am, 3, badstate), -EINVAL);
+  assert_int_equal(init_builtin_run(&am, 4, baddomain), -EINVAL);
+  assert_int_equal(init_builtin_run(&am, 4, badnum), -EINVAL);
+  assert_int_equal(pm_count(0, PM_NORMAL), count);
+
+#if CONFIG_PM_NDOMAINS > 1
+  count1 = pm_count(1, PM_NORMAL);
+  assert_int_equal(init_builtin_run(&am, 4, staydom1), 0);
+  assert_int_equal(pm_count(1, PM_NORMAL), count1 + 1);
+  assert_int_equal(pm_count(0, PM_NORMAL), count);
+  assert_int_equal(init_builtin_run(&am, 4, relaxdom1), 0);
+  assert_int_equal(pm_count(1, PM_NORMAL), count1);
+  assert_int_equal(pm_count(0, PM_NORMAL), count);
+#endif
+
+  action_manager_free_all(&am);
+#else
+  skip();
+#endif
+}

Reply via email to