Merge remote-tracking branch 'upstream/mueller/master' into mueller/master

This commit is contained in:
Robin Müller 2021-02-03 13:36:45 +01:00
commit 5f197be2ff
19 changed files with 220 additions and 66 deletions

View File

@ -2,76 +2,124 @@
#include "../serviceinterface/ServiceInterface.h" #include "../serviceinterface/ServiceInterface.h"
#include <bitset> #include <bitset>
void arrayprinter::print(const uint8_t *data, size_t size, OutputType type, void arrayprinter::print(const uint8_t *data, size_t size, OutputType type,
bool printInfo, size_t maxCharPerLine) { bool printInfo, size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
if(printInfo) { if(printInfo) {
sif::info << "Printing data with size " << size << ": "; sif::info << "Printing data with size " << size << ": " << std::endl;
} }
sif::info << "[";
#else #else
sif::printInfo("Printing data with size %zu: [", size); #if FSFW_NO_C99_IO == 1
#endif sif::printInfo("Printing data with size %lu: \n", static_cast<unsigned long>(size));
if(type == OutputType::HEX) { #else
arrayprinter::printHex(data, size, maxCharPerLine); sif::printInfo("Printing data with size %zu: \n", size);
} #endif /* FSFW_NO_C99_IO == 1 */
else if (type == OutputType::DEC) { #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
arrayprinter::printDec(data, size, maxCharPerLine);
} if(type == OutputType::HEX) {
else if(type == OutputType::BIN) { arrayprinter::printHex(data, size, maxCharPerLine);
arrayprinter::printBin(data, size); }
} else if (type == OutputType::DEC) {
arrayprinter::printDec(data, size, maxCharPerLine);
}
else if(type == OutputType::BIN) {
arrayprinter::printBin(data, size);
}
} }
void arrayprinter::printHex(const uint8_t *data, size_t size, void arrayprinter::printHex(const uint8_t *data, size_t size,
size_t maxCharPerLine) { size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << std::hex; if(sif::info.crAdditionEnabled()) {
for(size_t i = 0; i < size; i++) { std::cout << "\r" << std::endl;
sif::info << "0x" << static_cast<int>(data[i]); }
if(i < size - 1){
sif::info << " , ";
if(i > 0 and i % maxCharPerLine == 0) {
sif::info << std::endl;
} std::cout << "[" << std::hex;
} for(size_t i = 0; i < size; i++) {
} std::cout << "0x" << static_cast<int>(data[i]);
sif::info << std::dec; if(i < size - 1) {
sif::info << "]" << std::endl; std::cout << " , ";
if(i > 0 and (i + 1) % maxCharPerLine == 0) {
std::cout << std::endl;
}
}
}
std::cout << std::dec;
std::cout << "]" << std::endl;
#else #else
// how much memory to reserve for printout? // General format: 0x01, 0x02, 0x03 so it is number of chars times 6
// plus line break plus small safety margin.
char printBuffer[(size + 1) * 7 + 1];
size_t currentPos = 0;
for(size_t i = 0; i < size; i++) {
// To avoid buffer overflows.
if(sizeof(printBuffer) - currentPos <= 7) {
break;
}
currentPos += snprintf(printBuffer + currentPos, 6, "0x%02x", data[i]);
if(i < size - 1) {
currentPos += sprintf(printBuffer + currentPos, ", ");
if(i > 0 and (i + 1) % maxCharPerLine == 0) {
currentPos += sprintf(printBuffer + currentPos, "\n");
}
}
}
printf("[%s]\n", printBuffer);
#endif #endif
} }
void arrayprinter::printDec(const uint8_t *data, size_t size, void arrayprinter::printDec(const uint8_t *data, size_t size,
size_t maxCharPerLine) { size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << std::dec; if(sif::info.crAdditionEnabled()) {
for(size_t i = 0; i < size; i++) { std::cout << "\r" << std::endl;
sif::info << static_cast<int>(data[i]); }
if(i < size - 1){
sif::info << " , "; std::cout << "[" << std::dec;
if(i > 0 and i % maxCharPerLine == 0) { for(size_t i = 0; i < size; i++) {
sif::info << std::endl; std::cout << static_cast<int>(data[i]);
} if(i < size - 1){
} std::cout << " , ";
} if(i > 0 and (i + 1) % maxCharPerLine == 0) {
sif::info << "]" << std::endl; std::cout << std::endl;
}
}
}
std::cout << "]" << std::endl;
#else #else
// how much memory to reserve for printout? // General format: 32, 243, -12 so it is number of chars times 5
// plus line break plus small safety margin.
char printBuffer[(size + 1) * 5 + 1];
size_t currentPos = 0;
for(size_t i = 0; i < size; i++) {
// To avoid buffer overflows.
if(sizeof(printBuffer) - currentPos <= 5) {
break;
}
currentPos += snprintf(printBuffer + currentPos, 3, "%d", data[i]);
if(i < size - 1) {
currentPos += sprintf(printBuffer + currentPos, ", ");
if(i > 0 and (i + 1) % maxCharPerLine == 0) {
currentPos += sprintf(printBuffer + currentPos, "\n");
}
}
}
printf("[%s]\n", printBuffer);
#endif #endif
} }
void arrayprinter::printBin(const uint8_t *data, size_t size) { void arrayprinter::printBin(const uint8_t *data, size_t size) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "\n" << std::flush;
for(size_t i = 0; i < size; i++) { for(size_t i = 0; i < size; i++) {
sif::info << "Byte " << i + 1 << ": 0b"<< sif::info << "Byte " << i + 1 << ": 0b" << std::bitset<8>(data[i]) << std::endl;
std::bitset<8>(data[i]) << ",\n" << std::flush;
} }
sif::info << "]" << std::endl;
#else #else
// how much memory to reserve for printout? for(size_t i = 0; i < size; i++) {
sif::printInfo("Byte %d: 0b" BYTE_TO_BINARY_PATTERN "\n", i + 1, BYTE_TO_BINARY(data[i]));
}
#endif #endif
} }

