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

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.2 ! misho       6: int Kill, maxpins;
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:                "\t-v\t\tVerbose ...\n"
                     21:                "\n", compiled, compiledby, compilehost);
                     22: }
                     23: 
                     24: static void *
                     25: sigHandler(sched_task_t *task)
                     26: {
                     27:        switch (TASK_VAL(task)) {
                     28:                case SIGHUP:
                     29:                        cfgUnloadConfig(&cfg);
                     30:                        if (cfgLoadConfig(szCfgName, &cfg)) {
                     31:                                ELIBERR(cfg);
                     32:                                Kill++;
                     33:                        }
                     34:                        break;
                     35:                case SIGTERM:
                     36:                        Kill++;
                     37:                        break;
                     38:                default:
                     39:                        EERROR(EINVAL, "Undefined signal %lu!", TASK_VAL(task));
                     40:                        break;
                     41:        }
                     42:        taskExit(task, NULL);
                     43: }
                     44: 
1.1.2.2 ! misho      45: static void *
        !            46: butReset(sched_task_t *task)
        !            47: {
        !            48:        struct timespec ts = { 1, 0 };
        !            49:        const char *str;
        !            50:        struct gpio_pin pin;
        !            51:        struct gpio_req req;
        !            52: 
        !            53:        str = cfg_getAttribute(&cfg, "reset", "gpio_pin");
        !            54:        if (!str || maxpins < (pin.gp_pin = strtol(str, NULL, 0))) {
        !            55:                EERROR(EINVAL, "Reset button pin not defined or wrong number!");
        !            56:                goto end;
        !            57:        }
        !            58: 
        !            59:        if (ioctl((int) TASK_ARG(task), GPIOGETCONFIG, &pin) == -1)
        !            60:                goto end;
        !            61:        else
        !            62:                req.gp_pin = pin.gp_pin;
        !            63:        if (ioctl((int) TASK_ARG(task), GPIOGET, &req) == -1) {
        !            64:                ESYSERR(0);
        !            65:                goto end;
        !            66:        }
        !            67: 
        !            68: end:
        !            69:        schedTimer(TASK_ROOT(task), TASK_FUNC(task), TASK_ARG(task), 
        !            70:                        ts, TASK_DATA(task), TASK_DATLEN(task));
        !            71:        taskExit(task, NULL);
        !            72: }
        !            73: 
1.1.2.1   misho      74: 
                     75: int
                     76: main(int argc, char **argv)
                     77: {
                     78:        char ch;
1.1.2.2 ! misho      79:        int gpio;
        !            80:        struct timespec ts = { 1, 0 };
1.1.2.1   misho      81: 
1.1.2.2 ! misho      82:        while ((ch = getopt(argc, argv, "hvc:d:")) != -1)
1.1.2.1   misho      83:                switch (ch) {
1.1.2.2 ! misho      84:                        case 'd':
        !            85:                                strlcpy(szDevName, optarg, sizeof szDevName);
        !            86:                                break;
1.1.2.1   misho      87:                        case 'c':
                     88:                                strlcpy(szCfgName, optarg, sizeof szCfgName);
                     89:                                break;
                     90:                        case 'h':
                     91:                        default:
                     92:                                Usage();
                     93:                                return 1;
                     94:                }
                     95:        argc -= optind;
                     96:        argv += optind;
                     97: 
                     98:        openlog("butz", LOG_PID | LOG_CONS, LOG_DAEMON);
1.1.2.2 ! misho      99:        gpio = open(szDevName, O_RDONLY);
        !           100:        if (gpio == -1) {
        !           101:                printf("Error:: Can't open /dev/gpioc #%d - %s\n", 
        !           102:                                errno, strerror(errno));
        !           103:                return 1;
        !           104:        }
        !           105:        if (ioctl(gpio, GPIOMAXPIN, &maxpins) == -1) {
        !           106:                printf("Error:: Can't get max gpio pins #%d - %s\n", 
        !           107:                                errno, strerror(errno));
        !           108:                return 1;
        !           109:        }
        !           110: 
1.1.2.1   misho     111:        if (cfgLoadConfig(szCfgName, &cfg)) {
                    112:                ELIBERR(cfg);
1.1.2.2 ! misho     113:                close(gpio);
1.1.2.1   misho     114:                return 1;
                    115:        }
                    116:        root = schedBegin();
                    117:        if (!root) {
                    118:                ELIBERR(sched);
1.1.2.2 ! misho     119:                close(gpio);
1.1.2.1   misho     120:                return 1;
                    121:        }
                    122: 
                    123:        schedSignal(root, sigHandler, NULL, SIGHUP, NULL, 0);
                    124:        schedSignal(root, sigHandler, NULL, SIGTERM, NULL, 0);
1.1.2.2 ! misho     125:        schedTimer(root, butReset, (void*) gpio, ts, NULL, 0);
1.1.2.1   misho     126: 
1.1.2.2 ! misho     127:        schedRun(root, &Kill);
1.1.2.1   misho     128:        schedEnd(&root);
                    129:        cfgUnloadConfig(&cfg);
1.1.2.2 ! misho     130:        close(gpio);
1.1.2.1   misho     131:        closelog();
                    132:        return 0;
                    133: }

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