aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-10-14 16:47:12 +0200
committerlonkaars <loek@pipeframe.xyz>2022-10-14 16:47:12 +0200
commitec475b0469104a0a76542c52286ed08933ac750c (patch)
treebeb60a88481e3e9b9e94670383850eb90e7c7abe
parentf58ed35f0b73e933b6dd1f98472941c1d5d7ce9c (diff)
more protocol stuff working
-rw-r--r--shared/protocol.c9
-rw-r--r--shared/protocol.h9
-rw-r--r--shared/test.c16
3 files changed, 33 insertions, 1 deletions
diff --git a/shared/protocol.c b/shared/protocol.c
index 6e3bc73..bcb764e 100644
--- a/shared/protocol.c
+++ b/shared/protocol.c
@@ -39,10 +39,19 @@ void ws_protocol_parse_byte(ws_s_protocol_parser_state* state, char input) {
ws_s_protocol_response* response = ws_protocol_parse_finished(state->target);
//TODO: 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_s_bin* response_first_line_bin = ws_bin_s_alloc(strlen(response_first_line));
+ strncpy((char*) response_first_line_bin->data, response_first_line, strlen(response_first_line));
+ ws_protocol_send_data(response_first_line_bin);
+ ws_protocol_send_data(response->msg);
+ free(response_first_line_bin);
free(response->msg);
free(response);
+ //TODO: reset command in parser_state for next command
+
return;
}
diff --git a/shared/protocol.h b/shared/protocol.h
index a19fe48..74b289c 100644
--- a/shared/protocol.h
+++ b/shared/protocol.h
@@ -119,6 +119,15 @@ ws_s_bin* ws_protocol_req_last_records(unsigned int record_amount);
*/
void ws_protocol_res_last_records(ws_s_protocol_parsed_cmd* parsed_cmd, ws_s_protocol_response* response);
+/**
+ * @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.
+ */
+void ws_protocol_send_data(ws_s_bin* data);
+
/** @brief cmd codes (used to call handlers) */
typedef enum {
WS_PROTOCOL_CMD_UNKNOWN = -1,
diff --git a/shared/test.c b/shared/test.c
index 5f2cd60..2c3c3a3 100644
--- a/shared/test.c
+++ b/shared/test.c
@@ -3,11 +3,25 @@
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
+#include <string.h>
#include "protocol.h"
void ws_protocol_res_last_records(ws_s_protocol_parsed_cmd* parsed_cmd, ws_s_protocol_response* response) {
- printf("last-records detected!\n");
+ const char* response_text = ""
+ "id,temperature,humidity,atmospheric_pressure\n"
+ "10dc,2f,c5,7f\n"
+ "10dd,30,c6,7f\n"
+ "10de,31,c7,7f\n"
+ "10df,35,ca,7e\n"
+ "10e0,34,c9,7e\n";
+ response->success = WS_PROTOCOL_CMD_RETURN_OK;
+ response->msg = ws_bin_s_alloc(strlen(response_text));
+ strncpy((char*) response->msg->data, response_text, strlen(response_text));
+}
+
+void ws_protocol_send_data(ws_s_bin* data) {
+ printf("%.*s", data->bytes, data->data);
}
int main() {