afb-ditf: track daemon state
[src/app-framework-binder.git] / src / afb-ditf.c
index 6613676..28cd46a 100644 (file)
 #include "afb-evt.h"
 #include "afb-common.h"
 #include "afb-xreq.h"
+#include "afb-api.h"
+#include "afb-apiset.h"
 #include "afb-hook.h"
 #include "jobs.h"
 #include "verbose.h"
 
+extern struct afb_apiset *main_apiset;
+
 /**********************************************
 * normal flow
 **********************************************/
@@ -61,6 +65,14 @@ static struct afb_event event_make_cb(void *closure, const char *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_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);
@@ -79,6 +91,14 @@ static int event_broadcast_cb(void *closure, const char *name, struct json_objec
        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);
@@ -106,6 +126,17 @@ static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
        return afb_xreq_unstore(sreq);
 }
 
+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);
+}
+
 /**********************************************
 * hooked flow
 **********************************************/
@@ -192,6 +223,15 @@ static struct afb_req hooked_unstore_req_cb(void *closure, struct afb_stored_req
        return unstore_req_cb(closure, sreq);
 }
 
+static int hooked_require_api_cb(void *closure, const char *name, int initialized)
+{
+       int result;
+       struct afb_ditf *ditf = closure;
+       afb_hook_ditf_require_api(ditf, name, initialized);
+       result = require_api_cb(closure, name, initialized);
+       return afb_hook_ditf_require_api_result(ditf, name, initialized, result);
+}
+
 /**********************************************
 * vectors
 **********************************************/
@@ -206,7 +246,8 @@ static const struct afb_daemon_itf daemon_itf = {
        .rootdir_get_fd = afb_common_rootdir_get_fd,
        .rootdir_open_locale = rootdir_open_locale_cb,
        .queue_job = queue_job_cb,
-       .unstore_req = unstore_req_cb
+       .unstore_req = unstore_req_cb,
+       .require_api = require_api_cb
 };
 
 static const struct afb_daemon_itf hooked_daemon_itf = {
@@ -220,12 +261,14 @@ static const struct afb_daemon_itf hooked_daemon_itf = {
        .rootdir_get_fd = hooked_rootdir_get_fd,
        .rootdir_open_locale = hooked_rootdir_open_locale_cb,
        .queue_job = hooked_queue_job_cb,
-       .unstore_req = hooked_unstore_req_cb
+       .unstore_req = hooked_unstore_req_cb,
+       .require_api = hooked_require_api_cb
 };
 
 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);
@@ -234,6 +277,7 @@ void afb_ditf_init_v2(struct afb_ditf *ditf, const char *api, struct afb_binding
 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;