--- embedtools/src/pceng.c 2011/07/22 14:45:30 1.1.2.4 +++ embedtools/src/pceng.c 2012/04/05 12:22:44 1.1.2.12 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ - * $Id: pceng.c,v 1.1.2.4 2011/07/22 14:45:30 misho Exp $ + * $Id: pceng.c,v 1.1.2.12 2012/04/05 12:22:44 misho Exp $ * ************************************************************************* The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -49,7 +49,7 @@ SUCH DAMAGE. int Verbose, Kill; extern char compiled[], compiledby[], compilehost[]; -sl_config cfg; +cfg_root_t cfg; char szConf[MAXPATHLEN] = DEFAULT_PCENG_CFG; @@ -58,10 +58,10 @@ Usage() { printf( "-= PCEngines =- events managment tool\n" "=== %s === %s@%s ===\n\n" - " Syntax: pceng [options]\n" + " Syntax: pceng [options] [led_no[=<0|1>] ...]\n" "\n" "\t-v\t\tVerbose ...\n" - "\t-b\t\tRun in batch mode ...\n" + "\t-D\t\tRun in background mode ...\n" "\t-c \tConfig events file ...\n" "\n", compiled, compiledby, compilehost); } @@ -73,8 +73,8 @@ sigHand(int sig) switch (sig) { case SIGHUP: - UnloadConfig(&cfg); - if (LoadConfig(szConf, &cfg)) { + cfgUnloadConfig(&cfg); + if (cfgLoadConfig(szConf, &cfg)) { syslog(LOG_ERR, "Error:: #%d - %s", cfg_GetErrno(), cfg_GetError()); raise(SIGTERM); } @@ -93,17 +93,20 @@ sigHand(int sig) int main(int argc, char **argv) { - char ch; - int io, ret = 0, mode = 0; + char ch, ledno, *pos; + int i, ret = 0, mode = 1; struct sigaction sact; +#ifdef __FreeBSD__ + int io; +#endif - while ((ch = getopt(argc, argv, "vhbc:")) != -1) + while ((ch = getopt(argc, argv, "vhDc:")) != -1) switch (ch) { case 'c': strlcpy(szConf, optarg, sizeof szConf); break; - case 'b': - mode = 1; + case 'D': + mode = 0; break; case 'v': Verbose++; @@ -116,26 +119,49 @@ main(int argc, char **argv) argc -= optind; argv += optind; - openlog("pceng", LOG_CONS | LOG_PID, LOG_DAEMON); + if (!mode) + openlog("pceng", LOG_CONS | LOG_PID, LOG_DAEMON); + else + openlog("pceng", LOG_CONS | LOG_PID | LOG_PERROR, LOG_USER); - if (LoadConfig(szConf, &cfg)) { +#ifdef __FreeBSD__ + io = open(_PATH_DEVIO, O_RDONLY); + if (io == -1) { + printf("Error:: in open dev %s #%d - %s\n", _PATH_DEVIO, errno, strerror(errno)); + ret = 2; + goto end; + } +#endif + + if (cfgLoadConfig(szConf, &cfg)) { printf("Error:: #%d - %s\n", cfg_GetErrno(), cfg_GetError()); - return 1; + ret = 1; + goto end; } + if (argc) { + VERB(1) syslog(LOG_WARNING, "LED client ..."); + for (i = 0; !ret && i < argc; pos = NULL, i++) { + if ((pos = strchr(argv[i], '='))) { + *pos++ = 0; + ledno = (char) strtol(argv[i], NULL, 0); + ch = (char) strtol(pos, NULL, 0); + } else { + ledno = (char) strtol(argv[i], NULL, 0); + ch = -1; + } + ret = LED((u_char) ledno, (u_char) ch); + printf("%s:: LED#%d=%d\n", (ch == -1) ? "Get" : "Set", ledno, ret); + } + goto end; + } + sact.sa_handler = sigHand; sigemptyset(&sact.sa_mask); sigaction(SIGHUP, &sact, NULL); sigaction(SIGTERM, &sact, NULL); sigaction(SIGCHLD, &sact, NULL); - io = open(_PATH_DEVIO, O_RDWR); - if (io == -1) { - printf("Error:: in open dev %s #%d - %s\n", _PATH_DEVIO, errno, strerror(errno)); - ret = 2; - goto end; - } - if (!mode) switch (fork()) { case -1: @@ -161,12 +187,14 @@ main(int argc, char **argv) goto end; } - ret = Run(io); + ret = Run(); end: + cfgUnloadConfig(&cfg); +#ifdef __FreeBSD__ if (io > 2) close(io); - UnloadConfig(&cfg); +#endif closelog(); return ret; }