aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Bachmann <manuel.bachmann@iot.bzh>2015-12-23 18:54:24 +0100
committerManuel Bachmann <manuel.bachmann@iot.bzh>2015-12-23 18:54:24 +0100
commit97cfff753ccb7e5739ebe53e43a5af29dc0f6577 (patch)
treea1708825d3a60114d78d711e97b40eae75f0a4c7
parent5d293eac235f2d59eccc9b23de803821b821c12e (diff)
Add plugin handle into request, pass it to free callback
We need the global handle in requests, and in the free function. Signed-off-by: Manuel Bachmann <manuel.bachmann@iot.bzh>
-rw-r--r--include/local-def.h5
-rw-r--r--plugins/audio/audio-api.c2
-rw-r--r--plugins/radio/radio-api.c37
-rw-r--r--src/rest-api.c23
-rw-r--r--src/session.c32
5 files changed, 48 insertions, 51 deletions
diff --git a/include/local-def.h b/include/local-def.h
index 9c584417..843805f8 100644
--- a/include/local-def.h
+++ b/include/local-def.h
@@ -97,7 +97,7 @@ extern char *ERROR_LABEL[];
typedef json_object* (*AFB_apiCB)();
-typedef void (*AFB_freeCtxCB)(void*, char*);
+typedef void (*AFB_freeCtxCB)(void*, void*, char*);
// Error code are requested through function to manage json usage count
typedef struct {
@@ -237,7 +237,7 @@ typedef struct {
typedef struct {
const char *uuid;
const char *url;
- char *plugin;
+ char *prefix; // plugin convivial name
char *api;
AFB_PostRequest *post;
json_object *jresp;
@@ -247,6 +247,7 @@ typedef struct {
sigjmp_buf checkPluginCall; // context save for timeout set/longjmp
AFB_config *config; // plugin may need access to config
struct MHD_Connection *connection;
+ AFB_plugin *plugin; // provide callback and easy access to plugin
AFB_plugin **plugins;
} AFB_request;
diff --git a/plugins/audio/audio-api.c b/plugins/audio/audio-api.c
index 8671a879..1311b652 100644
--- a/plugins/audio/audio-api.c
+++ b/plugins/audio/audio-api.c
@@ -70,7 +70,7 @@ STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
_alsa_init("default", ctx);
jresp = json_object_new_object();
- json_object_object_add (jresp, "info", json_object_new_string ("Radio initialised"));
+ json_object_object_add (jresp, "info", json_object_new_string ("Audio initialised"));
return (jresp);
}
diff --git a/plugins/radio/radio-api.c b/plugins/radio/radio-api.c
index cfa25995..09fbcb55 100644
--- a/plugins/radio/radio-api.c
+++ b/plugins/radio/radio-api.c
@@ -117,12 +117,10 @@ STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
}
/* called when client session dies [e.g. client quits for more than 15mns] */
-STATIC json_object* freeRadio (AFB_clientCtx *client) {
+STATIC void freeRadio (void *context, void *handle) {
- releaseRadio (client->plugin->handle, client->ctx);
- free (client->ctx);
-
- return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
+ releaseRadio (handle, context);
+ free (context);
}
@@ -130,22 +128,20 @@ STATIC json_object* freeRadio (AFB_clientCtx *client) {
STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
- radioCtxHandleT *ctx;
json_object *jresp;
/* create a private client context */
- ctx = initRadioCtx();
- request->client->ctx = (radioCtxHandleT*)ctx;
+ request->context = initRadioCtx();
jresp = json_object_new_object();
- json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
+ json_object_object_add(jresp, "info", json_object_new_string ("Radio initialized"));
return jresp;
}
STATIC json_object* power (AFB_request *request) { /* AFB_SESSION_CHECK */
-
- pluginHandleT *handle = request->client->plugin->handle;
- radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
+
+ pluginHandleT *handle = (pluginHandleT*)request->plugin;
+ radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
const char *value = getQueryValue (request, "value");
json_object *jresp;
@@ -186,7 +182,7 @@ STATIC json_object* power (AFB_request *request) { /* AFB_SESSION_CHECK */
STATIC json_object* mode (AFB_request *request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
+ radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
const char *value = getQueryValue (request, "value");
json_object *jresp = json_object_new_object();
@@ -216,7 +212,7 @@ STATIC json_object* mode (AFB_request *request) { /* AFB_SESSION_CHECK */
STATIC json_object* freq (AFB_request *request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
+ radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
const char *value = getQueryValue (request, "value");
json_object *jresp = json_object_new_object();
double freq;
@@ -243,7 +239,7 @@ STATIC json_object* freq (AFB_request *request) { /* AFB_SESSION_CHECK */
STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
+ radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
const char *value = getQueryValue (request, "value");
json_object *jresp = json_object_new_object();
char *mute_str;
@@ -274,7 +270,7 @@ STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
+ radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
const char *value = getQueryValue (request, "value");
json_object *jresp = json_object_new_object();
@@ -304,12 +300,6 @@ STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
return jresp;
}
-STATIC json_object* refresh (AFB_request *request) { /* AFB_SESSION_RENEW */
- json_object *jresp = json_object_new_object();
- json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
- return jresp;
-}
-
STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
}
@@ -322,7 +312,6 @@ STATIC AFB_restapi pluginApis[]= {
{"freq" , AFB_SESSION_CHECK, (AFB_apiCB)freq , "Radio API - freq"},
{"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Radio API - mute"},
{"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Radio API - play"},
- {"refresh", AFB_SESSION_RENEW, (AFB_apiCB)refresh , "Radio API - refresh"},
{"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Radio API - ping"},
{NULL}
};
@@ -335,7 +324,7 @@ PUBLIC AFB_plugin* pluginRegister () {
plugin->apis = pluginApis;
plugin->handle = initRadioPlugin();
- plugin->freeCtxCB = freeRadio;
+ plugin->freeCtxCB = (AFB_freeCtxCB)freeRadio;
return (plugin);
};
diff --git a/src/rest-api.c b/src/rest-api.c
index 3ae9cc0e..437b97cf 100644
--- a/src/rest-api.c
+++ b/src/rest-api.c
@@ -128,7 +128,7 @@ STATIC AFB_error callPluginApi(AFB_request *request, int plugidx, void *context)
};
if (verbose) fprintf(stderr, "Plugin=[%s] Api=[%s] Middleware=[%d] Client=[0x%x] Uuid=[%s] Token=[%s]\n"
- , request->plugin, request->api, plugin->apis[idx].session, clientCtx, clientCtx->uuid, clientCtx->token);
+ , request->prefix, request->api, plugin->apis[idx].session, clientCtx, clientCtx->uuid, clientCtx->token);
switch(plugin->apis[idx].session) {
@@ -233,24 +233,24 @@ STATIC AFB_error findAndCallApi (AFB_request *request, void *context) {
int idx;
AFB_error status;
- if (!request->api || !request->plugin) return (AFB_FAIL);
+ if (!request->api || !request->prefix) return (AFB_FAIL);
// Search for a plugin with this urlpath
for (idx = 0; request->plugins[idx] != NULL; idx++) {
- if (!strcmp(request->plugins[idx]->prefix, request->plugin)) {
+ if (!strcmp(request->plugins[idx]->prefix, request->prefix)) {
status =callPluginApi(request, idx, context);
break;
}
}
// No plugin was found
if (request->plugins[idx] == NULL) {
- request->jresp = jsonNewMessage(AFB_FATAL, "No Plugin=[%s] Url=%s", request->plugin, request->url);
+ request->jresp = jsonNewMessage(AFB_FATAL, "No Plugin=[%s] Url=%s", request->prefix, request->url);
goto ExitOnError;
}
// plugin callback did not return a valid Json Object
if (status == AFB_FAIL) {
- request->jresp = jsonNewMessage(AFB_FATAL, "No API=[%s] for Plugin=[%s] url=[%s]", request->api, request->plugin, request->url);
+ request->jresp = jsonNewMessage(AFB_FATAL, "No API=[%s] for Plugin=[%s] url=[%s]", request->api, request->prefix, request->url);
goto ExitOnError;
}
@@ -306,7 +306,7 @@ doPostIterate (void *cls, enum MHD_ValueKind kind, const char *key,
STATIC void freeRequest (AFB_request *request) {
- free (request->plugin);
+ free (request->prefix);
free (request->api);
free (request);
}
@@ -314,7 +314,8 @@ STATIC void freeRequest (AFB_request *request) {
STATIC AFB_request *createRequest (struct MHD_Connection *connection, AFB_session *session, const char* url) {
AFB_request *request;
-
+ int idx;
+
// Start with a clean request
request = calloc (1, sizeof (AFB_request));
char *urlcpy1, *urlcpy2;
@@ -341,9 +342,15 @@ STATIC AFB_request *createRequest (struct MHD_Connection *connection, AFB_sessio
request->connection = connection;
request->config = session->config;
request->url = url;
- request->plugin = strdup (baseurl);
+ request->prefix = strdup (baseurl);
request->api = strdup (baseapi);
request->plugins= session->plugins;
+ for (idx = 0; idx < session->config->pluginCount; idx++) {
+ if (!strcmp(baseurl, session->plugins[idx]->prefix)) {
+ request->plugin = session->plugins[idx];
+ break;
+ }
+ }
Done:
free(urlcpy1);
diff --git a/src/session.c b/src/session.c
index b44e37e8..f9f24d21 100644
--- a/src/session.c
+++ b/src/session.c
@@ -81,8 +81,8 @@ STATIC json_object *checkCardDirExit (AFB_session *session, AFB_request *reques
int sessionDir, cardDir;
// card name should be more than 3 character long !!!!
- if (strlen (request->plugin) < 3) {
- return (jsonNewMessage (AFB_FAIL,"Fail invalid plugin=%s", request->plugin));
+ if (strlen (request->prefix) < 3) {
+ return (jsonNewMessage (AFB_FAIL,"Fail invalid plugin=%s", request->prefix));
}
// open session directory
@@ -92,11 +92,11 @@ STATIC json_object *checkCardDirExit (AFB_session *session, AFB_request *reques
}
// create session sndcard directory if it does not exit
- cardDir = openat (sessionDir, request->plugin, O_DIRECTORY);
+ cardDir = openat (sessionDir, request->prefix, O_DIRECTORY);
if (cardDir < 0) {
- cardDir = mkdirat (sessionDir, request->plugin, O_RDWR | S_IRWXU | S_IRGRP);
+ cardDir = mkdirat (sessionDir, request->prefix, O_RDWR | S_IRWXU | S_IRGRP);
if (cardDir < 0) {
- return (jsonNewMessage (AFB_FAIL,"Fail to create directory [%s/%s] error=%s", session->config->sessiondir, request->plugin, strerror(cardDir)));
+ return (jsonNewMessage (AFB_FAIL,"Fail to create directory [%s/%s] error=%s", session->config->sessiondir, request->prefix, strerror(cardDir)));
}
}
close (sessionDir);
@@ -120,13 +120,13 @@ PUBLIC json_object *sessionList (AFB_session *session, AFB_request *request) {
return (jsonNewMessage (AFB_FAIL,"Fail to open directory [%s] error=%s", session->config->sessiondir, strerror(sessionDir)));
}
- count = scandirat (sessionDir, request->plugin, &namelist, fileSelect, alphasort);
+ count = scandirat (sessionDir, request->prefix, &namelist, fileSelect, alphasort);
close (sessionDir);
if (count < 0) {
- return (jsonNewMessage (AFB_FAIL,"Fail to scan sessions.hash directory [%s/%s] error=%s", session->config->sessiondir, request->plugin, strerror(sessionDir)));
+ return (jsonNewMessage (AFB_FAIL,"Fail to scan sessions.hash directory [%s/%s] error=%s", session->config->sessiondir, request->prefix, strerror(sessionDir)));
}
- if (count == 0) return (jsonNewMessage (AFB_EMPTY,"[%s] no session at [%s]", request->plugin, session->config->sessiondir));
+ if (count == 0) return (jsonNewMessage (AFB_EMPTY,"[%s] no session at [%s]", request->prefix, session->config->sessiondir));
// loop on each session file, retrieve its date and push it into json response object
sessionsJ = json_object_new_array();
@@ -199,7 +199,7 @@ PUBLIC json_object *sessionFromDisk (AFB_session *session, AFB_request *request,
if (response != NULL) return response;
// add name and file extension to session name
- strncpy (filename, request->plugin, sizeof(filename));
+ strncpy (filename, request->prefix, sizeof(filename));
strncat (filename, "/", sizeof(filename));
if (defsession) strncat (filename, AFB_CURRENT_SESSION, sizeof(filename)-1);
else strncat (filename, name, sizeof(filename)-1);
@@ -224,7 +224,7 @@ PUBLIC json_object *sessionFromDisk (AFB_session *session, AFB_request *request,
}
// create a link to keep track of last uploaded session for this card
- if (!defsession) makeSessionLink (request->plugin, name);
+ if (!defsession) makeSessionLink (request->prefix, name);
return (jsonSession);
}
@@ -248,7 +248,7 @@ PUBLIC json_object * sessionToDisk (AFB_session *session, AFB_request *request,
if (response != NULL) return response;
// add cardname and file extension to session name
- strncpy (filename, request->plugin, sizeof(filename));
+ strncpy (filename, request->prefix, sizeof(filename));
strncat (filename, "/", sizeof(filename));
if (defsession) strncat (filename, AFB_CURRENT_SESSION, sizeof(filename)-1);
else strncat (filename, name, sizeof(filename)-1);
@@ -271,13 +271,13 @@ PUBLIC json_object * sessionToDisk (AFB_session *session, AFB_request *request,
// extract session info from args
info = json_tokener_parse (request->post->data);
if (!info) {
- response = jsonNewMessage (AFB_FATAL,"sndcard=%s session=%s invalid json args=%s", request->plugin, name, request->post);
+ response = jsonNewMessage (AFB_FATAL,"sndcard=%s session=%s invalid json args=%s", request->prefix, name, request->post);
goto OnErrorExit;
}
// info is a valid AFB_info type
if (!json_object_object_get_ex (info, "jtype", &jtype)) {
- response = jsonNewMessage (AFB_EMPTY,"sndcard=%s session=%s No 'AFB_pluginT' args=%s", request->plugin, name, request->post);
+ response = jsonNewMessage (AFB_EMPTY,"sndcard=%s session=%s No 'AFB_pluginT' args=%s", request->prefix, name, request->post);
goto OnErrorExit;
}
@@ -302,7 +302,7 @@ PUBLIC json_object * sessionToDisk (AFB_session *session, AFB_request *request,
// create a link to keep track of last uploaded session for this card
- if (!defsession) makeSessionLink (request->plugin, name);
+ if (!defsession) makeSessionLink (request->prefix, name);
// we're donne let's return status message
response = jsonNewMessage (AFB_SUCCESS,"Session= [%s] saved on disk", filename);
@@ -330,7 +330,7 @@ STATIC void ctxUuidFreeCB (AFB_clientCtx *client) {
if (client->contexts[idx] != NULL) {
freeCtxCB = client->plugins[idx]->freeCtxCB;
if (freeCtxCB == NULL) free (client->contexts[idx]);
- else if (freeCtxCB != (void*)-1) freeCtxCB(client->contexts[idx], client->uuid);
+ else if (freeCtxCB != (void*)-1) freeCtxCB(client->contexts[idx], client->plugins[idx]->handle, client->uuid);
}
}
}
@@ -476,7 +476,7 @@ PUBLIC AFB_clientCtx *ctxClientGet (AFB_request *request, int idx) {
if (clientCtx == NULL) {
clientCtx = calloc(1, sizeof(AFB_clientCtx)); // init NULL clientContext
clientCtx->contexts = calloc (1, request->config->pluginCount * (sizeof (void*)));
- clientCtx->plugins = request->plugins;
+ clientCtx->plugins = request->plugins;
}
uuid_generate(newuuid); // create a new UUID