/* * AlsaUseCase -- 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://kernel.readthedocs.io/en/sphinx-samples/writing-an-alsa-driver.html#control-names * https://01.org/linuxgraphics/gfx-docs/drm/sound/designs/control-names.html */ #define _GNU_SOURCE // needed for vasprintf #include #include #include #include #include #include "Alsa-ApiHat.h" // Performs like a toggle switch for attenuation, because they're bool (ref:user-ctl-element-set.c) #ifndef SNDRV_CTL_TLVD_DECLARE_DB_MINMAX // taken from alsa-lib-1.1.3:include/sound/tlv.h #define SNDRV_CTL_TLVD_LENGTH(...) \ ((unsigned int)sizeof((const unsigned int[]) { __VA_ARGS__ })) #define SNDRV_CTL_TLVD_ITEM(type, ...) \ (type), SNDRV_CTL_TLVD_LENGTH(__VA_ARGS__), __VA_ARGS__ #define SNDRV_CTL_TLVT_DB_MINMAX 4 #define SNDRV_CTL_TLVD_DB_MINMAX_ITEM(min_dB, max_dB) \ SNDRV_CTL_TLVD_ITEM(SNDRV_CTL_TLVT_DB_MINMAX, (min_dB), (max_dB)) #define SNDRV_CTL_TLVD_DECLARE_DB_MINMAX(name, min_dB, max_dB) \ unsigned int name[] = { \ SNDRV_CTL_TLVD_DB_MINMAX_ITEM(min_dB, max_dB) \ } #define SNDRV_CTL_TLVD_DB_SCALE_MASK 0xffff #define SNDRV_CTL_TLVD_DB_SCALE_MUTE 0x10000 #endif static const unsigned int *allocate_bool_fake_tlv(void) { static const SNDRV_CTL_TLVD_DECLARE_DB_MINMAX(range, -10000, 0); unsigned int *tlv = malloc(sizeof (range)); if (tlv == NULL) return NULL; memcpy(tlv, range, sizeof (range)); return tlv; } static const unsigned int *allocate_int_dbscale_tlv(int min, int step, int mute) { // SNDRV_CTL_TLVD_DECLARE_DB_SCALE(range, min, step, mute); size_t tlvSize = sizeof (4 * sizeof (unsigned int)); unsigned int *tlv = malloc(tlvSize); tlv[0] = SNDRV_CTL_TLVT_DB_LINEAR; tlv[1] = (int) tlvSize; tlv[2] = min * 100; tlv[3] = ((step * 100) & SNDRV_CTL_TLVD_DB_SCALE_MASK) | ((mute * 100) ? SNDRV_CTL_TLVD_DB_SCALE_MUTE : 0); return tlv; } static const unsigned int *allocate_int_linear_tlv(int max, int min) { // SNDRV_CTL_TLVD_DECLARE_DB_LINEAR (range, min, max); unsigned int *tlv = malloc(4 * sizeof (unsigned int)); tlv[0] = SNDRV_CTL_TLVT_DB_LINEAR; tlv[1] = (unsigned int)(2 * sizeof (unsigned int)); tlv[2] = (unsigned int)(-min * 100); tlv[3] = (unsigned int)(max * 100); return tlv; } STATIC json_object * addOneSndCtl(afb_req request, snd_ctl_t *ctlDev, json_object *ctlJ, queryModeE queryMode) { int err, done, ctlNumid, ctlValue=0, shouldCreate; json_object *tmpJ; const char *ctlName; ctlRequestT ctlRequest; int ctlMax=100, ctlMin=0, ctlStep, ctlCount, ctlSubDev=0, ctlSndDev=0; snd_ctl_elem_type_t ctlType=SND_CTL_ELEM_TYPE_NONE; snd_ctl_elem_info_t *elemInfo; snd_ctl_elem_id_t *elemId; snd_ctl_elem_value_t *elemValue; const unsigned int *elemTlv = NULL; // parse json ctl object json_object_object_get_ex(ctlJ, "name", &tmpJ); ctlName = json_object_get_string(tmpJ); json_object_object_get_ex(ctlJ, "ctl", &tmpJ); ctlNumid = json_object_get_int(tmpJ); if (!ctlNumid && !ctlName) { afb_req_fail_f(request, "ctl-invalid", "crl=%s name or numid missing", json_object_get_string(ctlJ)); goto OnErrorExit; } // We are facing a software ctl if (ctlNumid == CTL_AUTO) { done = json_object_object_get_ex(ctlJ, "type", &tmpJ); if (done) ctlType = json_object_get_int(tmpJ); else ctlType = SND_CTL_ELEM_TYPE_INTEGER; json_object_object_get_ex(ctlJ, "val", &tmpJ); ctlValue = json_object_get_int(tmpJ); // default for json_object_get_int is zero json_object_object_get_ex(ctlJ, "min", &tmpJ); ctlMin = json_object_get_int(tmpJ); done = json_object_object_get_ex(ctlJ, "max", &tmpJ); if (done) ctlMax = json_object_get_int(tmpJ); else { if (ctlType == SND_CTL_ELEM_TYPE_BOOLEAN) ctlMax = 1; else ctlMax = 100; } done = json_object_object_get_ex(ctlJ, "step", &tmpJ); if (!done) ctlStep = 1; else ctlStep = json_object_get_int(tmpJ); done = json_object_object_get_ex(ctlJ, "count", &tmpJ); if (!done) ctlCount = 1; else ctlCount = json_object_get_int(tmpJ); json_object_object_get_ex(ctlJ, "snddev", &tmpJ); ctlSndDev = json_object_get_int(tmpJ); json_object_object_get_ex(ctlJ, "subdev", &tmpJ); ctlSubDev = json_object_get_int(tmpJ); } // Assert that this ctls is not used snd_ctl_elem_info_alloca(&elemInfo); if (ctlName) snd_ctl_elem_info_set_name(elemInfo, ctlName); if (ctlNumid > 0)snd_ctl_elem_info_set_numid(elemInfo, ctlNumid); snd_ctl_elem_info_set_interface(elemInfo, SND_CTL_ELEM_IFACE_MIXER); // If control exist try to reuse it err = snd_ctl_elem_info(ctlDev, elemInfo); if (err) { shouldCreate = 1; } else { int count, min, max, sndDev, subDev; snd_ctl_elem_type_t type; // ctl exit let's get associated elemID snd_ctl_elem_id_alloca(&elemId); snd_ctl_elem_info_get_id(elemInfo, elemId); // If this is
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Note:
Individual files contain the following tag instead of the full license text.

    SPDX-License-Identifier: MIT

This enables machine processing of license information based on the SPDX
License Identifiers that are here available: http://spdx.org/licenses/