Skip to content

Commit

Permalink
sys/net/application_layer: add telnet server module
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Aug 17, 2021
1 parent 9c02e35 commit e122763
Show file tree
Hide file tree
Showing 9 changed files with 422 additions and 0 deletions.
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ PSEUDOMODULES += stdin
PSEUDOMODULES += stdio_cdc_acm
PSEUDOMODULES += stdio_ethos
PSEUDOMODULES += stdio_uart_rx
PSEUDOMODULES += stdio_telnet
PSEUDOMODULES += stm32_eth
PSEUDOMODULES += stm32_eth_auto
PSEUDOMODULES += stm32_eth_link_up
Expand Down
6 changes: 6 additions & 0 deletions makefiles/stdio.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ STDIO_MODULES = \
stdio_rtt \
stdio_semihosting \
stdio_uart \
stdio_telnet \
#

ifneq (,$(filter newlib picolibc,$(USEMODULE)))
Expand Down Expand Up @@ -49,6 +50,11 @@ ifneq (,$(filter stdio_semihosting,$(USEMODULE)))
FEATURES_REQUIRED_ANY += cpu_core_cortexm|arch_riscv
endif

ifneq (,$(filter stdio_telnet,$(USEMODULE)))
DEFAULT_MODULE += auto_init_telnet
USEMODULE += telnet
endif

# enable stdout buffering for modules that benefit from sending out buffers in larger chunks
ifneq (,$(filter picolibc,$(USEMODULE)))
ifneq (,$(filter stdio_cdc_acm stdio_ethos slipdev_stdio stdio_semihosting,$(USEMODULE)))
Expand Down
3 changes: 3 additions & 0 deletions sys/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ endif
ifneq (,$(filter cipher_modes,$(USEMODULE)))
DIRS += crypto/modes
endif
ifneq (,$(filter telnet,$(USEMODULE)))
DIRS += net/application_layer/telnet
endif
ifneq (,$(filter constfs,$(USEMODULE)))
DIRS += fs/constfs
endif
Expand Down
5 changes: 5 additions & 0 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ ifneq (,$(filter sema_inv,$(USEMODULE)))
USEMODULE += atomic_utils
endif

ifneq (,$(filter telnet,$(USEMODULE)))
USEMODULE += pipe
USEMODULE += sock_tcp
endif

ifneq (,$(filter luid,$(USEMODULE)))
FEATURES_OPTIONAL += periph_cpuid
endif
Expand Down
6 changes: 6 additions & 0 deletions sys/auto_init/auto_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ void auto_init(void)
gnrc_dhcpv6_client_simple_pd_init();
}

if (IS_USED(MODULE_AUTO_INIT_TELNET)) {
LOG_DEBUG("auto_init TELNET server\n");
extern void telnet_server_start(void);
telnet_server_start();
}

if (IS_USED(MODULE_AUTO_INIT_MULTIMEDIA)) {
LOG_DEBUG("auto_init MULTIMEDIA\n");
if (IS_USED(MODULE_DFPLAYER)) {
Expand Down
67 changes: 67 additions & 0 deletions sys/include/net/telnet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2021 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.
*/

/**
* @defgroup net_telnet basic Telnet server implementation
* @ingroup net_ipv6
* @brief Telnet server functions
* @{
*
* @file
* @brief Telnet server ([RFC 855](https://tools.ietf.org/html/rfc855)) implementation
* @note This implementation only supports a single client
*
* @author Benjamin Valentin <[email protected]>
*/
#ifndef NET_TELNET_H
#define NET_TELNET_H

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief The port for the Telnet server to listen on
*/
#ifndef TELNET_PORT
#define TELNET_PORT (23)
#endif

/**
* @brief Start the Telnet server thread
*
* @return 0 on success, error otherwise
*/
int telnet_server_start(void);

/**
* @brief Write data to the telnet client
*
* @params[in] buffer The buffer to send to the client
* @params[in] len The length of the buffer
*
* @return 0 on success, error otherwise
*/
int telnet_server_write(const void* buffer, size_t len);

/**
* @brief Read data from the telnet client, will block until data is available.
*
* @params[out] buffer The buffer to write data from the client
* @params[in] count Number of bytes to read
*
* @return number of bytes read, error otherwise
*/
int telnet_server_read(void* buffer, size_t count);

#ifdef __cplusplus
}
#endif

#endif /* NET_TELNET_H */
/** @} */
1 change: 1 addition & 0 deletions sys/net/application_layer/telnet/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
79 changes: 79 additions & 0 deletions sys/net/application_layer/telnet/stdio_telnet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2021 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
* @{
*
* @file
* @brief STDIO over Telnet implementation
*
* This file implements STDIO via a Telnet server with fallback UART output
*
* @author Benjamin Valentin <[email protected]>
*
* @}
*/

#include <errno.h>

#include "board.h"
#include "kernel_defines.h"
#include "net/telnet.h"
#if IS_USED(MODULE_PERIPH_UART)
#include "stdio_uart.h"
#include "periph/uart.h"
#endif
#if IS_USED(MODULE_VFS)
#include "vfs.h"
#endif

#define ENABLE_DEBUG 0
#include "debug.h"

static inline void _init_fallback(void)
{
#if defined(STDIO_UART_DEV) && IS_USED(MODULE_PERIPH_UART)
uart_init(STDIO_UART_DEV, STDIO_UART_BAUDRATE, NULL, NULL);
#endif
}

static inline int _write_fallback(const void* buffer, size_t len)
{
#if defined(STDIO_UART_DEV) && IS_USED(MODULE_PERIPH_UART)
uart_write(STDIO_UART_DEV, buffer, len);
#else
(void)buffer;
#endif

return len;
}

void stdio_init(void)
{
_init_fallback();

#if IS_USED(MODULE_VFS)
vfs_bind_stdio();
#endif
}

ssize_t stdio_read(void* buffer, size_t count)
{
return telnet_server_read(buffer, count);
}

ssize_t stdio_write(const void* buffer, size_t len)
{
int res = telnet_server_write(buffer, len);
if (res != -ENOTCONN) {
return res;
}

return _write_fallback(buffer, len);
}
Loading

0 comments on commit e122763

Please sign in to comment.