diff options
-rw-r--r-- | include/meson.build | 7 | ||||
-rw-r--r-- | include/net.h | 20 | ||||
-rw-r--r-- | include/protocol.h | 137 | ||||
-rw-r--r-- | include/utils.h | 22 | ||||
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | project.vim | 3 | ||||
-rw-r--r-- | src/common/meson.build | 4 | ||||
-rw-r--r-- | src/common/protocol.c | 133 | ||||
-rw-r--r-- | src/common/protocol_private.h | 48 | ||||
-rw-r--r-- | src/common/utils.c | 47 | ||||
-rw-r--r-- | src/device/device_network.cpp | 103 | ||||
-rwxr-xr-x | src/device/device_network.h | 40 | ||||
-rw-r--r-- | src/device/main.ino | 75 | ||||
-rw-r--r-- | src/device/meson.build | 7 | ||||
-rw-r--r-- | src/meson.build | 4 |
15 files changed, 581 insertions, 71 deletions
diff --git a/include/meson.build b/include/meson.build new file mode 100644 index 0000000..33e9426 --- /dev/null +++ b/include/meson.build @@ -0,0 +1,7 @@ +header_filenames = [ + 'utils.h', + 'net.h', + 'protocol.h' +] + +fw_headers = files(header_filenames) diff --git a/include/net.h b/include/net.h index ce8c8e2..e4ca0a6 100644 --- a/include/net.h +++ b/include/net.h @@ -19,8 +19,8 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef NET_H_INCLUDED -#define NET_H_INCLUDED +#ifndef USURPATION_NET_H_INCLUDED +#define USURPATION_NET_H_INCLUDED #define MTU 1500 @@ -33,9 +33,21 @@ enum response { TMPFILE, }; - +/** + * Initialises a listening socket and returns the associated network descriptor. + */ int net_init(const unsigned short int port); + +/** + * Closes connection associated with network descriptor. + */ int net_close(int nd); + +/** + * Get last data received from connection associated with network descriptor. + * If the pointer pointed by data is NULL, a buffer is allocated by the function + * and needs to be free()'d later. Otherwise, the supplied buffer is reused. + */ int net_getlastdata(int nd, char ** const data); -#endif /* NET_H_INCLUDED */ +#endif /* USURPATION_NET_H_INCLUDED */ diff --git a/include/protocol.h b/include/protocol.h new file mode 100644 index 0000000..34ad4a3 --- /dev/null +++ b/include/protocol.h @@ -0,0 +1,137 @@ +/* + * Usurpataion --- client-server protocol interface. + * + * 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 USURPATION_PROTOCOL_H_INCLUDED +#define USURPATION_PROTOCOL_H_INCLUDED + +#include <errno.h> + +#define E_TLV_OVERFLOW (1 << 0) +#define E_UNKNOWN_TYPE (1 << 1) +#define E_IVALID_DESCRIPTOR (1 << 2) +#define END_OF_PACKET (1 << 3) + +/** + * Regular packets contain tlv's defined by tlv_type. + * + * Hearbeat packet tell daemon that device is still alive and listening. + * + * Discovery packets are used for what they say. + */ +enum packet_type { + REGURAL, + HEARTBEAT, + DISCOVERY +}; + +/** + * Message sequence number since beggining of sesssion. + * + * Mainly used for identifying lost messages. + */ +typedef unsigned int msg_idx_t; + +enum tlv_type { + /** + * NULL-terminated string. To be put in a queue to display on the + * screen. + */ + TEXT, + + /** Fixed point. 1 decimal digit of precision. */ + FPI1, + + /** Literally time_t */ + TIMESTAMP, + + /** Represents a request for lost message. Data is unsigned integer + * that uniquely identifies the message. + */ + REQUEST, + + /** + * Response to request. Begins with unsigned integer that represents + * which message is being repeated and the actual null-terminated + * message after that. + */ + REPLY, + + /** + * UUID that represents a particular device. + */ + UUID +}; + +/** + * Packet data itself is a special type of tlv. A packet is either regular, + * hearbeat or discovery. + * + * May be used to send data. + */ +struct tlv_packet { + enum packet_type type; + size_t size; + size_t offset; + char *data; /* Bytes representing tlv's */ +}; + +/** + * Literally type-length-value + * */ +struct tlv { + enum tlv_type type; + size_t length; + void *data; +}; + +struct tlv_parser { + char *data; + size_t offset; + size_t size; +}; + +int get_tlv(struct tlv_parser *parser, struct tlv *ret); + +/** + * Appends data to the next packet to be sent. Type of data is determined by + * enum tlv_type. + * + * In case of overflow return E_TLV_OVERFLOW. + * + * On next call after retreiving last packet returns END_OF_PACKET. + * + * Overflow can be detected after forming tlv header. This means that the + * packet may have changes. + * */ +int push_data(struct tlv_packet *packet, enum tlv_type type, char *data); + + +/** + * Resets offset to 0 and set entire buffer to 0. + */ +void clear_data(struct tlv_packet *packet); + + +/** + * Tells what size of buffer is needed for next tlv. + */ +size_t tlv_data_size(struct tlv_parser *parser); + +#endif /* USURPATION_PROTOCOL_H_INCLUDED */ diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..95b553d --- /dev/null +++ b/include/utils.h @@ -0,0 +1,22 @@ +#ifndef USURPATION_UTILS_H +#define USURPATION_UTILS_H + +#include <stdint.h> + +typedef struct uuid_s { + char bytes[16]; +} uuid_t; + +int cmp_uuid(uuid_t *first, uuid_t *second); + +/** + * Fixed point number with one decimal digit of precision. + */ +typedef int fpi1_t; + +fpi1_t fpi1_add(fpi1_t a, fpi1_t b); +fpi1_t fpi1_sub(fpi1_t a, fpi1_t b); +fpi1_t fpi1_mul(fpi1_t a, fpi1_t b); +fpi1_t fpi1_div(fpi1_t a, fpi1_t b); + +#endif /* USURPATION_UTILS_H */ diff --git a/meson.build b/meson.build index f3fe05e..4efb996 100644 --- a/meson.build +++ b/meson.build @@ -10,6 +10,8 @@ resource_dir = join_paths(get_option('datadir'), progname) version_fallback = files('VERSION') inc = include_directories('include') +subdir('include') + resource_dir_arg = 'DATA_DIR=' + '"' + join_paths(get_option('prefix'), resource_dir) + '"' add_project_arguments('-D', resource_dir_arg, language : 'c') add_project_link_arguments('-rdynamic', language : 'c') diff --git a/project.vim b/project.vim new file mode 100644 index 0000000..024222d --- /dev/null +++ b/project.vim @@ -0,0 +1,3 @@ +syn keyword ProjectTypes fpi1_t + +hi ProjectTypes ctermfg=green diff --git a/src/common/meson.build b/src/common/meson.build new file mode 100644 index 0000000..b0f0c82 --- /dev/null +++ b/src/common/meson.build @@ -0,0 +1,4 @@ +common_filenames = [ +] + +common_sources = files(common_filenames) diff --git a/src/common/protocol.c b/src/common/protocol.c new file mode 100644 index 0000000..b2e0d40 --- /dev/null +++ b/src/common/protocol.c @@ -0,0 +1,133 @@ +/* + * Usurpataion --- clinet-server protocol implementation. + * + * 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 + */ + +/** + * Common parts of protocol implementation. Handling of anything that actually + * deals with connection descriptor has to be implemented by device and daemon + * separately. + */ + +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include "protocol.h" +#include "protocol_private.h" +#include "net.h" +#include "utils.h" + +int push_tlv(struct tlv_packet *packet, enum tlv_type type, char *data) +{ + int ret = 0; + size_t size; + + switch (type) { + case TEXT: + size = strlen(data) + 1; + break; + case FPI1: + size = sizeof(fpi1_t); + break; + case TIMESTAMP: + size = sizeof(time_t); + break; + case REQUEST: + size = sizeof(msg_idx_t); + break; + case REPLY: + size = sizeof(msg_idx_t) + strlen(data + sizeof(msg_idx_t)); + break; + case UUID: + size = sizeof(uuid_t); + break; + default: + ret = E_UNKNOWN_TYPE; + } + ret |= push_tlv_header(packet, type, size); + ret |= push_bytes(packet, data, size); + return ret; +} + +void clear_data(struct tlv_packet *packet) +{ + packet->offset = 0; + packet->type = 0; + memset(packet->data, 0, packet->size); +} + +static int push_bytes(struct tlv_packet *packet, char *data, size_t size) +{ + int ret = 0; + + if (packet->offset + size >= packet->size) { + ret = E_TLV_OVERFLOW; + } else { + memcpy(packet->data + packet->offset, data, size); + packet->offset += size; + } +} + +static int push_tlv_header(struct tlv_packet *packet, enum tlv_type type, size_t size) +{ + int ret = 0; + + if (packet->offset + sizeof(type) + sizeof(size) >= packet->size) { + ret = E_TLV_OVERFLOW; + } else { + memcpy(packet->data + packet->size, type, sizeof(type)); + packet->offset += sizeof(type); + memcpy(packet->data + packet->offset, size, sizeof(size)); + packet->offset += sizeof(size); + } + return ret; +} + +int get_tlv(struct tlv_parser *parser, struct tlv *ret) +{ + int ret = 0; + + if (parser->offset + sizeof(ret->type) + sizeof(ret->length) >= parser->size) { + ret = E_TLV_OVERFLOW; + } else if (parser -> offset == parser->size) { + ret = END_OF_PACKET; + } else { + ret->type = memcpy(&ret->type, parser->data + parser->offset, sizeof(ret->type)); + parser->size += sizeof(ret->type); + ret->length = memcpy(&ret->length, parser->data + parser->offset, sizeof(ret->length)); + parser->offset += sizeof(ret->length); + if (parser->offset + ret->length >= parser->size) { + ret = E_TLV_OVERFLOW; + } else { + memcpy(ret->data, parser->data, ret->length); + } + } + return ret; +} + +size_t tlv_data_size(struct tlv_parser *parser) +{ + size_t size; + + if (parser->offset + sizeof(enum tlv_type) + sizeof(size_t) >= parser->size) { + size = 0; + } else { + memcpy(&size, parser->data + parser->offset + sizeof(enum tlv_type), sizeof(size_t)); + } + return size; +} diff --git a/src/common/protocol_private.h b/src/common/protocol_private.h new file mode 100644 index 0000000..51d5431 --- /dev/null +++ b/src/common/protocol_private.h @@ -0,0 +1,48 @@ +/* + * Usurpataion --- clinet-server protocol implementation. + * + * 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 + */ + +/** + * Common parts of protocol implementation. Handling of anything that actually + * deals with connection descriptor has to be implemented by device and daemon + * separately. + */ + +#ifndef PROTOCOL_PRIVATE_H +#define PROTOCOL_PRIVATE_H + +#include "protocol_private.h" + +/** + * Convenience function that pushes bytes to the end of a packet and reports + * potential overflow. + * + * In case of detected overflow nothing is done to the packet. + */ +int push_bytes(struct tlv_packet *packet, char *data, size_t size); + +/** + * Convenience function that forms a tlv header at the end of a packet. Reports + * potential overflow. + * + * In case of detected overflow nothing is done to the packet. + */ +int push_tlv_header(struct tlv_packet *packet, enum tlv_type type, size_t size); + +#endif /* PROTOCOL_PRIVATE_H */ diff --git a/src/common/utils.c b/src/common/utils.c new file mode 100644 index 0000000..7522404 --- /dev/null +++ b/src/common/utils.c @@ -0,0 +1,47 @@ +/* + * Usurpataion --- utility functions. + * + * 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 "utils.h" +#include "string.h" + +int cmp_uuid(uuid_t *first, uuid_t *second) +{ + return memcmp(first, second, sizeof(*first)); +} + +fpi1_t fpi1_add(fpi1_t a, fpi1_t b) +{ + return a + b; +} + +fpi1_t fpi1_sub(fpi1_t a, fpi1_t b) +{ + return a - b; +} + +fpi1_t fpi1_mul(fpi1_t a, fpi1_t b) +{ + return (fpi1_t)(((long)a * (long)b) / 100); +} + +fpi1_t fpi1_div(fpi1_t a, fpi1_t b) +{ + return (fpi1_t)(((long)a * 10) / ((long)b * 10)); +} 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 <ESP8266WiFi.h> +#include <WiFiUdp.h> +#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 <ESP8266WiFi.h> +#include <WiFiUdp.h> + +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 1a34859..66e52e8 100644 --- a/src/device/main.ino +++ b/src/device/main.ino @@ -26,25 +26,15 @@ #include <stddef.h> #include "SSD1306Wire.h" #include "DejaVu_Sans_Mono_13.h" +#include "device_network.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); +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 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) @@ -58,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); @@ -73,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()); @@ -142,49 +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 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/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/meson.build b/src/meson.build index be166e6..12b583a 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,3 +1,7 @@ +#subdir('common') + +fw_true_sources = [fw_headers] + subdir('daemon') subdir('device') |