aboutsummaryrefslogtreecommitdiff
path: root/shared/protocol.h
blob: 5cf7ac6f85d54727fe7c8c70f7afc2497be54eff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#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_NEWLINE (0x0a)
#define WS_PROTOCOL_C_SPACE (0x20)
#define WS_PROTOCOL_C_NULL (0x00)

typedef struct {
	int argc;
	char* argv[];
} ws_s_protocol_parsed_cmd;

typedef struct {
	ws_s_protocol_parsed_cmd* target;
	bool valid;
	char* cmd;
	uint16_t cmd_len;
	uint16_t arg_len;
	uint16_t args_len[];
} ws_s_protocol_parser_state;

//TODO: document
ws_s_protocol_parser_state* ws_protocol_parser_alloc();
void ws_protocol_parser_free(ws_s_protocol_parser_state* state);
void ws_protocol_cmd_init(ws_s_protocol_parser_state* state);
void ws_protocol_cmd_free(ws_s_protocol_parsed_cmd* cmd);

/**
 * @brief parse incoming data byte by byte until a finished command is detected
 *
 * @remark [server]
 *
 * @param state  parser state object, each incoming request should have it's own parser state
 * @param input  input byte
 */
void ws_protocol_parse_byte(ws_s_protocol_parser_state* state, char input);
/**
 * @brief parse incoming data chunk
 *
 * @remark [server]
 *
 * @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_bytes(ws_s_protocol_parser_state* state, char* input, unsigned int length);

/**
 * @brief create a `last-records` request command
 * @remark [client]
 * @return ws_s_bin containing the command string
 */
ws_s_bin* ws_protocol_req_last_records(unsigned int record_amount);

/**
 * @brief `last-records` response handler
 *
 * @remark [server]
 *
 * gets fired when the weather station receives a complete `last-records`
 * command, and returns the response string
 *
 * @param parsed_cmd  complete parsed command from ws_protocol_parse_*
 *
 * @return ws_s_bin containing response string
 */
ws_s_bin* ws_protocol_res_last_records(ws_s_protocol_parsed_cmd* parsed_cmd);

typedef enum {
	WS_PROTOCOL_CMD_LAST_RECORDS = 0,
} ws_e_protocol_cmd;

static ws_s_bin* (*g_ws_protocol_res_handlers[WS_PROTOCOL_CMD_AMOUNT])(ws_s_protocol_parsed_cmd* parsed_cmd) = {
	[WS_PROTOCOL_CMD_LAST_RECORDS] = &ws_protocol_res_last_records
};