SCPI parser and status register model implementation (device side) written in C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
scpi_parser/scpi_parser.c

260 lines
5.8 KiB

9 years ago
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
9 years ago
#include "scpi_parser.h"
9 years ago
#include <stdio.h>
#include <stdlib.h>
typedef enum {
PARS_COMMAND,
PARS_ARG, // collect generic arg, terminated with comma or newline. Leading and trailing whitespace ignored.
PARS_ARG_STR_APOS, // collect arg - string with single quotes
PARS_ARG_STR_QUOT, // collect arg - string with double quotes
PARS_ARG_BLOB_PREAMBLE,
PARS_DISCARD_LINE, // used after detecting error
} parser_state_t;
/** parser internal state struct */
static struct {
char err_queue[ERR_QUEUE_LEN][MAX_ERROR_LEN];
9 years ago
uint8_t err_queue_ptr;
9 years ago
parser_state_t state; // current parser internal state
// string buffer, chars collected here until recognized
char charbuf[256];
uint16_t charbuf_ptr;
9 years ago
// recognized complete command level strings (FUNCtion) - exact copy from command struct
char cur_levels[MAX_LEVEL_COUNT][MAX_CMD_LEN];
uint8_t cur_level_ptr; // next free level slot index
9 years ago
9 years ago
bool cmdbuf_kept; // set to 1 after semicolon - cur_levels is kept (removed last part)
SCPI_command_t *detected_cmd; // command is put here after recognition, used as reference for args
SCPI_argval_t args[MAX_PARAM_COUNT];
uint8_t arg_ptr; // next free argument slot index
9 years ago
} pstate = {
// defaults
9 years ago
.err_queue_ptr = 0,
9 years ago
.state = PARS_COMMAND,
.charbuf_ptr = 0,
9 years ago
.cur_level_ptr = 0,
9 years ago
.cmdbuf_kept = false,
.detected_cmd = NULL,
9 years ago
.arg_ptr = 0
9 years ago
};
static void pars_cmd_colon(void); // colon starting a command sub-segment
static void pars_cmd_space(void); // space ending a command
static void pars_cmd_newline(void); // LF
static void pars_cmd_semicolon(void); // semicolon right after a command
9 years ago
static void pars_match_level(void); // match charbuf content to a level, advance level ptr (or fail)
9 years ago
9 years ago
// char matching
#define INRANGE(c, a, b) ((c) >= (a) && (c) <= (b))
#define IS_WHITESPACE(c) (INRANGE((c), 0, 9) || INRANGE((c), 11, 32))
#define IS_LCASE_CHAR(c) INRANGE((c), 'a', 'z')
#define IS_UCASE_CHAR(c) INRANGE((c), 'A', 'Z')
#define IS_NUMBER_CHAR(c) INRANGE((c), '0', '9')
#define IS_IDENT_CHAR(c) (IS_LCASE_CHAR((c)) || IS_UCASE_CHAR((c)) || IS_NUMBER_CHAR((c)) || (c) == '_')
#define IS_INT_CHAR(c) IS_NUMBER_CHAR((c))
#define IS_FLOAT_CHAR(c) (IS_NUMBER_CHAR((c)) || (c) == '.' || (c) == 'e' || (c) == 'E' || (e) == '+' || (e) == '-')
9 years ago
static void pars_reset_cmd(void)
{
pstate.state = PARS_COMMAND;
pstate.charbuf_ptr = 0;
9 years ago
pstate.cur_level_ptr = 0;
9 years ago
pstate.cmdbuf_kept = false;
pstate.detected_cmd = NULL;
9 years ago
pstate.arg_ptr = 0;
9 years ago
}
static void pars_reset_cmd_keeplevel(void)
{
pstate.state = PARS_COMMAND;
pstate.charbuf_ptr = 0;
// rewind to last colon
9 years ago
if (pstate.cur_level_ptr > 0) {
pstate.cur_level_ptr--; // keep prev levels
}
9 years ago
pstate.cmdbuf_kept = true;
pstate.detected_cmd = NULL;
9 years ago
pstate.arg_ptr = 0;
9 years ago
}
void scpi_receive_byte(const uint8_t b)
{
// TODO handle blob here
const char c = (char) b;
switch (pstate.state) {
case PARS_COMMAND:
// Collecting command
9 years ago
if (pstate.charbuf_ptr == 0 && pstate.cur_level_ptr == 0) {
if (IS_WHITESPACE(c)) {
9 years ago
// leading whitespace is ignored
break;
}
}
9 years ago
if (IS_IDENT_CHAR(c)) {
9 years ago
// valid command char
9 years ago
9 years ago
if (pstate.charbuf_ptr < MAX_CMD_LEN) {
pstate.charbuf[pstate.charbuf_ptr++] = c;
9 years ago
} else {
printf("ERROR command part too long.\n");//TODO error
pstate.state = PARS_DISCARD_LINE;
9 years ago
}
9 years ago
9 years ago
} else {
// invalid or delimiter
9 years ago
if (IS_WHITESPACE(c)) {
// pars_cmd_space(); // whitespace in command - end of command?
9 years ago
break;
}
switch (c) {
case ':':
pars_cmd_colon(); // end of a section
break;
case '\n': // line terminator
// pars_cmd_newline();
9 years ago
break;
case ';': // ends a command, does not reset cmd path.
// pars_cmd_semicolon();
9 years ago
break;
default:
printf("ERROR unexpected char '%c' in command.\n", c);//TODO error
pstate.state = PARS_DISCARD_LINE;
}
}
break;
case PARS_DISCARD_LINE:
// drop it. Clear state on newline.
if (c == '\r' || c == '\n') {
pars_reset_cmd();
}
break;
// TODO
}
}
static void pars_cmd_colon(void)
{
if (pstate.charbuf_ptr == 0) {
// No command text before colon
9 years ago
if (pstate.cur_level_ptr == 0 || pstate.cmdbuf_kept) {
// top level command starts with colon (or after semicolon - reset level)
9 years ago
pars_reset_cmd();
} else {
// colon after nothing - error
printf("ERROR unexpected colon in command.\n");//TODO error
pstate.state = PARS_DISCARD_LINE;
}
} else {
9 years ago
// internal colon
pars_match_level();
9 years ago
}
}
#define CHAR_TO_LOWER(ucase) ((ucase) + 32)
#define CHAR_TO_UPPER(lcase) ((lcase) - 32)
/** Check if chars equal, ignore case */
static bool char_equals_ci(char a, char b)
{
if (IS_LCASE_CHAR(a)) {
if (IS_LCASE_CHAR(b)) {
return a == b;
} else if (IS_UCASE_CHAR(b)) {
return a == CHAR_TO_LOWER(b);
} else {
return false;
}
} else if (IS_UCASE_CHAR(a)) {
if (IS_UCASE_CHAR(b)) {
return a == b;
} else if (IS_LCASE_CHAR(b)) {
return a == CHAR_TO_UPPER(b);
} else {
return false;
}
} else {
return a == b; // exact match, not letters
}
}
/** Check if command matches a pattern */
static bool level_str_matches(const char *test, const char *pattern)
{
const uint8_t testlen = strlen(test);
uint8_t pi, ti;
for (pi = 0, ti = 0; pi < strlen(pattern); pi++) {
if (ti > testlen) return false; // not match
const char pc = pattern[pi];
const char tc = test[ti]; // may be at the \0 terminator
if (IS_LCASE_CHAR(pc)) {
// optional char
if (char_equals_ci(pc, tc)) {
ti++; // advance test string
}
continue; // next pi - tc stays in place
} else {
// require exact match (case insensitive)
if (char_equals_ci(pc, tc)) {
ti++;
} else {
return false;
}
}
}
return (ti >= testlen);
}
9 years ago
9 years ago
static void pars_match_level(void)
9 years ago
{
9 years ago
// terminate segment
pstate.charbuf[pstate.charbuf_ptr] = '\0';
9 years ago
9 years ago
//TODO
9 years ago
}