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