summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorhathach <[email protected]>2025-09-24 12:49:52 +0700
committerhathach <[email protected]>2025-09-24 12:49:52 +0700
commit6317730be6d7a97fc12376d6f80c90a6dbfc20ea (patch)
tree45eb71ce1fffbffbf67f60a68ea0b9c4f79c418d /tools
parent3c39f60f63109f7828009ebda135fc675288d610 (diff)
unify callback argument. support multiple packet get object
Diffstat (limited to 'tools')
-rw-r--r--tools/file2carray.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/tools/file2carray.py b/tools/file2carray.py
new file mode 100644
index 000000000..abfb4e21b
--- /dev/null
+++ b/tools/file2carray.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+import argparse
+import random
+import os
+import sys
+import time
+import subprocess
+from pathlib import Path
+from multiprocessing import Pool
+from weakref import finalize
+
+
+def print_carray(f, payload):
+ while len(payload) > 0:
+ f.write('\n ')
+ f.write(', '.join('0x{:02x}'.format(x) for x in payload[0:16]))
+ f.write(',')
+ payload = payload[16:]
+ f.write('\n')
+
+
+def main():
+ parser = argparse.ArgumentParser(description='Convert binary files to C array format')
+ parser.add_argument('files', nargs='+', help='Binary files to convert')
+ args = parser.parse_args()
+
+ files = args.files
+ for fin_name in files:
+ if not os.path.isfile(fin_name):
+ print(f"File {fin_name} does not exist")
+ continue
+
+ with open(fin_name, 'rb') as fin:
+ contents = fin.read()
+ fout_name = fin_name + '.h'
+ with open(fout_name, 'w') as fout:
+ print(f"Converting {fin_name} to {fout_name}")
+ fout.write(f'const size_t bindata_len = {len(contents)};\n')
+ fout.write(f'const uint8_t bindata[] __attribute__((aligned(16))) = {{')
+ print_carray(fout, contents)
+ fout.write('};\n')
+
+
+if __name__ == '__main__':
+ sys.exit(main())