]> git.scottworley.com Git - tattlekey/commitdiff
client: Send UDP packets
authorScott Worley <scottworley@scottworley.com>
Sun, 8 Oct 2023 04:08:58 +0000 (21:08 -0700)
committerScott Worley <scottworley@scottworley.com>
Wed, 11 Oct 2023 01:48:03 +0000 (18:48 -0700)
client/CMakeLists.txt
client/config.c
client/config.h
client/net.c [new file with mode: 0644]
client/net.h [new file with mode: 0644]
client/tattlekey.c

index aeeae37e067f3d5ddd49e3e04cfd72229a90a4f9..ae972002a7de59e0c826e2fae33ebe1e510c2068 100644 (file)
@@ -10,6 +10,7 @@ pico_sdk_init()
 add_executable(tattlekey
   blink.c
   config.c
+  net.c
   tattlekey.c
   )
 
index 538f432648adfe06065a8bb1e74b17ee6dbfafd6..e3f444b7fe519371de45019f1e0be804d8bfa981 100644 (file)
@@ -2,3 +2,8 @@
 
 char wifi_ssid[] = "THEWIFISSID";
 char wifi_pass[] = "THEWIFIPASSWORD";
+
+char tattle_server_ip_address[] = "192.168.10.10";
+u16_t tattle_port = 29803; // 'tk'
+
+u16_t this_tattler_identity = 1;
index 5c8af1ecff0c05aed6196212ea6f9336f57ef0ac..7b89ba2cb583a3fc014cf93df211e2db7ec28793 100644 (file)
@@ -1,7 +1,14 @@
 #ifndef CONFIG_H
 #define CONFIG_H
 
+#include "lwip/arch.h"
+
 extern char wifi_ssid[];
 extern char wifi_pass[];
 
+extern u16_t tattle_port;
+extern char tattle_server_ip_address[];
+
+extern u16_t this_tattler_identity;
+
 #endif
diff --git a/client/net.c b/client/net.c
new file mode 100644 (file)
index 0000000..da33727
--- /dev/null
@@ -0,0 +1,58 @@
+#include "net.h"
+#include "blink.h"
+#include "config.h"
+
+#include "pico/cyw43_arch.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 void initialize_the_pcb() {
+  if (the_pcb)
+    return;
+
+  the_pcb = udp_new();
+  if (!the_pcb)
+    signal_error_by_blinking();
+
+  ip_addr_t ipaddr;
+  if (ip4addr_aton(tattle_server_ip_address, &ipaddr) == 0)
+    signal_error_by_blinking();
+
+  if (udp_connect(the_pcb, &ipaddr, htons(tattle_port)) != ERR_OK)
+    signal_error_by_blinking();
+}
+
+struct tattle_message_wire_format {
+  u16_t sender;
+  u16_t seq;
+  u16_t ago;
+};
+
+void send_report(u16_t seq, u16_t ago) {
+  cyw43_arch_lwip_begin();
+
+  initialize_the_pcb();
+
+  struct pbuf *p = pbuf_alloc(
+      PBUF_TRANSPORT, sizeof(struct tattle_message_wire_format), PBUF_RAM);
+  if (!p)
+    signal_error_by_blinking();
+
+  struct tattle_message_wire_format *msg =
+      (struct tattle_message_wire_format *)(p->payload);
+  msg->sender = htons(this_tattler_identity);
+  msg->seq = htons(seq);
+  msg->ago = htons(ago);
+
+  if (udp_send(the_pcb, p) != ERR_OK)
+    signal_error_by_blinking();
+
+  pbuf_free(p);
+
+  cyw43_arch_lwip_end();
+}
diff --git a/client/net.h b/client/net.h
new file mode 100644 (file)
index 0000000..3cc0791
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef NET_H
+#define NET_H
+
+#include "lwip/arch.h"
+
+void send_report(u16_t seq, u16_t ago);
+
+#endif
index d7a82e57eb7267289049a918cfe346b341872b2c..ec7710789fbf6efcc65e8cc7b085fe3f597a8f8a 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "blink.h"
 #include "config.h"
+#include "net.h"
 
 int main() {
   stdio_init_all();
@@ -15,5 +16,9 @@ int main() {
                                          CYW43_AUTH_WPA2_AES_PSK, 10000))
     signal_error_by_blinking();
   signal(3, 200);
-  signal_success_by_blinking();
+
+  for (int i = 0;; i++) {
+    send_report(i, 0);
+    signal(4, 200);
+  }
 }