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 json_object* freeAudio (AFB_clientCtx *client) {
56 releaseAudio (client->ctx);
59 return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
63 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
65 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
71 /* create a private client context */
73 request->client->ctx = (audioCtxHandleT*)ctx;
75 _alsa_init("default", ctx);
77 jresp = json_object_new_object();
78 json_object_object_add (jresp, "token", json_object_new_string (request->client->token));
82 STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
84 audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
85 const char *value = getQueryValue (request, "value");
92 /* no "?value=" parameter : return current state */
94 for (i = 0; i < 8; i++) {
95 ctx->volume[i] = _alsa_get_volume (ctx->idx, i);
96 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
97 len_str = strlen(volume_str);
99 jresp = json_object_new_object();
100 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
103 /* "?value=" parameter, set volume */
105 volume_i = strdup (value);
106 volume_i = strtok (volume_i, ",");
107 volume[0] = atoi (volume_i);
109 if (100 < volume[0] < 0) {
111 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
112 return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
114 ctx->volume[0] = volume[0];
115 _alsa_set_volume (ctx->idx, 0, ctx->volume[0]);
116 snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]);
118 for (i = 1; i < 8; i++) {
119 volume_i = strtok (NULL, ",");
120 /* if there is only one value, set all channels to this one */
121 if (!volume_i && i == 1)
122 _alsa_set_volume_all (ctx->idx, ctx->volume[0]);
123 if (!volume_i || 100 < atoi(volume_i) < 0) {
124 ctx->volume[i] = _alsa_get_volume (ctx->idx, i);
126 ctx->volume[i] = atoi(volume_i);
127 _alsa_set_volume (ctx->idx, i, ctx->volume[i]);
129 len_str = strlen(volume_str);
130 snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
132 jresp = json_object_new_object();
133 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
139 STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
141 audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
142 const char *value = getQueryValue (request, "value");
143 json_object *jresp = json_object_new_object();
144 char channels_str[256];
146 /* no "?value=" parameter : return current state */
148 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
149 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
152 /* "?value=" parameter, set channels */
154 ctx->channels = atoi (value);
155 _alsa_set_channels (ctx->idx, ctx->channels);
157 snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
158 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
164 STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
166 audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
167 const char *value = getQueryValue (request, "value");
168 json_object *jresp = json_object_new_object();
170 /* no "?value=" parameter : return current state */
172 ctx->mute = _alsa_get_mute (ctx->idx);
174 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
175 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
178 /* "?value=" parameter is "1" or "true" */
179 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
181 _alsa_set_mute (ctx->idx, ctx->mute);
183 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
186 /* "?value=" parameter is "0" or "false" */
187 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
189 _alsa_set_mute (ctx->idx, ctx->mute);
191 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
197 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
199 audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
200 const char *value = getQueryValue (request, "value");
201 json_object *jresp = json_object_new_object();
203 /* no "?value=" parameter : return current state */
206 json_object_object_add (jresp, "play", json_object_new_string ("on"))
207 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
210 /* "?value=" parameter is "1" or "true" */
211 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
213 _alsa_play (ctx->idx);
215 json_object_object_add (jresp, "play", json_object_new_string ("on"));
218 /* "?value=" parameter is "0" or "false" */
219 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
221 _alsa_stop (ctx->idx);
223 json_object_object_add (jresp, "play", json_object_new_string ("off"));
229 STATIC json_object* refresh (AFB_request *request) { /* AFB_SESSION_RENEW */
230 json_object *jresp = json_object_new_object();
231 json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
235 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
236 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
240 STATIC AFB_restapi pluginApis[]= {
241 {"init" , AFB_SESSION_CREATE, (AFB_apiCB)init , "Audio API - init"},
242 {"volume" , AFB_SESSION_CHECK, (AFB_apiCB)volume , "Audio API - volume"},
243 {"channels", AFB_SESSION_CHECK, (AFB_apiCB)channels , "Audio API - channels"},
244 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Audio API - mute"},
245 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Audio API - play"},
246 {"refresh" , AFB_SESSION_RENEW, (AFB_apiCB)refresh , "Audio API - refresh"},
247 {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Audio API - ping"},
251 PUBLIC AFB_plugin *pluginRegister () {
252 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
253 plugin->type = AFB_PLUGIN_JSON;
254 plugin->info = "Application Framework Binder - Audio plugin";
255 plugin->prefix = "audio";
256 plugin->apis = pluginApis;
258 plugin->freeCtxCB = freeAudio;