aboutsummaryrefslogtreecommitdiffstats
path: root/AlsaSound/HALayer/Shared/SharedHalLib.c
blob: ffa25e0f96a98aeb119330f9ed2c13238ed93cee (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

@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: #bb006
/*
 * Copyright (C) 2016 "IoT.bzh"
 * Author 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.
 * 
 * reference: 
 *   amixer contents; amixer controls;
 *   http://www.tldp.org/HOWTO/Alsa-sound-6.html 
 */
#define _GNU_SOURCE  // needed for vasprintf
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <math.h>
#include <sys/time.h>
#include <sys/types.h>

#include <json-c/json.h>
#include <afb/afb-binding.h>
#include <afb/afb-service-itf.h>

#include "MiscHelpers.h"
#include "AlsaHalIface.h"

typdef struct {
    int numid;
} shareHallMap_T;

static struct afb_service srvitf;
static const struct afb_binding_interface *afbIface;
static shareHallMap_T *shareHallMap;


STATIC void localping(struct afb_req request) {
    json_object *query = afb_req_json(request);
    afb_req_success(request, query, NULL); 
}

// This callback when api/alsacore/subscribe 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
STATIC void halMonitor(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
STATIC void halSubscribe (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
STATIC void halGetVol(struct afb_req request) {
   
    // Should call here Hardware Alsa Abstraction Layer for corresponding Sound Card
    afb_req_success (request, NULL, NULL);
    return;
    
}

STATIC void halSetVol(struct afb_req request) {
    const char *arg;
    const char *pcm;
    
    arg = afb_req_value(request, "vol");
    if (arg == NULL) {
        afb_req_fail_f (request, "argument-missing", "vol=[0,100] missing");
        goto OnErrorExit;
    }
    
    pcm = afb_req_value(request, "pcm");
    if (pcm == NULL) pcm="Master";
   
    // Should call here Hardware Alsa Abstraction Layer for corresponding Sound Card
    afb_req_success (request, NULL, NULL);
    return;
    
  OnErrorExit:
    return;
    
}

// this is call when after all bindings are loaded
STATIC int halGetControl(struct afb_service service) {
    srvitf = service;
    struct json_object *queryin, *queryout, *ctls, *devid;

    // get query from request
    queryin = afb_req_json(request);

    // check devid was given
    devid= json_object_object_get(queryin,"devid");
    if (!ctls) {
        afb_req_fail_f(request, "devid-notfound", "No DevID given query=[%s]", json_object_get_string(queryin));
        goto OnErrorExit;
    }
    
    // loop on requested controls
    ctls= json_object_object_get(queryin,"ctls");
    if (!ctls || json_object_array_length(ctls) <= 0) {
        afb_req_fail_f(request, "ctls-notfound", "No Controls given query=[%s]", json_object_get_string(queryin));
        goto OnErrorExit;
    }
    
    for (int idx=0; idx< json_object_array_length(ctls), idx++) {
        struct json_object *ctl;
        halControlEnumT control;
        int value;
        
        // each controls should be halControlEnumT+value
        ctl = json_object_array_get_idx(ctls, idx);
        if (json_object_array_length(ctl != 2)) {
            afb_req_fail_f(request, "ctl-invalid", "Invalid Control devid=%s ctl=[%s]"json_object_get_string(devid), json_object_get_string(cls)); 
            goto OnErrorExit;                     
        }
        
        // As HAL and Business logic use the same AlsaMixerHal.h direct conversion is not an issue
        control = (halControlEnumT)json_object_get_int(json_object_array_get_idx(ctl,0));
        value   = json_object_get_int(json_object_array_get_idx(ctl,0));
        
        if (control >= EndHalCrlTag || control <= StartHalCrlTag) {
            afb_req_fail_f(request, "ctl-invalid", "Invalid Control devid=%s ctl=[%s] should be [%d=%d]"
                          , json_object_get_string(devid), json_object_get_string(cls)), StartHalCrlTag, EndHalCrlTag; 
            goto OnErrorExit;                                 
        }
        
 
            default:
          NOTICE (afbIface, "audioLogicOpenCB2 unknown HAL control=[%s]", json_object_get_string(ctl)):
        }       
    }
    
    
    // register HAL with Alsa Low Level Binder devid=hw:0&numid=1&quiet=0
    queryurl=json_object_new_object();
    json_object_object_add(queryurl, "prefix",json_object_new_string(alsaHalBinding.v1.prefix));
    json_object_object_add(queryurl, "name"  ,json_object_new_string(alsaHalSndCard.name));
    afb_service_call(srvitf, "alsacore",  "registerHal", queryurl, halGetControlCB, queryurl);
    
 
    afb_req_success (request, sndctrls, NULL);
    return;
    
OnErrorExit:
        
};

STATIC void halInitCB (void *handle, int iserror, struct json_object *result) {
    struct json_object *queryurl = (json_object*)handle;

    if (iserror) NOTICE (afbIface, "halInitCB: registration alsaHAL query=[%s] Fail", json_object_to_json_string(queryurl));
    else DEBUG(afbIface, "halInitCB: registration alsaHAL card=[%s] Success", json_object_to_json_string(queryurl));
}

// 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) {
    srvitf = service;
    struct json_object *queryurl;
    
    // API prefix is used as sndcard halname
   alsaHalBinding.v1.prefix= prefix;
    
    // register HAL with Alsa Low Level Binder
    queryurl=json_object_new_object();
    json_object_object_add(ctx->queryurl, "prefix",json_object_new_string(alsaHalBinding.v1.prefix));
    json_object_object_add(ctx->queryurl, "name"  ,json_object_new_string(alsaHalSndCard.name));
    afb_service_call(srvitf, "alsacore",  "registerHal", queryurl, halInitCB, NULL);

    return 0;
};


// Every HAL export the same API & Interface Mapping from SndCard to AudioLogic is done through alsaHalSndCardT
STATIC const struct afb_verb_desc_v1 halSharedApi[] = {
  /* VERB'S NAME            SESSION MANAGEMENT          FUNCTION TO CALL         SHORT DESCRIPTION */
  { .name= "ping"   ,    .session= AFB_SESSION_NONE, .callback= localping,    .info= "Ping Binding" },
  { .name= "getcontrol", .session= AFB_SESSION_NONE, .callback= halGetControl,.info= "Get Control" },
  { .name= "setvolume",  .session= AFB_SESSION_NONE, .callback= halSetVol,    .info= "Set Volume" },
  { .name= "getvolume",  .session= AFB_SESSION_NONE, .callback= halGetVol,    .info= "Get Volume" },
  { .name= "subscribe",  .session= AFB_SESSION_NONE, .callback= halSubscribe, .info= "Subscribe AudioBinding Events" },
  { .name= NULL } /* marker for end of the array */
};

// Process HAL mapping from alsaHalSndCardT before registering HAL binder
PUBLIC const struct afb_binding *afbBindingV1Register(const struct afb_binding_interface *itf) {
    int count;
    afbIface= itf;                     // need to keep a static trace of binder interface for avances functions
    alsaHalBinding.verbs=halSharedApi; // complete sndcard specific alsaHalBinding with standard HAL APIs
    alsaHalCtlMapT  *alsaHalCtls = MapalsaHalSndCard.alsaHalCtlsMap; // Get sndcard specific HAL control mapping
    
    if (alsaHalCtls == NULL) {
        ERROR (afbIface, "afbBindingV1Register Fail alsaHalCtlsMap==NULL");
        return NULL;
    }
    
    // Create a zone to store HAL high/low level mapping
    shareHallMap = calloc (EndHalCrlTag * sizeof(shareHallMap_T));
    for (int idx= 0; alsaHalCtlsMap[idx].numid != 0; idx++) {
        shareHallMap[alsaHalCtlsMap[idx].]->numid  = alsaHalCtlsMap[idx].numid;
    }
    
    return alsaHalBinding;	/* returns the description of the binding */
}