RPC - Remote Procedure Call of mbed Interfaces

If you give an mbed Interface object a name, you can talk to it via a text based interface.

Methods are called using a url-like scheme:

/<objectname>/<method> <arg1> <arg2>,...

So, here is a basic example:

#include "mbed.h"

DigitalOut led(p1, "led");

int main() {
    char result[64];
    
    rpc("/led/write 1", result); 
    printf("%s\n", result);
    
    rpc("/led/read", result); 
    printf("%s\n", result);
}

Remote control

If you connect mbed up to a terminal, you can enter the commands by hand over serial if you set the mbed to listen, and get responses back:

#include "mbed.h"

Serial pc(USBTX, USBRX);

DigitalIn led(p1, "led");
AnalogIn pot(p20, "pot");

int main() {
    char buf[256], outbuf[256];
    while(1) {
        pc.gets(buf, 256);
        rpc(buf, outbuf); 
        pc.printf("%s\n", outbuf);
    }
}

If you ensure your terminal is sending LF (or CR+LF), you can type

  • /led/write 1<return> to turn on the led
  • /pot/read<return> and you should get the value of the AnalogIn pin 20.

Remote Creation of mbed Interfaces

You can create and delete interfaces remotely using the RPC interface too.

To support new, you just need to tell mbed about the interfaces that are available. Here is an example of a serial app to allow this to work from a remote app:

/* RPCSerial - RPC over a serial port transport
 * sford
 *
 * When running, you can send RPC commands to the mbed
 * via the serial port, to create and manipulate objects
 *
 * Example:
 * > "/DigitalOut/new p2\n"
 * > > "objXYZ\n"
 * > "/objXYZ/write 1\n"
 * > "\n"
 * > "/objXYZ/read"
 * > "1\n"
 */
#include "mbed.h"
#include "rpc.h"
Serial pc(USBTX, USBRX);
int main() {
    // setup the classes that can be created dynamically
    Base::add_rpc_class<AnalogIn>();
    Base::add_rpc_class<AnalogOut>();
    Base::add_rpc_class<DigitalIn>();
    Base::add_rpc_class<DigitalOut>();
    Base::add_rpc_class<PwmOut>();
    Base::add_rpc_class<Timer>();
    Base::add_rpc_class<SPI>();
    Base::add_rpc_class<BusOut>();
    Base::add_rpc_class<BusIn>();
    char command[256] = {0};
    char response[256] = {0};
    // receive commands, and send back the responses
    while(1) {
        pc.gets(command, 256);
        rpc(command, response);
        pc.printf("%s\n", response);
    }
}
Source Code of RPCSerial.bin (LPC1768 LPC2368)
  • Project:
    http://mbed.org/projects/cookbook/svn/RPC/RPCSerial/
    

Example:

// Ensure terminal/app is sending a linefeed at the end

"/DigitalOut/new p3"
> "obj400058B0"
"/obj400058B0/write 1"
> ""

"/AnalogIn/new p3 fooble"
> "fooble"
"/fooble/read"
> "0.297"
"/fooble/delete
> ""