Skip to content

Commit

Permalink
parse more tuya config, also add a tricky way to skip tuya pages
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed May 6, 2023
1 parent 0267a9a commit 88246ce
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
5 changes: 5 additions & 0 deletions BK7231Flasher/MiscUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,10 @@ public static int indexOf(byte[] src, byte[] needle)
}
return -1;
}

internal static byte[] stripBinary(byte[] str)
{
throw new NotImplementedException();
}
}
}
92 changes: 89 additions & 3 deletions BK7231Flasher/TuyaConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public bool fromBytes(byte[] data)

byte[] key = null;

decrypted = new MemoryStream();
BinaryWriter bw = new BinaryWriter(decrypted);
byte[] first = myDecrypt(data, needle, 0, Encoding.ASCII.GetBytes(KEY_MASTER));
using (BinaryReader br = new BinaryReader(new MemoryStream(first)))
{
Expand All @@ -92,9 +94,8 @@ public bool fromBytes(byte[] data)
return true;
}
key = makeSecondaryKey(key);
bw.Write(first, 8, first.Length - 8);
}
decrypted = new MemoryStream();
BinaryWriter bw = new BinaryWriter(decrypted);
for (int blockIndex = 1; blockIndex < 500; blockIndex++)
{
byte[] next = myDecrypt(data, needle, blockIndex, key);
Expand Down Expand Up @@ -134,6 +135,12 @@ public string getKeysHumanReadable()
int number = int.Parse(Regex.Match(key, "\\d+").Value);
desc += "- LED (channel " + number + ") on P" + value + Environment.NewLine;
}
else if (Regex.IsMatch(key, "^netled\\d+_pin$"))
{
// some devices have netled1_pin, some have netled_pin
int number = int.Parse(Regex.Match(key, "\\d+").Value);
desc += "- WiFi LED on P" + value + Environment.NewLine;
}
else if (Regex.IsMatch(key, "^rl\\d+_pin$"))
{
int number = int.Parse(Regex.Match(key, "\\d+").Value);
Expand All @@ -156,6 +163,7 @@ public string getKeysHumanReadable()
}
else if (key == "netled_pin")
{
// some devices have netled1_pin, some have netled_pin
desc += "- WiFi LED on P" + value + Environment.NewLine;
}
else if(key == "ele_pin")
Expand Down Expand Up @@ -203,6 +211,55 @@ public string getKeysHumanReadable()

}
}
// LED
string iicscl = getKeyValue("iicscl");
string iicsda = getKeyValue("iicsda");
if (iicscl.Length > 0 && iicsda.Length > 0)
{
string iicr = getKeyValue("iicr");
string iicg = getKeyValue("iicg");
string iicb = getKeyValue("iicb");
string iicc = getKeyValue("iicc");
string iicw = getKeyValue("iicw");
string map = "" + iicr + " " + iicg + " " + iicb + " " + iicc + " " + iicw;
string ledType = "Unknown";
string ehccur = getKeyValue("ehccur");
string dccur = getKeyValue("dccur");
string cjwcur = getKeyValue("cjwcur");
string _2235ccur = getKeyValue("2235ccur");
string _2335ccur = getKeyValue("2335ccur");
// use current (color/cw) setting
if (ehccur.Length>0)
{
ledType = "SM2135";
}
else if (dccur.Length > 0)
{
ledType = "BP5758D_";
}
else if (cjwcur.Length > 0)
{
ledType = "BP1658CJ_";
}
else if (_2235ccur.Length > 0)
{
ledType = "SM2235";
}
else if (_2335ccur.Length > 0)
{
// fallback
ledType = "SM2235";
}
else
{

}
string dat_name = ledType + "DAT";
string clk_name = ledType + "CLK";
desc += "- " + dat_name + " on P" + iicsda + Environment.NewLine;
desc += "- " + clk_name + " on P" + iicscl + Environment.NewLine;
desc += "- LED remap is " + map + Environment.NewLine;
}
if (desc.Length > 0)
{
desc = "Device configuration, as extracted from Tuya: " + Environment.NewLine + desc;
Expand All @@ -213,6 +270,17 @@ public string getKeysHumanReadable()
}
return desc;
}
public string getKeyValue(string key)
{
for (int i = 0; i < parms.Count; i++)
{
if (parms[i].Key == key)
{
return parms[i].Value;
}
}
return "";
}
public string getKeysAsJSON()
{
string r = "{" + Environment.NewLine;
Expand Down Expand Up @@ -242,7 +310,25 @@ public string getKeysAsJSON()
}
int first_at = keys_at + 5;
byte[] str = MiscUtils.subArray(descryptedRaw, first_at, stopAT - first_at);
string asciiString = Encoding.ASCII.GetString(str);
string asciiString;
// There is still some kind of Tuya paging here,
// let's skip it in a quick and dirty way
#if false
str = MiscUtils.stripBinary(str);
asciiString = Encoding.ASCII.GetString(str);
#else
asciiString = "";
for(int i = 0; i < str.Length; i++)
{
byte b = str[i];
if (b < 32)
continue;
if (b > 127)
continue;
char ch = (char)b;
asciiString += ch;
}
#endif
string[] pairs = asciiString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for(int i = 0; i < pairs.Length; i++)
{
Expand Down

0 comments on commit 88246ce

Please sign in to comment.