summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Aillet <jonathan.aillet@iot.bzh>2019-10-02 16:21:46 +0200
committerJonathan Aillet <jonathan.aillet@iot.bzh>2019-10-02 16:40:51 +0200
commit1238224125535c8a64615dff006b116d9c78e5b3 (patch)
treeaf1d4d015590bfb76a63ec650d2d87f3b5a1569f
parent3c407194add77ca55ab12b1ab6ea7cf54f89c092 (diff)
Add function to deal with PCM availability event
Add a dedicated function to refresh monitored PCM status and fire event if PCM availability has changed. BUG-AGL: SPEC-2849 Change-Id: I1358b4019d7da80a8055bfbcd0bbf3f55a2499e3 Signed-off-by: Jonathan Aillet <jonathan.aillet@iot.bzh>
-rw-r--r--alsa-binding/Alsa-RegEvt.c126
1 files changed, 74 insertions, 52 deletions
diff --git a/alsa-binding/Alsa-RegEvt.c b/alsa-binding/Alsa-RegEvt.c
index 1a803cb..1241e0e 100644
--- a/alsa-binding/Alsa-RegEvt.c
+++ b/alsa-binding/Alsa-RegEvt.c
@@ -242,6 +242,67 @@ STATIC void freeCardAlsaControlHandle(ctrlEvtHandleT **cardControlHandle)
*cardControlHandle = NULL;
}
+STATIC int updatePcmAvailabilityAndFireEvent(pcmEvtHandleT *pcmEventHandle)
+{
+ int err, pcmStatus, subscriberCount = -1;
+
+ char *pcmName;
+
+ json_object *eventJ = NULL;
+
+ pcmStatus = alsaIsPcmAvailableUsingId(pcmEventHandle->card,
+ pcmEventHandle->device,
+ pcmEventHandle->subdevice,
+ pcmEventHandle->stream);
+ if(pcmEventHandle->available == pcmStatus)
+ return 0;
+
+ pcmEventHandle->available = pcmStatus;
+
+ err = asprintf(&pcmName,
+ "hw:%i,%i,%i",
+ pcmEventHandle->card,
+ pcmEventHandle->device,
+ pcmEventHandle->subdevice);
+ if(err <= 0) {
+ AFB_ERROR("Did not succeed to generate pcm name string (card : %i, device : %i, subdevice : %i)",
+ pcmEventHandle->card,
+ pcmEventHandle->device,
+ pcmEventHandle->subdevice);
+ freePcmHandle(pcmEventHandle);
+ return -1;
+ }
+
+ err = wrap_json_pack(&eventJ,
+ "{s:s, s:i, s:i}",
+ "name", pcmName,
+ "stream", pcmEventHandle->stream,
+ "available", pcmEventHandle->available);
+ if(err) {
+ AFB_ERROR("Did not succeed to generate pcm event json info (name : %s, stream : %s, available : %i)",
+ pcmName,
+ (pcmEventHandle->stream == SND_PCM_STREAM_PLAYBACK) ? "playback" : "capture",
+ pcmEventHandle->available);
+ free(pcmName);
+ freePcmHandle(pcmEventHandle);
+ return -2;
+ }
+
+ subscriberCount = afb_event_push(pcmEventHandle->afbevt, eventJ);
+
+ if(! subscriberCount) {
+ AFB_WARNING("Nobody listening for pcm %s hw:%i,%i,%i availability events, stop monitoring from now on",
+ (pcmEventHandle->stream == SND_PCM_STREAM_PLAYBACK) ? "playback" : "capture",
+ pcmEventHandle->card,
+ pcmEventHandle->device,
+ pcmEventHandle->subdevice);
+ freePcmHandle(pcmEventHandle);
+ return -3;
+ }
+
+ return 0;
+}
+
STATIC void updateSelectedAlsaCardsAvailabilityAndFireEvents(sndCardsT *sndCards, int first, int last)
{
int idx, available, subscriberCount = -1;
@@ -291,7 +352,8 @@ STATIC void updateSelectedAlsaCardsAvailabilityAndFireEvents(sndCardsT *sndCards
}
}
-STATIC void updateAllAlsaCardsAvailabilityAndFireEvents(sndCardsT *sndCards) {
+STATIC void updateAllAlsaCardsAvailabilityAndFireEvents(sndCardsT *sndCards)
+{
updateSelectedAlsaCardsAvailabilityAndFireEvents(sndCards, 0, (MAX_SND_CARD - 1));
}
@@ -322,16 +384,14 @@ STATIC int sndCardTimerForCreatedFileCB(TimerHandleT *context)
STATIC int pcmEventCB(sd_event_source* src, int fd, uint32_t revents, void* userData)
{
- int err, pcmStatus, subscriberCount = -1;
+ int err;
ssize_t length, i = 0;
- char buffer[INOTIFY_EVENT_BUF_LEN], *pcmName;
+ char buffer[INOTIFY_EVENT_BUF_LEN];
struct inotify_event *event;
pcmEvtHandleT *pcmEventHandle = (pcmEvtHandleT *) userData;
- json_object *eventJ = NULL;
-
if(revents & EPOLLERR) {
AFB_ERROR("An error has been send by event loop polling, prevent new errors to be fired by deleting event src");
freePcmHandle(pcmEventHandle);
@@ -358,58 +418,20 @@ STATIC int pcmEventCB(sd_event_source* src, int fd, uint32_t revents, void* user
}
if((event->mask & IN_OPEN) || (event->mask & IN_CLOSE)) {
- pcmStatus = alsaIsPcmAvailableUsingId(pcmEventHandle->card,
- pcmEventHandle->device,
- pcmEventHandle->subdevice,
- pcmEventHandle->stream);
- if(pcmEventHandle->available != pcmStatus) {
- pcmEventHandle->available = pcmStatus;
-
- err = asprintf(&pcmName,
- "hw:%i,%i,%i",
- pcmEventHandle->card,
- pcmEventHandle->device,
- pcmEventHandle->subdevice);
- if(err <= 0) {
- AFB_ERROR("Did not succeed to generate pcm name string (card : %i, device : %i, subdevice : %i)",
- pcmEventHandle->card,
- pcmEventHandle->device,
- pcmEventHandle->subdevice);
- freePcmHandle(pcmEventHandle);
- return -4;
- }
-
- err = wrap_json_pack(&eventJ,
- "{s:s, s:i, s:i}",
- "name", pcmName,
- "stream", pcmEventHandle->stream,
- "available", pcmEventHandle->available);
- if(err) {
- AFB_ERROR("Did not succeed to generate pcm event json info (name : %s, stream : %s, available : %i)",
- pcmName,
- (pcmEventHandle->stream == SND_PCM_STREAM_PLAYBACK) ? "playback" : "capture",
- pcmEventHandle->available);
- free(pcmName);
- freePcmHandle(pcmEventHandle);
- return -5;
- }
-
- subscriberCount = afb_event_push(pcmEventHandle->afbevt, eventJ);
+ err = updatePcmAvailabilityAndFireEvent(pcmEventHandle);
+ if(err) {
+ AFB_ERROR("Error %i happened when tried to refresh %s pcm hw:%i,%i,%i availability",
+ err,
+ (pcmEventHandle->stream == SND_PCM_STREAM_PLAYBACK) ? "playback" : "capture",
+ pcmEventHandle->card,
+ pcmEventHandle->device,
+ pcmEventHandle->subdevice);
+ return -4;
}
}
}
}
- if(! subscriberCount) {
- AFB_WARNING("Nobody listening for pcm %s hw:%i,%i,%i availability events, stop monitoring from now on",
- (pcmEventHandle->stream == SND_PCM_STREAM_PLAYBACK) ? "playback" : "capture",
- pcmEventHandle->card,
- pcmEventHandle->device,
- pcmEventHandle->subdevice);
- freePcmHandle(pcmEventHandle);
- return -6;
- }
-
return 0;
}