diff options
Diffstat (limited to 'src/device/main.ino')
-rw-r--r-- | src/device/main.ino | 65 |
1 files changed, 43 insertions, 22 deletions
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(); |