/************************************************************************* * (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; 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"; static void Usage() { printf( " -= Butz =- Service for board buttons management\n" "=== %s === %s@%s ===\n\n" " Syntax: butz [options] [set_value]\n" "\n" "\t-c \tConfig file [default: /etc/butz.conf]\n" "\t-d \tGPIO control device [default: /dev/gpioc]\n" "\n", compiled, compiledby, compilehost); } static int prepareGPIO() { const char *str; struct gpio_pin pin; struct gpio_req req; str = cfg_getAttribute(&cfg, "reset", "default_after"); if (str) defact = strtol(str, NULL, 0); str = cfg_getAttribute(&cfg, "reset", "gpio_led_ping"); if (str) led = strtol(str, NULL, 0); str = cfg_getAttribute(&cfg, "reset", "gpio_pin"); if (!str || maxpins < (resbut = strtol(str, NULL, 0))) { EERROR(EINVAL, "Reset button pin not defined or wrong number!"); return -1; } /* switch button pin into input state */ pin.gp_pin = resbut; pin.gp_flags = GPIO_PIN_INPUT; if (ioctl(gpio, GPIOSETCONFIG, &pin) == -1) return -1; /* toggle led pin */ if (led != -1) { req.gp_pin = led; if (ioctl(gpio, GPIOTOGGLE, &req) == -1) { ESYSERR(0); return -1; } } return 0; } static void * sigHandler(sched_task_t *task) { switch (TASK_VAL(task)) { case SIGHUP: cfgUnloadConfig(&cfg); if (cfgLoadConfig(szCfgName, &cfg)) { ELIBERR(cfg); Kill++; } if (prepareGPIO()) Kill++; break; case SIGTERM: Kill++; break; default: EERROR(EINVAL, "Undefined signal %lu!", TASK_VAL(task)); break; } schedSignalSelf(task); taskExit(task, NULL); } static void * execReset(sched_task_t *task) { pid_t pid; const char *str; str = cfg_getAttribute(&cfg, "reset", "click"); if (!str) taskExit(task, NULL); if ((pid = fork()) == -1) ESYSERR(0); else if (!pid) { execl(str, str, "click", NULL); ESYSERR(0); _exit(127); } flg ^= flg; taskExit(task, NULL); } static void * execDefault(sched_task_t *task) { pid_t pid; const char *str; str = cfg_getAttribute(&cfg, "reset", "default"); if (!str) taskExit(task, NULL); if ((pid = fork()) == -1) ESYSERR(0); else if (!pid) { execl(str, str, "default", NULL); ESYSERR(0); _exit(127); } flg ^= flg; taskExit(task, NULL); } static void * butReset(sched_task_t *task) { struct timespec ts = { 1, 0 }; struct gpio_req req; /* check reset button */ req.gp_pin = resbut; if (ioctl(gpio, GPIOGET, &req) == -1) { ESYSERR(0); goto end; } /* switch on */ 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 > 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)); taskExit(task, NULL); } int main(int argc, char **argv) { char ch; pid_t pid; struct timespec ts = { 1, 0 }; while ((ch = getopt(argc, argv, "hc:d:")) != -1) switch (ch) { case 'd': strlcpy(szDevName, optarg, sizeof szDevName); break; case 'c': strlcpy(szCfgName, optarg, sizeof szCfgName); break; case 'h': default: Usage(); return 1; } 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) { ESYSERR(0); return 1; } if (ioctl(gpio, GPIOMAXPIN, &maxpins) == -1) { ESYSERR(0); return 1; } if (cfgLoadConfig(szCfgName, &cfg)) { ELIBERR(cfg); close(gpio); return 1; } if (prepareGPIO()) { close(gpio); return 1; } root = schedBegin(); if (!root) { ELIBERR(sched); close(gpio); return 1; } schedSignal(root, sigHandler, NULL, SIGHUP, NULL, 0); schedSignal(root, sigHandler, NULL, SIGTERM, NULL, 0); schedTimer(root, butReset, NULL, ts, NULL, 0); schedRun(root, &Kill); schedEnd(&root); cfgUnloadConfig(&cfg); close(gpio); closelog(); return 0; }