View File

@ -1,20 +1,25 @@
#ifndef FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ #ifndef FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_
#define FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ #define FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
enum class OutputType { enum class OutputType {
DEC, DEC,
HEX, HEX,
BIN BIN
}; };
namespace arrayprinter { namespace arrayprinter {
void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX, void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX,
bool printInfo = true, size_t maxCharPerLine = 12); bool printInfo = true, size_t maxCharPerLine = 10);
void printHex(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); void printHex(const uint8_t* data, size_t size, size_t maxCharPerLine = 10);
void printDec(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); void printDec(const uint8_t* data, size_t size, size_t maxCharPerLine = 10);
void printBin(const uint8_t* data, size_t size); void printBin(const uint8_t* data, size_t size);
} }
#endif /* FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ */ #endif /* FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ */

View File

@ -152,7 +152,7 @@ void PosixThread::createTask(void* (*fnc_)(void*), void* arg_) {
" the requested " << stackMb << " MB" << std::endl; " the requested " << stackMb << " MB" << std::endl;
#else #else
sif::printError("PosixThread::createTask: Insufficient memory for " sif::printError("PosixThread::createTask: Insufficient memory for "
"the requested %zu MB\n", stackMb); "the requested %lu MB\n", static_cast<unsigned long>(stackMb));
#endif #endif
} }
else if(errno == EINVAL) { else if(errno == EINVAL) {

View File

@ -167,7 +167,9 @@ std::string* ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
return &preamble; return &preamble;
} }
bool ServiceInterfaceBuffer::crAdditionEnabled() const {
return addCrToPreamble;
}
#ifdef UT699 #ifdef UT699
#include "../osal/rtems/Interrupt.h" #include "../osal/rtems/Interrupt.h"

View File

@ -70,6 +70,8 @@ private:
void putChars(char const* begin, char const* end); void putChars(char const* begin, char const* end);
std::string* getPreamble(size_t * preambleSize = nullptr); std::string* getPreamble(size_t * preambleSize = nullptr);
bool crAdditionEnabled() const;
}; };
#endif #endif

View File

@ -1,7 +1,24 @@
#ifndef FSFW_SERVICEINTERFACE_SERVICEINTERFACEPRINTER
#define FSFW_SERVICEINTERFACE_SERVICEINTERFACEPRINTER
#if FSFW_DISABLE_PRINTOUT == 0 #if FSFW_DISABLE_PRINTOUT == 0
#include <cstdio> #include <cstdio>
#endif #endif
//! https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format
//! Can be used to print out binary numbers in human-readable format.
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
namespace sif { namespace sif {
enum PrintLevel { enum PrintLevel {
@ -38,3 +55,4 @@ void printError(const char* fmt, ...);
} }
#endif /* FSFW_SERVICEINTERFACE_SERVICEINTERFACEPRINTER */

View File

@ -15,5 +15,9 @@ std::string* ServiceInterfaceStream::getPreamble() {
return streambuf.getPreamble(); return streambuf.getPreamble();
} }
bool ServiceInterfaceStream::crAdditionEnabled() const {
return streambuf.crAdditionEnabled();
}
#endif #endif

View File

@ -39,6 +39,8 @@ public:
*/ */
std::string* getPreamble(); std::string* getPreamble();
bool crAdditionEnabled() const;
protected: protected:
ServiceInterfaceBuffer streambuf; ServiceInterfaceBuffer streambuf;
}; };

