diff options
author | fulup <fulup.arfoll@iot.bzh> | 2017-07-25 11:37:10 +0200 |
---|---|---|
committer | fulup <fulup.arfoll@iot.bzh> | 2017-07-25 11:37:10 +0200 |
commit | 593ed6dc72274f86046aae3dc6b264c568241917 (patch) | |
tree | 32615a58bedbc0b2fcdf05db4a66d15ceafd763a | |
parent | a7a6ae4be87ee9178905ab784d84a4c0a8303f9b (diff) |
Initial version of HAL with Set/Get operation
-rw-r--r-- | ALSA-afb/Alsa-AddCtl.c | 2 | ||||
-rw-r--r-- | ALSA-afb/Alsa-ApiHat.h | 6 | ||||
-rw-r--r-- | ALSA-afb/Alsa-RegEvt.c | 33 | ||||
-rw-r--r-- | ALSA-afb/Alsa-SetGet.c | 236 | ||||
-rw-r--r-- | ALSA-afb/Alsa-Ucm.c | 39 | ||||
-rw-r--r-- | HAL-afb/HAL-interface/hal-interface.c | 150 | ||||
-rw-r--r-- | HAL-afb/HAL-interface/hal-interface.h | 7 | ||||
-rw-r--r-- | HAL-afb/HAL-interface/hal-volmap.c | 97 | ||||
-rw-r--r-- | HAL-afb/Scarlett-Focusrite/ScarlettUsbHAL.c | 1 | ||||
-rw-r--r-- | Shared-Interface/audio-interface.c | 28 | ||||
-rw-r--r-- | Shared-Interface/audio-interface.h | 15 | ||||
-rw-r--r-- | conf.d/cmake/config.cmake | 1 | ||||
-rw-r--r-- | htdocs/AudioBinding.js | 102 | ||||
-rw-r--r-- | htdocs/alsa-core.html | 38 | ||||
-rw-r--r-- | htdocs/alsa-hal.html | 10 | ||||
-rw-r--r-- | nbproject/configurations.xml | 54 |
16 files changed, 437 insertions, 382 deletions
diff --git a/ALSA-afb/Alsa-AddCtl.c b/ALSA-afb/Alsa-AddCtl.c index 4635503..b7e3d90 100644 --- a/ALSA-afb/Alsa-AddCtl.c +++ b/ALSA-afb/Alsa-AddCtl.c @@ -183,7 +183,7 @@ STATIC json_object * addOneSndCtl(afb_req request, snd_ctl_t *ctlDev, json_obje OnSucessExit: alsaGetSingleCtl (ctlDev, elemId, &ctlRequest, queryMode); if (ctlRequest.used < 0) goto OnErrorExit; - return ctlRequest.jValues; + return ctlRequest.valuesJ; OnErrorExit: return NULL; diff --git a/ALSA-afb/Alsa-ApiHat.h b/ALSA-afb/Alsa-ApiHat.h index 6c9f4c3..6e50e11 100644 --- a/ALSA-afb/Alsa-ApiHat.h +++ b/ALSA-afb/Alsa-ApiHat.h @@ -32,7 +32,7 @@ typedef enum { // generic structure to pass parsed query values typedef struct { const char *devid; - json_object *jNumIds; + json_object *numidsJ; halQueryMode mode; int count; } queryValuesT; @@ -41,13 +41,13 @@ typedef struct { typedef struct { unsigned int numId; json_object *jToken; - json_object *jValues; + json_object *valuesJ; int used; } ctlRequestT; // import from AlsaAfbBinding extern const struct afb_binding_interface *afbIface; -PUBLIC int alsaCheckQuery (struct afb_req request, queryValuesT *queryValues); +PUBLIC json_object *alsaCheckQuery (struct afb_req request, queryValuesT *queryValues); // AlseCoreSetGet exports PUBLIC int alsaGetSingleCtl (snd_ctl_t *ctlDev, snd_ctl_elem_id_t *elemId, ctlRequestT *ctlRequest, halQueryMode queryMode); diff --git a/ALSA-afb/Alsa-RegEvt.c b/ALSA-afb/Alsa-RegEvt.c index 7b0a722..df6570a 100644 --- a/ALSA-afb/Alsa-RegEvt.c +++ b/ALSA-afb/Alsa-RegEvt.c @@ -44,6 +44,31 @@ typedef struct { cardRegistryT *cardRegistry[MAX_SND_CARD+1]; +PUBLIC json_object *alsaCheckQuery (afb_req request, queryValuesT *queryValues) { + + json_object *tmpJ; + int done; + + // get query from request + json_object *queryInJ = afb_req_json(request); + + done= json_object_object_get_ex (queryInJ, "devid" , &tmpJ); + if (!done) { + afb_req_fail_f(request, "devid-missing", "Invalid query='%s'", json_object_get_string(queryInJ)); + goto OnErrorExit; + } + queryValues->devid = json_object_get_string(tmpJ); + + done= json_object_object_get_ex (queryInJ, "mode" , &tmpJ); + if (!done) queryValues->mode=QUERY_QUIET; // default quiet + else queryValues->mode = json_object_get_int (tmpJ); + + return queryInJ; + +OnErrorExit: + return NULL; +} + // 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; @@ -97,7 +122,7 @@ STATIC int sndCtlEventCB (sd_event_source* src, int fd, uint32_t revents, void* json_object_object_add(ctlEventJ, "iface" ,json_object_new_int (iface)); json_object_object_add(ctlEventJ, "devname",json_object_new_string (devname)); } - if (ctlRequest.jValues) (json_object_object_add(ctlEventJ, "values" ,ctlRequest.jValues)); + if (ctlRequest.valuesJ) (json_object_object_add(ctlEventJ, "value" ,ctlRequest.valuesJ)); AFB_DEBUG( "sndCtlEventCB=%s", json_object_get_string(ctlEventJ)); afb_event_push(evtHandle->afbevt, ctlEventJ); } @@ -119,10 +144,8 @@ PUBLIC void alsaEvtSubcribe (afb_req request) { snd_ctl_card_info_t *cardinfo; queryValuesT queryValues; - - err = alsaCheckQuery (request, &queryValues); - if (err) goto OnErrorExit; - + json_object *queryJ = alsaCheckQuery (request, &queryValues); + if (!queryJ) goto OnErrorExit; // open control interface for devid err = snd_ctl_open(&ctlDev, queryValues.devid, SND_CTL_READONLY); diff --git a/ALSA-afb/Alsa-SetGet.c b/ALSA-afb/Alsa-SetGet.c index 0815f35..50b62ce 100644 --- a/ALSA-afb/Alsa-SetGet.c +++ b/ALSA-afb/Alsa-SetGet.c @@ -21,62 +21,69 @@ http://alsa.opensrc.org/HowTo_access_a_mixer_control https://github.com/gch1p/alsa-volume-monitor/blob/master/main.c https://github.com/DongheonKim/android_hardware_alsa-sound/blob/master/ALSAControl.cpp (ALSA low level API) + https://www.kernel.org/doc/html/v4.11/sound/index.html */ #define _GNU_SOURCE // needed for vasprintf - #include "Alsa-ApiHat.h" -PUBLIC void NumidsListParse (queryValuesT *queryValues, ctlRequestT *ctlRequest) { - json_object *jValues; +PUBLIC void NumidsListParse (ActionSetGetT action, queryValuesT *queryValues, ctlRequestT *ctlRequest) { int length; - - for (int idx=0; idx < queryValues->count; idx ++) { - ctlRequest[idx].jToken = json_object_array_get_idx (queryValues->jNumIds, idx); - ctlRequest[idx].jValues = NULL; - ctlRequest[idx].used=0; + for (int idx=0; idx < queryValues->count; idx ++) { + json_object *jId, *valuesJ; + ctlRequest[idx].used=0; + ctlRequest[idx].valuesJ = NULL; + + // when only one NUMID is provided it might not be encapsulated in a JSON array + if (json_type_array == json_object_get_type(queryValues->numidsJ)) ctlRequest[idx].jToken = json_object_array_get_idx (queryValues->numidsJ, idx); + else ctlRequest[idx].jToken=queryValues->numidsJ; + enum json_type jtype=json_object_get_type(ctlRequest[idx].jToken); switch (jtype) { - json_object *jId, *jVal; case json_type_int: // if NUMID is not an array then it should be an integer numid with no value ctlRequest[idx].numId = json_object_get_int (ctlRequest[idx].jToken); + + // Special SET simple short numid form [numid, [VAL1...VALX]] + if (action == ACTION_SET && queryValues->count == 2) { + ctlRequest[idx].valuesJ = json_object_array_get_idx (queryValues->numidsJ, 1); + queryValues->count =1; //In this form count==2 , when only one numid is to set + idx ++; + continue; + } else break; case json_type_array: // NUMID is an array 1st slot should be numid, optionally values may come after length=json_object_array_length (ctlRequest[idx].jToken); - if (length < 1 || length >2) { - ctlRequest[idx].used=-1; - continue; - } + // numid must be in 1st slot of numid json array ctlRequest[idx].numId =json_object_get_int(json_object_array_get_idx (ctlRequest[idx].jToken, 0)); + if (action == ACTION_GET) continue; - if (length == 2) { - jValues = json_object_array_get_idx (ctlRequest[idx].jToken, 1); - if (jValues == NULL) { - ctlRequest[idx].used=-1; - continue; - } - // Value is an int or an array with potentially multiple subvalues - ctlRequest[idx].jValues = jValues; + // In Write mode second value should be the value + if (action == ACTION_SET && length == 2) { + ctlRequest[idx].valuesJ = json_object_array_get_idx (ctlRequest[idx].jToken, 1); + continue; } + + // no numid value + ctlRequest[idx].used=-1; break; case json_type_object: // numid+values formated as {id:xxx, val:[aa,bb...,nn]} - if (!json_object_object_get_ex (ctlRequest[idx].jToken,"id", &jId) || !json_object_object_get_ex (ctlRequest[idx].jToken,"val",&jVal)) { + if (!json_object_object_get_ex (ctlRequest[idx].jToken,"id", &jId) || !json_object_object_get_ex (ctlRequest[idx].jToken,"val",&valuesJ)) { AFB_NOTICE("Invalid Json=%s missing 'id'|'val'", json_object_get_string(ctlRequest[idx].jToken)); ctlRequest[idx].used=-1; } else { ctlRequest[idx].numId =json_object_get_int(jId); - ctlRequest[idx].jValues =jVal; + if (action == ACTION_SET) ctlRequest[idx].valuesJ=valuesJ; } - + break; default: ctlRequest[idx].used=-1; @@ -84,59 +91,6 @@ PUBLIC void NumidsListParse (queryValuesT *queryValues, ctlRequestT *ctlRequest) } } -PUBLIC int alsaCheckQuery (afb_req request, queryValuesT *queryValues) { - queryValues->devid = afb_req_value(request, "devid"); - if (queryValues->devid == NULL) goto OnErrorExit; - const char *numids; - json_object *jNumIds; - - const char *rqtmode = afb_req_value(request, "mode"); - if (!rqtmode) queryValues->mode=QUERY_QUIET; // default quiet - else if (rqtmode && ! sscanf (rqtmode, "%d", (int)&queryValues->mode)) { - json_object *query = afb_req_json(request); - - afb_req_fail_f (request, "mode-notinteger","Query=%s mode not integer &mode=%s&", json_object_get_string(query), rqtmode); - goto OnErrorExit; - }; - - // no NumId is interpreted as ALL for get and error for set - numids = afb_req_value(request, "numid"); - if (numids == NULL) { - queryValues->count=0; - goto OnExit; - } - - jNumIds = json_tokener_parse(numids); - if (!jNumIds) { - afb_req_fail_f (request, "numids-notjson","numids=%s not a valid json entry", numids); - goto OnErrorExit; - }; - - enum json_type jtype= json_object_get_type(jNumIds); - switch (jtype) { - case json_type_array: - queryValues->jNumIds = jNumIds; - queryValues->count = json_object_array_length (jNumIds); - break; - - case json_type_int: - queryValues->count = 1; - queryValues->jNumIds = json_object_new_array (); - json_object_array_add (queryValues->jNumIds, jNumIds); - break; - - default: - afb_req_fail_f (request, "numid-notarray","NumId=%s NumId not valid JSON array", numids); - goto OnErrorExit; - } - -OnExit: - return 0; - -OnErrorExit: - return 1; -} - STATIC json_object *DB2StringJsonOject (long dB) { char label [20]; if (dB < 0) { @@ -380,14 +334,14 @@ STATIC json_object* alsaCardProbe (const char *rqtSndId) { int err; if ((err = snd_ctl_open(&handle, rqtSndId, 0)) < 0) { - AFB_INFO ("alsaCardProbe [%s] Not Found", rqtSndId); + AFB_INFO ("alsaCardProbe '%s' Not Found", rqtSndId); return NULL; } snd_ctl_card_info_alloca(&cardinfo); if ((err = snd_ctl_card_info(handle, cardinfo)) < 0) { snd_ctl_close(handle); - AFB_WARNING ("SndCard [%s] info error: %s", rqtSndId, snd_strerror(err)); + AFB_WARNING ("SndCard '%s' info error: %s", rqtSndId, snd_strerror(err)); return NULL; } @@ -427,7 +381,7 @@ PUBLIC void alsaGetInfo (afb_req request) { // only one card was requested let's probe it ctlDev = alsaCardProbe (rqtSndId); if (ctlDev != NULL) afb_req_success(request, ctlDev, NULL); - else afb_req_fail_f (request, "sndscard-notfound", "SndCard [%s] Not Found", rqtSndId); + else afb_req_fail_f (request, "sndscard-notfound", "SndCard '%s' Not Found", rqtSndId); } else { // return an array of ctlDev @@ -487,22 +441,22 @@ PUBLIC int alsaSetSingleCtl (snd_ctl_t *ctlDev, snd_ctl_elem_id_t *elemId, ctlRe snd_ctl_elem_info_alloca(&elemInfo); snd_ctl_elem_info_set_id(elemInfo, elemId); // map ctlInfo to ctlId elemInfo is updated !!! if (snd_ctl_elem_info(ctlDev, elemInfo) < 0) { - AFB_NOTICE( "Fail to load ALSA NUMID=%d Values=[%s]", ctlRequest->numId, json_object_get_string(ctlRequest->jValues)); + AFB_NOTICE( "Fail to load ALSA NUMID=%d Values='%s'", ctlRequest->numId, json_object_get_string(ctlRequest->valuesJ)); goto OnErrorExit; } if (!snd_ctl_elem_info_is_writable(elemInfo)) { - AFB_NOTICE( "Not Writable ALSA NUMID=%d Values=[%s]", ctlRequest->numId, json_object_get_string(ctlRequest->jValues)); + AFB_NOTICE( "Not Writable ALSA NUMID=%d Values='%s'", ctlRequest->numId, json_object_get_string(ctlRequest->valuesJ)); goto OnErrorExit; } count = snd_ctl_elem_info_get_count (elemInfo); if (count == 0) goto OnErrorExit; - - enum json_type jtype= json_object_get_type(ctlRequest->jValues); + + enum json_type jtype= json_object_get_type(ctlRequest->valuesJ); switch (jtype) { case json_type_array: - length = json_object_array_length (ctlRequest->jValues); + length = json_object_array_length (ctlRequest->valuesJ); valueIsArray=1; break; case json_type_int: @@ -510,14 +464,13 @@ PUBLIC int alsaSetSingleCtl (snd_ctl_t *ctlDev, snd_ctl_elem_id_t *elemId, ctlRe valueIsArray=0; break; default: - count =0; length = 0; break; } - if (count == 0 || count < length) { - AFB_NOTICE( "Invalid values NUMID='%d' Values='%s' count='%d' wanted='%d'", ctlRequest->numId, json_object_get_string(ctlRequest->jValues), length, count); + if (length == 0) { + AFB_NOTICE( "Invalid values NUMID='%d' Values='%s' count='%d' wanted='%d'", ctlRequest->numId, json_object_get_string(ctlRequest->valuesJ), length, count); goto OnErrorExit; } @@ -526,12 +479,16 @@ PUBLIC int alsaSetSingleCtl (snd_ctl_t *ctlDev, snd_ctl_elem_id_t *elemId, ctlRe if (snd_ctl_elem_read(ctlDev, elemData) < 0) goto OnErrorExit; // Loop on every control value and push to sndcard - for (int index=0; index < count && index < length; index++) { + for (int index=0; index < count; index++) { json_object *element; int value; - - if (valueIsArray) element= json_object_array_get_idx(ctlRequest->jValues, index); - else element= ctlRequest->jValues; + + // when not enough value duplicate last provided one + if (!valueIsArray) element= ctlRequest->valuesJ; + else { + if (index < length) element= json_object_array_get_idx(ctlRequest->valuesJ, index); + else element= json_object_array_get_idx(ctlRequest->valuesJ, length-1); + } value= json_object_get_int (element); snd_ctl_elem_value_set_integer(elemData, index, value); @@ -539,7 +496,7 @@ PUBLIC int alsaSetSingleCtl (snd_ctl_t *ctlDev, snd_ctl_elem_id_t *elemId, ctlRe err = snd_ctl_elem_write(ctlDev, elemData); if (err < 0) { - AFB_NOTICE( "Fail to write ALSA NUMID=%d Values=[%s] Error=%s", ctlRequest->numId, json_object_get_string(ctlRequest->jValues), snd_strerror(err)); + AFB_NOTICE( "Fail to write ALSA NUMID=%d Values='%s' Error=%s", ctlRequest->numId, json_object_get_string(ctlRequest->valuesJ), snd_strerror(err)); goto OnErrorExit; } @@ -576,11 +533,11 @@ PUBLIC int alsaGetSingleCtl (snd_ctl_t *ctlDev, snd_ctl_elem_id_t *elemId, ctlRe int numid= snd_ctl_elem_info_get_numid(elemInfo); - ctlRequest->jValues= json_object_new_object(); - json_object_object_add (ctlRequest->jValues,"numid" , json_object_new_int(numid)); - if (queryMode >= 1) json_object_object_add (ctlRequest->jValues,"name" , json_object_new_string(snd_ctl_elem_id_get_name (elemId))); - if (queryMode >= 2) json_object_object_add (ctlRequest->jValues,"iface" , json_object_new_string(snd_ctl_elem_iface_name(snd_ctl_elem_id_get_interface(elemId)))); - if (queryMode >= 3) json_object_object_add (ctlRequest->jValues,"actif", json_object_new_boolean(!snd_ctl_elem_info_is_inactive(elemInfo))); + ctlRequest->valuesJ= json_object_new_object(); + json_object_object_add (ctlRequest->valuesJ,"id" , json_object_new_int(numid)); + if (queryMode >= 1) json_object_object_add (ctlRequest->valuesJ,"name" , json_object_new_string(snd_ctl_elem_id_get_name (elemId))); + if (queryMode >= 2) json_object_object_add (ctlRequest->valuesJ,"iface" , json_object_new_string(snd_ctl_elem_iface_name(snd_ctl_elem_id_get_interface(elemId)))); + if (queryMode >= 3) json_object_object_add (ctlRequest->valuesJ,"actif", json_object_new_boolean(!snd_ctl_elem_info_is_inactive(elemInfo))); json_object *jsonValuesCtl = json_object_new_array(); for (idx = 0; idx < count; idx++) { // start from one in amixer.c !!! @@ -618,7 +575,7 @@ PUBLIC int alsaGetSingleCtl (snd_ctl_t *ctlDev, snd_ctl_elem_id_t *elemId, ctlRe break; } } - json_object_object_add (ctlRequest->jValues,"val",jsonValuesCtl); + json_object_object_add (ctlRequest->valuesJ,"val",jsonValuesCtl); if (queryMode >= 1) { // in simple mode do not print usable values json_object *jsonClassCtl = json_object_new_object(); @@ -653,18 +610,18 @@ PUBLIC int alsaGetSingleCtl (snd_ctl_t *ctlDev, snd_ctl_elem_id_t *elemId, ctlRe } // add collected class info with associated ACLs - json_object_object_add (ctlRequest->jValues,"ctl", jsonClassCtl); + json_object_object_add (ctlRequest->valuesJ,"ctl", jsonClassCtl); - if (queryMode >= QUERY_FULL) json_object_object_add (ctlRequest->jValues,"acl" , getControlAcl (elemInfo)); + if (queryMode >= QUERY_FULL) json_object_object_add (ctlRequest->valuesJ,"acl" , getControlAcl (elemInfo)); // check for tlv [direct port from amixer.c] if (snd_ctl_elem_info_is_tlv_readable(elemInfo)) { - unsigned int *tlv = alloca(4096); + unsigned int *tlv = alloca(TLV_BYTE_SIZE); if ((err = snd_ctl_elem_tlv_read(ctlDev, elemId, tlv, 4096)) < 0) { fprintf (stderr, "Control %s element TLV read error\n", snd_strerror(err)); goto OnErrorExit; } else { - json_object_object_add (ctlRequest->jValues,"tlv", decodeTlv (tlv, 4096, queryMode)); + json_object_object_add (ctlRequest->valuesJ,"tlv", decodeTlv (tlv, TLV_BYTE_SIZE, queryMode)); } } } @@ -678,38 +635,61 @@ PUBLIC int alsaGetSingleCtl (snd_ctl_t *ctlDev, snd_ctl_elem_id_t *elemId, ctlRe } // assign multiple control to the same value -STATIC void alsaSetGetCtls (afb_req request, ActionSetGetT action) { +STATIC void alsaSetGetCtls (ActionSetGetT action, afb_req request) { ctlRequestT *ctlRequest; const char *warmsg=NULL; - int err=0, status=0; + int err=0, status=0, done; unsigned int ctlCount; snd_ctl_t *ctlDev; snd_ctl_elem_list_t *ctlList; json_object *sndctls=json_object_new_array();; queryValuesT queryValues; + json_object *queryJ, *numidsJ; - err = alsaCheckQuery (request, &queryValues); - if (err) goto OnErrorExit; + queryJ = alsaCheckQuery (request, &queryValues); + if (!queryJ) goto OnErrorExit; + // Prase Numids + optional values + done= json_object_object_get_ex (queryJ, "numid" , &numidsJ); + if (!done) queryValues.count=0; + else { + enum json_type jtype= json_object_get_type(numidsJ); + switch (jtype) { + case json_type_array: + queryValues.numidsJ = numidsJ; + queryValues.count = json_object_array_length (numidsJ); + break; + case json_type_int: + case json_type_object: + queryValues.count = 1; + queryValues.numidsJ = numidsJ; + break; + + default: + afb_req_fail_f (request, "numid-notarray","NumId=%s NumId not valid JSON array", json_object_get_string(numidsJ)); + goto OnErrorExit; + } + } + if ((err = snd_ctl_open(&ctlDev, queryValues.devid, 0)) < 0) { - afb_req_fail_f (request, "sndcrl-notfound","devid=[%s] load fail error=%s\n", queryValues.devid, snd_strerror(err)); + afb_req_fail_f (request, "sndcrl-notfound","devid='%s' load fail error=%s\n", queryValues.devid, snd_strerror(err)); goto OnErrorExit; } snd_ctl_elem_list_alloca(&ctlList); if ((err = snd_ctl_elem_list (ctlDev, ctlList)) < 0) { - afb_req_fail_f (request, "listInit-failed","devid=[%s] load fail error=%s\n", queryValues.devid, snd_strerror(err)); + afb_req_fail_f (request, "listInit-failed","devid='%s' load fail error=%s\n", queryValues.devid, snd_strerror(err)); goto OnErrorExit; } if ((err = snd_ctl_elem_list_alloc_space(ctlList, snd_ctl_elem_list_get_count(ctlList))) < 0) { - afb_req_fail_f (request, "listAlloc-failed","devid=[%s] load fail error=%s\n", queryValues.devid, snd_strerror(err)); + afb_req_fail_f (request, "listAlloc-failed","devid='%s' load fail error=%s\n", queryValues.devid, snd_strerror(err)); goto OnErrorExit; } if ((err = snd_ctl_elem_list (ctlDev, ctlList)) < 0) { - afb_req_fail_f (request, "listOpen-failed","devid=[%s] load fail error=%s\n", queryValues.devid, snd_strerror(err)); + afb_req_fail_f (request, "listOpen-failed","devid='%s' load fail error=%s\n", queryValues.devid, snd_strerror(err)); goto OnErrorExit; } @@ -719,7 +699,7 @@ STATIC void alsaSetGetCtls (afb_req request, ActionSetGetT action) { ctlRequest= alloca (sizeof(ctlRequestT)*(ctlCount)); } else { ctlRequest= alloca (sizeof(ctlRequestT)*(queryValues.count)); - NumidsListParse (&queryValues, ctlRequest); + NumidsListParse (action, &queryValues, ctlRequest); } // Loop on all ctlDev controls @@ -754,45 +734,47 @@ STATIC void alsaSetGetCtls (afb_req request, ActionSetGetT action) { switch (action) { case ACTION_GET: err = alsaGetSingleCtl (ctlDev, elemId, &ctlRequest[jdx], queryValues.mode); - break; + break; case ACTION_SET: err = alsaSetSingleCtl (ctlDev, elemId, &ctlRequest[jdx]); + break; default: err = 1; } if (err) status++; else { - json_object_array_add (sndctls, ctlRequest[jdx].jValues); + if (action == ACTION_GET) json_object_array_add (sndctls, ctlRequest[jdx].valuesJ); } } } // if we had error let's add them into response message info - json_object *warnings = json_object_new_array(); + json_object *warningsJ = json_object_new_array(); for (int jdx=0; jdx < queryValues.count; jdx++) { if (ctlRequest[jdx].used <= 0) { json_object *failctl = json_object_new_object(); json_object_object_add (failctl, "numid", ctlRequest[jdx].jToken); - if (ctlRequest[jdx].jValues) - json_object_object_add(failctl, "values", ctlRequest[jdx].jValues); + if (ctlRequest[jdx].valuesJ) json_object_object_add(failctl, "value", ctlRequest[jdx].valuesJ); + + AFB_NOTICE ("*** jToken=%s value=%s", json_object_get_string(ctlRequest[jdx].jToken), json_object_get_string(ctlRequest[jdx].valuesJ)); - if (ctlRequest[jdx].numId == -1) json_object_object_add (failctl, "info", json_object_new_string ("Invalid NumID")); + if (ctlRequest[jdx].numId == -1) json_object_object_add (failctl, "error", json_object_new_string ("Numid Invalid")); else { - if (ctlRequest[jdx].used == 0) json_object_object_add (failctl, "info", json_object_new_string ("Does Not Exist")); - if (ctlRequest[jdx].used == -1) json_object_object_add (failctl, "info", json_object_new_string ("Invalid Value")); + if (ctlRequest[jdx].used == 0) json_object_object_add (failctl, "error", json_object_new_string ("Numid Does Not Exist")); + if (ctlRequest[jdx].used == -1) json_object_object_add (failctl, "error", json_object_new_string ("Value invalid")); } - json_object_array_add (warnings, failctl); + json_object_array_add (warningsJ, failctl); } - /* WARNING!!!! Check with Jose why following put free jValues + /* WARNING!!!! Check with Jose why following put free valuesJ if (ctlRequest[jdx].jToken) json_object_put(ctlRequest[jdx].jToken); - if (ctlRequest[jdx].jValues) json_object_put(ctlRequest[jdx].jValues); + if (ctlRequest[jdx].valuesJ) json_object_put(ctlRequest[jdx].valuesJ); */ } - if (json_object_array_length(warnings)) warmsg=json_object_get_string (warnings); - else json_object_put(warnings); + if (json_object_array_length(warningsJ) > 0) warmsg=json_object_get_string (warningsJ); + else json_object_put(warningsJ); // send response+warning if any afb_req_success (request, sndctls, warmsg); @@ -803,11 +785,11 @@ STATIC void alsaSetGetCtls (afb_req request, ActionSetGetT action) { } PUBLIC void alsaGetCtls (afb_req request) { - alsaSetGetCtls (request, ACTION_GET); + alsaSetGetCtls (ACTION_GET, request); } PUBLIC void alsaSetCtls (afb_req request) { - alsaSetGetCtls (request, ACTION_SET); + alsaSetGetCtls (ACTION_SET, request); } diff --git a/ALSA-afb/Alsa-Ucm.c b/ALSA-afb/Alsa-Ucm.c index f512417..ee2b36d 100644 --- a/ALSA-afb/Alsa-Ucm.c +++ b/ALSA-afb/Alsa-Ucm.c @@ -111,16 +111,15 @@ STATIC int alsaUseCaseOpen (struct afb_req request, queryValuesT *queryValues, i PUBLIC void alsaUseCaseQuery(struct afb_req request) { - int err, verbCount, ucmIdx; + int verbCount, ucmIdx; const char **verbList; snd_use_case_mgr_t *ucmHandle; queryValuesT queryValues; json_object *ucmJs; const char *cardName; - - err = alsaCheckQuery (request, &queryValues); - if (err) goto OnErrorExit; + json_object *queryJ = alsaCheckQuery (request, &queryValues); + if (!queryJ) goto OnErrorExit; ucmIdx = alsaUseCaseOpen (request, &queryValues, TRUE); if (ucmIdx < 0) goto OnErrorExit; @@ -158,7 +157,7 @@ PUBLIC void alsaUseCaseQuery(struct afb_req request) { json_object_array_add (devsJ, devJ); } json_object_object_add(ucmJ,"devices", devsJ); - snd_use_case_free_list(devList, err); + snd_use_case_free_list(devList, devCount); } snprintf (identifier, sizeof(identifier), "_modifiers/%s", verbList[idx]); @@ -174,7 +173,7 @@ PUBLIC void alsaUseCaseQuery(struct afb_req request) { json_object_array_add (modsJ, modJ); } json_object_object_add(ucmJ,"modifiers", modsJ); - snd_use_case_free_list(modList, err); + snd_use_case_free_list(modList, modCount); } snprintf (identifier, sizeof(identifier), "TQ/%s", verbList[idx]); @@ -190,14 +189,14 @@ PUBLIC void alsaUseCaseQuery(struct afb_req request) { json_object_array_add (tqsJ, tqJ); } json_object_object_add(ucmJ,"tqs", tqsJ); - snd_use_case_free_list(tqList, err); + snd_use_case_free_list(tqList, tqCount); } json_object_array_add (ucmJs, ucmJ); } afb_req_success (request, ucmJs, NULL); - snd_use_case_free_list(verbList, err); + snd_use_case_free_list(verbList, verbCount); OnErrorExit: return; @@ -235,14 +234,15 @@ STATIC json_object *ucmGetValue (ucmHandleT *ucmHandle, const char *verb, const } PUBLIC void alsaUseCaseGet (struct afb_req request) { - int err, ucmIdx, labelCount; + int ucmIdx, labelCount; queryValuesT queryValues; json_object *jResponse = json_object_new_object(); json_object *jWarnings = json_object_new_array(); const char *warnings=NULL; - err = alsaCheckQuery (request, &queryValues); - if (err) goto OnErrorExit; + json_object *queryJ = alsaCheckQuery (request, &queryValues); + if (!queryJ) goto OnErrorExit; +; ucmIdx = alsaUseCaseOpen (request, &queryValues, TRUE); if (ucmIdx < 0) goto OnErrorExit; @@ -261,7 +261,7 @@ PUBLIC void alsaUseCaseGet (struct afb_req request) { // device selection is handle as a modifier if (dev) mod=dev; - const char *labels = afb_req_value(request, "values"); + const char *labels = afb_req_value(request, "value"); if (!labels) { afb_req_fail_f (request, "ucmget-labels", "SndCard devid=[%s] name=[%s] UCM values name missing", queryValues.devid, cardName); goto OnErrorExit; @@ -323,8 +323,9 @@ PUBLIC void alsaUseCaseSet(struct afb_req request) { queryValuesT queryValues; json_object *jResponse = json_object_new_object(); - err = alsaCheckQuery (request, &queryValues); - if (err) goto OnErrorExit; + json_object *queryJ = alsaCheckQuery (request, &queryValues); + if (!queryJ) goto OnErrorExit; + ucmIdx = alsaUseCaseOpen (request, &queryValues, TRUE); if (ucmIdx < 0) goto OnErrorExit; @@ -362,7 +363,7 @@ PUBLIC void alsaUseCaseSet(struct afb_req request) { } // label are requested transfert request to get - if (afb_req_value(request, "values")) return alsaUseCaseGet(request); + if (afb_req_value(request, "value")) return alsaUseCaseGet(request); if (queryValues.mode <= 3) { json_object *jValue; @@ -388,8 +389,8 @@ PUBLIC void alsaUseCaseReset (struct afb_req request) { int err, ucmIdx; queryValuesT queryValues; - err = alsaCheckQuery (request, &queryValues); - if (err) goto OnErrorExit; + json_object *queryJ = alsaCheckQuery (request, &queryValues); + if (!queryJ) goto OnErrorExit; ucmIdx = alsaUseCaseOpen (request, &queryValues, FALSE); if (ucmIdx < 0) goto OnErrorExit; @@ -410,8 +411,8 @@ PUBLIC void alsaUseCaseClose (struct afb_req request) { int err, ucmIdx; queryValuesT queryValues; - err = alsaCheckQuery (request, &queryValues); - if (err) goto OnErrorExit; + json_object *queryJ = alsaCheckQuery (request, &queryValues); + if (!queryJ) goto OnErrorExit; ucmIdx = alsaUseCaseOpen (request, &queryValues, FALSE); if (ucmIdx < 0) goto OnErrorExit; diff --git a/HAL-afb/HAL-interface/hal-interface.c b/HAL-afb/HAL-interface/hal-interface.c index 1f39a81..f4fc8df 100644 --- a/HAL-afb/HAL-interface/hal-interface.c +++ b/HAL-afb/HAL-interface/hal-interface.c @@ -52,12 +52,10 @@ STATIC void halSubscribe(afb_req request) { } } - - // Map HAL ctlName to ctlLabel STATIC int halCtlStringToIndex (const char* label) { - for (int idx = 0; halCtls[idx].ctl.numid; idx++) { + for (int idx = 0; halCtls[idx].tag != EndHalCrlTag; idx++) { if (!strcmp (halCtls[idx].label, label)) return idx; } @@ -67,7 +65,7 @@ STATIC int halCtlStringToIndex (const char* label) { STATIC int halCtlTagToIndex (halCtlsEnumT tag) { - for (int idx = 0; halCtls[idx].ctl.numid; idx++) { + for (int idx = 0; halCtls[idx].tag != EndHalCrlTag; idx++) { if (halCtls[idx].tag == tag) return idx; } @@ -93,122 +91,98 @@ PUBLIC void halListCtls(afb_req request) { afb_req_success (request, ctlsHalJ, NULL); } - STATIC int halGetCtlIndex (afb_req request, struct json_object*ctlInJ) { struct json_object *tmpJ; - int tag, index; + int tag, index, done; // check 1st short command mode [tag1, tag2, ...] - tag = json_object_get_type (ctlInJ); - - if (!tag) { - json_object_object_get_ex (ctlInJ, "tag" , &tmpJ); - tag = json_object_get_int(tmpJ); - } - - if (tag) { - index = halCtlTagToIndex((halCtlsEnumT) tag); - } else { - // tag was not provided let's try label - const char *label; + enum json_type jtype = json_object_get_type (ctlInJ); + switch (jtype) { + case json_type_array: + tmpJ = json_object_array_get_idx (ctlInJ, 0); + tag = json_object_get_int(tmpJ); + index = halCtlTagToIndex(tag); + break; + + case json_type_int: + tag = json_object_get_int(ctlInJ); + index = halCtlTagToIndex(tag); + break; - json_object_object_get_ex (ctlInJ, "label" , &tmpJ); - label = json_object_get_string(tmpJ); - index = halCtlStringToIndex(label); + case json_type_object: + done = json_object_object_get_ex (ctlInJ, "tag" , &tmpJ); + if (done) { + tag = json_object_get_int(tmpJ); + index = halCtlTagToIndex(tag); + } else { + const char *label; + done = json_object_object_get_ex (ctlInJ, "label" , &tmpJ); + if (!done) goto OnErrorExit; + label = json_object_get_string(tmpJ); + index = halCtlStringToIndex(label); + } + break; + + default: + goto OnErrorExit; } + - if (index < 0) { - afb_req_fail_f(request, "ctl-invalid", "No Label/Tag given ctl='%s'", json_object_get_string(ctlInJ)); - goto OnErrorExit; - } + if (index < 0) goto OnErrorExit; // return corresponding lowlevel numid to querylist return index; OnErrorExit: + afb_req_fail_f(request, "ctl-invalid", "No Label/Tag given ctl='%s'", json_object_get_string(ctlInJ)); return -1; } -// HAL normalise volume values to 0-100% -STATIC struct json_object *UnNormaliseValue(const alsaHalCtlMapT *halCtls, struct json_object *valuesJ) { - int length; - - // assert response as the right length - length = json_object_array_length(valuesJ); - if (length != halCtls->count) { - AFB_WARNING ("UnNormaliseValue invalid ctl='%s' values count=%d len=%d", halCtls->name, halCtls->count, length); - return NULL; - } - - json_object *normalisedJ= json_object_new_array(); - for (int idx=0; idx < halCtls->count; idx++) { - int value; - - // use last value in array when number of values does not match with actual ctl count - if (idx < length) { - json_object *valueJ = json_object_array_get_idx (valuesJ, idx); - value = json_object_get_int(valueJ); - - // cleanup and normalise value - if (value > halCtls->maxval) value= halCtls->maxval; - if (value < halCtls->minval) value= halCtls->minval; - - // If Integer move from 0-100% to effective value - if (halCtls->type == SND_CTL_ELEM_TYPE_INTEGER) { - value = (value * (halCtls->maxval-halCtls->minval))/100; - } - } - - // add unnormalised value into response - json_object_array_add(normalisedJ, json_object_new_int(value)); - } - - return (normalisedJ); -} - // Translate high level control to low level and call lower layer PUBLIC void halSetCtls(afb_req request) { - int err, index; + int err, done, index; struct json_object *ctlsInJ, *ctlsOutJ, *queryJ, *valuesJ, *responseJ; // get query from request ctlsInJ = afb_req_json(request); - ctlsOutJ = json_object_new_array(); switch (json_object_get_type(ctlsInJ)) { case json_type_object: { + ctlsOutJ = json_object_new_object(); + // control is in literal form {tag=xxx, label=xxx, value=xxxx} index = halGetCtlIndex (request, ctlsInJ); - if (index <=0) goto OnErrorExit; + if (index < 0) goto OnErrorExit; - err= json_object_object_get_ex (ctlsInJ, "value" , &valuesJ); - if (err) { + done= json_object_object_get_ex (ctlsInJ, "val" , &valuesJ); + if (!done) { afb_req_fail_f(request, "ctl-invalid", "No val=[val1, ...] ctl='%s'", json_object_get_string(ctlsInJ)); goto OnErrorExit; } - json_object_array_add (ctlsOutJ, json_object_new_int(halCtls[index].ctl.numid)); - json_object_array_add (ctlsOutJ, UnNormaliseValue (&halCtls[index].ctl, valuesJ)); + json_object_object_add (ctlsOutJ, "id", json_object_new_int(halCtls[index].ctl.numid)); + json_object_object_add (ctlsOutJ,"val", SetGetNormaliseVolumes (ACTION_SET, &halCtls[index].ctl, valuesJ)); break; } case json_type_array: { + ctlsOutJ = json_object_new_array(); for (int idx= 0; idx < json_object_array_length (ctlsInJ); idx++) { struct json_object *ctlInJ = json_object_array_get_idx (ctlsInJ, idx); index= halGetCtlIndex (request, ctlInJ); - if (index<=0) goto OnErrorExit; + if (index < 0) goto OnErrorExit; - err= json_object_object_get_ex (ctlInJ, "value" , &valuesJ); - if (err) { + done= json_object_object_get_ex (ctlInJ, "val" , &valuesJ); + if (!done) { afb_req_fail_f(request, "ctl-invalid", "No val=[val1, ...] ctl='%s'", json_object_get_string(ctlsInJ)); goto OnErrorExit; } // let's create alsa low level set control request - struct json_object *ctlOutJ = json_object_new_array(); - json_object_array_add (ctlOutJ, json_object_new_int(halCtls[index].ctl.numid)); - json_object_array_add (ctlOutJ, UnNormaliseValue (&halCtls[index].ctl, valuesJ)); + struct json_object *ctlOutJ = json_object_new_object(); + json_object_object_add (ctlOutJ, "id", json_object_new_int(halCtls[index].ctl.numid)); + json_object_object_add (ctlOutJ, "val", SetGetNormaliseVolumes (ACTION_SET, &halCtls[index].ctl, valuesJ)); json_object_array_add (ctlsOutJ, ctlOutJ); } @@ -225,19 +199,12 @@ PUBLIC void halSetCtls(afb_req request) { json_object_object_add(queryJ, "devid", json_object_new_string (halDevid)); json_object_object_add(queryJ, "numid", ctlsOutJ); - err= afb_service_call_sync("alsacore", "setctls", queryJ, &responseJ); + err= afb_service_call_sync("alsacore", "setctl", queryJ, &responseJ); if (err) { afb_req_fail_f(request, "subcall:alsacore/setctl", "%s", json_object_get_string(responseJ)); goto OnErrorExit; } - // Let ignore info data if any and keep on response - json_object_object_get_ex (responseJ, "response", &responseJ); - - // map back low level response to HAL ctl with normalised values - //struct json_object *halResponse = CtlSetPrepareResponse(request, responseJ); - //if (!halResponse) goto OnErrorExit; - afb_req_success (request, NULL, NULL); return; @@ -246,7 +213,7 @@ OnErrorExit: }; // Remap low level controls into HAL hight level ones -STATIC json_object *CtlGetPrepareResponse(afb_req request, struct json_object *ctlsJ) { +STATIC json_object *HalGetPrepareResponse(afb_req request, struct json_object *ctlsJ) { struct json_object *halResponseJ; // make sure return controls have a valid type @@ -264,7 +231,7 @@ STATIC json_object *CtlGetPrepareResponse(afb_req request, struct json_object *c int numid; sndCtlJ = json_object_array_get_idx(ctlsJ, idx); - if (!json_object_object_get_ex(sndCtlJ, "numid", &numidJ) || !json_object_object_get_ex(sndCtlJ, "val", &valJ)) { + if (!json_object_object_get_ex(sndCtlJ, "id", &numidJ) || !json_object_object_get_ex(sndCtlJ, "val", &valJ)) { afb_req_fail_f(request, "ctl-invalid", "Invalid Control return from alsa/getcontrol ctl=%s", json_object_get_string(sndCtlJ)); goto OnErrorExit; } @@ -279,7 +246,7 @@ STATIC json_object *CtlGetPrepareResponse(afb_req request, struct json_object *c struct json_object *halCtlJ = json_object_new_object(); json_object_object_add(halCtlJ, "label", json_object_new_string(halCtls[idx].label)); // idx+1 == HAL/NUMID json_object_object_add(halCtlJ, "tag" , json_object_new_int(halCtls[idx].tag)); // idx+1 == HAL/NUMID - json_object_object_add(halCtlJ, "val" , GetNormaliseVolume(&halCtls[idx].ctl, valJ)); + json_object_object_add(halCtlJ, "val" , SetGetNormaliseVolumes(ACTION_GET, &halCtls[idx].ctl, valJ)); json_object_array_add(halResponseJ, halCtlJ); break; } @@ -308,7 +275,7 @@ PUBLIC void halGetCtls(afb_req request) { case json_type_object: { index = halGetCtlIndex (request, ctlsInJ); - if (index <=0) goto OnErrorExit; + if (index < 0) goto OnErrorExit; json_object_array_add (ctlsOutJ, json_object_new_int(halCtls[index].ctl.numid)); break; } @@ -318,7 +285,7 @@ PUBLIC void halGetCtls(afb_req request) { for (int idx= 0; idx < json_object_array_length (ctlsInJ); idx++) { struct json_object *ctlInJ = json_object_array_get_idx (ctlsInJ, idx); index= halGetCtlIndex (request, ctlInJ); - if (index<=0) goto OnErrorExit; + if (index < 0) goto OnErrorExit; json_object_array_add (ctlsOutJ, json_object_new_int(halCtls[index].ctl.numid)); } break; @@ -344,7 +311,7 @@ PUBLIC void halGetCtls(afb_req request) { json_object_object_get_ex (responseJ, "response", &responseJ); // map back low level response to HAL ctl with normalised values - struct json_object *halResponse = CtlGetPrepareResponse(request, responseJ); + struct json_object *halResponse = HalGetPrepareResponse(request, responseJ); if (!halResponse) goto OnErrorExit; afb_req_success (request, halResponse, NULL); @@ -361,7 +328,7 @@ PUBLIC void halServiceEvent(const char *evtname, struct json_object *eventJ) { AFB_NOTICE("halServiceEvent evtname=%s [msg=%s]", evtname, json_object_get_string(eventJ)); - json_object_object_get_ex (eventJ, "values" , &valuesJ); + json_object_object_get_ex (eventJ, "val" , &valuesJ); if (!valuesJ) { AFB_ERROR("halServiceEvent novalues: evtname=%s [msg=%s]", evtname, json_object_get_string(eventJ)); return; @@ -387,7 +354,7 @@ STATIC int UpdateOneSndCtl (alsaHalCtlMapT *ctl, struct json_object *sndCtlJ) { json_object_object_get_ex (sndCtlJ, "name" , &tmpJ); ctl->name = (char*)json_object_get_string(tmpJ); - json_object_object_get_ex (sndCtlJ, "numid" , &tmpJ); + json_object_object_get_ex (sndCtlJ, "id" , &tmpJ); ctl->numid = json_object_get_int(tmpJ); // make sure we face a valid Alsa Low level ctl @@ -547,6 +514,7 @@ PUBLIC afb_verb_v2 halServiceApi[] = { { .verb = "ping", .callback = pingtest}, { .verb = "ctl-list", .callback = halListCtls}, { .verb = "ctl-get", .callback = halGetCtls}, + { .verb = "ctl-set", .callback = halSetCtls}, { .verb = "evt-sub", .callback = halSubscribe}, { .verb = NULL} /* marker for end of the array */ }; diff --git a/HAL-afb/HAL-interface/hal-interface.h b/HAL-afb/HAL-interface/hal-interface.h index afe480e..c31c145 100644 --- a/HAL-afb/HAL-interface/hal-interface.h +++ b/HAL-afb/HAL-interface/hal-interface.h @@ -23,6 +23,11 @@ #include "audio-interface.h" +typedef enum { + ACTION_SET, + ACTION_GET +} ActionSetGetT; + typedef struct { int min; int max; @@ -69,7 +74,7 @@ PUBLIC void halServiceEvent(const char *evtname, struct json_object *object); PUBLIC int halServiceInit (const char *apiPrefix, alsaHalSndCardT *alsaHalSndCard); // hal-volmap.c -PUBLIC struct json_object *GetNormaliseVolume(const alsaHalCtlMapT *halCtls, struct json_object *valuesJ); +PUBLIC struct json_object *SetGetNormaliseVolumes(ActionSetGetT action, const alsaHalCtlMapT *halCtls, struct json_object *valuesJ); #endif /* SHAREHALLIB_H */ diff --git a/HAL-afb/HAL-interface/hal-volmap.c b/HAL-afb/HAL-interface/hal-volmap.c index de99974..2b619fd 100644 --- a/HAL-afb/HAL-interface/hal-volmap.c +++ b/HAL-afb/HAL-interface/hal-volmap.c @@ -24,26 +24,30 @@ typedef enum { - DB_NORMALIZE_LINEAR, - DB_NORMALIZE_MATH, + NORMALIZE_NONE=0, + NORMALIZE_DB_LINEAR, + NORMALIZE_DB_MATH, + NORMALIZE_LINEAR, } enumRandeModeDB_T; + // Return Value express from 0-100% STATIC int dbNormalizeVal (enumRandeModeDB_T normaliseMode, const alsaHalDBscaleT *dbscale, int value) { double normalized, min_norm; - + // To get real DB from TLV DB values should be divided by 100 switch (normaliseMode) { - case DB_NORMALIZE_LINEAR: - normalized = ((double)(value-dbscale->min)/(double)(dbscale->max-dbscale->min))*100; + case NORMALIZE_DB_LINEAR: + normalized = ((double)(value-dbscale->min)/(double)(dbscale->max-dbscale->min)); break; - case DB_NORMALIZE_MATH: + case NORMALIZE_DB_MATH: normalized = exp10((double)(value - dbscale->max) / 6000.0); if (dbscale->min != SND_CTL_TLV_DB_GAIN_MUTE) { - min_norm = exp10((double)(dbscale->min - dbscale->max) / 6000.0); + min_norm = exp10((double)(dbscale->min - dbscale->max) / 20); normalized = (normalized - min_norm) / (1 - min_norm); } + break; default: @@ -54,35 +58,82 @@ STATIC int dbNormalizeVal (enumRandeModeDB_T normaliseMode, const alsaHalDBscale } // HAL normalise volume values to 0-100% -PUBLIC struct json_object *GetNormaliseVolume(const alsaHalCtlMapT *halCtls, struct json_object *valuesJ) { - int useNormalizeDB; +PUBLIC struct json_object *SetGetNormaliseVolumes(ActionSetGetT action, const alsaHalCtlMapT *halCtls, struct json_object *valuesJ) { + enumRandeModeDB_T useNormalizeDB; + int length; + + // If Integer look for DBscale + if (halCtls->type == SND_CTL_ELEM_TYPE_INTEGER) { - // If not valid db_scale let's use raw_scale - if (!halCtls->dbscale || (halCtls->dbscale->min >= halCtls->dbscale->max)) { + // If not valid db_scale let's use raw_scale + if (!halCtls->dbscale || (halCtls->dbscale->min >= halCtls->dbscale->max)) { // dbscale is invalid let's try raw range if (halCtls->minval >= halCtls->maxval) goto ExitOnError; // Use Raw Scale Model - useNormalizeDB= 0; + useNormalizeDB= NORMALIZE_LINEAR; - } else { // db_scale looks OK let's use it - if ((halCtls->dbscale->max - halCtls->dbscale->min) <= MAX_LINEAR_DB_SCALE * 100) useNormalizeDB= DB_NORMALIZE_LINEAR; - else useNormalizeDB = DB_NORMALIZE_MATH; - - } + } else { // db_scale looks OK let's use it + if ((halCtls->dbscale->max - halCtls->dbscale->min) <= MAX_LINEAR_DB_SCALE * 100) useNormalizeDB= NORMALIZE_DB_LINEAR; + else useNormalizeDB = NORMALIZE_LINEAR; // Fulup not sure how to handle this useNormalizeDB=NORMALIZE_DB_MATH; + } + } else useNormalizeDB= NORMALIZE_NONE; + // loop on values to normalise - int length = json_object_array_length(valuesJ); + enum json_type jtype=json_object_get_type(valuesJ); + if (jtype == json_type_array) length = json_object_array_length(valuesJ); + else length = 1; + json_object *normalisedJ= json_object_new_array(); - for (int idx=0; idx <length; idx++) { - json_object *valueJ = json_object_array_get_idx (valuesJ, idx); - int value = json_object_get_int(valueJ); + for (int idx=0; idx < length; idx++) { + int value; + + if (jtype == json_type_array) { + json_object *valueJ = json_object_array_get_idx (valuesJ, idx); + value = json_object_get_int(valueJ); + } else { + value = json_object_get_int(valuesJ); + } // If Integer scale to 0/100 if (halCtls->type == SND_CTL_ELEM_TYPE_INTEGER) { - if (useNormalizeDB) value = dbNormalizeVal (useNormalizeDB, halCtls->dbscale, value); - else value = 100* (value - halCtls->minval) / (halCtls->maxval - halCtls->minval); + + switch (action) { + + case ACTION_GET: + switch (useNormalizeDB) { + case NORMALIZE_LINEAR: + value = 100* (value - halCtls->minval) / (halCtls->maxval - halCtls->minval); + break; + case NORMALIZE_DB_MATH: //ToBeDone + value = dbNormalizeVal (useNormalizeDB, halCtls->dbscale, value); + break; + case NORMALIZE_NONE: + default: + value = value; + } + break; + + case ACTION_SET: + switch (useNormalizeDB) { + case NORMALIZE_LINEAR: + value = (value * (halCtls->maxval - halCtls->minval))/100; + break; + case NORMALIZE_DB_MATH: //ToBeDone + value = dbNormalizeVal (useNormalizeDB, halCtls->dbscale, value); + break; + case NORMALIZE_NONE: + default: + value = value; + } + break; + + default: + AFB_NOTICE ("SetGetNormaliseVolumes: invalid action value=%d", (int)action); + goto ExitOnError; + } } json_object_array_add(normalisedJ, json_object_new_int(value)); diff --git a/HAL-afb/Scarlett-Focusrite/ScarlettUsbHAL.c b/HAL-afb/Scarlett-Focusrite/ScarlettUsbHAL.c index cc62be1..10ef204 100644 --- a/HAL-afb/Scarlett-Focusrite/ScarlettUsbHAL.c +++ b/HAL-afb/Scarlett-Focusrite/ScarlettUsbHAL.c @@ -19,6 +19,7 @@ * aplay -l # Check sndcard name name in between [] * amixer -D hw:USB controls # get supported controls * amixer -Dhw:USB cget name=Power-Switch + * amixer -Dhw:USB cset name=Power-Switch true * */ #define _GNU_SOURCE diff --git a/Shared-Interface/audio-interface.c b/Shared-Interface/audio-interface.c index edaaf7f..b4750ae 100644 --- a/Shared-Interface/audio-interface.c +++ b/Shared-Interface/audio-interface.c @@ -23,34 +23,6 @@ #include "audio-interface.h" -typedef struct { - int index; - int numid; -} shareHallMap_T; - - -PUBLIC int cbCheckResponse(struct afb_req request, int iserror, struct json_object *result) { - struct json_object *response, *status, *info; - - if (iserror) { // on error proxy information we got from lower layer - if (result) { - 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 OnErrorExit; - } - } else { - afb_req_fail(request, "cbCheckFail", "No Result inside API response"); - } - goto OnErrorExit; - } - return (0); - -OnErrorExit: - return (-1); -} - PUBLIC void pingtest(struct afb_req request) { json_object *query = afb_req_json(request); diff --git a/Shared-Interface/audio-interface.h b/Shared-Interface/audio-interface.h index 09cb6c2..01c71cd 100644 --- a/Shared-Interface/audio-interface.h +++ b/Shared-Interface/audio-interface.h @@ -62,19 +62,18 @@ typedef enum { typedef enum { StartHalCrlTag=0, - // HighLevel Audio Control List - Master_Playback_Volume, - Master_OnOff_Switch, - Master_Playback_Ramp, - PCM_Playback_Volume, - PCM_Playback_Switch, - Capture_Volume, + // HighLevel Audio Control List, + Master_Playback_Volume =1, + Master_Playback_Ramp =2, + PCM_Playback_Volume =3, + PCM_Playback_Switch =4, + Capture_Volume =5, + Master_OnOff_Switch =6, EndHalCrlTag // used to compute number of ctls } halCtlsEnumT; -PUBLIC int cbCheckResponse(struct afb_req request, int iserror, struct json_object *result) ; PUBLIC void pingtest(struct afb_req request); #endif /* AUDIO_INTERF_H */ diff --git a/conf.d/cmake/config.cmake b/conf.d/cmake/config.cmake index 97c4986..e7801c8 100644 --- a/conf.d/cmake/config.cmake +++ b/conf.d/cmake/config.cmake @@ -45,6 +45,7 @@ set(CMAKE_BUILD_TYPE "DEBUG") # ----------------------------- add_compile_options(-DMAX_SND_CARD=16) # should be more than enough even in luxury vehicule add_compile_options(-DMAX_LINEAR_DB_SCALE=24) # until 24db volume normalisation use a simple linear scale +add_compile_options(-DTLV_BYTE_SIZE=256) # Alsa use 4096 as default but 256 should fit most sndcards # Compiler selection if needed. Overload the detected compiler. # ----------------------------------------------- diff --git a/htdocs/AudioBinding.js b/htdocs/AudioBinding.js index e000a70..607d31c 100644 --- a/htdocs/AudioBinding.js +++ b/htdocs/AudioBinding.js @@ -2,6 +2,7 @@ var ws; var sndcard="HALNotSelected"; var evtidx=0; + var numid=0; function syntaxHighlight(json) { if (typeof json !== 'string') { @@ -48,31 +49,10 @@ if (!sndname) sndname="PCH"; var mode=getParameterByName("mode"); - if (!mode) mode="99"; + if (!mode) mode="0"; - function init(elemid, api, verb, query) { - - function onopen() { - // check for active HALs - probeActiveHal (elemid, api, verb, query); - - 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"; - - } - - ws = new afb.ws(onopen, onabort); - } function replyok(obj) { console.log("replyok:" + JSON.stringify(obj)); @@ -106,9 +86,17 @@ } - // Retrieve the list of active HAL - function probeActiveHal (elemid, api, verb, query) { + // Retreive Select value and Text from the binder + // Note: selection of value/text for a given context is huggly!!! + function querySelectList (elemid, api, verb, query) { + + console.log("querySelectList elemid=%s api=%s verb=%s query=%s", elemid, api, verb, query); + var selectobj = document.getElementById(elemid); + if (!selectobj) { + console.log ("****** elemid=%s does not exit in HTML page ****", elemid); + return; + } // onlick update selected HAL api selectobj.onclick=function(){ @@ -139,12 +127,72 @@ sndcard= selectobj.value; } - - console.log ("subscribe api="+api+" verb="+verb+" query=" +query); - + + var question = urlws +"/"+api +"/" +verb + "?query=" + JSON.stringify(query); + document.getElementById("question").innerHTML = syntaxHighlight(question); + + // request lowlevel ALSA to get API list + ws.call(api+"/"+verb, query).then(gotit, replyerr); + } + + function refresh_list (self, api, verb, query) { + console.log("refresh_list id=%s api=%s verb=%s query=%s", self.id, api, verb, query); + + if (self.value > 0) return; + + // onlick update selected HAL api + self.onclick=function(){ + numid = parseInt(self.value); + console.log ("Default numid=%d", numid); + }; + + function gotit (result) { + + // display response as for normal onclick action + replyok(result); + var response=result.response; + + + + // fulfill select with avaliable active HAL + for (idx=0; idx<response.length; idx++) { + var opt = document.createElement('option'); + + // Alsa LowLevel selection mode + opt.text = response[idx].name + ' id=' + response[idx].id; + opt.value = response[idx].id; + + self.appendChild(opt); + } + self.selectedIndex=2; + numid = parseInt (self.value); + } + var question = urlws +"/"+api +"/" +verb + "?query=" + JSON.stringify(query); document.getElementById("question").innerHTML = syntaxHighlight(question); // request lowlevel ALSA to get API list ws.call(api+"/"+verb, query).then(gotit, replyerr); } + + + function init(elemid, api, verb, query) { + + function onopen() { + // check for active HALs + querySelectList (elemid, api, verb, query); + + 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"; + + } + ws = new afb.ws(onopen, onabort); + } diff --git a/htdocs/alsa-core.html b/htdocs/alsa-core.html index 9b6e214..dafd43f 100644 --- a/htdocs/alsa-core.html +++ b/htdocs/alsa-core.html @@ -2,19 +2,22 @@ <head> <link rel="stylesheet" href="AudioBinding.css"> <title>Alsa Low Level Simple Test</title> - - + <script type="text/javascript" src="AFB-websock.js"></script> <script type="text/javascript" src="AudioBinding.js"></script> </head> -<body onload="init('alsa_registry','alsacore', 'getinfo');"> +<body onload="init('alsa_registry','alsacore', 'getinfo')"> -<b> references</b> - <button id="connected" onclick="init('alsa_registry','alsacore', 'getinfo')">Binder WS Fail</button> + <button id="connected" onclick="init('alsa_registry','alsacore', 'getinfo');">Binder WS Fail</button> <br><br> <b>Selected SndCard </b> <select id='alsa_registry'></select> + + <b>Select NUMID </b> + <select id='alsa_ctl_list' onclick="refresh_list(this, 'alsacore', 'getctl', {devid:sndcard, mode:1})"> + <option value='-1'>Refresh NUMID list</option> + </select> <b>API Verbosity </b> <select id='api_verbosity' onclick='mode=this.value'> @@ -22,21 +25,22 @@ <option value='1'>Compact</option> <option value='2'>Verbose</option> <option value='3'>Full</option> - </select> - <br> - + </select> + + <br> <ol> <li><button onclick="callbinder('alsacore','getctl', {devid:sndcard, mode:mode})">Get all Alsa Ctls</button></li> - <li><button onclick="callbinder('alsacore','getctl', {devid:sndcard, mode:mode, numids:[6]})">Get Alsa Ctls numids:[6]</button></li> - <li><button onclick="callbinder('alsacore','getctl', {devid:sndcard, mode:mode, numids:[1,2]})">Get Alsa Ctls numids:[1,2]</button></li> - <li><button onclick="callbinder('alsacore','getctl', {devid:sndcard, mode:mode, numids:17})">Get Alsa Ctl numids:17</button></li> - <br> - <li><button onclick="callbinder('alsacore','setctl', {devid:sndcard, mode:mode, numids:[[17,20]]})">Set Alsa Ctl numid:[[17,20]]</button></li> - <li><button onclick="callbinder('alsacore','setctl', {devid:sndcard, mode:mode, numids:[{id:17,val:50}]})">Set Alsa Ctl numid:[{id:17,val:50}]}</button></li> - <li><button onclick="callbinder('alsacore','setctl', {devid:sndcard, mode:mode, numids:[[1,[20,20]]]})">Set Alsa Ctl numids:[[1,[20,20]]]</button></li> - <li><button onclick="callbinder('alsacore','setctl', {devid:sndcard, mode:mode, numids:[{id:1,val:[50,50]}]})">Set Alsa Ctl numids:[{id:1,val:[50,50]}]</button></li> - <li><button onclick="callbinder('alsacore','setctl', {devid:sndcard, mode:mode, numids:[{id:1,val:[50,50]}, {id:17,val:50,50}]})">Set Alsa Ctl numids:[{id:1,val:[50,50]}, {id:17,val:50,50}]</button></li> + <li><button onclick="callbinder('alsacore','getctl', {devid:sndcard, mode:mode, numid:[numid]})">Get Alsa Ctls [numid]</button></li> + <li><button onclick="callbinder('alsacore','getctl', {devid:sndcard, mode:mode, numid:[numid,numid+1]})">Get Alsa Ctls [numid,numid+1]</button></li> + <br> + + + <li><button onclick="callbinder('alsacore','setctl', {devid:sndcard, mode:mode, numid:[[9,20]]})">Set Alsa Ctl numid:[[9,20]]</button></li> + <li><button onclick="callbinder('alsacore','setctl', {devid:sndcard, mode:mode, numid:[{id:9,val:50}]})">Set Alsa Ctl numid:[{id:9,val:50}]}</button></li> + <li><button onclick="callbinder('alsacore','setctl', {devid:sndcard, mode:mode, numid:[[6,[20,20]]]})">Set Alsa Ctl numid:[[6,[20,20]]]</button></li> + <li><button onclick="callbinder('alsacore','setctl', {devid:sndcard, mode:mode, numid:[{id:6,val:[50,50]}]})">Set Alsa Ctl numid:[{id:2,val:[50,50]}]</button></li> + <li><button onclick="callbinder('alsacore','setctl', {devid:sndcard, mode:mode, numid:[{id:6,val:[50,50]}, {id:9,val:50,50}]})">Set Alsa Ctl numid:[{id:6,val:[50,50]}, {id:9,val:50,50}]</button></li> <br> <li><button onclick="callbinder('alsacore','ucmquery', {devid:sndcard, mode:mode})">List UCM verbs</button></li> <li><button onclick="callbinder('alsacore','ucmset' , {devid:sndcard, mode:mode, verb:'HiFi'})">Set UCM HiFi</button></li> diff --git a/htdocs/alsa-hal.html b/htdocs/alsa-hal.html index 7a8655c..7377bd4 100644 --- a/htdocs/alsa-hal.html +++ b/htdocs/alsa-hal.html @@ -27,15 +27,15 @@ <br> <ol> - <li><button onclick="callbinder(sndcard,'listctls')">List Selected HAL Controls </button></li> + <li><button onclick="callbinder(sndcard,'ctl-list')">List Selected HAL Controls </button></li> <li><button onclick="callbinder(sndcard,'ctl-get', {label:'Master_Playback_Volume'})">Get {label:'Master_Playback_Volume'}</button></li> <li><button onclick="callbinder(sndcard,'ctl-get', [{tag:4},{tag:5}])">Get[{tag:4},{tag:5}]</button></li> <li><button onclick="callbinder(sndcard,'ctl-get', [4,5])">Get [4,5]</button></li> <br> - <li><button onclick="callbinder(sndcard,'ctl-set', {label:'Master_Playback_Volume', value=[50]})">Set {label:'Master_Playback_Volume', value=[50]}</button></li> - <li><button onclick="callbinder(sndcard,'ctl-set', [{tag:4, value:25},{tag:5, value:25}])">Set[{tag:4, value:25},{tag:5, value:25}]</button></li> - <li><button onclick="callbinder(sndcard,'ctl-set', [{tag:4, value:[55,45]},{tag:5, value:[45,55]}])">Set[{tag:4, value:[55,45]},{tag:5, value:[45,55]}]]</button></li> - <li><button onclick="callbinder(sndcard,'ctl-set', [4,5])">Set [4,5]</button></li> + <li><button onclick="callbinder(sndcard,'ctl-set', {label:'Master_Playback_Volume', val:[50]})">Set {label:'Master_Playback_Volume', value=[50]}</button></li> + <li><button onclick="callbinder(sndcard,'ctl-set', {tag: 4, val:5})">Set {tag: 4, val:5}</button></li> + <li><button onclick="callbinder(sndcard,'ctl-set', [{tag:4, val:25},{tag:5, val:25}])">Set[{tag:4, val:25},{tag:5, val:25}]</button></li> + <li><button onclick="callbinder(sndcard,'ctl-set', [{tag:4, val:[55,45]},{tag:5, val:[45,55]}])">Set[{tag:4, val:[55,45]},{tag:5, val:[45,55]}]]</button></li> <br> </ol> diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 18d1e63..938c355 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -107,6 +107,8 @@ <item path="ALSA-afb/Alsa-AddCtl.c" ex="false" tool="0" flavor2="3"> <cTool flags="4"> <incDir> + <pElem>../../../opt/include/afb</pElem> + <pElem>ALSA-afb</pElem> <pElem>../../../opt/include/alsa</pElem> <pElem>/usr/include/json-c</pElem> <pElem>Shared-Interface</pElem> @@ -118,6 +120,8 @@ <item path="ALSA-afb/Alsa-ApiHat.c" ex="false" tool="0" flavor2="3"> <cTool flags="4"> <incDir> + <pElem>../../../opt/include/afb</pElem> + <pElem>ALSA-afb</pElem> <pElem>build/ALSA-afb</pElem> </incDir> </cTool> @@ -125,6 +129,8 @@ <item path="ALSA-afb/Alsa-RegEvt.c" ex="false" tool="0" flavor2="3"> <cTool flags="4"> <incDir> + <pElem>../../../opt/include/afb</pElem> + <pElem>ALSA-afb</pElem> <pElem>../../../opt/include/alsa</pElem> <pElem>/usr/include/json-c</pElem> <pElem>Shared-Interface</pElem> @@ -134,19 +140,30 @@ </cTool> </item> <item path="ALSA-afb/Alsa-SetGet.c" ex="false" tool="0" flavor2="3"> - <cTool flags="4"> + <cTool flags="1"> <incDir> + <pElem>../../../opt/include</pElem> <pElem>../../../opt/include/alsa</pElem> + <pElem>/usr/include/p11-kit-1</pElem> <pElem>/usr/include/json-c</pElem> <pElem>Shared-Interface</pElem> - <pElem>../../../opt/include</pElem> <pElem>build/ALSA-afb</pElem> </incDir> + <preprocessorList> + <Elem>CONTROL_CDEV_RX="/dev/inic-usb-crx"</Elem> + <Elem>CONTROL_CDEV_TX="/dev/inic-usb-ctx"</Elem> + <Elem>MAX_LINEAR_DB_SCALE=24</Elem> + <Elem>MAX_SND_CARD=16</Elem> + <Elem>TLV_BYTE_SIZE=256</Elem> + <Elem>alsa_lowlevel_EXPORTS</Elem> + </preprocessorList> </cTool> </item> <item path="ALSA-afb/Alsa-Ucm.c" ex="false" tool="0" flavor2="3"> <cTool flags="4"> <incDir> + <pElem>../../../opt/include/afb</pElem> + <pElem>ALSA-afb</pElem> <pElem>../../../opt/include/alsa</pElem> <pElem>/usr/include/json-c</pElem> <pElem>Shared-Interface</pElem> @@ -163,28 +180,17 @@ ex="false" tool="0" flavor2="3"> - <cTool flags="1"> + <cTool flags="4"> <incDir> - <pElem>../../../opt/include/afb</pElem> - <pElem>../../../opt/include</pElem> - <pElem>../../../opt/include/alsa</pElem> - <pElem>/usr/include/p11-kit-1</pElem> - <pElem>/usr/include/json-c</pElem> - <pElem>HAL-afb/HAL-interface</pElem> <pElem>Shared-Interface</pElem> + <pElem>../../../opt/include</pElem> <pElem>build/HAL-afb/HAL-interface</pElem> </incDir> </cTool> </item> <item path="HAL-afb/HAL-interface/hal-volmap.c" ex="false" tool="0" flavor2="3"> - <cTool flags="1"> + <cTool flags="4"> <incDir> - <pElem>../../../opt/include</pElem> - <pElem>../../../opt/include/alsa</pElem> - <pElem>/usr/include/p11-kit-1</pElem> - <pElem>/usr/include/json-c</pElem> - <pElem>HAL-afb/HAL-interface</pElem> - <pElem>Shared-Interface</pElem> <pElem>build/HAL-afb/HAL-interface</pElem> </incDir> </cTool> @@ -298,14 +304,6 @@ <cTool flags="4"> </cTool> </item> - <folder path="0/ALSA-afb"> - <cTool> - <incDir> - <pElem>../../../opt/include/afb</pElem> - <pElem>ALSA-afb</pElem> - </incDir> - </cTool> - </folder> <folder path="0/Common"> <cTool> <incDir> @@ -335,9 +333,11 @@ </folder> <folder path="0/HAL-afb/HAL-interface"> <cTool> - <preprocessorList> - <Elem>MAX_LINEAR_DB_SCALE=24</Elem> - </preprocessorList> + <incDir> + <pElem>HAL-afb/HAL-interface</pElem> + <pElem>../../../opt/include/alsa</pElem> + <pElem>/usr/include/json-c</pElem> + </incDir> </cTool> </folder> <folder path="0/HAL-afb/HAL-plugin"> |