Beispiel #1
0
        private async Task <bool> SendCommandWithBuffer(UInt32 command, byte[] buff)
        {
            if (!_isConnected)
            {
                return(await Task.FromResult(false));
            }
            else
            {
                if (_analyzerChar != null)
                {
                    byte[] frame = new byte[4 + buff.Length];

                    // Copio il codice del comando
                    ArrConverter.SetBufferFromUInt32(command, frame, 0);
                    buff.CopyTo(frame, 4);

                    await SendFrame(frame);
                }
                else
                {
                    return(await Task.FromResult(false));
                }
            }
            return(await Task.FromResult(true));
        }
Beispiel #2
0
        private async Task <bool> SendFrame(byte[] frame)
        {
            if (!_isConnected)
            {
                return(await Task.FromResult(false));
            }
            else
            {
                if (_analyzerChar != null)
                {
                    _responseFrame = null;
                    byte[] frameToSend = new byte[8 + frame.Length];

                    // Imposto l'header
                    Encoding.ASCII.GetBytes(_frameMarker).CopyTo(frameToSend, 0);

                    // Imposto il frame da inviare
                    frame.CopyTo(frameToSend, 4);

                    // Calcolo e imposto il crc
                    UInt32 crc = Crc32_STM.CalculateFromBuffer(frameToSend, frameToSend.Length - 4);
                    ArrConverter.SetBufferFromUInt32(crc, frameToSend, frameToSend.Length - 4);

                    await _analyzerChar.WriteAsync(frameToSend);
                }
                else
                {
                    return(await Task.FromResult(false));
                }
            }
            return(await Task.FromResult(true));
        }
Beispiel #3
0
        private async Task <Response> SendReceiveCommand(UInt32 command, byte[] buffer)
        {
            await SendReceiveInitCommand((UInt32)(4 + buffer.Length));
            await SendCommandWithBuffer(command, buffer);

            var receivedBuff = await ReceiveFrame();

            // La risposta è composta da:
            // 1 - un marker (4 byte)
            // 2 - il codice di errore (4 byte)
            // 3 - un codice di risultato (4 byte)
            // 4 - se necessario un buffer
            // 5 - il crc del messaggio (4 byte)
            var result = new Response();

            result.responseData = ArrConverter.GetUInt32FromBuffer(receivedBuff, 8);

            if (receivedBuff.Length > 16)
            {
                result.responseBuff = receivedBuff.Skip(12).Take(receivedBuff.Length - 16).ToArray();
            }
            else
            {
                result.responseBuff = null;
            }

            return(result);
        }
Beispiel #4
0
        // Invia il frame di start della comunicazione
        // indicando la lunghezza del frame successivo
        // compresi la dimensione dell'header e del CRC finale
        private async Task <bool> SendReceiveInitCommand(UInt32 nextLength)
        {
            if (!_isConnected)
            {
                return(await Task.FromResult(false));
            }
            else
            {
                try
                {
                    if (_analyzerChar != null)
                    {
                        byte[] frame = new byte[4];

                        // Copio la lunghezza del messaggio da inviare successivamente
                        ArrConverter.SetBufferFromUInt32(nextLength, frame, 0);

                        await SendFrame(frame);
                        await ReceiveFrame();

                        return(await Task.FromResult(true));
                    }
                    else
                    {
                        return(await Task.FromResult(false));
                    }
                }
                catch (Exception e)
                {
                    return(await Task.FromResult(false));
                }
            }
        }
Beispiel #5
0
        public async Task <List <int> > GetSpyFileSizes(SpyFileType type)
        {
            var fileSizes = new List <int>();

            UInt32 getNumCommand;
            UInt32 getFileSizeCommand;

            if (type == SpyFileType.FileTypeCAN1)
            {
                getNumCommand      = 0x00000003;
                getFileSizeCommand = 0x00000009;
            }
            else if (type == SpyFileType.FileTypeCAN2)
            {
                getNumCommand      = 0x00000004;
                getFileSizeCommand = 0x0000000A;
            }
            else if (type == SpyFileType.FileTypeK)
            {
                getNumCommand      = 0x00000005;
                getFileSizeCommand = 0x0000000B;
            }
            else
            {
                throw new Exception("FileType not implemented!");
            }

            var getNumResponse = await SendReceiveCommand(getNumCommand);

            var filesNum = getNumResponse.responseData;

            for (UInt32 fileIndex = 0; fileIndex < filesNum; fileIndex++)
            {
                var fileIndexBuf = new byte[4];
                ArrConverter.SetBufferFromUInt32(fileIndex, fileIndexBuf, 0);

                var getFileSizeResponse = await SendReceiveCommand(getFileSizeCommand, fileIndexBuf);

                UInt32 fileSize = getFileSizeResponse.responseData;
                fileSizes.Add((int)fileSize);
            }

            return(fileSizes);
        }