View File

@ -1,2 +1,5 @@
add_subdirectory(internal) add_subdirectory(internal)
add_subdirectory(tests)
if(LINK_CATCH2)
add_subdirectory(tests)
endif()

View File

@ -5,3 +5,4 @@ target_sources(${TARGET_NAME} PRIVATE
add_subdirectory(osal) add_subdirectory(osal)
add_subdirectory(serialize) add_subdirectory(serialize)
add_subdirectory(globalfunctions)

View File

@ -5,6 +5,7 @@
#include "osal/IntTestSemaphore.h" #include "osal/IntTestSemaphore.h"
#include "osal/IntTestMutex.h" #include "osal/IntTestMutex.h"
#include "serialize/IntTestSerialization.h" #include "serialize/IntTestSerialization.h"
#include "globalfunctions/TestArrayPrinter.h"
#include <cstdlib> #include <cstdlib>
@ -12,17 +13,26 @@ InternalUnitTester::InternalUnitTester() {}
InternalUnitTester::~InternalUnitTester() {} InternalUnitTester::~InternalUnitTester() {}
ReturnValue_t InternalUnitTester::performTests() { ReturnValue_t InternalUnitTester::performTests(struct InternalUnitTester::TestConfig& testConfig) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "Running internal unit tests.." << std::endl; sif::info << "Running internal unit tests.." << std::endl;
#else
sif::printInfo("Running internal unit tests..\n");
#endif #endif
testserialize::test_serialization(); testserialize::test_serialization();
testmq::testMq(); testmq::testMq();
testsemaph::testBinSemaph(); testsemaph::testBinSemaph();
testsemaph::testCountingSemaph(); testsemaph::testCountingSemaph();
testmutex::testMutex(); testmutex::testMutex();
if(testConfig.testArrayPrinter) {
arrayprinter::testArrayPrinter();
}
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "Internal unit tests finished." << std::endl; sif::info << "Internal unit tests finished." << std::endl;
#else
sif::printInfo("Internal unit tests finished.\n");
#endif #endif
return RETURN_OK; return RETURN_OK;
} }

View File

@ -4,6 +4,7 @@
#include "UnittDefinitions.h" #include "UnittDefinitions.h"
#include "../../returnvalues/HasReturnvaluesIF.h" #include "../../returnvalues/HasReturnvaluesIF.h"
/** /**
* @brief Can be used for internal testing, for example for hardware specific * @brief Can be used for internal testing, for example for hardware specific
* tests which can not be run on a host-machine. * tests which can not be run on a host-machine.
@ -15,6 +16,10 @@
*/ */
class InternalUnitTester: public HasReturnvaluesIF { class InternalUnitTester: public HasReturnvaluesIF {
public: public:
struct TestConfig {
bool testArrayPrinter;
};
InternalUnitTester(); InternalUnitTester();
virtual~ InternalUnitTester(); virtual~ InternalUnitTester();
@ -22,7 +27,7 @@ public:
* Some function which calls all other tests * Some function which calls all other tests
* @return * @return
*/ */
virtual ReturnValue_t performTests(); virtual ReturnValue_t performTests(struct InternalUnitTester::TestConfig& testConfig);
}; };

View File

@ -5,7 +5,7 @@
sif::error << "Unit Tester error: Failed at test ID " sif::error << "Unit Tester error: Failed at test ID "
<< errorId << std::endl; << errorId << std::endl;
#else #else
sif::printError("Unit Tester error: Failed at test ID 0x%08x", errorId); sif::printError("Unit Tester error: Failed at test ID %s\n", errorId.c_str());
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }

View File

@ -0,0 +1,3 @@
target_sources(${TARGET_NAME} PRIVATE
TestArrayPrinter.cpp
)

View File

