Fix plugins loading, cleanup and dispatch Radio API code
[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* power (AFB_request *request) {      /* AFB_SESSION_CREATE */
125     
126     pluginHandleT *handle = request->client->plugin->handle; 
127     radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
128     const char *value = getQueryValue (request, "value");
129     json_object *jresp;
130
131     /* create a private client context if needed */
132     if (!ctx) ctx = initRadioCtx();
133
134     /* no "?value=" parameter : return current state */
135     if (!value) {
136         jresp = json_object_new_object();
137         ctx->radio ?
138             json_object_object_add (jresp, "power", json_object_new_string ("on"))
139           : json_object_object_add (jresp, "power", json_object_new_string ("off"));
140     }
141
142     /* "?value=" parameter is "1" or "on" */
143     else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
144         if (!ctx->radio) {
145             if (reserveRadio (handle, ctx) == AFB_FAIL) {
146                 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
147                 return (jsonNewMessage (AFB_FAIL, "No more radio devices available"));
148             }
149         }
150         jresp = json_object_new_object();
151         json_object_object_add (jresp, "power", json_object_new_string ("on"));
152     }
153
154     /* "?value=" parameter is "0" or "off" */
155     else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
156         if (ctx->radio) {
157             if (releaseRadio (handle, ctx) == AFB_FAIL) {
158                 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
159                 return (jsonNewMessage (AFB_FAIL, "Unable to release radio device"));
160             }
161         }
162         jresp = json_object_new_object();
163         json_object_object_add (jresp, "power", json_object_new_string ("off"));
164     }
165
166     return jresp;
167 }
168
169 STATIC json_object* mode (AFB_request *request) {        /* AFB_SESSION_CHECK */
170
171     radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
172     const char *value = getQueryValue (request, "value");
173     json_object *jresp;
174
175     /* no "?value=" parameter : return current state */
176     if (!value) {
177         jresp = json_object_new_object();
178         ctx->mode ?
179             json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
180           : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
181     }
182
183     /* "?value=" parameter is "1" or "on" */
184     else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
185         ctx->mode = AM;
186         _radio_set_mode (ctx->idx, ctx->mode);
187
188         jresp = json_object_new_object();
189         json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
190     }
191
192     /* "?value=" parameter is "0" or "off" */
193     else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
194         ctx->mode = FM;
195         _radio_set_mode (ctx->idx, ctx->mode);
196
197         jresp = json_object_new_object();
198         json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
199     }
200     
201     return jresp;
202 }
203
204 STATIC json_object* freq (AFB_request *request) {        /* AFB_SESSION_CHECK */
205
206     radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
207     const char *value = getQueryValue (request, "value");
208     json_object *jresp = json_object_new_object();
209     char freq_str[256];
210
211     /* no "?value=" parameter : return current state */
212     if (!value) {
213         snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
214         json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
215     }
216
217     /* "?value=" parameter, set frequency */
218     else {
219         ctx->freq = strtof (value, NULL);
220         _radio_set_freq (ctx->idx, ctx->freq);
221         
222         snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
223         json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
224     }
225     
226     return jresp;
227 }
228
229 STATIC json_object* mute (AFB_request *request) {        /* AFB_SESSION_CHECK */
230
231     radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
232     const char *value = getQueryValue (request, "value");
233     json_object *jresp;
234     char *mute_str;
235
236     /* no "?value=" parameter : return current state */
237     if (!value) {
238         ctx->mute ?
239             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
240           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
241     }
242
243     /* "?value=" parameter is "1" or "on" */
244     else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
245         ctx->mute = 1;
246         _radio_set_mute (ctx->idx, ctx->mute);
247         
248         jresp = json_object_new_object();
249         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
250     }
251
252     /* "?value=" parameter is "0" or "off" */
253     else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
254         ctx->mute = 0;
255         _radio_set_mute (ctx->idx, ctx->mute);
256         
257         jresp = json_object_new_object();
258         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
259     }
260     
261     return jresp;
262 }
263
264 STATIC json_object* play (AFB_request *request) {        /* AFB_SESSION_CHECK */
265
266     radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
267     const char *value = getQueryValue (request, "value");
268     json_object *jresp;
269     
270     /* no "?value=" parameter : return current state */
271     if (!value) {
272         ctx->is_playing ?
273             json_object_object_add (jresp, "play", json_object_new_string ("on"))
274           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
275     }
276
277     /* "?value=" parameter is "1" or "on" */
278     else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
279         /* radio playback */
280         ctx->is_playing = 1;
281         _radio_play (ctx->idx);
282
283         jresp = json_object_new_object();
284         json_object_object_add (jresp, "play", json_object_new_string ("on"));
285     }
286
287     /* "?value=" parameter is "0" or "off" */
288     else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
289         /* radio stop */
290         ctx->is_playing = 0;
291         _radio_stop (ctx->idx);
292
293         jresp = json_object_new_object();
294         json_object_object_add (jresp, "play-on", json_object_new_string ("off"));
295     }
296
297     return jresp;
298 }
299
300 STATIC json_object* status (AFB_request *request) {
301     return NULL;
302 }
303
304
305 STATIC AFB_restapi pluginApis[]= {
306   {"power"  , AFB_SESSION_CREATE, (AFB_apiCB)power      , "Radio API - power"},
307   {"mode"   , AFB_SESSION_CHECK,  (AFB_apiCB)mode       , "Radio API - mode"},
308   {"freq"   , AFB_SESSION_CHECK,  (AFB_apiCB)freq       , "Radio API - freq"},
309   {"mute"   , AFB_SESSION_CHECK,  (AFB_apiCB)mute       , "Radio API - mute"},
310   {"play"   , AFB_SESSION_CHECK,  (AFB_apiCB)play       , "Radio API - play"},
311   {"status" , AFB_SESSION_RENEW,  (AFB_apiCB)status     , "Radio API - status"},
312   {NULL}
313 };
314
315 PUBLIC AFB_plugin* radioRegister () {
316     AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
317     plugin->type  = AFB_PLUGIN_JSON;
318     plugin->info  = "Application Framework Binder - Radio plugin";
319     plugin->prefix  = "radio";
320     plugin->apis  = pluginApis;
321
322     plugin->handle = initRadioPlugin();
323     plugin->freeCtxCB = freeRadio;
324
325     return (plugin);
326 };