private void ExecuteWriteCoding(bool readOnly = false)
        {
            if (_instanceData.CurrentCoding == null || _instanceData.CurrentCodingType == null)
            {
                return;
            }
            if (IsJobRunning())
            {
                return;
            }

            EdiabasOpen();

            CustomProgressDialog progress = new CustomProgressDialog(this);

            progress.SetMessage(GetString(Resource.String.xml_tool_execute_test_job));
            progress.ButtonAbort.Visibility = ViewStates.Gone;
            progress.Show();

            bool executeFailed = false;
            bool writeFailed   = false;
            bool readFailed    = false;

            _jobThread = new Thread(() =>
            {
                try
                {
                    ActivityCommon.ResolveSgbdFile(_ediabas, _ecuInfo.Sgbd);

                    string codingString   = BitConverter.ToString(_instanceData.CurrentCoding).Replace("-", "");
                    string readJobName    = string.Empty;
                    string readJobArgs    = string.Empty;
                    string readResultName = string.Empty;
                    string writeJobName   = string.Empty;
                    string writeJobArgs   = string.Empty;
                    bool shortCoding      = false;
                    XmlToolActivity.EcuInfoSubSys subSystem = null;
                    switch (_instanceData.CurrentCodingType.Value)
                    {
                    case XmlToolActivity.EcuInfo.CodingType.ShortV1:
                        readJobName    = XmlToolActivity.JobReadEcuVersion;
                        readResultName = "CODIERUNG";
                        shortCoding    = true;
                        break;

                    case XmlToolActivity.EcuInfo.CodingType.ShortV2:
                        readJobName    = XmlToolActivity.JobReadEcuVersion2;
                        readResultName = "GERAETECODIERUNG";
                        shortCoding    = true;
                        break;

                    case XmlToolActivity.EcuInfo.CodingType.LongUds:
                        readJobName    = XmlToolActivity.JobReadS22Uds;
                        readResultName = "ERGEBNIS1WERT";
                        writeJobName   = XmlToolActivity.JobWriteS2EUds;
                        if (_instanceData.SelectedSubsystem == 0)
                        {
                            readJobArgs  = "0x0600";
                            writeJobArgs = readJobArgs + ";" + codingString;
                        }
                        else
                        {
                            if (_ecuInfo.SubSystems == null || _instanceData.SelectedSubsystem >= _ecuInfo.SubSystems.Count)
                            {
                                break;
                            }
                            subSystem    = _ecuInfo.SubSystems[_instanceData.SelectedSubsystem];
                            readJobArgs  = string.Format(CultureInfo.InvariantCulture, "{0}", 0x6000 + subSystem.SubSysAddr);
                            writeJobArgs = readJobArgs + ";" + codingString;
                        }
                        break;

                    case XmlToolActivity.EcuInfo.CodingType.ReadLong:
                        readJobName    = XmlToolActivity.JobReadLongCoding;
                        readResultName = "CODIERUNGWERTBINAER";
                        writeJobName   = XmlToolActivity.JobWriteLongCoding;
                        writeJobArgs   = codingString;
                        break;

                    case XmlToolActivity.EcuInfo.CodingType.CodingS22:
                        readJobName    = XmlToolActivity.JobReadCoding;
                        readResultName = "CODIERUNGWERTBINAER";
                        writeJobName   = XmlToolActivity.JobWriteCoding;
                        writeJobArgs   = codingString;
                        break;
                    }

                    if (!readOnly)
                    {
                        if (string.IsNullOrEmpty(writeJobName))
                        {
                            throw new Exception("Not supported");
                        }
                        _ediabas.ArgString       = writeJobArgs;
                        _ediabas.ArgBinaryStd    = null;
                        _ediabas.ResultsRequests = string.Empty;
                        _ediabas.ExecuteJob(writeJobName);

                        bool resultOk = false;
                        List <Dictionary <string, EdiabasNet.ResultData> > resultSets = _ediabas.ResultSets;
                        if (resultSets != null && resultSets.Count >= 2)
                        {
                            Dictionary <string, EdiabasNet.ResultData> resultDict = resultSets[0];
                            if (resultDict.TryGetValue("JOBSTATUS", out EdiabasNet.ResultData resultData))
                            {
                                if (resultData.OpData is string)
                                {
                                    string result = (string)resultData.OpData;
                                    if (string.Compare(result, "OKAY", StringComparison.OrdinalIgnoreCase) == 0)
                                    {
                                        resultOk = true;
                                    }
                                }
                            }
                        }

                        if (!resultOk)
                        {
                            writeFailed = true;
                        }
                    }

                    {
                        if (string.IsNullOrEmpty(readJobName))
                        {
                            throw new Exception("Not supported");
                        }
                        _ediabas.ArgString       = readJobArgs;
                        _ediabas.ArgBinaryStd    = null;
                        _ediabas.ResultsRequests = string.Empty;
                        _ediabas.ExecuteJob(readJobName);

                        bool resultOk = false;
                        List <Dictionary <string, EdiabasNet.ResultData> > resultSets = _ediabas.ResultSets;
                        if (resultSets != null && resultSets.Count >= 2)
                        {
                            Dictionary <string, EdiabasNet.ResultData> resultDict = resultSets[0];
                            if (resultDict.TryGetValue("JOBSTATUS", out EdiabasNet.ResultData resultData))
                            {
                                if (resultData.OpData is string)
                                {
                                    string result = (string)resultData.OpData;
                                    if (string.Compare(result, "OKAY", StringComparison.OrdinalIgnoreCase) == 0)
                                    {
                                        resultOk = true;
                                    }
                                }
                            }
                            if (resultOk)
                            {
                                Dictionary <string, EdiabasNet.ResultData> resultDict1 = resultSets[1];
                                if (resultDict1.TryGetValue(readResultName, out resultData))
                                {
                                    if (shortCoding)
                                    {
                                        if (resultData.OpData is string text)
                                        {
                                            if (UInt64.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out UInt64 value))
                                            {
                                                _ecuInfo.VagCodingShort = value;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (resultData.OpData is byte[] coding)
                                        {
                                            if (subSystem != null)
                                            {
                                                subSystem.VagCodingLong = coding;
                                            }
                                            else
                                            {
                                                _ecuInfo.VagCodingLong = coding;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (!resultOk)
                        {
                            readFailed = true;
                        }
                    }
                }
                catch (Exception)
                {
                    executeFailed = true;
                }

                RunOnUiThread(() =>
                {
                    if (_activityCommon == null)
                    {
                        return;
                    }
                    progress.Dismiss();
                    progress.Dispose();

                    if (executeFailed || writeFailed)
                    {
                        _activityCommon.ShowAlert(GetString(Resource.String.vag_coding_write_coding_failed), Resource.String.alert_title_error);
                    }
                    else if (readFailed)
                    {
                        _activityCommon.ShowAlert(GetString(Resource.String.vag_coding_read_coding_failed), Resource.String.alert_title_error);
                    }

                    UpdateCoding();
                });
            });
            _jobThread.Start();

            UpdateCodingText();
        }
Exemple #2
0
        /// <summary>
        /// Start adapter detection
        /// </summary>
        /// <param name="deviceAddress">Device Bluetooth address</param>
        /// <param name="deviceName">Device Bleutooth name</param>
        private void DetectAdapter(string deviceAddress, string deviceName)
        {
            CustomProgressDialog progress = new CustomProgressDialog(this);

            progress.SetMessage(GetString(Resource.String.detect_adapter));
            progress.ButtonAbort.Visibility = ViewStates.Gone;
            progress.Show();

            _sbLog.Clear();

            Thread detectThread = new Thread(() =>
            {
                AdapterType adapterType = AdapterType.Unknown;
                try
                {
                    BluetoothDevice device = _btAdapter.GetRemoteDevice(deviceAddress);
                    if (device != null)
                    {
                        BluetoothSocket bluetoothSocket = null;

                        adapterType = AdapterType.ConnectionFailed;
                        if (adapterType == AdapterType.ConnectionFailed)
                        {
                            try
                            {
                                bluetoothSocket = device.CreateRfcommSocketToServiceRecord(SppUuid);
                                if (bluetoothSocket != null)
                                {
                                    try
                                    {
                                        bluetoothSocket.Connect();
                                    }
                                    catch (Exception)
                                    {
                                        // sometimes the second connect is working
                                        bluetoothSocket.Connect();
                                    }
                                    Thread.Sleep(500);
                                    adapterType = AdapterTypeDetection(bluetoothSocket);
                                }
                            }
                            catch (Exception)
                            {
                                adapterType = AdapterType.ConnectionFailed;
                            }
                            finally
                            {
                                bluetoothSocket?.Close();
                            }
                        }

                        if (adapterType == AdapterType.ConnectionFailed)
                        {
                            try
                            {
                                // this socket sometimes looses data for long telegrams
                                IntPtr createRfcommSocket = Android.Runtime.JNIEnv.GetMethodID(device.Class.Handle,
                                                                                               "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
                                if (createRfcommSocket == IntPtr.Zero)
                                {
                                    throw new Exception("No createRfcommSocket");
                                }
                                IntPtr rfCommSocket = Android.Runtime.JNIEnv.CallObjectMethod(device.Handle,
                                                                                              createRfcommSocket, new Android.Runtime.JValue(1));
                                if (rfCommSocket == IntPtr.Zero)
                                {
                                    throw new Exception("No rfCommSocket");
                                }
                                bluetoothSocket = GetObject <BluetoothSocket>(rfCommSocket, Android.Runtime.JniHandleOwnership.TransferLocalRef);
                                if (bluetoothSocket != null)
                                {
                                    bluetoothSocket.Connect();
                                    Thread.Sleep(500);
                                    adapterType = AdapterTypeDetection(bluetoothSocket);
                                }
                            }
                            catch (Exception)
                            {
                                adapterType = AdapterType.ConnectionFailed;
                            }
                            finally
                            {
                                bluetoothSocket?.Close();
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    adapterType = AdapterType.ConnectionFailed;
                }

                RunOnUiThread(() =>
                {
                    progress.Dismiss();
                    progress.Dispose();
                    switch (adapterType)
                    {
                    case AdapterType.ConnectionFailed:
                        _altertInfoDialog = new AlertDialog.Builder(this)
                                            .SetPositiveButton(Resource.String.button_yes, (sender, args) =>
                        {
                            ReturnDeviceType(deviceAddress + ";" + EdBluetoothInterface.RawTag, deviceName);
                        })
                                            .SetNegativeButton(Resource.String.button_no, (sender, args) =>
                        {
                        })
                                            .SetCancelable(true)
                                            .SetMessage(Resource.String.adapter_connection_failed)
                                            .SetTitle(Resource.String.alert_title_error)
                                            .Show();
                        _altertInfoDialog.DismissEvent += (sender, args) =>
                        {
                            _altertInfoDialog = null;
                        };
                        break;

                    case AdapterType.Unknown:
                        {
                            if (!ActivityCommon.IsBtReliable())
                            {
                                _activityCommon.ShowAlert(GetString(Resource.String.can_adapter_bt_not_reliable), Resource.String.alert_title_error);
                                break;
                            }

                            bool yesSelected  = false;
                            _altertInfoDialog = new AlertDialog.Builder(this)
                                                .SetPositiveButton(Resource.String.button_yes, (sender, args) =>
                            {
                                yesSelected = true;
                            })
                                                .SetNegativeButton(Resource.String.button_no, (sender, args) =>
                            {
                            })
                                                .SetCancelable(true)
                                                .SetMessage(Resource.String.unknown_adapter_type)
                                                .SetTitle(Resource.String.alert_title_error)
                                                .Show();
                            _altertInfoDialog.DismissEvent += (sender, args) =>
                            {
                                _altertInfoDialog = null;
                                _activityCommon.RequestSendMessage(_appDataDir, _sbLog.ToString(),
                                                                   PackageManager.GetPackageInfo(PackageName, 0), GetType(), (o, eventArgs) =>
                                {
                                    if (yesSelected)
                                    {
                                        ReturnDeviceType(deviceAddress + ";" + EdBluetoothInterface.RawTag, deviceName);
                                    }
                                });
                            };
                            break;
                        }

                    case AdapterType.Elm327:
                        {
                            _altertInfoDialog = new AlertDialog.Builder(this)
                                                .SetNeutralButton(Resource.String.button_ok, (sender, args) =>
                            {
                                ReturnDeviceType(deviceAddress + ";" + EdBluetoothInterface.Elm327Tag, deviceName);
                            })
                                                .SetCancelable(true)
                                                .SetMessage(Resource.String.adapter_elm_replacement)
                                                .SetTitle(Resource.String.alert_title_info)
                                                .Show();
                            _altertInfoDialog.DismissEvent += (sender, args) =>
                            {
                                _altertInfoDialog = null;
                            };
                            TextView messageView = _altertInfoDialog.FindViewById <TextView>(Android.Resource.Id.Message);
                            if (messageView != null)
                            {
                                messageView.MovementMethod = new LinkMovementMethod();
                            }
                            break;
                        }

                    case AdapterType.Elm327Invalid:
                    case AdapterType.Elm327Fake21:
                        {
                            string message =
                                GetString(adapterType == AdapterType.Elm327Fake21
                                    ? Resource.String.fake_elm_adapter_type
                                    : Resource.String.invalid_adapter_type);
                            message          += "<br>" + GetString(Resource.String.recommened_adapter_type);
                            _altertInfoDialog = new AlertDialog.Builder(this)
                                                .SetNeutralButton(Resource.String.button_ok, (sender, args) =>
                            {
                            })
                                                .SetCancelable(true)
                                                .SetMessage(ActivityCommon.FromHtml(message))
                                                .SetTitle(Resource.String.alert_title_error)
                                                .Show();
                            _altertInfoDialog.DismissEvent += (sender, args) =>
                            {
                                _altertInfoDialog = null;
                                _activityCommon.RequestSendMessage(_appDataDir, _sbLog.ToString(), PackageManager.GetPackageInfo(PackageName, 0), GetType());
                            };
                            TextView messageView = _altertInfoDialog.FindViewById <TextView>(Android.Resource.Id.Message);
                            if (messageView != null)
                            {
                                messageView.MovementMethod = new LinkMovementMethod();
                            }
                            break;
                        }

                    case AdapterType.Custom:
                    case AdapterType.CustomUpdate:
                        _altertInfoDialog = new AlertDialog.Builder(this)
                                            .SetPositiveButton(Resource.String.button_yes, (sender, args) =>
                        {
                            ReturnDeviceType(deviceAddress, deviceName, true);
                        })
                                            .SetNegativeButton(Resource.String.button_no, (sender, args) =>
                        {
                            ReturnDeviceType(deviceAddress, deviceName);
                        })
                                            .SetCancelable(true)
                                            .SetMessage(adapterType == AdapterType.CustomUpdate ? Resource.String.adapter_fw_update : Resource.String.adapter_cfg_required)
                                            .SetTitle(Resource.String.alert_title_info)
                                            .Show();
                        _altertInfoDialog.DismissEvent += (sender, args) =>
                        {
                            _altertInfoDialog = null;
                        };
                        break;

                    case AdapterType.EchoOnly:
                        ReturnDeviceType(deviceAddress + ";" + EdBluetoothInterface.RawTag, deviceName);
                        break;

                    default:
                        ReturnDeviceType(deviceAddress, deviceName);
                        break;
                    }
                });
            })
            {
                Priority = System.Threading.ThreadPriority.Highest
            };

            detectThread.Start();
        }
Exemple #3
0
        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();
        }
Exemple #4
0
        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();
                    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.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();
        }