Compare commits

10 Commits

Author SHA1 Message Date
a6b356d397 add printf
mpaland/printf <- github
2022-12-29 20:38:53 +01:00
a2401f45cc cbufer added 2022-12-03 20:52:05 +01:00
841e30913e fix minor compile problems 2022-12-03 20:21:08 +01:00
b1f7e448ab Merge branch 'oled/wip-main' 2022-12-03 20:06:53 +01:00
44e4ff3d24 Merge commit '179778d4acb8fd12acdd04f3d70f738a4a171fb9' 2022-12-03 20:02:00 +01:00
179778d4ac first working version 2022-12-03 20:00:11 +01:00
e67c12358f Configure test extension for vscode 2022-11-27 14:55:06 +01:00
363c9568a7 Configure workspace 2022-11-27 09:25:31 +01:00
c8dc4e7962 Merge branch 'master' into parser/wip-main 2022-11-27 08:12:51 +01:00
47f14990d5 apply changes from oled branch in cmake file 2022-11-27 08:07:06 +01:00
18 changed files with 8512 additions and 15 deletions

View File

@@ -5,9 +5,11 @@
"includePath": [
"${workspaceFolder}/utility/unity/core",
"${workspaceFolder}/utility/unity/fixture",
"${workspaceFolder}/utility/fff",
"${workspaceFolder}/test/cmd_parser/helpers/inc",
"${workspaceFolder}/cmd_parser",
"${workspaceFolder}/test/oled/helpers/inc",
"${workspaceFolder}/test/oled/",
"${workspaceFolder}/**"
"${workspaceFolder}/oled"
],
"defines": [
"_DEBUG",

28
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,28 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Unity Test Explorer Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${command:unityExplorer.debugTestExecutable}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/Apps/mingw64/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

25
.vscode/settings.json vendored
View File

@@ -1,14 +1,17 @@
{
"unityExplorer.testSourceFileRegex": "\\w+_test.c",
"unityExplorer.testCaseRegex": "void\\s+(test_.*)\\s*\\(.*\\)",
"unityExplorer.testExecutableRegex": "build/exe/$1.exe",
"unityExplorer.testSourceFolder": "test",
"unityExplorer.testBuildApplication": "ninja",
"unityExplorer.testBuildCwdPath": "build",
// "unityExplorer.unitUnderTestFolder": "cmd_parser",
"unityExplorer.unitUnderTestFileRegex": "\\w+\\.[ch]",
"unityExplorer.prettyTestCaseRegex": "test_(\\w+)",
"unityExplorer.prettyTestFileRegex": "(\\w+)_test\\.c",
"unityExplorer.debugConfiguration": "Unity Test Explorer Debug",
"files.associations": {
"climits": "c",
"cmath": "c",
"cstdarg": "c",
"cstdint": "c",
"cstdio": "c",
"cstdlib": "c",
"type_traits": "c",
"limits": "c",
"*.tcc": "c",
"typeinfo": "c"
}
"main.h": "c"
},
}

