]> git.scottworley.com Git - tattlekey/blame_incremental - client/net.c
client: Bump default config_resend_count 5 → 16
[tattlekey] / client / net.c
... / ...
CommitLineData
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
18#include "net.h"
19#include "blink.h"
20#include "config.h"
21
22#include "pico/cyw43_arch.h"
23
24#include "lwip/pbuf.h"
25#include "lwip/udp.h"
26
27/* We only ever send to one address, and we only ever have one thread, so just
28 * use one udp_pcb */
29static struct udp_pcb *the_pcb = NULL;
30
31static void initialize_the_pcb() {
32 if (the_pcb)
33 return;
34
35 the_pcb = udp_new();
36 if (!the_pcb)
37 signal_error_by_blinking();
38
39 ip_addr_t ipaddr;
40 if (ip4addr_aton(config_tattle_server_ip_address, &ipaddr) == 0)
41 signal_error_by_blinking();
42
43 if (udp_connect(the_pcb, &ipaddr, config_tattle_port) != ERR_OK)
44 signal_error_by_blinking();
45}
46
47struct tattle_message_wire_format {
48 u16_t sender;
49 u16_t seq;
50 u16_t ago;
51};
52
53void send_report_packet(u16_t seq, u16_t ago) {
54 cyw43_arch_lwip_begin();
55
56 initialize_the_pcb();
57
58 struct pbuf *p = pbuf_alloc(
59 PBUF_TRANSPORT, sizeof(struct tattle_message_wire_format), PBUF_RAM);
60 if (!p)
61 signal_error_by_blinking();
62
63 struct tattle_message_wire_format *msg =
64 (struct tattle_message_wire_format *)(p->payload);
65 msg->sender = htons(config_this_tattler_identity);
66 msg->seq = htons(seq);
67 msg->ago = htons(ago);
68
69 if (udp_send(the_pcb, p) != ERR_OK)
70 signal_error_by_blinking();
71
72 pbuf_free(p);
73
74 cyw43_arch_lwip_end();
75}