1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-25 14:33:52 +01:00

Merge pull request #2041 from LudwigOrtmann/native-random

native: peripheral random interface
This commit is contained in:
Ludwig Ortmann 2014-11-19 06:25:39 -08:00
commit 6eaaac6372
6 changed files with 121 additions and 8 deletions

View File

@ -1 +1,2 @@
FEATURES_PROVIDED += transceiver periph_cpuid cpp
FEATURES_PROVIDED += periph_random

View File

@ -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);

View File

@ -0,0 +1,35 @@
/**
* Native CPU peripheral configuration
*
* Copyright (C) 2014 Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
*
* 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 <ludwig.ortmann@fu-berlin.de>
*/
#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 */
/** @} */

View File

@ -0,0 +1,72 @@
/*
* Copyright (C) 2014 Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
*
* 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 <ludwig.ortmann@fu-berlin.de>
*/
#include <string.h>
#include <stdlib.h>
#include <err.h>
#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;
}
/**
* @}
*/

View File

@ -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");

View File

@ -20,16 +20,15 @@
#include <stdio.h>
#include <string.h>
#include <vtimer.h>
#include <periph/random.h>
#include <periph_conf.h>
#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");
}