PX4 Firmware
PX4 Autopilot Software http://px4.io
|
MAVLink remote file server. Support FTP like commands using MAVLINK_MSG_ID_FILE_TRANSFER_PROTOCOL message. More...
#include <mavlink_ftp.h>
Classes | |
struct | _session_info |
Session info, fd=-1 for no active session. More... | |
struct | SessionInfo |
Public Types | |
enum | Opcode : uint8_t { kCmdNone, kCmdTerminateSession, kCmdResetSessions, kCmdListDirectory, kCmdOpenFileRO, kCmdReadFile, kCmdCreateFile, kCmdWriteFile, kCmdRemoveFile, kCmdCreateDirectory, kCmdRemoveDirectory, kCmdOpenFileWO, kCmdTruncateFile, kCmdRename, kCmdCalcFileCRC32, kCmdBurstReadFile, kRspAck = 128, kRspNak } |
Command opcodes. More... | |
enum | ErrorCode : uint8_t { kErrNone, kErrFail, kErrFailErrno, kErrInvalidDataSize, kErrInvalidSession, kErrNoSessionsAvailable, kErrEOF, kErrUnknownCommand, kErrFailFileExists, kErrFailFileProtected } |
Error codes returned in Nak response PayloadHeader.data[0]. More... | |
typedef void(* | ReceiveMessageFunc_t) (const mavlink_file_transfer_protocol_t *ftp_req, void *worker_data) |
Public Member Functions | |
MavlinkFTP (Mavlink *mavlink) | |
~MavlinkFTP () | |
void | send (const hrt_abstime t) |
Handle sending of messages. More... | |
void | handle_message (const mavlink_message_t *msg) |
Handle possible FTP message. More... | |
void | set_unittest_worker (ReceiveMessageFunc_t rcvMsgFunc, void *worker_data) |
Sets up the server to run in unit test mode. More... | |
struct | __attribute__ ((__packed__)) PayloadHeader |
This is the payload which is in mavlink_file_transfer_protocol_t.payload. More... | |
unsigned | get_size () |
Private Member Functions | |
char * | _data_as_cstring (PayloadHeader *payload) |
Guarantees that the payload data is null terminated. More... | |
void | _process_request (mavlink_file_transfer_protocol_t *ftp_req, uint8_t target_system_id, uint8_t target_comp_id) |
Processes an FTP message. More... | |
void | _reply (mavlink_file_transfer_protocol_t *ftp_req) |
Sends the specified FTP response message out through mavlink. More... | |
int | _copy_file (const char *src_path, const char *dst_path, size_t length) |
Copy file (with limited space) More... | |
ErrorCode | _workList (PayloadHeader *payload, bool list_hidden=false) |
Responds to a List command. More... | |
ErrorCode | _workOpen (PayloadHeader *payload, int oflag) |
Responds to an Open command. More... | |
ErrorCode | _workRead (PayloadHeader *payload) |
Responds to a Read command. More... | |
ErrorCode | _workBurst (PayloadHeader *payload, uint8_t target_system_id, uint8_t target_component_id) |
Responds to a Stream command. More... | |
ErrorCode | _workWrite (PayloadHeader *payload) |
Responds to a Write command. More... | |
ErrorCode | _workTerminate (PayloadHeader *payload) |
Responds to a Terminate command. More... | |
ErrorCode | _workReset (PayloadHeader *payload) |
Responds to a Reset command. More... | |
ErrorCode | _workRemoveDirectory (PayloadHeader *payload) |
Responds to a RemoveDirectory command. More... | |
ErrorCode | _workCreateDirectory (PayloadHeader *payload) |
Responds to a CreateDirectory command. More... | |
ErrorCode | _workRemoveFile (PayloadHeader *payload) |
Responds to a RemoveFile command. More... | |
ErrorCode | _workTruncateFile (PayloadHeader *payload) |
Responds to a TruncateFile command. More... | |
ErrorCode | _workRename (PayloadHeader *payload) |
Responds to a Rename command. More... | |
ErrorCode | _workCalcFileCRC32 (PayloadHeader *payload) |
Responds to a CalcFileCRC32 command. More... | |
uint8_t | _getServerSystemId (void) |
uint8_t | _getServerComponentId (void) |
uint8_t | _getServerChannel (void) |
bool | _ensure_buffers_exist () |
make sure that the working buffers _work_buffer* are allocated More... | |
MavlinkFTP (const MavlinkFTP &) | |
MavlinkFTP | operator= (const MavlinkFTP &) |
Private Attributes | |
ReceiveMessageFunc_t | _utRcvMsgFunc {} |
Unit test override for mavlink message sending. More... | |
void * | _worker_data {nullptr} |
Additional parameter to _utRcvMsgFunc;. More... | |
Mavlink * | _mavlink |
char * | _work_buffer1 {nullptr} |
char * | _work_buffer2 {nullptr} |
hrt_abstime | _last_work_buffer_access {0} |
timestamp when the buffers were last accessed More... | |
bool | _last_reply_valid = false |
uint8_t | _last_reply [MAVLINK_MSG_ID_FILE_TRANSFER_PROTOCOL_LEN - MAVLINK_MSG_FILE_TRANSFER_PROTOCOL_FIELD_PAYLOAD_LEN+sizeof(PayloadHeader)+sizeof(uint32_t)] |
Static Private Attributes | |
static const char | kDirentFile = 'F' |
Identifies File returned from List command. More... | |
static const char | kDirentDir = 'D' |
Identifies Directory returned from List command. More... | |
static const char | kDirentSkip = 'S' |
Identifies Skipped entry from List command. More... | |
static const uint8_t | kMaxDataLength = MAVLINK_MSG_FILE_TRANSFER_PROTOCOL_FIELD_PAYLOAD_LEN - sizeof(PayloadHeader) |
Maximum data size in RequestHeader::data. More... | |
static constexpr int | _work_buffer1_len = kMaxDataLength |
static constexpr int | _work_buffer2_len = 256 |
static constexpr const char | _root_dir [] = PX4_ROOTFSDIR |
static constexpr const int | _root_dir_len = sizeof(_root_dir) - 1 |
Friends | |
class | MavlinkFtpTest |
MAVLink remote file server. Support FTP like commands using MAVLINK_MSG_ID_FILE_TRANSFER_PROTOCOL message.
Definition at line 52 of file mavlink_ftp.h.
typedef void(* MavlinkFTP::ReceiveMessageFunc_t) (const mavlink_file_transfer_protocol_t *ftp_req, void *worker_data) |
Definition at line 67 of file mavlink_ftp.h.
enum MavlinkFTP::ErrorCode : uint8_t |
Error codes returned in Nak response PayloadHeader.data[0].
Enumerator | |
---|---|
kErrNone | |
kErrFail | Unknown failure. |
kErrFailErrno | Command failed, errno sent back in PayloadHeader.data[1]. |
kErrInvalidDataSize | PayloadHeader.size is invalid. |
kErrInvalidSession | Session is not currently open. |
kErrNoSessionsAvailable | All available Sessions in use. |
kErrEOF | Offset past end of file for List and Read commands. |
kErrUnknownCommand | Unknown command opcode. |
kErrFailFileExists | File exists already. |
kErrFailFileProtected | File is write protected. |
Definition at line 113 of file mavlink_ftp.h.
enum MavlinkFTP::Opcode : uint8_t |
Command opcodes.
Enumerator | |
---|---|
kCmdNone | ignored, always acked |
kCmdTerminateSession | Terminates open Read session. |
kCmdResetSessions | Terminates all open Read sessions. |
kCmdListDirectory | List files in <path> from <offset> |
kCmdOpenFileRO | Opens file at <path> for reading, returns <session> |
kCmdReadFile | Reads <size> bytes from <offset> in <session> |
kCmdCreateFile | Creates file at <path> for writing, returns <session> |
kCmdWriteFile | Writes <size> bytes to <offset> in <session> |
kCmdRemoveFile | Remove file at <path> |
kCmdCreateDirectory | Creates directory at <path> |
kCmdRemoveDirectory | Removes Directory at <path>, must be empty. |
kCmdOpenFileWO | Opens file at <path> for writing, returns <session> |
kCmdTruncateFile | Truncate file at <path> to <offset> length. |
kCmdRename | Rename <path1> to <path2> |
kCmdCalcFileCRC32 | Calculate CRC32 for file at <path> |
kCmdBurstReadFile | Burst download session file. |
kRspAck | Ack response. |
kRspNak | Nak response. |
Definition at line 90 of file mavlink_ftp.h.
MavlinkFTP::MavlinkFTP | ( | Mavlink * | mavlink | ) |
Definition at line 51 of file mavlink_ftp.cpp.
MavlinkFTP::~MavlinkFTP | ( | ) |
Definition at line 58 of file mavlink_ftp.cpp.
References _work_buffer1, and _work_buffer2.
|
private |
|
inline |
This is the payload which is in mavlink_file_transfer_protocol_t.payload.
This needs to be packed, because it's typecasted from mavlink_file_transfer_protocol_t.payload, which starts at a 3 byte offset, causing an unaligned access to seq_number and offset
< sequence number for message
< Session id for read and write commands
< Command opcode
< Size of data
< Request opcode returned in kRspAck, kRspNak message
< Only used if req_opcode=kCmdBurstReadFile - 1: set of burst packets complete, 0: More burst packets coming.
< 32 bit aligment padding
< Offsets for List and Read commands
< command data, varies by Opcode
Definition at line 77 of file mavlink_ftp.h.
References data.
|
private |
Copy file (with limited space)
Definition at line 889 of file mavlink_ftp.cpp.
References _work_buffer2, _work_buffer2_len, read(), and write().
Referenced by _workTruncateFile().
|
private |
Guarantees that the payload data is null terminated.
Definition at line 873 of file mavlink_ftp.cpp.
References kMaxDataLength.
Referenced by _workCalcFileCRC32(), _workCreateDirectory(), _workList(), _workOpen(), _workRemoveDirectory(), _workRemoveFile(), _workRename(), and _workTruncateFile().
|
private |
make sure that the working buffers _work_buffer* are allocated
Definition at line 294 of file mavlink_ftp.cpp.
References _last_work_buffer_access, _work_buffer1, _work_buffer1_len, _work_buffer2, _work_buffer2_len, and hrt_absolute_time().
Referenced by _process_request().
|
private |
Definition at line 109 of file mavlink_ftp.cpp.
References _mavlink, Mavlink::get_channel(), and MavlinkFtpTest::serverChannel.
Referenced by _process_request().
|
private |
Definition at line 97 of file mavlink_ftp.cpp.
References _mavlink, Mavlink::get_component_id(), and MavlinkFtpTest::serverComponentId.
Referenced by handle_message().
|
private |
Definition at line 85 of file mavlink_ftp.cpp.
References _mavlink, Mavlink::get_system_id(), and MavlinkFtpTest::serverSystemId.
Referenced by handle_message().
|
private |
Processes an FTP message.
Definition at line 143 of file mavlink_ftp.cpp.
References _ensure_buffers_exist(), _getServerChannel(), _last_reply, _last_reply_valid, _mavlink, _reply(), _workBurst(), _workCalcFileCRC32(), _workCreateDirectory(), _workList(), _workOpen(), _workRead(), _workRemoveDirectory(), _workRemoveFile(), _workRename(), _workReset(), _workTerminate(), _workTruncateFile(), _workWrite(), Mavlink::get_channel(), kCmdBurstReadFile, kCmdCalcFileCRC32, kCmdCreateDirectory, kCmdCreateFile, kCmdListDirectory, kCmdNone, kCmdOpenFileRO, kCmdOpenFileWO, kCmdReadFile, kCmdRemoveDirectory, kCmdRemoveFile, kCmdRename, kCmdResetSessions, kCmdTerminateSession, kCmdTruncateFile, kCmdWriteFile, kErrFailErrno, kErrFailFileExists, kErrInvalidDataSize, kErrNone, kErrUnknownCommand, kMaxDataLength, kRspAck, and kRspNak.
Referenced by handle_message().
|
private |
Sends the specified FTP response message out through mavlink.
Definition at line 311 of file mavlink_ftp.cpp.
References _last_reply, _last_reply_valid, _mavlink, _utRcvMsgFunc, _worker_data, Mavlink::get_channel(), and kRspAck.
Referenced by _process_request(), and send().
|
private |
Responds to a Stream command.
Definition at line 575 of file mavlink_ftp.cpp.
References kErrInvalidSession, and kErrNone.
Referenced by _process_request().
|
private |
Responds to a CalcFileCRC32 command.
Definition at line 835 of file mavlink_ftp.cpp.
References _data_as_cstring(), _root_dir, _root_dir_len, _work_buffer2, _work_buffer2_len, fd, kErrFailErrno, kErrNone, and read().
Referenced by _process_request().
|
private |
Responds to a CreateDirectory command.
Definition at line 817 of file mavlink_ftp.cpp.
References _data_as_cstring(), _root_dir, _root_dir_len, _work_buffer1, _work_buffer1_len, kErrFailErrno, and kErrNone.
Referenced by _process_request().
|
private |
Responds to a List command.
Definition at line 339 of file mavlink_ftp.cpp.
References _data_as_cstring(), _root_dir, _root_dir_len, _work_buffer1, _work_buffer1_len, _work_buffer2, _work_buffer2_len, kDirentDir, kDirentFile, kDirentSkip, kErrEOF, kErrFailErrno, kErrNone, and kMaxDataLength.
Referenced by _process_request().
|
private |
Responds to an Open command.
Definition at line 490 of file mavlink_ftp.cpp.
References _data_as_cstring(), _root_dir, _root_dir_len, _work_buffer1, _work_buffer1_len, fd, kErrFailErrno, kErrNone, and kErrNoSessionsAvailable.
Referenced by _process_request().
|
private |
Responds to a Read command.
Definition at line 539 of file mavlink_ftp.cpp.
References kErrEOF, kErrFailErrno, kErrInvalidSession, kErrNone, kMaxDataLength, and read().
Referenced by _process_request().
|
private |
Responds to a RemoveDirectory command.
Definition at line 799 of file mavlink_ftp.cpp.
References _data_as_cstring(), _root_dir, _root_dir_len, _work_buffer1, _work_buffer1_len, kErrFailErrno, and kErrNone.
Referenced by _process_request().
|
private |
Responds to a RemoveFile command.
Definition at line 625 of file mavlink_ftp.cpp.
References _data_as_cstring(), _root_dir, _root_dir_len, _work_buffer1, _work_buffer1_len, kErrFailErrno, and kErrNone.
Referenced by _process_request().
|
private |
Responds to a Rename command.
Definition at line 769 of file mavlink_ftp.cpp.
References _data_as_cstring(), _root_dir, _root_dir_len, _work_buffer1, _work_buffer1_len, _work_buffer2, _work_buffer2_len, kErrFailErrno, and kErrNone.
Referenced by _process_request().
|
private |
Responds to a Reset command.
Definition at line 754 of file mavlink_ftp.cpp.
References kErrNone.
Referenced by _process_request().
|
private |
Responds to a Terminate command.
Definition at line 737 of file mavlink_ftp.cpp.
References kErrInvalidSession, and kErrNone.
Referenced by _process_request().
|
private |
Responds to a TruncateFile command.
Definition at line 643 of file mavlink_ftp.cpp.
References _copy_file(), _data_as_cstring(), _root_dir, _root_dir_len, _work_buffer1, _work_buffer1_len, fd, kErrFailErrno, kErrNone, and write().
Referenced by _process_request().
|
private |
Responds to a Write command.
Definition at line 597 of file mavlink_ftp.cpp.
References kErrFailErrno, kErrInvalidSession, kErrNone, and write().
Referenced by _process_request().
unsigned MavlinkFTP::get_size | ( | ) |
Definition at line 65 of file mavlink_ftp.cpp.
References _utRcvMsgFunc, _worker_data, and set_unittest_worker().
Referenced by MavlinkFtpTest::_receive_message_handler_burst(), and send().
void MavlinkFTP::handle_message | ( | const mavlink_message_t * | msg | ) |
Handle possible FTP message.
Definition at line 121 of file mavlink_ftp.cpp.
References _getServerComponentId(), _getServerSystemId(), and _process_request().
Referenced by MavlinkFtpTest::_bad_datasize_test(), MavlinkFtpTest::_burst_test(), MavlinkFtpTest::_send_receive_msg(), and MavlinkReceiver::Run().
|
private |
void MavlinkFTP::send | ( | const hrt_abstime | t | ) |
Handle sending of messages.
Call this regularly at a fixed frequency.
t | current time |
Definition at line 948 of file mavlink_ftp.cpp.
References _last_work_buffer_access, _mavlink, _reply(), _work_buffer1, _work_buffer2, Mavlink::get_free_tx_buf(), get_size(), hrt_elapsed_time(), kCmdBurstReadFile, kErrEOF, kErrFailErrno, kErrNone, kMaxDataLength, kRspAck, kRspNak, and read().
Referenced by MavlinkFtpTest::_burst_test(), MavlinkFtpTest::_receive_message_handler_burst(), and MavlinkReceiver::Run().
void MavlinkFTP::set_unittest_worker | ( | ReceiveMessageFunc_t | rcvMsgFunc, |
void * | worker_data | ||
) |
Sets up the server to run in unit test mode.
rcvmsgFunc | Function which will be called to handle outgoing mavlink messages. |
worker_data | Data to pass to worker |
Referenced by MavlinkFtpTest::_burst_test(), MavlinkFtpTest::_init(), and get_size().
|
friend |
Definition at line 208 of file mavlink_ftp.h.
|
private |
Definition at line 205 of file mavlink_ftp.h.
Referenced by _process_request(), and _reply().
|
private |
Definition at line 203 of file mavlink_ftp.h.
Referenced by _process_request(), and _reply().
|
private |
timestamp when the buffers were last accessed
Definition at line 192 of file mavlink_ftp.h.
Referenced by _ensure_buffers_exist(), and send().
|
private |
Definition at line 181 of file mavlink_ftp.h.
Referenced by _getServerChannel(), _getServerComponentId(), _getServerSystemId(), _process_request(), _reply(), and send().
|
staticprivate |
Definition at line 199 of file mavlink_ftp.h.
Referenced by _workCalcFileCRC32(), _workCreateDirectory(), _workList(), _workOpen(), _workRemoveDirectory(), _workRemoveFile(), _workRename(), and _workTruncateFile().
|
staticprivate |
Definition at line 201 of file mavlink_ftp.h.
Referenced by _workCalcFileCRC32(), _workCreateDirectory(), _workList(), _workOpen(), _workRemoveDirectory(), _workRemoveFile(), _workRename(), and _workTruncateFile().
|
private |
Unit test override for mavlink message sending.
Definition at line 178 of file mavlink_ftp.h.
Referenced by _reply(), and get_size().
|
private |
Definition at line 188 of file mavlink_ftp.h.
Referenced by _ensure_buffers_exist(), _workCreateDirectory(), _workList(), _workOpen(), _workRemoveDirectory(), _workRemoveFile(), _workRename(), _workTruncateFile(), send(), and ~MavlinkFTP().
|
staticprivate |
Definition at line 189 of file mavlink_ftp.h.
Referenced by _ensure_buffers_exist(), _workCreateDirectory(), _workList(), _workOpen(), _workRemoveDirectory(), _workRemoveFile(), _workRename(), and _workTruncateFile().
|
private |
Definition at line 190 of file mavlink_ftp.h.
Referenced by _copy_file(), _ensure_buffers_exist(), _workCalcFileCRC32(), _workList(), _workRename(), send(), and ~MavlinkFTP().
|
staticprivate |
Definition at line 191 of file mavlink_ftp.h.
Referenced by _copy_file(), _ensure_buffers_exist(), _workCalcFileCRC32(), _workList(), and _workRename().
|
private |
Additional parameter to _utRcvMsgFunc;.
Definition at line 179 of file mavlink_ftp.h.
Referenced by _reply(), and get_size().
|
staticprivate |
Identifies Directory returned from List command.
Definition at line 160 of file mavlink_ftp.h.
Referenced by _workList().
|
staticprivate |
Identifies File returned from List command.
Definition at line 159 of file mavlink_ftp.h.
Referenced by _workList().
|
staticprivate |
Identifies Skipped entry from List command.
Definition at line 161 of file mavlink_ftp.h.
Referenced by _workList().
|
staticprivate |
Maximum data size in RequestHeader::data.
Definition at line 164 of file mavlink_ftp.h.
Referenced by _data_as_cstring(), _process_request(), _workList(), _workRead(), and send().