summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-05-26 14:55:36 +0300
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-05-26 14:55:36 +0300
commit03d020c10113db431efeb6ae0d3380b2553387ac (patch)
tree6dc073fa8ecdb015b0cd3a7ab92b9b66001bdd89 /src
parentcce1aad04a2ce4ea0447ec8f3f4c41a8936d4a1a (diff)
parentb25865cc827f4a6a9c31f3d92a4e443485fd5d93 (diff)
downloadusurpation-03d020c10113db431efeb6ae0d3380b2553387ac.tar.gz
usurpation-03d020c10113db431efeb6ae0d3380b2553387ac.tar.bz2
usurpation-03d020c10113db431efeb6ae0d3380b2553387ac.zip
Merge branch '31-Net-Protocol-Handling'
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src')
-rw-r--r--src/common/meson.build4
-rw-r--r--src/common/protocol.c133
-rw-r--r--src/common/protocol_private.h48
-rw-r--r--src/common/utils.c47
-rw-r--r--src/device/device_network.cpp103
-rwxr-xr-xsrc/device/device_network.h40
-rw-r--r--src/device/main.ino75
-rw-r--r--src/device/meson.build7
-rw-r--r--src/meson.build4
9 files changed, 394 insertions, 67 deletions
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')