Skip to content

Commit db25341

Browse files
committed
Improve HPM API with kwargs
1 parent f315d6d commit db25341

3 files changed

Lines changed: 33 additions & 30 deletions

File tree

bin2hpm/cli.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,21 @@ def main():
108108
print(f'FW version {v_maj}.{v_min:02d} / 0x{args.auxillary:08x}\n')
109109

110110
# Build HPM upgrade image header
111-
result = hpm.upg_image_hdr({
112-
'device_id': args.device,
113-
'manufacturer_id': args.manufacturer,
114-
'product_id': args.product,
115-
'components': components,
116-
'version_major': v_maj,
117-
'version_minor': v_min,
118-
'version_aux': v_aux
119-
})
111+
result = hpm.upg_image_hdr(
112+
device_id=args.device,
113+
manufacturer_id=args.manufacturer,
114+
product_id=args.product,
115+
components=components,
116+
version_major=v_maj,
117+
version_minor=v_min,
118+
version_aux=v_aux,
119+
)
120120

121121
# Append HPM upgrade action (HPM prepare action)
122-
result += hpm.upg_action_hdr(components, hpm.UpgradeActionType.Prepare)
122+
result += hpm.upg_action_hdr(
123+
action_type=hpm.UpgradeActionType.Prepare,
124+
components=components
125+
)
123126

124127
# Read input file
125128
with open(args.infile, 'rb') as f:
@@ -144,12 +147,12 @@ def main():
144147

145148
# Append HPM upgrade action image
146149
result += hpm.upg_action_img(
147-
components,
148-
v_maj,
149-
v_min,
150-
v_aux,
151-
args.description or os.path.basename(args.infile)[:20],
152-
img_data
150+
img_data,
151+
components=components,
152+
version_major=v_maj,
153+
version_minor=v_min,
154+
version_aux=v_aux,
155+
desc_str=args.description or os.path.basename(args.infile)[:20]
153156
)
154157

155158
# Append MD5 hash

bin2hpm/hpm.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def encode_generic(table, values):
7171
else:
7272
if name == 'version_minor':
7373
val = to_bcd(val)
74+
elif isinstance(val, Enum):
75+
val = val.value
7476
result += int.to_bytes(val, length=size, byteorder='little')
7577

7678
except OverflowError:
@@ -81,27 +83,25 @@ def encode_generic(table, values):
8183
sys.exit(-1)
8284
return result
8385

84-
def upg_image_hdr(values):
85-
result = encode_generic(UPG_IMG_HEADER, values)
86+
def upg_image_hdr(**kwargs):
87+
result = encode_generic(UPG_IMG_HEADER, kwargs)
8688
result += zero_cksum(result)
8789
return result
8890

89-
def upg_action_hdr(components, action_type):
90-
result = encode_generic(UPG_ACTION_HEADER, {
91-
'action_type': action_type.value,
92-
'components': components
93-
})
91+
def upg_action_hdr(**kwargs):
92+
result = encode_generic(UPG_ACTION_HEADER, kwargs)
9493
result += zero_cksum(result)
9594
return result
9695

97-
def upg_action_img(components, v_maj, v_min, v_aux, description, img_data):
98-
result = upg_action_hdr(components, UpgradeActionType.Upload)
96+
def upg_action_img(img_data,**kwargs):
97+
98+
result = upg_action_hdr(**{
99+
'action_type': UpgradeActionType.Upload,
100+
**kwargs
101+
})
99102
result += encode_generic(UPG_ACTION_IMG_INFO, {
100-
'version_major': v_maj,
101-
'version_minor': v_min,
102-
'version_aux': v_aux,
103-
'desc_str': description,
104-
'img_length': len(img_data)
103+
'img_length': len(img_data),
104+
**kwargs
105105
})
106106
result += img_data
107107
return result

test.hpm

-51.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)