From 5b29230bfda0782c3b42de8e567faf674daa7995 Mon Sep 17 00:00:00 2001 From: Arturo Buzarra Date: Wed, 18 Jul 2018 11:14:45 +0200 Subject: [PATCH] dey-examples: add libdigiapix Watchdog example https://jira.digi.com/browse/DEL-6071 Signed-off-by: Arturo Buzarra --- apix-watchdog-example/Makefile | 37 ++++++++ apix-watchdog-example/README.md | 55 ++++++++++++ apix-watchdog-example/main.c | 150 ++++++++++++++++++++++++++++++++ samples-manifest.xml | 16 ++++ 4 files changed, 258 insertions(+) create mode 100644 apix-watchdog-example/Makefile create mode 100644 apix-watchdog-example/README.md create mode 100644 apix-watchdog-example/main.c diff --git a/apix-watchdog-example/Makefile b/apix-watchdog-example/Makefile new file mode 100644 index 0000000..a417c3d --- /dev/null +++ b/apix-watchdog-example/Makefile @@ -0,0 +1,37 @@ +# +# Copyright 2018, Digi International Inc. +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +# PERFORMANCE OF THIS SOFTWARE. +# + +BINARY := apix-watchdog-example + +CFLAGS += -Wall -O0 + +CFLAGS += $(shell pkg-config --cflags libdigiapix) +LDLIBS += $(shell pkg-config --libs libdigiapix) + +$(BINARY): main.o + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +.PHONY: install +install: $(BINARY) + install -d $(DESTDIR)/usr/bin + install -m 0755 $^ $(DESTDIR)/usr/bin/ + +.PHONY: all +all: $(BINARY) + +.PHONY: clean +clean: + -rm -f *.o $(BINARY) diff --git a/apix-watchdog-example/README.md b/apix-watchdog-example/README.md new file mode 100644 index 0000000..20a4096 --- /dev/null +++ b/apix-watchdog-example/README.md @@ -0,0 +1,55 @@ +Digi APIX Watchdog Example Application +=================================== + +Example application to access and manage watchdog using the Digi APIX library. + +This application enables a watchdog device on the board. After that, the +application starts refreshing the watchdog timer until the test time is consumed, +then the device will reboot automatically after the timeout expires. + +Running the application +----------------------- +Once the binary is in the target, launch the application: + +``` +# ./apix-watchdog-example +Example application using libdigiapix Watchdog support + +Usage: apix-watchdog-example + + Watchdog device file to manage + Timeout to set Watchdog timer (default 10) + Test duration in seconds (default 60) + +``` + +If no arguments are provided, the example will use the default values: + - Specific application default values are defined in the main file. + +Compiling the application +------------------------- +This demo can be compiled using a Digi Embedded Yocto based toolchain. Make +sure to source the corresponding toolchain of the platform you are using, e.g: + +``` +$> . /environment-setup-cortexa7hf-vfp-neon-dey-linux-gnueabi +$> make +``` + +More information about [Digi Embedded Yocto](https://github.com/digi-embedded/meta-digi). + +License +------- +Copyright 2018, Digi International Inc. + +Permission to use, copy, modify, and/or distribute this software for any purpose +with or without fee is hereby granted, provided that the above copyright notice +and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER +TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF +THIS SOFTWARE. diff --git a/apix-watchdog-example/main.c b/apix-watchdog-example/main.c new file mode 100644 index 0000000..0bc635d --- /dev/null +++ b/apix-watchdog-example/main.c @@ -0,0 +1,150 @@ +/* + * Copyright 2018, Digi International Inc. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE + * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include + +#include "libdigiapix/watchdog.h" + +#define DEFAULT_WD_DEVICE_FILE "/dev/watchdog" +#define DEFAULT_WD_TIMEOUT 10 +#define DEFAULT_WD_TEST_TIME 60 + +static wd_t *wd; + +/* + * usage_and_exit() - Show usage information and exit with 'exitval' return + * value + * + * @name: Application name. + * @exitval: The exit code. + */ +static void usage_and_exit(char *name, int exitval) +{ + fprintf(stdout, + "Example application using libdigiapix Watchdog support\n" + "\n" + "Usage: %s \n\n" + " Watchdog device file to manage\n" + " Timeout to set Watchdog timer (default 10)\n" + " Test duration in seconds (default 60)\n" + "\n", name); + + exit(exitval); +} + +/* + * cleanup() - Frees all the allocated memory before exiting + */ +static void cleanup(void) +{ + /* Free watchdog */ + ldx_watchdog_free(wd); +} + +/* + * sigaction_handler() - Handler to execute after receiving a signal + * + * @signum: Received signal. + */ +static void sigaction_handler(int signum) +{ + /* 'atexit' executes the cleanup function */ + exit(EXIT_FAILURE); +} + +/* + * register_signals() - Registers program signals + */ +static void register_signals(void) +{ + struct sigaction action; + + action.sa_handler = sigaction_handler; + action.sa_flags = 0; + sigemptyset(&action.sa_mask); + + sigaction(SIGHUP, &action, NULL); + sigaction(SIGINT, &action, NULL); + sigaction(SIGTERM, &action, NULL); +} + +int main(int argc, char *argv[]) +{ + int timeout = 0, test_time = 0; + char *arg = NULL; + char *name = basename(argv[0]); + char wd_device_file[512] = {0}; + + /* Check input parameters */ + if (argc == 1) { + /* Use default values */ + strcpy(wd_device_file, DEFAULT_WD_DEVICE_FILE); + timeout = DEFAULT_WD_TIMEOUT; + test_time = DEFAULT_WD_TEST_TIME; + } else if (argc == 4) { + /* Parse command line arguments */ + timeout = (int)strtol(argv[2], &arg, 10); + if (*arg || timeout <= 0) { + printf("%s: Invalid timeout value\n", __func__); + return EXIT_FAILURE; + } + test_time = (int)strtol(argv[3], &arg, 10); + if (*arg || test_time <= 0) { + printf("%s: Invalid test time value\n", __func__); + return EXIT_FAILURE; + } + strcpy(wd_device_file, argv[1]); + } else { + usage_and_exit(name, EXIT_FAILURE); + } + + /* Register signals and exit cleanup function */ + atexit(cleanup); + register_signals(); + + wd = ldx_watchdog_request(wd_device_file); + if (!wd) { + printf("Failed to initialize Watchdog\n"); + return EXIT_FAILURE; + } + + /* Configure watchdog timeout */ + if (ldx_watchdog_set_timeout(wd, timeout) != 0) { + printf("Failed to set watchdog timeout to %d seconds\n", timeout); + return EXIT_FAILURE; + } + printf("Watchdog timeout modified to %d seconds\n", timeout); + + while (test_time > 0) { + printf("Refreshing watchdog timer (%d s)\n", test_time); + ldx_watchdog_refresh(wd); + sleep(1); + test_time--; + } + + while (timeout > 0) { + printf("System will reboot in %d s\n", timeout - 1); + sleep(1); + timeout--; + } + + return EXIT_SUCCESS; +} diff --git a/samples-manifest.xml b/samples-manifest.xml index 1ead127..627b277 100644 --- a/samples-manifest.xml +++ b/samples-manifest.xml @@ -79,4 +79,20 @@ the page size and the address size in bytes. ccimx6ulsbc + + Digi APIX Watchdog Example + +Example application to access and manage watchdog using the Digi APIX library. +This application enables a watchdog device on the board. After that, the +application starts refreshing the watchdog timer until the test time is consumed, +then the device will reboot automatically after the timeout expires. + + apix-watchdog-example + + ccimx6sbc + ccimx6qpsbc + ccimx6ulstarter + ccimx6ulsbc + +