X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=bindings%2Fsamples%2FHelloWorld.c;h=134ea6207c8d540963c9225d064806a442a14056;hb=8b71d1123cee70df8e25558a0e1636c5748fb3b3;hp=e2f38ee491e7ede8cb1601ad5cb11bbf2439b9c4;hpb=acf0c66981ef55bf212352442e2673fdfe4a5505;p=src%2Fapp-framework-binder.git diff --git a/bindings/samples/HelloWorld.c b/bindings/samples/HelloWorld.c index e2f38ee4..134ea620 100644 --- a/bindings/samples/HelloWorld.c +++ b/bindings/samples/HelloWorld.c @@ -59,7 +59,7 @@ static int event_del(const char *tag) *p = e->next; /* destroys */ - afb_event_drop(e->event); + afb_event_unref(e->event); free(e); return 0; } @@ -109,6 +109,13 @@ static int event_push(struct json_object *args, const char *tag) return e ? afb_event_push(e->event, json_object_get(args)) : -1; } +static int event_broadcast(struct json_object *args, const char *tag) +{ + struct event *e; + e = event_get(tag); + return e ? afb_event_broadcast(e->event, json_object_get(args)) : -1; +} + // Sample Generic Ping Debug API static void ping(afb_req request, json_object *jresp, const char *tag) { @@ -162,10 +169,10 @@ static void pingJson (afb_req request) { ping(request, jresp, "pingJson"); } -static void subcallcb (void *prequest, int iserror, json_object *object) +static void subcallcb (void *prequest, int status, json_object *object) { afb_req request = afb_req_unstore(prequest); - if (iserror) + if (status < 0) afb_req_fail(request, "failed", json_object_to_json_string(object)); else afb_req_success(request, json_object_get(object), NULL); @@ -185,6 +192,27 @@ static void subcall (afb_req request) afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request)); } +static void subcallreqcb (void *prequest, int status, json_object *object, afb_req request) +{ + if (status < 0) + afb_req_fail(request, "failed", json_object_to_json_string(object)); + else + afb_req_success(request, json_object_get(object), NULL); +} + +static void subcallreq (afb_req request) +{ + const char *api = afb_req_value(request, "api"); + const char *verb = afb_req_value(request, "verb"); + const char *args = afb_req_value(request, "args"); + json_object *object = api && verb && args ? json_tokener_parse(args) : NULL; + + if (object == NULL) + afb_req_fail(request, "failed", "bad arguments"); + else + afb_req_subcall_req(request, api, verb, object, subcallreqcb, NULL); +} + static void subcallsync (afb_req request) { int rc; @@ -197,7 +225,7 @@ static void subcallsync (afb_req request) afb_req_fail(request, "failed", "bad arguments"); else { rc = afb_req_subcall_sync(request, api, verb, object, &result); - if (rc) + if (rc >= 0) afb_req_success(request, result, NULL); else { afb_req_fail(request, "failed", json_object_to_json_string(result)); @@ -280,10 +308,10 @@ static void eventpush (afb_req request) json_object_put(object); } -static void callcb (void *prequest, int iserror, json_object *object) +static void callcb (void *prequest, int status, json_object *object) { afb_req request = afb_req_unstore(prequest); - if (iserror) + if (status < 0) afb_req_fail(request, "failed", json_object_to_json_string(object)); else afb_req_success(request, json_object_get(object), NULL); @@ -315,7 +343,7 @@ static void callsync (afb_req request) afb_req_fail(request, "failed", "bad arguments"); else { rc = afb_service_call_sync(api, verb, object, &result); - if (rc) + if (rc >= 0) afb_req_success(request, result, NULL); else { afb_req_fail(request, "failed", json_object_to_json_string(result)); @@ -354,49 +382,101 @@ static void exitnow (afb_req request) if (!json_object_object_get_ex(query,"reason",&l)) l = NULL; - REQ_NOTICE(request, "in phase of exiting with code %d, reason: %s", code, l ? json_object_get_string(l) : "unknown"); + AFB_REQ_NOTICE(request, "in phase of exiting with code %d, reason: %s", code, l ? json_object_get_string(l) : "unknown"); afb_req_success(request, NULL, NULL); exit(code); } +static void broadcast(afb_req request) +{ + const char *tag = afb_req_value(request, "tag"); + const char *name = afb_req_value(request, "name"); + const char *data = afb_req_value(request, "data"); + json_object *object = data ? json_tokener_parse(data) : NULL; + + if (tag != NULL) { + pthread_mutex_lock(&mutex); + if (0 > event_broadcast(object, tag)) + afb_req_fail(request, "failed", "broadcast error"); + else + afb_req_success(request, NULL, NULL); + pthread_mutex_unlock(&mutex); + } else if (name != NULL) { + if (0 > afb_daemon_broadcast_event(name, object)) + afb_req_fail(request, "failed", "broadcast error"); + else + afb_req_success(request, NULL, NULL); + } else { + afb_req_fail(request, "failed", "bad arguments"); + } + json_object_put(object); +} + +static void hasperm (afb_req request) +{ + const char *perm = afb_req_value(request, "perm"); + if (afb_req_has_permission(request, perm)) + afb_req_success_f(request, NULL, "permission %s granted", perm?:"(null)"); + else + afb_req_fail_f(request, "not-granted", "permission %s NOT granted", perm?:"(null)"); +} + +static void appid (afb_req request) +{ + char *aid = afb_req_get_application_id(request); + afb_req_success_f(request, aid ? json_object_new_string(aid) : NULL, "application is %s", aid?:"?"); + free(aid); +} + +static void uid (afb_req request) +{ + int uid = afb_req_get_uid(request); + afb_req_success_f(request, json_object_new_int(uid), "uid is %d", uid); +} + static int preinit() { - NOTICE("hello binding comes to live"); + AFB_NOTICE("hello binding comes to live"); return 0; } static int init() { - NOTICE("hello binding starting"); + AFB_NOTICE("hello binding starting"); return 0; } static void onevent(const char *event, struct json_object *object) { - NOTICE("received event %s(%s)", event, json_object_to_json_string(object)); + AFB_NOTICE("received event %s(%s)", event, json_object_to_json_string(object)); } // NOTE: this sample does not use session to keep test a basic as possible // in real application most APIs should be protected with AFB_SESSION_CHECK static const afb_verb_v2 verbs[]= { - { "ping" , pingSample , NULL, AFB_SESSION_NONE }, - { "pingfail" , pingFail , NULL, AFB_SESSION_NONE }, - { "pingnull" , pingNull , NULL, AFB_SESSION_NONE }, - { "pingbug" , pingBug , NULL, AFB_SESSION_NONE }, - { "pingJson" , pingJson , NULL, AFB_SESSION_NONE }, - { "pingevent", pingEvent , NULL, AFB_SESSION_NONE }, - { "subcall", subcall , NULL, AFB_SESSION_NONE }, - { "subcallsync", subcallsync, NULL, AFB_SESSION_NONE }, - { "eventadd", eventadd , NULL, AFB_SESSION_NONE }, - { "eventdel", eventdel , NULL, AFB_SESSION_NONE }, - { "eventsub", eventsub , NULL, AFB_SESSION_NONE }, - { "eventunsub", eventunsub , NULL, AFB_SESSION_NONE }, - { "eventpush", eventpush , NULL, AFB_SESSION_NONE }, - { "call", call , NULL, AFB_SESSION_NONE }, - { "callsync", callsync , NULL, AFB_SESSION_NONE }, - { "verbose", verbose , NULL, AFB_SESSION_NONE }, - { "exit", exitnow , NULL, AFB_SESSION_NONE }, - { NULL} + { .verb="ping", .callback=pingSample }, + { .verb="pingfail", .callback=pingFail }, + { .verb="pingnull", .callback=pingNull }, + { .verb="pingbug", .callback=pingBug }, + { .verb="pingJson", .callback=pingJson }, + { .verb="pingevent", .callback=pingEvent }, + { .verb="subcall", .callback=subcall }, + { .verb="subcallreq", .callback=subcallreq }, + { .verb="subcallsync", .callback=subcallsync }, + { .verb="eventadd", .callback=eventadd }, + { .verb="eventdel", .callback=eventdel }, + { .verb="eventsub", .callback=eventsub }, + { .verb="eventunsub", .callback=eventunsub }, + { .verb="eventpush", .callback=eventpush }, + { .verb="call", .callback=call }, + { .verb="callsync", .callback=callsync }, + { .verb="verbose", .callback=verbose }, + { .verb="broadcast", .callback=broadcast }, + { .verb="hasperm", .callback=hasperm }, + { .verb="appid", .callback=appid }, + { .verb="uid", .callback=uid }, + { .verb="exit", .callback=exitnow }, + { .verb=NULL} }; const afb_binding_v2 afbBindingV2 = {