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