Skip to content

Introduce deviceOSProtobuf.getDefinition(name) to device-os-protobuf #10

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 41 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
fa3bc77
Adds .nvmrc to node 12
Jan 16, 2022
c99b810
Adds devDependencies: mocha chai sinon eslint eslint-config-particle,…
Jan 16, 2022
da4771e
Introduces npm run test, lint, and lint:fix (mocha + eslint based)
Jan 16, 2022
ebd14d7
npm run build + npm run build:json generate code that will be committ…
Jan 16, 2022
df42a2d
Introduce particle specific eslint config
Jan 16, 2022
b311629
Add pbjs generated json and javascript
Jan 16, 2022
6f3884b
Introduce getDefinition('SERIAL_NUMBER')
Jan 16, 2022
57129e2
Add CircleCI badge
Jan 16, 2022
f45bbed
Adds code coverage via nyc `npm run coverage` script
Jan 18, 2022
b58e9de
Refactors getDefinition to take the protobuf message name ("GetSerial…
Jan 18, 2022
17b14d3
Remove lodash, no longer needed
Jan 19, 2022
75cb880
Ignore pbjs-generated/index.js from eslinter
Jan 19, 2022
e71d656
Implement getDefinitions()
Jan 19, 2022
64e33db
Adds code coverage minimums (enforced by `npm run coverage`)
Jan 19, 2022
b8cf4f2
Introduce npm run test:ci (linter, test, coverage tasks are green)
Jan 19, 2022
6537c20
Use `npm run test:ci` in CircleCI
Jan 19, 2022
c3ae4dd
getDefinition() namespace's now supported
Jan 19, 2022
8d11063
Adds `npm run build:docs`
Jan 19, 2022
99c6aad
Update getDefinition jsdoc to reflect where we are headed.
Jan 19, 2022
db8dad4
Update README/docs; provide broader technical context
Jan 19, 2022
b914bd4
getDefinition now supports replyMessage key
Jan 19, 2022
bd9de9b
Update docs
Jan 19, 2022
c620bff
Improve the docs
Jan 19, 2022
5c31453
Update readme and add placeholder test case for request/reply decoding
Jan 19, 2022
4ba5b7b
Update the documentation
Jan 19, 2022
5381713
Moar readme updates
Jan 19, 2022
2752464
1.1.0-0
Jan 19, 2022
c339497
Bugfix: Change package.json main to point at correct index.js
Jan 19, 2022
2d7bc2e
1.1.0-1
Jan 19, 2022
5c61875
Eliminate fs + path dependencies to load pbjs json file
Jan 19, 2022
881d054
Adds test coverage for usage of create/encode of replyMessage
Jan 19, 2022
7a184ff
Simplify number of steps in build process to 1 command: `npm run build`
Jan 19, 2022
0a47605
Rename pbjs json to definitions.json to avoid confusion
Jan 19, 2022
15aa18f
1.1.0-2
Jan 19, 2022
1f7266d
Introduce encode + decode functions
Jan 20, 2022
bfb31a6
Update docs to include encode + decode
Jan 20, 2022
507a924
Typo fix
Jan 20, 2022
8303d38
1.1.0-3
Jan 20, 2022
985fc87
encode/decode functions now support passing a protobuf message with c…
Jan 20, 2022
84d18d4
Update the documentation
Jan 20, 2022
9726381
1.1.0-4
Jan 20, 2022
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
command: npm ci
- run:
name: Run tests
command: npm run test
command: npm run test:ci
- persist_to_workspace:
root: ~/repo
paths: .
Expand Down
16 changes: 16 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
extends: ['eslint-config-particle'],
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module'
},
env: {
commonjs: true,
es6: true,
node: true,
mocha: true
},
rules: {
'max-statements': ['warn', 50]
}
};
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
# Node
node_modules

# Automated tests
.nyc_output

# Generated files
dist/


1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12
6 changes: 6 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"branches": 100,
"lines": 100,
"functions": 90,
"statements":100
}
47 changes: 15 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,36 @@
# Particle Device OS Protobuf Definitions

