public static void FreeRunRead(string port) { AdapterProtocol ap = new AdapterProtocol(port); Task.Run(() => { Console.WriteLine("press any key to cancel..."); Console.ReadKey(); ap.Cancel(); }); try { ap.Open(); while (true) { byte data = ap.GetByte(0); // no timeout Console.WriteLine("0x{0:X2}", data); } } catch (Exception ex) { Console.WriteLine("fatal error: {0}", ex.Message); } finally { Console.WriteLine("closed serial port"); ap.Close(); } }
public static string ReadModel(BaseDevice device) { string model = string.Empty; if (device.TwiKfdtoolDevice.ComPort == string.Empty) { throw new ArgumentException("No device selected"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(device.TwiKfdtoolDevice.ComPort); ap.Open(); ap.Clear(); byte mod = ap.ReadModelId(); if (mod == 0x00) { model = "NOT SET"; } else if (mod == 0x01) { model = "KFD100"; } else if (mod == 0x02) { model = "KFD-AVR"; } else { model = "UNKNOWN"; } } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(model); }
private void Start_Button_Click(object sender, RoutedEventArgs e) { if (Settings.SelectedDevice.DeviceType != BaseDevice.DeviceTypeOptions.TwiKfdtool) { ErrorEmulation(string.Format("The device type {0} does not support MR emulation", Settings.SelectedDevice.DeviceType.ToString())); return; } if (Settings.SelectedDevice.TwiKfdtoolDevice.ComPort == string.Empty) { ErrorEmulation("No device selected"); return; } StartEmulation(); Task.Run(() => { ap = null; try { ap = new AdapterProtocol(Settings.SelectedDevice.TwiKfdtoolDevice.ComPort); ap.Open(); ap.Clear(); twp = new ThreeWireProtocol(ap); twp.StatusChanged += OnProgressUpdated; twp.MrRunProducer(); } catch (Exception ex) { ErrorEmulation(ex.Message); } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Log.Warn("could not close serial port: {0}", ex.Message); } } }); }
public static string ReadUniqueId(BaseDevice device) { string uniqueId = string.Empty; if (device.TwiKfdtoolDevice.ComPort == string.Empty) { throw new ArgumentException("No device selected"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(device.TwiKfdtoolDevice.ComPort); ap.Open(); ap.Clear(); byte[] id = ap.ReadUniqueId(); if (id.Length == 0) { uniqueId = "NONE"; } else { uniqueId = BitConverter.ToString(id).Replace("-", string.Empty); } } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(uniqueId); }
public static string ReadHardwareRevision(BaseDevice device) { string version = string.Empty; if (device.TwiKfdtoolDevice.ComPort == string.Empty) { throw new ArgumentException("No device selected"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(device.TwiKfdtoolDevice.ComPort); ap.Open(); ap.Clear(); byte[] ver = ap.ReadHardwareRevision(); if (ver[0] == 0x00 && ver[1] == 0x00) { version = "NOT SET"; } else { version = string.Format("{0}.{1}", ver[0], ver[1]); } } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(version); }
public static string ReadSerialNumber(string port) { string serialNumber = string.Empty; if (port == string.Empty) { throw new ArgumentException("port empty"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(port); ap.Open(); ap.Clear(); byte[] ser = ap.ReadSerialNumber(); if (ser.Length == 0) { serialNumber = "NONE"; } else { serialNumber = Encoding.ASCII.GetString(ser); } } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(serialNumber); }
private void Start_Button_Click(object sender, RoutedEventArgs e) { Settings.InProgressScreen = "NavigateP25MrEmulator"; StartEmulation(); Task.Run(() => { if (Settings.Port == string.Empty) { throw new ArgumentException("port empty"); } ap = null; try { ap = new AdapterProtocol(Settings.Port); ap.Open(); ap.Clear(); twp = new ThreeWireProtocol(ap); twp.StatusChanged += OnProgressUpdated; twp.MrRunProducer(); } catch (Exception ex) { ErrorEmulation(ex.Message); } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { //Logger.Warn("could not close serial port: {0}", ex.Message); } } }); }
public static RspChangeoverInfo ActivateKeyset(BaseDevice device, int keysetSuperseded, int keysetActivated) { RspChangeoverInfo result = new RspChangeoverInfo(); if (device.TwiKfdtoolDevice.ComPort == string.Empty) { throw new ArgumentException("No device selected"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(device.TwiKfdtoolDevice.ComPort); ap.Open(); ap.Clear(); ManualRekeyApplication mra = new ManualRekeyApplication(ap); //result = mra.LoadConfig(kmfRsi, mnp); result = mra.ActivateKeyset(keysetSuperseded, keysetActivated); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(result); }
public static RspRsiInfo LoadConfig(string port, int kmfRsi, int mnp) { RspRsiInfo result = new RspRsiInfo(); if (port == string.Empty) { throw new ArgumentException("port empty"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(port); ap.Open(); ap.Clear(); ManualRekeyApplication mra = new ManualRekeyApplication(ap); result = mra.LoadConfig(kmfRsi, mnp); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(result); }
public static string ReadFirmwareVersion(string port) { string version = string.Empty; if (port == string.Empty) { throw new ArgumentException("port empty"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(port); ap.Open(); ap.Clear(); byte[] ver = ap.ReadFirmwareVersion(); version = string.Format("{0}.{1}.{2}", ver[0], ver[1], ver[2]); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(version); }
public static int ViewKmfRsi(string port) { int result = new int(); if (port == string.Empty) { throw new ArgumentException("port empty"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(port); ap.Open(); ap.Clear(); ManualRekeyApplication mra = new ManualRekeyApplication(ap); result = mra.ViewKmfRsi(); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(result); }
public static RspRsiInfo ChangeRsi(BaseDevice device, int rsiOld, int rsiNew, int mnp) { RspRsiInfo result = new RspRsiInfo(); if (device.TwiKfdtoolDevice.ComPort == string.Empty) { throw new ArgumentException("No device selected"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(device.TwiKfdtoolDevice.ComPort); ap.Open(); ap.Clear(); ManualRekeyApplication mra = new ManualRekeyApplication(ap); result = mra.ChangeRsi(rsiOld, rsiNew, mnp); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(result); }
public static List <RspKeysetInfo> ViewKeysetTaggingInfo(BaseDevice device) { List <RspKeysetInfo> result = new List <RspKeysetInfo>(); if (device.TwiKfdtoolDevice.ComPort == string.Empty) { throw new ArgumentException("No device selected"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(device.TwiKfdtoolDevice.ComPort); ap.Open(); ap.Clear(); ManualRekeyApplication mra = new ManualRekeyApplication(ap); result = mra.ViewKeysetTaggingInfo(); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(result); }
public static void Keyload(string port, bool useActiveKeyset, int keysetId, int sln, int keyId, int algId, List <byte> key) { if (port == string.Empty) { throw new ArgumentException("port empty"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(port); ap.Open(); ap.Clear(); ManualRekeyApplication mra = new ManualRekeyApplication(ap); mra.Keyload(useActiveKeyset, keysetId, sln, keyId, algId, key); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } }
public static void EraseAllKeys(BaseDevice device) { if (device.TwiKfdtoolDevice.ComPort == string.Empty) { throw new ArgumentException("No device selected"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(device.TwiKfdtoolDevice.ComPort); ap.Open(); ap.Clear(); ManualRekeyApplication mra = new ManualRekeyApplication(ap); mra.EraseAllKeys(); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } }
public static void CheckTargetMrConnection(string port) { if (port == string.Empty) { throw new ArgumentException("port empty"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(port); ap.Open(); ap.Clear(); ThreeWireProtocol twp = new ThreeWireProtocol(ap); twp.CheckTargetMrConnection(); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } }
public static void EraseKey(string port, List <CmdKeyItem> keys) { if (port == string.Empty) { throw new ArgumentException("port empty"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(port); ap.Open(); ap.Clear(); ManualRekeyApplication mra = new ManualRekeyApplication(ap); mra.EraseKeys(keys); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } }
public static void EnterBslMode(string port) { if (port == string.Empty) { throw new ArgumentException("port empty"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(port); ap.Open(); ap.Clear(); ap.EnterBslMode(); } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } }
private void Initialize_Button_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show(string.Format("This function is only to be used to bootstrap blank hardware that you built yourself{0}{0}You should only use this function if you know what it does{0}{0}Continue?", Environment.NewLine), "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No) { return; } Logger.Debug("update package path: {0}", UpdatePackage); if (UpdatePackage.Equals(string.Empty)) { Logger.Error("no file selected"); MessageBox.Show("No file selected", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } // file existed when it was selected but now doesn't if (!File.Exists(UpdatePackage)) { Logger.Error("selected file does not exist"); MessageBox.Show("Selected file does not exist", "Error", MessageBoxButton.OK, MessageBoxImage.Error); UpdatePackage = string.Empty; lblPath.Content = NO_FILE_SELECTED; return; } // check if package is cfp or ufp string ext = System.IO.Path.GetExtension(UpdatePackage); Logger.Debug("update package extension: {0}", ext); List <Update> pkg = null; if (ext == ".cfp") { Logger.Debug("opening compressed update package"); try { pkg = Firmware.OpenCompressedUpdatePackage(UpdatePackage); } catch (Exception ex) { Logger.Error("unable to parse file: {0}", ex.Message); MessageBox.Show(string.Format("Unable to parse file: {0}", ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else if (ext == ".ufp") { Logger.Debug("opening uncompressed update package"); try { pkg = Firmware.OpenUncompressedUpdatePackage(UpdatePackage); } catch (Exception ex) { Logger.Error("unable to parse file: {0}", ex.Message); MessageBox.Show(string.Format("Unable to parse file: {0}", ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else { Logger.Error("invalid file selected"); MessageBox.Show("Invalid file selected", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } Logger.Debug("items in update package: {0}", pkg.Count); if (pkg.Count < 1) { Logger.Error("update package does not contain any updates"); MessageBox.Show("Update package does not contain any updates", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } int selIndex = cboAvailPkg.SelectedIndex; if (selIndex < 0) { Logger.Error("invalid selected update: {0}", selIndex); MessageBox.Show(string.Format("Invalid selected update: {0}", selIndex), "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } UpdateItem itm = UpdItm[selIndex]; Update fw = pkg[itm.PkgIndex]; if (fw != null) { Logger.Debug("found applicable firmware update in package"); } else { Logger.Error("could not find applicable firmware update in package"); MessageBox.Show("Could not find applicable firmware update in package", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } Logger.Debug("firmware audience: {0}", fw.Audience); // warn the user if the firmware audience is anything other than release if (fw.Audience != "RELEASE") { string message = string.Format( "THIS FIRMWARE IS NOT FOR GENERAL USE{0}" + "{0}" + "IT MAY NOT WORK CORRECTLY OR AT ALL{0}" + "{0}" + "Audience: {1}{0}" + "{0}" + "DO NOT PROCEED UNLESS YOU HAVE BEEN{0}" + "DIRECTED TO DO SO IN ORDER TO TROUBLESHOOT AN ISSUE{0}" + "{0}" + "Continue?", Environment.NewLine, fw.Audience); if (MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No) { return; } } // check if any devices are in app mode int appDeviceCount = ManualDetection.DetectConnectedAppDevices().Count; Logger.Debug("app devices detected: {0}", appDeviceCount); // check if any devices are in bsl mode int bslDeviceCount = ManualDetection.DetectConnectedBslDevices(); Logger.Debug("bsl devices detected: {0}", bslDeviceCount); // there should not be more than one device attached at the same time if (appDeviceCount > 0) { Logger.Error("one or more devices is connected in APP mode"); MessageBox.Show("One or more devices is connected in APP mode", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } else if (bslDeviceCount > 1) { Logger.Error("more than one device is connected in bsl mode"); MessageBox.Show("More than one device is connected in BSL mode", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } else if (bslDeviceCount == 1) { Logger.Debug("device is already in bsl mode"); } else { Logger.Debug("no device is connected in bsl mode"); MessageBox.Show("No device is connected in BSL mode", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (MessageBox.Show(string.Format("Make sure that you have selected the correct model and hardware revision - an incorrect choice may damage the device{0}{0}Continue?", Environment.NewLine), "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No) { return; } Logger.Debug("updating device"); bool updateResult = false; try { FirmwareUpdate upd = new FirmwareUpdate(); updateResult = upd.UpdateDevice(fw); } catch (Exception ex) { Logger.Error("failed to load firmware: {0}", ex.Message); MessageBox.Show(string.Format("Failed to load firmware: {0}", ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (updateResult) { Logger.Debug("update success"); } else { Logger.Error("update failed"); MessageBox.Show("Firmware update failed", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } Thread.Sleep(5000); // TODO do this better - added to fix catching the device as it first connects and is not ready yet List <string> connected = ManualDetection.DetectConnectedAppDevices(); if (connected.Count > 1) { Logger.Error("more than one device detected"); MessageBox.Show("More than one device detected", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } else if (connected.Count == 1) { Logger.Debug("one device found"); } else { Logger.Error("no device detected"); MessageBox.Show("No device detected", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } try { AdapterProtocol ap = new AdapterProtocol(connected[0]); ap.Open(); ap.Clear(); ap.WriteInfo(itm.MdlId, itm.HwRevMaj, itm.HwRevMin); ap.Close(); } catch (Exception ex) { Logger.Error("failed to write model and hardware revision: {0}", ex.Message); MessageBox.Show(string.Format("Failed to write model and hardware revision: {0}", ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } MessageBox.Show("Firmware Updated Successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information); }
public static string SelfTest(BaseDevice device) { string result = string.Empty; if (device.TwiKfdtoolDevice.ComPort == string.Empty) { throw new ArgumentException("No device selected"); } AdapterProtocol ap = null; try { ap = new AdapterProtocol(device.TwiKfdtoolDevice.ComPort); ap.Open(); ap.Clear(); byte res = ap.SelfTest(); if (res == 0x01) { result = string.Format("Data shorted to ground (0x{0:X2})", res); } else if (res == 0x02) { result = string.Format("Sense shorted to ground (0x{0:X2})", res); } else if (res == 0x03) { result = string.Format("Data shorted to power (0x{0:X2})", res); } else if (res == 0x04) { result = string.Format("Sense shorted to power (0x{0:X2})", res); } else if (res == 0x05) { result = string.Format("Data and Sense shorted (0x{0:X2})", res); } else if (res == 0x06) { result = string.Format("Sense and Data shorted (0x{0:X2})", res); } else if (res != 0x00) { result = string.Format("Unknown self test result (0x{0:X2})", res); } } catch (Exception) { throw; } finally { try { if (ap != null) { ap.Close(); } } catch (System.IO.IOException ex) { Logger.Warn("could not close serial port: {0}", ex.Message); } } return(result); }