Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating the flash script to python3 #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions prog/usb-flasher/usb-flash.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
'''
Copyright (C) 2016 Bastille Networks

Expand All @@ -25,32 +25,32 @@
# Check pyusb dependency
try:
from usb import core as _usb_core
except ImportError, ex:
print '''
except ImportError as ex:
print ('''
------------------------------------------
| PyUSB was not found or is out of date. |
------------------------------------------

Please update PyUSB using pip:

sudo pip install -U -I pip && sudo pip install -U -I pyusb
'''
''')
sys.exit(1)

# USB timeout sufficiently long for operating in a VM
usb_timeout = 2500

# Verify that we received a command line argument
if len(sys.argv) < 2:
print 'Usage: ./usb-flash.py path-to-firmware.bin'
print ('Usage: ./usb-flash.py path-to-firmware.bin')
quit()

# Read in the firmware
with open(sys.argv[1], 'rb') as f:
data = f.read()

# Zero pad the data to a multiple of 512 bytes
data += '\000' * (512 - len(data) % 512)
data += b'\000' * (512 - len(data) % 512)

# Find an attached device running CrazyRadio or RFStorm firmware
logging.info("Looking for a compatible device that can jump to the Nordic bootloader")
Expand Down Expand Up @@ -93,7 +93,7 @@

# Write the data, one page at a time
logging.info("Writing image to flash")
page_count = len(data) / 512
page_count = int(len(data) / 512)
for page in range(page_count):

# Tell the bootloader that we are going to write a page
Expand Down
2 changes: 1 addition & 1 deletion tools/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


import logging, argparse
from nrf24 import *
from lib.nrf24 import *

channels = []
args = None
Expand Down
10 changes: 5 additions & 5 deletions tools/lib/nrf24.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
# Check pyusb dependency
try:
from usb import core as _usb_core
except ImportError, ex:
print '''
except ImportError as ex:
print ('''
------------------------------------------
| PyUSB was not found or is out of date. |
------------------------------------------

Please update PyUSB using pip:

sudo pip install -U -I pip && sudo pip install -U -I pyusb
'''
''')
sys.exit(1)

# USB commands
Expand Down Expand Up @@ -65,14 +65,14 @@ def __init__(self, index=0):
try:
self.dongle = list(usb.core.find(idVendor=0x1915, idProduct=0x0102, find_all=True))[index]
self.dongle.set_configuration()
except usb.core.USBError, ex:
except usb.core.USBError as ex:
raise ex
except:
raise Exception('Cannot find USB dongle.')

# Put the radio in pseudo-promiscuous mode
def enter_promiscuous_mode(self, prefix=[]):
self.send_usb_command(ENTER_PROMISCUOUS_MODE, [len(prefix)]+map(ord, prefix))
self.send_usb_command(ENTER_PROMISCUOUS_MODE, [len(prefix)]+list(set(map(ord, prefix))))
self.dongle.read(0x81, 64, timeout=nrf24.usb_timeout)
if len(prefix) > 0:
logging.debug('Entered promiscuous mode with address prefix {0}'.
Expand Down
2 changes: 1 addition & 1 deletion tools/nrf24-continuous-tone-test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
'''
Copyright (C) 2016 Bastille Networks

Expand Down
4 changes: 2 additions & 2 deletions tools/nrf24-network-mapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
'''
Copyright (C) 2016 Bastille Networks

Expand All @@ -24,7 +24,7 @@
common.init_args('./nrf24-network-mapper.py')
common.parser.add_argument('-a', '--address', type=str, help='Known address', required=True)
common.parser.add_argument('-k', '--ack_timeout', type=int, help='ACK timeout in microseconds, accepts [250,4000], step 250', default=500)
common.parser.add_argument('-r', '--retries', type=int, help='Auto retry limit, accepts [0,15]', default='5', choices=xrange(0, 16), metavar='RETRIES')
common.parser.add_argument('-r', '--retries', type=int, help='Auto retry limit, accepts [0,15]', default='5', choices=range(0, 16), metavar='RETRIES')
common.parser.add_argument('-p', '--ping_payload', type=str, help='Ping payload, ex 0F:0F:0F:0F', default='0F:0F:0F:0F', metavar='PING_PAYLOAD')
common.parse_and_init()

Expand Down
4 changes: 2 additions & 2 deletions tools/nrf24-scanner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
'''
Copyright (C) 2016 Bastille Networks

Expand Down Expand Up @@ -27,7 +27,7 @@
common.parse_and_init()

# Parse the prefix addresses
prefix_address = common.args.prefix.replace(':', '').decode('hex')
prefix_address = common.args.prefix.replace(':', '').encode().decode('hex')
if len(prefix_address) > 5:
raise Exception('Invalid prefix address: {0}'.format(args.address))

Expand Down
4 changes: 2 additions & 2 deletions tools/nrf24-sniffer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
'''
Copyright (C) 2016 Bastille Networks

Expand All @@ -25,7 +25,7 @@
common.parser.add_argument('-a', '--address', type=str, help='Address to sniff, following as it changes channels', required=True)
common.parser.add_argument('-t', '--timeout', type=float, help='Channel timeout, in milliseconds', default=100)
common.parser.add_argument('-k', '--ack_timeout', type=int, help='ACK timeout in microseconds, accepts [250,4000], step 250', default=250)
common.parser.add_argument('-r', '--retries', type=int, help='Auto retry limit, accepts [0,15]', default=1, choices=xrange(0, 16), metavar='RETRIES')
common.parser.add_argument('-r', '--retries', type=int, help='Auto retry limit, accepts [0,15]', default=1, choices=range(0, 16), metavar='RETRIES')
common.parser.add_argument('-p', '--ping_payload', type=str, help='Ping payload, ex 0F:0F:0F:0F', default='0F:0F:0F:0F', metavar='PING_PAYLOAD')
common.parse_and_init()

Expand Down