--- embedtools/src/butz.c 2014/02/07 23:37:41 1.1.2.1 +++ embedtools/src/butz.c 2014/02/08 00:40:31 1.1.2.2 @@ -3,9 +3,10 @@ cfg_root_t cfg; sched_root_task_t *root; -int Verbose, Kill; +int Kill, maxpins; extern char compiled[], compiledby[], compilehost[]; char szCfgName[PATH_MAX] = BUTZ_CFG; +char szDevName[PATH_MAX] = _PATH_DEVGPIOC"0"; static void Usage() @@ -15,6 +16,7 @@ Usage() " 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" "\t-v\t\tVerbose ...\n" "\n", compiled, compiledby, compilehost); } @@ -40,20 +42,51 @@ sigHandler(sched_task_t *task) 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; + 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; + } + + 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) { + ESYSERR(0); + goto end; + } + +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; + int gpio; + struct timespec ts = { 1, 0 }; - while ((ch = getopt(argc, argv, "hvc:")) != -1) + while ((ch = getopt(argc, argv, "hvc:d:")) != -1) switch (ch) { + case 'd': + strlcpy(szDevName, optarg, sizeof szDevName); + break; case 'c': strlcpy(szCfgName, optarg, sizeof szCfgName); break; - case 'v': - Verbose++; - break; case 'h': default: Usage(); @@ -63,22 +96,38 @@ main(int argc, char **argv) argv += optind; 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)); + return 1; + } + if (ioctl(gpio, GPIOMAXPIN, &maxpins) == -1) { + printf("Error:: Can't get max gpio pins #%d - %s\n", + errno, strerror(errno)); + return 1; + } + if (cfgLoadConfig(szCfgName, &cfg)) { ELIBERR(cfg); + 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); - schedRun(root, &Kill); + schedTimer(root, butReset, (void*) gpio, ts, NULL, 0); + schedRun(root, &Kill); schedEnd(&root); cfgUnloadConfig(&cfg); + close(gpio); closelog(); return 0; }