| Summary |
#include <net_config.h>
U16 tnet_process_cmd (
U8* cmd, /* Pointer to command string from the telnet client. */
U8* buf, /* Location where to write the return message. */
U16 buflen, /* Number of bytes in the output buffer. */
U32* pvar ); /* Pointer to a storage variable. */
|
| Description | The tnet_process_cmd function processes and executes the command requested by the telnet client. The telnet server running on TCPnet calls the tnet_process_cmd function when it receives the consecutive Carriage Return (CR) and Line Feed (LF) character sequence from the telnet client (this is usually produced by the user pressing Enter on the telnet client terminal). The argument cmd points to the message containing the command that is received from the telnet client. The argument buf points to the output buffer where the tnet_process_cmd must write the message to be returned to the telnet client. The argument buflen specifies the length of the output buffer in bytes. The argument pvar is a pointer to a variable that never gets altered by the Telnet Server. You can use *pvar as a repeat counter or simply to distinguish between different calls of the tnet_process_cmd function. The tnet_process_cmd function is part of RL-TCPnet. The prototype is defined in net_config.h. You must customize the function in telnet_uif.c. note - The length of the output buffer, buflen, might vary because buffer length is determined by the TCP socket Maximum Segment Size (MSS) negotiation. The buffer length is normally around 1400 bytes for local LAN. But this can be reduced to 500 bytes or even less.
- If the tnet_process_cmd function writes more bytes than buflen into the output buffer, then a system crash resulting from corruption of memory link pointers is highly likely.
- The telnet server does not automatically expand the CR character. The tnet_process_cmd function must write the CR and LF characters into the the buffer to indicate the end of each message.
- The argument pvar is private to each Telnet Session. The Telnet Server clears the data in the pvar pointer, to 0, before the tnet_process_cmd function is called for the first time in each session.
|
| Return Value | The tnet_process_cmd function returns the number of bytes written to the output buffer. It also encodes the values of the repeat flag and the disconnect flag into the return value. If bit 14 (repeat flag) of the return value is set to 1, the telnet server running on TCPnet calls the tnet_process_cmd function again with the argument cmd and storage variable *pvar of the same value. The function tnet_process_cmd can then enter more data into the buffer buf. If bit 15 (disconnect flag) of the return value is set to 1, the telnet server disconnects the telnet session. |
| Example |
U16 tnet_process_cmd (U8 *cmd, U8 *buf, U16 buflen, U32 *pvar) {
U32 len,val,ch;
/* Simple Command line parser */
len = strlen (cmd);
if (tnet_ccmp (cmd, "BYE") == __TRUE) {
/* 'BYE' command, send message and disconnect */
len = str_copy (buf, "\r\nDisconnect...\r\n");
/* Hi bit of return value is a disconnect flag */
return (len | 0x8000);
}
if (tnet_ccmp (cmd, "ADIN") == __TRUE) {
/* 'ADIN' command received */
if (len >= 6) {
sscanf (cmd+5,"%d",&ch);
val = AD_in (ch);
len = sprintf (buf,"\r\n ADIN %d = %d",ch,val);
return (len);
}
}
if (tnet_ccmp (cmd, "HELP") == __TRUE || tnet_ccmp (cmd, "?") == __TRUE) {
/* 'HELP' command, display help text */
len = str_copy (buf,tnet_help);
return (len);
}
/* Unknown command, display message */
len = str_copy (buf, "\r\n==> Unknown Command: ");
len += str_copy (buf+len, cmd);
return (len);
}
|