This repository has been archived by the owner on Feb 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig-converter.py
executable file
·103 lines (90 loc) · 3.4 KB
/
config-converter.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
#
# LoRa-Connect
#
# Copyright (C) 2023 Richard "Shred" Körber
# https://codeberg.org/shred/lora-connect
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
from base64 import urlsafe_b64encode
import json
import os
import sys
standardErrorMap = {0: 'Off', 1: 'Present', 2: 'Confirmed'}
def main(argv):
loraKey = bytearray(os.urandom(32))
loraKeyBase64 = urlsafe_b64encode(loraKey).decode('ASCII').rstrip('=')
with open(argv[0], "r") as f:
devices = json.load(f)
if len(devices) > 1:
print('Only one appliance is supported at the moment', file=sys.stderr)
return
device = devices[0]
if 'iv' not in device:
print('Only appliances using port 80 are supported at the moment', file=sys.stderr)
return
print('Use these lines in your sender/config.h file:', file=sys.stderr)
print('', file=sys.stderr)
print('#define HC_APPLIANCE_KEY "%s"' % (device['key']), file=sys.stderr)
print('#define HC_APPLIANCE_IV "%s"' % (device['iv']), file=sys.stderr)
print('', file=sys.stderr)
print('New random key for your sender/config.h and receiver/config.h file:', file=sys.stderr)
print('', file=sys.stderr)
print('#define LORA_ENCRYPT_KEY "%s"' % loraKeyBase64, file=sys.stderr)
print('', file=sys.stderr)
featureMap = {}
valueMap = {}
for key, description in device['features'].items():
intKey = int(key)
featureMap[intKey] = description['name']
if 'values' in description:
kvMap = {}
for vk, vd in description['values'].items():
kvMap[int(vk)] = vd
valueMap[intKey] = kvMap
print('/* THIS FILE WAS AUTO-GENERATED WITH config-converter.py */')
print('/* All manual changes will be lost. */')
print()
print('#include "mapping.h"')
print()
print('String mapKey(uint16_t key) {')
print(' switch (key) {')
for key, value in sorted(featureMap.items()):
print(' case %d: return F("%s");' % (key, value))
print(' default: return String(key, DEC);')
print(' }')
print('}')
print()
print('String mapIntValue(uint16_t key, int32_t value) {')
print(' switch (key) {')
for key, value in sorted(valueMap.items()):
if value == standardErrorMap:
print(' case %d:' % (key))
print(' switch (value) {')
for vk, vd in standardErrorMap.items():
print(' case %d: return F("%s");' % (vk, vd))
print(' }')
print(' break;')
for key, value in sorted(valueMap.items()):
if value != standardErrorMap:
print(' case %d:' % (key))
print(' switch (value) {')
for vk, vd in sorted(value.items()):
print(' case %d: return F("%s");' % (vk, vd))
print(' }')
print(' break;')
print(' }')
print(' return F("");');
print('}')
print()
if __name__ == "__main__":
main(sys.argv[1:])