0% found this document useful (0 votes)
20 views8 pages

5 Mserver

The document contains code for utility functions used to communicate with an MTK device. It includes functions for sending and receiving data, converting between data types, padding strings, and error handling.

Uploaded by

yaelintun1999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

5 Mserver

The document contains code for utility functions used to communicate with an MTK device. It includes functions for sending and receiving data, converting between data types, padding strings, and error handling.

Uploaded by

yaelintun1999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

using Microsoft.

VisualBasic;
using System;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static mtkclient.main;

namespace mtkclient.MTK.Client
{
class utils
{
public static string magichexFlow = "efeeeefe01000000";
public static string magichexMessage = "efeeeefe02000000";
public static string magichex = "efeeeefe";

public static async Task xsend8(


IMtkDevice device,
CancellationToken cancellationToken,
byte[] databytes
)
{
byte[] datapaket = HexStringToBytes("EF EE EE FE 01 00 00 00 08 00 00
00");
await writemtk(device, cancellationToken, datapaket);
await writemtk(device, cancellationToken, databytes);
}

public static async Task xsend4(


IMtkDevice device,
CancellationToken cancellationToken,
byte[] databytes
)
{
byte[] datapaket = HexStringToBytes("EFEEEEFE0100000004000000");
await writemtk(device, cancellationToken, datapaket);
await writemtk(device, cancellationToken, databytes);
}

public static async Task<bool> send00000(


IMtkDevice device,
CancellationToken cancellationToken,
int panjangresponse,
string eksekusi,
string hex,
string delay
)
{
if (!cancellationToken.IsCancellationRequested)
{
if (string.IsNullOrEmpty(hex))
{
hex = "00000000";
}
if (string.IsNullOrEmpty(delay))
{
delay = "10";
}
byte[] d = HexStringToBytes(hex); //1660
await xsend4(device, cancellationToken, d);
Thread.Sleep(Convert.ToInt32(delay));
string status = BytesToHextring(
await readmtk(device, cancellationToken,
panjangresponse.ToString())
);

if (status.Length >= 8)
{
if (applyshiftKiri2(status.Substring(status.Length - 8)) ==
"c0010003")
{
return true;
}

if (applyshiftKiri2(status.Substring(status.Length - 8)) ==
"40040005")
{
return true;
}

if (applyshiftKiri2(status.Substring(status.Length - 8)) ==
"40040004")
{
return true;
}
}

if (!(status.Substring(0, 8) == magichex))
{
logs("Wrong Magic : " + eksekusi + " === " + status, true);
return false;
}

if (status.Length == panjangresponse * 2)
{
if (!(status.Substring((panjangresponse * 2) - 2, 2) == "00"))
{
logs("", true);
logs(
"Error at : "
+ eksekusi
+ " === "
+ applyshiftKiri2(status.Substring(status.Length -
8)),
true
);
return false;
}
}
return true;
}
else
{
if (cancellationToken.IsCancellationRequested)
{
return false;
}
}
return false;
}

public static async Task<byte[]> readmtk(


IMtkDevice device,
CancellationToken cancellationToken,
string len
)
{
if (cancellationToken.IsCancellationRequested)
{
return new byte[0];
}
try
{
return await device.ReadCustomAsync(cancellationToken,
Convert.ToInt32(len));
}
catch (Exception ex)
{
logs("Error " + ex.ToString(), true);
return new byte[0];
}
}

public static async Task writemtk(


IMtkDevice device,
CancellationToken cancellationToken,
byte[] data
)
{
if (cancellationToken.IsCancellationRequested)
{
return;
}
try
{
await device.WriteAsync(data, 0, data.Length, cancellationToken);
}
catch (Exception ex)
{
logs("Error " + ex.ToString(), true);
}
}

public static string GetFileSize(long TheSize)


{
string str = "0KB";
try
{
long num = TheSize;

double DoubleBytes;
if (num >= 1099511627776L)
{
DoubleBytes = TheSize / 1099511627776.0;
str = string.Concat(
Strings.FormatNumber(
DoubleBytes,
2,
TriState.UseDefault,
TriState.UseDefault,
TriState.UseDefault
),
" TB"
);
}
else if (num >= 1073741824L && num <= 1099511627775L)
{
DoubleBytes = TheSize / 1073741824.0;
str = string.Concat(
Strings.FormatNumber(
DoubleBytes,
2,
TriState.UseDefault,
TriState.UseDefault,
TriState.UseDefault
),
" GB"
);
}
else if (num >= 1048576L && num <= 1073741823L)
{
DoubleBytes = TheSize / 1048576.0;
str = string.Concat(
Strings.FormatNumber(
DoubleBytes,
2,
TriState.UseDefault,
TriState.UseDefault,
TriState.UseDefault
),
" MB"
);
}
else if (num >= 1024L && num <= 1048575L)
{
DoubleBytes = TheSize / 1024.0;
str = string.Concat(
Strings.FormatNumber(
DoubleBytes,
2,
TriState.UseDefault,
TriState.UseDefault,
TriState.UseDefault
),
" KB"
);
}
else if (num < 0L || num > 1023L)
{
str = "";
}
else
{
DoubleBytes = TheSize;
str = string.Concat(
Strings.FormatNumber(
DoubleBytes,
2,
TriState.UseDefault,
TriState.UseDefault,
TriState.UseDefault
),
" bytes"
);
}
}
catch { }
return str;
}

public static long HexToLong(string hexstring)


{
if (hexstring == "none")
{
hexstring = "0";
}
else if (hexstring == "")
{
hexstring = "0";
}
return Convert.ToInt64(hexstring, 16);
}

public static string LongToHex(long LongLen)


{
long val = LongLen;
string hexString = val.ToString("x");
return "0x" + hexString;
}

public static byte[] HexStringToBytes(string s)


{
try
{
s = s.Replace(" ", "").Replace("-", "").ToUpper();
int nBytes = s.Length / 2;
byte[] a = new byte[nBytes];

for (int i = 0; i < nBytes; i++)


{
a[i] = Convert.ToByte(s.Substring(i * 2, 2), 16);
}
return a;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return new byte[0];
}

public static string penggenapan8(string input)


{
var len = input.Length;
if (len == 16)
{
return input;
}
else if (len == 14)
{
return input + "00";
}
else if (len == 12)
{
return input + "0000";
}
else if (len == 10)
{
return input + "000000";
}
else if (len == 8)
{
return input + "00000000";
}
else if (len == 6)
{
return input + "0000000000";
}
else if (len == 4)
{
return input + "000000000000";
}
else if (len == 2)
{
return input + "00000000000000";
}
else
{
return input + "0000000000000000";
}
}

public static string penggenapan4belakang(string input)


{
string len = input.Length.ToString();
if (len == "8")
{
return input;
}
else if (len == "7")
{
return input + "0";
}
else if (len == "6")
{
return input + "00";
}
else if (len == "5")
{
return input + "000";
}
else if (len == "4")
{
return input + "0000";
}
else if (len == "3")
{
return input + "00000";
}
else if (len == "2")
{
return input + "000000";
}
else if (len == "1")
{
return "0" + input + "000000";
}

return input;
}

public static string DecimalToHexadecimal(long dec)


{
if (dec < 1)
{
return "0";
}

long hex = 0;
string hexStr = string.Empty;

while (dec > 0)


{
hex = dec % 16;

if (hex < 10)


{
hexStr = hexStr.Insert(0, Convert.ToChar(hex + 48).ToString());
}
else
{
hexStr = hexStr.Insert(0, Convert.ToChar(hex + 55).ToString());
}

dec = Convert.ToInt64(Math.Floor(dec / 16.0));


}

return hexStr;
}

public static string applyshiftKiri2(string hexstring)


{
string output = "";
if (!(hexstring.Length % 2 == 0))
{
hexstring = "0" + hexstring;
}
int countsnya = hexstring.Length - 2;
int lenoutput = hexstring.Length;

for (var i = 0; i < hexstring.Length / 2; i++)


{
output += hexstring.Substring(countsnya, 2);
countsnya -= 2;
}

output = Regex.Replace(output, "^0+", "");

if (!(output.Length % 2 == 0))
{
output = "0" + output;
}

double inputlen = hexstring.Length / 2;

double kurangnya = inputlen - (output.Length / 2);

for (var zz = 0; zz < kurangnya; zz++)


{
output = "00" + output;
}

return (output);
}

public static string BytesToHextring(byte[] input)


{
return BitConverter.ToString(input).Replace("-", "").ToLower();
}
}
}

You might also like