summaryrefslogtreecommitdiffstats
path: root/src/device
diff options
context:
space:
mode:
Diffstat (limited to 'src/device')
-rw-r--r--src/device/device_network.cpp29
-rw-r--r--src/device/device_network.h3
-rw-r--r--src/device/main.ino65
3 files changed, 66 insertions, 31 deletions
diff --git a/src/device/device_network.cpp b/src/device/device_network.cpp
index f2456d9..1810852 100644
--- a/src/device/device_network.cpp
+++ b/src/device/device_network.cpp
@@ -22,7 +22,8 @@
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "device_network.h"
-#include "net.h"
+#include "utils.h"
+#include "tlv.h"
static struct netstate {
WiFiUDP udp;
@@ -73,17 +74,31 @@ size_t udp_get_data(char *buf, size_t size)
void discover_client(const int port)
{
IPAddress bcastip(255, 255, 255, 255);
- char buffer[32] = {0};
+ char buffer[128] = {0};
+ struct tlv field;
+ struct tlv_packet pack;
+ size_t expected_s;
+
+ tlv_init(&field, HEARTBEAT);
+ field.data = strdup(heartbeat_device);
+ field.head.size = sizeof(heartbeat_device);
+ tlv_packet_init(&pack);
+ tlv_pack(&pack, &field);
+ tlv_packet_finalize(&pack);
+ tlv_destroy(&field);
do {
udp_init_packet_expaddr(bcastip, port);
- udp_push(servermagic, sizeof(servermagic));
+ udp_push(pack.data, pack.cursor + 1);
udp_flush();
delay(5);
+
+ expected_s = sizeof(field.head) + sizeof(heartbeat_server);
while (state.udp.parsePacket()) {
- if (state.udp.available() >= sizeof(clientmagic)) {
- state.udp.read(buffer, sizeof(clientmagic));
- if (!(strcmp(clientmagic, buffer))) {
+ if (state.udp.available() >= expected_s) {
+ state.udp.read(buffer, sizeof(buffer));
+ tlv_get(buffer, &field, NULL);
+ if (field.head.type == HEARTBEAT && !(strcmp(heartbeat_server, field.data))) {
state.daemon_ip = state.udp.remoteIP();
++state.acquired;
}
@@ -91,6 +106,8 @@ void discover_client(const int port)
}
delay(95);
} while (!state.acquired);
+
+ tlv_packet_destroy(&pack);
}
IPAddress *get_daemon_address(void)
diff --git a/src/device/device_network.h b/src/device/device_network.h
index 92af429..ae719f0 100644
--- a/src/device/device_network.h
+++ b/src/device/device_network.h
@@ -35,7 +35,4 @@ size_t udp_get_data(char *buf, size_t size);
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 4376346..0835906 100644
--- a/src/device/main.ino
+++ b/src/device/main.ino
@@ -28,7 +28,7 @@
#include "DejaVu_Sans_Mono_13.h"
#include "device_network.h"
#include "screen.h"
-#include "net.h"
+#include "utils.h"
#include "tlv.h"
static const unsigned int internal_led = 2;
@@ -42,20 +42,22 @@ static void blink_led(const int pin, const int ontime, const int offtime);
void handle_tlv(const struct tlv *data);
static struct progstate_t {
- int ip_print_count = 5;
- struct display_status ds = {0};
- struct tlv_parser parser = {0};
- struct tlv crr_data;
- size_t bytes_read = 0;
- char in_packet_buf[MTU];
+ int ip_print_count;
+ struct display_status ds;
+ struct tlv field;
+ struct tlv_packet heartbeat;
+ size_t bytes_read;
+ char buf[MTU];
+ char hbcounter;
} progstate;
void setup(void)
{
extern const char * const ssid;
extern const char * const password;
- pinMode(internal_led, OUTPUT);
+ struct tlv field;
+ pinMode(internal_led, OUTPUT);
toggle_led(internal_led);
init_OLED();
display.fillCircle(32, 16, 12);
@@ -67,9 +69,16 @@ void setup(void)
discover_client(com_port);
display.fillCircle(92, 16, 12);
display.display();
+ progstate.ip_print_count = 5;
+ tlv_init(&field, HEARTBEAT);
+ field.data = strdup(heartbeat_device);
+ field.head.size = sizeof(heartbeat_device);
+ tlv_packet_init(&progstate.heartbeat);
+ tlv_pack(&progstate.heartbeat, &field);
+ tlv_packet_finalize(&progstate.heartbeat);
+ tlv_destroy(&field);
}
-/* the logic is a placeholder right now */
void loop(void)
{
static const String devstr = "Device IP:";
@@ -77,16 +86,22 @@ void loop(void)
static String prefix;
static IPAddress ip_to_print;
static IPAddress *daemon_ip = NULL;
+#if 0
+ char *saveptr;
+#endif
static unsigned int delta = 2000; /* sleep length to use (ms) */
delay(delta);
- /* Initial display of ip's. */
- if (progstate.ip_print_count > 0) {
+ if (!progstate.hbcounter) {
udp_init_packet(com_port);
- udp_push(clientmagic, sizeof(clientmagic));
+ udp_push(progstate.heartbeat.data, progstate.heartbeat.cursor + 1);
udp_flush();
+ progstate.hbcounter = 2;
+ }
+ if (progstate.ip_print_count) {
+ /* Initial display of ip's. */
if (!daemon_ip) {
daemon_ip = get_daemon_address();
}
@@ -98,27 +113,33 @@ void loop(void)
display.drawString(0, 16, ip_to_print.toString());
display.display();
progstate.ip_print_count--;
- } else { /* Dealing with tlv's one at a time. */
- progstate.bytes_read = udp_get_data(progstate.in_packet_buf, sizeof(progstate.in_packet_buf));
+ } else {
+ progstate.bytes_read = udp_get_data(progstate.buf, sizeof(progstate.buf));
if (progstate.bytes_read > 0) {
- progstate.parser.data = progstate.in_packet_buf;
- progstate.parser.offset = 0;
- /* Ignore errors for now. */
- while (tlv_get(&progstate.parser, &progstate.crr_data) == 0) {
- handle_tlv(&progstate.crr_data);
+ tlv_get(progstate.buf, &progstate.field, NULL);
+ handle_tlv(&progstate.field);
+#if 0
+ /* Dealing with tlv's one at a time. */
+ saveptr = progstate.buf;
+ while (!(tlv_get(saveptr, &progstate.field, &saveptr))) {
+ handle_tlv(&progstate.field);
}
+#endif
}
+
display_update_scroll(&progstate.ds);
}
+
+ --progstate.hbcounter;
}
-void handle_tlv(const struct tlv *t)
+void handle_tlv(const struct tlv *in)
{
/* Currently just dealing with text.
* */
- switch (t->type) {
+ switch (in->head.type) {
case TEXT:
- display_status_init(&display, &progstate.ds, (char *)t->data);
+ display_status_init(&display, &progstate.ds, in->data);
break;
default:
display.clear();