Embedding Ioto

Ioto can be integrated into your system in three ways:

  1. Rebuild the Ioto program with customized start.c and stop.c functions.
  2. Link with the Ioto library.
  3. Provide your own main program.

Rebuild Ioto with Start.c and Stop.c

The simplest integration is to modify the start.c and stop.c files as required and then rebuild Ioto.

The Ioto Makefile builds a stand-alone agent program called ioto. The agent is built with stub start.c and stop.c files that you can customize as required. You can add code to these files and link against any external libraries you require and then rebuild ioto.

It is best not to modify other Ioto source files as that can make upgrading to future versions more difficult.

The second way to integrate Ioto is to link with the Ioto library and provide your own start and stop functions. In this manner, your app becomes empowered with MQTT, HTTP and AWS integration services.

The Ioto Makefile builds a library called libioto.a. Ioto can be customized by providing ioStart and ioStop functions. These functions are invoked by Ioto during startup and shutdown.

The default versions of these functions are provided by the library via the start.c and stop.c files that are linked into the libioto.a library. However if you provide your alternates and link against them before libioto.a, your versions will be used instead.

The embedding API requires only a few lines of code. See the Link Library Sample for an example.

To include the Ioto library in your program you need to do the following things:

  1. Add #include "ioto/ioto.h" to the relevant source files.
  2. Add the libioto.a library to your Makefile.
  3. Provide an ioStart function to initialize your code and ioStop function to terminate your code.
  4. Build your application.
  5. Create or edit the Ioto config.json and other Ioto config files to suit your needs.

Embedding API

The following code demonstrates the Ioto embedding API. This will create the Ioto services based on the config.json configuration file.

#include "ioto/ioto.h"

PUBLIC void ioStart(void *arg)
{
    rinfo("sample", "Hello World\n");

    //  Instruct ioto to exit the sample
    rStop();
}

To build this sample and link with the Ioto library:

cc -o server server.c -lioto

Provide your own main()

The last way to integrate Ioto is to create your own main program and link with the Ioto library.

This method is desirable if you have more extensive customizations or need to parse custom command line options.

The Ioto libioto.a library also includes a default main() program. If you provide your own main() and link with libioto.a, then your main will be used instead.

See the Link Main Sample for an example.

To build with your own main, you need to do the following things:

  1. Add **#include "ioto/ioto.h"* to the relevant source files.
  2. Add the libioto.a library to your Makefile.
  3. Provide a main().
  4. Invoke ioInit and ioTerm.
  5. Build your application.
  6. Create or edit the Ioto config.json and other Ioto config files to suit your needs.

The following code demonstrates providing your own main().

#include "ioto/ioto.h"
static void start(void *arg);

int main(int argc, char** argv)
{
    rInit((RFiberProc) start, 0, 0);

    //  Block until rStop()
    rServiceEvents();
    ioTerm();
    rTerm();
    return 0;
}

static void start(void *arg)
{
    ioInit(0);
    rinfo("sample", "Hello World\n");
    //  Exit the sample
    rStop();
}

To build this sample and link with the Ioto library:

cc -o server server.c -lioto

Samples

The Link Library Ioto sample demonstrates linking with the Ioto library. The Link Main Ioto sample demonstrates linking with your own main.

More Details

For more details about the embedding API, please consult the Ioto APIs.

© Embedthis Software. All rights reserved.