aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-10-29 18:26:16 +0200
committerlonkaars <loek@pipeframe.xyz>2022-10-29 18:26:16 +0200
commit7022c6fbdbdee82339fbe161df169d97235471f0 (patch)
tree8505d6afc4a0985c66b6e44653088d592fdcf493
parentaae57dc32a843351fb2e17721afcd841bedec0a6 (diff)
update protocol to use hex digits in command arguments and accept offset value in `last-records`
-rw-r--r--shared/protocol.h4
-rw-r--r--shared/protocol.md26
-rw-r--r--stm32f091/protocol.c11
3 files changed, 23 insertions, 18 deletions
diff --git a/shared/protocol.h b/shared/protocol.h
index 5bcb114..96c039a 100644
--- a/shared/protocol.h
+++ b/shared/protocol.h
@@ -5,7 +5,7 @@
#include "bin.h"
-#define WS_PROTOCOL_CMD_MAX_ARGUMENTS (2)
+#define WS_PROTOCOL_CMD_MAX_ARGUMENTS (3)
#define WS_PROTOCOL_CMD_BUFFER_LEN (40)
#define WS_PROTOCOL_CMD_AMOUNT (1)
@@ -148,4 +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
+unsigned short ws_protocol_get_header_size(ws_s_protocol_res* response);
diff --git a/shared/protocol.md b/shared/protocol.md
index b6e955c..1e52e42 100644
--- a/shared/protocol.md
+++ b/shared/protocol.md
@@ -9,10 +9,10 @@ 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. See [code
-implementation](#code-implementation) section for more details about naming
-conventions.
+~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
@@ -24,22 +24,28 @@ conventions.
## Commands
-### `last-records <n>`
+### `last-records <n> <o>`
-Returns the last `n` records in csv format. The first line has the csv table
-header, with the fields `id`, `temperature`, `humidity`, and
+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<0a>
+< last-records 5 0<0a>
> ok,73<0a>
> id,temperature,humidity,atmospheric_pressure<0a>
> 10dc,2f,c5,7f<0a>
@@ -49,7 +55,3 @@ starting with `<`, and response by lines starting with `>`.
> 10e0,34,c9,7e<0a>
```
-## Code implementation
-
-
-
diff --git a/stm32f091/protocol.c b/stm32f091/protocol.c
index ca46b3b..f4be08f 100644
--- a/stm32f091/protocol.c
+++ b/stm32f091/protocol.c
@@ -7,7 +7,8 @@
#include "esp8266.h"
void ws_protocol_res_last_records(ws_s_protocol_parsed_req_cmd* parsed_cmd, ws_s_protocol_res* response, bool send) {
- static unsigned int record_amount = 0;
+ static int record_amount = 0;
+ static unsigned int record_offset = 0;
const char* response_header = "id,temperature,humidity,atmospheric_pressure\n";
const unsigned int response_line_len = strlen("xxxx,xx,xx,xx\n");
@@ -16,9 +17,11 @@ void ws_protocol_res_last_records(ws_s_protocol_parsed_req_cmd* parsed_cmd, ws_s
response->csh = true;
response->msg = ws_bin_s_alloc(0);
response->msg->bytes = 0;
- if (sscanf(parsed_cmd->argv[1], "%u", &record_amount) < 1) response->success = WS_PROTOCOL_CMD_RETURN_ERROR;
+ if (sscanf(parsed_cmd->argv[1], "%x", &record_amount) < 1) response->success = WS_PROTOCOL_CMD_RETURN_ERROR;
+ if (sscanf(parsed_cmd->argv[2], "%x", &record_offset) < 1) response->success = WS_PROTOCOL_CMD_RETURN_ERROR;
else {
- record_amount = WS_MIN(record_amount, ws_backlog_get_record_count());
+ record_amount = WS_MIN(record_amount + record_offset, ws_backlog_get_record_count());
+ record_amount = WS_MAX(0, record_amount - record_offset);
response->msg->bytes = strlen(response_header) + response_line_len * record_amount;
}
} else {
@@ -26,7 +29,7 @@ void ws_protocol_res_last_records(ws_s_protocol_parsed_req_cmd* parsed_cmd, ws_s
ws_protocol_send_data(response_header, strlen(response_header));
char line[response_line_len + 1]; // + 1 for string terminator
for (unsigned int i = 0; i < record_amount; i++) {
- ws_s_backlog_record* record = ws_backlog_get_last_record(i);
+ ws_s_backlog_record* record = ws_backlog_get_last_record(i + record_offset);
sprintf(line, "%04x,%02x,%02x,%02x\n", record->id, record->sens_temperature, record->sens_humidity, record->sens_atm_pressure);
ws_protocol_send_data(line, response_line_len);
}