diff options
author | UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> | 2022-10-30 10:57:11 +0100 |
---|---|---|
committer | UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> | 2022-10-30 10:57:50 +0100 |
commit | 8952e19f00e0930e53e3731fd30be5e2600d9fd6 (patch) | |
tree | 2770bfd4319402c88b6872d1f8699252329c6603 /shared | |
parent | 6d9df6fe97502a852b68c8c8e6781d7208170cae (diff) | |
parent | 60f954dbaa399f8f0bcaeeb307953fc3b37b9dc7 (diff) |
finish merge
Diffstat (limited to 'shared')
-rw-r--r-- | shared/bin.c | 11 | ||||
-rw-r--r-- | shared/bin.h | 13 | ||||
-rw-r--r-- | shared/protocol.c | 147 | ||||
-rw-r--r-- | shared/protocol.h | 151 | ||||
-rw-r--r-- | shared/protocol.md | 57 | ||||
-rw-r--r-- | shared/shared.mk | 1 | ||||
-rw-r--r-- | shared/util.c | 7 | ||||
-rw-r--r-- | shared/util.h | 6 |
8 files changed, 393 insertions, 0 deletions
diff --git a/shared/bin.c b/shared/bin.c new file mode 100644 index 0000000..def2aa8 --- /dev/null +++ b/shared/bin.c @@ -0,0 +1,11 @@ +#include <stdlib.h> +#include <stdint.h> +#include <memory.h> + +#include "bin.h" + +ws_s_bin *ws_bin_s_alloc(uint16_t bytes) { + ws_s_bin *temp = malloc(sizeof(ws_s_bin) + sizeof(uint8_t) * bytes); + temp->bytes = bytes; + return temp; +} diff --git a/shared/bin.h b/shared/bin.h new file mode 100644 index 0000000..bfcda0c --- /dev/null +++ b/shared/bin.h @@ -0,0 +1,13 @@ +#pragma once + +#include <stdint.h> + +/** @brief binary data container with length */ +typedef struct { + uint16_t bytes; + uint8_t data[]; +} ws_s_bin; + +/** @brief allocate new ws_s_bin struct */ +ws_s_bin *ws_bin_s_alloc(uint16_t bytes); + diff --git a/shared/protocol.c b/shared/protocol.c new file mode 100644 index 0000000..8887070 --- /dev/null +++ b/shared/protocol.c @@ -0,0 +1,147 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "protocol.h" +#include "util.h" + +#define WS_CMD_MAP(parsed_cmd, name, code) \ + if (strlen(parsed_cmd->argv[0]) == strlen(name) && strncmp(parsed_cmd->argv[0], name, strlen(name)) == 0) return code; + +static ws_e_protocol_cmd ws_protocol_get_req_cmd_code(ws_s_protocol_parsed_req_cmd* parsed_cmd) { + if (parsed_cmd == NULL) return WS_PROTOCOL_CMD_UNKNOWN; // invalid command + WS_CMD_MAP(parsed_cmd, "last-records", WS_PROTOCOL_CMD_LAST_RECORDS); + + return WS_PROTOCOL_CMD_UNKNOWN; +} + +void ws_protocol_parse_req_byte(ws_s_protocol_req_parser_state* state, char input) { + switch(input) { + case WS_PROTOCOL_C_EOL: { + break; + } + + case WS_PROTOCOL_C_SPACE: { + if (!state->valid) return; + state->arg_len++; + return; + } + + case WS_PROTOCOL_C_NULL: { + state->valid = false; + return; + } + + default: { + if (!state->valid) return; + state->cmd[state->cmd_len++] = input; + state->args_len[state->arg_len] += 1; + if (state->cmd_len == WS_PROTOCOL_CMD_BUFFER_LEN) state->valid = false; + return; + } + } + // arg_len is used as an index while parsing, so add 1 to get length + state->arg_len++; + + // parse cmd into argc and argv + if (state->valid) ws_protocol_req_cmd_init(state); + // create response + ws_s_protocol_res* response = ws_protocol_parse_req_finished(state->target); + + // send response + char response_first_line[16]; + sprintf(response_first_line, "%s,%x\n", response->success == WS_PROTOCOL_CMD_RETURN_OK ? "ok" : "error", response->msg->bytes); + ws_protocol_send_data(response_first_line, strlen(response_first_line)); + if (!response->csh) ws_protocol_send_data((char*) response->msg->data, response->msg->bytes); + else (*g_ws_protocol_res_handlers[response->cmd_code])(state->target, response, true); + + // free response data containers + free(response->msg); + free(response); + + // reset parser + ws_protocol_req_parser_reset(state); + + return; +} + +ws_s_protocol_res* ws_protocol_parse_req_finished(ws_s_protocol_parsed_req_cmd* parsed_cmd) { + ws_s_protocol_res* response = malloc(sizeof(ws_s_protocol_res)); + response->success = WS_PROTOCOL_CMD_RETURN_ERROR; + response->csh = false; + response->msg = NULL; + response->cmd_code = ws_protocol_get_req_cmd_code(parsed_cmd); + + if (response->cmd_code == WS_PROTOCOL_CMD_UNKNOWN) goto ws_protocol_parse_exit; + if (response->cmd_code >= WS_PROTOCOL_CMD_AMOUNT) goto ws_protocol_parse_exit; + + ws_protocol_res_handler_t* ws_protocol_res_handler = g_ws_protocol_res_handlers[response->cmd_code]; + if (ws_protocol_res_handler == NULL) goto ws_protocol_parse_exit; + (*ws_protocol_res_handler)(parsed_cmd, response, false); + +ws_protocol_parse_exit: + + if (response->msg == NULL) response->msg = ws_bin_s_alloc(0); + return response; +} + +void ws_protocol_parse_req_bytes(ws_s_protocol_req_parser_state* state, char* input, unsigned int length) { + for (unsigned int i = 0; i < length; i++) ws_protocol_parse_req_byte(state, input[i]); +} + +ws_s_protocol_req_parser_state* ws_protocol_req_parser_alloc() { + ws_s_protocol_req_parser_state* parser_state = malloc(sizeof(ws_s_protocol_req_parser_state) + sizeof(uint16_t) * WS_PROTOCOL_CMD_MAX_ARGUMENTS); + parser_state->cmd = malloc(sizeof(char) * WS_PROTOCOL_CMD_BUFFER_LEN); + parser_state->target = NULL; + ws_protocol_req_parser_reset(parser_state); + return parser_state; +} + +void ws_protocol_req_cmd_init(ws_s_protocol_req_parser_state* state) { + state->target = malloc(sizeof(ws_s_protocol_parsed_req_cmd) + sizeof(char*) * state->arg_len); + unsigned int args = WS_MIN(state->arg_len, WS_PROTOCOL_CMD_MAX_ARGUMENTS); + for (unsigned int i = 0; i < args; i++) + state->target->argv[i] = malloc(sizeof(char) * (state->args_len[i] + 1)); + + state->target->argc = args; + + unsigned int head = 0; + for (unsigned int i = 0; i < state->arg_len; i++) { + strncpy(state->target->argv[i], &state->cmd[head], state->args_len[i]); + state->target->argv[i][state->args_len[i]] = 0x00; // terminate argument with null byte + head += state->args_len[i]; + } +} + +void ws_protocol_req_parser_free(ws_s_protocol_req_parser_state* state) { + if (state == NULL) return; + if (state->target != NULL) ws_protocol_req_cmd_free(state->target); + state->target = NULL; + free(state->cmd); + free(state); + return; +} + +void ws_protocol_req_parser_reset(ws_s_protocol_req_parser_state* state) { + if (state->target != NULL) ws_protocol_req_cmd_free(state->target); + state->target = NULL; + state->valid = true; + state->cmd_len = 0; + state->arg_len = 0; + memset(state->args_len, 0, sizeof(uint16_t) * WS_PROTOCOL_CMD_MAX_ARGUMENTS); +} + +void ws_protocol_req_cmd_free(ws_s_protocol_parsed_req_cmd* cmd) { + for (int i = 0; i < cmd->argc; i++) + free(cmd->argv[i]); + free(cmd); + return; +} + +unsigned short ws_protocol_get_header_size(ws_s_protocol_res* response) { + unsigned short size = 2; // comma and trailing newline + if (response->success == WS_PROTOCOL_CMD_RETURN_OK) size += 2; // ok + if (response->success == WS_PROTOCOL_CMD_RETURN_ERROR) size += 5; // error + size += ws_log16(response->msg->bytes) + 1; // amount of characters for message size (hex) + return size; +}
\ No newline at end of file diff --git a/shared/protocol.h b/shared/protocol.h new file mode 100644 index 0000000..96c039a --- /dev/null +++ b/shared/protocol.h @@ -0,0 +1,151 @@ +#pragma once + +#include <stdint.h> +#include <stdbool.h> + +#include "bin.h" + +#define WS_PROTOCOL_CMD_MAX_ARGUMENTS (3) +#define WS_PROTOCOL_CMD_BUFFER_LEN (40) + +#define WS_PROTOCOL_CMD_AMOUNT (1) + +#define WS_PROTOCOL_C_EOL (0x0a) +#define WS_PROTOCOL_C_SPACE (0x20) +#define WS_PROTOCOL_C_NULL (0x00) + +/** + * @brief parsed request cmd struct, holds arguments similar to argc and argv + * provided to `int main()` + */ +typedef struct { + int argc; /** argument count */ + char* argv[]; /** argument array, null terminated strings */ +} ws_s_protocol_parsed_req_cmd; + +/** + * @brief holds parser state variables for `ws_protocol_parse_req_byte` function. + * each incoming tcp request should get it's own parser 'instance' + */ +typedef struct { + ws_s_protocol_parsed_req_cmd* target; /** parsed cmd reference */ + bool valid; /** command still valid flag */ + char* cmd; /** raw cmd */ + uint16_t cmd_len; /** raw cmd string length */ + uint16_t arg_len; /** amount of arguments */ + uint16_t args_len[]; /** array of argument lengths */ +} ws_s_protocol_req_parser_state; + +/** @brief return values for command handlers */ +typedef enum { + WS_PROTOCOL_CMD_RETURN_OK = 0, + WS_PROTOCOL_CMD_RETURN_ERROR = 1, +} ws_e_protocol_cmd_return_value; + +/** @brief cmd codes (used to call handlers) */ +typedef enum { + WS_PROTOCOL_CMD_UNKNOWN = -1, + + WS_PROTOCOL_CMD_LAST_RECORDS = 0, +} ws_e_protocol_cmd; + +/** @brief request response data struct */ +typedef struct { + ws_e_protocol_cmd_return_value success; /** status code for response + validity, defaults to + WS_PROTOCOL_CMD_RETURN_ERROR */ + bool csh; /** whether the response handler has logic for a custom send + handler, false by default */ + ws_s_bin* msg; /** pointer to response data, uninitialized by default */ + ws_e_protocol_cmd cmd_code; /** cmd code */ +} ws_s_protocol_res; + +/** + * @brief allocate parser struct + * + * @return pointer to newly allocated struct + */ +ws_s_protocol_req_parser_state* ws_protocol_req_parser_alloc(); +/** @brief deallocate parser struct, automatically frees all child pointers */ +void ws_protocol_req_parser_free(ws_s_protocol_req_parser_state* state); +/** @brief reset parser state to parse a new request */ +void ws_protocol_req_parser_reset(ws_s_protocol_req_parser_state* state); +/** + * @brief initialize ws_s_protocol_parsed_req_cmd struct pointer of + * ws_s_protocol_req_parser_state (internal only) + */ +void ws_protocol_req_cmd_init(ws_s_protocol_req_parser_state* state); +/** @brief deallocate ws_s_protocol_parsed_req_cmd struct pointer (internal only) */ +void ws_protocol_req_cmd_free(ws_s_protocol_parsed_req_cmd* cmd); + +/** + * @brief parse incoming data byte by byte until a finished command is detected + * + * @param state parser state object, each incoming request should have it's own parser state + * @param input input byte + */ +void ws_protocol_parse_req_byte(ws_s_protocol_req_parser_state* state, char input); +/** + * @brief parse incoming data chunk + * + * @param state parser state object, each incoming request should have it's own parser state + * @param input input byte array + * @param length input byte array length + */ +void ws_protocol_parse_req_bytes(ws_s_protocol_req_parser_state* state, char* input, unsigned int length); +/** + * @brief handle complete command + * + * this function gets called when ws_protocol_parse_req_byte(s) has detected a + * finished command. this function decides which command handler gets called, + * given that argv[0] contains a valid command. command argument parsing is + * handled by the command handler function. + * + * @return response + * + * @param parsed_cmd cmd parsed into ws_s_protocol_parsed_req_cmd struct + */ +ws_s_protocol_res* ws_protocol_parse_req_finished(ws_s_protocol_parsed_req_cmd* parsed_cmd); + +/** + * @brief create a `last-records` request command + * @return ws_s_bin containing the command string + */ +ws_s_bin* ws_protocol_req_last_records(unsigned int record_amount); + +/** + * @brief response handler + * + * gets fired when the weather station receives a complete command, and returns + * a response struct with a success code and an optional message. if + * response->csh is set to `true` within the handler, it gets fired a second + * time after the response header is sent, but with the `send` parameter set to + * `true`. this is so response handlers can send large amounts of data without + * allocating large areas of memory. + * + * @param parsed_cmd complete parsed command from ws_protocol_parse_req_* + * @param response response struct with uninitialized pointer to msg + * @param send `false` on first run, `true` on second run if `response->csh` was set to true + */ +typedef void ws_protocol_res_handler_t(ws_s_protocol_parsed_req_cmd*, ws_s_protocol_res*, bool); + +ws_protocol_res_handler_t ws_protocol_res_last_records; + +/** + * @brief data sender wrapper + * + * this function should be implemented in the source files of each target + * platform, as the send interface will be different on desktop and on the + * stm32. + * + * @param data pointer to data char array + * @param length length of data array + */ +void ws_protocol_send_data(const char* data, unsigned int length); + +/** @brief response handlers, called when a command is parsed */ +static ws_protocol_res_handler_t* g_ws_protocol_res_handlers[WS_PROTOCOL_CMD_AMOUNT] = { + [WS_PROTOCOL_CMD_LAST_RECORDS] = &ws_protocol_res_last_records, +}; + +unsigned short ws_protocol_get_header_size(ws_s_protocol_res* response); diff --git a/shared/protocol.md b/shared/protocol.md new file mode 100644 index 0000000..1e52e42 --- /dev/null +++ b/shared/protocol.md @@ -0,0 +1,57 @@ +# Protocol spec + +This is a brief overview of the protocol specifications that the weather +station uses to send and receive data between the weather station and qt +client. This protocol is text-based, and used over a TCP connection. This +document will only go into detail about the data sent over this connection, not +requirements about the connection itself. + +The protocol is only used in a request-response fashion, so all commands are +assumed to be sent by the qt client, and responded to by the weather station. + +~Functions for generating commands and parsing incoming data are provided by +the protocol.c and protocol.h files in this folder.~ A server using these files +should implement every protocol handler function in a seperate c file, along +with a data sending function that is also used internally. + +- LF for newline instead of CRLF +- Commands are single-line +- Spaces used for separating command arguments +- Commands with malformed data are discarded and return error +- Response consist of `ok` or `error`, a comma, and the byte length of the + remaining response (if any) +- Numbers are sent as hexadecimal + +## Commands + +### `last-records <n> <o>` + +Returns the last `n` records with offset `<o>` in csv format. The first line +has the csv table header, with the fields `id`, `temperature`, `humidity`, and +`atmospheric_pressure`. The rest of the response consists of 1 record per line. +The amount of records is limited to the amount of valid records in the backlog +buffer. When the amount of returned records is 0, the response consists of the +csv header, but without any following records. + +Offset `<o>` is a positive integer, representing the starting point for the +most recent record that is returned, this will get subtracted from the id of +the most recent record. E.g. if the last record has id `00f0`, and a request is +sent with parameters `n=3` and `o=5`, the records with id's `00eb`, `00ea`, and +`00e9` will be returned. + +## Example transaction + +In the following example, newlines are indicated by `<0a>`, request by lines +starting with `<`, and response by lines starting with `>`. + +``` +< last-records 5 0<0a> +> ok,73<0a> +> id,temperature,humidity,atmospheric_pressure<0a> +> 10dc,2f,c5,7f<0a> +> 10dd,30,c6,7f<0a> +> 10de,31,c7,7f<0a> +> 10df,35,ca,7e<0a> +> 10e0,34,c9,7e<0a> +``` + diff --git a/shared/shared.mk b/shared/shared.mk new file mode 100644 index 0000000..f9586ff --- /dev/null +++ b/shared/shared.mk @@ -0,0 +1 @@ +OBJS += $(patsubst %.c,%-stm.o, $(wildcard ../shared/*.c)) diff --git a/shared/util.c b/shared/util.c new file mode 100644 index 0000000..ea972b0 --- /dev/null +++ b/shared/util.c @@ -0,0 +1,7 @@ +#include "util.h" + +unsigned int ws_log16(unsigned int x) { + unsigned int l = 0; + while (x >>= 4) ++l; // bitshift right by 4 until x == 0 + return l; +}
\ No newline at end of file diff --git a/shared/util.h b/shared/util.h new file mode 100644 index 0000000..94a3dfe --- /dev/null +++ b/shared/util.h @@ -0,0 +1,6 @@ +#pragma once + +#define WS_MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define WS_MAX(a, b) (((a) > (b)) ? (a) : (b)) + +unsigned int ws_log16(unsigned int x); |