1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/stdio_udp/stdio_udp.c

94 lines
1.9 KiB
C
Raw Normal View History

2023-01-02 17:04:58 +01:00
/*
* Copyright (C) 2023 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_stdio_udp
* @{
*
* @file
* @brief STDIO over UDP implementation
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*
* @}
*/
#include <errno.h>
2023-06-12 18:19:50 +02:00
#include <stdio.h>
2023-01-02 17:04:58 +01:00
2023-06-12 18:19:50 +02:00
#include "stdio_base.h"
2023-01-02 17:04:58 +01:00
#include "macros/utils.h"
2023-06-12 18:19:50 +02:00
#include "net/sock/async.h"
2023-01-02 17:04:58 +01:00
#include "net/sock/udp.h"
#ifndef CONFIG_STDIO_UDP_PORT
#define CONFIG_STDIO_UDP_PORT 2323
#endif
2023-06-12 18:19:50 +02:00
#ifndef EOT
#define EOT 0x4
2023-01-02 17:04:58 +01:00
#endif
static sock_udp_t sock;
static sock_udp_ep_t remote;
2023-06-12 18:19:50 +02:00
static void _sock_cb(sock_udp_t *sock, sock_async_flags_t flags, void *arg)
{
(void)arg;
if ((flags & SOCK_ASYNC_MSG_RECV) == 0) {
return;
}
void *data, *ctx = NULL;
int res;
while ((res = sock_udp_recv_buf(sock, &data, &ctx, 0, &remote)) > 0) {
isrpipe_write(&stdin_isrpipe, data, res);
/* detach remote */
if (res == 1 && *(int8_t *)data == EOT) {
const char msg[] = "\nremote detached\n";
sock_udp_send(sock, msg, sizeof(msg), &remote);
memset(&remote, 0, sizeof(remote));
}
}
}
static void _init(void)
2023-01-02 17:04:58 +01:00
{
const sock_udp_ep_t local = {
.family = AF_INET6,
.netif = SOCK_ADDR_ANY_NETIF,
.port = CONFIG_STDIO_UDP_PORT,
};
sock_udp_create(&sock, &local, NULL, 0);
2023-06-12 18:19:50 +02:00
sock_udp_set_cb(&sock, _sock_cb, NULL);
2023-01-02 17:04:58 +01:00
}
2023-06-12 18:19:50 +02:00
static ssize_t _write(const void* buffer, size_t len)
2023-01-02 17:04:58 +01:00
{
if (remote.port == 0) {
return -ENOTCONN;
}
if (len == 0) {
return 0;
}
return sock_udp_send(&sock, buffer, len, &remote);
}
2023-06-12 18:19:50 +02:00
static void _detach(void)
{
sock_udp_close(&sock);
memset(&remote, 0, sizeof(remote));
}
STDIO_PROVIDER(STDIO_UDP, _init, _detach, _write)