diff options
author | px4dev <px4@purgatory.org> | 2012-12-29 00:01:04 -0800 |
---|---|---|
committer | px4dev <px4@purgatory.org> | 2012-12-29 00:01:04 -0800 |
commit | 35c82ff2fc63ab823770f9776e6b6a0f81cd4452 (patch) | |
tree | 86956f9f00f1270eafc921748ac024a7daa92f80 /apps/systemcmds/mixer/mixer.c | |
parent | f0da789626c32695e670b55dab29283eed4a05c6 (diff) | |
download | px4-firmware-35c82ff2fc63ab823770f9776e6b6a0f81cd4452.tar.gz px4-firmware-35c82ff2fc63ab823770f9776e6b6a0f81cd4452.tar.bz2 px4-firmware-35c82ff2fc63ab823770f9776e6b6a0f81cd4452.zip |
Make mixer ioctls load from a memory buffer rather than a file. This is prep for uploading the memory buffer to IO to be processed there.
Diffstat (limited to 'apps/systemcmds/mixer/mixer.c')
-rw-r--r-- | apps/systemcmds/mixer/mixer.c | 68 |
1 files changed, 45 insertions, 23 deletions
diff --git a/apps/systemcmds/mixer/mixer.c b/apps/systemcmds/mixer/mixer.c index 3f52bdbf1..8d73bfcc4 100644 --- a/apps/systemcmds/mixer/mixer.c +++ b/apps/systemcmds/mixer/mixer.c @@ -43,14 +43,17 @@ #include <unistd.h> #include <fcntl.h> #include <errno.h> +#include <ctype.h> +#include <nuttx/compiler.h> +#include <systemlib/err.h> #include <drivers/drv_mixer.h> #include <uORB/topics/actuator_controls.h> __EXPORT int mixer_main(int argc, char *argv[]); -static void usage(const char *reason); -static void load(const char *devname, const char *fname); +static void usage(const char *reason) noreturn_function; +static void load(const char *devname, const char *fname) noreturn_function; int mixer_main(int argc, char *argv[]) @@ -63,12 +66,9 @@ mixer_main(int argc, char *argv[]) usage("missing device or filename"); load(argv[2], argv[3]); - - } else { - usage("unrecognised command"); } - return 0; + usage("unrecognised command"); } static void @@ -79,34 +79,56 @@ usage(const char *reason) fprintf(stderr, "usage:\n"); fprintf(stderr, " mixer load <device> <filename>\n"); - /* XXX automatic setups for quad, etc. */ + /* XXX other useful commands? */ exit(1); } static void load(const char *devname, const char *fname) { - int dev = -1; - int ret, result = 1; + int dev; + FILE *fp; + char line[80]; + char buf[512]; /* open the device */ - if ((dev = open(devname, 0)) < 0) { - fprintf(stderr, "can't open %s\n", devname); - goto out; - } + if ((dev = open(devname, 0)) < 0) + err(1, "can't open %s\n", devname); - /* tell it to load the file */ - ret = ioctl(dev, MIXERIOCLOADFILE, (unsigned long)fname); + /* reset mixers on the device */ + if (ioctl(dev, MIXERIOCRESET, 0)) + err(1, "can't reset mixers on %s", devname); - if (ret != 0) { - fprintf(stderr, "failed loading %s\n", fname); - } + /* open the mixer definition file */ + fp = fopen(fname, "r"); + if (fp == NULL) + err(1, "can't open %s", fname); + + /* read valid lines from the file into a buffer */ + buf[0] = '\0'; + for (;;) { - result = 0; -out: + /* get a line, bail on error/EOF */ + line[0] = '\0'; + if (fgets(line, sizeof(line), fp) == NULL) + break; + + /* if the line doesn't look like a mixer definition line, skip it */ + if ((strlen(line) < 2) || !isupper(line[0]) || (line[1] != ':')) + continue; + + /* if the line is too long to fit in the buffer, bail */ + if ((strlen(line) + strlen(buf) + 1) >= sizeof(buf)) + break; + + /* add the line to the buffer */ + strcat(buf, line); + } - if (dev != -1) - close(dev); + /* XXX pass the buffer to the device */ + int ret = ioctl(dev, MIXERIOCLOADBUF, (unsigned long)buf); - exit(result); + if (ret < 0) + err(1, "error loading mixers from %s", fname); + exit(0); } |