aboutsummaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-10-29 18:01:56 +0200
committerlonkaars <loek@pipeframe.xyz>2022-10-29 18:01:56 +0200
commit9e9ea9d07ed2a176d6f6e83b5b100da22d8fca50 (patch)
treea683bde4664dad75c3b5a1efbd4223a4ab3b40dd /shared
parentead710db271795380207141f0add7278ba12ada0 (diff)
parentaae57dc32a843351fb2e17721afcd841bedec0a6 (diff)
Merge branch 'protocol' into dev
Diffstat (limited to 'shared')
-rw-r--r--shared/protocol.c14
-rw-r--r--shared/protocol.h3
-rw-r--r--shared/util.c7
-rw-r--r--shared/util.h2
4 files changed, 23 insertions, 3 deletions
diff --git a/shared/protocol.c b/shared/protocol.c
index c6e5ddd..8887070 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;
@@ -98,10 +99,11 @@ 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++)
+ 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 = state->arg_len;
+ state->target->argc = args;
unsigned int head = 0;
for (unsigned int i = 0; i < state->arg_len; i++) {
@@ -135,3 +137,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) + 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
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);