forked from evinrude/pico-rng
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpico_rng_test.py
executable file
·77 lines (65 loc) · 2.2 KB
/
pico_rng_test.py
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
#!/usr/bin/env python3
import usb.core
import usb.util
import os
import random
import time
import argparse
import sys
# Parser stuff
parser = argparse.ArgumentParser(description="Raspberry Pi Pico Random Number Generator Test Tool")
parser.add_argument("--performance", action="store_true", help="Performance test the RNG.")
parser.add_argument("--endless", action="store_true", help="Outputs random bytes endlessly.")
parser.add_argument("--size", default="100", help="Number of bytes to output.")
parser.add_argument("--vid", default="0000", help="VID.")
parser.add_argument("--pid", default="0004", help="PID.")
args = parser.parse_args()
# If this is set, then the /dev/pico_rng file exists
rng_chardev = None
if os.path.exists("/dev/pico_rng"):
rng_chardev = open("/dev/pico_rng", "rb")
# File does not exist, test with usb.core
if not rng_chardev:
# Get the device
rng = usb.core.find(idVendor=int(args.vid, base=16), idProduct=int(args.pid, base=16))
assert rng is not None
# Get the configuration of the device
cfg = rng.get_active_configuration()
# Get the only interface of our device
intf = cfg.interfaces()[0]
# Get the endpoint
endpt = intf.endpoints()[0]
# Time tracking for bits/s
count = 0
start_time = (int(time.time()) - 1)
def get_data():
return rng_chardev.read(64) if rng_chardev else endpt.read(64, 500)
def get_and_print():
data = get_data()
sys.stdout.buffer.write(data)
if args.performance:
while True:
try:
from_device = get_data()
count = count+1
#print(from_device, end="")
print("Speed: {0:.2f} KB/s".format((int((count * 64) / (int(time.time()) - start_time))) / 1024 ), end='\r')
except KeyboardInterrupt:
exit(0)
elif args.endless:
while True:
try:
get_and_print()
except KeyboardInterrupt:
exit(0)
except BrokenPipeError:
exit(0)
elif args.size:
size = int(float(args.size))
for i in range(0,size,64):
data = get_data()
sys.stdout.buffer.write(data[:min(size-i,len(data))])
else:
from_device = get_data()
print(from_device)
sys.stdout.buffer.write(bytearray(from_device))