Skip to content

Commit

Permalink
Tuya Zosung support + ..
Browse files Browse the repository at this point in the history
  • Loading branch information
tcharp38 committed Oct 9, 2023
1 parent c96ab80 commit 9fb73d2
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 55 deletions.
12 changes: 6 additions & 6 deletions core/class/AbeilleCmdProcess.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5081,7 +5081,7 @@ function processCmd($Command) {
$tData = $Command['data']; // Supposed to be 00 (NOT connected), 01 (connected) or 02 (timeout)
$tLen = sprintf("%04X", strlen($tData) / 2);

cmdLog('debug', ' internetStatus: tSqn='.$tSqn.', tLen='.$tLen.', tData='.$tData);
cmdLog2('debug', $addr, ' internetStatus: tSqn='.$tSqn.', tLen='.$tLen.', tData='.$tData);
$data2 = $zclHeader.$tSqn.$tLen.$tData;
} else {
// ZCL header
Expand All @@ -5097,16 +5097,16 @@ function processCmd($Command) {
$dpData = $dp['data'];
$dpLen = strlen($dpData) / 2;
if (($dpType == "02") && ($dpLen > 4)) {
cmdLog('error', ' Wrong dpData size (max=4B for type 02)');
cmdLog2('error', $addr, ' Wrong dpData size (max=4B for type 02)');
return;
}

$tuyaSqn = isset($Command['tuyaSqn']) ? $Command['tuyaSqn'] : tuyaGenSqn(); // Tuya transaction ID
$tSqn = isset($Command['tuyaSqn']) ? $Command['tuyaSqn'] : tuyaGenSqn(); // Tuya transaction ID
$dpId = $dp['id'];
$dpLen = sprintf("%04X", $dpLen);

cmdLog('debug', ' '.$Command['cmd'].': tuyaSqn='.$tuyaSqn.', dpId='.$dpId.', dpType='.$dpType.', dpData='.$dpData);
$data2 = $fcf.$sqn.$cmdId.$tuyaSqn.$dpId.$dpType.$dpLen.$dpData;
cmdLog2('debug', $addr, ' '.$Command['cmd'].': tSqn='.$tSqn.', dpId='.$dpId.', dpType='.$dpType.', dpData='.$dpData);
$data2 = $fcf.$sqn.$cmdId.$tSqn.$dpId.$dpType.$dpLen.$dpData;
}
$dataLen2 = sprintf("%02X", strlen($data2) / 2);
$data1 = $addrMode.$addr.$srcEp.$dstEp.$clustId.$profId.$secMode.$radius.$dataLen2;
Expand All @@ -5133,7 +5133,7 @@ function processCmd($Command) {
} else if (method_exists($this, $fctName)) {
$this->$fctName($dest, $addr, $ep, $cmd, $message);
} else {
cmdLog('error', " Commande privée: Fonction '${fctName}' inconnue");
cmdLog2('error', $addr, " Commande privée: Fonction '${fctName}' inconnue");
}
return;
} // End 'cmd-Private'
Expand Down
12 changes: 8 additions & 4 deletions core/class/AbeilleParser.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4321,7 +4321,7 @@ function decode8002($dest, $payload, $lqi) {
if ($this->isDuplicated($dest, $srcAddr, $fcf, $sqn))
return;

$attrReportN = tuyaDecodeEF00Cmd($dest, $srcAddr, $srcEp, $cmd, $msg, $toMon);
$attrReportN = tuyaDecodeEF00Cmd($dest, $srcAddr, $srcEp, $cmd, $msg);
} // End $clustId == "EF00"

