eive-obsw/bsp_q7s/fs/SdCardManager.h

244 lines
8.8 KiB
C
Raw Normal View History

2021-07-07 20:50:11 +02:00
#ifndef BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_
#define BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_
#include <fsfw/objectmanager/SystemObject.h>
2021-08-05 10:14:17 +02:00
#include <poll.h>
2022-01-17 15:58:27 +01:00
#include <array>
2021-07-07 20:50:11 +02:00
#include <cstdint>
#include <optional>
2022-01-17 15:58:27 +01:00
#include <string>
#include <utility>
#include "events/subsystemIdRanges.h"
#include "fsfw/events/Event.h"
2022-08-24 17:27:47 +02:00
#include "fsfw/returnvalues/returnvalue.h"
2022-01-17 15:58:27 +01:00
#include "fsfw_hal/linux/CommandExecutor.h"
2022-03-01 17:41:58 +01:00
#include "mission/memory/SdCardMountedIF.h"
#include "mission/memory/definitions.h"
2022-01-17 15:58:27 +01:00
#include "returnvalues/classIds.h"
2021-07-07 20:50:11 +02:00
class MutexIF;
/**
* @brief Manages handling of SD cards like switching them on or off or getting the current
* state
*/
2022-08-24 17:27:47 +02:00
class SdCardManager : public SystemObject, public SdCardMountedIF {
2022-01-17 15:58:27 +01:00
friend class SdCardAccess;
public:
2022-03-01 17:41:58 +01:00
using mountInitCb = ReturnValue_t (*)(void* args);
2022-01-17 15:58:27 +01:00
enum class Operations { SWITCHING_ON, SWITCHING_OFF, MOUNTING, UNMOUNTING, IDLE };
enum class OpStatus { IDLE, TIMEOUT, ONGOING, SUCCESS, FAIL };
using SdStatePair = std::pair<sd::SdState, sd::SdState>;
2022-05-24 00:20:23 +02:00
struct SdInfo {
sd::SdCard pref = sd::SdCard::NONE;
sd::SdCard other = sd::SdCard::NONE;
sd::SdCard active = sd::SdCard::NONE;
} sdInfo;
2022-01-17 15:58:27 +01:00
static constexpr uint8_t INTERFACE_ID = CLASS_ID::SD_CARD_MANAGER;
2022-08-24 17:27:47 +02:00
static constexpr ReturnValue_t OP_ONGOING = returnvalue::makeCode(INTERFACE_ID, 0);
static constexpr ReturnValue_t ALREADY_ON = returnvalue::makeCode(INTERFACE_ID, 1);
2022-08-26 14:28:06 +02:00
static constexpr ReturnValue_t ALREADY_MOUNTED = returnvalue::makeCode(INTERFACE_ID, 2);
2022-08-24 17:27:47 +02:00
static constexpr ReturnValue_t ALREADY_OFF = returnvalue::makeCode(INTERFACE_ID, 3);
2022-08-26 14:28:06 +02:00
static constexpr ReturnValue_t STATUS_FILE_NEXISTS = returnvalue::makeCode(INTERFACE_ID, 10);
2022-01-17 15:58:27 +01:00
static constexpr ReturnValue_t STATUS_FILE_FORMAT_INVALID =
2022-08-24 17:27:47 +02:00
returnvalue::makeCode(INTERFACE_ID, 11);
static constexpr ReturnValue_t MOUNT_ERROR = returnvalue::makeCode(INTERFACE_ID, 12);
2022-08-26 14:28:06 +02:00
static constexpr ReturnValue_t UNMOUNT_ERROR = returnvalue::makeCode(INTERFACE_ID, 13);
static constexpr ReturnValue_t SYSTEM_CALL_ERROR = returnvalue::makeCode(INTERFACE_ID, 14);
static constexpr ReturnValue_t POPEN_CALL_ERROR = returnvalue::makeCode(INTERFACE_ID, 15);
2022-01-17 15:58:27 +01:00
static constexpr uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::FILE_SYSTEM;
static constexpr Event SANITIZATION_FAILED = event::makeEvent(SUBSYSTEM_ID, 0, severity::LOW);
static constexpr Event MOUNTED_SD_CARD = event::makeEvent(SUBSYSTEM_ID, 1, severity::INFO);
2022-01-17 15:58:27 +01:00
// C++17 does not support constexpr std::string yet
static constexpr char SD_0_DEV_NAME[] = "/dev/mmcblk0p1";
static constexpr char SD_1_DEV_NAME[] = "/dev/mmcblk1p1";
2022-09-26 11:39:17 +02:00
2022-01-17 15:58:27 +01:00
static constexpr char SD_STATE_FILE[] = "/tmp/sd_status.txt";
virtual ~SdCardManager();
static void create();
/**
* Returns the single instance of the SD card manager.
*/
static SdCardManager* instance();
/**
* Set the preferred SD card which will determine which SD card will be used as the primary
* SD card in hot redundant and cold redundant mode. This function will not switch the
* SD cards which are currently on and mounted, this needs to be implemented by
* an upper layer by using #switchOffSdCard , #switchOnSdCard and #updateSdCardStateFile
* @param sdCard
* @return
*/
ReturnValue_t setPreferredSdCard(sd::SdCard sdCard);
/**
* Get the currently configured preferred SD card
* @param sdCard
* @return
*/
2022-09-12 17:23:43 +02:00
std::optional<sd::SdCard> getPreferredSdCard() const override;
2022-01-17 15:58:27 +01:00
/**
* Switch on the specified SD card.
* @param sdCard
* @param doMountSdCard Mount the SD card after switching it on, which is necessary
* to use it
* @param statusPair If the status pair is already available, it can be passed here
2022-08-24 17:27:47 +02:00
* @return - returnvalue::OK on success, ALREADY_ON if it is already on,
2022-01-17 15:58:27 +01:00
* SYSTEM_CALL_ERROR on system error
*/
ReturnValue_t switchOnSdCard(sd::SdCard sdCard, bool doMountSdCard = true,
SdStatePair* statusPair = nullptr);
/**
* Switch off the specified SD card.
* @param sdCard
* @param doUnmountSdCard Unmount the SD card before switching the card off, which makes
* the operation safer
* @param statusPair If the status pair is already available, it can be passed here
2022-08-24 17:27:47 +02:00
* @return - returnvalue::OK on success, ALREADY_ON if it is already on,
2022-01-17 15:58:27 +01:00
* SYSTEM_CALL_ERROR on system error
*/
ReturnValue_t switchOffSdCard(sd::SdCard sdCard, bool doUnmountSdCard = true,
SdStatePair* statusPair = nullptr);
/**
* Update the state file or creates one if it does not exist. You need to call this
* function before calling #sdCardActive
* @return
2022-08-24 17:27:47 +02:00
* - returnvalue::OK if the state file was updated successfully
2022-01-17 15:58:27 +01:00
* - CommandExecutor::COMMAND_PENDING: Non-blocking command is pending
2022-08-24 17:27:47 +02:00
* - returnvalue::FAILED: blocking command failed
2022-01-17 15:58:27 +01:00
*/
ReturnValue_t updateSdCardStateFile();
/**
* Get the state of the SD cards. If the state file does not exist, this function will
* take care of updating it. If it does not, the function will use the state file to get
* the status of the SD cards and set the field of the provided boolean pair.
* @param active Pair of booleans, where the first entry is the state of the first SD card
* and the second one the state of the second SD card
2022-08-24 17:27:47 +02:00
* @return - returnvalue::OK if the state was read successfully
2022-01-17 15:58:27 +01:00
* - STATUS_FILE_FORMAT_INVALID if there was an issue with the state file. The user
* should call #updateSdCardStateFile again in that case
* - STATUS_FILE_NEXISTS if the status file does not exist
*/
2022-05-24 00:20:23 +02:00
ReturnValue_t getSdCardsStatus(SdStatePair& active);
2022-01-17 15:58:27 +01:00
/**
* Mount the specified SD card. This is necessary to use it.
* @param sdCard
* @return
*/
ReturnValue_t mountSdCard(sd::SdCard sdCard);
2022-05-24 00:20:23 +02:00
/**
* Set the currently active SD card. This does not necessarily mean that the SD card is on or
* mounted
* @param sdCard
*/
void setActiveSdCard(sd::SdCard sdCard) override;
/**
* Get the currently active SD card. This does not necessarily mean that the SD card is on or
* mounted
* @return
*/
2022-09-12 17:23:43 +02:00
std::optional<sd::SdCard> getActiveSdCard() const override;
2022-05-24 00:20:23 +02:00
2022-01-17 15:58:27 +01:00
/**
* Unmount the specified SD card. This is recommended before switching it off. The SD card
* can't be used after it has been unmounted.
* @param sdCard
* @return
*/
ReturnValue_t unmountSdCard(sd::SdCard sdCard);
/**
* In case that there is a discrepancy between the preferred SD card and the currently
* mounted one, this function will sanitize the state by attempting to mount the
* currently preferred SD card. If the caller already has state information, it can be
* passed into the function. For now, this operation will be enforced in blocking mode.
* @param statusPair Current SD card status capture with #getSdCardActiveStatus
* @param prefSdCard Preferred SD card captured with #getPreferredSdCard
* @throws std::bad_alloc if one of the two arguments was a nullptr and an allocation failed
* @return
*/
ReturnValue_t sanitizeState(SdStatePair* statusPair = nullptr,
sd::SdCard prefSdCard = sd::SdCard::NONE);
/**
* If sd::SdCard::NONE is passed as an argument, this function will get the currently
* preferred SD card from the scratch buffer.
* @param prefSdCardPtr
* @return
*/
2023-02-22 18:06:34 +01:00
const char* getCurrentMountPrefix() const override;
2022-01-17 15:58:27 +01:00
OpStatus checkCurrentOp(Operations& currentOp);
/**
* If there are issues with the state machine, it can be reset with this function
*/
void resetState();
void setBlocking(bool blocking);
void setPrintCommandOutput(bool print);
/**
2022-03-01 17:41:58 +01:00
* @brief Checks if an SD card is mounted.
2022-01-17 15:58:27 +01:00
*
* @param sdCard The SD card to check
2022-01-17 15:58:27 +01:00
*
* @return true if mounted, otherwise false
*/
2022-10-25 11:22:31 +02:00
bool isSdCardUsable(std::optional<sd::SdCard> sdCard) override;
2022-01-17 15:58:27 +01:00
2022-05-11 17:02:57 +02:00
ReturnValue_t isSdCardMountedReadOnly(sd::SdCard sdcard, bool& readOnly);
ReturnValue_t remountReadWrite(sd::SdCard sdcard);
ReturnValue_t performFsck(sd::SdCard sdcard, bool printOutput, int& linuxError);
2022-10-25 11:04:24 +02:00
void markUnusable();
2022-01-17 15:58:27 +01:00
private:
CommandExecutor cmdExecutor;
Operations currentOp = Operations::IDLE;
bool blocking = false;
2022-09-27 10:51:07 +02:00
bool sdCardActive = true;
2022-01-17 15:58:27 +01:00
bool printCmdOutput = true;
2022-10-25 11:04:24 +02:00
bool markedUnusable = false;
2023-03-02 15:21:45 +01:00
MutexIF* sdLock = nullptr;
2023-03-02 15:41:09 +01:00
static constexpr MutexIF::TimeoutType LOCK_TYPE = MutexIF::TimeoutType::WAITING;
2023-03-04 14:32:18 +01:00
static constexpr uint32_t LOCK_TIMEOUT = 150;
2023-03-02 15:41:09 +01:00
static constexpr char LOCK_CTX[] = "SdCardManager";
2022-01-17 15:58:27 +01:00
SdCardManager();
ReturnValue_t setSdCardState(sd::SdCard sdCard, bool on);
void processSdStatusLine(SdStatePair& active, std::string& line, uint8_t& idx,
sd::SdCard& currentSd);
2023-02-22 18:06:34 +01:00
std::optional<std::string> currentPrefix;
2022-01-17 15:58:27 +01:00
2022-05-24 00:20:23 +02:00
static SdCardManager* INSTANCE;
2021-07-07 20:50:11 +02:00
};
#endif /* BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_ */