forked from attermann/microReticulum_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextra_script.py
More file actions
872 lines (749 loc) · 32.2 KB
/
Copy pathextra_script.py
File metadata and controls
872 lines (749 loc) · 32.2 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
import time
import hashlib
import json
import os
import re
import shutil
import subprocess
import sys
import sysconfig
import tempfile
import zipfile
FEND = 0xC0
FESC = 0xDB
TFEND = 0xDC
TFESC = 0xDD
CMD_DETECT = 0x08
CMD_FW_HASH = 0x58
CMD_HASHES = 0x60
DETECT_REQ = 0x73
DETECT_RESP = 0x46
SERIAL_BAUDRATE = 115200
def stop_dashboard_for_upload_port(env, reason):
port = env.subst("$UPLOAD_PORT")
if not port or "$" in port:
return False
tools_dir = os.path.join(env.subst("$PROJECT_DIR"), "tools")
if tools_dir not in sys.path:
sys.path.insert(0, tools_dir)
try:
from micrornode_port_guard import stop_dashboard_for_port
except Exception as exc:
print("Warning: could not load dashboard port guard:", exc)
return False
return stop_dashboard_for_port(port, reason=reason)
def rnodeconf_path():
path = shutil.which("rnodeconf")
if path:
return path
script_name = "rnodeconf.exe" if os.name == "nt" else "rnodeconf"
candidates = []
try:
candidates.append(os.path.join(sysconfig.get_path("scripts", scheme="nt_user"), script_name))
except Exception:
pass
try:
candidates.append(os.path.join(sysconfig.get_path("scripts"), script_name))
except Exception:
pass
for candidate in candidates:
if candidate and os.path.exists(candidate):
return candidate
return None
def run_rnodeconf(args, optional=True):
executable = rnodeconf_path()
if executable is None:
print("rnodeconf not found; skipping rnodeconf step")
return False
print(" ".join([executable] + args))
result = subprocess.run([executable] + args, check=False)
if result.returncode != 0:
message = "rnodeconf exited with code %d" % result.returncode
if optional:
print("Warning:", message)
return False
raise RuntimeError(message)
return True
def run_micrornodeconf(env, args, optional=True):
script = os.path.join(env.subst("$PROJECT_DIR"), "tools", "micrornodeconf.py")
if not os.path.isfile(script):
print("tools/micrornodeconf.py not found; skipping local config step")
return False
port = env.subst("$UPLOAD_PORT")
command = [sys.executable, script, port] + list(args)
print(" ".join(command))
result = subprocess.run(command, check=False)
if result.returncode != 0:
message = "micrornodeconf exited with code %d" % result.returncode
if optional:
print("Warning:", message)
return False
raise RuntimeError(message)
return True
def kiss_escape(payload):
out = bytearray()
for byte in payload:
if byte == FEND:
out.extend((FESC, TFEND))
elif byte == FESC:
out.extend((FESC, TFESC))
else:
out.append(byte)
return bytes(out)
def kiss_unescape(payload):
out = bytearray()
i = 0
while i < len(payload):
byte = payload[i]
if byte == FESC and i + 1 < len(payload):
escaped = payload[i + 1]
if escaped == TFEND:
out.append(FEND)
elif escaped == TFESC:
out.append(FESC)
else:
out.append(escaped)
i += 2
else:
out.append(byte)
i += 1
return bytes(out)
def kiss_frame(command, payload=b""):
return bytes((FEND, command)) + kiss_escape(payload) + bytes((FEND,))
def extract_kiss_frames(buffer):
frames = []
while True:
try:
start = buffer.index(FEND)
except ValueError:
if len(buffer) > 4096:
del buffer[:-256]
break
if start > 0:
del buffer[:start]
try:
end = buffer.index(FEND, 1)
except ValueError:
break
raw_frame = bytes(buffer[1:end])
del buffer[:end + 1]
if raw_frame:
frames.append(kiss_unescape(raw_frame))
return frames
def read_kiss_frames(ser, seconds, tx_each=None):
frames = []
buffer = bytearray()
start = time.time()
next_tx = 0.0
while time.time() - start < seconds:
now = time.time() - start
if tx_each is not None and now >= next_tx:
ser.write(tx_each)
ser.flush()
next_tx += 1.0
data = ser.read(4096)
if data:
buffer.extend(data)
frames.extend(extract_kiss_frames(buffer))
time.sleep(0.03)
return frames
def wait_for_kiss_detect(ser, seconds):
detect_frame = kiss_frame(CMD_DETECT, bytes((DETECT_REQ,)))
frames = read_kiss_frames(ser, seconds, tx_each=detect_frame)
return any(frame == bytes((CMD_DETECT, DETECT_RESP)) for frame in frames)
def query_firmware_hashes(ser):
ser.write(kiss_frame(CMD_HASHES, b"\x01"))
ser.write(kiss_frame(CMD_HASHES, b"\x02"))
ser.flush()
target_hash = None
running_hash = None
for frame in read_kiss_frames(ser, 2.0):
if len(frame) == 34 and frame[0] == CMD_HASHES:
if frame[1] == 0x01:
target_hash = frame[2:]
elif frame[1] == 0x02:
running_hash = frame[2:]
return target_hash, running_hash
def write_firmware_hash(port, expected_hash, detect_timeout=45):
try:
import serial
except ImportError as exc:
raise RuntimeError("pyserial is required for post-upload firmware hash update") from exc
print("Writing firmware hash over KISS...")
print("Serial port:", port)
with serial.Serial(port, SERIAL_BAUDRATE, timeout=0.2, write_timeout=1) as ser:
try:
ser.dtr = False
ser.rts = False
except Exception:
pass
ser.reset_input_buffer()
if not wait_for_kiss_detect(ser, detect_timeout):
raise RuntimeError("Device did not answer KISS detect before firmware hash update")
target_hash, running_hash = query_firmware_hashes(ser)
if target_hash is not None:
print("Current target firmware hash:", target_hash.hex())
if running_hash is not None:
print("Running firmware hash:", running_hash.hex())
ser.write(kiss_frame(CMD_FW_HASH, expected_hash))
ser.flush()
# The device resets after this command when the previous target hash was
# invalid, so wait for it to come back and verify both hash values.
time.sleep(2.0)
if not wait_for_kiss_detect(ser, detect_timeout):
raise RuntimeError("Device did not answer KISS detect after firmware hash update")
target_hash, running_hash = query_firmware_hashes(ser)
if target_hash is None or running_hash is None:
raise RuntimeError("Could not verify firmware hashes after update")
if target_hash != expected_hash or running_hash != expected_hash:
raise RuntimeError(
"Firmware hash verification failed: target=%s running=%s expected=%s" % (
target_hash.hex(),
running_hash.hex(),
expected_hash.hex(),
)
)
print("Firmware hash verified:", expected_hash.hex())
def remove_file(path):
try:
os.remove(path)
except FileNotFoundError:
pass
def copy_file(src, dst):
if not os.path.isfile(src):
raise FileNotFoundError(src)
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.copy2(src, dst)
def patch_microstore_file(path, insert_before):
if not os.path.isfile(path):
return False
with open(path, "r", encoding="utf-8") as file:
text = file.read()
if "#ifndef USTORE_LOG" in text:
return True
patched = re.sub(r"(?<![A-Za-z0-9_])printf\(", "USTORE_LOG(", text)
macro = (
"#ifndef USTORE_LOG\n"
" #if defined(USTORE_ENABLE_LOGGING)\n"
" #define USTORE_LOG(...) printf(__VA_ARGS__)\n"
" #else\n"
" #define USTORE_LOG(...) ((void)0)\n"
" #endif\n"
"#endif\n\n"
)
marker = insert_before
if marker not in patched:
raise RuntimeError(f"Could not patch microStore logging in {path}: marker not found")
patched = patched.replace(marker, macro + marker, 1)
with open(path, "w", encoding="utf-8", newline="") as file:
file.write(patched)
print("Patched microStore logging:", path)
return True
def patch_microstore_logging(env):
libdeps_dir = env.subst("$PROJECT_LIBDEPS_DIR")
if not libdeps_dir or libdeps_dir == "$PROJECT_LIBDEPS_DIR":
libdeps_dir = os.path.join(env.subst("$PROJECT_DIR"), ".pio", "libdeps")
microstore_dir = os.path.join(libdeps_dir, env.subst("$PIOENV"), "microStore")
if not os.path.isdir(microstore_dir):
print("microStore dependency is not installed yet; logging patch will retry during build.")
return
patch_microstore_file(
os.path.join(microstore_dir, "include", "microStore", "FileStore.h"),
"namespace microStore {",
)
patch_microstore_file(
os.path.join(microstore_dir, "include", "microStore", "Adapters", "PosixFileSystem.h"),
"namespace microStore { namespace Adapters {",
)
def patch_microstore_logging_action(source, target, env):
patch_microstore_logging(env)
def patch_microstore_filestore_compaction_file(path):
if not os.path.isfile(path):
return False
with open(path, "r", encoding="utf-8") as file:
text = file.read()
if "Closing active file before threshold compaction" in text:
return True
old = (
"\tvoid compact_if_threshold()\n"
"\t{\n"
"#if USTORE_COMPACT_THRESHOLD > 0\n"
"\t\tuint32_t total = (uint32_t)_index.size() + _dead_since_compact;\n"
"\t\tif (total > 0 && _dead_since_compact * 100 / total >= USTORE_COMPACT_THRESHOLD) {\n"
"USTORE_LOG(\"[ustore] Compaction triggered by deleted threshold\\n\");\n"
"\t\t\tif (compact()) {\n"
"\t\t\t\t// After threshold-triggered compaction, seg0 holds the compacted data.\n"
"\t\t\t\t// Open seg1 for new writes, mirroring what rotate_segment_if_needed() does\n"
"\t\t\t\t// after a rotation-triggered compaction.\n"
"\t\t\t\tcurrent_segment = 1;\n"
"\t\t\t\topen_segment(current_segment);\n"
"\t\t\t}\n"
"\t\t}\n"
"#endif\n"
"\t}\n"
)
new = (
"\tvoid compact_if_threshold()\n"
"\t{\n"
"#if USTORE_COMPACT_THRESHOLD > 0\n"
"\t\tuint32_t total = (uint32_t)_index.size() + _dead_since_compact;\n"
"\t\tif (total > 0 && _dead_since_compact * 100 / total >= USTORE_COMPACT_THRESHOLD) {\n"
"USTORE_LOG(\"[ustore] Compaction triggered by deleted threshold\\n\");\n"
"\t\t\tuint32_t reopen_segment = current_segment;\n"
"\t\t\tbool reopen_active = false;\n"
"\t\t\tif (active_file) {\n"
"USTORE_LOG(\"[ustore] Closing active file before threshold compaction\\n\");\n"
"\t\t\t\tflush_buffer();\n"
"\t\t\t\tactive_file.close();\n"
"\t\t\t\treopen_active = true;\n"
"\t\t\t}\n"
"\t\t\tif (compact()) {\n"
"\t\t\t\t// After threshold-triggered compaction, seg0 holds the compacted data.\n"
"\t\t\t\t// Open seg1 for new writes, mirroring what rotate_segment_if_needed() does\n"
"\t\t\t\t// after a rotation-triggered compaction.\n"
"\t\t\t\tcurrent_segment = 1;\n"
"\t\t\t\topen_segment(current_segment);\n"
"\t\t\t}\n"
"\t\t\telse if (reopen_active) {\n"
"\t\t\t\topen_segment(reopen_segment);\n"
"\t\t\t}\n"
"\t\t}\n"
"#endif\n"
"\t}\n"
)
if old not in text:
raise RuntimeError(f"Could not patch microStore threshold compaction in {path}: marker not found")
text = text.replace(old, new, 1)
with open(path, "w", encoding="utf-8", newline="") as file:
file.write(text)
print("Patched microStore threshold compaction:", path)
return True
def patch_microstore_filestore_compaction(env):
libdeps_dir = env.subst("$PROJECT_LIBDEPS_DIR")
if not libdeps_dir or libdeps_dir == "$PROJECT_LIBDEPS_DIR":
libdeps_dir = os.path.join(env.subst("$PROJECT_DIR"), ".pio", "libdeps")
filestore_h = os.path.join(libdeps_dir, env.subst("$PIOENV"), "microStore", "include", "microStore", "FileStore.h")
if not os.path.isfile(filestore_h):
print("microStore dependency is not installed yet; compaction patch will retry during build.")
return
patch_microstore_filestore_compaction_file(filestore_h)
def patch_microstore_filestore_compaction_action(source, target, env):
patch_microstore_filestore_compaction(env)
def patch_microreticulum_identity_file(path):
if not os.path.isfile(path):
return False
with open(path, "r", encoding="utf-8") as file:
text = file.read()
if "announce_min_data_len" in text:
return True
marker = "\t\tif (packet.packet_type() == Type::Packet::ANNOUNCE) {\n"
guard = (
"\t\tif (packet.packet_type() == Type::Packet::ANNOUNCE) {\n"
"\t\t\tconst size_t announce_min_data_len = KEYSIZE/8 + NAME_HASH_LENGTH/8 + RANDOM_HASH_LENGTH/8 + SIGLENGTH/8;\n"
"\t\t\tif (packet.data().size() < announce_min_data_len) {\n"
"\t\t\t\tWARNINGF(\"Dropping short announce packet: %u bytes\", (unsigned)packet.data().size());\n"
"\t\t\t\treturn false;\n"
"\t\t\t}\n"
)
if marker not in text:
raise RuntimeError(f"Could not patch microReticulum Identity.cpp: marker not found in {path}")
text = text.replace(marker, guard, 1)
with open(path, "w", encoding="utf-8", newline="") as file:
file.write(text)
print("Patched microReticulum announce length guard:", path)
return True
def patch_microreticulum_identity_guard(env):
libdeps_dir = env.subst("$PROJECT_LIBDEPS_DIR")
if not libdeps_dir or libdeps_dir == "$PROJECT_LIBDEPS_DIR":
libdeps_dir = os.path.join(env.subst("$PROJECT_DIR"), ".pio", "libdeps")
identity_cpp = os.path.join(libdeps_dir, env.subst("$PIOENV"), "microReticulum", "src", "microReticulum", "Identity.cpp")
if not os.path.isfile(identity_cpp):
print("microReticulum dependency is not installed yet; announce guard patch will retry during build.")
return
patch_microreticulum_identity_file(identity_cpp)
def patch_microreticulum_identity_guard_action(source, target, env):
patch_microreticulum_identity_guard(env)
def patch_microreticulum_breadcrumbs_file(path):
if not os.path.isfile(path):
return False
with open(path, "r", encoding="utf-8") as file:
text = file.read()
if "URTNDBG rns_job=" in text:
return True
helper_marker = "using namespace RNS::Utilities;\n"
helper = (
"using namespace RNS::Utilities;\n\n"
"#ifdef ARDUINO\n"
"static void urn_dbg_rns_job(const char *stage) {\n"
"#if defined(ESP32)\n"
" Serial.printf(\"URTNDBG rns_job=%s ms=%lu heap=%u min_heap=%u\\r\\n\", stage, (unsigned long)millis(), ESP.getFreeHeap(), ESP.getMinFreeHeap());\n"
"#else\n"
" Serial.printf(\"URTNDBG rns_job=%s ms=%lu\\r\\n\", stage, (unsigned long)millis());\n"
"#endif\n"
"}\n"
"#else\n"
"static void urn_dbg_rns_job(const char *) {}\n"
"#endif\n"
)
if helper_marker not in text:
raise RuntimeError(f"Could not patch microReticulum breadcrumbs in {path}: namespace marker not found")
text = text.replace(helper_marker, helper, 1)
clean_marker = "\tif (now > _object->_last_cache_clean + _clean_interval) {\n\t\tclean_caches();\n\t}\n"
clean_repl = (
"\tif (now > _object->_last_cache_clean + _clean_interval) {\n"
"\t\turn_dbg_rns_job(\"clean_begin\");\n"
"\t\tclean_caches();\n"
"\t\turn_dbg_rns_job(\"clean_end\");\n"
"\t}\n"
)
persist_marker = "\tif (now > _object->_last_data_persist + _persist_interval) {\n\t\tpersist_data();\n\t}\n"
persist_repl = (
"\tif (now > _object->_last_data_persist + _persist_interval) {\n"
"\t\turn_dbg_rns_job(\"persist_begin\");\n"
"\t\tpersist_data();\n"
"\t\turn_dbg_rns_job(\"persist_end\");\n"
"\t}\n"
)
for marker, repl in ((clean_marker, clean_repl), (persist_marker, persist_repl)):
if marker not in text:
raise RuntimeError(f"Could not patch microReticulum breadcrumbs in {path}: marker not found")
text = text.replace(marker, repl, 1)
with open(path, "w", encoding="utf-8", newline="") as file:
file.write(text)
print("Patched microReticulum RNS breadcrumbs:", path)
return True
def patch_microreticulum_breadcrumbs(env):
libdeps_dir = env.subst("$PROJECT_LIBDEPS_DIR")
if not libdeps_dir or libdeps_dir == "$PROJECT_LIBDEPS_DIR":
libdeps_dir = os.path.join(env.subst("$PROJECT_DIR"), ".pio", "libdeps")
reticulum_cpp = os.path.join(libdeps_dir, env.subst("$PIOENV"), "microReticulum", "src", "microReticulum", "Reticulum.cpp")
if not os.path.isfile(reticulum_cpp):
print("microReticulum dependency is not installed yet; breadcrumb patch will retry during build.")
return
patch_microreticulum_breadcrumbs_file(reticulum_cpp)
def patch_microreticulum_breadcrumbs_action(source, target, env):
patch_microreticulum_breadcrumbs(env)
def firmware_version(project_dir):
major_version = None
minor_version = None
with open(os.path.join(project_dir, "Config.h"), "rb") as file:
for line in file.read().splitlines():
dline = line.decode("utf-8").strip()
if dline.startswith("#define MAJ_VERS"):
major_version = int(dline.split()[2], 0)
elif dline.startswith("#define MIN_VERS"):
minor_version = int(dline.split()[2], 0)
if major_version is None or minor_version is None:
raise RuntimeError("Could not determine firmware version from Config.h")
return "%d.%02d" % (major_version, minor_version)
def build_release_json(project_dir, release_dir):
version = firmware_version(project_dir)
release_hashes = {}
for filename in sorted(os.listdir(release_dir)):
path = os.path.join(release_dir, filename)
if os.path.isfile(path) and filename.startswith("rnode_firmware") and filename.endswith(".zip") and not filename.endswith("_debug.zip"):
with open(path, "rb") as file:
release_hashes[filename] = {
"hash": hashlib.sha256(file.read()).hexdigest(),
"version": version,
}
release_json = os.path.join(release_dir, "release.json")
with open(release_json, "w", encoding="utf-8") as file:
json.dump(release_hashes, file, separators=(",", ":"))
return version, release_hashes
def build_release_feed(release_dir, version, release_hashes):
feed_dir = os.path.join(release_dir, "feed")
latest_dir = os.path.join(feed_dir, "latest", "download")
version_dir = os.path.join(feed_dir, "download", version)
os.makedirs(latest_dir, exist_ok=True)
os.makedirs(version_dir, exist_ok=True)
release_json = json.dumps(release_hashes, separators=(",", ":"))
for target_dir in (latest_dir, version_dir):
with open(os.path.join(target_dir, "release.json"), "w", encoding="utf-8") as file:
file.write(release_json)
for filename in release_hashes:
copy_file(os.path.join(release_dir, filename), os.path.join(version_dir, filename))
def zip_files(zip_path, files):
remove_file(zip_path)
with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:
for src, arcname in files:
if not os.path.isfile(src):
raise FileNotFoundError(src)
archive.write(src, arcname)
def parse_int(value):
return int(str(value).strip(), 0)
def spiffs_partition_size(env):
partitions = env.GetProjectOption("board_build.partitions", "")
if not partitions:
return None
project_dir = env.subst("$PROJECT_DIR")
csv_path = partitions
if not os.path.isabs(csv_path):
csv_path = os.path.join(project_dir, csv_path)
if not os.path.isfile(csv_path):
return None
with open(csv_path, "r", encoding="utf-8") as file:
for line in file:
clean = line.split("#", 1)[0].strip()
if not clean:
continue
fields = [field.strip() for field in clean.split(",")]
if len(fields) >= 5 and fields[0] == "spiffs":
return parse_int(fields[4])
return None
def build_minimal_console_image(env, image_size):
project_dir = env.subst("$PROJECT_DIR")
build_dir = env.subst("$BUILD_DIR")
spiffsgen = os.path.join(project_dir, "Release", "esptool", "spiffsgen.py")
if not os.path.isfile(spiffsgen):
raise FileNotFoundError(spiffsgen)
source_dir = tempfile.mkdtemp(prefix="micrort-console-")
image_path = os.path.join(build_dir, env.subst("$PROGNAME") + ".console_image.bin")
try:
with open(os.path.join(source_dir, "index.html"), "w", encoding="utf-8") as file:
file.write("<!doctype html><meta charset=\"utf-8\"><title>microReticulum</title><body>microReticulum transport node</body>")
subprocess.run([sys.executable, spiffsgen, str(image_size), source_dir, image_path], check=True)
finally:
shutil.rmtree(source_dir, ignore_errors=True)
return image_path
def console_image_for_package(env, release_dir):
variant = env.GetProjectOption("custom_variant")
if variant == "tbeam":
image_size = spiffs_partition_size(env)
if image_size is None:
raise RuntimeError("Could not determine T-Beam SPIFFS partition size")
print("Building minimal T-Beam console image, size:", image_size)
return build_minimal_console_image(env, image_size)
return os.path.join(release_dir, "console_image.bin")
#
# Custom targets
#
def target_package(target, source, env):
print("*** Executing target_package steps...")
print("Platform:", env.GetProjectOption("platform"))
print("Board:", env.GetProjectOption("board"))
print("Variant:", env.GetProjectOption("custom_variant"))
if env.GetProjectOption("custom_variant").endswith('_local'):
print("*** Skipping target_package for local build")
return
# do some actions
platform = env.GetProjectOption("platform")
board = env.GetProjectOption("board")
firmware_package(env)
#
# Upload actions
#
def pre_upload(source, target, env):
print("*** Executing pre_upload steps...")
stop_dashboard_for_upload_port(env, "PlatformIO upload")
def post_upload(source, target, env):
print("*** Executing post_upload steps...")
print("Platform:", env.GetProjectOption("platform"))
print("Board:", env.GetProjectOption("board"))
print("Variant:", env.GetProjectOption("custom_variant"))
print("Serial port:", env.subst("$UPLOAD_PORT"))
# do some actions
platform = env.GetProjectOption("platform")
board = env.GetProjectOption("board")
if ("espressif32" in platform):
time.sleep(10)
# device provisioning is incomplete and only currently appropriate for 915MHz T-Beam
device_provision(env)
firmware_hash(source, env)
# firmware pacakaging is incomplete due to missing console image
#firmware_package(env)
elif ("nordicnrf52" in platform):
time.sleep(5)
# device provisioning is incomplete and only currently appropriate for 915MHz RAK4631
device_provision(env)
time.sleep(5)
firmware_hash(source, env)
# firmware pacakaging is incomplete due to missing console image
#firmware_package(env)
def pre_clean(env):
print("*** Executing pre_clean steps...")
print("Platform:", env.GetProjectOption("platform"))
print("Board:", env.GetProjectOption("board"))
print("Variant:", env.GetProjectOption("custom_variant"))
project_dir = env.subst("$PROJECT_DIR")
print("project_dir:", project_dir)
remove_file(os.path.join(project_dir, "Release", env.subst("$PROGNAME") + ".zip"))
remove_file(os.path.join(project_dir, "Debug", env.subst("$PROGNAME") + ".elf"))
remove_file(os.path.join(project_dir, "Debug", env.subst("$PROGNAME") + ".map"))
remove_file(os.path.join(project_dir, "Release", env.subst("$PROGNAME") + "_debug.zip"))
def full_clean(env):
print("*** Executing full_clean steps...")
project_dir = env.subst("$PROJECT_DIR")
print("project_dir:", project_dir)
remove_file(os.path.join(project_dir, "Release", "release.json"))
shutil.rmtree(os.path.join(project_dir, "Release", "feed"), ignore_errors=True)
def device_wipe(env):
# Device wipe
print("--- Wiping Device ---")
run_micrornodeconf(env, ["--eeprom-wipe"], optional=False)
def device_provision(env):
# Device provision
print("--- Provisioning Device ---")
platform = env.GetProjectOption("platform")
print("Platform:", platform)
board = env.GetProjectOption("board")
print("Board:", board)
variant = env.GetProjectOption("custom_variant")
print("Variant:", variant)
match variant:
case "tbeam" | "tbeam_local":
run_micrornodeconf(env, ["--product", "e0", "--model", "e9", "--hwrev", "1", "--rom"])
case "lora32v21" | "lora32v21_local":
run_micrornodeconf(env, ["--product", "b1", "--model", "b9", "--hwrev", "1", "--rom"])
case "heltec32v4pa" | "heltec32v4pa_local":
run_micrornodeconf(env, ["--product", "c3", "--model", "c8", "--hwrev", "1", "--rom"])
case "rak4631" | "rak4631_local":
run_micrornodeconf(env, ["--product", "10", "--model", "12", "--hwrev", "1", "--rom"])
case "heltec_t114" | "heltec_t114_local":
run_micrornodeconf(env, ["--product", "c2", "--model", "c7", "--hwrev", "1", "--rom"])
case _:
print(f"Unknown board variant {variant}, can not provision device!")
def firmware_hash(source, env):
# Firmware hash
print("--- Updating Firmware Hash ---")
source_file = source[0].get_abspath()
platform = env.GetProjectOption("platform")
print("Platform:", platform)
if ("nordicnrf52" in platform):
build_dir = env.subst("$BUILD_DIR")
env.Execute("cd " + build_dir + "; unzip -o " + source_file + " " + env.subst("$PROGNAME") + ".bin")
#source_file.replace(".zip", ".bin")
source_file = build_dir + "/" + env.subst("$PROGNAME") + ".bin";
print("source_file:", source_file)
firmware_data = open(source_file, "rb").read()
calc_hash = hashlib.sha256(firmware_data).digest()
hex_hash = calc_hash.hex()
print("firmware_hash:", hex_hash)
stop_dashboard_for_upload_port(env, "firmware hash update")
write_firmware_hash(env.subst("$UPLOAD_PORT"), calc_hash)
else:
print("source_file:", source_file)
firmware_data = open(source_file, "rb").read()
calc_hash = hashlib.sha256(firmware_data[0:-32]).digest()
part_hash = firmware_data[-32:]
hex_hash = calc_hash.hex()
print("firmware_hash:", hex_hash)
if (calc_hash == part_hash):
stop_dashboard_for_upload_port(env, "firmware hash update")
write_firmware_hash(env.subst("$UPLOAD_PORT"), calc_hash)
else:
print("Calculated hash does not match!")
def firmware_package(env):
# Firmware package
print("--- Packaging Firmware ---")
platform = env.GetProjectOption("platform")
print("Platform:", platform)
board = env.GetProjectOption("board")
print("Board:", board)
variant = env.GetProjectOption("custom_variant")
print("Variant:", variant)
core_dir = env.subst("$CORE_DIR")
print("core_dir:", core_dir)
packages_dir = env.subst("$PACKAGES_DIR")
print("packages_dir:", packages_dir)
workspace_dir = env.subst("$WORKSPACE_DIR")
print("workspace_dir:", workspace_dir)
project_dir = env.subst("$PROJECT_DIR")
print("project_dir:", project_dir)
build_dir = env.subst("$BUILD_DIR")
print("build_dir:", build_dir)
release_dir = os.path.join(project_dir, "Release")
debug_dir = os.path.join(project_dir, "Debug")
os.makedirs(release_dir, exist_ok=True)
os.makedirs(debug_dir, exist_ok=True)
if ("espressif32" in platform):
progname = env.subst("$PROGNAME")
boot_app0_src = os.path.join(packages_dir, "framework-arduinoespressif32", "tools", "partitions", "boot_app0.bin")
if not os.path.isfile(boot_app0_src):
boot_app0_src = os.path.expanduser("~/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin")
boot_app0 = os.path.join(build_dir, progname + ".boot_app0")
bootloader = os.path.join(build_dir, progname + ".bootloader")
partitions = os.path.join(build_dir, progname + ".partitions")
copy_file(boot_app0_src, boot_app0)
copy_file(os.path.join(build_dir, "bootloader.bin"), bootloader)
copy_file(os.path.join(build_dir, "partitions.bin"), partitions)
console_image = console_image_for_package(env, release_dir)
firmware_zip = os.path.join(release_dir, "rnode_firmware_" + variant + ".zip")
zip_files(firmware_zip, [
(os.path.join(release_dir, "esptool", "esptool.py"), "esptool.py"),
(console_image, "console_image.bin"),
(os.path.join(build_dir, progname + ".bin"), progname + ".bin"),
(boot_app0, progname + ".boot_app0"),
(bootloader, progname + ".bootloader"),
(partitions, progname + ".partitions"),
])
elf = os.path.join(build_dir, progname + ".elf")
map_file = os.path.join(build_dir, progname + ".map")
if os.path.isfile(elf):
copy_file(elf, os.path.join(debug_dir, os.path.basename(elf)))
if os.path.isfile(map_file):
copy_file(map_file, os.path.join(debug_dir, os.path.basename(map_file)))
if os.path.isfile(elf) and os.path.isfile(map_file):
zip_files(os.path.join(release_dir, "rnode_firmware_" + variant + "_debug.zip"), [
(elf, os.path.basename(elf)),
(map_file, os.path.basename(map_file)),
])
elif ("nordicnrf52" in platform):
copy_file(
os.path.join(build_dir, env.subst("$PROGNAME") + ".zip"),
os.path.join(release_dir, env.subst("$PROGNAME") + ".zip"),
)
version, release_hashes = build_release_json(project_dir, release_dir)
build_release_feed(release_dir, version, release_hashes)
print("Release feed written:", os.path.join(release_dir, "feed"))
#
# Main script
#
Import("env")
env.Replace(PROGNAME="rnode_firmware_%s" % env.GetProjectOption("custom_variant"))
print("PROGNAME:", env.subst("$PROGNAME"))
print("*** Running custom script...")
platform = env.GetProjectOption("platform")
print("Platform:", platform)
targets = env.GetProjectOption("targets", [])
print("Targets:", targets)
patch_microstore_logging(env)
patch_microstore_filestore_compaction(env)
patch_microreticulum_identity_guard(env)
patch_microreticulum_breadcrumbs(env)
env.AddPreAction("$BUILD_DIR/src/RNode_Firmware.ino.cpp.o", patch_microstore_logging_action)
env.AddPreAction("$BUILD_DIR/src/RNode_Firmware.ino.cpp.o", patch_microstore_filestore_compaction_action)
env.AddPreAction("$BUILD_DIR/src/RNode_Firmware.ino.cpp.o", patch_microreticulum_identity_guard_action)
env.AddPreAction("$BUILD_DIR/src/RNode_Firmware.ino.cpp.o", patch_microreticulum_breadcrumbs_action)
# Clean
if env.IsCleanTarget():
pre_clean(env)
if "cleanall" in targets or "fullclean" in targets:
full_clean(env)
# Add custom targets
if ("espressif32" in platform):
env.AddCustomTarget(
name="package",
dependencies="$BUILD_DIR/${PROGNAME}.bin",
actions=[
target_package
],
title="Package",
description="Package esp32 firmware for delivery"
)
elif ("nordicnrf52" in platform):
# remove --specs=nano.specs to allow exceptions to work
if '--specs=nano.specs' in env['LINKFLAGS']:
env['LINKFLAGS'].remove('--specs=nano.specs')
env.AddCustomTarget(
name="package",
dependencies="$BUILD_DIR/${PROGNAME}.zip",
actions=[
target_package
],
title="Package",
description="Package nrf52 firmware for delivery"
)
# Register actions
env.AddPreAction("upload", pre_upload)
env.AddPostAction("upload", post_upload)