aboutsummaryrefslogtreecommitdiff
path: root/shared/serial_parse.c
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-01-07 14:43:03 +0100
committerlonkaars <loek@pipeframe.xyz>2023-01-07 14:43:03 +0100
commitadb70d5ee1987cfb1680114d2db9786923aad1f1 (patch)
treea15de82808f40153cd285994cab7534ed9a10538 /shared/serial_parse.c
parent46ede2c109fe2aa390936ec4f0a30321fb7b5d86 (diff)
parentb083c8e08d610a4e74f39f66f3daa191301893b1 (diff)
Merge branch 'qt-gui-meshconnector-serial' into dev
Diffstat (limited to 'shared/serial_parse.c')
-rw-r--r--shared/serial_parse.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/shared/serial_parse.c b/shared/serial_parse.c
new file mode 100644
index 0000000..bfc374a
--- /dev/null
+++ b/shared/serial_parse.c
@@ -0,0 +1,61 @@
+#include <string.h>
+
+#include "consts.h"
+#include "serial_parse.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+bool cd_serial_parse(uint8_t byte) {
+ static uint8_t current_message[CD_SERIAL_READ_BUFFER_SIZE] = {0};
+ static uint8_t current_message_index = 0;
+ static uint8_t complete_message_length = 2;
+
+ static bool attentive = false;
+ static bool listening = false;
+
+ if (byte == CD_SERIAL_START_BYTE) {
+ attentive = !attentive;
+ if (attentive && listening) return CD_SERIAL_READ_SUCCESS;
+ } else if (attentive) {
+ attentive = false;
+ listening = !listening;
+ if (!listening) return CD_SERIAL_READ_FAILURE;
+ }
+
+ if (!listening) return CD_SERIAL_READ_SUCCESS;
+ current_message[current_message_index++] = byte;
+
+ complete_message_length = cd_cmd_sizeof(current_message, current_message_index);
+
+ if (current_message_index == complete_message_length) {
+ cd_cmd_handle(current_message, current_message_index);
+
+ memset(&current_message, 0, CD_SERIAL_READ_BUFFER_SIZE);
+ current_message_index = 0;
+ complete_message_length = 1;
+ attentive = false;
+ listening = false;
+ return CD_SERIAL_READ_SUCCESS;
+ }
+
+ return CD_SERIAL_READ_SUCCESS;
+}
+
+void cd_cmd_handle(uint8_t data[CD_SERIAL_READ_BUFFER_SIZE], uint8_t data_length) {
+ cd_s_bin *copy = cd_bin_s_alloc(data_length, data);
+
+ if (data[0] >= CD_CMD_COUNT) return;
+ cd_cmd_handler_t* handler = CD_CMD_HANDLERS[data[0]];
+
+ if (handler == NULL) return;
+ (*handler)(copy);
+
+ free(copy);
+}
+
+#ifdef __cplusplus
+}
+#endif
+