Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of webserverBlinky by
Diff: main.cpp
- Revision:
- 106:d14e6b597ca3
- Parent:
- 105:45001195b325
- Child:
- 107:5fe806713d49
diff -r 45001195b325 -r d14e6b597ca3 main.cpp --- a/main.cpp Mon Jul 31 15:19:16 2017 +0000 +++ b/main.cpp Thu Aug 03 11:14:28 2017 +0000 @@ -30,6 +30,7 @@ // See also https://en.wikipedia.org/wiki/Point-to-Point_Protocol_daemon // Ok, enough talking, time to check out some code!! + #include "mbed.h" // The #define below enables/disables a second (OPTIONAL) serial port that prints out interesting diagnostic messages. @@ -118,7 +119,6 @@ struct { int frameStartIndex; // frame start marker int frameEndIndex; // frame end marker - int frameFound; // we have found at least one start of frame already } hdlc; // hdlc frame objects }; @@ -136,7 +136,6 @@ ppp.pkt.len=0; ppp.ident=1000; ppp.ledState=0; - ppp.hdlc.frameFound=0; ppp.hdlc.frameStartIndex=0; ppp.httpPageCount=0; } @@ -274,15 +273,12 @@ { debug("Their IPCP Config Req, Our Ack\n"); ppp.pkt.buf[4]=2; // change code to ack - send_pppFrame(); // simply acknowledge everything they ask for + send_pppFrame(); // acknowledge everything they ask for - assume it's an IP address + debug("Our IPCP Ask\n"); ppp.pkt.buf[4]=1; // change code to request - if (ppp.pkt.buf[8]==3) { // check if this is an ip address request - ppp.pkt.buf[13]++; // choose next ip address - } else { // not an IP request, respond with empty request - ppp.pkt.len=10; // shortest ipcp packet possible (4 ppp + 4 ipcp + 2 crc) - ppp.pkt.buf[7]=4; // no options in this request - } + ppp.pkt.buf[7]=4; // no options in this request + ppp.pkt.len=10; // no options in this request shortest ipcp packet possible (4 ppp + 4 ipcp + 2 crc) send_pppFrame(); // send our request } @@ -291,9 +287,19 @@ debug("Their IPCP Grant\n"); } + + + + + + void ipcpNackHandler() { - debug("Their IPCP Nack\n"); + debug("Their IPCP Nack, Our ACK\n"); + if (ppp.pkt.buf[8]=3) { // check if the NACK contains an IP address parameter + ppp.pkt.buf[4]=1; // assume the NACK contains our "suggested" IP address + send_pppFrame(); // let's request this IP address as ours + } // if it's not an IP nack we ignore it } void ipcpDefaultHandler() @@ -573,12 +579,12 @@ memcpy(n+dataStart,rootWebPage,sizeof(rootWebPage)); n = n + sizeof(rootWebPage); } else { - if (xFetch == 0) { + if (xFetch == 0) { // the request started with "GET /x" #define W3C_COMPLIANT_RESPONSE_NO // change the above to W3C_COMPLIANT_RESPONSE_YES if you want a W3C.org compliant HTTP response #ifdef W3C_COMPLIANT_RESPONSE_YES - n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed-ppp-blinky</title>"); // html title (W3C.org required element) + n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed-ppp-blinky</title>"); // html title (W3C.org required elements) n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.httpPageCount); // body = the http frame count #else #define BENCHMARK_USING_BROWSER_NO /* set to _YES if you want to use your browser as a benchmark tool of http fetches */ @@ -690,9 +696,7 @@ int doFin = 0; // flag to see if we have to send an extra FIN message to shut down the link - // A sparse TCP flag interpreter that implements simple TCP connections from a single source - // Clients are allowed ONE push packet, after which the link is closed with a FIN flag in the ACK packet - // This strategy allows web browsers, netcat and curl to work ok while keeping the state machine simple + // A sparse TCP flag interpreter that implements stateless TCP connections switch ( flagsTCP ) { case TCP_FLAG_SYN: @@ -705,7 +709,7 @@ flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // set the FIN flag to start closing this TCP connection dataLen = httpResponse(tcpDataOut); // send an http response } else { - dataLen = tcpResponse(tcpDataOut,tcpDataSize); // not a web request, just report number of received bytes + dataLen = tcpResponse(tcpDataOut,tcpDataSize); // not a web request, send a packet reporting number of received bytes } break; case TCP_FLAG_FIN | TCP_FLAG_PSH | TCP_FLAG_ACK: @@ -733,6 +737,7 @@ memcpy(dsttcp, tempHold,2); // swap ip port source/dest sendTCP: + acktcp[0]=ack_out>>24; acktcp[1]=ack_out>>16; acktcp[2]=ack_out>>8; @@ -750,7 +755,7 @@ ident[0] = ppp.ident>>8; ident[1] = ppp.ident>>0; // insert OUR ident - // Now we recalculate all the header sizes, and do the IP and TCP checksums + // Now we recalculate all the header sizes int newPacketSize = headerSizeIP + headerSizeTCP + dataLen; // calculate size of the outgoing packet pktLen[0] = (newPacketSize>>8); pktLen[1]=newPacketSize; // ip total packet size @@ -943,17 +948,11 @@ int oldTail = ppp.rx.tail; // remember where the character is located in the buffer int rx = pc_getBuf(); // get the character if (rx==FRAME_7E) { - if (ppp.hdlc.frameFound == 0) { // we are still waiting for a frame start - ppp.hdlc.frameFound = 1; // we found our first frame start - ppp.hdlc.frameStartIndex=ppp.rx.tail; // remember where the frame character is in the buffer - } else { - // we have previously found a frame start - ppp.hdlc.frameEndIndex=oldTail; // mark the frame end character - processHDLCFrame(ppp.hdlc.frameStartIndex, ppp.hdlc.frameEndIndex); // process the frame - ppp.rx.rtail = ppp.rx.tail; - ppp.hdlc.frameStartIndex = ppp.rx.tail; // where next frame will start - break; - } + ppp.hdlc.frameEndIndex=oldTail; // mark the frame end character + processHDLCFrame(ppp.hdlc.frameStartIndex, ppp.hdlc.frameEndIndex); // process the frame + ppp.rx.rtail = ppp.rx.tail; + ppp.hdlc.frameStartIndex = ppp.rx.tail; // where next frame will start + break; } } } @@ -964,14 +963,15 @@ while(ppp.online == 0) { fillbuf(); // search for Windows Dialup Networking "Direct Connection Between Two Computers" expected connect string - char * found1 = strstr( (char *)ppp.rx.buf, "CLIENTCLIENT" ); + char * found1 = strstr( (char *)ppp.rx.buf, "CLIENT" ); // also search for HDLC frame start character 0x7e void * found2 = memchr( (char *)ppp.rx.buf, 0x7e, RXBUFLEN ); if( (found1 != NULL) | (found2 != NULL) ) { if (found1 != NULL) { // respond with Windows Dialup networking expected "Direct Connection Between Two Computers" response string + led1Toggle(); pc.puts("CLIENTSERVER"); - if (v0) debug("Found connect string \"CLIENTCLIENT\"\n"); + if (v0) debug("Found connect string \"CLIENT\"\n"); } if (found2 != NULL) { if (v0) debug("Found HDLC frame start (7E)\n");