2 * Copyright (C) 2016 "IoT.bzh"
3 * Author "Manuel Bachmann"
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 #include <json-c/json.h>
24 #include "media-api.h"
25 #include "media-rygel.h"
27 #include <afb/afb-plugin.h>
28 #include <afb/afb-req-itf.h>
30 json_object* _rygel_list (mediaCtxHandleT *);
32 /* ------ LOCAL HELPER FUNCTIONS --------- */
34 /* private client context creation ; default values */
35 static mediaCtxHandleT* initMediaCtx () {
39 ctx = malloc (sizeof(mediaCtxHandleT));
40 ctx->media_server = NULL;
46 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
48 static void init (struct afb_req request) { /* AFB_SESSION_CHECK */
50 mediaCtxHandleT *ctx = afb_req_context_get(request);
53 /* create a private client context */
56 afb_req_context_set (request, ctx, free);
59 /* initialize server connection */
60 if (!ctx->media_server)
63 jresp = json_object_new_object ();
64 json_object_object_add (jresp, "init", json_object_new_string ("success"));
65 afb_req_success (request, jresp, "Media - Initialized");
68 static void list (struct afb_req request) { /* AFB_SESSION_CHECK */
70 mediaCtxHandleT *ctx = afb_req_context_get(request);
73 /* check that context is initialized */
75 afb_req_fail (request, "failed", "uninitialized");
79 jresp = _rygel_list (ctx);
82 afb_req_fail (request, "failed", "no content found in media server");
86 afb_req_success (request, jresp, "Media - Listed");
89 static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */
91 mediaCtxHandleT *ctx = afb_req_context_get(request);
92 const char *value = afb_req_value (request, "value");
97 /* check that context is initialized */
99 afb_req_fail (request, "failed", "uninitialized");
103 /* no "?value=" parameter : return current index */
105 snprintf (index_str, sizeof(index_str), "%d", ctx->index);
106 jresp = json_object_new_object();
107 json_object_object_add (jresp, "index", json_object_new_string (index_str));
110 /* "?value=" parameter is negative */
111 else if (atoi(value) < 0) {
112 afb_req_fail (request, "failed", "chosen index cannot be negative");
116 /* "?value=" parameter is positive */
117 else if (atoi(value) >= 0) {
118 index = (unsigned int) atoi(value);
120 if (!_rygel_select (ctx, index)) {
121 afb_req_fail (request, "failed", "chosen index superior to current media count");
126 jresp = json_object_new_object();
127 json_object_object_add (jresp, "index", json_object_new_string (value));
132 afb_req_success (request, jresp, "Media - Listed");
135 static void play (struct afb_req request) { /* AFB_SESSION_CHECK */
137 mediaCtxHandleT *ctx = afb_req_context_get(request);
140 /* check that context is initialized */
142 afb_req_fail (request, "failed", "uninitialized");
146 if (!_rygel_do (ctx, PLAY, NULL)) {
147 afb_req_fail (request, "failed", "could not play chosen media");
151 jresp = json_object_new_object ();
152 json_object_object_add (jresp, "play", json_object_new_string ("success"));
153 afb_req_success (request, jresp, "Media - Listed");
156 static void stop (struct afb_req request) { /* AFB_SESSION_CHECK */
158 mediaCtxHandleT *ctx = afb_req_context_get(request);
161 /* check that context is initialized */
163 afb_req_fail (request, "failed", "uninitialized");
167 if (!_rygel_do (ctx, STOP, NULL)) {
168 afb_req_fail (request, "failed", "could not stop chosen media");
172 jresp = json_object_new_object ();
173 json_object_object_add (jresp, "stop", json_object_new_string ("success"));
174 afb_req_success (request, jresp, "Media - Stopped");
177 static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */
179 mediaCtxHandleT *ctx = afb_req_context_get(request);
182 /* check that context is initialized */
184 afb_req_fail (request, "failed", "uninitialized");
188 if (!_rygel_do (ctx, PAUSE, NULL)) {
189 afb_req_fail (request, "failed", "could not pause chosen media");
193 jresp = json_object_new_object();
194 json_object_object_add (jresp, "pause", json_object_new_string ("success"));
195 afb_req_success (request, jresp, "Media - Paused");
198 static void seek (struct afb_req request) { /* AFB_SESSION_CHECK */
200 mediaCtxHandleT *ctx = afb_req_context_get(request);
201 const char *value = afb_req_value (request, "value");
204 /* check that context is initialized */
206 afb_req_fail (request, "failed", "uninitialized");
210 /* no "?value=" parameter : return error */
212 afb_req_fail (request, "failed", "you must provide a time");
216 if (!_rygel_do (ctx, SEEK, (char *)value)) {
217 afb_req_fail (request, "failed", "could not seek chosen media");
221 jresp = json_object_new_object();
222 json_object_object_add (jresp, "seek", json_object_new_string ("success"));
223 afb_req_success (request, jresp, "Media - Sought");
226 static char *renamed_filename(struct afb_arg argfile)
229 const char *e = strrchr(argfile.path, '/');
231 result = strdup(argfile.value);
233 result = malloc((++e - argfile.path) + strlen(argfile.value) + 1);
235 strcpy(stpncpy(result, argfile.path, e - argfile.path), argfile.value);
240 static void on_uploaded(struct afb_req *prequest, int status)
242 struct afb_req request = afb_req_unstore(prequest);
243 struct afb_arg argfile = afb_req_get(request, "file-upload");
244 char *file = renamed_filename(argfile);
249 afb_req_fail (request, "failed", "expected file not received");
251 afb_req_success_f (request, NULL, "uploaded file %s", argfile.value);
252 afb_req_unref(request);
255 static void upload (struct afb_req request) { /* AFB_SESSION_CHECK */
257 mediaCtxHandleT *ctx = afb_req_context_get(request);
258 struct afb_req *prequest;
259 struct afb_arg argfile;
262 /* check that context is initialized */
264 afb_req_fail (request, "failed", "uninitialized");
269 argfile = afb_req_get(request, "file-upload");
270 if (!argfile.value || !argfile.path) {
271 afb_req_fail (request, "failed", "expected file not received");
275 /* rename the file */
276 path = renamed_filename(argfile);
278 afb_req_fail (request, "failed", "out of memory");
281 if (rename(argfile.path, path) != 0) {
283 afb_req_fail (request, "failed", "system error");
287 /* for asynchronous processing */
288 prequest = afb_req_store(request);
291 afb_req_fail (request, "failed", "out of memory");
293 else if (!_rygel_upload (ctx, path, (void*)on_uploaded, prequest)) {
294 afb_req_unref(afb_req_unstore(prequest));
296 afb_req_fail (request, "failed", "Error when uploading file to media server... could not complete");
301 static void ping (struct afb_req request) { /* AFB_SESSION_NONE */
302 afb_req_success (request, NULL, "Media - Ping succeeded");
306 static const struct AFB_verb_desc_v1 verbs[]= {
307 {"init" , AFB_SESSION_CHECK, init , "Media API - init" },
308 {"list" , AFB_SESSION_CHECK, list , "Media API - list" },
309 {"select" , AFB_SESSION_CHECK, selecting , "Media API - select" },
310 {"play" , AFB_SESSION_CHECK, play , "Media API - play" },
311 {"stop" , AFB_SESSION_CHECK, stop , "Media API - stop" },
312 {"pause" , AFB_SESSION_CHECK, pausing , "Media API - pause" },
313 {"seek" , AFB_SESSION_CHECK, seek , "Media API - seek" },
314 // {"upload" , AFB_SESSION_CHECK, upload , "Media API - upload" },
315 {"ping" , AFB_SESSION_NONE, ping , "Media API - ping" },
319 static const struct AFB_plugin pluginDesc = {
320 .type = AFB_PLUGIN_VERSION_1,
322 .info = "Application Framework Binder - Media plugin",
328 const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf)