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
00089 #include <cfg/arch.h>
00090
00091
00092
00093
00094 #define PPPUSER "me"
00095 #define PPPPASS "secret"
00096
00097
00098
00099
00100
00101
00102 #define PPPCHAT "TIMEOUT 2 '' CLIENT\\c CLIENTSERVER"
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 #if defined(__AVR__)
00114 #define PPPDEV devAhdlc0
00115
00116 #else
00117 #warning "Works on ATmega128 only."
00118 #endif
00119 #define PPPCOM "uart0"
00120 #define PPPSPEED 115200
00121 #define PPPRXTO 1000
00122
00123
00124
00125
00126
00127 #define RXBUFFSIZE 256
00128
00129 #include <cfg/os.h>
00130 #include <dev/debug.h>
00131
00132 #include <dev/ahdlcavr.h>
00133
00134 #include <dev/ppp.h>
00135 #include <dev/chat.h>
00136
00137 #include <sys/version.h>
00138 #include <sys/heap.h>
00139 #include <sys/thread.h>
00140 #include <sys/socket.h>
00141
00142 #include <sys/timer.h>
00143
00144 #include <arpa/inet.h>
00145 #include <netdb.h>
00146 #include <net/if_var.h>
00147 #include <net/route.h>
00148
00149 #ifdef NUTDEBUG
00150 #include <net/netdebug.h>
00151 #endif
00152
00153 #include <stdlib.h>
00154 #include <string.h>
00155 #include <stdio.h>
00156 #include <io.h>
00157 #include <fcntl.h>
00158
00159 #if defined(__IMAGECRAFT__)
00160 #define CC_STRING "ICCAVR"
00161 #elif defined(__GNUC__)
00162 #define CC_STRING "AVRGCC"
00163 #else
00164 #define CC_STRING "Compiler unknown"
00165 #endif
00166
00167
00168
00169
00170 #ifdef __AVR_ENHANCED__
00171 #define DBGDEV devDebug1
00172
00173 #define DBGCOM "uart1"
00174 #define DBGSPEED 115200
00175 #endif
00176
00177 prog_char vbanner_P[] = "\n\nPPP Client Sample - Nut/OS %s - " CC_STRING "\n";
00178 prog_char banner_P[] = "200 Welcome to tcps. Type help to get help.\r\n";
00179 prog_char help_P[] = "400 List of commands follows\r\n"
00180 "m[emory]\tQueries number of RAM bytes free.\r\n"
00181 "t[hreads]\tLists all created threads.\r\n"
00182 "ti[mers]\tLists all running timers.\r\n" "q[uit]\t\tTerminates connection.\r\n" ".\r\n";
00183 prog_char thread_intro_P[] = "220 List of threads with name,state,prio,stack,mem,timeout follows\r\n";
00184 prog_char timer_intro_P[] = "221 List of timers with ticks left and interval follows\r\n";
00185 prog_char mem_fmt_P[] = "210 %lu bytes RAM free\r\n";
00186
00187
00188
00189
00190
00191 void ProcessRequests(FILE * stream)
00192 {
00193 int got;
00194 char *cp;
00195 char *buff;
00196
00197
00198
00199
00200 buff = malloc(RXBUFFSIZE);
00201
00202
00203
00204
00205 fputs_P(banner_P, stream);
00206 for (;;) {
00207
00208
00209
00210
00211 fflush(stream);
00212 if (fgets(buff, RXBUFFSIZE, stream) == 0)
00213 break;
00214
00215
00216
00217
00218 if ((cp = strchr(buff, '\r')) != 0)
00219 *cp = 0;
00220 if ((cp = strchr(buff, '\n')) != 0)
00221 *cp = 0;
00222
00223
00224
00225
00226 got = strlen(buff);
00227 if (got == 0)
00228 continue;
00229
00230
00231
00232
00233 if (strncmp(buff, "memory", got) == 0) {
00234 fprintf_P(stream, mem_fmt_P, NutHeapAvailable());
00235 continue;
00236 }
00237
00238
00239
00240
00241 if (strncmp(buff, "threads", got) == 0) {
00242 NUTTHREADINFO *tdp;
00243 NUTTIMERINFO *tnp;
00244
00245 fputs_P(thread_intro_P, stream);
00246 for (tdp = nutThreadList; tdp; tdp = tdp->td_next) {
00247 fputs(tdp->td_name, stream);
00248 switch (tdp->td_state) {
00249 case TDS_TERM:
00250 fputs("\tTerm\t", stream);
00251 break;
00252 case TDS_RUNNING:
00253 fputs("\tRun\t", stream);
00254 break;
00255 case TDS_READY:
00256 fputs("\tReady\t", stream);
00257 break;
00258 case TDS_SLEEP:
00259 fputs("\tSleep\t", stream);
00260 break;
00261 }
00262 fprintf(stream, "%u\t%u", tdp->td_priority, (u_short) tdp->td_sp - (u_short) tdp->td_memory);
00263 if (*((u_long *) tdp->td_memory) != DEADBEEF)
00264 fputs("\tCorrupted\t", stream);
00265 else
00266 fputs("\tOK\t", stream);
00267
00268 if ((tnp = (NUTTIMERINFO *) tdp->td_timer) != 0)
00269 fprintf(stream, "%lu\r\n", tnp->tn_ticks_left);
00270 else
00271 fputs("None\r\n", stream);
00272 }
00273 fputs(".\r\n", stream);
00274 continue;
00275 }
00276
00277
00278
00279
00280 if (strncmp("timers", buff, got) == 0) {
00281 NUTTIMERINFO *tnp;
00282
00283 fputs_P(timer_intro_P, stream);
00284 for (tnp = nutTimerList; tnp; tnp = tnp->tn_next) {
00285 fprintf(stream, "%lu\t", tnp->tn_ticks_left);
00286 if (tnp->tn_ticks)
00287 fprintf(stream, "%lu\r\n", tnp->tn_ticks);
00288 else
00289 fputs("Oneshot\r\n", stream);
00290 }
00291 fputs(".\r\n", stream);
00292 continue;
00293 }
00294
00295
00296
00297
00298 if (strncmp("quit", buff, got) == 0) {
00299 break;
00300 }
00301
00302
00303
00304
00305 fputs_P(help_P, stream);
00306 }
00307 }
00308
00309
00310
00311
00312 int main(void)
00313 {
00314 int pppcom;
00315 u_long lctl;
00316 #ifdef PPPDEV
00317 PPPDCB *dcb;
00318 int rc;
00319 #endif
00320
00321
00322
00323 #ifdef __AVR_ENHANCED__
00324 NutRegisterDevice(&DBGDEV, 0, 0);
00325 #endif
00326 #ifdef PPPDEV
00327 NutRegisterDevice(&PPPDEV, 0, 0);
00328 NutRegisterDevice(&devPpp, 0, 0);
00329 #endif
00330
00331
00332
00333
00334 if(freopen("uart1", "w", stdout) == 0) {
00335 for(;;);
00336 }
00337
00338
00339
00340
00341 #ifdef __AVR_ENHANCED__
00342 lctl = DBGSPEED;
00343 _ioctl(_fileno(stdout), UART_SETSPEED, &lctl);
00344 #endif
00345
00346
00347
00348
00349 printf_P(vbanner_P, NutVersionString());
00350
00351
00352
00353
00354 printf("Open uart...");
00355 if ((pppcom = _open("ppp:" PPPCOM "/" PPPUSER "/" PPPPASS, _O_RDWR | _O_BINARY)) == -1) {
00356 printf("Failed to open " PPPCOM "\n");
00357 for (;;);
00358 }
00359 puts("done");
00360
00361 #ifdef PPPDEV
00362
00363
00364
00365 lctl = PPPSPEED;
00366 _ioctl(pppcom, UART_SETSPEED, &lctl);
00367
00368
00369
00370
00371
00372 lctl = PPPRXTO;
00373 _ioctl(pppcom, UART_SETREADTIMEOUT, &lctl);
00374
00375 #ifdef NUTDEBUG
00376
00377
00378
00379 NutTracePPP(stdout, 1);
00380 #endif
00381
00382
00383
00384
00385 NutSleep(5000);
00386
00387
00388
00389
00390 for (;;) {
00391
00392
00393
00394
00395 printf("Connecting...");
00396 if ((rc = NutChat(pppcom, PPPCHAT)) != 0) {
00397 printf("no connect, reason = %d\n", rc);
00398 continue;
00399 }
00400 puts("done");
00401
00402
00403
00404
00405
00406
00407 printf("Configure PPP...");
00408 rc = NutNetIfConfig("ppp", 0, 0, 0);
00409 if (rc != 0) {
00410 puts("failed");
00411
00412
00413
00414 continue;
00415 }
00416 puts("done");
00417
00418
00419
00420
00421
00422 dcb = devPpp.dev_dcb;
00423 NutDnsConfig2(0, 0, dcb->dcb_ip_dns1, dcb->dcb_ip_dns2);
00424 NutIpRouteAdd(0, 0, dcb->dcb_remote_ip, &devPpp);
00425
00426
00427
00428
00429 printf(" Local IP: %s\n", inet_ntoa(dcb->dcb_local_ip));
00430 printf(" Remote IP: %s\n", inet_ntoa(dcb->dcb_remote_ip));
00431 printf(" Primary DNS: %s\n", inet_ntoa(dcb->dcb_ip_dns1));
00432 printf("Secondary DNS: %s\n", inet_ntoa(dcb->dcb_ip_dns2));
00433
00434
00435
00436
00437 for (;;) {
00438 TCPSOCKET *sock;
00439 FILE *stream;
00440
00441
00442
00443
00444 if ((sock = NutTcpCreateSocket()) != 0) {
00445
00446
00447
00448
00449 printf("Waiting for a client...");
00450 if (NutTcpAccept(sock, 23) == 0) {
00451 puts("connected");
00452
00453
00454
00455
00456
00457
00458 if ((stream = _fdopen((int) sock, "r+b")) != 0) {
00459
00460
00461
00462 ProcessRequests(stream);
00463 puts("\nDisconnected");
00464
00465
00466
00467
00468 fclose(stream);
00469 } else {
00470 puts("Assigning a stream failed");
00471 }
00472 } else {
00473 puts("failed");
00474 }
00475
00476
00477
00478
00479 NutTcpCloseSocket(sock);
00480 }
00481 NutSleep(1000);
00482 printf("%u bytes free\n", NutHeapAvailable());
00483 }
00484 }
00485 #endif
00486 return 0;
00487 }