From cabb90c1240015ee5cd17d91573588527bcc2482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 21 May 2019 17:41:21 +0300 Subject: Protocol: changes to interface and some re-implementation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Most functions exposed in protocol.h take a connection descriptor (cd_t) as first argument. This allows for multiple connections. Device gets only one connection which means that cd_t is effectively 0 all the time. Additionaly, any function that actually does anything with a connection descriptor instead of just passing it to another function must be implemented separately in device and daemon. Signed-off-by: Ramūnas Mažeikis --- src/device/protocol_device.ino | 38 ++++++++++++++++++++++++++++++++++++ src/device/protocol_device_private.h | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100755 src/device/protocol_device.ino create mode 100755 src/device/protocol_device_private.h (limited to 'src/device') diff --git a/src/device/protocol_device.ino b/src/device/protocol_device.ino new file mode 100755 index 0000000..778196d --- /dev/null +++ b/src/device/protocol_device.ino @@ -0,0 +1,38 @@ +#include "protocol.h" +#include "protocol_private.h" +#include "protocol_device_private.h" +#include +#include +#include + +static struct connection_t connection; + +static int push_bytes(cd_t cd, char *data, size_t size) +{ + int ret = 0; + + if (cd >= MAX_CONNECTIONS) { + ret = E_IVALID_DESCRIPTOR; + } else if (connection.outp_crs + size >= sizeof(connection.outp_buf)) { + ret = E_PACKET_OVERFLOW; + } else { + + } + return ret; +} + +static int push_tlv_header(cd_t connection, enum tlv_type type, size_t size) +{ + return 0; +} + +struct tlv * get_tlv() +{ + return NULL; +} + + +void get_last_data(cd_t connection) +{ + +} diff --git a/src/device/protocol_device_private.h b/src/device/protocol_device_private.h new file mode 100755 index 0000000..0fb46c7 --- /dev/null +++ b/src/device/protocol_device_private.h @@ -0,0 +1,37 @@ +#ifndef PROTOCOL_DEVICE_PRIVATE_H +#define PROTOCOL_DEVICE_PRIVATE_H + +#define MAX_PACKET_SIZE_OUT (64) +#define MAX_PACKET_SIZE_IN (512) +#define MAX_CONNECTIONS (1UL) + +/** + * Device's view of connection to daemon. + */ +struct connection_t { + /** + * Initially 0, non-zero when connection is open, 0 when connection + * is closed again. + */ + int is_live; + + /** Daemon IP adress */ + IPAddress address; + + /** Time last packet was reveived */ + time_t last_packet_sec; + + /** Incoming packet buffer */ + char inp_buf[MAX_PACKET_SIZE_IN]; + + /** Incoming packet cursor */ + size_t inp_crs; + + /** Outgoing packet buffer */ + char outp_buf[MAX_PACKET_SIZE_OUT]; + + /** Outgoing packet cursor */ + size_t outp_crs; +}; + +#endif /* PROTOCOL_DEVICE_PRIVATE_H */ \ No newline at end of file -- cgit v1.2.3 From 36b3e7f310c624b7b4e31829090dd02131c528d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 21 May 2019 19:16:22 +0300 Subject: Protocol: implementation of device-side protocol. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Parts of protocol specific for device are implementepd to reflect the fact that the device is a client. This effectively means that device gets a single connection only and severe memory restrictions about which the daemon might get informed about in the future. Signed-off-by: Ramūnas Mažeikis --- src/device/protocol_device.c | 86 ++++++++++++++++++++++++++++++++++++ src/device/protocol_device.ino | 38 ---------------- src/device/protocol_device_private.h | 27 ++++++++++- 3 files changed, 111 insertions(+), 40 deletions(-) create mode 100755 src/device/protocol_device.c delete mode 100755 src/device/protocol_device.ino (limited to 'src/device') diff --git a/src/device/protocol_device.c b/src/device/protocol_device.c new file mode 100755 index 0000000..3b3d8c8 --- /dev/null +++ b/src/device/protocol_device.c @@ -0,0 +1,86 @@ +#include "net.h" +#include "protocol.h" +#include "protocol_private.h" +#include "protocol_device_private.h" +#include "settings.h" +#include +#include +#include + +#define READ_AS(type, from) (*((type*)(from))) + +static struct connection_t connection; + +cd_t protocol_init(void) +{ + connection.is_live = 1; + connection.nd = net_init(setting_port()); + return 0; +} + +static int push_bytes(cd_t cd, char *data, size_t size) +{ + int ret = 0; + + if (cd >= MAX_CONNECTIONS) { + ret = E_IVALID_DESCRIPTOR; + } else if (connection.outp_crs + size >= sizeof(connection.outp_buf)) { + ret = E_PACKET_OVERFLOW; + } else { + memcpy(connection.outp_buf, data, size); + connection.outp_crs += size; + } + return ret; +} + +static int push_tlv_header(cd_t cd, enum tlv_type type, size_t size) +{ + int ret = 0; + + if (cd >= MAX_CONNECTIONS) { + errno = E_IVALID_DESCRIPTOR; + ret = NULL; + } else if (connection.outp_crs >= MAX_PACKET_SIZE_IN) { + errno = E_TLV_OVERFLOW; + ret = NULL; + } else { + READ_AS(enum tlv_type, connection.outp_buf + connection.outp_crs) + = type; + connection.outp_crs += sizeof(enum tlv_type); + READ_AS(size_t, connection.outp_buf + connection.outp_crs) = size; + connection.outp_crs += sizeof(size_t); + } + return ret; +} + +struct tlv * get_tlv(cd_t cd) +{ + struct tlv *ret = &connection.next_tlv; + size_t offset = connection.inp_crs; + char *data = connection.inp_buf; + + if (cd >= MAX_CONNECTIONS) { + errno = E_IVALID_DESCRIPTOR; + ret = NULL; + } else if (offset >= MAX_PACKET_SIZE_IN) { + errno = E_TLV_OVERFLOW; + ret = NULL; + } else { + ret->type = READ_AS(enum tlv_type, data + offset); + offset += sizeof(enum tlv_type); + ret->length = READ_AS(size_t, data + offset); + offset += sizeof(size_t); + ret->data = data + offset; + connection.inp_crs = offset; + } + return ret; +} + + +int get_last_data(cd_t cd) +{ + if (cd >= MAX_CONNECTIONS) { + return E_IVALID_DESCRIPTOR; + } + net_getlastdata(connection.nd, connection.inp_buf); +} diff --git a/src/device/protocol_device.ino b/src/device/protocol_device.ino deleted file mode 100755 index 778196d..0000000 --- a/src/device/protocol_device.ino +++ /dev/null @@ -1,38 +0,0 @@ -#include "protocol.h" -#include "protocol_private.h" -#include "protocol_device_private.h" -#include -#include -#include - -static struct connection_t connection; - -static int push_bytes(cd_t cd, char *data, size_t size) -{ - int ret = 0; - - if (cd >= MAX_CONNECTIONS) { - ret = E_IVALID_DESCRIPTOR; - } else if (connection.outp_crs + size >= sizeof(connection.outp_buf)) { - ret = E_PACKET_OVERFLOW; - } else { - - } - return ret; -} - -static int push_tlv_header(cd_t connection, enum tlv_type type, size_t size) -{ - return 0; -} - -struct tlv * get_tlv() -{ - return NULL; -} - - -void get_last_data(cd_t connection) -{ - -} diff --git a/src/device/protocol_device_private.h b/src/device/protocol_device_private.h index 0fb46c7..29ff567 100755 --- a/src/device/protocol_device_private.h +++ b/src/device/protocol_device_private.h @@ -1,10 +1,27 @@ #ifndef PROTOCOL_DEVICE_PRIVATE_H #define PROTOCOL_DEVICE_PRIVATE_H +#include "net.h" + #define MAX_PACKET_SIZE_OUT (64) + +/* Maximum size of packet that can fit into the packet buffer. UDP can carry + * bigger packets but memory is hard to come by and more won't be needed + * anyway. + * */ #define MAX_PACKET_SIZE_IN (512) + +/** + * Device only gets one connection. Because daemon. + */ #define MAX_CONNECTIONS (1UL) +/** + * Error which says that writing any more to packet buffer will overflow + * outgoing buffer. + */ +#define E_PACKET_OVERFLOW (1 << 0) + /** * Device's view of connection to daemon. */ @@ -15,8 +32,8 @@ struct connection_t { */ int is_live; - /** Daemon IP adress */ - IPAddress address; + /** Network descriptor that maps to daemon */ + int nd; /** Time last packet was reveived */ time_t last_packet_sec; @@ -32,6 +49,12 @@ struct connection_t { /** Outgoing packet cursor */ size_t outp_crs; + + /** + * Next tlv to be returned via get_tlv. NULL initially and after last + * element. + * */ + struct tlv next_tlv; }; #endif /* PROTOCOL_DEVICE_PRIVATE_H */ \ No newline at end of file -- cgit v1.2.3 From 5ca7e24e58f6bd657787b9235247149ffff420ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 21 May 2019 19:28:23 +0300 Subject: Protocol: common no longer has device-specific implementations. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Forgot to remove device-specific code from common protocol code. Signed-off-by: Ramūnas Mažeikis --- src/device/protocol_device.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/device') diff --git a/src/device/protocol_device.c b/src/device/protocol_device.c index 3b3d8c8..3e38e19 100755 --- a/src/device/protocol_device.c +++ b/src/device/protocol_device.c @@ -18,6 +18,11 @@ cd_t protocol_init(void) return 0; } +void clear_data(void) +{ + connection.inp_crs = 0; +} + static int push_bytes(cd_t cd, char *data, size_t size) { int ret = 0; -- cgit v1.2.3 From fe71239fc0d1e65e06ba9dcf2fb35239bff21466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 21 May 2019 20:01:08 +0300 Subject: Protocol: moved udp code out of main. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An effor was made to increase modularity of device code. It was a partial success. IP adress, communications port, and wifi login details had to be made static and visible in main. Time is scarce and I am not about to push this further. Signed-off-by: Ramūnas Mažeikis --- src/device/main.ino | 29 +---------------------------- src/device/protocol_device.c | 14 ++++++++++++-- src/device/protocol_device_private.h | 3 +-- src/device/udp.ino | 25 +++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 32 deletions(-) create mode 100755 src/device/udp.ino (limited to 'src/device') diff --git a/src/device/main.ino b/src/device/main.ino index 1a34859..d4e092d 100644 --- a/src/device/main.ino +++ b/src/device/main.ino @@ -26,25 +26,17 @@ #include #include "SSD1306Wire.h" #include "DejaVu_Sans_Mono_13.h" +#include "udp.h" -static char udppacketbuffer[32] = {0}; -static char *udppacketcursor = NULL; static const unsigned int internal_led = 2; static unsigned int led_state = 0; static const char servermagic[] = "I love coffee!"; static const char clientmagic[] = "I love tea!"; -static const int com_port = 6996; -IPAddress ip; /* Daemon IP */ -WiFiUDP Udp; SSD1306Wire display(0x3c, 4, 5, GEOMETRY_128_32); static void init_OLED(void); unsigned int toggle_led(const int ip); static int wifi_connect(const char * const ssid, const char * const password, const char doblink, const int ledpin); -static void discover_client(const int port); -static void udp_init_packet(IPAddress ip, const int port); -static void udp_push(const void * const data, const size_t size); -static int udp_flush(void); static void blink_led(const int pin, const int ontime, const int offtime); void setup(void) @@ -166,25 +158,6 @@ static void discover_client(const int port) } while (!done); } -static void udp_init_packet(IPAddress ip, const int port) -{ - Udp.beginPacket(ip, port); - memset(udppacketbuffer, 0, sizeof(udppacketbuffer)); - udppacketcursor = udppacketbuffer; -} - -static void udp_push(const void * const data, const size_t size) -{ - memcpy(udppacketcursor, data, size); - udppacketcursor += size; -} - -static int udp_flush(void) -{ - Udp.write((const uint8_t *) udppacketbuffer, udppacketcursor - udppacketbuffer); - return Udp.endPacket(); -} - static void blink_led(const int pin, const int ontime, const int offtime) { toggle_led(pin); diff --git a/src/device/protocol_device.c b/src/device/protocol_device.c index 3e38e19..5910546 100755 --- a/src/device/protocol_device.c +++ b/src/device/protocol_device.c @@ -18,9 +18,15 @@ cd_t protocol_init(void) return 0; } -void clear_data(void) +int clear_data(cd_t cd) { - connection.inp_crs = 0; + int ret = 0; + if (cd >= MAX_CONNECTIONS) { + ret = E_IVALID_DESCRIPTOR; + } else { + connection.inp_crs = 0; + } + return ret; } static int push_bytes(cd_t cd, char *data, size_t size) @@ -81,6 +87,10 @@ struct tlv * get_tlv(cd_t cd) return ret; } +int flush_data(cd_t cd) +{ + return 0; +} int get_last_data(cd_t cd) { diff --git a/src/device/protocol_device_private.h b/src/device/protocol_device_private.h index 29ff567..6754ecf 100755 --- a/src/device/protocol_device_private.h +++ b/src/device/protocol_device_private.h @@ -9,8 +9,7 @@ * bigger packets but memory is hard to come by and more won't be needed * anyway. * */ -#define MAX_PACKET_SIZE_IN (512) - +#define MAX_PACKET_SIZE_IN 512 /** * Device only gets one connection. Because daemon. */ diff --git a/src/device/udp.ino b/src/device/udp.ino new file mode 100755 index 0000000..3074b65 --- /dev/null +++ b/src/device/udp.ino @@ -0,0 +1,25 @@ +#include "udp.h" +#include +#include + +static char udppacketbuffer[32] = {0}; +static char *udppacketcursor = NULL; + +void udp_init_packet(IPAddress ip, const int port) +{ + Udp.beginPacket(ip, port); + memset(udppacketbuffer, 0, sizeof(udppacketbuffer)); + udppacketcursor = udppacketbuffer; +} + +void udp_push(const void * const data, const size_t size) +{ + memcpy(udppacketcursor, data, size); + udppacketcursor += size; +} + +int udp_flush(void) +{ + Udp.write((const uint8_t *) udppacketbuffer, udppacketcursor - udppacketbuffer); + return Udp.endPacket(); +} -- cgit v1.2.3 From 8a94a75975ea776c69238b3fff87da78d0567bba Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Tue, 21 May 2019 20:25:34 +0300 Subject: device: strip CRs. Signed-off-by: Gediminas Jakutis --- src/device/udp.ino | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'src/device') diff --git a/src/device/udp.ino b/src/device/udp.ino index 3074b65..b88f314 100755 --- a/src/device/udp.ino +++ b/src/device/udp.ino @@ -1,25 +1,25 @@ -#include "udp.h" -#include -#include - -static char udppacketbuffer[32] = {0}; -static char *udppacketcursor = NULL; - -void udp_init_packet(IPAddress ip, const int port) -{ - Udp.beginPacket(ip, port); - memset(udppacketbuffer, 0, sizeof(udppacketbuffer)); - udppacketcursor = udppacketbuffer; -} - -void udp_push(const void * const data, const size_t size) -{ - memcpy(udppacketcursor, data, size); - udppacketcursor += size; -} - -int udp_flush(void) -{ - Udp.write((const uint8_t *) udppacketbuffer, udppacketcursor - udppacketbuffer); - return Udp.endPacket(); -} +#include "udp.h" +#include +#include + +static char udppacketbuffer[32] = {0}; +static char *udppacketcursor = NULL; + +void udp_init_packet(IPAddress ip, const int port) +{ + Udp.beginPacket(ip, port); + memset(udppacketbuffer, 0, sizeof(udppacketbuffer)); + udppacketcursor = udppacketbuffer; +} + +void udp_push(const void * const data, const size_t size) +{ + memcpy(udppacketcursor, data, size); + udppacketcursor += size; +} + +int udp_flush(void) +{ + Udp.write((const uint8_t *) udppacketbuffer, udppacketcursor - udppacketbuffer); + return Udp.endPacket(); +} -- cgit v1.2.3 From d9aeb50a22f1a49663151a48c23c32797f983696 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Tue, 21 May 2019 20:28:56 +0300 Subject: device: fix shoddy file permissions. Signed-off-by: Gediminas Jakutis --- src/device/protocol_device.c | 0 src/device/protocol_device_private.h | 0 src/device/udp.ino | 0 3 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/device/protocol_device.c mode change 100755 => 100644 src/device/protocol_device_private.h mode change 100755 => 100644 src/device/udp.ino (limited to 'src/device') diff --git a/src/device/protocol_device.c b/src/device/protocol_device.c old mode 100755 new mode 100644 diff --git a/src/device/protocol_device_private.h b/src/device/protocol_device_private.h old mode 100755 new mode 100644 diff --git a/src/device/udp.ino b/src/device/udp.ino old mode 100755 new mode 100644 -- cgit v1.2.3 From 13015f9a8d2b6b982d7d84e62943fbba15aee05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Wed, 22 May 2019 10:49:08 +0300 Subject: Protocol: switched files to C++ and did minor fixes. The code has a chance to compile now. No changes to actual build files yet. --- src/device/protocol_device.c | 101 ---------------------------------------- src/device/protocol_device.ino | 102 +++++++++++++++++++++++++++++++++++++++++ src/device/udp.ino | 50 ++++++++++---------- 3 files changed, 127 insertions(+), 126 deletions(-) delete mode 100755 src/device/protocol_device.c create mode 100755 src/device/protocol_device.ino (limited to 'src/device') diff --git a/src/device/protocol_device.c b/src/device/protocol_device.c deleted file mode 100755 index 5910546..0000000 --- a/src/device/protocol_device.c +++ /dev/null @@ -1,101 +0,0 @@ -#include "net.h" -#include "protocol.h" -#include "protocol_private.h" -#include "protocol_device_private.h" -#include "settings.h" -#include -#include -#include - -#define READ_AS(type, from) (*((type*)(from))) - -static struct connection_t connection; - -cd_t protocol_init(void) -{ - connection.is_live = 1; - connection.nd = net_init(setting_port()); - return 0; -} - -int clear_data(cd_t cd) -{ - int ret = 0; - if (cd >= MAX_CONNECTIONS) { - ret = E_IVALID_DESCRIPTOR; - } else { - connection.inp_crs = 0; - } - return ret; -} - -static int push_bytes(cd_t cd, char *data, size_t size) -{ - int ret = 0; - - if (cd >= MAX_CONNECTIONS) { - ret = E_IVALID_DESCRIPTOR; - } else if (connection.outp_crs + size >= sizeof(connection.outp_buf)) { - ret = E_PACKET_OVERFLOW; - } else { - memcpy(connection.outp_buf, data, size); - connection.outp_crs += size; - } - return ret; -} - -static int push_tlv_header(cd_t cd, enum tlv_type type, size_t size) -{ - int ret = 0; - - if (cd >= MAX_CONNECTIONS) { - errno = E_IVALID_DESCRIPTOR; - ret = NULL; - } else if (connection.outp_crs >= MAX_PACKET_SIZE_IN) { - errno = E_TLV_OVERFLOW; - ret = NULL; - } else { - READ_AS(enum tlv_type, connection.outp_buf + connection.outp_crs) - = type; - connection.outp_crs += sizeof(enum tlv_type); - READ_AS(size_t, connection.outp_buf + connection.outp_crs) = size; - connection.outp_crs += sizeof(size_t); - } - return ret; -} - -struct tlv * get_tlv(cd_t cd) -{ - struct tlv *ret = &connection.next_tlv; - size_t offset = connection.inp_crs; - char *data = connection.inp_buf; - - if (cd >= MAX_CONNECTIONS) { - errno = E_IVALID_DESCRIPTOR; - ret = NULL; - } else if (offset >= MAX_PACKET_SIZE_IN) { - errno = E_TLV_OVERFLOW; - ret = NULL; - } else { - ret->type = READ_AS(enum tlv_type, data + offset); - offset += sizeof(enum tlv_type); - ret->length = READ_AS(size_t, data + offset); - offset += sizeof(size_t); - ret->data = data + offset; - connection.inp_crs = offset; - } - return ret; -} - -int flush_data(cd_t cd) -{ - return 0; -} - -int get_last_data(cd_t cd) -{ - if (cd >= MAX_CONNECTIONS) { - return E_IVALID_DESCRIPTOR; - } - net_getlastdata(connection.nd, connection.inp_buf); -} diff --git a/src/device/protocol_device.ino b/src/device/protocol_device.ino new file mode 100755 index 0000000..218ca85 --- /dev/null +++ b/src/device/protocol_device.ino @@ -0,0 +1,102 @@ +#include "net.h" +#include "protocol.h" +#include "protocol_private.h" +#include "protocol_device_private.h" +#include "settings.h" +#include "udp.h" +#include +#include +#include + +#define READ_AS(type, from) (*((type*)(from))) + +static struct connection_t connection; + +cd_t protocol_init(void) +{ + connection.is_live = 1; + connection.nd = net_init(setting_port()); + return 0; +} + +int clear_data(cd_t cd) +{ + int ret = 0; + if (cd >= MAX_CONNECTIONS) { + ret = E_IVALID_DESCRIPTOR; + } else { + connection.inp_crs = 0; + } + return ret; +} + +static int push_bytes(cd_t cd, char *data, size_t size) +{ + int ret = 0; + + if (cd >= MAX_CONNECTIONS) { + ret = E_IVALID_DESCRIPTOR; + } else if (connection.outp_crs + size >= sizeof(connection.outp_buf)) { + ret = E_PACKET_OVERFLOW; + } else { + memcpy(connection.outp_buf, data, size); + connection.outp_crs += size; + } + return ret; +} + +static int push_tlv_header(cd_t cd, enum tlv_type type, size_t size) +{ + int ret = 0; + + if (cd >= MAX_CONNECTIONS) { + errno = E_IVALID_DESCRIPTOR; + ret = NULL; + } else if (connection.outp_crs >= MAX_PACKET_SIZE_IN) { + errno = E_TLV_OVERFLOW; + ret = NULL; + } else { + READ_AS(enum tlv_type, connection.outp_buf + connection.outp_crs) + = type; + connection.outp_crs += sizeof(enum tlv_type); + READ_AS(size_t, connection.outp_buf + connection.outp_crs) = size; + connection.outp_crs += sizeof(size_t); + } + return ret; +} + +struct tlv * get_tlv(cd_t cd) +{ + struct tlv *ret = &connection.next_tlv; + size_t offset = connection.inp_crs; + char *data = connection.inp_buf; + + if (cd >= MAX_CONNECTIONS) { + errno = E_IVALID_DESCRIPTOR; + ret = NULL; + } else if (offset >= MAX_PACKET_SIZE_IN) { + errno = E_TLV_OVERFLOW; + ret = NULL; + } else { + ret->type = READ_AS(enum tlv_type, data + offset); + offset += sizeof(enum tlv_type); + ret->length = READ_AS(size_t, data + offset); + offset += sizeof(size_t); + ret->data = data + offset; + connection.inp_crs = offset; + } + return ret; +} + +int flush_data(cd_t cd) +{ + return 0; +} + +int get_last_data(cd_t cd) +{ + if (cd >= MAX_CONNECTIONS) { + return E_IVALID_DESCRIPTOR; + } + net_getlastdata(connection.nd, connection.inp_buf); +} diff --git a/src/device/udp.ino b/src/device/udp.ino index 3074b65..b88f314 100755 --- a/src/device/udp.ino +++ b/src/device/udp.ino @@ -1,25 +1,25 @@ -#include "udp.h" -#include -#include - -static char udppacketbuffer[32] = {0}; -static char *udppacketcursor = NULL; - -void udp_init_packet(IPAddress ip, const int port) -{ - Udp.beginPacket(ip, port); - memset(udppacketbuffer, 0, sizeof(udppacketbuffer)); - udppacketcursor = udppacketbuffer; -} - -void udp_push(const void * const data, const size_t size) -{ - memcpy(udppacketcursor, data, size); - udppacketcursor += size; -} - -int udp_flush(void) -{ - Udp.write((const uint8_t *) udppacketbuffer, udppacketcursor - udppacketbuffer); - return Udp.endPacket(); -} +#include "udp.h" +#include +#include + +static char udppacketbuffer[32] = {0}; +static char *udppacketcursor = NULL; + +void udp_init_packet(IPAddress ip, const int port) +{ + Udp.beginPacket(ip, port); + memset(udppacketbuffer, 0, sizeof(udppacketbuffer)); + udppacketcursor = udppacketbuffer; +} + +void udp_push(const void * const data, const size_t size) +{ + memcpy(udppacketcursor, data, size); + udppacketcursor += size; +} + +int udp_flush(void) +{ + Udp.write((const uint8_t *) udppacketbuffer, udppacketcursor - udppacketbuffer); + return Udp.endPacket(); +} -- cgit v1.2.3 From 3fa7dd642af57b8b138e9a0d674c2d9960ebedf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Wed, 22 May 2019 11:33:51 +0300 Subject: Protocol: fixed type errors. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added const where appropriate to satisfy stricter type checking of C++. Signed-off-by: Ramūnas Mažeikis --- src/device/protocol_device.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/device') diff --git a/src/device/protocol_device.ino b/src/device/protocol_device.ino index 218ca85..22e1be1 100644 --- a/src/device/protocol_device.ino +++ b/src/device/protocol_device.ino @@ -30,7 +30,7 @@ int clear_data(cd_t cd) return ret; } -static int push_bytes(cd_t cd, char *data, size_t size) +static int push_bytes(cd_t cd, const char *data, size_t size) { int ret = 0; -- cgit v1.2.3 From 3703a6f28d48ac3f2c28d07405fab17a2a402df4 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Wed, 22 May 2019 14:44:00 +0300 Subject: device: actually make it all build and such. Signed-off-by: Gediminas Jakutis --- src/device/device_network.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++ src/device/device_network.h | 40 ++++++++++++++++ src/device/main.ino | 48 ++++---------------- src/device/meson.build | 7 ++- src/device/protocol_device.c | 26 +++++++++-- src/device/udp.ino | 25 ---------- 6 files changed, 181 insertions(+), 68 deletions(-) create mode 100644 src/device/device_network.cpp create mode 100755 src/device/device_network.h delete mode 100644 src/device/udp.ino (limited to 'src/device') diff --git a/src/device/device_network.cpp b/src/device/device_network.cpp new file mode 100644 index 0000000..d7781a0 --- /dev/null +++ b/src/device/device_network.cpp @@ -0,0 +1,103 @@ +/* + * Usurpation – wearable device udp packet handling + * + * Copyright (C) 2019 Gediminas Jakutis + * Copyright (C) 2019 Ramūnas Mažeikis + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include +#include +#include "device_network.h" +#include "net.h" + +static struct netstate { + WiFiUDP udp; + char udppacketbuffer[1500]; + char *udppacketcursor; + IPAddress daemon_ip; + bool acquired; +} state; + + +static void udp_init_packet_expaddr(IPAddress ip, const int port); + +void udp_init(const int port) +{ + state.udp.begin(com_port); +} + +void udp_init_packet(const int port) +{ + state.udp.beginPacket(state.daemon_ip, port); + memset(state.udppacketbuffer, 0, sizeof(state.udppacketbuffer)); + state.udppacketcursor = state.udppacketbuffer; +} + +void udp_push(const void * const data, const size_t size) +{ + memcpy(state.udppacketcursor, data, size); + state.udppacketcursor += size; +} + +int udp_flush(void) +{ + state.udp.write((const uint8_t *) state.udppacketbuffer, state.udppacketcursor - state.udppacketbuffer); + return state.udp.endPacket(); +} + +void discover_client(const int port) +{ + IPAddress bcastip(255, 255, 255, 255); + char buffer[32] = {0}; + + do { + udp_init_packet_expaddr(bcastip, port); + udp_push(servermagic, sizeof(servermagic)); + udp_flush(); + delay(5); + while (state.udp.parsePacket()) { + if (state.udp.available() >= sizeof(clientmagic)) { + state.udp.read(buffer, sizeof(clientmagic)); + if (!(strcmp(clientmagic, buffer))) { + state.daemon_ip = state.udp.remoteIP(); + ++state.acquired; + } + } + } + delay(95); + } while (!state.acquired); +} + +IPAddress *get_daemon_address(void) +{ + IPAddress *ret; + if (!state.acquired) { + ret = new IPAddress(0, 0, 0, 0); + } else { + ret = new IPAddress(state.daemon_ip); + } + + return ret; +} + +static void udp_init_packet_expaddr(IPAddress ip, const int port) +{ + state.udp.beginPacket(ip, port); + memset(state.udppacketbuffer, 0, sizeof(state.udppacketbuffer)); + state.udppacketcursor = state.udppacketbuffer; +} + diff --git a/src/device/device_network.h b/src/device/device_network.h new file mode 100755 index 0000000..d8f41a1 --- /dev/null +++ b/src/device/device_network.h @@ -0,0 +1,40 @@ +/* + * Usurpation – wearable device udp packet handling + * + * Copyright (C) 2019 Gediminas Jakutis + * Copyright (C) 2019 Ramūnas Mažeikis + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef DEVICE_UDP_H +#define DEVICE_UDP_H + +#include +#include + +static const int com_port = 6996; + +void udp_init(const int port); +void udp_init_packet(const int port); +void udp_push(const void * const data, const size_t size); +int udp_flush(void); +void discover_client(const int port); +IPAddress *get_daemon_address(void); + +static const char servermagic[] = "I love coffee!"; +static const char clientmagic[] = "I love tea!"; + +#endif /* DEVICE_UDP_H */ diff --git a/src/device/main.ino b/src/device/main.ino index d4e092d..66e52e8 100644 --- a/src/device/main.ino +++ b/src/device/main.ino @@ -26,16 +26,14 @@ #include #include "SSD1306Wire.h" #include "DejaVu_Sans_Mono_13.h" -#include "udp.h" +#include "device_network.h" static const unsigned int internal_led = 2; static unsigned int led_state = 0; -static const char servermagic[] = "I love coffee!"; -static const char clientmagic[] = "I love tea!"; SSD1306Wire display(0x3c, 4, 5, GEOMETRY_128_32); static void init_OLED(void); -unsigned int toggle_led(const int ip); +unsigned int toggle_led(const int pin); static int wifi_connect(const char * const ssid, const char * const password, const char doblink, const int ledpin); static void blink_led(const int pin, const int ontime, const int offtime); @@ -50,7 +48,7 @@ void setup(void) display.fillCircle(32, 16, 12); display.display(); wifi_connect(ssid, password, 1, internal_led); - Udp.begin(com_port); + udp_init(com_port); display.fillCircle(64, 16, 12); display.display(); discover_client(com_port); @@ -65,25 +63,23 @@ void loop(void) static const String daemonstr = "Daemon IP:"; static String prefix; static IPAddress ip_to_print; + static IPAddress *daemon_ip = NULL; static int print_dev_ip = 0; static unsigned int delta = 2000; /* sleep length to use (ms) */ /* static int dot_idx = 0; */ delay(delta); - udp_init_packet(ip, com_port); + udp_init_packet(com_port); udp_push(clientmagic, sizeof(clientmagic)); udp_flush(); -#if 0 - if (dot_idx >= 2) { - display.clear(); - dot_idx = 0; + + if (!daemon_ip) { + daemon_ip = get_daemon_address(); } - display.fillCircle(32 * (dot_idx + 1), 16, 12); - dot_idx++; -#endif + prefix = (print_dev_ip) ? devstr : daemonstr; - ip_to_print = (print_dev_ip) ? WiFi.localIP() : ip; + ip_to_print = (print_dev_ip) ? WiFi.localIP() : *daemon_ip; display.clear(); display.drawString(0, 0, prefix); display.drawString(0, 16, ip_to_print.toString()); @@ -134,30 +130,6 @@ static int wifi_connect(const char * const ssid, const char * const password, co return 0; } -static void discover_client(const int port) -{ - IPAddress bcastip(255, 255, 255, 255); - char buffer[32] = {0}; - size_t done = 0; - - do { - udp_init_packet(bcastip, port); - udp_push(servermagic, sizeof(servermagic)); - udp_flush(); - delay(5); - while (Udp.parsePacket()) { - if (Udp.available() >= sizeof(clientmagic)) { - Udp.read(buffer, sizeof(clientmagic)); - if (!(strcmp(clientmagic, buffer))) { - ip = Udp.remoteIP(); - ++done; - } - } - } - delay(95); - } while (!done); -} - static void blink_led(const int pin, const int ontime, const int offtime) { toggle_led(pin); diff --git a/src/device/meson.build b/src/device/meson.build index 1635f7b..d2e3c6e 100644 --- a/src/device/meson.build +++ b/src/device/meson.build @@ -17,8 +17,11 @@ if get_option('fwbuild') cat = find_program('cat') cp = find_program('cp') - fw_filenames = ['main.ino', 'DejaVu_Sans_Mono_13.h'] - fw_true_sources = files(fw_filenames) + fw_filenames = ['main.ino', + 'DejaVu_Sans_Mono_13.h', + 'device_network.cpp', + 'device_network.h'] + fw_true_sources += files(fw_filenames) fw_filenames += oledlibnames fw_true_sources += oledlib diff --git a/src/device/protocol_device.c b/src/device/protocol_device.c index 5910546..73d8ece 100644 --- a/src/device/protocol_device.c +++ b/src/device/protocol_device.c @@ -1,11 +1,31 @@ +/* + * Usurpation – wearable device udp packet handling + * + * Copyright (C) 2019 Ramūnas Mažeikis + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include +#include +#include #include "net.h" #include "protocol.h" #include "protocol_private.h" #include "protocol_device_private.h" #include "settings.h" -#include -#include -#include #define READ_AS(type, from) (*((type*)(from))) diff --git a/src/device/udp.ino b/src/device/udp.ino deleted file mode 100644 index b88f314..0000000 --- a/src/device/udp.ino +++ /dev/null @@ -1,25 +0,0 @@ -#include "udp.h" -#include -#include - -static char udppacketbuffer[32] = {0}; -static char *udppacketcursor = NULL; - -void udp_init_packet(IPAddress ip, const int port) -{ - Udp.beginPacket(ip, port); - memset(udppacketbuffer, 0, sizeof(udppacketbuffer)); - udppacketcursor = udppacketbuffer; -} - -void udp_push(const void * const data, const size_t size) -{ - memcpy(udppacketcursor, data, size); - udppacketcursor += size; -} - -int udp_flush(void) -{ - Udp.write((const uint8_t *) udppacketbuffer, udppacketcursor - udppacketbuffer); - return Udp.endPacket(); -} -- cgit v1.2.3 From 27992079c057ea068a355e01365800d77b7ace13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Wed, 22 May 2019 14:56:59 +0300 Subject: Protocol: attempted to fix stupids. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ramūnas Mažeikis --- src/device/protocol_device.c | 102 +++++++++++++++++++++++++++++++++++++++++ src/device/protocol_device.ino | 102 ----------------------------------------- src/device/udp.c | 25 ++++++++++ src/device/udp.ino | 25 ---------- 4 files changed, 127 insertions(+), 127 deletions(-) create mode 100644 src/device/protocol_device.c delete mode 100644 src/device/protocol_device.ino create mode 100644 src/device/udp.c delete mode 100644 src/device/udp.ino (limited to 'src/device') diff --git a/src/device/protocol_device.c b/src/device/protocol_device.c new file mode 100644 index 0000000..22e1be1 --- /dev/null +++ b/src/device/protocol_device.c @@ -0,0 +1,102 @@ +#include "net.h" +#include "protocol.h" +#include "protocol_private.h" +#include "protocol_device_private.h" +#include "settings.h" +#include "udp.h" +#include +#include +#include + +#define READ_AS(type, from) (*((type*)(from))) + +static struct connection_t connection; + +cd_t protocol_init(void) +{ + connection.is_live = 1; + connection.nd = net_init(setting_port()); + return 0; +} + +int clear_data(cd_t cd) +{ + int ret = 0; + if (cd >= MAX_CONNECTIONS) { + ret = E_IVALID_DESCRIPTOR; + } else { + connection.inp_crs = 0; + } + return ret; +} + +static int push_bytes(cd_t cd, const char *data, size_t size) +{ + int ret = 0; + + if (cd >= MAX_CONNECTIONS) { + ret = E_IVALID_DESCRIPTOR; + } else if (connection.outp_crs + size >= sizeof(connection.outp_buf)) { + ret = E_PACKET_OVERFLOW; + } else { + memcpy(connection.outp_buf, data, size); + connection.outp_crs += size; + } + return ret; +} + +static int push_tlv_header(cd_t cd, enum tlv_type type, size_t size) +{ + int ret = 0; + + if (cd >= MAX_CONNECTIONS) { + errno = E_IVALID_DESCRIPTOR; + ret = NULL; + } else if (connection.outp_crs >= MAX_PACKET_SIZE_IN) { + errno = E_TLV_OVERFLOW; + ret = NULL; + } else { + READ_AS(enum tlv_type, connection.outp_buf + connection.outp_crs) + = type; + connection.outp_crs += sizeof(enum tlv_type); + READ_AS(size_t, connection.outp_buf + connection.outp_crs) = size; + connection.outp_crs += sizeof(size_t); + } + return ret; +} + +struct tlv * get_tlv(cd_t cd) +{ + struct tlv *ret = &connection.next_tlv; + size_t offset = connection.inp_crs; + char *data = connection.inp_buf; + + if (cd >= MAX_CONNECTIONS) { + errno = E_IVALID_DESCRIPTOR; + ret = NULL; + } else if (offset >= MAX_PACKET_SIZE_IN) { + errno = E_TLV_OVERFLOW; + ret = NULL; + } else { + ret->type = READ_AS(enum tlv_type, data + offset); + offset += sizeof(enum tlv_type); + ret->length = READ_AS(size_t, data + offset); + offset += sizeof(size_t); + ret->data = data + offset; + connection.inp_crs = offset; + } + return ret; +} + +int flush_data(cd_t cd) +{ + return 0; +} + +int get_last_data(cd_t cd) +{ + if (cd >= MAX_CONNECTIONS) { + return E_IVALID_DESCRIPTOR; + } + net_getlastdata(connection.nd, connection.inp_buf); +} diff --git a/src/device/protocol_device.ino b/src/device/protocol_device.ino deleted file mode 100644 index 22e1be1..0000000 --- a/src/device/protocol_device.ino +++ /dev/null @@ -1,102 +0,0 @@ -#include "net.h" -#include "protocol.h" -#include "protocol_private.h" -#include "protocol_device_private.h" -#include "settings.h" -#include "udp.h" -#include -#include -#include - -#define READ_AS(type, from) (*((type*)(from))) - -static struct connection_t connection; - -cd_t protocol_init(void) -{ - connection.is_live = 1; - connection.nd = net_init(setting_port()); - return 0; -} - -int clear_data(cd_t cd) -{ - int ret = 0; - if (cd >= MAX_CONNECTIONS) { - ret = E_IVALID_DESCRIPTOR; - } else { - connection.inp_crs = 0; - } - return ret; -} - -static int push_bytes(cd_t cd, const char *data, size_t size) -{ - int ret = 0; - - if (cd >= MAX_CONNECTIONS) { - ret = E_IVALID_DESCRIPTOR; - } else if (connection.outp_crs + size >= sizeof(connection.outp_buf)) { - ret = E_PACKET_OVERFLOW; - } else { - memcpy(connection.outp_buf, data, size); - connection.outp_crs += size; - } - return ret; -} - -static int push_tlv_header(cd_t cd, enum tlv_type type, size_t size) -{ - int ret = 0; - - if (cd >= MAX_CONNECTIONS) { - errno = E_IVALID_DESCRIPTOR; - ret = NULL; - } else if (connection.outp_crs >= MAX_PACKET_SIZE_IN) { - errno = E_TLV_OVERFLOW; - ret = NULL; - } else { - READ_AS(enum tlv_type, connection.outp_buf + connection.outp_crs) - = type; - connection.outp_crs += sizeof(enum tlv_type); - READ_AS(size_t, connection.outp_buf + connection.outp_crs) = size; - connection.outp_crs += sizeof(size_t); - } - return ret; -} - -struct tlv * get_tlv(cd_t cd) -{ - struct tlv *ret = &connection.next_tlv; - size_t offset = connection.inp_crs; - char *data = connection.inp_buf; - - if (cd >= MAX_CONNECTIONS) { - errno = E_IVALID_DESCRIPTOR; - ret = NULL; - } else if (offset >= MAX_PACKET_SIZE_IN) { - errno = E_TLV_OVERFLOW; - ret = NULL; - } else { - ret->type = READ_AS(enum tlv_type, data + offset); - offset += sizeof(enum tlv_type); - ret->length = READ_AS(size_t, data + offset); - offset += sizeof(size_t); - ret->data = data + offset; - connection.inp_crs = offset; - } - return ret; -} - -int flush_data(cd_t cd) -{ - return 0; -} - -int get_last_data(cd_t cd) -{ - if (cd >= MAX_CONNECTIONS) { - return E_IVALID_DESCRIPTOR; - } - net_getlastdata(connection.nd, connection.inp_buf); -} diff --git a/src/device/udp.c b/src/device/udp.c new file mode 100644 index 0000000..b88f314 --- /dev/null +++ b/src/device/udp.c @@ -0,0 +1,25 @@ +#include "udp.h" +#include +#include + +static char udppacketbuffer[32] = {0}; +static char *udppacketcursor = NULL; + +void udp_init_packet(IPAddress ip, const int port) +{ + Udp.beginPacket(ip, port); + memset(udppacketbuffer, 0, sizeof(udppacketbuffer)); + udppacketcursor = udppacketbuffer; +} + +void udp_push(const void * const data, const size_t size) +{ + memcpy(udppacketcursor, data, size); + udppacketcursor += size; +} + +int udp_flush(void) +{ + Udp.write((const uint8_t *) udppacketbuffer, udppacketcursor - udppacketbuffer); + return Udp.endPacket(); +} diff --git a/src/device/udp.ino b/src/device/udp.ino deleted file mode 100644 index b88f314..0000000 --- a/src/device/udp.ino +++ /dev/null @@ -1,25 +0,0 @@ -#include "udp.h" -#include -#include - -static char udppacketbuffer[32] = {0}; -static char *udppacketcursor = NULL; - -void udp_init_packet(IPAddress ip, const int port) -{ - Udp.beginPacket(ip, port); - memset(udppacketbuffer, 0, sizeof(udppacketbuffer)); - udppacketcursor = udppacketbuffer; -} - -void udp_push(const void * const data, const size_t size) -{ - memcpy(udppacketcursor, data, size); - udppacketcursor += size; -} - -int udp_flush(void) -{ - Udp.write((const uint8_t *) udppacketbuffer, udppacketcursor - udppacketbuffer); - return Udp.endPacket(); -} -- cgit v1.2.3 From 28b26ca86cf18947d6d9543ad753ef112ff4da89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Wed, 22 May 2019 20:33:08 +0300 Subject: Protocol: interface redesign. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every function is a stub now. Reimplementaion coming up. Signed-off-by: Ramūnas Mažeikis --- src/device/protocol_device.c | 121 ----------------------------------- src/device/protocol_device_private.h | 59 ----------------- 2 files changed, 180 deletions(-) delete mode 100644 src/device/protocol_device.c delete mode 100644 src/device/protocol_device_private.h (limited to 'src/device') diff --git a/src/device/protocol_device.c b/src/device/protocol_device.c deleted file mode 100644 index d5ec62d..0000000 --- a/src/device/protocol_device.c +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Usurpation – wearable device udp packet handling - * - * Copyright (C) 2019 Ramūnas Mažeikis - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; version 2.1 - * of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#include -#include -#include -#include "net.h" -#include "protocol.h" -#include "protocol_private.h" -#include "protocol_device_private.h" -#include "settings.h" - -#define READ_AS(type, from) (*((type*)(from))) - -static struct connection_t connection; - -cd_t protocol_init(void) -{ - connection.is_live = 1; - connection.nd = net_init(setting_port()); - return 0; -} - -int clear_data(cd_t cd) -{ - int ret = 0; - if (cd >= MAX_CONNECTIONS) { - ret = E_IVALID_DESCRIPTOR; - } else { - connection.inp_crs = 0; - } - return ret; -} - -static int push_bytes(cd_t cd, const char *data, size_t size) -{ - int ret = 0; - - if (cd >= MAX_CONNECTIONS) { - ret = E_IVALID_DESCRIPTOR; - } else if (connection.outp_crs + size >= sizeof(connection.outp_buf)) { - ret = E_PACKET_OVERFLOW; - } else { - memcpy(connection.outp_buf, data, size); - connection.outp_crs += size; - } - return ret; -} - -static int push_tlv_header(cd_t cd, enum tlv_type type, size_t size) -{ - int ret = 0; - - if (cd >= MAX_CONNECTIONS) { - errno = E_IVALID_DESCRIPTOR; - ret = NULL; - } else if (connection.outp_crs >= MAX_PACKET_SIZE_IN) { - errno = E_TLV_OVERFLOW; - ret = NULL; - } else { - READ_AS(enum tlv_type, connection.outp_buf + connection.outp_crs) - = type; - connection.outp_crs += sizeof(enum tlv_type); - READ_AS(size_t, connection.outp_buf + connection.outp_crs) = size; - connection.outp_crs += sizeof(size_t); - } - return ret; -} - -struct tlv * get_tlv(cd_t cd) -{ - struct tlv *ret = &connection.next_tlv; - size_t offset = connection.inp_crs; - char *data = connection.inp_buf; - - if (cd >= MAX_CONNECTIONS) { - errno = E_IVALID_DESCRIPTOR; - ret = NULL; - } else if (offset >= MAX_PACKET_SIZE_IN) { - errno = E_TLV_OVERFLOW; - ret = NULL; - } else { - ret->type = READ_AS(enum tlv_type, data + offset); - offset += sizeof(enum tlv_type); - ret->length = READ_AS(size_t, data + offset); - offset += sizeof(size_t); - ret->data = data + offset; - connection.inp_crs = offset; - } - return ret; -} - -int flush_data(cd_t cd) -{ - return 0; -} - -int get_last_data(cd_t cd) -{ - if (cd >= MAX_CONNECTIONS) { - return E_IVALID_DESCRIPTOR; - } - net_getlastdata(connection.nd, connection.inp_buf); -} diff --git a/src/device/protocol_device_private.h b/src/device/protocol_device_private.h deleted file mode 100644 index 6754ecf..0000000 --- a/src/device/protocol_device_private.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef PROTOCOL_DEVICE_PRIVATE_H -#define PROTOCOL_DEVICE_PRIVATE_H - -#include "net.h" - -#define MAX_PACKET_SIZE_OUT (64) - -/* Maximum size of packet that can fit into the packet buffer. UDP can carry - * bigger packets but memory is hard to come by and more won't be needed - * anyway. - * */ -#define MAX_PACKET_SIZE_IN 512 -/** - * Device only gets one connection. Because daemon. - */ -#define MAX_CONNECTIONS (1UL) - -/** - * Error which says that writing any more to packet buffer will overflow - * outgoing buffer. - */ -#define E_PACKET_OVERFLOW (1 << 0) - -/** - * Device's view of connection to daemon. - */ -struct connection_t { - /** - * Initially 0, non-zero when connection is open, 0 when connection - * is closed again. - */ - int is_live; - - /** Network descriptor that maps to daemon */ - int nd; - - /** Time last packet was reveived */ - time_t last_packet_sec; - - /** Incoming packet buffer */ - char inp_buf[MAX_PACKET_SIZE_IN]; - - /** Incoming packet cursor */ - size_t inp_crs; - - /** Outgoing packet buffer */ - char outp_buf[MAX_PACKET_SIZE_OUT]; - - /** Outgoing packet cursor */ - size_t outp_crs; - - /** - * Next tlv to be returned via get_tlv. NULL initially and after last - * element. - * */ - struct tlv next_tlv; -}; - -#endif /* PROTOCOL_DEVICE_PRIVATE_H */ \ No newline at end of file -- cgit v1.2.3