-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
hfplus_fw.py
executable file
·86 lines (66 loc) · 2.26 KB
/
hfplus_fw.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
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
#pylint: disable=line-too-long,anomalous-backslash-in-string
"""
Program to print the firmware version running on a Airspy HF+ device.
"""
import sys
import argparse
import usb.core
import usb.util
PY3 = sys.version_info.major == 3
def find_device():
"""
Detect Airspy HF+ Discovery.
$ lsusb
Bus 003 Device 011: ID 03eb:800c Atmel Corp. Airspy HF+
"""
msg = "ATTENTION: The device is in 'bootloader' mode. Use the 'hfplus_reboot.py' script to switch the device to 'normal' mode."
devs = usb.core.find(find_all=True)
for dev in devs:
if dev.idVendor == 0x03eb and dev.idProduct == 0x800c:
return dev
for dev in devs:
if dev.idVendor == 0x03eb and dev.idProduct == 0x6124:
print(msg)
sys.exit(0)
return None
def main():
"""
Driver code
"""
if not PY3:
print("This script requires Python 3.x to run!")
sys.exit(1)
parser = argparse.ArgumentParser()
parser.add_argument('-d', action='store_true', dest='debug_enabled', help="run in debugging mode")
args = parser.parse_args()
# find device
dev = find_device()
if dev is None:
raise ValueError('Device not found!')
# work with the device
cfg = dev.get_active_configuration()
if args.debug_enabled:
print(cfg)
dev.set_configuration(configuration=1)
usb.util.claim_interface(dev, interface=0)
dev.set_interface_altsetting(interface=0, alternate_setting=1)
dev.clear_halt(ep=0x81)
# Function prototype -> libusb_control_transfer (libusb_device_handle
# *dev_handle, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue,
# uint16_t wIndex, unsigned char *data, uint16_t wLength, unsigned int
# timeout)
# Actual call -> libusb_control_transfer(dev, 0xc0, 9, 0, 0, buffer, 0x3f, 0);
# ctrl_transfer(bmRequestType, bRequest, wValue=0, wIndex=0, data_or_wLength=None, timeout=None)
msg = dev.ctrl_transfer(0xc0, 9, 0, 0, 0x3f).tobytes()
omsg = []
for element in msg:
omsg.append(chr(element))
if element == 0x00:
break
firmware_version = ''.join(omsg)
print(firmware_version)
# cleanup
usb.util.dispose_resources(dev)
if __name__ == "__main__":
main()