diff options
author | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2019-10-17 15:55:52 +0200 |
---|---|---|
committer | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2019-10-17 15:56:41 +0200 |
commit | 3996b68e08b48010a38d93418ec5292224e16c4c (patch) | |
tree | af939f1deb18df1f881c48c9ec7702ca1302586d | |
parent | 1ffc6ec2e5377e53bd0a29628feab6e9da9902b4 (diff) |
Use dedicated systemd function for inotify eventsicefish_8.99.2icefish_8.99.1icefish/8.99.2icefish/8.99.18.99.28.99.1
Use inotify events dedicated systemd function 'sd_event_add_inotify'
instead of standard 'sd_event_add_io' function.
BUG-AGL: SPEC-2909
Change-Id: Ie33d7ff049c9baa70e063a4f0192fc4e77daffac
Signed-off-by: Jonathan Aillet <jonathan.aillet@iot.bzh>
-rw-r--r-- | alsa-binding/Alsa-RegEvt.c | 189 |
1 files changed, 39 insertions, 150 deletions
diff --git a/alsa-binding/Alsa-RegEvt.c b/alsa-binding/Alsa-RegEvt.c index faacb01..389fc1e 100644 --- a/alsa-binding/Alsa-RegEvt.c +++ b/alsa-binding/Alsa-RegEvt.c @@ -56,8 +56,6 @@ typedef struct { int available; char *pcmFileToMonitor; - int fd; - int wd; sd_event_source *src; afb_event_t afbevt; @@ -66,8 +64,6 @@ typedef struct { } pcmEvtHandleT; typedef struct { - int fd; - int wd; sd_event_source *src; afb_event_t afbevt; } sndCardEvtHandleT; @@ -182,12 +178,6 @@ STATIC void freePcmHandle(pcmEvtHandleT *pcmEventHandle) if(afb_event_is_valid(pcmEventHandle->afbevt)) afb_event_unref(pcmEventHandle->afbevt); - if(pcmEventHandle->fd != -1 && pcmEventHandle->wd != -1) - inotify_rm_watch(pcmEventHandle->fd, pcmEventHandle->wd); - - if(pcmEventHandle->fd != -1) - close(pcmEventHandle->fd); - free(pcmEventHandle->pcmFileToMonitor); free(pcmEventHandle); @@ -208,12 +198,6 @@ STATIC void freeSndCardHandle(sndCardEvtHandleT **cardMonitoringHandle) if(afb_event_is_valid(cardMonitoringHandleToFree->afbevt)) afb_event_unref(cardMonitoringHandleToFree->afbevt); - if(cardMonitoringHandleToFree->fd != -1 && cardMonitoringHandleToFree->wd != -1) - inotify_rm_watch(cardMonitoringHandleToFree->fd, cardMonitoringHandleToFree->wd); - - if(cardMonitoringHandleToFree->fd != -1) - close(cardMonitoringHandleToFree->fd); - free(cardMonitoringHandleToFree); *cardMonitoringHandle = NULL; @@ -420,116 +404,64 @@ STATIC int forceEventRefreshCB(TimerHandleT *context) } #endif -STATIC int pcmEventCB(sd_event_source* src, int fd, uint32_t revents, void* userData) +STATIC int pcmEventCB(sd_event_source* src, const struct inotify_event *event, void* userData) { int err; - ssize_t length, i = 0; - char buffer[INOTIFY_EVENT_BUF_LEN]; - - struct inotify_event *event; pcmEvtHandleT *pcmEventHandle = (pcmEvtHandleT *) userData; - if(revents & EPOLLERR) { - AFB_ERROR("An error has been send by event loop polling, prevent new errors to be fired by deleting event src"); + if((event->mask & IN_DELETE) || (event->mask & IN_DELETE_SELF)) { + AFB_WARNING("Monitored file has been deleted, delete monitoring"); freePcmHandle(pcmEventHandle); return -1; } - if(revents & EPOLLIN) { - length = read(fd, buffer, INOTIFY_EVENT_BUF_LEN); - if(length < 0) { - AFB_ERROR("Nothing read using inotify"); + if((event->mask & IN_OPEN) || (event->mask & IN_CLOSE)) { + 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); freePcmHandle(pcmEventHandle); return -2; } - - while(i < length) { - event = (struct inotify_event *) &buffer[i]; - - i += INOTIFY_EVENT_SIZE + event->len; - - if((event->mask & IN_DELETE) || (event->mask & IN_DELETE_SELF)) { - AFB_WARNING("Monitored file has been deleted, delete monitoring"); - freePcmHandle(pcmEventHandle); - return -3; - } - - if((event->mask & IN_OPEN) || (event->mask & IN_CLOSE)) { - 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; - } - } - } } return 0; } // This routine is called when sound cards are added/removed (when inotify '/dev/snd/by-path' events are fired) -STATIC int sndCardEventCB(sd_event_source* src, int fd, uint32_t revents, void* userData) +STATIC int sndCardEventCB(sd_event_source* src, const struct inotify_event *event, void* userData) { - ssize_t length, i = 0; - char buffer[INOTIFY_EVENT_BUF_LEN]; char *createdFile; - struct inotify_event *event; - sndCardCardFileEventHandleT *createdFileHandle; TimerHandleT *createdFileTimerHandle; sndCardsT *sndCards = (sndCardsT *) userData; - if(revents & EPOLLERR) { - AFB_ERROR("An error has been send by event loop polling, prevent new errors to be fired by deleting event src"); - freeSndCardHandle(&sndCards->cardMonitoring); - return -1; - } + if((event->mask & IN_CREATE) && + ! (event->mask & IN_ISDIR) && + (asprintf(&createdFile, "%s/%s", CARD_DIR_TO_WATCH, event->name) > 0)) { + createdFileHandle = calloc(1, sizeof(sndCardCardFileEventHandleT)); + createdFileHandle->createdFileName = strdup(createdFile); + createdFileHandle->sndCards = sndCards; - if(revents & EPOLLIN) { - length = read(fd, buffer, INOTIFY_EVENT_BUF_LEN); - if(length < 0) { - AFB_ERROR("Nothing read using inotify"); - freeSndCardHandle(&sndCards->cardMonitoring); - return -2; - } - - while(i < length) { - event = (struct inotify_event *) &buffer[i]; - - i += INOTIFY_EVENT_SIZE + event->len; + createdFileTimerHandle = calloc(1, sizeof(TimerHandleT)); - if(! event->len) - continue; - - if((event->mask & IN_CREATE) && - ! (event->mask & IN_ISDIR) && - (asprintf(&createdFile, "%s/%s", CARD_DIR_TO_WATCH, event->name) > 0)) { - createdFileHandle = calloc(1, sizeof(sndCardCardFileEventHandleT)); - createdFileHandle->createdFileName = strdup(createdFile); - createdFileHandle->sndCards = sndCards; + createdFileTimerHandle->uid = "sound card file creation delay"; + createdFileTimerHandle->count = 10; + createdFileTimerHandle->delay = 2; - createdFileTimerHandle = calloc(1, sizeof(TimerHandleT)); - - createdFileTimerHandle->uid = "sound card file creation delay"; - createdFileTimerHandle->count = 10; - createdFileTimerHandle->delay = 2; - - TimerEvtStart(sndCards->apiHandle, createdFileTimerHandle, sndCardTimerForCreatedFileCB, (void *) createdFileHandle); - } - - if((event->mask & IN_DELETE) && ! (event->mask & IN_ISDIR)) - updateAllAlsaCardsAvailabilityAndFireEvents(sndCards); - } + TimerEvtStart(sndCards->apiHandle, createdFileTimerHandle, sndCardTimerForCreatedFileCB, (void *) createdFileHandle); } + if((event->mask & IN_DELETE) && ! (event->mask & IN_ISDIR)) + updateAllAlsaCardsAvailabilityAndFireEvents(sndCards); + return 0; } @@ -710,8 +642,6 @@ STATIC afb_event_t alsaEvtSubscribeUnsubscribePcmAvailabilityEvent(afb_req_t req requestedPcmEventHandle->device = device; requestedPcmEventHandle->subdevice = subdevice; requestedPcmEventHandle->stream = stream; - requestedPcmEventHandle->fd = -1; - requestedPcmEventHandle->wd = -1; err = asprintf(&requestedPcmEventHandle->pcmFileToMonitor, PCM_DEV_FILE_STRING, @@ -757,26 +687,6 @@ STATIC afb_event_t alsaEvtSubscribeUnsubscribePcmAvailabilityEvent(afb_req_t req free(pcmEventName); - requestedPcmEventHandle->fd = inotify_init(); - if(requestedPcmEventHandle->fd < 0) { - afb_req_fail_f(request, "inotify-init", "Error %s happened while getting file descriptor for inotify", strerror(errno)); - freePcmHandle(requestedPcmEventHandle); - return NULL; - } - - requestedPcmEventHandle->wd = inotify_add_watch(requestedPcmEventHandle->fd, - requestedPcmEventHandle->pcmFileToMonitor, - IN_ALL_EVENTS); - if(requestedPcmEventHandle->wd < 0) { - afb_req_fail_f(request, - "inotify-watch", - "Error %s happened while setting watcher on '%s' file using inotify", - strerror(errno), - requestedPcmEventHandle->pcmFileToMonitor); - freePcmHandle(requestedPcmEventHandle); - return NULL; - } - requestedPcmEventHandle->available = alsaIsPcmAvailableUsingId(card, device, subdevice, stream); if(requestedPcmEventHandle->available < 0) { afb_req_fail_f(request, @@ -790,12 +700,12 @@ STATIC afb_event_t alsaEvtSubscribeUnsubscribePcmAvailabilityEvent(afb_req_t req return NULL; } - err = sd_event_add_io(afb_daemon_get_event_loop(), - &requestedPcmEventHandle->src, - requestedPcmEventHandle->fd, - EPOLLIN, - pcmEventCB, - requestedPcmEventHandle); + err = sd_event_add_inotify(afb_daemon_get_event_loop(), + &requestedPcmEventHandle->src, + requestedPcmEventHandle->pcmFileToMonitor, + IN_ALL_EVENTS, + pcmEventCB, + requestedPcmEventHandle); if(err < 0) { afb_req_fail_f(request, "register-mainloop", @@ -834,27 +744,6 @@ STATIC afb_event_t alsaEvtSubscribeUnsubscribeSoundCardEvent(afb_req_t request, return NULL; } - sndCards->cardMonitoring->fd = -1; - sndCards->cardMonitoring->wd = -1; - - sndCards->cardMonitoring->fd = inotify_init(); - if(sndCards->cardMonitoring->fd < 0) { - afb_req_fail_f(request, "inotify-init", "Error %s happened while getting file descriptor for inotify", strerror(errno)); - freeSndCardHandle(&sndCards->cardMonitoring); - return NULL; - } - - sndCards->cardMonitoring->wd = inotify_add_watch(sndCards->cardMonitoring->fd, CARD_DIR_TO_WATCH, IN_CREATE | IN_DELETE); - if(sndCards->cardMonitoring->wd < 0) { - afb_req_fail_f(request, - "inotify-watch", - "Error %s happened while setting watcher on '%s' directory using inotify", - strerror(errno), - CARD_DIR_TO_WATCH); - freeSndCardHandle(&sndCards->cardMonitoring); - return NULL; - } - sndCards->cardMonitoring->afbevt = afb_daemon_make_event("card-monitoring"); if(! afb_event_is_valid(sndCards->cardMonitoring->afbevt)) { afb_req_fail_f(request, "card-monitoring-event", "Cannot register new binder card-monitoring event"); @@ -863,12 +752,12 @@ STATIC afb_event_t alsaEvtSubscribeUnsubscribeSoundCardEvent(afb_req_t request, } // register sound event to binder main loop - err = sd_event_add_io(afb_daemon_get_event_loop(), - &sndCards->cardMonitoring->src, - sndCards->cardMonitoring->fd, - EPOLLIN, - sndCardEventCB, - sndCards); + err = sd_event_add_inotify(afb_daemon_get_event_loop(), + &sndCards->cardMonitoring->src, + CARD_DIR_TO_WATCH, + IN_CREATE | IN_DELETE, + sndCardEventCB, + sndCards); if(err < 0) { afb_req_fail_f(request, "register-mainloop", "Cannot hook sound card events to mainloop err=%d", err); freeSndCardHandle(&sndCards->cardMonitoring); |