2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Manuel Bachmann"
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include <json-c/json.h>
22 #include "audio-api.h"
23 #include "audio-alsa.h"
25 #include "audio-pulse.h"
28 #include "afb-plugin.h"
29 #include "afb-req-itf.h"
31 /* ------ BACKEND FUNCTIONS ------- */
33 unsigned char _backend_init (const char *name, audioCtxHandleT *ctx) {
35 char *backend_env = getenv ("AFB_AUDIO_OUTPUT");
36 unsigned char res = 0;
39 if (!backend_env || (strcasecmp (backend_env, "Pulse") == 0))
40 res = _pulse_init (name, ctx);
43 res = _alsa_init (name, ctx);
46 fprintf (stderr, "Could not initialize Audio backend\n");
51 void _backend_free (audioCtxHandleT *ctx) {
54 if (ctx->audio_dev) _pulse_free (ctx); else
56 _alsa_free (ctx->name);
59 void _backend_play (audioCtxHandleT *ctx) {
62 if (ctx->audio_dev) _pulse_play (ctx); else
64 _alsa_play (ctx->idx);
67 void _backend_stop (audioCtxHandleT *ctx) {
70 if (ctx->audio_dev) _pulse_stop (ctx); else
72 _alsa_stop (ctx->idx);
75 unsigned int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
78 if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else
80 return _alsa_get_volume (ctx->idx, channel);
83 void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) {
86 if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else
88 _alsa_set_volume (ctx->idx, channel, vol);
91 void _backend_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) {
94 if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else
96 _alsa_set_volume_all (ctx->idx, vol);
99 unsigned char _backend_get_mute (audioCtxHandleT *ctx) {
102 if (ctx->audio_dev) return _pulse_get_mute (ctx); else
104 return _alsa_get_mute (ctx->idx);
107 void _backend_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
110 if (ctx->audio_dev) _pulse_set_mute (ctx, mute); else
112 _alsa_set_mute (ctx->idx, mute);
115 void _backend_set_channels (audioCtxHandleT *ctx, unsigned int channels) {
118 if (ctx->audio_dev) return; else
120 _alsa_set_channels (ctx->idx, channels);
123 /* ------ LOCAL HELPER FUNCTIONS --------- */
125 /* private client context constructor ; default values */
126 static audioCtxHandleT* initAudioCtx () {
128 audioCtxHandleT *ctx;
131 ctx = malloc (sizeof(audioCtxHandleT));
132 ctx->audio_dev = NULL;
135 for (i = 0; i < 8; i++)
144 static void releaseAudioCtx (void *context) {
146 audioCtxHandleT *ctx = (audioCtxHandleT*) context;
151 /* clean client context */
152 ctx->audio_dev = NULL;
160 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
162 static void init (struct afb_req request) { /* AFB_SESSION_CHECK */
164 audioCtxHandleT *ctx = (audioCtxHandleT*) afb_req_context_get(request);
167 /* create a private client context */
169 ctx = initAudioCtx();
170 afb_req_context_set (request, ctx, releaseAudioCtx);
173 if (!_backend_init ("default", ctx)) {
174 afb_req_fail (request, "failed", "backend initialization failed");
177 jresp = json_object_new_object();
178 json_object_object_add (jresp, "init", json_object_new_string ("success"));
179 afb_req_success (request, jresp, "Audio initialized");
182 static void volume (struct afb_req request) { /* AFB_SESSION_CHECK */
184 audioCtxHandleT *ctx = (audioCtxHandleT*) afb_req_context_get(request);
185 struct afb_arg arg = afb_req_get (request, "value");
186 const char *value = arg.value;
188 unsigned int volume[8], i;
190 char volume_str[256];
193 /* no "?value=" parameter : return current state */
195 for (i = 0; i < 8; i++) {
196 ctx->volume[i] = _backend_get_volume (ctx, i);
197 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
198 len_str = strlen(volume_str);
200 jresp = json_object_new_object();
201 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
204 /* "?value=" parameter, set volume */
206 volume_i = strdup (value);
207 volume_i = strtok (volume_i, ",");
208 volume[0] = (unsigned int) atoi (volume_i);
210 if (100 < volume[0]) {
212 afb_req_fail (request, "failed", "volume must be between 0 and 100");
215 ctx->volume[0] = volume[0];
216 _backend_set_volume (ctx, 0, ctx->volume[0]);
217 snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]);
219 for (i = 1; i < 8; i++) {
220 volume_i = strtok (NULL, ",");
221 /* if there is only one value, set all channels to this one */
222 if (!volume_i && i == 1)
223 _backend_set_volume_all (ctx, ctx->volume[0]);
224 if (!volume_i || 100 < atoi(volume_i) || atoi(volume_i) < 0) {
225 ctx->volume[i] = _backend_get_volume (ctx, i);
227 ctx->volume[i] = (unsigned int) atoi(volume_i);
228 _backend_set_volume (ctx, i, ctx->volume[i]);
230 len_str = strlen(volume_str);
231 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
233 jresp = json_object_new_object();
234 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
237 afb_req_success (request, jresp, "Audio - Volume changed");
240 static void channels (struct afb_req request) { /* AFB_SESSION_CHECK */
242 audioCtxHandleT *ctx = (audioCtxHandleT*) afb_req_context_get(request);
243 struct afb_arg arg = afb_req_get (request, "value");
244 const char *value = arg.value;
245 json_object *jresp = json_object_new_object();
246 char channels_str[256];
248 /* no "?value=" parameter : return current state */
250 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
251 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
254 /* "?value=" parameter, set channels */
256 ctx->channels = (unsigned int) atoi (value);
257 _backend_set_channels (ctx, ctx->channels);
259 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
260 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
263 afb_req_success (request, jresp, "Audio - Channels set");
266 static void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
268 audioCtxHandleT *ctx = (audioCtxHandleT*) afb_req_context_get(request);
269 struct afb_arg arg = afb_req_get (request, "value");
270 const char *value = arg.value;
271 json_object *jresp = json_object_new_object();
273 /* no "?value=" parameter : return current state */
275 ctx->mute = _backend_get_mute (ctx);
277 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
278 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
281 /* "?value=" parameter is "1" or "true" */
282 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
284 _backend_set_mute (ctx, ctx->mute);
286 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
289 /* "?value=" parameter is "0" or "false" */
290 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
292 _backend_set_mute (ctx, ctx->mute);
294 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
297 afb_req_success (request, jresp, "Audio - Mute set");
300 static void play (struct afb_req request) { /* AFB_SESSION_CHECK */
302 audioCtxHandleT *ctx = (audioCtxHandleT*) afb_req_context_get(request);
303 struct afb_arg arg = afb_req_get (request, "value");
304 const char *value = arg.value;
305 json_object *jresp = json_object_new_object();
307 /* no "?value=" parameter : return current state */
310 json_object_object_add (jresp, "play", json_object_new_string ("on"))
311 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
314 /* "?value=" parameter is "1" or "true" */
315 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
319 json_object_object_add (jresp, "play", json_object_new_string ("on"));
322 /* "?value=" parameter is "0" or "false" */
323 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
327 json_object_object_add (jresp, "play", json_object_new_string ("off"));
330 afb_req_success (request, jresp, "Audio - Play");
333 static void ping (struct afb_req request) { /* AFB_SESSION_NONE */
334 afb_req_success (request, NULL, "Audio - Ping success");
337 static const struct AFB_restapi pluginApis[]= {
338 {"init" , AFB_SESSION_CHECK, init , "Audio API - init"},
339 {"volume" , AFB_SESSION_CHECK, volume , "Audio API - volume"},
340 {"channels", AFB_SESSION_CHECK, channels , "Audio API - channels"},
341 {"mute" , AFB_SESSION_CHECK, mute , "Audio API - mute"},
342 {"play" , AFB_SESSION_CHECK, play , "Audio API - play"},
343 {"ping" , AFB_SESSION_NONE, ping , "Audio API - ping"},
347 static const struct AFB_plugin pluginDesc = {
348 .type = AFB_PLUGIN_JSON,
349 .info = "Application Framework Binder - Audio plugin",
354 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)