aboutsummaryrefslogtreecommitdiffstats
path: root/ALSA-afb/Alsa-AddCtl.c
blob: 25e2a31bcac1f20da4e2f741179015a9c4a3d9c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Liter
/*
 * 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 <alsa/asoundlib.h>
#include <alsa/sound/tlv.h>
#include <systemd/sd-event.h>
#include <sys/ioctl.h>

#include "Alsa-ApiHat.h"

// Performs like a toggle switch for attenuation, because they're bool (ref:user-ctl-element-set.c)
static const unsigned int *allocate_bool_elem_set_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 json_object * addOneSndCtl(afb_req request, snd_ctl_t  *ctlDev, json_object *ctlJ) {
    int err, ctlNumid;
    json_object *tmpJ;
    ctlRequestT ctlRequest;
    const char *ctlName;
    int  ctlMax, ctlMin, ctlStep, ctlCount, ctlSubDev, ctlSndDev;
    snd_ctl_elem_type_t  ctlType;
    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, "numid" , &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;
    }
    
    // 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)snd_ctl_elem_info_set_numid(elemInfo, ctlNumid);
    snd_ctl_elem_info_set_interface (elemInfo, SND_CTL_ELEM_IFACE_MIXER);
    err = snd_ctl_elem_info(ctlDev, elemInfo);
    if (!err) {
        AFB_NOTICE ("ctlName=%s numid=%d already exit", snd_ctl_elem_info_get_name(elemInfo), snd_ctl_elem_info_get_numid(elemInfo));
        snd_ctl_elem_id_alloca(&elemId);    
        snd_ctl_elem_info_get_id(elemInfo, elemId);        
        goto OnSucessExit;
    }
    
    // default for json_object_get_int is zero
    json_object_object_get_ex (ctlJ, "min" , &tmpJ);
    ctlMin = json_object_get_int(tmpJ);

    json_object_object_get_ex (ctlJ, "max" , &tmpJ);
    if (!tmpJ) ctlMax=1;
    else ctlMax = json_object_get_int(tmpJ);
    
    json_object_object_get_ex (ctlJ, "step" , &tmpJ);
    if (!tmpJ) ctlStep=1;
    else ctlStep = json_object_get_int(tmpJ);
    
    json_object_object_get_ex (ctlJ, "count" , &tmpJ);
    if (!tmpJ) ctlCount=2;
    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);
   
    json_object_object_get_ex (ctlJ, "type" , &tmpJ);
    if (!tmpJ) ctlType=SND_CTL_ELEM_TYPE_BOOLEAN;
    else ctlType = json_object_get_int(tmpJ);
    
    // Add requested ID into elemInfo
    snd_ctl_elem_info_set_device(elemInfo, ctlSndDev);
    snd_ctl_elem_info_set_subdevice(elemInfo, ctlSubDev);

    // prepare value set
    snd_ctl_elem_value_alloca(&elemValue);
    
    switch (ctlType) {
        case SND_CTL_ELEM_TYPE_BOOLEAN:
            err = snd_ctl_add_boolean_elem_set(ctlDev, elemInfo, 1, ctlCount);
            if (err) {
                afb_req_fail_f (request, "ctl-invalid-bool", "devid=%s crl=%s invalid boolean data", snd_ctl_name(ctlDev), json_object_get_string(ctlJ));
                goto OnErrorExit;                
            }            

            elemTlv = allocate_bool_elem_set_tlv();
                    
            // Provide FALSE as default value
            for (int idx=0; idx < ctlCount; idx ++) {
                snd_ctl_elem_value_set_boolean (elemValue, idx, 1);
            }           
            break;
            
        case SND_CTL_ELEM_TYPE_INTEGER:
            err = snd_ctl_add_integer_elem_set (ctlDev, elemInfo, 1, ctlCount, ctlMin, ctlMax, ctlStep);
            if (err) {
                afb_req_fail_f (request, "ctl-invalid-bool", "devid=%s crl=%s invalid boolean data", snd_ctl_name(ctlDev), json_object_get_string(ctlJ));
                goto OnErrorExit;                
            }            

            // Provide 0 as default value
            for (int idx=0; idx < ctlCount; idx ++) {
                snd_ctl_elem_value_set_integer (elemValue, idx, 0);
            }            
            break;
            
        case SND_CTL_ELEM_TYPE_INTEGER64:
            err = snd_ctl_add_integer64_elem_set (ctlDev, elemInfo, 1, ctlCount, ctlMin, ctlMax, ctlStep);
            if (err) {
                afb_req_fail_f (request, "ctl-invalid-bool", "devid=%s crl=%s invalid boolean data", snd_ctl_name(ctlDev), json_object_get_string(ctlJ));
                goto OnErrorExit;                
            }            

            // Provide 0 as default value
            for (int idx=0; idx < ctlCount; idx ++) {
                snd_ctl_elem_value_set_integer64 (elemValue, idx, 0);
            }            
            break;
            
        case SND_CTL_ELEM_TYPE_ENUMERATED:            
        case SND_CTL_ELEM_TYPE_BYTES:
        default:
            afb_req_fail_f (request, "ctl-invalid-type", "crl=%s invalid/unknown type", json_object_get_string(ctlJ));
            goto OnErrorExit;                
    }

    // write default values in newly created control
    snd_ctl_elem_id_alloca(&elemId);    
    snd_ctl_elem_info_get_id(elemInfo, elemId);
    snd_ctl_elem_value_set_id(elemValue, elemId);
    err =  snd_ctl_elem_write (ctlDev, elemValue);
    if (err < 0) {
        afb_req_fail_f (request, "ctl-write-fail", "crl=%s numid=%d fail to write data error=%s", json_object_get_string(ctlJ), snd_ctl_elem_info_get_numid(elemInfo), snd_strerror(err));
        goto OnErrorExit;                
    }            
    
    // write a default null TLV (if usefull should be implemented for every ctl type) 
    if (elemTlv) {
        err=snd_ctl_elem_tlv_write (ctlDev, elemId, elemTlv);
        if (err < 0) {
            afb_req_fail_f (request, "TLV-write-fail", "crl=%s numid=%d fail to write data error=%s", json_object_get_string(ctlJ), snd_ctl_elem_info_get_numid(elemInfo), snd_strerror(err));
            goto OnErrorExit;                
        }
    }    

    // return newly created as a JSON object
    OnSucessExit:
        alsaGetSingleCtl (ctlDev, elemId, &ctlRequest, 0);
        if (ctlRequest.used < 0) goto OnErrorExit;   
        return ctlRequest.jValues;
    
    OnErrorExit:
        return NULL;
}

PUBLIC void alsaAddCustomCtls(afb_req request) {
    int err;
    json_object *ctlsJ, *ctlsValues, *ctlValues;
    enum json_type;
    snd_ctl_t  *ctlDev=NULL;
    const char *devid;

    devid = afb_req_value(request, "devid");
    if (devid == NULL) {
        afb_req_fail_f (request, "devid-missing", "devid MUST be defined for alsaAddCustomCtls");
        goto OnErrorExit;
    }
    
    // open control interface for devid
    err = snd_ctl_open(&ctlDev, devid, 0);
    if (err < 0) {
        afb_req_fail_f (request, "devid-unknown", "SndCard devid=[%s] Not Found err=%s", devid, snd_strerror(err));
        goto OnErrorExit;
    }
    
    // extract sound controls and parse json
    ctlsJ = json_tokener_parse (afb_req_value(request, "ctls"));
    if (!ctlsJ) {
        afb_req_fail_f (request, "ctls-missing", "ctls MUST be defined as a JSON array for alsaAddCustomCtls");
        goto OnErrorExit;
    }
     
    switch (json_object_get_type(ctlsJ)) { 
        case json_type_object:
             ctlsValues= addOneSndCtl(request, ctlDev, ctlsJ);
             
             break;
        
        case json_type_array:
            ctlsValues= json_object_new_array();
            for (int idx= 0; idx < json_object_array_length (ctlsJ); idx++) {
                json_object *ctlJ = json_object_array_get_idx (ctlsJ, idx);
                ctlValues= addOneSndCtl(request, ctlDev, ctlJ) ;
                if (ctlValues) json_object_array_add (ctlsValues, ctlValues);
            }
            break;
            
        default:
            afb_req_fail_f (request, "ctls-invalid","ctls=%s not valid JSON array", json_object_get_string(ctlsJ));
            goto OnErrorExit;
    }
    
    // get ctl as a json response
    afb_req_success(request, ctlsValues, NULL);
            
    OnErrorExit:
        if (ctlDev) snd_ctl_close(ctlDev);   
        return;
}