aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/audio/audio-api.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/audio/audio-api.c')
-rw-r--r--plugins/audio/audio-api.c126
1 files changed, 119 insertions, 7 deletions
diff --git a/plugins/audio/audio-api.c b/plugins/audio/audio-api.c
index 547f97ed..0fcc73d1 100644
--- a/plugins/audio/audio-api.c
+++ b/plugins/audio/audio-api.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2015 "IoT.bzh"
- * Author "Fulup Ar Foll"
+ * Author "Manuel Bachmann"
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -28,17 +28,29 @@ STATIC audioCtxHandleT* initAudioCtx () {
audioCtxHandleT *ctx;
ctx = malloc (sizeof(audioCtxHandleT));
+ ctx->idx = -1;
ctx->volume = 25;
- ctx->rate = 22050;
ctx->channels = 2;
+ ctx->mute = 0;
return ctx;
}
+STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
+
+ /* power it off */
+ _alsa_free (ctx->idx);
+
+ /* clean client context */
+ ctx->idx = -1;
+
+ return AFB_SUCCESS;
+}
+
/* called when client session dies [e.g. client quits for more than 15mns] */
STATIC json_object* freeAudio (AFB_clientCtx *client) {
- //releaseAudio (client->plugin->handle, client->ctx);
+ releaseAudio (client->ctx);
free (client->ctx);
return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
@@ -51,6 +63,7 @@ STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
audioCtxHandleT *ctx;
json_object *jresp;
+ int idx;
/* create a private client context */
ctx = initAudioCtx();
@@ -60,13 +73,112 @@ STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
jresp = json_object_new_object();
json_object_object_add (jresp, "token", json_object_new_string (request->client->token));
+ return jresp;
}
+STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
-STATIC AFB_restapi pluginApis[]= {
- {"init" , AFB_SESSION_CREATE, (AFB_apiCB)init , "Audio API - init"},
-// {"error" , AFB_SESSION_CHECK, (AFB_apiCB)wrongApi , "Ping Application Framework"},
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
+ const char *value = getQueryValue (request, "value");
+ json_object *jresp;
+ int volume;
+ char volume_str[256];
+
+ /* no "?value=" parameter : return current state */
+ if (!value) {
+ ctx->volume = _alsa_get_volume (ctx->idx);
+ snprintf (volume_str, sizeof(volume_str), "%d", ctx->volume);
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
+ }
+
+ /* "?value=" parameter, set volume */
+ else {
+ volume = atoi (value);
+ if (100 < volume < 0) {
+ request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
+ return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
+ }
+ ctx->volume = volume;
+ _alsa_set_volume (ctx->idx, ctx->volume);
+
+ snprintf (volume_str, sizeof(volume_str), "%d", ctx->volume);
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
+ }
+
+ return jresp;
+}
+
+STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
+
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
+ const char *value = getQueryValue (request, "value");
+ json_object *jresp = json_object_new_object();
+ char channels_str[256];
+
+ /* no "?value=" parameter : return current state */
+ if (!value) {
+ snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
+ json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
+ }
+
+ /* "?value=" parameter, set channels */
+ else {
+ ctx->channels = atoi (value);
+ _alsa_set_channels (ctx->idx, ctx->channels);
+
+ snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
+ json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
+ }
+
+ return jresp;
+}
+STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
+
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
+ const char *value = getQueryValue (request, "value");
+ json_object *jresp = json_object_new_object();
+
+ /* no "?value=" parameter : return current state */
+ if (!value) {
+ ctx->mute = _alsa_get_mute (ctx->idx);
+ ctx->mute ?
+ json_object_object_add (jresp, "mute", json_object_new_string ("on"))
+ : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
+ }
+
+ /* "?value=" parameter is "1" or "on" */
+ else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
+ ctx->mute = 1;
+ _alsa_set_mute (ctx->idx, ctx->mute);
+
+ json_object_object_add (jresp, "mute", json_object_new_string ("on"));
+ }
+
+ /* "?value=" parameter is "0" or "off" */
+ if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
+ ctx->mute = 0;
+ _alsa_set_mute (ctx->idx, ctx->mute);
+
+ json_object_object_add (jresp, "mute", json_object_new_string ("off"));
+ }
+
+ return jresp;
+}
+
+STATIC json_object* status (AFB_request *request) { /* AFB_SESSION_RENEW */
+ return NULL;
+}
+
+
+STATIC AFB_restapi pluginApis[]= {
+ {"init" , AFB_SESSION_CREATE, (AFB_apiCB)init , "Audio API - init"},
+ {"volume" , AFB_SESSION_CHECK, (AFB_apiCB)volume , "Audio API - volume"},
+ {"channels", AFB_SESSION_CHECK, (AFB_apiCB)channels , "Audio API - channels"},
+ {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Audio API - mute"},
+ {"status" , AFB_SESSION_RENEW, (AFB_apiCB)status , "Audio API - status"},
{NULL}
};
@@ -80,4 +192,4 @@ PUBLIC AFB_plugin *audioRegister () {
plugin->freeCtxCB = freeAudio;
return (plugin);
-}; \ No newline at end of file
+};