]> git.scottworley.com Git - tattlekey/blame - client/net.c
client: xcalloc
[tattlekey] / client / net.c
CommitLineData
1e0a316e
SW
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 */
12static struct udp_pcb *the_pcb = NULL;
13
14static 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;
d7789e5b 23 if (ip4addr_aton(config_tattle_server_ip_address, &ipaddr) == 0)
1e0a316e
SW
24 signal_error_by_blinking();
25
d7789e5b 26 if (udp_connect(the_pcb, &ipaddr, config_tattle_port) != ERR_OK)
1e0a316e
SW
27 signal_error_by_blinking();
28}
29
30struct tattle_message_wire_format {
31 u16_t sender;
32 u16_t seq;
33 u16_t ago;
34};
35
adf355dc 36void send_report_packet(u16_t seq, u16_t ago) {
1e0a316e
SW
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);
d7789e5b 48 msg->sender = htons(config_this_tattler_identity);
1e0a316e
SW
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}