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
sol-mainloop.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include "sol-common-buildopts.h"
22 
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <time.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
301 int sol_init(void);
302 
312 int sol_run(void);
313 
321 void sol_quit(void);
322 
333 void sol_quit_with_code(int return_code);
334 
343 void sol_shutdown(void);
344 
350 struct sol_timeout;
351 typedef struct sol_timeout sol_timeout;
352 
367 struct sol_timeout *sol_timeout_add(uint32_t timeout_ms, bool (*cb)(void *data), const void *data);
368 
382 bool sol_timeout_del(struct sol_timeout *handle);
383 
391 struct sol_idle;
392 typedef struct sol_idle sol_idle;
393 
411 struct sol_idle *sol_idle_add(bool (*cb)(void *data), const void *data);
412 
423 bool sol_idle_del(struct sol_idle *handle);
424 
432 #ifndef SOL_MAINLOOP_FD_ENABLED
433 /* keep doxygen happy */
434 #define SOL_MAINLOOP_FD_ENABLED
435 #undef SOL_MAINLOOP_FD_ENABLED
436 #endif
437 
438 #ifdef SOL_MAINLOOP_FD_ENABLED
439 
447 enum sol_fd_flags {
448  SOL_FD_FLAGS_NONE = 0,
452  SOL_FD_FLAGS_IN = (1 << 0),
456  SOL_FD_FLAGS_OUT = (1 << 1),
460  SOL_FD_FLAGS_PRI = (1 << 2),
466  SOL_FD_FLAGS_ERR = (1 << 3),
472  SOL_FD_FLAGS_HUP = (1 << 4),
478  SOL_FD_FLAGS_NVAL = (1 << 5)
479 };
480 
485 struct sol_fd;
486 typedef struct sol_fd sol_fd;
487 
503 struct sol_fd *sol_fd_add(int fd, uint32_t flags, bool (*cb)(void *data, int fd, uint32_t active_flags), const void *data);
504 
514 bool sol_fd_del(struct sol_fd *handle);
515 
526 bool sol_fd_set_flags(struct sol_fd *handle, uint32_t flags);
527 
538 bool sol_fd_remove_flags(struct sol_fd *handle, uint32_t flags);
539 
550 bool sol_fd_add_flags(struct sol_fd *handle, uint32_t flags);
551 
561 uint32_t sol_fd_get_flags(const struct sol_fd *handle);
562 #endif
563 
564 
572 #ifndef SOL_MAINLOOP_FORK_WATCH_ENABLED
573 /* keep doxygen happy */
574 #define SOL_MAINLOOP_FORK_WATCH_ENABLED
575 #undef SOL_MAINLOOP_FORK_WATCH_ENABLED
576 #endif
577 
578 #ifdef SOL_MAINLOOP_FORK_WATCH_ENABLED
579 
586 struct sol_child_watch;
587 typedef struct sol_child_watch sol_child_watch;
588 
605 struct sol_child_watch *sol_child_watch_add(uint64_t pid, void (*cb)(void *data, uint64_t pid, int status), const void *data);
606 
620 bool sol_child_watch_del(struct sol_child_watch *handle);
621 #endif
622 
638 #ifndef SOL_NO_API_VERSION
639 /* keep doxygen happy */
640 #define SOL_NO_API_VERSION
641 #undef SOL_NO_API_VERSION
642 #endif
643 
658 #ifndef SOL_SET_API_VERSION
659 /* keep doxygen happy */
660 #define SOL_SET_API_VERSION(expression)
661 #undef SOL_SET_API_VERSION
662 #endif
663 
667 typedef struct sol_mainloop_source_type {
668 #ifndef SOL_NO_API_VERSION
669 #define SOL_MAINLOOP_SOURCE_TYPE_API_VERSION (1)
673  uint16_t api_version;
674 #endif
675 
697  bool (*prepare)(void *data);
698 
721  bool (*get_next_timeout)(void *data, struct timespec *timeout);
722 
736  bool (*check)(void *data);
737 
744  void (*dispatch)(void *data);
745 
754  void (*dispose)(void *data);
756 
761 struct sol_mainloop_source;
763 
814 struct sol_mainloop_source *sol_mainloop_add_source(const struct sol_mainloop_source_type *type, const void *data);
815 
826 void sol_mainloop_del_source(struct sol_mainloop_source *handle);
827 
841 void *sol_mainloop_source_get_data(const struct sol_mainloop_source *handle);
842 
847 #ifndef SOL_NO_API_VERSION
848 #define SOL_MAINLOOP_IMPLEMENTATION_API_VERSION (1)
852  uint16_t api_version;
853 #endif
854 
862  int (*init)(void);
863 
871  void (*shutdown)(void);
872 
881  void (*run)(void);
882 
894  void (*quit)(void);
895 
918  void *(*timeout_add)(uint32_t timeout_ms, bool (*cb)(void *data), const void *data);
919 
933  bool (*timeout_del)(void *handle);
934 
962  void *(*idle_add)(bool (*cb)(void *data), const void *data);
963 
977  bool (*idle_del)(void *handle);
978 
979 #ifdef SOL_MAINLOOP_FD_ENABLED
980 
1007  void *(*fd_add)(int fd, uint32_t flags, bool (*cb)(void *data, int fd, uint32_t active_flags), const void *data);
1008 
1027  bool (*fd_del)(void *handle);
1028 
1044  bool (*fd_set_flags)(void *handle, uint32_t flags);
1045 
1061  uint32_t (*fd_get_flags)(const void *handle);
1062 #endif
1063 
1064 #ifdef SOL_MAINLOOP_FORK_WATCH_ENABLED
1065 
1085  void *(*child_watch_add)(uint64_t pid, void (*cb)(void *data, uint64_t pid, int status), const void *data);
1086 
1104  bool (*child_watch_del)(void *handle);
1105 #endif
1106 
1124  void *(*source_add)(const struct sol_mainloop_source_type *type, const void *data);
1125 
1137  void (*source_del)(void *handle);
1138 
1149  void *(*source_get_data)(const void *handle);
1151 
1152 
1158 
1172 
1206 
1212 int sol_argc(void);
1213 
1219 char **sol_argv(void);
1220 
1237 void sol_set_args(int argc, char *argv[]);
1238 
1246 typedef struct sol_main_callbacks {
1247 #ifndef SOL_NO_API_VERSION
1248 #define SOL_MAIN_CALLBACKS_API_VERSION (1)
1249  uint16_t api_version;
1250 #endif
1251  uint16_t flags;
1252  void (*startup)(void);
1253  void (*shutdown)(void);
1255 
1265 int sol_mainloop_default_main(const struct sol_main_callbacks *callbacks, int argc, char *argv[]);
1266 
1271 #ifdef __cplusplus
1272 }
1273 #endif
void * sol_mainloop_source_get_data(const struct sol_mainloop_source *handle)
Retrieve the user data (context) given to the source at creation time.
bool(* idle_del)(void *handle)
Function to be called to remove an idler to be dispatched.
Definition: sol-mainloop.h:977
int sol_init(void)
Initializes the Soletta library.
struct sol_main_callbacks sol_main_callbacks
Structure used to keep the application main callbacks.
const struct sol_mainloop_implementation * sol_mainloop_get_implementation(void)
Returns the current mainloop implementation in use.
int sol_run(void)
Runs the main loop.
void(* shutdown)(void)
Function to be called to shutdown (cleanup) the mainloop implementation.
Definition: sol-mainloop.h:871
void(* dispose)(void *data)
Function to be called when the source is deleted.
Definition: sol-mainloop.h:754
void sol_set_args(int argc, char *argv[])
Sets a new list of arguments and its count.
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 ...
struct sol_mainloop_source_type sol_mainloop_source_type
Structure representing the type of a source of mainloop events.
void sol_mainloop_del_source(struct sol_mainloop_source *handle)
Destroy a source of main loop events.
bool(* check)(void *data)
Function to be called to check if there are events to be dispatched.
Definition: sol-mainloop.h:736
Structure representing the type of a source of mainloop events.
Definition: sol-mainloop.h:667
bool sol_mainloop_set_implementation(const struct sol_mainloop_implementation *impl)
Changes the mainloop implementation.
bool(* timeout_del)(void *handle)
Function to be called to remove a timeout to be dispatched.
Definition: sol-mainloop.h:933
void sol_shutdown(void)
Shutdown Soletta library.
int sol_argc(void)
Gets the argument count the application was launched with, if any.
struct sol_idle * sol_idle_add(bool(*cb)(void *data), const void *data)
Adds a function to be called when the application goes idle.
struct sol_idle sol_idle
Handle for idlers.
Definition: sol-mainloop.h:392
uint16_t flags
Application flags.
Definition: sol-mainloop.h:1251
void(* startup)(void)
Application startup function.
Definition: sol-mainloop.h:1252
static FILE * fd
Definition: download.c:28
uint16_t api_version
API version.
Definition: sol-mainloop.h:1249
static struct sol_timeout * timeout
Definition: browse.c:36
void sol_quit(void)
Terminates the main loop.
struct sol_mainloop_source sol_mainloop_source
Structure of a Source of mainloop events.
Definition: sol-mainloop.h:762
struct sol_timeout * sol_timeout_add(uint32_t timeout_ms, bool(*cb)(void *data), const void *data)
Adds a function to be called periodically by the main loop.
void(* source_del)(void *handle)
Function to be called to remove mainloop event source.
Definition: sol-mainloop.h:1137
bool(* prepare)(void *data)
Function to be called to prepare to check for events.
Definition: sol-mainloop.h:697
struct sol_mainloop_implementation sol_mainloop_implementation
Structure representing a mainloop implementation (hooks).
bool sol_timeout_del(struct sol_timeout *handle)
Deletes the given timeout.
int(* init)(void)
Function to be called to initialize the mainloop implementation.
Definition: sol-mainloop.h:862
void(* quit)(void)
Function to be called to quit the mainloop.
Definition: sol-mainloop.h:894
struct sol_timeout sol_timeout
Handle for timers tracking the timeouts.
Definition: sol-mainloop.h:351
Structure representing a mainloop implementation (hooks).
Definition: sol-mainloop.h:846
Structure used to keep the application main callbacks.
Definition: sol-mainloop.h:1246
bool(* get_next_timeout)(void *data, struct timespec *timeout)
Function to be called to query the next timeout for the next event in this source.
Definition: sol-mainloop.h:721
void(* shutdown)(void)
Application shutdown function.
Definition: sol-mainloop.h:1253
void sol_quit_with_code(int return_code)
Terminates the main loop, setting a specific return code.
int sol_mainloop_default_main(const struct sol_main_callbacks *callbacks, int argc, char *argv[])
Helper function called by SOL_MAIN.
char ** sol_argv(void)
Gets the list of arguments the application was launched with, if any.
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.
void(* dispatch)(void *data)
Function to be called during main loop iterations if prepare() or check() returns true...
Definition: sol-mainloop.h:744
bool sol_idle_del(struct sol_idle *handle)
Deletes the given idler.
void(* run)(void)
Function to be called to run the mainloop.
Definition: sol-mainloop.h:881