Add Media Plugin
[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
31     return ctx;
32 }
33
34 /* called when client session dies [e.g. client quits for more than 15mns] */
35 STATIC void freeMedia (void *context, void *handle) {
36
37     free (context);
38 }
39
40 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
41
42 STATIC json_object* init (AFB_request *request) {        /* AFB_SESSION_CHECK */
43
44     json_object *jresp;
45
46     /* create a private client context */
47     if (!request->context)
48         request->context = initMediaCtx();
49
50     /* initialize server connection */
51     _rygel_init (request->context);
52
53     jresp = json_object_new_object();
54     json_object_object_add(jresp, "info", json_object_new_string ("Media initialized"));
55     return jresp;
56 }
57
58 STATIC json_object* list (AFB_request *request) {        /* AFB_SESSION_CHECK */
59
60     char *result;
61
62     result = _rygel_list (request->context);
63
64     return jsonNewMessage(AFB_SUCCESS, result);
65 }
66
67 STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
68     return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Media API");
69 }
70
71
72 STATIC AFB_restapi pluginApis[]= {
73   {"init"   , AFB_SESSION_CHECK,  (AFB_apiCB)init       , "Media API - init"},
74   {"list"   , AFB_SESSION_CHECK,  (AFB_apiCB)list       , "Media API - list"},
75   {"ping"   , AFB_SESSION_NONE,   (AFB_apiCB)ping       , "Media API - ping"},
76   {NULL}
77 };
78
79 PUBLIC AFB_plugin* pluginRegister () {
80     AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
81     plugin->type  = AFB_PLUGIN_JSON;
82     plugin->info  = "Application Framework Binder - Media plugin";
83     plugin->prefix  = "media";
84     plugin->apis  = pluginApis;
85
86     /*plugin->handle = initRadioPlugin();*/
87     plugin->freeCtxCB = (AFB_freeCtxCB)freeMedia;
88
89     return (plugin);
90 };