Annotation of embedtools/src/butz.c, revision 1.1.2.4

1.1.2.1   misho       1: #include "global.h"
                      2: 
                      3: 
                      4: cfg_root_t cfg;
                      5: sched_root_task_t *root;
1.1.2.3   misho       6: int Kill, gpio, maxpins, defact, resbut, led = -1;
1.1.2.1   misho       7: extern char compiled[], compiledby[], compilehost[];
                      8: char szCfgName[PATH_MAX] = BUTZ_CFG;
1.1.2.2   misho       9: char szDevName[PATH_MAX] = _PATH_DEVGPIOC"0";
1.1.2.1   misho      10: 
                     11: static void
                     12: Usage()
                     13: {
                     14:        printf( " -= Butz =- Service for board buttons management\n"
                     15:                "=== %s === %s@%s ===\n\n"
                     16:                "  Syntax: butz [options] [set_value]\n"
                     17:                "\n"
                     18:                "\t-c <cfgfile>\tConfig file [default: /etc/butz.conf]\n"
1.1.2.2   misho      19:                "\t-d <devname>\tGPIO control device [default: /dev/gpioc]\n"
1.1.2.1   misho      20:                "\n", compiled, compiledby, compilehost);
                     21: }
                     22: 
1.1.2.3   misho      23: static int
                     24: prepareGPIO()
                     25: {
                     26:        const char *str;
                     27:        struct gpio_pin pin;
                     28:        struct gpio_req req;
                     29: 
                     30:        str = cfg_getAttribute(&cfg, "reset", "default_after");
                     31:        if (str)
                     32:                defact = strtol(str, NULL, 0);
                     33:        str = cfg_getAttribute(&cfg, "reset", "gpio_led_ping");
                     34:        if (str)
                     35:                led = strtol(str, NULL, 0);
                     36:        str = cfg_getAttribute(&cfg, "reset", "gpio_pin");
                     37:        if (!str || maxpins < (resbut = strtol(str, NULL, 0))) {
                     38:                EERROR(EINVAL, "Reset button pin not defined or wrong number!");
                     39:                return -1;
                     40:        }
                     41: 
                     42:        /* switch button pin into input state */
                     43:        pin.gp_pin = resbut;
                     44:        pin.gp_flags = GPIO_PIN_INPUT;
                     45:        if (ioctl(gpio, GPIOSETCONFIG, &pin) == -1)
                     46:                return -1;
                     47:        /* toggle led pin */
                     48:        if (led != -1) {
                     49:                req.gp_pin = led;
                     50:                if (ioctl(gpio, GPIOTOGGLE, &req) == -1) {
                     51:                        ESYSERR(0);
                     52:                        return -1;
                     53:                }
                     54:        }
                     55: 
                     56:        return 0;
                     57: }
                     58: 
1.1.2.1   misho      59: static void *
                     60: sigHandler(sched_task_t *task)
                     61: {
                     62:        switch (TASK_VAL(task)) {
                     63:                case SIGHUP:
                     64:                        cfgUnloadConfig(&cfg);
                     65:                        if (cfgLoadConfig(szCfgName, &cfg)) {
                     66:                                ELIBERR(cfg);
                     67:                                Kill++;
                     68:                        }
1.1.2.3   misho      69:                        if (prepareGPIO())
                     70:                                Kill++;
1.1.2.1   misho      71:                        break;
                     72:                case SIGTERM:
                     73:                        Kill++;
                     74:                        break;
                     75:                default:
                     76:                        EERROR(EINVAL, "Undefined signal %lu!", TASK_VAL(task));
                     77:                        break;
                     78:        }
                     79:        taskExit(task, NULL);
                     80: }
                     81: 
1.1.2.2   misho      82: static void *
1.1.2.3   misho      83: execReset(sched_task_t *task)
                     84: {
                     85:        pid_t pid;
                     86:        const char *str;
                     87: 
                     88:        str = cfg_getAttribute(&cfg, "reset", "click");
                     89:        if (!str)
                     90:                taskExit(task, NULL);
                     91: 
                     92:        if ((pid = fork()) == -1)
                     93:                ESYSERR(0);
                     94:        else if (!pid) {
                     95:                execl(str, str, "click", NULL);
                     96:                ESYSERR(0);
                     97:                _exit(127);
                     98:        }
                     99: 
                    100:        taskExit(task, NULL);
                    101: }
                    102: 
                    103: static void *
                    104: execDefault(sched_task_t *task)
                    105: {
                    106:        pid_t pid;
                    107:        const char *str;
                    108: 
                    109:        str = cfg_getAttribute(&cfg, "reset", "default");
                    110:        if (!str)
                    111:                taskExit(task, NULL);
                    112: 
                    113:        if ((pid = fork()) == -1)
                    114:                ESYSERR(0);
                    115:        else if (!pid) {
                    116:                execl(str, str, "default", NULL);
                    117:                ESYSERR(0);
                    118:                _exit(127);
                    119:        }
                    120: 
                    121:        taskExit(task, NULL);
                    122: }
                    123: 
                    124: static void *
