Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Typedefs | Functions | Variables
Mainloop

Data Structures

struct  sol_main_callbacks
 Structure used to keep the application main callbacks. More...
 
struct  sol_mainloop_implementation
 Structure representing a mainloop implementation (hooks). More...
 
struct  sol_mainloop_source_type
 Structure representing the type of a source of mainloop events. More...
 

Macros

#define SOL_MAIN(CALLBACKS)
 Convenience macro to declare the main function and properly initialize and execute a Soletta Application. More...
 
#define SOL_MAIN_DEFAULT(STARTUP, SHUTDOWN)
 Preferred entry point for Soletta applications. More...
 
#define SOL_MAINLOOP_FD_ENABLED
 This macro is defined by Soletta at build time and will state if file descriptor (fd) functionality is available, this is common in Linux/UNIX environments. More...
 
#define SOL_MAINLOOP_FORK_WATCH_ENABLED
 This macro is defined by Soletta at build time and will state if monitoring (watch) of child process functionality is available, this is common in Linux/UNIX environments. More...
 
#define SOL_NO_API_VERSION
 This macro is defined by Soletta at build time and will state if api_version structure members and related defines shouldn't be used. More...
 
#define SOL_SET_API_VERSION(expression)
 This macro will cope with SOL_NO_API_VERSION and allows easy declaration of api_version fields. More...
 

Typedefs

typedef struct sol_idle sol_idle
 Handle for idlers. More...
 
typedef struct sol_main_callbacks sol_main_callbacks
 Structure used to keep the application main callbacks. More...
 
typedef struct
sol_mainloop_implementation 
sol_mainloop_implementation
 Structure representing a mainloop implementation (hooks). More...
 
typedef struct sol_mainloop_source sol_mainloop_source
 Structure of a Source of mainloop events. More...
 
typedef struct
sol_mainloop_source_type 
sol_mainloop_source_type
 Structure representing the type of a source of mainloop events. More...
 
typedef struct sol_timeout sol_timeout
 Handle for timers tracking the timeouts. More...
 

Functions

int sol_argc (void)
 Gets the argument count the application was launched with, if any. More...
 
char ** sol_argv (void)
 Gets the list of arguments the application was launched with, if any. More...
 
struct sol_idlesol_idle_add (bool(*cb)(void *data), const void *data)
 Adds a function to be called when the application goes idle. More...
 
bool sol_idle_del (struct sol_idle *handle)
 Deletes the given idler. More...
 
int sol_init (void)
 Initializes the Soletta library. More...
 
struct sol_mainloop_sourcesol_mainloop_add_source (const struct sol_mainloop_source_type *type, const void *data)
 Create a new source of events to the main loop. More...
 
int sol_mainloop_default_main (const struct sol_main_callbacks *callbacks, int argc, char *argv[])
 Helper function called by SOL_MAIN. More...
 
void sol_mainloop_del_source (struct sol_mainloop_source *handle)
 Destroy a source of main loop events. More...
 
const struct
sol_mainloop_implementation
sol_mainloop_get_implementation (void)
 Returns the current mainloop implementation in use. More...
 
bool sol_mainloop_set_implementation (const struct sol_mainloop_implementation *impl)
 Changes the mainloop implementation. More...
 
void * sol_mainloop_source_get_data (const struct sol_mainloop_source *handle)
 Retrieve the user data (context) given to the source at creation time. More...
 
void sol_quit (void)
 Terminates the main loop. More...
 
void sol_quit_with_code (int return_code)
 Terminates the main loop, setting a specific return code. More...
 
int sol_run (void)
 Runs the main loop. More...
 
void sol_set_args (int argc, char *argv[])
 Sets a new list of arguments and its count. More...
 
void sol_shutdown (void)
 Shutdown Soletta library. More...
 
struct sol_timeoutsol_timeout_add (uint32_t timeout_ms, bool(*cb)(void *data), const void *data)
 Adds a function to be called periodically by the main loop. More...
 
bool sol_timeout_del (struct sol_timeout *handle)
 Deletes the given timeout. More...
 

Variables

const struct
sol_mainloop_implementation
SOL_MAINLOOP_IMPLEMENTATION_DEFAULT
 Pointer to Soletta's internal mainloop implementation, that is the default to be used if no other is set. More...
 

Detailed Description

Macro Definition Documentation

