summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFulup Ar Foll <fulup@iot.bzh>2017-11-05 13:32:29 +0100
committerFulup Ar Foll <fulup@iot.bzh>2017-11-05 13:32:29 +0100
commite5742e9244b21a866a1dc15b618a11a4e515e019 (patch)
tree608c3fa6249a8560db1024235fd5bb615f488bfe
parent665073e727623ac7228a86b003004956bf774adb (diff)
Improve Sound Card By Driver Name Detection
-rw-r--r--alsa-binding/Alsa-RegEvt.c61
1 files changed, 36 insertions, 25 deletions
diff --git a/alsa-binding/Alsa-RegEvt.c b/alsa-binding/Alsa-RegEvt.c
index be46c17..224d513 100644
--- a/alsa-binding/Alsa-RegEvt.c
+++ b/alsa-binding/Alsa-RegEvt.c
@@ -20,6 +20,10 @@
#include "Alsa-ApiHat.h"
+#ifndef MAX_SND_HAL
+#define MAX_SND_HAL 10
+#endif
+
// generic sndctrl event handle hook to event callback when pooling
typedef struct {
@@ -45,7 +49,17 @@ typedef struct {
char *longname;
} cardRegistryT;
-cardRegistryT *cardRegistry[MAX_SND_CARD + 1];
+cardRegistryT *cardRegistry[MAX_SND_HAL + 1];
+
+STATIC int getHalIdxFromCardid (int cardid) {
+
+ for (int idx = 0; idx < MAX_SND_HAL; idx++) {
+ if (!cardRegistry[idx]) return -1;
+ if (cardRegistry[idx]->cardid == cardid) return idx;
+ }
+
+ return -1;
+}
PUBLIC json_object *alsaCheckQuery(afb_req request, queryValuesT *queryValues) {
@@ -298,10 +312,9 @@ STATIC json_object *alsaProbeCardId(afb_req request) {
// check if short|long name match
if (!strcasecmp(sndname, ctlName)) break;
if (!strcasecmp(sndname, shortname)) break;
- if (!strcasecmp(sndname, drivername) && driverId==NULL) {
- driverId=strdup(devid);
- }
- if (!strcasecmp(sndname, longname)) break;
+
+ // if name does not match search for a free HAL with driver name matching
+ if (driverId==NULL && getHalIdxFromCardid(card)<0 && !strcasecmp(sndname, drivername)) driverId=strdup(devid);
}
if (card == MAX_SND_CARD) {
@@ -309,15 +322,17 @@ STATIC json_object *alsaProbeCardId(afb_req request) {
afb_req_fail_f(request, "ctlDev-notfound", "Fail to find card with name=%s", sndname);
goto OnErrorExit;
}
+
+ // refresh devid and clean up driverId
+ strncpy (devid, driverId, sizeof(devid));
+ free(driverId);
-
- err = snd_ctl_open(&ctlDev, driverId, SND_CTL_READONLY);
+ err = snd_ctl_open(&ctlDev, devid, SND_CTL_READONLY);
if (err < 0) {
afb_req_fail_f(request, "ctlDev-notfound", "Fail to find card with name=%s devid=%s", sndname, devid);
goto OnErrorExit;
}
-
// Sound not found by name, backup to driver name
snd_ctl_card_info(ctlDev, cardinfo);
index = snd_ctl_card_info_get_card(cardinfo);
@@ -326,8 +341,7 @@ STATIC json_object *alsaProbeCardId(afb_req request) {
longname = snd_ctl_card_info_get_longname(cardinfo);
mixername = snd_ctl_card_info_get_mixername(cardinfo);
drivername = snd_ctl_card_info_get_driver(cardinfo);
- AFB_WARNING("alsaProbeCardId Fallbak to HAL=%s ==> devid=%s name=%s long=%s\n ", drivername, driverId, shortname, longname);
- free(driverId);
+ AFB_WARNING("alsaProbeCardId Fallback to HAL=%s ==> devid=%s name=%s long=%s\n ", drivername, devid, shortname, longname);
snd_ctl_close(ctlDev);
}
@@ -346,7 +360,7 @@ STATIC json_object *alsaProbeCardId(afb_req request) {
}
// search for a HAL binder card mapping name to api prefix
- for (idx = 0; (idx < MAX_SND_CARD && cardRegistry[idx]); idx++) {
+ for (idx = 0; (idx < MAX_SND_HAL && cardRegistry[idx]); idx++) {
if (!strcmp(cardRegistry[idx]->shortname, shortname)) {
json_object_object_add(responseJ, "halapi", json_object_new_string(cardRegistry[idx]->apiprefix));
break;
@@ -370,18 +384,15 @@ PUBLIC void alsaGetCardId(afb_req request) {
// Return HAL information about a given sound card ID
-STATIC int getHalFromCardId(int cardid, json_object *responseJ) {
-
- for (int idx = 0; idx < MAX_SND_CARD; idx++) {
- if (!cardRegistry[idx]) goto OnErrorExit;
+STATIC int getHalApiFromCardid(int cardid, json_object *responseJ) {
- if (cardRegistry[idx]->cardid == cardid) {
- json_object_object_add(responseJ, "api", json_object_new_string(cardRegistry[idx]->apiprefix));
- if (cardRegistry[idx]->shortname)json_object_object_add(responseJ, "shortname", json_object_new_string(cardRegistry[idx]->shortname));
- if (cardRegistry[idx]->longname) json_object_object_add(responseJ, "longname", json_object_new_string(cardRegistry[idx]->longname));
- break;
- }
- }
+
+ int idx = getHalIdxFromCardid (cardid);
+ if (idx < 0) goto OnErrorExit;
+
+ json_object_object_add(responseJ, "api", json_object_new_string(cardRegistry[idx]->apiprefix));
+ if (cardRegistry[idx]->shortname)json_object_object_add(responseJ, "shortname", json_object_new_string(cardRegistry[idx]->shortname));
+ if (cardRegistry[idx]->longname) json_object_object_add(responseJ, "longname", json_object_new_string(cardRegistry[idx]->longname));
return 0;
@@ -395,7 +406,7 @@ OnErrorExit:
PUBLIC void alsaActiveHal(afb_req request) {
json_object *responseJ = json_object_new_array();
- for (int idx = 0; idx < MAX_SND_CARD; idx++) {
+ for (int idx = 0; idx < MAX_SND_HAL; idx++) {
if (!cardRegistry[idx]) break;
json_object *haldevJ = json_object_new_object();
@@ -430,7 +441,7 @@ PUBLIC void alsaRegisterHal(afb_req request) {
goto OnErrorExit;
}
- if (index == MAX_SND_CARD) {
+ if (index == MAX_SND_HAL) {
afb_req_fail_f(request, "alsahal-toomany", "Fail to register sndname=[%s]", shortname);
goto OnErrorExit;
}
@@ -528,7 +539,7 @@ PUBLIC void alsaPcmInfo (struct afb_req request) {
// prepare an object for response
responseJ = json_object_new_object();
- err = getHalFromCardId (cardId, responseJ);
+ err = getHalApiFromCardid (cardId, responseJ);
if (err < 0 ) {
afb_req_fail_f(request, "pcm:error", "PCM 'name:%s' snddev=hw:%d fail to retrieve hal API", pcmName, cardId);
goto OnErrorExit;