From 1393b5d318e22dbd1625692847a51c27932fd442 Mon Sep 17 00:00:00 2001 From: Fulup Ar Foll Date: Wed, 8 Mar 2017 14:23:35 +0100 Subject: Events Partially Working --- AudioLogic/AudioLogicBinding.c | 84 +++++++++++++++++++++++++++++++++++++++ AudioLogic/AudioLogicLib.c | 89 ++++++++++++++++++++++++++++++++++++++++++ AudioLogic/AudioLogicLib.h | 50 ++++++++++++++++++++++++ AudioLogic/CMakeLists.txt | 36 +++++++++++++++++ AudioLogic/README.md | 18 +++++++++ 5 files changed, 277 insertions(+) create mode 100644 AudioLogic/AudioLogicBinding.c create mode 100644 AudioLogic/AudioLogicLib.c create mode 100644 AudioLogic/AudioLogicLib.h create mode 100644 AudioLogic/CMakeLists.txt create mode 100644 AudioLogic/README.md (limited to 'AudioLogic') diff --git a/AudioLogic/AudioLogicBinding.c b/AudioLogic/AudioLogicBinding.c new file mode 100644 index 0000000..65943c0 --- /dev/null +++ b/AudioLogic/AudioLogicBinding.c @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author Fulup Ar Foll + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 +#include +#include +#include +#include +#include +#include + +#define _GNU_SOURCE // needed for vasprintf +#include "AudioLogicLib.h" + +PUBLIC const struct afb_binding_interface *afbIface; + +STATIC void localping(struct afb_req request) { + json_object *query = afb_req_json(request); + afb_req_success(request, query, NULL); +} + +/* + * array of the verbs exported to afb-daemon + */ +STATIC const struct afb_verb_desc_v1 binding_verbs[] = { + /* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL SHORT DESCRIPTION */ + { .name= "ping" , .session= AFB_SESSION_NONE, .callback= localping, .info= "Ping Binding" }, + { .name= "setvolume", .session= AFB_SESSION_NONE, .callback= audioLogicSetVol, .info= "Set Volume" }, + { .name= "getvolume", .session= AFB_SESSION_NONE, .callback= audioLogicGetVol, .info= "Get Volume" }, + { .name= "monitor", .session= AFB_SESSION_NONE, .callback= audioLogicMonitor, .info= "Subscribe Volume Events" }, + { .name= NULL } /* marker for end of the array */ +}; + +/* + * description of the binding for afb-daemon + */ +STATIC const struct afb_binding binding_description = { + /* description conforms to VERSION 1 */ + .type= AFB_BINDING_VERSION_1, + .v1= { + .prefix= "audio", + .info= "High Level Interface to Audio bindings", + .verbs = binding_verbs + } +}; + +// This receive all event this binding subscribe to +PUBLIC void afbBindingV1ServiceEvent(const char *evtname, struct json_object *object) { + + NOTICE (afbIface, "afbBindingV1ServiceEvent evtname=%s [msg=%s]", evtname, json_object_to_json_string(object)); +} + +// this is call when after all bindings are loaded +PUBLIC int afbBindingV1ServiceInit(struct afb_service service) { + + return (audioLogicInit(service)); +}; + +/* + * activation function for registering the binding called by afb-daemon + */ +PUBLIC const struct afb_binding *afbBindingV1Register(const struct afb_binding_interface *itf) { + afbIface= itf; + + return &binding_description; /* returns the description of the binding */ +} + diff --git a/AudioLogic/AudioLogicLib.c b/AudioLogic/AudioLogicLib.c new file mode 100644 index 0000000..ebe241c --- /dev/null +++ b/AudioLogic/AudioLogicLib.c @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author Fulup Ar Foll + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "AudioLogicLib.h" +static struct afb_service srvitf; + +#define _GNU_SOURCE // needed for vasprintf + +// this function is call after all binder are loaded and initialised +PUBLIC int audioLogicInit (struct afb_service service) { + srvitf = service; + return 0; +} + +// This callback is fired when afb_service_call for api/alsacore/subctl returns +STATIC void alsaSubcribeCB (void *handle, int iserror, struct json_object *result) { + struct afb_req request = afb_req_unstore(handle); + struct json_object *x, *resp = NULL; + const char *info = NULL; + + if (result) { + INFO (afbIface, "result=[%s]\n", json_object_to_json_string (result)); + if (json_object_object_get_ex(result, "request", &x) && json_object_object_get_ex(x, "info", &x)) + info = json_object_get_string(x); + if (!json_object_object_get_ex(result, "response", &resp)) resp = NULL; + } + + // push message respond + if (iserror) afb_req_fail_f(request, "Fail", info); + else afb_req_success(request, resp, info); + + // free calling request + afb_req_unref(request); +} + +// Create and subscribe to alsacore ctl events +PUBLIC void audioLogicMonitor(struct afb_req request) { + + // save request in session as it might be used after return by callback + struct afb_req *handle = afb_req_store(request); + + // push request to low level binding + if (!handle) afb_req_fail(request, "error", "out of memory"); + else afb_service_call(srvitf, "alsacore", "subctl", json_object_get(afb_req_json(request)), alsaSubcribeCB, handle); + + // success/failure messages return from callback +} + +// Call when all bindings are loaded and ready to accept request +PUBLIC void audioLogicGetVol(struct afb_req request) { + + afb_req_success (request, NULL, NULL); + return; + +} + +PUBLIC void audioLogicSetVol(struct afb_req request) { + + + afb_req_success (request, NULL, NULL); + return; + +} + diff --git a/AudioLogic/AudioLogicLib.h b/AudioLogic/AudioLogicLib.h new file mode 100644 index 0000000..21513fd --- /dev/null +++ b/AudioLogic/AudioLogicLib.h @@ -0,0 +1,50 @@ +/* + * AlsaLibMapping -- provide low level interface with AUDIO lib (extracted from alsa-json-gateway code) + * Copyright (C) 2015,2016,2017, Fulup Ar Foll fulup@iot.bzh + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + + +// few coding convention +typedef int BOOL; +#ifndef PUBLIC + #define PUBLIC +#endif +#ifndef FALSE + #define FALSE 0 +#endif +#ifndef TRUE + #define TRUE 1 +#endif +#define STATIC static + +#ifndef AUDIOLIBMAPPING_H +#define AUDIOLIBMAPPING_H + +#include +#include +#include + + +// import from AlsaAfbBinding +extern const struct afb_binding_interface *afbIface; + +// import from AlsaAfbMapping +PUBLIC void audioLogicSetVol (struct afb_req request); +PUBLIC void audioLogicGetVol(struct afb_req request); +PUBLIC void audioLogicMonitor(struct afb_req request); +PUBLIC int audioLogicInit (struct afb_service service); + +#endif /* AUDIOLIBMAPPING_H */ + diff --git a/AudioLogic/CMakeLists.txt b/AudioLogic/CMakeLists.txt new file mode 100644 index 0000000..0b72b5c --- /dev/null +++ b/AudioLogic/CMakeLists.txt @@ -0,0 +1,36 @@ +########################################################################### +# Copyright 2015, 2016, 2017 IoT.bzh +# +# author: Fulup Ar Foll +# +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. +########################################################################### + + +INCLUDE_DIRECTORIES(${include_dirs}) + +################################################## +# AudioLogicBinding +################################################## +ADD_LIBRARY(audiologic-afb MODULE AudioLogicBinding.c AudioLogicLib.c) + +SET_TARGET_PROPERTIES(audiologic-afb PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_SOURCE_DIR}/export.map" +) + +TARGET_LINK_LIBRARIES(audiologic-afb ${link_libraries}) +INSTALL(TARGETS audiologic-afb + LIBRARY DESTINATION ${binding_install_dir}) + + diff --git a/AudioLogic/README.md b/AudioLogic/README.md new file mode 100644 index 0000000..399f905 --- /dev/null +++ b/AudioLogic/README.md @@ -0,0 +1,18 @@ +------------------------------------------------------------------------ + AudioLogic High Level APIs +------------------------------------------------------------------------ + +Testing: (from project directory bindings) + * start binder: ~/opt/bin/afb-daemon --ldpaths=./build --roothttp=htdocs + * connect browser on http://localhost:1234 + + # Subscribe event for a given board + http://localhost:1234/api/audio/subscribe?devid=hw:0 + + # Increase Volume + http://localhost:1234/api/audio/setvol?devid=hw:0&pcm=master&vol=50% + + # Get Volume + http://localhost:1234/api/audio/getvol?devid=hw:0&pcm=master + + \ No newline at end of file -- cgit 1.2.3-korg