Esempio n. 1
0
int board_pin_string_to_device_index(const char *str_p)
{
    long pin;

    if (tolower((int)str_p[0]) == 'd') {
        if (std_strtol(&str_p[1], &pin) == NULL) {
            return (-1);
        }
        
        if ((pin < 2) || (pin > 13)) {
            return (-1);
        }
    } else if (tolower((int)str_p[0]) == 'a') {
        if (std_strtol(&str_p[1], &pin) == NULL) {
            return (-1);
        }
        
        if ((pin < 0) || (pin > 5)) {
            return (-1);
        }
        
        pin += 14;
    } else if (strcmp(str_p, "led") == 0) {
        pin = 13;
    } else if (strcmp(str_p, "rx") == 0) {
        pin = 0;
    } else if (strcmp(str_p, "tx") == 0) {
        pin = 1;
    } else {
        return (-1);
    }

    return (pin);
}
Esempio n. 2
0
File: main.c Progetto: eerimoq/simba
static int cmd_set_bits_per_sample_cb(int argc,
                                      const char *argv[],
                                      void *out_p,
                                      void *in_p,
                                      void *arg_p,
                                      void *call_arg_p)
{
    long bits_per_sample;

    if (argc != 2) {
        std_fprintf(out_p, FSTR("Usage: %s <number of bits>\r\n"),
                    argv[0]);

        return (-EINVAL);
    }

    if (std_strtol(argv[1], &bits_per_sample) != 0) {
        std_fprintf(out_p, FSTR("Usage: %s <number of bits>\r\n"),
                    argv[0]);

        return (-EINVAL);
    }

    if ((bits_per_sample < 0) || (bits_per_sample > 12)) {
        std_printf(FSTR("The number of bits per sample bust be "
                        "an interger from 0 to 12.\r\n"));

        return (-EINVAL);
    }

    return (music_player_set_bits_per_sample(&music_player,
                                             bits_per_sample));
}
Esempio n. 3
0
File: board.c Progetto: wuwx/simba
int board_pin_string_to_device_index(const char *str_p)
{
    int port;
    int gpio;
    long bit;

    /* Get gpio and bit. */
    if (str_p[0] != 'p') {
        return (-1);
    }

    port = tolower((int)str_p[1]);

    if ((port < 'a') || (port > 'd')) {
        return (-1);
    }

    gpio = (port - 'a');

    if (std_strtol(&str_p[2], &bit) == NULL) {
        return (-1);
    }

    if (bit < 0) {
        return (-1);
    }

    if ((gpio == 'd' && bit > 3) || (bit > 15)) {
        return (-1);
    }

    return (16 * gpio + bit);
}
Esempio n. 4
0
static int read_request(struct http_server_t *self_p,
                        struct http_server_connection_t *connection_p,
                        struct http_server_request_t *request_p)
{
    int res;
    char buf[128];
    char *header_p;
    char *value_p;

    /* Read the intial line in the request. */
    if (read_initial_request_line(&connection_p->socket,
                                  buf,
                                  request_p) != 0) {
        return (-EIO);
    }

    memset(&request_p->headers, 0, sizeof(request_p->headers));

    /* Read the header lines. */
    while (1) {
        res = read_header_line(&connection_p->socket,
                               buf,
                               &header_p,
                               &value_p);

        if (res == 1) {
            break;
        } else if (res < 0) {
            return (-EIO);
        }

        log_object_print(NULL, LOG_DEBUG, FSTR("%s: %s\r\n"), header_p, value_p);

        /* Save the header field in the request object. */
        if (strcmp(header_p, "Sec-WebSocket-Key") == 0) {
            request_p->headers.sec_websocket_key.present = 1;
            strncpy(request_p->headers.sec_websocket_key.value,
                    value_p,
                    sizeof(request_p->headers.sec_websocket_key.value));
        } else if (strcmp(header_p, "Content-Type") == 0) {
            request_p->headers.content_type.present = 1;
            strncpy(request_p->headers.content_type.value,
                    value_p,
                    sizeof(request_p->headers.content_type.value));
        } else if (strcmp(header_p, "Content-Length") == 0) {
            if (std_strtol(value_p, &request_p->headers.content_length.value) != NULL) {
                request_p->headers.content_length.present = 1;
            }
        } else if (strcmp(header_p, "Authorization") == 0) {
            request_p->headers.authorization.present = 1;
            strncpy(request_p->headers.authorization.value,
                    value_p,
                    sizeof(request_p->headers.authorization.value));
        }
    }

