2 * Copyright (C) 2016 "IoT.bzh"
3 * Author "Manuel Bachmann"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "media-api.h"
21 /* ------ LOCAL HELPER FUNCTIONS --------- */
23 /* private client context creation ; default values */
24 STATIC mediaCtxHandleT* initMediaCtx () {
28 ctx = malloc (sizeof(mediaCtxHandleT));
29 ctx->media_server = NULL;
35 /* called when client session dies [e.g. client quits for more than 15mns] */
36 STATIC void freeMedia (void *context, void *handle) {
41 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
43 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CHECK */
47 /* create a private client context */
48 if (!request->context)
49 request->context = initMediaCtx();
51 /* initialize server connection */
52 _rygel_init (request->context);
54 jresp = json_object_new_object();
55 json_object_object_add(jresp, "info", json_object_new_string ("Media initialized"));
59 STATIC json_object* list (AFB_request *request) { /* AFB_SESSION_CHECK */
61 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
65 result = _rygel_list (ctx);
68 return jsonNewMessage(AFB_FAIL, "No content found in media server");
70 jresp = json_object_new_object();
71 json_object_object_add(jresp, "list", json_object_new_string (result));
75 STATIC json_object* choose (AFB_request *request) { /* AFB_SESSION_CHECK */
77 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
78 const char *value = getQueryValue (request, "value");
83 /* no "?value=" parameter : return current index */
85 snprintf (index_str, sizeof(index_str), "%d", ctx->index);
86 jresp = json_object_new_object();
87 json_object_object_add (jresp, "index", json_object_new_string (index_str));
90 /* "?value=" parameter is negative */
91 else if (atoi(value) < 0)
92 return jsonNewMessage(AFB_FAIL, "Chosen index cannot be negative");
94 /* "?value=" parameter is positive */
95 else if (atoi(value) >= 0) {
96 index = (unsigned int) atoi(value);
98 if (!_rygel_choose (ctx, index))
99 return jsonNewMessage(AFB_FAIL, "Chosen index superior to current media count");
102 jresp = json_object_new_object();
103 json_object_object_add (jresp, "index", json_object_new_string (value));
109 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
111 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
113 if (!_rygel_do (ctx, PLAY))
114 return jsonNewMessage(AFB_FAIL, "Could not play chosen media");
116 return jsonNewMessage(AFB_SUCCESS, "PLaying media");
119 STATIC json_object* stop (AFB_request *request) { /* AFB_SESSION_CHECK */
121 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
123 if (!_rygel_do (ctx, STOP))
124 return jsonNewMessage(AFB_FAIL, "Could not stop chosen media");
126 return jsonNewMessage(AFB_SUCCESS, "Stopped media");
129 STATIC json_object* paused (AFB_request *request) { /* AFB_SESSION_CHECK */
131 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
133 if (!_rygel_do (ctx, PAUSE))
134 return jsonNewMessage(AFB_FAIL, "Could not pause chosen media");
136 return jsonNewMessage(AFB_SUCCESS, "Paused media");
139 STATIC json_object* upload (AFB_request *request) { /* AFB_SESSION_CHECK */
141 mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
142 const char *value = getQueryValue (request, "value");
146 /* no "?value=" parameter : return error */
148 return jsonNewMessage(AFB_FAIL, "You must provide a file name");
150 snprintf (path, sizeof(path), "/tmp/%s", value);
151 if (access (path, R_OK) == -1)
152 return jsonNewMessage(AFB_FAIL, "File not found");
154 if (!_rygel_upload (ctx, path))
155 return jsonNewMessage(AFB_FAIL, "Error when uploading file... could not complete");
157 return jsonNewMessage(AFB_SUCCESS, "File successfully uploaded");
160 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
161 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Media API");
165 STATIC AFB_restapi pluginApis[]= {
166 {"init" , AFB_SESSION_CHECK, (AFB_apiCB)init , "Media API - init" },
167 {"list" , AFB_SESSION_CHECK, (AFB_apiCB)list , "Media API - list" },
168 {"choose" , AFB_SESSION_CHECK, (AFB_apiCB)choose , "Media API - choose" },
169 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Media API - play" },
170 {"stop" , AFB_SESSION_CHECK, (AFB_apiCB)stop , "Media API - stop" },
171 {"pause" , AFB_SESSION_CHECK, (AFB_apiCB)paused , "Media API - pause" },
172 {"upload" , AFB_SESSION_CHECK, (AFB_apiCB)upload , "Media API - upload" },
173 {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Media API - ping" },
177 PUBLIC AFB_plugin* pluginRegister () {
178 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
179 plugin->type = AFB_PLUGIN_JSON;
180 plugin->info = "Application Framework Binder - Media plugin";
181 plugin->prefix = "media";
182 plugin->apis = pluginApis;
184 /*plugin->handle = initRadioPlugin();*/
185 plugin->freeCtxCB = (AFB_freeCtxCB)freeMedia;