diff --git a/boards/native/Makefile.features b/boards/native/Makefile.features index c933e2bf3f..2c1e593763 100644 --- a/boards/native/Makefile.features +++ b/boards/native/Makefile.features @@ -1 +1,2 @@ FEATURES_PROVIDED += transceiver periph_cpuid cpp +FEATURES_PROVIDED += periph_random diff --git a/cpu/native/include/native_internal.h b/cpu/native/include/native_internal.h index 62947d6314..2bbfc5746d 100644 --- a/cpu/native/include/native_internal.h +++ b/cpu/native/include/native_internal.h @@ -79,6 +79,7 @@ extern void* (*real_calloc)(size_t nmemb, size_t size); extern void* (*real_malloc)(size_t size); extern void* (*real_realloc)(void *ptr, size_t size); extern void (*real_freeaddrinfo)(struct addrinfo *res); +extern void (*real_srandom)(unsigned int seed); /* The ... is a hack to save includes: */ extern int (*real_accept)(int socket, ...); /* The ... is a hack to save includes: */ @@ -99,6 +100,7 @@ extern int (*real_setsockopt)(int socket, ...); extern int (*real_socket)(int domain, int type, int protocol); extern int (*real_printf)(const char *format, ...); extern int (*real_unlink)(const char *); +extern long int (*real_random)(void); extern const char* (*real_gai_strerror)(int errcode); extern FILE* (*real_fopen)(const char *path, const char *mode); diff --git a/cpu/native/include/periph_conf.h b/cpu/native/include/periph_conf.h new file mode 100644 index 0000000000..0774418052 --- /dev/null +++ b/cpu/native/include/periph_conf.h @@ -0,0 +1,35 @@ +/** + * Native CPU peripheral configuration + * + * Copyright (C) 2014 Ludwig Ortmann + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + * + * @ingroup native_cpu + * @{ + * @file + * @author Ludwig Ortmann + */ + +#ifndef PERIPH_CONF_H +#define PERIPH_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/** + * @name Random Number Generator configuration + * @{ + */ +#define RANDOM_NUMOF (1U) +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* PERIPH_CONF_H */ +/** @} */ diff --git a/cpu/native/periph/random.c b/cpu/native/periph/random.c new file mode 100644 index 0000000000..427a1f2e75 --- /dev/null +++ b/cpu/native/periph/random.c @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2014 Ludwig Ortmann + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup native_cpu + * @{ + * + * @author Ludwig Ortmann + */ + +#include +#include +#include + +#include "cpu-conf.h" +#include "native_internal.h" + +#include "periph/random.h" + +#define ENABLE_DEBUG (0) +#include "debug.h" + +static int powered = 0; + +void random_init(void) +{ + DEBUGF("random_init: powering on\n"); + random_poweron(); +} + +int random_read(char *buf, unsigned int num) +{ + if (!powered) { + warnx("random_read: random device not powered, failing\n"); + return 0; + } + + DEBUGF("random_read: writing %ui bytes\n", num); + for (unsigned i = 0; i < num; i++) { + _native_syscall_enter(); + buf[i] = (char)real_random(); + _native_syscall_leave(); + } + return num; +} + +/** + * seed host random module with @ref _native_id + */ +void random_poweron(void) +{ + DEBUGF("random_poweron: power on\n"); + powered = 1; + _native_syscall_enter(); + real_srandom((unsigned int)_native_id); + _native_syscall_leave(); +} + +void random_poweroff(void) +{ + DEBUGF("random_poweroff: power off\n"); + powered = 0; +} + +/** + * @} + */ diff --git a/cpu/native/syscalls.c b/cpu/native/syscalls.c index 716997ee7e..61fc1653d6 100644 --- a/cpu/native/syscalls.c +++ b/cpu/native/syscalls.c @@ -56,6 +56,7 @@ void* (*real_malloc)(size_t size); void* (*real_calloc)(size_t nmemb, size_t size); void* (*real_realloc)(void *ptr, size_t size); void (*real_freeaddrinfo)(struct addrinfo *res); +void (*real_srandom)(unsigned int seed); int (*real_accept)(int socket, ...); int (*real_bind)(int socket, ...); int (*real_printf)(const char *format, ...); @@ -73,6 +74,7 @@ int (*real_pipe)(int[2]); int (*real_setsockopt)(int socket, ...); int (*real_socket)(int domain, int type, int protocol); int (*real_unlink)(const char *); +long int (*real_random)(void); const char* (*real_gai_strerror)(int errcode); FILE* (*real_fopen)(const char *path, const char *mode); @@ -362,6 +364,7 @@ void _native_init_syscalls(void) *(void **)(&real_realloc) = dlsym(RTLD_NEXT, "realloc"); *(void **)(&real_free) = dlsym(RTLD_NEXT, "free"); *(void **)(&real_freeaddrinfo) = dlsym(RTLD_NEXT, "freeaddrinfo"); + *(void **)(&real_srandom) = dlsym(RTLD_NEXT, "srandom"); *(void **)(&real_accept) = dlsym(RTLD_NEXT, "accept"); *(void **)(&real_bind) = dlsym(RTLD_NEXT, "bind"); *(void **)(&real_printf) = dlsym(RTLD_NEXT, "printf"); @@ -375,6 +378,7 @@ void _native_init_syscalls(void) *(void **)(&real_setsockopt) = dlsym(RTLD_NEXT, "setsockopt"); *(void **)(&real_socket) = dlsym(RTLD_NEXT, "socket"); *(void **)(&real_unlink) = dlsym(RTLD_NEXT, "unlink"); + *(void **)(&real_random) = dlsym(RTLD_NEXT, "random"); *(void **)(&real_execve) = dlsym(RTLD_NEXT, "execve"); *(void **)(&real_listen) = dlsym(RTLD_NEXT, "listen"); *(void **)(&real_pause) = dlsym(RTLD_NEXT, "pause"); diff --git a/tests/periph_random/main.c b/tests/periph_random/main.c index 20a49d1e5b..5e211d5402 100644 --- a/tests/periph_random/main.c +++ b/tests/periph_random/main.c @@ -20,16 +20,15 @@ #include #include -#include -#include -#include + +#include "vtimer.h" +#include "periph/random.h" #define LIMIT (20U) int main(void) { char buf[LIMIT]; - int count = 0; puts("\nRandom number generator low-level driver test\n"); printf("This test will print from 1 to %i random bytes about every second\n\n", LIMIT); @@ -42,9 +41,9 @@ int main(void) memset(buf, 0, sizeof(buf)); /* create random numbers */ - for (int i = 1; i <= LIMIT; i++) { + for (unsigned i = 1; i <= LIMIT; i++) { printf("generating %i random byte(s)\n", i); - count = random_read(buf, i); + unsigned count = random_read(buf, i); if (count != i) { printf("Error generating random bytes, got %i instead of %i", count, i); @@ -52,8 +51,8 @@ int main(void) } printf("Got:"); - for (int j = 0; j < i; j++) { - printf(" 0x%02x", buf[j]); + for (unsigned j = 0; j < i; j++) { + printf(" 0x%02x", (unsigned char)buf[j]); } printf("\n"); }