Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
144:01d98cf7738e
Parent:
143:c5019f856a56
Child:
145:098b6ed2f7f2
--- a/PPP-Blinky/ppp-blinky.cpp	Tue Aug 29 17:26:42 2017 +0000
+++ b/PPP-Blinky/ppp-blinky.cpp	Tue Aug 29 18:42:20 2017 +0000
@@ -391,7 +391,7 @@
 }
 
 /// send a PPP frame in HDLC format
-void send_pppFrame()
+void sendPppFrame()
 {
     int crc = crcBuf(ppp.pkt.buf, ppp.pkt.len-2); // update crc
     ppp.pkt.buf[ ppp.pkt.len-2 ] = (~crc>>0); // fcs lo (crc)
@@ -423,13 +423,13 @@
     }
 
     ppp.pkt.buf[4]=2; // change code to ack
-    send_pppFrame(); // acknowledge everything they ask for - assume it's IP addresses
+    sendPppFrame(); // acknowledge everything they ask for - assume it's IP addresses
 
     debugPrintf("Our IPCP Ask (no options)\n");
     ppp.pkt.buf[4]=1; // change code to 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
+    sendPppFrame(); // send our request
 }
 
 /// handle IPCP acknowledge (do nothing)
@@ -445,7 +445,7 @@
     debugPrintf("Their IPCP Nack");
     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
+        sendPppFrame(); // let's request this IP address as ours
         debugPrintf("Our IPCP ACK (received an IP)\n");
     } // if it's not an IP nack we ignore it
 }
@@ -482,12 +482,14 @@
     int headerSizeIP = (( udpPkt[0]&0xf)*4);
     char * udpBlock = udpPkt + headerSizeIP; // udp info start
     char * udpInf = udpBlock+8; // actual start of info
-#ifdef SERIAL_PORT_MONITOR_YES
     char * udpSrc = udpBlock; // source port
     char * udpDst = udpBlock+2; // destination port
     char * udpLen = udpBlock+4; // udp data length
+    char * udpCks = udpBlock+6; // udp checksum
     char * srcIP = udpPkt+12; // udp src addr
     char * dstIP = udpPkt+16; // udp dst addr
+    if (udpLen); // shut up compiler warning for unused udpLen
+#ifdef SERIAL_PORT_MONITOR_YES
     int srcPort = (udpSrc[0]<<8) | udpSrc[1];
     int dstPort = (udpDst[0]<<8) | udpDst[1];
     int udpLength = ((udpLen[0]<<8) | udpLen[1]) - 8; // size of the actual udp data
@@ -508,7 +510,18 @@
     }
     if (v0) debugPrintf("\n");
 #endif
-    if (strncmp(udpInf,"echo ", 5) == 5) debugPrintf("echo found\n");
+    if (strncmp(udpInf,"echo", 4)==0) { // if the UDP string starts with "echo" we echo it back
+        char tempHold[12]; // it's 12 long because we later reuse it when building the TCP pseudo-header
+        memcpy(tempHold, srcIP,4);
+        memcpy(srcIP, dstIP,4);
+        memcpy(dstIP, tempHold,4); // swap ip address source/dest
+        memcpy(tempHold, udpSrc,2);
+        memcpy(udpSrc, udpDst,2);
+        memcpy(udpDst, tempHold,2); // swap udp port source/dest
+        memcpy(udpInf,"Got:",4);
+        memset(udpCks,0,2); // we don't compute the checksum (it's optional), so have to zero it
+        sendPppFrame();
+    }
 }
 
 /// perform a 16-bit checksum. if the byte count is odd, stuff in an extra zero byte.
@@ -620,7 +633,7 @@
             }
             putcWhileCheckingInput('\n');
         }
-        send_pppFrame(); // reply to the ping
+        sendPppFrame(); // reply to the ping
     } else {
         if (v0) {
             debugPrintf("ICMP type=%d \n", icmpType[0]);
@@ -1026,7 +1039,7 @@
         checkPc(); // catch any incoming characters
         wait_us(10); // wait less than 1 character duration at 115200
     }
-    send_pppFrame(); // All preparation complete - send the TCP response
+    sendPppFrame(); // All preparation complete - send the TCP response
     if(0) dumpPPPFrame(); // set to 1 to dump transmitted ppp frame
     memset(ppp.pkt.buf+44,0,500); // flush out traces of previous data that we may scan for
 }
@@ -1097,14 +1110,14 @@
     if (ppp.pkt.buf[7] != 4) {
         ppp.pkt.buf[4]=4; // allow only "no options" which means Maximum Receive Unit (MRU) is default 1500 bytes
         debugPrintf("Reject\n");
-        send_pppFrame();
+        sendPppFrame();
     } else {
         ppp.pkt.buf[4]=2; // ack zero conf
         debugPrintf("Ack\n");
-        send_pppFrame();
+        sendPppFrame();
         debugPrintf("LCP Ask\n");
         ppp.pkt.buf[4]=1; // request no options
-        send_pppFrame();
+        sendPppFrame();
     }
 }
 
@@ -1118,7 +1131,7 @@
 void LCPend()
 {
     ppp.pkt.buf[4]=6;
-    send_pppFrame(); // acknowledge
+    sendPppFrame(); // acknowledge
     ppp.online=0; // start hunting for connect string again
     pppInitStruct(); // flush the receive buffer
     debugPrintf("LCP End (Disconnect from host)\n");
@@ -1229,25 +1242,11 @@
     }
 }
 
-/// set the PPP and debug baud rates and initialize data structures
-void initialize()
+/// Initialize PPP data structure and set serial port(s) baud rate(s)
+void initializePpp()
 {
     pc.baud(115200); // USB serial port to pc
-    debugBaudRate(115200); // baud rate for our (optional) debug port
+    debugBaudRate(115200); // baud rate for (optional) debug serial port
     debugPrintf("\x1b[2J\x1b[H\x1b[30mmbed PPP-Blinky HTTP & WebSocket server ready :)\n"); // VT100 codes for clear_screen, home, black_text - Tera Term is a handy VT100 terminal
     pppInitStruct(); // initialize all the PPP properties
 }
-
-/*
-/// initialize, wait for the connect message, then start processing PPP frames
-int main()
-{
-    initialize();
-    while(1) {
-        scanForConnectString(); // wait for connect from PC dial-up networking
-        while(ppp.online) {
-            waitForPppFrame(); // wait for a PPP frame
-        }
-    }
-}
-*/
\ No newline at end of file