/*
- * Copyright (C) 2016 "IoT.bzh"
+ * Copyright (C) 2016, 2017 "IoT.bzh"
* Author: José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
#include <stdarg.h>
#include <stdio.h>
-#include <afb/afb-event-itf.h>
+#include "afb-event-itf.h"
/* avoid inclusion of <json-c/json.h> */
struct json_object;
* It records the functions to be called for the request.
* Don't use this structure directly.
* Use the helper functions documented below.
- */
+ */
struct afb_req_itf {
/* CAUTION: respect the order, add at the end */
int (*unsubscribe)(void *closure, struct afb_event event);
void (*subcall)(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
+ int (*subcallsync)(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result);
};
/*
void *closure; /* the closure argument for functions of 'itf' */
};
+/*
+ * Checks wether the request 'req' is valid or not.
+ *
+ * Returns 0 if not valid or 1 if valid.
+ */
+static inline int afb_req_is_valid(struct afb_req req)
+{
+ return req.itf != NULL;
+}
+
/*
* Gets from the request 'req' the argument of 'name'.
* Returns a PLAIN structure of type 'struct afb_arg'.
*/
static inline struct afb_req *afb_req_store(struct afb_req req)
{
- struct afb_req *result = malloc(sizeof *result);
+ struct afb_req *result = (struct afb_req*)malloc(sizeof *result);
if (result != NULL) {
*result = req;
afb_req_addref(req);
req.itf->subcall(req.closure, api, verb, args, callback, closure);
}
+/*
+ * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
+ * This call is made in the context of the request 'req'.
+ * This call is synchronous, it waits untill completion of the request.
+ * It returns 0 on an error answer and returns 1 when no error was detected.
+ * The object pointed by 'result' is filled and must be released by the caller
+ * after its use by calling 'json_object_put'.
+ */
+static inline int afb_req_subcall_sync(struct afb_req req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
+{
+ return req.itf->subcallsync(req.closure, api, verb, args, result);
+}
+
/* internal use */
static inline const char *afb_req_raw(struct afb_req req, size_t *size)
{