diff options
Diffstat (limited to 'src/device')
-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 |
4 files changed, 158 insertions, 67 deletions
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 |