From 62715093e446d952bbc3fa82fdb65deacd576be2 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Wed, 1 Dec 2021 23:42:19 -0700 Subject: Add a controller shell. This adds a mini shell to the tcp server that allows the user to modify the current state. Commands are in the form: set [int_attribute] (=|+=|-=) [number] set [bool_attribute] (on|off|toggle) print end for example: set brightness += 5 set power on print end --- main/sockbuf.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 main/sockbuf.c (limited to 'main/sockbuf.c') diff --git a/main/sockbuf.c b/main/sockbuf.c new file mode 100644 index 0000000..0fede63 --- /dev/null +++ b/main/sockbuf.c @@ -0,0 +1,43 @@ +#include "sockbuf.h" + +#include +#include + +void sockbuf_write(sockbuf_t* sockbuf, const char* value) +{ + write(sockbuf->socket, value, strlen(value)); +} + +sockbuf_t* init_sockbuf(sockbuf_t* sockbuf, int socket) +{ + memset(sockbuf, 0, sizeof(sockbuf_t)); + sockbuf->socket = socket; + + return sockbuf; +} + +int peek_char(sockbuf_t* sockbuf) +{ + if (sockbuf->next == sockbuf->last) { + sockbuf->next = 0; + int size = read(sockbuf->socket, sockbuf->buf, sizeof(sockbuf->buf)); + sockbuf->last = (uint8_t) size; + + if (size <= 0) { + return size - 1; + } + } + + return sockbuf->buf[sockbuf->next]; +} + +int get_char(sockbuf_t* sockbuf) +{ + int ret = peek_char(sockbuf); + + if (ret >= 0) { + sockbuf->next ++; + } + + return ret; +} -- cgit