### 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] * [schema][12] * [ProtobufDefinition][13] * [Properties][14] * [ProtobufMessage][15] * [Properties][16] ## encode [src/index.js:16-21][17] Create a valid Buffer of bytes that can be sent to DeviceOS, typically used with "Request" messages. ### Parameters * `protobufMessageNameOrMessage` **([string][18] | [ProtobufMessage][19])** Protobuf message name or actual message object. See getDefinitions() to valid options. * `protobufMessageData` **[Object][20]** 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][21]** A Buffer of bytes representing a valid protobuf message that Device OS can interpret ## decode [src/index.js:46-49][22] Create a JavaScript object by decoding a Buffer representing a protobuf message from DeviceOS; typically used with "Reply" messages" ### Parameters * `protobufMessageNameOrMessage` **([string][18] | [ProtobufMessage][19])** Protobuf message name or actual message object. See getDefinitions() to valid options. * `buffer` **[Buffer][21]** 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][20]** A JavaScript object with properties for each data item declared in the \*.proto file ## getDefinition [src/index.js:55-72][23] ### Parameters * `protobufMessageName` **[string][18]** Protobuf definition from \*.proto files like "GetSerialNumberRequest". To access definitions in a namespace, prefix with "." Returns **[ProtobufDefinition][24]** protobufDefinition An object containing code to encode & decode protobufjs messages from Device OS ## getDefinitions [src/index.js:94-108][25] Returns **[Array][26]** 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:113-121][27] Returns **[Array][26]** valid dot prefixes to getDefinition() arguments (i.e. the "cellular" from "cellular".GetIccidRequest, etc) ## \_pbjsJSON [src/index.js:174-174][28] Parsed JSON object generated via `npm run build:json`; this is how we get the type id associated with a given ctrl request ## schema [src/index.js:180-180][29] All of the interesting auto-generated Javascript objects from `*.proto` files live in this definitions object (which is keyed by protobuf message name) ## ProtobufDefinition [src/index.js:94-108][30] Type: [Object][20] ### Properties * `message` **[Function][31]** protobufjs generated Javascript function that includes encode and decode methods. * `id` **([number][32] | null)** integer request ID of the message for "Request" protobuf definitions, null otherwise. * `replyMessage` **([Function][31] | null)** The corresponding reply message to a given "Request" message, null otherwise. ## ProtobufMessage [src/index.js:94-108][33] This is typedef describing the auto-generated code that pbjs generates Type: [Object][20] ### Properties * `create` **[Function][31]** * `encode` **[Function][31]** * `decode` **[Function][31]** [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]: #schema [13]: #protobufdefinition [14]: #properties [15]: #protobufmessage [16]: #properties-1 [17]: https://github.com/particle-iot/device-os-protobuf/blob/46d44e433e79517315072086c8f6e1266c0e0338/src/index.js#L16-L21 "Source code on GitHub" [18]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String [19]: #protobufmessage [20]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object [21]: https://nodejs.org/api/buffer.html [22]: https://github.com/particle-iot/device-os-protobuf/blob/46d44e433e79517315072086c8f6e1266c0e0338/src/index.js#L46-L49 "Source code on GitHub" [23]: https://github.com/particle-iot/device-os-protobuf/blob/46d44e433e79517315072086c8f6e1266c0e0338/src/index.js#L55-L72 "Source code on GitHub" [24]: #protobufdefinition [25]: https://github.com/particle-iot/device-os-protobuf/blob/46d44e433e79517315072086c8f6e1266c0e0338/src/index.js#L94-L108 "Source code on GitHub" [26]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array [27]: https://github.com/particle-iot/device-os-protobuf/blob/46d44e433e79517315072086c8f6e1266c0e0338/src/index.js#L113-L121 "Source code on GitHub" [28]: https://github.com/particle-iot/device-os-protobuf/blob/46d44e433e79517315072086c8f6e1266c0e0338/src/index.js#L174-L174 "Source code on GitHub" [29]: https://github.com/particle-iot/device-os-protobuf/blob/46d44e433e79517315072086c8f6e1266c0e0338/src/index.js#L180-L180 "Source code on GitHub" [30]: https://github.com/particle-iot/device-os-protobuf/blob/46d44e433e79517315072086c8f6e1266c0e0338/src/index.js#L74-L79 "Source code on GitHub" [31]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function [32]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number [33]: https://github.com/particle-iot/device-os-protobuf/blob/46d44e433e79517315072086c8f6e1266c0e0338/src/index.js#L82-L88 "Source code on GitHub"