diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-09-22 15:59:48 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-10-09 14:08:33 +0200 |
commit | f30be1ca9ce04cfab55b9e6ce25cec6952f7e21d (patch) | |
tree | 88a0d681faa3604202cfbda4a94584b950ec4a74 | |
parent | 9a304ebddac4deb4992ea931ea1131b639798b3b (diff) |
Add a closure argument to dynamic verbs
Also demonstrate the mix of api v2 with dynapi.
Change-Id: I95e8d32ac836590ce3f7b3f0b5f29e5574808976
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | bindings/samples/ave.c | 15 | ||||
-rw-r--r-- | include/afb/afb-dynapi-itf.h | 1 | ||||
-rw-r--r-- | include/afb/afb-dynapi.h | 3 | ||||
-rw-r--r-- | include/afb/afb-request-itf.h | 4 | ||||
-rw-r--r-- | src/afb-api-dyn.c | 10 | ||||
-rw-r--r-- | src/afb-api-dyn.h | 2 | ||||
-rw-r--r-- | src/afb-export.c | 6 |
7 files changed, 35 insertions, 6 deletions
diff --git a/bindings/samples/ave.c b/bindings/samples/ave.c index 3ddfda62..e6b195b8 100644 --- a/bindings/samples/ave.c +++ b/bindings/samples/ave.c @@ -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; diff --git a/include/afb/afb-dynapi-itf.h b/include/afb/afb-dynapi-itf.h index dd291ae8..38978a29 100644 --- a/include/afb/afb-dynapi-itf.h +++ b/include/afb/afb-dynapi-itf.h @@ -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); diff --git a/include/afb/afb-dynapi.h b/include/afb/afb-dynapi.h index 3d7c1469..edae4915 100644 --- a/include/afb/afb-dynapi.h +++ b/include/afb/afb-dynapi.h @@ -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); } diff --git a/include/afb/afb-request-itf.h b/include/afb/afb-request-itf.h index b9e4366a..eb7c5347 100644 --- a/include/afb/afb-request-itf.h +++ b/include/afb/afb-request-itf.h @@ -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; }; /* diff --git a/src/afb-api-dyn.c b/src/afb-api-dyn.c index 88022109..e829f8be 100644 --- a/src/afb-api-dyn.c +++ b/src/afb-api-dyn.c @@ -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; } diff --git a/src/afb-api-dyn.h b/src/afb-api-dyn.h index ea184df1..61180e27 100644 --- a/src/afb-api-dyn.h +++ b/src/afb-api-dyn.h @@ -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); diff --git a/src/afb-export.c b/src/afb-export.c index 5688cfee..f8e4be71 100644 --- a/src/afb-export.c +++ b/src/afb-export.c @@ -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( |