RealtimeCompLab2
Dependencies: mbed
Fork of PPP-Blinky by
Diff: main.cpp
- Revision:
- 50:ad4e7c3c88e5
- Parent:
- 49:2213f9c132b2
- Child:
- 51:a86d56844324
- Child:
- 52:accc3026b2b0
--- a/main.cpp Tue Jan 24 19:34:28 2017 +0000 +++ b/main.cpp Sun Jan 29 00:58:03 2017 +0000 @@ -54,6 +54,8 @@ char frbuf[2000]; // send/receive buffer for ppp frames // a structure to keep all our ppp globals in + + struct pppType { int online; // we hunt for a PPP connection if this is zero int ident; // our IP ident value @@ -70,6 +72,11 @@ int crc; // PPP CRC (frame check) char * buf; // the actual buffer } pkt; // ppp buffer objects + struct { + int frameStartIndex; // frame start marker + int frameEndIndex; // frame end marker + int frameBusy; // busy capturing a frame + } hdlc; // hdlc frame objects }; pppType ppp; // our global - definitely not thread safe @@ -86,6 +93,7 @@ ppp.ident=0; ppp.seq=1000; ppp.ledState=0; + ppp.hdlc.frameBusy=0; } void led1Toggle() @@ -143,7 +151,7 @@ } else return -1; } -void processFrame(int start, int end) // process received frame +void processHDLCFrame(int start, int end) // process received frame { led1Toggle(); // change led1 state on every frame we receive if(start==end) { @@ -539,7 +547,7 @@ if ( strncmp(dataStart, "GET / HTTP/1.1", 14) == 0) { dataLen = 32*32; // this block has to hold the web page below, but keep it under 1024 or increase this memset(dataStart,'x', dataLen ); // initialize the data block - + int n=0; // number of bytes we have printed so far n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: PPP-Blinky\r\n"); // http header n=n+sprintf(n+dataStart,"Content-Length: "); // http header @@ -550,17 +558,17 @@ n=n+sprintf(n+dataStart,"<!DOCTYPE html>\n<html><head><title>mbed-PPP-Blinky</title>\n<script>window.onload=function()"); // html start n=n+sprintf(n+dataStart,"{setInterval(function(){function x(){return document.getElementById('w');};\n"); // html - n=n+sprintf(n+dataStart,"x().textContent = parseInt(x().textContent)+1;},100);};</script></head>\n"); // html + n=n+sprintf(n+dataStart,"x().textContent = parseInt(x().textContent)+1;},100);};</script></head>\n"); // html n=n+sprintf(n+dataStart,"<body style=\"font-family: sans-serif; font-size:30px; color:#807070\">"); // html n=n+sprintf(n+dataStart,"<h1>mbed PPP-Blinky Up and Running</h1>\n<h1 id=\"w\" style=\"text-align:"); // html n=n+sprintf(n+dataStart," center;\">0</h1><h1><a href=\"http://bit.ly/pppBlink2\">Source on mbed</a></h1>\n</body></html>\r\n"); // html end - + int contentLength = n-nHeader; // Content-Length is the count of every character after the header int cSize = 5; // maximum number of digits we allow for Content-Length char contentLengthString[cSize+1]; // temporary buffer to create Content-Length string snprintf(contentLengthString,cSize+1,"%*d",cSize,contentLength); // print Content-Length with leading spaces and fixed width = csize memcpy(dataStart+contentLengthStart, contentLengthString, cSize); // copy Content-Length to the send buffer - + if (v0) { debug("HTTP GET BufferSize %d*32=%d Header %d Content-Length %d Total %d Available %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n); } @@ -568,7 +576,7 @@ } else if ( strncmp(dataStart, "GET /", 4) == 0) { // all other HTTP GET requests get 404 Not Found response dataLen = 32*32; // maximum size for File not found webpage memset(dataStart,'x', dataLen ); // initialize the data block - + int n=0; // number of bytes we have printed so far n=n+sprintf(n+dataStart,"HTTP/1.1 404 Not Found\r\nServer: PPP-Blinky\r\n"); // http header n=n+sprintf(n+dataStart,"Content-Length: "); // http header @@ -841,6 +849,31 @@ } } +void wait_for_HDLC_frame() +{ + + while ( rxbufNotEmpty() ) { + int rx = pc_getBuf(); + if (ppp.hdlc.frameBusy) { + if (rx==FRAME_7E) { + ppp.hdlc.frameBusy=0; // done gathering frame + if (ppp.rx.tail == 0) { // did we just wrap around? + ppp.hdlc.frameEndIndex=BUFLEN-1; // wrap back to end of buffer + } + else { + ppp.hdlc.frameEndIndex=ppp.rx.tail-1; // remember where frame ends + } + processHDLCFrame(ppp.hdlc.frameStartIndex, ppp.hdlc.frameEndIndex); + } + } else { + if (rx==FRAME_7E) { + ppp.hdlc.frameBusy=1; // start gathering frame + ppp.hdlc.frameStartIndex=ppp.rx.tail; // remember where frame started + } + } + } +} + void scanForConnectString() { if ( ppp.online==0 ) { @@ -862,28 +895,9 @@ xx.puts("\x1b[2J\x1b[HReady\n"); // VT100 code for clear screen & home #endif pppInitStruct(); // initialize all the PPP properties - pc.attach(&rxHandler,Serial::RxIrq); // start the receive handler - - int frameStartIndex, frameEndIndex; - int frameBusy=0; - while(1) { - if ( ppp.online==0 ) scanForConnectString(); // try to connect - while ( rxbufNotEmpty() ) { - int rx = pc_getBuf(); - if (frameBusy) { - if (rx==FRAME_7E) { - frameBusy=0; // done gathering frame - frameEndIndex=ppp.rx.tail-1; // remember where frame ends - processFrame(frameStartIndex, frameEndIndex); - } - } else { - if (rx==FRAME_7E) { - frameBusy=1; // start gathering frame - frameStartIndex=ppp.rx.tail; // remember where frame started - } - } - } + if ( ppp.online==0 ) scanForConnectString(); // respond to connect string from PC + wait_for_HDLC_frame(); } }