From 49b861b4066d56ffb2c7ec71da6843d8e3c5e04b Mon Sep 17 00:00:00 2001 From: Mark Farrugia Date: Thu, 11 Oct 2018 17:37:06 +1100 Subject: Add map to AVIRT_CreateStream Signed-off-by: Mark Farrugia --- src/avirt-config.c | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/avirt-config.c b/src/avirt-config.c index 167bcc2..b2e0d34 100644 --- a/src/avirt-config.c +++ b/src/avirt-config.c @@ -20,8 +20,6 @@ * along with this library. If not, see . */ -#define _GNU_SOURCE - #include #include @@ -58,6 +56,17 @@ } while (0) #endif +#define WRITE_TO_PATH(path, fmt, args...) \ + do { \ + FILE *fd = fopen(path, "w"); \ + if (!fd) { \ + AVIRT_ERROR_V("Failed to open file at '%s'", path); \ + return -EPERM; \ + } \ + fprintf(fd, fmt, ##args); \ + fclose(fd); \ + } while (0) + static bool configfs_mounted = false; static bool card_sealed = false; @@ -74,11 +83,12 @@ static int mount_configfs() return err; } -int AVIRT_CreateStream(const char *name, unsigned int channels, int direction) +int AVIRT_CreateStream(const char *name, unsigned int channels, int direction, + const char *map) { int err; char path[AVIRT_CONFIGFS_PATH_MAXLEN]; - char path_chans[AVIRT_CONFIGFS_PATH_MAXLEN]; + char path_attr[AVIRT_CONFIGFS_PATH_MAXLEN]; FILE *fd; // Check whether the configfs filesystem is mounted @@ -123,19 +133,19 @@ int AVIRT_CreateStream(const char *name, unsigned int channels, int direction) return err; } - // Open file and write to it - strcpy(path_chans, path); - strcat(path_chans, "/channels"); - fd = fopen(path_chans, "w"); - if (!fd) + // Write channels + strcpy(path_attr, path); + strcat(path_attr, "/channels"); + WRITE_TO_PATH(path_attr, "%d", channels); + + if (map) { - AVIRT_ERROR_V("Failed to open file at '%s'", path_chans); - return -1; + // Write mapping + strcpy(path_attr, path); + strcat(path_attr, "/map"); + WRITE_TO_PATH(path_attr, "%s", map); } - fprintf(fd, "%d", channels); - fclose(fd); - return 0; } @@ -161,15 +171,7 @@ int AVIRT_SealCard() strcpy(path_sealed, AVIRT_CONFIGFS_PATH_STREAMS); strcat(path_sealed, "/sealed"); - fd = fopen(path_sealed, "w"); - if (!fd) - { - AVIRT_ERROR_V("Failed to open file at '%s'", path_sealed); - return -1; - } - - fprintf(fd, "%d", 1); - fclose(fd); + WRITE_TO_PATH(path_sealed, "%d", 1); card_sealed = true; } -- cgit 1.2.3-korg