Update date of copyright notices
[src/app-framework-binder.git] / bindings / samples / HelloWorld.c
index 77de24a..fef87d2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Copyright (C) 2015-2018 "IoT.bzh"
  * Author "Fulup Ar Foll"
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -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;
 }
@@ -169,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);
@@ -192,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;
@@ -204,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));
@@ -287,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);
@@ -322,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));
@@ -361,7 +382,7 @@ 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);
 }
@@ -391,21 +412,43 @@ static void broadcast(afb_req request)
        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
@@ -418,6 +461,7 @@ static const afb_verb_v2 verbs[]= {
   { .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 },
@@ -428,6 +472,9 @@ static const afb_verb_v2 verbs[]= {
   { .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}
 };