From 74c9202e2a2347620cfe0459a2de060eebd7051b Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 11 May 2022 13:22:59 +0200 Subject: [wip] error handling module --- robot/consts.h | 3 ++- robot/errcatch.c | 29 ++++++++++++++++++++++++++++- robot/errcatch.h | 19 +++++++++++++++++++ robot/readme.md | 16 ++++++++-------- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/robot/consts.h b/robot/consts.h index 1195422..fb67f26 100644 --- a/robot/consts.h +++ b/robot/consts.h @@ -1,12 +1,13 @@ #pragma once #ifndef W2_BUILD_STR -// should be defined with -DBUILD_STR in makefile +// is defined by CFLAGS += -DW2_BUILD_STR in makefile #define W2_BUILD_STR ("????????") #endif #define W2_MAX_MODULE_CYCLE_MS (20) #define W2_SERIAL_BAUD (9600) +#define W2_ERROR_BUFFER_SIZE (16) #define W2_ERR_TYPE_CRIT (0b00 << 6) #define W2_ERR_TYPE_WARN (0b01 << 6) diff --git a/robot/errcatch.c b/robot/errcatch.c index 5b42bb4..a70a409 100644 --- a/robot/errcatch.c +++ b/robot/errcatch.c @@ -1,7 +1,34 @@ +#include +#include + #include "errcatch.h" +w2_s_error g_w2_error_buffer[W2_ERROR_BUFFER_SIZE] = {}; +uint8_t g_w2_error_index = 0; +uint8_t g_w2_error_offset = 0; + void w2_errcatch_main() {} -void w2_errcatch_throw_msg(enum w2_e_errorcodes code, uint16_t length, const char *message) {} + +/** + * allocate and initialize error struct + * + * TODO: doesn't handle null pointers from calloc + */ +inline w2_s_error* w2_alloc_error(enum w2_e_errorcodes code, uint16_t length, const char *message) { + w2_s_error* error = calloc(sizeof(w2_s_error) + length, 1); + + memcpy(error, &(w2_s_error const){ .code = code, .message_length = length }, sizeof(w2_s_error)); + + // strncpy(error->message, message, length); + + return error; +} + +void w2_errcatch_throw_msg(enum w2_e_errorcodes code, uint16_t length, const char *message) { + w2_s_error error = *w2_alloc_error(code, length, message); + g_w2_error_buffer[g_w2_error_index] = error; + g_w2_error_index = (g_w2_error_index) & W2_ERROR_BUFFER_SIZE; +} void w2_errcatch_throw(enum w2_e_errorcodes code) { w2_errcatch_throw_msg(code, 0, ""); } diff --git a/robot/errcatch.h b/robot/errcatch.h index 48e2a75..dcea672 100644 --- a/robot/errcatch.h +++ b/robot/errcatch.h @@ -12,3 +12,22 @@ void w2_errcatch_throw(enum w2_e_errorcodes code); /** append error to error buffer (with debug message) */ void w2_errcatch_throw_msg(enum w2_e_errorcodes code, uint16_t length, const char *message); + +/** + * error struct + * + * holds an error with type `code`, and an optional `message` with length + * `message_length` + */ +typedef struct { + enum w2_e_errorcodes code; + uint8_t message_length; + uint8_t message[]; +} w2_s_error; + +/** error ring buffer */ +extern w2_s_error g_w2_error_buffer[W2_ERROR_BUFFER_SIZE]; +/** stores head of ring buffer */ +extern uint8_t g_w2_error_index; +/** stores start of ring buffer */ +extern uint8_t g_w2_error_offset; diff --git a/robot/readme.md b/robot/readme.md index bb88e80..1ffdb65 100644 --- a/robot/readme.md +++ b/robot/readme.md @@ -48,12 +48,12 @@ what they're supposed to do: |module |internal name|author|purpose| |----------------|-------------|-|-| |hypervisor |`hypervisor `|N/a| backbone of all other modules; stores global variables; controls when other modules run| -|pc communication|`sercomm `|Jorn & Abdullaahi| reads and parses incoming serial data; sends all data in the message buffer| +|pc communication|`sercomm `|Fiona| reads and parses incoming serial data; sends all data in the message buffer| |error handling |`errcatch `|Loek| receives error codes; controls how errors are handled| -|i/o read & write|`io `|Fiona| reads all inputs to global state; writes all outputs| +|i/o read & write|`io `|Jorn & Abdullaahi| reads all inputs to global state; writes all outputs| |mode logic |`modes `|N/a| executes the appropriate module for current mode| -|maze |`mode_maze `|TBD| controls robot during maze portion of map; hands off control to warehouse module| -|warehouse |`mode_grid `|TDB| controls robot during warehouse portion of map; hands off control to maze module| +|maze |`mode_maze `|Jorn & Abdullaahi| controls robot during maze portion of map; hands off control to warehouse module| +|warehouse |`mode_grid `|Loek| controls robot during warehouse portion of map; hands off control to maze module| |emergency stop |`mode_halt `|Fiona| stops all execution until emergency mode is reset by software or user| |calibration |`mode_calb `|Fiona| find line by turning on own axis if lost| @@ -120,10 +120,10 @@ to act on accordingly. the error handling module (a) provides functions for other modules to report errors, and (b) handles errors accordingly. -- [ ] create an error `struct` that holds: - - [ ] error code - - [ ] message length - - [ ] message contents +- [x] create an error `struct` that holds: + - [x] error code + - [x] message length + - [x] message contents - [ ] create a global error ring buffer with an appropriate size that holds error messages - [ ] handle errors in the error buffer, referencing the functional -- cgit v1.2.3