diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-10-17 12:11:37 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-10-17 12:11:37 +0200 |
commit | 42bf616eac96eba1846c64123a4b3822061b418c (patch) | |
tree | ef9924ee14b593d25c3cf93170fc7977e25dd502 /shared | |
parent | a8ec533f4e548601e283cee628aaf5ffc1e7d662 (diff) |
update naming scheme in protocol.{c,h}
Diffstat (limited to 'shared')
-rw-r--r-- | shared/protocol.c | 44 | ||||
-rw-r--r-- | shared/protocol.h | 57 | ||||
-rw-r--r-- | shared/test.c | 16 |
3 files changed, 53 insertions, 64 deletions
diff --git a/shared/protocol.c b/shared/protocol.c index 377e79e..6795197 100644 --- a/shared/protocol.c +++ b/shared/protocol.c @@ -4,9 +4,9 @@ #include "protocol.h" -void ws_protocol_parse_byte(ws_s_protocol_parser_state* state, char input) { +void ws_protocol_parse_req_byte(ws_s_protocol_req_parser_state* state, char input) { switch(input) { - case WS_PROTOCOL_C_NEWLINE: { + case WS_PROTOCOL_C_EOL: { break; } @@ -33,9 +33,9 @@ void ws_protocol_parse_byte(ws_s_protocol_parser_state* state, char input) { state->arg_len++; // parse cmd into argc and argv - if (state->valid) ws_protocol_cmd_init(state); + if (state->valid) ws_protocol_req_cmd_init(state); // create response - ws_s_protocol_response* response = ws_protocol_parse_finished(state->target); + ws_s_protocol_res* response = ws_protocol_parse_req_finished(state->target); // send response char response_first_line[16]; @@ -51,7 +51,7 @@ void ws_protocol_parse_byte(ws_s_protocol_parser_state* state, char input) { free(response); // reset parser - ws_protocol_parser_reset(state); + ws_protocol_req_parser_reset(state); return; } @@ -59,23 +59,23 @@ void ws_protocol_parse_byte(ws_s_protocol_parser_state* state, char input) { #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_cmd_code(ws_s_protocol_parsed_cmd* parsed_cmd) { +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; } -ws_s_protocol_response* ws_protocol_parse_finished(ws_s_protocol_parsed_cmd* parsed_cmd) { - ws_s_protocol_response* response = malloc(sizeof(ws_s_protocol_response)); +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->msg = NULL; - ws_e_protocol_cmd cmd_code = ws_protocol_get_cmd_code(parsed_cmd); + ws_e_protocol_cmd cmd_code = ws_protocol_get_req_cmd_code(parsed_cmd); if (cmd_code == WS_PROTOCOL_CMD_UNKNOWN) goto ws_protocol_parse_exit; if (cmd_code >= WS_PROTOCOL_CMD_AMOUNT) goto ws_protocol_parse_exit; - void (*ws_protocol_res_handler)(ws_s_protocol_parsed_cmd*, ws_s_protocol_response*) = + void (*ws_protocol_res_handler)(ws_s_protocol_parsed_req_cmd*, ws_s_protocol_res*) = g_ws_protocol_res_handlers[cmd_code]; if (ws_protocol_res_handler == NULL) goto ws_protocol_parse_exit; (*ws_protocol_res_handler)(parsed_cmd, response); @@ -86,19 +86,19 @@ ws_protocol_parse_exit: return response; } -void ws_protocol_parse_bytes(ws_s_protocol_parser_state* state, char* input, unsigned int length) { - for (unsigned int i = 0; i < length; i++) ws_protocol_parse_byte(state, input[i]); +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_parser_state* ws_protocol_parser_alloc() { - ws_s_protocol_parser_state* parser_state = malloc(sizeof(ws_s_protocol_parser_state) + sizeof(uint16_t) * WS_PROTOCOL_CMD_MAX_ARGUMENTS); +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); - ws_protocol_parser_reset(parser_state); + ws_protocol_req_parser_reset(parser_state); return parser_state; } -void ws_protocol_cmd_init(ws_s_protocol_parser_state* state) { - state->target = malloc(sizeof(ws_s_protocol_parsed_cmd) + sizeof(char*) * state->arg_len); +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)); @@ -112,17 +112,17 @@ void ws_protocol_cmd_init(ws_s_protocol_parser_state* state) { } } -void ws_protocol_parser_free(ws_s_protocol_parser_state* state) { +void ws_protocol_req_parser_free(ws_s_protocol_req_parser_state* state) { if (state == NULL) return; - if (state->target != NULL) ws_protocol_cmd_free(state->target); + if (state->target != NULL) ws_protocol_req_cmd_free(state->target); state->target = NULL; free(state->cmd); free(state); return; } -void ws_protocol_parser_reset(ws_s_protocol_parser_state* state) { - if (state->target != NULL) ws_protocol_cmd_free(state->target); +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; @@ -130,7 +130,7 @@ void ws_protocol_parser_reset(ws_s_protocol_parser_state* state) { memset(state->args_len, 0, sizeof(uint16_t) * WS_PROTOCOL_CMD_MAX_ARGUMENTS); } -void ws_protocol_cmd_free(ws_s_protocol_parsed_cmd* cmd) { +void ws_protocol_req_cmd_free(ws_s_protocol_parsed_req_cmd* cmd) { for (unsigned int i = 0; i < cmd->argc; i++) free(cmd->argv[i]); free(cmd); diff --git a/shared/protocol.h b/shared/protocol.h index b7b92bb..9f17e8d 100644 --- a/shared/protocol.h +++ b/shared/protocol.h @@ -10,31 +10,31 @@ #define WS_PROTOCOL_CMD_AMOUNT (1) -#define WS_PROTOCOL_C_NEWLINE (0x0a) +#define WS_PROTOCOL_C_EOL (0x0a) #define WS_PROTOCOL_C_SPACE (0x20) #define WS_PROTOCOL_C_NULL (0x00) /** - * @brief parsed cmd struct, holds arguments similar to argc and argv provided - * to `int main()` + * @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_cmd; +} ws_s_protocol_parsed_req_cmd; /** - * @brief holds parser state variables for `ws_protocol_parse_byte` function. + * @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_cmd* target; /** parsed cmd reference */ + 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_parser_state; +} ws_s_protocol_req_parser_state; /** @brief return values for command handlers */ typedef enum { @@ -46,64 +46,57 @@ typedef enum { typedef struct { ws_e_protocol_cmd_return_value success; ws_s_bin* msg; -} ws_s_protocol_response; +} ws_s_protocol_res; /** * @brief allocate parser struct * * @return pointer to newly allocated struct */ -ws_s_protocol_parser_state* ws_protocol_parser_alloc(); +ws_s_protocol_req_parser_state* ws_protocol_req_parser_alloc(); /** @brief deallocate parser struct, automatically frees all child pointers */ -void ws_protocol_parser_free(ws_s_protocol_parser_state* state); +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_parser_reset(ws_s_protocol_parser_state* state); +void ws_protocol_req_parser_reset(ws_s_protocol_req_parser_state* state); /** - * @brief initialize ws_s_protocol_parsed_cmd struct pointer of - * ws_s_protocol_parser_state (internal only) + * @brief initialize ws_s_protocol_parsed_req_cmd struct pointer of + * ws_s_protocol_req_parser_state (internal only) */ -void ws_protocol_cmd_init(ws_s_protocol_parser_state* state); -/** @brief deallocate ws_s_protocol_parsed_cmd struct pointer (internal only) */ -void ws_protocol_cmd_free(ws_s_protocol_parsed_cmd* cmd); +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 * - * @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); +void ws_protocol_parse_req_byte(ws_s_protocol_req_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); +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_byte has detected a + * 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. * - * @remark [server] - * * @return response * - * @param parsed_cmd cmd parsed into ws_s_protocol_parsed_cmd struct + * @param parsed_cmd cmd parsed into ws_s_protocol_parsed_req_cmd struct */ -ws_s_protocol_response* ws_protocol_parse_finished(ws_s_protocol_parsed_cmd* parsed_cmd); +ws_s_protocol_res* ws_protocol_parse_req_finished(ws_s_protocol_parsed_req_cmd* parsed_cmd); /** * @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); @@ -111,15 +104,13 @@ 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_* + * @param parsed_cmd complete parsed command from ws_protocol_parse_req_* * @param response response struct with uninitialized pointer to msg */ -void ws_protocol_res_last_records(ws_s_protocol_parsed_cmd* parsed_cmd, ws_s_protocol_response* response); +void ws_protocol_res_last_records(ws_s_protocol_parsed_req_cmd* parsed_cmd, ws_s_protocol_res* response); /** * @brief data sender wrapper @@ -138,7 +129,7 @@ typedef enum { } ws_e_protocol_cmd; /** @brief response handlers, called when a command is parsed */ -static void (*g_ws_protocol_res_handlers[WS_PROTOCOL_CMD_AMOUNT])(ws_s_protocol_parsed_cmd*, ws_s_protocol_response*) = { +static void (*g_ws_protocol_res_handlers[WS_PROTOCOL_CMD_AMOUNT])(ws_s_protocol_parsed_req_cmd*, ws_s_protocol_res*) = { [WS_PROTOCOL_CMD_LAST_RECORDS] = &ws_protocol_res_last_records }; diff --git a/shared/test.c b/shared/test.c index 588cd16..f9d4bbc 100644 --- a/shared/test.c +++ b/shared/test.c @@ -7,7 +7,7 @@ #include "protocol.h" -void ws_protocol_res_last_records(ws_s_protocol_parsed_cmd* parsed_cmd, ws_s_protocol_response* response) { +void ws_protocol_res_last_records(ws_s_protocol_parsed_req_cmd* parsed_cmd, ws_s_protocol_res* response) { const char* response_text = "" "id,temperature,humidity,atmospheric_pressure\n" "10dc,2f,c5,7f\n" @@ -34,17 +34,15 @@ int main() { term.c_cc[VMIN] = 1; tcsetattr(STDIN_FILENO, 0, &term); - ws_s_protocol_parser_state* parser1 = ws_protocol_parser_alloc(); + ws_s_protocol_req_parser_state* parser1 = ws_protocol_req_parser_alloc(); - for(;;) { - fflush(stdout); + fflush(stdout); - char byte; - while(read(STDIN_FILENO, &byte, 1) > 0) - ws_protocol_parse_byte(parser1, byte); - } + char byte; + while(read(STDIN_FILENO, &byte, 1) > 0) + ws_protocol_parse_req_byte(parser1, byte); - ws_protocol_parser_free(parser1); + ws_protocol_req_parser_free(parser1); parser1 = NULL; return 0; |