Example #1
0
        private void kCmdResetSessions()
        {
            var payload = new FTPPayloadHeader()
            {
                opcode     = FTPOpcode.kCmdResetSessions,
                seq_number = seq_no++,
                session    = 0
            };

            fileTransferProtocol.payload = payload;

            RetryTimeout timeout = new RetryTimeout();

            var sub = _mavint.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.FILE_TRANSFER_PROTOCOL, message =>
            {
                var msg = (MAVLink.mavlink_file_transfer_protocol_t)message.data;
                FTPPayloadHeader ftphead = msg.payload;

                log.Info(ftphead.opcode + " " + ftphead.req_opcode + " " + ftphead.size);

                // error at far end
                if (ftphead.opcode == FTPOpcode.kRspNak)
                {
                    var errorcode = (FTPErrorCode)ftphead.data[0];
                    if (errorcode == FTPErrorCode.kErrFailErrno)
                    {
                        var _ftp_errno = (FTPErrorCode)ftphead.data[1];
                        log.Error(ftphead.req_opcode + " " + errorcode + " " + _ftp_errno);
                    }
                    else
                    {
                        log.Error(ftphead.req_opcode + " " + errorcode);
                    }

                    return(true);
                }

                // not for us or bad seq no
                if (payload.opcode != ftphead.req_opcode || payload.seq_number + 1 != ftphead.seq_number)
                {
                    return(true);
                }

                // only ack's
                if (ftphead.opcode != FTPOpcode.kRspAck)
                {
                    return(true);
                }

                timeout.Complete = true;

                return(true);
            });

            timeout.WorkToDo = () => _mavint.sendPacket(fileTransferProtocol, _sysid, _compid);

            timeout.DoWork();

            //_mavint.sendPacket(fileTransferProtocol, _sysid, _compid);

            log.Info(payload.opcode);

            _mavint.UnSubscribeToPacketType(sub);
        }
Example #2
0
        private List <FtpFileInfo> kCmdListDirectory(string dir)
        {
            List <FtpFileInfo> answer = new List <FtpFileInfo>();

            fileTransferProtocol.target_system    = _sysid;
            fileTransferProtocol.target_component = _compid;
            fileTransferProtocol.target_network   = 0;
            var payload = new FTPPayloadHeader()
            {
                opcode     = FTPOpcode.kCmdListDirectory,
                data       = ASCIIEncoding.ASCII.GetBytes(dir),
                seq_number = seq_no++,
                offset     = 0
            };

            fileTransferProtocol.payload = payload;


            var timeout = new RetryTimeout();

            var sub = _mavint.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.FILE_TRANSFER_PROTOCOL,
                                                    message =>
            {
                var msg = (MAVLink.mavlink_file_transfer_protocol_t)message.data;
                FTPPayloadHeader ftphead = msg.payload;
                // error at far end
                if (ftphead.opcode == FTPOpcode.kRspNak)
                {
                    var errorcode = (FTPErrorCode)ftphead.data[0];
                    if (errorcode == FTPErrorCode.kErrFailErrno)
                    {
                        var _ftp_errno = (FTPErrorCode)ftphead.data[1];
                    }

                    if (errorcode == FTPErrorCode.kErrEOF)
                    {
                        timeout.Complete = true;
                    }


                    log.Error(ftphead.req_opcode + " " + errorcode);

                    return(true);
                }

                // not for us or bad seq no
                if (payload.opcode != ftphead.req_opcode || payload.seq_number + 1 != ftphead.seq_number)
                {
                    return(true);
                }

                // only ack's
                if (ftphead.opcode != FTPOpcode.kRspAck)
                {
                    return(true);
                }

                var requested_offset = ftphead.offset;
                var offset           = 0;

                while (offset < ftphead.size)
                {
                    var b = ftphead.data[offset++];
                    switch (b)
                    {
                    case kDirentFile:
                        var filename = new StringBuilder();
                        while (b != 0x0)
                        {
                            b = ftphead.data[offset++];
                            if (b != 0x0)
                            {
                                filename.Append((char)b);
                            }
                        }

                        var items = filename.ToString().Split('\t');

                        var size = int.Parse(items[1]);

                        answer.Add(new FtpFileInfo(items[0], dir, false, (int)size));
                        break;

                    case kDirentDir:
                        var name = new StringBuilder();
                        while (b != 0x0)
                        {
                            b = ftphead.data[offset++];
                            if (b != 0x0)
                            {
                                name.Append((char)b);
                            }
                        }

                        answer.Add(new FtpFileInfo(name.ToString(), dir, true));
                        break;

                    case kDirentSkip:
                        while (b != 0x0)
                        {
                            b = ftphead.data[offset++];
                        }

                        answer.Add(new FtpFileInfo("", dir, true));
                        break;

                    default:
                        var nameextra = new StringBuilder();
                        while (b != 0x0)
                        {
                            b = ftphead.data[offset++];
                            if (b != 0x0)
                            {
                                nameextra.Append((char)b);
                            }
                        }

                        if (nameextra.ToString() != "")
                        {
                            answer.Add(new FtpFileInfo(nameextra.ToString(), dir, false));
                        }
                        break;
                    }
                }

                payload.offset               = (uint)answer.Count;
                payload.seq_number           = seq_no++;
                fileTransferProtocol.payload = payload;
                _mavint.sendPacket(fileTransferProtocol, _sysid, _compid);
                timeout.ResetTimeout();

                return(true);
            });

            timeout.WorkToDo = () => _mavint.sendPacket(fileTransferProtocol, _sysid, _compid);

            timeout.DoWork();

            _mavint.UnSubscribeToPacketType(sub);
            return(answer);
        }
