Embedding Ioto
The Ioto agent is provided as library with a stand-alone command line program.
The Ioto agent can be embedded two ways:
- Use your own main and code and link with the Ioto library.
- Use the Ioto main and provide ioStart and ioStop functions that link to your code.
If you need to embed Ioto in your own main program, you will chose option (1). If you are running on a generic Linux system, you may find option (2) attractive.
Embed in Your Main Program
The first way to integrate Ioto with your code is to create your own main program and link with the Ioto library. This method is desirable if you are using an RTOS like FreeRTOS and need to integrate Ioto into your main program.
To embed Ioto into your main program, you need to do the following things:
- Add #include "ioto.h" to the relevant source files.
- Provide a main().
- Invoke ioStartRuntime and ioRun from your main().
- Build your app and references the libioto.a library.
- Create or edit the Ioto ioto.json5 configuration file and other Ioto config files to suit your needs.
The following code demonstrates providing your own main().
#include "ioto.h"
int main()
{
ioStartRuntime();
// Service requests until told to stop
ioRun();
ioStopRuntime();
return 0;
}
void ioStart()
{
rInfo("sample", "Hello World\n");
// Your code here
}
void ioStop() {}
If this source is contained in a file called main.c, you can build this sample and link with the Ioto library and the OpenSSL library:
cc -o server -I .../ioto/build/inc main.c .../ioto/build/bin/libioto.a main.c -lssl -lcrypto
Use the Ioto Command Program
The second way to integrate Ioto is to use the Ioto main program and provide your own ioStart and ioStop functions. These functions are invoked by Ioto during startup and shutdown.
To build with your start/stop functions you need to do the following things:
- Add #include "ioto.h" to the relevant source files.
- Provide ioStart and ioStop functions.
- Add the libioto.a library to the build/link target in your Makefile.
- Build your application.
- Create or edit the Ioto ioto.json5 and other Ioto config files to suit your needs.
Embedding API
The following code demonstrates the Ioto embedding API.
#include "ioto.h"
PUBLIC void ioStart(void *arg)
{
rInfo("sample", "Hello World\n");
}
If this source is contained in a file called main.c, you can build this sample and link with the Ioto library:
cc -o server -I .../ioto/build/inc main.c .../ioto/build/bin/libioto.a -lssl -lcrypto
If you are building on Mac OS, you will need to install openssl and provide a -L /opt/homebrew/lib library directory option.
brew install openssl
cc-o server -I .../build/inc main.c .../build/bin/libioto.a -L/opt/homebrew/lib -lssl -lcrypto
Samples
The link-agent-main sample demonstrates linking with the Ioto library.
The own-main sample demonstrates linking with your own main.
More Details
For more details about the Ioto API, please consult the Ioto APIs.