removed private api and fix some few warnings
[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 /* 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     /* stop playing if it was doing this (blocks otherwise) */
100     if (ctx->is_playing) {
101         ctx->is_playing = 0;
102         _radio_stop (ctx->idx);
103     }
104
105     /* power it off */
106     _radio_off (ctx->idx);
107
108     /* globally mark it as free */
109     handle->radios[ctx->idx]->used = FALSE;
110
111     /* clean client context */
112     ctx->radio = NULL;
113     ctx->idx = -1;
114
115     return AFB_SUCCESS;
116 }
117
118 /* called when client session dies [e.g. client quits for more than 15mns] */
119 STATIC void freeRadio (void *context, void *handle) {
120
121     releaseRadio (handle, context);
122     free (context);
123 }
124
125
126 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
127
128 STATIC json_object* init (AFB_request *request) {        /* AFB_SESSION_CHECK */
129
130     json_object *jresp;
131
132     /* create a private client context */
133     if (!request->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->handle;
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     else
180         jresp = NULL;
181
182     return jresp;
183 }
184
185 STATIC json_object* mode (AFB_request *request) {        /* AFB_SESSION_CHECK */
186
187     radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
188     const char *value = getQueryValue (request, "value");
189     json_object *jresp = json_object_new_object();
190
191     /* no "?value=" parameter : return current state */
192     if (!value || !ctx->radio) {
193         ctx->mode ?
194             json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
195           : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
196     }
197
198     /* "?value=" parameter is "1" or "AM" */
199     else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
200         ctx->mode = AM;
201         _radio_set_mode (ctx->idx, ctx->mode);
202         json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
203     }
204
205     /* "?value=" parameter is "0" or "FM" */
206     else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
207         ctx->mode = FM;
208         _radio_set_mode (ctx->idx, ctx->mode);
209         json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
210     }
211     
212     return jresp;
213 }
214
215 STATIC json_object* freq (AFB_request *request) {        /* AFB_SESSION_CHECK */
216
217     radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
218     const char *value = getQueryValue (request, "value");
219     json_object *jresp = json_object_new_object();
220     double freq;
221     char freq_str[256];
222
223     /* no "?value=" parameter : return current state */
224     if (!value || !ctx->radio) {
225         snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
226         json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
227     }
228
229     /* "?value=" parameter, set frequency */
230     else {
231         freq = strtod (value, NULL);
232         _radio_set_freq (ctx->idx, freq);
233         ctx->freq = (float)freq;
234
235         snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
236         json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
237     }
238     
239     return jresp;
240 }
241
242 STATIC json_object* mute (AFB_request *request) {        /* AFB_SESSION_CHECK */
243
244     radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
245     const char *value = getQueryValue (request, "value");
246     json_object *jresp = json_object_new_object();
247     char *mute_str;
248
249     /* no "?value=" parameter : return current state */
250     if (!value || !ctx->radio) {
251         ctx->mute ?
252             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
253           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
254     }
255
256     /* "?value=" parameter is "1" or "true" */
257     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
258         ctx->mute = 1;
259         _radio_set_mute (ctx->idx, ctx->mute);
260         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
261     }
262
263     /* "?value=" parameter is "0" or "false" */
264     else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
265         ctx->mute = 0;
266         _radio_set_mute (ctx->idx, ctx->mute);
267         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
268     }
269     
270     return jresp;
271 }
272
273 STATIC json_object* play (AFB_request *request) {        /* AFB_SESSION_CHECK */
274
275     radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
276     const char *value = getQueryValue (request, "value");
277     json_object *jresp = json_object_new_object();
278     
279     /* no "?value=" parameter : return current state */
280     if (!value || !ctx->radio) {
281         ctx->is_playing ?
282             json_object_object_add (jresp, "play", json_object_new_string ("on"))
283           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
284     }
285
286     /* "?value=" parameter is "1" or "true" */
287     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
288         /* radio playback */
289         ctx->is_playing = 1;
290         _radio_play (ctx->idx);
291         json_object_object_add (jresp, "play", json_object_new_string ("on"));
292     }
293
294     /* "?value=" parameter is "0" or "false" */
295     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
296         /* radio stop */
297         ctx->is_playing = 0;
298         _radio_stop (ctx->idx);
299         json_object_object_add (jresp, "play", json_object_new_string ("off"));
300     }
301
302     return jresp;
303 }
304
305 STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
306     return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
307 }
308
309
310 STATIC AFB_restapi pluginApis[]= {
311   {"init"   , AFB_SESSION_CHECK,  (AFB_apiCB)init       , "Radio API - init"},
312   {"power"  , AFB_SESSION_CHECK,  (AFB_apiCB)power      , "Radio API - power"},
313   {"mode"   , AFB_SESSION_CHECK,  (AFB_apiCB)mode       , "Radio API - mode"},
314   {"freq"   , AFB_SESSION_CHECK,  (AFB_apiCB)freq       , "Radio API - freq"},
315   {"mute"   , AFB_SESSION_CHECK,  (AFB_apiCB)mute       , "Radio API - mute"},
316   {"play"   , AFB_SESSION_CHECK,  (AFB_apiCB)play       , "Radio API - play"},
317   {"ping"   , AFB_SESSION_NONE,   (AFB_apiCB)ping       , "Radio API - ping"},
318   {NULL}
319 };
320
321 PUBLIC AFB_plugin* pluginRegister () {
322     AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
323     plugin->type  = AFB_PLUGIN_JSON;
324     plugin->info  = "Application Framework Binder - Radio plugin";
325     plugin->prefix  = "radio";
326     plugin->apis  = pluginApis;
327
328     plugin->handle = initRadioPlugin();
329     plugin->freeCtxCB = (AFB_freeCtxCB)freeRadio;
330
331     return (plugin);
332 };