particle-iot / particle-usb

A library for accessing Particle USB devices
Apache License 2.0
5 stars 1 forks source link

Add support for a progress callback #90

Closed keeramis closed 1 year ago

keeramis commented 1 year ago

Description

This PR adds support for a progress callback that the user may use to log progress during the flashing process.

The following events have been added:

Usage

  1. Checkout this branch
  2. Point the usb module of your test script to the folder path of this branch
    
    const usb = require('/path/to/local/particle-usb');
    const fs = require('fs-extra');
    const { HalModuleParser } = require('binary-version-reader');

async function main() { const devices = await usb.getDevices(); if (devices.length === 0) { throw new Error('No devices found'); } const device = devices[0]; console.log('Device : ', device); await device.open(); await device.enterDfuMode();

const buf = await fs.readFile('/path/to/binaries/5.4.0/p2/p2-system-part1@5.4.0.bin'); const reader = new HalModuleParser(); const parsed = await reader.parseBuffer({ fileBuffer: buf }); const addr = parseInt(parsed.prefixInfo.moduleStartAddy, 16); await dev.writeOverDfu({ altSetting: 0, buffer: parsed.fileBuffer, addr: addr, options: {}, progress: this.progressCb });

const bufTinker = await fs.readFile('/path/to/binaries/5.4.0/p2/p2-tinker@5.4.0.bin'); const parsedTinker = await reader.parseBuffer({ fileBuffer: bufTinker }); const addrTinker = parseInt(parsedTinker.prefixInfo.moduleStartAddy, 16); await dev.writeOverDfu({ altSetting: 0, buffer: parsedTinker.fileBuffer, addr: addrTinker, options: {}, progress: this.progressCb });

await device.reset(); }

main().catch((error) => { console.error('Error:', error.message); });


This is the progressCb function, for example

progressCb({event: event, bytes: bytes}) { console.log('Event: ' + event + ' Bytes: ' + bytes); }


### Example from a P2 in DFU

Event: start-erase Bytes: 1011712 // Started erasing System-part1 over DFU Event: erased Bytes: 4096 Event: erased Bytes: 4096 Event: erased Bytes: 4096 . . Event: erased Bytes: 4096 Event: erased Bytes: 4096 Event: start-download Bytes: 1009100 // Started downloading System-part1 over DFU Event: downloaded Bytes: 4096 Event: downloaded Bytes: 4096 . . Event: downloaded Bytes: 4096 Event: downloaded Bytes: 4096 Event: downloaded Bytes: 4096 Event: downloaded Bytes: 1484 Event: complete-download Bytes: 1009100 Event: start-erase Bytes: 49152 // Started erasing Tinker over DFU Event: erased Bytes: 4096 Event: erased Bytes: 4096 . . Event: erased Bytes: 4096 Event: erased Bytes: 4096 Event: start-download Bytes: 49152 // Started download Tinker over DFU Event: downloaded Bytes: 4096 Event: downloaded Bytes: 4096 . . Event: downloaded Bytes: 4096 Event: downloaded Bytes: 4096 Event: complete-download Bytes: 49152


### Example from a tracker over control requests

Calling update firmware on tracker-bootloader@4.1.0.bin Event: start-erase Bytes: 46132 Event: erased Bytes: 46132 Event: start-download Bytes: 46132 Event: downloaded Bytes: 1024 Event: downloaded Bytes: 1024 . . Event: downloaded Bytes: 1024 Event: downloaded Bytes: 52 Event: complete-download Bytes: 46132

Calling update firmware on tracker-esp32-ncp@5.3.0.bin Event: start-erase Bytes: 864464 Event: erased Bytes: 864464 Event: start-download Bytes: 864464 Event: downloaded Bytes: 1024 Event: downloaded Bytes: 1024 . . Event: downloaded Bytes: 1024 Event: downloaded Bytes: 208 Event: complete-download Bytes: 864464 ! System firmware update successfully completed!

Your device should now restart automatically.

Process finished with exit code 0