    return (0);
}
Esempio n. 5
0
int configfile_get_long(struct configfile_t *self_p,
                        const char *section_p,
                        const char *property_p,
                        long *value_p)
{
    char buf[16];
    const char *next_p;

    /* Get the property value as a string. */
    if (configfile_get(self_p, section_p, property_p, buf, sizeof(buf)) == NULL) {
        return (-1);
    }

    /* Convert the property value string to a long. */
    next_p = std_strtol(buf, value_p);

    if ((next_p == NULL) || (*next_p != '\0')) {
        return (-1);
    }

    return (0);
}
Esempio n. 6
0
File: main.c Progetto: eerimoq/simba
static int cmd_play_cb(int argc,
                       const char *argv[],
                       void *out_p,
                       void *in_p,
                       void *arg_p,
                       void *call_arg_p)
{
    long song_number;
    struct song_t *song_p;

    if (argc > 2) {
        std_fprintf(out_p, FSTR("Usage: %s [<song number>]\r\n"), argv[0]);

        return (-EINVAL);
    }

    if (argc == 2) {
        if (std_strtol(argv[1], &song_number) != 0) {
            return (-EINVAL);
        }

        /* Find the song in the hash map. */
        song_p = hash_map_get(&song_map, song_number);

        if (song_p == NULL) {
            return (-EINVAL);
        }

        /* Stop the current song and set the new current song. */
        music_player_song_stop(&music_player);
        sem_take(&sem, NULL);
        current_song = song_number;
        sem_give(&sem, 1);
    }

    /* Play the song or resume it if it's paused. */
    return (music_player_song_play(&music_player));
}
Esempio n. 7
0
static int cmd_write_cb(int argc,
                        const char *argv[],
                        void *chout_p,
                        void *chin_p,
                        void *arg_p,
                        void *call_arg_p)
{
    const FAR struct setting_t *setting_p;
    long value;
    int8_t int8;
    int16_t int16;
    int32_t int32;

    if (argc != 3) {
        std_fprintf(chout_p, FSTR("Usage: %s <name> <value>\r\n"), argv[0]);

        return (-1);
    }

    /* Find the setting in the settings array. */
    setting_p = &settings[0];

    while (setting_p->name_p != NULL) {
        if (std_strcmp(argv[1], setting_p->name_p) == 0) {
            switch (setting_p->type) {

            case setting_type_int8_t:
                if (std_strtol(argv[2], &value) == NULL) {
                    return (-1);
                }

                /* Range check. */
                if ((value > 127) || (value < -128)) {
                    std_fprintf(chout_p,
                                FSTR("%ld: value out of range\r\n"),
                                value);
                    return (-1);
                }

                int8 = (int8_t)value;
                settings_write(setting_p->address, &int8, setting_p->size);
                break;

            case setting_type_int16_t:
                if (std_strtol(argv[2], &value) == NULL) {
                    return (-1);
                }

                /* Range check. */
                if ((value > 32767) || (value < -32768)) {
                    std_fprintf(chout_p,
                                FSTR("%ld: value out of range\r\n"),
                                value);
                    return (-1);
                }

                int16 = (int16_t)value;
                settings_write(setting_p->address, &int16, setting_p->size);
                break;

            case setting_type_int32_t:
                if (std_strtol(argv[2], &value) == NULL) {
                    return (-1);
                }

                /* Range check. */
                if ((value > 2147483647) || (value < -2147483648)) {
                    std_fprintf(chout_p,
                                FSTR("%ld: value out of range\r\n"),
                                value);
                    return (-1);
                }

                int32 = (int32_t)value;
                settings_write(setting_p->address, &int32, setting_p->size);
                break;

            case setting_type_string_t:
                /* Range check. */
                if (strlen(argv[2]) >= setting_p->size) {
                    std_fprintf(chout_p,
                                FSTR("%s: string too long\r\n"),
                                argv[2]);
                    return (-1);
                }

                settings_write(setting_p->address, argv[2], setting_p->size);
                break;

            default:
                std_fprintf(chout_p,
                            FSTR("bad setting type %d\r\n"),
                            setting_p->type);
            }

            return (0);
        }

        setting_p++;
    }

    std_fprintf(chout_p, FSTR("%s: setting not found\r\n"), argv[1]);

    return (-1);
}