* Encodes a payload into a Tuya-protocol-complient packet for protocol version 3.5 * @param {Object} options Options for encoding * @param {Buffer|String|Object} options.data data to encode * @param {Boolean} options.encrypted whether or not to encrypt the data * @param {Number} options.
(options)
| 479 | * @returns {Buffer} Encoded Buffer |
| 480 | */ |
| 481 | _encode35(options) { |
| 482 | let payload = options.data; |
| 483 | |
| 484 | if (options.commandByte !== CommandType.DP_QUERY && |
| 485 | options.commandByte !== CommandType.HEART_BEAT && |
| 486 | options.commandByte !== CommandType.DP_QUERY_NEW && |
| 487 | options.commandByte !== CommandType.SESS_KEY_NEG_START && |
| 488 | options.commandByte !== CommandType.SESS_KEY_NEG_FINISH && |
| 489 | options.commandByte !== CommandType.DP_REFRESH) { |
| 490 | // Add 3.5 header |
| 491 | const buffer = Buffer.alloc(payload.length + 15); |
| 492 | Buffer.from('3.5').copy(buffer, 0); |
| 493 | payload.copy(buffer, 15); |
| 494 | payload = buffer; |
| 495 | // OO options.data = '3.5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + options.data; |
| 496 | } |
| 497 | |
| 498 | // Allocate buffer for prefix, unknown, sequence, command, length |
| 499 | let buffer = Buffer.alloc(18); |
| 500 | |
| 501 | // Add prefix, command, and length |
| 502 | buffer.writeUInt32BE(0x00006699, 0); // Prefix |
| 503 | buffer.writeUInt16BE(0x0, 4); // Unknown |
| 504 | buffer.writeUInt32BE(options.sequenceN, 6); // Sequence |
| 505 | buffer.writeUInt32BE(options.commandByte, 10); // Command |
| 506 | buffer.writeUInt32BE(payload.length + 28 /* 0x1c */, 14); // Length |
| 507 | |
| 508 | const encrypted = this.cipher.encrypt({ |
| 509 | data: payload, |
| 510 | aad: buffer.slice(4, 18) |
| 511 | }); |
| 512 | |
| 513 | buffer = Buffer.concat([buffer, encrypted]); |
| 514 | |
| 515 | return buffer; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | module.exports = {MessageParser, CommandType}; |