RealtimeCompLab2

Dependencies:   mbed

Fork of PPP-Blinky by Nicolas Nackel

main.cpp

Committer:
nixnax
Date:
2016-12-27
Revision:
2:b6ccdc962742
Parent:
1:9e03798d4367
Child:
3:bcc66de0bdcd

File content as of revision 2:b6ccdc962742:

#include "mbed.h"

// Proof-of-concept for TCP/IP using Windows 7/8/10 Dial Up Networking over MBED USB Virtual COM Port

// Toggles LED1 every time the PC tries to establish a Dial-up Networking Connection

Serial pc(USBTX, USBRX); // The USB com port - Set this up as a Dial-Up Modem

DigitalOut myled(LED1);

#define BUFLEN (1<<12)
char rxbuf[BUFLEN];

int headPointer = 0; // head of receive buffer
int tailPointer = 0; // tail of receive buffer

void rxHandler() // serial port receive interrupt handler
{
    rxbuf[headPointer]=pc.getc(); // insert in buffer
    __disable_irq();
    headPointer=(headPointer+1)&(BUFLEN-1);
    __enable_irq();
}

int pc_readable() // check if buffer has data
{
    return (headPointer==tailPointer) ? 0 : 1 ;
}

int pc_getBuf() // get one character from the buffer
{
    if (pc_readable()) {
        int x = rxbuf[tailPointer];
        tailPointer=(tailPointer+1)&(BUFLEN-1);
        return x;
    }
    return -1;
}

// ppp frame start/end flag
#define FRAME_START_END 0x7e

int main()
{
    pc.baud(115200);
    pc.attach(&rxHandler,Serial::RxIrq); // activate the receive interrupt handler
    int waitingForPpp = 1; // client string not found yet
    int flagCount=0;
    while(1) {
        while ( pc_readable() ) {
            int rx = pc_getBuf();
            if (rx == FRAME_START_END) flagCount++;
            char * clientFound = strstr( rxbuf, "CLIENTCLIENT" ); // look for string
            if( clientFound ) {
                strcpy( clientFound, "FOUND!FOUND!" ); // overwrite found string
                if (waitingForPpp) pc.printf("CLIENTSERVER"); // respond to PC
                waitingForPpp = waitingForPpp ? 0 : 1; // TOGGLE waiting flag
            }
            if ( flagCount>0 ) myled = ((flagCount/2)%2); // toggle on PPP frame found
        }
    }
}