Exemple #1
0
int main(int argc, char** argv) {
    struct option long_options[] = { { "gpio", 1, 0, 0 },
        { "address", 1, 0, 0 }, { "command", 1, 0, 0 }, { "receiver", 1, 0, 0 },
        { "retry", 1, 0, 0 }, { NULL, 0, 0, 0 } };
    unsigned int address = 0;
    unsigned char receiver = 1;
    long int a2i;
    int gpio = -1;
    char command = UNKNOWN;
    char *end;
    int retry = 5, i, c;

    if (setuid(0)) {
        perror("setuid");
        return -1;
    }

    while (1) {
        c = getopt_long(argc, argv, "", long_options, &i);
        if (c == -1)
            break;
        switch (c) {
            case 0:
                if (strcmp(long_options[i].name, "gpio") == 0) {
                    a2i = strtol(optarg, &end, 10);
                    if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) {
                        break;
                    }
                    gpio = a2i;
                } else if (strcmp(long_options[i].name, "address") == 0) {
                    a2i = strtol(optarg, &end, 10);
                    if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) {
                        break;
                    }
                    address = a2i;
                } else if (strcmp(long_options[i].name, "receiver") == 0) {
                    a2i = strtol(optarg, &end, 10);
                    if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) {
                        break;
                    }
                    receiver = a2i;
                } else if (strcmp(long_options[i].name, "command") == 0) {
                    command = get_command_char(optarg);
                } else if (strcmp(long_options[i].name, "command") == 0) {
                    a2i = strtol(optarg, &end, 10);
                    if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) {
                        break;
                    }
                    retry = a2i;
                }
                break;
            default:
                usage(argv[0]);
        }
    }

    if (command == UNKNOWN || address == 0 || gpio == -1 || receiver == 0) {
        usage(argv[0]);
    }

    // store pid and lock it
    store_pid();

    if (wiringPiSetup() == -1) {
        fprintf(stderr, "Wiring Pi not installed");
        return -1;
    }

    openlog("homeasy", LOG_PID | LOG_CONS, LOG_USER);
    syslog(LOG_INFO, "remote: %d, receiver, %d, command: %d\n", address,
           receiver, command);
    closelog();

    pinMode(gpio, OUTPUT);
    piHiPri(99);

    for (c = 0; c != retry; c++) {
        for (i = 0; i < 5; i++) {
            transmit(gpio, address, receiver, command);
        }

        sleep(1);
    }

    return 0;
}
Exemple #2
0
int main(int argc, char **argv) {
    struct option long_options[] = { { "gpio", 1, 0, 0 },
        { "address", 1, 0, 0 }, { "command", 1, 0, 0 }, { NULL, 0, 0, 0 } };
    unsigned char key;
    unsigned short address = 0;
    unsigned short code = 0;
    long int a2i;
    int gpio = -1, i, c;
    char command = UNKNOWN;
    char *progname, *end;

    if (setuid(0)) {
        perror("setuid");
        return -1;
    }

    while (1) {
        c = getopt_long(argc, argv, "", long_options, &i);
        if (c == -1)
            break;
        switch (c) {
            case 0:
                if (strcmp(long_options[i].name, "gpio") == 0) {
                    a2i = strtol(optarg, &end, 10);
                    if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) {
                        break;
                    }
                    gpio = a2i;
                } else if (strcmp(long_options[i].name, "address") == 0) {
                    a2i = strtol(optarg, &end, 10);
                    if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) {
                        break;
                    }
                    address = a2i;
                } else if (strcmp(long_options[i].name, "command") == 0) {
                    command = get_command_char(optarg);
                }
                break;
            default:
                usage(argv[0]);
        }
    }

    if (command == UNKNOWN || address == 0 || gpio == -1) {
        usage(argv[0]);
    }

    // store pid and lock it
    store_pid();

    srand(time(NULL));
    key = rand() % 255;

    if (wiringPiSetup() == -1) {
        fprintf(stderr, "Wiring Pi not installed");
        return -1;
    }

    openlog("srts", LOG_PID | LOG_CONS, LOG_USER);

    progname = basename(argv[0]);

    code = get_next_code(progname, address);
    syslog(LOG_INFO, "remote: %d, command: %d, code: %d\n", address, command,
           code);
    closelog();

    piHiPri (99);
    pinMode(gpio, OUTPUT);
    srts_transmit(gpio, key, address, command, code, 0);

    c = 7;
    if (command == PROG) {
        c = 20;
    }

    for (i = 0; i < c; i++) {
        srts_transmit(gpio, key, address, command, code, 1);
    }
    store_code(progname, address, code);

    return 0;
}
Exemple #3
0
int main( void )
{
    int i;
    
    /* holds both command characters */
    char command[ 2 ];
    
    /* set up the three libraries */
    struct Ordered_container* lib_title = OC_create_container( comp_Record_by_title );
    struct Ordered_container* lib_ID    = OC_create_container( comp_Record_by_ID );
    struct Ordered_container* catalog   = OC_create_container( comp_Collection_by_name );
    
    for ( ; ; )
    {
        printf( "\nEnter command: " );
        
        /* load the command chars */
        for ( i = 0 ; i < 2; ++i)
        {
            command[ i ] = get_command_char();
        }
        
        switch ( command[ 0 ] )
        {
            case 'f' :/* find (records only)*/
                switch ( command[ 1 ] )
                {
                    case 'r' :
                        find_record_print( lib_title );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'p' : /* print */
                switch ( command[ 1 ] )
                {
                    case 'r' :
                        print_record( lib_ID );
                        break;
                    case 'L':
                        print_containter( lib_title, "Library", "records", (void (*)(void*))print_Record );
                        break;
                    case 'C':
                        /* print_catalog( catalog ); */
                        print_containter( catalog, "Catalog", "collections", (void (*)(void*))print_Collection );
                        break;
                    case 'a': /* allocation */
                        print_allocation( lib_title, lib_ID, catalog );
                        break;
                    case 'c':
                        print_collection_main( catalog );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'm': /* modify (rating only) */
                switch ( command[ 1 ] )
                {
                    case 'r':
                        modify_rating( lib_ID );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'a' : /* add */
                switch ( command[ 1 ] )
                {
                    case 'r' :
                        add_record( lib_title, lib_ID );
                        break;
                    case 'c':
                        add_coll( catalog );
                        break;
                    case 'm':
                        add_member( lib_ID , catalog );
                        break;
                    case 'a': /* allocation */
                        /* throw error */
                        print_error_clear( "Unrecognized command!\n");
                        break;
                    default:
                        break;
                }
                break;
            case 'd': /* delete */
                switch ( command[ 1 ] )
                {
                    case 'r' :
                        delete_record( lib_title, lib_ID, catalog );
                        break;
                    case 'c':
                        delete_collection( catalog );
                        break;
                    case 'm':
                         remove_member( lib_ID , catalog );
                        break;
                    case 'a': /* allocation */
                        /* throw error */
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'c': /* clear */
                switch ( command[ 1 ] )
                {
                    case 'L':
                        clear_library( lib_title, lib_ID, catalog, "All records deleted\n" );
                        break;
                    case 'C':
                        clear_container( catalog, ( void(*)(void*) )destroy_Collection, "All collections deleted\n" );
                        break;
                    case 'A':
                        clear_all(lib_title, lib_ID, catalog, "All data deleted\n" );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 's': /* save */
                switch ( command[ 1 ] ) {
                    case 'A':
                        save_all_to_file( lib_title, catalog );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'r': /* restore */
                switch ( command[ 1 ] ) {
                    case 'A':
                        load_from_file( lib_title, lib_ID, catalog );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'q':
                switch ( command[ 1 ] )
                {
                    case 'q':
                        /* clean up memory */
                        quit( lib_title, lib_ID, catalog );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
            default:
                /* throw error for bad input */
                print_error_clear( "Unrecognized command!\n");
                break;
        }
    }
	return 0;
}