예제 #1
0
        public void Test_packFileInfo()
        {
            byte[]          test  = new byte[] { 0x03, 0x00, 0x00, 0x00 };
            RemoteFile.File file1 = new RemoteFile.File();
            file1.name    = "test.txt";
            file1.length  = 100;
            file1.address = 10000;
            List <byte> data = RemoteFileUtil.packFileInfo(file1, "<");

            Assert.AreEqual(data.Count, RemoteFile.Constants.RMF_FILEINFO_BASE_LEN + file1.name.Length + 1); // +1 null termination
            Assert.IsTrue(data.GetRange(0, 4).SequenceEqual(new List <byte> {
                0x03, 0x00, 0x00, 0x00
            }));
            Assert.IsTrue(data.GetRange(4, 4).SequenceEqual(new List <byte> {
                0x10, 0x27, 0x00, 0x00
            }));
            Assert.IsTrue(data.GetRange(8, 4).SequenceEqual(new List <byte> {
                0x64, 0x00, 0x00, 0x00
            }));
            Assert.IsTrue(data.GetRange(12, 4).SequenceEqual(new List <byte> {
                0x00, 0x00, 0x00, 0x00
            }));
            foreach (byte b in data.GetRange(16, 32))
            {
                Assert.IsTrue(b == 0);
            }
            byte[] bname    = data.GetRange(48, data.Count - 48 - 1).ToArray(); // -1 for null termination
            string fileName = System.Text.Encoding.ASCII.GetString(bname);

            Assert.AreEqual("test.txt", fileName);
            Assert.IsTrue(data[data.Count - 1] == 0);
        }
예제 #2
0
        public void Test_unPackFileClose()
        {
            List <byte> blist = new List <byte> {
                0x0B, 0x00, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12
            };
            uint address = RemoteFileUtil.unPackFileClose(blist, "<");

            Assert.AreEqual(address, (uint)0x12345678);

            try
            {
                List <byte> blist2 = new List <byte> {
                    0x0A, 0x00, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12
                };
                address = RemoteFileUtil.unPackFileClose(blist2, "<");
                Assert.Fail("An exception should have been thrown");
            }
            catch (ArgumentException ae)
            {
                Assert.AreEqual(true, (ae.Message.Length > 0));
            }
            catch (Exception e)
            {
                Assert.Fail(
                    string.Format("Unexpected exception of type {0} caught: {1}",
                                  e.GetType(), e.Message)
                    );
            }
        }
예제 #3
0
 protected override void _processCmd(List <byte> data)
 {
     if (data.Count >= 4)
     {
         uint cmd = BitConverter.ToUInt32(data.GetRange(0, 4).ToArray(), 0);
         if (cmd == RemoteFile.Constants.RMF_CMD_FILE_INFO)
         {
             RemoteFile.File remoteFile = RemoteFileUtil.unPackFileInfo(data);
             for (int i = 0; i < requestedFiles.Count; i++)
             {
                 if (requestedFiles[i].name == remoteFile.name)
                 {
                     requestedFiles[i].address    = remoteFile.address;
                     requestedFiles[i].fileType   = remoteFile.fileType;
                     requestedFiles[i].digestType = remoteFile.digestType;
                     requestedFiles[i].digestData = remoteFile.digestData;
                     requestedFiles[i].open();
                     remoteFileMap.insert(requestedFiles[i]);
                     Msg msg = new Msg(RemoteFile.Constants.RMF_MSG_FILEOPEN, 0, 0, requestedFiles[i].address);
                     requestedFiles.RemoveAt(i);
                     msgQueue.Add(msg);
                 }
                 else
                 {
                     remoteFileMap.insert(remoteFile);
                 }
             }
         }
         else if (cmd == RemoteFile.Constants.RMF_CMD_FILE_OPEN)
         {
             uint address = RemoteFileUtil.unPackFileOpen(data, "<");
             Console.WriteLine("received open command, address: " + address);
             Apx.File file = localFileMap.findByAddress(address);
             if (file != null)
             {
                 Console.WriteLine("received open command, name: " + file.name);
                 file.open();
                 List <byte> fileContent = file.read(0, (int)file.length);
                 if (fileContent.Count > 0)
                 {
                     Msg msg = new Msg(RemoteFile.Constants.RMF_MSG_WRITE_DATA, file.address, 0, fileContent);
                     msgQueue.Add(msg);
                 }
             }
         }
         else if (cmd == RemoteFile.Constants.RMF_CMD_FILE_CLOSE)
         {
             throw new NotImplementedException();
         }
         else
         {
             throw new ArgumentException("Unknown command, cannot process");
         }
     }
     else
     {
         throw new ArgumentException("too short command to proccess");
     }
 }
