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 () {
30 ctx = malloc (sizeof(audioCtxHandleT));
39 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
42 _alsa_free (ctx->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 */
68 /* create a private client context */
70 request->client->ctx = (audioCtxHandleT*)ctx;
72 _alsa_init("default", ctx);
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 *ctx = (audioCtxHandleT*)request->client->ctx;
82 const char *value = getQueryValue (request, "value");
87 /* no "?value=" parameter : return current state */
89 ctx->volume = _alsa_get_volume (ctx->idx);
90 snprintf (volume_str, sizeof(volume_str), "%d", ctx->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 ctx->volume = volume;
103 _alsa_set_volume (ctx->idx, ctx->volume);
105 snprintf (volume_str, sizeof(volume_str), "%d", ctx->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 *ctx = (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", ctx->channels);
123 json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
126 /* "?value=" parameter, set channels */
128 ctx->channels = atoi (value);
129 _alsa_set_channels (ctx->idx, ctx->channels);
131 snprintf (channels_str, sizeof(channels_str), "%d", ctx->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 *ctx = (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 ctx->mute = _alsa_get_mute (ctx->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 (ctx->idx, ctx->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 (ctx->idx, ctx->mute);
165 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
171 STATIC json_object* status (AFB_request *request) { /* AFB_SESSION_RENEW */
176 STATIC AFB_restapi pluginApis[]= {
177 {"init" , AFB_SESSION_CREATE, (AFB_apiCB)init , "Audio API - init"},
178 {"volume" , AFB_SESSION_CHECK, (AFB_apiCB)volume , "Audio API - volume"},
179 {"channels", AFB_SESSION_CHECK, (AFB_apiCB)channels , "Audio API - channels"},
180 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Audio API - mute"},
181 {"status" , AFB_SESSION_RENEW, (AFB_apiCB)status , "Audio API - status"},
185 PUBLIC AFB_plugin *audioRegister () {
186 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
187 plugin->type = AFB_PLUGIN_JSON;
188 plugin->info = "Application Framework Binder - Audio plugin";
189 plugin->prefix = "audio";
190 plugin->apis = pluginApis;
192 plugin->freeCtxCB = freeAudio;