Add nuttx to the system framework, which is 10.1.0

This commit is contained in:
TangYiwen123
2021-06-09 14:33:15 +08:00
parent 06c351e27c
commit 804bd57aa0
5000 changed files with 1488544 additions and 0 deletions
@@ -0,0 +1,56 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config EXAMPLES_WATCHER
tristate "Watcher example"
default n
depends on DRIVER_NOTERAM
depends on FSUTILS_MKFATFS
---help---
Enable the watcher example. The watcher is a task that will monitor
other tasks that have previously subscribed to be watched. If the
watched tasks don't signal the watcher during the watchdog time period,
the watchdog timer will expire and the watcher will print the tasks that
did not signal and the ones that signaled. This example will only be supported
by the chips that supports interrupt on timeout, i.e., which have the \"capture\"
command implemented.
if EXAMPLES_WATCHER
config EXAMPLES_WATCHER_PROGNAME
string "Program name"
default "watcher"
---help---
This is the name of the program that will be used when the NSH ELF
program is installed.
config EXAMPLES_WATCHER_PRIORITY
int "Watcher task priority"
default 100
config EXAMPLES_WATCHER_STACKSIZE
int "Watcher stack size"
default DEFAULT_TASK_STACKSIZE
config EXAMPLES_WATCHER_DEVPATH
string "Watchdog device path"
default "/dev/watchdog0"
config EXAMPLES_WATCHER_TIMEOUT
int "Watchdog timeout in ms"
default 5000
config EXAMPLES_WATCHER_SIGNAL
int "Signal Number for communication"
default 18
---help---
This is the Signal Number used for communication between the watcher task and the watched tasks.
config EXAMPLES_WATCHER_SIGNAL_LOG
int "Signal Number for logging"
default 19
---help---
This is the Signal Number used by the wdt handler to notify the signal handler to log the tasks.
endif
@@ -0,0 +1,23 @@
############################################################################
# apps/examples/watcher/Make.defs
#
# 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.
#
############################################################################
ifneq ($(CONFIG_EXAMPLES_WATCHER),)
CONFIGURED_APPS += $(APPDIR)/examples/watcher
endif
@@ -0,0 +1,35 @@
############################################################################
# apps/examples/watcher/Make.defs
#
# 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.
#
############################################################################
include $(APPDIR)/Make.defs
# Watcher built-in application info
PROGNAME = $(CONFIG_EXAMPLES_WATCHER_PROGNAME)
PRIORITY = $(CONFIG_EXAMPLES_WATCHER_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_WATCHER_STACKSIZE)
MODULE = $(CONFIG_EXAMPLES_WATCHER)
# Watcher Example
CSRCS = ramdisk.c wdt.c task_mn.c
MAINSRC = watcher_main.c
include $(APPDIR)/Application.mk
@@ -0,0 +1,138 @@
/****************************************************************************
* examples/watcher/ramdisk.c
*
* 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/boardctl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/mount.h>
#include <nuttx/drivers/ramdisk.h>
#include "fsutils/mkfatfs.h"
#include "ramdisk.h"
/****************************************************************************
* Private Definitions
****************************************************************************/
#define BUFFER_SIZE \
(NSECTORS * SECTORSIZE)
/****************************************************************************
* Private Data
****************************************************************************/
static struct fat_format_s g_fmt = FAT_FORMAT_INITIALIZER;
/****************************************************************************
* Public Data
****************************************************************************/
static const char g_source[] = MOUNT_DEVNAME;
static const char g_filesystemtype[] = "vfat";
static const char g_target[] = "/mnt/watcher";
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: create_ramdisk
*
* Description:
* Create a RAM disk of the specified size formatting with a FAT file
* system
*
* Input Parameters:
* None
*
* Return:
* Zero on success, a negated errno on failure.
*
****************************************************************************/
int create_ramdisk(void)
{
struct boardioc_mkrd_s desc;
int ret;
/* Create a RAMDISK device to manage */
desc.minor = MINOR; /* Minor device number of the RAM disk. */
desc.nsectors = NSECTORS; /* The number of sectors in the RAM disk. */
desc.sectsize = SECTORSIZE; /* The size of one sector in bytes. */
desc.rdflags = RDFLAG_WRENABLED | RDFLAG_FUNLINK; /* See ramdisk.h. */
ret = boardctl(BOARDIOC_MKRD, (uintptr_t) & desc);
if (ret < 0)
{
printf("create_ramdisk: Failed to create ramdisk at %s: %d\n",
g_source, -ret);
return ret;
}
/* Create a FAT filesystem on the ramdisk */
ret = mkfatfs(g_source, &g_fmt);
if (ret < 0)
{
printf("mkfatfs: Failed to create FAT filesystem on ramdisk at %s\n",
g_source);
return ret;
}
return 0;
}
int prepare_fs(void)
{
int ret;
ret = create_ramdisk();
if (ret < 0)
{
int errcode = errno;
fprintf(stderr,
"create_ramdisk: failed on creating RAM disk \
and formatting it: %d\n",
errcode);
ret = errcode;
goto errout;
}
ret = mount(g_source, g_target, g_filesystemtype, 0, NULL);
if (ret < 0)
{
int errcode = errno;
fprintf(stderr, "mount: Failed to mount FS: %d\n", errcode);
ret = errcode;
goto errout;
}
errout:
return ret;
}
@@ -0,0 +1,56 @@
/****************************************************************************
* examples/watcher/ramdisk.h
*
* 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 __EXAMPLES_WATCHER_RAMDISK_H
#define __EXAMPLES_WATCHER_RAMDISK_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NSECTORS 64
#define SECTORSIZE 512
#define MINOR 0
#define STR_MACRO(m) #m
#define MKMOUNT_DEVNAME(m) "/dev/ram" STR_MACRO(m)
#define MOUNT_DEVNAME MKMOUNT_DEVNAME(MINOR)
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
int create_ramdisk(void);
int prepare_fs(void);
#endif /* __EXAMPLES_WATCHER_RAMDISK_H */
@@ -0,0 +1,312 @@
/****************************************************************************
* apps/examples/watcher/task_mn.c
*
* 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/boardctl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <nuttx/note/noteram_driver.h>
#include "task_mn.h"
/****************************************************************************
* Private Definitions
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
volatile struct request_s request;
struct task_list_s watched_tasks =
{
.head = NULL,
.tail = NULL
};
/****************************************************************************
* Public Functions
****************************************************************************/
void task_mn_print_tasks_status(void)
{
int notefd;
struct noteram_get_taskname_s task;
struct task_node_s *node;
/* If the list is not empty */
if (watched_tasks.head != NULL)
{
/* Open the note driver */
notefd = open("/dev/note", O_RDONLY);
if (notefd < 0)
{
printf("Error: cannot open /dev/note\n");
return;
}
/* Print all the nodes */
for (node = watched_tasks.head; node != NULL; node = node->next)
{
task.pid = node->task_id;
ioctl(notefd, NOTERAM_GETTASKNAME, (unsigned long)&task);
if (node->reset)
{
printf("%s fed the dog.\n", task.taskname);
}
else
{
printf("%s starved the dog.\n", task.taskname);
}
}
/* Close the note driver */
close(notefd);
}
else
{
printf("Error: Task list is empty to print\n");
}
}
void task_mn_reset_all(void)
{
struct task_node_s *node;
for (node = watched_tasks.head; node != NULL; node = node->next)
{
node->reset = false;
}
}
struct task_node_s *task_mn_is_task_subscribed(pid_t id)
{
struct task_node_s *node;
/* If list is not empty */
if (watched_tasks.head != NULL)
{
/* Search for the node */
for (node = watched_tasks.head; node != NULL; node = node->next)
{
if (node->task_id == id)
{
return node;
}
}
}
return NULL;
}
void task_mn_add_to_list(pid_t id)
{
struct task_node_s *node;
/* Alloc the node */
node = malloc(sizeof(struct task_node_s));
if (node == NULL)
{
fprintf(stderr, "watcher daemon: Couldn't alloc a node to list\n");
return;
}
node->task_id = id;
/* NOTE: Once a task is subscribed, its initial status is that it fed the
* dog. This approach was used first to avoid a false-positive result,
* e.g., the task has been subscribed immediately before the watchdog
* expiration and it did not feed the dog within this interval,
* so the wdt handler would be triggered even if the subscribed
* task would feed the dog in time.
* The second reason is that we can consider the subscription request
* itself an advertisement that the watched task is alive and not stuck.
*/
node->reset = true;
node->next = NULL;
/* If list is not empty */
if (watched_tasks.head != NULL)
{
watched_tasks.tail->next = node;
}
else
{
watched_tasks.head = node;
}
watched_tasks.tail = node;
}
void task_mn_remove_from_list(pid_t id)
{
struct task_node_s *prev;
struct task_node_s *current;
/* If list is empty */
if (watched_tasks.head == NULL)
{
fprintf(stderr, "watcher daemon: List is empty\n");
return;
}
/* First element */
else if (watched_tasks.head->task_id == id)
{
if (watched_tasks.head == watched_tasks.tail)
{
free(watched_tasks.head);
watched_tasks.head = NULL;
watched_tasks.tail = NULL;
return;
}
else
{
prev = watched_tasks.head;
watched_tasks.head = prev->next;
free(prev);
return;
}
}
else
{
/* Search the node */
prev = watched_tasks.head;
current = prev->next;
while (current != NULL)
{
if (current->task_id == id)
{
prev->next = current->next;
/* In case the one that will be removed is the tail */
if (prev->next == NULL)
{
watched_tasks.tail = prev;
}
free(current);
return;
}
prev = prev->next;
current = current->next;
}
}
fprintf(stderr, "watcher daemon: This node is not in the list.\n");
}
void task_mn_get_task_name(struct noteram_get_taskname_s *task)
{
int notefd;
notefd = open("/dev/note", O_RDONLY);
if (notefd < 0)
{
fprintf(stderr, "trace: cannot open /dev/note\n");
return;
}
ioctl(notefd, NOTERAM_GETTASKNAME, (unsigned long)task);
close(notefd);
}
void task_mn_subscribe(pid_t id)
{
struct noteram_get_taskname_s task;
/* Verify if the task exists in the list */
if (task_mn_is_task_subscribed(id) != NULL)
{
task.pid = id;
task_mn_get_task_name(&task);
printf("Task %s was already subscribed\n", task.taskname);
}
else
{
/* If it doesn't, include it to the list */
task_mn_add_to_list(id);
}
}
void task_mn_unsubscribe(pid_t id)
{
struct noteram_get_taskname_s task;
/* Verify if the task exists in the list */
if (task_mn_is_task_subscribed(id) != NULL)
{
/* If it does, remove it from the list */
task_mn_remove_from_list(id);
}
else
{
task.pid = id;
task_mn_get_task_name(&task);
printf("Task %s is not subscribed\n", task.taskname);
}
}
bool task_mn_all_tasks_fed(void)
{
struct task_node_s *node;
for (node = watched_tasks.head; node != NULL; node = node->next)
{
/* If at least one did not feed the dog, return false */
if (node->reset == false)
{
return false;
}
}
return true;
}
@@ -0,0 +1,79 @@
/****************************************************************************
* examples/watcher/task_mn.h
*
* 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 __EXAMPLES_WATCHER_TASK_MN_H
#define __EXAMPLES_WATCHER_TASK_MN_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/note/noteram_driver.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Types
****************************************************************************/
struct request_s
{
pid_t task_id;
int code;
};
struct task_node_s
{
pid_t task_id;
bool reset;
struct task_node_s *next;
};
struct task_list_s
{
struct task_node_s *head;
struct task_node_s *tail;
};
/****************************************************************************
* Public Data
****************************************************************************/
extern volatile struct request_s request;
extern struct task_list_s watched_tasks;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
void task_mn_print_tasks_status(void);
void task_mn_reset_all(void);
struct task_node_s *task_mn_is_task_subscribed(pid_t id);
void task_mn_add_to_list(pid_t id);
void task_mn_remove_from_list(pid_t id);
void task_mn_get_task_name(struct noteram_get_taskname_s *task);
void task_mn_subscribe(pid_t id);
void task_mn_unsubscribe(pid_t id);
bool task_mn_all_tasks_fed(void);
#endif /* __EXAMPLES_WATCHER_TASK_MN_H */
@@ -0,0 +1,270 @@
/****************************************************************************
* apps/examples/watcher/watcher_main.c
*
* 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 <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sched.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "ramdisk.h"
#include "wdt.h"
#include "task_mn.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define SUBSCRIBE_CMD 1
#define UNSUSBCRIBE_CMD -1
#define FEED_CMD 2
/****************************************************************************
* Private Data
****************************************************************************/
static const char g_info_file_name[] = "/mnt/watcher/info.txt";
/****************************************************************************
* Private Functions
****************************************************************************/
static void feed_sighandler(int signo, FAR siginfo_t * siginfo,
FAR void *context)
{
struct task_node_s *node;
request = *(struct request_s *)siginfo->si_value.sival_ptr;
switch (request.code)
{
case FEED_CMD:
/* Update the current requester task status */
node = task_mn_is_task_subscribed(request.task_id);
if (node != NULL)
{
node->reset = true;
}
else
{
fprintf(stderr,
"watcher daemon: task is not subscribed to feed dog.\n");
}
/* Verify if all tasks requested to feed the dog */
if (task_mn_all_tasks_fed())
{
/* If all tasks required, reset it and reset all tasks' status" */
wdt_feed_the_dog();
task_mn_reset_all();
}
break;
case SUBSCRIBE_CMD:
/* Include the current requester task to the watched tasks list */
task_mn_subscribe(request.task_id);
break;
case UNSUSBCRIBE_CMD:
/* Excludes the current requester task from the watched tasks list */
task_mn_unsubscribe(request.task_id);
/* Verify if all tasks has already requested to feed the dog in this
* round. Because maybe the watcher was only expecting the task that
* was unsubscribed to finally reset the dog.
* If this task is no longer being watched and the others have already
* sent a feed request, so it's time to feed the dog.
*/
if (task_mn_all_tasks_fed())
{
/* If all tasks required, reset it and reset all tasks' status" */
wdt_feed_the_dog();
task_mn_reset_all();
}
break;
default:
fprintf(stderr, "watcher daemon: Invalid command\n");
}
}
static void wdt_log(int signo, FAR siginfo_t * siginfo,
FAR void *context)
{
if (watched_tasks.head != NULL)
{
printf("*** Printing Tasks Status ***\n");
task_mn_print_tasks_status();
task_mn_reset_all();
}
}
/****************************************************************************
* Name: watcher_daemon
****************************************************************************/
static int watcher_daemon(int argc, FAR char *argv[])
{
int ret;
struct sigaction act;
struct sigaction act_log;
pid_t watcher_pid;
FILE *fp;
printf("Watcher Daemon has started!\n");
/* Initialize and configure the wdt */
wdt_init();
/* Configure signals action */
/* Configure signal to receive the subscribe/unsubscribe
* and feed requests
*/
act.sa_sigaction = feed_sighandler; /* The handler to be triggered when
* receiving a signal */
act.sa_flags = SA_SIGINFO; /* Invoke the signal-catching function */
sigfillset(&act.sa_mask);
/* Block all other signals less this one */
sigdelset(&act.sa_mask, CONFIG_EXAMPLES_WATCHER_SIGNAL);
ret = sigaction(CONFIG_EXAMPLES_WATCHER_SIGNAL, &act, NULL);
if (ret != OK)
{
int errcode = errno;
fprintf(stderr, "ERROR: sigaction failed: %d\n", errcode);
ret = errcode;
goto errout;
}
/* Configure signal to log the taks at wdt timeout */
act_log.sa_sigaction = wdt_log; /* The handler to be triggered when
* receiving a signal */
act_log.sa_flags = SA_SIGINFO; /* Invoke the signal-catching function */
sigfillset(&act_log.sa_mask);
/* Block all other signals less this one */
sigdelset(&act_log.sa_mask, CONFIG_EXAMPLES_WATCHER_SIGNAL_LOG);
ret = sigaction(CONFIG_EXAMPLES_WATCHER_SIGNAL_LOG, &act_log, NULL);
if (ret != OK)
{
int errcode = errno;
fprintf(stderr, "ERROR: sigaction failed: %d\n", errcode);
ret = errcode;
goto errout;
}
/* Collecting the necessary information of the current task */
watcher_pid = getpid();
/* Writing PID, SIGNAL NUMBER, and COMMANDS' value to a file */
fp = fopen(g_info_file_name, "w+");
if (fp == NULL)
{
int errcode = errno;
fprintf(stderr, "ERROR: Failed to open %s: %d\n",
g_info_file_name, errcode);
ret = errcode;
goto errout;
}
fprintf(fp, "%d %d %d %d %d\n", (int)watcher_pid,
(int)CONFIG_EXAMPLES_WATCHER_SIGNAL, (int)SUBSCRIBE_CMD,
(int)FEED_CMD, (int)UNSUSBCRIBE_CMD);
fclose(fp);
/* Suspends the calling thread until delivery of a non-blocked signal. */
while (1)
{
pause();
}
errout:
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* watcher_main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
int ret;
FILE *fp;
/* Check if the watcher has already been initialized */
fp = fopen(g_info_file_name, "r");
if (fp)
{
fclose(fp);
printf("Watcher is already running.\n");
ret = EBUSY;
goto errout;
}
/* Create a RAMDISK device, format it and mount it */
prepare_fs();
/* Start Daemon */
ret = task_create("watcher_daemon", CONFIG_EXAMPLES_WATCHER_PRIORITY,
CONFIG_EXAMPLES_WATCHER_STACKSIZE, watcher_daemon, NULL);
if (ret < 0)
{
int errcode = errno;
printf("watcher_main: ERROR: Failed to start watcher_daemon: %d\n",
errcode);
ret = errcode;
goto errout;
}
errout:
return ret;
return EXIT_SUCCESS;
}
@@ -0,0 +1,188 @@
/****************************************************************************
* apps/examples/watcher/wdt.c
*
* 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/boardctl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <debug.h>
#include <sys/ioctl.h>
#include "wdt.h"
#include "task_mn.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
struct wdt_loginfo_s
{
pid_t task_id; /* Id of the task that performs logging */
int signal; /* Signal that triggers the logging */
};
/****************************************************************************
* Private Definitions
****************************************************************************/
static int wdt_handler(int irq, FAR void *context, FAR void *arg);
/****************************************************************************
* Private Data
****************************************************************************/
static struct wdog_params_s wdog =
{
.timeout = CONFIG_EXAMPLES_WATCHER_TIMEOUT,
.handlers.oldhandler = NULL,
.handlers.newhandler = wdt_handler
};
static struct wdt_loginfo_s log_info =
{
.signal = CONFIG_EXAMPLES_WATCHER_SIGNAL_LOG
};
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: wdt_print_handler
*
****************************************************************************/
static int wdt_handler(int irq, FAR void *context, FAR void *arg)
{
int ret = OK;
/* Notify the watcher task to log */
ret = kill(log_info.task_id, log_info.signal);
if (ret == ERROR)
{
int errcode = errno;
_err("Error: %d\n", errcode);
ret = errcode;
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
int wdt_init(void)
{
int fd;
int ret;
strcpy(wdog.devname, CONFIG_EXAMPLES_WATCHER_DEVPATH);
/* Open the watchdog device for reading */
fd = open(wdog.devname, O_RDONLY);
if (fd < 0)
{
int errcode = errno;
printf("wdt_init: open %s failed: %d\n", wdog.devname, errcode);
ret = errcode;
goto errout;
}
/* Set the watchdog timeout */
ret = ioctl(fd, WDIOC_SETTIMEOUT, (unsigned long)wdog.timeout);
if (ret < 0)
{
int errcode = errno;
printf("wdt_init: ioctl(WDIOC_SETTIMEOUT) failed: %d\n", errcode);
ret = errcode;
goto errout;
}
/* Register the expiration callback to be triggered on timeout */
ret =
ioctl(fd, WDIOC_CAPTURE, (unsigned long)((uintptr_t) & (wdog.handlers)));
if (ret < 0)
{
int errcode = errno;
printf("wdt_init: ioctl(WDIOC_CAPTURE) failed: %d\n", errcode);
ret = errcode;
goto errout;
}
/* Then start the watchdog timer. */
ret = ioctl(fd, WDIOC_START, 0);
if (ret < 0)
{
int errcode = errno;
printf("wdt_init: ioctl(WDIOC_START) failed: %d\n", errcode);
ret = errcode;
goto errout;
}
close(fd);
/* Set the pid, so the handler will be able to notify watcher */
log_info.task_id = getpid();
errout:
return ret;
}
void wdt_feed_the_dog(void)
{
int fd;
int ret;
fd = open(wdog.devname, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "trace: cannot open %s\n", wdog.devname);
return;
}
ret = ioctl(fd, WDIOC_KEEPALIVE, 0);
if (ret < 0)
{
printf("watcher: ioctl(WDIOC_KEEPALIVE) failed: %d\n", ret);
}
close(fd);
}
@@ -0,0 +1,59 @@
/****************************************************************************
* examples/watcher/wdt.h
*
* 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 __EXAMPLES_WATCHER_WDT_H
#define __EXAMPLES_WATCHER_WDT_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/timers/watchdog.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define DEVNAME_SIZE 16
/****************************************************************************
* Public Types
****************************************************************************/
struct wdog_params_s
{
uint32_t timeout;
char devname[DEVNAME_SIZE];
struct watchdog_capture_s handlers;
};
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
int wdt_init(void);
void wdt_feed_the_dog(void);
#endif /* __EXAMPLES_WATCHER_WDT_H */