Implement Media Plugin upload API, update README.md
[src/app-framework-binder.git] / plugins / media / media-api.c
1 /*
2  * Copyright (C) 2016 "IoT.bzh"
3  * Author "Manuel Bachmann"
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include "media-api.h"
20
21 /* ------ LOCAL HELPER FUNCTIONS --------- */
22
23 /* private client context creation ; default values */
24 STATIC mediaCtxHandleT* initMediaCtx () {
25
26     mediaCtxHandleT *ctx;
27
28     ctx = malloc (sizeof(mediaCtxHandleT));
29     ctx->media_server = NULL;
30     ctx->index = 0;
31
32     return ctx;
33 }
34
35 /* called when client session dies [e.g. client quits for more than 15mns] */
36 STATIC void freeMedia (void *context, void *handle) {
37
38     free (context);
39 }
40
41 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
42
43 STATIC json_object* init (AFB_request *request) {        /* AFB_SESSION_CHECK */
44
45     json_object *jresp;
46
47     /* create a private client context */
48     if (!request->context)
49         request->context = initMediaCtx();
50
51     /* initialize server connection */
52     _rygel_init (request->context);
53
54     jresp = json_object_new_object();
55     json_object_object_add(jresp, "info", json_object_new_string ("Media initialized"));
56     return jresp;
57 }
58
59 STATIC json_object* list (AFB_request *request) {        /* AFB_SESSION_CHECK */
60
61     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
62     json_object *jresp;
63     char *result;
64
65     result = _rygel_list (ctx);
66
67     if (!result)
68       return jsonNewMessage(AFB_FAIL, "No content found in media server");
69
70     jresp = json_object_new_object();
71     json_object_object_add(jresp, "list", json_object_new_string (result));
72     return jresp;
73 }
74
75 STATIC json_object* choose (AFB_request *request) {      /* AFB_SESSION_CHECK */
76
77     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
78     const char *value = getQueryValue (request, "value");
79     json_object *jresp;
80     unsigned int index;
81     char index_str[5];
82
83     /* no "?value=" parameter : return current index */
84     if (!value) {
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));
88     }
89
90     /* "?value=" parameter is negative */
91     else if (atoi(value) < 0)
92         return jsonNewMessage(AFB_FAIL, "Chosen index cannot be negative");
93
94     /* "?value=" parameter is positive */
95     else if (atoi(value) >= 0) {
96         index = (unsigned int) atoi(value);
97
98         if (!_rygel_choose (ctx, index))
99           return jsonNewMessage(AFB_FAIL, "Chosen index superior to current media count");
100
101         ctx->index = index;
102         jresp = json_object_new_object();
103         json_object_object_add (jresp, "index", json_object_new_string (value));
104     }
105
106     return jresp;
107 }
108
109 STATIC json_object* play (AFB_request *request) {        /* AFB_SESSION_CHECK */
110
111     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
112
113     if (!_rygel_do (ctx, PLAY))
114       return jsonNewMessage(AFB_FAIL, "Could not play chosen media");
115
116     return jsonNewMessage(AFB_SUCCESS, "PLaying media");
117 }
118
119 STATIC json_object* stop (AFB_request *request) {        /* AFB_SESSION_CHECK */
120
121     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
122
123     if (!_rygel_do (ctx, STOP))
124       return jsonNewMessage(AFB_FAIL, "Could not stop chosen media");
125
126     return jsonNewMessage(AFB_SUCCESS, "Stopped media");
127 }
128
129 STATIC json_object* paused (AFB_request *request) {      /* AFB_SESSION_CHECK */
130
131     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
132
133     if (!_rygel_do (ctx, PAUSE))
134       return jsonNewMessage(AFB_FAIL, "Could not pause chosen media");
135
136     return jsonNewMessage(AFB_SUCCESS, "Paused media");
137 }
138
139 STATIC json_object* upload (AFB_request *request) {      /* AFB_SESSION_CHECK */
140
141     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
142     const char *value = getQueryValue (request, "value");
143     json_object *jresp;
144     char path[256];
145
146     /* no "?value=" parameter : return error */
147     if (!value)
148       return jsonNewMessage(AFB_FAIL, "You must provide a file name");
149
150     snprintf (path, sizeof(path), "/tmp/%s", value);
151     if (access (path, R_OK) == -1)
152       return jsonNewMessage(AFB_FAIL, "File not found");
153
154     if (!_rygel_upload (ctx, path))
155       return jsonNewMessage(AFB_FAIL, "Error when uploading file... could not complete");
156
157     return jsonNewMessage(AFB_SUCCESS, "File successfully uploaded");
158 }
159
160 STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
161     return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Media API");
162 }
163
164
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"   },
174   {NULL}
175 };
176
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;
183
184     /*plugin->handle = initRadioPlugin();*/
185     plugin->freeCtxCB = (AFB_freeCtxCB)freeMedia;
186
187     return (plugin);
188 };