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