diff options
author | Vladimir Ermakov <vooon341@gmail.com> | 2014-09-22 15:19:38 +0400 |
---|---|---|
committer | Vladimir Ermakov <vooon341@gmail.com> | 2014-09-22 15:19:38 +0400 |
commit | ef5a93c09ce4bd08729cf3fbf87a6c1d453c646f (patch) | |
tree | 690c45511c71bd647a63c5230fb3fece82607ee9 /src | |
parent | 8eb310061693c86a9c8b3c7f6e2bab1ec680c46a (diff) | |
download | px4-firmware-ef5a93c09ce4bd08729cf3fbf87a6c1d453c646f.tar.gz px4-firmware-ef5a93c09ce4bd08729cf3fbf87a6c1d453c646f.tar.bz2 px4-firmware-ef5a93c09ce4bd08729cf3fbf87a6c1d453c646f.zip |
FTP: Add file checksum calculation command.
Diffstat (limited to 'src')
-rw-r--r-- | src/modules/mavlink/mavlink_ftp.cpp | 39 | ||||
-rw-r--r-- | src/modules/mavlink/mavlink_ftp.h | 2 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/modules/mavlink/mavlink_ftp.cpp b/src/modules/mavlink/mavlink_ftp.cpp index 117cc4ec1..c26fe6b4b 100644 --- a/src/modules/mavlink/mavlink_ftp.cpp +++ b/src/modules/mavlink/mavlink_ftp.cpp @@ -34,6 +34,7 @@ /// @file mavlink_ftp.cpp /// @author px4dev, Don Gagne <don@thegagnes.com> +#include <crc32.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> @@ -220,6 +221,11 @@ MavlinkFTP::_process_request(Request *req) errorCode = _workRemoveDirectory(payload); break; + + case kCmdCalcFileCRC32: + errorCode = _workCalcFileCRC32(payload); + break; + default: errorCode = kErrUnknownCommand; break; @@ -690,6 +696,39 @@ MavlinkFTP::_workCreateDirectory(PayloadHeader* payload) } } +/// @brief Responds to a CalcFileCRC32 command +MavlinkFTP::ErrorCode +MavlinkFTP::_workCalcFileCRC32(PayloadHeader* payload) +{ + char file_buf[256]; + uint32_t checksum = 0; + ssize_t bytes_read; + strncpy(file_buf, _data_as_cstring(payload), kMaxDataLength); + + int fd = ::open(file_buf, O_RDONLY); + if (fd < 0) { + return kErrFailErrno; + } + + do { + bytes_read = ::read(fd, file_buf, sizeof(file_buf)); + if (bytes_read < 0) { + int r_errno = errno; + ::close(fd); + errno = r_errno; + return kErrFailErrno; + } + + checksum = crc32part((uint8_t*)file_buf, bytes_read, checksum); + } while (bytes_read == sizeof(file_buf)); + + ::close(fd); + + payload->size = sizeof(uint32_t); + *((uint32_t*)payload->data) = checksum; + return kErrNone; +} + /// @brief Returns true if the specified session is a valid open session bool MavlinkFTP::_valid_session(unsigned index) diff --git a/src/modules/mavlink/mavlink_ftp.h b/src/modules/mavlink/mavlink_ftp.h index 0fbd010be..bef6775a9 100644 --- a/src/modules/mavlink/mavlink_ftp.h +++ b/src/modules/mavlink/mavlink_ftp.h @@ -99,6 +99,7 @@ public: 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> kRspAck = 128, ///< Ack response kRspNak ///< Nak response @@ -154,6 +155,7 @@ private: ErrorCode _workRemoveFile(PayloadHeader *payload); ErrorCode _workTruncateFile(PayloadHeader *payload); ErrorCode _workRename(PayloadHeader *payload); + ErrorCode _workCalcFileCRC32(PayloadHeader *payload); static const unsigned kRequestQueueSize = 2; ///< Max number of queued requests Request _request_bufs[kRequestQueueSize]; ///< Request buffers which hold work |