Files
laatzen/Common/CommonConsole/FactoryUnseal.cs
T
Stoyan Zlatev 02943ebcbc omni before testing,
- recalculating correction factors and accuracies
- factory unseal
2024-02-05 14:48:35 +01:00

307 lines
13 KiB
C#

namespace CommonConsole
{
using OmniPlus;
using System;
using System.Linq;
using System.Text;
public static class OmniExtensions
{
public static void FactoryUnseal(OmniUniProConnection connection)
{
var _sealed = connection.Send(0xFD, 0x63)[1] == 1;
Console.WriteLine($"--- Sealed: {connection.Send(0xFD, 0x63)[1] == 1}");
Console.WriteLine($"--- Version & Type: {Encoding.ASCII.GetString(connection.Send(0x05))}");
Console.WriteLine("----------------------------------------------------------------------");
Console.WriteLine("--- 0. Test parameters:");
Console.WriteLine("--- SSSS: 1");
Console.WriteLine("--- IIII: 2");
Console.WriteLine("--- CCCC: 3");
Console.WriteLine("--- CCCC: 4");
Console.WriteLine("--- Command: FD 64 00 SS SS II II CC CC VV VV (Hash is BE)");
Console.WriteLine("--- : FD 64 00 each part is LE except the last one");
Console.WriteLine("--- 1.0 According docs: FD 64 00 01 00 02 00 03 00 00 04 (Hash is BE)");
Console.WriteLine("--- 1.1 According docs: FD 64 00 04 00 00 03 00 02 00 01 (Hash is LE)");
Console.WriteLine("--- 2.0 When all LE: FD 64 00 01 00 02 00 03 00 04 00 (Hash is BE)");
Console.WriteLine("--- 2.1 When all LE: FD 64 00 00 04 00 03 00 02 00 01 (Hash is LE)");
Console.WriteLine("--- 3.0 When all BE: FD 64 00 00 01 00 02 00 03 00 04 (Hash is BE)");
Console.WriteLine("--- 3.1 When all BE: FD 64 00 04 00 03 00 02 00 01 00 (Hash is LE)");
Console.WriteLine("----------------------------------------------------------------------");
if (_sealed)
{
//____________________0_____1_____2_____3_____4_____5_____6_____7_____8_____9____10__
//_____________________________________SS____SS____II____II____CC____CC____VV____VV__
byte[] request = new byte[] { 0xFD, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Console.WriteLine("--- 1.0 According docs: hash is sent BE");
Console.WriteLine("----------------------------------------------------------------------");
byte[] hash = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// Test case 1 - according documentation - hash BE
connection.Calculate_IIII_LE().CopyTo(hash, 2);
connection.Calculate_CCCC_LE().CopyTo(hash, 4);
connection.Calculate_VVVV_LE().CopyTo(hash, 6);
connection.Calculate_SSSS_LE().CopyTo(hash, 0); // .Reverse().ToArray()
Array.Copy(hash, 0, request, 3, 8);
connection.Send(request);
}
//Console.WriteLine("----------------------------------------------------------------------");
//Console.WriteLine("--- 1.1 According docs: hash is sent LE");
//Console.WriteLine("----------------------------------------------------------------------");
//hash = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
//// Test case 1 - according documentation - hash LE
//connection.Calculate_IIII_LE().CopyTo(hash, 2);
//connection.Calculate_CCCC_LE().CopyTo(hash, 4);
//connection.Calculate_VVVV_LE().CopyTo(hash, 6);
//connection.Calculate_SSSS_LE().Reverse().ToArray().CopyTo(hash, 0);
//Array.Copy(hash.Reverse().ToArray(), 0, request, 3, 8);
//connection.Send(request);
//Console.WriteLine("----------------------------------------------------------------------");
//Console.WriteLine("--- 2.0 When all LE: hash is sent BE");
//Console.WriteLine("----------------------------------------------------------------------");
//hash = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
//// Test case 2 all Little endian - hash BE
//connection.Calculate_IIII_LE().CopyTo(hash, 2);
//connection.Calculate_CCCC_LE().CopyTo(hash, 4);
//connection.Calculate_VVVV_LE().CopyTo(hash, 6);
//connection.Calculate_SSSS_LE().CopyTo(hash, 0);
//Array.Copy(hash, 0, request, 3, 8);
//connection.Send(request);
//Console.WriteLine("----------------------------------------------------------------------");
//Console.WriteLine("--- 2.1 When all LE: hash is sent LE");
//Console.WriteLine("----------------------------------------------------------------------");
//hash = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
//// Test case 2 all Little endian - hash LE
//connection.Calculate_IIII_LE().CopyTo(hash, 2);
//connection.Calculate_CCCC_LE().CopyTo(hash, 4);
//connection.Calculate_VVVV_LE().CopyTo(hash, 6);
//connection.Calculate_SSSS_LE().CopyTo(hash, 0);
//Array.Copy(hash.Reverse().ToArray(), 0, request, 3, 8);
//connection.Send(request);
//Console.WriteLine("----------------------------------------------------------------------");
//Console.WriteLine("--- 3.0 When all BE: hash is sent BE");
//Console.WriteLine("----------------------------------------------------------------------");
//hash = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
//// Test case 3 all Big endian - hash BE
//connection.Calculate_IIII_LE().Reverse().ToArray().CopyTo(hash, 2);
//connection.Calculate_CCCC_LE().Reverse().ToArray().CopyTo(hash, 4);
//connection.Calculate_VVVV_LE().Reverse().ToArray().CopyTo(hash, 6);
//connection.Calculate_SSSS_LE().Reverse().ToArray().CopyTo(hash, 0);
//Array.Copy(hash, 0, request, 3, 8);
//connection.Send(request);
//Console.WriteLine("----------------------------------------------------------------------");
//Console.WriteLine("--- 3.1 When all BE: hash is sent LE");
//Console.WriteLine("----------------------------------------------------------------------");
//hash = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
//// Test case 3 all Big endian - hash BE
//connection.Calculate_IIII_LE().Reverse().ToArray().CopyTo(hash, 2);
//connection.Calculate_CCCC_LE().Reverse().ToArray().CopyTo(hash, 4);
//connection.Calculate_VVVV_LE().Reverse().ToArray().CopyTo(hash, 6);
//connection.Calculate_SSSS_LE().Reverse().ToArray().CopyTo(hash, 0);
//Array.Copy(hash.Reverse().ToArray(), 0, request, 3, 8);
//connection.Send(request);
Console.WriteLine("----------------------------------------------------------------------");
Console.WriteLine($"--- Sealed: {connection.Send(0xFD, 0x63)[1] == 1}");
}
static byte[] Calculate_SSSS_LE(this OmniUniProConnection connection)
{
// response with status byte at the start
byte[] response = connection.Send(0xFD, 0x3D);
ushort result = 0;
if (response[0] == 0x01)
{
// returns uint32 from 4 little endian bytes
uint seconds = BitConverter.ToUInt32(response, 1);
Console.WriteLine($"--- Seconds active: {seconds}");
result = (ushort)((seconds >> 8) & 0xFFFF);
Console.WriteLine($"--- (x >> 8) & 0xFFFF: {result}");
}
var ssss_LE = BitConverter.GetBytes(result);
// returns 2 little endian bytes array from the ushort result
Console.WriteLine($"--- SSSS (LE): {BitConverter.ToString(ssss_LE).Replace("-", " ")}");
return ssss_LE;
}
static byte[] Calculate_IIII_LE(this OmniUniProConnection connection)
{
// response with status byte at the start
byte[] response = connection.Send(0x01);
int responseLength = Math.Max(response.Length - 1, 0);
string factoryID = Encoding
.ASCII
.GetString(response, 1, responseLength)
.Trim('\0')
.Trim();
ushort crcU16 = 0;
Console.WriteLine($"--- Factory ID: {factoryID}");
if (response[0] == 0x01)
{
byte[] plainData;
if (string.IsNullOrWhiteSpace(factoryID))
{
plainData = Encoding.ASCII.GetBytes("Factory");
}
else
{
plainData = Encoding.ASCII.GetBytes(factoryID);
}
Console.WriteLine($"--- Bytes to encode: {BitConverter.ToString(plainData).Replace("-", " ")}");
foreach (byte byteU8 in plainData)
{
ushort tempU16 = crcU16;
crcU16 = (ushort)(tempU16 >> 8);
crcU16 += (ushort)(tempU16 << 8);
crcU16 ^= byteU8;
tempU16 = (ushort)((crcU16 >> 4) & 0x000F);
crcU16 ^= tempU16;
tempU16 = (ushort)((crcU16 & 0x000F) << 12);
crcU16 ^= tempU16;
tempU16 = (ushort)((crcU16 & 0x00FF) << 5);
crcU16 ^= tempU16;
}
}
byte[] crc_Bytes_LE = BitConverter.GetBytes(crcU16);
Console.WriteLine($"--- CRC uint16: {crcU16}");
Console.WriteLine($"--- IIII (LE): {BitConverter.ToString(crc_Bytes_LE).Replace("-", " ")}");
// returns 2 little endian bytes array from the ushort crcU16
return crc_Bytes_LE;
}
static byte[] Calculate_CCCC_LE(this OmniUniProConnection connection)
{
// response with status byte at the start
byte[] response = connection.Send(0x07);
int responseLength = Math.Max(response.Length - 1, 0);
string customerText = Encoding
.ASCII
.GetString(response, 1, responseLength)
.Trim('\0');
Console.WriteLine($"--- Customer text: {customerText}");
byte[] cccc = new byte[2];
if (response[0] == 0x01)
{
byte[] plainData = new byte[4];
if (string.IsNullOrWhiteSpace(customerText))
{
plainData = Encoding.ASCII.GetBytes("Factory");
}
else if (customerText.Length < 4)
{
Encoding.ASCII.GetBytes(customerText).CopyTo(plainData, 0);
}
else
{
plainData = Encoding.ASCII.GetBytes(customerText);
}
Console.WriteLine($"--- Text to encode: {Encoding.ASCII.GetString(plainData)}");
Console.Write("--- |");
byte[] bits = new byte[16];
string hex = string.Empty;
// for each byte from the first 4 bytes
for (int i = 0; i < 4; i++)
{
byte current = plainData[i];
byte count1 = 0;
Console.Write($"{current}:");
// count bits set to 1
for (int b = 0; b < 8; b++)
{
int bit = current >> b & 1;
count1 += (byte)bit;
Console.Write(bit);
}
hex += count1;
Console.Write($":{count1}|");
}
Console.WriteLine();
cccc[0] = Convert.ToByte(hex.Substring(0, 2), 16);
cccc[1] = Convert.ToByte(hex.Substring(2, 2), 16);
}
Console.WriteLine($"--- CCCC (LE): {BitConverter.ToString(cccc).Replace("-", " ")}");
return cccc;
}
static byte[] Calculate_VVVV_LE(this OmniUniProConnection connection)
{
byte[] response = connection.Send(0x13);
int responseLength = Math.Max(response.Length - 1, 0);
string readingPreset = Encoding
.ASCII
.GetString(response, 1, responseLength)
.Trim('\0');
Console.WriteLine($"--- Customer text: {readingPreset}");
Console.WriteLine($"--- Reading preset: {Encoding.ASCII.GetString(response)}");
ushort result = 0;
if (response[0] == 0x01)
{
byte[] bytes = new byte[4];
Array.Copy(response, 3, bytes, 0, 4);
bytes = bytes.Reverse().ToArray();
// copies response bytes 3..6
uint number = BitConverter.ToUInt32(bytes, 0);
Console.WriteLine($"--- UInt32: {number}");
result = (ushort)(number % 11);
Console.WriteLine($"--- UInt32 % 11: {result}");
}
byte[] vvvv_LE = BitConverter.GetBytes(result);
Console.WriteLine($"--- VVVV (LE): {BitConverter.ToString(vvvv_LE).Replace("-", " ")}");
// returns 2 little endian bytes array from the ushort result
return vvvv_LE;
}
}
}