Skip to content

Commit

Permalink
cleanup some code and add 3.2 support (adresses #234)
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Nov 8, 2022
1 parent 19d1198 commit 0188804
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
});

Expand All @@ -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;
Expand Down Expand Up @@ -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
});

Expand Down
4 changes: 2 additions & 2 deletions lib/cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions lib/message-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ 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
*/
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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0188804

Please sign in to comment.