Fix Audio plugin loading, mute on Porter/Koelsch
[src/app-framework-binder.git] / plugins / audio / audio-api.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Manuel Bachmann"
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include "audio-api.h"
20 #include "audio-alsa.h"
21
22
23 /* ------ LOCAL HELPER FUNCTIONS --------- */
24
25 /* private client context creation ; default values */
26 STATIC audioCtxHandleT* initAudioCtx () {
27
28     audioCtxHandleT *ctx;
29     int i;
30
31     ctx = malloc (sizeof(audioCtxHandleT));
32     ctx->idx = -1;
33     for (i = 0; i < 8; i++)
34         ctx->volume[i] = 25;
35     ctx->channels = 2;
36     ctx->mute = 0;
37     ctx->is_playing = 0;
38
39     return ctx;
40 }
41
42 STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
43
44     /* power it off */
45     _alsa_free (ctx->idx);
46
47     /* clean client context */
48     ctx->idx = -1;
49
50     return AFB_SUCCESS;
51 }
52
53 /* called when client session dies [e.g. client quits for more than 15mns] */
54 STATIC void freeAudio (void *context) {
55     free (context);    
56 }
57
58
59 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
60
61 STATIC json_object* init (AFB_request *request) {       /* AFB_SESSION_CREATE */
62
63     json_object *jresp;
64     int idx;
65
66     /* create a private client context */
67     request->context = initAudioCtx();
68     
69     _alsa_init("default", request->context);
70     
71     jresp = json_object_new_object();
72     json_object_object_add (jresp, "info", json_object_new_string ("Audio initialised"));
73     return (jresp);
74 }
75
76 STATIC json_object* volume (AFB_request *request) {      /* AFB_SESSION_CHECK */
77
78     audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
79     const char *value = getQueryValue (request, "value");
80     json_object *jresp;
81     int volume[8], i;
82     char *volume_i;
83     char volume_str[256];
84     int len_str = 0;
85
86     /* no "?value=" parameter : return current state */
87     if (!value) {
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);
92         }
93         jresp = json_object_new_object();
94         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
95     }
96
97     /* "?value=" parameter, set volume */
98     else {
99         volume_i = strdup (value);
100         volume_i = strtok (volume_i, ",");
101         volume[0] = atoi (volume_i);
102
103         if (100 < volume[0] < 0) {
104             free (volume_i);
105             request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
106             return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
107         }
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]);
111
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);
119             } else {
120                ctx->volume[i] = atoi(volume_i);
121                _alsa_set_volume (ctx->idx, i, ctx->volume[i]);
122             }
123             len_str = strlen(volume_str);
124             snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]);
125         }
126         jresp = json_object_new_object();
127         json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
128     }
129
130     return jresp;
131 }
132
133 STATIC json_object* channels (AFB_request *request) {    /* AFB_SESSION_CHECK */
134
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];
139
140     /* no "?value=" parameter : return current state */
141     if (!value) {
142         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
143         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
144     }
145
146     /* "?value=" parameter, set channels */
147     else {
148         ctx->channels = atoi (value);
149         _alsa_set_channels (ctx->idx, ctx->channels);
150
151         snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
152         json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
153     }
154
155     return jresp;
156 }
157
158 STATIC json_object* mute (AFB_request *request) {        /* AFB_SESSION_CHECK */
159
160     audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
161     const char *value = getQueryValue (request, "value");
162     json_object *jresp = json_object_new_object();
163
164     /* no "?value=" parameter : return current state */
165     if (!value) {
166         ctx->mute = _alsa_get_mute (ctx->idx);
167         ctx->mute ?
168             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
169           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
170     }
171
172     /* "?value=" parameter is "1" or "true" */
173     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
174         ctx->mute = 1;
175         _alsa_set_mute (ctx->idx, ctx->mute);
176
177         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
178     }
179
180     /* "?value=" parameter is "0" or "false" */
181     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
182         ctx->mute = 0;
183         _alsa_set_mute (ctx->idx, ctx->mute);
184
185         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
186     }
187
188     return jresp;
189 }
190
191 STATIC json_object* play (AFB_request *request) {        /* AFB_SESSION_CHECK */
192
193     audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
194     const char *value = getQueryValue (request, "value");
195     json_object *jresp = json_object_new_object();
196
197     /* no "?value=" parameter : return current state */
198     if (!value) {
199         ctx->is_playing ?
200             json_object_object_add (jresp, "play", json_object_new_string ("on"))
201           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
202     }
203
204     /* "?value=" parameter is "1" or "true" */
205     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
206         ctx->is_playing = 1;
207         _alsa_play (ctx->idx);
208
209         json_object_object_add (jresp, "play", json_object_new_string ("on"));
210     }
211
212     /* "?value=" parameter is "0" or "false" */
213     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
214         ctx->is_playing = 0;
215         _alsa_stop (ctx->idx);
216
217         json_object_object_add (jresp, "play", json_object_new_string ("off"));
218     }
219
220     return jresp;
221 }
222
223 STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
224     return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
225 }
226
227 STATIC AFB_restapi pluginApis[]= {
228   {"init"    , AFB_SESSION_CREATE, (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"},
234   {NULL}
235 };
236
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;
243
244     plugin->freeCtxCB = (AFB_freeCtxCB)freeAudio;
245
246     return (plugin);
247 };