1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
* Copyright (c) 2011-2023, Shanghai Real-Thread Electronic Technology Co.,Ltd
*
* Change Logs:
* Date Author Notes
* 2020-12-18 quanzhao the first version
*/
#include <time.h>
#include <string.h>
#include <rtthread.h>
static struct rt_device random_dev;
static unsigned long seed;
static rt_uint16_t calc_random(void)
{
seed = 214013L * seed + 2531011L;
return (seed >> 16) & 0x7FFF; /* return bits 16~30 */
}
static rt_ssize_t random_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
rt_uint16_t rand;
ssize_t ret = 0;
while (size >= sizeof(rand))
{
/* update rand */
rand = calc_random();
*(rt_uint16_t *)buffer = rand;
buffer = (char *)buffer + sizeof(rand);
ret += sizeof(rand);
size -= sizeof(rand);
}
if (size)
{
rand = calc_random();
memcpy(buffer, &rand, size);
ret += size;
}
return ret;
}
static rt_ssize_t random_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
ssize_t ret = sizeof(seed) < size ? sizeof(seed) : size;
rt_memcpy(&seed, buffer, ret);
return ret;
}
static rt_err_t random_control(rt_device_t dev, int cmd, void *args)
{
return RT_EOK;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops random_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
random_read,
random_write,
random_control
};
#endif
int random_device_init(void)
{
static rt_bool_t init_ok = RT_FALSE;
if (init_ok)
{
return 0;
}
RT_ASSERT(!rt_device_find("random"));
random_dev.type = RT_Device_Class_Miscellaneous;
#ifdef RT_USING_DEVICE_OPS
random_dev.ops = &random_ops;
#else
random_dev.init = RT_NULL;
random_dev.open = RT_NULL;
random_dev.close = RT_NULL;
random_dev.read = random_read;
random_dev.write = random_write;
random_dev.control = random_control;
#endif
/* no private */
random_dev.user_data = RT_NULL;
rt_device_register(&random_dev, "random", RT_DEVICE_FLAG_RDWR);
init_ok = RT_TRUE;
return 0;
}
INIT_DEVICE_EXPORT(random_device_init);
static struct rt_device urandom_dev;
static unsigned long useed;
static rt_uint16_t calc_urandom(void)
{
useed = 214013L * useed + 2531011L;
return (useed >> 16) & 0x7FFF; /* return bits 16~30 */
}
static rt_ssize_t random_uread(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
rt_uint16_t rand;
ssize_t ret = 0;
while (size >= sizeof(rand))
{
/* update rand */
rand = calc_urandom();
*(rt_uint16_t *)buffer = rand;
buffer = (char *)buffer + sizeof(rand);
ret += sizeof(rand);
size -= sizeof(rand);
}
if (size)
{
rand = calc_urandom();
memcpy(buffer, &rand, size);
ret += size;
}
return ret;
}
static rt_ssize_t random_uwrite(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
ssize_t ret = sizeof(useed) < size ? sizeof(useed) : size;
rt_memcpy(&useed, buffer, ret);
return ret;
}
static rt_err_t random_ucontrol(rt_device_t dev, int cmd, void *args)
{
return RT_EOK;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops urandom_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
random_uread,
random_uwrite,
random_ucontrol
};
#endif
int urandom_device_init(void)
{
static rt_bool_t init_ok = RT_FALSE;
if (init_ok)
{
return 0;
}
RT_ASSERT(!rt_device_find("urandom"));
urandom_dev.type = RT_Device_Class_Miscellaneous;
#ifdef RT_USING_DEVICE_OPS
urandom_dev.ops = &urandom_ops;
#else
urandom_dev.init = RT_NULL;
urandom_dev.open = RT_NULL;
urandom_dev.close = RT_NULL;
urandom_dev.read = random_uread;
urandom_dev.write = random_uwrite;
urandom_dev.control = random_ucontrol;
#endif
/* no private */
urandom_dev.user_data = RT_NULL;
rt_device_register(&urandom_dev, "urandom", RT_DEVICE_FLAG_RDWR);
init_ok = RT_TRUE;
return 0;
}
INIT_DEVICE_EXPORT(urandom_device_init);
|