17
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,17 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "build",
"command": "C:/Apps/mingw64/bin/ninja.exe",
"options": {
"cwd": "${workspaceFolder}/build"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@@ -15,4 +15,5 @@ add_definitions(${GLOBAL_DEFINITION})
add_subdirectory(utility/unity unity)
link_libraries(unity)
add_subdirectory(test/oled oled)
add_subdirectory(test/oled oled)
add_subdirectory(test/cmd_parser cmd_parser)

62
cbuffer/circular_buffer.c Normal file
View File

@@ -0,0 +1,62 @@
#include <string.h>
#include "stdint.h"
#include "circular_buffer.h"
CB_StatusTypeDef cb_init(CB_TypeDef *cb, void *buffer, uint16_t capacity, uint8_t item_size)
{
if (buffer == NULL || cb == NULL)
{
return CB_ERROR;
}
cb->buffer = buffer;
cb->buffer_end = (uint8_t *)cb->buffer + capacity * item_size;
cb->capacity = capacity;
cb->count = 0;
cb->item_size = item_size;
cb->head = cb->buffer;
cb->tail = cb->buffer;
return CB_OK;
}
CB_StatusTypeDef cb_push_back(CB_TypeDef *cb, const void *item)
{
if (cb == NULL)
{
return CB_ERROR;
}
if (cb->count == cb->capacity)
{
return CB_FULL;
}
memcpy(cb->head, item, cb->item_size);
cb->head = (uint8_t *)cb->head + cb->item_size;
if (cb->head == cb->buffer_end)
cb->head = cb->buffer;
cb->count++;
return CB_OK;
}
CB_StatusTypeDef cb_pop_front(CB_TypeDef *cb, void *item)
{
if (cb == NULL)
{
return CB_ERROR;
}
if (cb->count == 0)
{
return CB_EMPTY;
}
memcpy(item, cb->tail, cb->item_size);
cb->tail = (uint8_t *)cb->tail + cb->item_size;
if (cb->tail == cb->buffer_end)
cb->tail = cb->buffer;
cb->count--;
return CB_OK;
}

37
cbuffer/circular_buffer.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef __CIRCULAT_BUFFER__
#define __CIRCULAT_BUFFER__
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
typedef enum
{
CB_OK = 0x00,
CB_ERROR = -0x01,
CB_EMPTY = -0x02,
CB_FULL = -0x03
} CB_StatusTypeDef;
typedef struct
{
void *buffer; // data buffer
void *buffer_end; // end of data buffer
uint16_t capacity; // maximum number of items in the buffer
uint16_t count; // number of items in the buffer
uint8_t item_size; // size of each item in the buffer
void *head; // pointer to head
void *tail; // pointer to tail
} CB_TypeDef;
CB_StatusTypeDef cb_init(CB_TypeDef *cb, void *buffer, uint16_t capacity, uint8_t item_size);
CB_StatusTypeDef cb_push_back(CB_TypeDef *cb, const void *item);
CB_StatusTypeDef cb_pop_front(CB_TypeDef *cb, void *item);
#ifdef __cplusplus
}
#endif
#endif /* __CIRCULAT_BUFFER__ */

89
cmd_parser/cmd_parser.c Normal file
View File

@@ -0,0 +1,89 @@
#include "string.h"
#include "stdint.h"
#include "cmd_parser.h"
static const cmd_t **cmd_array;
static uint8_t cmd_array_size;
void cmd_parser_init(const cmd_t *commands[], uint8_t size)
{
cmd_array = commands;
cmd_array_size = size;
}
void msg_parse(char *msg)
{
char *msgptr, *submsgptr;
parser_t parser = NULL;
result_t result = CMD_OK;
char key = 0;
char *token = strtok_r(msg, " ", &msgptr);
if (token[0] == '?')
{
// help message
}
for (uint8_t i = 0; i < cmd_array_size; i++)
{
if (strcmp(token, cmd_array[i]->name) == 0)
{
parser = cmd_array[i]->parser;
break;
}
}
if (parser == NULL)
{
return;
}
for (token = strtok_r(NULL, " ", &msgptr); token != NULL; token = strtok_r(NULL, " ", &msgptr))
{
if (token[0] == '-')
{
key = token[1];
result = parser(key, NULL);
if (strlen(token) > 2)
{
token = token + 2;
}
}
if (token[0] != '-')
{
switch (result)
{
case CMD_VALUE_REQ:
result = parser(key, token);
break;
case CMD_MULTIVAL_REQ:
for (char *subtoken = strtok_r(token, ", ", &submsgptr); subtoken != NULL; subtoken = strtok_r(NULL, ",", &submsgptr))
{
if (parser(key, subtoken) == CMD_UNKNOWN)
{
result = parser(CMDP_KEY_ARG, subtoken);
}
}
break;
default:
result = parser(CMDP_KEY_ARG, token);
break;
}
}
if (result == CMD_UNKNOWN)
{
break;
}
}
if (result != CMD_UNKNOWN)
{
parser(CMDP_KEY_DONE, NULL);
}
else
{
parser(CMDP_KEY_STOP, NULL);
}
}

