Skip to content

Commit 9b7ca49

Browse files
committed
Rework execute examples
1 parent b5b8e90 commit 9b7ca49

File tree

13 files changed

+132
-139
lines changed

13 files changed

+132
-139
lines changed

examples/daplink-flash/hid.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ const getDevices = (vendorID) => {
4444
await common.flash(transport, program);
4545
} catch(error) {
4646
console.error(error.message || error);
47-
} finally {
48-
process.exit();
4947
}
48+
process.exit();
5049
})();

examples/daplink-flash/usb.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ const getDevices = async (vendorID) => {
6565
await common.flash(transport, program);
6666
} catch(error) {
6767
console.error(error.message || error);
68-
} finally {
69-
process.exit();
7068
}
69+
process.exit();
7170
})();

examples/daplink-flash/webusb.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const usb = new USB({ devicesFound });
4545
await common.flash(transport, program);
4646
} catch(error) {
4747
console.error(error.message || error);
48-
} finally {
49-
process.exit();
5048
}
49+
process.exit();
5150
})();

examples/daplink-serial/hid.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const getDevices = (vendorID) => {
4343
await common.listen(transport);
4444
} catch(error) {
4545
console.error(error.message || error);
46-
} finally {
47-
process.exit();
4846
}
47+
process.exit();
4948
})();

examples/daplink-serial/usb.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ const getDevices = async (vendorID) => {
6464
await common.listen(transport);
6565
} catch(error) {
6666
console.error(error.message || error);
67-
} finally {
68-
process.exit();
6967
}
68+
process.exit();
7069
})();

examples/daplink-serial/webusb.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ const usb = new USB({ devicesFound });
4444
await common.listen(transport);
4545
} catch(error) {
4646
console.error(error.message || error);
47-
} finally {
48-
process.exit();
4947
}
48+
process.exit();
5049
})();

examples/execute/common.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,55 @@
2020
* SOFTWARE.
2121
*/
2222

23-
const EventEmitter = require("events");
24-
const crypto = require("crypto");
25-
const DAPjs = require("../../");
23+
const crypto = require('crypto');
24+
const DAPjs = require('../../');
2625

