From a237499e8c2e2a045ae3eea0b9a4aaac12032e81 Mon Sep 17 00:00:00 2001 From: Fulup Ar Foll Date: Wed, 8 Mar 2017 23:56:40 +0100 Subject: Late Evening Commit --- Alsa/CMakeLists.txt | 31 - Alsa/core-binding/AlsaAfbBinding.c | 78 --- Alsa/core-binding/AlsaLibMapping.c | 728 -------------------- Alsa/core-binding/AlsaLibMapping.h | 49 -- Alsa/core-binding/CMakeLists.txt | 36 - Alsa/core-binding/README.md | 20 - AlsaSound/CMakeLists.txt | 32 + AlsaSound/CoreBinding/AlsaAfbBinding.c | 78 +++ AlsaSound/CoreBinding/AlsaLibMapping.c | 787 ++++++++++++++++++++++ AlsaSound/CoreBinding/AlsaLibMapping.h | 51 ++ AlsaSound/CoreBinding/CMakeLists.txt | 36 + AlsaSound/CoreBinding/README.md | 30 + AlsaSound/HardAbsLayer/CMakeLists.txt | 23 + AlsaSound/HardAbsLayer/IntelHda/CMakeLists.txt | 36 + AlsaSound/HardAbsLayer/IntelHda/IntelHdaBinding.c | 82 +++ AlsaSound/HardAbsLayer/IntelHda/IntelHdaLib.c | 138 ++++ AlsaSound/HardAbsLayer/IntelHda/IntelHdaLib.h | 49 ++ AlsaSound/include/AlsaMixerHal.h | 71 ++ AudioLogic/AudioLogicBinding.c | 14 +- AudioLogic/AudioLogicLib.c | 153 ++++- AudioLogic/AudioLogicLib.h | 14 + AudioLogic/README.md | 29 +- CMakeLists.txt | 4 +- README.md | 6 +- htdocs/AFB-websock.js | 172 +++++ htdocs/AudioBinding.js | 67 ++ htdocs/README.md | 2 +- htdocs/alsa-core.html | 44 +- htdocs/audio-logic.html | 47 +- htdocs/websock.js | 117 ---- nbproject/configurations.xml | 9 + 31 files changed, 1881 insertions(+), 1152 deletions(-) delete mode 100644 Alsa/CMakeLists.txt delete mode 100644 Alsa/core-binding/AlsaAfbBinding.c delete mode 100644 Alsa/core-binding/AlsaLibMapping.c delete mode 100644 Alsa/core-binding/AlsaLibMapping.h delete mode 100644 Alsa/core-binding/CMakeLists.txt delete mode 100644 Alsa/core-binding/README.md create mode 100644 AlsaSound/CMakeLists.txt create mode 100644 AlsaSound/CoreBinding/AlsaAfbBinding.c create mode 100644 AlsaSound/CoreBinding/AlsaLibMapping.c create mode 100644 AlsaSound/CoreBinding/AlsaLibMapping.h create mode 100644 AlsaSound/CoreBinding/CMakeLists.txt create mode 100644 AlsaSound/CoreBinding/README.md create mode 100644 AlsaSound/HardAbsLayer/CMakeLists.txt create mode 100644 AlsaSound/HardAbsLayer/IntelHda/CMakeLists.txt create mode 100644 AlsaSound/HardAbsLayer/IntelHda/IntelHdaBinding.c create mode 100644 AlsaSound/HardAbsLayer/IntelHda/IntelHdaLib.c create mode 100644 AlsaSound/HardAbsLayer/IntelHda/IntelHdaLib.h create mode 100644 AlsaSound/include/AlsaMixerHal.h create mode 100644 htdocs/AFB-websock.js create mode 100644 htdocs/AudioBinding.js delete mode 100644 htdocs/websock.js diff --git a/Alsa/CMakeLists.txt b/Alsa/CMakeLists.txt deleted file mode 100644 index e8173f9..0000000 --- a/Alsa/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -########################################################################### -# 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. -########################################################################### - -PROJECT(alsa-binding C) -PKG_CHECK_MODULES(alsa REQUIRED alsa) - -# Max Sound Card Number eligible to ctlevent subscription -defstr(MAX_SND_CARD 16) - -SET(link_libraries - ${alsa_LIBRARIES} - ${link_libraries} - ) - -ADD_SUBDIRECTORY(core-binding) - diff --git a/Alsa/core-binding/AlsaAfbBinding.c b/Alsa/core-binding/AlsaAfbBinding.c deleted file mode 100644 index 95d5e8d..0000000 --- a/Alsa/core-binding/AlsaAfbBinding.c +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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 "AlsaLibMapping.h" -#include - -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= "getinfo", .session= AFB_SESSION_NONE, .callback= alsaGetInfo, .info= "List All/One Sound Cards Info" }, - { .name= "getctl", .session= AFB_SESSION_NONE, .callback= alsaGetCtl, .info= "List All/One Controls from selected sndcard" }, - { .name= "subctl", .session= AFB_SESSION_NONE, .callback= alsaSubCtl, .info= "Subscribe to events from selected sndcard" }, - { .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= "alsacore", - .info= "Low Level Interface to Alsa Sound Lib", - .verbs = binding_verbs - } -}; - -extern int afbBindingV1ServiceInit(struct afb_service service) { - // this is call when after all bindings are loaded - return (0); -}; - -/* - * activation function for registering the binding called by afb-daemon - */ -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/Alsa/core-binding/AlsaLibMapping.c b/Alsa/core-binding/AlsaLibMapping.c deleted file mode 100644 index 59ce8ff..0000000 --- a/Alsa/core-binding/AlsaLibMapping.c +++ /dev/null @@ -1,728 +0,0 @@ -/* - * AlsaLibMapping -- provide low level interface with ALSA 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. - - References: - https://github.com/fulup-bzh/AlsaJsonGateway (original code) - http://alsa-lib.sourcearchive.com/documentation/1.0.20/modules.html - http://alsa-lib.sourcearchive.com/documentation/1.0.8/group__Control_gd48d44da8e3bfe150e928267008b8ff5.html - http://alsa.opensrc.org/HowTo_access_a_mixer_control - https://github.com/gch1p/alsa-volume-monitor/blob/master/main.c - -*/ - -#define _GNU_SOURCE // needed for vasprintf - -#include -#include "AlsaLibMapping.h" - -#include - -// generic structure to pass parsed query values -typedef struct { - const char *devid; - int numid; - int quiet; -} queryValuesT; - -// generic sndctrl event handle hook to event callback when pooling -typedef struct { - struct pollfd pfds; - sd_event_source *src; - snd_ctl_t *ctl; - struct afb_event afbevt; -} evtHandleT; - -typedef struct { - int cardid; - int ucount; - evtHandleT *evtHandle; -} sndHandleT; - - - - -STATIC json_object *DB2StringJsonOject (long dB) { - char label [20]; - if (dB < 0) { - snprintf(label, sizeof(label), "-%li.%02lidB", -dB / 100, -dB % 100); - } else { - snprintf(label, sizeof(label), "%li.%02lidB", dB / 100, dB % 100); - } - - // json function takes care of string copy - return (json_object_new_string (label)); -} - - -// Direct port from amixer TLV decode routine. This code is too complex for me. -// I hopefully did not break it when porting it. - -STATIC json_object *decodeTlv(unsigned int *tlv, unsigned int tlv_size) { - char label[20]; - unsigned int type = tlv[0]; - unsigned int size; - unsigned int idx = 0; - const char *chmap_type = NULL; - json_object * decodeTlvJson = json_object_new_object(); - - if (tlv_size < (unsigned int) (2 * sizeof (unsigned int))) { - printf("TLV size error!\n"); - return NULL; - } - type = tlv[idx++]; - size = tlv[idx++]; - tlv_size -= (unsigned int) (2 * sizeof (unsigned int)); - if (size > tlv_size) { - fprintf(stderr, "TLV size error (%i, %i, %i)!\n", type, size, tlv_size); - return NULL; - } - switch (type) { - - case SND_CTL_TLVT_CONTAINER: - { - json_object * containerJson = json_object_new_array(); - - size += (unsigned int) (sizeof (unsigned int) - 1); - size /= (unsigned int) (sizeof (unsigned int)); - while (idx < size) { - json_object *embedJson; - - if (tlv[idx + 1] > (size - idx) * sizeof (unsigned int)) { - fprintf(stderr, "TLV size error in compound!\n"); - return NULL; - } - embedJson = decodeTlv(tlv + idx, tlv[idx + 1] + 8); - json_object_array_add(containerJson, embedJson); - idx += (unsigned int) (2 + (tlv[idx + 1] + sizeof (unsigned int) - 1) / sizeof (unsigned int)); - } - json_object_object_add(decodeTlvJson, "container", containerJson); - break; - } - - case SND_CTL_TLVT_DB_SCALE: - { - json_object * dbscaleJson = json_object_new_object(); - - if (size != 2 * sizeof (unsigned int)) { - json_object * arrayJson = json_object_new_array(); - while (size > 0) { - snprintf(label, sizeof (label), "0x%08x,", tlv[idx++]); - json_object_array_add(arrayJson, json_object_new_string(label)); - size -= (unsigned int) sizeof (unsigned int); - } - json_object_object_add(dbscaleJson, "array", arrayJson); - } else { - json_object_object_add(dbscaleJson, "min", DB2StringJsonOject((int) tlv[2])); - json_object_object_add(dbscaleJson, "step", DB2StringJsonOject(tlv[3] & 0xffff)); - json_object_object_add(dbscaleJson, "mute", DB2StringJsonOject((tlv[3] >> 16) & 1)); - } - json_object_object_add(decodeTlvJson, "dbscale", dbscaleJson); - break; - } - -#ifdef SND_CTL_TLVT_DB_LINEAR - case SND_CTL_TLVT_DB_LINEAR: - { - json_object * dbLinearJson = json_object_new_object(); - - if (size != 2 * sizeof (unsigned int)) { - json_object * arrayJson = json_object_new_array(); - while (size > 0) { - snprintf(label, sizeof (label), "0x%08x,", tlv[idx++]); - json_object_array_add(arrayJson, json_object_new_string(label)); - size -= (unsigned int) sizeof (unsigned int); - } - json_object_object_add(dbLinearJson, "offset", arrayJson); - } else { - json_object_object_add(dbLinearJson, "min", DB2StringJsonOject((int) tlv[2])); - json_object_object_add(dbLinearJson, "max", DB2StringJsonOject((int) tlv[3])); - } - json_object_object_add(decodeTlvJson, "dblinear", dbLinearJson); - break; - } -#endif - -#ifdef SND_CTL_TLVT_DB_RANGE - case SND_CTL_TLVT_DB_RANGE: - { - json_object *dbRangeJson = json_object_new_object(); - - if ((size % (6 * sizeof (unsigned int))) != 0) { - json_object *arrayJson = json_object_new_array(); - while (size > 0) { - snprintf(label, sizeof (label), "0x%08x,", tlv[idx++]); - json_object_array_add(arrayJson, json_object_new_string(label)); - size -= (unsigned int) sizeof (unsigned int); - } - json_object_object_add(dbRangeJson, "dbrange", arrayJson); - break; - } - while (size > 0) { - json_object * embedJson = json_object_new_object(); - snprintf(label, sizeof (label), "%i,", tlv[idx++]); - json_object_object_add(embedJson, "rangemin", json_object_new_string(label)); - snprintf(label, sizeof (label), "%i", tlv[idx++]); - json_object_object_add(embedJson, "rangemax", json_object_new_string(label)); - embedJson = decodeTlv(tlv + idx, 4 * sizeof (unsigned int)); - json_object_object_add(embedJson, "tlv", embedJson); - idx += 4; - size -= (unsigned int) (6 * sizeof (unsigned int)); - json_object_array_add(dbRangeJson, embedJson); - } - json_object_object_add(decodeTlvJson, "dbrange", dbRangeJson); - break; - } -#endif -#ifdef SND_CTL_TLVT_DB_MINMAX - case SND_CTL_TLVT_DB_MINMAX: - case SND_CTL_TLVT_DB_MINMAX_MUTE: - { - json_object * dbMinMaxJson = json_object_new_object(); - - if (size != 2 * sizeof (unsigned int)) { - json_object * arrayJson = json_object_new_array(); - while (size > 0) { - snprintf(label, sizeof (label), "0x%08x,", tlv[idx++]); - json_object_array_add(arrayJson, json_object_new_string(label)); - size -= (unsigned int) sizeof (unsigned int); - } - json_object_object_add(dbMinMaxJson, "array", arrayJson); - - } else { - json_object_object_add(dbMinMaxJson, "min", DB2StringJsonOject((int) tlv[2])); - json_object_object_add(dbMinMaxJson, "max", DB2StringJsonOject((int) tlv[3])); - } - - if (type == SND_CTL_TLVT_DB_MINMAX_MUTE) { - json_object_object_add(decodeTlvJson, "dbminmaxmute", dbMinMaxJson); - } else { - json_object_object_add(decodeTlvJson, "dbminmax", dbMinMaxJson); - } - break; - } -#endif -#ifdef SND_CTL_TLVT_CHMAP_FIXED - case SND_CTL_TLVT_CHMAP_FIXED: - chmap_type = "fixed"; - /* Fall through */ - case SND_CTL_TLVT_CHMAP_VAR: - if (!chmap_type) - chmap_type = "variable"; - /* Fall through */ - case SND_CTL_TLVT_CHMAP_PAIRED: - if (!chmap_type) - chmap_type = "paired"; - - json_object * chmapJson = json_object_new_object(); - json_object * arrayJson = json_object_new_array(); - - while (size > 0) { - snprintf(label, sizeof (label), "%s", snd_pcm_chmap_name(tlv[idx++])); - size -= (unsigned int) sizeof (unsigned int); - json_object_array_add(arrayJson, json_object_new_string(label)); - } - json_object_object_add(chmapJson, chmap_type, arrayJson); - json_object_object_add(decodeTlvJson, "chmap", chmapJson); - break; -#endif - default: - { - printf("unk-%i-", type); - json_object * arrayJson = json_object_new_array(); - - while (size > 0) { - snprintf(label, sizeof (label), "0x%08x,", tlv[idx++]); - size -= (unsigned int) sizeof (unsigned int); - json_object_array_add(arrayJson, json_object_new_string(label)); - } - break; - json_object_object_add(decodeTlvJson, "unknown", arrayJson); - } - } - - return (decodeTlvJson); -} - - -// retreive info for one given card -STATIC json_object* alsaCardProbe (const char *rqtSndId) { - const char *info, *name; - const char *devid, *driver; - json_object *sndcard; - snd_ctl_t *handle; - snd_ctl_card_info_t *cardinfo; - int err; - - - snd_ctl_card_info_alloca(&cardinfo); - - if ((err = snd_ctl_open(&handle, rqtSndId, 0)) < 0) { - INFO (afbIface, "SndCard [%s] Not Found", rqtSndId); - return NULL; - } - - if ((err = snd_ctl_card_info(handle, cardinfo)) < 0) { - snd_ctl_close(handle); - WARNING (afbIface, "SndCard [%s] info error: %s", rqtSndId, snd_strerror(err)); - return NULL; - } - - // start a new json object to store card info - sndcard = json_object_new_object(); - - devid= snd_ctl_card_info_get_id(cardinfo); - json_object_object_add (sndcard, "devid" , json_object_new_string(devid)); - name = snd_ctl_card_info_get_name(cardinfo); - json_object_object_add (sndcard, "name", json_object_new_string (name)); - - if (afbIface->verbosity > 1) { - json_object_object_add (sndcard, "devid", json_object_new_string(rqtSndId)); - driver= snd_ctl_card_info_get_driver(cardinfo); - json_object_object_add (sndcard, "driver" , json_object_new_string(driver)); - info = strdup(snd_ctl_card_info_get_longname (cardinfo)); - json_object_object_add (sndcard, "info" , json_object_new_string (info)); - INFO (afbIface, "AJG: Soundcard Devid=%-5s Cardid=%-7s Name=%s\n", rqtSndId, devid, info); - } - - // free card handle and return info - snd_ctl_close(handle); - return (sndcard); -} - -// Loop on every potential Sound card and register active one -PUBLIC void alsaGetInfo (struct afb_req request) { - int card; - json_object *sndcard, *sndcards; - char devid[32]; - - const char *rqtSndId = afb_req_value(request, "devid"); - - // if no specific card requested loop on all - - if (rqtSndId != NULL) { - // only one card was requested let's probe it - sndcard = alsaCardProbe (rqtSndId); - if (sndcard != NULL) afb_req_success(request, sndcard, NULL); - else afb_req_fail_f (request, "SndCard [%s] Not Found", rqtSndId); - - } else { - // return an array of sndcard - sndcards =json_object_new_array(); - - // loop on potential card number - for (card =0; card < 32; card++) { - - // build card devid and probe it - snprintf (devid, sizeof(devid), "hw:%i", card); - sndcard = alsaCardProbe (devid); - - // Alsa has hole within card list [ignore them] - if (sndcard != NULL) { - // add current sndcard to sndcards object - json_object_array_add (sndcards, sndcard); - } - } - afb_req_success (request, sndcards, NULL); - } -} - -// pack Alsa element's ACL into a JSON object -STATIC json_object *getControlAcl (snd_ctl_elem_info_t *info) { - - json_object * jsonAclCtl = json_object_new_object(); - - json_object_object_add (jsonAclCtl, "read" , json_object_new_boolean(snd_ctl_elem_info_is_readable(info))); - json_object_object_add (jsonAclCtl, "write" , json_object_new_boolean(snd_ctl_elem_info_is_writable(info))); - json_object_object_add (jsonAclCtl, "inact" , json_object_new_boolean(snd_ctl_elem_info_is_inactive(info))); - json_object_object_add (jsonAclCtl, "volat" , json_object_new_boolean(snd_ctl_elem_info_is_volatile(info))); - json_object_object_add (jsonAclCtl, "lock" , json_object_new_boolean(snd_ctl_elem_info_is_locked(info))); - - // if TLV is readable we insert its ACL - if (!snd_ctl_elem_info_is_tlv_readable(info)) { - json_object * jsonTlv = json_object_new_object(); - - json_object_object_add (jsonTlv, "read" , json_object_new_boolean(snd_ctl_elem_info_is_tlv_readable(info))); - json_object_object_add (jsonTlv, "write" , json_object_new_boolean(snd_ctl_elem_info_is_tlv_writable(info))); - json_object_object_add (jsonTlv, "command", json_object_new_boolean(snd_ctl_elem_info_is_tlv_commandable(info))); - - json_object_object_add (jsonAclCtl, "tlv", jsonTlv); - } - return (jsonAclCtl); -} - - - -// pack element from ALSA control into a JSON object -STATIC json_object * alsaGetSingleCtl (snd_hctl_elem_t *elem, snd_ctl_elem_info_t *info, queryValuesT *queryValues) { - - int err; - json_object *jsonAlsaCtl,*jsonClassCtl; - snd_ctl_elem_id_t *elemid; - snd_ctl_elem_type_t elemtype; - snd_ctl_elem_value_t *control; - int count, idx; - - // allocate ram for ALSA elements - snd_ctl_elem_value_alloca(&control); - snd_ctl_elem_id_alloca (&elemid); - - // get elemId out of elem - snd_hctl_elem_get_id(elem, elemid); - - // when ctrlid is set, return only this ctrl - if (queryValues->numid != -1 && queryValues->numid != snd_ctl_elem_id_get_numid(elemid)) return NULL; - - // build a json object out of element - jsonAlsaCtl = json_object_new_object(); // http://alsa-lib.sourcearchive.com/documentation/1.0.24.1-3/group__Control_ga4e4f251147f558bc2ad044e836e449d9.html - json_object_object_add (jsonAlsaCtl,"numid", json_object_new_int(snd_ctl_elem_id_get_numid (elemid))); - if (queryValues->quiet < 2) json_object_object_add (jsonAlsaCtl,"name" , json_object_new_string(snd_ctl_elem_id_get_name (elemid))); - if (queryValues->quiet < 1) json_object_object_add (jsonAlsaCtl,"iface", json_object_new_string(snd_ctl_elem_iface_name(snd_ctl_elem_id_get_interface(elemid)))); - if (queryValues->quiet < 3)json_object_object_add (jsonAlsaCtl,"actif", json_object_new_boolean(!snd_ctl_elem_info_is_inactive(info))); - - elemtype = snd_ctl_elem_info_get_type(info); - - // number item and value(s) within control. - count = snd_ctl_elem_info_get_count (info); - - if (snd_ctl_elem_info_is_readable(info)) { - json_object *jsonValuesCtl = json_object_new_array(); - - if ((err = snd_hctl_elem_read(elem, control)) < 0) { - json_object *jsonValuesCtl = json_object_new_object(); - json_object_object_add (jsonValuesCtl,"error", json_object_new_string(snd_strerror(err))); - } else { - for (idx = 0; idx < count; idx++) { // start from one in amixer.c !!! - switch (elemtype) { - case SND_CTL_ELEM_TYPE_BOOLEAN: { - json_object_array_add (jsonValuesCtl, json_object_new_boolean (snd_ctl_elem_value_get_boolean(control, idx))); - break; - } - case SND_CTL_ELEM_TYPE_INTEGER: - json_object_array_add (jsonValuesCtl, json_object_new_int ((int)snd_ctl_elem_value_get_integer(control, idx))); - break; - case SND_CTL_ELEM_TYPE_INTEGER64: - json_object_array_add (jsonValuesCtl, json_object_new_int64 (snd_ctl_elem_value_get_integer64(control, idx))); - break; - case SND_CTL_ELEM_TYPE_ENUMERATED: - json_object_array_add (jsonValuesCtl, json_object_new_int (snd_ctl_elem_value_get_enumerated(control, idx))); - break; - case SND_CTL_ELEM_TYPE_BYTES: - json_object_array_add (jsonValuesCtl, json_object_new_int ((int)snd_ctl_elem_value_get_byte(control, idx))); - break; - case SND_CTL_ELEM_TYPE_IEC958: { - json_object *jsonIec958Ctl = json_object_new_object(); - snd_aes_iec958_t iec958; - snd_ctl_elem_value_get_iec958(control, &iec958); - - json_object_object_add (jsonIec958Ctl,"AES0",json_object_new_int(iec958.status[0])); - json_object_object_add (jsonIec958Ctl,"AES1",json_object_new_int(iec958.status[1])); - json_object_object_add (jsonIec958Ctl,"AES2",json_object_new_int(iec958.status[2])); - json_object_object_add (jsonIec958Ctl,"AES3",json_object_new_int(iec958.status[3])); - json_object_array_add (jsonValuesCtl, jsonIec958Ctl); - break; - } - default: - json_object_array_add (jsonValuesCtl, json_object_new_string ("?unknown?")); - break; - } - } - } - json_object_object_add (jsonAlsaCtl,"value",jsonValuesCtl); - } - - - if (!queryValues->quiet) { // in simple mode do not print usable values - jsonClassCtl = json_object_new_object(); - json_object_object_add (jsonClassCtl,"type" , json_object_new_string(snd_ctl_elem_type_name(elemtype))); - json_object_object_add (jsonClassCtl,"count", json_object_new_int(count)); - - switch (elemtype) { - case SND_CTL_ELEM_TYPE_INTEGER: - json_object_object_add (jsonClassCtl,"min", json_object_new_int((int)snd_ctl_elem_info_get_min(info))); - json_object_object_add (jsonClassCtl,"max", json_object_new_int((int)snd_ctl_elem_info_get_max(info))); - json_object_object_add (jsonClassCtl,"step", json_object_new_int((int)snd_ctl_elem_info_get_step(info))); - break; - case SND_CTL_ELEM_TYPE_INTEGER64: - json_object_object_add (jsonClassCtl,"min", json_object_new_int64(snd_ctl_elem_info_get_min64(info))); - json_object_object_add (jsonClassCtl,"max", json_object_new_int64(snd_ctl_elem_info_get_max64(info))); - json_object_object_add (jsonClassCtl,"step", json_object_new_int64(snd_ctl_elem_info_get_step64(info))); - break; - case SND_CTL_ELEM_TYPE_ENUMERATED: { - unsigned int item, items = snd_ctl_elem_info_get_items(info); - json_object *jsonEnum = json_object_new_array(); - - for (item = 0; item < items; item++) { - snd_ctl_elem_info_set_item(info, item); - if ((err = snd_hctl_elem_info(elem, info)) >= 0) { - json_object_array_add (jsonEnum, json_object_new_string(snd_ctl_elem_info_get_item_name(info))); - } - } - json_object_object_add (jsonClassCtl, "enums",jsonEnum); - break; - } - default: break; // ignore any unknown type - } - - // add collected class info with associated ACLs - json_object_object_add (jsonAlsaCtl,"ctrl", jsonClassCtl); - json_object_object_add (jsonAlsaCtl,"acl" , getControlAcl (info)); - - // check for tlv [direct port from amixer.c] - if (snd_ctl_elem_info_is_tlv_readable(info)) { - unsigned int *tlv; - tlv = malloc(4096); - if ((err = snd_hctl_elem_tlv_read(elem, tlv, 4096)) < 0) { - fprintf (stderr, "Control %s element TLV read error\n", snd_strerror(err)); - free(tlv); - } else { - json_object_object_add (jsonAlsaCtl,"tlv", decodeTlv (tlv, 4096)); - } - } - } - return (jsonAlsaCtl); -} - - -PUBLIC void alsaGetCtl(struct afb_req request) { - int err = 0; - snd_hctl_t *handle; - snd_hctl_elem_t *elem; - snd_ctl_elem_info_t *info; - json_object *sndctrls, *control; - queryValuesT queryValues; - - // get API params - queryValues.devid = afb_req_value(request, "devid"); - - const char *rqtNumid = afb_req_value(request, "numid"); - if (!rqtNumid) queryValues.numid=-1; - else if (rqtNumid && ! sscanf (rqtNumid, "%d", &queryValues.numid)) { - json_object *query = afb_req_json(request); - - afb_req_fail_f (request, "Query=%s NumID not integer &numid=%s&", json_object_to_json_string(query), rqtNumid); - goto ExitOnError; - }; - - const char *rqtQuiet = afb_req_value(request, "quiet"); - if (!rqtQuiet) queryValues.quiet=99; // default super quiet - else if (rqtQuiet && ! sscanf (rqtQuiet, "%d", &queryValues.quiet)) { - json_object *query = afb_req_json(request); - - afb_req_fail_f (request, "Query=%s NumID not integer &numid=%s&", json_object_to_json_string(query), rqtQuiet); - goto ExitOnError; - }; - - // Open sound we use Alsa high level API like amixer.c - if (!queryValues.devid || (err = snd_hctl_open(&handle, queryValues.devid, 0)) < 0) { - afb_req_fail_f (request, "alsaGetControl devid=[%s] open fail error=%s\n", queryValues.devid, snd_strerror(err)); - goto ExitOnError; - } - - if ((err = snd_hctl_load(handle)) < 0) { - afb_req_fail_f (request, "alsaGetControl devid=[%s] load fail error=%s\n", queryValues.devid, snd_strerror(err)); - goto ExitOnError; - } - - // allocate ram for ALSA elements - snd_ctl_elem_info_alloca(&info); - - // create an json array to hold all sndcard response - sndctrls = json_object_new_array(); - - for (elem = snd_hctl_first_elem(handle); elem != NULL; elem = snd_hctl_elem_next(elem)) { - - if ((err = snd_hctl_elem_info(elem, info)) < 0) { - json_object_put(sndctrls); // we abandon request let's free response - afb_req_fail_f (request, "alsaGetControl devid=[%s/%s] snd_hctl_elem_info error: %s\n" - , queryValues.devid, snd_hctl_name(handle), snd_strerror(err)); - goto ExitOnError; - } - - // each control is added into a JSON array - control = alsaGetSingleCtl(elem, info, &queryValues); - if (control) json_object_array_add(sndctrls, control); - - } - - afb_req_success (request, sndctrls, NULL); - return; - - // nothing special only for debugger breakpoint - ExitOnError: - return; -} - -// This routine is called when ALSA event are fired -STATIC int sndCtlEventCB (sd_event_source* src, int fd, uint32_t revents, void* userData) { - int err; - evtHandleT *evtHandle = (evtHandleT*)userData; - snd_ctl_event_t *ctlEvent; - json_object *ctlEventJson; - unsigned int mask; - int numid; - int iface; - int device; - int subdev; - const char*devname; - int index; - - if ((revents & EPOLLHUP) != 0) { - NOTICE (afbIface, "SndCtl hanghup [car disconnected]"); - goto ExitOnSucess; - } - - if ((revents & EPOLLIN) != 0) { - - snd_ctl_event_alloca(&ctlEvent); // initialise event structure on stack - - err = snd_ctl_read(evtHandle->ctl, ctlEvent); - if (err < 0) goto ExitOnError; - - // we only process sndctrl element - if (snd_ctl_event_get_type(ctlEvent) != SND_CTL_EVENT_ELEM) goto ExitOnSucess; - - // we only process value changed events - mask = snd_ctl_event_elem_get_mask(ctlEvent); - if (!(mask & SND_CTL_EVENT_MASK_VALUE)) goto ExitOnSucess; - - numid = snd_ctl_event_elem_get_numid(ctlEvent); - iface = snd_ctl_event_elem_get_interface(ctlEvent); - device = snd_ctl_event_elem_get_device(ctlEvent); - subdev = snd_ctl_event_elem_get_subdevice(ctlEvent); - devname= snd_ctl_event_elem_get_name(ctlEvent); - index = snd_ctl_event_elem_get_index(ctlEvent); - - DEBUG(afbIface, "sndCtlEventCB: (%i,%i,%i,%i,%s,%i)", numid, iface, device, subdev, devname, index); - - // proxy ctlevent as a binder event - ctlEventJson = json_object_new_object(); - json_object_object_add(ctlEventJson, "numid" ,json_object_new_int (numid)); - json_object_object_add(ctlEventJson, "iface" ,json_object_new_int (iface)); - json_object_object_add(ctlEventJson, "device" ,json_object_new_int (device)); - json_object_object_add(ctlEventJson, "subdev" ,json_object_new_int (subdev)); - json_object_object_add(ctlEventJson, "devname",json_object_new_string (devname)); - json_object_object_add(ctlEventJson, "index" ,json_object_new_int (index)); - afb_event_push(evtHandle->afbevt, ctlEventJson); - } - - ExitOnSucess: - return 0; - - ExitOnError: - WARNING (afbIface, "sndCtlEventCB: ignored unsupported event type"); - return (0); -} - -// Loop on every potential Sound card and register active one -PUBLIC void alsaSubCtl (struct afb_req request) { - static sndHandleT sndHandles[MAX_SND_CARD]; - evtHandleT *evtHandle; - snd_ctl_t *ctlHandle; - snd_ctl_card_info_t *card_info; - int err, idx, cardid; - int idxFree=-1; - - const char *devid = afb_req_value(request, "devid"); - if (devid == NULL) { - afb_req_fail_f (request, "devid-missing", "devid=hw:xxx missing"); - goto ExitOnError; - } - - // open control interface for devid - err = snd_ctl_open(&ctlHandle, devid, SND_CTL_READONLY); - if (err < 0) { - afb_req_fail_f (request, "devid-unknown", "SndCard devid=%s Not Found err=%d", devid, err); - goto ExitOnError; - } - - // get sound card index use to search existing subscription - snd_ctl_card_info_alloca(&card_info); - snd_ctl_card_info(ctlHandle, card_info); - cardid = snd_ctl_card_info_get_card(card_info); - - // search for an existing subscription and mark 1st free slot - for (idx= 0; idx < MAX_SND_CARD; idx ++) { - if (sndHandles[idx].ucount > 0 && sndHandles[idx].cardid == cardid) { - evtHandle= sndHandles[idx].evtHandle; - break; - } else if (idxFree == -1) idxFree= idx; - }; - - // if not subscription exist for the event let's create one - if (idx == MAX_SND_CARD) { - - // reach MAX_SND_CARD event registration - if (idxFree == -1) { - afb_req_fail_f (request, "register-toomany", "Cannot register new event Maxcard==%devent name=%s", idx); - snd_ctl_close(ctlHandle); - goto ExitOnError; - } - - evtHandle = malloc (sizeof(evtHandleT)); - evtHandle->ctl = ctlHandle; - sndHandles[idxFree].ucount = 0; - sndHandles[idxFree].cardid = cardid; - - // subscribe for sndctl events attached to devid - err = snd_ctl_subscribe_events(evtHandle->ctl, 1); - if (err < 0) { - afb_req_fail_f (request, "subscribe-fail", "Cannot subscribe events from devid=%s err=%d", devid, err); - snd_ctl_close(ctlHandle); - goto ExitOnError; - } - - // get pollfd attach to this sound board - snd_ctl_poll_descriptors(evtHandle->ctl, &evtHandle->pfds, 1); - - // register sound event to binder main loop - err = sd_event_add_io(afb_daemon_get_event_loop(afbIface->daemon), &evtHandle->src, evtHandle->pfds.fd, EPOLLIN, sndCtlEventCB, evtHandle); - if (err < 0) { - afb_req_fail_f (request, "register-mainloop", "Cannot hook events to mainloop devid=%s err=%d", devid, err); - snd_ctl_close(ctlHandle); - goto ExitOnError; - } - - // create binder event attached to devid name - evtHandle->afbevt = afb_daemon_make_event (afbIface->daemon, devid); - if (!afb_event_is_valid (evtHandle->afbevt)) { - afb_req_fail_f (request, "register-event", "Cannot register new binder event name=%s", devid); - snd_ctl_close(ctlHandle); - goto ExitOnError; - } - - // everything looks OK let's move forward - idx=idxFree; - } - - // subscribe to binder event - err = afb_req_subscribe(request, evtHandle->afbevt); - if (err != 0) { - afb_req_fail_f (request, "register-eventname", "Cannot subscribe binder event name=%s [invalid channel]", devid, err); - goto ExitOnError; - } - - json_object *ctlEventJson = json_object_new_object(); - json_object_object_add(ctlEventJson, "test",json_object_new_string ("done")); - afb_event_push(evtHandle->afbevt, ctlEventJson ); - - // increase usage count and return success - sndHandles[idx].ucount ++; - afb_req_success(request, NULL, NULL); - return; - - ExitOnError: - return; -} diff --git a/Alsa/core-binding/AlsaLibMapping.h b/Alsa/core-binding/AlsaLibMapping.h deleted file mode 100644 index 5fc1b98..0000000 --- a/Alsa/core-binding/AlsaLibMapping.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * AlsaLibMapping -- provide low level interface with ALSA 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 ALSALIBMAPPING_H -#define ALSALIBMAPPING_H - -#include -#include - - -// import from AlsaAfbBinding -extern const struct afb_binding_interface *afbIface; - -// import from AlsaAfbMapping -PUBLIC void alsaGetInfo (struct afb_req request); -PUBLIC void alsaGetCtl(struct afb_req request); -PUBLIC void alsaSubCtl (struct afb_req request); - - -#endif /* ALSALIBMAPPING_H */ - diff --git a/Alsa/core-binding/CMakeLists.txt b/Alsa/core-binding/CMakeLists.txt deleted file mode 100644 index 0475e72..0000000 --- a/Alsa/core-binding/CMakeLists.txt +++ /dev/null @@ -1,36 +0,0 @@ -########################################################################### -# 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}) - -################################################## -# AlsaBinding -################################################## -ADD_LIBRARY(alsacore-afb MODULE AlsaAfbBinding.c AlsaLibMapping.c) - -SET_TARGET_PROPERTIES(alsacore-afb PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_SOURCE_DIR}/export.map" -) - -TARGET_LINK_LIBRARIES(alsacore-afb ${link_libraries}) -INSTALL(TARGETS alsacore-afb - LIBRARY DESTINATION ${binding_install_dir}) - - diff --git a/Alsa/core-binding/README.md b/Alsa/core-binding/README.md deleted file mode 100644 index 5f0c562..0000000 --- a/Alsa/core-binding/README.md +++ /dev/null @@ -1,20 +0,0 @@ ------------------------------------------------------------------------- - AlsaCore Low level binding maps AlsaLib APIs ------------------------------------------------------------------------- - -Testing: (from project directory bindings) - * start binder: ~/opt/bin/afb-daemon --ldpaths=./build --roothttp=htdocs - * connect browser on http://localhost:1234 - - # List Avaliable Sound cards - http://localhost:1234/api/alsacore/getinfo - - # Get Info on a given Sound Card - http://localhost:1234/api/alsacore/getinfo?devid=hw:0 - - # Get all controls from a given sound card - http://localhost:1234/api/alsacore/getctl?devid=hw:0 - - # Get detail on a given control (optional quiet=0=verbose,1,2) - http://localhost:1234/api/alsacore/getctl?devid=hw:0&numid=1&quiet=0 - diff --git a/AlsaSound/CMakeLists.txt b/AlsaSound/CMakeLists.txt new file mode 100644 index 0000000..7b1d5e9 --- /dev/null +++ b/AlsaSound/CMakeLists.txt @@ -0,0 +1,32 @@ +########################################################################### +# 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. +########################################################################### + +PROJECT(alsa-bindings C) +PKG_CHECK_MODULES(alsa REQUIRED alsa) + +# Max Sound Card Number eligible to ctlevent subscription +defstr(MAX_SND_CARD 16) + +SET(link_libraries + ${alsa_LIBRARIES} + ${link_libraries} + ) + +ADD_SUBDIRECTORY(CoreBinding) +ADD_SUBDIRECTORY(HardAbsLayer) + diff --git a/AlsaSound/CoreBinding/AlsaAfbBinding.c b/AlsaSound/CoreBinding/AlsaAfbBinding.c new file mode 100644 index 0000000..2c5c789 --- /dev/null +++ b/AlsaSound/CoreBinding/AlsaAfbBinding.c @@ -0,0 +1,78 @@ +/* + * 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 "AlsaLibMapping.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= "getinfo", .session= AFB_SESSION_NONE, .callback= alsaGetInfo, .info= "List All/One Sound Cards Info" }, + { .name= "getctl", .session= AFB_SESSION_NONE, .callback= alsaGetCtl, .info= "List All/One Controls from selected sndcard" }, + { .name= "subscribe", .session= AFB_SESSION_NONE, .callback= alsaSubcribe, .info= "Subscribe to events from selected sndcard" }, + { .name= "getcardid", .session= AFB_SESSION_NONE, .callback= alsaGetCardId,.info= "Get CardId from its short/long name" }, + { .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= "alsacore", + .info= "Low Level Interface to Alsa Sound Lib", + .verbs = binding_verbs + } +}; + +extern int afbBindingV1ServiceInit(struct afb_service service) { + // this is call when after all bindings are loaded + alsaLibInit (service); // AlsaBinding check for sound card at installation time + return (0); +}; + +/* + * activation function for registering the binding called by afb-daemon + */ +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/AlsaSound/CoreBinding/AlsaLibMapping.c b/AlsaSound/CoreBinding/AlsaLibMapping.c new file mode 100644 index 0000000..8dc1772 --- /dev/null +++ b/AlsaSound/CoreBinding/AlsaLibMapping.c @@ -0,0 +1,787 @@ +/* + * AlsaLibMapping -- provide low level interface with ALSA 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. + + References: + https://github.com/fulup-bzh/AlsaJsonGateway (original code) + http://alsa-lib.sourcearchive.com/documentation/1.0.20/modules.html + http://alsa-lib.sourcearchive.com/documentation/1.0.8/group__Control_gd48d44da8e3bfe150e928267008b8ff5.html + http://alsa.opensrc.org/HowTo_access_a_mixer_control + https://github.com/gch1p/alsa-volume-monitor/blob/master/main.c + +*/ + +#define _GNU_SOURCE // needed for vasprintf + +#include +#include "AlsaLibMapping.h" + +#include +static struct afb_service srvitf; + +// generic structure to pass parsed query values +typedef struct { + const char *devid; + int numid; + int quiet; +} queryValuesT; + +// generic sndctrl event handle hook to event callback when pooling +typedef struct { + struct pollfd pfds; + sd_event_source *src; + snd_ctl_t *ctl; + struct afb_event afbevt; +} evtHandleT; + +typedef struct { + int cardid; + int ucount; + evtHandleT *evtHandle; +} sndHandleT; + +STATIC json_object *DB2StringJsonOject (long dB) { + char label [20]; + if (dB < 0) { + snprintf(label, sizeof(label), "-%li.%02lidB", -dB / 100, -dB % 100); + } else { + snprintf(label, sizeof(label), "%li.%02lidB", dB / 100, dB % 100); + } + + // json function takes care of string copy + return (json_object_new_string (label)); +} + + +// Direct port from amixer TLV decode routine. This code is too complex for me. +// I hopefully did not break it when porting it. + +STATIC json_object *decodeTlv(unsigned int *tlv, unsigned int tlv_size) { + char label[20]; + unsigned int type = tlv[0]; + unsigned int size; + unsigned int idx = 0; + const char *chmap_type = NULL; + json_object * decodeTlvJson = json_object_new_object(); + + if (tlv_size < (unsigned int) (2 * sizeof (unsigned int))) { + printf("TLV size error!\n"); + return NULL; + } + type = tlv[idx++]; + size = tlv[idx++]; + tlv_size -= (unsigned int) (2 * sizeof (unsigned int)); + if (size > tlv_size) { + fprintf(stderr, "TLV size error (%i, %i, %i)!\n", type, size, tlv_size); + return NULL; + } + switch (type) { + + case SND_CTL_TLVT_CONTAINER: + { + json_object * containerJson = json_object_new_array(); + + size += (unsigned int) (sizeof (unsigned int) - 1); + size /= (unsigned int) (sizeof (unsigned int)); + while (idx < size) { + json_object *embedJson; + + if (tlv[idx + 1] > (size - idx) * sizeof (unsigned int)) { + fprintf(stderr, "TLV size error in compound!\n"); + return NULL; + } + embedJson = decodeTlv(tlv + idx, tlv[idx + 1] + 8); + json_object_array_add(containerJson, embedJson); + idx += (unsigned int) (2 + (tlv[idx + 1] + sizeof (unsigned int) - 1) / sizeof (unsigned int)); + } + json_object_object_add(decodeTlvJson, "container", containerJson); + break; + } + + case SND_CTL_TLVT_DB_SCALE: + { + json_object * dbscaleJson = json_object_new_object(); + + if (size != 2 * sizeof (unsigned int)) { + json_object * arrayJson = json_object_new_array(); + while (size > 0) { + snprintf(label, sizeof (label), "0x%08x,", tlv[idx++]); + json_object_array_add(arrayJson, json_object_new_string(label)); + size -= (unsigned int) sizeof (unsigned int); + } + json_object_object_add(dbscaleJson, "array", arrayJson); + } else { + json_object_object_add(dbscaleJson, "min", DB2StringJsonOject((int) tlv[2])); + json_object_object_add(dbscaleJson, "step", DB2StringJsonOject(tlv[3] & 0xffff)); + json_object_object_add(dbscaleJson, "mute", DB2StringJsonOject((tlv[3] >> 16) & 1)); + } + json_object_object_add(decodeTlvJson, "dbscale", dbscaleJson); + break; + } + +#ifdef SND_CTL_TLVT_DB_LINEAR + case SND_CTL_TLVT_DB_LINEAR: + { + json_object * dbLinearJson = json_object_new_object(); + + if (size != 2 * sizeof (unsigned int)) { + json_object * arrayJson = json_object_new_array(); + while (size > 0) { + snprintf(label, sizeof (label), "0x%08x,", tlv[idx++]); + json_object_array_add(arrayJson, json_object_new_string(label)); + size -= (unsigned int) sizeof (unsigned int); + } + json_object_object_add(dbLinearJson, "offset", arrayJson); + } else { + json_object_object_add(dbLinearJson, "min", DB2StringJsonOject((int) tlv[2])); + json_object_object_add(dbLinearJson, "max", DB2StringJsonOject((int) tlv[3])); + } + json_object_object_add(decodeTlvJson, "dblinear", dbLinearJson); + break; + } +#endif + +#ifdef SND_CTL_TLVT_DB_RANGE + case SND_CTL_TLVT_DB_RANGE: + { + json_object *dbRangeJson = json_object_new_object(); + + if ((size % (6 * sizeof (unsigned int))) != 0) { + json_object *arrayJson = json_object_new_array(); + while (size > 0) { + snprintf(label, sizeof (label), "0x%08x,", tlv[idx++]); + json_object_array_add(arrayJson, json_object_new_string(label)); + size -= (unsigned int) sizeof (unsigned int); + } + json_object_object_add(dbRangeJson, "dbrange", arrayJson); + break; + } + while (size > 0) { + json_object * embedJson = json_object_new_object(); + snprintf(label, sizeof (label), "%i,", tlv[idx++]); + json_object_object_add(embedJson, "rangemin", json_object_new_string(label)); + snprintf(label, sizeof (label), "%i", tlv[idx++]); + json_object_object_add(embedJson, "rangemax", json_object_new_string(label)); + embedJson = decodeTlv(tlv + idx, 4 * sizeof (unsigned int)); + json_object_object_add(embedJson, "tlv", embedJson); + idx += 4; + size -= (unsigned int) (6 * sizeof (unsigned int)); + json_object_array_add(dbRangeJson, embedJson); + } + json_object_object_add(decodeTlvJson, "dbrange", dbRangeJson); + break; + } +#endif +#ifdef SND_CTL_TLVT_DB_MINMAX + case SND_CTL_TLVT_DB_MINMAX: + case SND_CTL_TLVT_DB_MINMAX_MUTE: + { + json_object * dbMinMaxJson = json_object_new_object(); + + if (size != 2 * sizeof (unsigned int)) { + json_object * arrayJson = json_object_new_array(); + while (size > 0) { + snprintf(label, sizeof (label), "0x%08x,", tlv[idx++]); + json_object_array_add(arrayJson, json_object_new_string(label)); + size -= (unsigned int) sizeof (unsigned int); + } + json_object_object_add(dbMinMaxJson, "array", arrayJson); + + } else { + json_object_object_add(dbMinMaxJson, "min", DB2StringJsonOject((int) tlv[2])); + json_object_object_add(dbMinMaxJson, "max", DB2StringJsonOject((int) tlv[3])); + } + + if (type == SND_CTL_TLVT_DB_MINMAX_MUTE) { + json_object_object_add(decodeTlvJson, "dbminmaxmute", dbMinMaxJson); + } else { + json_object_object_add(decodeTlvJson, "dbminmax", dbMinMaxJson); + } + break; + } +#endif +#ifdef SND_CTL_TLVT_CHMAP_FIXED + case SND_CTL_TLVT_CHMAP_FIXED: + chmap_type = "fixed"; + /* Fall through */ + case SND_CTL_TLVT_CHMAP_VAR: + if (!chmap_type) + chmap_type = "variable"; + /* Fall through */ + case SND_CTL_TLVT_CHMAP_PAIRED: + if (!chmap_type) + chmap_type = "paired"; + + json_object * chmapJson = json_object_new_object(); + json_object * arrayJson = json_object_new_array(); + + while (size > 0) { + snprintf(label, sizeof (label), "%s", snd_pcm_chmap_name(tlv[idx++])); + size -= (unsigned int) sizeof (unsigned int); + json_object_array_add(arrayJson, json_object_new_string(label)); + } + json_object_object_add(chmapJson, chmap_type, arrayJson); + json_object_object_add(decodeTlvJson, "chmap", chmapJson); + break; +#endif + default: + { + printf("unk-%i-", type); + json_object * arrayJson = json_object_new_array(); + + while (size > 0) { + snprintf(label, sizeof (label), "0x%08x,", tlv[idx++]); + size -= (unsigned int) sizeof (unsigned int); + json_object_array_add(arrayJson, json_object_new_string(label)); + } + break; + json_object_object_add(decodeTlvJson, "unknown", arrayJson); + } + } + + return (decodeTlvJson); +} + + +// retreive info for one given card +STATIC json_object* alsaCardProbe (const char *rqtSndId) { + const char *info, *name; + const char *devid, *driver; + json_object *sndcard; + snd_ctl_t *handle; + snd_ctl_card_info_t *cardinfo; + int err; + + + snd_ctl_card_info_alloca(&cardinfo); + + if ((err = snd_ctl_open(&handle, rqtSndId, 0)) < 0) { + INFO (afbIface, "SndCard [%s] Not Found", rqtSndId); + return NULL; + } + + if ((err = snd_ctl_card_info(handle, cardinfo)) < 0) { + snd_ctl_close(handle); + WARNING (afbIface, "SndCard [%s] info error: %s", rqtSndId, snd_strerror(err)); + return NULL; + } + + // start a new json object to store card info + sndcard = json_object_new_object(); + + devid= snd_ctl_card_info_get_id(cardinfo); + json_object_object_add (sndcard, "devid" , json_object_new_string(devid)); + name = snd_ctl_card_info_get_name(cardinfo); + json_object_object_add (sndcard, "name", json_object_new_string (name)); + + if (afbIface->verbosity > 1) { + json_object_object_add (sndcard, "devid", json_object_new_string(rqtSndId)); + driver= snd_ctl_card_info_get_driver(cardinfo); + json_object_object_add (sndcard, "driver" , json_object_new_string(driver)); + info = strdup(snd_ctl_card_info_get_longname (cardinfo)); + json_object_object_add (sndcard, "info" , json_object_new_string (info)); + INFO (afbIface, "AJG: Soundcard Devid=%-5s Cardid=%-7s Name=%s\n", rqtSndId, devid, info); + } + + // free card handle and return info + snd_ctl_close(handle); + return (sndcard); +} + +// Loop on every potential Sound card and register active one +PUBLIC void alsaGetInfo (struct afb_req request) { + int card; + json_object *sndcard, *sndcards; + char devid[32]; + + const char *rqtSndId = afb_req_value(request, "devid"); + + // if no specific card requested loop on all + + if (rqtSndId != NULL) { + // only one card was requested let's probe it + sndcard = alsaCardProbe (rqtSndId); + if (sndcard != NULL) afb_req_success(request, sndcard, NULL); + else afb_req_fail_f (request, "SndCard [%s] Not Found", rqtSndId); + + } else { + // return an array of sndcard + sndcards =json_object_new_array(); + + // loop on potential card number + for (card =0; card < MAX_SND_CARD; card++) { + + // build card devid and probe it + snprintf (devid, sizeof(devid), "hw:%i", card); + sndcard = alsaCardProbe (devid); + + // Alsa has hole within card list [ignore them] + if (sndcard != NULL) { + // add current sndcard to sndcards object + json_object_array_add (sndcards, sndcard); + } + } + afb_req_success (request, sndcards, NULL); + } +} + +// pack Alsa element's ACL into a JSON object +STATIC json_object *getControlAcl (snd_ctl_elem_info_t *info) { + + json_object * jsonAclCtl = json_object_new_object(); + + json_object_object_add (jsonAclCtl, "read" , json_object_new_boolean(snd_ctl_elem_info_is_readable(info))); + json_object_object_add (jsonAclCtl, "write" , json_object_new_boolean(snd_ctl_elem_info_is_writable(info))); + json_object_object_add (jsonAclCtl, "inact" , json_object_new_boolean(snd_ctl_elem_info_is_inactive(info))); + json_object_object_add (jsonAclCtl, "volat" , json_object_new_boolean(snd_ctl_elem_info_is_volatile(info))); + json_object_object_add (jsonAclCtl, "lock" , json_object_new_boolean(snd_ctl_elem_info_is_locked(info))); + + // if TLV is readable we insert its ACL + if (!snd_ctl_elem_info_is_tlv_readable(info)) { + json_object * jsonTlv = json_object_new_object(); + + json_object_object_add (jsonTlv, "read" , json_object_new_boolean(snd_ctl_elem_info_is_tlv_readable(info))); + json_object_object_add (jsonTlv, "write" , json_object_new_boolean(snd_ctl_elem_info_is_tlv_writable(info))); + json_object_object_add (jsonTlv, "command", json_object_new_boolean(snd_ctl_elem_info_is_tlv_commandable(info))); + + json_object_object_add (jsonAclCtl, "tlv", jsonTlv); + } + return (jsonAclCtl); +} + + + +// pack element from ALSA control into a JSON object +STATIC json_object * alsaGetSingleCtl (snd_hctl_elem_t *elem, snd_ctl_elem_info_t *info, queryValuesT *queryValues) { + + int err; + json_object *jsonAlsaCtl,*jsonClassCtl; + snd_ctl_elem_id_t *elemid; + snd_ctl_elem_type_t elemtype; + snd_ctl_elem_value_t *control; + int count, idx; + + // allocate ram for ALSA elements + snd_ctl_elem_value_alloca(&control); + snd_ctl_elem_id_alloca (&elemid); + + // get elemId out of elem + snd_hctl_elem_get_id(elem, elemid); + + // when ctrlid is set, return only this ctrl + if (queryValues->numid != -1 && queryValues->numid != snd_ctl_elem_id_get_numid(elemid)) return NULL; + + // build a json object out of element + jsonAlsaCtl = json_object_new_object(); // http://alsa-lib.sourcearchive.com/documentation/1.0.24.1-3/group__Control_ga4e4f251147f558bc2ad044e836e449d9.html + json_object_object_add (jsonAlsaCtl,"numid", json_object_new_int(snd_ctl_elem_id_get_numid (elemid))); + if (queryValues->quiet < 2) json_object_object_add (jsonAlsaCtl,"name" , json_object_new_string(snd_ctl_elem_id_get_name (elemid))); + if (queryValues->quiet < 1) json_object_object_add (jsonAlsaCtl,"iface", json_object_new_string(snd_ctl_elem_iface_name(snd_ctl_elem_id_get_interface(elemid)))); + if (queryValues->quiet < 3)json_object_object_add (jsonAlsaCtl,"actif", json_object_new_boolean(!snd_ctl_elem_info_is_inactive(info))); + + elemtype = snd_ctl_elem_info_get_type(info); + + // number item and value(s) within control. + count = snd_ctl_elem_info_get_count (info); + + if (snd_ctl_elem_info_is_readable(info)) { + json_object *jsonValuesCtl = json_object_new_array(); + + if ((err = snd_hctl_elem_read(elem, control)) < 0) { + json_object *jsonValuesCtl = json_object_new_object(); + json_object_object_add (jsonValuesCtl,"error", json_object_new_string(snd_strerror(err))); + } else { + for (idx = 0; idx < count; idx++) { // start from one in amixer.c !!! + switch (elemtype) { + case SND_CTL_ELEM_TYPE_BOOLEAN: { + json_object_array_add (jsonValuesCtl, json_object_new_boolean (snd_ctl_elem_value_get_boolean(control, idx))); + break; + } + case SND_CTL_ELEM_TYPE_INTEGER: + json_object_array_add (jsonValuesCtl, json_object_new_int ((int)snd_ctl_elem_value_get_integer(control, idx))); + break; + case SND_CTL_ELEM_TYPE_INTEGER64: + json_object_array_add (jsonValuesCtl, json_object_new_int64 (snd_ctl_elem_value_get_integer64(control, idx))); + break; + case SND_CTL_ELEM_TYPE_ENUMERATED: + json_object_array_add (jsonValuesCtl, json_object_new_int (snd_ctl_elem_value_get_enumerated(control, idx))); + break; + case SND_CTL_ELEM_TYPE_BYTES: + json_object_array_add (jsonValuesCtl, json_object_new_int ((int)snd_ctl_elem_value_get_byte(control, idx))); + break; + case SND_CTL_ELEM_TYPE_IEC958: { + json_object *jsonIec958Ctl = json_object_new_object(); + snd_aes_iec958_t iec958; + snd_ctl_elem_value_get_iec958(control, &iec958); + + json_object_object_add (jsonIec958Ctl,"AES0",json_object_new_int(iec958.status[0])); + json_object_object_add (jsonIec958Ctl,"AES1",json_object_new_int(iec958.status[1])); + json_object_object_add (jsonIec958Ctl,"AES2",json_object_new_int(iec958.status[2])); + json_object_object_add (jsonIec958Ctl,"AES3",json_object_new_int(iec958.status[3])); + json_object_array_add (jsonValuesCtl, jsonIec958Ctl); + break; + } + default: + json_object_array_add (jsonValuesCtl, json_object_new_string ("?unknown?")); + break; + } + } + } + json_object_object_add (jsonAlsaCtl,"value",jsonValuesCtl); + } + + + if (!queryValues->quiet) { // in simple mode do not print usable values + jsonClassCtl = json_object_new_object(); + json_object_object_add (jsonClassCtl,"type" , json_object_new_string(snd_ctl_elem_type_name(elemtype))); + json_object_object_add (jsonClassCtl,"count", json_object_new_int(count)); + + switch (elemtype) { + case SND_CTL_ELEM_TYPE_INTEGER: + json_object_object_add (jsonClassCtl,"min", json_object_new_int((int)snd_ctl_elem_info_get_min(info))); + json_object_object_add (jsonClassCtl,"max", json_object_new_int((int)snd_ctl_elem_info_get_max(info))); + json_object_object_add (jsonClassCtl,"step", json_object_new_int((int)snd_ctl_elem_info_get_step(info))); + break; + case SND_CTL_ELEM_TYPE_INTEGER64: + json_object_object_add (jsonClassCtl,"min", json_object_new_int64(snd_ctl_elem_info_get_min64(info))); + json_object_object_add (jsonClassCtl,"max", json_object_new_int64(snd_ctl_elem_info_get_max64(info))); + json_object_object_add (jsonClassCtl,"step", json_object_new_int64(snd_ctl_elem_info_get_step64(info))); + break; + case SND_CTL_ELEM_TYPE_ENUMERATED: { + unsigned int item, items = snd_ctl_elem_info_get_items(info); + json_object *jsonEnum = json_object_new_array(); + + for (item = 0; item < items; item++) { + snd_ctl_elem_info_set_item(info, item); + if ((err = snd_hctl_elem_info(elem, info)) >= 0) { + json_object_array_add (jsonEnum, json_object_new_string(snd_ctl_elem_info_get_item_name(info))); + } + } + json_object_object_add (jsonClassCtl, "enums",jsonEnum); + break; + } + default: break; // ignore any unknown type + } + + // add collected class info with associated ACLs + json_object_object_add (jsonAlsaCtl,"ctrl", jsonClassCtl); + json_object_object_add (jsonAlsaCtl,"acl" , getControlAcl (info)); + + // check for tlv [direct port from amixer.c] + if (snd_ctl_elem_info_is_tlv_readable(info)) { + unsigned int *tlv; + tlv = malloc(4096); + if ((err = snd_hctl_elem_tlv_read(elem, tlv, 4096)) < 0) { + fprintf (stderr, "Control %s element TLV read error\n", snd_strerror(err)); + free(tlv); + } else { + json_object_object_add (jsonAlsaCtl,"tlv", decodeTlv (tlv, 4096)); + } + } + } + return (jsonAlsaCtl); +} + + +PUBLIC void alsaGetCtl(struct afb_req request) { + int err = 0; + snd_hctl_t *handle; + snd_hctl_elem_t *elem; + snd_ctl_elem_info_t *info; + json_object *sndctrls, *control; + queryValuesT queryValues; + + // get API params + queryValues.devid = afb_req_value(request, "devid"); + + const char *rqtNumid = afb_req_value(request, "numid"); + if (!rqtNumid) queryValues.numid=-1; + else if (rqtNumid && ! sscanf (rqtNumid, "%d", &queryValues.numid)) { + json_object *query = afb_req_json(request); + + afb_req_fail_f (request, "Query=%s NumID not integer &numid=%s&", json_object_to_json_string(query), rqtNumid); + goto ExitOnError; + }; + + const char *rqtQuiet = afb_req_value(request, "quiet"); + if (!rqtQuiet) queryValues.quiet=99; // default super quiet + else if (rqtQuiet && ! sscanf (rqtQuiet, "%d", &queryValues.quiet)) { + json_object *query = afb_req_json(request); + + afb_req_fail_f (request, "Query=%s NumID not integer &numid=%s&", json_object_to_json_string(query), rqtQuiet); + goto ExitOnError; + }; + + // Open sound we use Alsa high level API like amixer.c + if (!queryValues.devid || (err = snd_hctl_open(&handle, queryValues.devid, 0)) < 0) { + afb_req_fail_f (request, "alsaGetControl devid=[%s] open fail error=%s\n", queryValues.devid, snd_strerror(err)); + goto ExitOnError; + } + + if ((err = snd_hctl_load(handle)) < 0) { + afb_req_fail_f (request, "alsaGetControl devid=[%s] load fail error=%s\n", queryValues.devid, snd_strerror(err)); + goto ExitOnError; + } + + // allocate ram for ALSA elements + snd_ctl_elem_info_alloca(&info); + + // create an json array to hold all sndcard response + sndctrls = json_object_new_array(); + + for (elem = snd_hctl_first_elem(handle); elem != NULL; elem = snd_hctl_elem_next(elem)) { + + if ((err = snd_hctl_elem_info(elem, info)) < 0) { + json_object_put(sndctrls); // we abandon request let's free response + afb_req_fail_f (request, "alsaGetControl devid=[%s/%s] snd_hctl_elem_info error: %s\n" + , queryValues.devid, snd_hctl_name(handle), snd_strerror(err)); + goto ExitOnError; + } + + // each control is added into a JSON array + control = alsaGetSingleCtl(elem, info, &queryValues); + if (control) json_object_array_add(sndctrls, control); + + } + + afb_req_success (request, sndctrls, NULL); + return; + + // nothing special only for debugger breakpoint + ExitOnError: + return; +} + +// This routine is called when ALSA event are fired +STATIC int sndCtlEventCB (sd_event_source* src, int fd, uint32_t revents, void* userData) { + int err; + evtHandleT *evtHandle = (evtHandleT*)userData; + snd_ctl_event_t *ctlEvent; + json_object *ctlEventJson; + unsigned int mask; + int numid; + int iface; + int device; + int subdev; + const char*devname; + int index; + + if ((revents & EPOLLHUP) != 0) { + NOTICE (afbIface, "SndCtl hanghup [car disconnected]"); + goto ExitOnSucess; + } + + if ((revents & EPOLLIN) != 0) { + + snd_ctl_event_alloca(&ctlEvent); // initialise event structure on stack + + err = snd_ctl_read(evtHandle->ctl, ctlEvent); + if (err < 0) goto ExitOnError; + + // we only process sndctrl element + if (snd_ctl_event_get_type(ctlEvent) != SND_CTL_EVENT_ELEM) goto ExitOnSucess; + + // we only process value changed events + mask = snd_ctl_event_elem_get_mask(ctlEvent); + if (!(mask & SND_CTL_EVENT_MASK_VALUE)) goto ExitOnSucess; + + numid = snd_ctl_event_elem_get_numid(ctlEvent); + iface = snd_ctl_event_elem_get_interface(ctlEvent); + device = snd_ctl_event_elem_get_device(ctlEvent); + subdev = snd_ctl_event_elem_get_subdevice(ctlEvent); + devname= snd_ctl_event_elem_get_name(ctlEvent); + index = snd_ctl_event_elem_get_index(ctlEvent); + + DEBUG(afbIface, "sndCtlEventCB: (%i,%i,%i,%i,%s,%i)", numid, iface, device, subdev, devname, index); + + // proxy ctlevent as a binder event + ctlEventJson = json_object_new_object(); + json_object_object_add(ctlEventJson, "numid" ,json_object_new_int (numid)); + json_object_object_add(ctlEventJson, "iface" ,json_object_new_int (iface)); + json_object_object_add(ctlEventJson, "device" ,json_object_new_int (device)); + json_object_object_add(ctlEventJson, "subdev" ,json_object_new_int (subdev)); + json_object_object_add(ctlEventJson, "devname",json_object_new_string (devname)); + json_object_object_add(ctlEventJson, "index" ,json_object_new_int (index)); + afb_event_push(evtHandle->afbevt, ctlEventJson); + } + + ExitOnSucess: + return 0; + + ExitOnError: + WARNING (afbIface, "sndCtlEventCB: ignored unsupported event type"); + return (0); +} + +// Subscribe to every Alsa CtlEvent send by a given board +PUBLIC void alsaSubcribe (struct afb_req request) { + static sndHandleT sndHandles[MAX_SND_CARD]; + evtHandleT *evtHandle; + snd_ctl_t *ctlHandle; + snd_ctl_card_info_t *card_info; + int err, idx, cardid; + int idxFree=-1; + + const char *devid = afb_req_value(request, "devid"); + if (devid == NULL) { + afb_req_fail_f (request, "devid-missing", "devid=hw:xxx missing"); + goto ExitOnError; + } + + // open control interface for devid + err = snd_ctl_open(&ctlHandle, devid, SND_CTL_READONLY); + if (err < 0) { + afb_req_fail_f (request, "devid-unknown", "SndCard devid=%s Not Found err=%d", devid, err); + goto ExitOnError; + } + + // get sound card index use to search existing subscription + snd_ctl_card_info_alloca(&card_info); + snd_ctl_card_info(ctlHandle, card_info); + cardid = snd_ctl_card_info_get_card(card_info); + + // search for an existing subscription and mark 1st free slot + for (idx= 0; idx < MAX_SND_CARD; idx ++) { + if (sndHandles[idx].ucount > 0 && sndHandles[idx].cardid == cardid) { + evtHandle= sndHandles[idx].evtHandle; + break; + } else if (idxFree == -1) idxFree= idx; + }; + + // if not subscription exist for the event let's create one + if (idx == MAX_SND_CARD) { + + // reach MAX_SND_CARD event registration + if (idxFree == -1) { + afb_req_fail_f (request, "register-toomany", "Cannot register new event Maxcard==%devent name=%s", idx); + snd_ctl_close(ctlHandle); + goto ExitOnError; + } + + evtHandle = malloc (sizeof(evtHandleT)); + evtHandle->ctl = ctlHandle; + sndHandles[idxFree].ucount = 0; + sndHandles[idxFree].cardid = cardid; + sndHandles[idxFree].evtHandle = evtHandle; + + // subscribe for sndctl events attached to devid + err = snd_ctl_subscribe_events(evtHandle->ctl, 1); + if (err < 0) { + afb_req_fail_f (request, "subscribe-fail", "Cannot subscribe events from devid=%s err=%d", devid, err); + snd_ctl_close(ctlHandle); + goto ExitOnError; + } + + // get pollfd attach to this sound board + snd_ctl_poll_descriptors(evtHandle->ctl, &evtHandle->pfds, 1); + + // register sound event to binder main loop + err = sd_event_add_io(afb_daemon_get_event_loop(afbIface->daemon), &evtHandle->src, evtHandle->pfds.fd, EPOLLIN, sndCtlEventCB, evtHandle); + if (err < 0) { + afb_req_fail_f (request, "register-mainloop", "Cannot hook events to mainloop devid=%s err=%d", devid, err); + snd_ctl_close(ctlHandle); + goto ExitOnError; + } + + // create binder event attached to devid name + evtHandle->afbevt = afb_daemon_make_event (afbIface->daemon, devid); + if (!afb_event_is_valid (evtHandle->afbevt)) { + afb_req_fail_f (request, "register-event", "Cannot register new binder event name=%s", devid); + snd_ctl_close(ctlHandle); + goto ExitOnError; + } + + // everything looks OK let's move forward + idx=idxFree; + } + + // subscribe to binder event + err = afb_req_subscribe(request, evtHandle->afbevt); + if (err != 0) { + afb_req_fail_f (request, "register-eventname", "Cannot subscribe binder event name=%s [invalid channel]", devid, err); + goto ExitOnError; + } + + // increase usage count and return success + sndHandles[idx].ucount ++; + afb_req_success(request, NULL, NULL); + return; + + ExitOnError: + return; +} + +// Subscribe to every Alsa CtlEvent send by a given board +PUBLIC void alsaGetCardId (struct afb_req request) { + char devid [10]; + int card, err, index; + json_object *respJson; + snd_ctl_t *ctlHandle; + snd_ctl_card_info_t *cardinfo; + + const char *sndname = afb_req_value(request, "sndname"); + if (sndname == NULL) { + afb_req_fail_f (request, "argument-missing", "sndname=SndCardName missing"); + goto ExitOnError; + } + + // loop on potential card number + snd_ctl_card_info_alloca(&cardinfo); + for (card =0; card < MAX_SND_CARD; card++) { + + // build card devid and probe it + snprintf (devid, sizeof(devid), "hw:%i", card); + + // open control interface for devid + err = snd_ctl_open(&ctlHandle, devid, SND_CTL_READONLY); + if (err < 0) continue; + + snd_ctl_card_info(ctlHandle, cardinfo); + index = snd_ctl_card_info_get_card(cardinfo); + + // check if short|long name match + if (!strcmp (sndname, snd_ctl_card_info_get_id(cardinfo))) break; + if (!strcmp (sndname, snd_ctl_card_info_get_name(cardinfo))) break; + if (!strcmp (sndname, snd_ctl_card_info_get_longname(cardinfo))) break; + } + + if (card == MAX_SND_CARD) { + afb_req_fail_f (request, "sndcard-notfound", "Fail to find card with name=%s", sndname); + goto ExitOnError; + } + + // proxy ctlevent as a binder event + respJson = json_object_new_object(); + json_object_object_add(respJson, "index" ,json_object_new_int (index)); + json_object_object_add(respJson, "devid" ,json_object_new_string (devid)); + json_object_object_add(respJson, "shortname" ,json_object_new_string (snd_ctl_card_info_get_name(cardinfo))); + json_object_object_add(respJson, "longname" ,json_object_new_string (snd_ctl_card_info_get_longname(cardinfo))); + + afb_req_success(request, respJson, NULL); + return; + + ExitOnError: + return; +} + + + +// This function is call when all plugins are loaded +PUBLIC int alsaLibInit (struct afb_service service) { + + // For future use + srvitf = service; + + return 0; +} \ No newline at end of file diff --git a/AlsaSound/CoreBinding/AlsaLibMapping.h b/AlsaSound/CoreBinding/AlsaLibMapping.h new file mode 100644 index 0000000..cd6b409 --- /dev/null +++ b/AlsaSound/CoreBinding/AlsaLibMapping.h @@ -0,0 +1,51 @@ +/* + * AlsaLibMapping -- provide low level interface with ALSA 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 ALSALIBMAPPING_H +#define ALSALIBMAPPING_H + +#include +#include +#include + +// import from AlsaAfbBinding +extern const struct afb_binding_interface *afbIface; + +// import from AlsaAfbMapping +PUBLIC int alsaLibInit (struct afb_service service); +PUBLIC void alsaGetInfo (struct afb_req request); +PUBLIC void alsaGetCtl(struct afb_req request); +PUBLIC void alsaSubcribe (struct afb_req request); +PUBLIC void alsaGetCardId (struct afb_req request); + + +#endif /* ALSALIBMAPPING_H */ + diff --git a/AlsaSound/CoreBinding/CMakeLists.txt b/AlsaSound/CoreBinding/CMakeLists.txt new file mode 100644 index 0000000..0475e72 --- /dev/null +++ b/AlsaSound/CoreBinding/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}) + +################################################## +# AlsaBinding +################################################## +ADD_LIBRARY(alsacore-afb MODULE AlsaAfbBinding.c AlsaLibMapping.c) + +SET_TARGET_PROPERTIES(alsacore-afb PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_SOURCE_DIR}/export.map" +) + +TARGET_LINK_LIBRARIES(alsacore-afb ${link_libraries}) +INSTALL(TARGETS alsacore-afb + LIBRARY DESTINATION ${binding_install_dir}) + + diff --git a/AlsaSound/CoreBinding/README.md b/AlsaSound/CoreBinding/README.md new file mode 100644 index 0000000..5b6f026 --- /dev/null +++ b/AlsaSound/CoreBinding/README.md @@ -0,0 +1,30 @@ +------------------------------------------------------------------------ + AlsaCore Low level binding maps AlsaLib APIs +------------------------------------------------------------------------ + +Testing: (from project directory bindings) + * start binder: ~/opt/bin/afb-daemon --ldpaths=./build --token=mysecret --roothttp=htdocs + * connect browser on http://localhost:1234?devid=hw:0 + + # List Avaliable Sound cards + http://localhost:1234/api/alsacore/getinfo + + # Get Info on a given Sound Card + http://localhost:1234/api/alsacore/getinfo?devid=hw:0 + + # Get all controls from a given sound card + http://localhost:1234/api/alsacore/getctl?devid=hw:0 + + # Get detail on a given control (optional quiet=0=verbose,1,2) + http://localhost:1234/api/alsacore/getctl?devid=hw:0&numid=1&quiet=0 + +# Debug event with afb-client-demo +``` + ~/opt/bin/afb-client-demo localhost:1234/api?token=mysecret + alsacore subscribe {"devid":"hw:0"} +``` + +# Open AlsaMixer and play with Volume +``` + alsamixer -D hw:0 +``` diff --git a/AlsaSound/HardAbsLayer/CMakeLists.txt b/AlsaSound/HardAbsLayer/CMakeLists.txt new file mode 100644 index 0000000..e24a9d9 --- /dev/null +++ b/AlsaSound/HardAbsLayer/CMakeLists.txt @@ -0,0 +1,23 @@ +########################################################################### +# 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. +########################################################################### + +PROJECT(hadware-abstraction-layer C) + + +ADD_SUBDIRECTORY(IntelHda) + diff --git a/AlsaSound/HardAbsLayer/IntelHda/CMakeLists.txt b/AlsaSound/HardAbsLayer/IntelHda/CMakeLists.txt new file mode 100644 index 0000000..0d3f344 --- /dev/null +++ b/AlsaSound/HardAbsLayer/IntelHda/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}) + +################################################## +# Inte-HDA sound card Hardware Abstraction Layer +################################################## +ADD_LIBRARY(intel-hda-hal MODULE IntelHdaBinding.c IntelHdaLib.c) + +SET_TARGET_PROPERTIES(intel-hda-hal PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_SOURCE_DIR}/export.map" +) + +TARGET_LINK_LIBRARIES(intel-hda-hal ${link_libraries}) +INSTALL(TARGETS intel-hda-hal + LIBRARY DESTINATION ${binding_install_dir}) + + diff --git a/AlsaSound/HardAbsLayer/IntelHda/IntelHdaBinding.c b/AlsaSound/HardAbsLayer/IntelHda/IntelHdaBinding.c new file mode 100644 index 0000000..0452314 --- /dev/null +++ b/AlsaSound/HardAbsLayer/IntelHda/IntelHdaBinding.c @@ -0,0 +1,82 @@ +/* + * 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 "IntelHdaLib.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= intelHdaSetVol, .info= "Set Volume" }, + { .name= "getvolume", .session= AFB_SESSION_NONE, .callback= intelHdaGetVol, .info= "Get Volume" }, + { .name= "subscribe", .session= AFB_SESSION_NONE, .callback= intelHdaSubscribe, .info= "Subscribe AudioBinding 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= "intel-hda", + .info= "Hardware Abstraction Layer for IntelHDA sound card", + .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 (intelHdaInit(service, binding_description.v1.prefix)); +}; + +/* + * 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/AlsaSound/HardAbsLayer/IntelHda/IntelHdaLib.c b/AlsaSound/HardAbsLayer/IntelHda/IntelHdaLib.c new file mode 100644 index 0000000..ad4fcf4 --- /dev/null +++ b/AlsaSound/HardAbsLayer/IntelHda/IntelHdaLib.c @@ -0,0 +1,138 @@ +/* + * 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. + * + * reference: + * amixer contents; amixer controls; + * http://www.tldp.org/HOWTO/Alsa-sound-6.html + */ +#define _GNU_SOURCE // needed for vasprintf +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "IntelHdaLib.h" +#include "AlsaMixerHal.h" + +static struct afb_service srvitf; + +// Normalise Intel-HDA numid +static AlsaHalMapT alsaCtlsMap[]= { // check with amixer -D hw:xx controls; amixer -D "hw:3" cget numid=xx + /* ACTION(enum) NUMID(int) IFACE(enum) VALUES(int) MinVal(int) MaxVal(int) Step(int) ACL=READ|WRITE|RW INFO=comment */ + { .action=Master_Playback_Volume, .numid=16, .group=OUTVOL, .values=1, .minval=0, .maxval= 87 , .step=0, .acl=RW, .info= "Master Playback Volume" }, + { .action=PCM_Playback_Volume , .numid=27, .group=PCMVOL, .values=2, .minval=0, .maxval= 255, .step=0, .acl=RW, .info= "PCM Playback Volume" }, + { .action=PCM_Playback_Switch , .numid=17, .group=SWITCH, .values=1, .minval=0, .maxval= 1 , .step=0, .acl=RW, .info= "Master Playback Switch" }, + { .action=Capture_Volume , .numid=12, .group=INVOL , .values=2, .minval=0, .maxval= 31 , .step=0, .acl=RW, .info= "Capture Volume" }, + { .numid=0 } /* marker for end of the array */ +} ; + +// Warning: Longname is used to locate board on the system +static AlsaHalSndT alsaSndCard = { + .longname= "HDA Intel PCH", + .info = "Hardware Abstraction Layer for IntelHDA sound card", + .ctls = alsaCtlsMap, +}; + + +// 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 intelHdaMonitor(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 +} + +// Subscribe to AudioBinding events +PUBLIC void intelHdaSubscribe (struct afb_req request) { + const char *devid = afb_req_value(request, "devid"); + if (devid == NULL) { + afb_req_fail_f (request, "devid-missing", "devid=hw:xxx missing"); + } + +} + + +// Call when all bindings are loaded and ready to accept request +PUBLIC void intelHdaGetVol(struct afb_req request) { + + // Should call here Hardware Alsa Abstraction Layer for corresponding Sound Card + afb_req_success (request, NULL, NULL); + return; + +} + +PUBLIC void intelHdaSetVol(struct afb_req request) { + const char *arg; + char *pcm; + + arg = afb_req_value(request, "vol"); + if (arg == NULL) { + afb_req_fail_f (request, "argument-missing", "vol=[0,100] missing"); + goto ExitOnError; + } + + arg = afb_req_value(request, "pcm"); + if (arg == NULL) pcm="Master"; + + // Should call here Hardware Alsa Abstraction Layer for corresponding Sound Card + afb_req_success (request, NULL, NULL); + return; + + ExitOnError: + return; + +} + + +// this function is call after all binder are loaded and initialised +PUBLIC int intelHdaInit (struct afb_service service, const char *cardname) { + srvitf = service; + + // API prefix is used as sndcard halname + alsaSndCard.halname= cardname; + return 0; +} \ No newline at end of file diff --git a/AlsaSound/HardAbsLayer/IntelHda/IntelHdaLib.h b/AlsaSound/HardAbsLayer/IntelHda/IntelHdaLib.h new file mode 100644 index 0000000..edf9474 --- /dev/null +++ b/AlsaSound/HardAbsLayer/IntelHda/IntelHdaLib.h @@ -0,0 +1,49 @@ +/* + * 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 intelHdaSetVol (struct afb_req request); +PUBLIC void intelHdaGetVol(struct afb_req request); +PUBLIC void intelHdaSubscribe(struct afb_req request); +PUBLIC int intelHdaInit (struct afb_service service, const char *cardname); + +#endif /* AUDIOLIBMAPPING_H */ + diff --git a/AlsaSound/include/AlsaMixerHal.h b/AlsaSound/include/AlsaMixerHal.h new file mode 100644 index 0000000..399f8ab --- /dev/null +++ b/AlsaSound/include/AlsaMixerHal.h @@ -0,0 +1,71 @@ +/* + * 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. + * + * reference: + * amixer contents; amixer controls; + * http://www.tldp.org/HOWTO/Alsa-sound-6.html + */ + + +#ifndef ALSAMIXERMAP_H +#define ALSAMIXERMAP_H + +// Most controls are MIXER but some vendor specific are possible +typedef enum { + OUTVOL, + PCMVOL, + INVOL, + SWITCH, + ROUTE, + CARD, +} groupEnum; + +typedef enum { + READ, + WRITE, + RW, +} aclEnum; + +typedef enum { + Master_Playback_Volume, + PCM_Playback_Volume, + PCM_Playback_Switch, + Capture_Volume, +} actionEnum; + +typedef const struct { + actionEnum action; + int numid; + groupEnum group; + int values; + int minval; + int maxval; + int step; + char* info; + aclEnum acl; + +} AlsaHalMapT; + +typedef struct { + const char *halname; + const char *longname; + const char *info; + AlsaHalMapT *ctls; + +} AlsaHalSndT; + +#endif /* ALSAMIXERMAP_H */ + diff --git a/AudioLogic/AudioLogicBinding.c b/AudioLogic/AudioLogicBinding.c index 65943c0..1220f29 100644 --- a/AudioLogic/AudioLogicBinding.c +++ b/AudioLogic/AudioLogicBinding.c @@ -26,7 +26,7 @@ #include #include -#define _GNU_SOURCE // needed for vasprintf +#include "AlsaMixerHal.h" #include "AudioLogicLib.h" PUBLIC const struct afb_binding_interface *afbIface; @@ -36,15 +36,19 @@ STATIC void localping(struct afb_req 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= "ping" , .session= AFB_SESSION_NONE, .callback= localping, .info= "Ping Binding" }, + { .name= "setvolume", .session= AFB_SESSION_CHECK, .callback= audioLogicSetVol, .info= "Set Volume" }, + { .name= "getvolume", .session= AFB_SESSION_CHECK, .callback= audioLogicGetVol, .info= "Get Volume" }, + { .name= "subscribe", .session= AFB_SESSION_CHECK, .callback= audioLogicSubscribe, .info= "Subscribe AudioBinding Events" }, + { .name= "monitor", .session= AFB_SESSION_CHECK, .callback= audioLogicMonitor, .info= "Activate AlsaCtl Monitoring" }, + { .name= "open", .session= AFB_SESSION_CREATE,.callback= audioLogicOpen, .info= "Open a Dedicated SoundCard" }, + { .name= "close", .session= AFB_SESSION_CLOSE, .callback= audioLogicClose, .info= "Close previously open SoundCard" }, { .name= NULL } /* marker for end of the array */ }; diff --git a/AudioLogic/AudioLogicLib.c b/AudioLogic/AudioLogicLib.c index ebe241c..857cc9f 100644 --- a/AudioLogic/AudioLogicLib.c +++ b/AudioLogic/AudioLogicLib.c @@ -29,12 +29,34 @@ #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; +PUBLIC void audioLogicSetVol(struct afb_req request) { + + const char *vol = afb_req_value(request, "vol"); + if (vol == NULL) { + afb_req_fail_f (request, "argument-missing", "vol=+-%[0,100] missing"); + goto ExitOnError; + } + + switch (vol[0]) { + case '+': + break; + case '-': + break; + case '%': + break; + + default: + afb_req_fail_f (request, "value-invalid", "volume should be (+-%[0-100]xxx) vol=%s", vol); + goto ExitOnError; + } + + // Should call here Hardware Alsa Abstraction Layer for corresponding Sound Card + afb_req_success (request, NULL, NULL); + return; + + ExitOnError: + return; } // This callback is fired when afb_service_call for api/alsacore/subctl returns @@ -46,44 +68,139 @@ STATIC void alsaSubcribeCB (void *handle, int iserror, struct json_object *resul 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; - } + 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 + // keep request for callback to respond struct afb_req *handle = afb_req_store(request); - + + // get client context + AudioLogicCtxT *ctx = afb_req_context_get(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); + NOTICE (afbIface, "audioLogicMonitor ctx->devid=%s [ctx->queryurl=%s]", ctx->devid, json_object_to_json_string(ctx->queryurl)); + + if (ctx->queryurl) { + json_object_get (ctx->queryurl); // Make sure usage count does not fall to zero + afb_service_call(srvitf, "alsacore", "subscribe", ctx->queryurl, alsaSubcribeCB, handle); + } + + else afb_req_fail_f(request, "context-invalid", "No valid queryurl in client context"); // success/failure messages return from callback } +// Subscribe to AudioBinding events +PUBLIC void audioLogicSubscribe (struct afb_req request) { + + return; +} + + // Call when all bindings are loaded and ready to accept request PUBLIC void audioLogicGetVol(struct afb_req request) { + // Should call here Hardware Alsa Abstraction Layer for corresponding Sound Card afb_req_success (request, NULL, NULL); return; } -PUBLIC void audioLogicSetVol(struct afb_req request) { - +// This callback is fired when afb_service_call for api/alsacore/subctl returns +STATIC void audioLogicOpenCB (void *handle, int iserror, struct json_object *result) { + struct afb_req request = afb_req_unstore(handle); + struct json_object *response; + //INFO (afbIface, "result=[%s]\n", json_object_to_json_string (result)); - afb_req_success (request, NULL, NULL); - return; + + if (iserror) { // on error proxy information we got from lower layer + if (result) { + struct json_object *status, *info; + + if (json_object_object_get_ex(result, "request", &response)) { + json_object_object_get_ex(response, "info" , &info); + json_object_object_get_ex(response, "status", &status); + afb_req_fail(request, json_object_get_string(status), json_object_get_string(info)); + goto OnExit; + } + } else { + afb_req_fail(request, "Fail", "Unknown Error" ); + } + goto OnExit; + } + + // Get response from object + json_object_object_get_ex(result, "response", &response); + if (response) { + struct json_object *subobj; + + // attach client context to session + AudioLogicCtxT *ctx = malloc (sizeof(AudioLogicCtxT)); + + // extract information from Json Alsa Object + json_object_object_get_ex(response, "cardid", &subobj); + if (subobj) ctx->cardid= json_object_get_int(subobj); + + // store devid as an object for further alsa request + json_object_object_get_ex(response, "devid", &subobj); + if (subobj) ctx->devid= strdup(json_object_get_string(subobj)); + + json_object_object_get_ex(response, "shortname", &subobj); + if (subobj)ctx->shortname=strdup(json_object_get_string(subobj)); + + json_object_object_get_ex(response, "longname", &subobj); + if (subobj)ctx->longname=strdup(json_object_get_string(subobj)); + // add AudioLogicCtxT to Client Session + NOTICE (afbIface, "audioLogicOpen ctx->devid=[%s]", ctx->devid); + + // save queryurl with devid only for further ALSA request + ctx->queryurl=json_object_new_object(); + json_object_object_add(ctx->queryurl, "devid",json_object_new_string(ctx->devid)); + + afb_req_context_set(request, ctx, free); + } + + // release original calling request + afb_req_success(request, response, NULL); + + + OnExit: + afb_req_unref(request); + return; } +// Delegate to lowerlevel the mapping of soundcard name with soundcard ID +PUBLIC void audioLogicOpen(struct afb_req request) { + + // Delegate query to lower level + struct afb_req *handle = afb_req_store(request); + if (!handle) afb_req_fail(request, "error", "out of memory"); + else afb_service_call(srvitf, "alsacore", "getCardId", json_object_get(afb_req_json(request)), audioLogicOpenCB, handle); +} + +// Free client context create from audioLogicOpenCB +PUBLIC void audioLogicClose (struct afb_req request) { + + // retrieve current client context to print debug info + AudioLogicCtxT *ctx = (AudioLogicCtxT*) afb_req_context_get(request); + DEBUG (afbIface, "audioLogicClose cardid=%d devid=%s shortname=%s longname=%s", ctx->cardid, ctx->devid, ctx->shortname, ctx->longname); +} + + +// this function is call after all binder are loaded and initialised +PUBLIC int audioLogicInit (struct afb_service service) { + srvitf = service; + + return 0; +} diff --git a/AudioLogic/AudioLogicLib.h b/AudioLogic/AudioLogicLib.h index 21513fd..8530360 100644 --- a/AudioLogic/AudioLogicLib.h +++ b/AudioLogic/AudioLogicLib.h @@ -40,10 +40,24 @@ typedef int BOOL; // import from AlsaAfbBinding extern const struct afb_binding_interface *afbIface; + +// This structure hold private data for a given client of binding +typedef struct { + + int cardid; + const char *devid; + const char *shortname; + const char *longname; + json_object *queryurl; +} AudioLogicCtxT; + // 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 void audioLogicOpen(struct afb_req request); +PUBLIC void audioLogicClose(struct afb_req request); +PUBLIC void audioLogicSubscribe(struct afb_req request); PUBLIC int audioLogicInit (struct afb_service service); #endif /* AUDIOLIBMAPPING_H */ diff --git a/AudioLogic/README.md b/AudioLogic/README.md index 399f905..fa50173 100644 --- a/AudioLogic/README.md +++ b/AudioLogic/README.md @@ -3,16 +3,33 @@ ------------------------------------------------------------------------ Testing: (from project directory bindings) - * start binder: ~/opt/bin/afb-daemon --ldpaths=./build --roothttp=htdocs - * connect browser on http://localhost:1234 + * start binder: ~/opt/bin/afb-daemon --ldpaths=./build --token=mysecret --roothttp=htdocs + * connect browser on http://localhost:1234?devid=hw:0 + + # Open Sound Card from its name + http://localhost:1234/api/audio/open?token=mysecret&sndname=H650e # Subscribe event for a given board - http://localhost:1234/api/audio/subscribe?devid=hw:0 + http://localhost:1234/api/audio/subscribe?token=mysecret&devid=hw:0 # Increase Volume - http://localhost:1234/api/audio/setvol?devid=hw:0&pcm=master&vol=50% + http://localhost:1234/api/audio/setvol?token=mysecret&devid=hw:0&pcm=master&vol=50% # Get Volume - http://localhost:1234/api/audio/getvol?devid=hw:0&pcm=master + http://localhost:1234/api/audio/getvol?token=mysecret&devid=hw:0&pcm=master + + # Close Session + http://localhost:1234/api/audio/close?token=mysecret + + +Testing with afb-client-demo - \ No newline at end of file +``` +~/opt/bin/afb-client-demo localhost:1234/api?token=mysecret +alsacore subctl {"devid":"hw:0"} +``` + +Start AlsaMixer and change volume +``` +alsamixer -D hw:0 +``` \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f120e8..56b5f09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,7 +98,7 @@ ENDIF(CMAKE_BUILD_TYPE MATCHES Debug) SET(include_dirs ${INCLUDE_DIRS} - ${CMAKE_SOURCE_DIR}/include + ${CMAKE_SOURCE_DIR}/AlsaSound/include ${json-c_INCLUDE_DIRS} ${afb-daemon_INCLUDE_DIRS} ) @@ -110,6 +110,6 @@ SET(link_libraries # Bindings to compile # -------------------- -add_subdirectory(Alsa) +add_subdirectory(AlsaSound) add_subdirectory(AudioLogic) add_subdirectory(htdocs) diff --git a/README.md b/README.md index b33f843..10b7780 100644 --- a/README.md +++ b/README.md @@ -63,5 +63,9 @@ ls # Start the binder afb-daemon --token=x --ldpaths=$INSTALL_DIR/lib/audio --port=1234 --roothttp=$INSTALL_DIR/htdocs/audio-bindings --verbose ``` +Start a browser on http://localhost:1234?devid=hw:0 - +Start AlsaMixer and change volume you should see event in your browser +``` +alsamixer -D hw:0 +``` \ No newline at end of file diff --git a/htdocs/AFB-websock.js b/htdocs/AFB-websock.js new file mode 100644 index 0000000..f904a58 --- /dev/null +++ b/htdocs/AFB-websock.js @@ -0,0 +1,172 @@ +AFB = function(base, initialtoken){ + +var urlws = "ws://"+window.location.host+"/"+base; +var urlhttp = "http://"+window.location.host+"/"+base; + +/*********************************************/ +/**** ****/ +/**** AFB_context ****/ +/**** ****/ +/*********************************************/ +var AFB_context; +{ + var UUID = undefined; + var TOKEN = initialtoken; + + var context = function(token, uuid) { + this.token = token; + this.uuid = uuid; + } + + context.prototype = { + get token() {return TOKEN;}, + set token(tok) {if(tok) TOKEN=tok;}, + get uuid() {return UUID;}, + set uuid(id) {if(id) UUID=id;} + }; + + AFB_context = new context(); +} +/*********************************************/ +/**** ****/ +/**** AFB_websocket ****/ +/**** ****/ +/*********************************************/ +var AFB_websocket; +{ + var CALL = 2; + var RETOK = 3; + var RETERR = 4; + var EVENT = 5; + + var PROTO1 = "x-afb-ws-json1"; + + AFB_websocket = function(onopen, onabort) { + var u = urlws; + if (AFB_context.token) { + u = u + '?x-afb-token=' + AFB_context.token; + if (AFB_context.uuid) + u = u + '&x-afb-uuid=' + AFB_context.uuid; + } + this.ws = new WebSocket(u, [ PROTO1 ]); + this.pendings = {}; + this.awaitens = {}; + this.counter = 0; + this.ws.onopen = onopen.bind(this); + this.ws.onerror = onerror.bind(this); + this.ws.onclose = onclose.bind(this); + this.ws.onmessage = onmessage.bind(this); + this.onopen = onopen; + this.onabort = onabort; + } + + function onerror(event) { + var f = this.onabort; + if (f) { + delete this.onopen; + delete this.onabort; + f && f(this); + } + this.onerror && this.onerror(this); + } + + function onopen(event) { + var f = this.onopen; + delete this.onopen; + delete this.onabort; + f && f(this); + } + + function onclose(event) { + for (var id in this.pendings) { + var ferr = this.pendings[id].onerror; + ferr && ferr(null, this); + } + this.pendings = {}; + this.onclose && this.onclose(); + } + + function fire(awaitens, name, data) { + var a = awaitens[name]; + if (a) + a.forEach(function(handler){handler(data);}); + var i = name.indexOf("/"); + if (i >= 0) { + a = awaitens[name.substring(0,i)]; + if (a) + a.forEach(function(handler){handler(data);}); + } + a = awaitens["*"]; + if (a) + a.forEach(function(handler){handler(data);}); + } + + function reply(pendings, id, ans, offset) { + if (id in pendings) { + var p = pendings[id]; + delete pendings[id]; + var f = p[offset]; + f(ans); + } + } + + function onmessage(event) { + var obj = JSON.parse(event.data); + var code = obj[0]; + var id = obj[1]; + var ans = obj[2]; + AFB_context.token = obj[3]; + switch (code) { + case RETOK: + reply(this.pendings, id, ans, 0); + break; + case RETERR: + reply(this.pendings, id, ans, 1); + break; + case EVENT: + default: + fire(this.awaitens, id, ans); + break; + } + } + + function close() { + this.ws.close(); + } + + function call(method, request) { + return new Promise((function(resolve, reject){ + var id, arr; + do { + id = String(this.counter = 4095 & (this.counter + 1)); + } while (id in this.pendings); + this.pendings[id] = [ resolve, reject ]; + arr = [CALL, id, method, request ]; + if (AFB_context.token) arr.push(AFB_context.token); + this.ws.send(JSON.stringify(arr)); + }).bind(this)); + } + + function onevent(name, handler) { + var id = name; + var list = this.awaitens[id] || (this.awaitens[id] = []); + list.push(handler); + } + + AFB_websocket.prototype = { + close: close, + call: call, + onevent: onevent + }; +} +/*********************************************/ +/**** ****/ +/**** ****/ +/**** ****/ +/*********************************************/ +return { + context: AFB_context, + ws: AFB_websocket +}; +}; + diff --git a/htdocs/AudioBinding.js b/htdocs/AudioBinding.js new file mode 100644 index 0000000..a1267b1 --- /dev/null +++ b/htdocs/AudioBinding.js @@ -0,0 +1,67 @@ + var afb = new AFB("api", "mysecret"); + var ws; + var evtidx=0; + + function getParameterByName(name, url) { + if (!url) { + url = window.location.href; + } + name = name.replace(/[\[\]]/g, "\\$&"); + var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), + results = regex.exec(url); + if (!results) return null; + if (!results[2]) return ''; + return decodeURIComponent(results[2].replace(/\+/g, " ")); + } + + // default soundcard is hw:0 + var devid=getParameterByName("devid"); + if (!devid) devid="hw:0"; + + var sndname=getParameterByName("sndname"); + if (!sndname) sndname="PCH"; + + function init() { + ws = new afb.ws(onopen, onabort); + } + + function onopen() { + document.getElementById("main").style.visibility = "visible"; + document.getElementById("connected").innerHTML = "Binder WS Active"; + document.getElementById("connected").style.background = "lightgreen"; + ws.onevent("*", gotevent); + } + + function onabort() { + document.getElementById("main").style.visibility = "hidden"; + document.getElementById("connected").innerHTML = "Connected Closed"; + document.getElementById("connected").style.background = "red"; + + } + + function replyok(obj) { + console.log("replyok:" + JSON.stringify(obj)); + document.getElementById("output").innerHTML = "OK: "+JSON.stringify(obj); + } + + function replyerr(obj) { + console.log("replyerr:" + JSON.stringify(obj)); + document.getElementById("output").innerHTML = "ERROR: "+JSON.stringify(obj); + } + + function gotevent(obj) { + console.log("gotevent:" + JSON.stringify(obj)); + document.getElementById("outevt").innerHTML = (evtidx++) +": "+JSON.stringify(obj); + } + + function send(message) { + var api = document.getElementById("api").value; + var verb = document.getElementById("verb").value; + ws.call(api+"/"+verb, {data:message}).then(replyok, replyerr); + } + + + function callbinder(api, verb, query) { + console.log ("subscribe api="+api+" verb="+verb+" query=" +query); + ws.call(api+"/"+verb, query).then(replyok, replyerr); + } \ No newline at end of file diff --git a/htdocs/README.md b/htdocs/README.md index 6468356..79b57d8 100644 --- a/htdocs/README.md +++ b/htdocs/README.md @@ -1,5 +1,5 @@ ------------------------------------------------------------------------ - Simple HTML test + Basic HTML & WS test ------------------------------------------------------------------------ # Load bindings directly from development tree for debug diff --git a/htdocs/alsa-core.html b/htdocs/alsa-core.html index 0969b6f..9d0fd83 100644 --- a/htdocs/alsa-core.html +++ b/htdocs/alsa-core.html @@ -2,41 +2,23 @@ Hello world test - - + - function onopen() { - document.getElementById("main").style.visibility = "visible"; - document.getElementById("connected").innerHTML = "WebSocket Open"; - } - function onabort() { - document.getElementById("main").style.visibility = "hidden"; - document.getElementById("connected").innerHTML = "Connected Closed"; - } - function init() { - ws = new AfbWsItf("api", onopen, onabort, new AfbCtxItf("mysecret")); - } - function replyok(obj) { - document.getElementById("output").innerHTML = "OK: "+JSON.stringify(obj); - } - function replyerr(obj) { - document.getElementById("output").innerHTML = "ERROR: "+JSON.stringify(obj); - } - function subscribe(devid) { - ws.call("alsacore", "subctl", {devid:devid}, replyok, replyerr); - } - -

