RealtimeCompLab2

Dependencies:   mbed

Fork of PPP-Blinky by Nicolas Nackel

Revision:
16:cb0b80c24ba2
Parent:
15:b0154c910143
Child:
17:4918c893d802
diff -r b0154c910143 -r cb0b80c24ba2 main.cpp
--- a/main.cpp	Sun Jan 01 13:25:23 2017 +0000
+++ b/main.cpp	Sun Jan 01 20:34:59 2017 +0000
@@ -17,12 +17,12 @@
 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); // debug((((( port - use an additional USB serial port to monitor this
 
-#define debug(x) xx.printf x
+#define debug(x) xx.printf x 
 
 DigitalOut led1(LED1);
 
 #define FRAME_7E (0x7e)
-#define BUFLEN (1<<13)
+#define BUFLEN (1<<15)
 volatile char rxbuf[BUFLEN];
 char frbuf[3000]; // buffer for ppp frame
 
@@ -51,10 +51,10 @@
 
 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.buf[ppp.rx.head]=pc.getc(); // insert in buffer
-        __disable_irq();
         ppp.rx.head=next;
         //ppp.rx.total++;
         __enable_irq();
@@ -94,15 +94,15 @@
     for (int i=start; i<end; i++) {
         if (unstuff==0) {
             if (rxbuf[i]==0x7d) unstuff=1; 
-            else { *dest++ = rxbuf[i]; ppp.pkt.len++; crcDo(rxbuf[i]);}
+            else { *dest = rxbuf[i]; ppp.pkt.len++; dest++; crcDo(rxbuf[i]); }
         } else { // unstuff
-            *dest++ = rxbuf[i]^0x20; ppp.pkt.len++; crcDo((int)rxbuf[i]^0x20);
+            *dest = rxbuf[i]^0x20; ppp.pkt.len++; dest++; crcDo((int)rxbuf[i]^0x20);
             unstuff=0;
         }
     }
     ppp.pkt.crc = crcG & 0xffff;
     if (ppp.pkt.crc == 0xf0b8) { // check for good CRC
-        void determinePacketType(); // declare early
+        void determinePacketType(); // declaration only
         determinePacketType();
     } else { // crc error
          debug(("CRC is %x Len is %d\n",ppp.pkt.crc,ppp.pkt.len));
@@ -112,20 +112,21 @@
 }
 
 void dumpFrame() {
-    for(int i=0;i<ppp.pkt.len/2;i++) debug(("%02x ", ppp.pkt.buf[i]));
-    debug((" C %02x %02x L=%d\n", ppp.pkt.crc&0xff, (ppp.pkt.crc>>8)&0xff, ppp.pkt.len));
+    for(int i=0;i<ppp.pkt.len;i++) debug(("%02x ", ppp.pkt.buf[i]));
+    debug((" C=%02x %02x L=%d\n", ppp.pkt.crc&0xff, (ppp.pkt.crc>>8)&0xff, ppp.pkt.len));
+}
+
+void hdlcPut(int ch) { // do hdlc handling of special (flag) characters
+    if ( (ch<0x20) || (ch==0x7d) || (ch==0x7e) ) { pc.putc(0x7d); pc.putc(ch^0x20); } else { pc.putc(ch); }
 }
 
 void sendFrame(){
-    int crc = crcBuf(ppp.pkt.buf, ppp.pkt.len-2); // get crc
+    int crc = crcBuf(ppp.pkt.buf, ppp.pkt.len-2); // calculate 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); // frame start flag
-    for(int i=0;i<ppp.pkt.len;i++) {
-        unsigned int cc = (unsigned int)ppp.pkt.buf[i];
-        if (cc>32) pc.putc(cc); else {pc.putc(0x7d); pc.putc(cc+32);}
-    } 
-    pc.putc(0x7e); // frame end flag
+    pc.putc(0x7e); // hdlc start-of-frame "flag"
+    for(int i=0;i<ppp.pkt.len;i++) hdlcPut( ppp.pkt.buf[i] );
+    pc.putc(0x7e); // hdlc end-of-frame "flag"
 }
 
 void ipRequestHandler(){
@@ -165,8 +166,8 @@
 void UDPpacket() {
     char * udpPkt = ppp.pkt.buf+4; // udp packet start
     //char * pktLen = udpPkt+2; // total packet length
-    int headerSize = (( udpPkt[0]&0xf)*4);
-    char * udpBlock = udpPkt + headerSize; // udp info start 
+    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
@@ -183,7 +184,7 @@
     debug(("%d.%d.%d.%d:%d ",    dstIP[1],dstIP[1],dstIP[1],dstIP[1],dstPort));
     debug(("Len %d ", udpLength));
     int printSize = udpLength; if (printSize > 20) printSize = 20; // print only first 20 characters
-    for (int i=0; i<printSize; i++) { char ch = udpInf[i]; if (ch>31 && ch<127) { debug(("%c", ch)); } else { debug(("?")); } } 
+    for (int i=0; i<printSize; i++) { char ch = udpInf[i]; if (ch>31 && ch<127) { debug(("%c", ch)); } else { debug(("_")); } } 
     debug(("\n"));
 }
 
@@ -220,8 +221,8 @@
     char * ipPkt = ppp.pkt.buf+4; // ip packet start
     char * pktLen = ipPkt+2;
     int packetLength = (pktLen[0]<<8) | pktLen[1]; // icmp packet length
-    int headerSize = (( ipPkt[0]&0xf)*4);
-    char * icmpType = ipPkt + headerSize; // icmp data start
+    int headerSizeIP = (( ipPkt[0]&0xf)*4);
+    char * icmpType = ipPkt + headerSizeIP; // icmp data start
     char * icmpSum = icmpType+2; // icmp checksum
 
     #define ICMP_TYPE_PING_REQUEST 8
@@ -240,20 +241,21 @@
         chkSum[0]=0; chkSum[1]=0;
         headerCheckSum();  // new ip header checksum
         #define ICMP_TYPE_ECHO_REPLY 0
-        icmpType[0]=ICMP_TYPE_ECHO_REPLY; // icmp echo replay
+        icmpType[0]=ICMP_TYPE_ECHO_REPLY; // icmp echo reply
         icmpSum[0]=0; icmpSum[1]=0; // zero the checksum for recalculation
-        int dataLength = packetLength - headerSize; // length of ICMP data portion
-        int sum = dataCheckSum( icmpType, dataLength); // this checksum on icmp data portion
+        int icmpLength = packetLength - headerSizeIP; // length of ICMP data portion
+        int sum = dataCheckSum( icmpType, icmpLength); // this checksum on icmp data portion
         icmpSum[0]=sum>>8; icmpSum[1]=sum; // new checksum for ICMP data portion
-        
-        int printSize = dataLength-8; // exclude size of icmp header
+
+        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",'?')); }}
+        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
         
+        
     } else {
         debug(("ICMP type=%d \n", icmpType[0])); 
     }