+ return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
+}
+
+
+/* ------ PUBLIC PLUGIN FUNCTIONS --------- */
+
+STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
+
+ audioCtxHandleT *ctx;
+ json_object *jresp;
+ int idx;
+
+ /* create a private client context */
+ ctx = initAudioCtx();
+ request->client->ctx = (audioCtxHandleT*)ctx;
+
+ _alsa_init("default", ctx);
+
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "token", json_object_new_string (request->client->token));
+ return jresp;
+}
+
+STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
+
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
+ const char *value = getQueryValue (request, "value");
+ json_object *jresp;
+ int volume;
+ char volume_str[256];
+
+ /* no "?value=" parameter : return current state */
+ if (!value) {
+ ctx->volume = _alsa_get_volume (ctx->idx);
+ snprintf (volume_str, sizeof(volume_str), "%d", ctx->volume);
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
+ }
+
+ /* "?value=" parameter, set volume */
+ else {
+ volume = atoi (value);
+ if (100 < volume < 0) {
+ request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
+ return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
+ }
+ ctx->volume = volume;
+ _alsa_set_volume (ctx->idx, ctx->volume);
+
+ snprintf (volume_str, sizeof(volume_str), "%d", ctx->volume);
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
+ }
+
+ return jresp;
+}
+
+STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
+
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
+ const char *value = getQueryValue (request, "value");
+ json_object *jresp = json_object_new_object();
+ char channels_str[256];
+
+ /* no "?value=" parameter : return current state */
+ if (!value) {
+ snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
+ json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
+ }
+
+ /* "?value=" parameter, set channels */
+ else {
+ ctx->channels = atoi (value);
+ _alsa_set_channels (ctx->idx, ctx->channels);
+
+ snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
+ json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
+ }
+
+ return jresp;