f065a8ad1d3d7eec36329f74478d295be2a2404f
[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    /* power it off */
101    _radio_off (ctx->idx);
102
103    /* globally mark it as free */
104    handle->radios[ctx->idx]->used = FALSE;
105
106    /* clean client context */
107    ctx->radio = NULL;
108    ctx->idx = -1;
109
110    return AFB_SUCCESS;
111 }
112
113 /* called when client session dies [e.g. client quits for more than 15mns] */
114 STATIC json_object* freeRadio (AFB_clientCtx *client) {
115
116     releaseRadio (client->plugin->handle, client->ctx);
117     free (client->ctx);
118     
119     return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
120 }
121
122
123 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
124
125 STATIC json_object* init (AFB_request *request) {       /* AFB_SESSION_CREATE */
126
127     radioCtxHandleT *ctx;
128     json_object *jresp;
129
130     /* create a private client context */
131     ctx = initRadioCtx();
132     request->client->ctx = (radioCtxHandleT*)ctx;
133
134     jresp = json_object_new_object();
135     json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
136 }
137
138 STATIC json_object* power (AFB_request *request) {       /* AFB_SESSION_CHECK */
139     
140     pluginHandleT *handle = request->client->plugin->handle; 
141     radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
142     const char *value = getQueryValue (request, "value");
143     json_object *jresp;
144
145     /* no "?value=" parameter : return current state */
146     if (!value) {
147         jresp = json_object_new_object();
148         ctx->radio ?
149             json_object_object_add (jresp, "power", json_object_new_string ("on"))
150           : json_object_object_add (jresp, "power", json_object_new_string ("off"));
151     }
152
153     /* "?value=" parameter is "1" or "on" */
154     else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
155         if (!ctx->radio) {
156             if (reserveRadio (handle, ctx) == AFB_FAIL) {
157                 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
158                 return (jsonNewMessage (AFB_FAIL, "No more radio devices available"));
159             }
160         }
161         jresp = json_object_new_object();
162         json_object_object_add (jresp, "power", json_object_new_string ("on"));
163     }
164
165     /* "?value=" parameter is "0" or "off" */
166     else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
167         if (ctx->radio) {
168             if (releaseRadio (handle, ctx) == AFB_FAIL) {
169                 request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
170                 return (jsonNewMessage (AFB_FAIL, "Unable to release radio device"));
171             }
172         }
173         jresp = json_object_new_object();
174         json_object_object_add (jresp, "power", json_object_new_string ("off"));
175     }
176
177     return jresp;
178 }
179
180 STATIC json_object* mode (AFB_request *request) {        /* AFB_SESSION_CHECK */
181
182     radioCtxHandleT *ctx = (radioCtxHandleT*)request->client->ctx;
183     const char *value = getQueryValue (request, "value");
184     json_object *jresp;
185
186     /* no "?value=" parameter : return current state */
187     if (!value) {
188         jresp = json_object_new_object();
189         ctx->mode ?
190             json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
191           : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
192     }
193
194     /* "?value=" parameter is "1" or "on" */
195     else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
196         ctx->mode = AM;
197         _radio_set_mode (ctx->idx, ctx->mode);
198
199         jresp = json_object_new_object();
200         json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
201     }
202
203     /* "?value=" parameter is "0" or "off" */
204     else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
205         ctx->mode = FM;
206         _radio_set_mode (ctx->idx, ctx->mode);
207
208         jresp = json_object_new_object();
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->client->ctx;
218     const char *value = getQueryValue (request, "value");
219     json_object *jresp = json_object_new_object();
220     char freq_str[256];
221
222     /* no "?value=" parameter : return current state */
223     if (!value) {
224         snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
225         json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
226     }
227
228     /* "?value=" parameter, set frequency */
229     else {
230         ctx->freq = strtof (value, NULL);
231         _radio_set_freq (ctx->idx, ctx->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->client->ctx;
243     const char *value = getQueryValue (request, "value");
244     json_object *jresp;
245     char *mute_str;
246
247     /* no "?value=" parameter : return current state */
248     if (!value) {
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 "on" */
255     else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
256         ctx->mute = 1;
257         _radio_set_mute (ctx->idx, ctx->mute);
258         
259         jresp = json_object_new_object();
260         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
261     }
262
263     /* "?value=" parameter is "0" or "off" */
264     else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
265         ctx->mute = 0;
266         _radio_set_mute (ctx->idx, ctx->mute);
267         
268         jresp = json_object_new_object();
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->client->ctx;
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) {
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 "on" */
289     else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) {
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 "off" */
297     else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
298         /* radio stop */
299         ctx->is_playing = 0;
300         _radio_stop (ctx->idx);
301         json_object_object_add (jresp, "play-on", json_object_new_string ("off"));
302     }
303
304     return jresp;
305 }
306
307 STATIC json_object* status (AFB_request *request) {
308     return NULL;
309 }
310
311
312 STATIC AFB_restapi pluginApis[]= {
313   {"init"   , AFB_SESSION_CREATE, (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   {"status" , AFB_SESSION_RENEW,  (AFB_apiCB)status     , "Radio API - status"},
320   {NULL}
321 };
322
323 PUBLIC AFB_plugin* radioRegister () {
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->handle = initRadioPlugin();
331     plugin->freeCtxCB = freeRadio;
332
333     return (plugin);
334 };