You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
19 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BirdyFlash.Lib;
using MaterialSkin;
using MaterialSkin.Controls;
namespace BirdyFlash {
public partial class mainForm : MaterialForm {
public mainForm() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
ShowHideConsole.ToggleConsoleWindow(Properties.Settings.Default.showConsole);
ckbDebug.Checked = Properties.Settings.Default.showConsole;
MaterialSkinManager.Instance.AddFormToManage(this);
DeviceFinder.instance.PortDataUpdated += Instance_PortDataUpdated;
DeviceFinder.instance.RescanLocalSerialPorts();
}
private void Instance_PortDataUpdated(object sender, EventArgs e) {
foreach (MaterialCheckbox item in lbxAvailablePorts.Items.ToList())
if (DeviceFinder.instance.registeredPorts.Contains(item.Text) == false)
lbxAvailablePorts.Items.Remove(item);
foreach (string portName in DeviceFinder.instance.registeredPorts)
if (lbxAvailablePorts.Items.Where(x => x.Text == portName).Count() == 0) {
MaterialCheckbox ckBoxGenPort = new MaterialCheckbox{
Text = portName
};
if (portName.IndexOf("serial://") == 0) {
ckBoxGenPort.MouseDown += CkBoxGenSerialport_MouseDown;
}
lbxAvailablePorts.Items.Add(ckBoxGenPort);
}
foreach (MaterialCheckbox item in lbxAvailablePorts.Items.ToList()) {
item.Checked = DeviceFinder.instance.IsPortEnabled(item.Text);
if(item.Tag == null) // if not yet set, add eventhandler
item.CheckedChanged += Item_CheckedChanged;
item.Tag = false;
}
}
private void CkBoxGenSerialport_MouseDown(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Right) return;
Console.WriteLine(e.Button);
ctxManageSerialPort.Tag = sender;
ctxManageSerialPort.Show(Cursor.Position);
}
private void force56700bdToolStripMenuItem_Click(object sender, EventArgs e) {
MaterialCheckbox ckBox = (MaterialCheckbox)((MaterialContextMenuStrip)((ToolStripItem)sender).Owner).Tag;
if (ckBox.Text.EndsWith("slow")) {
DeviceFinder.instance.SetPortOption(ckBox.Text, "");
} else {
DeviceFinder.instance.SetPortOption(ckBox.Text, "slow");
}
Console.WriteLine(ckBox.Text);
}
private void Item_CheckedChanged(object sender, EventArgs e) {
Console.WriteLine("{0} {1} {2}", sender.ToString(), ((MaterialCheckbox)sender).Text, ((MaterialCheckbox)sender).Checked);
var cb = (MaterialCheckbox)sender;
if ((bool)cb.Tag == true) return; // skip if we set the tag to true, to prevent loops
cb.Tag = true;
if (cb.Checked)
DeviceFinder.instance.EnablePort(cb.Text);
else
DeviceFinder.instance.DisablePort(cb.Text);
/*foreach (MaterialCheckbox item in lbxAvailablePorts.Items.ToList())
item.Checked = DeviceFinder.instance.IsPortEnabled(item.Text);*/
}
private void btnRefreshPorts_Click(object sender, EventArgs e) {
ctxPortManagment.Show(Cursor.Position);
}
private void ckbxPagerWatchMode_CheckedChanged(object sender, EventArgs e) {
//pnlManualMode.Visible = !ckbxPagerWatchMode.Checked;
}
private async void btnManualDiscoverPager_Click(object sender, EventArgs e) {
btnManualDiscoverPager.Enabled = false;
manualPagerSelect.Enabled = false;
MaterialListBoxItem taskItem = null;
this.Invoke(new Action(() => {
taskItem = new MaterialListBoxItem("Scanning Pagers", "lol");
lbxTasks.Items.Add(taskItem);
}));
try {
var t = Task.Run(() => DeviceFinder.instance.ScanPagers());
await t;
} catch(Exception ex) {
Console.WriteLine(ex.ToString());
} finally {
this.Invoke(new Action(() => {
lbxTasks.Items.Remove(taskItem);
}));
manualPagerSelect.Items.Clear();
foreach (CommPortInfo commPortInfo in DeviceFinder.instance.GetAvailablePagers()) {
manualPagerSelect.Items.Add(new PagerSelectListEntry(commPortInfo));
}
manualInvalidateSelection();
btnManualDiscoverPager.Enabled = true;
manualPagerSelect.Enabled = true;
}
}
private void manualPagerSelect_SelectedIndexChanged(object sender, EventArgs e) {
PagerSelectListEntry entry = (PagerSelectListEntry)manualPagerSelect.SelectedItem;
if (entry == null) return;
pbSlim.Visible = (entry.commPortInfo.deviceInfo.model == "");
txtManualSerial.Text = entry.commPortInfo.deviceInfo.deviceSerial;
btnManualDumpE2P.Enabled = true;
btnManualWriteE2P.Enabled = true;
btnWriteBOSkrypt.Enabled = true;
btnResetE2P.Enabled = true;
btnChangeSerial.Enabled = true;
materialButton5.Enabled = true;
materialButton6.Enabled = true;
btnManualRestart.Enabled = true;
}
private void manualPagerLock(bool lockControls) {
Control[] controls = new Control[] {
btnManualWriteE2P, btnManualDumpE2P,btnResetE2P,
btnChangeSerial,btnManualRestart,
materialButton5,materialButton6,
};
foreach (Control control in controls)
control.Enabled = !lockControls;
}
private async void btnManualDumpE2P_Click(object sender, EventArgs e) {
manualPagerLock(true);
PagerSelectListEntry entry = (PagerSelectListEntry)manualPagerSelect.SelectedItem;
string targetFilePath = "";
E2PFileFormat targetedFormat;
using (var fileDialog = new SaveFileDialog()) {
switch (cbxE2PFileFormat.SelectedIndex) {
case 0: // TPL Original (.rev2)
fileDialog.Filter = "TPL-Original-File|*." + DeviceE2PContents.getFileExtension(entry.commPortInfo.deviceInfo);
targetedFormat = E2PFileFormat.TPLOriginal;
break;
case 1: // BIN - Dump(.bin)
fileDialog.Filter = "BIN-File|*.bin";
targetedFormat = E2PFileFormat.Binary;
break;
case 2: // Hex - Text(.hex)
fileDialog.Filter = "HEX-File|*.hex";
targetedFormat = E2PFileFormat.HEXText;
break;
default: // wtf?
return;
}
if (fileDialog.ShowDialog() != DialogResult.OK) {
manualPagerLock(false);
return;
}
targetFilePath = fileDialog.FileName;
}
using (var comm = new BirdyComm()) {
comm.SetPort(entry.commPortInfo.port);
//if (comm.commPort is CommPortSerial) ((CommPortSerial)comm.commPort).ResetReOpen();
//await comm.ForceGotoProg();
//await Task.Delay(2000);
//if (1 < 3) return false;
try {
var data = await comm.TryGetDeviceInfo();
Console.WriteLine("deviceSerial: {0}", data.deviceSerial);
if (comm.type == BirdyType.Slim && await comm.AskHighspeed() == false) throw new Exception("Highspeed mode did not work");
var e2pContents = await comm.TryReadE2P();
Console.WriteLine(e2pContents.data.Count);
e2pContents.writeAsFile(targetedFormat, targetFilePath);
if (comm.type == BirdyType.Slim && await comm.AskLowspeed() == false) throw new Exception("Highspeed mode did not work"); else Console.WriteLine("E2PDump, Highspeed success");
/*this.Invoke(new Action(() => {
txtLog.AppendText(string.Format("{0} type {1} serial {2}\n", portName, data.model, data.deviceSerial));
}));*/
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
manualPagerLock(false);
}
private async void materialButton1_Click_1(object sender, EventArgs e) {
manualPagerLock(true);
PagerSelectListEntry entry = (PagerSelectListEntry)manualPagerSelect.SelectedItem;
string targetFilePath = "";
E2PFileFormat targetedFormat;
using (var fileDialog = new OpenFileDialog()) {
switch (cbxE2PFileFormat.SelectedIndex) {
case 0: // TPL Original (.rev2)
fileDialog.Filter = "TPL-Original-File|*." + DeviceE2PContents.getFileExtension(entry.commPortInfo.deviceInfo);
targetedFormat = E2PFileFormat.TPLOriginal;
break;
case 1: // BIN - Dump(.bin)
fileDialog.Filter = "BIN-File|*.bin";
targetedFormat = E2PFileFormat.Binary;
break;
case 2: // Hex - Text(.hex)
fileDialog.Filter = "HEX-File|*.hex";
targetedFormat = E2PFileFormat.HEXText;
break;
default: // wtf?
return;
}
if (fileDialog.ShowDialog() != DialogResult.OK) {
manualPagerLock(false);
return;
}
targetFilePath = fileDialog.FileName;
}
using (var comm = new BirdyComm()) {
comm.SetPort(entry.commPortInfo.port);
//if (comm.commPort is CommPortSerial) ((CommPortSerial)comm.commPort).ResetReOpen();
//await comm.ForceGotoProg();
//await Task.Delay(2000);
//if (1 < 3) return false;
try {
var data = await comm.TryGetDeviceInfo();
Console.WriteLine("deviceSerial: {0}", data.deviceSerial);
if (comm.type == BirdyType.Slim && await comm.AskHighspeed() == false) throw new Exception("Highspeed mode did not work");
var e2pContents = new DeviceE2PContents();
e2pContents.readFromFile(targetedFormat, targetFilePath);
Console.WriteLine(e2pContents.data.Count);
await comm.TryWriteE2P(e2pContents);
if (comm.type == BirdyType.Slim && await comm.AskLowspeed() == false) throw new Exception("Highspeed mode did not work"); else Console.WriteLine("E2PDump, Highspeed success");
/*this.Invoke(new Action(() => {
txtLog.AppendText(string.Format("{0} type {1} serial {2}\n", portName, data.model, data.deviceSerial));
}));*/
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
private async void materialButton1_Click_2(object sender, EventArgs e) {
PagerSelectListEntry entry = (PagerSelectListEntry)manualPagerSelect.SelectedItem;
using (var comm = new BirdyComm()) {
comm.SetPort(entry.commPortInfo.port);
//if (comm.commPort is CommPortSerial) ((CommPortSerial)comm.commPort).ResetReOpen();
//await comm.ForceGotoProg();
//await Task.Delay(2000);
//if (1 < 3) return false;
try {
await comm.TryE2PResetViaBootloader();
/*this.Invoke(new Action(() => {
txtLog.AppendText(string.Format("{0} type {1} serial {2}\n", portName, data.model, data.deviceSerial));
}));*/
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
public static byte[] StringToByteArrayFastest(string hex) {
hex = hex.Trim().Trim(' ');
if (hex.Length % 2 == 1)
throw new Exception("The binary key cannot have an odd number of digits");
byte[] arr = new byte[hex.Length >> 1];
for (int i = 0; i < hex.Length >> 1; ++i) {
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
}
return arr;
}
public static int GetHexVal(char hex) {
int val = (int)hex;
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}
////
private async void materialButton5_Click(object sender, EventArgs e) {
try {
var hex = materialTextBox1.Text;
byte[] cmd = StringToByteArrayFastest(hex);
PagerSelectListEntry entry = (PagerSelectListEntry)manualPagerSelect.SelectedItem;
using (var comm = new BirdyComm()) {
comm.SetPort(entry.commPortInfo.port);
Task.WaitAny(comm.commPort.SendCommand(cmd).Task, Task.Delay(2000));
}
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
private void materialTextBox1_TextChanged(object sender, EventArgs e) {
}
private async void materialButton6_Click(object sender, EventArgs e) {
PagerSelectListEntry entry = (PagerSelectListEntry)manualPagerSelect.SelectedItem;
using (var comm = new BirdyComm()) {
comm.SetPort(entry.commPortInfo.port);
try {
var data = await comm.TryGetDeviceInfo();
Console.WriteLine("deviceSerial: {0}", data.deviceSerial);
if (comm.type == BirdyType.Slim && await comm.AskHighspeed() == false) throw new Exception("Highspeed mode did not work"); else Console.WriteLine("E2PDump, Highspeed success");
data = await comm.TryGetDeviceInfo();
Console.WriteLine("deviceSerial: {0}", data.deviceSerial);
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
private void manualInvalidateSelection() {
txtManualSerial.Text = "";
manualPagerSelect.Enabled = true;
btnManualDumpE2P.Enabled = false;
btnManualWriteE2P.Enabled = false;
btnWriteBOSkrypt.Enabled = false;
btnResetE2P.Enabled = false;
btnChangeSerial.Enabled = false;
materialButton5.Enabled = false;
materialButton6.Enabled = false;
btnManualRestart.Enabled = false;
manualPagerSelect.SelectedIndex = -1;
}
private async void btnChangeSerial_Click(object sender, EventArgs e) {
PagerSelectListEntry entry = (PagerSelectListEntry)manualPagerSelect.SelectedItem;
ChangeSerialDialog changeSerialDialog = new ChangeSerialDialog();
changeSerialDialog.Setup(entry.commPortInfo.deviceInfo);
if (changeSerialDialog.ShowDialog() != DialogResult.Yes) return; // if not yes, break
using (var comm = new BirdyComm()) {
comm.SetPort(entry.commPortInfo.port);
try {
var data = await comm.TryGetDeviceInfo();
if (data.deviceSerial != changeSerialDialog.oldSN) throw new Exception(string.Format("dev={0} != old={1} Old Serial does not match with targeted device. SEEK HELP!!", data.deviceSerial, changeSerialDialog.oldSN));
if (await comm.TryChangeSerialNumber(changeSerialDialog.newSN) == true)
manualInvalidateSelection();
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
private void ckbDebug_CheckedChanged(object sender, EventArgs e) {
Properties.Settings.Default.showConsole = ckbDebug.Checked;
Properties.Settings.Default.Save();
ShowHideConsole.ToggleConsoleWindow(Properties.Settings.Default.showConsole);
}
private void addTCPPortToolStripMenuItem_Click(object sender, EventArgs e) {
Console.WriteLine("AddTCP-Port");
DeviceFinder.instance.RegisterNewPort("tcp://127.0.0.1:33333");
}
private void scanForLocalSerialPorts_Click(object sender, EventArgs e) {
Console.WriteLine("Scan For Local Ports");
DeviceFinder.instance.RescanLocalSerialPorts();
}
private void addPagerbaseToolStripMenuItem_Click(object sender, EventArgs e) {
Console.WriteLine("Add Pagerbase");
}
private async void btnManualRestart_Click(object sender, EventArgs e) {
PagerSelectListEntry entry = (PagerSelectListEntry)manualPagerSelect.SelectedItem;
using (var comm = new BirdyComm()) {
comm.SetPort(entry.commPortInfo.port);
try {
var data = await comm.TryGetDeviceInfo();
if (await comm.TryRestart() == true)
manualInvalidateSelection();
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
private void ctxManageTCPPort_RemovePort_Click(object sender, EventArgs e) {
}
private void btnWriteBOSkrypt_Click(object sender, EventArgs e) {
}
}
public class PagerSelectListEntry {
public CommPortInfo commPortInfo;
public PagerSelectListEntry(CommPortInfo commPortInfo) {
this.commPortInfo = commPortInfo;
}
public override string ToString() {
return string.Format("{0} {1}", commPortInfo.deviceInfo.model, commPortInfo.deviceInfo.deviceSerial);
}
}
}