Skip to content

Commit d639c16

Browse files
committed
@pybricks/firmware: add firmware reader class
This moves some of the stuff for extracting data from firmware.zip files from pybricks-code into this package.
1 parent dc68693 commit d639c16

File tree

12 files changed

+4047
-202
lines changed

12 files changed

+4047
-202
lines changed

npm/firmware/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11

2+
## Unreleased
3+
### Added
4+
- Added new `FirmwareMetadata` interface for strongly typing `firmware.metadata.json`.
5+
- Added new `FirmwareReader` class for reading firmware.zip files.
6+
### Changed
7+
- Moved zip file names into `zipFileNameMap` and added `HubType` enum as lookup key.
8+
29
## 3.1.0 - 2020-11-09
310
### Changed
411
- Updated firmware to v3.0.0a10.
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`reading data works 1`] = `
4+
Uint8Array [
5+
0,
6+
64,
7+
0,
8+
32,
9+
197,
10+
240,
11+
0,
12+
8,
13+
17,
14+
241,
15+
0,
16+
8,
17+
17,
18+
241,
19+
0,
20+
8,
21+
0,
22+
0,
23+
0,
24+
0,
25+
0,
26+
0,
27+
0,
28+
0,
29+
0,
30+
0,
31+
0,
32+
0,
33+
0,
34+
0,
35+
0,
36+
0,
37+
0,
38+
0,
39+
0,
40+
0,
41+
0,
42+
0,
43+
0,
44+
0,
45+
0,
46+
0,
47+
0,
48+
0,
49+
17,
50+
241,
51+
0,
52+
8,
53+
0,
54+
0,
55+
0,
56+
0,
57+
0,
58+
0,
59+
0,
60+
0,
61+
17,
62+
241,
63+
0,
64+
8,
65+
137,
66+
50,
67+
1,
68+
8,
69+
17,
70+
241,
71+
0,
72+
8,
73+
0,
74+
0,
75+
0,
76+
0,
77+
17,
78+
241,
79+
0,
80+
8,
81+
17,
82+
241,
83+
0,
84+
8,
85+
17,
86+
241,
87+
0,
88+
8,
89+
229,
90+
51,
91+
1,
92+
8,
93+
237,
94+
43,
95+
1,
96+
8,
97+
17,
98+
241,
99+
0,
100+
8,
101+
0,
102+
0,
103+
0,
104+
0,
105+
]
106+
`;
107+
108+
exports[`reading data works 2`] = `
109+
Object {
110+
"checksum-type": "sum",
111+
"device-id": 64,
112+
"firmware-version": "v3.0.0a10",
113+
"max-firmware-size": 108544,
114+
"metadata-version": "1.0.0",
115+
"mpy-abi-version": 5,
116+
"mpy-cross-options": Array [
117+
"-mno-unicode",
118+
],
119+
"user-mpy-offset": 100008,
120+
}
121+
`;
122+
123+
exports[`reading data works 3`] = `
124+
"from pybricks import version
125+
from pybricks.tools import wait
126+
127+
print(version)
128+
wait(5000)
129+
print(\\"Hello, World!\\")
130+
"
131+
`;
132+
133+
exports[`reading data works 4`] = `
134+
"========================================================================================================================
135+
136+
pybricks-micropython-movehub-firmware
137+
138+
------------------------------------------------------------------------------------------------------------------------
139+
140+
========================================================================================================================
141+
142+
MAIN LICENSES
143+
144+
------------------------------------------------------------------------------------------------------------------------
145+
146+
MIT
147+
148+
MIT License Copyright (c) <year> <copyright holders>
149+
150+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \\"Software\\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
151+
152+
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
153+
154+
THE SOFTWARE IS PROVIDED \\"AS IS\\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
155+
156+
------------------------------------------------------------------------------------------------------------------------
157+
"
158+
`;
1.38 KB
Binary file not shown.
1.34 KB
Binary file not shown.
1.24 KB
Binary file not shown.
760 Bytes
Binary file not shown.

npm/firmware/__tests__/movehub.zip

1.56 KB
Binary file not shown.

npm/firmware/index.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2020 The Pybricks Authors
3+
4+
import * as fs from 'fs';
5+
import * as path from 'path';
6+
import * as util from 'util';
7+
import { FirmwareReader, FirmwareReaderErrorCode } from '.';
8+
9+
const readFile = util.promisify(fs.readFile);
10+
11+
test('bad zip data', async () => {
12+
try {
13+
await FirmwareReader.load(new Uint8Array(100));
14+
fail('FirmwareReader.load() should have failed');
15+
} catch (err) {
16+
expect(err.name).toMatch('FirmwareReaderError');
17+
expect(err.code).toBe(FirmwareReaderErrorCode.ZipError);
18+
}
19+
});
20+
21+
test('missing firmware-base.bin', async () => {
22+
var file = await readFile(
23+
path.resolve(__dirname, '__tests__', 'movehub-no-firmware-base.zip')
24+
);
25+
try {
26+
await FirmwareReader.load(file);
27+
fail('FirmwareReader.load() should have failed');
28+
} catch (err) {
29+
expect(err.name).toMatch('FirmwareReaderError');
30+
expect(err.code).toBe(FirmwareReaderErrorCode.MissingFirmwareBaseBin);
31+
}
32+
});
33+
34+
test('missing firmware.metadata.json', async () => {
35+
var file = await readFile(
36+
path.resolve(__dirname, '__tests__', 'movehub-no-metadata.zip')
37+
);
38+
try {
39+
await FirmwareReader.load(file);
40+
fail('FirmwareReader.load() should have failed');
41+
} catch (err) {
42+
expect(err.name).toMatch('FirmwareReaderError');
43+
expect(err.code).toBe(FirmwareReaderErrorCode.MissingMetadataJson);
44+
}
45+
});
46+
47+
test('missing main.py', async () => {
48+
var file = await readFile(
49+
path.resolve(__dirname, '__tests__', 'movehub-no-main-py.zip')
50+
);
51+
try {
52+
await FirmwareReader.load(file);
53+
fail('FirmwareReader.load() should have failed');
54+
} catch (err) {
55+
expect(err.name).toMatch('FirmwareReaderError');
56+
expect(err.code).toBe(FirmwareReaderErrorCode.MissingMainPy);
57+
}
58+
});
59+
60+
test('missing ReadMe_OSS.txt', async () => {
61+
var file = await readFile(
62+
path.resolve(__dirname, '__tests__', 'movehub-no-readme-oss.zip')
63+
);
64+
try {
65+
await FirmwareReader.load(file);
66+
fail('FirmwareReader.load() should have failed');
67+
} catch (err) {
68+
expect(err.name).toMatch('FirmwareReaderError');
69+
expect(err.code).toBe(FirmwareReaderErrorCode.MissingReadmeOssTxt);
70+
}
71+
});
72+
73+
test('reading data works', async () => {
74+
var file = await readFile(
75+
path.resolve(__dirname, '__tests__', 'movehub.zip')
76+
);
77+
var reader = await FirmwareReader.load(file);
78+
expect(await reader.readFirmwareBase()).toMatchSnapshot();
79+
expect(await reader.readMetadata()).toMatchSnapshot();
80+
expect(await reader.readMainPy()).toMatchSnapshot();
81+
expect(await reader.readReadMeOss()).toMatchSnapshot();
82+
});

0 commit comments

Comments
 (0)