private void PerformUpdate() { EdiabasInit(); Android.App.ProgressDialog progress = new Android.App.ProgressDialog(this); progress.SetCancelable(false); progress.SetMessage(GetString(Resource.String.can_adapter_fw_update_active)); progress.Show(); _adapterThread = new Thread(() => { bool updateOk = false; bool connectOk = false; try { connectOk = !InterfacePrepare(); BluetoothSocket bluetoothSocket = EdBluetoothInterface.BluetoothSocket; if (bluetoothSocket == null) { connectOk = false; } else { connectOk = true; updateOk = PicBootloader.FwUpdate(bluetoothSocket); } } catch (Exception) { updateOk = false; } RunOnUiThread(() => { if (IsJobRunning()) { _adapterThread.Join(); } progress.Hide(); progress.Dispose(); string message; if (updateOk) { message = GetString(Resource.String.can_adapter_fw_update_ok); } else { message = connectOk ? GetString(Resource.String.can_adapter_fw_update_failed) : GetString(Resource.String.can_adapter_fw_update_conn_failed); } _activityCommon.ShowAlert(message, updateOk ? Resource.String.alert_title_info : Resource.String.alert_title_error); UpdateDisplay(); if (updateOk) { PerformRead(); } }); }); _adapterThread.Start(); UpdateDisplay(); }
/// <summary> /// Detects the CAN adapter type /// </summary> /// <param name="bluetoothSocket">Bluetooth socket for communication</param> /// <returns>Adapter type</returns> private AdapterType AdapterTypeDetection(BluetoothSocket bluetoothSocket) { const int versionRespLen = 9; byte[] customData = { 0x82, 0xF1, 0xF1, 0xFD, 0xFD, 0x5E }; AdapterType adapterType = AdapterType.Unknown; try { Stream bluetoothInStream = bluetoothSocket.InputStream; Stream bluetoothOutStream = bluetoothSocket.OutputStream; { // custom adapter bluetoothInStream.Flush(); while (bluetoothInStream.IsDataAvailable()) { bluetoothInStream.ReadByte(); } LogData(customData, 0, customData.Length, "Send"); bluetoothOutStream.Write(customData, 0, customData.Length); LogData(null, 0, 0, "Resp"); List <byte> responseList = new List <byte>(); long startTime = Stopwatch.GetTimestamp(); for (; ;) { while (bluetoothInStream.IsDataAvailable()) { int data = bluetoothInStream.ReadByte(); if (data >= 0) { LogByte((byte)data); responseList.Add((byte)data); startTime = Stopwatch.GetTimestamp(); } } if (responseList.Count >= customData.Length + versionRespLen) { LogString("Custom adapter length"); bool validEcho = !customData.Where((t, i) => responseList[i] != t).Any(); if (!validEcho) { LogString("*** Echo incorrect"); break; } byte checkSum = 0x00; for (int i = 0; i < versionRespLen - 1; i++) { checkSum += responseList[i + customData.Length]; } if (checkSum != responseList[customData.Length + versionRespLen - 1]) { LogString("*** Checksum incorrect"); break; } int adapterTypeId = responseList[customData.Length + 5] + (responseList[customData.Length + 4] << 8); int fwVersion = responseList[customData.Length + 7] + (responseList[customData.Length + 6] << 8); int fwUpdateVersion = PicBootloader.GetFirmwareVersion((uint)adapterTypeId); if (fwUpdateVersion >= 0 && fwUpdateVersion > fwVersion) { LogString("Custom adapter with old firmware detected"); return(AdapterType.CustomUpdate); } LogString("Custom adapter detected"); return(AdapterType.Custom); } if (Stopwatch.GetTimestamp() - startTime > ResponseTimeout * TickResolMs) { if (responseList.Count >= customData.Length) { bool validEcho = !customData.Where((t, i) => responseList[i] != t).Any(); if (validEcho) { LogString("Valid echo detected"); adapterType = AdapterType.EchoOnly; } } break; } } } // ELM327 bool elmReports21 = false; for (int i = 0; i < 2; i++) { bluetoothInStream.Flush(); while (bluetoothInStream.IsDataAvailable()) { bluetoothInStream.ReadByte(); } byte[] sendData = Encoding.UTF8.GetBytes("ATI\r"); LogData(sendData, 0, sendData.Length, "Send"); bluetoothOutStream.Write(sendData, 0, sendData.Length); string response = GetElm327Reponse(bluetoothInStream); if (response != null) { if (response.Contains("ELM327")) { LogString("ELM327 detected"); if (response.Contains("ELM327 v2.1")) { LogString("Version 2.1 detected"); elmReports21 = true; } adapterType = AdapterType.Elm327; break; } } } if (adapterType == AdapterType.Elm327) { foreach (string command in EdBluetoothInterface.Elm327InitCommands) { bluetoothInStream.Flush(); while (bluetoothInStream.IsDataAvailable()) { bluetoothInStream.ReadByte(); } byte[] sendData = Encoding.UTF8.GetBytes(command + "\r"); LogData(sendData, 0, sendData.Length, "Send"); bluetoothOutStream.Write(sendData, 0, sendData.Length); string response = GetElm327Reponse(bluetoothInStream); if (response == null) { LogString("*** No ELM response"); adapterType = AdapterType.Elm327Invalid; break; } if (!response.Contains("OK\r")) { LogString("*** No ELM OK found"); adapterType = AdapterType.Elm327Invalid; break; } } if (adapterType == AdapterType.Elm327Invalid && elmReports21) { adapterType = AdapterType.Elm327Fake21; } } } catch (Exception) { return(AdapterType.Unknown); } return(adapterType); }
private void UpdateDisplay() { bool requestFwUpdate = false; bool bEnabled = !IsJobRunning(); bool fwUpdateEnabled = bEnabled; bool expertMode = _checkBoxExpert.Checked; _buttonRead.Enabled = bEnabled; _buttonWrite.Enabled = bEnabled; _editTextBtPin.Enabled = bEnabled && _btPin != null && _btPin.Length >= 4; int maxPinLength = (_btPin != null && _btPin.Length > 0) ? _btPin.Length : 4; _editTextBtPin.SetFilters(new Android.Text.IInputFilter[] { new Android.Text.InputFilterLengthFilter(maxPinLength) }); if (!_editTextBtPin.Enabled) { _editTextBtPin.Text = string.Empty; } _editTextBtName.Enabled = bEnabled && _btName != null && _btName.Length > 0; int maxTextLength = (_btName != null && _btName.Length > 0) ? _btName.Length : 16; _editTextBtName.SetFilters(new Android.Text.IInputFilter[] { new Android.Text.InputFilterLengthFilter(maxTextLength) }); if (!_editTextBtName.Enabled) { _editTextBtName.Text = string.Empty; } _textViewSerNum.Enabled = bEnabled; if (bEnabled) { if ((_separationTime < 0) || (_separationTime >= _spinnerCanAdapterSepTimeAdapter.Items.Count)) { _spinnerCanAdapterSepTime.SetSelection(0); } else { _spinnerCanAdapterSepTime.SetSelection(_separationTime); if (_separationTime != 0) { expertMode = true; } } if ((_blockSize < 0) || (_blockSize >= _spinnerCanAdapterBlockSizeAdapter.Items.Count)) { _spinnerCanAdapterBlockSize.SetSelection(0); } else { _spinnerCanAdapterBlockSize.SetSelection(_blockSize); if (_blockSize != 0) { expertMode = true; } } // moved down because of expert mode setting if (_activityCommon.SelectedInterface == ActivityCommon.InterfaceType.Bluetooth) { if (_canMode == (int)AdapterMode.Can100) { expertMode = true; } _spinnerCanAdapterModeAdapter.Items.Clear(); if (_adapterType >= 0x0002 && _fwVersion >= 0x0008) { _spinnerCanAdapterModeAdapter.Items.Add(new StringObjType(GetString(Resource.String.button_can_adapter_can_auto), AdapterMode.CanAuto)); } _spinnerCanAdapterModeAdapter.Items.Add(new StringObjType(GetString(Resource.String.button_can_adapter_can_500), AdapterMode.Can500)); if (expertMode) { _spinnerCanAdapterModeAdapter.Items.Add(new StringObjType(GetString(Resource.String.button_can_adapter_can_100), AdapterMode.Can100)); } _spinnerCanAdapterModeAdapter.Items.Add(new StringObjType(GetString(Resource.String.button_can_adapter_can_off), AdapterMode.CanOff)); _spinnerCanAdapterModeAdapter.NotifyDataSetChanged(); int indexMode = 0; for (int i = 0; i < _spinnerCanAdapterModeAdapter.Count; i++) { if ((int)_spinnerCanAdapterModeAdapter.Items[i].Data == _canMode) { indexMode = i; } } _spinnerCanAdapterMode.SetSelection(indexMode); } if (_editTextBtPin.Enabled && _btPin != null) { string btPin = PinDataToString(_btPin); _editTextBtPin.Text = btPin.Length >= 4 ? btPin : "1234"; } if (_editTextBtName.Enabled && _btName != null) { try { int length = _btName.TakeWhile(value => value != 0x00).Count(); _editTextBtName.Text = Encoding.UTF8.GetString(_btName, 0, length); } catch (Exception) { _editTextBtName.Text = string.Empty; } } string ignitionText = string.Empty; if (_ignitionState >= 0) { ignitionText = (_ignitionState & 0x01) != 0x00 ? GetString(Resource.String.can_adapter_ignition_on) : GetString(Resource.String.can_adapter_ignition_off); if ((_ignitionState & 0x80) != 0) { ignitionText = "(" + ignitionText + ")"; } } _textViewIgnitionState.Text = ignitionText; string voltageText = string.Empty; if (_adapterType > 1 && _batteryVoltage >= 0) { voltageText = string.Format(ActivityMain.Culture, "{0,4:0.0}V", (double)_batteryVoltage / 10); } _textViewBatteryVoltage.Text = voltageText; string versionText = string.Empty; if (_adapterType >= 0 && _fwVersion >= 0) { versionText = string.Format(ActivityMain.Culture, "{0}.{1} / ", (_fwVersion >> 8) & 0xFF, _fwVersion & 0xFF); int fwUpdateVersion = PicBootloader.GetFirmwareVersion((uint)_adapterType); if (fwUpdateVersion >= 0) { if (!_fwUpdateShown && _fwVersion < fwUpdateVersion) { requestFwUpdate = true; } versionText += string.Format(ActivityMain.Culture, "{0}.{1}", (fwUpdateVersion >> 8) & 0xFF, fwUpdateVersion & 0xFF); } else { versionText += "--"; } fwUpdateEnabled = fwUpdateVersion >= 0 && ((_fwVersion != fwUpdateVersion) || ActivityCommon.CollectDebugInfo); } _textViewFwVersion.Text = versionText; if (_textViewSerNum.Enabled) { _textViewSerNum.Text = (_serNum == null) ? string.Empty : BitConverter.ToString(_serNum).Replace("-", ""); } } _buttonFwUpdate.Enabled = fwUpdateEnabled; _spinnerCanAdapterMode.Enabled = bEnabled; _spinnerCanAdapterSepTime.Enabled = bEnabled && expertMode; _spinnerCanAdapterBlockSize.Enabled = bEnabled && expertMode; _checkBoxExpert.Enabled = bEnabled; _checkBoxExpert.Checked = expertMode; if (requestFwUpdate) { _fwUpdateShown = true; new AlertDialog.Builder(this) .SetPositiveButton(Resource.String.button_yes, (sender, args) => { PerformUpdateMessage(); }) .SetNegativeButton(Resource.String.button_no, (sender, args) => { }) .SetCancelable(true) .SetMessage(Resource.String.can_adapter_fw_update_present) .SetTitle(Resource.String.alert_title_question) .Show(); } HideKeyboard(); }
private void PerformUpdate() { EdiabasInit(); CustomProgressDialog progress = new CustomProgressDialog(this); progress.SetMessage(GetString(Resource.String.can_adapter_fw_update_active)); progress.ButtonAbort.Visibility = ViewStates.Gone; progress.Show(); _adapterThread = new Thread(() => { bool updateOk = false; bool connectOk = false; try { connectOk = InterfacePrepare(); Stream inStream = null; Stream outStream = null; if (connectOk) { switch (_activityCommon.SelectedInterface) { case ActivityCommon.InterfaceType.Bluetooth: { BluetoothSocket bluetoothSocket = EdBluetoothInterface.BluetoothSocket; if (bluetoothSocket == null) { connectOk = false; break; } inStream = bluetoothSocket.InputStream; outStream = bluetoothSocket.OutputStream; break; } case ActivityCommon.InterfaceType.DeepObdWifi: { NetworkStream networkStream = EdCustomWiFiInterface.NetworkStream; if (networkStream == null) { connectOk = false; break; } inStream = networkStream; outStream = networkStream; break; } } } if (inStream == null || outStream == null) { connectOk = false; } if (connectOk) { updateOk = PicBootloader.FwUpdate(inStream, outStream); } } catch (Exception) { updateOk = false; } RunOnUiThread(() => { if (_activityCommon == null) { return; } if (IsJobRunning()) { _adapterThread.Join(); } progress.Dismiss(); progress.Dispose(); string message; if (updateOk) { message = GetString(Resource.String.can_adapter_fw_update_ok); } else { message = connectOk ? GetString(Resource.String.can_adapter_fw_update_failed) : GetString(Resource.String.can_adapter_fw_update_conn_failed); } _activityCommon.ShowAlert(message, updateOk ? Resource.String.alert_title_info : Resource.String.alert_title_error); UpdateDisplay(); if (updateOk) { PerformRead(); } }); }); _adapterThread.Start(); UpdateDisplay(); }