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