Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of PPP-Blinky by
Diff: main.cpp
- Revision:
- 118:54d1936e3768
- Parent:
- 117:c819ae068336
- Child:
- 119:e14dd2bf0ea3
--- a/main.cpp Mon Aug 07 15:06:25 2017 +0000
+++ b/main.cpp Tue Aug 08 01:58:22 2017 +0000
@@ -48,12 +48,21 @@
Serial xx(PC_10, PC_11); // Second serial port on NUCLEO boards - not required to run, if you get compile error here, change #define SERIAL_PORT_MONITOR_YES to #define SERIAL_PORT_MONITOR_NO
#elif defined(TARGET_LPC11U24)
#error The LPC11U24 does not have a second serial port to use for debugging - change SERIAL_PORT_MONITOR_YES back to SERIAL_PORT_MONITOR_NO
+#elif defined(YOUR_TARGET_BOARD_NAME_HERE)
+Serial xx(p9, p10); // insert your board's debug serial port pins here - and please send it to me if it works
#else
#error Add your target board's second serial port here if you want to use debugging - or choose SERIAL_PORT_MONITOR_NO
#endif
#define debug(x...) xx.printf (x) /* if we have a serial port we print debug messages */
+#define debugputc(x...) xx.putc(x)
+#define debugBaudRate(x...) xx.baud(x)
+#define debugPuts(x...) xx.puts(x)
#else
-#define debug(x...) {} /* if we don't have a serial port we do nothing */
+// if we don't have a debug port the debug functions do nothing
+#define debug(x...) {}
+#define debugputc(x...) {}
+#define debugBaudRate(x...) {}
+#define debugPuts(x...) {}
#endif
// verbosity flags used in debug printouts - change to 1 to see increasingly more detailed debug info.
@@ -149,26 +158,6 @@
led1 = ppp.ledState; // toggle led
}
-void crcReset()
-{
- ppp.crc=0xffff; // crc restart
-}
-
-void crcDo(int x) // cumulative crc
-{
- for (int i=0; i<8; i++) {
- ppp.crc=((ppp.crc&1)^(x&1))?(ppp.crc>>1)^0x8408:ppp.crc>>1; // crc calculator
- x>>=1;
- }
-}
-
-int crcBuf(char * buf, int size) // crc on an entire block of memory
-{
- crcReset();
- for(int i=0; i<size; i++)crcDo(*buf++);
- return ppp.crc;
-}
-
// fill our own receive buffer with characters from the PPP serial port
void fillbuf()
{
@@ -192,6 +181,40 @@
}
}
+// print to debug port while checking for incoming characters
+void printWhileCheckingInput( char * data )
+{
+#ifdef SERIAL_PORT_MONITOR_YES
+ char * nextChar = data;
+ while( *nextChar != 0 ) {
+ debugputc( *nextChar ); // write one character to debug port
+ nextChar++;
+ fillbuf(); // check the PC input stream
+ }
+#endif
+}
+
+void crcReset()
+{
+ ppp.crc=0xffff; // crc restart
+}
+
+void crcDo(int x) // cumulative crc
+{
+ for (int i=0; i<8; i++) {
+ ppp.crc=((ppp.crc&1)^(x&1))?(ppp.crc>>1)^0x8408:ppp.crc>>1; // crc calculator
+ x>>=1;
+ }
+ // fillbuf();
+}
+
+int crcBuf(char * buf, int size) // crc on an entire block of memory
+{
+ crcReset();
+ for(int i=0; i<size; i++)crcDo(*buf++);
+ return ppp.crc;
+}
+
int rxbufNotEmpty() // check if rx buffer has data
{
int emptyStatus = (ppp.rx.head==ppp.rx.tail) ? 0 : 1 ;
@@ -250,8 +273,11 @@
void determinePacketType(); // declaration only
determinePacketType();
} else {
- if (0) { // don't normally report FCS because windows frequently early-terminates packets, and early-terminated packets all have FCS errors.
- debug("\nPPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len); // ignore packets with CRC errors but print a debug line
+ if (1) {
+ char pbuf[50]; // local print buffer
+ sprintf(pbuf, "\nPPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len); // print a debug line
+ printWhileCheckingInput( pbuf );
+ if(0) dumpPPPFrame();
}
}
}
@@ -333,13 +359,13 @@
char * udpPkt = ppp.pkt.buf+4; // udp packet start
int headerSizeIP = (( udpPkt[0]&0xf)*4);
char * udpBlock = udpPkt + headerSizeIP; // udp info start
-#ifndef SERIAL_PORT_MONITOR_NO
+#ifdef SERIAL_PORT_MONITOR_YES
char * udpSrc = udpBlock; // source port
char * udpDst = udpBlock+2; // destination port
#endif
char * udpLen = udpBlock+4; // udp data length
char * udpInf = udpBlock+8; // actual start of info
-#ifndef SERIAL_PORT_MONITOR_NO
+#ifdef SERIAL_PORT_MONITOR_YES
int srcPort = (udpSrc[0]<<8) | udpSrc[1];
int dstPort = (udpDst[0]<<8) | udpDst[1];
char * srcIP = udpPkt+12; // udp src addr
@@ -478,12 +504,24 @@
void dumpHeaderIP (int outGoing)
{
+
#if defined(IP_HEADER_DUMP_YES) && defined(SERIAL_PORT_MONITOR_YES)
+ if(0) {
+ if ( ppp.pkt.buf[37] != 0x10 ) return;
+ if ( pc.readable() ) {
+ printWhileCheckingInput("DH_IP\n"); // flag this condition in debug
+ return;
+ }
+ if ((outGoing==1) && (ppp.pkt.buf[37]==0x11)) {
+ //printWhileCheckingInput("A\n"); // flag this condigtion in debug
+ return;
+ }
+ }
char * ipPkt = ppp.pkt.buf+4; // ip packet start
char * ident = ipPkt+4; // 2 bytes
+#ifdef UNUSED_IP_VARIABLES
char * srcAdr = ipPkt+12; // 4 bytes
char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes
-#ifdef UNUSED_IP_VARIABLES
char * version = ipPkt; // top 4 bits
char * ihl = ipPkt; // bottom 4 bits
char * dscp = ipPkt+1; // top 6 bits
@@ -504,21 +542,35 @@
unsigned int checksumIP = (headercheck[0]<<8)|headercheck[1];
#endif
int identIP = (ident[0]<<8)|ident[1];
+ int n=0;
+ char pbuf[140]; // local print buffer
#ifdef DUMP_FULL_IP_ADDRESS
- xx.printf("IP %d.%d.%d.%d %d.%d.%d.%d ",srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3], dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]); // full ip addresses
+ n=n+sprintf(pbuf+n, "IP %d.%d.%d.%d %d.%d.%d.%d ",srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3], dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]); // full ip addresses
#else
- xx.printf("IP %d %d ",srcAdr[3], dstAdr[3]); // short format - only least significant byte of the ip addresses
+ n=n+sprintf(pbuf+n, "%s ", outGoing ? "ME" : "IP" ); // write ME for outgoing and IP for incoming IP packets
+ n=n+sprintf(pbuf+n, "%05d ",identIP); // ident is a good way to correlate our dumps with net monitor or wireshark traces
#endif
- xx.printf("Ident %05d ",identIP); // ident is a good way to correlate our dumps with net monitor or wireshark traces
+ printWhileCheckingInput( pbuf );
#ifndef TCP_HEADER_DUMP_YES
- xx.putc('\n'); // there is no TCP header dump, so terminate the line with \n
+ debugputc('\n'); // there is no TCP header dump, so terminate the line with \n
#endif
#endif
}
void dumpHeaderTCP(int outGoing)
{
+
#if defined(TCP_HEADER_DUMP_YES) && defined(SERIAL_PORT_MONITOR_YES)
+ if (0) {
+ if ( ppp.pkt.buf[37] != 0x10 ) return;
+ if ( pc.readable() ) {
+ printWhileCheckingInput("DH_TCP\n"); // flag this condition in debug
+ return;
+ }
+ if ((outGoing==1) && (ppp.pkt.buf[37]==0x11)) {
+ return;
+ }
+ }
int headerSizeIP = (ppp.pkt.buf[4]&0xf)*4; // header size of ip portion
char * tcpStart = ppp.pkt.buf+4+headerSizeIP; // start of tcp packet
char * seqtcp = tcpStart + 4; // 4 bytes
@@ -541,13 +593,27 @@
if (flags & (1<<5)) flagInfo[2]='U';
if (flags & (1<<6)) flagInfo[1]='E';
if (flags & (1<<7)) flagInfo[0]='C';
+
+ char sflagInfo[4];
+ if (flags & (1<<1)) sflagInfo[0]='S';
+ if (flags & (1<<4)) sflagInfo[0]='A';
+ if (flags & (1<<3)) sflagInfo[0]='P';
+ if (flags & (1<<0)) sflagInfo[0]='F';
+ if (flags & (1<<2)) sflagInfo[0]='R';
+ sflagInfo[1]=' ';
+ sflagInfo[2]=0;
+ sflagInfo[3]=0;
+
+ //char pbuf[140]; // local print buffer
#ifdef DUMP_IP_SEQUENCE_NUMBER
- xx.printf("%s Seq %10u\n", flagInfo, seq); // tcp flags and the sequence number
-#else
- xx.printf("%s\n", flagInfo); // tcp flags only
-#endif
- if( (flags == (1<<4)) && outGoing ) // if this is a pure ack its probably then of the TCP link
- xx.putc('\n'); // so insert an extra line to mark the end of the conversation
+ //sprintf(pbuf, "%s Seq %10u\n", flagInfo, seq); // tcp flags and the sequence number
+#else
+ //sprintf(pbuf, "%s\n", flagInfo); // tcp flags only
+#endif
+ printWhileCheckingInput( sflagInfo );
+ if( outGoing && ( ppp.pkt.buf[37] == 0x11 ) ) { // if this is an outgoing ACK/FIN with no data its probably the end of the TCP link
+ debugputc('\n'); // so insert an extra line to mark the end of the conversation
+ }
#endif
}
@@ -632,7 +698,7 @@
void tcpHandler()
{
// IP header
-
+ //fillbuf();
char * ipPkt = ppp.pkt.buf+4; // ip packet start
char * ihl = ipPkt; // bottom 4 bits
char * pktLen = ipPkt+2; // 2 bytes
@@ -703,7 +769,7 @@
break;
case TCP_FLAG_ACK | TCP_FLAG_PSH:
if ( strncmp(tcpDataIn, "GET /", 5) == 0) { // check for an http GET command
- flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // set outgoing FIN flag to ask them to close from their side
+ flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN | TCP_FLAG_PSH; // set outgoing FIN flag to ask them to close from their side
dataLen = httpResponse(tcpDataOut); // send an http response
} else {
dataLen = tcpResponse(tcpDataOut,tcpDataSize); // not a web request, send a packet reporting number of received bytes
@@ -780,7 +846,10 @@
memcpy( tcp-12, tempHold, 12); // restore the 12 bytes that the pseudo-header overwrote
dumpHeaderIP(1); // dump outgoing IP header
dumpHeaderTCP(1); // dump outgoing TCP header
- wait_ms(20); // wait long enough to ensure we will be given time to dump the headers if we wanted to
+ // for(int i=0; i<1; i++) {
+ // wait_ms(1); // wait long enough to ensure we will be given time to dump the headers if we wanted to
+ // fillbuf();
+ //}
send_pppFrame(); // All preparation complete - send the TCP response
}
@@ -989,10 +1058,8 @@
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
+ debugBaudRate(115200); // baud rate for our debug port if we have one
+ debugPuts("\x1b[2J\x1b[HReady\n"); // VT100 code for clear screen & home on our debug port
pppInitStruct(); // initialize all the PPP properties
while(1) {
scanForConnectString(); // respond to connect command from windows dial up networking
