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 () {
28 audioCtxHandleT *actx;
30 actx = malloc (sizeof(audioCtxHandleT));
39 STATIC AFB_error releaseAudio (audioCtxHandleT *actx) {
42 _alsa_free (actx->idx);
44 /* clean client context */
50 /* called when client session dies [e.g. client quits for more than 15mns] */
51 STATIC json_object* freeAudio (AFB_clientCtx *client) {
53 releaseAudio (client->ctx);
56 return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
60 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
62 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
64 audioCtxHandleT *actx;
68 /* create a private client context */
69 actx = initAudioCtx();
70 request->client->ctx = (audioCtxHandleT*)actx;
72 _alsa_init("default", actx);
74 jresp = json_object_new_object();
75 json_object_object_add (jresp, "token", json_object_new_string (request->client->token));
79 STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
81 audioCtxHandleT *actx = (audioCtxHandleT*)request->client->ctx;
82 const char *value = getQueryValue (request, "value");
87 /* no "?value=" parameter : return current state */
89 actx->volume = _alsa_get_volume (actx->idx);
90 snprintf (volume_str, sizeof(volume_str), "%d", actx->volume);
91 jresp = json_object_new_object();
92 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
95 /* "?value=" parameter, set volume */
97 volume = atoi (value);
98 if (100 < volume < 0) {
99 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
100 return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
102 actx->volume = volume;
103 _alsa_set_volume (actx->idx, actx->volume);
105 snprintf (volume_str, sizeof(volume_str), "%d", actx->volume);
106 jresp = json_object_new_object();
107 json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
113 STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
115 audioCtxHandleT *actx = (audioCtxHandleT*)request->client->ctx;
116 const char *value = getQueryValue (request, "value");
117 json_object *jresp = json_object_new_object();
118 char channels_str[256];
120 /* no "?value=" parameter : return current state */
122 snprintf (channels_str, sizeof(channels_str), "%d", actx->channels);
123 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
126 /* "?value=" parameter, set channels */
128 actx->channels = atoi (value);
129 _alsa_set_channels (actx->idx, actx->channels);
131 snprintf (channels_str, sizeof(channels_str), "%d", actx->channels);
132 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
138 STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
140 audioCtxHandleT *actx = (audioCtxHandleT*)request->client->ctx;
141 const char *value = getQueryValue (request, "value");
142 json_object *jresp = json_object_new_object();
144 /* no "?value=" parameter : return current state */
146 actx->mute = _alsa_get_mute (actx->idx);
148 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
149 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
152 /* "?value=" parameter is "1" or "on" */
153 else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
155 _alsa_set_mute (actx->idx, actx->mute);
157 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
160 /* "?value=" parameter is "0" or "off" */
161 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
163 _alsa_set_mute (actx->idx, actx->mute);
165 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
171 STATIC json_object* refresh (AFB_request *request) { /* AFB_SESSION_RENEW */
172 json_object *jresp = json_object_new_object();
173 json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
177 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
178 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
182 STATIC AFB_restapi pluginApis[]= {
183 {"init" , AFB_SESSION_CREATE, (AFB_apiCB)init , "Audio API - init"},
184 {"volume" , AFB_SESSION_CHECK, (AFB_apiCB)volume , "Audio API - volume"},
185 {"channels", AFB_SESSION_CHECK, (AFB_apiCB)channels , "Audio API - channels"},
186 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Audio API - mute"},
187 {"refresh", AFB_SESSION_RENEW, (AFB_apiCB)refresh , "Audio API - refresh"},
188 {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Audio API - ping"},
192 PUBLIC AFB_plugin *audioRegister () {
193 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
194 plugin->type = AFB_PLUGIN_JSON;
195 plugin->info = "Application Framework Binder - Audio plugin";
196 plugin->prefix = "audio";
197 plugin->apis = pluginApis;
199 plugin->freeCtxCB = freeAudio;