summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/avirt/avirt.h4
-rw-r--r--src/avirt-config.c48
2 files changed, 28 insertions, 24 deletions
diff --git a/include/avirt/avirt.h b/include/avirt/avirt.h
index b795df9..18941fa 100644
--- a/include/avirt/avirt.h
+++ b/include/avirt/avirt.h
@@ -28,13 +28,15 @@
* @name: The name of the stream
* @channels: The number of channels for the stream
* @direction: The stream direction (SND_PCM_STREAM_PLAYBACK or SND_PCM_STREAM_CAPTURE)
+ * @map: The audio path to map this stream to
* @return: 0 on success, negative ERRNO otherwise
*
* Each stream creates a PCM device for the AVIRT sound card.
* Streams will not appear to the user-space until `AVIRT_SealCard()` is called.
* NOTE: Once `AVIRT_SealCard` is called, no more streams may be added.
*/
-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);
/**
* AVIRT_SealCard - Finalize AVIRT stream creation and register sound card
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 <http://www.gnu.org/licenses/>.
*/
-#define _GNU_SOURCE
-
#include <avirt/avirt.h>
#include <stdbool.h>
@@ -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;
}