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 <bitset>
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(printInfo) {
sif::info << "Printing data with size " << size << ": ";
}
sif::info << "[";
if(printInfo) {
sif::info << "Printing data with size " << size << ": " << std::endl;
}
#else
sif::printInfo("Printing data with size %zu: [", size);
#endif
if(type == OutputType::HEX) {
arrayprinter::printHex(data, size, maxCharPerLine);
}
else if (type == OutputType::DEC) {
arrayprinter::printDec(data, size, maxCharPerLine);
}
else if(type == OutputType::BIN) {
arrayprinter::printBin(data, size);
}
#if FSFW_NO_C99_IO == 1
sif::printInfo("Printing data with size %lu: \n", static_cast<unsigned long>(size));
#else
sif::printInfo("Printing data with size %zu: \n", size);
#endif /* FSFW_NO_C99_IO == 1 */
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
if(type == OutputType::HEX) {
arrayprinter::printHex(data, size, maxCharPerLine);
}
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,
size_t maxCharPerLine) {
size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << std::hex;
for(size_t i = 0; i < size; i++) {
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;
if(sif::info.crAdditionEnabled()) {
std::cout << "\r" << std::endl;
}
}
}
}
sif::info << std::dec;
sif::info << "]" << std::endl;
std::cout << "[" << std::hex;
for(size_t i = 0; i < size; i++) {
std::cout << "0x" << static_cast<int>(data[i]);
if(i < size - 1) {
std::cout << " , ";
if(i > 0 and (i + 1) % maxCharPerLine == 0) {
std::cout << std::endl;
}
}
}
std::cout << std::dec;
std::cout << "]" << std::endl;
#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
}
void arrayprinter::printDec(const uint8_t *data, size_t size,
size_t maxCharPerLine) {
size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << std::dec;
for(size_t i = 0; i < size; i++) {
sif::info << static_cast<int>(data[i]);
if(i < size - 1){
sif::info << " , ";
if(i > 0 and i % maxCharPerLine == 0) {
sif::info << std::endl;
}
}
}
sif::info << "]" << std::endl;
if(sif::info.crAdditionEnabled()) {
std::cout << "\r" << std::endl;
}
std::cout << "[" << std::dec;
for(size_t i = 0; i < size; i++) {
std::cout << static_cast<int>(data[i]);
if(i < size - 1){
std::cout << " , ";
if(i > 0 and (i + 1) % maxCharPerLine == 0) {
std::cout << std::endl;
}
}
}
std::cout << "]" << std::endl;
#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
}
void arrayprinter::printBin(const uint8_t *data, size_t size) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "\n" << std::flush;
for(size_t i = 0; i < size; i++) {
sif::info << "Byte " << i + 1 << ": 0b"<<
std::bitset<8>(data[i]) << ",\n" << std::flush;
sif::info << "Byte " << i + 1 << ": 0b" << std::bitset<8>(data[i]) << std::endl;
}
sif::info << "]" << std::endl;
#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
}

View File

@ -1,20 +1,25 @@
#ifndef FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_
#define FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_
#include <cstdint>
#include <cstddef>
enum class OutputType {
DEC,
HEX,
BIN
DEC,
HEX,
BIN
};
namespace arrayprinter {
void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX,
bool printInfo = true, size_t maxCharPerLine = 12);
void printHex(const uint8_t* data, size_t size, size_t maxCharPerLine = 12);
void printDec(const uint8_t* data, size_t size, size_t maxCharPerLine = 12);
bool printInfo = true, size_t maxCharPerLine = 10);
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 = 10);
void printBin(const uint8_t* data, size_t size);
}
#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;
#else
sif::printError("PosixThread::createTask: Insufficient memory for "
"the requested %zu MB\n", stackMb);
"the requested %lu MB\n", static_cast<unsigned long>(stackMb));
#endif
}
else if(errno == EINVAL) {

View File

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

View File

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

View File

@ -1,7 +1,24 @@
#ifndef FSFW_SERVICEINTERFACE_SERVICEINTERFACEPRINTER
#define FSFW_SERVICEINTERFACE_SERVICEINTERFACEPRINTER
#if FSFW_DISABLE_PRINTOUT == 0
#include <cstdio>
#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 {
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();
}
bool ServiceInterfaceStream::crAdditionEnabled() const {
return streambuf.crAdditionEnabled();
}
#endif

View File

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

View File

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

View File

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

View File

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

View File

@ -4,6 +4,7 @@
#include "UnittDefinitions.h"
#include "../../returnvalues/HasReturnvaluesIF.h"
/**
* @brief Can be used for internal testing, for example for hardware specific
* tests which can not be run on a host-machine.
@ -15,6 +16,10 @@
*/
class InternalUnitTester: public HasReturnvaluesIF {
public:
struct TestConfig {
bool testArrayPrinter;
};
InternalUnitTester();
virtual~ InternalUnitTester();
@ -22,7 +27,7 @@ public:
* Some function which calls all other tests
* @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 "
<< errorId << std::endl;
#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 */
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)/serialize/*.cpp)
CXXSRC += $(wildcard $(CURRENTPATH)/globalfunctions/*.cpp)
CXXSRC += $(wildcard $(CURRENTPATH)/*.cpp)

View File

@ -3,8 +3,8 @@
#include <fsfw/ipc/MutexFactory.h>
#include <fsfw/unittest/internal/UnittDefinitions.h>
#if defined(hosted)
#include <fsfw/osal/hosted/Mutex.h>
#if defined(WIN32) || defined(UNIX)
#include <fsfw/osal/host/Mutex.h>
#include <thread>
#include <future>
#endif
@ -19,10 +19,11 @@ void testmutex::testMutex() {
}
// timed_mutex from the C++ library specifies undefined behaviour if
// 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
// 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();
#else
result = mutex->lockMutex(MutexIF::TimeoutType::WAITING, 1);
@ -35,8 +36,12 @@ void testmutex::testMutex() {
if(result != HasReturnvaluesIF::RETURN_OK) {
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) {
unitt::put_error(id);
}
#endif
}

View File

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