27-
// Emit keyboard input
28-
const inputEmitter = new EventEmitter();
29-
function setupEmitter() {
30-
process.stdin.setRawMode(true);
31-
process.stdin.setEncoding("utf8");
32-
process.stdin.on("readable", () => {
33-
let input;
34-
while (input = process.stdin.read()) {
35-
if (input === "\u0003") {
36-
process.exit();
37-
} else if (input !== null) {
38-
let index = parseInt(input);
39-
inputEmitter.emit("input", index);
26+
// Handle single character input from the user
27+
const readHandler = (inputHandler, stream = process.stdin) => {
28+
return new Promise(resolve => {
29+
stream.setRawMode(true);
30+
stream.setEncoding('utf8');
31+
32+
const onResolve = result => {
33+
stream.off('readable', read);
34+
stream.setRawMode(false);
35+
resolve(result);
36+
}
37+
38+
const read = () => {
39+
let input;
40+
while (input = stream.read()) {
41+
inputHandler(input, onResolve);
42+
}
43+
}
44+
45+
stream.on('readable', read);
46+
});
47+
}
48+
49+
// Select a device from the list
50+
const selectDevice = async (devices) => {
51+
if (devices.length === 0) {
52+
throw new Error('No devices found');
53+
}
54+
55+
console.log('Select a device to execute on:');
56+
devices.forEach((device, index) => {
57+
console.log(`${index + 1}: ${device.name}`);
58+
});
59+
60+
const device = await readHandler((input, resolve) => {
61+
if (input === '\u0003') {
62+
process.exit();
63+
} else if (input !== null) {
64+
let index = parseInt(input);
65+
if (index <= devices.length) {
66+
resolve(devices[index - 1]);
4067
}
4168
}
4269
});
70+
71+
return device;
4372
}
4473

4574
const nodeHash = data => {
@@ -123,8 +152,8 @@ const calculateHash = async (processor, data) => {
123152
};
124153

125154
module.exports = {
126-
inputEmitter,
127-
setupEmitter,
155+
DAPLINK_VENDOR: 0xD28,
156+
selectDevice,
128157
nodeHash,
129158
deviceHash
130159
};

examples/execute/hid.js

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,37 @@
2020
* SOFTWARE.
2121
*/
2222

23-
const hid = require("node-hid");
24-
const DAPjs = require("../../");
25-
const common = require("./common");
23+
const hid = require('node-hid');
24+
const DAPjs = require('../../');
25+
const common = require('./common');
2626

27-
const data = "hello world";
27+
const DATA = 'hello world';
2828

29-
// Allow user to select a device
30-
const selectDevice = vendorID => {
31-
return new Promise((resolve, reject) => {
32-
let devices = hid.devices();
33-
devices = devices.filter(device => device.vendorId === vendorID);
29+
// List all devices
30+
const getDevices = (vendorID) => {
31+
let devices = hid.devices();
32+
devices = devices.filter(device => device.vendorId === vendorID);
3433

35-
if (devices.length === 0) {
36-
return reject("No devices found");
37-
}
38-
39-
common.inputEmitter.addListener("input", index => {
40-
if (index <= devices.length) resolve(devices[index - 1]);
41-
});
42-
43-
console.log("Select a device to execute on:");
44-
devices.forEach((device, index) => {
45-
console.log(`${index + 1}: ${device.product}`);
46-
});
47-
});
34+
return devices.map(device => ({
35+
...device,
36+
name: device.product
37+
}));
4838
}
4939

50-
const run = async data => {
40+
(async () => {
5141
try {
52-
common.setupEmitter();
53-
const device = await selectDevice(0xD28);
42+
const devices = getDevices(common.DAPLINK_VENDOR);
43+
const device = await common.selectDevice(devices);
5444
const transport = new DAPjs.HID(device);
55-
const deviceHash = await common.deviceHash(transport, data);
56-
const nodeHash = common.nodeHash(data);
45+
46+
const deviceHash = await common.deviceHash(transport, DATA);
47+
const nodeHash = common.nodeHash(DATA);
5748

5849
console.log(deviceHash);
5950
console.log(nodeHash);
60-
console.log(deviceHash === nodeHash ? "match" : "mismatch");
61-
} catch (error) {
51+
console.log(deviceHash === nodeHash ? 'match' : 'mismatch');
52+
} catch(error) {
6253
console.error(error.message || error);
6354
}
6455
process.exit();
65-
}
66-
67-
(async () => await run(data))();
56+
})();

examples/execute/usb.js

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,58 @@
2020
* SOFTWARE.
2121
*/
2222

23-
const usb = require("usb");
24-
const DAPjs = require("../../");
25-
const common = require("./common");
23+
const usb = require('usb');
24+
const DAPjs = require('../../');
25+
const common = require('./common');
2626

27-
const data = "hello world";
27+
const DATA = 'hello world';
2828

2929
// Read USB device descriptor
30-
const getStringDescriptor = (device, index) => {
30+
const getStringDescriptor = async (device, index) => {
31+
try {
32+
device.open();
33+
} catch (_e) {
34+
return '';
35+
}
36+
3137
return new Promise((resolve, reject) => {
32-
try {
33-
device.open();
34-
} catch (_e) {
35-
resolve("");
36-
}
3738
device.getStringDescriptor(index, (error, buffer) => {
3839
device.close();
39-
if (error) return reject(error);
40-
resolve(buffer.toString());
40+
if (error) {
41+
reject(new Error(error));
42+
} else {
43+
resolve(buffer.toString());
44+
}
4145
});
4246
});
43-
};
44-
45-
// Allow user to select a device
46-
const selectDevice = vendorID => {
47-
return new Promise((resolve, reject) => {
48-
let devices = usb.getDeviceList();
49-
devices = devices.filter(device => device.deviceDescriptor.idVendor === vendorID);
47+
}
5048

51-
if (devices.length === 0) {
52-
return reject("No devices found");
53-
}
49+
// List all devices
50+
const getDevices = async (vendorID) => {
51+
let devices = usb.getDeviceList();
52+
devices = devices.filter(device => device.deviceDescriptor.idVendor === vendorID);
5453

55-
common.inputEmitter.addListener("input", index => {
56-
if (index && index <= devices.length) resolve(devices[index - 1]);
57-
});
54+
for (device of devices) {
55+
device.name = await getStringDescriptor(device, device.deviceDescriptor.iProduct);
56+
}
5857

59-
console.log("Select a device to listen to execute on:");
60-
devices.forEach((device, index) => {
61-
getStringDescriptor(device, device.deviceDescriptor.iProduct)
62-
.then(name => {
63-
console.log(`${index + 1}: ${name}`);
64-
});
65-
});
66-
});
67-
};
58+
return devices;
59+
}
6860

69-
const run = async data => {
61+
(async () => {
7062
try {
71-
common.setupEmitter();
72-
const device = await selectDevice(0xD28);
63+
const devices = await getDevices(common.DAPLINK_VENDOR);
64+
const device = await common.selectDevice(devices);
7365
const transport = new DAPjs.USB(device);
74-
const deviceHash = await common.deviceHash(transport, data);
75-
const nodeHash = common.nodeHash(data);
66+
67+
const deviceHash = await common.deviceHash(transport, DATA);
68+
const nodeHash = common.nodeHash(DATA);
7669

7770
console.log(deviceHash);
7871
console.log(nodeHash);
79-
console.log(deviceHash === nodeHash ? "match" : "mismatch");
80-
} catch (error) {
72+
console.log(deviceHash === nodeHash ? 'match' : 'mismatch');
73+
} catch(error) {
8174
console.error(error.message || error);
8275
}
8376
process.exit();
84-
}
85-
86-
(async () => await run(data))();
77+
})();

examples/execute/webusb.js

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,38 @@
2020
* SOFTWARE.
2121
*/
2222

23-
const USB = require("webusb").USB;
24-
const common = require("./common");
25-
const DAPjs = require("../../");
23+
const USB = require('webusb').USB;
24+
const common = require('./common');
25+
const DAPjs = require('../../');
2626

27-
const data = "hello world";
27+
const DATA = 'hello world';
2828

2929
// Allow user to select a device
30-
function handleDevicesFound(devices, selectFn) {
31-
32-
common.inputEmitter.addListener("input", index => {
33-
if (index && index <= devices.length) selectFn(devices[index - 1]);
34-
});
30+
const devicesFound = async (devices) => {
31+
for (device of devices) {
32+
device.name = device.productName || device.serialNumber;
33+
}
3534

36-
console.log("Select a device to listen to execute on:");
37-
devices.forEach((device, index) => {
38-
console.log(`${index + 1}: ${device.productName || device.serialNumber}`);
39-
});
35+
return common.selectDevice(devices);
4036
}
4137

42-
const run = async data => {
38+
const usb = new USB({ devicesFound });
39+
40+
(async () => {
4341
try {
44-
common.setupEmitter();
45-
let usb = new USB({
46-
devicesFound: handleDevicesFound
47-
});
4842
const device = await usb.requestDevice({
49-
filters: [{vendorId: 0xD28}]
50-
});
43+
filters: [{vendorId: common.DAPLINK_VENDOR}]
44+
})
5145
const transport = new DAPjs.WebUSB(device);
52-
const deviceHash = await common.deviceHash(transport, data);
53-
const nodeHash = common.nodeHash(data);
46+
47+
const deviceHash = await common.deviceHash(transport, DATA);
48+
const nodeHash = common.nodeHash(DATA);
5449

5550
console.log(deviceHash);
5651
console.log(nodeHash);
57-
console.log(deviceHash === nodeHash ? "match" : "mismatch");
58-
} catch (error) {
52+
console.log(deviceHash === nodeHash ? 'match' : 'mismatch');
53+
} catch(error) {
5954
console.error(error.message || error);
6055
}
6156
process.exit();
62-
}
63-
64-
(async () => await run(data))();
57+
})();

0 commit comments

Comments
 (0)