2 * Copyright (C) 2015 "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 "audio-api.h"
21 /* ------ BACKEND FUNCTIONS ------- */
23 void _backend_init (const char *name, audioCtxHandleT *ctx) {
26 if (_pulse_init (name, ctx) < 0)
28 _alsa_init (name, ctx);
31 void _backend_free (audioCtxHandleT *ctx) {
34 if (ctx->audio_dev) _pulse_free (ctx); else
36 _alsa_free (ctx->idx);
39 void _backend_play (audioCtxHandleT *ctx) {
42 if (ctx->audio_dev) _pulse_play (ctx); else
44 _alsa_play (ctx->idx);
47 void _backend_stop (audioCtxHandleT *ctx) {
50 if (ctx->audio_dev) _pulse_stop (ctx); else
52 _alsa_stop (ctx->idx);
55 int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
58 if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else
60 return _alsa_get_volume (ctx->idx, channel);
63 void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, int vol) {
66 if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else
68 _alsa_set_volume (ctx->idx, channel, vol);
71 void _backend_set_volume_all (audioCtxHandleT *ctx, int vol) {
74 if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else
76 _alsa_set_volume_all (ctx->idx, vol);
79 unsigned char _backend_get_mute (audioCtxHandleT *ctx) {
82 if (ctx->audio_dev) return _pulse_get_mute (ctx); else
84 return _alsa_get_mute (ctx->idx);
87 void _backend_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
90 if (ctx->audio_dev) _pulse_set_mute (ctx, mute); else
92 _alsa_set_mute (ctx->idx, mute);
95 void _backend_set_channels (audioCtxHandleT *ctx, unsigned int channels) {
98 if (ctx->audio_dev) return; else
100 _alsa_set_channels (ctx->idx, channels);
103 /* ------ LOCAL HELPER FUNCTIONS --------- */
105 /* private client context creation ; default values */
106 STATIC audioCtxHandleT* initAudioCtx () {
108 audioCtxHandleT *ctx;
111 ctx = malloc (sizeof(audioCtxHandleT));
112 ctx->audio_dev = NULL;
114 for (i = 0; i < 8; i++)
123 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
128 /* clean client context */
134 /* called when client session dies [e.g. client quits for more than 15mns] */
135 STATIC void freeAudio (void *context) {
140 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
142 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CHECK */
147 /* create a private client context */
148 if (!request->context)
149 request->context = initAudioCtx();
151 _backend_init("default", request->context);
153 jresp = json_object_new_object();
154 json_object_object_add (jresp, "info", json_object_new_string ("Audio initialised"));
158 STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
160 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
161 const char *value = getQueryValue (request, "value");
165 char volume_str[256];
168 /* no "?value=" parameter : return current state */
170 for (i = 0; i < 8; i++) {
171 ctx->volume[i] = _backend_get_volume (ctx, i);
172 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
173 len_str = strlen(volume_str);
175 jresp = json_object_new_object();
176 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
179 /* "?value=" parameter, set volume */
181 volume_i = strdup (value);
182 volume_i = strtok (volume_i, ",");
183 volume[0] = atoi (volume_i);
185 if (100 < volume[0] < 0) {
187 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
188 return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
190 ctx->volume[0] = volume[0];
191 _backend_set_volume (ctx, 0, ctx->volume[0]);
192 snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]);
194 for (i = 1; i < 8; i++) {
195 volume_i = strtok (NULL, ",");
196 /* if there is only one value, set all channels to this one */
197 if (!volume_i && i == 1)
198 _backend_set_volume_all (ctx, ctx->volume[0]);
199 if (!volume_i || 100 < atoi(volume_i) < 0) {
200 ctx->volume[i] = _backend_get_volume (ctx, i);
202 ctx->volume[i] = atoi(volume_i);
203 _backend_set_volume (ctx, i, ctx->volume[i]);
205 len_str = strlen(volume_str);
206 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
208 jresp = json_object_new_object();
209 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
215 STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
217 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
218 const char *value = getQueryValue (request, "value");
219 json_object *jresp = json_object_new_object();
220 char channels_str[256];
222 /* no "?value=" parameter : return current state */
224 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
225 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
228 /* "?value=" parameter, set channels */
230 ctx->channels = atoi (value);
231 _backend_set_channels (ctx, ctx->channels);
233 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
234 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
240 STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
242 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
243 const char *value = getQueryValue (request, "value");
244 json_object *jresp = json_object_new_object();
246 /* no "?value=" parameter : return current state */
248 ctx->mute = _backend_get_mute (ctx);
250 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
251 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
254 /* "?value=" parameter is "1" or "true" */
255 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
257 _backend_set_mute (ctx, ctx->mute);
259 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
262 /* "?value=" parameter is "0" or "false" */
263 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
265 _backend_set_mute (ctx, ctx->mute);
267 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
273 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
275 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
276 const char *value = getQueryValue (request, "value");
277 json_object *jresp = json_object_new_object();
279 /* no "?value=" parameter : return current state */
282 json_object_object_add (jresp, "play", json_object_new_string ("on"))
283 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
286 /* "?value=" parameter is "1" or "true" */
287 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
291 json_object_object_add (jresp, "play", json_object_new_string ("on"));
294 /* "?value=" parameter is "0" or "false" */
295 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
299 json_object_object_add (jresp, "play", json_object_new_string ("off"));
305 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
306 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
309 STATIC AFB_restapi pluginApis[]= {
310 {"init" , AFB_SESSION_CHECK, (AFB_apiCB)init , "Audio API - init"},
311 {"volume" , AFB_SESSION_CHECK, (AFB_apiCB)volume , "Audio API - volume"},
312 {"channels", AFB_SESSION_CHECK, (AFB_apiCB)channels , "Audio API - channels"},
313 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Audio API - mute"},
314 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Audio API - play"},
315 {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Audio API - ping"},
319 PUBLIC AFB_plugin *pluginRegister () {
320 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
321 plugin->type = AFB_PLUGIN_JSON;
322 plugin->info = "Application Framework Binder - Audio plugin";
323 plugin->prefix = "audio";
324 plugin->apis = pluginApis;
326 plugin->freeCtxCB = (AFB_freeCtxCB)freeAudio;