daniel-p-carvalho commented on code in PR #20782:
URL: https://github.com/apache/nuttx/pull/20782#discussion_r4355927456


##########
drivers/timers/taskwdt.c:
##########
@@ -0,0 +1,436 @@
+/****************************************************************************
+ * drivers/timers/taskwdt.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 <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <errno.h>
+#include <nuttx/debug.h>
+
+#include <nuttx/clock.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/list.h>
+#include <nuttx/mutex.h>
+#include <nuttx/sched.h>
+#include <nuttx/wqueue.h>
+#include <nuttx/timers/taskwdt.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define TASKWDT_INTERVAL_TICK MSEC2TICK(CONFIG_TASKWDT_INTERVAL)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* A channel is the state behind one open() of the device */
+
+struct taskwdt_chan_s
+{
+  struct list_node node;                            /* Entry in 
taskwdt_s.chans */
+  pid_t            pid;                             /* Task that opened it */
+  char             name[CONFIG_TASK_NAME_SIZE + 1]; /* Its name */
+  clock_t          timeout;                         /* Timeout in ticks */
+  clock_t          deadline;                        /* Tick the channel 
expires */
+  bool             armed;                           /* Is the channel 
supervised */
+};
+
+struct taskwdt_s
+{
+  struct file      wdt;     /* The hardware watchdog */
+  struct work_s    work;    /* Checks the channels, feeds the hardware */
+  struct list_node chans;   /* All the channels (struct taskwdt_chan_s) */
+  mutex_t          lock;    /* Protects everything above and below */
+  bool             started; /* The hardware watchdog is armed */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int     taskwdt_open(FAR struct file *filep);

Review Comment:
   Agreed, I will go that way. You are right about the cost: rereading
   a2c01567, it only changes the upper half and does not touch any lower half,
   so I was overestimating the work. The plan:
   
   1. cherry-pick a2c01567, ported to master, without
      WDIOC_SETSOFT and without the software watchdog;
   2. the supervisor on top of it, in the upper half: one channel per open(),
      the hardware fed (through the real lower half) only while every armed
      channel is in time, and the owning task reported when one expires;
   3. the global monitor together with the per-task one: the hardware keeps 
being
      fed by a worker even with no channel, so the system reboots if either a
      task or the system itself stops responding. I still have to define how
      this coexists with WATCHDOG_AUTOMONITOR, which feeds blindly today;
   4. the WATCHDOG_TASKMONITOR_* prefix and the test app (already merged in
      nuttx-apps) I will handle after the redesign, in a small nuttx-apps PR.
   
   I will rewrite the commits in this PR. Before pushing I will redo the board
   validation, since the design changes.
   



##########
boards/sim/sim/sim/src/sim_watchdog.c:
##########
@@ -0,0 +1,180 @@
+/****************************************************************************
+ * boards/sim/sim/sim/src/sim_watchdog.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 <nuttx/config.h>
+
+#include <stdbool.h>
+#include <errno.h>
+#include <nuttx/debug.h>
+
+#include <nuttx/board.h>
+#include <nuttx/clock.h>
+#include <nuttx/wdog.h>
+#include <nuttx/timers/watchdog.h>
+
+#include "sim.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_wdt_s
+{
+  struct watchdog_lowerhalf_s lower;
+  struct wdog_s wdog;
+  uint32_t      timeout;  /* Timeout in milliseconds */
+  bool          started;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_wdt_start(struct watchdog_lowerhalf_s *lower);
+static int sim_wdt_stop(struct watchdog_lowerhalf_s *lower);
+static int sim_wdt_keepalive(struct watchdog_lowerhalf_s *lower);
+static int sim_wdt_getstatus(struct watchdog_lowerhalf_s *lower,
+                             struct watchdog_status_s *status);
+static int sim_wdt_settimeout(struct watchdog_lowerhalf_s *lower,
+                              uint32_t timeout);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct watchdog_ops_s g_sim_wdt_ops =
+{
+  .start      = sim_wdt_start,
+  .stop       = sim_wdt_stop,
+  .keepalive  = sim_wdt_keepalive,
+  .getstatus  = sim_wdt_getstatus,
+  .settimeout = sim_wdt_settimeout,
+};
+
+static struct sim_wdt_s g_sim_wdt =
+{
+  .lower.ops = &g_sim_wdt_ops,
+  .timeout   = CONFIG_SIM_WATCHDOG_TIMEOUT,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sim_wdt_expire
+ *
+ * Description:
+ *   The simulator has no reset, so a watchdog timeout ends it with
+ *   CONFIG_SIM_WATCHDOG_EXITCODE.
+ *
+ ****************************************************************************/
+
+static void sim_wdt_expire(wdparm_t arg)
+{
+  syslog(LOG_EMERG, "Simulated watchdog expired, exit %d\n",
+         CONFIG_SIM_WATCHDOG_EXITCODE);
+  board_power_off(CONFIG_SIM_WATCHDOG_EXITCODE);
+}
+
+static void sim_wdt_arm(struct sim_wdt_s *priv)
+{
+  wd_start(&priv->wdog, MSEC2TICK(priv->timeout), sim_wdt_expire,
+           (wdparm_t)priv);
+}
+
+static int sim_wdt_start(struct watchdog_lowerhalf_s *lower)
+{
+  struct sim_wdt_s *priv = (struct sim_wdt_s *)lower;
+
+  priv->started = true;
+  sim_wdt_arm(priv);
+  return OK;
+}
+
+static int sim_wdt_stop(struct watchdog_lowerhalf_s *lower)
+{
+  struct sim_wdt_s *priv = (struct sim_wdt_s *)lower;
+
+  priv->started = false;
+  wd_cancel(&priv->wdog);
+  return OK;
+}
+
+static int sim_wdt_keepalive(struct watchdog_lowerhalf_s *lower)
+{
+  struct sim_wdt_s *priv = (struct sim_wdt_s *)lower;
+
+  if (priv->started)
+    {
+      sim_wdt_arm(priv);
+    }
+
+  return OK;
+}
+
+static int sim_wdt_getstatus(struct watchdog_lowerhalf_s *lower,
+                             struct watchdog_status_s *status)
+{
+  struct sim_wdt_s *priv = (struct sim_wdt_s *)lower;
+
+  status->flags    = priv->started ? WDFLAGS_ACTIVE | WDFLAGS_RESET : 0;
+  status->timeout  = priv->timeout;
+  status->timeleft = TICK2MSEC(wd_gettime(&priv->wdog));
+  return OK;
+}
+
+static int sim_wdt_settimeout(struct watchdog_lowerhalf_s *lower,
+                              uint32_t timeout)
+{
+  struct sim_wdt_s *priv = (struct sim_wdt_s *)lower;
+
+  if (timeout == 0)
+    {
+      return -EINVAL;
+    }
+
+  priv->timeout = timeout;
+  return sim_wdt_keepalive(lower);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sim_watchdog_initialize
+ *
+ * Description:
+ *   Register the simulated watchdog at CONFIG_WATCHDOG_DEVPATH.
+ *
+ ****************************************************************************/
+
+int sim_watchdog_initialize(void)

Review Comment:
   Sure, I will move it to drivers/timers as a generic wdog-based watchdog lower
   half, like timer_wdog.c, with the simulator only providing the expiry action.
   I will send it as a separate PR since it is independent of the supervisor.
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to