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"
20 #include "audio-alsa.h"
23 /* ------ LOCAL HELPER FUNCTIONS --------- */
25 /* private client context creation ; default values */
26 STATIC audioCtxHandleT* initAudioCtx () {
31 ctx = malloc (sizeof(audioCtxHandleT));
33 for (i = 0; i < 8; i++)
42 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
45 _alsa_free (ctx->idx);
47 /* clean client context */
53 /* called when client session dies [e.g. client quits for more than 15mns] */
54 STATIC void freeAudio (void *context) {
59 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
61 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CHECK */
66 /* create a private client context */
67 request->context = initAudioCtx();
69 _alsa_init("default", request->context);
71 jresp = json_object_new_object();
72 json_object_object_add (jresp, "info", json_object_new_string ("Audio initialised"));
76 STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
78 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
79 const char *value = getQueryValue (request, "value");
86 /* no "?value=" parameter : return current state */
88 for (i = 0; i < 8; i++) {
89 ctx->volume[i] = _alsa_get_volume (ctx->idx, i);
90 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
91 len_str = strlen(volume_str);
93 jresp = json_object_new_object();
94 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
97 /* "?value=" parameter, set volume */
99 volume_i = strdup (value);
100 volume_i = strtok (volume_i, ",");
101 volume[0] = atoi (volume_i);
103 if (100 < volume[0] < 0) {
105 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
106 return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
108 ctx->volume[0] = volume[0];
109 _alsa_set_volume (ctx->idx, 0, ctx->volume[0]);
110 snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]);
112 for (i = 1; i < 8; i++) {
113 volume_i = strtok (NULL, ",");
114 /* if there is only one value, set all channels to this one */
115 if (!volume_i && i == 1)
116 _alsa_set_volume_all (ctx->idx, ctx->volume[0]);
117 if (!volume_i || 100 < atoi(volume_i) < 0) {
118 ctx->volume[i] = _alsa_get_volume (ctx->idx, i);
120 ctx->volume[i] = atoi(volume_i);
121 _alsa_set_volume (ctx->idx, i, ctx->volume[i]);
123 len_str = strlen(volume_str);
124 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
126 jresp = json_object_new_object();
127 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
133 STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
135 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
136 const char *value = getQueryValue (request, "value");
137 json_object *jresp = json_object_new_object();
138 char channels_str[256];
140 /* no "?value=" parameter : return current state */
142 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
143 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
146 /* "?value=" parameter, set channels */
148 ctx->channels = atoi (value);
149 _alsa_set_channels (ctx->idx, ctx->channels);
151 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
152 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
158 STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
160 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
161 const char *value = getQueryValue (request, "value");
162 json_object *jresp = json_object_new_object();
164 /* no "?value=" parameter : return current state */
166 ctx->mute = _alsa_get_mute (ctx->idx);
168 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
169 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
172 /* "?value=" parameter is "1" or "true" */
173 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
175 _alsa_set_mute (ctx->idx, ctx->mute);
177 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
180 /* "?value=" parameter is "0" or "false" */
181 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
183 _alsa_set_mute (ctx->idx, ctx->mute);
185 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
191 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
193 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
194 const char *value = getQueryValue (request, "value");
195 json_object *jresp = json_object_new_object();
197 /* no "?value=" parameter : return current state */
200 json_object_object_add (jresp, "play", json_object_new_string ("on"))
201 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
204 /* "?value=" parameter is "1" or "true" */
205 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
207 _alsa_play (ctx->idx);
209 json_object_object_add (jresp, "play", json_object_new_string ("on"));
212 /* "?value=" parameter is "0" or "false" */
213 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
215 _alsa_stop (ctx->idx);
217 json_object_object_add (jresp, "play", json_object_new_string ("off"));
223 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
224 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
227 STATIC AFB_restapi pluginApis[]= {
228 {"init" , AFB_SESSION_CHECK, (AFB_apiCB)init , "Audio API - init"},
229 {"volume" , AFB_SESSION_CHECK, (AFB_apiCB)volume , "Audio API - volume"},
230 {"channels", AFB_SESSION_CHECK, (AFB_apiCB)channels , "Audio API - channels"},
231 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Audio API - mute"},
232 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Audio API - play"},
233 {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Audio API - ping"},
237 PUBLIC AFB_plugin *pluginRegister () {
238 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
239 plugin->type = AFB_PLUGIN_JSON;
240 plugin->info = "Application Framework Binder - Audio plugin";
241 plugin->prefix = "audio";
242 plugin->apis = pluginApis;
244 plugin->freeCtxCB = (AFB_freeCtxCB)freeAudio;