X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=inline;f=plugins%2Fradio%2Fradio-api.c;h=0ed60e1c346d45e7b2f1ff65283e43969a139d09;hb=a1ed2c12ac2a305a54f35a3dad99b85cbe3113e6;hp=b64d083086868f136aee93f82dc3ae63d24c1ea8;hpb=e6298876fdbf457b6dd61556472060a9fa652c82;p=src%2Fapp-framework-binder.git
diff --git a/plugins/radio/radio-api.c b/plugins/radio/radio-api.c
index b64d0830..0ed60e1c 100644
--- a/plugins/radio/radio-api.c
+++ b/plugins/radio/radio-api.c
@@ -1,29 +1,29 @@
/*
- * Copyright (C) 2015 "IoT.bzh"
+ * Copyright (C) 2015, 2016 "IoT.bzh"
* Author "Manuel Bachmann"
*
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*/
#define _GNU_SOURCE
#include
+#include
#include "radio-api.h"
#include "radio-rtlsdr.h"
-#include "afb-plugin.h"
-#include "afb-req-itf.h"
+#include
+#include
/* ********************************************************
@@ -36,38 +36,37 @@
static pluginHandleT *the_radio = NULL;
/* detect new radio devices */
-STATIC void updateRadioDevList(pluginHandleT *handle) {
+void updateRadioDevList(pluginHandleT *handle) {
int idx;
// loop on existing radio if any
for (idx = 0; idx < _radio_dev_count(); idx++) {
if (idx == MAX_RADIO) break;
- handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set used to FALSE */
- handle->radios[idx]->name = (char *) _radio_dev_name(idx);
+ handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set "used" to FALSE */
+ handle->radios[idx]->name = (char *) _radio_dev_name(idx);
}
handle->devCount = _radio_dev_count();
}
/* global plugin context creation ; at loading time [radio devices might not be visible] */
-STATIC pluginHandleT* initRadioPlugin() {
+static void initRadioPlugin() {
pluginHandleT *handle;
handle = calloc (1, sizeof(pluginHandleT));
updateRadioDevList (handle);
-
- return handle;
+ the_radio = handle;
}
/* private client context creation ; default values */
-STATIC radioCtxHandleT* initRadioCtx () {
+static radioCtxHandleT* initRadioCtx () {
radioCtxHandleT *ctx;
ctx = malloc (sizeof(radioCtxHandleT));
ctx->radio = NULL;
- //ctx->idx = -1;
+ ctx->idx = -1;
ctx->mode = FM;
ctx->freq = 100.0;
ctx->mute = 0;
@@ -77,7 +76,8 @@ STATIC radioCtxHandleT* initRadioCtx () {
}
/* reserve a radio device for requesting client, power it on */
-STATIC AFB_error reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
+unsigned char reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
+
unsigned int idx;
/* loop on all devices, find an unused one */
@@ -85,10 +85,11 @@ STATIC AFB_error reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
if (idx == MAX_RADIO) break;
if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */
}
- return AFB_FAIL;
+ return 0;
- found_radio:
+ found_radio:
/* try to power it on, passing client context info such as frequency... */
+
_radio_on (idx, ctx);
/* TODO : try to re-iterate from the next ones if it failed ! */
@@ -99,11 +100,11 @@ STATIC AFB_error reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
ctx->radio = handle->radios[idx];
ctx->idx = idx;
- return AFB_SUCCESS;
+ return 1;
}
/* free a radio device from requesting client, power it off */
-STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
+unsigned char releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
/* stop playing if it was doing this (blocks otherwise) */
if (ctx->is_playing) {
@@ -119,13 +120,13 @@ STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
/* clean client context */
ctx->radio = NULL;
- //ctx->idx = -1;
+ ctx->idx = -1;
- return AFB_SUCCESS;
+ return 1;
}
/* called when client session dies [e.g. client quits for more than 15mns] */
-STATIC void freeRadio (void *context) {
+static void freeRadio (void *context) {
releaseRadio (the_radio, context);
free (context);
@@ -134,57 +135,67 @@ STATIC void freeRadio (void *context) {
/* ------ PUBLIC PLUGIN FUNCTIONS --------- */
-STATIC void init (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void init (struct afb_req request) { /* AFB_SESSION_CHECK */
+ radioCtxHandleT *ctx = afb_req_context_get (request);
json_object *jresp;
+ /* create a global plugin handle */
+ if (!the_radio)
+ initRadioPlugin();
+
/* create a private client context */
- if (!request.context)
- request.context = initRadioCtx();
+ if (!ctx) {
+ ctx = initRadioCtx();
+ afb_req_context_set (request, ctx, free);
+ }
jresp = json_object_new_object();
- json_object_object_add(jresp, "info", json_object_new_string ("Radio initialized"));
- afb_req_success (request, jresp, "Radio - Initialized");
+ json_object_object_add(jresp, "init", json_object_new_string ("success"));
+ afb_req_success (request, jresp, "Radio initialized");
}
-STATIC void power (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void power (struct afb_req request) { /* AFB_SESSION_CHECK */
pluginHandleT *handle = the_radio;
- radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
- const char *value = afb_req_argument (request, "value");
+ radioCtxHandleT *ctx = afb_req_context_get (request);
+ const char *value = afb_req_value (request, "value");
json_object *jresp;
+ if (!ctx) {
+ afb_req_fail (request, "failed", "you must call 'init' first");
+ return;
+ }
+ jresp = json_object_new_object();
+
/* no "?value=" parameter : return current state */
if (!value) {
- jresp = json_object_new_object();
ctx->radio ?
json_object_object_add (jresp, "power", json_object_new_string ("on"))
: json_object_object_add (jresp, "power", json_object_new_string ("off"));
+ afb_req_success (request, jresp, "Radio - Power status obtained");
+ return;
}
/* "?value=" parameter is "1" or "true" */
else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
if (!ctx->radio) {
- if (reserveRadio (handle, ctx) == AFB_FAIL) {
- //request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
- afb_req_fail (request, "failed", "No more radio devices available");
- return;
+ if (!reserveRadio (handle, ctx)) {
+ afb_req_fail (request, "failed", "no more radio devices available");
+ return;
}
}
- jresp = json_object_new_object();
json_object_object_add (jresp, "power", json_object_new_string ("on"));
}
/* "?value=" parameter is "0" or "false" */
else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
if (ctx->radio) {
- if (releaseRadio (handle, ctx) == AFB_FAIL) {
- //request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
+ if (!releaseRadio (handle, ctx)) {
afb_req_fail (request, "failed", "Unable to release radio device");
- return;
+ return;
}
}
- jresp = json_object_new_object();
json_object_object_add (jresp, "power", json_object_new_string ("off"));
}
else
@@ -193,11 +204,17 @@ STATIC void power (struct afb_req request) { /* AFB_SESSION_CHECK */
afb_req_success (request, jresp, "Radio - Power set");
}
-STATIC void mode (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void mode (struct afb_req request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
- const char *value = afb_req_argument (request, "value");
- json_object *jresp = json_object_new_object();
+ radioCtxHandleT *ctx = afb_req_context_get (request);
+ const char *value = afb_req_value (request, "value");
+ json_object *jresp;
+
+ if (!ctx) {
+ afb_req_fail (request, "failed", "you must call 'init' first");
+ return;
+ }
+ jresp = json_object_new_object();
/* no "?value=" parameter : return current state */
if (!value || !ctx->radio) {
@@ -223,14 +240,20 @@ STATIC void mode (struct afb_req request) { /* AFB_SESSION_CHECK */
afb_req_success (request, jresp, "Radio - Mode set");
}
-STATIC void freq (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void freq (struct afb_req request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
- const char *value = afb_req_argument (request, "value");
- json_object *jresp = json_object_new_object();
+ radioCtxHandleT *ctx = afb_req_context_get (request);
+ const char *value = afb_req_value (request, "value");
+ json_object *jresp;
double freq;
char freq_str[256];
+ if (!ctx) {
+ afb_req_fail (request, "failed", "you must call 'init' first");
+ return;
+ }
+ jresp = json_object_new_object();
+
/* no "?value=" parameter : return current state */
if (!value || !ctx->radio) {
snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
@@ -250,12 +273,16 @@ STATIC void freq (struct afb_req request) { /* AFB_SESSION_CHECK */
afb_req_success (request, jresp, "Radio - Frequency Set");
}
-STATIC void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
- const char *value = afb_req_argument (request, "value");
+ radioCtxHandleT *ctx = afb_req_context_get (request);
+ const char *value = afb_req_value (request, "value");
json_object *jresp = json_object_new_object();
- //char *mute_str;
+
+ if (!ctx) {
+ afb_req_fail (request, "failed", "you must call 'init' first");
+ return;
+ }
/* no "?value=" parameter : return current state */
if (!value || !ctx->radio) {
@@ -281,11 +308,16 @@ STATIC void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
afb_req_success (request, jresp, "Radio - Mute set");
}
-STATIC void play (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void play (struct afb_req request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
- const char *value = afb_req_argument (request, "value");
+ radioCtxHandleT *ctx = afb_req_context_get (request);
+ const char *value = afb_req_value (request, "value");
json_object *jresp = json_object_new_object();
+
+ if (!ctx) {
+ afb_req_fail (request, "failed", "you must call 'init' first");
+ return;
+ }
/* no "?value=" parameter : return current state */
if (!value || !ctx->radio) {
@@ -313,12 +345,12 @@ STATIC void play (struct afb_req request) { /* AFB_SESSION_CHECK */
afb_req_success (request, jresp, "Radio - Play succeeded");
}
-STATIC void ping (struct afb_req request) { /* AFB_SESSION_NONE */
+static void ping (struct afb_req request) { /* AFB_SESSION_NONE */
afb_req_success (request, NULL, "Radio - Ping succeeded");
}
-STATIC const struct AFB_restapi pluginApis[]= {
+static const struct AFB_verb_desc_v1 verbs[] = {
{"init" , AFB_SESSION_CHECK, init , "Radio API - init"},
{"power" , AFB_SESSION_CHECK, power , "Radio API - power"},
{"mode" , AFB_SESSION_CHECK, mode , "Radio API - mode"},
@@ -329,11 +361,16 @@ STATIC const struct AFB_restapi pluginApis[]= {
{NULL}
};
-STATIC const struct AFB_plugin plug_desc = {
- .type = AFB_PLUGIN_JSON,
- .info = "Application Framework Binder - Radio plugin",
- .prefix = "radio",
- .apis = pluginApis,
- //plugin->freeCtxCB = (AFB_freeCtxCB)freeRadio;
- //the_radio = initRadioPlugin();
+static const struct AFB_plugin pluginDesc = {
+ .type = AFB_PLUGIN_VERSION_1,
+ .v1 = {
+ .info = "Application Framework Binder - Radio plugin",
+ .prefix = "radio",
+ .verbs = verbs
+ }
};
+
+const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf)
+{
+ return &pluginDesc;
+}