2 * Copyright (C) 2015, 2016 "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.
20 #include <json-c/json.h>
22 #include "radio-api.h"
23 #include "radio-rtlsdr.h"
25 #include <afb/afb-plugin.h>
26 #include <afb/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 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 void initRadioPlugin() {
55 pluginHandleT *handle = the_radio;
57 handle = calloc (1, sizeof(pluginHandleT));
58 updateRadioDevList (handle);
61 /* private client context creation ; default values */
62 static radioCtxHandleT* initRadioCtx () {
66 ctx = malloc (sizeof(radioCtxHandleT));
77 /* reserve a radio device for requesting client, power it on */
78 unsigned char reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
81 /* loop on all devices, find an unused one */
82 for (idx = 0; idx < _radio_dev_count(); idx++) {
83 if (idx == MAX_RADIO) break;
84 if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */
89 /* try to power it on, passing client context info such as frequency... */
91 /* TODO : try to re-iterate from the next ones if it failed ! */
93 /* globally mark it as reserved */
94 handle->radios[idx]->used = TRUE;
96 /* store relevant info to client context (direct pointer, index) */
97 ctx->radio = handle->radios[idx];
103 /* free a radio device from requesting client, power it off */
104 unsigned char releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
106 /* stop playing if it was doing this (blocks otherwise) */
107 if (ctx->is_playing) {
109 _radio_stop (ctx->idx);
113 _radio_off (ctx->idx);
115 /* globally mark it as free */
116 handle->radios[ctx->idx]->used = FALSE;
118 /* clean client context */
125 /* called when client session dies [e.g. client quits for more than 15mns] */
126 static void freeRadio (void *context) {
128 releaseRadio (the_radio, context);
133 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
135 static void init (struct afb_req request) { /* AFB_SESSION_CHECK */
137 radioCtxHandleT *ctx = (radioCtxHandleT*) afb_req_context_get(request);
140 /* create a private client context */
142 ctx = initRadioCtx();
143 afb_req_context_set (request, ctx, free);
146 jresp = json_object_new_object();
147 json_object_object_add(jresp, "init", json_object_new_string ("success"));
148 afb_req_success (request, jresp, "Radio - Initialized");
151 static void power (struct afb_req request) { /* AFB_SESSION_CHECK */
153 pluginHandleT *handle = the_radio;
154 radioCtxHandleT *ctx = (radioCtxHandleT*) afb_req_context_get(request);
155 const char *value = afb_req_value (request, "value");
158 /* no "?value=" parameter : return current state */
160 jresp = json_object_new_object();
162 json_object_object_add (jresp, "power", json_object_new_string ("on"))
163 : json_object_object_add (jresp, "power", json_object_new_string ("off"));
166 /* "?value=" parameter is "1" or "true" */
167 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
169 if (!reserveRadio (handle, ctx)) {
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)) {
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*) afb_req_context_get(request);
198 const char *value = afb_req_value (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*) afb_req_context_get(request);
228 const char *value = afb_req_value (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*) afb_req_context_get(request);
255 const char *value = afb_req_value (request, "value");
256 json_object *jresp = json_object_new_object();
258 /* no "?value=" parameter : return current state */
259 if (!value || !ctx->radio) {
261 json_object_object_add (jresp, "mute", json_object_new_string ("on"))
262 : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
265 /* "?value=" parameter is "1" or "true" */
266 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
268 _radio_set_mute (ctx->idx, ctx->mute);
269 json_object_object_add (jresp, "mute", json_object_new_string ("on"));
272 /* "?value=" parameter is "0" or "false" */
273 else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
275 _radio_set_mute (ctx->idx, ctx->mute);
276 json_object_object_add (jresp, "mute", json_object_new_string ("off"));
279 afb_req_success (request, jresp, "Radio - Mute set");
282 static void play (struct afb_req request) { /* AFB_SESSION_CHECK */
284 radioCtxHandleT *ctx = (radioCtxHandleT*) afb_req_context_get(request);
285 const char *value = afb_req_value (request, "value");
286 json_object *jresp = json_object_new_object();
288 /* no "?value=" parameter : return current state */
289 if (!value || !ctx->radio) {
291 json_object_object_add (jresp, "play", json_object_new_string ("on"))
292 : json_object_object_add (jresp, "play", json_object_new_string ("off"));
295 /* "?value=" parameter is "1" or "true" */
296 else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
299 _radio_play (ctx->idx);
300 json_object_object_add (jresp, "play", json_object_new_string ("on"));
303 /* "?value=" parameter is "0" or "false" */
304 else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
307 _radio_stop (ctx->idx);
308 json_object_object_add (jresp, "play", json_object_new_string ("off"));
311 afb_req_success (request, jresp, "Radio - Play succeeded");
314 static void ping (struct afb_req request) { /* AFB_SESSION_NONE */
315 afb_req_success (request, NULL, "Radio - Ping succeeded");
319 static const struct AFB_restapi pluginApis[]= {
320 {"init" , AFB_SESSION_CHECK, init , "Radio API - init"},
321 {"power" , AFB_SESSION_CHECK, power , "Radio API - power"},
322 {"mode" , AFB_SESSION_CHECK, mode , "Radio API - mode"},
323 {"freq" , AFB_SESSION_CHECK, freq , "Radio API - freq"},
324 {"mute" , AFB_SESSION_CHECK, mute , "Radio API - mute"},
325 {"play" , AFB_SESSION_CHECK, play , "Radio API - play"},
326 {"ping" , AFB_SESSION_NONE, ping , "Radio API - ping"},
330 static const struct AFB_plugin pluginDesc = {
331 .type = AFB_PLUGIN_JSON,
332 .info = "Application Framework Binder - Radio plugin",
337 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)