From 8c649b3e700aa1a1965b56a0588762669f779c60 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Tue, 30 Nov 2021 16:01:02 +0100 Subject: [PATCH] str image loader wip --- bsp_q7s/devices/PlocUpdater.cpp | 35 +----------- bsp_q7s/devices/PlocUpdater.h | 7 --- bsp_q7s/devices/StrImageLoader.cpp | 75 ++++++++++++++++++++++++++ bsp_q7s/devices/StrImageLoader.h | 56 +++++++++++++++++++ bsp_q7s/memory/SdCardManager.cpp | 30 +++++++++++ bsp_q7s/memory/SdCardManager.h | 9 ++++ common/config/commonClassIds.h | 2 +- mission/devices/StarTrackerHandler.cpp | 17 ++++-- mission/devices/StarTrackerHandler.h | 9 +++- 9 files changed, 195 insertions(+), 45 deletions(-) create mode 100644 bsp_q7s/devices/StrImageLoader.cpp create mode 100644 bsp_q7s/devices/StrImageLoader.h diff --git a/bsp_q7s/devices/PlocUpdater.cpp b/bsp_q7s/devices/PlocUpdater.cpp index 4771e14c..592a2c0d 100644 --- a/bsp_q7s/devices/PlocUpdater.cpp +++ b/bsp_q7s/devices/PlocUpdater.cpp @@ -168,13 +168,13 @@ ReturnValue_t PlocUpdater::getImageLocation(const uint8_t* data, size_t size) { #if BOARD_TE0720 == 0 // Check if file is stored on SD card and if associated SD card is mounted if (std::string(reinterpret_cast(data), SD_PREFIX_LENGTH) == std::string(SdCardManager::SD_0_MOUNT_POINT)) { - if (!isSdCardMounted(sd::SLOT_0)) { + if (!sdcMan->isSdCardMounted(sd::SLOT_0)) { sif::warning << "PlocUpdater::getImageLocation: SD card 0 not mounted" << std::endl; return SD_NOT_MOUNTED; } } else if (std::string(reinterpret_cast(data), SD_PREFIX_LENGTH) == std::string(SdCardManager::SD_1_MOUNT_POINT)) { - if (!isSdCardMounted(sd::SLOT_0)) { + if (!sdcMan->isSdCardMounted(sd::SLOT_0)) { sif::warning << "PlocUpdater::getImageLocation: SD card 1 not mounted" << std::endl; return SD_NOT_MOUNTED; } @@ -193,37 +193,6 @@ ReturnValue_t PlocUpdater::getImageLocation(const uint8_t* data, size_t size) { return RETURN_OK; } -#if BOARD_TE0720 == 0 -bool PlocUpdater::isSdCardMounted(sd::SdCard sdCard) { - SdCardManager::SdStatePair active; - ReturnValue_t result = sdcMan->getSdCardActiveStatus(active); - if (result != RETURN_OK) { - sif::debug << "PlocUpdater::isSdCardMounted: Failed to get SD card active state"; - return false; - } - if (sdCard == sd::SLOT_0) { - if (active.first == sd::MOUNTED) { - return true; - } - else { - return false; - } - } - else if (sdCard == sd::SLOT_1) { - if (active.second == sd::MOUNTED) { - return true; - } - else { - return false; - } - } - else { - sif::debug << "PlocUpdater::isSdCardMounted: Unknown SD card specified" << std::endl; - } - return false; -} -#endif /* #if BOARD_TE0720 == 0 */ - void PlocUpdater::stepSuccessfulReceived(ActionId_t actionId, uint8_t step) { } diff --git a/bsp_q7s/devices/PlocUpdater.h b/bsp_q7s/devices/PlocUpdater.h index 50404d14..d016b9e5 100644 --- a/bsp_q7s/devices/PlocUpdater.h +++ b/bsp_q7s/devices/PlocUpdater.h @@ -174,13 +174,6 @@ private: */ void commandUpdateVerify(); -#if BOARD_TE0720 == 0 - /** - * @brief Checks whether the SD card to read from is mounted or not. - */ - bool isSdCardMounted(sd::SdCard sdCard); -#endif - void calcImageCrc(); void adjustSequenceFlags(PLOC_SPV::UpdatePacket& packet); diff --git a/bsp_q7s/devices/StrImageLoader.cpp b/bsp_q7s/devices/StrImageLoader.cpp new file mode 100644 index 00000000..803fb122 --- /dev/null +++ b/bsp_q7s/devices/StrImageLoader.cpp @@ -0,0 +1,75 @@ +#include "StrImageLoader.h" + +#include + +StrImageLoader::StrImageLoader(object_id_t objectId) { + +} + +StrImageLoader::~StrImageLoader() { +} + +ReturnValue_t StrImageLoader::initialize() { + sdcMan = SdCardManager::instance(); + if (sdcMan == nullptr) { + sif::warning << "StrImageLoader::initialize: Invalid SD Card Manager" << std::endl; + } +} + +ReturnValue_t StrImageLoader::performOperation(uint8_t operationCode) { + semaphore.acquire(); + while(true) { + switch(internalState) { + case InternalState::IDLE: + semaphore.acquire(); + break; + case InternalState::UPLOAD_IMAGE: + uploadImage(); + break; + case InternalState::DOWNLOAD_IMAGE: + break; + } + } +} + +void StrImageLoader::setComIF(DeviceCommunicationIF* communicationInterface_) { + communicationInterface = communicationInterface_; +} + +void StrImageLoader::setComCookie(CookieIF* comCookie_) { + comCookie = comCookie_; +} + +ReturnValue_t StrImageLoader::startImageUpload(std::string image) { + + //TODO: Use string part not data pointer + // Check if file is stored on SD card and if associated SD card is mounted + if (image.substr(0, sizeof(SdCardManager::SD_0_MOUNT_POINT)) + == std::string(SdCardManager::SD_0_MOUNT_POINT)) { + if (!sdcMan->isSdCardMounted(sd::SLOT_0)) { + sif::warning << "StrImageLoader::getImageLocation: SD card 0 not mounted" << std::endl; + return SD_NOT_MOUNTED; + } + } else if (image.substr(0, sizeof(SdCardManager::SD_1_MOUNT_POINT)) + == std::string(SdCardManager::SD_1_MOUNT_POINT)) { + if (!sdcMan->isSdCardMounted(sd::SLOT_0)) { + sif::warning << "StrImageLoader::getImageLocation: SD card 1 not mounted" << std::endl; + return SD_NOT_MOUNTED; + } + } + + uploadImage = image; + + if(not std::filesystem::exists(uploadImage)) { + return FILE_NOT_EXISTS; + } + + internalState = InternalState::UPLOAD_IMAGE; + + semaphore.release(); +} + +ReturnValue_t StrImageLoader::uploadImage() { + +} + diff --git a/bsp_q7s/devices/StrImageLoader.h b/bsp_q7s/devices/StrImageLoader.h new file mode 100644 index 00000000..39013fe6 --- /dev/null +++ b/bsp_q7s/devices/StrImageLoader.h @@ -0,0 +1,56 @@ +#ifndef BSP_Q7S_DEVICES_STRIMAGELOADER_H_ +#define BSP_Q7S_DEVICES_STRIMAGELOADER_H_ + +#include + +#include "fsfw/osal/linux/BinarySemaphore.h" +#include "bsp_q7s/memory/SdCardManager.h" + +/** + * @brief An object of this class runs in a separate task and is responsible for uploading and + * downloading images to/from the star tracker. This is required because uploading and + * downloading via the star tracker handler takes a lot of time because each upload or + * download packet can transport a maximum of 1024 bytes. + */ +class StrImageLoader: public SystemObject, public ExecutableObjectIF { +public: + StrImageLoader(object_id_t objectId); + virtual ~StrImageLoader(); + + ReturnValue_t performOperation(uint8_t operationCode = 0) override; + + void setComIF(DeviceCommunicationIF* communicationInterface_); + void setComCookie(CookieIF* comCookie_); + +private: + + static const uint8_t INTERFACE_ID = CLASS_ID::STR_IMG_LOADER; + + //! [EXPORT] : [COMMENT] SD card specified in path string not mounted + static const ReturnValue_t SD_NOT_MOUNTED = MAKE_RETURN_CODE(0xA0); + //! [EXPORT] : [COMMENT] Specified file does not exist on filesystem + static const ReturnValue_t FILE_NOT_EXISTS = MAKE_RETURN_CODE(0xA1); + + enum class InternalState { + IDLE, + UPLOAD_IMAGE, + DOWNLOAD_IMAGE + }; + + InternalState internalState = InternalState::IDLE; + + BinarySemaphore semaphore; + + // Absolute path and name to image to upload + std::string uploadImage; + + SdCardManager* sdcMan = nullptr; + + /** + * Communication object responsible for low level access of star tracker + * Must be set by star tracker handler + */ + DeviceCommunicationIF * communicationInterface = nullptr; +}; + +#endif /* BSP_Q7S_DEVICES_STRIMAGELOADER_H_ */ diff --git a/bsp_q7s/memory/SdCardManager.cpp b/bsp_q7s/memory/SdCardManager.cpp index ebebf854..ea16b672 100644 --- a/bsp_q7s/memory/SdCardManager.cpp +++ b/bsp_q7s/memory/SdCardManager.cpp @@ -455,3 +455,33 @@ void SdCardManager::setPrintCommandOutput(bool print) { } +bool SdCardManager::isSdCardMounted(sd::SdCard sdCard) { + SdCardManager::SdStatePair active; + ReturnValue_t result = sdcMan->getSdCardActiveStatus(active); + if (result != RETURN_OK) { + sif::debug << "SdCardManager::isSdCardMounted: Failed to get SD card active state"; + return false; + } + if (sdCard == sd::SLOT_0) { + if (active.first == sd::MOUNTED) { + return true; + } + else { + return false; + } + } + else if (sdCard == sd::SLOT_1) { + if (active.second == sd::MOUNTED) { + return true; + } + else { + return false; + } + } + else { + sif::debug << "SdCardManager::isSdCardMounted: Unknown SD card specified" << std::endl; + } + return false; +} + + diff --git a/bsp_q7s/memory/SdCardManager.h b/bsp_q7s/memory/SdCardManager.h index 6f03e7f1..6e4930da 100644 --- a/bsp_q7s/memory/SdCardManager.h +++ b/bsp_q7s/memory/SdCardManager.h @@ -195,6 +195,15 @@ public: void setBlocking(bool blocking); void setPrintCommandOutput(bool print); + + /** + * @brief Checks if an SD card is mounted + * + * @param sdCard The SD crad to check + * + * @return true if mounted, otherwise false + */ + bool isSdCardMounted(sd::SdCard sdCard); private: CommandExecutor cmdExecutor; Operations currentOp = Operations::IDLE; diff --git a/common/config/commonClassIds.h b/common/config/commonClassIds.h index b2885009..a47e97d1 100644 --- a/common/config/commonClassIds.h +++ b/common/config/commonClassIds.h @@ -19,7 +19,7 @@ enum commonClassIds: uint8_t { CCSDS_IP_CORE_BRIDGE, //IPCI PTME, //PTME PLOC_UPDATER, //PLUD - STR_IMG_HELPER, //STRIMGH + STR_IMG_LOADER, //STRIMGL GOM_SPACE_HANDLER, //GOMS PLOC_MEMORY_DUMPER, //PLMEMDUMP PDEC_HANDLER, //PDEC diff --git a/mission/devices/StarTrackerHandler.cpp b/mission/devices/StarTrackerHandler.cpp index ed5f5e56..f05dec71 100644 --- a/mission/devices/StarTrackerHandler.cpp +++ b/mission/devices/StarTrackerHandler.cpp @@ -10,16 +10,27 @@ extern "C" { } StarTrackerHandler::StarTrackerHandler(object_id_t objectId, object_id_t comIF, - CookieIF * comCookie) : + CookieIF * comCookie, StrImageLoader* strImageLoader) : DeviceHandlerBase(objectId, comIF, comCookie), temperatureSet(this), versionSet(this), powerSet( - this), interfaceSet(this), timeSet(this), solutionSet(this) { - if (comCookie == NULL) { + this), interfaceSet(this), timeSet(this), solutionSet(this), strImageLoader( + strImageLoader) { + if (comCookie == nullptr) { sif::error << "StarTrackerHandler: Invalid com cookie" << std::endl; } + if (strImageLoader == nullptr) { + sif::error << "StarTrackerHandler: Invalid str image loader" << std::endl; + } slipInit(); } StarTrackerHandler::~StarTrackerHandler() { + DeviceHandlerBase::initialize(); + strImageLoader->setComIF(communicationInterface); + strImageLoader->setComCookie(comCookie); +} + +ReturnValue_t StarTrackerHandler::initialize() { + } void StarTrackerHandler::doStartUp() { diff --git a/mission/devices/StarTrackerHandler.h b/mission/devices/StarTrackerHandler.h index cbf05242..d74d6fd2 100644 --- a/mission/devices/StarTrackerHandler.h +++ b/mission/devices/StarTrackerHandler.h @@ -6,6 +6,7 @@ #include #include #include +#include /** * @brief This is the device handler for the star tracker from arcsec. @@ -28,9 +29,12 @@ public: * @param enablePin GPIO connected to the enable pin of the reaction wheels. Must be pulled * to high to enable the device. */ - StarTrackerHandler(object_id_t objectId, object_id_t comIF, CookieIF * comCookie); + StarTrackerHandler(object_id_t objectId, object_id_t comIF, CookieIF * comCookie, + StrImageLoader* strImageLoader); virtual ~StarTrackerHandler(); + ReturnValue_t initialize() override; + protected: void doStartUp() override; void doShutDown() override; @@ -106,6 +110,9 @@ private: static const MutexIF::TimeoutType TIMEOUT_TYPE= MutexIF::TimeoutType::WAITING; static const uint32_t MUTEX_TIMEOUT = 20; + // Pointer to object responsible for uploading and downloading images to/from the star tracker + StrImageLoader* strImageLoader = nullptr; + StarTracker::TemperatureSet temperatureSet; StarTracker::VersionSet versionSet; StarTracker::PowerSet powerSet;