From eceb65a96a41c3cdcdc1b7273e03722b3c879ec8 Mon Sep 17 00:00:00 2001 From: ea Date: Sun, 28 Jan 2018 14:36:40 -0600 Subject: [PATCH] Added command to export random integers as per suggestion in #63 Adds a `-R n/ --randint n` command which returns a sequence of n 16bit integers as generated by `true_rand()`. --- bin/goodwatch.py | 10 +++++++++- firmware/monitor.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/bin/goodwatch.py b/bin/goodwatch.py index 813cb67..4fecdf0 100755 --- a/bin/goodwatch.py +++ b/bin/goodwatch.py @@ -259,6 +259,11 @@ class GoodWatch: def dmesg(self): """Returns the DMESG buffer.""" return self.transact("\x04"); + def randint(self,n): + """Returns n random 16bit integer. """ + import struct + r = struct.unpack("<"+"H"*n,self.transact("\x05\x00"+chr16(n))); + return "%04x "*n%r def radioonoff(self,on=1): """Turns the radio on or off.""" return self.transact("\x10"+chr(on)); @@ -301,7 +306,8 @@ if __name__=='__main__': help='Write a string the LCD.'); parser.add_argument('-D','--dmesg', help='Prints the dmesg.',action='count'); - + parser.add_argument('-R','--randint', + help='Get RANDINT random 16bit integers.'); parser.add_argument('-b','--beacon', help='Transmits a beacon.'); parser.add_argument('-B','--beaconsniff', @@ -330,6 +336,8 @@ if __name__=='__main__': if args.dmesg>0: print goodwatch.dmesg(); + if args.randint != None: + print goodwatch.randint(int(args.randint)); if args.beacon!=None: print "Turning radio on."; diff --git a/firmware/monitor.c b/firmware/monitor.c index 233d73f..513c0d5 100644 --- a/firmware/monitor.c +++ b/firmware/monitor.c @@ -12,6 +12,7 @@ #include #include "api.h" +#include "rng.h" enum { SETTURBOMODE = 0x00, @@ -19,6 +20,7 @@ enum { POKE = 0x02, LCDSTRING = 0x03, DMESG = 0x04, + RANDINT = 0x05, RADIOONOFF = 0x10, RADIOCONFIG = 0x11, @@ -48,6 +50,30 @@ static void send_dmesg(){ uart_tx(0x00); } +//! Local command to generate and send n random integers. +static void send_randint(uint16_t n){ + + uint16_t i; + uint16_t len = n*2; + uint16_t rints[n]; // are var len arrays ok here? + + for(i = 0; i < n; i++){ //pre-generate enough ints + rints[i] = true_rand(); + } + //Start the frame + uart_tx(0x00); + uart_tx(0x80); + //Length + uart_tx(len&0xFF); + uart_tx(len>>8); + //Send integers byte by byte + for(i=0;i