Example #3
0
        private MemoryStream kCmdReadFile(string file, int size)
        {
            RetryTimeout timeout = new RetryTimeout();
            KeyValuePair <MAVLink.MAVLINK_MSG_ID, Func <MAVLink.MAVLinkMessage, bool> > sub;
            var payload = new FTPPayloadHeader()
            {
                opcode     = FTPOpcode.kCmdReadFile,
                data       = ASCIIEncoding.ASCII.GetBytes(file),
                seq_number = seq_no++,
                offset     = 0
            };

            fileTransferProtocol.payload = payload;

            log.Info("get " + payload.opcode + " " + file + " " + size);

            MemoryStream answer = new MemoryStream();

            sub = _mavint.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.FILE_TRANSFER_PROTOCOL, message =>
            {
                var msg = (MAVLink.mavlink_file_transfer_protocol_t)message.data;
                FTPPayloadHeader ftphead = msg.payload;
                // error at far end
                if (ftphead.opcode == FTPOpcode.kRspNak)
                {
                    var errorcode = (FTPErrorCode)ftphead.data[0];
                    if (errorcode == FTPErrorCode.kErrFailErrno)
                    {
                        var _ftp_errno = (FTPErrorCode)ftphead.data[1];
                        log.Error(ftphead.req_opcode + " " + errorcode + " " + _ftp_errno);
                    }
                    else
                    {
                        log.Error(ftphead.req_opcode + " " + errorcode);
                    }

                    return(true);
                }

                // not for us or bad seq no
                if (payload.opcode != ftphead.req_opcode || payload.seq_number + 1 != ftphead.seq_number)
                {
                    return(true);
                }

                // only ack's
                if (ftphead.opcode != FTPOpcode.kRspAck)
                {
                    return(true);
                }

                // log.Debug(ftphead.req_opcode + " " + file + " " + ftphead.size + " " + ftphead.offset);

                answer.Seek(ftphead.offset, SeekOrigin.Begin);
                answer.Write(ftphead.data, 0, ftphead.size);

                if (ftphead.offset + ftphead.size >= size)
                {
                    timeout.Complete = true;
                    return(true);
                }

                payload.offset               = ftphead.offset + ftphead.size;
                payload.seq_number           = seq_no++;
                fileTransferProtocol.payload = payload;
                _mavint.sendPacket(fileTransferProtocol, _sysid, _compid);
                timeout.ResetTimeout();

                return(true);
            });

            timeout.WorkToDo = () => _mavint.sendPacket(fileTransferProtocol, _sysid, _compid);

            timeout.DoWork();

            _mavint.UnSubscribeToPacketType(sub);
            return(answer);
        }
Example #4
0
        public void kCmdOpenFileRO(string file, out int size)
        {
            fileTransferProtocol.target_system    = _sysid;
            fileTransferProtocol.target_component = _compid;
            fileTransferProtocol.target_network   = 0;

            var payload = new FTPPayloadHeader()
            {
                opcode     = FTPOpcode.kCmdOpenFileRO,
                data       = ASCIIEncoding.ASCII.GetBytes(file),
                seq_number = seq_no++,
                session    = 0
            };

            fileTransferProtocol.payload = payload;

            var timeout = new RetryTimeout();

            size = 0;
            var localsize = size;

            var sub = _mavint.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.FILE_TRANSFER_PROTOCOL, message =>
            {
                var msg = (MAVLink.mavlink_file_transfer_protocol_t)message.data;
                FTPPayloadHeader ftphead = msg.payload;
                // error at far end
                if (ftphead.opcode == FTPOpcode.kRspNak)
                {
                    var errorcode = (FTPErrorCode)ftphead.data[0];
                    if (errorcode == FTPErrorCode.kErrFailErrno)
                    {
                        var _ftp_errno = ftphead.data[1];
                        log.Error(ftphead.req_opcode + " " + errorcode + " " + _ftp_errno);
                    }
                    else
                    {
                        log.Error(ftphead.req_opcode + " " + errorcode);
                    }

                    if (errorcode == FTPErrorCode.kErrNoSessionsAvailable)
                    {
                        kCmdResetSessions();
                    }

                    return(true);
                }

                // not for us or bad seq no
                if (payload.opcode != ftphead.req_opcode || payload.seq_number + 1 != ftphead.seq_number)
                {
                    return(true);
                }

                // only ack's
                if (ftphead.opcode != FTPOpcode.kRspAck)
                {
                    return(true);
                }

                localsize = BitConverter.ToInt32(ftphead.data, 0);

                log.Info(ftphead.req_opcode + " " + file + " " + localsize);

                timeout.Complete = true;

                return(true);
            });

            timeout.WorkToDo = () => _mavint.sendPacket(fileTransferProtocol, _sysid, _compid);

            timeout.DoWork();

            _mavint.UnSubscribeToPacketType(sub);

            size = localsize;
        }