aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/agl-identity-binding.c163
1 files changed, 89 insertions, 74 deletions
diff --git a/src/agl-identity-binding.c b/src/agl-identity-binding.c
index c8c5924..70c195c 100644
--- a/src/agl-identity-binding.c
+++ b/src/agl-identity-binding.c
@@ -24,19 +24,15 @@
#include <json-c/json.h>
-#define AFB_BINDING_VERSION 2
+#define AFB_BINDING_VERSION 3
#include <afb/afb-binding.h>
#include "agl-forgerock.h"
-#ifndef NULL
-#define NULL 0
-#endif
-
-static struct afb_event event;
-
-static struct json_object *current_identity;
+static afb_event_t event;
+static json_object *current_identity;
+/* default vin corresponds to Toyota Camry 2016, 429 */
static const char default_vin[] = "4T1BF1FK5GU260429";
static char *vin;
@@ -46,7 +42,7 @@ static struct json_object *readjson(int fd)
{
char *buffer;
struct stat s;
- struct json_object *result = NULL;
+ json_object *result = NULL;
int rc;
rc = fstat(fd, &s);
@@ -54,11 +50,11 @@ static struct json_object *readjson(int fd)
buffer = alloca((size_t)(s.st_size)+1);
if (read(fd, buffer, (size_t)s.st_size) == (ssize_t)s.st_size) {
buffer[s.st_size] = 0;
- //AFB_NOTICE("Config file: %s", buffer);
+ //AFB_API_NOTICE(afbBindingV3root, "Config file: %s", buffer);
result = json_tokener_parse(buffer);
//if (!result)
//{
- // AFB_ERROR("Config file is not a valid JSON: %s", json_tokener_error_desc(json_tokener_get_error(NULL)));
+ // AFB_API_ERROR(afbBindingV3root, "Config file is not a valid JSON: %s", json_tokener_error_desc(json_tokener_get_error(NULL)));
//}
}
}
@@ -70,7 +66,7 @@ static struct json_object *readjson(int fd)
static struct json_object *get_global_config(const char *name, const char *locale)
{
int fd = afb_daemon_rootdir_open_locale(name, O_RDONLY, locale);
- if (fd < 0) AFB_ERROR("Config file not found: %s", name);
+ if (fd < 0) AFB_API_ERROR(afbBindingV3root, "Config file not found: %s", name);
return fd < 0 ? NULL : readjson(fd);
}
@@ -80,7 +76,8 @@ static struct json_object *get_local_config(const char *name)
return fd < 0 ? NULL : readjson(fd);
}
-static void confsetstr(struct json_object *conf, const char *name, char **value, const char *def)
+static void confsetstr(struct json_object *conf, const char *name,
+ char **value, const char *def)
{
struct json_object *v;
const char *s;
@@ -137,7 +134,7 @@ static void do_login(struct json_object *desc)
const char* b = json_object_to_json_string(desc);
if (strcmp(a, b) == 0)
{
- AFB_NOTICE("Reloging of the same user.");
+ AFB_API_NOTICE(afbBindingV3root, "Reloging of the same user.");
return; // Switching from one user to the same user -> do nothing
}
}
@@ -145,7 +142,8 @@ static void do_login(struct json_object *desc)
struct json_object *object;
/* switching the user */
- AFB_INFO("Switching to user %s", desc ? json_object_to_json_string(desc) : "null");
+ AFB_API_INFO(afbBindingV3root, "Switching to user %s",
+ desc ? json_object_to_json_string(desc) : "null");
object = current_identity;
current_identity = json_object_get(desc);
json_object_put(object);
@@ -159,7 +157,7 @@ static void do_logout()
{
struct json_object *object;
- AFB_INFO("Switching to no user");
+ AFB_API_INFO(afbBindingV3root, "Switching to no user");
object = current_identity;
current_identity = 0;
json_object_put(object);
@@ -170,7 +168,7 @@ static void do_logout()
static void on_forgerock_data(struct json_object *data, const char *error)
{
if (error) {
- AFB_ERROR("Can't get data: %s", error);
+ AFB_API_ERROR(afbBindingV3root, "Can't get data: %s", error);
} else {
do_login(data);
}
@@ -178,70 +176,76 @@ static void on_forgerock_data(struct json_object *data, const char *error)
/****************************************************************/
-static void subscribe (struct afb_req request)
+static void subscribe (afb_req_t request)
{
- int rc;
-
- rc = afb_req_subscribe(request, event);
- if (rc < 0)
- afb_req_fail(request, "failed", "subscribtion failed");
+ if (afb_req_subscribe(request, event)) {
+ AFB_REQ_ERROR(request, "subscribe error");
+ afb_req_reply(request, NULL, "failed", "subscribtion failed");
+ }
else
- afb_req_success(request, NULL, NULL);
+ afb_req_reply(request, NULL, NULL, NULL);
}
-static void unsubscribe (struct afb_req request)
+static void unsubscribe (afb_req_t request)
{
afb_req_unsubscribe(request, event);
- afb_req_success(request, NULL, NULL);
+ afb_req_reply(request, NULL, NULL, NULL);
}
-static void logout (struct afb_req request)
+static void logout (afb_req_t request)
{
do_logout();
- afb_req_success(request, NULL, NULL);
+ afb_req_reply(request, NULL, NULL, NULL);
}
-static void fake_login (struct afb_req request)
+static void fake_login (afb_req_t request)
{
struct json_object *desc = afb_req_json(request);
do_logout();
if (desc)
do_login(desc);
- afb_req_success(request, NULL, NULL);
+ afb_req_reply(request, NULL, NULL, NULL);
}
-static void get (struct afb_req request)
+static void get (afb_req_t request)
{
- afb_req_success(request, json_object_get(current_identity), NULL);
+ afb_req_reply(request, json_object_get(current_identity), NULL, NULL);
}
-static void on_nfc_subscribed(void *closure, int status, struct json_object *result)
+static void on_nfc_subscribed(void *closure, struct json_object *result,
+ const char *error, const char *info,
+ afb_api_t api)
{
- if(status)
- AFB_ERROR("Failed to subscribe to nfc events.");
+ if (error)
+ AFB_API_ERROR(api, "Failed to subscribe to nfc events.");
+ else
+ AFB_API_DEBUG(api, "Subscribed to nfc events.");
}
-static int service_init()
+static int service_init(afb_api_t api)
{
struct json_object *jrequest = NULL;
agl_forgerock_setcb(on_forgerock_data);
- event = afb_daemon_make_event("event");
- if (!afb_event_is_valid(event))
- return -1;
+ event = afb_api_make_event(api, "event");
+ if (!afb_event_is_valid(event)) {
+ AFB_API_ERROR(api, "Failed to create event");
+ return -1;
+ }
+
readconfig();
- if (afb_daemon_require_api("nfc", 1))
+ if (afb_api_require_api(api, "nfc", 1))
return -1;
- if (afb_daemon_require_api("persistence", 1))
+ if (afb_api_require_api(api, "persistence", 1))
return -1;
jrequest = json_object_new_object();
json_object_object_add(jrequest, "value", json_object_new_string("presence"));
- afb_service_call("nfc", "subscribe", jrequest, on_nfc_subscribed, NULL);
+ afb_api_call(api, "nfc", "subscribe", jrequest, on_nfc_subscribed, NULL);
return 0;
}
@@ -254,25 +258,29 @@ static void on_nfc_target_add(struct json_object *object)
if (json_object_object_get_ex(object, "uid", &json_uid))
{
uid = json_object_get_string(json_uid);
- AFB_NOTICE("nfc tag detected, call forgerock with vincode=%s and key=%s", vin ? vin : default_vin, uid);
+ AFB_API_NOTICE(afbBindingV3root,
+ "nfc tag detected, call forgerock with vincode=%s and key=%s",
+ vin ? vin : default_vin, uid);
send_event_object("incoming", uid, uid);
agl_forgerock_download_request(vin ? vin : default_vin, "nfc", uid);
}
- else AFB_ERROR("nfc target add event is received but no UID found: %s", json_object_to_json_string(object));
+ else AFB_API_ERROR(afbBindingV3root,
+ "nfc target add event is received but no UID found: %s",
+ json_object_to_json_string(object));
}
-static void onevent(const char *event, struct json_object *object)
+static void onevent(afb_api_t api, const char *event, struct json_object *object)
{
- AFB_NOTICE("Received event: %s", event);
+ AFB_API_NOTICE(api, "Received event: %s", event);
if (!strcmp("nfc/presence", event))
{
on_nfc_target_add(object);
return;
}
- AFB_WARNING("Unhandled event: %s", event);
+ AFB_API_WARNING(api, "Unhandled event: %s", event);
}
-static void fake_auth(struct afb_req req)
+static void fake_auth(afb_req_t req)
{
struct json_object* req_object;
struct json_object* kind_object;
@@ -280,41 +288,48 @@ static void fake_auth(struct afb_req req)
req_object = afb_req_json(req);
- if (!json_object_object_get_ex(req_object, "kind", &kind_object))
+ if ((!json_object_object_get_ex(req_object, "kind", &kind_object)) ||
+ (!json_object_object_get_ex(req_object, "key", &key_object)))
{
- afb_req_fail(req, "Missing arg: kind", NULL);
- return;
- }
-
- if (!json_object_object_get_ex(req_object, "key", &key_object))
- {
- afb_req_fail(req, "Missing arg: key", NULL);
- return;
+ AFB_REQ_ERROR(req, "bad request: %s",
+ json_object_get_string(req_object));
+ afb_req_reply(req, NULL, "Missing arg", NULL);
}
+ else {
+ const char* kind = json_object_get_string(kind_object);
+ const char* key = json_object_get_string(key_object);
- const char* kind = json_object_get_string(kind_object);
- const char* key = json_object_get_string(key_object);
+ AFB_REQ_NOTICE(req, "kind: %s, key: %s", kind, key);
+
+ send_event_object("incoming", key, key);
+ agl_forgerock_download_request(vin ? vin : default_vin, kind, key);
- send_event_object("incoming", key, key);
- agl_forgerock_download_request(vin ? vin : default_vin, kind, key);
-
- afb_req_success(req, NULL, "fake auth success!");
+ afb_req_reply(req, NULL, NULL, "fake auth success!");
+ }
}
-// 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 struct afb_verb_v2 verbs[]=
+/*
+ * NOTE: this sample does not use session to keep the test as basic as possible
+ * in real application most APIs should be protected with AFB_SESSION_CHECK
+ */
+const afb_verb_t verbs[]=
{
- {"subscribe" , subscribe , NULL, "subscribe to events" , AFB_SESSION_NONE },
- {"unsubscribe", unsubscribe , NULL, "unsubscribe to events" , AFB_SESSION_NONE },
- {"fake-login" , fake_login , NULL, "fake a login" , AFB_SESSION_NONE },
- {"logout" , logout , NULL, "log the current user out", AFB_SESSION_NONE },
- {"get" , get , NULL, "get data" , AFB_SESSION_NONE },
- {"fake-auth" , fake_auth , NULL, "fake an authentication" , AFB_SESSION_NONE },
- {NULL}
+ {.verb = "subscribe" , .callback = subscribe , .auth = NULL,
+ .info = "subscribe to events" , .session = AFB_SESSION_NONE },
+ {.verb = "unsubscribe", .callback = unsubscribe , .auth = NULL,
+ .info = "unsubscribe to events" , .session = AFB_SESSION_NONE },
+ {.verb = "fake-login" , .callback = fake_login , .auth = NULL,
+ .info = "fake a login" , .session = AFB_SESSION_NONE },
+ {.verb = "logout" , .callback = logout , .auth = NULL,
+ .info = "log the current user out", .session = AFB_SESSION_NONE },
+ {.verb = "get" , .callback = get , .auth = NULL,
+ .info = "get data" , .session = AFB_SESSION_NONE },
+ {.verb = "fake-auth" , .callback = fake_auth , .auth = NULL,
+ .info = "fake an authentication" , .session = AFB_SESSION_NONE },
+ {NULL}
};
-const struct afb_binding_v2 afbBindingV2 =
+const afb_binding_t afbBindingExport =
{
.api = "identity",
.specification = NULL,