1 /*
2  * Copyright (c) 2019 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef SHELL_TELNET_H__
8 #define SHELL_TELNET_H__
9 
10 #include <zephyr/net/socket.h>
11 #include <zephyr/shell/shell.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 extern const struct shell_transport_api shell_telnet_transport_api;
18 
19 #define SHELL_TELNET_POLLFD_COUNT 3
20 #define SHELL_TELNET_MAX_CMD_SIZE 3
21 
22 /** Line buffer structure. */
23 struct shell_telnet_line_buf {
24 	/** Line buffer. */
25 	char buf[CONFIG_SHELL_TELNET_LINE_BUF_SIZE];
26 
27 	/** Current line length. */
28 	uint16_t len;
29 };
30 
31 /** TELNET-based shell transport. */
32 struct shell_telnet {
33 	/** Handler function registered by shell. */
34 	shell_transport_handler_t shell_handler;
35 
36 	/** Context registered by shell. */
37 	void *shell_context;
38 
39 	/** Buffer for outgoing line. */
40 	struct shell_telnet_line_buf line_out;
41 
42 	/** Array for sockets used by the telnet service. */
43 	struct zsock_pollfd fds[SHELL_TELNET_POLLFD_COUNT];
44 
45 	/** Input buffer. */
46 	uint8_t rx_buf[CONFIG_SHELL_CMD_BUFF_SIZE];
47 
48 	/** Number of data bytes within the input buffer. */
49 	size_t rx_len;
50 
51 	/** Mutex protecting the input buffer access. */
52 	struct k_mutex rx_lock;
53 	uint8_t cmd_buf[SHELL_TELNET_MAX_CMD_SIZE];
54 	uint8_t cmd_len;
55 
56 	/** The delayed work is used to send non-lf terminated output that has
57 	 *  been around for "too long". This will prove to be useful
58 	 *  to send the shell prompt for instance.
59 	 */
60 	struct k_work_delayable send_work;
61 	struct k_work_sync work_sync;
62 
63 	/** If set, no output is sent to the TELNET client. */
64 	bool output_lock;
65 };
66 
67 #define SHELL_TELNET_DEFINE(_name)					\
68 	static struct shell_telnet _name##_shell_telnet;		\
69 	struct shell_transport _name = {				\
70 		.api = &shell_telnet_transport_api,			\
71 		.ctx = (struct shell_telnet *)&_name##_shell_telnet	\
72 	}
73 
74 /**
75  * @brief This function provides pointer to shell telnet backend instance.
76  *
77  * Function returns pointer to the shell telnet instance. This instance can be
78  * next used with shell_execute_cmd function in order to test commands behavior.
79  *
80  * @returns Pointer to the shell instance.
81  */
82 const struct shell *shell_backend_telnet_get_ptr(void);
83 
84 #ifdef __cplusplus
85 }
86 #endif
87 
88 #endif /* SHELL_TELNET_H__ */
89