24
cmd_parser/cmd_parser.h Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
#define CMDP_KEY_DONE 1
#define CMDP_KEY_ARG 2
#define CMDP_KEY_STOP 3
typedef enum
{
CMD_OK,
CMD_VALUE_REQ,
CMD_MULTIVAL_REQ,
CMD_UNKNOWN,
} result_t;
typedef result_t (*parser_t)(char key, char *arg);
typedef struct
{
char *name;
parser_t parser;
} cmd_t;
void cmd_parser_init(const cmd_t *commands[], uint8_t size);
void msg_parse(char *msg);

59
cmd_parser/cmd_parser2.h Normal file
View File

@@ -0,0 +1,59 @@
#pragma once
#include "main.h"
#define CMDP_KEY_ARG 1
typedef void (*help_filter)(char key, const char *text);
// key = opt_id, arg = value or NULL
// key = CMDP_KEY_ARG, arg = id_arg or token
typedef enum
{
CMD_NULL,
CMD_OK,
CMD_VALUE_REQ,
CMD_MULTIVAL_REQ,
CMD_UNKNOWN,
} result_t;
typedef result_t (*parser)(char key, char *arg);
typedef enum
{
NO_VALUE = 0,
SINGLE_VALE = 1,
MULTI_VALUE = 2,
NOT_OPTIONAL = 4,
} option_flags_t;
typedef const struct
{
uint8_t opt_id;
char *name;
char *description;
option_flags_t flags;
} option_t;
typedef const struct
{
char *name;
uint8_t arg_id;
char *description;
option_t *options;
uint8_t opt_num;
// parser arg_parser;
} argument_t;
typedef const struct
{
char *name;
char *description;
argument_t *arguments;
uint8_t arg_num;
option_t *options;
uint8_t opt_num;
parser cmd_parser;
} cmd_parser_t;
void cmd_parser_init(cmd_parser_t* commands[], uint8_t size);
// void cmd_parse(char *msg, parser parser);
void cmd_parse(char *msg);

1043
printf/printf.c Normal file

File diff suppressed because it is too large Load Diff

109
printf/printf.h Normal file
View File

@@ -0,0 +1,109 @@
///////////////////////////////////////////////////////////////////////////////
// \author (c) Marco Paland (info@paland.com)
// 2014-2019, PALANDesign Hannover, Germany
//
// \license The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on
// embedded systems with a very limited resources.
// Use this instead of bloated standard/newlib printf.
// These routines are thread safe and reentrant.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef _PRINTF_H_
#define _PRINTF_H_
#include <stdarg.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* Output a character to a custom device like UART, used by the printf() function
* This function is declared here only. You have to write your custom implementation somewhere
* \param character Character to output
*/
void _putchar(char character);
/**
* Tiny printf implementation
* You have to implement _putchar if you use printf()
* To avoid conflicts with the regular printf() API it is overridden by macro defines
* and internal underscore-appended functions like printf_() are used
* \param format A string that specifies the format of the output
* \return The number of characters that are written into the array, not counting the terminating null character
*/
#define printf printf_
int printf_(const char *format, ...);
/**
* Tiny sprintf implementation
* Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD!
* \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output!
* \param format A string that specifies the format of the output
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
*/
#define sprintf sprintf_
int sprintf_(char *buffer, const char *format, ...);
/**
* Tiny snprintf/vsnprintf implementation
* \param buffer A pointer to the buffer where to store the formatted string
* \param count The maximum number of characters to store in the buffer, including a terminating null character
* \param format A string that specifies the format of the output
* \param va A value identifying a variable arguments list
* \return The number of characters that COULD have been written into the buffer, not counting the terminating
* null character. A value equal or larger than count indicates truncation. Only when the returned value
* is non-negative and less than count, the string has been completely written.
*/
#define snprintf snprintf_
#define vsnprintf vsnprintf_
int snprintf_(char *buffer, size_t count, const char *format, ...);
int vsnprintf_(char *buffer, size_t count, const char *format, va_list va);
/**
* Tiny vprintf implementation
* \param format A string that specifies the format of the output
* \param va A value identifying a variable arguments list
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
*/
#define vprintf vprintf_
int vprintf_(const char *format, va_list va);
/**
* printf with output function
* You may use this as dynamic alternative to printf() with its fixed _putchar() output
* \param out An output function which takes one character and an argument pointer
* \param arg An argument pointer for user data passed to output function
* \param format A string that specifies the format of the output
* \return The number of characters that are sent to the output function, not counting the terminating null character
*/
int fctprintf(void (*out)(char character, void *arg), void *arg, const char *format, ...);
#ifdef __cplusplus
}
#endif
#endif // _PRINTF_H_

