*/
#define _GNU_SOURCE
+
+#include <string.h>
+
#include <json-c/json.h>
#include "media-api.h"
#include "media-rygel.h"
-#include "afb-plugin.h"
-#include "afb-req-itf.h"
+#include <afb/afb-plugin.h>
+#include <afb/afb-req-itf.h>
json_object* _rygel_list (mediaCtxHandleT *);
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 */
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) {
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);
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;
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;
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;
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");
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);
+ afb_req_unref(request);
+}
- /* 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)) {
+ afb_req_unref(afb_req_unstore(prequest));
+ unlink(path);
+ afb_req_fail (request, "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" },
{"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 *pluginAfbV1Register (const struct AFB_interface *itf)
{
- return &pluginDesc;
+ return &pluginDesc;
}