#define SOL_MAIN (   CALLBACKS)
Value:
int main(int argc, char *argv[]) { \
return sol_mainloop_default_main(&(CALLBACKS), argc, argv); \
}
#define SOL_LOG_LEVEL_INIT()
Sets the global log level based on the SOL_LOG_LEVEL macro.
Definition: sol-log.h:488
int main(int argc, char *argv[])
Definition: iotivity-test-client.c:613
#define SOL_LOG_LEVELS_INIT()
Sets the log level of the given log domains.
Definition: sol-log.h:489
int sol_mainloop_default_main(const struct sol_main_callbacks *callbacks, int argc, char *argv[])
Helper function called by SOL_MAIN.

Convenience macro to declare the main function and properly initialize and execute a Soletta Application.

Warning
Prefer to use SOL_MAIN_DEFAULT since it handles different platforms.
#define SOL_MAIN_DEFAULT (   STARTUP,
  SHUTDOWN 
)
Value:
static const struct sol_main_callbacks sol_main_callbacks_instance = { \
.startup = (STARTUP), \
.shutdown = (SHUTDOWN) \
}; \
SOL_MAIN(sol_main_callbacks_instance)
#define SOL_MAIN(CALLBACKS)
Convenience macro to declare the main function and properly initialize and execute a Soletta Applicat...
Definition: soletta.h:105
uint16_t api_version
API version.
Definition: sol-mainloop.h:1249
static void shutdown(void)
Definition: browse.c:167
#define SOL_SET_API_VERSION(expression)
This macro will cope with SOL_NO_API_VERSION and allows easy declaration of api_version fields...
Definition: sol-mainloop.h:660
Structure used to keep the application main callbacks.
Definition: sol-mainloop.h:1246
#define SOL_MAIN_CALLBACKS_API_VERSION
Definition: sol-mainloop.h:1248

Preferred entry point for Soletta applications.

Different platforms may have different ways in which an application is started, so in order to remain portable, Soletta applications should avoid using platform specific main functions.

SOL_MAIN_DEFAULT will be defined to something that makes sense for the target platform, ensuring that Soletta is properly initialized before calling the provided startup function, where all of the application specific initialization must take place. If they make sense, command line arguments will have been set and can be retrieved with sol_argc() and sol_argv().

After startup is called, the main loop will start, and once that finishes, the shutdown function, if provided, will be called to perform any necessary termination procedures by the application, before Soletta itself is shutdown and the program terminated.

Examples:
/src/samples/bluetooth/browse.c, /src/samples/bluetooth/connect-paired.c, /src/samples/bluetooth/heartbeat.c, /src/samples/common/linux-micro-init.c, /src/samples/common/uart.c, /src/samples/crypto/message-digest.c, /src/samples/crypto/sha256sum.c, /src/samples/design_patterns/stream_sample.c, /src/samples/flow/c-api/find-type.c, /src/samples/flow/c-api/highlevel.c, /src/samples/flow/c-api/lowlevel.c, /src/samples/flow/c-api/simple-c-type.c, /src/samples/flow/c-api/single-node.c, /src/samples/http/client.c, /src/samples/http/download.c, /src/samples/http/server-https.c, /src/samples/http/server-sse.c, /src/samples/http/server.c, /src/samples/http/static-files.c, /src/samples/network/echo-client.c, /src/samples/network/echo-server.c, /src/samples/network/netctl.c, and /src/samples/network/network-status.c.
#define SOL_MAINLOOP_FD_ENABLED

This macro is defined by Soletta at build time and will state if file descriptor (fd) functionality is available, this is common in Linux/UNIX environments.

#define SOL_MAINLOOP_FORK_WATCH_ENABLED

This macro is defined by Soletta at build time and will state if monitoring (watch) of child process functionality is available, this is common in Linux/UNIX environments.

#define SOL_NO_API_VERSION

This macro is defined by Soletta at build time and will state if api_version structure members and related defines shouldn't be used.

API Version is used when dynamic libraries are available so we can check in runtime if the structures match expectations. However in static builds such as those in Small OSes it's guaranteed at build time and the space and checks can be saved.

Users can rely on macros SOL_SET_API_VERSION() to set these members in a conditional way.

Examples:
/src/samples/flow/c-api/simple-c-type.c.

Referenced by mytype_func().

#define SOL_SET_API_VERSION (   expression)

Typedef Documentation

Handle for idlers.

This structure is used to help setup and control Idlers.

Structure used to keep the application main callbacks.

It's intended to be used through SOL_MAIN_DEFAULT. Keeps the startup and shutdown callbacks of the application that will be called by SOL_MAIN_DEFAULT when appropriated.

