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"
20 #include "radio-rtlsdr.h"
22 /* ********************************************************
24 FULUP integration proposal with client session context
26 ******************************************************** */
28 /* ------ LOCAL HELPER FUNCTIONS --------- */
30 /* detect new radio devices */
31 STATIC void updateRadioDevList(pluginHandleT *handle) {
35 // loop on existing radio if any
36 for (idx = 0; idx < _radio_dev_count(); idx++) {
37 if (idx == MAX_RADIO) break;
38 handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set used to FALSE */
39 handle->radios[idx]->name = (char *) _radio_dev_name(idx);
41 handle->devCount = _radio_dev_count();
44 /* global plugin context creation ; at loading time [radio devices might not be visible] */
45 STATIC pluginHandleT* initRadioPlugin() {
47 pluginHandleT *handle;
49 handle = calloc (1, sizeof(pluginHandleT));
50 updateRadioDevList (handle);
55 /* private client context creation ; default values */
56 STATIC radioCtxHandleT* initRadioCtx () {
60 ctx = malloc (sizeof(radioCtxHandleT));
71 /* reserve a radio device for requesting client, power it on */
72 STATIC AFB_error reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
75 /* loop on all devices, find an unused one */
76 for (idx = 0; idx < _radio_dev_count(); idx++) {
77 if (idx == MAX_RADIO) break;
78 if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */
83 /* try to power it on, passing client context info such as frequency... */
85 /* TODO : try to re-iterate from the next ones if it failed ! */
87 /* globally mark it as reserved */
88 handle->radios[idx]->used = TRUE;
90 /* store relevant info to client context (direct pointer, index) */
91 ctx->radio = handle->radios[idx];
97 /* free a radio device from requesting client, power it off */
98 STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
100 /* stop playing if it was doing this (blocks otherwise) */
101 if (ctx->is_playing) {
103 _radio_stop (ctx->idx);
107 _radio_off (ctx->idx);
109 /* globally mark it as free */
110 handle->radios[ctx->idx]->used = FALSE;
112 /* clean client context */
119 /* called when client session dies [e.g. client quits for more than 15mns] */
120 STATIC void freeRadio (void *context, void *handle) {
122 releaseRadio (handle, context);
127 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
129 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CHECK */
133 /* create a private client context */
134 if (!request->context)
135 request->context = initRadioCtx();
137 jresp = json_object_new_object();
138 json_object_object_add(jresp, "info", json_object_new_string ("Radio initialized"));
142 STATIC json_object* power (AFB_request *request) { /* AFB_SESSION_CHECK */
144 pluginHandleT *handle = (pluginHandleT*)request->handle;
145 radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
146 const char *value = getQueryValue (request, "value");
149 /* no "?value=" parameter : return current state */
151 jresp = json_object_new_object();
153 json_object_object_add (jresp, "power", json_object_new_string ("on"))
154 : json_object_object_add (jresp, "power", json_object_new_string ("off"));
157 /* "?value=" parameter is "1" or "true" */
158 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
160 if (reserveRadio (handle, ctx) == AFB_FAIL) {
161 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
162 return (jsonNewMessage (AFB_FAIL, "No more radio devices available"));
165 jresp = json_object_new_object();
166 json_object_object_add (jresp, "power", json_object_new_string ("on"));
169 /* "?value=" parameter is "0" or "false" */
170 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
172 if (releaseRadio (handle, ctx) == AFB_FAIL) {
173 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
174 return (jsonNewMessage (AFB_FAIL, "Unable to release radio device"));
177 jresp = json_object_new_object();
178 json_object_object_add (jresp, "power", json_object_new_string ("off"));
184 STATIC json_object* mode (AFB_request *request) { /* AFB_SESSION_CHECK */
186 radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
187 const char *value = getQueryValue (request, "value");
188 json_object *jresp = json_object_new_object();
190 /* no "?value=" parameter : return current state */
191 if (!value || !ctx->radio) {
193 json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
194 : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
197 /* "?value=" parameter is "1" or "AM" */
198 else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
200 _radio_set_mode (ctx->idx, ctx->mode);
201 json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
204 /* "?value=" parameter is "0" or "FM" */
205 else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
207 _radio_set_mode (ctx->idx, ctx->mode);
208 json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
214 STATIC json_object* freq (AFB_request *request) { /* AFB_SESSION_CHECK */
216 radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
217 const char *value = getQueryValue (request, "value");
218 json_object *jresp = json_object_new_object();
222 /* no "?value=" parameter : return current state */
223 if (!value || !ctx->radio) {
224 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
225 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
228 /* "?value=" parameter, set frequency */
230 freq = strtod (value, NULL);
231 _radio_set_freq (ctx->idx, freq);
232 ctx->freq = (float)freq;
234 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
235 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
241 STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
243 radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
244 const char *value = getQueryValue (request, "value");
245 json_object *jresp = json_object_new_object();
248 /* no "?value=" parameter : return current state */
249 if (!value || !ctx->radio) {
251 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
252 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
255 /* "?value=" parameter is "1" or "true" */
256 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
258 _radio_set_mute (ctx->idx, ctx->mute);
259 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
262 /* "?value=" parameter is "0" or "false" */
263 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
265 _radio_set_mute (ctx->idx, ctx->mute);
266 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
272 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
274 radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
275 const char *value = getQueryValue (request, "value");
276 json_object *jresp = json_object_new_object();
278 /* no "?value=" parameter : return current state */
279 if (!value || !ctx->radio) {
281 json_object_object_add (jresp, "play", json_object_new_string ("on"))
282 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
285 /* "?value=" parameter is "1" or "true" */
286 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
289 _radio_play (ctx->idx);
290 json_object_object_add (jresp, "play", json_object_new_string ("on"));
293 /* "?value=" parameter is "0" or "false" */
294 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
297 _radio_stop (ctx->idx);
298 json_object_object_add (jresp, "play", json_object_new_string ("off"));
304 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
305 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
309 STATIC AFB_restapi pluginApis[]= {
310 {"init" , AFB_SESSION_CHECK, (AFB_apiCB)init , "Radio API - init"},
311 {"power" , AFB_SESSION_CHECK, (AFB_apiCB)power , "Radio API - power"},
312 {"mode" , AFB_SESSION_CHECK, (AFB_apiCB)mode , "Radio API - mode"},
313 {"freq" , AFB_SESSION_CHECK, (AFB_apiCB)freq , "Radio API - freq"},
314 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Radio API - mute"},
315 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Radio API - play"},
316 {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Radio API - ping"},
320 PUBLIC AFB_plugin* pluginRegister () {
321 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
322 plugin->type = AFB_PLUGIN_JSON;
323 plugin->info = "Application Framework Binder - Radio plugin";
324 plugin->prefix = "radio";
325 plugin->apis = pluginApis;
327 plugin->handle = initRadioPlugin();
328 plugin->freeCtxCB = (AFB_freeCtxCB)freeRadio;