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