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

Device::getAssetInfo() and new format in Device::getFirmwareModuleInfo() #96

Merged
merged 7 commits into from
Nov 29, 2023
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
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ module.exports = {
mocha: true,
worker: true,
serviceworker: true
}
},
ignorePatterns: '/proto'
};
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
"lint:fix": "npm run lint -- --fix",
"coverage": "nyc --include='src/**/*.js' --temp-dir=./tmp/ --check-coverage npm run test:silent",
"build": "npm run build:proto && npm run build:web",
"build:proto": "mkdirp lib && pbjs proto/control/*.proto -t static-module -o lib/pb-message.js --no-beautify --no-comments --no-delimited --no-convert --no-verify",
"build:proto": "mkdirp lib && pbjs proto/control/*.proto proto/cloud/*.proto -t static-module -o lib/pb-message.js --no-beautify --no-comments --no-delimited --no-convert --no-verify",
"build:web": "webpack-cli",
"docs": "documentation build src/** -g -f md -o docs/reference.md",
"clean": "rm -rf ./dist ./lib",
"reinstall": "npm run clean && rm -rf ./node_modules && npm i"
},
"dependencies": {
"@particle/device-os-protobuf": "^1.2.1",
"@particle/device-os-protobuf": "^2.2.0",
"protobufjs": "^6.11.3",
"usb": "^2.11.0"
},
Expand Down
2 changes: 1 addition & 1 deletion proto
Submodule proto updated from 3fe90e to 16f255
125 changes: 111 additions & 14 deletions src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ const proto = require('./protocol');

const DeviceOSProtobuf = require('@particle/device-os-protobuf');

