Example #1
0
int main(int argc, char *argv[]) {
    struct USBConnection *connection;
    int opt;
    int repeatCount = 0;
    char *script = NULL;
    int delay = 10;
    int daemon = 0;
    while ((opt = getopt(argc, argv, "s:d:c:Dv")) != -1) {
        switch (opt) {
            case 's':
                script = optarg;
                break;
            case 'd':
                delay = atoi(optarg);
                break;
            case 'c':
                repeatCount = atoi(optarg);
                break;
            case 'D':
                daemon = 1;
                break;
            case 'v':
                enable_debug();
                break;
            default:
                printUsage(argv[0]);
                return -1;
        }
    }
    if (optind >= argc) {
        fprintf(stderr, "Missing USB device parameter.\n");
        printUsage(argv[0]);
        return -1;
    }
    if (daemon && script == NULL) {
        fprintf(stderr, "Missing script parameter. Running the program as a daemon implies -s.\n");
        return -1;
    }
    if (daemon) {
        initlog(1);
        daemonize();
    }
    else {
        initlog(0);
    }
    log_output(LOG_DEBUG, "Opening USB device\n");
    connection = initUSBConnection(argv[optind]);
    if (connection != NULL) {
        int i;
        int increment = 1;
        log_output(LOG_INFO, "Connection initialization successful. UVR mode 0x%X\n", (unsigned int)connection->uvr_mode);
        if (repeatCount == 0) {
            increment = 0;
            repeatCount = 1; // prepare the values in a way that the loop below runs infinitely
        }
        for (i = 0; i < repeatCount; i+=increment) {
            struct SystemState *result;
            result = readCurrentData(connection);
            if (result != NULL) {
                if (script != NULL) {
                    executeProgram(script, result);
                }
                else {
                    printf("Inputs\n");
                    printValueList("S", result->inputs);
                    printf("Outputs\n");
                    printValueList("O", result->outputs);
                    printf("Heat registers\n");
                    printValueList("", result->heatRegisters);
                }
                freeSystemState(result);
                result = NULL;
            }
            sleep(delay);
        }
    }
    else {
        fprintf(stderr, "Could not initialize connection to UVR. %s\n", strerror(errno));
        cleanupUSBConnection(connection);
        return -1;
    }
    cleanupUSBConnection(connection);
    return 0;
}
Example #2
0
static void
print_a_value(FILE * fp, int depth, tOptDesc * pOD, tOptionValue const * ovp)
{
    static char const bool_atr[]  = "<%1$s type=boolean>%2$s</%1$s>\n";
    static char const numb_atr[]  = "<%1$s type=integer>0x%2$lX</%1$s>\n";
    static char const type_atr[]  = "<%s type=%s>";
    static char const null_atr[]  = "<%s/>\n";

    while (--depth >= 0)
        putc(' ', fp), putc(' ', fp);

    switch (ovp->valType) {
    default:
    case OPARG_TYPE_NONE:
        fprintf(fp, null_atr, ovp->pzName);
        break;

    case OPARG_TYPE_STRING:
        print_a_string(fp, ovp->pzName, ovp->v.strVal);
        break;

    case OPARG_TYPE_ENUMERATION:
    case OPARG_TYPE_MEMBERSHIP:
        if (pOD != NULL) {
            tAoUI     opt_state = pOD->fOptState;
            uintptr_t val = pOD->optArg.argEnum;
            char const * typ = (ovp->valType == OPARG_TYPE_ENUMERATION)
                ? "keyword" : "set-membership";

            fprintf(fp, type_atr, ovp->pzName, typ);

            /*
             *  This is a magic incantation that will convert the
             *  bit flag values back into a string suitable for printing.
             */
            (*(pOD->pOptProc))(OPTPROC_RETURN_VALNAME, pOD );
            if (pOD->optArg.argString != NULL) {
                fputs(pOD->optArg.argString, fp);

                if (ovp->valType != OPARG_TYPE_ENUMERATION) {
                    /*
                     *  set membership strings get allocated
                     */
                    AGFREE( pOD->optArg.argString );
                }
            }

            pOD->optArg.argEnum = val;
            pOD->fOptState = opt_state;
            fprintf(fp, close_xml, ovp->pzName);
            break;
        }
        /* FALLTHROUGH */

    case OPARG_TYPE_NUMERIC:
        fprintf(fp, numb_atr, ovp->pzName, ovp->v.longVal);
        break;

    case OPARG_TYPE_BOOLEAN:
        fprintf(fp, bool_atr, ovp->pzName,
                ovp->v.boolVal ? "true" : "false");
        break;

    case OPARG_TYPE_HIERARCHY:
        printValueList(fp, ovp->pzName, ovp->v.nestVal);
        break;
    }
}