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 webserverBlinky by
Diff: PPP-Blinky/ppp-blinky.cpp
- Revision:
- 179:ba2b2ddd0da5
- Parent:
- 178:3f17aa0ce5ce
- Child:
- 180:fb1ab50e5e7a
--- a/PPP-Blinky/ppp-blinky.cpp Thu Sep 07 16:22:20 2017 +0000 +++ b/PPP-Blinky/ppp-blinky.cpp Mon Sep 11 14:58:43 2017 +0000 @@ -45,25 +45,25 @@ // Using the second serial port will slow down packet response time // Note - the LPC11U24 does NOT have a second serial port -#define SERIAL_PORT_MONITOR_NO /* change to SERIAL_PORT_MONITOR_YES for debug messages */ +#define SERIAL_PORT_MONITOR_YES /* change to SERIAL_PORT_MONITOR_YES for debug messages */ // here we define the OPTIONAL, second debug serial port for various mbed target boards #ifdef SERIAL_PORT_MONITOR_YES #if defined(TARGET_LPC1768) -Serial xx(p9, p10); // Second serial port on LPC1768 - not required to run, if you get compile error here, change #define SERIAL_PORT_MONITOR_YES to #define SERIAL_PORT_MONITOR_NO +RawSerial xx(p9, p10); // Second serial port on LPC1768 - 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_NUCLEO_F446RE) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_L053R8) || defined(TARGET_NUCLEO_L476RG) || defined(TARGET_NUCLEO_F401RE) -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 +RawSerial 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 (TARGET_KL46Z) || (TARGET_KL25Z) -Serial xx(PTE0,PTE1); // Second serial port on FRDM-KL46Z board +RawSerial xx(PTE0,PTE1); // Second serial port on FRDM-KL46Z board #elif defined(YOUR_TARGET_BOARD_NAME_HERE) // change the next line to YOUR target board's second serial port pin definition if it's not present - and if it works, please send it to me - thanks!!! -Serial xx(p9, p10); // change this to YOUR board second serial port pin definition - and please send it to me if it works!!! +RawSerial xx(p9, p10); // change this to YOUR board second serial port pin definition - 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 simply change SERIAL_PORT_MONITOR_YES to SERIAL_PORT_MONITOR_NO #endif -#define debugPrintf(x...) xx.printf (x) /* if we have a serial port we print debug messages */ +#define debugPrintf(x...) xx.printf (x) /* if we have a serial port we can print debug messages */ #define debugPutc(x...) xx.putc(x) #define debugBaudRate(x...) xx.baud(x) #else @@ -148,7 +148,7 @@ // The modem baud rate should be set to 115200 baud // See instructions at the top. // On a typical mbed hardware platform this serial port is a USB virtual com port (VCP) and the USB serial driver is supplied by the board vendor. -BufferedSerial pc(USBTX, USBRX, 100); // usb virtual com port for mbed hardware +RawSerial pc (USBTX, USBRX); // usb virtual com port for mbed hardware DigitalOut led1(LED1); // this led toggles when a packet is received @@ -183,18 +183,23 @@ ppp.ledState++; } -/// returns 1 after a connect message, 0 at startup or after a disconnect message +/// Returns 1 after a connect message, 0 at startup or after a disconnect message int connectedPpp() { return ppp.online; } +/// Previously used to check for characters in receive buffer. +/// Now just a stub. +void checkPc() {}; + +/// PPP serial port receive interrupt handler. /// Check for available characters from the PC and read them into our own circular serial receive buffer at ppp.rx.buf. /// Also, if we are offline and a 0x7e frame start character is seen, we go online immediately -void checkPc() +void pppReceiveHandler() { char ch; - if ( pc.readable() ) { + while ( pc.readable() ) { int hd = (ppp.rx.head+1)&(RXBUFLEN-1); // increment/wrap head index if ( hd == ppp.rx.rtail ) { debugPrintf("\nReceive buffer full\n"); @@ -379,7 +384,8 @@ } /// convert 4-byte ip address to 32-bit -unsigned int ip( int a, int b, int c, int d){ +unsigned int ip( int a, int b, int c, int d) +{ return a<<24 | b<<16 | c<<8 | d; } @@ -644,14 +650,17 @@ /// Sends a 48 byte IP/UDP header for every 1 byte of data so line-mode would probably be better. /// If you want ip packets from ppp blinky to be routed to other networks, ensure you have ip routing enabled. /// See http://www.wikihow.com/Enable-IP-Routing. -/// Also ensure that the firewall on the receiving machine has the receiving TCP/UDP port enabled. +/// Also ensure that the firewall on the receiving machine has the receiving UDP port (12345 in this example) enabled. +/// The netcat UDP receive command on the remote host would be: nc -ul 12345 void sendUdpData() { #ifdef SERIAL_PORT_MONITOR_YES - if (xx.readable()) { - char inchar = xx.getc(); - xx.putc( inchar ); // echo the char - sendUdp(ip(172,10,10,2), ip(192,168,0,120), 1, 12345, &inchar, 1); // send 1 byte message to 192.168.0.120:12345 + if (ppp.online) { + if (xx.readable()) { + char inchar = xx.getc(); + xx.putc( inchar ); // echo the received character on the debug serial port + sendUdp(ip(172,10,10,2), ip(192,168,0,109), 1, 12345, &inchar, 1); // send a 1 byte UDP message to a remote machine at IP 192.168.0.109:12345 + } } #endif } @@ -943,10 +952,8 @@ { #ifdef SERIAL_PORT_MONITOR_YES if (v2) { - // IP header int packetLengthIp = __REV16(ppp.ip->lengthR ); // size of ip packet int headerSizeIp = 4 * ppp.ip->headerLength; // size of ip header - // TCP header ppp.tcpStart = ppp.ipStart + headerSizeIp; // calculate where the TCP header starts int headerSizeTcp = 4 * (ppp.tcp->offset); // tcp "offset" for start of data is also the header size ppp.tcpData = ppp.tcpStart + headerSizeTcp; // start of tcp data @@ -1015,10 +1022,11 @@ 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_PSH; // set outgoing FIN flag to ask them to close from their side + flagsOut = TCP_FLAG_ACK | TCP_FLAG_PSH; // we have data, set the PSH flag dataLen = httpResponse(tcpDataOut); // send an http response } else { dataLen = tcpResponse(tcpDataOut,tcpDataSize, &flagsOut); // not an http GET, handle as a tcp connection + if (dataLen > 0) flagsOut = TCP_FLAG_ACK | TCP_FLAG_PSH; // if we have any data set the PSH flag } break; case TCP_FLAG_FIN: @@ -1028,7 +1036,7 @@ ack_out++; // for FIN flag we have to increase the sequence by 1 break; default: - return; // ignore remaining packets + return; // ignore all other packets } // switch // The TCP flag handling is now done @@ -1057,11 +1065,7 @@ dumpHeaderIP(1); // dump outgoing IP header before sending the frame dumpHeaderTCP(1); // dump outgoing TCP header before sending the frame dumpDataTCP(1); // dump outgoing TCP data before sending the frame - - for (int i=0; i<45*1000/10; i++) { // 45 ms delay before sending frame - a typical internet delay time - checkPc(); // catch any incoming characters - wait_us(10); // wait less than 1 character duration at 115200 - } + wait_ms(45); // 45 ms delay before sending frame - a typical internet delay time 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 @@ -1250,9 +1254,9 @@ /// 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 (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 + pppInitStruct(); // initialize all the variables/properties/buffers + pc.baud(115200); // pc serial port acting as a dial-up modem - for PPP traffic + pc.attach(&pppReceiveHandler, RawSerial::RxIrq); // set up serial port receive interrupt handler } -