Add a closure argument to dynamic verbs
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 22 Sep 2017 13:59:48 +0000 (15:59 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Mon, 9 Oct 2017 12:08:33 +0000 (14:08 +0200)
Also demonstrate the mix of api v2 with dynapi.

Change-Id: I95e8d32ac836590ce3f7b3f0b5f29e5574808976
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
bindings/samples/ave.c
include/afb/afb-dynapi-itf.h
include/afb/afb-dynapi.h
include/afb/afb-request-itf.h
src/afb-api-dyn.c
src/afb-api-dyn.h
src/afb-export.c

index 3ddfda6..e6b195b 100644 (file)
@@ -447,6 +447,18 @@ static const struct {
   { .verb=NULL}
 };
 
+static void pingoo(afb_req req)
+{
+       json_object *args = afb_req_json(req);
+       afb_req_success_f(req, json_object_get(args), "You reached pingoo \\o/ nice args: %s", json_object_to_json_string(args));
+}
+
+static const afb_verb_v2 verbsv2[]= {
+  { .verb="pingoo",      .callback=pingoo },
+  { .verb="ping",      .callback=pingoo },
+  { .verb=NULL}
+};
+
 static const char *apis[] = { "ave", "hi", "salut", NULL };
 
 static int api_preinit(void *closure, afb_dynapi *dynapi)
@@ -458,8 +470,9 @@ static int api_preinit(void *closure, afb_dynapi *dynapi)
        afb_dynapi_on_init(dynapi, init);
        afb_dynapi_on_event(dynapi, onevent);
 
+       rc = afb_dynapi_set_verbs_v2(dynapi, verbsv2);
        for (i = rc = 0; verbs[i].verb && rc >= 0 ; i++) {
-               rc = afb_dynapi_add_verb(dynapi, verbs[i].verb, NULL, verbs[i].callback, NULL, 0);
+               rc = afb_dynapi_add_verb(dynapi, verbs[i].verb, NULL, verbs[i].callback, (void*)(intptr_t)i, NULL, 0);
        }
        afb_dynapi_seal(dynapi);
        return rc;
index dd291ae..38978a2 100644 (file)
@@ -140,6 +140,7 @@ struct afb_dynapi_itf
                const char *verb,
                const char *info,
                void (*callback)(struct afb_request *request),
+               void *vcbdata,
                const struct afb_auth *auth,
                uint32_t session);
 
index 3d7c146..edae491 100644 (file)
@@ -245,10 +245,11 @@ static inline int afb_dynapi_add_verb(
        const char *verb,
        const char *info,
        void (*callback)(struct afb_request *request),
+       void *vcbdata,
        const struct afb_auth *auth,
        uint32_t session)
 {
-       return dynapi->itf->api_add_verb(dynapi, verb, info, callback, auth, session);
+       return dynapi->itf->api_add_verb(dynapi, verb, info, callback, vcbdata, auth, session);
 }
 
 
index b9e4366..eb7c534 100644 (file)
@@ -53,6 +53,10 @@ struct afb_request
 
        /* current dynapi if dynapi (is NULL for bindings v1 and v2) */
        struct afb_dynapi *dynapi;
+
+       /* closure associated with the callback processing the verb of the request
+        * as given at its declaration */
+       void *vcbdata;
 };
 
 /*
index 8802210..e829f8b 100644 (file)
@@ -58,11 +58,14 @@ int afb_api_dyn_add_verb(
                const char *verb,
                const char *info,
                void (*callback)(struct afb_request *request),
+               void *vcbdata,
                const struct afb_auth *auth,
                uint32_t session)
 {
        struct afb_api_dyn_verb *v, **vv;
 
+       afb_api_dyn_sub_verb(dynapi, verb);
+
        vv = realloc(dynapi->verbs, (1 + dynapi->count) * sizeof *vv);
        if (!vv)
                goto oom;
@@ -73,6 +76,7 @@ int afb_api_dyn_add_verb(
                goto oom;
 
        v->callback = callback;
+       v->vcbdata = vcbdata;
        v->auth = auth;
        v->session = session;
 
@@ -114,7 +118,7 @@ int afb_api_dyn_sub_verb(
 static void call_cb(void *closure, struct afb_xreq *xreq)
 {
        struct afb_api_dyn *dynapi = closure;
-       struct afb_api_dyn_verb **verbs;
+       struct afb_api_dyn_verb **verbs, *v;
        const struct afb_verb_v2 *verbsv2;
        int i;
        const char *name;
@@ -126,7 +130,9 @@ static void call_cb(void *closure, struct afb_xreq *xreq)
        verbs = dynapi->verbs;
        i = dynapi->count;
        while (i) {
-               if (!strcasecmp(verbs[--i]->verb, name)) {
+               v = verbs[--i];
+               if (!strcasecmp(v->verb, name)) {
+                       xreq->request.vcbdata = v->vcbdata;
                        afb_xreq_call_verb_vdyn(xreq, verbs[i]);
                        return;
                }
index ea184df..61180e2 100644 (file)
@@ -27,6 +27,7 @@ struct afb_verb_v2;
 struct afb_api_dyn_verb
 {
        void (*callback)(struct afb_request *request);
+       void *vcbdata;
        const struct afb_auth *auth;
        const char *info;
        int session;
@@ -46,6 +47,7 @@ extern int afb_api_dyn_add_verb(
                const char *verb,
                const char *info,
                void (*callback)(struct afb_request *request),
+               void *vcbdata,
                const struct afb_auth *auth,
                uint32_t session);
 
index 5688cfe..f8e4be7 100644 (file)
@@ -841,13 +841,14 @@ static int api_add_verb_cb(
                const char *verb,
                const char *info,
                void (*callback)(struct afb_request *request),
+               void *vcbdata,
                const struct afb_auth *auth,
                uint32_t session)
 {
        struct afb_export *export = from_dynapi(dynapi);
 
        if (export->apidyn)
-               return afb_api_dyn_add_verb(export->apidyn, verb, info, callback, auth, session);
+               return afb_api_dyn_add_verb(export->apidyn, verb, info, callback, vcbdata, auth, session);
 
        errno = EPERM;
        return -1;
@@ -904,11 +905,12 @@ static int hooked_api_add_verb_cb(
                const char *verb,
                const char *info,
                void (*callback)(struct afb_request *request),
+               void *vcbdata,
                const struct afb_auth *auth,
                uint32_t session)
 {
        /* TODO */
-       return api_add_verb_cb(dynapi, verb, info, callback, auth, session);
+       return api_add_verb_cb(dynapi, verb, info, callback, vcbdata, auth, session);
 }
 
 static int hooked_api_sub_verb_cb(