예제 #4
0
 public void ping()
 {
     if (isConnected && tcpStream.CanWrite)
     {
         List <byte> data    = RemoteFileUtil.packHeader(RemoteFile.Constants.RMF_CMD_START_ADDR, false);
         List <byte> payload = BitConverter.GetBytes(Constants.RMF_CMD_FILE_HEARTBEAT_REQ).ToList();
         data.AddRange(payload);
         send(data);
     }
 }
예제 #5
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));
        }
예제 #6
0
        protected override void processMessage(Msg msg)
        {
            if (msg.msgType == RemoteFile.Constants.RMF_MSG_CONNECT)
            {
                throw new System.ArgumentException("Not applicable for c# version (handled in onConnected)");
            }
            else if (msg.msgType == RemoteFile.Constants.RMF_MSG_FILEINFO)
            {
                Apx.File file = (Apx.File)msg.msgData3;
                Console.WriteLine("processMessage, Name of file: " + file.name);

                List <byte> data    = RemoteFileUtil.packHeader(RemoteFile.Constants.RMF_CMD_START_ADDR, false);
                List <byte> payload = RemoteFileUtil.packFileInfo(file, "<");
                data.AddRange(payload);

                if (transmitHandler != null)
                {
                    transmitHandler.send(data);
                }
            }
            else if (msg.msgType == RemoteFile.Constants.RMF_MSG_WRITE_DATA)
            {
                uint        address = msg.msgData1;
                List <byte> data    = RemoteFileUtil.packHeader(address);
                List <byte> payload = (List <byte>)msg.msgData3;
                data.AddRange(payload);

                if (transmitHandler != null)
                {
                    transmitHandler.send(data);
                }
            }
            else if (msg.msgType == RemoteFile.Constants.RMF_MSG_FILEOPEN)
            {
                List <byte> data    = RemoteFileUtil.packHeader(RemoteFile.Constants.RMF_CMD_START_ADDR);
                List <byte> payload = RemoteFileUtil.packFileOpen(msg.msgData1);
                data.AddRange(payload);

                if (transmitHandler != null)
                {
                    transmitHandler.send(data);
                }
            }
            else
            {
                throw new System.ArgumentException("Unknown msgType");
            }
        }
예제 #7
0
        public void Test_packFileClose()
        {
            List <byte> blist = new List <byte>();

            blist = RemoteFileUtil.packFileClose(0x12345678, "<");
            // Address
            Assert.AreEqual(blist[7], (byte)0x12);
            Assert.AreEqual(blist[6], (byte)0x34);
            Assert.AreEqual(blist[5], (byte)0x56);
            Assert.AreEqual(blist[4], (byte)0x78);
            // Command (RMF_CMD_FILE_CLOSE == 11 / 0x0B)
            Assert.AreEqual(blist[3], (byte)0x00);
            Assert.AreEqual(blist[2], (byte)0x00);
            Assert.AreEqual(blist[1], (byte)0x00);
            Assert.AreEqual(blist[0], (byte)0x0B);
        }
예제 #8
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
 }
예제 #9
0
        public void Test_unPackFileInfo()
        {
            // Header content
            List <byte> data = new List <byte> {
                0x03, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };
            // Digest data
            List <byte> ddata = Enumerable.Repeat((byte)0, 32).ToList();

            data.AddRange(ddata);
            // Name
            List <byte> ndata = ASCIIEncoding.ASCII.GetBytes("test.txt").ToList();

            data.AddRange(ndata);
            // Null termination
            data.Add(0);
            RemoteFile.File file = RemoteFileUtil.unPackFileInfo(data, "<");

            Assert.AreEqual(file.address, (uint)10000);
            Assert.IsTrue(file.digestData.SequenceEqual(ddata));
            Assert.AreEqual(file.digestType, RemoteFile.Constants.RMF_DIGEST_TYPE_NONE);
            Assert.AreEqual(file.name, "test.txt");
        }