@ -0,0 +1,29 @@
#include "TestArrayPrinter.h"
void arrayprinter::testArrayPrinter() {
{
const std::array<uint8_t, 5> testDataSmall = {0x01, 0x02, 0x03, 0x04, 0x05};
arrayprinter::print(testDataSmall.data(), testDataSmall.size());
arrayprinter::print(testDataSmall.data(), testDataSmall.size(), OutputType::DEC);
arrayprinter::print(testDataSmall.data(), testDataSmall.size(), OutputType::BIN);
}
{
std::array<uint8_t, 16> testDataMed;
for(size_t idx = 0; idx < testDataMed.size(); idx ++) {
testDataMed[idx] = testDataMed.size() - idx;
}
arrayprinter::print(testDataMed.data(), testDataMed.size());
arrayprinter::print(testDataMed.data(), testDataMed.size(), OutputType::DEC, 8);
}
{
std::array<uint8_t, 32> testDataLarge;
for(size_t idx = 0; idx < testDataLarge.size(); idx ++) {
testDataLarge[idx] = idx;
}
arrayprinter::print(testDataLarge.data(), testDataLarge.size());
arrayprinter::print(testDataLarge.data(), testDataLarge.size(), OutputType::DEC);
}
}

View File

@ -0,0 +1,13 @@
#ifndef FSFW_UNITTEST_INTERNAL_GLOBALFUNCTIONS_TESTARRAYPRINTER_H_
#define FSFW_UNITTEST_INTERNAL_GLOBALFUNCTIONS_TESTARRAYPRINTER_H_
#include <fsfw/globalfunctions/arrayprinter.h>
#include <array>
namespace arrayprinter {
void testArrayPrinter();
}
#endif /* FSFW_UNITTEST_INTERNAL_GLOBALFUNCTIONS_TESTARRAYPRINTER_H_ */

View File

@ -1,3 +1,4 @@
CXXSRC += $(wildcard $(CURRENTPATH)/osal/*.cpp) CXXSRC += $(wildcard $(CURRENTPATH)/osal/*.cpp)
CXXSRC += $(wildcard $(CURRENTPATH)/serialize/*.cpp) CXXSRC += $(wildcard $(CURRENTPATH)/serialize/*.cpp)
CXXSRC += $(wildcard $(CURRENTPATH)/globalfunctions/*.cpp)
CXXSRC += $(wildcard $(CURRENTPATH)/*.cpp) CXXSRC += $(wildcard $(CURRENTPATH)/*.cpp)

View File

@ -3,8 +3,8 @@
#include <fsfw/ipc/MutexFactory.h> #include <fsfw/ipc/MutexFactory.h>
#include <fsfw/unittest/internal/UnittDefinitions.h> #include <fsfw/unittest/internal/UnittDefinitions.h>
#if defined(hosted) #if defined(WIN32) || defined(UNIX)
#include <fsfw/osal/hosted/Mutex.h> #include <fsfw/osal/host/Mutex.h>
#include <thread> #include <thread>
#include <future> #include <future>
#endif #endif
@ -19,10 +19,11 @@ void testmutex::testMutex() {
} }
// timed_mutex from the C++ library specifies undefined behaviour if // timed_mutex from the C++ library specifies undefined behaviour if
// the timed mutex is locked twice from the same thread. // the timed mutex is locked twice from the same thread.
#if defined(hosted) // TODO: we should pass a define like FSFW_OSAL_HOST to the build.
#if defined(WIN32) || defined(UNIX)
// This calls the function from // This calls the function from
// another thread and stores the returnvalue in a future. // another thread and stores the returnvalue in a future.
auto future = std::async(&MutexIF::lockMutex, mutex, 1); auto future = std::async(&MutexIF::lockMutex, mutex, MutexIF::TimeoutType::WAITING, 1);
result = future.get(); result = future.get();
#else #else
result = mutex->lockMutex(MutexIF::TimeoutType::WAITING, 1); result = mutex->lockMutex(MutexIF::TimeoutType::WAITING, 1);
@ -35,8 +36,12 @@ void testmutex::testMutex() {
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
unitt::put_error(id); unitt::put_error(id);
} }
result = mutex->unlockMutex();
// TODO: we should pass a define like FSFW_OSAL_HOST to the build.
#if !defined(WIN32) && !defined(UNIX)
result = mutex->unlockMutex();
if(result != MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX) { if(result != MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX) {
unitt::put_error(id); unitt::put_error(id);
} }
#endif
} }

View File

@ -0,0 +1,3 @@
target_sources(${TARGET_NAME} PRIVATE
TestArrayPrinter.cpp
)