Skip to content

Commit

Permalink
Obtain device state for dfu devices more accurately
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jul 26, 2024
1 parent d4312f1 commit dc1511c
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/dfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,27 @@ class Dfu {
* @return {Promise} Object with property 'protected'
*/
async getProtectionState() {
// setting 0 is for Internal Flash
await this.setAltSetting(0);

let allSegmentsProtected = true;
this._memoryInfo.segments.forEach(s => {
if (!(s.erasable === true && s.writable === false && s.readable === false)) {
allSegmentsProtected = false;
try {
const res = await this._getStringDescriptor(0xfe);
const state = res.split(';').find(kv => kv.startsWith('s='))?.split('=')[1];

switch (state) {
case 'o': return { protected: false, overridden: false };
case 'p': return { protected: true };
case 's': return { protected: false, overridden: true };
default: throw new Error('Unknown device state');
}
});
// FIXME: Currently, device-os does not reliably distinguish the `overridden` value for different protection modes.
// As a workaround, we use `null` to uniquely indicate the distinction.
return { protected: allSegmentsProtected, overridden: null };
} catch (error) {
// Fallback for devices with Device-OS < 6.1.2
await this.setAltSetting(0); // setting 0 is for Internal Flash

const allSegmentsProtected = this._memoryInfo.segments.every(s =>
s.erasable === true && s.writable === false && s.readable === false
);

// Use `null` for `overridden` to indicate older Device-OS version
return { protected: allSegmentsProtected, overridden: null };
}
}

/**
Expand Down

0 comments on commit dc1511c

Please sign in to comment.