* Thus, in the case where 'object' should remain available after
* the function returns, the function 'json_object_get' shall be used.
*
+ * Calling this function is only forbidden during preinit.
+ *
* Returns the count of clients that received the event.
*/
int afb_daemon_broadcast_event(const char *name, struct json_object *object);
/*
* Creates an event of 'name' and returns it.
*
+ * Calling this function is only forbidden during preinit.
+ *
* See afb_event_is_valid to check if there is an error.
*/
struct afb_event afb_daemon_make_event(const char *name);
/*
* Tells that it requires the API of "name" to exist
* and if 'initialized' is not null to be initialized.
+ * Calling this function is only allowed within init.
* Returns 0 in case of success or -1 in case of error.
*/
int afb_daemon_require_api(const char *name, int initialized)
* Thus, in the case where 'object' should remain available after
* the function returns, the function 'json_object_get' shall be used.
*
+ * Calling this function is only forbidden during preinit.
+ *
* Returns the count of clients that received the event.
*/
static inline int afb_daemon_broadcast_event_v1(struct afb_daemon daemon, const char *name, struct json_object *object)
/*
* Creates an event of 'name' and returns it.
* 'daemon' MUST be the daemon given in interface when activating the binding.
+ *
+ * Calling this function is only forbidden during preinit.
+ *
+ * See afb_event_is_valid to check if there is an error.
*/
static inline struct afb_event afb_daemon_make_event_v1(struct afb_daemon daemon, const char *name)
{
/*
* Tells that it requires the API of "name" to exist
* and if 'initialized' is not null to be initialized.
+ * Calling this function is only allowed within init.
* Returns 0 in case of success or -1 in case of error.
*/
static inline int afb_daemon_require_api_v1(struct afb_daemon daemon, const char *name, int initialized)
* Thus, in the case where 'object' should remain available after
* the function returns, the function 'json_object_get' shall be used.
*
+ * Calling this function is only forbidden during preinit.
+ *
* Returns the count of clients that received the event.
*/
static inline int afb_daemon_broadcast_event_v2(const char *name, struct json_object *object)
/*
* Creates an event of 'name' and returns it.
+ *
+ * Calling this function is only forbidden during preinit.
+ *
+ * See afb_event_is_valid to check if there is an error.
*/
static inline struct afb_event afb_daemon_make_event_v2(const char *name)
{
/*
* Tells that it requires the API of "name" to exist
* and if 'initialized' is not null to be initialized.
+ * Calling this function is only allowed within init.
* Returns 0 in case of success or -1 in case of error.
*/
static inline int afb_daemon_require_api_v2(const char *name, int initialized)
if (desc->service != NULL) {
/* not an error when onneed */
if (onneed != 0)
- return 0;
+ goto done;
/* already started: it is an error */
ERROR("Service %s already started", desc->binding->v1.prefix);
if (init == NULL && onevent == NULL) {
/* not an error when onneed */
if (onneed != 0)
- return 0;
+ goto done;
/* no initialisation method */
ERROR("Binding %s is not a service", desc->binding->v1.prefix);
}
/* Starts the service */
+ desc->ditf.state = Daemon_Init;
rc = afb_svc_start_v1(desc->service, init);
if (rc < 0) {
/* initialisation error */
return rc;
}
+done:
+ desc->ditf.state = Daemon_Run;
return 0;
}
if (desc->service != NULL) {
/* not an error when onneed */
if (onneed != 0)
- return 0;
+ goto done;
/* already started: it is an error */
ERROR("Service %s already started", desc->binding->api);
if (start == NULL && onevent == NULL) {
/* not an error when onneed */
if (onneed != 0)
- return 0;
+ goto done;
/* no initialisation method */
ERROR("Binding %s is not a service", desc->binding->api);
}
/* Starts the service */
+ desc->ditf.state = Daemon_Init;
rc = afb_svc_start_v2(desc->service, start);
if (rc < 0) {
/* initialisation error */
return rc;
}
-
+done:
+ desc->ditf.state = Daemon_Run;
return 0;
}
char *event;
struct afb_ditf *ditf = closure;
+ /* check daemon state */
+ if (ditf->state == Daemon_Pre_Init) {
+
+ ERROR("[API %s] Bad call to 'afb_daemon_event_make(%s)', must not be in PreInit", ditf->api, name);
+ errno = EINVAL;
+ return (struct afb_event){ .itf = NULL, .closure = NULL };
+ }
+
/* makes the event name */
plen = strlen(ditf->api);
nlen = strlen(name);
char *event;
struct afb_ditf *ditf = closure;
+ /* check daemon state */
+ if (ditf->state == Daemon_Pre_Init) {
+
+ ERROR("[API %s] Bad call to 'afb_daemon_event_broadcast(%s, %s)', must not be in PreInit", ditf->api, name, json_object_to_json_string(object));
+ errno = EINVAL;
+ return 0;
+ }
+
/* makes the event name */
plen = strlen(ditf->api);
nlen = strlen(name);
static int require_api_cb(void *closure, const char *name, int initialized)
{
+ struct afb_ditf *ditf = closure;
+ if (ditf->state != Daemon_Init) {
+ ERROR("[API %s] Bad call to 'afb_daemon_require(%s, %d)', must be in Init", ditf->api, name, initialized);
+ errno = EINVAL;
+ return -1;
+ }
return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(main_apiset, name, 1);
}
void afb_ditf_init_v2(struct afb_ditf *ditf, const char *api, struct afb_binding_data_v2 *data)
{
ditf->version = 2;
+ ditf->state = Daemon_Pre_Init;
ditf->v2 = data;
data->daemon.closure = ditf;
afb_ditf_rename(ditf, api);
void afb_ditf_init_v1(struct afb_ditf *ditf, const char *api, struct afb_binding_interface_v1 *itf)
{
ditf->version = 1;
+ ditf->state = Daemon_Pre_Init;
ditf->v1 = itf;
itf->verbosity = verbosity;
itf->mode = AFB_MODE_LOCAL;
struct afb_binding_interface_v1;
struct afb_binding_data_v2;
+enum afb_ditf_state
+{
+ Daemon_Pre_Init,
+ Daemon_Init,
+ Daemon_Run
+};
+
struct afb_ditf
{
int version;
+ enum afb_ditf_state state;
const char *api;
union {
struct afb_binding_interface_v1 *v1;