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* power (AFB_request *request) { /* AFB_SESSION_CREATE */
126 pluginHandleT *handle = request->client->plugin->handle;
127 radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
128 const char *value = getQueryValue (request, "value");
131 /* create a private client context if needed */
132 if (!ctx) ctx = initRadioCtx();
134 /* no "?value=" parameter : return current state */
136 jresp = json_object_new_object();
138 json_object_object_add (jresp, "power", json_object_new_string ("on"))
139 : json_object_object_add (jresp, "power", json_object_new_string ("off"));
142 /* "?value=" parameter is "1" or "on" */
143 else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
145 if (reserveRadio (handle, ctx) == AFB_FAIL) {
146 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
147 return (jsonNewMessage (AFB_FAIL, "No more radio devices available"));
150 jresp = json_object_new_object();
151 json_object_object_add (jresp, "power", json_object_new_string ("on"));
154 /* "?value=" parameter is "0" or "off" */
155 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
157 if (releaseRadio (handle, ctx) == AFB_FAIL) {
158 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
159 return (jsonNewMessage (AFB_FAIL, "Unable to release radio device"));
162 jresp = json_object_new_object();
163 json_object_object_add (jresp, "power", json_object_new_string ("off"));
169 STATIC json_object* mode (AFB_request *request) { /* AFB_SESSION_CHECK */
171 radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
172 const char *value = getQueryValue (request, "value");
175 /* no "?value=" parameter : return current state */
177 jresp = json_object_new_object();
179 json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
180 : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
183 /* "?value=" parameter is "1" or "on" */
184 else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
186 _radio_set_mode (ctx->idx, ctx->mode);
188 jresp = json_object_new_object();
189 json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
192 /* "?value=" parameter is "0" or "off" */
193 else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
195 _radio_set_mode (ctx->idx, ctx->mode);
197 jresp = json_object_new_object();
198 json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
204 STATIC json_object* freq (AFB_request *request) { /* AFB_SESSION_CHECK */
206 radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
207 const char *value = getQueryValue (request, "value");
208 json_object *jresp = json_object_new_object();
211 /* no "?value=" parameter : return current state */
213 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
214 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
217 /* "?value=" parameter, set frequency */
219 ctx->freq = strtof (value, NULL);
220 _radio_set_freq (ctx->idx, ctx->freq);
222 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
223 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
229 STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
231 radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
232 const char *value = getQueryValue (request, "value");
236 /* no "?value=" parameter : return current state */
239 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
240 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
243 /* "?value=" parameter is "1" or "on" */
244 else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
246 _radio_set_mute (ctx->idx, ctx->mute);
248 jresp = json_object_new_object();
249 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
252 /* "?value=" parameter is "0" or "off" */
253 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
255 _radio_set_mute (ctx->idx, ctx->mute);
257 jresp = json_object_new_object();
258 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
264 STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
266 radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
267 const char *value = getQueryValue (request, "value");
270 /* no "?value=" parameter : return current state */
273 json_object_object_add (jresp, "play", json_object_new_string ("on"))
274 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
277 /* "?value=" parameter is "1" or "on" */
278 else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
281 _radio_play (ctx->idx);
283 jresp = json_object_new_object();
284 json_object_object_add (jresp, "play", json_object_new_string ("on"));
287 /* "?value=" parameter is "0" or "off" */
288 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
291 _radio_stop (ctx->idx);
293 jresp = json_object_new_object();
294 json_object_object_add (jresp, "play-on", json_object_new_string ("off"));
300 STATIC json_object* status (AFB_request *request) {
305 STATIC AFB_restapi pluginApis[]= {
306 {"power" , AFB_SESSION_CREATE, (AFB_apiCB)power , "Radio API - power"},
307 {"mode" , AFB_SESSION_CHECK, (AFB_apiCB)mode , "Radio API - mode"},
308 {"freq" , AFB_SESSION_CHECK, (AFB_apiCB)freq , "Radio API - freq"},
309 {"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Radio API - mute"},
310 {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Radio API - play"},
311 {"status" , AFB_SESSION_RENEW, (AFB_apiCB)status , "Radio API - status"},
315 PUBLIC AFB_plugin* radioRegister () {
316 AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
317 plugin->type = AFB_PLUGIN_JSON;
318 plugin->info = "Application Framework Binder - Radio plugin";
319 plugin->prefix = "radio";
320 plugin->apis = pluginApis;
322 plugin->handle = initRadioPlugin();
323 plugin->freeCtxCB = freeRadio;