--- embedtools/src/butz.c 2014/02/08 00:40:31 1.1.2.2 +++ embedtools/src/butz.c 2014/02/08 17:07:40 1.1.2.5 @@ -3,7 +3,7 @@ cfg_root_t cfg; sched_root_task_t *root; -int Kill, maxpins; +int Kill, gpio, maxpins, defact, resbut, led = -1; extern char compiled[], compiledby[], compilehost[]; char szCfgName[PATH_MAX] = BUTZ_CFG; char szDevName[PATH_MAX] = _PATH_DEVGPIOC"0"; @@ -17,10 +17,45 @@ Usage() "\n" "\t-c \tConfig file [default: /etc/butz.conf]\n" "\t-d \tGPIO control device [default: /dev/gpioc]\n" - "\t-v\t\tVerbose ...\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) { @@ -31,6 +66,8 @@ sigHandler(sched_task_t *task) ELIBERR(cfg); Kill++; } + if (prepareGPIO()) + Kill++; break; case SIGTERM: Kill++; @@ -43,28 +80,78 @@ sigHandler(sched_task_t *task) } 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); + } + + 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); + } + + taskExit(task, NULL); +} + +static void * butReset(sched_task_t *task) { struct timespec ts = { 1, 0 }; - const char *str; - struct gpio_pin pin; struct gpio_req req; + static int flg = 0; - str = cfg_getAttribute(&cfg, "reset", "gpio_pin"); - if (!str || maxpins < (pin.gp_pin = strtol(str, NULL, 0))) { - EERROR(EINVAL, "Reset button pin not defined or wrong number!"); - goto end; + /* toggle led pin */ + if (led != -1) { + req.gp_pin = led; + if (ioctl(gpio, GPIOTOGGLE, &req) == -1) + ESYSERR(0); } - - if (ioctl((int) TASK_ARG(task), GPIOGETCONFIG, &pin) == -1) - goto end; - else - req.gp_pin = pin.gp_pin; - if (ioctl((int) TASK_ARG(task), GPIOGET, &req) == -1) { + /* check reset button */ + req.gp_pin = resbut; + if (ioctl(gpio, GPIOGET, &req) == -1) { ESYSERR(0); goto end; } - + /* switch on */ + if (!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 ^= flg; + goto end; + } + if (flg && req.gp_value) + schedResumeby(TASK_ROOT(task), CRITERIA_ID, (void*) 1); end: schedTimer(TASK_ROOT(task), TASK_FUNC(task), TASK_ARG(task), ts, TASK_DATA(task), TASK_DATLEN(task)); @@ -76,10 +163,10 @@ int main(int argc, char **argv) { char ch; - int gpio; + pid_t pid; struct timespec ts = { 1, 0 }; - while ((ch = getopt(argc, argv, "hvc:d:")) != -1) + while ((ch = getopt(argc, argv, "hc:d:")) != -1) switch (ch) { case 'd': strlcpy(szDevName, optarg, sizeof szDevName); @@ -95,16 +182,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; } @@ -113,6 +218,10 @@ main(int argc, char **argv) close(gpio); return 1; } + if (prepareGPIO()) { + close(gpio); + return 1; + } root = schedBegin(); if (!root) { ELIBERR(sched); @@ -122,7 +231,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);