aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Farrugia <mark.farrugia@fiberdyne.com.au>2018-10-01 16:20:43 +1000
committerMark Farrugia <mark.farrugia@fiberdyne.com.au>2018-10-30 13:43:39 +1100
commit3424997c580659e5ef7df4c4c70d172c6f9bdcd2 (patch)
treebc2d95756e5b0bc532282704be7588d091191a74
parenta42a991807ba56b30121b7faa0d0d243516e853b (diff)
Add initial working implementation
Signed-off-by: Mark Farrugia <mark.farrugia@fiberdyne.com.au>
-rw-r--r--CMakeLists.txt32
-rw-r--r--include/avirt.h29
-rw-r--r--src/avirt-config.c120
3 files changed, 181 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..9af162f
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,32 @@
+#*************************************************************************
+#
+# FIBERDYNE CONFIDENTIAL
+# __________________
+#
+# [2010] - [2017] Fiberdyne Systems Pty Ltd
+# All Rights Reserved.
+#
+# NOTICE: All information contained herein is, and remains
+# the property of Fiberdyne Systems Pty Ltd and its suppliers,
+# if any. The intellectual and technical concepts contained
+# herein are proprietary to Fiberdyne Systems Pty Ltd
+# and its suppliers and may be covered by U.S. and Foreign Patents,
+# patents in process, and are protected by trade secret or copyright law.
+# Dissemination of this information or reproduction of this material
+# is strictly forbidden unless prior written permission is obtained
+# from Fiberdyne Systems Pty Ltd.
+#
+#*************************************************************************
+
+#-------------------------------------------------------------------------------
+# LIBAVIRT CONFIGURATION LIBRARY
+#-------------------------------------------------------------------------------
+
+# Defines
+add_definitions(-D_GNU_SOURCE)
+
+# Target
+add_library(avirt STATIC src/avirt_*.c)
+
+# Target includes
+target_include_directories(avirt PUBLIC include)
diff --git a/include/avirt.h b/include/avirt.h
new file mode 100644
index 0000000..5bb8731
--- /dev/null
+++ b/include/avirt.h
@@ -0,0 +1,29 @@
+/*
+ * AVIRT User-space library
+ *
+ * avirt.h - AVIRT User-space library header
+ *
+ * Copyright (C) 2018 Fiberdyne Systems Pty Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _AVIRT_H_
+#define _AVIRT_H_
+
+int AVIRT_CreateStream(const char *name, unsigned int channels, int direction);
+
+int AVIRT_SealCard();
+
+#endif // _AVIRT_H_
diff --git a/src/avirt-config.c b/src/avirt-config.c
new file mode 100644
index 0000000..f819c65
--- /dev/null
+++ b/src/avirt-config.c
@@ -0,0 +1,120 @@
+/*
+ * AVIRT User-space library
+ *
+ * avirt-config.c - Main AVIRT configuration via configfs
+ *
+ * Copyright (C) 2018 Fiberdyne Systems Pty Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "avirt-config.h"
+
+#include <stdbool.h>
+#include <alsa/asoundlib.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+
+#define AVIRT_CONFIGFS_PATH_STREAMS "/config/avirt/streams/"
+#define AVIRT_CONFIGFS_PATH_MAXLEN 64
+
+static bool configfs_mounted = false;
+
+static int mount_configfs()
+{
+ int err;
+
+ err = mount("none", "/config", "configfs", 0, NULL);
+ if (!err)
+ configfs_mounted = true;
+ else
+ printf("AVIRT ERROR: Failed to mount configfs filesystem!");
+
+ return err;
+}
+
+int AVIRT_CreateStream(const char *name, unsigned int channels, int direction)
+{
+ int err;
+ char path[AVIRT_CONFIGFS_PATH_MAXLEN];
+ char path_chans[AVIRT_CONFIGFS_PATH_MAXLEN];
+ FILE *fd;
+
+ if (!configfs_mounted) {
+ err = mount_configfs();
+ if (err < 0)
+ return err;
+ }
+
+ strcpy(path, AVIRT_CONFIGFS_PATH_STREAMS);
+ switch (direction) {
+ case SND_PCM_STREAM_PLAYBACK:
+ strcat(path, "playback_");
+ break;
+ case SND_PCM_STREAM_CAPTURE:
+ strcat(path, "capture_");
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ if ((AVIRT_CONFIGFS_PATH_MAXLEN - strlen(path)) < strlen(name))
+ {
+ printf("AVIRT ERROR: Cannot create stream '%s' since name is too long!", name);
+ return -ENOMEM;
+ }
+
+ strcat(path, name);
+ err = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
+ if (err < 0)
+ {
+ printf("AVIRT ERROR: Cannot create stream '%s' at directory '%s'\n", name, path);
+ return err;
+ }
+
+ strcpy(path_chans, path);
+ strcat(path_chans, "/channels");
+ fd = fopen(path_chans, "w");
+ if (!fd)
+ {
+ printf("AVIRT ERROR: Failed to open file at '%s'", path_chans);
+ return -1;
+ }
+
+ fprintf(fd, "%d", channels);
+
+ return 0;
+}
+
+int AVIRT_SealCard()
+{
+ int err;
+
+ if (!configfs_mounted) {
+ err = mount_configfs();
+ if (err < 0)
+ return err;
+ }
+
+ strcpy(path_sealed, AVIRT_);
+ strcat(path_chans, "/sealed");
+ fd = fopen(path_chans, "w");
+ if (!fd)
+ {
+ printf("AVIRT ERROR: Failed to open file at '%s'", path_chans);
+ return -1;
+ }
+
+ fprintf(fd, "%d", 1);
+}