2013-10-20 19:37:56 +02:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/random/random_main.c
|
2013-10-20 19:37:56 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2013-10-20 19:37:56 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-10-20 19:37:56 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-10-20 19:37:56 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-03-24 17:43:45 +01:00
|
|
|
#include <string.h>
|
2013-10-20 19:37:56 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2013-10-20 19:37:56 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2023-02-04 14:56:06 +01:00
|
|
|
|
2013-10-20 19:37:56 +02:00
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_MAXSAMPLES
|
|
|
|
# define CONFIG_EXAMPLES_MAXSAMPLES 64
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_NSAMPLES
|
|
|
|
# define CONFIG_EXAMPLES_NSAMPLES 8
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_EXAMPLES_NSAMPLES > CONFIG_EXAMPLES_MAXSAMPLES
|
|
|
|
# warning CONFIG_EXAMPLES_NSAMPLES > CONFIG_EXAMPLES_MAXSAMPLES
|
|
|
|
# undef CONFIG_EXAMPLES_NSAMPLES
|
|
|
|
# define CONFIG_EXAMPLES_NSAMPLES CONFIG_EXAMPLES_MAXSAMPLES
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* rand_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2013-10-20 19:37:56 +02:00
|
|
|
{
|
|
|
|
uint32_t buffer[CONFIG_EXAMPLES_MAXSAMPLES];
|
|
|
|
int nsamples = CONFIG_EXAMPLES_NSAMPLES;
|
|
|
|
ssize_t nread;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* One argument is possible... the number of samples to collect */
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
2017-04-26 18:32:20 +02:00
|
|
|
nsamples = atoi(argv[1]);
|
2013-10-20 19:37:56 +02:00
|
|
|
}
|
|
|
|
|
2016-03-24 17:43:45 +01:00
|
|
|
/* Clip the number of samples to the configured buffer size */
|
2013-10-20 19:37:56 +02:00
|
|
|
|
2017-04-26 18:32:20 +02:00
|
|
|
if (nsamples < 0)
|
|
|
|
{
|
|
|
|
nsamples = 0;
|
|
|
|
}
|
|
|
|
else if (nsamples > CONFIG_EXAMPLES_MAXSAMPLES)
|
2013-10-20 19:37:56 +02:00
|
|
|
{
|
|
|
|
nsamples = CONFIG_EXAMPLES_MAXSAMPLES;
|
|
|
|
}
|
|
|
|
|
2023-02-04 14:56:06 +01:00
|
|
|
/* fill buffer to make it super-clear as to what has and has not been
|
|
|
|
* written
|
|
|
|
*/
|
2016-03-24 17:43:45 +01:00
|
|
|
|
2016-03-24 18:22:24 +01:00
|
|
|
memset(buffer, 0xcc, sizeof(buffer));
|
2016-03-24 17:43:45 +01:00
|
|
|
|
2013-10-20 19:37:56 +02:00
|
|
|
/* Open /dev/random */
|
|
|
|
|
|
|
|
fd = open("/dev/random", O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: Failed to open /dev/random: %d\n", errcode);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the requested number of samples */
|
|
|
|
|
|
|
|
printf("Reading %d random numbers\n", nsamples);
|
2016-03-24 17:43:45 +01:00
|
|
|
fflush(stdout);
|
2013-10-20 19:37:56 +02:00
|
|
|
nread = read(fd, buffer, nsamples * sizeof(uint32_t));
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2017-04-26 18:32:20 +02:00
|
|
|
fprintf(stderr, "ERROR: Read from /dev/random failed: %d\n", errcode);
|
2020-01-02 13:09:50 +01:00
|
|
|
close(fd);
|
2013-10-20 19:37:56 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2016-03-24 18:22:24 +01:00
|
|
|
|
2016-03-24 17:43:45 +01:00
|
|
|
if (nread != nsamples * sizeof(uint32_t))
|
|
|
|
{
|
2023-02-04 14:56:06 +01:00
|
|
|
fprintf(stderr, "ERROR: Read from /dev/random only produced %zd "
|
|
|
|
"bytes\n", nread);
|
2020-01-02 13:09:50 +01:00
|
|
|
close(fd);
|
2016-03-24 17:43:45 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-10-20 19:37:56 +02:00
|
|
|
|
|
|
|
/* Dump the sample buffer */
|
|
|
|
|
2023-02-04 14:56:06 +01:00
|
|
|
lib_dumpbuffer("Random values", (FAR const uint8_t *)buffer, nread);
|
2020-01-02 13:09:50 +01:00
|
|
|
close(fd);
|
2013-10-20 19:37:56 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|