Simple TCP server.
on a command prompt, replacing x.x.x.x with the IP address of your ethernut board. Enter help for a list of available commands.
#include <cfg/os.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#ifdef ETHERNUT2
#include <dev/lanc111.h>
#else
#include <dev/nicrtl.h>
#endif
#include <dev/debug.h>
#include <sys/version.h>
#include <sys/heap.h>
#include <sys/thread.h>
#include <sys/timer.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pro/dhcp.h>
#ifdef NUTDEBUG
#include <sys/osdebug.h>
#include <net/netdebug.h>
#endif
#include <sys/confnet.h>
static char buff[128];
#if defined(__IMAGECRAFT__)
#define CC_STRING "ICCAVR"
#elif defined(__GNUC__)
#define CC_STRING "AVRGCC"
#else
#define CC_STRING "Compiler unknown"
#endif
prog_char vbanner_P[] = "\n\nTCP Server Sample - Nut/OS %s - " CC_STRING "\n";
prog_char banner_P[] = "200 Welcome to tcps. Type help to get help.\r\n";
prog_char help_P[] = "400 List of commands follows\r\n"
"m[emory]\tQueries number of RAM bytes free.\r\n"
"t[hreads]\tLists all created threads.\r\n"
"ti[mers]\tLists all running timers.\r\n" "q[uit]\t\tTerminates connection.\r\n" ".\r\n";
prog_char thread_intro_P[] = "220 List of threads with name,state,prio,stack,mem,timeout follows\r\n";
prog_char timer_intro_P[] = "221 List of timers with ticks left and interval follows\r\n";
prog_char mem_fmt_P[] = "210 %u bytes RAM free\r\n";
void ProcessRequests(FILE * stream)
{
int got;
char *cp;
fputs_P(banner_P, stream);
for (;;) {
fflush(stream);
if (fgets(buff, sizeof(buff), stream) == 0)
break;
if ((cp = strchr(buff, '\r')) != 0)
*cp = 0;
if ((cp = strchr(buff, '\n')) != 0)
*cp = 0;
got = strlen(buff);
if (got == 0)
continue;
if (strncmp(buff, "memory", got) == 0) {
fprintf_P(stream, mem_fmt_P, NutHeapAvailable());
continue;
}
if (strncmp(buff, "threads", got) == 0) {
NUTTHREADINFO *tdp;
NUTTIMERINFO *tnp;
fputs_P(thread_intro_P, stream);
for (tdp = nutThreadList; tdp; tdp = tdp->td_next) {
fputs(tdp->td_name, stream);
switch (tdp->td_state) {
case TDS_TERM:
fputs("\tTerm\t", stream);
break;
case TDS_RUNNING:
fputs("\tRun\t", stream);
break;
case TDS_READY:
fputs("\tReady\t", stream);
break;
case TDS_SLEEP:
fputs("\tSleep\t", stream);
break;
}
fprintf(stream, "%u\t%u", tdp->td_priority, (u_short) tdp->td_sp - (u_short) tdp->td_memory);
if (*((u_long *) tdp->td_memory) != DEADBEEF)
fputs("\tCorrupted\t", stream);
else
fputs("\tOK\t", stream);
if ((tnp = (NUTTIMERINFO *) tdp->td_timer) != 0)
fprintf(stream, "%lu\r\n", tnp->tn_ticks_left);
else
fputs("None\r\n", stream);
}
fputs(".\r\n", stream);
continue;
}
if (strncmp("timers", buff, got) == 0) {
NUTTIMERINFO *tnp;
fputs_P(timer_intro_P, stream);
for (tnp = nutTimerList; tnp; tnp = tnp->tn_next) {
fprintf(stream, "%lu\t", tnp->tn_ticks_left);
if (tnp->tn_ticks)
fprintf(stream, "%lu\r\n", tnp->tn_ticks);
else
fputs("Oneshot\r\n", stream);
}
fputs(".\r\n", stream);
continue;
}
if (strncmp("quit", buff, got) == 0) {
break;
}
fputs_P(help_P, stream);
}
}
int main(void)
{
TCPSOCKET *sock;
FILE *stream;
u_long baud = 115200;
u_char mac[6] = { 0x00, 0x06, 0x98, 0x00, 0x00, 0x55 };
NutRegisterDevice(&devDebug0, 0, 0);
NutRegisterDevice(&DEV_ETHER, 0x8300, 5);
freopen("uart0", "w", stdout);
_ioctl(_fileno(stdout), UART_SETSPEED, &baud);
printf_P(vbanner_P, NutVersionString());
#ifdef NUTDEBUG
NutTraceTcp(stdout, 1);
NutTraceOs(stdout, 0);
NutTraceHeap(stdout, 0);
NutTracePPP(stdout, 0);
#endif
NutNetLoadConfig("eth0");
memcpy(confnet.cdn_mac, mac, 6);
NutNetSaveConfig();
printf("Configure eth0...");
if (NutDhcpIfConfig("eth0", 0, 60000)) {
printf("initial boot...");
if (NutDhcpIfConfig("eth0", mac, 60000)) {
u_long ip_addr = inet_addr("192.168.192.100");
u_long ip_mask = inet_addr("255.255.255.0");
printf("no DHCP...");
NutNetIfConfig("eth0", mac, ip_addr, ip_mask);
}
}
puts("OK");
printf("IP: %s\n", inet_ntoa(confnet.cdn_ip_addr));
for (;;) {
if ((sock = NutTcpCreateSocket()) != 0) {
printf("Waiting for a telnet client...");
if (NutTcpAccept(sock, 23) == 0) {
puts("connected");
if ((stream = _fdopen((int) sock, "r+b")) != 0) {
ProcessRequests(stream);
puts("Disconnected");
fclose(stream);
} else
puts("Assigning a stream failed");
} else
puts("failed");
NutTcpCloseSocket(sock);
}
}
}