b64d083086868f136aee93f82dc3ae63d24c1ea8
[src/app-framework-binder.git] / plugins / radio / radio-api.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Manuel Bachmann"
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #define _GNU_SOURCE
20 #include <strings.h>
21
22 #include "radio-api.h"
23 #include "radio-rtlsdr.h"
24
25 #include "afb-plugin.h"
26 #include "afb-req-itf.h"
27
28 /* ********************************************************
29
30    FULUP integration proposal with client session context
31
32    ******************************************************** */
33
34 /* ------ LOCAL HELPER FUNCTIONS --------- */
35
36 static pluginHandleT *the_radio = NULL;
37
38 /* detect new radio devices */
39 STATIC void updateRadioDevList(pluginHandleT *handle) {
40
41   int idx;  
42
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); 
48   }
49   handle->devCount = _radio_dev_count();
50 }
51
52 /* global plugin context creation ; at loading time [radio devices might not be visible] */
53 STATIC pluginHandleT* initRadioPlugin() {
54
55   pluginHandleT *handle;
56
57   handle = calloc (1, sizeof(pluginHandleT));
58   updateRadioDevList (handle);
59
60   return handle;
61 }
62
63 /* private client context creation ; default values */
64 STATIC radioCtxHandleT* initRadioCtx () {
65
66     radioCtxHandleT *ctx;
67
68     ctx = malloc (sizeof(radioCtxHandleT));
69     ctx->radio = NULL;
70     //ctx->idx = -1;
71     ctx->mode = FM;
72     ctx->freq = 100.0;
73     ctx->mute = 0;
74     ctx->is_playing = 0;
75
76     return ctx;
77 }
78
79 /* reserve a radio device for requesting client, power it on */
80 STATIC AFB_error reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
81     unsigned int idx;
82
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 */
87     }
88     return AFB_FAIL;
89
90    found_radio:
91     /* try to power it on, passing client context info such as frequency... */
92     _radio_on (idx, ctx);
93     /* TODO : try to re-iterate from the next ones if it failed ! */
94
95     /* globally mark it as reserved */
96     handle->radios[idx]->used = TRUE;
97
98     /* store relevant info to client context (direct pointer, index) */
99     ctx->radio = handle->radios[idx];
100     ctx->idx = idx;
101
102     return AFB_SUCCESS;
103 }
104
105 /* free a radio device from requesting client, power it off */
106 STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
107
108     /* stop playing if it was doing this (blocks otherwise) */
109     if (ctx->is_playing) {
110         ctx->is_playing = 0;
111         _radio_stop (ctx->idx);
112     }
113
114     /* power it off */
115     _radio_off (ctx->idx);
116
117     /* globally mark it as free */
118     handle->radios[ctx->idx]->used = FALSE;
119
120     /* clean client context */
121     ctx->radio = NULL;
122     //ctx->idx = -1;
123
124     return AFB_SUCCESS;
125 }
126
127 /* called when client session dies [e.g. client quits for more than 15mns] */
128 STATIC void freeRadio (void *context) {
129
130     releaseRadio (the_radio, context);
131     free (context);
132 }
133
134
135 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
136
137 STATIC void init (struct afb_req request) {        /* AFB_SESSION_CHECK */
138
139     json_object *jresp;
140
141     /* create a private client context */
142     if (!request.context)
143         request.context = initRadioCtx();
144
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");
148 }
149
150 STATIC void power (struct afb_req request) {       /* AFB_SESSION_CHECK */
151
152     pluginHandleT *handle = the_radio;
153     radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
154     const char *value = afb_req_argument (request, "value");
155     json_object *jresp;
156
157     /* no "?value=" parameter : return current state */
158     if (!value) {
159         jresp = json_object_new_object();
160         ctx->radio ?
161             json_object_object_add (jresp, "power", json_object_new_string ("on"))
162           : json_object_object_add (jresp, "power", json_object_new_string ("off"));
163     }
164
165     /* "?value=" parameter is "1" or "true" */
166     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
167         if (!ctx->radio) {
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");
171                 return;
172             }
173         }
174         jresp = json_object_new_object();
175         json_object_object_add (jresp, "power", json_object_new_string ("on"));
176     }
177
178     /* "?value=" parameter is "0" or "false" */
179     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
180         if (ctx->radio) {
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");
184                 return;
185             }
186         }
187         jresp = json_object_new_object();
188         json_object_object_add (jresp, "power", json_object_new_string ("off"));
189     }
190     else
191         jresp = NULL;
192
193     afb_req_success (request, jresp, "Radio - Power set");
194 }
195
196 STATIC void mode (struct afb_req request) {        /* AFB_SESSION_CHECK */
197
198     radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
199     const char *value = afb_req_argument (request, "value");
200     json_object *jresp = json_object_new_object();
201
202     /* no "?value=" parameter : return current state */
203     if (!value || !ctx->radio) {
204         ctx->mode ?
205             json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
206           : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
207     }
208
209     /* "?value=" parameter is "1" or "AM" */
210     else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
211         ctx->mode = AM;
212         _radio_set_mode (ctx->idx, ctx->mode);
213         json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
214     }
215
216     /* "?value=" parameter is "0" or "FM" */
217     else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
218         ctx->mode = FM;
219         _radio_set_mode (ctx->idx, ctx->mode);
220         json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
221     }
222
223     afb_req_success (request, jresp, "Radio - Mode set");
224 }
225
226 STATIC void freq (struct afb_req request) {        /* AFB_SESSION_CHECK */
227
228     radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
229     const char *value = afb_req_argument (request, "value");
230     json_object *jresp = json_object_new_object();
231     double freq;
232     char freq_str[256];
233
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));
238     }
239
240     /* "?value=" parameter, set frequency */
241     else {
242         freq = strtod (value, NULL);
243         _radio_set_freq (ctx->idx, freq);
244         ctx->freq = (float)freq;
245
246         snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
247         json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
248     }
249
250     afb_req_success (request, jresp, "Radio - Frequency Set");
251 }
252
253 STATIC void mute (struct afb_req request) {        /* AFB_SESSION_CHECK */
254
255     radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
256     const char *value = afb_req_argument (request, "value");
257     json_object *jresp = json_object_new_object();
258     //char *mute_str;
259
260     /* no "?value=" parameter : return current state */
261     if (!value || !ctx->radio) {
262         ctx->mute ?
263             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
264           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
265     }
266
267     /* "?value=" parameter is "1" or "true" */
268     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
269         ctx->mute = 1;
270         _radio_set_mute (ctx->idx, ctx->mute);
271         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
272     }
273
274     /* "?value=" parameter is "0" or "false" */
275     else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
276         ctx->mute = 0;
277         _radio_set_mute (ctx->idx, ctx->mute);
278         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
279     }
280
281     afb_req_success (request, jresp, "Radio - Mute set"); 
282 }
283
284 STATIC void play (struct afb_req request) {        /* AFB_SESSION_CHECK */
285
286     radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
287     const char *value = afb_req_argument (request, "value");
288     json_object *jresp = json_object_new_object();
289     
290     /* no "?value=" parameter : return current state */
291     if (!value || !ctx->radio) {
292         ctx->is_playing ?
293             json_object_object_add (jresp, "play", json_object_new_string ("on"))
294           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
295     }
296
297     /* "?value=" parameter is "1" or "true" */
298     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
299         /* radio playback */
300         ctx->is_playing = 1;
301         _radio_play (ctx->idx);
302         json_object_object_add (jresp, "play", json_object_new_string ("on"));
303     }
304
305     /* "?value=" parameter is "0" or "false" */
306     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
307         /* radio stop */
308         ctx->is_playing = 0;
309         _radio_stop (ctx->idx);
310         json_object_object_add (jresp, "play", json_object_new_string ("off"));
311     }
312
313     afb_req_success (request, jresp, "Radio - Play succeeded");
314 }
315
316 STATIC void ping (struct afb_req request) {         /* AFB_SESSION_NONE */
317     afb_req_success (request, NULL, "Radio - Ping succeeded");
318 }
319
320
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"},
329   {NULL}
330 };
331
332 STATIC const struct AFB_plugin plug_desc = {
333     .type  = AFB_PLUGIN_JSON,
334     .info  = "Application Framework Binder - Radio plugin",
335     .prefix  = "radio",
336     .apis  = pluginApis,
337     //plugin->freeCtxCB = (AFB_freeCtxCB)freeRadio;
338     //the_radio = initRadioPlugin();
339 };