/// <summary> /// Write the parameter blocks (VIN, problem history, etc) /// </summary> private void writeParametersButton_Click(object sender, EventArgs e) { DelayDialogBox dialogBox = new DelayDialogBox(); DialogResult dialogResult = dialogBox.ShowDialog(); if (dialogResult == DialogResult.Cancel) { return; } if (!BackgroundWorker.IsAlive) { DialogResult result = MessageBox.Show( "This software is still new, and it is not as reliable as commercial software." + Environment.NewLine + "The PCM can be rendered unusuable, and special tools may be needed to make the PCM work again." + Environment.NewLine + "If your PCM stops working, will that make your life difficult?", "Answer carefully...", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) { this.AddUserMessage("Please try again with a less important PCM."); } else { BackgroundWorker = new System.Threading.Thread(() => write_BackgroundThread(WriteType.Parameters)); BackgroundWorker.IsBackground = true; BackgroundWorker.Start(); } } }
/// <summary> /// Write the entire flash. /// </summary> private void writeFullContentsButton_Click(object sender, EventArgs e) { DelayDialogBox dialogBox = new DelayDialogBox(); DialogResult dialogResult = dialogBox.ShowDialog(); if (dialogResult == DialogResult.Cancel) { return; } if (!BackgroundWorker.IsAlive) { DialogResult result = MessageBox.Show( "Changing the operating system can render the PCM unusable." + Environment.NewLine + "Special tools may be needed to make the PCM work again." + Environment.NewLine + "Are you sure you really want to take that risk?", "This is dangerous.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { this.AddUserMessage("You have made a wise choice."); } else { BackgroundWorker = new System.Threading.Thread(() => write_BackgroundThread(WriteType.Full)); BackgroundWorker.IsBackground = true; BackgroundWorker.Start(); } } }
private void testWriteButton_Click(object sender, EventArgs e) { DelayDialogBox dialogBox = new DelayDialogBox(); DialogResult dialogResult = dialogBox.ShowDialog(); if (dialogResult == DialogResult.Cancel) { return; } if (!BackgroundWorker.IsAlive) { BackgroundWorker = new System.Threading.Thread(() => write_BackgroundThread(WriteType.TestWrite)); BackgroundWorker.IsBackground = true; BackgroundWorker.Start(); } }
/// <summary> /// Read the entire contents of the flash. /// </summary> private async void readFullContents_Routine() { try { this.Invoke((MethodInvoker) delegate() { this.DisableUserInput(); this.cancelButton.Enabled = true; }); this.cancellationTokenSource = new CancellationTokenSource(); if (this.vehicle == null) { // This shouldn't be possible - it would mean the buttons // were enabled when they shouldn't be. return; } DelayDialogBox dialogBox = new DelayDialogBox(); DialogResult dialogResult = dialogBox.ShowDialog(); if (dialogResult == DialogResult.Cancel) { return; } this.AddUserMessage("Querying operating system of current PCM."); Response <uint> osidResponse = await this.vehicle.QueryOperatingSystemId(); if (osidResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Operating system query failed, will retry: " + osidResponse.Status); await this.vehicle.ExitKernel(); osidResponse = await this.vehicle.QueryOperatingSystemId(); if (osidResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Operating system query failed, giving up: " + osidResponse.Status); return; } } // Look up the information about this PCM, based on the OSID; this.AddUserMessage("OSID: " + osidResponse.Value); PcmInfo info = new PcmInfo(osidResponse.Value); await this.vehicle.SuppressChatter(); bool unlocked = await this.vehicle.UnlockEcu(info.KeyAlgorithm); if (!unlocked) { this.AddUserMessage("Unlock was not successful."); return; } this.AddUserMessage("Unlock succeeded."); if (this.cancellationTokenSource.Token.IsCancellationRequested) { return; } // Do the actual reading. Response <Stream> readResponse = await this.vehicle.ReadContents(info, this.cancellationTokenSource.Token); if (readResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Read failed, " + readResponse.Status.ToString()); return; } // Get the path to save the image to. // // TODO: remember this value and offer to re-use it, in case // the read fails and the user has to try again. // string path = ""; this.Invoke((MethodInvoker) delegate() { path = this.ShowSaveAsDialog(); }); if (path == null) { this.AddUserMessage("Save canceled."); return; } this.AddUserMessage("Will save to " + path); // Save the contents to the path that the user provided. try { this.AddUserMessage("Saving contents to " + path); readResponse.Value.Position = 0; using (Stream output = File.OpenWrite(path)) { await readResponse.Value.CopyToAsync(output); } } catch (IOException exception) { this.AddUserMessage("Unable to save file: " + exception.Message); this.AddDebugMessage(exception.ToString()); } } catch (Exception exception) { this.AddUserMessage("Read failed: " + exception.ToString()); } finally { this.Invoke((MethodInvoker) delegate() { this.EnableUserInput(); this.cancelButton.Enabled = false; }); // This should not get used again. If it does, that would // indicate a bug, so let's make sure that any attempt to // use it won't go un-noticed. this.cancellationTokenSource = null; } }