Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
66:f005b9fdf4d1
Parent:
65:23b17c43aa0f
Child:
67:a63e3486bcda
diff -r 23b17c43aa0f -r f005b9fdf4d1 main.cpp
--- a/main.cpp	Sun Jun 04 21:26:17 2017 +0000
+++ b/main.cpp	Mon Jun 05 15:34:53 2017 +0000
@@ -26,35 +26,58 @@
 // https://technet.microsoft.com/en-us/sysinternals/pstools.aspx - psping for fast testing of ICMP ping function
 // https://eternallybored.org/misc/netcat/ - use netcat -u 172.10.10.1 80 to send/receive UDP packets from PPP-Blinky
 
-// The #define below enables/disables a second serial port that prints out interesting diagnostic messages.
-// change to SERIAL_PORT_MONITOR_YES to enable diagnistics messages. You will also need a second serial port.
+
+// The #define below enables/disables a SECOND (optional) serial port that prints out interesting diagnostic messages.
+// Change to SERIAL_PORT_MONITOR_YES to enable diagnostics messages. You need to wire a second serial port to your mbed hardware to monitor this.
 #define SERIAL_PORT_MONITOR_NO /* or SERIAL_PORT_MONITOR_YES */
 
 #ifndef SERIAL_PORT_MONITOR_NO
-Serial xx(PC_10, PC_11); // Not needed to run, if you get compile error here change #define SERIAL_PORT_MONITOR_YES to #define SERIAL_PORT_MONITOR_NO
+Serial xx(PC_10, PC_11); // Not required to run, if you get compile error here, change #define SERIAL_PORT_MONITOR_YES to #define SERIAL_PORT_MONITOR_NO
 #define debug(x...) xx.printf (x)
 #else
 #define debug(x...) {}
 #endif
 
-Serial pc(USBTX, USBRX); // The serial port on your mbed hardware. Your PC thinks this serial port is a dial-up modem.
+// verbosity flag used in debug printouts - change to 0 to see less debug info. Lots of interesting info.
+#define v0 1
+// verbosity flag used in debug printouts - change to 0 to see less debug info. Lots of interesting info.
+#define v1 1
 
-int v0=1;
-int v1=1; // verbosity flags used in debug printouts - change to 1/0 to see more/less debug info
+// this is the webpage we serve when we get an HTTP request
+// keep size under 900 bytes to fit in a single frame
+const static char ourWebPage[] = "\
+<!DOCTYPE html>\
+<html>\
+<head>\
+<title>mbed-PPP-Blinky</title>\
+<script>\
+window.onload=function(){\
+setInterval(function(){function x(){return document.getElementById('w');};\
+x().textContent = parseInt(x().textContent)+1;},100);};\
+</script>\
+</head>\
+<body style=\"font-family: sans-serif; font-size:30px; color:#807070\">\
+<h1>mbed PPP-Blinky Up and Running</h1>\
+<h1 id=\"w\" style=\"text-align:center;\">0</h1>\
+<h1><a href=\"http://bit.ly/pppBlink2\">Source on mbed</a></h1>\
+</body>\
+</html>"; // current size is approximately 500 characters
+
+// The serial port on your mbed hardware. Your PC should view this as a standard dial-up networking modem. See instructions at the top.
+// On a typical mbed hardware platform this is a USB virtual com port (VCP)
+Serial pc(USBTX, USBRX); // usb virtual com port
 
 DigitalOut led1(LED1); // this led toggles when a packet is received
 
-// the standard hdlc frame start/end character
+// the standard hdlc frame start/end character. It's the tilde character "~"
 #define FRAME_7E (0x7e)
 
 // the serial port receive buffer and packet buffer
 #define BUFLEN (1<<12)
-char rxbufppp[BUFLEN]; // BUFLEN must be a power of two because we use & operator for fast wrap-around in rxHandler
-char hdlcBuffer[2000]; // send/receive buffer for unstuffed (decoded) hdlc frame
+char rxbufppp[BUFLEN]; // BUFLEN MUST be a power of two because we use & operator for fast wrap-around in rxHandler
+char hdlcBuffer[2000]; // send and receive buffer for unstuffed (decoded) hdlc 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
@@ -169,7 +192,7 @@
                 dest++;
                 crcDo(rxbufppp[idx]);
             }
-        } else { // unstuff
+        } else { // unstuff characters prefixed with 0x7d
             *dest = rxbufppp[idx]^0x20;
             ppp.pkt.len++;
             dest++;
@@ -184,7 +207,7 @@
         void determinePacketType(); // declaration only
         determinePacketType();
     } else if (v0) {
-        debug("PPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len);
+        debug("PPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len); // ignore packets with CRC errors but print a debug line
     }
 }
 
@@ -201,13 +224,13 @@
 {
     if ( (ch<0x20) || (ch==0x7d) || (ch==0x7e) ) {
         pc.putc(0x7d);
-        pc.putc(ch^0x20);  // three characters need special handling
+        pc.putc(ch^0x20);  // these characters need special handling
     } else {
         pc.putc(ch);
     }
 }
 
-void sendFrame() // send one PPP frame in HDLC format
+void sendFrame() // send a PPP frame in HDLC format
 {
     int crc = crcBuf(ppp.pkt.buf, ppp.pkt.len-2); // update crc
     ppp.pkt.buf[ ppp.pkt.len-2 ] = (~crc>>0); // fcs lo (crc)
@@ -217,11 +240,11 @@
     pc.putc(0x7e); // hdlc end-of-frame "flag"
 }
 
-void ipRequestHandler()
+void ipConfigRequestHandler()
 {
     debug("IPCP Conf ");
     if ( ppp.pkt.buf[7] != 4 ) {
-        debug("Rej\n"); // reject if any options are requested
+        debug("Rej\n"); // reject any options that are requested
         ppp.pkt.buf[4]=4;
         sendFrame();
     } else  {
@@ -256,7 +279,7 @@
     int code = ppp.pkt.buf[4]; // packet type is here
     switch (code) {
         case 1:
-            ipRequestHandler();
+            ipConfigRequestHandler();
             break;
         case 2:
             ipAckHandler();
@@ -472,8 +495,10 @@
 #endif
     int flags = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1];
 
-    char flagInfo[10];
-    memset(flagInfo,'.',10); // text presentation of TCP flags
+    char flagInfo[10]; // text string presentating the TCP flags
+    memset(flagInfo,'.', 9); // fill string with "........."
+    memset(flagInfo,0,1); // null terminate string
+
     if (flags & (1<<0)) flagInfo[0]='F';
     if (flags & (1<<1)) flagInfo[1]='S';
     if (flags & (1<<2)) flagInfo[2]='R';
@@ -483,9 +508,8 @@
     if (flags & (1<<6)) flagInfo[6]='E';
     if (flags & (1<<7)) flagInfo[7]='C';
     if (flags & (1<<8)) flagInfo[8]='N';
-    flagInfo[9]=0; // null terminate string
     if (v0) {
-        debug("Flags %s Seq %u Ack %u", flagInfo, seq, ack);
+        debug("Flags %s Seq %u Ack %u", flagInfo, seq, ack); // show the flags in debug
     }
 }
 
@@ -542,7 +566,8 @@
 
     ppp.seq = ack;    // always adopt their sequence number calculation in place of doing our own calculation
 
-    if ( ((flagsTCP & ~TCP_FLAG_ACK) == 0) && ((flagsTCP & TCP_FLAG_ACK) != 0) ) {
+    // if ( ((flagsTCP & ~TCP_FLAG_ACK) == 0) && ((flagsTCP & TCP_FLAG_ACK) != 0) ) {
+    if ( flagsTCP == TCP_FLAG_ACK ) {
         if (incomingLen == 0) { // ignore - just an empty ack packet
             return;
         }
@@ -556,7 +581,7 @@
         fastResponse = 1; // we can respond fast to a push
         // It's a push, so let's check the incoming data for an HTTP GET request
         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
+            dataLen = 1024; // this buffer has to hold the web page and the content below, so keep total size under 1024 or increase this
             memset(dataStart,'x', dataLen ); // initialize the data block
 
             int n=0; // number of bytes we have printed so far
@@ -565,20 +590,16 @@
             int contentLengthStart = n; // remember where Content-Length is in buffer
             n=n+sprintf(n+dataStart,"?????\r\n"); // leave five spaces for content length - will be updated later
             n=n+sprintf(n+dataStart,"Content-Type: text/html; charset=us-ascii\r\n\r\n"); // http header must end with empty line (\r\n)
-            int nHeader=n; // byte total of all headers. Note - seems like this must be 1+(multiple of four)
+            int nHeader=n; // byte size of the HTTP header. Note - seems like this must be 1+(multiple of four)
 
-            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,"<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
+            // this is where we insert our web page into the buffer
+            n=n+sprintf(n+dataStart,"%s\r\n", ourWebPage);
 
-            int contentLength = n-nHeader; // Content-Length is the count of every character after the header
+            int contentLength = n-nHeader; // contentLength 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
+            snprintf(contentLengthString,cSize+1,"%*d",cSize,contentLength); // print Content-Length with leading spaces and fixed width equal to csize
+            memcpy(dataStart+contentLengthStart, contentLengthString, cSize); // copy Content-Length to it's place in 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);