Hello world test

+ +
    -
  1. getinfo: List Sound Cards -
  2. Card info about hw:0 -
  3. List controls for hw:0 (quiet) -
  4. List controls for hw:0 (verbose) -
  5. return control numid=1 for hw:0 -
  6. +
  7. getinfo: List Sound Cards +
  8. Card info about hw:0 +
  9. List controls for hw:0 (quiet) +
  10. List controls for hw:0 (verbose) +
  11. return control numid=1 for hw:0 +
    +
  12. +
    diff --git a/htdocs/audio-logic.html b/htdocs/audio-logic.html index ff806ad..79ab114 100644 --- a/htdocs/audio-logic.html +++ b/htdocs/audio-logic.html @@ -2,39 +2,28 @@ Hello world test - - + +

    Hello world test

    + +
      -
    1. Set Master PCM volume to 50% -
    2. Get Master PCM volume -
    3. Activate devid=hw:0 monitoring -
    4. +
    5. +
      +
    6. +
    7. +
    8. +
    9. +
      +
    10. +
    11. +
      +
    12. + +
      diff --git a/htdocs/websock.js b/htdocs/websock.js deleted file mode 100644 index c429553..0000000 --- a/htdocs/websock.js +++ /dev/null @@ -1,117 +0,0 @@ - -AfbCtxItf = (function(){ - - var UUID = undefined; - var TOKEN = undefined; - - function AfbCtxItf(token, uuid) { - this.token = token; - this.uuid = uuid; - } - - AfbCtxItf.prototype = { - get token() {return TOKEN;}, - set token(tok) {if(tok) TOKEN=tok;}, - get uuid() {return UUID;}, - set uuid(id) {if(id) UUID=id;} - }; - - return AfbCtxItf; -})(); - - -AfbWsItf = (function(){ - - var CALL = 2; - var RETOK = 3; - var RETERR = 4; - - function AfbWsItf(base, onopen, onabort, ctx) { - ctx = ctx || new AfbCtxItf(); - var wl = window.location; - var u = "ws://"+wl.host+"/"+base; - if (ctx.token) { - u = u + '?x-afb-token=' + ctx.token; - if (ctx.uuid) - u = u + '&x-afb-uuid=' + ctx.uuid; - } - this.ws = new (WebSocket || MozWebSocket)(u, [ "x-afb-ws-json1" ]); - this.pendings = {}; - this.counter = 0; - this.ctx = ctx; - this.ws.onopen = onopen.bind(this); - this.ws.onerror = onerror.bind(this); - this.ws.onclose = onclose.bind(this); - this.ws.onmessage = onmessage.bind(this); - this.onopen = onopen; - this.onabort = onabort; - } - - function onerror(event) { - var f = this.onabort; - if (f) { - delete this.onopen; - delete this.onabort; - f && f(this); - } - this.onerror && this.onerror(this); - } - - function onopen(event) { - var f = this.onopen; - delete this.onopen; - delete this.onabort; - f && f(this); - } - - function onclose(event) { - for (var id in this.pendings) { - var ferr = this.pendings[id].onerror; - ferr && ferr(null, this); - } - this.pendings = {}; - this.onclose && this.onclose(); - } - - function onmessage(event) { - var obj = JSON.parse(event.data); - var code = obj[0]; - var id = obj[1]; - var ans = obj[2]; - this.ctx.token = obj[3]; - var pend; - if (id && id in this.pendings) { - pend = this.pendings[id]; - delete this.pendings[id]; - } - switch (code) { - case RETOK: - pend && pend.onsuccess && pend.onsuccess(ans, this); - break; - case RETERR: - default: - pend && pend.onerror && pend.onerror(ans, this); - break; - } - } - - function close() { - this.ws.close(); - } - - function call(api, verb, request, onsuccess, onerror) { - var id = String(++this.counter); - this.pendings[id] = { onsuccess: onsuccess, onerror: onerror }; - var arr = [CALL, id, api+"/"+verb, request ]; - if (this.ctx.token) arr.push(this.ctx.token); - this.ws.send(JSON.stringify(arr)); - } - - AfbWsItf.prototype = { - close: close, - call: call - }; - - return AfbWsItf; -})(); - diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 370176d..2da26c2 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -2,6 +2,13 @@ + + + + AlsaMixerHal.h + + + ConnectionInfo.cpp @@ -57,6 +64,8 @@ true + + -- cgit 1.2.3-korg