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
00034
00035
00036
00053
00054 #include <dev/board.h>
00055 #include <dev/reset.h>
00056 #include <dev/gpio.h>
00057
00058
00059 #include <sys/version.h>
00060 #include <sys/confnet.h>
00061 #include <sys/heap.h>
00062 #include <sys/timer.h>
00063 #include <sys/socket.h>
00064
00065
00066 #include <arpa/inet.h>
00067 #include <net/if_var.h>
00068 #include <pro/dhcp.h>
00069
00070
00071 #include <stdlib.h>
00072 #include <stdio.h>
00073 #include <io.h>
00074 #include <string.h>
00075 #include <time.h>
00076 #include <ctype.h>
00077
00078
00079 #define APP_VERSION "2.0.0"
00080
00081
00082 #define MAX_INPUT_LINE 32
00083
00084
00085 #define TCP_SERVER_PORT 23
00086
00087 static time_t start_time;
00088
00089
00090
00091
00092 static void FatalError(char *msg)
00093 {
00094
00095 puts(msg);
00096
00097 for (;;);
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 static char *ParseLine(char *line, char **pp1, char **pp2)
00135 {
00136 char *p0;
00137 char *cp;
00138
00139
00140 *pp1 = NULL;
00141 *pp2 = NULL;
00142
00143
00144 cp = strchr(line, '\r');
00145 if (cp) {
00146 *cp = 0;
00147 }
00148 cp = strchr(line, '\n');
00149 if (cp) {
00150 *cp = 0;
00151 }
00152
00153
00154
00155
00156 p0 = line;
00157 while (isspace(*p0)) {
00158
00159 p0++;
00160 }
00161 if (*p0 == '\0') {
00162
00163 return NULL;
00164 }
00165 cp = strchr(p0, ' ');
00166 if (cp) {
00167 *cp++ = '\0';
00168 while (isspace(*cp)) {
00169
00170 cp++;
00171 }
00172 if (*cp) {
00173
00174 *pp1 = cp;
00175 cp = strchr(cp, ' ');
00176 } else {
00177 cp = NULL;
00178 }
00179 if (cp) {
00180 *cp++ = '\0';
00181 while (isspace(*cp)) {
00182
00183 cp++;
00184 }
00185 if (*cp) {
00186
00187 *pp2 = cp;
00188 }
00189 }
00190 }
00191
00192 return p0;
00193 }
00194
00195
00196
00197
00198
00199
00200
00201 static void ProcessRequests(FILE * stream)
00202 {
00203 char *buff;
00204 char *cmd;
00205 size_t clen;
00206 char *p1;
00207 char *p2;
00208
00209
00210
00211
00212 buff = malloc(MAX_INPUT_LINE + 1);
00213 if (buff == NULL) {
00214 return;
00215 }
00216
00217
00218
00219
00220 fputs("200 Welcome to tcps. Type help to get help.\r\n", stream);
00221 for (;;) {
00222
00223
00224
00225
00226
00227
00228
00229 fflush(stream);
00230 if (fgets(buff, MAX_INPUT_LINE, stream) == NULL) {
00231
00232 break;
00233 }
00234
00235 cmd = ParseLine(buff, &p1, &p2);
00236 if (cmd == NULL) {
00237
00238 continue;
00239 }
00240
00241 clen = strlen(cmd);
00242
00243
00244
00245
00246
00247
00248 if (strncmp(cmd, "heap", clen) == 0) {
00249 fprintf(stream, "210 %u bytes RAM free\r\n", (unsigned int)NutHeapAvailable());
00250 continue;
00251 }
00252
00253
00254
00255
00256 if (strncmp(cmd, "ip", clen) == 0) {
00257 uint32_t ip = p1 ? inet_addr(p1) : (uint32_t) -1;
00258
00259 if (ip == (uint32_t) -1) {
00260 fputs("420 Invalid or missing address\r\n", stream);
00261 } else {
00262 confnet.cdn_cip_addr = ip;
00263 if (NutNetSaveConfig()) {
00264 fputs("421 Failed to save configuration\r\n", stream);
00265 } else {
00266 fputs("220 Configuration saved\r\n", stream);
00267 }
00268 }
00269 continue;
00270 }
00271
00272
00273
00274
00275 if (strncmp(cmd, "mask", clen) == 0) {
00276 uint32_t mask = p1 ? inet_addr(p1) : (uint32_t) -1;
00277
00278 if (mask == (uint32_t) -1) {
00279 fputs("430 Invalid or missing mask\r\n", stream);
00280 } else {
00281 confnet.cdn_ip_mask = mask;
00282 if (NutNetSaveConfig()) {
00283 fputs("421 Failed to save configuration\r\n", stream);
00284 } else {
00285 fputs("230 Configuration saved\r\n", stream);
00286 }
00287 }
00288 continue;
00289 }
00290
00291
00292
00293
00294
00295
00296 if (strncmp(cmd, "pin", clen) == 0) {
00297 int bank = p1 ? atoi(p1) : 0;
00298 int bit = p2 ? atoi(p2) : 0;
00299 int state = GpioPinGet(bank, bit);
00300
00301 fprintf(stream, "240 %d at GPIO bank %d bit %d\r\n", state, bank, bit);
00302 continue;
00303 }
00304
00305
00306
00307
00308 if (strncmp(cmd, "send", clen) == 0) {
00309 if (p1) {
00310 printf("%s", p1);
00311 if (p1) {
00312 printf(" %s", p2);
00313 }
00314 }
00315 putchar('\n');
00316 fputs("250 Message sent\r\n", stream);
00317 continue;
00318 }
00319
00320
00321
00322
00323 if (strncmp(cmd, "uptime", clen) == 0) {
00324 fprintf(stream, "220 %ld seconds running\r\n", (long)(time(NULL) - start_time));
00325 continue;
00326 }
00327
00328
00329
00330
00331
00332
00333 if (strncmp(cmd, "reset", clen) == 0) {
00334 fputs("910 System reset\r\n", stream);
00335 fflush(stream);
00336 NutSleep(1000);
00337 NutReset();
00338 fputs("490 System reset not implemented\r\n", stream);
00339 continue;
00340 }
00341
00342
00343
00344
00345 if (strncmp(cmd, "quit", clen) == 0) {
00346 fputs("900 Bye\r\n", stream);
00347 fflush(stream);
00348 break;
00349 }
00350
00351
00352
00353
00354 fputs("400 List of commands follows\r\n"
00355 "h[eap] Query heap memory bytes available.\r\n"
00356 "i[p] Set IP <address>.\r\n"
00357 "m[ask] Set IP <mask>.\r\n"
00358 "p[in] Query status of GPIO pin <bank> <bit>.\r\n"
00359 "r[eset] Reset system.\r\n"
00360 "s[end] Send <message> to serial port.\r\n"
00361 "u[ptime] Query number of seconds the system is running.\r\n"
00362 "q[uit] Terminates connection.\r\n"
00363 ".\r\n", stream);
00364 }
00365 free(buff);
00366 }
00367
00368
00369
00370
00371
00372
00373 int main(void)
00374 {
00375 TCPSOCKET *sock;
00376 FILE *stream;
00377 uint32_t baud = 115200;
00378
00379
00380
00381
00382 NutRegisterDevice(&DEV_DEBUG, 0, 0);
00383 freopen(DEV_DEBUG_NAME, "w", stdout);
00384 _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00385
00386
00387
00388
00389 printf("\n\nNut/OS %s\n", NutVersionString());
00390 printf("TCP Server Sample %s\n", APP_VERSION);
00391
00392
00393
00394
00395
00396
00397
00398
00399 printf("Configure %s...", DEV_ETHER_NAME);
00400 if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
00401 FatalError("failed");
00402 }
00403 if (NutDhcpIfConfig("eth0", 0, 60000)) {
00404 FatalError("no valid network configuration");
00405 }
00406 printf("OK\nRun 'telnet %s", inet_ntoa(confnet.cdn_ip_addr));
00407 #if TCP_SERVER_PORT != 23
00408 printf(" %d", TCP_SERVER_PORT);
00409 #endif
00410 puts("' to connect to this server");
00411
00412
00413 start_time = time(NULL);
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 for (;;) {
00425
00426 if ((sock = NutTcpCreateSocket()) != 0) {
00427 printf("Waiting for a telnet client...");
00428
00429 if (NutTcpAccept(sock, TCP_SERVER_PORT) == 0) {
00430 puts("connected");
00431
00432
00433
00434
00435
00436
00437 stream = _fdopen((int) sock, "r+b");
00438 if (stream) {
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452 ProcessRequests(stream);
00453
00454 fclose(stream);
00455 } else {
00456 puts("Assigning a stream failed");
00457 }
00458 } else {
00459 puts("failed");
00460 }
00461
00462
00463 NutTcpCloseSocket(sock);
00464 puts("Disconnected");
00465 }
00466 }
00467
00468 return 0;
00469 }