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 if (!request->context)
68 request->context = initAudioCtx();
70 _alsa_init("default", request->context);
72 jresp = json_object_new_object();
73 json_object_object_add (jresp, "info", json_object_new_string ("Audio initialised"));
77 STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
79 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
80 const char *value = getQueryValue (request, "value");
87 /* no "?value=" parameter : return current state */
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);
94 jresp = json_object_new_object();
95 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
98 /* "?value=" parameter, set volume */
100 volume_i = strdup (value);
101 volume_i = strtok (volume_i, ",");
102 volume[0] = atoi (volume_i);
104 if (100 < volume[0] < 0) {
106 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
107 return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
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]);
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);
121 ctx->volume[i] = atoi(volume_i);
122 _alsa_set_volume (ctx->idx, i, ctx->volume[i]);
124 len_str = strlen(volume_str);
125 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
127 jresp = json_object_new_object();
128 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
134 STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
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];
141 /* no "?value=" parameter : return current state */
143 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
144 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
147 /* "?value=" parameter, set channels */
149 ctx->channels = atoi (value);
150 _alsa_set_channels (ctx->idx, ctx->channels);
152 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
153 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
159 STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
161 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
162 const char *value = getQueryValue (request, "value");
163 json_object *jresp = json_object_new_object();
165 /* no "?value=" parameter : return current state */
167 ctx->mute = _alsa_get_mute (ctx->idx);
169 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
170 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
173 /* "?value=" parameter is "1" or "true" */
174 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
176 _alsa_set_mute (ctx->idx, ctx->mute);
178 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
181 /* "?value=" parameter is "0" or "false" */
182 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
184 _alsa_set_mute (ctx->idx, ctx->mute);
186 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
192 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
194 audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
195 const char *value = getQueryValue (request, "value");
196 json_object *jresp = json_object_new_object();
198 /* no "?value=" parameter : return current state */
201 json_object_object_add (jresp, "play", json_object_new_string ("on"))
202 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
205 /* "?value=" parameter is "1" or "true" */
206 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
208 _alsa_play (ctx->idx);
210 json_object_object_add (jresp, "play", json_object_new_string ("on"));
213 /* "?value=" parameter is "0" or "false" */
214 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
216 _alsa_stop (ctx->idx);
218 json_object_object_add (jresp, "play", json_object_new_string ("off"));
224 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
225 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
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"},
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;
245 plugin->freeCtxCB = (AFB_freeCtxCB)freeAudio;