1.1.2.2   misho     125: butReset(sched_task_t *task)
                    126: {
                    127:        struct timespec ts = { 1, 0 };
                    128:        struct gpio_req req;
1.1.2.3   misho     129:        static int flg = 0;
1.1.2.2   misho     130: 
1.1.2.3   misho     131:        /* toggle led pin */
                    132:        if (led != -1) {
                    133:                req.gp_pin = led;
                    134:                if (ioctl(gpio, GPIOTOGGLE, &req) == -1)
                    135:                        ESYSERR(0);
                    136:        }
                    137:        /* check reset button */
                    138:        req.gp_pin = resbut;
                    139:        if (ioctl(gpio, GPIOGET, &req) == -1) {
                    140:                ESYSERR(0);
1.1.2.2   misho     141:                goto end;
                    142:        }
1.1.2.3   misho     143:        /* switch on */
                    144:        if (!req.gp_value)
                    145:                if (++flg == 1)
                    146:                        schedSuspend(TASK_ROOT(task), execReset, NULL, flg, NULL, 0);
                    147:        if (defact && flg >= defact) {
                    148:                schedCancelby(TASK_ROOT(task), taskSUSPEND, CRITERIA_CALL, execReset, NULL);
                    149:                schedEvent(TASK_ROOT(task), execDefault, NULL, flg, NULL, 0);
1.1.2.2   misho     150:                goto end;
                    151:        }
1.1.2.3   misho     152:        if (flg && req.gp_value)
                    153:                schedResumeby(TASK_ROOT(task), CRITERIA_ID, (void*) 1);
1.1.2.2   misho     154: end:
                    155:        schedTimer(TASK_ROOT(task), TASK_FUNC(task), TASK_ARG(task), 
                    156:                        ts, TASK_DATA(task), TASK_DATLEN(task));
                    157:        taskExit(task, NULL);
                    158: }
                    159: 
1.1.2.1   misho     160: 
                    161: int
                    162: main(int argc, char **argv)
                    163: {
                    164:        char ch;
1.1.2.4 ! misho     165:        pid_t pid;
1.1.2.2   misho     166:        struct timespec ts = { 1, 0 };
1.1.2.1   misho     167: 
1.1.2.3   misho     168:        while ((ch = getopt(argc, argv, "hc:d:")) != -1)
1.1.2.1   misho     169:                switch (ch) {
1.1.2.2   misho     170:                        case 'd':
                    171:                                strlcpy(szDevName, optarg, sizeof szDevName);
                    172:                                break;
1.1.2.1   misho     173:                        case 'c':
                    174:                                strlcpy(szCfgName, optarg, sizeof szCfgName);
                    175:                                break;
                    176:                        case 'h':
                    177:                        default:
                    178:                                Usage();
                    179:                                return 1;
                    180:                }
                    181:        argc -= optind;
                    182:        argv += optind;
                    183: 
1.1.2.4 ! misho     184:        switch ((pid = fork())) {
        !           185:                case -1:
        !           186:                        printf("Error:: failed fork() #%d - %s\n", errno, strerror(errno));
        !           187:                        return 1;
        !           188:                case 0:
        !           189:                        setsid();
        !           190:                        chdir("/");
        !           191: 
        !           192:                        gpio = open(_PATH_DEVNULL, O_RDWR);
        !           193:                        if (gpio > 2) {
        !           194:                                dup2(gpio, STDIN_FILENO);
        !           195:                                dup2(gpio, STDOUT_FILENO);
        !           196:                                dup2(gpio, STDERR_FILENO);
        !           197:                                close(gpio);
        !           198:                        }
        !           199:                        break;
        !           200:                default:
        !           201:                        return 0;
        !           202:        }
        !           203: 
1.1.2.1   misho     204:        openlog("butz", LOG_PID | LOG_CONS, LOG_DAEMON);
1.1.2.2   misho     205:        gpio = open(szDevName, O_RDONLY);
                    206:        if (gpio == -1) {
1.1.2.4 ! misho     207:                ESYSERR(0);
1.1.2.2   misho     208:                return 1;
                    209:        }
                    210:        if (ioctl(gpio, GPIOMAXPIN, &maxpins) == -1) {
1.1.2.4 ! misho     211:                ESYSERR(0);
1.1.2.2   misho     212:                return 1;
                    213:        }
                    214: 
1.1.2.1   misho     215:        if (cfgLoadConfig(szCfgName, &cfg)) {
                    216:                ELIBERR(cfg);
1.1.2.2   misho     217:                close(gpio);
1.1.2.1   misho     218:                return 1;
                    219:        }
1.1.2.3   misho     220:        if (prepareGPIO()) {
                    221:                close(gpio);
                    222:                return 1;
                    223:        }
1.1.2.1   misho     224:        root = schedBegin();
                    225:        if (!root) {
                    226:                ELIBERR(sched);
1.1.2.2   misho     227:                close(gpio);
1.1.2.1   misho     228:                return 1;
                    229:        }
                    230: 
                    231:        schedSignal(root, sigHandler, NULL, SIGHUP, NULL, 0);
                    232:        schedSignal(root, sigHandler, NULL, SIGTERM, NULL, 0);
1.1.2.4 ! misho     233:        schedTimer(root, butReset, NULL, ts, NULL, 0);
1.1.2.1   misho     234: 
1.1.2.2   misho     235:        schedRun(root, &Kill);
1.1.2.1   misho     236:        schedEnd(&root);
                    237:        cfgUnloadConfig(&cfg);
1.1.2.2   misho     238:        close(gpio);
1.1.2.1   misho     239:        closelog();
                    240:        return 0;
                    241: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>