/// <summary> /// Ask all of the devices on the VPW bus for permission to switch to 4X speed. /// </summary> private async Task <List <byte> > RequestHighSpeedPermission(ToolPresentNotifier notifier) { Message permissionCheck = this.protocol.CreateHighSpeedPermissionRequest(DeviceId.Broadcast); await this.device.SendMessage(permissionCheck); // Note that as of right now, the AllPro only receives 6 of the 11 responses. // So until that gets fixed, we could miss a 'refuse' response and try to switch // to 4X anyhow. That just results in an aborted read attempt, with no harm done. List <byte> result = new List <byte>(); Message response = null; bool anyRefused = false; while ((response = await this.device.ReceiveMessage()) != null) { this.logger.AddDebugMessage("Parsing " + response.GetBytes().ToHex()); Protocol.HighSpeedPermissionResult parsed = this.protocol.ParseHighSpeedPermissionResponse(response); if (!parsed.IsValid) { await Task.Delay(100); continue; } result.Add(parsed.DeviceId); if (parsed.PermissionGranted) { this.logger.AddUserMessage(string.Format("Module 0x{0:X2} ({1}) has agreed to enter high-speed mode.", parsed.DeviceId, DeviceId.DeviceCategory(parsed.DeviceId))); // Forcing a notification message should help ELM devices receive responses. await notifier.ForceNotify(); await Task.Delay(100); continue; } this.logger.AddUserMessage(string.Format("Module 0x{0:X2} ({1}) has refused to enter high-speed mode.", parsed.DeviceId, DeviceId.DeviceCategory(parsed.DeviceId))); anyRefused = true; } if (anyRefused) { return(null); } return(result); }
/// <summary> /// Does everything required to switch to VPW 4x /// </summary> public async Task <bool> VehicleSetVPW4x(VpwSpeed newSpeed, ToolPresentNotifier notifier) { if (!device.Supports4X) { if (newSpeed == VpwSpeed.FourX) { // where there is no support only report no switch to 4x logger.AddUserMessage("This interface does not support VPW 4x"); } return(true); } // Configure the vehicle bus when switching to 4x if (newSpeed == VpwSpeed.FourX) { logger.AddUserMessage("Attempting switch to VPW 4x"); await device.SetTimeout(TimeoutScenario.ReadProperty); // The list of modules may not be useful after all, but // checking for an empty list indicates an uncooperative // module on the VPW bus. List <byte> modules = await this.RequestHighSpeedPermission(notifier); if (modules == null) { // A device has refused the switch to high speed mode. return(false); } Message broadcast = this.protocol.CreateBeginHighSpeed(DeviceId.Broadcast); await this.device.SendMessage(broadcast); // Check for any devices that refused to switch to 4X speed. // These responses usually get lost, so this code might be pointless. Message response = null; while ((response = await this.device.ReceiveMessage()) != null) { Response <bool> refused = this.protocol.ParseHighSpeedRefusal(response); if (refused.Status != ResponseStatus.Success) { // This should help ELM devices receive responses. await notifier.ForceNotify(); continue; } if (refused.Value == false) { // TODO: Add module number. this.logger.AddUserMessage("Module refused high-speed switch."); return(false); } } } else { logger.AddUserMessage("Reverting to VPW 1x"); } // Request the device to change await device.SetVpwSpeed(newSpeed); TimeoutScenario scenario = newSpeed == VpwSpeed.Standard ? TimeoutScenario.ReadProperty : TimeoutScenario.ReadMemoryBlock; await device.SetTimeout(scenario); return(true); }