Update Radio plugin, Media plugin
[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 #include <json-c/json.h>
21
22 #include "radio-api.h"
23 #include "radio-rtlsdr.h"
24
25 #include "afb-plugin.h"
26 #include "afb-req-itf.h"
27
28 /* ********************************************************
29
30    FULUP integration proposal with client session context
31
32    ******************************************************** */
33
34 /* ------ LOCAL HELPER FUNCTIONS --------- */
35
36 static pluginHandleT *the_radio = NULL;
37
38 /* detect new radio devices */
39 void updateRadioDevList(pluginHandleT *handle) {
40
41   int idx;  
42
43   // loop on existing radio if any
44   for (idx = 0; idx < _radio_dev_count(); idx++) {
45       if (idx == MAX_RADIO) break;
46       handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set used to FALSE */
47       handle->radios[idx]->name = (char *) _radio_dev_name(idx); 
48   }
49   handle->devCount = _radio_dev_count();
50 }
51
52 /* global plugin context creation ; at loading time [radio devices might not be visible] */
53 static void initRadioPlugin() {
54
55   pluginHandleT *handle = the_radio;
56
57   handle = calloc (1, sizeof(pluginHandleT));
58   updateRadioDevList (handle);
59 }
60
61 /* private client context creation ; default values */
62 static radioCtxHandleT* initRadioCtx () {
63
64     radioCtxHandleT *ctx;
65
66     ctx = malloc (sizeof(radioCtxHandleT));
67     ctx->radio = NULL;
68     ctx->idx = -1;
69     ctx->mode = FM;
70     ctx->freq = 100.0;
71     ctx->mute = 0;
72     ctx->is_playing = 0;
73
74     return ctx;
75 }
76
77 /* reserve a radio device for requesting client, power it on */
78 unsigned char reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
79     unsigned int idx;
80
81     /* loop on all devices, find an unused one */
82     for (idx = 0; idx < _radio_dev_count(); idx++) {
83         if (idx == MAX_RADIO) break;
84         if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */
85     }
86     return 0;
87
88    found_radio:
89     /* try to power it on, passing client context info such as frequency... */
90     _radio_on (idx, ctx);
91     /* TODO : try to re-iterate from the next ones if it failed ! */
92
93     /* globally mark it as reserved */
94     handle->radios[idx]->used = TRUE;
95
96     /* store relevant info to client context (direct pointer, index) */
97     ctx->radio = handle->radios[idx];
98     ctx->idx = idx;
99
100     return 1;
101 }
102
103 /* free a radio device from requesting client, power it off */
104 unsigned char releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
105
106     /* stop playing if it was doing this (blocks otherwise) */
107     if (ctx->is_playing) {
108         ctx->is_playing = 0;
109         _radio_stop (ctx->idx);
110     }
111
112     /* power it off */
113     _radio_off (ctx->idx);
114
115     /* globally mark it as free */
116     handle->radios[ctx->idx]->used = FALSE;
117
118     /* clean client context */
119     ctx->radio = NULL;
120     ctx->idx = -1;
121
122     return 1;
123 }
124
125 /* called when client session dies [e.g. client quits for more than 15mns] */
126 static void freeRadio (void *context) {
127
128     releaseRadio (the_radio, context);
129     free (context);
130 }
131
132
133 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
134
135 static void init (struct afb_req request) {        /* AFB_SESSION_CHECK */
136
137     radioCtxHandleT *ctx = (radioCtxHandleT*) afb_req_context_get(request);
138     json_object *jresp;
139
140     /* create a private client context */
141     if (!ctx) {
142         ctx = initRadioCtx();
143         afb_req_context_set (request, ctx, free);
144     }
145
146     jresp = json_object_new_object();
147     json_object_object_add(jresp, "init", json_object_new_string ("success"));
148     afb_req_success (request, jresp, "Radio - Initialized");
149 }
150
151 static void power (struct afb_req request) {       /* AFB_SESSION_CHECK */
152
153     pluginHandleT *handle = the_radio;
154     radioCtxHandleT *ctx = (radioCtxHandleT*) afb_req_context_get(request);
155     const char *value = afb_req_value (request, "value");
156     json_object *jresp;
157
158     /* no "?value=" parameter : return current state */
159     if (!value) {
160         jresp = json_object_new_object();
161         ctx->radio ?
162             json_object_object_add (jresp, "power", json_object_new_string ("on"))
163           : json_object_object_add (jresp, "power", json_object_new_string ("off"));
164     }
165
166     /* "?value=" parameter is "1" or "true" */
167     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
168         if (!ctx->radio) {
169             if (!reserveRadio (handle, ctx)) {
170                 afb_req_fail (request, "failed", "no more radio devices available");
171                         return;
172             }
173         }
174         jresp = json_object_new_object();
175         json_object_object_add (jresp, "power", json_object_new_string ("on"));
176     }
177
178     /* "?value=" parameter is "0" or "false" */
179     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
180         if (ctx->radio) {
181             if (!releaseRadio (handle, ctx)) {
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*) afb_req_context_get(request);
198     const char *value = afb_req_value (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*) afb_req_context_get(request);
228     const char *value = afb_req_value (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*) afb_req_context_get(request);
255     const char *value = afb_req_value (request, "value");
256     json_object *jresp = json_object_new_object();
257
258     /* no "?value=" parameter : return current state */
259     if (!value || !ctx->radio) {
260         ctx->mute ?
261             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
262           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
263     }
264
265     /* "?value=" parameter is "1" or "true" */
266     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
267         ctx->mute = 1;
268         _radio_set_mute (ctx->idx, ctx->mute);
269         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
270     }
271
272     /* "?value=" parameter is "0" or "false" */
273     else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
274         ctx->mute = 0;
275         _radio_set_mute (ctx->idx, ctx->mute);
276         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
277     }
278
279     afb_req_success (request, jresp, "Radio - Mute set"); 
280 }
281
282 static void play (struct afb_req request) {        /* AFB_SESSION_CHECK */
283
284     radioCtxHandleT *ctx = (radioCtxHandleT*) afb_req_context_get(request);
285     const char *value = afb_req_value (request, "value");
286     json_object *jresp = json_object_new_object();
287     
288     /* no "?value=" parameter : return current state */
289     if (!value || !ctx->radio) {
290         ctx->is_playing ?
291             json_object_object_add (jresp, "play", json_object_new_string ("on"))
292           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
293     }
294
295     /* "?value=" parameter is "1" or "true" */
296     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
297         /* radio playback */
298         ctx->is_playing = 1;
299         _radio_play (ctx->idx);
300         json_object_object_add (jresp, "play", json_object_new_string ("on"));
301     }
302
303     /* "?value=" parameter is "0" or "false" */
304     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
305         /* radio stop */
306         ctx->is_playing = 0;
307         _radio_stop (ctx->idx);
308         json_object_object_add (jresp, "play", json_object_new_string ("off"));
309     }
310
311     afb_req_success (request, jresp, "Radio - Play succeeded");
312 }
313
314 static void ping (struct afb_req request) {         /* AFB_SESSION_NONE */
315     afb_req_success (request, NULL, "Radio - Ping succeeded");
316 }
317
318
319 static const struct AFB_restapi pluginApis[]= {
320   {"init"   , AFB_SESSION_CHECK,  init       , "Radio API - init"},
321   {"power"  , AFB_SESSION_CHECK,  power      , "Radio API - power"},
322   {"mode"   , AFB_SESSION_CHECK,  mode       , "Radio API - mode"},
323   {"freq"   , AFB_SESSION_CHECK,  freq       , "Radio API - freq"},
324   {"mute"   , AFB_SESSION_CHECK,  mute       , "Radio API - mute"},
325   {"play"   , AFB_SESSION_CHECK,  play       , "Radio API - play"},
326   {"ping"   , AFB_SESSION_NONE,   ping       , "Radio API - ping"},
327   {NULL}
328 };
329
330 static const struct AFB_plugin pluginDesc = {
331     .type  = AFB_PLUGIN_JSON,
332     .info  = "Application Framework Binder - Radio plugin",
333     .prefix  = "radio",
334     .apis  = pluginApis
335 };
336
337 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
338 {
339     initRadioPlugin();
340         return &pluginDesc;
341 }