Structure representing a mainloop implementation (hooks).

Structure of a Source of mainloop events.

Structure representing the type of a source of mainloop events.

Handle for timers tracking the timeouts.

Function Documentation

int sol_argc ( void  )
char** sol_argv ( void  )
struct sol_idle* sol_idle_add ( bool(*)(void *data)  cb,
const void *  data 
)

Adds a function to be called when the application goes idle.

Idlers are called when the main loop reaches the idle state. That is, after all pending events have been processed and no timeout has expired. This means that if there's always something to do, an idler function may never be called. Like timeouts, if an idler function returns false, it will be automatically removed, otherwise they will kept to be called until some other event becomes available.

Parameters
cbThe function to call when the idle state is reached
dataThe user data pointer to pass to the function
Returns
A handle that can be used to delete the idler
Note
MT-safe if compiled with threads support.
bool sol_idle_del ( struct sol_idle handle)

Deletes the given idler.

Parameters
handleThe idler to delete
Returns
True if the idler was deleted, false if the handle is invalid or if it had been marked as removed already
Note
MT-safe if compiled with threads support.
int sol_init ( void  )
struct sol_mainloop_source* sol_mainloop_add_source ( const struct sol_mainloop_source_type type,
const void *  data 
)

Create a new source of events to the main loop.

Some libraries will have their own internal main loop, in the case we should integrate them with Soletta's we do so by adding a new source of events to Soletta's main loop.

The source is described by its type, a set of functions to be called back at various phases:

  • prepare called before all other callbacks at each main loop iteration.
  • get_next_timeout called before configuring the maximum timeout to wait for events. The smallest value of all sources and the first timeout of Soletta's will be used to determine the value to use (ie: the one to give to poll(2) if using posix main loop). If returns false the main loop can sleep forever. If not provided, then return false is assumed.
  • check called after all event sources and Soletta's internal events are polled and are ready to be dispatched. If it returns true, then there are events to be dispatched in this source.
  • dispatch called if check() returns true.
  • dispose called when the source is deleted. This will happen at sol_shutdown() if the source is not manually deleted.

If a source doesn't know the time for the next event (say an interruption service handler or an internal file descriptor), then further integration work needs to be done. The interruption service handler can use sol_timeout_add() from a thread to schedule a main loop wake, that in turn will run the source's prepare() automatically. Analogously, the internal file descriptors can be added to soletta's at prepare(). If the main loop uses epoll(), that fd can be added to chain the monitoring.

Parameters
typethe description of the source of main loop events. This pointer is not modified and is not copied, thus it must exist during the lifetime of the source
datathe user data (context) to give to callbacks in type
Returns
the new main loop source instance or NULL on failure
See Also
sol_mainloop_del_source()
Note
MT-safe if compiled with threads support.

Referenced by sol_glib_integration().

int sol_mainloop_default_main ( const struct sol_main_callbacks callbacks,
int  argc,
char *  argv[] 
)

Helper function called by SOL_MAIN.

Shouldn't be called directly.

Parameters
callbacksApplication callback structure
argcThe count of elements in argv
argvArray of NUL terminated strings, each represents one argument
Returns
0 on success, error code (always negative) otherwise
void sol_mainloop_del_source ( struct sol_mainloop_source handle)

Destroy a source of main loop events.

Parameters
handlea valid handle previously created with sol_mainloop_add_source().
See Also
sol_mainloop_add_source()
Note
MT-safe if compiled with threads support.
const struct sol_mainloop_implementation* sol_mainloop_get_implementation ( void  )

Returns the current mainloop implementation in use.

By default it is SOL_MAINLOOP_IMPLEMENTATION_DEFAULT, but can be changed by sol_mainloop_set_implementation().

Before accessing the members of the returned pointer check if your known SOL_MAINLOOP_IMPLEMENTATION_API_VERSION matches its api_version field (which is conditional to SOL_NO_API_VERSION).

Returns
the pointer to the current implementation, always non-null.
bool sol_mainloop_set_implementation ( const struct sol_mainloop_implementation impl)

Changes the mainloop implementation.

By setting the mainloop implementation one can override the Soletta behavior, this is often useful if libsoletta is being used as a guest library in some system that already hosts one, such as a Node.JS application. In this case we want to forward requests to that mainloop and be as lean as possible.

This is an alternative to sol_mainloop_add_source(), in that case Soletta would be the host and other main loops can be the guest, see sol-glib-integration.h for one example.

