|
20 | 20 | * SOFTWARE. |
21 | 21 | */ |
22 | 22 |
|
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'); |
26 | 26 |
|
27 | | -const data = "hello world"; |
| 27 | +const DATA = 'hello world'; |
28 | 28 |
|
29 | 29 | // 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 | + |
31 | 37 | return new Promise((resolve, reject) => { |
32 | | - try { |
33 | | - device.open(); |
34 | | - } catch (_e) { |
35 | | - resolve(""); |
36 | | - } |
37 | 38 | device.getStringDescriptor(index, (error, buffer) => { |
38 | 39 | 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 | + } |
41 | 45 | }); |
42 | 46 | }); |
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 | +} |
50 | 48 |
|
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); |
54 | 53 |
|
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 | + } |
58 | 57 |
|
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 | +} |
68 | 60 |
|
69 | | -const run = async data => { |
| 61 | +(async () => { |
70 | 62 | 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); |
73 | 65 | 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); |
76 | 69 |
|
77 | 70 | console.log(deviceHash); |
78 | 71 | console.log(nodeHash); |
79 | | - console.log(deviceHash === nodeHash ? "match" : "mismatch"); |
80 | | - } catch (error) { |
| 72 | + console.log(deviceHash === nodeHash ? 'match' : 'mismatch'); |
| 73 | + } catch(error) { |
81 | 74 | console.error(error.message || error); |
82 | 75 | } |
83 | 76 | process.exit(); |
84 | | -} |
85 | | - |
86 | | -(async () => await run(data))(); |
| 77 | +})(); |
0 commit comments