--- embedtools/src/butz.c 2014/02/08 16:37:56 1.1.2.3 +++ embedtools/src/butz.c 2017/06/28 15:19:32 1.3 @@ -1,9 +1,55 @@ +/************************************************************************* + * (C) 2014 AITNET - Sofia/Bulgaria - + * by Michael Pounov + * + * $Author: misho $ + * $Id: butz.c,v 1.3 2017/06/28 15:19:32 misho Exp $ + * + ************************************************************************* +The ELWIX and AITNET software is distributed under the following +terms: + +All of the documentation and software included in the ELWIX and AITNET +Releases is copyrighted by ELWIX - Sofia/Bulgaria + +Copyright 2004 - 2017 + by Michael Pounov . All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: +This product includes software developed by Michael Pounov +ELWIX - Embedded LightWeight unIX and its contributors. +4. Neither the name of AITNET nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. +*/ #include "global.h" cfg_root_t cfg; sched_root_task_t *root; -int Kill, gpio, maxpins, defact, resbut, led = -1; +volatile intptr_t Kill; +int gpio, flg, maxpins, defact, resbut, led = -1; extern char compiled[], compiledby[], compilehost[]; char szCfgName[PATH_MAX] = BUTZ_CFG; char szDevName[PATH_MAX] = _PATH_DEVGPIOC"0"; @@ -76,6 +122,8 @@ sigHandler(sched_task_t *task) EERROR(EINVAL, "Undefined signal %lu!", TASK_VAL(task)); break; } + + schedSignalSelf(task); taskExit(task, NULL); } @@ -97,6 +145,7 @@ execReset(sched_task_t *task) _exit(127); } + flg ^= flg; taskExit(task, NULL); } @@ -118,6 +167,7 @@ execDefault(sched_task_t *task) _exit(127); } + flg ^= flg; taskExit(task, NULL); } @@ -126,14 +176,7 @@ butReset(sched_task_t *task) { struct timespec ts = { 1, 0 }; struct gpio_req req; - static int flg = 0; - /* toggle led pin */ - if (led != -1) { - req.gp_pin = led; - if (ioctl(gpio, GPIOTOGGLE, &req) == -1) - ESYSERR(0); - } /* check reset button */ req.gp_pin = resbut; if (ioctl(gpio, GPIOGET, &req) == -1) { @@ -141,16 +184,24 @@ butReset(sched_task_t *task) goto end; } /* switch on */ - if (!req.gp_value) + if (flg != -1 && !req.gp_value) if (++flg == 1) schedSuspend(TASK_ROOT(task), execReset, NULL, flg, NULL, 0); if (defact && flg >= defact) { schedCancelby(TASK_ROOT(task), taskSUSPEND, CRITERIA_CALL, execReset, NULL); schedEvent(TASK_ROOT(task), execDefault, NULL, flg, NULL, 0); + flg = -1; goto end; } - if (flg && req.gp_value) + if (flg > 0 && req.gp_value) schedResumeby(TASK_ROOT(task), CRITERIA_ID, (void*) 1); + + /* toggle led pin */ + if (flg != -1 && led != -1) { + req.gp_pin = led; + if (ioctl(gpio, GPIOTOGGLE, &req) == -1) + ESYSERR(0); + } end: schedTimer(TASK_ROOT(task), TASK_FUNC(task), TASK_ARG(task), ts, TASK_DATA(task), TASK_DATLEN(task)); @@ -162,7 +213,7 @@ int main(int argc, char **argv) { char ch; - int gpio; + pid_t pid; struct timespec ts = { 1, 0 }; while ((ch = getopt(argc, argv, "hc:d:")) != -1) @@ -181,16 +232,34 @@ main(int argc, char **argv) argc -= optind; argv += optind; + switch ((pid = fork())) { + case -1: + printf("Error:: failed fork() #%d - %s\n", errno, strerror(errno)); + return 1; + case 0: + setsid(); + chdir("/"); + + gpio = open(_PATH_DEVNULL, O_RDWR); + if (gpio > 2) { + dup2(gpio, STDIN_FILENO); + dup2(gpio, STDOUT_FILENO); + dup2(gpio, STDERR_FILENO); + close(gpio); + } + break; + default: + return 0; + } + openlog("butz", LOG_PID | LOG_CONS, LOG_DAEMON); gpio = open(szDevName, O_RDONLY); if (gpio == -1) { - printf("Error:: Can't open /dev/gpioc #%d - %s\n", - errno, strerror(errno)); + ESYSERR(0); return 1; } if (ioctl(gpio, GPIOMAXPIN, &maxpins) == -1) { - printf("Error:: Can't get max gpio pins #%d - %s\n", - errno, strerror(errno)); + ESYSERR(0); return 1; } @@ -212,7 +281,7 @@ main(int argc, char **argv) schedSignal(root, sigHandler, NULL, SIGHUP, NULL, 0); schedSignal(root, sigHandler, NULL, SIGTERM, NULL, 0); - schedTimer(root, butReset, (void*) gpio, ts, NULL, 0); + schedTimer(root, butReset, NULL, ts, NULL, 0); schedRun(root, &Kill); schedEnd(&root);