Skip to content

Commit

Permalink
OTA filename according to ZCL
Browse files Browse the repository at this point in the history
  • Loading branch information
devbis committed Oct 14, 2023
1 parent 151f0c3 commit 7335b66
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Build
id: make
run: |
cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} --target=z03mmc.ota
cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} --target=z03mmc.zigbee
echo "status=success" >> $GITHUB_OUTPUT
- name: Build additional firmware with Heiman model
Expand All @@ -51,5 +51,5 @@ jobs:
# A file, directory or wildcard pattern that describes what to upload
path: |
${{ env.ARTIFACT_DIR }}/*.bin
${{ env.ARTIFACT_DIR }}/*.ota
${{ env.ARTIFACT_DIR }}/*.zigbee
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ it compatible with Zigbee networks.
3. Configure and build:
```sh
cmake -DSDK_PREFIX=$(pwd)/../tl_zigbee_sdk -DTOOLCHAIN_PREFIX=$(pwd)/../tc32 -B build .
cmake --build build --target z03mmc.bin
cmake --build build --target z03mmc.zigbee
```

Firmware binary is located at `build/src/z03mmc.bin`
The binary with OTA header is at the same folder, ending with `z03mmc.zigbee`

## Flashing firmware with USB to UART

Expand Down
4 changes: 2 additions & 2 deletions cmake/TelinkSDK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ FUNCTION(ADD_OTA_TARGET TARGET TOOLS_PATH)
ELSE()
SET(FILENAME "${TARGET}")
ENDIF()
ADD_CUSTOM_TARGET("${TARGET}.ota"
ADD_CUSTOM_TARGET("${TARGET}.zigbee"
DEPENDS ${TARGET}.bin
COMMAND python3 ${TOOLS_PATH}/make_ota.py ${FILENAME}.bin -o ${FILENAME}.ota
COMMAND python3 ${TOOLS_PATH}/make_ota.py ${FILENAME}.bin
)
ENDFUNCTION()

Expand Down
34 changes: 27 additions & 7 deletions tools/make_ota.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import argparse
import binascii
import os
import struct

OTA_MAGIC = b'\x5d\x02'
Expand All @@ -10,7 +11,7 @@
def main(args):
assert args.input_file != args.output

with open(args.input_file, 'rb') as bin_file:
with (open(args.input_file, 'rb') as bin_file):
bin_file.seek(0, 0)
firmware = bytearray(bin_file.read(-1))
if firmware[6:8] != OTA_MAGIC:
Expand All @@ -30,30 +31,49 @@ def main(args):
header_size = 56
firmware_len = len(firmware)
total_image_size = firmware_len + header_size + 6
manufacturer_code = int.from_bytes(firmware[18:20], byteorder='little')
image_type = int.from_bytes(firmware[20:22], byteorder='little')
file_version = args.set_version or int.from_bytes(firmware[2:6], byteorder='little')
ota_hdr = ota_hdr_s.pack(
0xbeef11e,
0x100, # header version is 0x0100
header_size,
0, # ota_ext_hdr_value if ota_ext_hdr else 0,
int.from_bytes(firmware[18:20], byteorder='little'), # args.manufacturer,
int.from_bytes(firmware[20:22], byteorder='little'), # args.image_type,
args.set_version or int.from_bytes(firmware[2:6], byteorder='little'), # 0x10023001 # options.File_Version
manufacturer_code, # args.manufacturer,
image_type, # args.image_type,
file_version, # options.File_Version
args.ota_version, # options.stack_version,
b'\x00' * 32, # OTA_Header_String.encode(),
total_image_size,
)
# add chunk header: 0 - firmware type
ota_hdr += struct.pack('<HI', 0, firmware_len)
with open(args.output, 'wb') as output:

out_filename = args.output
if not out_filename:
head, tail = os.path.split(args.input_file)
if args.output_title:
name = args.output_title
else:
name, _ = os.path.splitext(tail)
out_filename = os.path.join(head, '{:04x}-{:04x}-{:08x}-{}.zigbee'.format(
manufacturer_code,
image_type,
file_version,
name,
))
with open(out_filename, 'wb') as output:
bin_file.seek(0, 0)
output.write(ota_hdr)
output.write(firmware)
print("%s was created with ZCL OTA Header." % args.output)
print("%s was created with ZCL OTA Header." % out_filename)


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("input_file", help="path to input file")
parser.add_argument("-o", '--output', help="path to output file", required=True)
parser.add_argument("-ot", '--output-title', help="replace original file name with the string")
parser.add_argument("-o", '--output', help="path to output file")
# sync with g_zcl_basicAttrs.stackVersion
parser.add_argument("-s", '--ota-version', type=int, help="OTA stack version", default=2)
parser.add_argument("-v", '--set-version', type=lambda x: int(x, 0), help="Override version from BIN")
Expand Down

0 comments on commit 7335b66

Please sign in to comment.