1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

improved read_random() to return read bytes and fix return type

This commit is contained in:
Steffen Vogel 2017-02-12 14:36:44 -03:00
parent e9f8a50c3c
commit 88a21ade2e
4 changed files with 16 additions and 21 deletions

View file

@ -208,7 +208,7 @@ int version_parse(const char *s, struct version *v);
#endif
/** Fill buffer with random data */
int read_random(char *buf, size_t len);
ssize_t read_random(char *buf, size_t len);
/** Hexdump bytes */
void printb(void *mem, size_t len);

View file

@ -302,31 +302,28 @@ void * memdup(const void *src, size_t bytes)
return dst;
}
int read_random(char *buf, size_t len)
ssize_t read_random(char *buf, size_t len)
{
int fd;
ssize_t bytes, total;
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
return -1;
ssize_t bytes, total = 0;
bytes = 0;
total = 0;
while (total < len) {
bytes = read(fd, buf + total, len - total);
if (bytes < 0)
goto out;
break;
total += bytes;
}
close(fd);
return 0;
out:
close(fd);
return -1;
return bytes;
}
void printb(void *mem, size_t len)

View file

@ -232,8 +232,8 @@ int fpga_test_fifo(struct fpga *f)
/* Get some random data to compare */
memset(dst, 0, sizeof(dst));
ret = read_random((char *) src, sizeof(src));
if (ret)
len = read_random((char *) src, sizeof(src));
if (len != sizeof(src))
error("Failed to get random data");
len = fifo_write(fifo, (char *) src, sizeof(src));
@ -267,7 +267,7 @@ int fpga_test_dma(struct fpga *f)
/* Simple DMA can only transfer up to 4 kb due to
* PCIe page size burst limitation */
ssize_t len = dma->dma.inst.HasSg ? 64 << 20 : 1 << 2;
ssize_t len2, len = dma->dma.inst.HasSg ? 64 << 20 : 1 << 2;
ret = dma_alloc(dma, &mem, 2 * len, 0);
if (ret)
@ -278,8 +278,8 @@ int fpga_test_dma(struct fpga *f)
return -1;
/* Get new random data */
ret = read_random(src.base_virt, len);
if (ret)
len2 = read_random(src.base_virt, len);
if (len2 != len)
serror("Failed to get random data");
int irq_mm2s = dma->irq;

View file

@ -1,6 +1,7 @@
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <villas/utils.h>
#include <villas/advio.h>
#include <errno.h>
@ -42,10 +43,7 @@ Test(advio, upload)
char buffer[128];
/* Get some random bytes */
FILE *r = fopen("/dev/urandom", "r");
cr_assert(r);
len1 = fread(rnd, 1, sizeof(rnd), r);
len1 = read_random(rnd, sizeof(rnd));
/* Open file for writing */
af = afopen(uri, "w+");