2015-02-12 18:06:34 +01:00
|
|
|
/*
|
2014-01-20 11:08:42 +01:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2015-07-30 10:20:57 +02:00
|
|
|
* 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
2014-01-20 11:08:42 +01:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* 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.
|
2015-02-12 18:06:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup sys_shell_commands
|
2014-01-20 11:08:42 +01:00
|
|
|
* @{
|
2015-02-12 18:06:34 +01:00
|
|
|
*
|
|
|
|
* @file
|
2015-10-12 16:07:54 +02:00
|
|
|
* @brief Shell commands for random generators
|
2015-02-12 18:06:34 +01:00
|
|
|
*
|
|
|
|
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
2015-07-30 10:20:57 +02:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2014-01-20 11:08:42 +01:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2022-05-31 23:27:27 +02:00
|
|
|
#include "random.h"
|
|
|
|
#include "shell.h"
|
|
|
|
|
2022-09-16 12:38:19 +02:00
|
|
|
#include "ztimer.h"
|
2015-07-30 10:20:57 +02:00
|
|
|
|
2022-06-08 08:51:38 +02:00
|
|
|
static int _random_init(int argc, char **argv)
|
2014-01-20 11:08:42 +01:00
|
|
|
{
|
|
|
|
int initval;
|
|
|
|
|
2014-02-21 19:10:24 +01:00
|
|
|
if (argc == 1) {
|
2022-09-16 12:38:19 +02:00
|
|
|
if (IS_USED(MODULE_ZTIMER_USEC)) {
|
|
|
|
initval = ztimer_now(ZTIMER_USEC);
|
|
|
|
printf("PRNG initialized to current time: %d\n", initval);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
puts("xtimer module not compiled in, can't initialize by time.\n"
|
|
|
|
"Please provide a seed.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-02-16 20:50:48 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-02-21 19:10:24 +01:00
|
|
|
initval = atoi(argv[1]);
|
|
|
|
printf("PRNG initialized given value: %d\n", initval);
|
2014-01-20 11:08:42 +01:00
|
|
|
}
|
|
|
|
|
2016-02-14 20:04:10 +01:00
|
|
|
random_init(initval);
|
2015-03-20 08:51:45 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-01-20 11:08:42 +01:00
|
|
|
}
|
|
|
|
|
2022-05-31 23:27:27 +02:00
|
|
|
SHELL_COMMAND(random_init, "initializes the PRNG", _random_init);
|
|
|
|
|
2022-06-08 08:51:38 +02:00
|
|
|
static int _random_get(int argc, char **argv)
|
2014-01-20 11:08:42 +01:00
|
|
|
{
|
2014-02-21 19:10:24 +01:00
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
2014-01-20 11:08:42 +01:00
|
|
|
|
2016-02-14 20:04:10 +01:00
|
|
|
printf("%" PRIu32 "\n", random_uint32());
|
2015-03-20 08:51:45 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-01-20 11:08:42 +01:00
|
|
|
}
|
2022-05-31 23:27:27 +02:00
|
|
|
|
|
|
|
SHELL_COMMAND(random_get, "returns 32 bit of pseudo randomness", _random_get);
|