aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore16
-rw-r--r--shared/bin.c11
-rw-r--r--shared/bin.h13
-rw-r--r--shared/protocol.c137
-rw-r--r--shared/protocol.h150
-rw-r--r--shared/protocol.md55
-rw-r--r--shared/shared.mk1
-rw-r--r--shared/util.h4
-rw-r--r--stm32f091/backlog.c36
-rw-r--r--stm32f091/backlog.h4
-rw-r--r--stm32f091/consts.h21
-rw-r--r--stm32f091/esp8266.c40
-rw-r--r--stm32f091/main.c1
-rw-r--r--stm32f091/makefile8
-rw-r--r--stm32f091/protocol.c33
-rw-r--r--stm32f091/readme.md7
-rw-r--r--stm32f091/sensor.c6
-rw-r--r--stm32f091/server.c2
-rw-r--r--stm32f091/server.h29
-rw-r--r--stm32f091/setup.h15
-rw-r--r--stm32f091/util.h16
-rw-r--r--stm32f091/wifi.def.h5
22 files changed, 554 insertions, 56 deletions
diff --git a/.gitignore b/.gitignore
index 1c68c8a..18c2616 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,11 +1,21 @@
-copyright
+# blob
**/*.o
-stm32f091/main.elf
-stm32f091/main.bin
**/.cache
**/compile_commands.json
+
+# stm32-specific files
+stm32f091/main.elf
+stm32f091/main.bin
+stm32f091/wifi.h
+
+# client-specific files
client/makefile
client/client
client/moc_*
+
+# others
+shared/main
.qmake.stash
.vscode/.cortex-debug.registers.state.json
+copyright
+
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..c6e5ddd
--- /dev/null
+++ b/shared/protocol.c
@@ -0,0 +1,137 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "protocol.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);
+ for (unsigned int i = 0; i < state->arg_len; i++)
+ state->target->argv[i] = malloc(sizeof(char) * (state->args_len[i] + 1));
+
+ state->target->argc = state->arg_len;
+
+ 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;
+}
diff --git a/shared/protocol.h b/shared/protocol.h
new file mode 100644
index 0000000..fbe29d6
--- /dev/null
+++ b/shared/protocol.h
@@ -0,0 +1,150 @@
+#pragma once
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "bin.h"
+
+#define WS_PROTOCOL_CMD_MAX_ARGUMENTS (1)
+#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,
+};
+
diff --git a/shared/protocol.md b/shared/protocol.md
new file mode 100644
index 0000000..b6e955c
--- /dev/null
+++ b/shared/protocol.md
@@ -0,0 +1,55 @@
+# 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. See [code
+implementation](#code-implementation) section for more details about naming
+conventions.
+
+- 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>`
+
+Returns the last `n` records 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.
+
+## 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>
+> 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>
+```
+
+## Code implementation
+
+
+
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.h b/shared/util.h
new file mode 100644
index 0000000..f39ff34
--- /dev/null
+++ b/shared/util.h
@@ -0,0 +1,4 @@
+#pragma once
+
+#define WS_MIN(a, b) (((a) < (b)) ? (a) : (b))
+#define WS_MAX(a, b) (((a) > (b)) ? (a) : (b))
diff --git a/stm32f091/backlog.c b/stm32f091/backlog.c
index 3f21924..662fc75 100644
--- a/stm32f091/backlog.c
+++ b/stm32f091/backlog.c
@@ -2,33 +2,41 @@
#include "backlog.h"
-ws_s_backlog_database* WS_G_BACKLOG_DATABASE = NULL;
+ws_s_backlog_database* g_ws_backlog_database = NULL;
void ws_backlog_alloc(uint16_t record_amt) {
- WS_G_BACKLOG_DATABASE = malloc(sizeof(ws_s_backlog_database) + sizeof(ws_s_backlog_record) * record_amt);
- WS_G_BACKLOG_DATABASE->buffer_size = record_amt;
- WS_G_BACKLOG_DATABASE->buffer_start = 0;
- WS_G_BACKLOG_DATABASE->buffer_end = 0;
+ g_ws_backlog_database = malloc(sizeof(ws_s_backlog_database) + sizeof(ws_s_backlog_record) * record_amt);
+ g_ws_backlog_database->buffer_size = record_amt;
+ g_ws_backlog_database->buffer_start = 0;
+ g_ws_backlog_database->buffer_end = 0;
}
void ws_backlog_add_record(ws_s_backlog_record record) {
static uint16_t id = 0;
- WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].id = id++;
- WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].sens_atm_pressure = record.sens_atm_pressure;
- WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].sens_humidity = record.sens_humidity;
- WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].sens_temperature = record.sens_temperature;
+ g_ws_backlog_database->records[g_ws_backlog_database->buffer_end].id = id++;
+ g_ws_backlog_database->records[g_ws_backlog_database->buffer_end].sens_atm_pressure = record.sens_atm_pressure;
+ g_ws_backlog_database->records[g_ws_backlog_database->buffer_end].sens_humidity = record.sens_humidity;
+ g_ws_backlog_database->records[g_ws_backlog_database->buffer_end].sens_temperature = record.sens_temperature;
// shift buffer start/end
- WS_G_BACKLOG_DATABASE->buffer_end = (WS_G_BACKLOG_DATABASE->buffer_end + 1) % WS_G_BACKLOG_DATABASE->buffer_size;
- if (WS_G_BACKLOG_DATABASE->buffer_end == WS_G_BACKLOG_DATABASE->buffer_start)
- WS_G_BACKLOG_DATABASE->buffer_start = (WS_G_BACKLOG_DATABASE->buffer_start + 1) % WS_G_BACKLOG_DATABASE->buffer_size;
+ g_ws_backlog_database->buffer_end = (g_ws_backlog_database->buffer_end + 1) % g_ws_backlog_database->buffer_size;
+ if (g_ws_backlog_database->buffer_end == g_ws_backlog_database->buffer_start)
+ g_ws_backlog_database->buffer_start = (g_ws_backlog_database->buffer_start + 1) % g_ws_backlog_database->buffer_size;
}
ws_s_backlog_record* ws_backlog_get_record(uint16_t record_index) {
- return &WS_G_BACKLOG_DATABASE->records[record_index];
+ return &g_ws_backlog_database->records[record_index];
}
ws_s_backlog_record* ws_backlog_get_last_record(uint16_t record_offset) {
- return ws_backlog_get_record((WS_G_BACKLOG_DATABASE->buffer_end - record_offset - 1) % WS_G_BACKLOG_DATABASE->buffer_size);
+ return ws_backlog_get_record((g_ws_backlog_database->buffer_end - record_offset - 1) % g_ws_backlog_database->buffer_size);
+}
+
+uint16_t ws_backlog_get_record_count() {
+ // add buffer_size to the result of the modulo operation if it's result is negative
+ // (only works when buffer_size is less than 2^15)
+ // this is a consequence of the way in which c handles negative numbers in modulo operations
+ int16_t mod = (g_ws_backlog_database->buffer_end - g_ws_backlog_database->buffer_start) % g_ws_backlog_database->buffer_size;
+ return mod < 0 ? mod + g_ws_backlog_database->buffer_size : mod;
}
diff --git a/stm32f091/backlog.h b/stm32f091/backlog.h
index 465b3c0..c8ea019 100644
--- a/stm32f091/backlog.h
+++ b/stm32f091/backlog.h
@@ -30,7 +30,7 @@ typedef struct {
#pragma pack(pop)
/** @brief global record backlog database pointer */
-extern ws_s_backlog_database* WS_G_BACKLOG_DATABASE;
+extern ws_s_backlog_database* g_ws_backlog_database;
/**
* @brief add record to database
@@ -55,3 +55,5 @@ ws_s_backlog_record* ws_backlog_get_record(uint16_t record_index);
/** @brief get pointer to last record with offset `record_offset` from the database */
ws_s_backlog_record* ws_backlog_get_last_record(uint16_t record_offset);
+/** @brief return amount of valid records in database */
+uint16_t ws_backlog_get_record_count();
diff --git a/stm32f091/consts.h b/stm32f091/consts.h
new file mode 100644
index 0000000..349ff21
--- /dev/null
+++ b/stm32f091/consts.h
@@ -0,0 +1,21 @@
+#pragma once
+
+#include "wifi.h"
+
+#define WS_SERVER_PORT "80"
+
+#define WS_PINOUT_I2C_SDA_PIN GPIO_PIN_9
+#define WS_PINOUT_I2C_SDA_PORT GPIOB
+#define WS_PINOUT_I2C_SCL_PIN GPIO_PIN_8
+#define WS_PINOUT_I2C_SCL_PORT GPIOB
+
+#define WS_PINOUT_USART1_RX_PIN GPIO_PIN_10
+#define WS_PINOUT_USART1_RX_PORT GPIOA
+#define WS_PINOUT_USART1_TX_PIN GPIO_PIN_9
+#define WS_PINOUT_USART1_TX_PORT GPIOA
+
+#define WS_PINOUT_USART2_RX_PIN GPIO_PIN_3
+#define WS_PINOUT_USART2_RX_PORT GPIOA
+#define WS_PINOUT_USART2_TX_PIN GPIO_PIN_2
+#define WS_PINOUT_USART2_TX_PORT GPIOA
+
diff --git a/stm32f091/esp8266.c b/stm32f091/esp8266.c
index 6f12191..4fdf7e6 100644
--- a/stm32f091/esp8266.c
+++ b/stm32f091/esp8266.c
@@ -5,6 +5,7 @@
#include "esp8266.h"
#include "setup.h"
+#include "consts.h"
void ws_esp8266_ATsendCommand(uint8_t* data){
char dataChar[20];
@@ -22,7 +23,7 @@ void ws_esp8266_ATsendCommand(uint8_t* data){
}
int ws_esp8266_checkOK(uint8_t *receiveData,int length){
char *ret="";
- char *ret1="";
+ // char *ret1="";
HAL_UART_Transmit(&huart2, receiveData,length,1000);
ret = strstr((char*)receiveData,"OK");
// ret = strstr((char*)receiveData,"change");
@@ -47,6 +48,7 @@ int ws_esp8266_receivingMsg(uint8_t *receiveData,int length){
HAL_UART_Transmit(&huart2, receiveData,length,1000);
ret = strstr((char*)receiveData,"+IPD");
// memset(receiveData,0,30);
+
if((ret[0]='+') && (ret[1]=='I')){
//HAL_UART_Transmit(&huart2, (uint8_t*)ret, sizeof(ret), 100);
return 1;
@@ -74,13 +76,12 @@ int ws_esp8266_unlink(uint8_t *receiveData,int length){
}
void ws_esp8266_StartEsp(){
-
uint8_t Tx_AT[]="AT\r\n";
uint8_t Rx_buffer[10]={0};
for(int i=0;i<3;i++){
// HAL_UART_Transmit(&huart2, hier,sizeof(hier),100);
- HAL_UART_Transmit_IT(&huart1, Tx_AT,strlen((char*)Tx_AT));
- HAL_UART_Receive_IT(&huart1, Rx_buffer, 10);
+ HAL_UART_Transmit(&huart1, Tx_AT,strlen((char*)Tx_AT), 100);
+ HAL_UART_Receive(&huart1, Rx_buffer, 10, 100);
HAL_UART_Transmit(&huart2, Rx_buffer,10,100);
@@ -94,8 +95,8 @@ void ws_esp8266_disconnect(){
uint8_t Tx_disconnect[]="AT+CWQAP\r\n";uint8_t buffer[17]={0};
while(ret!=1){
- HAL_UART_Transmit_IT(&huart1, Tx_disconnect,strlen((char*)Tx_disconnect));
- HAL_UART_Receive_IT(&huart1, buffer, 17);
+ HAL_UART_Transmit(&huart1, Tx_disconnect,strlen((char*)Tx_disconnect), 100);
+ HAL_UART_Receive(&huart1, buffer, 17, 100);
HAL_Delay(2000);
if(ws_esp8266_checkOK(buffer,17)==1){
@@ -112,8 +113,8 @@ void ws_esp8266_mode(){
while(ret!=1){
- HAL_UART_Transmit_IT(&huart1, Tx_mode,strlen((char*)Tx_mode));
- HAL_UART_Receive_IT(&huart1, buffer1, 20);
+ HAL_UART_Transmit(&huart1, Tx_mode,strlen((char*)Tx_mode), 100);
+ HAL_UART_Receive(&huart1, buffer1, 20, 100);
HAL_Delay(1000);
if(ws_esp8266_checkOK(buffer1,20)==1){
@@ -126,7 +127,7 @@ void ws_esp8266_mode(){
HAL_Delay(1000);
}
void ws_esp8266_connect(){
- uint8_t Tx_network[]="AT+CWJAP=\"Test\",\"12345678\"\r\n";
+ uint8_t Tx_network[]="AT+CWJAP=\"" WS_ESP8266_WLAN_SSID "\",\"" WS_ESP8266_WLAN_PASSWD "\"\r\n";
HAL_UART_Transmit(&huart1, Tx_network,strlen((char*)Tx_network),1000);
@@ -141,12 +142,12 @@ void ws_esp8266_connect(){
}
void ws_esp8266_serveraan(){
int ret;
- uint8_t buffer1[30]={0}; uint8_t Tx_server[]="AT+CIPSERVER=1,80\r\n";
+ uint8_t buffer1[30]={0}; uint8_t Tx_server[]="AT+CIPSERVER=1," WS_SERVER_PORT "\r\n";
while(ret!=1){
- HAL_UART_Transmit_IT(&huart1, Tx_server,strlen((char*)Tx_server));
- HAL_UART_Receive_IT(&huart1, buffer1, 30);
+ HAL_UART_Transmit(&huart1, Tx_server,strlen((char*)Tx_server), 100);
+ HAL_UART_Receive(&huart1, buffer1, 30, 100);
HAL_Delay(2000);
if(ws_esp8266_checkOK(buffer1,30)==1){
@@ -159,13 +160,14 @@ void ws_esp8266_serveraan(){
HAL_Delay(1000);
}
void ws_esp8266_serveruit(){
- int ret;
- uint8_t buffer1[27]={0}; uint8_t Tx_server[]="AT+CIPSERVER=0\r\n";
+ //int ret;
+ //uint8_t buffer1[27]={0};
+ uint8_t Tx_server[]="AT+CIPSERVER=0\r\n";
//
// while(ret!=1){
- HAL_UART_Transmit_IT(&huart1, Tx_server,strlen((char*)Tx_server));
-// HAL_UART_Receive_IT(&huart1, buffer1, 27);
+ HAL_UART_Transmit(&huart1, Tx_server,strlen((char*)Tx_server), 100);
+// HAL_UART_Receive(&huart1, buffer1, 27, 100);
HAL_Delay(3000);
// if(unlink(buffer1,27)==1){
@@ -183,8 +185,8 @@ void ws_esp8266_mux(){
while(ret!=1){
- HAL_UART_Transmit_IT(&huart1, Tx_mux,strlen((char*)Tx_mux));
- HAL_UART_Receive_IT(&huart1, buffer2, 20);
+ HAL_UART_Transmit(&huart1, Tx_mux,strlen((char*)Tx_mux), 100);
+ HAL_UART_Receive(&huart1, buffer2, 20, 100);
HAL_Delay(2000);
if(ws_esp8266_checkOK(buffer2,20)==1){
@@ -201,7 +203,7 @@ void ws_esp8266_close(){
uint8_t Tx_close[]="AT+CIPCLOSE=0\r\n";
- HAL_UART_Transmit_IT(&huart1, Tx_close,strlen((char*)Tx_close));
+ HAL_UART_Transmit(&huart1, Tx_close,strlen((char*)Tx_close), 100);
HAL_Delay(3000);
diff --git a/stm32f091/main.c b/stm32f091/main.c
index 9235f1b..7cb0718 100644
--- a/stm32f091/main.c
+++ b/stm32f091/main.c
@@ -16,7 +16,6 @@ int main() {
});
ws_backlog_alloc(24 * 60);
- ws_sensor_read();
xTaskCreate(ws_sensor_read_task, "sensor", 128, NULL, 1, NULL);
vTaskStartScheduler();
diff --git a/stm32f091/makefile b/stm32f091/makefile
index 5a185de..20424c3 100644
--- a/stm32f091/makefile
+++ b/stm32f091/makefile
@@ -5,6 +5,8 @@ RM = rm -f
TARGET = main
+include ../shared/shared.mk
+
SHARED_FLAGS += -g
SHARED_FLAGS += -DSTM32F091xC
SHARED_FLAGS += -Wall
@@ -74,9 +76,15 @@ $(TARGET).bin: $(TARGET).elf
%.o: %.s
$(CC) -c $(AFLAGS) $< -o $@
+lib/%.o: lib/%.c
+ $(CC) -c $(CFLAGS) -w $< -o $@
+
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
+%-stm.o: %.c
+ $(CC) -c $(CFLAGS) $< -o $@
+
$(TARGET).elf: $(OBJS)
$(LD) $(LFLAGS) $^ -o $@
diff --git a/stm32f091/protocol.c b/stm32f091/protocol.c
new file mode 100644
index 0000000..589ee5d
--- /dev/null
+++ b/stm32f091/protocol.c
@@ -0,0 +1,33 @@
+#include <stdio.h>
+
+#include "../shared/protocol.h"
+#include "backlog.h"
+#include "util.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;
+ const char* response_header = "id,temperature,humidity,atmospheric_pressure\n";
+ const size_t response_line_size = sizeof("xxxx,xx,xx,xx\n");
+
+ if (!send) {
+ response->success = WS_PROTOCOL_CMD_RETURN_OK;
+ if (sscanf(parsed_cmd->argv[1], "%u", &record_amount) < 1) response->success = WS_PROTOCOL_CMD_RETURN_ERROR;
+ record_amount = WS_MIN(record_amount, ws_backlog_get_record_count());
+ response->csh = true;
+ response->msg = ws_bin_s_alloc(0);
+ response->msg->bytes = strlen(response_header) + response_line_size * record_amount;
+ } else {
+ ws_protocol_send_data(response_header, strlen(response_header));
+ char line[response_line_size + 1]; // +1 for null terminator -> sprintf
+ for (unsigned int i = 0; i < record_amount; i++) {
+ ws_s_backlog_record* record = ws_backlog_get_last_record(i);
+ 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_size);
+ }
+ }
+}
+
+void ws_protocol_send_data(const char* data, unsigned int length) {
+ //TODO: implement on esp data channels
+ HAL_UART_Transmit(&huart2, (uint8_t*) data, length, HAL_MAX_DELAY);
+}
diff --git a/stm32f091/readme.md b/stm32f091/readme.md
new file mode 100644
index 0000000..97492d5
--- /dev/null
+++ b/stm32f091/readme.md
@@ -0,0 +1,7 @@
+# stm32 firmware subdirectory
+
+- uses make
+- make sure to initialize the git submodules
+- all warnings from source files in the lib/ subfolder are hidden
+- copy wifi.def.h to wifi.h and edit the network credentials
+
diff --git a/stm32f091/sensor.c b/stm32f091/sensor.c
index bc3cfd3..1c94e2a 100644
--- a/stm32f091/sensor.c
+++ b/stm32f091/sensor.c
@@ -58,6 +58,12 @@ void ws_sensor_read() {
.sens_humidity = ws_sensor_humidity()
};
ws_backlog_add_record(record);
+
+ // < DEBUG PROTOCOL PARSING CODE >
+ // ws_s_protocol_req_parser_state* parser = ws_protocol_req_parser_alloc();
+ // const char* request = "last-records 5\n";
+ // ws_protocol_parse_req_bytes(parser, (char*) request, strlen(request));
+ // ws_protocol_req_parser_free(parser);
}
void ws_sensor_read_task() {
diff --git a/stm32f091/server.c b/stm32f091/server.c
index e289245..4840527 100644
--- a/stm32f091/server.c
+++ b/stm32f091/server.c
@@ -28,7 +28,7 @@ void ws_server_demo() {
uint8_t receive[24]={0};
uint8_t sendToQTData[]="gelukt";
uint8_t test[]="test";
- int ret;
+ //int ret;
//ATsendCommand(sendToQTData);
while (1)
{
diff --git a/stm32f091/server.h b/stm32f091/server.h
index 6a3501d..e134079 100644
--- a/stm32f091/server.h
+++ b/stm32f091/server.h
@@ -1,6 +1,29 @@
#pragma once
-void ws_server_demo();
+#include <stdlib.h>
+#include <stdint.h>
+
+// void ws_server_demo();
+//
+// /** FreeRTOS task that listens for incoming requests from the esp */
+// void ws_server_listen_task();
+
+/**
+ * @brief +IPD incoming request handler
+ *
+ * this function takes chunks of data from the esp8266 and parses +IPD
+ * commands. when a valid +IPD command is detected, it gets forwarded to
+ * ws_protocol_parse_req_byte.
+ *
+ * @param data pointer to data array
+ * @param size amount of bytes allowed to be read from `data`
+ */
+void ws_server_req_incoming(uint8_t* data, size_t size);
+
+// TODO: server req incoming implementation
+//
+// example +IPD command:
+// +IPD,0,15:last-records 5\n
+//
+// TODO: protocol req response parser
-/** FreeRTOS task that listens for incoming requests from the esp */
-void ws_server_listen_task();
diff --git a/stm32f091/setup.h b/stm32f091/setup.h
index 8ca3720..04fa177 100644
--- a/stm32f091/setup.h
+++ b/stm32f091/setup.h
@@ -4,20 +4,7 @@
#include <stm32f0xx_hal_i2c.h>
#include <stm32f0xx_hal_uart.h>
-#define WS_PINOUT_I2C_SDA_PIN GPIO_PIN_9
-#define WS_PINOUT_I2C_SDA_PORT GPIOB
-#define WS_PINOUT_I2C_SCL_PIN GPIO_PIN_8
-#define WS_PINOUT_I2C_SCL_PORT GPIOB
-
-#define WS_PINOUT_USART1_RX_PIN GPIO_PIN_10
-#define WS_PINOUT_USART1_RX_PORT GPIOA
-#define WS_PINOUT_USART1_TX_PIN GPIO_PIN_9
-#define WS_PINOUT_USART1_TX_PORT GPIOA
-
-#define WS_PINOUT_USART2_RX_PIN GPIO_PIN_3
-#define WS_PINOUT_USART2_RX_PORT GPIOA
-#define WS_PINOUT_USART2_TX_PIN GPIO_PIN_2
-#define WS_PINOUT_USART2_TX_PORT GPIOA
+#include "consts.h"
extern I2C_HandleTypeDef hi2c1;
extern UART_HandleTypeDef huart1;
diff --git a/stm32f091/util.h b/stm32f091/util.h
new file mode 100644
index 0000000..92f093f
--- /dev/null
+++ b/stm32f091/util.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stm32f0xx_hal.h>
+#include <string.h>
+
+#include "setup.h"
+#include "../shared/util.h"
+
+#define ws_usb_printf(fmt, ...) { \
+ char temp[255]; \
+ snprintf(temp, 255, fmt, ##__VA_ARGS__); \
+ HAL_UART_Transmit(&huart2, (uint8_t*) temp, sizeof(char) * strlen(temp), HAL_MAX_DELAY); \
+}
+
diff --git a/stm32f091/wifi.def.h b/stm32f091/wifi.def.h
new file mode 100644
index 0000000..f0748d2
--- /dev/null
+++ b/stm32f091/wifi.def.h
@@ -0,0 +1,5 @@
+#pragma once
+
+#define WS_ESP8266_WLAN_SSID "Test"
+#define WS_ESP8266_WLAN_PASSWD "12345678"
+