forked from ExpressLRS/ExpressLRS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_via_esp8266_backpack.py
More file actions
127 lines (108 loc) · 4.75 KB
/
upload_via_esp8266_backpack.py
File metadata and controls
127 lines (108 loc) · 4.75 KB
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
import subprocess, os
from elrs_helpers import ElrsUploadResult
def process_http_result(output_json_file: str) -> int:
import json
# Print the HTTP result that was saved to the json file
# Returns: ElrsUploadResult enum
with open(output_json_file) as f:
output_json = json.load(f)
result = output_json['status']
msg = output_json['msg']
if result == 'ok':
# Update complete. Please wait for LED to resume blinking before disconnecting power.
msg = f'UPLOAD SUCCESS\n\033[32m{msg}\033[0m' # green
# 'ok' is the only acceptable result
retval = ElrsUploadResult.Success
elif result == 'mismatch':
# <b>Current target:</b> LONG_ASS_NAME.<br><b>Uploaded image:</b> OTHER_NAME.<br/><br/>Flashing the wrong firmware may lock or damage your device.
msg = msg.replace('<br>', '\n').replace('<br/>', '\n') # convert breaks to newline
msg = msg.replace('<b>', '\033[34m').replace('</b>', '\033[0m') # bold to blue
msg = '\033[33mTARGET MISMATCH\033[0m\n' + msg # yellow warning
retval = ElrsUploadResult.ErrorMismatch
else:
# Not enough space.
msg = f'UPLOAD ERROR\n\033[31m{msg}\033[0m' # red
retval = ElrsUploadResult.ErrorGeneral
print()
print(msg, flush=True)
return retval
def do_upload(elrs_bin_target, pio_target, upload_addr, isstm, env):
bootloader_target = None
app_start = 0 # eka bootloader offset
# Parse upload flags:
upload_flags = env.get('UPLOAD_FLAGS', [])
for line in upload_flags:
flags = line.split()
for flag in flags:
if "VECT_OFFSET=" in flag:
offset = flag.split("=")[1]
if "0x" in offset:
offset = int(offset, 16)
else:
offset = int(offset, 10)
app_start = offset
if "BOOTLOADER=" in flag:
bootloader_file = flag.split("=")[1]
bootloader_target = os.path.join((env.get('PROJECT_DIR')), bootloader_file)
bin_upload_output = os.path.splitext(elrs_bin_target)[0] + '-output.json'
if os.path.exists(bin_upload_output):
os.remove(bin_upload_output)
cmd = ["curl", "--max-time", "60",
"--retry", "2", "--retry-delay", "1",
"--header", "X-FileSize: " + str(os.path.getsize(elrs_bin_target)),
"-o", "%s" % (bin_upload_output)]
uri = 'update'
do_bin_upload = True
if pio_target == 'uploadconfirm':
uri = 'forceupdate?action=confirm'
do_bin_upload = False
if pio_target == 'uploadforce':
cmd += ["-F", "force=1"]
if isstm:
cmd += ["-F", "flash_address=0x%X" % (app_start,)]
cmd += ["-F", "type=tx"]
if do_bin_upload:
cmd += "-F", "data=@%s" % (elrs_bin_target),
if bootloader_target is not None and isstm:
cmd_bootloader = ["curl", "--max-time", "60",
"--retry", "2", "--retry-delay", "1",
"-F", "data=@%s" % (bootloader_target,), "-F", "flash_address=0x0000"]
upload_port = env.get('UPLOAD_PORT', None)
if upload_port is not None:
upload_addr = [upload_port]
returncode = ElrsUploadResult.ErrorGeneral
for addr in upload_addr:
addr = "http://%s/%s" % (addr, uri)
print(" ** UPLOADING TO: %s" % addr)
try:
# Flash bootloader first if set
if bootloader_target is not None:
print("** Flashing Bootloader...")
print(cmd_bootloader)
subprocess.check_call(cmd_bootloader + [addr])
print("** Bootloader Flashed!")
print()
# Flash main application binary
subprocess.check_call(cmd + [addr])
returncode = process_http_result(bin_upload_output)
except subprocess.CalledProcessError as e:
returncode = e.returncode
if returncode == ElrsUploadResult.Success:
return returncode
if returncode != ElrsUploadResult.Success:
print("WIFI upload FAILED!")
return returncode
def on_upload(source, target, env):
firmware_path = str(source[0])
bin_path = os.path.dirname(firmware_path)
upload_addr = ['elrs_tx', 'elrs_tx.local']
elrs_bin_target = os.path.join(bin_path, 'firmware.elrs')
if not os.path.exists(elrs_bin_target):
elrs_bin_target = os.path.join(bin_path, 'firmware.bin.gz')
if not os.path.exists(elrs_bin_target):
elrs_bin_target = os.path.join(bin_path, 'firmware.bin')
if not os.path.exists(elrs_bin_target):
raise Exception("No valid binary found!")
pio_target = target[0].name
isstm = env.get('PIOPLATFORM', '') in ['ststm32']
return do_upload(elrs_bin_target, pio_target, upload_addr, isstm, env)