diff options
Diffstat (limited to 'shared')
-rw-r--r-- | shared/protocol.c | 14 | ||||
-rw-r--r-- | shared/protocol.h | 3 | ||||
-rw-r--r-- | shared/util.c | 7 | ||||
-rw-r--r-- | shared/util.h | 2 |
4 files changed, 21 insertions, 5 deletions
diff --git a/shared/protocol.c b/shared/protocol.c index ca0edc4..fcc84a4 100644 --- a/shared/protocol.c +++ b/shared/protocol.c @@ -3,6 +3,7 @@ #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; @@ -99,10 +100,7 @@ ws_s_protocol_req_parser_state* ws_protocol_req_parser_alloc() { 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); for (unsigned int i = 0; i < state->arg_len; i++) - state->target->argv[i] = malloc(sizeof(char) * (state->args_len[i] + 1)); // segfaults on 2nd run? - // state->target->argv stays empty according to debugger - // goes to HardFault_Handler on 2nd iteration - // this might be the stm running out of heap memory though i'm not sure + state->target->argv[i] = malloc(sizeof(char) * (state->args_len[i] + 1)); state->target->argc = state->arg_len; @@ -138,3 +136,11 @@ void ws_protocol_req_cmd_free(ws_s_protocol_parsed_req_cmd* cmd) { 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); // 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 index fbe29d6..5bcb114 100644 --- a/shared/protocol.h +++ b/shared/protocol.h @@ -5,7 +5,7 @@ #include "bin.h" -#define WS_PROTOCOL_CMD_MAX_ARGUMENTS (1) +#define WS_PROTOCOL_CMD_MAX_ARGUMENTS (2) #define WS_PROTOCOL_CMD_BUFFER_LEN (40) #define WS_PROTOCOL_CMD_AMOUNT (1) @@ -148,3 +148,4 @@ static ws_protocol_res_handler_t* g_ws_protocol_res_handlers[WS_PROTOCOL_CMD_AMO [WS_PROTOCOL_CMD_LAST_RECORDS] = &ws_protocol_res_last_records, }; +unsigned short ws_protocol_get_header_size(ws_s_protocol_res* response);
\ No newline at end of file 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 index f39ff34..94a3dfe 100644 --- a/shared/util.h +++ b/shared/util.h @@ -2,3 +2,5 @@ #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); |