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