]> begriffs open source - cmsis-driver-validation/blob - Tools/SockServer/Source/Telnet_Server_UIF.c
- Minor spelling corrections in Abstract.txt file of example for STMicroelectronics...
[cmsis-driver-validation] / Tools / SockServer / Source / Telnet_Server_UIF.c
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
7  * Rev.:    V7.0.0
8  *----------------------------------------------------------------------------*/
9 //! [code_Telnet_Server_UIF]
10 #include <stdio.h>
11 #include <string.h>
12 #include "rl_net.h"
13
14 // ANSI ESC Sequences for terminal control
15 #define CLS     "\033[2J"
16
17 // Global variables
18 extern SOCKADDR_IN remote_addr;
19 extern uint32_t rx_cnt;
20 extern uint32_t tx_cnt;
21
22 static const char help[] = {
23   "\r\n"
24   "Commands:\r\n"
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"
32 };
33
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) {
40   uint32_t len = 0;
41  
42   switch (msg) {
43     case netTELNETs_MessageWelcome:
44       // Initial welcome message
45       len = sprintf (buf, "\r\n"
46                           "*** SockServer ***\r\n"
47                           "%s",help);
48       break;
49  
50     case netTELNETs_MessagePrompt:
51       // Prompt message
52       len = sprintf (buf, "\r\n"
53                           "Cmd> ");
54       break;
55
56     default:
57       break;
58   }
59   return (len);
60 }
61  
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
74 //                                          the Telnet client.
75 uint32_t netTELNETs_ProcessCommand (const char *cmd, char *buf, uint32_t buf_len, uint32_t *pvar) {
76   uint32_t len = 0;
77
78   // Command line parser
79   if (netTELNETs_CheckCommand (cmd, "STAT") == true) {
80     // Display SockServer status
81     if (*pvar == 0) {
82       // Confirm the command
83       *pvar = 1;
84       len = (uint32_t)sprintf (buf, " Ok\r\n\n");
85     }
86     else {
87       // Update status
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);
93     }
94     // Update interval 10 ticks (1 second)
95     netTELNETs_RepeatCommand (10);
96     return (len | (1u << 31));
97   }
98
99   if (netTELNETs_CheckCommand (cmd, "CLEAR") == true) {
100     // Clear system counters and IP address
101     rx_cnt = 0;
102     tx_cnt = 0;
103     memset (&remote_addr, 0, sizeof(remote_addr));
104     len = (uint32_t)sprintf (buf, " Ok");
105     return (len);
106   }
107
108   if (netTELNETs_CheckCommand (cmd, "CLS") == true) {
109     // Clear the client screen
110     len = (uint32_t)sprintf (buf, CLS);
111     return (len);
112   }
113
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));
119   }
120
121   if (netTELNETs_CheckCommand (cmd, "HELP") == true ||
122       netTELNETs_CheckCommand (cmd, "?") == true) {
123     // Display help text
124     len = (uint32_t)sprintf (buf, help);
125     return (len);
126   }
127   // Unknown command
128   len = (uint32_t)sprintf (buf, " <- Unknown Command");
129   return (len);
130 }
131 //! [code_Telnet_Server_UIF]