From b30eb2b2d8f83f29874e2d63dd752e9cf296da37 Mon Sep 17 00:00:00 2001 From: Jonathan Aillet Date: Mon, 19 Aug 2019 11:26:47 +0200 Subject: Make a function to get card number from card path Make a function to get card number from card path, will be needed to for added/removed card events. Bug-AGL: SPEC-2749 Change-Id: Ic0cc6fd84a54432c696892772f2f66d1bde59a65 Signed-off-by: Jonathan Aillet --- alsa-binding/Alsa-ApiHat.h | 1 + alsa-binding/Alsa-SetGet.c | 60 +++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 22 deletions(-) (limited to 'alsa-binding') diff --git a/alsa-binding/Alsa-ApiHat.h b/alsa-binding/Alsa-ApiHat.h index 0300f3d..59a9b0c 100644 --- a/alsa-binding/Alsa-ApiHat.h +++ b/alsa-binding/Alsa-ApiHat.h @@ -90,6 +90,7 @@ extern const struct afb_binding_interface *afbIface; PUBLIC json_object *alsaCheckQuery (afb_req_t request, queryValuesT *queryValues); // AlseCoreSetGet exports +PUBLIC int getCardNbFromCardPath(char *cardPath); PUBLIC json_object *getCardInfo(int card); PUBLIC int alsaGetSingleCtl (snd_ctl_t *ctlDev, snd_ctl_elem_id_t *elemId, ctlRequestT *ctlRequest, queryModeE queryMode); PUBLIC void alsaGetInfo (afb_req_t request); diff --git a/alsa-binding/Alsa-SetGet.c b/alsa-binding/Alsa-SetGet.c index e8c6563..8c189de 100644 --- a/alsa-binding/Alsa-SetGet.c +++ b/alsa-binding/Alsa-SetGet.c @@ -345,14 +345,43 @@ STATIC json_object *decodeTlv(unsigned int *tlv, unsigned int tlv_size, int mode return (decodeTlvJson); } -STATIC int searchInQueryForAlsaCardToProbe(queryCardInfo *queryInfo, int *cardToProbe) -{ - int err, cardPathFileId; +PUBLIC int getCardNbFromCardPath(char *cardPath) { + int err, cardPathFileFd, fileCardId; snd_ctl_card_info_t *currentCardInfo; + if (! cardPath) + return -1; + + cardPathFileFd = open(cardPath, O_RDONLY); + if (cardPathFileFd < 0) { + AFB_ERROR("CardPath '%s' error %i during open", cardPath, cardPathFileFd); + return -2; + } + snd_ctl_card_info_alloca(¤tCardInfo); + err = ioctl(cardPathFileFd, SNDRV_CTL_IOCTL_CARD_INFO(snd_ctl_card_info_sizeof()), currentCardInfo); + close(cardPathFileFd); + if (err < 0) { + AFB_ERROR("CardPath '%s' error %i during ioctl", cardPath, err); + return -3; + } + + fileCardId = snd_ctl_card_info_get_card(currentCardInfo); + + if (fileCardId < 0 || fileCardId >= MAX_SND_CARD) { + AFB_ERROR("CardPath '%s' returned an incorrect card number %i", cardPath, fileCardId); + return -4; + } + + return fileCardId; +} + +STATIC int searchInQueryForAlsaCardToProbe(queryCardInfo *queryInfo, int *cardToProbe) +{ + int err; + if(! queryInfo) { AFB_ERROR("Structure to look for an ALSA card is NULL"); return -1; @@ -381,31 +410,18 @@ STATIC int searchInQueryForAlsaCardToProbe(queryCardInfo *queryInfo, int *cardTo } if(queryInfo->cardPath) { - cardPathFileId = open(queryInfo->cardPath, O_RDONLY); - if(cardPathFileId < 0) { - AFB_ERROR("CardPath '%s' error %i during open", queryInfo->cardPath, cardPathFileId); - return -4; - } + *cardToProbe = getCardNbFromCardPath(queryInfo->cardPath); - err = ioctl(cardPathFileId, SNDRV_CTL_IOCTL_CARD_INFO(snd_ctl_card_info_sizeof()), currentCardInfo); - close(cardPathFileId); - - *cardToProbe = snd_ctl_card_info_get_card(currentCardInfo); - - if(err < 0) { - AFB_ERROR("CardPath '%s' error %i during ioctl", queryInfo->cardPath, err); - return -5; - } - else if(*cardToProbe < 0 || *cardToProbe >= MAX_SND_CARD) { + if(*cardToProbe < 0 || *cardToProbe >= MAX_SND_CARD) { AFB_ERROR("CardPath '%s' returned an incorrect card number %i", queryInfo->cardPath, *cardToProbe); - return -6; + return -4; } else if(queryInfo->cardNb != NO_CARD_SELECTED && *cardToProbe != queryInfo->cardNb) { AFB_WARNING("CardPath '%s' (id %i) is not corresponding to the requested card number %i", queryInfo->cardPath, *cardToProbe, queryInfo->cardNb); - return -7; + return -5; } queryInfo->cardNb = *cardToProbe; @@ -417,12 +433,12 @@ STATIC int searchInQueryForAlsaCardToProbe(queryCardInfo *queryInfo, int *cardTo err = snd_card_next(cardToProbe); if(err < 0) { AFB_ERROR("Error %i happened when tried to get next ALSA card number", err); - return -8; + return -6; } if(*cardToProbe < 0) { AFB_NOTICE("No ALSA card connected"); - return -9; + return -7; } return 1; -- cgit 1.2.3-korg