From 018880490904469ab696292363211584184c7495 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Tue, 8 Nov 2022 10:19:08 +0100 Subject: [PATCH] cleanup some code and add 3.2 support (adresses #234) --- index.js | 7 ++++--- lib/cipher.js | 4 ++-- lib/message-parser.js | 23 ++++++++++++++--------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 0da1a6d..be0410a 100644 --- a/index.js +++ b/index.js @@ -57,6 +57,7 @@ class TuyaDevice extends EventEmitter { super(); // Set device to user-passed options + version = version.toString(); this.device = {ip, port, id, gwID, key, productKey, version}; this.globalOptions = { issueGetOnConnect, @@ -648,7 +649,7 @@ class TuyaDevice extends EventEmitter { const buffer = this.device.parser.encode({ data: this._tmpLocalKey, encrypted: true, - commandByte: CommandType.BIND, + commandByte: CommandType.SESS_KEY_NEG_START, sequenceN: ++this._currentSequenceN }); @@ -675,7 +676,7 @@ class TuyaDevice extends EventEmitter { clearTimeout(this._sendTimeout); // Protocol 3.4 - Response to Msg 0x03 - if (packet.commandByte === CommandType.RENAME_GW) { + if (packet.commandByte === CommandType.SESS_KEY_NEG_RES) { if (!this.connectPromise) { debug('Protocol 3.4: Ignore Key exchange message because no connection in progress.'); return; @@ -703,7 +704,7 @@ class TuyaDevice extends EventEmitter { const buffer = this.device.parser.encode({ data: this.device.parser.cipher.hmac(this._tmpRemoteKey), encrypted: true, - commandByte: CommandType.RENAME_DEVICE, + commandByte: CommandType.SESS_KEY_NEG_FINISH, sequenceN: ++this._currentSequenceN }); diff --git a/lib/cipher.js b/lib/cipher.js index 3501770..01c2b99 100644 --- a/lib/cipher.js +++ b/lib/cipher.js @@ -107,8 +107,8 @@ class TuyaCipher { let format = 'buffer'; if (data.indexOf(this.version) === 0) { - if (this.version === '3.3') { - // Remove 3.3 header + if (this.version === '3.3' || this.version === '3.2') { + // Remove 3.3/3.2 header data = data.slice(15); } else { // Data has version number and is encoded in base64 diff --git a/lib/message-parser.js b/lib/message-parser.js index f6ee3fa..eab4d6c 100644 --- a/lib/message-parser.js +++ b/lib/message-parser.js @@ -6,6 +6,7 @@ const HEADER_SIZE = 16; /** * Human-readable definitions * of command bytes. + * See also https://github.com/tuya/tuya-iotos-embeded-sdk-wifi-ble-bk7231n/blob/master/sdk/include/lan_protocol.h * @readonly * @private */ @@ -13,9 +14,12 @@ const CommandType = { UDP: 0, AP_CONFIG: 1, ACTIVE: 2, - BIND: 3, - RENAME_GW: 4, - RENAME_DEVICE: 5, + BIND: 3, // ?? leave in for backward compatibility + SESS_KEY_NEG_START: 3, // negotiate session key + RENAME_GW: 4, // ?? leave in for backward compatibility + SESS_KEY_NEG_RES: 4, // negotiate session key response + RENAME_DEVICE: 5, // ?? leave in for backward compatibility + SESS_KEY_NEG_FINISH: 5,// finalize session key negotiation UNBIND: 6, CONTROL: 7, STATUS: 8, @@ -27,10 +31,11 @@ const CommandType = { ENABLE_WIFI: 14, DP_QUERY_NEW: 16, SCENE_EXECUTE: 17, - DP_REFRESH: 18, + DP_REFRESH: 18, // Request refresh of DPS UPDATEDPS / LAN_QUERY_DP UDP_NEW: 19, AP_CONFIG_NEW: 20, - UDP_NEW_34: 35, + BOARDCAST_LPV34: 35, + LAN_EXT_STREAM: 40, LAN_GW_ACTIVE: 240, LAN_SUB_DEV_REQUEST: 241, LAN_DELETE_SUB_DEV: 242, @@ -294,8 +299,8 @@ class MessageParser { // Construct payload let payload = options.data; - // Protocol 3.3 is always encrypted - if (this.version === '3.3') { + // Protocol 3.3 and 3.2 is always encrypted + if (this.version === '3.3' || this.version === '3.2') { // Encrypt data payload = this.cipher.encrypt({ data: payload, @@ -365,8 +370,8 @@ class MessageParser { if (options.commandByte !== CommandType.DP_QUERY && options.commandByte !== CommandType.HEART_BEAT && options.commandByte !== CommandType.DP_QUERY_NEW && - options.commandByte !== CommandType.BIND && - options.commandByte !== CommandType.RENAME_DEVICE && + options.commandByte !== CommandType.SESS_KEY_NEG_START && + options.commandByte !== CommandType.SESS_KEY_NEG_FINISH && options.commandByte !== CommandType.DP_REFRESH) { // Add 3.4 header // check this: mqc_very_pcmcd_mcd(int a1, unsigned int a2)