X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=plugins%2Fradio%2Fradio-api.c;h=3ef098a9c4fb2f01d7471d7400c5629dd7a93c6f;hb=83b48bb7331232020068d537716435458786a0cd;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..3ef098a9 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,7 +36,7 @@ static pluginHandleT *the_radio = NULL; /* detect new radio devices */ -STATIC void updateRadioDevList(pluginHandleT *handle) { +void updateRadioDevList(pluginHandleT *handle) { int idx; @@ -50,24 +50,22 @@ STATIC void updateRadioDevList(pluginHandleT *handle) { } /* global plugin context creation ; at loading time [radio devices might not be visible] */ -STATIC pluginHandleT* initRadioPlugin() { +static void initRadioPlugin() { - pluginHandleT *handle; + pluginHandleT *handle = the_radio; handle = calloc (1, sizeof(pluginHandleT)); updateRadioDevList (handle); - - return 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 +75,7 @@ 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,7 +83,7 @@ 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: /* try to power it on, passing client context info such as frequency... */ @@ -99,11 +97,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 +117,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,24 +132,27 @@ 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 = (radioCtxHandleT*) afb_req_context_get(request); json_object *jresp; /* 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")); + 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 = (radioCtxHandleT*) afb_req_context_get(request); + const char *value = afb_req_value (request, "value"); json_object *jresp; /* no "?value=" parameter : return current state */ @@ -165,10 +166,9 @@ STATIC void power (struct afb_req request) { /* AFB_SESSION_CHECK */ /* "?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(); @@ -178,10 +178,9 @@ STATIC void power (struct afb_req request) { /* AFB_SESSION_CHECK */ /* "?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(); @@ -193,10 +192,10 @@ 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"); + radioCtxHandleT *ctx = (radioCtxHandleT*) afb_req_context_get(request); + const char *value = afb_req_value (request, "value"); json_object *jresp = json_object_new_object(); /* no "?value=" parameter : return current state */ @@ -223,10 +222,10 @@ 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"); + radioCtxHandleT *ctx = (radioCtxHandleT*) afb_req_context_get(request); + const char *value = afb_req_value (request, "value"); json_object *jresp = json_object_new_object(); double freq; char freq_str[256]; @@ -250,12 +249,11 @@ 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 = (radioCtxHandleT*) afb_req_context_get(request); + const char *value = afb_req_value (request, "value"); json_object *jresp = json_object_new_object(); - //char *mute_str; /* no "?value=" parameter : return current state */ if (!value || !ctx->radio) { @@ -281,10 +279,10 @@ 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 = (radioCtxHandleT*) afb_req_context_get(request); + const char *value = afb_req_value (request, "value"); json_object *jresp = json_object_new_object(); /* no "?value=" parameter : return current state */ @@ -313,12 +311,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 +327,17 @@ 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 *pluginAfbV1Entry (const struct AFB_interface *itf) +{ + initRadioPlugin(); + return &pluginDesc; +}