-
Notifications
You must be signed in to change notification settings - Fork 0
/
ospupdfw
executable file
·181 lines (145 loc) · 4.34 KB
/
ospupdfw
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
#!/usr/bin/python
'''
SharkRF openSpot, download and update firmware
Author: Veijo Arponen OH3NFC <[email protected]>
'''
import argparse
import os
import os.path
import sys
import urllib
from getpass import getuser
from time import sleep
import serial
import time
CRLF = '\r\n'
try:
from msvcrt import getch
except ImportError:
def getch():
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def open_serial(device):
"Open serial port"
ser = serial.Serial()
ser.port = device
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.timeout = None
ser.xonxoff = False
ser.rtscts = False
ser.dsrdtr = False
ser.writeTimeout = 10
try:
ser.open()
time.sleep(1)
ser.flushInput()
except Exception, e:
print "error open serial port: " + str(e)
exit()
return ser
def ask_info(ser):
"Send 'inf' to the openSPOT"
ser.write("inf" + CRLF)
time.sleep(1)
info = "\nSharkRF openSPOT device information (" + ser.port + "):\n"
while ser.inWaiting() > 0:
info += ser.read(1)
print info
return
def reboot_to_bootloader(serial):
"Send 'rbb' to the openSPOT"
print "Rebooting SharkRF openSPOT device to bootloader"
ser.write("rbb" + CRLF)
return
def download(url, filename):
"Download the openSPOT firmware file"
if not os.path.isfile(filename):
print "Downloading the firmware:\n " + url
# TODO: Check status, 404 Not Found
resp = urllib.urlretrieve(url, filename)
else:
print "Using already downloaded firmware:\n " + filename
return
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Update SharkRF openSPOT firmware',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--version', action='version',
version='%(prog)s v1.1.1')
parser.add_argument('-d', '--device', nargs='?', dest='device',
default='/dev/ttyACM0',
help='serial device port where the openSPOT is connected to')
parser.add_argument('-i','--info', dest='info', action='store_true',
help='print openSPOT info and exit', )
parser.add_argument('-t','--type', dest='type',
help='firmware type', choices=['beta', 'stable'], default='stable')
parser.add_argument('-fw','--firmware', dest='fw_version',
help='firmware version to download and install', default='0142')
parser.add_argument('-hw','--hardware', dest='hw_version',
help='hardware version (see the sticker at bottom of your openSPOT)',
default='1.1')
args = parser.parse_args()
# Print device info and exit
if args.info == True:
ser = open_serial(args.device)
ask_info(ser)
sys.exit(0)
# Download the firmware file
# Examples of download urls:
# https://x.sharkrf.com/fw/osp/srf-osp-1.1-0127.bin
# https://x.sharkrf.com/fw-beta/osp/srf-osp-1.1-0130.bin
host = 'sharkrf-x.fra1.cdn.digitaloceanspaces.com'
if args.type == 'stable':
directory = 'fw'
else:
directory = 'fw-beta'
filename = 'srf-osp-' + args.hw_version +'-' + args.fw_version + '.bin'
url = 'https://' + host + '/' + directory + '/osp/' + filename
download(url, filename)
# Open the serial port and read device information
print "Opening serial device " + args.device
ser = open_serial(args.device)
# Print device info
ask_info(ser)
# Ask user confirmation for the firmware update
print "\nDo you want to continue (Y/N)?"
while True:
char = getch()
if char.lower() in ("y", "n"):
print char
break
# Stop updating process
if char == 'n':
print "Firmware update cancelled!"
sys.exit(2)
# Reboot to bootloader
reboot_to_bootloader(ser)
ser.close()
# Wait for the destination folder to show up
user = getuser()
dest = '/media/' + user + '/BOOTLOADER/'
print 'Waiting the bootloader directory ' + dest + ' to show up'
retrycnt = 0
while not os.path.exists(dest):
sleep(5)
retrycnt = retrycnt + 1
if retrycnt > 5:
print 'Error, no access to the bootloader directory ' + dest + ', exiting!'
sys.exit(-1)
sleep(1)
# Copy the firmware file to the destination folder
print "Copying " + filename + " to " + dest
print "Please, wait a couple of minutes"
os.system('cp ' + filename + ' ' + dest)
os.system('sync')
print "\nPlease, stand by one more minute, the openSPOT is starting up!"