00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00086 #include <cfg/os.h>
00087 #include <string.h>
00088 #include <stdio.h>
00089 #include <io.h>
00090
00091 #include <dev/board.h>
00092
00093 #include <sys/version.h>
00094 #include <sys/heap.h>
00095 #include <sys/thread.h>
00096 #include <sys/timer.h>
00097 #include <sys/socket.h>
00098
00099 #include <arpa/inet.h>
00100 #include <pro/dhcp.h>
00101
00102 #ifdef NUTDEBUG
00103 #include <sys/osdebug.h>
00104 #include <net/netdebug.h>
00105 #endif
00106
00107 #include <sys/confnet.h>
00108
00109 static char buff[128];
00110
00111
00112
00113
00114
00115
00116
00117 #if defined(__IMAGECRAFT__)
00118 #define CC_STRING "ICCAVR"
00119 #elif defined(__GNUC__)
00120 #define CC_STRING "AVRGCC"
00121 #else
00122 #define CC_STRING "Compiler unknown"
00123 #endif
00124
00125 prog_char vbanner_P[] = "\n\nTCP Server Sample - Nut/OS %s - " CC_STRING "\n";
00126 prog_char banner_P[] = "200 Welcome to tcps. Type help to get help.\r\n";
00127 prog_char help_P[] = "400 List of commands follows\r\n"
00128 "m[emory]\tQueries number of RAM bytes free.\r\n"
00129 "t[hreads]\tLists all created threads.\r\n"
00130 "ti[mers]\tLists all running timers.\r\n" "q[uit]\t\tTerminates connection.\r\n" ".\r\n";
00131 prog_char thread_intro_P[] = "220 List of threads with name,state,prio,stack,mem,timeout follows\r\n";
00132 prog_char timer_intro_P[] = "221 List of timers with ticks left and interval follows\r\n";
00133 prog_char mem_fmt_P[] = "210 %u bytes RAM free\r\n";
00134
00135
00136
00137
00138 void ProcessRequests(FILE * stream)
00139 {
00140 int got;
00141 char *cp;
00142
00143
00144
00145
00146 fputs_P(banner_P, stream);
00147 for (;;) {
00148
00149
00150
00151
00152 fflush(stream);
00153 if (fgets(buff, sizeof(buff), stream) == 0)
00154 break;
00155
00156
00157
00158
00159 if ((cp = strchr(buff, '\r')) != 0)
00160 *cp = 0;
00161 if ((cp = strchr(buff, '\n')) != 0)
00162 *cp = 0;
00163
00164
00165
00166
00167 got = strlen(buff);
00168 if (got == 0)
00169 continue;
00170
00171
00172
00173
00174 if (strncmp(buff, "memory", got) == 0) {
00175 fprintf_P(stream, mem_fmt_P, (u_int)NutHeapAvailable());
00176 continue;
00177 }
00178
00179
00180
00181
00182 if (strncmp(buff, "threads", got) == 0) {
00183 NUTTHREADINFO *tdp;
00184 NUTTIMERINFO *tnp;
00185
00186 fputs_P(thread_intro_P, stream);
00187 for (tdp = nutThreadList; tdp; tdp = tdp->td_next) {
00188 fputs(tdp->td_name, stream);
00189 switch (tdp->td_state) {
00190 case TDS_TERM:
00191 fputs("\tTerm\t", stream);
00192 break;
00193 case TDS_RUNNING:
00194 fputs("\tRun\t", stream);
00195 break;
00196 case TDS_READY:
00197 fputs("\tReady\t", stream);
00198 break;
00199 case TDS_SLEEP:
00200 fputs("\tSleep\t", stream);
00201 break;
00202 }
00203 fprintf(stream, "%u\t%u", tdp->td_priority, (u_int) tdp->td_sp - (u_int) tdp->td_memory);
00204 if (*((u_long *) tdp->td_memory) != DEADBEEF)
00205 fputs("\tCorrupted\t", stream);
00206 else
00207 fputs("\tOK\t", stream);
00208
00209 if ((tnp = (NUTTIMERINFO *) tdp->td_timer) != 0)
00210 fprintf(stream, "%lu\r\n", tnp->tn_ticks_left);
00211 else
00212 fputs("None\r\n", stream);
00213 }
00214 fputs(".\r\n", stream);
00215 continue;
00216 }
00217
00218
00219
00220
00221 if (strncmp("timers", buff, got) == 0) {
00222 NUTTIMERINFO *tnp;
00223
00224 fputs_P(timer_intro_P, stream);
00225 for (tnp = nutTimerList; tnp; tnp = tnp->tn_next) {
00226 fprintf(stream, "%lu\t", tnp->tn_ticks_left);
00227 if (tnp->tn_ticks)
00228 fprintf(stream, "%lu\r\n", tnp->tn_ticks);
00229 else
00230 fputs("Oneshot\r\n", stream);
00231 }
00232 fputs(".\r\n", stream);
00233 continue;
00234 }
00235
00236
00237
00238
00239 if (strncmp("quit", buff, got) == 0) {
00240 break;
00241 }
00242
00243
00244
00245
00246 fputs_P(help_P, stream);
00247 }
00248 }
00249
00250
00251
00252
00253
00254
00255 int main(void)
00256 {
00257 TCPSOCKET *sock;
00258 FILE *stream;
00259 u_long baud = 115200;
00260 u_char mac[6] = { 0x00, 0x06, 0x98, 0x00, 0x00, 0x55 };
00261
00262
00263
00264
00265 NutRegisterDevice(&DEV_DEBUG, 0, 0);
00266 NutRegisterDevice(&DEV_ETHER, 0x8300, 5);
00267
00268
00269
00270
00271 freopen(DEV_DEBUG_NAME, "w", stdout);
00272 _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00273 printf_P(vbanner_P, NutVersionString());
00274 #ifdef NUTDEBUG
00275 NutTraceTcp(stdout, 1);
00276 NutTraceOs(stdout, 0);
00277 NutTraceHeap(stdout, 0);
00278 NutTracePPP(stdout, 0);
00279 #endif
00280
00281 NutNetLoadConfig(DEV_ETHER_NAME);
00282 memcpy(confnet.cdn_mac, mac, 6);
00283 NutNetSaveConfig();
00284
00285
00286
00287
00288
00289
00290 printf("Configure eth0...");
00291 if (NutDhcpIfConfig("eth0", 0, 60000)) {
00292 printf("initial boot...");
00293 if (NutDhcpIfConfig("eth0", mac, 60000)) {
00294 u_long ip_addr = inet_addr("192.168.192.100");
00295 u_long ip_mask = inet_addr("255.255.255.0");
00296
00297 printf("no DHCP...");
00298 NutNetIfConfig("eth0", mac, ip_addr, ip_mask);
00299
00300
00301 }
00302 }
00303 puts("OK");
00304 printf("IP: %s\n", inet_ntoa(confnet.cdn_ip_addr));
00305
00306
00307
00308
00309 for (;;) {
00310
00311
00312
00313 if ((sock = NutTcpCreateSocket()) != 0) {
00314
00315
00316
00317 printf("Waiting for a telnet client...");
00318 if (NutTcpAccept(sock, 23) == 0) {
00319 puts("connected");
00320
00321
00322
00323
00324
00325
00326 if ((stream = _fdopen((int) sock, "r+b")) != 0) {
00327
00328
00329
00330 ProcessRequests(stream);
00331 puts("Disconnected");
00332
00333
00334
00335
00336 fclose(stream);
00337 } else
00338 puts("Assigning a stream failed");
00339 } else
00340 puts("failed");
00341
00342
00343
00344
00345 NutTcpCloseSocket(sock);
00346 }
00347 }
00348 return 0;
00349 }