Particle devices expose Control Requests over USB and BLE for functionality like device information and Wi-Fi setup. This repo contains the definition for all these control requests in Protobuf format.

<!--
## Documentation

Goal would be to have auto-generated documentation in docs.particle.io
[![CircleCI](https://circleci.com/gh/particle-iot/device-os-protobuf/tree/main.svg?style=svg)](https://circleci.com/gh/particle-iot/device-os-protobuf/tree/main)

```
npm run build
npm run docs
```
Then parse dist/index-docs.json in the Particle docs build to generate a page describing the control requests
# Particle Device OS Protobuf Definitions

Particle devices expose Control Requests over USB and BLE for functionality like device information and Wi-Fi setup. This repo contains the definition for all these control requests in Protobuf format.

Auto-generated documentation can be found at https://docs.particle.io/reference/device-os/control-requests/
Note that most common Javascript use cases Device OS Protobuf usage over USB would be better accommodated by [particle-usb](https://github.com/particle-iot/particle-usb) rather than this project because it provides a higher level API that is easier to use and covers protobuf transmission over USB (not just encoding/decoding);

-->
Device OS declares all control request IDs in [system-control.h](https://github.com/particle-iot/device-os/blob/develop/system/inc/system_control.h). Additionally, Device OS depends on `control/*.proto` files in this repo via a `git submodule`.

## Node.js
iOS and Android applications also depend on `control/*.proto` files directly via a `git submodule`.

### Usage
## Usage
### Node.js

Install package with `npm install @particle/device-os-protobuf`

See [docs/reference.md](/docs/reference.md) for public api documentation

```
const proto = require('@particle/device-os-protobuf');
const controlProto = proto.particle.ctrl;

// Encode message
const request = controlProto.GetSerialNumberRequest;
const m = request.create({ /* GetSerialNumberRequest fields */ });
const buf = request.encode(m).finish();
See `src/*.test.js` files for basic usage examples.

// Decode response
const response = controlProto.GetSerialNumberReply;
const responseObj = response.decode(data);
```
See `particle-usb` for production usage examples.

### Development

Ensure you have installed dependencies via `npm install`.

Edit the protocol buffer definitions in [`proto`](proto).

Generate the JavaScript files with `npm run build`
Generate the [protobufjs JavaScript & JSON](https://www.npmjs.com/package/protobufjs) files and markdown documentation with `npm run build`

## Release
Ensure `npm run test:ci` is passing

Tag a new version with `npm version` and push `git push --follow-tags`. CircleCI will publish the package to npm.
## Release

Tag a new version with `npm version` push `git push --follow-tags`. CircleCI will publish the package to npm.
170 changes: 170 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

* [encode][1]
* [Parameters][2]
* [Examples][3]
* [decode][4]
* [Parameters][5]
* [Examples][6]
* [getDefinition][7]
* [Parameters][8]
* [getDefinitions][9]
* [getNamespaces][10]
* [\_pbjsJSON][11]
* [\_pbjsObjects][12]
* [ProtobufDefinition][13]
* [Properties][14]

## encode

[src/index.js:15-20][15]

Create a valid Buffer of bytes that can be sent to Device, typically used with "Request" messages.

### Parameters

* `protobufMessageName` **[string][16]** Protobuf message name. See getDefinitions() to valid options.
* `protobufMessageData` **[Object][17]** An object containing key data/code to encode & decode protobufjs messages from Device OS (optional, default `null`)

### Examples

Encoding a request to get serial number

```javascript
// returns a zero length Buffer because there is no properties for this message, just the option type_id
const buffer = DeviceOSProtobuf.encode('GetSerialNumberRequest');
```

Returns **[Buffer][18]** A Buffer of bytes representing a valid protobuf message that Device OS can interpret

## decode

[src/index.js:34-37][19]

Create a JavaScript object by decoding a Buffer representing a protobuf message from DeviceOS; typically used with "Reply" messages"

### Parameters

* `protobufMessageName` **[string][16]** Protobuf message name. See getDefinitions() to valid options.
* `buffer` **[Buffer][18]** Buffer from DeviceOS representing valid non-decoded Protobuf message

### Examples

Decode a GetSerialNumberReply

```javascript
// returns a Javascript object with .serial property
const object = DeviceOSProtobuf.decode('GetSerialNumberReply', buffer);
// shows the serial number as a string
console.log(object.serial);
```

Returns **[Object][17]** A JavaScript object with properties for each data item declared in the \*.proto file

## getDefinition

[src/index.js:43-60][20]

### Parameters

* `protobufMessageName` **[string][16]** Protobuf definition from \*.proto files like "GetSerialNumberRequest". To access definitions in a namespace, prefix with "<namespace>."

Returns **[ProtobufDefinition][21]** protobufDefinition An object containing code to encode & decode protobufjs messages from Device OS

## getDefinitions

[src/index.js:72-86][22]

Returns **[Array][23]** valid strings that can be passed to getDefinition(). Includes all Request/Reply style messages as well as non request messages and enums.

## getNamespaces

[src/index.js:91-99][24]

Returns **[Array][23]** valid dot prefixes to getDefinition() arguments (i.e. the "cellular" from "cellular".GetIccidRequest, etc)

## \_pbjsJSON

[src/index.js:152-152][25]

Parsed JSON object generated via `npm run build:json`; this is how we get the type id associated with
a given ctrl request

## \_pbjsObjects

[src/index.js:158-158][26]

All of the interesting auto-generated Javascript objects from `control/*.proto` files live in this \_pbjsObjects object
(which is keyed by protobuf message name)

## ProtobufDefinition

[src/index.js:72-86][27]

Type: [Object][17]

### Properties

* `message` **[Function][28]** protobufjs generated Javascript function that includes encode and decode methods.
* `id` **([number][29] | null)** integer request ID of the message for "Request" protobuf definitions, null otherwise.
* `replyMessage` **([Function][28] | null)** The corresponding reply message to a given "Request" message, null otherwise.

[1]: #encode

[2]: #parameters

[3]: #examples

[4]: #decode

[5]: #parameters-1

[6]: #examples-1

[7]: #getdefinition

[8]: #parameters-2

[9]: #getdefinitions

[10]: #getnamespaces

[11]: #_pbjsjson

[12]: #_pbjsobjects

[13]: #protobufdefinition

[14]: #properties

[15]: https://github.com/particle-iot/device-os-protobuf/blob/1f7266d03f4f188fcb8f08ac910b17451eddff21/src/index.js#L15-L20 "Source code on GitHub"

[16]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[17]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[18]: https://nodejs.org/api/buffer.html

[19]: https://github.com/particle-iot/device-os-protobuf/blob/1f7266d03f4f188fcb8f08ac910b17451eddff21/src/index.js#L34-L37 "Source code on GitHub"

[20]: https://github.com/particle-iot/device-os-protobuf/blob/1f7266d03f4f188fcb8f08ac910b17451eddff21/src/index.js#L43-L60 "Source code on GitHub"

[21]: #protobufdefinition

[22]: https://github.com/particle-iot/device-os-protobuf/blob/1f7266d03f4f188fcb8f08ac910b17451eddff21/src/index.js#L72-L86 "Source code on GitHub"

[23]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

[24]: https://github.com/particle-iot/device-os-protobuf/blob/1f7266d03f4f188fcb8f08ac910b17451eddff21/src/index.js#L91-L99 "Source code on GitHub"

[25]: https://github.com/particle-iot/device-os-protobuf/blob/1f7266d03f4f188fcb8f08ac910b17451eddff21/src/index.js#L152-L152 "Source code on GitHub"

[26]: https://github.com/particle-iot/device-os-protobuf/blob/1f7266d03f4f188fcb8f08ac910b17451eddff21/src/index.js#L158-L158 "Source code on GitHub"

[27]: https://github.com/particle-iot/device-os-protobuf/blob/1f7266d03f4f188fcb8f08ac910b17451eddff21/src/index.js#L62-L67 "Source code on GitHub"

[28]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function

[29]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
Loading