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 "radio-api.h"
21 /* ********************************************************
23 FULUP integration proposal with client session context
25 ******************************************************** */
27 /* ------ LOCAL HELPER FUNCTIONS --------- */
29 static pluginHandleT *the_radio = NULL;
31 /* detect new radio devices */
32 STATIC void updateRadioDevList(pluginHandleT *handle) {
36 // loop on existing radio if any
37 for (idx = 0; idx < _radio_dev_count(); idx++) {
38 if (idx == MAX_RADIO) break;
39 handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set used to FALSE */
40 handle->radios[idx]->name = (char *) _radio_dev_name(idx);
42 handle->devCount = _radio_dev_count();
45 /* global plugin context creation ; at loading time [radio devices might not be visible] */
46 STATIC pluginHandleT* initRadioPlugin() {
48 pluginHandleT *handle;
50 handle = calloc (1, sizeof(pluginHandleT));
51 updateRadioDevList (handle);
56 /* private client context creation ; default values */
57 STATIC radioCtxHandleT* initRadioCtx () {
61 ctx = malloc (sizeof(radioCtxHandleT));
72 /* reserve a radio device for requesting client, power it on */
73 STATIC AFB_error reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
76 /* loop on all devices, find an unused one */
77 for (idx = 0; idx < _radio_dev_count(); idx++) {
78 if (idx == MAX_RADIO) break;
79 if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */
84 /* try to power it on, passing client context info such as frequency... */
86 /* TODO : try to re-iterate from the next ones if it failed ! */
88 /* globally mark it as reserved */
89 handle->radios[idx]->used = TRUE;
91 /* store relevant info to client context (direct pointer, index) */
92 ctx->radio = handle->radios[idx];
98 /* free a radio device from requesting client, power it off */
99 STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
101 /* stop playing if it was doing this (blocks otherwise) */
102 if (ctx->is_playing) {
104 _radio_stop (ctx->idx);
108 _radio_off (ctx->idx);
110 /* globally mark it as free */
111 handle->radios[ctx->idx]->used = FALSE;
113 /* clean client context */
120 /* called when client session dies [e.g. client quits for more than 15mns] */
121 STATIC void freeRadio (void *context) {
123 releaseRadio (the_radio, context);
128 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
130 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CHECK */
134 /* create a private client context */
135 if (!request->context)
136 request->context = initRadioCtx();
138 jresp = json_object_new_object();
139 json_object_object_add(jresp, "info", json_object_new_string ("Radio initialized"));
143 STATIC json_object* power (AFB_request *request) { /* AFB_SESSION_CHECK */
145 pluginHandleT *handle = the_radio;
146 radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
147 const char *value = getQueryValue (request, "value");
150 /* no "?value=" parameter : return current state */
152 jresp = json_object_new_object();
154 json_object_object_add (jresp, "power", json_object_new_string ("on"))
155 : json_object_object_add (jresp, "power", json_object_new_string ("off"));
158 /* "?value=" parameter is "1" or "true" */
159 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
161 if (reserveRadio (handle, ctx) == AFB_FAIL) {
162 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
163 return (jsonNewMessage (AFB_FAIL, "No more radio devices available"));
166 jresp = json_object_new_object();
167 json_object_object_add (jresp, "power", json_object_new_string ("on"));
170 /* "?value=" parameter is "0" or "false" */
171 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
173 if (releaseRadio (handle, ctx) == AFB_FAIL) {
174 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
175 return (jsonNewMessage (AFB_FAIL, "Unable to release radio device"));
178 jresp = json_object_new_object();
179 json_object_object_add (jresp, "power", json_object_new_string ("off"));
187 STATIC json_object* mode (AFB_request *request) { /* AFB_SESSION_CHECK */
189 radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
190 const char *value = getQueryValue (request, "value");
191 json_object *jresp = json_object_new_object();
193 /* no "?value=" parameter : return current state */
194 if (!value || !ctx->radio) {
196 json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
197 : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
200 /* "?value=" parameter is "1" or "AM" */
201 else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
203 _radio_set_mode (ctx->idx, ctx->mode);
204 json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
207 /* "?value=" parameter is "0" or "FM" */
208 else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
210 _radio_set_mode (ctx->idx, ctx->mode);
211 json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
217 STATIC json_object* freq (AFB_request *request) { /* AFB_SESSION_CHECK */
219 radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
220 const char *value = getQueryValue (request, "value");
221 json_object *jresp = json_object_new_object();
225 /* no "?value=" parameter : return current state */
226 if (!value || !ctx->radio) {
227 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
228 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
231 /* "?value=" parameter, set frequency */
233 freq = strtod (value, NULL);
234 _radio_set_freq (ctx->idx, freq);
235 ctx->freq = (float)freq;
237 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
238 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
244 STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
246 radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
247 const char *value = getQueryValue (request, "value");
248 json_object *jresp = json_object_new_object();
251 /* no "?value=" parameter : return current state */
252 if (!value || !ctx->radio) {
254 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
255 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
258 /* "?value=" parameter is "1" or "true" */
259 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
261 _radio_set_mute (ctx->idx, ctx->mute);
262 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
265 /* "?value=" parameter is "0" or "false" */
266 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
268 _radio_set_mute (ctx->idx, ctx->mute);
269 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
275 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
277 radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
278 const char *value = getQueryValue (request, "value");
279 json_object *jresp = json_object_new_object();
281 /* no "?value=" parameter : return current state */
282 if (!value || !ctx->radio) {
284 json_object_object_add (jresp, "play", json_object_new_string ("on"))
285 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
288 /* "?value=" parameter is "1" or "true" */
289 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
292 _radio_play (ctx->idx);
293 json_object_object_add (jresp, "play", json_object_new_string ("on"));
296 /* "?value=" parameter is "0" or "false" */
297 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
300 _radio_stop (ctx->idx);
301 json_object_object_add (jresp, "play", json_object_new_string ("off"));
307 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
308 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
312 STATIC AFB_restapi pluginApis[]= {
313 {"init" , AFB_SESSION_CHECK, (AFB_apiCB)init , "Radio API - init"},
314 {"power" , AFB_SESSION_CHECK, (AFB_apiCB)power , "Radio API - power"},
315 {"mode" , AFB_SESSION_CHECK, (AFB_apiCB)mode , "Radio API - mode"},
316 {"freq" , AFB_SESSION_CHECK, (AFB_apiCB)freq , "Radio API - freq"},
317 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Radio API - mute"},
318 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Radio API - play"},
319 {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Radio API - ping"},
323 PUBLIC AFB_plugin* pluginRegister () {
324 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
325 plugin->type = AFB_PLUGIN_JSON;
326 plugin->info = "Application Framework Binder - Radio plugin";
327 plugin->prefix = "radio";
328 plugin->apis = pluginApis;
330 plugin->freeCtxCB = (AFB_freeCtxCB)freeRadio;
332 the_radio = initRadioPlugin();