X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=plugins%2Fmedia%2Fmedia-api.c;h=dace151fd459240d0e8cd06247f43579ac4df382;hb=65bc678960567038ca4d07d1f9c5784b6c7a7834;hp=460894b197442a98d1b37469d596dec6f899cbb7;hpb=04c2e56502996e87a573499fdc916ff74895a2f1;p=src%2Fapp-framework-binder.git diff --git a/plugins/media/media-api.c b/plugins/media/media-api.c index 460894b1..dace151f 100644 --- a/plugins/media/media-api.c +++ b/plugins/media/media-api.c @@ -16,13 +16,16 @@ */ #define _GNU_SOURCE + +#include + #include #include "media-api.h" #include "media-rygel.h" -#include "afb-plugin.h" -#include "afb-req-itf.h" +#include +#include json_object* _rygel_list (mediaCtxHandleT *); @@ -44,7 +47,7 @@ static mediaCtxHandleT* initMediaCtx () { static void init (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; /* create a private client context */ @@ -64,9 +67,15 @@ static void init (struct afb_req request) { /* AFB_SESSION_CHECK */ static void list (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + jresp = _rygel_list (ctx); if (!jresp) { @@ -79,12 +88,18 @@ static void list (struct afb_req request) { /* AFB_SESSION_CHECK */ static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); const char *value = afb_req_value (request, "value"); json_object *jresp; unsigned int index; char index_str[5]; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + /* no "?value=" parameter : return current index */ if (!value) { snprintf (index_str, sizeof(index_str), "%d", ctx->index); @@ -119,9 +134,15 @@ static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */ static void play (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + if (!_rygel_do (ctx, PLAY, NULL)) { afb_req_fail (request, "failed", "could not play chosen media"); return; @@ -134,9 +155,15 @@ static void play (struct afb_req request) { /* AFB_SESSION_CHECK */ static void stop (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + if (!_rygel_do (ctx, STOP, NULL)) { afb_req_fail (request, "failed", "could not stop chosen media"); return; @@ -149,9 +176,15 @@ static void stop (struct afb_req request) { /* AFB_SESSION_CHECK */ static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + if (!_rygel_do (ctx, PAUSE, NULL)) { afb_req_fail (request, "failed", "could not pause chosen media"); return; @@ -164,10 +197,16 @@ static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */ static void seek (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); const char *value = afb_req_value (request, "value"); json_object *jresp; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + /* no "?value=" parameter : return error */ if (!value) { afb_req_fail (request, "failed", "you must provide a time"); @@ -184,45 +223,85 @@ static void seek (struct afb_req request) { /* AFB_SESSION_CHECK */ afb_req_success (request, jresp, "Media - Sought"); } -#if 0 -static void upload (AFB_request *request, AFB_PostItem *item) { /* AFB_SESSION_CHECK */ +static char *renamed_filename(struct afb_arg argfile) +{ + char *result; + const char *e = strrchr(argfile.path, '/'); + if (e == NULL) + result = strdup(argfile.value); + else { + result = malloc((++e - argfile.path) + strlen(argfile.value) + 1); + if (result != NULL) + strcpy(stpncpy(result, argfile.path, e - argfile.path), argfile.value); + } + return result; +} - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); - AFB_PostCtx *postFileCtx; - json_object *jresp; - char *path; +static void on_uploaded(struct afb_req *prequest, int status) +{ + struct afb_req request = afb_req_unstore(prequest); + struct afb_arg argfile = afb_req_get(request, "file-upload"); + char *file = renamed_filename(argfile); + if (file != NULL) + unlink(file); + free(file); + if (status) + afb_req_fail (request, "failed", "expected file not received"); + else + afb_req_success_f (request, NULL, "uploaded file %s", argfile.value); +} - /* item is !NULL until transfer is complete */ - if (item != NULL) - return getPostFile (request, item, "media"); +static void upload (struct afb_req request) { /* AFB_SESSION_CHECK */ - /* target intermediary file path */ - path = getPostPath (request); + mediaCtxHandleT *ctx = afb_req_context_get(request); + struct afb_req *prequest; + struct afb_arg argfile; + char *path; - if (!path) - fprintf (stderr, "Error encoutered during intermediary file transfer\n"); + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } - else if (!_rygel_upload (ctx, path)) { - request->errcode = MHD_HTTP_EXPECTATION_FAILED; - request->jresp = jsonNewMessage (AFB_FAIL, "Error when uploading file to media server... could not complete"); + /* get the file */ + argfile = afb_req_get(request, "file-upload"); + if (!argfile.value || !argfile.path) { + afb_req_fail (request, "failed", "expected file not received"); + return; } - else { - request->errcode = MHD_HTTP_OK; - request->jresp = jsonNewMessage (AFB_SUCCESS, "upload=%s done", path); + /* rename the file */ + path = renamed_filename(argfile); + if (path == NULL) { + afb_req_fail (request, "failed", "out of memory"); + return; + } + if (rename(argfile.path, path) != 0) { + free(path); + afb_req_fail (request, "failed", "system error"); + return; } - /* finalizes file transfer */ - return getPostFile (request, item, NULL); + /* for asynchronous processing */ + prequest = afb_req_store(request); + if (path == NULL) { + unlink(path); + afb_req_fail (request, "failed", "out of memory"); + } + else if (!_rygel_upload (ctx, path, (void*)on_uploaded, prequest)) { + unlink(path); + afb_req_fail (afb_req_unstore(prequest), "failed", "Error when uploading file to media server... could not complete"); + } + free(path); } -#endif static void ping (struct afb_req request) { /* AFB_SESSION_NONE */ afb_req_success (request, NULL, "Media - Ping succeeded"); } -static const struct AFB_restapi pluginApis[]= { +static const struct AFB_verb_desc_v1 verbs[]= { {"init" , AFB_SESSION_CHECK, init , "Media API - init" }, {"list" , AFB_SESSION_CHECK, list , "Media API - list" }, {"select" , AFB_SESSION_CHECK, selecting , "Media API - select" }, @@ -230,19 +309,21 @@ static const struct AFB_restapi pluginApis[]= { {"stop" , AFB_SESSION_CHECK, stop , "Media API - stop" }, {"pause" , AFB_SESSION_CHECK, pausing , "Media API - pause" }, {"seek" , AFB_SESSION_CHECK, seek , "Media API - seek" }, -// {"upload" , AFB_SESSION_CHECK, (AFB_apiCB)upload , "Media API - upload" }, +// {"upload" , AFB_SESSION_CHECK, upload , "Media API - upload" }, {"ping" , AFB_SESSION_NONE, ping , "Media API - ping" }, {NULL} }; static const struct AFB_plugin pluginDesc = { - .type = AFB_PLUGIN_JSON, - .info = "Application Framework Binder - Media plugin", - .prefix = "media", - .apis = pluginApis + .type = AFB_PLUGIN_VERSION_1, + .v1 = { + .info = "Application Framework Binder - Media plugin", + .prefix = "media", + .verbs = verbs + } }; -const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf) +const struct AFB_plugin *pluginAfbV1Entry (const struct AFB_interface *itf) { - return &pluginDesc; + return &pluginDesc; }