]> git.scottworley.com Git - tattlekey/blob - client/net.c
client: Respond to button press
[tattlekey] / client / net.c
1 #include "net.h"
2 #include "blink.h"
3 #include "config.h"
4
5 #include "pico/cyw43_arch.h"
6
7 #include "lwip/pbuf.h"
8 #include "lwip/udp.h"
9
10 /* We only ever send to one address, and we only ever have one thread, so just
11 * use one udp_pcb */
12 static struct udp_pcb *the_pcb = NULL;
13
14 static void initialize_the_pcb() {
15 if (the_pcb)
16 return;
17
18 the_pcb = udp_new();
19 if (!the_pcb)
20 signal_error_by_blinking();
21
22 ip_addr_t ipaddr;
23 if (ip4addr_aton(tattle_server_ip_address, &ipaddr) == 0)
24 signal_error_by_blinking();
25
26 if (udp_connect(the_pcb, &ipaddr, tattle_port) != ERR_OK)
27 signal_error_by_blinking();
28 }
29
30 struct tattle_message_wire_format {
31 u16_t sender;
32 u16_t seq;
33 u16_t ago;
34 };
35
36 void send_report(u16_t seq, u16_t ago) {
37 cyw43_arch_lwip_begin();
38
39 initialize_the_pcb();
40
41 struct pbuf *p = pbuf_alloc(
42 PBUF_TRANSPORT, sizeof(struct tattle_message_wire_format), PBUF_RAM);
43 if (!p)
44 signal_error_by_blinking();
45
46 struct tattle_message_wire_format *msg =
47 (struct tattle_message_wire_format *)(p->payload);
48 msg->sender = htons(this_tattler_identity);
49 msg->seq = htons(seq);
50 msg->ago = htons(ago);
51
52 if (udp_send(the_pcb, p) != ERR_OK)
53 signal_error_by_blinking();
54
55 pbuf_free(p);
56
57 cyw43_arch_lwip_end();
58 }