Beispiel #6
0
        public async Task <List <string> > GetSpyFileNames(SpyFileType type)
        {
            var fileNames = new List <string>();

            UInt32 getNumCommand;
            UInt32 getFileNameCommand;

            if (type == SpyFileType.FileTypeCAN1)
            {
                getNumCommand      = 0x00000003;
                getFileNameCommand = 0x00000006;
            }
            else if (type == SpyFileType.FileTypeCAN2)
            {
                getNumCommand      = 0x00000004;
                getFileNameCommand = 0x00000007;
            }
            else if (type == SpyFileType.FileTypeK)
            {
                getNumCommand      = 0x00000005;
                getFileNameCommand = 0x00000008;
            }
            else
            {
                throw new Exception("FileType not implemented!");
            }

            var getNumResponse = await SendReceiveCommand(getNumCommand);

            var filesNum = getNumResponse.responseData;

            for (UInt32 fileIndex = 0; fileIndex < filesNum; fileIndex++)
            {
                var fileIndexBuf = new byte[4];
                ArrConverter.SetBufferFromUInt32(fileIndex, fileIndexBuf, 0);

                var getFileNameResponse = await SendReceiveCommand(getFileNameCommand, fileIndexBuf);

                var fileName = Encoding.ASCII.GetString(getFileNameResponse.responseBuff);
                fileNames.Add(fileName);
            }

            return(fileNames);
        }
Beispiel #7
0
        public async Task <bool> SetCANParametersAsync(SpyType type, CANSpyParameters param)
        {
            const UInt32 setParamCAN1Spy = 0x00010003;
            const UInt32 setParamCAN2Spy = 0x00020003;

            byte[] paramData = new byte[CANSpyParameters.ParamSize];
            ArrConverter.SetBufferFromUInt32((UInt32)param.BitTiming, paramData, 0);
            ArrConverter.SetBufferFromUInt32((UInt32)param.FrameFormat, paramData, 4);

            if (param.ErrorReception)
            {
                ArrConverter.SetBufferFromUInt32((UInt32)1, paramData, 8);
            }
            else
            {
                ArrConverter.SetBufferFromUInt32((UInt32)0, paramData, 8);
            }

            if (param.ApplyMask)
            {
                ArrConverter.SetBufferFromUInt32((UInt32)1, paramData, 12);
            }
            else
            {
                ArrConverter.SetBufferFromUInt32((UInt32)0, paramData, 12);
            }

            ArrConverter.SetBufferFromUInt32((UInt32)param.Mask, paramData, 16);
            ArrConverter.SetBufferFromUInt32((UInt32)param.ID, paramData, 20);

            if (type == SpyType.CANSpyOne)
            {
                await SendReceiveCommand(setParamCAN1Spy, paramData);
            }
            else if (type == SpyType.CANSpyTwo)
            {
                await SendReceiveCommand(setParamCAN2Spy, paramData);
            }

            return(await Task.FromResult(true));
        }
