X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=include%2Fafb%2Fafb-req-itf.h;h=8c18a83177e6ed75040d26772fa3bf76070b3a91;hb=524ce4c40da3b25630dffdd80c5aca99364a3569;hp=5767c8539a39d503418363848a5fb2451351be56;hpb=18439204034fca982b3b65c2d581f80a9ee62f56;p=src%2Fapp-framework-binder.git diff --git a/include/afb/afb-req-itf.h b/include/afb/afb-req-itf.h index 5767c853..8c18a831 100644 --- a/include/afb/afb-req-itf.h +++ b/include/afb/afb-req-itf.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 "IoT.bzh" + * Copyright (C) 2016, 2017 "IoT.bzh" * Author: José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,13 +17,9 @@ #pragma once -#if !defined(_GNU_SOURCE) -# error "_GNU_SOURCE must be defined for using vasprintf" -#endif - -#include #include -#include + +#include "afb-event-itf.h" /* avoid inclusion of */ struct json_object; @@ -44,7 +40,7 @@ struct afb_arg { * 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 */ @@ -54,8 +50,8 @@ struct afb_req_itf { void (*success)(void *closure, struct json_object *obj, const char *info); void (*fail)(void *closure, const char *status, const char *info); - const char *(*raw)(void *closure, size_t *size); - void (*send)(void *closure, const char *buffer, size_t size); + void (*vsuccess)(void *closure, struct json_object *obj, const char *fmt, va_list args); + void (*vfail)(void *closure, const char *status, const char *fmt, va_list args); void *(*context_get)(void *closure); void (*context_set)(void *closure, void *value, void (*free_value)(void*)); @@ -65,16 +61,34 @@ struct afb_req_itf { void (*session_close)(void *closure); int (*session_set_LOA)(void *closure, unsigned level); + + int (*subscribe)(void *closure, struct afb_event event); + 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 (*vverbose)(void*closure, int level, const char *file, int line, const char * func, const char *fmt, va_list args); }; /* - * Describes the request by plugins from afb-daemon + * Describes the request by bindings from afb-daemon */ struct afb_req { const struct afb_req_itf *itf; /* the interface to use */ 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'. @@ -130,6 +144,10 @@ static inline struct json_object *afb_req_json(struct afb_req req) * The status of the reply is automatically set to "success". * Its send the object 'obj' (can be NULL) with an * informationnal comment 'info (can also be NULL). + * + * For convenience, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. */ static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info) { @@ -139,17 +157,31 @@ static inline void afb_req_success(struct afb_req req, struct json_object *obj, /* * Same as 'afb_req_success' but the 'info' is a formatting * string followed by arguments. + * + * For convenience, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. */ +static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...) __attribute__((format(printf, 3, 4))); static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...) { - char *message; va_list args; va_start(args, info); - if (info == NULL || vasprintf(&message, info, args) < 0) - message = NULL; + req.itf->vsuccess(req.closure, obj, info, args); va_end(args); - afb_req_success(req, obj, message); - free(message); +} + +/* + * Same as 'afb_req_success_f' but the arguments to the format 'info' + * are given as a variable argument list instance. + * + * For convenience, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args) +{ + req.itf->vsuccess(req.closure, obj, info, args); } /* @@ -170,21 +202,27 @@ static inline void afb_req_fail(struct afb_req req, const char *status, const ch * Same as 'afb_req_fail' but the 'info' is a formatting * string followed by arguments. */ +static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...) __attribute__((format(printf, 3, 4))); static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...) { - char *message; va_list args; va_start(args, info); - if (info == NULL || vasprintf(&message, info, args) < 0) - message = NULL; + req.itf->vfail(req.closure, status, info, args); va_end(args); - afb_req_fail(req, status, message); - free(message); } /* - * Gets the pointer stored by the plugin for the session of 'req'. - * When the plugin has not yet recorded a pointer, NULL is returned. + * Same as 'afb_req_fail_f' but the arguments to the format 'info' + * are given as a variable argument list instance. + */ +static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args) +{ + req.itf->vfail(req.closure, status, info, args); +} + +/* + * Gets the pointer stored by the binding for the session of 'req'. + * When the binding has not yet recorded a pointer, NULL is returned. */ static inline void *afb_req_context_get(struct afb_req req) { @@ -192,9 +230,9 @@ static inline void *afb_req_context_get(struct afb_req req) } /* - * Stores for the plugin the pointer 'context' to the session of 'req'. + * Stores for the binding the pointer 'context' to the session of 'req'. * The function 'free_context' will be called when the session is closed - * or if plugin stores an other pointer. + * or if binding stores an other pointer. */ static inline void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*)) { @@ -202,7 +240,7 @@ static inline void afb_req_context_set(struct afb_req req, void *context, void ( } /* - * Gets the pointer stored by the plugin for the session of 'req'. + * Gets the pointer stored by the binding for the session of 'req'. * If the stored pointer is NULL, indicating that no pointer was * already stored, afb_req_context creates a new context by calling * the function 'create_context' and stores it with the freeing function @@ -219,7 +257,7 @@ static inline void *afb_req_context(struct afb_req req, void *(*create_context)( } /* - * Frees the pointer stored by the plugin for the session of 'req' + * Frees the pointer stored by the binding for the session of 'req' * and sets it to NULL. * * Shortcut for: afb_req_context_set(req, NULL, NULL) @@ -277,7 +315,7 @@ static inline int afb_req_session_set_LOA(struct afb_req req, unsigned level) */ 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); @@ -299,15 +337,90 @@ static inline struct afb_req afb_req_unstore(struct afb_req *req) return result; } -/* internal use */ -static inline const char *afb_req_raw(struct afb_req req, size_t *size) +/* + * Establishes for the client link identified by 'req' a subscription + * to the 'event'. + * Returns 0 in case of successful subscription or -1 in case of error. + */ +static inline int afb_req_subscribe(struct afb_req req, struct afb_event event) +{ + return req.itf->subscribe(req.closure, event); +} + +/* + * Revokes the subscription established to the 'event' for the client + * link identified by 'req'. + * Returns 0 in case of successful subscription or -1 in case of error. + */ +static inline int afb_req_unsubscribe(struct afb_req req, struct afb_event event) +{ + return req.itf->unsubscribe(req.closure, event); +} + +/* + * 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'. + * On completion, the function 'callback' is invoked with the + * 'closure' given at call and two other parameters: 'iserror' and 'result'. + * 'iserror' is a boolean that indicates if the reply is an error reply. + * 'result' is the json object of the reply, you must not call json_object_put + * on the result. + * + * For convenience, the function calls 'json_object_put' for 'args'. + * Thus, in the case where 'args' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +static inline void afb_req_subcall(struct afb_req req, const char *api, const char *verb, struct json_object *args, void (*callback)(void *closure, int iserror, struct json_object *result), void *closure) { - return req.itf->raw(req.closure, size); + req.itf->subcall(req.closure, api, verb, args, callback, closure); } -/* internal use */ -static inline void afb_req_send(struct afb_req req, const char *buffer, size_t size) +/* + * 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'. + * + * For convenience, the function calls 'json_object_put' for 'args'. + * Thus, in the case where 'args' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +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) { - req.itf->send(req.closure, buffer, size); + return req.itf->subcallsync(req.closure, api, verb, args, result); } +/* + * Send associated to 'req' a message described by 'fmt' and following parameters + * to the journal for the verbosity 'level'. + * + * 'file', 'line' and 'func' are indicators of position of the code in source files + * (see macros __FILE__, __LINE__ and __func__). + * + * 'level' is defined by syslog standard: + * EMERGENCY 0 System is unusable + * ALERT 1 Action must be taken immediately + * CRITICAL 2 Critical conditions + * ERROR 3 Error conditions + * WARNING 4 Warning conditions + * NOTICE 5 Normal but significant condition + * INFO 6 Informational + * DEBUG 7 Debug-level messages + */ +static inline void afb_req_verbose(struct afb_req req, int level, const char *file, int line, const char * func, const char *fmt, ...) __attribute__((format(printf, 6, 7))); +static inline void afb_req_verbose(struct afb_req req, int level, const char *file, int line, const char * func, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + req.itf->vverbose(req.closure, level, file, line, func, fmt, args); + va_end(args); +} + +/* macro for setting file, line and function automatically */ +# if !defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS) +#define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,__FILE__,__LINE__,__func__,__VA_ARGS__) +#else +#define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,NULL,0,NULL,__VA_ARGS__) +#endif