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

Get network configuration of a device #733

Merged
merged 7 commits into from
May 15, 2024
Merged

Conversation

keeramis
Copy link
Contributor

@keeramis keeramis commented May 14, 2024

Description

Implements a command which resembles the linux ifconfig command that shows information about the network interfaces and their states

This is only possible on Gen2 because the USB control requests to request this information are guarded by HAL_PLATFORM_IFAPI which is enabled only for Gen3 and above.

How to Test

Check out this branch and from the particle-cli root directory, run npm start -- usb network-interfaces

  1. Connect a single device
  2. Or Connect muitple devices

Expected Outcome

Screenshot 2024-05-14 at 12 27 31 PM

Related Issues / Discussions

particle-iot/particle-usb#102

Completeness

  • User is totes amazing for contributing!
  • Contributor has signed CLA
  • Problem and solution clearly stated
  • Tests have been provided
  • Docs have been updated
  • CI is passing

Copy link
Member

@monkbroc monkbroc left a comment

Choose a reason for hiding this comment

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

Looks good overall. I'd like to see unit tests for the formatting logic.

src/cmd/usb.js Outdated
Comment on lines 321 to 329
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) => {
output = output.concat(outputData);
});
Copy link
Member

Choose a reason for hiding this comment

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

Always prefer feature detection over version checks if possible. What does a Gen 2 device or maybe even a Gen 3 device without support for the NetworkIface control request do? If it errors out quickly with a predictable error code (NOT_SUPPORTED or something), then it's best to catch the error and say "Getting network interfaces is not supported for this device". If the error takes a while or it does something else weird, it's ok to fall back to checking the version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines +345 to +342
const ifaceList = await usbDevice.getNetworkInterfaceList();
for (const iface of ifaceList) {
const ifaceInfo = await usbDevice.getNetworkInterface({ index: iface.index, timeout: 10000 });
Copy link
Member

Choose a reason for hiding this comment

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

I suggest separating the code that does the control requests from the code that formats the data. Then write one or a couple of unit tests passing data the data structure that you get from the control request to the formatting function to check that all the output logic works as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done also!

@keeramis keeramis force-pushed the feature/get-network-ifaces branch from 2a7ee0e to 345224a Compare May 14, 2024 20:57
@keeramis
Copy link
Contributor Author

@monkbroc updated the new output format when there is an error.

@keeramis keeramis requested a review from monkbroc May 14, 2024 21:11
Copy link
Member

@monkbroc monkbroc left a comment

Choose a reason for hiding this comment

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

Nice work!

access_token: '1234'
},
});
const res = await usbCommands._formatNetworkIfaceOutput(nwInfo, 'p2', '0123456789abcdef');
Copy link
Member

Choose a reason for hiding this comment

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

Nice test! The test doesn't need to be async and this line doesn't need await since _formatNetworkIfaceOutput is not async.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yessss

@keeramis keeramis force-pushed the feature/get-network-ifaces branch from c585f60 to b919cf5 Compare May 15, 2024 20:04
@keeramis keeramis merged commit 9477747 into master May 15, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants