Skip to content

Introduce low level way to interact directly with Device OS control requests over USB #56

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

Merged
merged 12 commits into from
Jan 24, 2022

Conversation

joegoggins
Copy link
Contributor

@joegoggins joegoggins commented Jan 19, 2022

Overview

This PR introduces the ability to interact with Device OS via USB control requests even if the there isn't a corresponding high level method in particle-usb. For example, Device OS exposes a wifi.JoinNewNetworkRequest protobuf message, but it cannot be used because there is no corresponding high level method like joinNewWifiNetwork() implemented yet on particle-usb. In this situation, with this code, you can interact directly via the new sendProtobufRequest if need to.

Change summary

  • device.sendProtobufRequest() introduced
  • getSerialNumber implementation uses sendProtobufRequest under the hood. no longer depends on sendRequest
  • Test coverage for getSerialNumber and different mocking strategy that will allow us to add test coverage for all other high level methods like it. (device.js test coverage goes from 11% to 15%)
  • Utilizes the newly shipped [email protected] npm module; see release. this will allow elimination of the ctrl request id to message names that request.js implements and the git submodule to device-os-protobuf

See sc-95109 for more context/details.

Script used to validate photon, p1, boron enterListeningMode still works

const particleUSB = require('./src/particle-usb');
async function main() {
	const devices = await particleUSB.getDevices();
	const device = devices[0];
	await device.open();

	const highLevelReply = await device.enterListeningMode();
	console.log('highLevelReply', highLevelReply);

	// const lowLevelReply = await device.sendProtobufRequest('StartListeningModeRequest');
	// console.log('lowLevelReply', lowLevelReply);

	await device.close();
}

main();

out of scope

This PR is deliberately small. Hoping to align, address feedback, merge on this first, then pursue this work in other PRs:

  • high level api for wifi scan + join wifi command sc-95533
  • Refactors to eliminate git submodule to firmware-protobuf, etc sc-95947

@joegoggins joegoggins changed the title Sc 95109/enumerable ctrl request types Introduce low level way to interact directly with Device OS control requests over USB Jan 21, 2022
@joegoggins joegoggins force-pushed the sc-95109/enumerable-ctrl-request-types branch from 9c6b7fc to 5030e02 Compare January 21, 2022 00:37
@joegoggins joegoggins marked this pull request as ready for review January 21, 2022 00:38
Copy link
Member

@sergeuz sergeuz 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 apart from a minor bug that I noticed, thank you!

r = DeviceOSProtobuf.decode(
protobufDefinition.replyMessage,
rep.data
);
Copy link
Member

Choose a reason for hiding this comment

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

This will overwrite the existing object with the result code when the dontThrow option is used. Same for the other assignment below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right on @sergeuz , thanks for the review.

dontThrow is an undocumented, untested construct that only enterListeningMode uses. (and sendRequest, but we want to eliminate that anyway).

I feel like it'd be better to eliminate it and instead gracefully catch the error there. That approach work for you? If so, I'll add test coverage for enterListeningMode that ensures it works correctly when an error is thrown (and port enterListeningMode to use sendProtobufRequest instead of sendRequest)

Copy link
Member

Choose a reason for hiding this comment

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

That would work too 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sergeuz I eliminated dontThrow option in the last couple of commits. Also managed to get 100% test coverage over enterListeningMode + sendProtobufRequest and changed CI build to be test:ci which includes linter, tests, and code coverage minimums.

Hope this will work for ya, I'll check for your input first thing Monday and merge after if everything is cool and cut a 2.1.0 release

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.

The changes look great!

@@ -742,6 +742,11 @@ function addAssetTracker(options) {
return addDevice(opts);
}

function addP2(options) {
const opts = Object.assign({}, options, { type: deviceConstants.p2.name });
Copy link
Member

Choose a reason for hiding this comment

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

Little JS style thing: you can use the spread operator to combine objects more concisely than with Object.assign. No need to change it here, just sharing a trick.

Suggested change
const opts = Object.assign({}, options, { type: deviceConstants.p2.name });
const opts = { ...options, type: deviceConstants.p2.name };

@joegoggins joegoggins requested a review from sergeuz January 22, 2022 00:35
Copy link
Contributor

@busticated busticated 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 @joegoggins 🙌 👍

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.

4 participants