Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
41:e58a5a09f411
Parent:
39:b90183d35f1e
Child:
42:4de44be70bfd
--- a/main.cpp	Thu Jan 05 17:47:17 2017 +0000
+++ b/main.cpp	Mon Jan 09 08:13:40 2017 +0000
@@ -2,40 +2,47 @@
 
 // Copyright 2016 Nicolas Nackel aka Nixnax. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-// Proof-of-concept for TCP/IP using Windows 7/8/10 Dial Up Networking over MBED USB Virtual COM Port
+// PPP-Blinky - "My Internet Of Thing"
+// A Tiny Webserver Using Windows XP/7/8/10 Networking Over A Serial Port.
 
-// Toggles LED1 every time the PC sends an IP packet over the PPP link
+// Also receives UDP packets and responds to ping (ICMP Echo requests)
 
-// Note - turn off all Dial Up authentication, passwords, compression options - Simplest link possible.
-// Note - If you keep seeing "Dial Up Error 777" in Windows read this http://bit.ly/dialup777error
+// Notes and Instructions
+// http://bit.ly/PPP-Blinky-Instructions
 
-// Handy links
-// https://developer.mbed.org/users/nixnax/code/PPP-Blinky/  - Handy Notes, Instructions and introduction
-// http://atari.kensclassics.org/wcomlog.htm
+// Handy reading material
 // https://technet.microsoft.com/en-us/library/cc957992.aspx
 // https://en.wikibooks.org/wiki/Serial_Programming/IP_Over_Serial_Connections
 // http://bit.ly/dialup777error - how to solve Dial Up Error 777 in Windows 7/8/10
+// http://atari.kensclassics.org/wcomlog.htm
 
 // Handy tools
-// https://ttssh2.osdn.jp/index.html.en - A good terminal program to monitor the debug output from the second serial port with!
+// https://ttssh2.osdn.jp/index.html.en - Tera Term, agood terminal program to monitor the debug output from the second serial port with!
 // Wireshark - can't monitor Dial-Up network packets on windows, but very hand - can import our dumpFrame routine's hex output
 // Microsoft network monitor - real-time monitoring of all our packets
 // http://pingtester.net/ - nice tool for high rate ping testing
 // http://www.sunshine2k.de/coding/javascript/crc/crc_js.html - Correctly calculates the 16-bit FCS (crc) on our frames (Choose CRC16_CCITT_FALSE)
-// The curl program in Windows Powershell - use like this to test the webserver:   while (1) { curl 172.10.10.1 }
+// The curl.exe program in Windows Powershell - use it like this to stress test the webserver: while (1) { curl 172.10.10.1 }
 // 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 our board
+// https://eternallybored.org/misc/netcat/ - use netcat -u 172.10.10.1 80 to send/receive UDP packets from PPP-Blinky
+
+// This #define enables/disables a second serial port that prints out interesting diagnostic messages
+#define SERIAL_PORT_MONITOR_YES
+// #define SERIAL_PORT_MONITOR_NO
 
-Serial pc(USBTX, USBRX); // The USB com port - Set this up as a Dial-Up Modem on your pc
-Serial xx(PC_10, PC_11); // See debug messages here. Not necessary to work, but VERY interesting output!
+#ifndef SERIAL_PORT_MONITOR_NO
+Serial xx(PC_10, PC_11); // See debug messages on this port. Not necessary to work, but VERY interesting output!
+#define debug(x) xx.printf x
+#else
+// no debug monitoring
+#define debug(x) {}
+#endif
+
+Serial pc(USBTX, USBRX); // The serial port on your mbed hardware. Your PC thinks this is a dial-up modem.
 
 int v0=1;
 int v1=1; // verbosity flags used in debug printouts - change to 1/0 to see more/less debug info
 
-// the commented #define below gets rid of ALL the debug printfs
-#define debug(x) xx.printf x
-//#define debug(x) {}
-
 DigitalOut led1(LED1); // this led toggles when a packet is received
 
 // the standard hdlc frame start/end character
@@ -44,7 +51,7 @@
 // the serial port receive buffer and packet buffer
 #define BUFLEN (1<<12)
 char rxbuf[BUFLEN];
-char frbuf[3000]; // send/receive buffer for ppp frames
+char frbuf[2000]; // send/receive buffer for ppp frames
 
 // a structure to keep all our ppp globals in
 struct pppType {
@@ -530,18 +537,18 @@
         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 = 15*32; // this block has to hold the web page below, but keep it under 1k
+            dataLen = 17*32; // this block has to hold the web page below, but keep it under 1k
             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: 376\r\n"); // http header
+            n=n+sprintf(n+dataStart,"Content-Length: 441\r\n"); // http header
             n=n+sprintf(n+dataStart,"Content-Type: text/html; charset=us-ascii\r\n\r\n"); // http header
             int nHeader=n; // byte total of all headers
-            n=n+sprintf(n+dataStart,"<html><head><title>mbed-PPP-Blinky</title><script>window.onload=function()"); // html
+            n=n+sprintf(n+dataStart,"<!DOCTYPE html><html><head><title>mbed-PPP-Blinky</title>\n<script>window.onload=function()"); // html
             n=n+sprintf(n+dataStart,"{setInterval(function(){function x(){return document.getElementById('w');};"); // html
-            n=n+sprintf(n+dataStart,"x().innerText = parseInt(x().innerText)+1;},100);};</script></head><body>"); // html
-            n=n+sprintf(n+dataStart,"<h1>mbed-PPP-Blinky Up and Running</h1><h1 id=\"w\" style=\"text-align:"); // html
-            n=n+sprintf(n+dataStart," center\";>0</h1><h1><a href=\"http://bit.ly/pppBlinky\">Source on mbed</h1></body></html>"); // html
+            n=n+sprintf(n+dataStart,"x().textContent = parseInt(x().textContent)+1;},100);};</script>\n</head><body style=\"font-size:30px; color:#807070\">"); // html
+            n=n+sprintf(n+dataStart,"<h1>mbed PPP-Blinky Up and Running</h1><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></body></html>"); // html
             int contentLength = dataLen-nHeader; // this is how to calculate Content-Length, but using curl -v is easier
             contentLength = contentLength+0; // get around unreferenced variable warning
             if (v0) {
@@ -551,7 +558,7 @@
             dataLen = 5*32; // block 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 400 Not Found\r\nServer: PPP-Blinky\r\n"); // http header
+            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: 58\r\n"); // http header
             n=n+sprintf(n+dataStart,"Content-Type: text/html; charset=us-ascii\r\n\r\n"); // http header
             int nHeader=n; // byte total of all headers
@@ -827,9 +834,10 @@
 int main()
 {
     pc.baud(115200); // USB virtual serial port
+#ifndef SERIAL_PORT_MONITOR_NO
     xx.baud(115200); // second serial port for debug(((((((( messages
     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