]> git.scottworley.com Git - tattlekey/blobdiff - client/net.c
client: New epoch if seq wraps
[tattlekey] / client / net.c
index da33727bbb9e00b0bde4bb7f9a6f59a3856026ba..3d4be8cea998f707472740155a601ea912f46001 100644 (file)
@@ -1,8 +1,26 @@
+/* tattlekey: A one-key UDP keyboard
+ * Copyright (C) 2023  Scott Worley <scottworley@scottworley.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
 #include "net.h"
 #include "blink.h"
 #include "config.h"
 
 #include "pico/cyw43_arch.h"
+#include "pico/rand.h"
 
 #include "lwip/pbuf.h"
 #include "lwip/udp.h"
 /* We only ever send to one address, and we only ever have one thread, so just
  * use one udp_pcb */
 static struct udp_pcb *the_pcb = NULL;
+static uint32_t epoch = 0;
 
-static void initialize_the_pcb() {
+static void net_local_init() {
   if (the_pcb)
     return;
 
+  new_epoch();
+
   the_pcb = udp_new();
   if (!the_pcb)
     signal_error_by_blinking();
 
   ip_addr_t ipaddr;
-  if (ip4addr_aton(tattle_server_ip_address, &ipaddr) == 0)
+  if (ip4addr_aton(config_tattle_server_ip_address, &ipaddr) == 0)
     signal_error_by_blinking();
 
-  if (udp_connect(the_pcb, &ipaddr, htons(tattle_port)) != ERR_OK)
+  if (udp_connect(the_pcb, &ipaddr, config_tattle_port) != ERR_OK)
     signal_error_by_blinking();
 }
 
+void new_epoch() { epoch = get_rand_32(); }
+
 struct tattle_message_wire_format {
+  u32_t epoch;
   u16_t sender;
   u16_t seq;
-  u16_t ago;
+  u32_t ago;
 };
 
-void send_report(u16_t seq, u16_t ago) {
+void send_report_packet(u16_t seq, u32_t ago) {
   cyw43_arch_lwip_begin();
 
-  initialize_the_pcb();
+  net_local_init();
 
   struct pbuf *p = pbuf_alloc(
       PBUF_TRANSPORT, sizeof(struct tattle_message_wire_format), PBUF_RAM);
@@ -45,9 +69,10 @@ void send_report(u16_t seq, u16_t ago) {
 
   struct tattle_message_wire_format *msg =
       (struct tattle_message_wire_format *)(p->payload);
-  msg->sender = htons(this_tattler_identity);
+  msg->epoch = htonl(epoch);
+  msg->sender = htons(config_this_tattler_identity);
   msg->seq = htons(seq);
-  msg->ago = htons(ago);
+  msg->ago = htonl(ago);
 
   if (udp_send(the_pcb, p) != ERR_OK)
     signal_error_by_blinking();