From 1b4eba187171e36b8222e9fb78e0ed4946df51db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 19 Mar 2021 16:58:14 +0100 Subject: [PATCH] Write parser for temp profile language. Not yet tested. --- .../config-parser/temp-profile-parser.c | 95 +++++++++++++++++-- .../config-parser/temp-profile-parser.h | 9 +- 2 files changed, 90 insertions(+), 14 deletions(-) diff --git a/stm-firmware/config-parser/temp-profile-parser.c b/stm-firmware/config-parser/temp-profile-parser.c index 526fe83..72b3145 100644 --- a/stm-firmware/config-parser/temp-profile-parser.c +++ b/stm-firmware/config-parser/temp-profile-parser.c @@ -20,6 +20,8 @@ #include #include +#include +#include #include struct pl_command_list_map { @@ -81,34 +83,113 @@ static int read_line_until_comment(FIL *f, char *buffer, uint32_t buffsize) return 0; } +static const struct pl_command_list_map *string_to_command(const char *str) +{ + uint32_t i; + const struct pl_command_list_map *ret = NULL; + + if (!str) + return NULL; + + for (i = 0u; i < _PL_NUM_CMDS; i++) { + if (!strcmp(str, cmd_list_map[i].token)) { + ret = &cmd_list_map[i]; + break; + } + } + + return ret; +} + +static int parse_line(char *line, struct pl_command *cmd) +{ + uint8_t token_idx = 0; + char *token; + const char * const delim = " \t"; + const struct pl_command_list_map *map; + char *endptr; + + if (!line || !cmd) + return -1000; + + token = strtok(line, delim); + while (token && token_idx <= PROFILE_LANG_MAX_NUM_ARGS) { + switch (token_idx) { + case 0: + map = string_to_command(token); + cmd->cmd = map->command; + break; + default: + if (!map) { + /* No valid command found */ + return -1; + } + cmd->params[token_idx - 1] = strtof(token, &endptr); + if (endptr == token) { + /* Invalid parameter */ + return -2; + } + break; + } + + token = strtok(NULL, delim); + token_idx++; + } + + return 0; +} + enum pl_ret_val temp_profile_parse_from_file(const char *filename, struct pl_command *cmd_list, uint32_t cmd_list_length, - struct pl_error *error, uint32_t *cmds_parsed) { FIL script_file; FRESULT fres; + int res; enum pl_ret_val ret = PL_RET_SUCCESS; char workbuff[256]; + uint32_t cmd_idx; - if (!filename || !cmd_list || !cmd_list_length || !cmds_parsed || !error) + if (!filename || !cmd_list || !cmd_list_length || !cmds_parsed) return PL_RET_PARAM_ERR; - error->column = 0; - error->line = 0; + fres = f_open(&script_file, filename, FA_READ); if (fres != FR_OK) { - error->errormsg = "Error opening file"; ret = PL_RET_DISK_ERR; goto exit; } + cmd_idx = 0; + *cmds_parsed = 0; + do { - /* TODO: Read lines and parse them */ + /* read in the line */ + res = read_line_until_comment(&script_file, workbuff, sizeof(workbuff)); + if (res < 0) { + ret = PL_RET_DISK_ERR; + goto exit_close; + } + + /* Check if list already full */ + if (cmd_idx >= cmd_list_length) { + ret = PL_RET_LIST_FULL; + goto exit_close; + } + + /* Parse the line */ + res = parse_line(workbuff, &cmd_list[cmd_idx]); + if (res) { + ret = PL_RET_SCRIPT_ERR; + goto exit_close; + } + cmd_idx++; + *cmds_parsed= cmd_idx; + } while (!f_eof(&script_file)); - +exit_close: (void)f_close(&script_file); exit: return ret; diff --git a/stm-firmware/include/reflow-controller/config-parser/temp-profile-parser.h b/stm-firmware/include/reflow-controller/config-parser/temp-profile-parser.h index 344d7d7..fed19e6 100644 --- a/stm-firmware/include/reflow-controller/config-parser/temp-profile-parser.h +++ b/stm-firmware/include/reflow-controller/config-parser/temp-profile-parser.h @@ -42,21 +42,16 @@ enum pl_ret_val { PL_RET_SCRIPT_ERR, }; -struct pl_error { - const char *errormsg; - uint32_t line; - uint32_t column; -}; +#define PROFILE_LANG_MAX_NUM_ARGS (8) struct pl_command { enum pl_command_type cmd; - float params[8]; + float params[PROFILE_LANG_MAX_NUM_ARGS]; }; enum pl_ret_val temp_profile_parse_from_file(const char *filename, struct pl_command *cmd_list, uint32_t cmd_list_length, - struct pl_error *error, uint32_t *cmds_parsed); #endif /* __CONFIG_PARSER_TEMP_PROFILE_PARSER_H__ */