8350377d128525cbde0223c48b34f539ccf390b7
[src/app-framework-binder.git] / plugins / audio / audio-api.c
1 /*
2  * Copyright (C) 2015 "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 "audio-api.h"
20 #include "audio-alsa.h"
21
22
23 /* ------ LOCAL HELPER FUNCTIONS --------- */
24
25 /* private client context creation ; default values */
26 STATIC audioCtxHandleT* initAudioCtx () {
27
28     audioCtxHandleT *ctx;
29     int i;
30
31     ctx = malloc (sizeof(audioCtxHandleT));
32     ctx->idx = -1;
33     for (i = 0; i < 8; i++)
34         ctx->volume[i] = 25;
35     ctx->channels = 2;
36     ctx->mute = 0;
37     ctx->is_playing = 0;
38
39     return ctx;
40 }
41
42 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
43
44     /* power it off */
45     _alsa_free (ctx->idx);
46
47     /* clean client context */
48     ctx->idx = -1;
49
50     return AFB_SUCCESS;
51 }
52
53 /* called when client session dies [e.g. client quits for more than 15mns] */
54 STATIC void freeAudio (void *context) {
55     free (context);    
56 }
57
58
59 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
60
61 STATIC json_object* init (AFB_request *request) {        /* AFB_SESSION_CHECK */
62
63     json_object *jresp;
64     int idx;
65
66     /* create a private client context */
67     if (!request->context)
68         request->context = initAudioCtx();
69     
70     _alsa_init("default", request->context);
71     
72     jresp = json_object_new_object();
73     json_object_object_add (jresp, "info", json_object_new_string ("Audio initialised"));
74     return (jresp);
75 }
76
77 STATIC json_object* volume (AFB_request *request) {      /* AFB_SESSION_CHECK */
78
79     audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
80     const char *value = getQueryValue (request, "value");
81     json_object *jresp;
82     int volume[8], i;
83     char *volume_i;
84     char volume_str[256];
85     int len_str = 0;
86
87     /* no "?value=" parameter : return current state */
88     if (!value) {
89         for (i = 0; i < 8; i++) {
90             ctx->volume[i] = _alsa_get_volume (ctx->idx, i);
91             snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
92             len_str = strlen(volume_str);
93         }
94         jresp = json_object_new_object();
95         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
96     }
97
98     /* "?value=" parameter, set volume */
99     else {
100         volume_i = strdup (value);
101         volume_i = strtok (volume_i, ",");
102         volume[0] = atoi (volume_i);
103
104         if (100 < volume[0] < 0) {
105             free (volume_i);
106             request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
107             return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
108         }
109         ctx->volume[0] = volume[0];
110         _alsa_set_volume (ctx->idx, 0, ctx->volume[0]);
111         snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]);
112
113         for (i = 1; i < 8; i++) {
114             volume_i = strtok (NULL, ",");
115             /* if there is only one value, set all channels to this one */
116             if (!volume_i && i == 1)
117                _alsa_set_volume_all (ctx->idx, ctx->volume[0]);
118             if (!volume_i || 100 < atoi(volume_i) < 0) {
119                ctx->volume[i] = _alsa_get_volume (ctx->idx, i);
120             } else {
121                ctx->volume[i] = atoi(volume_i);
122                _alsa_set_volume (ctx->idx, i, ctx->volume[i]);
123             }
124             len_str = strlen(volume_str);
125             snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
126         }
127         jresp = json_object_new_object();
128         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
129     }
130
131     return jresp;
132 }
133
134 STATIC json_object* channels (AFB_request *request) {    /* AFB_SESSION_CHECK */
135
136     audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
137     const char *value = getQueryValue (request, "value");
138     json_object *jresp = json_object_new_object();
139     char channels_str[256];
140
141     /* no "?value=" parameter : return current state */
142     if (!value) {
143         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
144         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
145     }
146
147     /* "?value=" parameter, set channels */
148     else {
149         ctx->channels = atoi (value);
150         _alsa_set_channels (ctx->idx, ctx->channels);
151
152         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
153         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
154     }
155
156     return jresp;
157 }
158
159 STATIC json_object* mute (AFB_request *request) {        /* AFB_SESSION_CHECK */
160
161     audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
162     const char *value = getQueryValue (request, "value");
163     json_object *jresp = json_object_new_object();
164
165     /* no "?value=" parameter : return current state */
166     if (!value) {
167         ctx->mute = _alsa_get_mute (ctx->idx);
168         ctx->mute ?
169             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
170           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
171     }
172
173     /* "?value=" parameter is "1" or "true" */
174     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
175         ctx->mute = 1;
176         _alsa_set_mute (ctx->idx, ctx->mute);
177
178         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
179     }
180
181     /* "?value=" parameter is "0" or "false" */
182     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
183         ctx->mute = 0;
184         _alsa_set_mute (ctx->idx, ctx->mute);
185
186         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
187     }
188
189     return jresp;
190 }
191
192 STATIC json_object* play (AFB_request *request) {        /* AFB_SESSION_CHECK */
193
194     audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
195     const char *value = getQueryValue (request, "value");
196     json_object *jresp = json_object_new_object();
197
198     /* no "?value=" parameter : return current state */
199     if (!value) {
200         ctx->is_playing ?
201             json_object_object_add (jresp, "play", json_object_new_string ("on"))
202           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
203     }
204
205     /* "?value=" parameter is "1" or "true" */
206     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
207         ctx->is_playing = 1;
208         _alsa_play (ctx->idx);
209
210         json_object_object_add (jresp, "play", json_object_new_string ("on"));
211     }
212
213     /* "?value=" parameter is "0" or "false" */
214     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
215         ctx->is_playing = 0;
216         _alsa_stop (ctx->idx);
217
218         json_object_object_add (jresp, "play", json_object_new_string ("off"));
219     }
220
221     return jresp;
222 }
223
224 STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
225     return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
226 }
227
228 STATIC AFB_restapi pluginApis[]= {
229   {"init"    , AFB_SESSION_CHECK,  (AFB_apiCB)init      , "Audio API - init"},
230   {"volume"  , AFB_SESSION_CHECK,  (AFB_apiCB)volume    , "Audio API - volume"},
231   {"channels", AFB_SESSION_CHECK,  (AFB_apiCB)channels  , "Audio API - channels"},
232   {"mute"    , AFB_SESSION_CHECK,  (AFB_apiCB)mute      , "Audio API - mute"},
233   {"play"    , AFB_SESSION_CHECK,  (AFB_apiCB)play      , "Audio API - play"},
234   {"ping"    , AFB_SESSION_NONE,   (AFB_apiCB)ping      , "Audio API - ping"},
235   {NULL}
236 };
237
238 PUBLIC AFB_plugin *pluginRegister () {
239     AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
240     plugin->type   = AFB_PLUGIN_JSON;
241     plugin->info   = "Application Framework Binder - Audio plugin";
242     plugin->prefix = "audio";        
243     plugin->apis   = pluginApis;
244
245     plugin->freeCtxCB = (AFB_freeCtxCB)freeAudio;
246
247     return (plugin);
248 };