2 * Copyright (C) 2015 "IoT.bzh"
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "radio-api.h"
19 #include "radio-rtlsdr.h"
21 /* ********************************************************
23 FULUP integration proposal with client session context
25 ******************************************************** */
27 /* ------ LOCAL HELPER FUNCTIONS --------- */
29 /* detect new radio devices */
30 STATIC void updateRadioDevList(pluginHandleT *handle) {
34 // loop on existing radio if any
35 for (idx = 0; idx < _radio_dev_count(); idx++) {
36 if (idx == MAX_RADIO) break;
37 handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set used to FALSE */
38 handle->radios[idx]->name = (char *) _radio_dev_name(idx);
40 handle->devCount = _radio_dev_count();
43 /* global plugin context creation ; at loading time [radio devices might not be visible] */
44 STATIC pluginHandleT* initRadioPlugin() {
46 pluginHandleT *handle;
48 handle = calloc (1, sizeof(pluginHandleT));
49 updateRadioDevList (handle);
54 /* private client context creation ; default values */
55 STATIC radioCtxHandleT* initRadioCtx () {
59 ctx = malloc (sizeof(radioCtxHandleT));
70 /* reserve a radio device for requesting client, power it on */
71 STATIC AFB_error reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
74 /* loop on all devices, find an unused one */
75 for (idx = 0; idx < _radio_dev_count(); idx++) {
76 if (idx == MAX_RADIO) break;
77 if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */
82 /* try to power it on, passing client context info such as frequency... */
84 /* TODO : try to re-iterate from the next ones if it failed ! */
86 /* globally mark it as reserved */
87 handle->radios[idx]->used = TRUE;
89 /* store relevant info to client context (direct pointer, index) */
90 ctx->radio = handle->radios[idx];
96 /* free a radio device from requesting client, power it off */
97 STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
100 _radio_off (ctx->idx);
102 /* globally mark it as free */
103 handle->radios[ctx->idx]->used = FALSE;
105 /* clean client context */
112 /* called when client session dies [e.g. client quits for more than 15mns] */
113 STATIC json_object* freeRadio (AFB_clientCtx *client) {
115 releaseRadio (client->plugin->handle, client->ctx);
118 return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
122 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
124 STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
126 radioCtxHandleT *ctx;
129 /* create a private client context */
130 ctx = initRadioCtx();
131 request->client->ctx = (radioCtxHandleT*)ctx;
133 jresp = json_object_new_object();
134 json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
137 STATIC json_object* power (AFB_request *request) { /* AFB_SESSION_CHECK */
139 pluginHandleT *handle = request->client->plugin->handle;
140 radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
141 const char *value = getQueryValue (request, "value");
144 /* no "?value=" parameter : return current state */
146 jresp = json_object_new_object();
148 json_object_object_add (jresp, "power", json_object_new_string ("on"))
149 : json_object_object_add (jresp, "power", json_object_new_string ("off"));
152 /* "?value=" parameter is "1" or "on" */
153 else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
155 if (reserveRadio (handle, ctx) == AFB_FAIL) {
156 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
157 return (jsonNewMessage (AFB_FAIL, "No more radio devices available"));
160 jresp = json_object_new_object();
161 json_object_object_add (jresp, "power", json_object_new_string ("on"));
164 /* "?value=" parameter is "0" or "off" */
165 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
167 if (releaseRadio (handle, ctx) == AFB_FAIL) {
168 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
169 return (jsonNewMessage (AFB_FAIL, "Unable to release radio device"));
172 jresp = json_object_new_object();
173 json_object_object_add (jresp, "power", json_object_new_string ("off"));
179 STATIC json_object* mode (AFB_request *request) { /* AFB_SESSION_CHECK */
181 radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
182 const char *value = getQueryValue (request, "value");
185 /* no "?value=" parameter : return current state */
187 jresp = json_object_new_object();
189 json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
190 : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
193 /* "?value=" parameter is "1" or "on" */
194 else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
196 _radio_set_mode (ctx->idx, ctx->mode);
198 jresp = json_object_new_object();
199 json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
202 /* "?value=" parameter is "0" or "off" */
203 else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
205 _radio_set_mode (ctx->idx, ctx->mode);
207 jresp = json_object_new_object();
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->client->ctx;
217 const char *value = getQueryValue (request, "value");
218 json_object *jresp = json_object_new_object();
221 /* no "?value=" parameter : return current state */
223 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
224 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
227 /* "?value=" parameter, set frequency */
229 ctx->freq = strtof (value, NULL);
230 _radio_set_freq (ctx->idx, ctx->freq);
232 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
233 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
239 STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
241 radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
242 const char *value = getQueryValue (request, "value");
246 /* no "?value=" parameter : return current state */
249 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
250 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
253 /* "?value=" parameter is "1" or "on" */
254 else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
256 _radio_set_mute (ctx->idx, ctx->mute);
258 jresp = json_object_new_object();
259 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
262 /* "?value=" parameter is "0" or "off" */
263 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
265 _radio_set_mute (ctx->idx, ctx->mute);
267 jresp = json_object_new_object();
268 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
274 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
276 radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
277 const char *value = getQueryValue (request, "value");
278 json_object *jresp = json_object_new_object();
280 /* no "?value=" parameter : return current state */
283 json_object_object_add (jresp, "play", json_object_new_string ("on"))
284 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
287 /* "?value=" parameter is "1" or "on" */
288 else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
291 _radio_play (ctx->idx);
292 json_object_object_add (jresp, "play", json_object_new_string ("on"));
295 /* "?value=" parameter is "0" or "off" */
296 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
299 _radio_stop (ctx->idx);
300 json_object_object_add (jresp, "play-on", json_object_new_string ("off"));
306 STATIC json_object* status (AFB_request *request) {
311 STATIC AFB_restapi pluginApis[]= {
312 {"init" , AFB_SESSION_CREATE, (AFB_apiCB)init , "Radio API - init"},
313 {"power" , AFB_SESSION_CHECK, (AFB_apiCB)power , "Radio API - power"},
314 {"mode" , AFB_SESSION_CHECK, (AFB_apiCB)mode , "Radio API - mode"},
315 {"freq" , AFB_SESSION_CHECK, (AFB_apiCB)freq , "Radio API - freq"},
316 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Radio API - mute"},
317 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Radio API - play"},
318 {"status" , AFB_SESSION_RENEW, (AFB_apiCB)status , "Radio API - status"},
322 PUBLIC AFB_plugin* radioRegister () {
323 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
324 plugin->type = AFB_PLUGIN_JSON;
325 plugin->info = "Application Framework Binder - Radio plugin";
326 plugin->prefix = "radio";
327 plugin->apis = pluginApis;
329 plugin->handle = initRadioPlugin();
330 plugin->freeCtxCB = freeRadio;