Browse Source

ft232r: Implement read buffer so ft232r_read always works like read(2)

Luke Dashjr 13 years ago
parent
commit
32245e443b
2 changed files with 18 additions and 4 deletions
  1. 18 3
      ft232r.c
  2. 0 1
      ft232r.h

+ 18 - 3
ft232r.c

@@ -148,6 +148,8 @@ struct ft232r_device_handle {
 	libusb_device_handle *h;
 	libusb_device_handle *h;
 	uint8_t i;
 	uint8_t i;
 	uint8_t o;
 	uint8_t o;
+	unsigned char ibuf[256];
+	uint8_t ibufLen;
 };
 };
 
 
 struct ft232r_device_handle *ft232r_open(libusb_device *dev)
 struct ft232r_device_handle *ft232r_open(libusb_device *dev)
@@ -249,10 +251,23 @@ ssize_t ft232r_write_all(struct ft232r_device_handle *dev, void *data, size_t co
 	return total ?: writ;
 	return total ?: writ;
 }
 }
 
 
-// Caveat: Reads of less than 512 bytes may fail :(
-ssize_t ft232r_read(struct ft232r_device_handle *dev, void *buf, size_t count)
+ssize_t ft232r_read(struct ft232r_device_handle *dev, void *data, size_t count)
 {
 {
-	return ft232r_readwrite(dev, dev->i, buf, count);
+	if (!dev->ibufLen) {
+		int transferred = ft232r_readwrite(dev, dev->i, dev->ibuf, sizeof(dev->ibuf));
+		if (transferred <= 0)
+			return transferred;
+		dev->ibufLen = transferred;
+	}
+	
+	if (count > dev->ibufLen)
+		count = dev->ibufLen;
+	memcpy(data, dev->ibuf, count);
+	if (count < dev->ibufLen) {
+		dev->ibufLen -= count;
+		memmove(dev->ibuf, &dev->ibuf[count], dev->ibufLen);
+	}
+	return count;
 }
 }
 
 
 #if 0
 #if 0

+ 0 - 1
ft232r.h

@@ -33,7 +33,6 @@ extern bool ft232r_purge_buffers(struct ft232r_device_handle *, enum ft232r_rese
 extern bool ft232r_set_bitmode(struct ft232r_device_handle *, uint8_t mask, uint8_t mode);
 extern bool ft232r_set_bitmode(struct ft232r_device_handle *, uint8_t mask, uint8_t mode);
 extern ssize_t ft232r_write(struct ft232r_device_handle *, void *data, size_t count);
 extern ssize_t ft232r_write(struct ft232r_device_handle *, void *data, size_t count);
 extern ssize_t ft232r_write_all(struct ft232r_device_handle *, void *data, size_t count);
 extern ssize_t ft232r_write_all(struct ft232r_device_handle *, void *data, size_t count);
-// ft232r_read caveat: Reads of less than 512 bytes may fail :(
 extern ssize_t ft232r_read(struct ft232r_device_handle *, void *buf, size_t count);
 extern ssize_t ft232r_read(struct ft232r_device_handle *, void *buf, size_t count);
 
 
 #endif
 #endif