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 json_object* freeRadio (AFB_clientCtx *client) {
122 releaseRadio (client->plugin->handle, client->ctx);
125 return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
129 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
131 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
133 radioCtxHandleT *ctx;
136 /* create a private client context */
137 ctx = initRadioCtx();
138 request->client->ctx = (radioCtxHandleT*)ctx;
140 jresp = json_object_new_object();
141 json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
145 STATIC json_object* power (AFB_request *request) { /* AFB_SESSION_CHECK */
147 pluginHandleT *handle = request->client->plugin->handle;
148 radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
149 const char *value = getQueryValue (request, "value");
152 /* no "?value=" parameter : return current state */
154 jresp = json_object_new_object();
156 json_object_object_add (jresp, "power", json_object_new_string ("on"))
157 : json_object_object_add (jresp, "power", json_object_new_string ("off"));
160 /* "?value=" parameter is "1" or "true" */
161 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
163 if (reserveRadio (handle, ctx) == AFB_FAIL) {
164 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
165 return (jsonNewMessage (AFB_FAIL, "No more radio devices available"));
168 jresp = json_object_new_object();
169 json_object_object_add (jresp, "power", json_object_new_string ("on"));
172 /* "?value=" parameter is "0" or "false" */
173 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
175 if (releaseRadio (handle, ctx) == AFB_FAIL) {
176 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
177 return (jsonNewMessage (AFB_FAIL, "Unable to release radio device"));
180 jresp = json_object_new_object();
181 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->client->ctx;
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->client->ctx;
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->client->ctx;
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->client->ctx;
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* refresh (AFB_request *request) { /* AFB_SESSION_RENEW */
308 json_object *jresp = json_object_new_object();
309 json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
313 STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
314 return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
318 STATIC AFB_restapi pluginApis[]= {
319 {"init" , AFB_SESSION_CREATE, (AFB_apiCB)init , "Radio API - init"},
320 {"power" , AFB_SESSION_CHECK, (AFB_apiCB)power , "Radio API - power"},
321 {"mode" , AFB_SESSION_CHECK, (AFB_apiCB)mode , "Radio API - mode"},
322 {"freq" , AFB_SESSION_CHECK, (AFB_apiCB)freq , "Radio API - freq"},
323 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Radio API - mute"},
324 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Radio API - play"},
325 {"refresh", AFB_SESSION_RENEW, (AFB_apiCB)refresh , "Radio API - refresh"},
326 {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Radio API - ping"},
330 PUBLIC AFB_plugin* radioRegister () {
331 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
332 plugin->type = AFB_PLUGIN_JSON;
333 plugin->info = "Application Framework Binder - Radio plugin";
334 plugin->prefix = "radio";
335 plugin->apis = pluginApis;
337 plugin->handle = initRadioPlugin();
338 plugin->freeCtxCB = freeRadio;