Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accurate device state detection for devices in DFU #115

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/dfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,26 @@ 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(0xfa);
const state = res.split(';').find(kv => kv.startsWith('sm=')).split('=')[1].trim().charAt(0);
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` since we cannot distinguish between Open and Service Mode
return { protected: allSegmentsProtected, overridden: null };
}
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/dfu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,35 @@ describe('dfu', () => {
});

describe('getProtectionState', () => {
it ('detects an Open Device from string desc for Device-OS >= 6.1.2', async () => {
const dfu = new Dfu();
sinon.stub(dfu, '_getStringDescriptor').resolves('sm=o');

const res = await dfu.getProtectionState();
expect(res.protected).to.eql(false);
expect(res.overridden).to.eql(false);
});

it ('detects a Protected Device from string desc for Device-OS >= 6.1.2', async () => {
const dfu = new Dfu();
sinon.stub(dfu, '_getStringDescriptor').resolves('sm=p');

const res = await dfu.getProtectionState();
expect(res.protected).to.eql(true);
});

it ('detects a Protected Device in Service Mode from string desc for Device-OS >= 6.1.2', async () => {
const dfu = new Dfu();
sinon.stub(dfu, '_getStringDescriptor').resolves('sm=s');

const res = await dfu.getProtectionState();
expect(res.protected).to.eql(false);
expect(res.overridden).to.eql(true);
});

it('returns that all segments are protected', async () => {
const dfu = new Dfu();
sinon.stub(dfu, '_getStringDescriptor').rejects('random error');
sinon.stub(dfu, 'setAltSetting').resolves();
const internalFlashDesc = {
'name': 'Internal Flash',
Expand Down Expand Up @@ -409,11 +436,13 @@ describe('dfu', () => {

const res = await dfu.getProtectionState();

expect(dfu.setAltSetting).to.have.been.calledOnce;
expect(res.protected).to.eql(true);
});

it('returns that all segments are not protected', async () => {
const dfu = new Dfu();
sinon.stub(dfu, '_getStringDescriptor').rejects('random error');
sinon.stub(dfu, 'setAltSetting').resolves();
const internalFlashDesc = {
'name': 'Internal Flash',
Expand Down Expand Up @@ -456,6 +485,7 @@ describe('dfu', () => {

const res = await dfu.getProtectionState();

expect(dfu.setAltSetting).to.have.been.calledOnce;
expect(res.protected).to.eql(false);
});
});
Expand Down
Loading