Skip to content

Commit b5a52e2

Browse files
Merge pull request csg-tokyo#94 from maejima-fumika/bugfix/load-protocol
Fix the bug in the loading protocol in the run command.
2 parents 539b54e + 1f621ae commit b5a52e2

File tree

4 files changed

+99
-8
lines changed

4 files changed

+99
-8
lines changed

cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"publishConfig": {
1212
"access": "public"
1313
},
14-
"keywords": ["BlueScript", "TypeScript", "Microcontroller", "MCU","ESP32"],
14+
"keywords": ["BlueScript", "TypeScript", "Microcontroller", "MCU", "IoT", "Robotics", "ESP32"],
1515
"author": "Fumika Mochizuki <fumika.maejima@csg.ci.i.u-tokyo.ac.jp>",
1616
"description": "A CLI for BlueScript",
1717
"repository": {

cli/src/services/device-protocol.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class ProtocolPacketBuilder {
3434
private lastUnitRemain: number;
3535

3636
constructor(unitSize: number, useFirstHeader = true) {
37+
this.checkUnitSize(unitSize);
3738
if (useFirstHeader) {
3839
this.unitSize = unitSize - FIRST_HEADER_SIZE;
3940
this.lastUnit = Buffer.from(FIRST_HEADER);
@@ -44,6 +45,12 @@ export class ProtocolPacketBuilder {
4445
this.lastUnitRemain = this.unitSize;
4546
}
4647

48+
private checkUnitSize(size: number) {
49+
if (size < 15) {
50+
throw new Error('Unit size is smaller than minimum unit size.');
51+
}
52+
}
53+
4754
public build(): Buffer[] {
4855
if (this.lastUnit.length > FIRST_HEADER_SIZE) {
4956
this.units.push(this.lastUnit);
@@ -64,11 +71,12 @@ export class ProtocolPacketBuilder {
6471

6572
while (dataOffset < data.length) {
6673
const dataRemain = data.length - dataOffset;
67-
const writtenBytes = this.loadChunk(currentAddress, data.subarray(dataOffset), dataRemain);
74+
let writtenBytes = this.loadChunk(currentAddress, data.subarray(dataOffset), dataRemain);
6875

6976
if (writtenBytes <= 0) {
7077
this.flushUnit();
71-
if (this.loadChunk(currentAddress, data.subarray(dataOffset), dataRemain) <= 0) {
78+
writtenBytes = this.loadChunk(currentAddress, data.subarray(dataOffset), dataRemain);
79+
if (writtenBytes === 0) {
7280
throw new Error("Failed to make progress in load method. Check data and unit sizes.");
7381
}
7482
}
@@ -93,7 +101,7 @@ export class ProtocolPacketBuilder {
93101
return 0;
94102
}
95103

96-
const header = this.createLoadHeader(Protocol.Load, address, chunkSize);
104+
const header = this.createLoadHeader(address, chunkSize);
97105
const chunk = data.subarray(0, chunkSize);
98106

99107
this.appendToCurrentUnit(header);
@@ -106,9 +114,9 @@ export class ProtocolPacketBuilder {
106114
return value & ~(alignment - 1);
107115
}
108116

109-
private createLoadHeader(loadCmd: number, address: number, size: number) {
117+
private createLoadHeader(address: number, size: number) {
110118
const header = Buffer.allocUnsafe(LOAD_HEADER_SIZE);
111-
header.writeUInt8(loadCmd, 0); // cmd(1)
119+
header.writeUInt8(Protocol.Load, 0); // cmd(1)
112120
header.writeUInt32LE(address, 1); // address(4)
113121
header.writeUInt32LE(size, 5); // size(4)
114122
return header;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { ProtocolPacketBuilder, Protocol } from '../../src/services/device-protocol'
2+
3+
4+
const BUFFER_SIZE = 17;
5+
6+
describe('ProtocolPacketBuilder', () => {
7+
test('should add jump command', () => {
8+
const builder = new ProtocolPacketBuilder(BUFFER_SIZE);
9+
builder.jump(1, 0x1234);
10+
const expectedBuffer = Buffer.from([
11+
0x03, 0x00, // First Header
12+
Protocol.Jump,
13+
0x01, 0x00, 0x00, 0x00, // id
14+
0x34, 0x12, 0x00, 0x00, // address
15+
]);
16+
expect(builder.build()).toEqual([expectedBuffer]);
17+
});
18+
19+
test('should add reset command', () => {
20+
const builder = new ProtocolPacketBuilder(BUFFER_SIZE);
21+
builder.reset();
22+
const expectedBuffer = Buffer.from([
23+
0x03, 0x00, // First Header
24+
Protocol.Reset,
25+
]);
26+
expect(builder.build()).toEqual([expectedBuffer]);
27+
});
28+
29+
test('should add short load command', () => {
30+
const builder = new ProtocolPacketBuilder(BUFFER_SIZE);
31+
builder.load(0x1234, Buffer.from([0x00, 0x01, 0x02, 0x03]));
32+
const expectedBuffer = Buffer.from([
33+
0x03, 0x00, // First Header
34+
Protocol.Load,
35+
0x34, 0x12, 0x00, 0x00, // address
36+
0x04, 0x00, 0x00, 0x00, // size
37+
0x00, 0x01, 0x02, 0x03, // data
38+
]);
39+
expect(builder.build()).toEqual([expectedBuffer]);
40+
});
41+
42+
test('should add long load command', () => {
43+
const builder = new ProtocolPacketBuilder(BUFFER_SIZE);
44+
builder.load(0x1234, Buffer.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06]));
45+
const expectedBuffer1 = Buffer.from([
46+
0x03, 0x00, // First Header
47+
Protocol.Load,
48+
0x34, 0x12, 0x00, 0x00, // address
49+
0x04, 0x00, 0x00, 0x00, // size
50+
0x00, 0x01, 0x02, 0x03, // data
51+
]);
52+
const expectedBuffer2 = Buffer.from([
53+
0x03, 0x00, // First Header
54+
Protocol.Load,
55+
0x38, 0x12, 0x00, 0x00, // address
56+
0x03, 0x00, 0x00, 0x00, // size
57+
0x04, 0x05, 0x06, // data
58+
]);
59+
expect(builder.build()).toEqual([expectedBuffer1, expectedBuffer2]);
60+
});
61+
62+
test('should add reset command after full load command', () => {
63+
const builder = new ProtocolPacketBuilder(BUFFER_SIZE);
64+
builder.load(0x1234, Buffer.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06]));
65+
builder.reset();
66+
const expectedBuffer1 = Buffer.from([
67+
0x03, 0x00, // First Header
68+
Protocol.Load,
69+
0x34, 0x12, 0x00, 0x00, // address
70+
0x04, 0x00, 0x00, 0x00, // size
71+
0x00, 0x01, 0x02, 0x03, // data
72+
]);
73+
const expectedBuffer2 = Buffer.from([
74+
0x03, 0x00, // First Header
75+
Protocol.Load,
76+
0x38, 0x12, 0x00, 0x00, // address
77+
0x03, 0x00, 0x00, 0x00, // size
78+
0x04, 0x05, 0x06, // data
79+
Protocol.Reset,
80+
]);
81+
expect(builder.build()).toEqual([expectedBuffer1, expectedBuffer2]);
82+
})
83+
})

lang/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"publishConfig": {
1313
"access": "public"
1414
},
15-
"keywords": ["BlueScript", "TypeScript", "Microcontroller", "MCU","ESP32"],
16-
"description": "A CLI for BlueScript",
15+
"keywords": ["BlueScript", "TypeScript", "Microcontroller", "MCU", "IoT", "Robotics", "ESP32"],
16+
"description": "A BlueScript Compiler",
1717
"repository": {
1818
"type": "git",
1919
"url": "git+https://github.com/csg-tokyo/bluescript.git"

0 commit comments

Comments
 (0)