Skip to content

Commit

Permalink
unittest enhancements. my own version of a fix for the bytes[] problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
atlas0fd00m committed Apr 3, 2019
1 parent b90c174 commit f47a554
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 36 deletions.
28 changes: 19 additions & 9 deletions rflib/bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@
from builtins import range
from builtins import bytes
from past.utils import old_div
import sys
import struct

fmtsLSB = [None, "B", "<H", "<I", "<I", "<Q", "<Q", "<Q", "<Q"]
fmtsMSB = [None, "B", ">H", ">I", ">I", ">Q", ">Q", ">Q", ">Q"]
sizes = [ 0, 1, 2, 4, 4, 8, 8, 8, 8]
masks = [ (1<<(8*i))-1 for i in range(9) ]

PYVER = int(sys.version[0])
def correctbytes(val):
global PYVER

if PYVER == 2:
return chr(val)

return bytes([val])

def wtfo(string):
outstr = []
bitlen = len(outstr) * 8
Expand Down Expand Up @@ -43,7 +53,7 @@ def strBitReverse(string):
# convert back from MSB number to string
out = []
for x in range(len(string)):
out.append(bytes([rnum&0xff]))
out.append(correctbytes([rnum&0xff]))
rnum >>= 8
out.reverse()
print(''.join(out).encode('hex'))
Expand Down Expand Up @@ -388,7 +398,7 @@ def bitSectString(string, startbit, endbit):
mask = ~ ( (1<<diff) - 1 )
byte &= mask

s += bytes([byte])
s += correctbytes([byte])

ent = old_div((min(entropy)+1.0), (max(entropy)+1))
#print "entropy: %f" % ent
Expand Down Expand Up @@ -481,7 +491,7 @@ def invertBits(data):
off = 0

if ldata&1:
output.append( bytes([ ord( data[0] ) ^ 0xff]) )
output.append( correctbytes( ord( data[0] ) ^ 0xff) )
off = 1

if ldata&2:
Expand Down Expand Up @@ -546,12 +556,12 @@ def diff_manchester_decode(data, align=False):

last = (bit0 << 1) | bit1
if (bidx & 1):
out.append(bytes([obyte]))
out.append(correctbytes(obyte))
obyte = 0

if not (bidx & 1):
obyte << 4 # pad 0's on end
out.append(bytes([obyte]))
out.append(correctbytes(obyte))
return ''.join(out)


Expand All @@ -573,12 +583,12 @@ def biphase_mark_coding_encode(data):
last = bit
if bidx & 1:
print("%d - write" % bidx)
out.append(bytes([obyte]))
out.append(correctbytes(obyte))
else:
print("%d - skip" % bidx)
if not (bidx & 1):
print("%d - write" % bidx)
out.append(bytes([obyte]))
out.append(correctbytes(obyte))

return ''.join(out)

Expand All @@ -602,12 +612,12 @@ def manchester_decode(data, hilo=1):

last = bit
if (bidx & 1):
out.append(bytes([obyte]))
out.append(correctbytes(obyte))
obyte = 0

if not (bidx & 1):
obyte << 4 # pad 0's on end
out.append(bytes([obyte]))
out.append(correctbytes(obyte))
return ''.join(out)

def manchester_encode(data, hilo=1):
Expand Down
3 changes: 2 additions & 1 deletion rflib/ccspecan.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import numpy
import threading
import rflib
from .bits import correctbytes
# import cPickle in Python 2 instead of pickle in Python 3
if sys.version_info < (3,):
import cPickle as pickle
Expand Down Expand Up @@ -415,7 +416,7 @@ def keyPressEvent(self, event):

# anything else is alphanumeric
try:
key= bytes([event.key()]).upper()
key= correctbytes(event.key()).upper()
event.accept()
except:
print('Unknown key pressed: 0x%x' % event.key())
Expand Down
13 changes: 7 additions & 6 deletions rflib/chipcon_nic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import threading
#from .chipcondefs import *
from .chipcon_usb import *
from .bits import correctbytes