else {
Expand Down Expand Up @@ -4365,9 +4365,9 @@ function decode8002($dest, $payload, $lqi) {
}
if (isset($supportType)) {
if ($supportType == 'tuya')
$attrReportN = tuyaDecodeEF00Cmd($dest, $srcAddr, $srcEp, $cmd, $pl, $toMon);
$attrReportN = tuyaDecodeEF00Cmd($dest, $srcAddr, $srcEp, $cmd, $pl);
else if ($supportType == 'tuya-zosung')
$attrReportN = tuyaDecodeZosungCmd($dest, $srcAddr, $srcEp, $cmd, $pl, $toMon);
$attrReportN = tuyaDecodeZosungCmd($dest, $srcAddr, $srcEp, $cmd, $pl);
else
parserLog("error", " Cluster specific command ".$clustId."-".$cmd.": unsupported type ".$supportType);
} else {
Expand Down Expand Up @@ -4419,11 +4419,15 @@ function decode8002($dest, $payload, $lqi) {

$this->whoTalked[] = $dest.'/'.$srcAddr;

// Monitor if requested
// Monitor if requested (OBSOLETE way !! Use parserLog2() instead)
if (isset($GLOBALS["dbgMonitorAddr"]) && !strcasecmp($GLOBALS["dbgMonitorAddr"], $srcAddr)) {
foreach ($toMon as $monMsg)
monMsgFromZigate($monMsg); // Send message to monitor
}
// Flush monitoring messages (if any) from parserLog2()
foreach ($GLOBALS['toMon'] as $monMsg)
monMsgFromZigate($monMsg); // Send message to monitor
$GLOBALS['toMon'] = []; // Clear monitoring queue
} // End decode8002()

/* 8009/Network State Reponse */
Expand Down
11 changes: 6 additions & 5 deletions core/php/AbeilleCmd-Tuya.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function tuyaCmd2Dp($abCmd) {
return $dp;
}

// Returns Tuya specific transaction ID (4 bytes)
// Returns Tuya specific transaction ID (2 bytes)
function tuyaGenSqn() {
global $tuyaTransId;
$tuyaTransId++;
Expand Down Expand Up @@ -144,7 +144,7 @@ function tuyaZosungCrc($message) {

// Use cases: ED00 cluster support (Moes universal remote)
function tuyaZosung($net, $addr, $ep, $cmd, $data) {
cmdLog('debug', " tuyaZosung(net=${net}, addr=${addr}, ep=${ep}, cmd=${cmd})");
cmdLog2('debug', $addr, " tuyaZosung(net=${net}, addr=${addr}, ep=${ep}, cmd=${cmd})");

if ($cmd == '00') { // Send IR code
$irMsg = array(
Expand Down Expand Up @@ -172,13 +172,14 @@ function tuyaZosung($net, $addr, $ep, $cmd, $data) {
// {name: 'unk3', type: DataType.uint8},
// {name: 'cmd', type: DataType.uint8},
// {name: 'unk4', type: DataType.uint16},
$seq = '0012'; // TODO
$seq = tuyaGenSqn();
$length = sprintf("%08X", strlen($irMsgJson));
$unk1 = '00000000';
$unk2 = 'e004';
$unk3 = '01';
$cmd = '02';
$unk4 = '0000';
cmdLog2('debug', $addr, " Cmd ED00-00: Seq=${seq}, Len=${length}");

$seq = AbeilleTools::reverseHex($seq);
$len = AbeilleTools::reverseHex($length);
Expand All @@ -200,14 +201,14 @@ function tuyaZosung($net, $addr, $ep, $cmd, $data) {
$params = json_decode($data, true);
$seq = $params['seq'];
$pos = $params['pos'];
cmdLog('debug', " Cmd 03: Seq=${seq}, Pos=${pos}");
cmdLog2('debug', $addr, " Cmd ED00-03: Seq=${seq}, Pos=${pos}");

$message = $GLOBALS['zosung_msg']['message'];
$msgPart = substr($message, $pos);
if ((strlen($msgPart) / 2) > 0x38)
$msgPart = substr($msgPart, 0, 0x37 * 2); // Truncate to maxLen
$msgPartCrc = tuyaZosungCrc($msgPart);
cmdLog('debug', " MsgPart=${msgPart}, MsgPartCrc=${msgPartCrc}");
cmdLog2('debug', $addr, " MsgPart=${msgPart}, MsgPartCrc=${msgPartCrc}");

// Cmd 03 reminder
// {name: 'zero', type: DataType.uint8},
Expand Down
14 changes: 12 additions & 2 deletions core/php/AbeilleCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@
include_once __DIR__.'/AbeilleCmd-Tuya.php';
include_once __DIR__.'/AbeilleZigateConst.php';

function cmdLog($loglevel = 'NONE', $message = "", $isEnable = 1) {
// Log if proper log level
function cmdLog($level, $message = "", $isEnable = 1) {
if ($isEnable == 0)
return;
logMessage($loglevel, $message);
logMessage($level, $message);
}

// Log if proper log level
// & monitor if proper address
function cmdLog2($level, $addr, $message) {
logMessage($level, $message);

if (isset($GLOBALS["dbgMonitorAddr"]) && ($GLOBALS["dbgMonitorAddr"] != "") && ($addr == $GLOBALS["dbgMonitorAddr"]))
monMsgToZigate($addr, $message);
}

/* Get device infos.
Expand Down
Loading

0 comments on commit 9fb73d2

Please sign in to comment.