eive-obsw/linux/acs/StrComHandler.h

380 lines
14 KiB
C
Raw Normal View History

2021-12-21 15:46:09 +01:00
#ifndef BSP_Q7S_DEVICES_STRHELPER_H_
#define BSP_Q7S_DEVICES_STRHELPER_H_
2021-11-30 16:01:02 +01:00
#include <mission/acs/str/ArcsecDatalinkLayer.h>
2021-11-30 16:01:02 +01:00
#include <string>
2022-01-17 15:58:27 +01:00
2022-02-05 18:08:54 +01:00
#include "OBSWConfig.h"
2022-02-05 13:19:20 +01:00
#ifdef XIPHOS_Q7S
2022-09-16 11:53:33 +02:00
#include "bsp_q7s/fs/SdCardManager.h"
2022-02-05 13:19:20 +01:00
#endif
2023-03-21 20:35:28 +01:00
#include "arcsec/client/generated/actionreq.h"
#include "arcsec/common/generated/tmtcstructs.h"
2022-01-17 15:58:27 +01:00
#include "fsfw/devicehandlers/CookieIF.h"
2021-12-06 19:36:21 +01:00
#include "fsfw/objectmanager/SystemObject.h"
2022-01-17 15:58:27 +01:00
#include "fsfw/osal/linux/BinarySemaphore.h"
2022-08-24 17:27:47 +02:00
#include "fsfw/returnvalues/returnvalue.h"
2021-12-06 19:36:21 +01:00
#include "fsfw/tasks/ExecutableObjectIF.h"
2022-11-10 18:07:59 +01:00
#include "fsfw_hal/linux/serial/SerialComIF.h"
2021-12-06 19:36:21 +01:00
2021-11-30 16:01:02 +01:00
/**
2021-12-21 15:46:09 +01:00
* @brief Helper class for the star tracker handler to accelerate large data transfers.
2022-02-14 11:28:15 +01:00
*
* @author J. Meier
2021-11-30 16:01:02 +01:00
*/
2023-03-21 20:35:28 +01:00
class StrComHandler : public SystemObject, public DeviceCommunicationIF, public ExecutableObjectIF {
2022-01-17 15:58:27 +01:00
public:
2023-03-21 20:35:28 +01:00
static const uint8_t INTERFACE_ID = CLASS_ID::STR_HELPER;
//! [EXPORT] : [COMMENT] SD card specified in path string not mounted
2023-03-22 02:20:14 +01:00
static const ReturnValue_t SD_NOT_MOUNTED = MAKE_RETURN_CODE(1);
2023-03-21 20:35:28 +01:00
//! [EXPORT] : [COMMENT] Specified file does not exist on filesystem
2023-03-22 02:20:14 +01:00
static const ReturnValue_t FILE_NOT_EXISTS = MAKE_RETURN_CODE(2);
2023-03-21 20:35:28 +01:00
//! [EXPORT] : [COMMENT] Specified path does not exist
2023-03-22 02:20:14 +01:00
static const ReturnValue_t PATH_NOT_EXISTS = MAKE_RETURN_CODE(3);
2023-03-21 20:35:28 +01:00
//! [EXPORT] : [COMMENT] Failed to create download image or read flash file
2023-03-22 02:20:14 +01:00
static const ReturnValue_t FILE_CREATION_FAILED = MAKE_RETURN_CODE(4);
2023-03-21 20:35:28 +01:00
//! [EXPORT] : [COMMENT] Region in flash write/read reply does not match expected region
2023-03-22 02:20:14 +01:00
static const ReturnValue_t REGION_MISMATCH = MAKE_RETURN_CODE(5);
2023-03-21 20:35:28 +01:00
//! [EXPORT] : [COMMENT] Address in flash write/read reply does not match expected address
2023-03-22 02:20:14 +01:00
static const ReturnValue_t ADDRESS_MISMATCH = MAKE_RETURN_CODE(6);
2023-03-21 20:35:28 +01:00
//! [EXPORT] : [COMMENT] Length in flash write/read reply does not match expected length
2023-03-22 02:20:14 +01:00
static const ReturnValue_t LENGTH_MISMATCH = MAKE_RETURN_CODE(7);
2023-03-21 20:35:28 +01:00
//! [EXPORT] : [COMMENT] Status field in reply signals error
2023-03-22 02:20:14 +01:00
static const ReturnValue_t STATUS_ERROR = MAKE_RETURN_CODE(8);
2023-03-21 20:35:28 +01:00
//! [EXPORT] : [COMMENT] Reply has invalid type ID (should be of action reply type)
2023-03-22 02:20:14 +01:00
static const ReturnValue_t INVALID_TYPE_ID = MAKE_RETURN_CODE(9);
static const ReturnValue_t RECEPTION_TIMEOUT = MAKE_RETURN_CODE(10);
static const ReturnValue_t DECODING_ERROR = MAKE_RETURN_CODE(11);
2023-03-21 20:35:28 +01:00
2022-01-17 15:58:27 +01:00
static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::STR_HELPER;
//! [EXPORT] : [COMMENT] Image upload failed
static const Event IMAGE_UPLOAD_FAILED = MAKE_EVENT(0, severity::LOW);
//! [EXPORT] : [COMMENT] Image download failed
static const Event IMAGE_DOWNLOAD_FAILED = MAKE_EVENT(1, severity::LOW);
//! [EXPORT] : [COMMENT] Uploading image to star tracker was successfulop
static const Event IMAGE_UPLOAD_SUCCESSFUL = MAKE_EVENT(2, severity::LOW);
//! [EXPORT] : [COMMENT] Image download was successful
static const Event IMAGE_DOWNLOAD_SUCCESSFUL = MAKE_EVENT(3, severity::LOW);
//! [EXPORT] : [COMMENT] Finished flash write procedure successfully
static const Event FLASH_WRITE_SUCCESSFUL = MAKE_EVENT(4, severity::LOW);
//! [EXPORT] : [COMMENT] Finished flash read procedure successfully
static const Event FLASH_READ_SUCCESSFUL = MAKE_EVENT(5, severity::LOW);
//! [EXPORT] : [COMMENT] Flash read procedure failed
2022-02-14 11:28:15 +01:00
static const Event FLASH_READ_FAILED = MAKE_EVENT(6, severity::LOW);
2022-02-01 11:45:22 +01:00
//! [EXPORT] : [COMMENT] Firmware update was successful
2022-02-14 11:28:15 +01:00
static const Event FIRMWARE_UPDATE_SUCCESSFUL = MAKE_EVENT(7, severity::LOW);
2022-02-01 11:45:22 +01:00
//! [EXPORT] : [COMMENT] Firmware update failed
2022-02-14 11:28:15 +01:00
static const Event FIRMWARE_UPDATE_FAILED = MAKE_EVENT(8, severity::LOW);
2022-01-17 15:58:27 +01:00
//! [EXPORT] : [COMMENT] Failed to read communication interface reply data
//! P1: Return code of failed communication interface read call
//! P1: Upload/download position for which the read call failed
2022-02-14 11:28:15 +01:00
static const Event STR_HELPER_READING_REPLY_FAILED = MAKE_EVENT(9, severity::LOW);
2022-01-17 15:58:27 +01:00
//! [EXPORT] : [COMMENT] Unexpected stop of decoding sequence
//! P1: Return code of failed communication interface read call
//! P1: Upload/download position for which the read call failed
2022-02-14 11:28:15 +01:00
static const Event STR_HELPER_COM_ERROR = MAKE_EVENT(10, severity::LOW);
2023-03-21 20:59:59 +01:00
//! [EXPORT] : [COMMENT] Star tracker did not send a valid reply for a certain timeout.
//! P1: Position of upload or download packet for which the packet wa sent. P2: Timeout
2023-03-22 02:20:14 +01:00
static const Event STR_COM_REPLY_TIMEOUT = MAKE_EVENT(11, severity::LOW);
2022-01-17 15:58:27 +01:00
//! [EXPORT] : [COMMENT] Error during decoding of received reply occurred
2023-03-21 20:59:59 +01:00
//! P1: Return value of decoding function
//! P2: Position of upload/download packet, or address of flash write/read request
static const Event STR_HELPER_DEC_ERROR = MAKE_EVENT(13, severity::LOW);
2022-01-17 15:58:27 +01:00
//! [EXPORT] : [COMMENT] Position mismatch
//! P1: The expected position and thus the position for which the image upload/download failed
2023-03-21 20:59:59 +01:00
static const Event POSITION_MISMATCH = MAKE_EVENT(14, severity::LOW);
2022-01-17 15:58:27 +01:00
//! [EXPORT] : [COMMENT] Specified file does not exist
//! P1: Internal state of str helper
2023-03-21 20:59:59 +01:00
static const Event STR_HELPER_FILE_NOT_EXISTS = MAKE_EVENT(15, severity::LOW);
2022-01-17 15:58:27 +01:00
//! [EXPORT] : [COMMENT] Sending packet to star tracker failed
//! P1: Return code of communication interface sendMessage function
//! P2: Position of upload/download packet, or address of flash write/read request for which
//! sending failed
2023-03-21 20:59:59 +01:00
static const Event STR_HELPER_SENDING_PACKET_FAILED = MAKE_EVENT(16, severity::LOW);
2022-01-17 15:58:27 +01:00
//! [EXPORT] : [COMMENT] Communication interface requesting reply failed
//! P1: Return code of failed request
//! P1: Upload/download position, or address of flash write/read request for which transmission
//! failed
2023-03-21 20:59:59 +01:00
static const Event STR_HELPER_REQUESTING_MSG_FAILED = MAKE_EVENT(17, severity::LOW);
2022-01-17 15:58:27 +01:00
2023-03-21 20:35:28 +01:00
StrComHandler(object_id_t objectId);
virtual ~StrComHandler();
2022-01-17 15:58:27 +01:00
ReturnValue_t initialize() override;
ReturnValue_t performOperation(uint8_t operationCode = 0) override;
/**
* @brief Starts sequence to upload image to star tracker
*
* @param uploadImage_ Name including absolute path of the image to upload. Must be previously
* transferred to the OBC with the CFDP protocol.
*/
ReturnValue_t startImageUpload(std::string uploadImage_);
/**
* @brief Calling this function initiates the download of an image from the star tracker.
*
* @param path Path where downloaded image will be stored
*/
ReturnValue_t startImageDownload(std::string path);
/**
2022-02-01 11:45:22 +01:00
* @brief Will start the firmware update
2022-01-17 15:58:27 +01:00
*
2022-02-01 11:45:22 +01:00
* @param fullname Full name including absolute path of file containing firmware
* update.
2022-01-17 15:58:27 +01:00
*/
2022-02-01 11:45:22 +01:00
ReturnValue_t startFirmwareUpdate(std::string fullname);
2022-01-17 15:58:27 +01:00
/**
* @brief Starts the flash read procedure
*
* @param path Path where file with read flash data will be created
2022-02-25 14:24:51 +01:00
* @param startRegion Region form where to start reading
2022-01-17 15:58:27 +01:00
* @param length Number of bytes to read from flash
*/
2022-02-25 14:24:51 +01:00
ReturnValue_t startFlashRead(std::string path, uint8_t startRegion, uint32_t length);
2022-01-17 15:58:27 +01:00
/**
* @brief Can be used to interrupt a running data transfer.
*/
void stopProcess();
/**
* @brief Changes the dafault name of downloaded images
*/
void setDownloadImageName(std::string filename);
/**
* @brief Sets the name of the file which will be created to store the data read from flash
*/
void setFlashReadFilename(std::string filename);
/**
* @brief Disables timestamp generation when new file is created
*/
void disableTimestamping();
/**
* @brief Enables timestamp generation when new file is created
*/
void enableTimestamping();
2022-01-17 15:58:27 +01:00
private:
2023-03-21 20:35:28 +01:00
//! [EXPORT] : [SKIP]
static constexpr ReturnValue_t NO_SERIAL_DATA_READ = MAKE_RETURN_CODE(128);
2022-01-17 15:58:27 +01:00
// Size of one image part which can be sent per action request
static const size_t SIZE_IMAGE_PART = 1024;
2022-02-14 11:28:15 +01:00
static const uint32_t FLASH_REGION_SIZE = 0x20000;
2022-01-17 15:58:27 +01:00
2022-04-12 17:42:41 +02:00
struct ImageDownload {
2022-01-17 15:58:27 +01:00
static const uint32_t LAST_POSITION = 4095;
};
static const uint32_t MAX_POLLS = 10000;
2023-03-21 20:35:28 +01:00
static const uint8_t ACTION_DATA_OFFSET = 3;
static const uint8_t POS_OFFSET = 3;
static const uint8_t IMAGE_DATA_OFFSET = 6;
static const uint8_t FLASH_READ_DATA_OFFSET = 9;
static const uint8_t REGION_OFFSET = 3;
static const uint8_t ADDRESS_OFFSET = 4;
2022-02-14 11:28:15 +01:00
static const size_t CHUNK_SIZE = 1024;
2022-02-25 14:24:51 +01:00
static const size_t CONFIG_MAX_DOWNLOAD_RETRIES = 3;
2022-02-23 18:15:34 +01:00
static const uint32_t FLASH_ERASE_DELAY = 500;
2022-01-17 15:58:27 +01:00
2023-03-22 02:20:14 +01:00
enum class InternalState {
SLEEPING,
POLL_ONE_REPLY,
UPLOAD_IMAGE,
DOWNLOAD_IMAGE,
FLASH_READ,
FIRMWARE_UPDATE
};
2022-01-17 15:58:27 +01:00
2023-03-22 02:20:14 +01:00
InternalState state = InternalState::SLEEPING;
2022-01-17 15:58:27 +01:00
ArcsecDatalinkLayer datalinkLayer;
2023-03-22 02:20:14 +01:00
MutexIF *lock;
2022-01-17 15:58:27 +01:00
BinarySemaphore semaphore;
2023-03-22 02:20:14 +01:00
Countdown replyTimeout = Countdown(20);
2023-03-21 20:35:28 +01:00
2022-04-12 17:42:41 +02:00
struct UploadImage {
2022-01-17 15:58:27 +01:00
// Name including absolute path of image to upload
std::string uploadFile;
};
UploadImage uploadImage;
2022-04-12 17:42:41 +02:00
struct DownloadImage {
2022-01-17 15:58:27 +01:00
// Path where the downloaded image will be stored
std::string path;
// Default name of downloaded image, can be changed via command
std::string filename = "image.bin";
};
DownloadImage downloadImage;
2022-04-12 17:42:41 +02:00
struct FlashWrite {
2022-01-17 15:58:27 +01:00
// File which contains data to write when executing the flash write command
std::string fullname;
2022-02-14 11:28:15 +01:00
// The first region to write to
uint8_t firstRegion = 0;
// Maximum region the flash write command is allowed to write to
uint8_t lastRegion = 0;
2022-01-17 15:58:27 +01:00
// Will be set with the flash write command and specifies the start address where to write the
// flash data to
uint32_t address = 0;
};
FlashWrite flashWrite;
2022-04-12 17:42:41 +02:00
struct FlashRead {
2022-01-17 15:58:27 +01:00
// Path where the file containing the read data will be stored
std::string path = "";
// Default name of file containing the data read from flash, can be changed via command
std::string filename = "flashread.bin";
// Will be set with the flash read command
2022-02-25 14:24:51 +01:00
uint8_t startRegion = 0;
2022-01-17 15:58:27 +01:00
// Number of bytes to read from flash
uint32_t size = 0;
};
FlashRead flashRead;
2022-02-05 13:19:20 +01:00
#ifdef XIPHOS_Q7S
2023-03-21 20:35:28 +01:00
SdCardManager *sdcMan = nullptr;
2022-02-05 13:19:20 +01:00
#endif
2022-01-17 15:58:27 +01:00
2023-03-22 02:20:14 +01:00
std::array<uint8_t, startracker::MAX_FRAME_SIZE> cmdBuf{};
2023-03-21 20:35:28 +01:00
std::array<uint8_t, 4096> recBuf{};
2022-01-17 15:58:27 +01:00
2023-03-22 02:20:14 +01:00
bool replyWasReceived = false;
const uint8_t *replyPtr = nullptr;
size_t replyLen = 0;
ReturnValue_t replyResult = returnvalue::OK;
2022-01-17 15:58:27 +01:00
bool terminate = false;
#ifdef EGSE
bool timestamping = false;
#else
bool timestamping = true;
#endif
2023-03-21 20:35:28 +01:00
int serialPort = 0;
struct termios tty = {};
2022-01-17 15:58:27 +01:00
// Queue id of raw data receiver
MessageQueueId_t rawDataReceiver = MessageQueueIF::NO_QUEUE;
2023-03-21 20:35:28 +01:00
ReturnValue_t initializeInterface(CookieIF *cookie) override;
ReturnValue_t sendMessage(CookieIF *cookie, const uint8_t *sendData, size_t sendLen) override;
ReturnValue_t getSendSuccess(CookieIF *cookie) override;
ReturnValue_t requestReceiveMessage(CookieIF *cookie, size_t requestLen) override;
ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) override;
2023-03-21 20:47:06 +01:00
ReturnValue_t handleSerialReception();
2023-03-21 20:35:28 +01:00
2022-01-17 15:58:27 +01:00
/**
* @brief Performs image uploading
*/
ReturnValue_t performImageUpload();
2022-02-14 11:28:15 +01:00
/**
* @brief Performs firmware update
*
2022-08-24 17:27:47 +02:00
* @return returnvalue::OK if successful, otherwise error return value
2022-02-14 11:28:15 +01:00
*/
ReturnValue_t performFirmwareUpdate();
2022-01-17 15:58:27 +01:00
/**
* @brief Performs download of last taken image from the star tracker.
*
* @details Download is split over multiple packets transporting each a maximum of 1024 bytes.
* In case the download of one position fails, the same packet will be again
* requested. If the download of the packet fails CONFIG_MAX_DOWNLOAD_RETRIES times,
* the download will be stopped.
*/
ReturnValue_t performImageDownload();
/**
* @brief Handles flash write procedure
*
2022-02-14 11:28:15 +01:00
* @param ID of first region to write to
*
2022-08-24 17:27:47 +02:00
* @return returnvalue::OK if successful, otherwise returnvalue::FAILED
2022-01-17 15:58:27 +01:00
*/
ReturnValue_t performFlashWrite();
/**
* @brief Sends a sequence of commands to the star tracker to read larger parts from the
* flash memory.
*/
ReturnValue_t performFlashRead();
/**
* @brief Sends packet to the star tracker and reads reply by using the communication
* interface
2023-03-22 02:20:14 +01:00
* @details
* The reply frame is stored in the data link layer helper. A pointer to the start of the frame
* is assigned to the @replyPtr member of this class. The frame length will be assigned to
* the @replyLen member.
2022-01-17 15:58:27 +01:00
* @param size Size of data beforehand written to the commandBuffer
* @param parameter Parameter 2 of trigger event function
*
2022-08-24 17:27:47 +02:00
* @return returnvalue::OK if successful, otherwise returnvalue::FAILED
2022-01-17 15:58:27 +01:00
*/
2023-03-22 02:20:14 +01:00
ReturnValue_t sendAndRead(size_t size, uint32_t parameter);
2022-01-17 15:58:27 +01:00
/**
* @brief Checks the header (type id and status fields) of the action reply
*
2022-08-24 17:27:47 +02:00
* @return returnvalue::OK if reply confirms success of packet transfer, otherwise REUTRN_FAILED
2022-01-17 15:58:27 +01:00
*/
2023-03-22 02:20:14 +01:00
ReturnValue_t checkActionReply(size_t replySize);
2022-01-17 15:58:27 +01:00
/**
* @brief Checks the position field in a star tracker upload/download reply.
*
* @param expectedPosition Value of expected position
*
2022-08-26 14:28:06 +02:00
* @return returnvalue::OK if received position matches expected position, otherwise
* returnvalue::FAILED
2022-01-17 15:58:27 +01:00
*/
2023-03-22 02:20:14 +01:00
ReturnValue_t checkReplyPosition(uint32_t expectedPosition);
2022-01-17 15:58:27 +01:00
2022-02-05 13:19:20 +01:00
#ifdef XIPHOS_Q7S
2022-01-17 15:58:27 +01:00
/**
* @brief Checks if a path points to an sd card and whether the SD card is monuted.
*
2022-08-24 17:27:47 +02:00
* @return SD_NOT_MOUNTED id SD card is not mounted, otherwise returnvalue::OK
2022-01-17 15:58:27 +01:00
*/
ReturnValue_t checkPath(std::string name);
2022-02-05 13:19:20 +01:00
#endif
2022-02-01 10:45:07 +01:00
2022-02-04 13:06:56 +01:00
/**
* @brief Unlocks a range of flash regions
*
* @param from First region in range to unlock
* @param to Last region in range to unlock
*
*/
2022-02-14 11:28:15 +01:00
ReturnValue_t unlockAndEraseRegions(uint32_t from, uint32_t to);
2023-03-22 02:20:14 +01:00
/**
* The reply frame is stored in the data link layer helper. A pointer to the start of the frame
* is assigned to the @replyPtr member of this class. The frame length will be assigned to
* the @replyLen member.
* @param failParameter
* @return
*/
ReturnValue_t readOneReply(uint32_t failParameter);
2021-11-30 16:01:02 +01:00
};
2021-12-21 15:46:09 +01:00
#endif /* BSP_Q7S_DEVICES_STRHELPER_H_ */