#include "adc.h" #include #include #include typedef struct { const char *start; size_t length; } Word; static int separator(int byte) { return byte == ' ' || byte == '\t'; } static int read_line(FILE *input, char *line, size_t capacity, int *too_long) { size_t used = 0; int byte; int found = 0; *too_long = 0; while ((byte = fgetc(input)) != EOF) { found = 1; if (byte == '\n') { break; } if (used + 1 < capacity) { line[used++] = (char)byte; } else { *too_long = 1; } if (byte == 0) { *too_long = 1; } } line[used] = '\0'; return found; } static int tokenize(const char *line, Word *words, int capacity) { const char *cursor = line; int count = 0; while (*cursor != '\0') { while (separator((unsigned char)*cursor)) { ++cursor; } if (*cursor == '\0') { break; } if (count == capacity) { return -1; } words[count].start = cursor; while (*cursor != '\0' && !separator((unsigned char)*cursor)) { unsigned char byte = (unsigned char)*cursor; if (byte < 33 || byte > 126) { return -1; } ++cursor; } words[count].length = (size_t)(cursor - words[count].start); ++count; } return count; } static int word_is(const Word *word, const char *name) { size_t size = strlen(name); if (word->length != size) { return 0; } return memcmp(word->start, name, size) == 0; } static int parse_integer(const Word *word, int *out) { size_t position = 0; unsigned long long value = 0; unsigned long long limit = INT_MAX; int negative = 0; if (word->length == 0) { return 0; } if (word->start[0] == '-') { negative = 1; position = 1; limit += 1; } if (position == word->length) { return 0; } while (position < word->length) { unsigned char byte = (unsigned char)word->start[position]; unsigned digit; if (byte < '0' || byte > '9') { return 0; } digit = (unsigned)(byte - '0'); if (value > (limit - digit) / 10) { return 0; } value = value * 10 + digit; ++position; } if (!negative) { *out = (int)value; } else if (value == (unsigned long long)INT_MAX + 1) { *out = INT_MIN; } else { *out = -(int)value; } return 1; } static const char *code_name(int result) { switch (result) { case ADC_OK: return "OK"; case ADC_RANGE: return "RANGE"; case ADC_BUSY: return "BUSY"; case ADC_IDLE: return "IDLE"; case ADC_EMPTY: return "EMPTY"; default: return "UNKNOWN"; } } static void print_result(FILE *output, int result) { if (result == ADC_OK) { fputs("OK\n", output); } else { fprintf(output, "ERR %s\n", code_name(result)); } } static void print_frame(FILE *output, int result, const AdcFrame *frame) { if (result == ADC_OK) { fprintf(output, "TX %d %d %d\n", frame->b0, frame->b1, frame->b2); } else { print_result(output, result); } } static void print_reading(FILE *output, int result, const AdcReading *reading) { if (result != ADC_OK) { print_result(output, result); return; } fprintf(output, "READ %d %d %d %d %d %d\n", reading->count, reading->sum, reading->minimum, reading->maximum, reading->last, reading->microvolts); } static void print_state(FILE *output, const AdcStatus *state) { fprintf(output, "STATE %d %d %d %d %d %d %d\n", state->vref, state->mask, state->rounds, state->active, state->next_channel, state->completed_rounds, state->frames); } static int expected_arguments(const Word *verb) { if (word_is(verb, "config")) { return 3; } if (word_is(verb, "feed")) { return 3; } if (word_is(verb, "read")) { return 1; } if (word_is(verb, "start")) { return 0; } if (word_is(verb, "tx")) { return 0; } if (word_is(verb, "state")) { return 0; } if (word_is(verb, "abort")) { return 0; } if (word_is(verb, "clear")) { return 0; } return -1; } static void dispatch(Adc *adc, const Word *verb, const int *arguments, FILE *output) { if (word_is(verb, "config")) { print_result(output, adc_config(adc, arguments[0], arguments[1], arguments[2])); return; } if (word_is(verb, "start")) { print_result(output, adc_start(adc)); return; } if (word_is(verb, "tx")) { AdcFrame frame; int result = adc_tx(adc, &frame); print_frame(output, result, &frame); return; } if (word_is(verb, "feed")) { print_result(output, adc_feed(adc, arguments[0], arguments[1], arguments[2])); return; } if (word_is(verb, "read")) { AdcReading reading; int result = adc_read(adc, arguments[0], &reading); print_reading(output, result, &reading); return; } if (word_is(verb, "state")) { AdcStatus state; adc_status(adc, &state); print_state(output, &state); return; } if (word_is(verb, "abort")) { print_result(output, adc_abort(adc)); return; } print_result(output, adc_clear(adc)); } static void process_line(Adc *adc, const char *line, int too_long, FILE *output) { Word words[5]; int arguments[3] = {0, 0, 0}; int count; int expected; int index; if (too_long) { fputs("ERR SYNTAX\n", output); return; } count = tokenize(line, words, 5); if (count <= 0) { fputs("ERR SYNTAX\n", output); return; } expected = expected_arguments(&words[0]); if (expected < 0 || count != expected + 1) { fputs("ERR SYNTAX\n", output); return; } for (index = 0; index < expected; ++index) { if (!parse_integer(&words[index + 1], &arguments[index])) { fputs("ERR SYNTAX\n", output); return; } } dispatch(adc, &words[0], arguments, output); } int adc_run_cli(FILE *input, FILE *output, Adc *result) { Adc *adc = result; char line[256]; int too_long; int physical = 0; int responses = 0; adc_reset(adc); while (read_line(input, line, sizeof(line), &too_long)) { ++physical; if (physical > 128) { fputs("ERR LIMIT\n", output); ++responses; break; } process_line(adc, line, too_long, output); ++responses; } return responses; }