# band limits in Hz
FREQ_MIN_300 = 281000000
Expand Down Expand Up @@ -443,14 +444,14 @@ def setRFRegister(self, regaddr, value, suppress=False):
if 'suppress' the radio state (RX/TX/IDLE) is not modified
'''
if suppress:
self.poke(regaddr, bytes([value]))
self.poke(regaddr, correctbytes(value))
return

marcstate = self.radiocfg.marcstate
if marcstate != MARC_STATE_IDLE:
self.strobeModeIDLE()

self.poke(regaddr, bytes([value]))
self.poke(regaddr, correctbytes(value))

self.strobeModeReturn(marcstate)
#if (marcstate == MARC_STATE_RX):
Expand Down Expand Up @@ -1303,7 +1304,7 @@ def getAmpMode(self):
return self.send(APP_NIC, NIC_GET_AMP_MODE, "")

def setPktAddr(self, addr):
return self.poke(ADDR, bytes([addr]))
return self.poke(ADDR, correctbytes(addr))

def getPktAddr(self):
return self.peek(ADDR)
Expand Down Expand Up @@ -2035,9 +2036,9 @@ def setMACperiod(self, dwell_ms, mhz=24):
t2ctl = (ord(self.peek(X_T2CTL)) & 0xfc) | (tipidx)
clkcon = (ord(self.peek(X_CLKCON)) & 0xc7) | (tickidx<<3)

self.poke(X_T2PR, bytes([PR]))
self.poke(X_T2CTL, bytes([t2ctl]))
self.poke(X_CLKCON, bytes([clkcon]))
self.poke(X_T2PR, correctbytes(PR))
self.poke(X_T2CTL, correctbytes(t2ctl))
self.poke(X_CLKCON, correctbytes(clkcon))

def _setMACmode(self, _mode):
'''
Expand Down
3 changes: 2 additions & 1 deletion rflib/chipcon_usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .chipcondefs import *
from .rflib_defs import *
from .rflib_version import *
from .bits import correctbytes

if os.name == 'nt':
import msvcrt
Expand Down Expand Up @@ -957,7 +958,7 @@ def unittest(self, mhz=24):
print(repr(self.peek(0xf000, 400)))

print("\nTesting USB poke/peek")
data = "".join([bytes([c]) for c in range(120)])
data = "".join([correctbytes(c) for c in range(120)])
where = 0xf300
self.poke(where, data)
ndata = self.peek(where, len(data))
Expand Down
7 changes: 4 additions & 3 deletions rflib/intelhex.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from bisect import bisect_right
import os
import sys
from .bits import correctbytes


# the Python 2 integer types int and long have been unified in Python 3
Expand Down Expand Up @@ -490,7 +491,7 @@ def write_hex_file(self, f, write_start_addr=True):
# timeit shows that using hexstr.translate(table)
# is faster than hexstr.upper():
# 0.452ms vs. 0.652ms (translate vs. upper)
table = ''.join(bytes([i]).upper() for i in range(256))
table = ''.join(correctbytes(i).upper() for i in range(256))

# start address record if any
if self.start_addr and write_start_addr:
Expand Down Expand Up @@ -708,7 +709,7 @@ def dump(self, tofile=None):
if x is not None:
tofile.write(' %02X' % x)
if 32 <= x < 128:
s.append(bytes([x]))
s.append(correctbytes(x))
else:
s.append('.')
else:
Expand Down Expand Up @@ -1010,7 +1011,7 @@ def _get_file_and_addr_range(s, _support_drive_letter=None):
_support_drive_letter = (os.name == 'nt')
drive = ''
if _support_drive_letter:
if s[1:2] == ':' and s[0].upper() in ''.join([bytes([i]) for i in range(ord('A'), ord('Z')+1)]):
if s[1:2] == ':' and s[0].upper() in ''.join([correctbytes(i) for i in range(ord('A'), ord('Z')+1)]):
drive = s[:2]
s = s[2:]
parts = s.split(':')
Expand Down
Binary file removed tests/.test_basics.py.un~
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ def test_importing(self):
import rflib
devs = rflib.getRfCatDevices()
self.assertEquals(type(devs), list, "rflib.getRfCatDevices() doesn't return a list!: %r" % devs)
import rflib.chipcon_nic
import rflib.chipcon_usb
import rflib.chipcondefs
import rflib.bits
import rflib.ccspecan
import rflib.intelhex
import rflib.rflib_defs

16 changes: 0 additions & 16 deletions tests/test_basics.py~

This file was deleted.

0 comments on commit f47a554

Please sign in to comment.