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/>.
22 #include "radio-api.h"
23 #include "radio-rtlsdr.h"
25 #include "afb-plugin.h"
26 #include "afb-req-itf.h"
28 /* ********************************************************
30 FULUP integration proposal with client session context
32 ******************************************************** */
34 /* ------ LOCAL HELPER FUNCTIONS --------- */
36 static pluginHandleT *the_radio = NULL;
38 /* detect new radio devices */
39 STATIC void updateRadioDevList(pluginHandleT *handle) {
43 // loop on existing radio if any
44 for (idx = 0; idx < _radio_dev_count(); idx++) {
45 if (idx == MAX_RADIO) break;
46 handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set used to FALSE */
47 handle->radios[idx]->name = (char *) _radio_dev_name(idx);
49 handle->devCount = _radio_dev_count();
52 /* global plugin context creation ; at loading time [radio devices might not be visible] */
53 STATIC pluginHandleT* initRadioPlugin() {
55 pluginHandleT *handle;
57 handle = calloc (1, sizeof(pluginHandleT));
58 updateRadioDevList (handle);
63 /* private client context creation ; default values */
64 STATIC radioCtxHandleT* initRadioCtx () {
68 ctx = malloc (sizeof(radioCtxHandleT));
79 /* reserve a radio device for requesting client, power it on */
80 STATIC AFB_error reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
83 /* loop on all devices, find an unused one */
84 for (idx = 0; idx < _radio_dev_count(); idx++) {
85 if (idx == MAX_RADIO) break;
86 if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */
91 /* try to power it on, passing client context info such as frequency... */
93 /* TODO : try to re-iterate from the next ones if it failed ! */
95 /* globally mark it as reserved */
96 handle->radios[idx]->used = TRUE;
98 /* store relevant info to client context (direct pointer, index) */
99 ctx->radio = handle->radios[idx];
105 /* free a radio device from requesting client, power it off */
106 STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
108 /* stop playing if it was doing this (blocks otherwise) */
109 if (ctx->is_playing) {
111 _radio_stop (ctx->idx);
115 _radio_off (ctx->idx);
117 /* globally mark it as free */
118 handle->radios[ctx->idx]->used = FALSE;
120 /* clean client context */
127 /* called when client session dies [e.g. client quits for more than 15mns] */
128 STATIC void freeRadio (void *context) {
130 releaseRadio (the_radio, context);
135 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
137 STATIC void init (struct afb_req request) { /* AFB_SESSION_CHECK */
141 /* create a private client context */
142 if (!request.context)
143 request.context = initRadioCtx();
145 jresp = json_object_new_object();
146 json_object_object_add(jresp, "info", json_object_new_string ("Radio initialized"));
147 afb_req_success (request, jresp, "Radio - Initialized");
150 STATIC void power (struct afb_req request) { /* AFB_SESSION_CHECK */
152 pluginHandleT *handle = the_radio;
153 radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
154 const char *value = afb_req_argument (request, "value");
157 /* no "?value=" parameter : return current state */
159 jresp = json_object_new_object();
161 json_object_object_add (jresp, "power", json_object_new_string ("on"))
162 : json_object_object_add (jresp, "power", json_object_new_string ("off"));
165 /* "?value=" parameter is "1" or "true" */
166 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
168 if (reserveRadio (handle, ctx) == AFB_FAIL) {
169 //request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
170 afb_req_fail (request, "failed", "No more radio devices available");
174 jresp = json_object_new_object();
175 json_object_object_add (jresp, "power", json_object_new_string ("on"));
178 /* "?value=" parameter is "0" or "false" */
179 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
181 if (releaseRadio (handle, ctx) == AFB_FAIL) {
182 //request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
183 afb_req_fail (request, "failed", "Unable to release radio device");
187 jresp = json_object_new_object();
188 json_object_object_add (jresp, "power", json_object_new_string ("off"));
193 afb_req_success (request, jresp, "Radio - Power set");
196 STATIC void mode (struct afb_req request) { /* AFB_SESSION_CHECK */
198 radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
199 const char *value = afb_req_argument (request, "value");
200 json_object *jresp = json_object_new_object();
202 /* no "?value=" parameter : return current state */
203 if (!value || !ctx->radio) {
205 json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
206 : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
209 /* "?value=" parameter is "1" or "AM" */
210 else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
212 _radio_set_mode (ctx->idx, ctx->mode);
213 json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
216 /* "?value=" parameter is "0" or "FM" */
217 else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
219 _radio_set_mode (ctx->idx, ctx->mode);
220 json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
223 afb_req_success (request, jresp, "Radio - Mode set");
226 STATIC void freq (struct afb_req request) { /* AFB_SESSION_CHECK */
228 radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
229 const char *value = afb_req_argument (request, "value");
230 json_object *jresp = json_object_new_object();
234 /* no "?value=" parameter : return current state */
235 if (!value || !ctx->radio) {
236 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
237 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
240 /* "?value=" parameter, set frequency */
242 freq = strtod (value, NULL);
243 _radio_set_freq (ctx->idx, freq);
244 ctx->freq = (float)freq;
246 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
247 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
250 afb_req_success (request, jresp, "Radio - Frequency Set");
253 STATIC void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
255 radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
256 const char *value = afb_req_argument (request, "value");
257 json_object *jresp = json_object_new_object();
260 /* no "?value=" parameter : return current state */
261 if (!value || !ctx->radio) {
263 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
264 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
267 /* "?value=" parameter is "1" or "true" */
268 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
270 _radio_set_mute (ctx->idx, ctx->mute);
271 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
274 /* "?value=" parameter is "0" or "false" */
275 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
277 _radio_set_mute (ctx->idx, ctx->mute);
278 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
281 afb_req_success (request, jresp, "Radio - Mute set");
284 STATIC void play (struct afb_req request) { /* AFB_SESSION_CHECK */
286 radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
287 const char *value = afb_req_argument (request, "value");
288 json_object *jresp = json_object_new_object();
290 /* no "?value=" parameter : return current state */
291 if (!value || !ctx->radio) {
293 json_object_object_add (jresp, "play", json_object_new_string ("on"))
294 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
297 /* "?value=" parameter is "1" or "true" */
298 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
301 _radio_play (ctx->idx);
302 json_object_object_add (jresp, "play", json_object_new_string ("on"));
305 /* "?value=" parameter is "0" or "false" */
306 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
309 _radio_stop (ctx->idx);
310 json_object_object_add (jresp, "play", json_object_new_string ("off"));
313 afb_req_success (request, jresp, "Radio - Play succeeded");
316 STATIC void ping (struct afb_req request) { /* AFB_SESSION_NONE */
317 afb_req_success (request, NULL, "Radio - Ping succeeded");
321 STATIC const struct AFB_restapi pluginApis[]= {
322 {"init" , AFB_SESSION_CHECK, init , "Radio API - init"},
323 {"power" , AFB_SESSION_CHECK, power , "Radio API - power"},
324 {"mode" , AFB_SESSION_CHECK, mode , "Radio API - mode"},
325 {"freq" , AFB_SESSION_CHECK, freq , "Radio API - freq"},
326 {"mute" , AFB_SESSION_CHECK, mute , "Radio API - mute"},
327 {"play" , AFB_SESSION_CHECK, play , "Radio API - play"},
328 {"ping" , AFB_SESSION_NONE, ping , "Radio API - ping"},
332 STATIC const struct AFB_plugin plug_desc = {
333 .type = AFB_PLUGIN_JSON,
334 .info = "Application Framework Binder - Radio plugin",
337 //plugin->freeCtxCB = (AFB_freeCtxCB)freeRadio;
338 //the_radio = initRadioPlugin();