1 /*------------------------------------------------------------------------------
2 * MDK Middleware - Component ::Network:Service
3 * Copyright (c) 2004-2019 Arm Limited (or its affiliates). All rights reserved.
4 *------------------------------------------------------------------------------
5 * Name: Telnet_Server_UIF.c
6 * Purpose: Telnet Server User Interface
8 *----------------------------------------------------------------------------*/
9 //! [code_Telnet_Server_UIF]
14 // ANSI ESC Sequences for terminal control
18 extern SOCKADDR_IN remote_addr;
19 extern uint32_t rx_cnt;
20 extern uint32_t tx_cnt;
22 static const char help[] = {
25 " stat - print Server status\r\n"
26 " clear - clear RX/TX counters\r\n"
27 " cls - clear screen\r\n"
28 " help, ? - display this help\r\n"
29 " <BS> - delete Character left\r\n"
30 " <UP>,<DOWN> - recall Command History\r\n"
31 " bye,<ESC>,^C- disconnect"
34 // \brief Request a message for a Telnet server session.
35 // \param[in] msg code of requested message.
36 // \param[out] buf output buffer to write the message to.
37 // \param[in] buf_len length of the output buffer in bytes.
38 // \return number of bytes written to output buffer.
39 uint32_t netTELNETs_ProcessMessage (netTELNETs_Message msg, char *buf, uint32_t buf_len) {
43 case netTELNETs_MessageWelcome:
44 // Initial welcome message
45 len = sprintf (buf, "\r\n"
46 "*** SockServer ***\r\n"
50 case netTELNETs_MessagePrompt:
52 len = sprintf (buf, "\r\n"
62 // \brief Process and execute a command requested by the Telnet client.
63 // \param[in] cmd pointer to command string from Telnet client.
64 // \param[out] buf output buffer to write the return message to.
65 // \param[in] buf_len length of the output buffer in bytes.
66 // \param[in,out] pvar pointer to a session's local buffer of 4 bytes.
67 // - 1st call = cleared to 0,
68 // - 2nd call = not altered by the system,
69 // - 3rd call = not altered by the system, etc.
70 // \return number of bytes written to output buffer.
71 // - return len | (1u<<31) = repeat flag, the system calls this function
72 // again for the same command.
73 // - return len | (1u<<30) = disconnect flag, the system disconnects
75 uint32_t netTELNETs_ProcessCommand (const char *cmd, char *buf, uint32_t buf_len, uint32_t *pvar) {
78 // Command line parser
79 if (netTELNETs_CheckCommand (cmd, "STAT") == true) {
80 // Display SockServer status
82 // Confirm the command
84 len = (uint32_t)sprintf (buf, " Ok\r\n\n");
88 len = sprintf (buf, "\r");
89 len += sprintf (buf+len, "IP=%s, ", inet_ntoa(remote_addr.sin_addr));
90 len += sprintf (buf+len, "port=%u, ", ntohs(remote_addr.sin_port));
91 len += sprintf (buf+len, "rx=%u, ", rx_cnt);
92 len += sprintf (buf+len, "tx=%u", tx_cnt);
94 // Update interval 10 ticks (1 second)
95 netTELNETs_RepeatCommand (10);
96 return (len | (1u << 31));
99 if (netTELNETs_CheckCommand (cmd, "CLEAR") == true) {
100 // Clear system counters and IP address
103 memset (&remote_addr, 0, sizeof(remote_addr));
104 len = (uint32_t)sprintf (buf, " Ok");
108 if (netTELNETs_CheckCommand (cmd, "CLS") == true) {
109 // Clear the client screen
110 len = (uint32_t)sprintf (buf, CLS);
114 if (netTELNETs_CheckCommand (cmd, "BYE") == true) {
115 // Disconnect the client
116 len = (uint32_t)sprintf (buf, "\r\nDisconnecting\r\n");
117 // Bit-30 of return value is a disconnect flag
118 return (len | (1u << 30));
121 if (netTELNETs_CheckCommand (cmd, "HELP") == true ||
122 netTELNETs_CheckCommand (cmd, "?") == true) {
124 len = (uint32_t)sprintf (buf, help);
128 len = (uint32_t)sprintf (buf, " <- Unknown Command");
131 //! [code_Telnet_Server_UIF]