View File

@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.10)
set(TEST_NAME cmd_parser_test)
set(INCLUDE_DIRS
../../cmd_parser
helpers/inc
../../utility/fff
)
set(SRCS
../../cmd_parser/cmd_parser.c
# helpers/src/led_cmd.c
cmd_parser_test.c
)
add_definitions(-DTEST)
add_executable(${TEST_NAME} ${SRCS})
target_include_directories(${TEST_NAME} PUBLIC ${INCLUDE_DIRS})

View File

@@ -0,0 +1,283 @@
#include "unity.h"
#include "fff.h"
#include "cmd_parser.h"
DEFINE_FFF_GLOBALS;
FAKE_VALUE_FUNC(result_t, parse_callback, char, char *);
cmd_t led_cmd = {.name = "led", .parser = parse_callback};
void setUp(void)
{
RESET_FAKE(parse_callback);
FFF_RESET_HISTORY();
}
void tearDown(void)
{
}
void test_splittoken1(void)
{
char msg[] = "led";
parse_callback_fake.return_val = CMD_OK;
msg_parse(msg);
TEST_ASSERT_EQUAL(1, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken2(void)
{
char msg[] = "led set";
parse_callback_fake.return_val = CMD_OK;
msg_parse(msg);
TEST_ASSERT_EQUAL(2, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_ARG, parse_callback_fake.arg0_history[0]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("set", parse_callback_fake.arg1_history[0], 3);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken3(void)
{
char msg[] = "led set -p";
parse_callback_fake.return_val = CMD_OK;
msg_parse(msg);
TEST_ASSERT_EQUAL(3, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_CHAR('p', parse_callback_fake.arg0_history[1]);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_history[1]);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken4(void)
{
char msg[] = "led set -p 50";
result_t returnValue[] = {CMD_OK, CMD_VALUE_REQ, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(4, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_CHAR('p', parse_callback_fake.arg0_history[1]);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_history[1]);
TEST_ASSERT_EQUAL_CHAR('p', parse_callback_fake.arg0_history[2]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("50", parse_callback_fake.arg1_history[2], 2);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken5(void)
{
char msg[] = "led set -p 50 -l";
result_t returnValue[] = {CMD_OK, CMD_VALUE_REQ, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(5, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[3]);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_history[3]);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken6(void)
{
char msg[] = "led set -p 50 -l Atop, Btop, Ctop";
result_t returnValue[] = {CMD_OK, CMD_VALUE_REQ, CMD_OK, CMD_MULTIVAL_REQ, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(8, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[3]);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_history[3]);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[4]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Atop", parse_callback_fake.arg1_history[4], 4);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[5]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Btop", parse_callback_fake.arg1_history[5], 4);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[6]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Ctop", parse_callback_fake.arg1_history[6], 4);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken7(void)
{
char msg[] = "led set -p -l Atop, Btop, Ctop";
result_t returnValue[] = {CMD_OK, CMD_OK, CMD_MULTIVAL_REQ, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(7, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[2]);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_history[2]);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[3]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Atop", parse_callback_fake.arg1_history[3], 4);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[4]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Btop", parse_callback_fake.arg1_history[4], 4);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[5]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Ctop", parse_callback_fake.arg1_history[5], 4);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken8(void)
{
char msg[] = "led set -p -l Atop, Btop, Ctop test";
result_t returnValue[] = {CMD_OK, CMD_OK, CMD_MULTIVAL_REQ, CMD_OK, CMD_OK, CMD_OK, CMD_UNKNOWN, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(9, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_ARG, parse_callback_fake.arg0_history[7]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("test", parse_callback_fake.arg1_history[7], 4);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken9(void)
{
char msg[] = "led set -p -l Atop, Btop, Wrong test";
result_t returnValue[] = {CMD_OK, CMD_OK, CMD_MULTIVAL_REQ, CMD_OK, CMD_OK, CMD_UNKNOWN, CMD_UNKNOWN, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(8, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_STOP, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken10(void)
{
char msg[] = "led set -p50";
result_t returnValue[] = {CMD_OK, CMD_VALUE_REQ, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(4, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_CHAR('p', parse_callback_fake.arg0_history[1]);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_history[1]);
TEST_ASSERT_EQUAL_CHAR('p', parse_callback_fake.arg0_history[2]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("50", parse_callback_fake.arg1_history[2], 2);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken11(void)
{
char msg[] = "led set -p50 -l Atop,Btop,Ctop";
result_t returnValue[] = {CMD_OK, CMD_VALUE_REQ, CMD_OK, CMD_MULTIVAL_REQ, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(8, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[3]);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_history[3]);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[4]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Atop", parse_callback_fake.arg1_history[4], 4);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[5]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Btop", parse_callback_fake.arg1_history[5], 4);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[6]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Ctop", parse_callback_fake.arg1_history[6], 4);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken12(void)
{
char msg[] = "led set -p50 -lAtop,Btop,Ctop";
result_t returnValue[] = {CMD_OK, CMD_VALUE_REQ, CMD_OK, CMD_MULTIVAL_REQ, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(8, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[3]);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_history[3]);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[4]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Atop", parse_callback_fake.arg1_history[4], 4);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[5]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Btop", parse_callback_fake.arg1_history[5], 4);
TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[6]);
TEST_ASSERT_EQUAL_CHAR_ARRAY("Ctop", parse_callback_fake.arg1_history[6], 4);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_DONE, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken13(void)
{
char msg[] = "led wrong";
result_t returnValue[] = {CMD_UNKNOWN, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, 5);
msg_parse(msg);
TEST_ASSERT_EQUAL(2, parse_callback_fake.call_count);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_STOP, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken14(void)
{
char msg[] = "led wrong -p 50 -l Atop,Btop,Ctop";
result_t returnValue[] = {CMD_UNKNOWN, CMD_VALUE_REQ, CMD_OK, CMD_MULTIVAL_REQ, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(2, parse_callback_fake.call_count);
// TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[3]);
// TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_history[3]);
// TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[4]);
// TEST_ASSERT_EQUAL_CHAR_ARRAY("Atop", parse_callback_fake.arg1_history[4], 4);
// TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[5]);
// TEST_ASSERT_EQUAL_CHAR_ARRAY("Btop", parse_callback_fake.arg1_history[5], 4);
// TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[6]);
// TEST_ASSERT_EQUAL_CHAR_ARRAY("Ctop", parse_callback_fake.arg1_history[6], 4);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_STOP, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
void test_splittoken15(void)
{
char msg[] = "led set -X 50 -l Atop,Btop,Ctop";
result_t returnValue[] = {CMD_OK, CMD_UNKNOWN, CMD_OK, CMD_MULTIVAL_REQ, CMD_OK};
SET_RETURN_SEQ(parse_callback, returnValue, sizeof(returnValue) / sizeof(returnValue[0]));
msg_parse(msg);
TEST_ASSERT_EQUAL(3, parse_callback_fake.call_count);
// TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[3]);
// TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_history[3]);
// TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[4]);
// TEST_ASSERT_EQUAL_CHAR_ARRAY("Atop", parse_callback_fake.arg1_history[4], 4);
// TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[5]);
// TEST_ASSERT_EQUAL_CHAR_ARRAY("Btop", parse_callback_fake.arg1_history[5], 4);
// TEST_ASSERT_EQUAL_CHAR('l', parse_callback_fake.arg0_history[6]);
// TEST_ASSERT_EQUAL_CHAR_ARRAY("Ctop", parse_callback_fake.arg1_history[6], 4);
TEST_ASSERT_EQUAL_UINT8(CMDP_KEY_STOP, parse_callback_fake.arg0_val);
TEST_ASSERT_EQUAL_PTR(NULL, parse_callback_fake.arg1_val);
}
int main(void)
{
cmd_t led_cmd = {.name = "led", .parser = parse_callback};
const cmd_t *cmd_list[] = {&led_cmd};
cmd_parser_init(cmd_list, sizeof(cmd_list)/sizeof(cmd_list[0]) );
UNITY_BEGIN();
RUN_TEST(test_splittoken1);
RUN_TEST(test_splittoken2);
RUN_TEST(test_splittoken3);
RUN_TEST(test_splittoken4);
RUN_TEST(test_splittoken5);
RUN_TEST(test_splittoken6);
RUN_TEST(test_splittoken7);
RUN_TEST(test_splittoken8);
RUN_TEST(test_splittoken9);
RUN_TEST(test_splittoken10);
RUN_TEST(test_splittoken11);
RUN_TEST(test_splittoken12);
RUN_TEST(test_splittoken13);
RUN_TEST(test_splittoken14);
RUN_TEST(test_splittoken15);
return UNITY_END();
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include "main.h"
#include "cmd_parser.h"
/*
Usage: led set|get [-p PNUM | --power=PNUM] [-w CHOICES | --which=CHOICES] [-m COICES | --mode=CHOICES]
-p, --power set or get power 0-100
-w, --which set or get specific leds [Atop, Abot, Btop, Bbot, Ctop, Cbot, all]
-m, --mode set or get mode of operation [allways_on, scan]
*/
// cmd_parser_t led_cmd;
result_t parse_callback(char key, char *arg);

View File

@@ -0,0 +1,62 @@
#include "led_cmd.h"
enum {set, get};
option_t set_options[] = {
{
.opt_id = 'p',
.name = "power",
.flags = SINGLE_VALE,
.description = "sets the power of the light",
},
{
.opt_id = 'l',
.name = "led",
.flags = MULTI_VALUE,
.description = "diode selection. OPTIONS = [Atop, Abot, Btop, Bbot, Ctop, Cbot, all]",
}
};
option_t get_options[] = {
{
.opt_id = 'p',
.name = "power",
.flags = NO_VALUE,
.description = "gets the power of the light",
},
{
.opt_id = 'l',
.name = "led",
.flags = MULTI_VALUE,
.description = "diode selection. OPTIONS = [Atop, Abot, Btop, Bbot, Ctop, Cbot, all]",
}
};
argument_t arguments[] = {
{
.name = "set",
.arg_id = set,
.options = set_options,
.opt_num = sizeof(set_options) / (sizeof(set_options[0])),
.description = "allows to set operating parameters",
},
{
.name = "get",
.arg_id = get,
.options = get_options,
.opt_num = sizeof(get_options) / (sizeof(get_options[0])),
.description = "allows to get operating parameters",
},
};
cmd_parser_t led_cmd = {
.name = "led",
.arguments = arguments,
.arg_num = sizeof(arguments) / sizeof(arguments[0]),
.description = "allows to controll and check leds",
.cmd_parser = parse_callback,
};

View File

@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10)
set(TEST_NAME oled)
set(TEST_NAME oled_test)
set(INCLUDE_DIRS
../../oled

6643
utility/fff/fff.h Normal file

File diff suppressed because it is too large Load Diff