PPP over serial and mbed OS Sockets (cellular modem)

Hi, I managed to let my ST Nucleo with mbed OS connect to Internet using an Adafruit Fona (a serial 2G modem). I used the PPP layer included in the sal-stack-lwip module, using this library as reference: https://github.com/mbedmicro/mbed/tree/master/libraries/net/cellular/CellularModem
It works good, I get an IP address, DNS server address ecc.
Now here is the problem: I need to continuously check if some data arrived on the serial port and pass them to PPP for processing. Since threads are not yet supported in mbed OS, I’m using an interrupt to save serial data in a buffer, then a minar periodic scheduled task takes these data and pass them to PPP every 300ms.
But I don’t know if this approach is compatible with the higher levels socket libraries: I mean, if a socket is blocked, minar won’t be able to schedule the “receiving task”, am I right? Or maybe mbed OS sockets are compatible with this approach?
How should I proceed?
Thank you

Scheduling a periodic receive task should work just fine since mbed OS sockets are non-blocking.

Could you use the interrupt to schedule a processing task instead of scheduling it periodically? You can bind the buffer to the task packet input function. For example:

Serial s;
void pppPacketAdder(Buffer b, int size){
    // Process your packet here
}


void uart_rx_callback(Buffer b, int size) {
    minar::Scheduler::postCallback(Serial::event_callback_t(pppPacketAdder).bind(b,size));
    // Get the next buffer
    s.read(nextBuffer, uart_rx_callback);
}

Thank you for your reply Brendan. Indeed, your solution is smarter. I’ll try this way and I’ll see if all works well.
Thank you again
Carmelo

Hi I would like to using PPP over serial. Can you give some example code that how we do it?
I have post in GSM/GPRS module and PPP protocol | Mbed since 2011 but no any answer for solution. Pls shade some light for us.

Hi oxtek, here you can find the the code I’m using to drive my Adafruit Fona serial modem on mbed OS: GitHub - carmelomigliore/sal-iface-serialmodem
I had to slightly modify the sal-stack-lwip module (on which the PPP serial layer depends) to let it work on my st nucleo board. So depending on your board you may need to modify it too.

WARNING: that code is highly experimental, not so well written and full of quirks… I will improve it someday in the future, but not too soon. However it “works” (at least for adafruit fona) and I’m able to connect to Internet.

in mbed classic I can use the follow to receive uart , in mbedos what should I do. thanks!!!
Serial s(xx,xx);
void callback(){
char tmp = s.getc();
}
void main()
{
s.attach(&callback);
while(true);
}

@zm1011 The attach and getc member functions should work the same way. I would suggest using burst reads if possible:

/** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. * * @param buffer The buffer where received data will be stored * @param length The buffer length * @param callback The event callback function * @param event The logical OR of RX events * @param char_match The matching character */ int read(void *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);

Another important point is that you should avoid busy waits in mbed OS. Taking the example you posted above I would suggest doing the following:

Serial s(xx,xx); void callback() { char tmp = s.getc(); } void app_start(int, char**) { s.attach(&callback); }

how using an intterrupt to save serial data in mbedos , can you give me some hint? thanks a lot!!!

after removed the code{ while(true); } ,when I send char to serial it can’t operate correct(in callbackFun() I add led state change). I need to continue to receive the message, and the message length is not necessarily. can you give me any suggestion? thank you !!!

Can you post more of your code? It’s important that you use app_start, rather than main.

In st-nucleo-f401re-gcc 0.2.4 board when receive uart message the code get frozen, but in frdm-k64f-gcc 2.0.0 the code can run correct. if i want make st-nucleo-f401re-gcc can also run with no error ,which file should modify? thank you!!!

#include "mbed-drivers/mbed.h"
#include "minar/minar.h"
#include "core-util/FunctionPointer.h"
using namespace mbed::util;
static char buf [10];
static DigitalOut led(LED1);
//static DigitalOut led(YOTTA_CFG_HARDWARE_PINS_D4);
static Serial pc(USBTX, USBRX);

static void led_toggle_user(void) {
led =!led;
//pc.printf("receive");
buf[0]=pc.getc();
}
static void test(){

pc.printf(buf);
}


void app_start(int, char**) {

pc.attach(&led_toggle_user);


buf[0]='*';
buf[1]='\0';

led=1;
minar::Scheduler::postCallback(test).period(minar::milliseconds(500));
}

I had also a similar problem with my ST Nucleo… I resolved using RawSerial instead of Serial, it’s working fine now.

When I change
static Serial pc(USBTX, USBRX);
to
static RawSerial pc(USBTX, USBRX);
the problem still exits, are you using pc.attach(&ca…) handle the uart receive message?