add .gitreview
[src/app-framework-binder.git] / bindings / radio / radio-api.c
1 /*
2  * Copyright (C) 2015, 2016 "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/afb-plugin.h>
26 #include <afb/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;
56
57   handle = calloc (1, sizeof(pluginHandleT));
58   updateRadioDevList (handle);
59   the_radio = 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 unsigned char reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
80
81     unsigned int idx;
82
83     /* loop on all devices, find an unused one */
84     for (idx = 0; idx < _radio_dev_count(); idx++) {
85         if (idx == MAX_RADIO) break;
86         if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */
87     }
88     return 0;
89
90   found_radio:
91     /* try to power it on, passing client context info such as frequency... */
92
93     _radio_on (idx, ctx);
94     /* TODO : try to re-iterate from the next ones if it failed ! */
95
96     /* globally mark it as reserved */
97     handle->radios[idx]->used = TRUE;
98
99     /* store relevant info to client context (direct pointer, index) */
100     ctx->radio = handle->radios[idx];
101     ctx->idx = idx;
102
103     return 1;
104 }
105
106 /* free a radio device from requesting client, power it off */
107 unsigned char releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
108
109     /* stop playing if it was doing this (blocks otherwise) */
110     if (ctx->is_playing) {
111         ctx->is_playing = 0;
112         _radio_stop (ctx->idx);
113     }
114
115     /* power it off */
116     _radio_off (ctx->idx);
117
118     /* globally mark it as free */
119     handle->radios[ctx->idx]->used = FALSE;
120
121     /* clean client context */
122     ctx->radio = NULL;
123     ctx->idx = -1;
124
125     return 1;
126 }
127
128 /* called when client session dies [e.g. client quits for more than 15mns] */
129 static void freeRadio (void *context) {
130
131     releaseRadio (the_radio, context);
132     free (context);
133 }
134
135
136 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
137
138 static void init (struct afb_req request) {        /* AFB_SESSION_CHECK */
139
140     radioCtxHandleT *ctx = afb_req_context_get (request);
141     json_object *jresp;
142
143     /* create a global plugin handle */
144     if (!the_radio)
145         initRadioPlugin();
146
147     /* create a private client context */
148     if (!ctx) {
149         ctx = initRadioCtx();
150         afb_req_context_set (request, ctx, free);
151     }
152
153     jresp = json_object_new_object();
154     json_object_object_add(jresp, "init", json_object_new_string ("success"));
155     afb_req_success (request, jresp, "Radio initialized");
156 }
157
158 static void power (struct afb_req request) {       /* AFB_SESSION_CHECK */
159
160     pluginHandleT *handle = the_radio;
161     radioCtxHandleT *ctx = afb_req_context_get (request);
162     const char *value = afb_req_value (request, "value");
163     json_object *jresp;
164
165         if (!ctx) {
166         afb_req_fail (request, "failed", "you must call 'init' first");
167         return;
168     }
169     jresp = json_object_new_object();
170
171     /* no "?value=" parameter : return current state */
172     if (!value) {
173         ctx->radio ?
174             json_object_object_add (jresp, "power", json_object_new_string ("on"))
175           : json_object_object_add (jresp, "power", json_object_new_string ("off"));
176         afb_req_success (request, jresp, "Radio - Power status obtained");
177         return;
178     }
179
180     /* "?value=" parameter is "1" or "true" */
181     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
182         if (!ctx->radio) {
183             if (!reserveRadio (handle, ctx)) {
184                 afb_req_fail (request, "failed", "no more radio devices available");
185                         return;
186             }
187         }
188         json_object_object_add (jresp, "power", json_object_new_string ("on"));
189     }
190
191     /* "?value=" parameter is "0" or "false" */
192     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
193         if (ctx->radio) {
194             if (!releaseRadio (handle, ctx)) {
195                 afb_req_fail (request, "failed", "Unable to release radio device");
196                         return;
197             }
198         }
199         json_object_object_add (jresp, "power", json_object_new_string ("off"));
200     }
201     else
202         jresp = NULL;
203
204     afb_req_success (request, jresp, "Radio - Power set");
205 }
206
207 static void mode (struct afb_req request) {        /* AFB_SESSION_CHECK */
208
209     radioCtxHandleT *ctx = afb_req_context_get (request);
210     const char *value = afb_req_value (request, "value");
211     json_object *jresp;
212
213         if (!ctx) {
214         afb_req_fail (request, "failed", "you must call 'init' first");
215         return;
216     }
217     jresp = json_object_new_object();
218
219     /* no "?value=" parameter : return current state */
220     if (!value || !ctx->radio) {
221         ctx->mode ?
222             json_object_object_add (jresp, "mode", json_object_new_string ("AM"))
223           : json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
224     }
225
226     /* "?value=" parameter is "1" or "AM" */
227     else if ( atoi(value) == 1 || !strcasecmp(value, "AM") ) {
228         ctx->mode = AM;
229         _radio_set_mode (ctx->idx, ctx->mode);
230         json_object_object_add (jresp, "mode", json_object_new_string ("AM"));
231     }
232
233     /* "?value=" parameter is "0" or "FM" */
234     else if ( atoi(value) == 0 || !strcasecmp(value, "FM") ) {
235         ctx->mode = FM;
236         _radio_set_mode (ctx->idx, ctx->mode);
237         json_object_object_add (jresp, "mode", json_object_new_string ("FM"));
238     }
239
240     afb_req_success (request, jresp, "Radio - Mode set");
241 }
242
243 static void freq (struct afb_req request) {        /* AFB_SESSION_CHECK */
244
245     radioCtxHandleT *ctx = afb_req_context_get (request);
246     const char *value = afb_req_value (request, "value");
247     json_object *jresp;
248     double freq;
249     char freq_str[256];
250
251         if (!ctx) {
252         afb_req_fail (request, "failed", "you must call 'init' first");
253         return;
254     }
255     jresp = json_object_new_object();
256
257     /* no "?value=" parameter : return current state */
258     if (!value || !ctx->radio) {
259         snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
260         json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
261     }
262
263     /* "?value=" parameter, set frequency */
264     else {
265         freq = strtod (value, NULL);
266         _radio_set_freq (ctx->idx, freq);
267         ctx->freq = (float)freq;
268
269         snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
270         json_object_object_add (jresp, "freq", json_object_new_string (freq_str));
271     }
272
273     afb_req_success (request, jresp, "Radio - Frequency Set");
274 }
275
276 static void mute (struct afb_req request) {        /* AFB_SESSION_CHECK */
277
278     radioCtxHandleT *ctx = afb_req_context_get (request);
279     const char *value = afb_req_value (request, "value");
280     json_object *jresp = json_object_new_object();
281
282         if (!ctx) {
283         afb_req_fail (request, "failed", "you must call 'init' first");
284         return;
285     }
286
287     /* no "?value=" parameter : return current state */
288     if (!value || !ctx->radio) {
289         ctx->mute ?
290             json_object_object_add (jresp, "mute", json_object_new_string ("on"))
291           : json_object_object_add (jresp, "mute", json_object_new_string ("off"));
292     }
293
294     /* "?value=" parameter is "1" or "true" */
295     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
296         ctx->mute = 1;
297         _radio_set_mute (ctx->idx, ctx->mute);
298         json_object_object_add (jresp, "mute", json_object_new_string ("on"));
299     }
300
301     /* "?value=" parameter is "0" or "false" */
302     else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
303         ctx->mute = 0;
304         _radio_set_mute (ctx->idx, ctx->mute);
305         json_object_object_add (jresp, "mute", json_object_new_string ("off"));
306     }
307
308     afb_req_success (request, jresp, "Radio - Mute set"); 
309 }
310
311 static void play (struct afb_req request) {        /* AFB_SESSION_CHECK */
312
313     radioCtxHandleT *ctx = afb_req_context_get (request);
314     const char *value = afb_req_value (request, "value");
315     json_object *jresp = json_object_new_object();
316
317         if (!ctx) {
318         afb_req_fail (request, "failed", "you must call 'init' first");
319         return;
320     }
321     
322     /* no "?value=" parameter : return current state */
323     if (!value || !ctx->radio) {
324         ctx->is_playing ?
325             json_object_object_add (jresp, "play", json_object_new_string ("on"))
326           : json_object_object_add (jresp, "play", json_object_new_string ("off"));
327     }
328
329     /* "?value=" parameter is "1" or "true" */
330     else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
331         /* radio playback */
332         ctx->is_playing = 1;
333         _radio_play (ctx->idx);
334         json_object_object_add (jresp, "play", json_object_new_string ("on"));
335     }
336
337     /* "?value=" parameter is "0" or "false" */
338     else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
339         /* radio stop */
340         ctx->is_playing = 0;
341         _radio_stop (ctx->idx);
342         json_object_object_add (jresp, "play", json_object_new_string ("off"));
343     }
344
345     afb_req_success (request, jresp, "Radio - Play succeeded");
346 }
347
348 static void ping (struct afb_req request) {         /* AFB_SESSION_NONE */
349     afb_req_success (request, NULL, "Radio - Ping succeeded");
350 }
351
352
353 static const struct AFB_verb_desc_v1 verbs[] = {
354   {"init"   , AFB_SESSION_CHECK,  init       , "Radio API - init"},
355   {"power"  , AFB_SESSION_CHECK,  power      , "Radio API - power"},
356   {"mode"   , AFB_SESSION_CHECK,  mode       , "Radio API - mode"},
357   {"freq"   , AFB_SESSION_CHECK,  freq       , "Radio API - freq"},
358   {"mute"   , AFB_SESSION_CHECK,  mute       , "Radio API - mute"},
359   {"play"   , AFB_SESSION_CHECK,  play       , "Radio API - play"},
360   {"ping"   , AFB_SESSION_NONE,   ping       , "Radio API - ping"},
361   {NULL}
362 };
363
364 static const struct AFB_plugin pluginDesc = {
365     .type  = AFB_PLUGIN_VERSION_1,
366     .v1 = {
367         .info  = "Application Framework Binder - Radio plugin",
368         .prefix  = "radio",
369         .verbs  = verbs
370     }
371 };
372
373 const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf)
374 {
375         return &pluginDesc;
376 }