public static bool TryReadWrapper(byte[] bytes, out TelemetryWrapper wrapper)
        {
            wrapper = null;
            using (var stream = new MemoryStream(bytes))
            {
                using (var reader = new BinaryReader(stream))
                {
                    if (!reader.TryReadStruct(out TelemetryHeader header))
                    {
                        Debug.WriteLine("Could not read telemetry header", "Serialization Error");
                        return(false);
                    }

                    if (header.MessageSize != reader.BytesRemaining())
                    {
                        Debug.WriteLine($"Invalid telemetry message size {reader.BytesRemaining()} expected {header.MessageSize}", "Serialization Error");
                        return(false);
                    }

                    byte[] content = reader.ReadBytes(header.MessageSize);
                    wrapper = new TelemetryWrapper(header, content);

                    return(true);
                }
            }
        }
Esempio n. 2
0
        public byte[] WriteTelemetry(ITelemetryEvent telemetryEvent, byte[] encryptionKey = null)
        {
            var t = (TelemetryEvent)telemetryEvent;

            if (t == null)
            {
                throw new SerializationException("Unknown telemetry event type");
            }

            byte[] payload;
            switch (t.Message)
            {
            case UavPositionReport r:
                payload = UavPositionReportSerializer.WriteReport(r);
                break;

            default:
                throw new SerializationException($"Unknown message type: {t.MessageType}");
            }

            var wrapper = new TelemetryWrapper(AutpMessageTypes.UavPosition, payload);

            byte[] wrapperBytes = TelemetryWrapperSerializer.WriteWrapper(wrapper);


            wrapperBytes = encryptionKey != null
                ? EncryptBytes(encryptionKey, t.SequenceNumber, wrapperBytes)
                : EncryptBytes(t.TelemetryId, t.SequenceNumber, wrapperBytes);

            var datagram = new UdpTelemetryDatagram(t.TelemetryId, t.SequenceNumber, wrapperBytes, true);

            return(TelemetryDatagramSerializer.WriteDatagram(datagram));
        }
        public static byte[] WriteWrapper(TelemetryWrapper item)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream))
                {
                    try
                    {
                        writer.WriteStruct(item.Header);
                        writer.Write(item.Message);
                    }
                    catch (Exception e) when(!(e is SerializationException))
                    {
                        throw new SerializationException(e.ToString());
                    }
                }

                return(stream.ToArray());
            }
        }