Update to latest tinycompress

01ff678 compress: Add support for capture streams
8c9d5da tinycompress: cplay: Correct a couple of small message typos

Change-Id: Ieee7cce63930eb2f846a7a86a92806f1db45656b
diff --git a/compress.c b/compress.c
index 5398e10..d5115b6 100644
--- a/compress.c
+++ b/compress.c
@@ -240,13 +240,12 @@
 		oops(&bad_compress, -EINVAL, "can't deduce device direction from given flags");
 		goto config_fail;
 	}
-	if (flags & COMPRESS_OUT) {
-		/* this should be removed once we have capture tested */
-		oops(&bad_compress, -EINVAL, "this version doesnt support capture");
-		goto config_fail;
-	}
 
-	compress->fd = open(fn, O_WRONLY);
+	if (flags & COMPRESS_OUT) {
+		compress->fd = open(fn, O_RDONLY);
+	} else {
+		compress->fd = open(fn, O_WRONLY);
+	}
 	if (compress->fd < 0) {
 		oops(&bad_compress, errno, "cannot open device '%s'", fn);
 		goto config_fail;
@@ -403,7 +402,60 @@
 
 int compress_read(struct compress *compress, void *buf, unsigned int size)
 {
-	return oops(compress, -ENOTTY, "Not supported yet in lib");
+	struct snd_compr_avail avail;
+	struct pollfd fds;
+	int to_read = 0;
+	int num_read, total = 0, ret;
+	char* cbuf = buf;
+	const unsigned int frag_size = compress->config->fragment_size;
+
+	if (!(compress->flags & COMPRESS_OUT))
+		return oops(compress, -EINVAL, "Invalid flag set");
+	if (!is_compress_ready(compress))
+		return oops(compress, -ENODEV, "device not ready");
+	fds.fd = compress->fd;
+	fds.events = POLLIN;
+
+	while (size) {
+		if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail))
+			return oops(compress, errno, "cannot get avail");
+
+		if ( (avail.avail < frag_size) && (avail.avail < size) ) {
+			/* Less than one fragment available and not at the
+			 * end of the read, so poll
+			 */
+			ret = poll(&fds, 1, compress->max_poll_wait_ms);
+			/* A pause will cause -EBADFD or zero.
+			 * This is not an error, just stop reading */
+			if ((ret == 0) || (ret == -EBADFD))
+				break;
+			if (ret < 0)
+				return oops(compress, errno, "poll error");
+			if (fds.revents & POLLIN) {
+				continue;
+			}
+			if (fds.revents & POLLERR) {
+				return oops(compress, -EIO, "poll returned error!");
+			}
+		}
+		/* read avail bytes */
+		if (size > avail.avail)
+			to_read = avail.avail;
+		else
+			to_read = size;
+		num_read = read(compress->fd, cbuf, to_read);
+		/* If play was paused the read returns -EBADFD */
+		if (num_read == -EBADFD)
+			break;
+		if (num_read < 0)
+			return oops(compress, errno, "read failed!");
+
+		size -= num_read;
+		cbuf += num_read;
+		total += num_read;
+	}
+
+	return total;
 }
 
 int compress_start(struct compress *compress)
diff --git a/cplay.c b/cplay.c
index b11d676..21e64a4 100644
--- a/cplay.c
+++ b/cplay.c
@@ -337,7 +337,7 @@
 				goto BUF_EXIT;
 			}
 			if (wrote != num_read) {
-				/* TODO: Buufer pointer needs to be set here */
+				/* TODO: Buffer pointer needs to be set here */
 				fprintf(stderr, "We wrote %d, DSP accepted %d\n", num_read, wrote);
 			}
 			if (verbose) {
@@ -348,7 +348,7 @@
 	} while (num_read > 0);
 
 	if (verbose)
-		printf("%s: exit sucess\n", __func__);
+		printf("%s: exit success\n", __func__);
 	/* issue drain if it supports */
 	compress_drain(compress);
 	free(buffer);
diff --git a/include/tinycompress/tinycompress.h b/include/tinycompress/tinycompress.h
index ba9f3b6..9867afc 100644
--- a/include/tinycompress/tinycompress.h
+++ b/include/tinycompress/tinycompress.h
@@ -76,7 +76,7 @@
 	__u32 encoder_padding;
 };
 
-#define COMPRESS_OUT        0x00000000
+#define COMPRESS_OUT        0x20000000
 #define COMPRESS_IN         0x10000000
 
 struct compress;