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/common/meson.build | 4 ++ src/common/protocol.c | 6 +-- 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 ---------- src/meson.build | 4 ++ 9 files changed, 192 insertions(+), 71 deletions(-) create mode 100644 src/common/meson.build 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') 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 #include #include +#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 +#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(); -} 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') -- cgit v1.2.3