Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
17:4918c893d802
Parent:
16:cb0b80c24ba2
Child:
18:3e35de1bc877
--- a/main.cpp	Sun Jan 01 20:34:59 2017 +0000
+++ b/main.cpp	Sun Jan 01 22:20:13 2017 +0000
@@ -22,9 +22,9 @@
 DigitalOut led1(LED1);
 
 #define FRAME_7E (0x7e)
-#define BUFLEN (1<<15)
+#define BUFLEN (1<<12)
 volatile char rxbuf[BUFLEN];
-char frbuf[3000]; // buffer for ppp frame
+char frbuf[6000]; // buffer for ppp frame
 
 struct {
     int online; 
@@ -51,13 +51,14 @@
 
 void rxHandler() // serial port receive interrupt handler
 {
-    ppp.rx.buf[ppp.rx.head]=pc.getc(); // insert in buffer
-    __disable_irq();
-    int next = (ppp.rx.head+1)&(BUFLEN-1);
-    if (next != ppp.rx.tail) {
-        ppp.rx.head=next;
-        //ppp.rx.total++;
-        __enable_irq();
+    while ( pc.readable() ) {
+        ppp.rx.buf[ppp.rx.head]=pc.getc(); // insert in rx buffer
+        int hd = (ppp.rx.head+1)&(BUFLEN-1); // increment/wrap
+        if ( hd != ppp.rx.tail ) { // watch for buffer full
+            __disable_irq();
+                ppp.rx.head = hd; // update head pointer
+            __enable_irq();
+        }
     }
 }
 
@@ -91,14 +92,17 @@
     char * dest = ppp.pkt.buf;
     ppp.pkt.len=0;
     int unstuff=0;
-    for (int i=start; i<end; i++) {
+    int idx = start;
+    while(1) {
         if (unstuff==0) {
-            if (rxbuf[i]==0x7d) unstuff=1; 
-            else { *dest = rxbuf[i]; ppp.pkt.len++; dest++; crcDo(rxbuf[i]); }
+            if (rxbuf[idx]==0x7d) unstuff=1; 
+            else { *dest = rxbuf[idx]; ppp.pkt.len++; dest++; crcDo(rxbuf[idx]); }
         } else { // unstuff
-            *dest = rxbuf[i]^0x20; ppp.pkt.len++; dest++; crcDo((int)rxbuf[i]^0x20);
+            *dest = rxbuf[idx]^0x20; ppp.pkt.len++; dest++; crcDo(rxbuf[idx]^0x20);
             unstuff=0;
         }
+        idx = (idx+1) & (BUFLEN-1);
+        if (idx == end) break;
     }
     ppp.pkt.crc = crcG & 0xffff;
     if (ppp.pkt.crc == 0xf0b8) { // check for good CRC
@@ -121,7 +125,7 @@
 }
 
 void sendFrame(){
-    int crc = crcBuf(ppp.pkt.buf, ppp.pkt.len-2); // calculate crc
+    int crc = crcBuf(ppp.pkt.buf, ppp.pkt.len-2); // update crc
     ppp.pkt.buf[ ppp.pkt.len-2 ] = (~crc>>0); // fcs lo (crc)
     ppp.pkt.buf[ ppp.pkt.len-1 ] = (~crc>>8); // fcs hi (crc)
     pc.putc(0x7e); // hdlc start-of-frame "flag"
@@ -165,15 +169,12 @@
 
 void UDPpacket() {
     char * udpPkt = ppp.pkt.buf+4; // udp packet start
-    //char * pktLen = udpPkt+2; // total packet length
     int headerSizeIP = (( udpPkt[0]&0xf)*4);
     char * udpBlock = udpPkt + headerSizeIP; // udp info start 
     char * udpSrc = udpBlock; // source port
     char * udpDst = udpBlock+2; // destination port
     char * udpLen = udpBlock+4; // udp data length
     char * udpInf = udpBlock+8; // actual start of info
-    //char * udpSum = udpBlock+6; // udp checksum
-    //int packetLength = (pktLen[0]<<8) | pktLen[1]; // udp total packet length
     int srcPort = (udpSrc[0]<<8) | udpSrc[1];
     int dstPort = (udpDst[0]<<8) | udpDst[1];
     char * srcIP = udpPkt+12; // udp src addr
@@ -231,7 +232,8 @@
         ipTTL[0]--; // decrement time to live
         char * srcAdr = ipPkt+12;
         char * dstAdr = ipPkt+16;
-        debug(("ICMP PING %d.%d.%d.%d %d.%d.%d.%d Head %04d Tail=%04d ", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3],dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3],ppp.rx.head,ppp.rx.tail));
+        debug(("Ping\n"));
+        if (0) { debug(("ICMP PING %d.%d.%d.%d %d.%d.%d.%d Head %04d Tail=%04d ", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3],dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3],ppp.rx.head,ppp.rx.tail)); }
         char src[4]; char dst[4];
         memcpy(src, srcAdr,4);
         memcpy(dst, dstAdr,4);
@@ -250,8 +252,8 @@
         int printSize = icmpLength-8; // exclude size of icmp header
         char * icmpData = icmpType+8; // the actual data is after the header
         if (printSize > 10) printSize = 10; // print only first 20 characters
-        for (int i=0; i<printSize; i++) { char ch = icmpData[i]; if (ch>31 && ch<127) { debug(("%c",ch)); } else { debug(("%c",'_')); }}
-        debug(("%c",'\n'));
+//        for (int i=0; i<printSize; i++) { char ch = icmpData[i]; if (ch>31 && ch<127) { debug(("%c",ch)); } else { debug(("%c",'_')); }}
+//        debug(("%c",'\n'));
         
         sendFrame(); // reply to the ping