Esempio n. 1
0
        public void Test_unpackHeader()
        {
            // Valid data
            RemoteFileUtil.headerReturn res = RemoteFileUtil.unpackHeader(new byte[] { 0x00, 0x00 });
            Assert.IsTrue((res.more_bit == false) && (res.address == 0) && (res.bytes_parsed == 2));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x40, 0x00 });
            Assert.IsTrue((res.more_bit == true) && (res.address == 0) && (res.bytes_parsed == 2));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x44, 0xD2 });
            Assert.IsTrue((res.more_bit == true) && (res.address == 1234) && (res.bytes_parsed == 2));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x3F, 0xFF });
            Assert.IsTrue((res.more_bit == false) && (res.address == 16383) && (res.bytes_parsed == 2));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x7F, 0xFF });
            Assert.IsTrue((res.more_bit == true) && (res.address == 16383) && (res.bytes_parsed == 2));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x80, 0x00, 0x40, 0x00 });
            Assert.IsTrue((res.more_bit == false) && (res.address == 16384) && (res.bytes_parsed == 4));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0xC0, 0x00, 0x40, 0x00 });
            Assert.IsTrue((res.more_bit == true) && (res.address == 16384) && (res.bytes_parsed == 4));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0xBF, 0xFF, 0xFF, 0xFF });
            Assert.IsTrue((res.more_bit == false) && (res.address == 1073741823) && (res.bytes_parsed == 4));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF });
            Assert.IsTrue((res.more_bit == true) && (res.address == 1073741823) && (res.bytes_parsed == 4));

            // Longer data fields
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x00, 0x00, 0x00 });
            Assert.IsTrue((res.more_bit == false) && (res.address == 0) && (res.bytes_parsed == 2));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x00, 0x00, 0x00, 0x00 });
            Assert.IsTrue((res.more_bit == false) && (res.address == 0) && (res.bytes_parsed == 2));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF });
            Assert.IsTrue((res.more_bit == true) && (res.address == 16383) && (res.bytes_parsed == 2));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0xBF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF });
            Assert.IsTrue((res.more_bit == false) && (res.address == 1073741823) && (res.bytes_parsed == 4));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0xBF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF });
            Assert.IsTrue((res.more_bit == false) && (res.address == 1073741823) && (res.bytes_parsed == 4));

            // Too short data fields
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x00 });
            Assert.IsTrue((res.more_bit == false) && (res.address == uint.MaxValue) && (res.bytes_parsed == 0));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x4E });
            Assert.IsTrue((res.more_bit == false) && (res.address == uint.MaxValue) && (res.bytes_parsed == 0));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0xC0 });
            Assert.IsTrue((res.more_bit == false) && (res.address == uint.MaxValue) && (res.bytes_parsed == 0));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0xC0, 0x00 });
            Assert.IsTrue((res.more_bit == false) && (res.address == uint.MaxValue) && (res.bytes_parsed == 0));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x80, 0x00, 0x40 });
            Assert.IsTrue((res.more_bit == false) && (res.address == uint.MaxValue) && (res.bytes_parsed == 0));
            res = RemoteFileUtil.unpackHeader(new byte[] { 0x80, 0x00 });
            Assert.IsTrue((res.more_bit == false) && (res.address == uint.MaxValue) && (res.bytes_parsed == 0));
        }
Esempio n. 2
0
 public override void onMsgReceived(List <byte> msg)
 {
     RemoteFileUtil.headerReturn header = RemoteFileUtil.unpackHeader(msg.ToArray());
     if (header.bytes_parsed > 0)
     {
         if (header.address == Constants.RMF_CMD_START_ADDR)
         {
             _processCmd(msg.GetRange(header.bytes_parsed, msg.Count - header.bytes_parsed));
         }
         else if (header.address < Constants.RMF_CMD_START_ADDR)
         {
             _processFileWrite(header.address, header.more_bit, msg.GetRange(header.bytes_parsed, msg.Count - header.bytes_parsed));
         }
         else
         {
             throw new ArgumentException("invalid address: " + header.address.ToString());
         }
     }
     // else do nothing
 }