From 19389bc033c602375820188f3a3cbc634c0e2139 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sat, 4 Oct 2014 13:02:53 -0500 Subject: [PATCH] auto-sync --- .../controllers/core/logic_expression.cpp | 49 +++++++++++++++++++ firmware/controllers/core/logic_expression.h | 3 ++ firmware/util/cli_registry.c | 18 ------- firmware/util/cli_registry.h | 2 - firmware/util/efilib.cpp | 27 ++++++++++ firmware/util/efilib.h | 3 ++ unit_tests/test_logic_expression.cpp | 34 ++++++++++++- 7 files changed, 114 insertions(+), 22 deletions(-) diff --git a/firmware/controllers/core/logic_expression.cpp b/firmware/controllers/core/logic_expression.cpp index 940cd3e449..66042aae42 100644 --- a/firmware/controllers/core/logic_expression.cpp +++ b/firmware/controllers/core/logic_expression.cpp @@ -132,6 +132,10 @@ LEElement *LEElementPool::next() { return &pool[index++]; } +bool isNumeric(const char* line) { + return line[0] >= '0' && line[0] <= '9'; +} + const char *processToken(const char *line, char *buffer) { while (line[0] != 0 && line[0] == ' ') { line++; @@ -151,3 +155,48 @@ const char *processToken(const char *line, char *buffer) { return line; } +le_action_e parseAction(const char * line) { + if (strEqualCaseInsensitive("or", line)) { + return LE_OPERATOR_OR; + } else if (strEqualCaseInsensitive("AND", line)) { + return LE_OPERATOR_AND; + } + return LE_UNDEFINED; +} + +static char parsingBuffer[64]; + +LEElement * parseExpression(LEElementPool *pool, const char * line) { + + LEElement *first = NULL; + LEElement *last = NULL; + + while (true) { + line = processToken(line, parsingBuffer); + + if (line == NULL) { + /** + * No more tokens in this line + */ + return first; + } + + LEElement *n = pool->next(); + + if (isNumeric(parsingBuffer)) { + n->init(LE_NUMERIC_VALUE, atoff(parsingBuffer)); + } else { + le_action_e action = parseAction(parsingBuffer); + n->init(action); + } + + if (first == NULL) { + first = n; + last = n; + } else { + last->next = n; + last = last->next; + } + } + return first; +} diff --git a/firmware/controllers/core/logic_expression.h b/firmware/controllers/core/logic_expression.h index 04865e7a52..bfde1c1693 100644 --- a/firmware/controllers/core/logic_expression.h +++ b/firmware/controllers/core/logic_expression.h @@ -73,5 +73,8 @@ private: }; const char *processToken(const char *line, char *buffer); +bool isNumeric(const char* line); +le_action_e parseAction(const char * line); +LEElement * parseExpression(LEElementPool *pool, const char * line); #endif /* LOGIC_EXPRESSION_H_ */ diff --git a/firmware/util/cli_registry.c b/firmware/util/cli_registry.c index 875d796430..a6e6a9c3e2 100644 --- a/firmware/util/cli_registry.c +++ b/firmware/util/cli_registry.c @@ -317,24 +317,6 @@ int tokenLength(const char *msgp) { return result; } -bool strEqualCaseInsensitive(const char *str1, const char *str2) { - // todo: actual implementation! - return strEqual(str1, str2); -} - -bool strEqual(const char *str1, const char *str2) { - // todo: there must be a standard function?! - int len1 = strlen(str1); - int len2 = strlen(str2); - if (len1 != len2) { - return false; - } - for (int i = 0; i < len1; i++) - if (str1[i] != str2[i]) - return false; - return true; -} - void initConsoleLogic() { #if EFI_PROD_CODE || EFI_SIMULATOR initLogging(&logging, "rfi console"); diff --git a/firmware/util/cli_registry.h b/firmware/util/cli_registry.h index 949939af74..a8b2e03499 100644 --- a/firmware/util/cli_registry.h +++ b/firmware/util/cli_registry.h @@ -60,8 +60,6 @@ typedef void (*VoidCharPtrCharPtrCharPtr)(const char *, const char *, const char typedef void (*VoidCharPtrCharPtrCharPtrCharPtrCharPtr)(const char *, const char *, const char *, const char *, const char *); char *validateSecureLine(char *line); -bool strEqualCaseInsensitive(const char *str1, const char *str2); -bool strEqual(const char *str1, const char *str2); void resetConsoleActions(void); void helpCommand(void); void initConsoleLogic(void); diff --git a/firmware/util/efilib.cpp b/firmware/util/efilib.cpp index e02d9c715c..b6ec343bee 100644 --- a/firmware/util/efilib.cpp +++ b/firmware/util/efilib.cpp @@ -180,3 +180,30 @@ float atoff(const char *param) { } return integerPart + decimal / divider; } + +#define TO_LOWER(x) (((x)>='A' && (x)<='Z') ? (x) - 'A' + 'a' : (x)) + +bool strEqualCaseInsensitive(const char *str1, const char *str2) { + int len1 = strlen(str1); + int len2 = strlen(str2); + if (len1 != len2) { + return false; + } + for (int i = 0; i < len1; i++) + if (TO_LOWER(str1[i]) != TO_LOWER(str2[i])) + return false; + return true; +} + +bool strEqual(const char *str1, const char *str2) { + // todo: there must be a standard function?! + int len1 = strlen(str1); + int len2 = strlen(str2); + if (len1 != len2) { + return false; + } + for (int i = 0; i < len1; i++) + if (str1[i] != str2[i]) + return false; + return true; +} diff --git a/firmware/util/efilib.h b/firmware/util/efilib.h index 271af7708c..034e4435ae 100644 --- a/firmware/util/efilib.h +++ b/firmware/util/efilib.h @@ -49,6 +49,9 @@ float maxF(float i1, float i2); char* itoa10(char *p, int num); bool isSameF(float v1, float v2); +bool strEqualCaseInsensitive(const char *str1, const char *str2); +bool strEqual(const char *str1, const char *str2); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/unit_tests/test_logic_expression.cpp b/unit_tests/test_logic_expression.cpp index 5ffdbb44ed..68427aa4f3 100644 --- a/unit_tests/test_logic_expression.cpp +++ b/unit_tests/test_logic_expression.cpp @@ -12,11 +12,13 @@ #include "logic_expression.h" #include "cli_registry.h" -void testLogicExpressions(void) { - printf("*************************************************** testLogicExpressions\r\n"); +static void testParsing(void) { char buffer[64]; + assertTrue(strEqualCaseInsensitive("hello", "HELlo")); + assertFalse(strEqualCaseInsensitive("hello", "HElo2")); + const char *ptr; ptr = processToken(" hello ", buffer); assertTrue(strEqual("hello", buffer)); @@ -29,6 +31,34 @@ void testLogicExpressions(void) { ptr = processToken(ptr, buffer); assertTrue(strEqual("world", buffer)); + assertTrue(isNumeric("123")); + assertFalse(isNumeric("a123")); + + LEElementPool pool; + + LEElement *element; + element = parseExpression(&pool, "1 3 AND"); + assertTrue(element != NULL); + + assertEquals(element->action, LE_NUMERIC_VALUE); + assertEquals(element->fValue, 1.0); + + element = element->next; + assertEquals(element->action, LE_NUMERIC_VALUE); + assertEquals(element->fValue, 3.0); + + element = element->next; + assertEquals(element->action, LE_OPERATOR_AND); + + element = element->next; + assertTrue(element == NULL); +} + +void testLogicExpressions(void) { + printf("*************************************************** testLogicExpressions\r\n"); + + testParsing(); + LECalculator c; LEElement value1;