Beispiel #8
0
        private async Task <byte[]> ReceiveFrame()
        {
            const UInt32 waitCommand    = 0x00040000;
            const UInt32 packetCommand  = 0x3F3F3F3F;
            const UInt32 bigFileCommand = 0x3F3F0001;

            try
            {
                if (!_isConnected)
                {
                    return(await Task.FromResult(new byte[0]));
                }
                else
                {
                    bool isWaitCommand = false;

                    do
                    {
                        await Task.Delay(100);

                        await _analyzerChar.StopUpdatesAsync();

                        if (_responseFrame == null || _responseFrame.Length == 0)
                        {
                            throw new Exception("dimensione della risposta errata!");
                        }

                        // Controllo il marker
                        string marker = Encoding.ASCII.GetString(_responseFrame, 0, 4);
                        if (marker != _frameMarker)
                        {
                            string tmp = "";
                            foreach (byte data in _responseFrame)
                            {
                                tmp += data.ToString("X2");
                            }

                            throw new Exception("invalid marker, received: " + tmp);
                        }

                        // Controllo il crc
                        UInt32 crcSent = ArrConverter.GetUInt32FromBuffer(_responseFrame, _responseFrame.Length - 4);
                        UInt32 crcCalc = Crc32_STM.CalculateFromBuffer(_responseFrame, _responseFrame.Length - 4);

                        if (crcSent != crcCalc)
                        {
                            throw new Exception("invalid crc");
                        }

                        // Controllo se ho una risposta con codice di errore
                        UInt32 errorCode = ArrConverter.GetUInt32FromBuffer(_responseFrame, 4);
                        if (errorCode != 0)
                        {
                            throw new Exception(errorCode.ToString());
                        }

                        if (_responseFrame.Length == 16)
                        {
                            if (ArrConverter.GetUInt32FromBuffer(_responseFrame, 8) == waitCommand)
                            {
                                isWaitCommand = true;
                            }
                        }
                        else if (_responseFrame.Length == 20)
                        {
                            if (ArrConverter.GetUInt32FromBuffer(_responseFrame, 8) == packetCommand)
                            {
                                int totalSize  = (int)ArrConverter.GetUInt32FromBuffer(_responseFrame, 12);
                                int packetsNum = ((totalSize % 20) == 0) ? (totalSize / 20) : ((totalSize / 20) + 1);

                                var fullResponse = new byte[totalSize];

                                // Il device cerca di trasmettere un pacchetto
                                await _analyzerChar.StartUpdatesAsync();
                                await SendCommand(packetCommand);

                                for (var packetIndex = 0; packetIndex < packetsNum; packetIndex++)
                                {
                                    await Task.Delay(100);

                                    await _analyzerChar.StopUpdatesAsync();

                                    if (_responseFrame == null || _responseFrame.Length == 0)
                                    {
                                        throw new Exception("dimensione della risposta errata!");
                                    }

                                    _responseFrame.CopyTo(fullResponse, 20 * packetIndex);

                                    await _analyzerChar.StartUpdatesAsync();
                                    await SendCommand(packetCommand);
                                }

                                return(await Task.FromResult(fullResponse));
                            }
                            else if (ArrConverter.GetUInt32FromBuffer(_responseFrame, 8) == bigFileCommand)
                            {
                                int totalSize      = (int)ArrConverter.GetUInt32FromBuffer(_responseFrame, 12);
                                int receivedLength = 0;

                                // Alloco un buffer per l'intero file e il preambolo
                                // Per adesso ignoro il preambolo e copio soltanto il buffer contenente il file
                                byte[] fullRespose = new byte[totalSize + 16];

                                // Il device cerca di trasmettere un pacchetto
                                await _analyzerChar.StartUpdatesAsync();
                                await SendCommand(packetCommand);

                                while (receivedLength < totalSize)
                                {
                                    await Task.Delay(100);

                                    await _analyzerChar.StopUpdatesAsync();

                                    if (_responseFrame == null || _responseFrame.Length == 0)
                                    {
                                        throw new Exception("dimensione della risposta errata!");
                                    }

                                    // Copio partendo da 12 e poi in base alla dimensione dei pacchetti ricevuti
                                    Buffer.BlockCopy(_responseFrame, 0, fullRespose, receivedLength + 12, _responseFrame.Length);

                                    MessagingCenter.Send <IAnalyzerDevice, double>(this, "DownloadFileProgress", (double)receivedLength / totalSize);

                                    receivedLength += _responseFrame.Length;

                                    await _analyzerChar.StartUpdatesAsync();
                                    await SendCommand(packetCommand);
                                }

                                return(await Task.FromResult(fullRespose));
                            }
                        }

                        await _analyzerChar.StartUpdatesAsync();
                    }while (isWaitCommand);
                }

                await _analyzerChar.StartUpdatesAsync();

                return(await Task.FromResult(_responseFrame));
            }
            catch (Exception ex)
            {
                await _analyzerChar.StartUpdatesAsync();

                throw ex;
            }
        }