summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/meson.build7
-rwxr-xr-xinclude/udp.h16
-rw-r--r--meson.build2
-rw-r--r--src/common/meson.build4
-rw-r--r--src/common/protocol.c6
-rw-r--r--src/device/device_network.cpp103
-rwxr-xr-xsrc/device/device_network.h40
-rw-r--r--src/device/main.ino48
-rw-r--r--src/device/meson.build7
-rw-r--r--src/device/protocol_device.c26
-rw-r--r--src/device/udp.ino25
-rw-r--r--src/meson.build4
12 files changed, 201 insertions, 87 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/udp.h b/include/udp.h
deleted file mode 100755
index 9b3f785..0000000
--- a/include/udp.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#ifndef DEVICE_UDP_H
-#define DEVICE_UDP_H
-
-#include <ESP8266WiFi.h>
-#include <WiFiUdp.h>
-
-static const int com_port = 6996;
-extern IPAddress ip; /* Daemon IP */
-extern WiFiUDP Udp;
-
-extern void discover_client(const int port);
-extern void udp_init_packet(IPAddress ip, const int port);
-extern void udp_push(const void * const data, const size_t size);
-extern int udp_flush(void);
-
-#endif /* DEVICE_UDP_H */ \ No newline at end of file
diff --git a/meson.build b/meson.build
index 4a780c8..69bcf7c 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/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
index dc0f532..0ffaede 100644
--- a/src/common/protocol.c
+++ b/src/common/protocol.c
@@ -24,12 +24,12 @@
* separately.
*/
-#include "protocol.h"
-#include "protocol_private.h"
-#include "net.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
+#include "protocol.h"
+#include "protocol_private.h"
+#include "net.h"
#define READ_AS(from, type) (*(type*)(from))
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 d4e092d..66e52e8 100644
--- a/src/device/main.ino
+++ b/src/device/main.ino
@@ -26,16 +26,14 @@
#include <stddef.h>
#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 <time.h>
+#include <stdlib.h>
+#include <string.h>
#include "net.h"
#include "protocol.h"
#include "protocol_private.h"
#include "protocol_device_private.h"
#include "settings.h"
-#include <time.h>
-#include <stdlib.h>
-#include <string.h>
#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 <ESP8266WiFi.h>
-#include <WiFiUdp.h>
-
-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/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')