2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Manuel Bachmann"
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 #include "radio-api.h"
22 #include "radio-rtlsdr.h"
24 #include "afb-plugin.h"
25 #include "afb-req-itf.h"
27 /* ********************************************************
29 FULUP integration proposal with client session context
31 ******************************************************** */
33 /* ------ LOCAL HELPER FUNCTIONS --------- */
35 static pluginHandleT *the_radio = NULL;
37 /* detect new radio devices */
38 STATIC void updateRadioDevList(pluginHandleT *handle) {
42 // loop on existing radio if any
43 for (idx = 0; idx < _radio_dev_count(); idx++) {
44 if (idx == MAX_RADIO) break;
45 handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set used to FALSE */
46 handle->radios[idx]->name = (char *) _radio_dev_name(idx);
48 handle->devCount = _radio_dev_count();
51 /* global plugin context creation ; at loading time [radio devices might not be visible] */
52 STATIC pluginHandleT* initRadioPlugin() {
54 pluginHandleT *handle;
56 handle = calloc (1, sizeof(pluginHandleT));
57 updateRadioDevList (handle);
62 /* private client context creation ; default values */
63 STATIC radioCtxHandleT* initRadioCtx () {
67 ctx = malloc (sizeof(radioCtxHandleT));
78 /* reserve a radio device for requesting client, power it on */
79 STATIC AFB_error reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
82 /* loop on all devices, find an unused one */
83 for (idx = 0; idx < _radio_dev_count(); idx++) {
84 if (idx == MAX_RADIO) break;
85 if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */
90 /* try to power it on, passing client context info such as frequency... */
92 /* TODO : try to re-iterate from the next ones if it failed ! */
94 /* globally mark it as reserved */
95 handle->radios[idx]->used = TRUE;
97 /* store relevant info to client context (direct pointer, index) */
98 ctx->radio = handle->radios[idx];
104 /* free a radio device from requesting client, power it off */
105 STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
107 /* stop playing if it was doing this (blocks otherwise) */
108 if (ctx->is_playing) {
110 _radio_stop (ctx->idx);
114 _radio_off (ctx->idx);
116 /* globally mark it as free */
117 handle->radios[ctx->idx]->used = FALSE;
119 /* clean client context */
126 /* called when client session dies [e.g. client quits for more than 15mns] */
127 STATIC void freeRadio (void *context) {
129 releaseRadio (the_radio, context);
134 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
136 STATIC void init (struct afb_req request) { /* AFB_SESSION_CHECK */
140 /* create a private client context */
141 if (!request.context)
142 request.context = initRadioCtx();
144 jresp = json_object_new_object();
145 json_object_object_add(jresp, "info", json_object_new_string ("Radio initialized"));
146 afb_req_success (request, jresp, "Radio - Initialized");
149 STATIC void power (struct afb_req request) { /* AFB_SESSION_CHECK */
151 pluginHandleT *handle = the_radio;
152 radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
153 const char *value = afb_req_argument (request, "value");
156 /* no "?value=" parameter : return current state */
158 jresp = json_object_new_object();
160 json_object_object_add (jresp, "power", json_object_new_string ("on"))
161 : json_object_object_add (jresp, "power", json_object_new_string ("off"));
164 /* "?value=" parameter is "1" or "true" */
165 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
167 if (reserveRadio (handle, ctx) == AFB_FAIL) {
168 //request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
169 afb_req_fail (request, "failed", "No more radio devices available");
173 jresp = json_object_new_object();
174 json_object_object_add (jresp, "power", json_object_new_string ("on"));
177 /* "?value=" parameter is "0" or "false" */
178 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
180 if (releaseRadio (handle, ctx) == AFB_FAIL) {
181 //request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
182 afb_req_fail (request, "failed", "Unable to release radio device");
186 jresp = json_object_new_object();
187 json_object_object_add (jresp, "power", json_object_new_string ("off"));
192 afb_req_success (request, jresp, "Radio - Power set");
195 STATIC void mode (struct afb_req request) { /* AFB_SESSION_CHECK */
197 radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
198 const char *value = afb_req_argument (request, "value");
199 json_object *jresp = json_object_new_object();
201 /* no "?value=" parameter : return current state */
202 if (!value || !ctx->radio) {
204 json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
205 : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
208 /* "?value=" parameter is "1" or "AM" */
209 else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
211 _radio_set_mode (ctx->idx, ctx->mode);
212 json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
215 /* "?value=" parameter is "0" or "FM" */
216 else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
218 _radio_set_mode (ctx->idx, ctx->mode);
219 json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
222 afb_req_success (request, jresp, "Radio - Mode set");
225 STATIC void freq (struct afb_req request) { /* AFB_SESSION_CHECK */
227 radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
228 const char *value = afb_req_argument (request, "value");
229 json_object *jresp = json_object_new_object();
233 /* no "?value=" parameter : return current state */
234 if (!value || !ctx->radio) {
235 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
236 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
239 /* "?value=" parameter, set frequency */
241 freq = strtod (value, NULL);
242 _radio_set_freq (ctx->idx, freq);
243 ctx->freq = (float)freq;
245 snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
246 json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
249 afb_req_success (request, jresp, "Radio - Frequency Set");
252 STATIC void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
254 radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
255 const char *value = afb_req_argument (request, "value");
256 json_object *jresp = json_object_new_object();
259 /* no "?value=" parameter : return current state */
260 if (!value || !ctx->radio) {
262 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
263 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
266 /* "?value=" parameter is "1" or "true" */
267 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
269 _radio_set_mute (ctx->idx, ctx->mute);
270 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
273 /* "?value=" parameter is "0" or "false" */
274 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
276 _radio_set_mute (ctx->idx, ctx->mute);
277 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
280 afb_req_success (request, jresp, "Radio - Mute set");
283 STATIC void play (struct afb_req request) { /* AFB_SESSION_CHECK */
285 radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
286 const char *value = afb_req_argument (request, "value");
287 json_object *jresp = json_object_new_object();
289 /* no "?value=" parameter : return current state */
290 if (!value || !ctx->radio) {
292 json_object_object_add (jresp, "play", json_object_new_string ("on"))
293 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
296 /* "?value=" parameter is "1" or "true" */
297 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
300 _radio_play (ctx->idx);
301 json_object_object_add (jresp, "play", json_object_new_string ("on"));
304 /* "?value=" parameter is "0" or "false" */
305 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
308 _radio_stop (ctx->idx);
309 json_object_object_add (jresp, "play", json_object_new_string ("off"));
312 afb_req_success (request, jresp, "Radio - Play succeeded");
315 STATIC void ping (struct afb_req request) { /* AFB_SESSION_NONE */
316 afb_req_success (request, NULL, "Radio - Ping succeeded");
320 STATIC const struct AFB_restapi pluginApis[]= {
321 {"init" , AFB_SESSION_CHECK, init , "Radio API - init"},
322 {"power" , AFB_SESSION_CHECK, power , "Radio API - power"},
323 {"mode" , AFB_SESSION_CHECK, mode , "Radio API - mode"},
324 {"freq" , AFB_SESSION_CHECK, freq , "Radio API - freq"},
325 {"mute" , AFB_SESSION_CHECK, mute , "Radio API - mute"},
326 {"play" , AFB_SESSION_CHECK, play , "Radio API - play"},
327 {"ping" , AFB_SESSION_NONE, ping , "Radio API - ping"},
331 STATIC const struct AFB_plugin plug_desc = {
332 .type = AFB_PLUGIN_JSON,
333 .info = "Application Framework Binder - Radio plugin",
336 //plugin->freeCtxCB = (AFB_freeCtxCB)freeRadio;
337 //the_radio = initRadioPlugin();