]>
Commit | Line | Data |
---|---|---|
a4a617fd SW |
1 | /* tattlekey: A one-key UDP keyboard |
2 | * Copyright (C) 2023 Scott Worley <scottworley@scottworley.com> | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
1e0a316e SW |
18 | #include "net.h" |
19 | #include "blink.h" | |
20 | #include "config.h" | |
21 | ||
22 | #include "pico/cyw43_arch.h" | |
8eacf88f | 23 | #include "pico/rand.h" |
1e0a316e SW |
24 | |
25 | #include "lwip/pbuf.h" | |
26 | #include "lwip/udp.h" | |
27 | ||
28 | /* We only ever send to one address, and we only ever have one thread, so just | |
29 | * use one udp_pcb */ | |
30 | static struct udp_pcb *the_pcb = NULL; | |
8eacf88f | 31 | static uint32_t epoch = 0; |
1e0a316e | 32 | |
8eacf88f | 33 | static void net_local_init() { |
1e0a316e SW |
34 | if (the_pcb) |
35 | return; | |
36 | ||
c13b9063 | 37 | new_epoch(); |
8eacf88f | 38 | |
1e0a316e SW |
39 | the_pcb = udp_new(); |
40 | if (!the_pcb) | |
41 | signal_error_by_blinking(); | |
42 | ||
43 | ip_addr_t ipaddr; | |
d7789e5b | 44 | if (ip4addr_aton(config_tattle_server_ip_address, &ipaddr) == 0) |
1e0a316e SW |
45 | signal_error_by_blinking(); |
46 | ||
d7789e5b | 47 | if (udp_connect(the_pcb, &ipaddr, config_tattle_port) != ERR_OK) |
1e0a316e SW |
48 | signal_error_by_blinking(); |
49 | } | |
50 | ||
c13b9063 SW |
51 | void new_epoch() { epoch = get_rand_32(); } |
52 | ||
1e0a316e | 53 | struct tattle_message_wire_format { |
8eacf88f | 54 | u32_t epoch; |
1e0a316e SW |
55 | u16_t sender; |
56 | u16_t seq; | |
9266b741 | 57 | u32_t ago; |
1e0a316e SW |
58 | }; |
59 | ||
9266b741 | 60 | void send_report_packet(u16_t seq, u32_t ago) { |
1e0a316e SW |
61 | cyw43_arch_lwip_begin(); |
62 | ||
8eacf88f | 63 | net_local_init(); |
1e0a316e SW |
64 | |
65 | struct pbuf *p = pbuf_alloc( | |
66 | PBUF_TRANSPORT, sizeof(struct tattle_message_wire_format), PBUF_RAM); | |
67 | if (!p) | |
68 | signal_error_by_blinking(); | |
69 | ||
70 | struct tattle_message_wire_format *msg = | |
71 | (struct tattle_message_wire_format *)(p->payload); | |
8eacf88f | 72 | msg->epoch = htonl(epoch); |
d7789e5b | 73 | msg->sender = htons(config_this_tattler_identity); |
1e0a316e | 74 | msg->seq = htons(seq); |
9266b741 | 75 | msg->ago = htonl(ago); |
1e0a316e SW |
76 | |
77 | if (udp_send(the_pcb, p) != ERR_OK) | |
78 | signal_error_by_blinking(); | |
79 | ||
80 | pbuf_free(p); | |
81 | ||
82 | cyw43_arch_lwip_end(); | |
83 | } |