Note
Primitives such as sol_idle_add(), sol_idle_del(), sol_timeout_add(), sol_timeout_del(), sol_fd_add(), sol_fd_del(), sol_fd_set_flags(), sol_fd_get_flags(), sol_child_watch_add() and sol_child_watch_del(), sol_mainloop_add_source(), sol_mainloop_del_source() and sol_mainloop_source_get_data() may be called from threads, then handle these as such.
this function must be called before sol_init() is called the first time, otherwise it will refuse and will return error.
Parameters
impla valid handle, this means api_version field must match SOL_MAINLOOP_IMPLEMENTATION_API_VERSION and all pointers are non-NULL. It is not copied, so the pointer must be valid until a new one is set to replace it.
Returns
true on success or false on failure.
void* sol_mainloop_source_get_data ( const struct sol_mainloop_source handle)

Retrieve the user data (context) given to the source at creation time.

Parameters
handlea valid handle previously created with sol_mainloop_add_source().
Returns
whatever was given to sol_mainloop_add_source() as second parameter. NULL is a valid return.
See Also
sol_mainloop_add_source()
Note
MT-safe if compiled with threads support.
void sol_quit ( void  )
void sol_quit_with_code ( int  return_code)
int sol_run ( void  )
void sol_set_args ( int  argc,
char *  argv[] 
)

Sets a new list of arguments and its count.

A reference to the argv pointer will be kept, so it must be valid at least until sol_set_args() is called again to set different arguments. This function is useful in cases where the main application may have arguments to handle and others that need to be passed forward to other sub-systems. For example, the flow systems has node types to retrieve the arguments the application was started with, and these may differ if the application is run as an FBP script, or as a compiled binary, so the FBP interpreter removes its own arguments and uses this function to set the list of arguments that the flow should see.

Parameters
argcThe count of elements in argv
argvArray of nul terminated strings, each represents one argument
void sol_shutdown ( void  )
struct sol_timeout* sol_timeout_add ( uint32_t  timeout_ms,
bool(*)(void *data)  cb,
const void *  data 
)

Adds a function to be called periodically by the main loop.

Timeouts are called by the main loop every timeout_ms milliseconds for as long as the given function cb returns true.

Parameters
timeout_msThe period in milliseconds in which the function will be called
cbThe function to call, it will be called every timeout_ms until it returns false
dataThe user data pointer to pass to the function
Returns
A handle that can be used to delete the timeout
Note
MT-safe if compiled with threads support.
Examples:
/src/samples/bluetooth/browse.c, /src/samples/bluetooth/heartbeat.c, /src/samples/coap/lwm2m-client.c, /src/samples/coap/simple-server.c, /src/samples/common/linux-micro-init.c, /src/samples/common/platform-simple.c, /src/samples/common/uart.c, /src/samples/design_patterns/stream_sample.c, /src/samples/flow/c-api/custom-node-types.c, /src/samples/flow/c-api/simple-c-type.c, /src/samples/iio+network/iio-gyroscope-console-and-mqtt-publish.c, /src/samples/mqtt/mqtt-publish.c, and /src/samples/mqtt/mqtt-subscribe.c.

Referenced by _can_read(), create_location_obj(), enabled(), found_device(), main(), mytype_func(), on_connect(), on_disconnect(), reader_open(), and startup().

bool sol_timeout_del ( struct sol_timeout handle)

Deletes the given timeout.

If it's necessary to keep a created timeout from being called, this function can delete it.

Parameters
handleThe timeout to delete
Returns
True if the timeout was deleted, false if the handle is invalid or if it had been marked as removed already
Note
MT-safe if compiled with threads support.
Examples:
/src/samples/coap/lwm2m-client.c, /src/samples/common/linux-micro-init.c, /src/samples/common/platform-simple.c, /src/samples/common/uart.c, /src/samples/design_patterns/stream_sample.c, /src/samples/flow/c-api/custom-node-types.c, /src/samples/flow/c-api/simple-c-type.c, /src/samples/iio+network/iio-gyroscope-console-and-mqtt-publish.c, and /src/samples/mqtt/mqtt-publish.c.

Referenced by create_location_obj(), del_location_obj(), main(), my_stream_api_close(), mytype_func(), reader_close(), and shutdown().

Variable Documentation

const struct sol_mainloop_implementation* SOL_MAINLOOP_IMPLEMENTATION_DEFAULT

Pointer to Soletta's internal mainloop implementation, that is the default to be used if no other is set.