uIP, Pico]OS and telnet servers

I started working with computers when only user interface was commonly command prompt. Maybe because of that I like my devices to have one, although I don’t object having graphical user interfaces or web pages in any way. They are nice, but command prompt is cool.

Serial console is a typical example of command prompt interface. But for a device having TCP/IP networking a more flexible implementation is to use telnet protocol, which is quite simple to implement and doesn’t require much processing power. But despite of simpliness, it still requires some kind of state machine to process various control sequences that affect how things work. My Pico]OS uIP socket library includes a suitable state machine implementation with simple-to-use API calls. State machine is adapted from examples included in original uIP code.

To enable telnet support, add this to netcfg.h:

#define NETCFG_TELNETD 1

An example command interpreter could look something like this:


void shellTask(void* arg)
{
 NetTelnet tel;
 NetSock* sock = (NetSock*) arg;
 int i;
 bool go = true;
 static char buf[512];

 telnetInit(&tel, sock);
 telnetWrite(&tel, "Pico[OS " POS_VER_S "\n");

 do {

   telnetWrite(&tel, "Ready>");
   telnetFlush(&tel);

   i = telnetReadLine(&tel, buf, sizeof(buf), MS(60000));
   if (i      break;

   buf[i - 1] = '\0';

   if (!strcmp(buf, "exit"))
     go = false;
   else
     help(&tel, buf);

  } while (go);

  netSockClose(sock);
}

Function reference

void telnetInit(NetTelnet* state, NetSock* sock);

Initializes telnet state machine. Must be called before any other telnet* calls.

void telnetWrite(NetTelnet* conn, char* data)

Write data to client, escaping control characters as necessary.

void telnetFlush(NetTelnet* conn);

Flush buffered data to client immediately.

int telnetReadLine(NetTelnet* conn, char* data,
                   int max, int timeout);

Read command line from client. Timeout must be specified in Pico]OS ticks, use MS macro to express it in milliseconds.

Ari Suutari

Father of three 🙂
{ Electronics | Music | Computer | Motorbike } hobbyist.
Factory IT professional.
FreeBSD since day one.

Facebook LinkedIn