internal PacketAnswerFileInfo(PacketAnswer answer, string name)
 {
     if (!Init((UInt16)CommandType.File_GetFileInfo, answer))
     {
         throw WrongTypeException;
     }
     FI = new SdCardDirectoryFileInfo()
     {
         FileDirectorySize = BitConverter.ToInt32(answer.Data, 0),
         CreationTime      = new DateTime(
             HelpMethods.GetNumber(answer.Data[9], answer.Data[10]),
             answer.Data[8],
             answer.Data[7],
             answer.Data[4],
             answer.Data[5],
             answer.Data[6]
             ),
         IsHidden    = answer.Data[11] == 1,
         IsLFN       = answer.Data[12] == 1,
         IsReadOnly  = answer.Data[13] == 1,
         IsSystem    = answer.Data[14] == 1,
         IsDirectory = answer.Data[15] == 1,
         Name        = name
     };
 }
Example #2
0
        public PacketAnswer(Packet base_, bool ignoreValidation)
        {
            if (base_.IsEmpty)
            {
                IsEmpty = true; return;
            }
            ;
            if (base_.Data == null || base_.Data.Length < 4)
            {
                IsEmpty = true; return;
            }
            ;
            Command  = HelpMethods.GetNumber(base_.Data[0], base_.Data[1]);
            Status   = (AnswerStatus)base_.Data[2];
            DataType = ((AnswerDataType)base_.Data[3]);
            Sender   = base_.Sender;
            if (DataType == AnswerDataType.CODE)
            {
                Code = base_.Data[4];
            }
            else if (DataType == AnswerDataType.DATA)
            {
                Data = base_.Data.ToList().GetRange(4, base_.Data.Length - 4).ToArray();
            }

            if (!ignoreValidation && Status == AnswerStatus.ValidationError)
            {
                throw new SecurityException("Необходимо провести валидацию с устройством");
            }
        }
Example #3
0
        private byte[] ReadAsync()
        {
            var lowLenByte  = (byte)Port.ReadByte();
            var highLenByte = (byte)Port.ReadByte();
            var Length      = HelpMethods.GetNumber(lowLenByte, highLenByte) - 255;
            var buffer      = new byte[Length];

            buffer[0] = lowLenByte;
            buffer[1] = highLenByte;
            for (int i = 0; i <= Length - 3; i++)
            {
                buffer[i + 2] = (byte)Port.ReadByte();
            }
            Port.ReadExisting();
            return(buffer);
        }
        public bool Device_SetTime(DateTime dt)
        {
            DateTime now = dt;

            byte[] data = new byte[7] {
                (byte)now.Hour,
                (byte)now.Minute,
                (byte)now.Second,
                (byte)now.Day,
                (byte)now.Month,
                HelpMethods.SplitNumber(now.Year).Item1, HelpMethods.SplitNumber(now.Year).Item2
            };
            var result = GetResult((UInt16)CommandType.SetTime, data);

            return(!result.IsEmpty);
        }
Example #5
0
        static public Packet GetPacket(UInt16 type, byte[] data, Sender sender)
        {
            var result    = new Packet(data, type, sender);
            var resdata   = new byte[result.Size];
            var sizeBytes = HelpMethods.SplitNumber(result.Size + 255);

            resdata[0] = sizeBytes.Item1;
            resdata[1] = sizeBytes.Item2;
            var commandBytes = HelpMethods.SplitNumber((UInt16)type);

            resdata[2] = commandBytes.Item1;
            resdata[3] = commandBytes.Item2;
            resdata[4] = (byte)sender.Type;
            Buffer.BlockCopy(sender.Mask, 0, resdata, 5, 7);
            Buffer.BlockCopy(data, 0, resdata, 12, data.Length);
            Buffer.BlockCopy(result.Crc, 0, resdata, 12 + data.Length, 2);
            result.TotalData = resdata;
            return(result);
        }
Example #6
0
 public static Packet ParsePacket(byte[] data, int len)
 {
     try
     {
         var a = new Packet()
         {
             TotalData = data
         };
         var command    = (UInt16)HelpMethods.GetNumber(data[2], data[3]);
         var sender     = new Sender(SenderType.UnNamedByteMask);
         var sendertype = (SenderType)data[4];
         sender.Type = sendertype;
         Buffer.BlockCopy(data, 5, sender.Mask, 0, 7);
         if (sendertype == SenderType.SevenByteName)
         {
             sender.Name = Encoding.Default.GetString(sender.Mask);
         }
         a.Sender  = sender;
         a.Size    = (short)len;
         a.Command = command;
         a.Data    = new byte[len - 14];
         Buffer.BlockCopy(data, 12, a.Data, 0, len - 14);
         CrCHandler crc = new CrCHandler();
         a.Crc = new byte[2] {
             data[data.Length - 2], data[data.Length - 1]
         };
         var newCrc = crc.ComputeChecksumBytes(a.Data);
         if (a.Crc[0] != newCrc[0] || a.Crc[1] != newCrc[1])
         {
             throw new WrongPacketInputException(a, new Tuple <byte, byte>(newCrc[0], newCrc[1]), new Tuple <byte, byte>(a.Crc[0], a.Crc[1]));
         }
         return(a);
     }
     catch (Exception e)
     {
         throw new WrongPacketInputException(e);
     }
 }
        public DateTimeRequestResult Device_GetTime()
        {
            var res    = new DateTime();
            var result = GetResult((UInt16)CommandType.GetDateTime);

            if (result.IsEmpty)
            {
                return(new DateTimeRequestResult());
            }
            res = new DateTime(
                HelpMethods.GetNumber(result.Data[5], result.Data[6]),
                result.Data[4],
                result.Data[3],
                result.Data[0],
                result.Data[1],
                result.Data[2]
                );
            return(new DateTimeRequestResult()
            {
                Success = true,
                Time = res
            });
        }