Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed May 14, 2024
1 parent 925e802 commit 345224a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 20 deletions.
2 changes: 0 additions & 2 deletions npm-shrinkwrap.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"particle-api-js": "^10.3.0",
"particle-commands": "^1.0.1",
"particle-library-manager": "^0.1.15",
"particle-usb": "^2.8.0",
"particle-usb": "^2.9.0",
"request": "https://github.com/particle-iot/request/releases/download/v2.75.1-relativepath.1/request-2.75.1-relativepath.1.tgz",
"safe-buffer": "^5.2.0",
"semver": "^7.5.2",
Expand Down
39 changes: 22 additions & 17 deletions src/cmd/usb.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,13 @@ module.exports = class UsbCommand extends CLICommandBase {

await this._forEachUsbDevice(args, usbDevice => {
const platform = platformForId(usbDevice.platformId);
if (platform.generation <= 2) {
output = output.concat(`Device ID: ${chalk.cyan(usbDevice.id)} (${chalk.cyan(platform.displayName)})`);
output = output.concat('\tCannot get network interfaces for Gen 2 devices');
output = output.concat('\n');
return;
}
return this.getNetworkIface(usbDevice)
.then((outputData) => {
return this.getNetworkIfaceInfo(usbDevice)
.then((nwIfaces) => {
const outputData = this._formatNetworkIfaceOutput(nwIfaces, platform.displayName, usbDevice.id);
output = output.concat(outputData);
})
.catch((error) => {
output = output.concat(`Error getting network interfaces (${platform.displayName} / ${usbDevice.id}): ${error.message}\n`);
});
});

Expand All @@ -337,40 +335,47 @@ module.exports = class UsbCommand extends CLICommandBase {
output.forEach((str) => console.log(str));
}

async getNetworkIface(usbDevice) {
const output = [];
const addToOutput = (str) => output.push(str);

addToOutput(`Device ID: ${chalk.cyan(usbDevice.id)} (${chalk.cyan(platformForId(usbDevice.platformId).displayName)})`);
async getNetworkIfaceInfo(usbDevice) {
let nwIfaces = [];
const ifaceList = await usbDevice.getNetworkInterfaceList();
for (const iface of ifaceList) {
const ifaceInfo = await usbDevice.getNetworkInterface({ index: iface.index, timeout: 10000 });
nwIfaces.push(ifaceInfo);
}
return nwIfaces;
}

_formatNetworkIfaceOutput(nwIfaces, platform, deviceId) {
const output = [];
output.push(`Device ID: ${chalk.cyan(deviceId)} (${chalk.cyan(platform)})`);
for (const ifaceInfo of nwIfaces) {
const flagsStr = ifaceInfo.flagsStrings.join(',');
addToOutput(`\t${ifaceInfo.name}(${ifaceInfo.type}): flags=${ifaceInfo.flagsVal}<${flagsStr}> mtu ${ifaceInfo.mtu}`);
output.push(`\t${ifaceInfo.name}(${ifaceInfo.type}): flags=${ifaceInfo.flagsVal}<${flagsStr}> mtu ${ifaceInfo.mtu}`);

// Process IPv4 addresses
if (ifaceInfo?.ipv4Config?.addresses.length > 0) {
for (const address of ifaceInfo.ipv4Config.addresses) {
const [ipv4Address, cidrBits] = address.split('/');
const ipv4NetMask = this._cidrToNetmask(parseInt(cidrBits, 10));
addToOutput(`\t\tinet ${ipv4Address} netmask ${ipv4NetMask}`);
output.push(`\t\tinet ${ipv4Address} netmask ${ipv4NetMask}`);
}
}

// Process IPv6 addresses
if (ifaceInfo?.ipv6Config?.addresses.length > 0) {
for (const address of ifaceInfo.ipv6Config.addresses) {
const [ipv6Address, ipv6Prefix] = address.split('/');
addToOutput(`\t\tinet6 ${ipv6Address} prefixlen ${ipv6Prefix}`);
output.push(`\t\tinet6 ${ipv6Address} prefixlen ${ipv6Prefix}`);
}
}

// Process hardware address
if (ifaceInfo?.hwAddress) {
addToOutput(`\t\tether ${ifaceInfo.hwAddress}`);
output.push(`\t\tether ${ifaceInfo.hwAddress}`);
}
}
return output;
}

};

112 changes: 112 additions & 0 deletions src/cmd/usb.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const { expect } = require('../../test/setup');
const UsbCommands = require('./usb');


describe('USB Commands', () => {
afterEach(() => {
});


describe('_formatNetworkIfaceOutput', () => {
it('formats the interface information to imitate linux `ifconfig` command', async () => {
const nwInfo = [
{
'index': 5,
'name': 'wl4',
'type': 'WIFI',
'hwAddress': '94:94:4a:04:af:80',
'mtu': 1500,
'flagsVal': 98371,
'extFlags': 1114112,
'flagsStrings': ['UP', 'BROADCAST', 'LOWER_UP', 'LOWER_UP', 'MULTICAST', 'NOND6'],
'metric': 0,
'profile': Buffer.alloc(0),
'ipv4Config': {
'addresses': ['10.2.3.4/32'],
'gateway': null,
'peer': null,
'dns': [],
'source': 'NONE'
},
'ipv6Config': {
'addresses': [],
'gateway': null,
'dns': [],
'source': 'NONE'
}
},
{
'index': 4,
'name': 'pp3',
'type': 'PPP',
'hwAddress': '',
'mtu': 1500,
'flagsVal': 81,
'extFlags': 1048576,
'flagsStrings': ['UP', 'POINTOPOINT', 'LOWER_UP', 'LOWER_UP'],
'metric': 0,
'profile': Buffer.alloc(0),
'ipv4Config': {
'addresses': ['10.20.30.40/32'],
'gateway': null,
'peer': null,
'dns': [],
'source': 'NONE'
},
'ipv6Config': {
'addresses': [],
'gateway': null,
'dns': [],
'source': 'NONE'
}
},
{
'index': 1,
'name': 'lo0',
'type': 'LOOPBACK',
'hwAddress': '',
'mtu': 0,
'flagsVal': 73,
'extFlags': 0,
'flagsStrings': ['UP', 'LOOPBACK', 'LOWER_UP', 'LOWER_UP'],
'metric': 0,
'profile': Buffer.alloc(0),
'ipv4Config': {
'addresses': ['10.11.12.13/32'],
'gateway': null,
'peer': null,
'dns': [],
'source': 'NONE'
},
'ipv6Config': {
'addresses': ['0000:0000:0000:0000:0000:0000:0000:0001/64'],
'gateway': null,
'dns': [],
'source': 'NONE'
}
}
];

const expectedOutput = [
'Device ID: \u001b[36m0123456789abcdef\u001b[39m (\u001b[36mp2\u001b[39m)',
'\twl4(WIFI): flags=98371<UP,BROADCAST,LOWER_UP,LOWER_UP,MULTICAST,NOND6> mtu 1500',
'\t\tinet 10.2.3.4 netmask 255.255.255.255',
'\t\tether 94:94:4a:04:af:80',
'\tpp3(PPP): flags=81<UP,POINTOPOINT,LOWER_UP,LOWER_UP> mtu 1500',
'\t\tinet 10.20.30.40 netmask 255.255.255.255',
'\tlo0(LOOPBACK): flags=73<UP,LOOPBACK,LOWER_UP,LOWER_UP> mtu 0',
'\t\tinet 10.11.12.13 netmask 255.255.255.255',
'\t\tinet6 0000:0000:0000:0000:0000:0000:0000:0001 prefixlen 64'
];

const usbCommands = new UsbCommands({
settings: {
access_token: '1234'
},
});
const res = await usbCommands._formatNetworkIfaceOutput(nwInfo, 'p2', '0123456789abcdef');

expect(res).to.eql(expectedOutput);
});
});
});

0 comments on commit 345224a

Please sign in to comment.