/**
* Firmware module types.
*
* @enum {String}
*/
const FirmwareModule = fromProtobufEnum(DeviceOSProtobuf.definitions.FirmwareModuleType, {
const FirmwareModuleDeprecated = fromProtobufEnum(DeviceOSProtobuf.definitions.FirmwareModuleType, {
/** Bootloader module. */
BOOTLOADER: 'BOOTLOADER',
/** System part module. */
Expand All @@ -30,18 +25,52 @@ const FirmwareModule = fromProtobufEnum(DeviceOSProtobuf.definitions.FirmwareMod
RADIO_STACK: 'RADIO_STACK'
});

/**
* Firmware module types.
*
* @enum {String}
*/
const FirmwareModule = fromProtobufEnum(DeviceOSProtobuf.cloudDefinitions.FirmwareModuleType, {
INVALID: 'INVALID_MODULE',
RESOURCE: 'RESOURCE_MODULE',
BOOTLOADER: 'BOOTLOADER_MODULE',
MONO_FIRMWARE: 'MONO_FIRMWARE_MODULE',
SYSTEM_PART: 'SYSTEM_PART_MODULE',
USER_PART: 'USER_PART_MODULE',
SETTINGS: 'SETTINGS_MODULE',
NCP_FIRMWARE: 'NCP_FIRMWARE_MODULE',
RADIO_STACK: 'RADIO_STACK_MODULE',
ASSET: 'ASSET_MODULE',
});

/**
* Firmware module store.
*
* @enum {String}
*/
const FirmwareModuleStore = fromProtobufEnum(DeviceOSProtobuf.cloudDefinitions.FirmwareModuleStore, {
MAIN: 'MAIN_MODULE_STORE',
FACTORY: 'FACTORY_MODULE_STORE',
BACKUP: 'BACKUP_MODULE_STORE',
SCRATCHPAD: 'SCRATCHPAD_MODULE_STORE',
});

/**
* Firmware module readable names
*
* @enum {String}
*/
const FirmwareModuleDisplayNames = {
[FirmwareModule.BOOTLOADER]: 'Bootloader',
[FirmwareModule.SYSTEM_PART]: 'System Part',
[FirmwareModule.USER_PART]: 'User Part',
[FirmwareModule.MONO_FIRMWARE]: 'Monolithic Firmware',
[FirmwareModule.NCP_FIRMWARE]: 'Network Co-processor Firmware',
[FirmwareModule.RADIO_STACK]: 'Radio Stack Module'
[FirmwareModule.INVALID_MODULE]: 'Invalid',
[FirmwareModule.RESOURCE_MODULE]: 'Resource',
[FirmwareModule.BOOTLOADER_MODULE]: 'Bootloader',
[FirmwareModule.MONO_FIRMWARE_MODULE]: 'Monolithic Firmware',
[FirmwareModule.SYSTEM_PART_MODULE]: 'System Part',
[FirmwareModule.USER_PART_MODULE]: 'User Part',
[FirmwareModule.SETTINGS_MODULE]: 'Settings',
[FirmwareModule.NCP_FIRMWARE_MODULE]: 'Network Co-processor Firmware',
[FirmwareModule.RADIO_STACK_MODULE]: 'Radio Stack Module',
[FirmwareModule.ASSET_MODULE]: 'Asset'
};

/**
Expand Down Expand Up @@ -425,13 +454,50 @@ class Device extends DeviceBase {
});
}

/**
* Get asset info.
*
*
* Supported platforms:
* - Gen 3+ (since Device OS 5.6.0)
*
* @return {Promise<Array>} List of asssets available on the device.
*/
async getAssetInfo() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keeramis can you add a test for this method?

if (this.isInDfuMode) {
throw new StateError('Cannot get information when the device is in DFU mode');
}

const assetInfoResponse = await this.sendProtobufRequest('GetAssetInfoRequest', null);
const available = assetInfoResponse.available.map(asset => {
const { name, size, storageSize } = asset;
const hash = asset.hash.toString('hex');
return {
name,
hash,
size,
storageSize
};
});
const required = assetInfoResponse.required.map(asset => {
const { name } = asset;
const hash = asset.hash.toString('hex');
return {
name,
hash,
};
});
return { available, required };
}

/**
* Get firmware module info.
*
*
* Supported platforms:
* - Gen 3 (since Device OS 0.9.0)
* - Gen 2 (since Device OS 0.8.0)
* - New format since 5.6.0 (old format in 'modules_deprecated')
*
* @return {Promise<Array>} List of modules installed into the device and their dependencies
*/
Expand All @@ -441,23 +507,53 @@ class Device extends DeviceBase {
}

const moduleInfoResponse = await this.sendProtobufRequest('GetModuleInfoRequest', null);
const { modules } = moduleInfoResponse;
const { modulesDeprecated, modules } = moduleInfoResponse;

if (modulesDeprecated && modulesDeprecated.length > 0) {
return modulesDeprecated.map(module => {
const { index, type, dependencies, size, version } = module;

return {
type: FirmwareModuleDeprecated.fromProtobuf(type),
index,
version,
size,
dependencies: dependencies.map(dependency => {
return {
index: dependency.index,
version: dependency.version,
type: FirmwareModuleDeprecated.fromProtobuf(dependency.type)
};
}),
};
});
}

return modules.map(module => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keeramis can you add a test for this new branch of the method?

const { index, type, dependencies, size, version } = module;
const { index, type, dependencies, size, version, assetDependencies, maxSize, store } = module;
const failedFlags = module.checkedFlags ^ module.passedFlags;

return {
type: FirmwareModule.fromProtobuf(type),
store: FirmwareModuleStore.fromProtobuf(store),
index,
version,
size,
maxSize,
failedFlags,
dependencies: dependencies.map(dependency => {
return {
index: dependency.index,
version: dependency.version,
type: FirmwareModule.fromProtobuf(dependency.type)
};
}),
assetDependencies: assetDependencies.map(asset => {
return {
name: asset.name,
hash: asset.hash.toString('hex')
};
})
};
});
}
Expand Down Expand Up @@ -982,6 +1078,7 @@ class Device extends DeviceBase {
module.exports = {
FirmwareModule,
FirmwareModuleDisplayNames,
FirmwareModuleStore,
DeviceMode,
LogLevel,
Device
Expand Down
Loading
Loading