Skip to content

Commit

Permalink
Merge pull request #98 from particle-iot/feature/cellular-imei
Browse files Browse the repository at this point in the history
Add new APIs to access device via control requests
  • Loading branch information
keeramis authored Jan 26, 2024
2 parents 590d4e7 + efab7e4 commit b17b061
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 63 deletions.
58 changes: 51 additions & 7 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"reinstall": "npm run clean && rm -rf ./node_modules && npm i"
},
"dependencies": {
"@particle/device-os-protobuf": "^2.2.0",
"@particle/device-os-protobuf": "^2.4.2",
"ip-address": "^9.0.5",
"protobufjs": "^6.11.3",
"usb": "^2.11.0"
},
Expand Down
12 changes: 12 additions & 0 deletions src/address-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function convertBufferToMacAddress(buffer) {
if (!buffer) {
return buffer;
}

const bytes = Array.from(buffer);
return bytes.map(byte => byte.toString(16).padStart(2, '0')).join(':');
}

module.exports = {
convertBufferToMacAddress
};
11 changes: 11 additions & 0 deletions src/address-util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const addressUtil = require('./address-util');
const { expect } = require('../test/support');

describe('convertBufferToMacAddress', () => {
it('should convert a buffer to MAC address', () => {
const buffer = Buffer.from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]); // Buffer with MAC address bytes
const expected = '00:11:22:33:44:55';
const result = addressUtil.convertBufferToMacAddress(buffer);
expect(result).to.eql(expected);
});
});
21 changes: 20 additions & 1 deletion src/cellular-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,26 @@ const { globalOptions } = require('./config');
*/
const CellularDevice = base => class extends base {
/**

Check warning on line 13 in src/cellular-device.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v12)

Missing JSDoc return description

Check warning on line 13 in src/cellular-device.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v14)

Missing JSDoc return description

Check warning on line 13 in src/cellular-device.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v16)

Missing JSDoc return description
* Get ICCID of the active SIM card.
* Get ICCID of the active SIM card, and the IMEI of the cell radio.
*
* Supported platforms:
* - Gen 3 (since Device OS 5.8.0)
* - Gen 2 (since Device OS 1.1.0)
*
* @param {Object} [options] Options.
* @param {Number} [options.timeout] Timeout (milliseconds).
* @return {Promise<Object>}
*/
async getCellularInfo({ timeout = globalOptions.requestTimeout } = {}) {
const r = await this.sendRequest(Request.CELLULAR_GET_ICCID, null /* msg */, { timeout });
return {
iccid: r.iccid,
imei: r.imei
};
}

/**

Check warning on line 32 in src/cellular-device.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v12)

Missing JSDoc return description

Check warning on line 32 in src/cellular-device.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v14)

Missing JSDoc return description

Check warning on line 32 in src/cellular-device.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v16)

Missing JSDoc return description
* Get ICCID of the active SIM card
*
* Supported platforms:
* - Gen 3 (since Device OS 0.9.0)
Expand Down
36 changes: 19 additions & 17 deletions src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ const FirmwareModuleStore = fromProtobufEnum(DeviceOSProtobuf.cloudDefinitions.F
* @enum {String}
*/
const FirmwareModuleDisplayNames = {
[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'
[FirmwareModule.INVALID]: 'Invalid',
[FirmwareModule.RESOURCE]: 'Resource',
[FirmwareModule.BOOTLOADER]: 'Bootloader',
[FirmwareModule.MONO_FIRMWARE]: 'Monolithic Firmware',
[FirmwareModule.SYSTEM_PART]: 'System Part',
[FirmwareModule.USER_PART]: 'User Part',
[FirmwareModule.SETTINGS]: 'Settings',
[FirmwareModule.NCP_FIRMWARE]: 'Network Co-processor Firmware',
[FirmwareModule.RADIO_STACK]: 'Radio Stack Module',
[FirmwareModule.ASSET]: 'Asset'
};

/**
Expand Down Expand Up @@ -463,12 +463,12 @@ class Device extends DeviceBase {
*
* @return {Promise<Array>} List of asssets available on the device.
*/
async getAssetInfo() {
async getAssetInfo({ timeout = globalOptions.timeout } = {}) {
if (this.isInDfuMode) {
throw new StateError('Cannot get information when the device is in DFU mode');
}

const assetInfoResponse = await this.sendProtobufRequest('GetAssetInfoRequest', null);
const assetInfoResponse = await this.sendProtobufRequest('GetAssetInfoRequest', null, { timeout });
const available = assetInfoResponse.available.map(asset => {
const { name, size, storageSize } = asset;
const hash = asset.hash.toString('hex');
Expand Down Expand Up @@ -498,26 +498,27 @@ class Device extends DeviceBase {
* - 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')
*
* @param {Number} [options.timeout] Timeout (milliseconds).
* @return {Promise<Array>} List of modules installed into the device and their dependencies
*/
async getFirmwareModuleInfo() {
async getFirmwareModuleInfo({ timeout = globalOptions.requestTimeout } = {}) {
if (this.isInDfuMode) {
throw new StateError('Cannot get information when the device is in DFU mode');
}

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

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

return {
type: FirmwareModuleDeprecated.fromProtobuf(type),
index,
version,
size,
validity,
dependencies: dependencies.map(dependency => {
return {
index: dependency.index,
Expand All @@ -530,7 +531,7 @@ class Device extends DeviceBase {
}

return modules.map(module => {
const { index, type, dependencies, size, version, assetDependencies, maxSize, store } = module;
const { index, type, dependencies, size, version, assetDependencies, maxSize, store, hash } = module;
const failedFlags = module.checkedFlags ^ module.passedFlags;

return {
Expand All @@ -540,6 +541,7 @@ class Device extends DeviceBase {
version,
size,
maxSize,
hash: hash.toString('hex'),
failedFlags,
dependencies: dependencies.map(dependency => {
return {
Expand Down
Loading

0 comments on commit b17b061

Please sign in to comment.