Skip to content

Commit

Permalink
Add and use a strong type for program ID (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickKa authored May 23, 2024
2 parents 79f6e50 + 8e41ee4 commit ab9c466
Show file tree
Hide file tree
Showing 22 changed files with 124 additions and 40 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ include(cmake/clang-tidy-cache.cmake)
find_package_and_notify(etl)
find_rodos()
find_package_and_notify(littlefs)
find_package_and_notify(NamedType)

# ---- Declare targets ----

add_library(Sts1CobcSw_Dummy STATIC)
add_library(Sts1CobcSw_Edu STATIC)
add_library(Sts1CobcSw_FileSystem STATIC)
add_library(Sts1CobcSw_Outcome INTERFACE)
add_library(Sts1CobcSw_ProgramId INTERFACE)
add_library(Sts1CobcSw_Serial INTERFACE)
add_library(Sts1CobcSw_Utility STATIC)
add_library(Sts1CobcSw_Periphery STATIC)
Expand Down
3 changes: 2 additions & 1 deletion Sts1CobcSw/CommandParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <Sts1CobcSw/Edu/Edu.hpp>
#include <Sts1CobcSw/Edu/ProgramQueue.hpp>
#include <Sts1CobcSw/EduProgramQueueThread.hpp>
#include <Sts1CobcSw/ProgramId/ProgramId.hpp>
#include <Sts1CobcSw/Utility/Debug.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

Expand Down Expand Up @@ -88,7 +89,7 @@ auto ParseAndAddQueueEntries(std::span<Byte const> queueEntries) -> void
auto entry =
Deserialize<edu::QueueEntry>(queueEntries.first<serialSize<edu::QueueEntry>>());

DEBUG_PRINT("Prog ID : %" PRIu16 "\n", entry.programId);
DEBUG_PRINT("Prog ID : %" PRIu16 "\n", entry.programId.get());
DEBUG_PRINT("Start Time : %" PRIi32 "\n", entry.startTime);
DEBUG_PRINT("Timeout : %" PRIi16 "\n", entry.timeout);

Expand Down
9 changes: 5 additions & 4 deletions Sts1CobcSw/Edu/EduMock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ auto TurnOff() -> void
auto StoreProgram(StoreProgramData const & data) -> Result<void>
{
PrintFormattedSystemUtc();
DEBUG_PRINT("Call to StoreProgram(programId = %d)\n", data.programId);
DEBUG_PRINT("Call to StoreProgram(programId = %" PRIu16 ")\n", data.programId.get());
return outcome_v2::success();
}


auto ExecuteProgram(ExecuteProgramData const & data) -> Result<void>
{
PrintFormattedSystemUtc();
DEBUG_PRINT("Call to ExecuteProgram(programId = %d, startTime = %" PRIi32 ", timeout = %d)\n",
data.programId,
DEBUG_PRINT("Call to ExecuteProgram(programId = %" PRIu16 ", startTime = %" PRIi32
", timeout = %d)\n",
data.programId.get(),
data.startTime,
data.timeout);
return outcome_v2::success();
Expand All @@ -75,7 +76,7 @@ auto GetStatus() -> Result<Status>
{
PrintFormattedSystemUtc();
DEBUG_PRINT("Call to GetStatus()\n");
return Status{.statusType = StatusType::invalid, .programId = 0, .startTime = 0, .exitCode = 0};
return Status();
}


Expand Down
1 change: 0 additions & 1 deletion Sts1CobcSw/Edu/ProgramQueue.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <Sts1CobcSw/Edu/ProgramQueue.hpp>
#include <Sts1CobcSw/Serial/Serial.hpp>


// TODO: Change namespace here too, or move it back to Sts1CobSw/
Expand Down
3 changes: 2 additions & 1 deletion Sts1CobcSw/Edu/ProgramQueue.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once


#include <Sts1CobcSw/ProgramId/ProgramId.hpp>
#include <Sts1CobcSw/Serial/Serial.hpp>

#include <etl/vector.h>
Expand All @@ -16,7 +17,7 @@ namespace edu
{
struct QueueEntry
{
std::uint16_t programId = 0;
ProgramId programId = ProgramId(0);
std::int32_t startTime = 0;
std::int16_t timeout = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion Sts1CobcSw/Edu/ProgramStatusHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace sts1cobcsw::edu
RODOS::RingBuffer<ProgramStatusHistoryEntry, programStatusHistorySize> programStatusHistory;


auto UpdateProgramStatusHistory(std::uint16_t programId,
auto UpdateProgramStatusHistory(ProgramId programId,
std::int32_t startTime,
ProgramStatus newStatus) -> void
{
Expand Down
6 changes: 4 additions & 2 deletions Sts1CobcSw/Edu/ProgramStatusHistory.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once


#include <Sts1CobcSw/ProgramId/ProgramId.hpp>

// clang-format off
#include <cstdint>
// ringbuffer.h does not include <cstdint> even though it requires it
Expand All @@ -25,7 +27,7 @@ enum class ProgramStatus : std::uint8_t

struct ProgramStatusHistoryEntry
{
std::uint16_t programId = 0;
ProgramId programId = ProgramId(0);
std::int32_t startTime = 0;
ProgramStatus status = ProgramStatus::programRunning;
};
Expand All @@ -35,7 +37,7 @@ inline constexpr auto programStatusHistorySize = 20;
extern RODOS::RingBuffer<ProgramStatusHistoryEntry, programStatusHistorySize> programStatusHistory;


auto UpdateProgramStatusHistory(std::uint16_t programId,
auto UpdateProgramStatusHistory(ProgramId programId,
std::int32_t startTime,
ProgramStatus newStatus) -> void;
}
13 changes: 7 additions & 6 deletions Sts1CobcSw/Edu/Types.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once


#include <Sts1CobcSw/ProgramId/ProgramId.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>
#include <Sts1CobcSw/Serial/Serial.hpp>

Expand Down Expand Up @@ -42,14 +43,14 @@ enum class StatusType
struct StoreProgramData
{
static constexpr auto id = 0x01_b;
std::uint16_t programId = 0;
ProgramId programId = ProgramId(0);
};


struct ExecuteProgramData
{
static constexpr auto id = 0x02_b;
std::uint16_t programId = 0;
ProgramId programId = ProgramId(0);
std::int32_t startTime = 0;
std::int16_t timeout = 0;
};
Expand All @@ -70,7 +71,7 @@ struct GetStatusData
struct ReturnResultData
{
static constexpr auto id = 0x05_b;
std::uint16_t programId = 0;
ProgramId programId = ProgramId(0);
std::int32_t startTime = 0;
};

Expand All @@ -85,7 +86,7 @@ struct UpdateTimeData
struct Status
{
StatusType statusType = StatusType::invalid;
std::uint16_t programId = 0;
ProgramId programId = ProgramId(0);
std::int32_t startTime = 0;
std::uint8_t exitCode = 0;
};
Expand All @@ -100,7 +101,7 @@ struct NoEventData
struct ProgramFinishedData
{
static constexpr auto id = 0x01_b;
std::uint16_t programId = 0;
ProgramId programId = ProgramId(0);
std::int32_t startTime = 0;
std::uint8_t exitCode = 0;
};
Expand All @@ -109,7 +110,7 @@ struct ProgramFinishedData
struct ResultsReadyData
{
static constexpr auto id = 0x02_b;
std::uint16_t programId = 0;
ProgramId programId = ProgramId(0);
std::int32_t startTime = 0;
};

Expand Down
3 changes: 2 additions & 1 deletion Sts1CobcSw/EduProgramQueueThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Sts1CobcSw/Edu/Types.hpp>
#include <Sts1CobcSw/EduCommunicationErrorThread.hpp>
#include <Sts1CobcSw/EduProgramQueueThread.hpp>
#include <Sts1CobcSw/ProgramId/ProgramId.hpp>
#include <Sts1CobcSw/ThreadPriorities.hpp>
#include <Sts1CobcSw/TopicsAndSubscribers.hpp>
#include <Sts1CobcSw/Utility/Debug.hpp>
Expand Down Expand Up @@ -125,7 +126,7 @@ class EduProgramQueueThread : public RODOS::StaticThread<stackSize>
auto programId = edu::programQueue[edu::queueIndex].programId;
auto timeout = edu::programQueue[edu::queueIndex].timeout;

DEBUG_PRINT("Executing program %d\n", programId);
DEBUG_PRINT("Executing program %" PRIu16 "\n", programId.get());
auto executeProgramData = edu::ExecuteProgramData{
.programId = programId, .startTime = startTime, .timeout = timeout};
// Start Process
Expand Down
2 changes: 1 addition & 1 deletion Sts1CobcSw/FileSystem/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ auto Unmount() -> int
}


auto OpenProgramFile(std::uint16_t programId, int flags) -> int
auto OpenProgramFile(ProgramId programId, int flags) -> int
{
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions Sts1CobcSw/FileSystem/FileSystem.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#pragma once


#include <Sts1CobcSw/ProgramId/ProgramId.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>

#include <littlefs/lfs.h>

#include <etl/vector.h>

#include <cstddef>
#include <cstdint>


namespace sts1cobcsw::fs::deprecated
Expand All @@ -25,7 +25,7 @@ auto Mount() -> int;
auto Unmount() -> int;

// File stuff
auto OpenProgramFile(std::uint16_t programId, int flags) -> int;
auto OpenProgramFile(ProgramId programId, int flags) -> int;
auto CloseProgramFile() -> int;
template<std::size_t size>
auto ReadProgramFile(etl::vector<Byte, size> * buffer) -> int;
Expand Down
2 changes: 2 additions & 0 deletions Sts1CobcSw/FlashStartupTestThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <Sts1CobcSw/SpiStartupTestAndSupervisorThread.hpp>
#include <Sts1CobcSw/ThreadPriorities.hpp>

#include <rodos_no_using_namespace.h>


namespace sts1cobcsw
{
Expand Down
4 changes: 4 additions & 0 deletions Sts1CobcSw/FramEpsStartupTestThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <Sts1CobcSw/SpiStartupTestAndSupervisorThread.hpp>
#include <Sts1CobcSw/ThreadPriorities.hpp>

#include <rodos_no_using_namespace.h>

#include <array>


namespace sts1cobcsw
{
Expand Down
1 change: 1 addition & 0 deletions Sts1CobcSw/ProgramId/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target_link_libraries(Sts1CobcSw_ProgramId INTERFACE NamedType::NamedType Sts1CobcSw_Serial)
27 changes: 27 additions & 0 deletions Sts1CobcSw/ProgramId/ProgramId.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once


#include <Sts1CobcSw/Serial/Serial.hpp>

#include <NamedType/named_type.hpp>

#include <cstdint>


namespace sts1cobcsw
{
using ProgramId = fluent::NamedType<std::uint16_t, struct ProgramIdTag, fluent::Callable>;

// TODO: Maybe make ProgramId completely serializable, i.e., overload (De-)Serialize() as well
template<>
inline constexpr std::size_t serialSize<ProgramId> = totalSerialSize<ProgramId::UnderlyingType>;


template<std::endian endianness>
[[nodiscard]] inline auto SerializeTo(void * destination, ProgramId const & data) -> void *;
template<std::endian endianness>
[[nodiscard]] inline auto DeserializeFrom(void const * source, ProgramId * data) -> void const *;
}


#include <Sts1CobcSw/ProgramId/ProgramId.ipp>
21 changes: 21 additions & 0 deletions Sts1CobcSw/ProgramId/ProgramId.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once


#include <Sts1CobcSw/ProgramId/ProgramId.hpp>


namespace sts1cobcsw
{
template<std::endian endianness>
inline auto SerializeTo(void * destination, ProgramId const & data) -> void *
{
return SerializeTo<endianness>(destination, data.get());
}


template<std::endian endianness>
inline auto DeserializeFrom(void const * source, ProgramId * data) -> void const *
{
return DeserializeFrom<endianness>(source, &(data->get()));
}
}
2 changes: 2 additions & 0 deletions Sts1CobcSw/RfStartupTestThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <Sts1CobcSw/SpiStartupTestAndSupervisorThread.hpp>
#include <Sts1CobcSw/ThreadPriorities.hpp>

#include <rodos_no_using_namespace.h>


namespace sts1cobcsw
{
Expand Down
1 change: 1 addition & 0 deletions Sts1CobcSw/Serial/Serial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ template<typename T, std::size_t size>
inline constexpr std::size_t serialSize<std::array<T, size>> = serialSize<T> * size;

template<typename... Ts>
requires((serialSize<Ts> != 0) and ...)
inline constexpr std::size_t totalSerialSize = (serialSize<Ts> + ...);

inline constexpr auto defaultEndianness = std::endian::little;
Expand Down
2 changes: 2 additions & 0 deletions Sts1CobcSw/SpiStartupTestAndSupervisorThread.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <Sts1CobcSw/SpiStartupTestAndSupervisorThread.hpp>
#include <Sts1CobcSw/ThreadPriorities.hpp>

#include <rodos_no_using_namespace.h>


namespace sts1cobcsw
{
Expand Down
41 changes: 27 additions & 14 deletions Tests/GoldenTests/UpdateRingBuffer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void PrintBuffer()
{
RODOS::PRINTF("Vals[%d] = .id(%d), .status(%s)\n",
i,
edu::programStatusHistory.vals[i].programId,
edu::programStatusHistory.vals[i].programId.get(),
ToString(edu::programStatusHistory.vals[i].status).data());
}
}
Expand All @@ -50,14 +50,22 @@ class UpdateRingBufferTest : public RODOS::StaticThread<>
{
printfMask = 1;

edu::programStatusHistory.put(edu::ProgramStatusHistoryEntry{
.programId = 1, .startTime = 1, .status = edu::ProgramStatus::programExecutionFailed});
edu::programStatusHistory.put(edu::ProgramStatusHistoryEntry{
.programId = 2, .startTime = 1, .status = edu::ProgramStatus::programRunning});
edu::programStatusHistory.put(edu::ProgramStatusHistoryEntry{
.programId = 3, .startTime = 1, .status = edu::ProgramStatus::programRunning});
edu::programStatusHistory.put(edu::ProgramStatusHistoryEntry{
.programId = 4, .startTime = 1, .status = edu::ProgramStatus::programRunning});
edu::programStatusHistory.put(
edu::ProgramStatusHistoryEntry{.programId = ProgramId(1),
.startTime = 1,
.status = edu::ProgramStatus::programExecutionFailed});
edu::programStatusHistory.put(
edu::ProgramStatusHistoryEntry{.programId = ProgramId(2),
.startTime = 1,
.status = edu::ProgramStatus::programRunning});
edu::programStatusHistory.put(
edu::ProgramStatusHistoryEntry{.programId = ProgramId(3),
.startTime = 1,
.status = edu::ProgramStatus::programRunning});
edu::programStatusHistory.put(
edu::ProgramStatusHistoryEntry{.programId = ProgramId(4),
.startTime = 1,
.status = edu::ProgramStatus::programRunning});


auto readCnt = edu::programStatusHistory.readCnt;
Expand All @@ -66,11 +74,16 @@ class UpdateRingBufferTest : public RODOS::StaticThread<>

PrintBuffer();

edu::UpdateProgramStatusHistory(2, 1, edu::ProgramStatus::programExecutionSucceeded);
edu::UpdateProgramStatusHistory(4, 1, edu::ProgramStatus::programExecutionFailed);
edu::programStatusHistory.put(edu::ProgramStatusHistoryEntry{
.programId = 5, .startTime = 1, .status = edu::ProgramStatus::programRunning});
edu::UpdateProgramStatusHistory(5, 1, edu::ProgramStatus::programExecutionSucceeded);
edu::UpdateProgramStatusHistory(
ProgramId(2), 1, edu::ProgramStatus::programExecutionSucceeded);
edu::UpdateProgramStatusHistory(
ProgramId(4), 1, edu::ProgramStatus::programExecutionFailed);
edu::programStatusHistory.put(
edu::ProgramStatusHistoryEntry{.programId = ProgramId(5),
.startTime = 1,
.status = edu::ProgramStatus::programRunning});
edu::UpdateProgramStatusHistory(
ProgramId(5), 1, edu::ProgramStatus::programExecutionSucceeded);

// 1, because we did not read anything
RODOS::PRINTF("readCnt unchanged : %d\n",
Expand Down
Loading

0 comments on commit ab9c466

Please sign in to comment.