Network Dual-Stack Component  Version 7.0 (Beta)
MDK-Professional Middleware for IPv4 and IPv6 Networking
 All Data Structures Files Functions Enumerations Groups Pages
User Callbacks

Functions to notify the user application about events on the Telnet server. More...

Functions to notify the user application about events on the Telnet server.

The netTELNETs_ProcessCommand function processes the Telnet command when it is received from a remote client. A command is any sequence of characters that is terminated by the CRLF sequence (the Enter key is pressed). The Telnet server assembles this command and passes it as an argument to the netTELNETs_ProcessCommand function. This function then generates a reply message and sends it back to the user. It is part of the Telnet_Server_UIF.c template file.

When the reply message is short, the whole message can be sent in a single TCP packet. However, when long reports are generated, multiple TCP packets must be sent to transfer the whole message. For example, when the log files are displayed this is often the case. Both single and multiple packets are supported by the Embedded Telnet Server.

Short Reply

In the following example, the Telnet command HELP is sent by the Telnet client:

cmd> HELP

This command is answered by the predefined help message telnet_help. This message is copied to the output buffer and sent to the remote Telnet client. The following code sends the reply message:

uint32_t netTELNETs_ProcessCommand (const char *cmd, char *buf, uint32_t buf_len, uint32_t *pvar) {
..
if (netTELNETs_CheckCommand (cmd, "HELP") == true) {
// 'HELP' command, print help text
len = sprintf (buf, telnet_help);
return (len);
}
..
}

Long Reply

A long reply message requires multiple calls to the function netTELNETs_ProcessCommand. Each call to this function generates part of the reply message until the entire message is generated and sent. To distinguish between different calls to the function, the argument pvar is used. This argument is a pointer to a variable that is set to 0 on the first call and not altered on each subsequent call to this function. The function's return value, which specifies the number of bytes in the reply message, cannot exceed 1500. Hence the high bits of the function's return value are used to store the flags:

In the following example, the MEAS command is given by the user using the Telnet client.

cmd> MEAS

When a new Telnet command is received, the function netTELNETs_ProcessCommand is called with the argument pvar set to 0. The command buffer cmd is checked to identify the command.

uint32_t netTELNETs_ProcessCommand (const char *cmd, char *buf, uint32_t buf_len, uint32_t *pvar) {
uint32_t len = 0;
switch (MYBUF(pvar)->id) {
case 0:
// First call to this function
break;
case 1:
// Command MEAS, repeated call
..
// Set request for another callback
return (len | (1u << 31));
..
}
if (netTELNETs_CheckCommand (cmd, "MEAS") == true) {
// MEAS command given, monitor analog inputs
MYBUF(pvar)->id = 1;
if (len > 5) {
uint32_t nmax;
sscanf (&cmd[5], "%u", &nmax);
if (nmax > 255) nmax = 255;
MYBUF(pvar)->nmax = nmax;
}
len = sprintf (buf, meas_header);
if (MYBUF(pvar)->nmax) {
// Bit-31 is a repeat flag
len |= (1u << 31);
}
return (len);
}
..
}

The above example uses 3 bytes of a storage variable pointed by pvar pointer for the following structure:

typedef struct {
uint8_t id;
uint8_t nmax;
uint8_t idx;
} MY_BUF;
#define MYBUF(p) ((MY_BUF *)p)

When the call to netTELNETs_ProcessCommand is repeated for the same command, the value of a storage variable pointed to by argument pvar is not altered any more. You can use the value of pvar to process the command differently. The pvar buffer now holds the private structure MYBUF, which is valid for the lifetime of processing the command. When the command processing is finished, this buffer is not used any more until the next command.

uint32_t netTELNETs_ProcessCommand (const char *cmd, char *buf, uint32_t buf_len, uint32_t *pvar) {
uint32_t len = 0;
switch (MYBUF(pvar)->id) {
case 0:
// First call to this function
break;
case 1:
// Command MEAS, repeated call
while (len < buf_len-80) {
// Let's use as much of the buffer as possible
len += sprintf (buf+len, "\r\n%4d", MYBUF(pvar)->idx);
for (val = 0; val < 8; val++) {
len += sprintf (buf+len, "%7d", AD_in(val));
}
if (++MYBUF(pvar)->idx >= MYBUF(pvar)->nmax) {
// Requested number of measurements done
return (len);
}
}
// Set request for another callback
return (len | (1u << 31));
case 2:
// Repeated call, TCP status display
..
}

After giving a meas 4 command, the Telnet Client screen looks like this:

telnet_meas.png
Note
You can check the Telnet Server to see how it works.