Merge branch 'mueller/reboot-file-handling' into mueller/master
Some checks failed
EIVE/eive-obsw/pipeline/head There was a failure building this commit
Some checks failed
EIVE/eive-obsw/pipeline/head There was a failure building this commit
This commit is contained in:
@ -2,6 +2,5 @@
|
||||
#include "fsfw_tests/unit/CatchRunner.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
// fsfwtest::customMain(argc, argv);
|
||||
// fsfwtest::customMain(argc, argv);
|
||||
}
|
||||
|
||||
|
1
unittest/rebootLogic/.gitignore
vendored
1
unittest/rebootLogic/.gitignore
vendored
@ -1 +1,2 @@
|
||||
/build
|
||||
c_cpp_properties.json
|
||||
|
51
unittest/rebootLogic/.vscode/settings.json
vendored
Normal file
51
unittest/rebootLogic/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
{
|
||||
"editor.tabSize": 2,
|
||||
"files.associations": {
|
||||
"iosfwd": "cpp",
|
||||
"array": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"chrono": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"codecvt": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"fstream": "cpp",
|
||||
"functional": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"numbers": "cpp",
|
||||
"optional": "cpp",
|
||||
"ostream": "cpp",
|
||||
"ratio": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"string": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"tuple": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"utility": "cpp",
|
||||
"variant": "cpp",
|
||||
"filesystem": "cpp",
|
||||
"queue": "cpp"
|
||||
}
|
||||
}
|
@ -1,10 +1,28 @@
|
||||
cmake_minimum_required(VERSION 3.0.0)
|
||||
cmake_minimum_required(VERSION 3.13.0)
|
||||
project(reboot-logic VERSION 0.1.0)
|
||||
|
||||
include(CTest)
|
||||
find_package(Catch2 3)
|
||||
if(NOT Catch2_FOUND)
|
||||
include(FetchContent)
|
||||
|
||||
FetchContent_Declare(
|
||||
Catch2
|
||||
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
||||
GIT_TAG v3.0.0-preview4
|
||||
)
|
||||
|
||||
FetchContent_MakeAvailable(Catch2)
|
||||
#fixes regression -preview4, to be confirmed in later releases
|
||||
set_target_properties(Catch2 PROPERTIES DEBUG_POSTFIX "")
|
||||
endif()
|
||||
|
||||
enable_testing()
|
||||
|
||||
add_executable(reboot-logic main.cpp)
|
||||
add_executable(reboot-logic)
|
||||
|
||||
add_subdirectory(src)
|
||||
target_link_libraries(reboot-logic PRIVATE Catch2::Catch2WithMain)
|
||||
|
||||
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
|
||||
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
|
||||
|
16
unittest/rebootLogic/README.md
Normal file
16
unittest/rebootLogic/README.md
Normal file
@ -0,0 +1,16 @@
|
||||
Reboot Logic Unittest
|
||||
=======
|
||||
|
||||
These tests were written with Catch2 and VS code.
|
||||
Install VS code with the C++ and CMake plugins and open this folder with VS code.
|
||||
You should be able to run the tests directly.
|
||||
|
||||
It is also recommended to [install Catch2](https://github.com/catchorg/Catch2/blob/devel/docs/cmake-integration.md):
|
||||
|
||||
```sh
|
||||
git clone https://github.com/catchorg/Catch2.git
|
||||
cd Catch2
|
||||
git checkout v3.0.0-preview4
|
||||
cmake -Bbuild -H. -DBUILD_TESTING=OFF
|
||||
sudo cmake --build build/ --target install
|
||||
```
|
@ -1,5 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
int main(int, char**) {
|
||||
std::cout << "Hello, world!\n";
|
||||
}
|
14
unittest/rebootLogic/src/CMakeLists.txt
Normal file
14
unittest/rebootLogic/src/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
target_sources(reboot-logic PRIVATE
|
||||
main.cpp
|
||||
CoreController.cpp
|
||||
SdCardManager.cpp
|
||||
event.cpp
|
||||
libxiphos.cpp
|
||||
print.c
|
||||
)
|
||||
|
||||
target_include_directories(reboot-logic PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
add_subdirectory(fsfw)
|
529
unittest/rebootLogic/src/CoreController.cpp
Normal file
529
unittest/rebootLogic/src/CoreController.cpp
Normal file
@ -0,0 +1,529 @@
|
||||
#include "CoreController.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "HasActionsIF.h"
|
||||
#include "SdCardManager.h"
|
||||
#include "conf.h"
|
||||
#include "event.h"
|
||||
#include "fsfw/serviceinterface/ServiceInterface.h"
|
||||
#include "libxiphos.h"
|
||||
|
||||
xsc::Chip CoreController::CURRENT_CHIP = xsc::Chip::NO_CHIP;
|
||||
xsc::Copy CoreController::CURRENT_COPY = xsc::Copy::NO_COPY;
|
||||
|
||||
CoreController::CoreController() {
|
||||
sdcMan = new SdCardManager();
|
||||
setCurrentBootCopy(xsc::CHIP_0, xsc::COPY_0);
|
||||
}
|
||||
|
||||
void CoreController::performRebootFileHandling(bool recreateFile) {
|
||||
using namespace std;
|
||||
std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE;
|
||||
if (not std::filesystem::exists(path) or recreateFile) {
|
||||
#if OBSW_VERBOSE_LEVEL >= 1
|
||||
sif::info << "CoreController::performRebootFileHandling: Recreating reboot file" << std::endl;
|
||||
#endif
|
||||
rebootFile.enabled = true;
|
||||
rebootFile.img00Cnt = 0;
|
||||
rebootFile.img01Cnt = 0;
|
||||
rebootFile.img10Cnt = 0;
|
||||
rebootFile.img11Cnt = 0;
|
||||
rebootFile.lastChip = xsc::Chip::CHIP_0;
|
||||
rebootFile.lastCopy = xsc::Copy::COPY_0;
|
||||
rebootFile.img00Lock = false;
|
||||
rebootFile.img01Lock = false;
|
||||
rebootFile.img10Lock = false;
|
||||
rebootFile.img11Lock = false;
|
||||
rebootFile.mechanismNextChip = xsc::Chip::NO_CHIP;
|
||||
rebootFile.mechanismNextCopy = xsc::Copy::NO_COPY;
|
||||
rebootFile.bootFlag = false;
|
||||
rewriteRebootFile(rebootFile);
|
||||
} else {
|
||||
if (not parseRebootFile(path, rebootFile)) {
|
||||
performRebootFileHandling(true);
|
||||
}
|
||||
}
|
||||
|
||||
if (CURRENT_CHIP == xsc::CHIP_0) {
|
||||
if (CURRENT_COPY == xsc::COPY_0) {
|
||||
rebootFile.img00Cnt++;
|
||||
} else {
|
||||
rebootFile.img01Cnt++;
|
||||
}
|
||||
} else {
|
||||
if (CURRENT_COPY == xsc::COPY_0) {
|
||||
rebootFile.img10Cnt++;
|
||||
} else {
|
||||
rebootFile.img11Cnt++;
|
||||
}
|
||||
}
|
||||
|
||||
if (rebootFile.bootFlag) {
|
||||
// Trigger event to inform ground that a reboot was triggered
|
||||
uint32_t p1 = rebootFile.lastChip << 16 | rebootFile.lastCopy;
|
||||
uint32_t p2 = rebootFile.img00Cnt << 24 | rebootFile.img01Cnt << 16 | rebootFile.img10Cnt << 8 |
|
||||
rebootFile.img11Cnt;
|
||||
triggerEvent(REBOOT_MECHANISM_TRIGGERED, p1, p2);
|
||||
// Clear the boot flag
|
||||
rebootFile.bootFlag = false;
|
||||
}
|
||||
|
||||
if (rebootFile.mechanismNextChip != xsc::NO_CHIP and
|
||||
rebootFile.mechanismNextCopy != xsc::NO_COPY) {
|
||||
if (CURRENT_CHIP != rebootFile.mechanismNextChip or
|
||||
CURRENT_COPY != rebootFile.mechanismNextCopy) {
|
||||
std::string infoString = std::to_string(rebootFile.mechanismNextChip) + " " +
|
||||
std::to_string(rebootFile.mechanismNextCopy);
|
||||
sif::warning << "CoreController::performRebootFileHandling: Expected to be on image "
|
||||
<< infoString << " but currently on other image. Locking " << infoString
|
||||
<< std::endl;
|
||||
// Firmware or other component might be corrupt and we are on another image then the target
|
||||
// image specified by the mechanism. We can't really trust the target image anymore.
|
||||
// Lock it for now
|
||||
if (rebootFile.mechanismNextChip == xsc::CHIP_0) {
|
||||
if (rebootFile.mechanismNextCopy == xsc::COPY_0) {
|
||||
rebootFile.img00Lock = true;
|
||||
} else {
|
||||
rebootFile.img01Lock = true;
|
||||
}
|
||||
} else {
|
||||
if (rebootFile.mechanismNextCopy == xsc::COPY_0) {
|
||||
rebootFile.img10Lock = true;
|
||||
} else {
|
||||
rebootFile.img11Lock = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rebootFile.lastChip = CURRENT_CHIP;
|
||||
rebootFile.lastCopy = CURRENT_COPY;
|
||||
// Only reboot if the reboot functionality is enabled.
|
||||
// The handler will still increment the boot counts
|
||||
if (rebootFile.enabled and (*rebootFile.relevantBootCnt >= rebootFile.maxCount)) {
|
||||
// Reboot to other image
|
||||
bool doReboot = false;
|
||||
xsc::Chip tgtChip = xsc::NO_CHIP;
|
||||
xsc::Copy tgtCopy = xsc::NO_COPY;
|
||||
determineAndExecuteReboot(rebootFile, doReboot, tgtChip, tgtCopy);
|
||||
if (doReboot) {
|
||||
rebootFile.bootFlag = true;
|
||||
#if OBSW_VERBOSE_LEVEL >= 1
|
||||
sif::info << "Boot counter for image " << CURRENT_CHIP << " " << CURRENT_COPY
|
||||
<< " too high. Rebooting to " << tgtChip << " " << tgtCopy << std::endl;
|
||||
#endif
|
||||
rebootFile.mechanismNextChip = tgtChip;
|
||||
rebootFile.mechanismNextCopy = tgtCopy;
|
||||
rewriteRebootFile(rebootFile);
|
||||
xsc_boot_copy(static_cast<xsc_libnor_chip_t>(tgtChip),
|
||||
static_cast<xsc_libnor_copy_t>(tgtCopy));
|
||||
}
|
||||
} else {
|
||||
rebootFile.mechanismNextChip = xsc::NO_CHIP;
|
||||
rebootFile.mechanismNextCopy = xsc::NO_COPY;
|
||||
}
|
||||
rewriteRebootFile(rebootFile);
|
||||
}
|
||||
|
||||
void CoreController::determineAndExecuteReboot(RebootFile &rf, bool &needsReboot,
|
||||
xsc::Chip &tgtChip, xsc::Copy &tgtCopy) {
|
||||
tgtChip = xsc::CHIP_0;
|
||||
tgtCopy = xsc::COPY_0;
|
||||
needsReboot = false;
|
||||
if ((CURRENT_CHIP == xsc::CHIP_0) and (CURRENT_COPY == xsc::COPY_0) and
|
||||
(rf.img00Cnt >= rf.maxCount)) {
|
||||
needsReboot = true;
|
||||
if (rf.img01Cnt < rf.maxCount and not rf.img01Lock) {
|
||||
tgtCopy = xsc::COPY_1;
|
||||
return;
|
||||
}
|
||||
if (rf.img10Cnt < rf.maxCount and not rf.img10Lock) {
|
||||
tgtChip = xsc::CHIP_1;
|
||||
return;
|
||||
}
|
||||
if (rf.img11Cnt < rf.maxCount and not rf.img11Lock) {
|
||||
tgtChip = xsc::CHIP_1;
|
||||
tgtCopy = xsc::COPY_1;
|
||||
return;
|
||||
}
|
||||
// Can't really do much here. Stay on image
|
||||
sif::warning
|
||||
<< "All reboot counts too high or all fallback images locked, already on fallback image"
|
||||
<< std::endl;
|
||||
needsReboot = false;
|
||||
return;
|
||||
}
|
||||
if ((CURRENT_CHIP == xsc::CHIP_0) and (CURRENT_COPY == xsc::COPY_1) and
|
||||
(rf.img01Cnt >= rf.maxCount)) {
|
||||
needsReboot = true;
|
||||
if (rf.img00Cnt < rf.maxCount and not rf.img00Lock) {
|
||||
// Reboot on fallback image
|
||||
return;
|
||||
}
|
||||
if (rf.img10Cnt < rf.maxCount and not rf.img10Lock) {
|
||||
tgtChip = xsc::CHIP_1;
|
||||
return;
|
||||
}
|
||||
if (rf.img11Cnt < rf.maxCount and not rf.img11Lock) {
|
||||
tgtChip = xsc::CHIP_1;
|
||||
tgtCopy = xsc::COPY_1;
|
||||
}
|
||||
if (rf.img00Lock) {
|
||||
needsReboot = false;
|
||||
}
|
||||
// Reboot to fallback image
|
||||
}
|
||||
if ((CURRENT_CHIP == xsc::CHIP_1) and (CURRENT_COPY == xsc::COPY_0) and
|
||||
(rf.img10Cnt >= rf.maxCount)) {
|
||||
needsReboot = true;
|
||||
if (rf.img11Cnt < rf.maxCount and not rf.img11Lock) {
|
||||
tgtChip = xsc::CHIP_1;
|
||||
tgtCopy = xsc::COPY_1;
|
||||
return;
|
||||
}
|
||||
if (rf.img00Cnt < rf.maxCount and not rf.img00Lock) {
|
||||
return;
|
||||
}
|
||||
if (rf.img01Cnt < rf.maxCount and not rf.img01Lock) {
|
||||
tgtCopy = xsc::COPY_1;
|
||||
return;
|
||||
}
|
||||
if (rf.img00Lock) {
|
||||
needsReboot = false;
|
||||
}
|
||||
// Reboot to fallback image
|
||||
}
|
||||
if ((CURRENT_CHIP == xsc::CHIP_1) and (CURRENT_COPY == xsc::COPY_1) and
|
||||
(rf.img11Cnt >= rf.maxCount)) {
|
||||
needsReboot = true;
|
||||
if (rf.img10Cnt < rf.maxCount and not rf.img10Lock) {
|
||||
tgtChip = xsc::CHIP_1;
|
||||
return;
|
||||
}
|
||||
if (rf.img00Cnt < rf.maxCount and not rf.img00Lock) {
|
||||
return;
|
||||
}
|
||||
if (rf.img01Cnt < rf.maxCount and not rf.img01Lock) {
|
||||
tgtCopy = xsc::COPY_1;
|
||||
return;
|
||||
}
|
||||
if (rf.img00Lock) {
|
||||
needsReboot = false;
|
||||
}
|
||||
// Reboot to fallback image
|
||||
}
|
||||
}
|
||||
|
||||
bool CoreController::parseRebootFile(std::string path, RebootFile &rf) {
|
||||
using namespace std;
|
||||
std::string selfMatch;
|
||||
if (CURRENT_CHIP == xsc::CHIP_0) {
|
||||
if (CURRENT_COPY == xsc::COPY_0) {
|
||||
selfMatch = "00";
|
||||
} else {
|
||||
selfMatch = "01";
|
||||
}
|
||||
} else {
|
||||
if (CURRENT_COPY == xsc::COPY_0) {
|
||||
selfMatch = "10";
|
||||
} else {
|
||||
selfMatch = "11";
|
||||
}
|
||||
}
|
||||
ifstream file(path);
|
||||
string word;
|
||||
string line;
|
||||
uint8_t lineIdx = 0;
|
||||
while (std::getline(file, line)) {
|
||||
istringstream iss(line);
|
||||
switch (lineIdx) {
|
||||
case 0: {
|
||||
iss >> word;
|
||||
if (word.find("on:") == string::npos) {
|
||||
// invalid file
|
||||
return false;
|
||||
}
|
||||
iss >> rf.enabled;
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
iss >> word;
|
||||
if (word.find("maxcnt:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> rf.maxCount;
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
iss >> word;
|
||||
if (word.find("img00:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> rf.img00Cnt;
|
||||
if (word.find(selfMatch) != string::npos) {
|
||||
rf.relevantBootCnt = &rf.img00Cnt;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
iss >> word;
|
||||
if (word.find("img01:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> rf.img01Cnt;
|
||||
if (word.find(selfMatch) != string::npos) {
|
||||
rf.relevantBootCnt = &rf.img01Cnt;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
iss >> word;
|
||||
if (word.find("img10:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> rf.img10Cnt;
|
||||
if (word.find(selfMatch) != string::npos) {
|
||||
rf.relevantBootCnt = &rf.img10Cnt;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 5: {
|
||||
iss >> word;
|
||||
if (word.find("img11:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> rf.img11Cnt;
|
||||
if (word.find(selfMatch) != string::npos) {
|
||||
rf.relevantBootCnt = &rf.img11Cnt;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 6: {
|
||||
iss >> word;
|
||||
if (word.find("img00lock:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> rf.img00Lock;
|
||||
break;
|
||||
}
|
||||
case 7: {
|
||||
iss >> word;
|
||||
if (word.find("img01lock:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> rf.img01Lock;
|
||||
break;
|
||||
}
|
||||
case 8: {
|
||||
iss >> word;
|
||||
if (word.find("img10lock:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> rf.img10Lock;
|
||||
break;
|
||||
}
|
||||
case 9: {
|
||||
iss >> word;
|
||||
if (word.find("img11lock:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> rf.img11Lock;
|
||||
break;
|
||||
}
|
||||
case 10: {
|
||||
iss >> word;
|
||||
if (word.find("bootflag:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> rf.bootFlag;
|
||||
break;
|
||||
}
|
||||
case 11: {
|
||||
iss >> word;
|
||||
int copyRaw = 0;
|
||||
int chipRaw = 0;
|
||||
if (word.find("last:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> chipRaw;
|
||||
if (iss.fail()) {
|
||||
return false;
|
||||
}
|
||||
iss >> copyRaw;
|
||||
if (iss.fail()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (chipRaw > 1 or copyRaw > 1) {
|
||||
return false;
|
||||
}
|
||||
rf.lastChip = static_cast<xsc::Chip>(chipRaw);
|
||||
rf.lastCopy = static_cast<xsc::Copy>(copyRaw);
|
||||
break;
|
||||
}
|
||||
case 12: {
|
||||
iss >> word;
|
||||
int copyRaw = 0;
|
||||
int chipRaw = 0;
|
||||
if (word.find("next:") == string::npos) {
|
||||
return false;
|
||||
}
|
||||
iss >> chipRaw;
|
||||
if (iss.fail()) {
|
||||
return false;
|
||||
}
|
||||
iss >> copyRaw;
|
||||
if (iss.fail()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (chipRaw > 2 or copyRaw > 2) {
|
||||
return false;
|
||||
}
|
||||
rf.mechanismNextChip = static_cast<xsc::Chip>(chipRaw);
|
||||
rf.mechanismNextCopy = static_cast<xsc::Copy>(copyRaw);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (iss.fail()) {
|
||||
return false;
|
||||
}
|
||||
lineIdx++;
|
||||
}
|
||||
if (lineIdx < 12) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void CoreController::resetRebootCount(xsc::Chip tgtChip, xsc::Copy tgtCopy) {
|
||||
std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE;
|
||||
// Disable the reboot file mechanism
|
||||
parseRebootFile(path, rebootFile);
|
||||
if (tgtChip == xsc::ALL_CHIP and tgtCopy == xsc::ALL_COPY) {
|
||||
rebootFile.img00Cnt = 0;
|
||||
rebootFile.img01Cnt = 0;
|
||||
rebootFile.img10Cnt = 0;
|
||||
rebootFile.img11Cnt = 0;
|
||||
} else {
|
||||
if (tgtChip == xsc::CHIP_0) {
|
||||
if (tgtCopy == xsc::COPY_0) {
|
||||
rebootFile.img00Cnt = 0;
|
||||
} else {
|
||||
rebootFile.img01Cnt = 0;
|
||||
}
|
||||
} else {
|
||||
if (tgtCopy == xsc::COPY_0) {
|
||||
rebootFile.img10Cnt = 0;
|
||||
} else {
|
||||
rebootFile.img11Cnt = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
rewriteRebootFile(rebootFile);
|
||||
}
|
||||
|
||||
void CoreController::rewriteRebootFile(RebootFile file) {
|
||||
std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE;
|
||||
std::ofstream rebootFile(path);
|
||||
if (rebootFile.is_open()) {
|
||||
// Initiate reboot file first. Reboot handling will be on on initialization
|
||||
rebootFile << "on: " << file.enabled << "\nmaxcnt: " << file.maxCount
|
||||
<< "\nimg00: " << file.img00Cnt << "\nimg01: " << file.img01Cnt
|
||||
<< "\nimg10: " << file.img10Cnt << "\nimg11: " << file.img11Cnt
|
||||
<< "\nimg00lock: " << file.img00Lock << "\nimg01lock: " << file.img01Lock
|
||||
<< "\nimg10lock: " << file.img10Lock << "\nimg11lock: " << file.img11Lock
|
||||
<< "\nbootflag: " << file.bootFlag << "\nlast: " << static_cast<int>(file.lastChip)
|
||||
<< " " << static_cast<int>(file.lastCopy)
|
||||
<< "\nnext: " << static_cast<int>(file.mechanismNextChip) << " "
|
||||
<< static_cast<int>(file.mechanismNextCopy) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t CoreController::executeAction(ActionId_t actionId, MessageQueueId_t commandedBy,
|
||||
const uint8_t *data, size_t size) {
|
||||
switch (actionId) {
|
||||
case (SWITCH_REBOOT_FILE_HANDLING): {
|
||||
if (size < 1) {
|
||||
return HasActionsIF::INVALID_PARAMETERS;
|
||||
}
|
||||
std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE;
|
||||
// Disable the reboot file mechanism
|
||||
parseRebootFile(path, rebootFile);
|
||||
if (data[0] == 0) {
|
||||
rebootFile.enabled = false;
|
||||
rewriteRebootFile(rebootFile);
|
||||
} else if (data[0] == 1) {
|
||||
rebootFile.enabled = true;
|
||||
rewriteRebootFile(rebootFile);
|
||||
} else {
|
||||
return HasActionsIF::INVALID_PARAMETERS;
|
||||
}
|
||||
return HasActionsIF::EXECUTION_FINISHED;
|
||||
}
|
||||
case (RESET_REBOOT_COUNTERS): {
|
||||
if (size == 0) {
|
||||
resetRebootCount(xsc::ALL_CHIP, xsc::ALL_COPY);
|
||||
} else if (size == 2) {
|
||||
if (data[0] > 1 or data[1] > 1) {
|
||||
return HasActionsIF::INVALID_PARAMETERS;
|
||||
}
|
||||
resetRebootCount(static_cast<xsc::Chip>(data[0]), static_cast<xsc::Copy>(data[1]));
|
||||
}
|
||||
return HasActionsIF::EXECUTION_FINISHED;
|
||||
}
|
||||
case (SWITCH_IMG_LOCK): {
|
||||
if (size != 3) {
|
||||
return HasActionsIF::INVALID_PARAMETERS;
|
||||
}
|
||||
if (data[1] > 1 or data[2] > 1) {
|
||||
return HasActionsIF::INVALID_PARAMETERS;
|
||||
}
|
||||
setRebootMechanismLock(data[0], static_cast<xsc::Chip>(data[1]),
|
||||
static_cast<xsc::Copy>(data[2]));
|
||||
return HasActionsIF::EXECUTION_FINISHED;
|
||||
}
|
||||
case (SET_MAX_REBOOT_CNT): {
|
||||
if (size < 1) {
|
||||
return HasActionsIF::INVALID_PARAMETERS;
|
||||
}
|
||||
std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE;
|
||||
// Disable the reboot file mechanism
|
||||
parseRebootFile(path, rebootFile);
|
||||
rebootFile.maxCount = data[0];
|
||||
rewriteRebootFile(rebootFile);
|
||||
return HasActionsIF::EXECUTION_FINISHED;
|
||||
}
|
||||
default: {
|
||||
return HasActionsIF::INVALID_ACTION_ID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CoreController::setRebootMechanismLock(bool lock, xsc::Chip tgtChip, xsc::Copy tgtCopy) {
|
||||
std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE;
|
||||
// Disable the reboot file mechanism
|
||||
parseRebootFile(path, rebootFile);
|
||||
if (tgtChip == xsc::CHIP_0) {
|
||||
if (tgtCopy == xsc::COPY_0) {
|
||||
rebootFile.img00Lock = lock;
|
||||
} else {
|
||||
rebootFile.img01Lock = lock;
|
||||
}
|
||||
} else {
|
||||
if (tgtCopy == xsc::COPY_0) {
|
||||
rebootFile.img10Lock = lock;
|
||||
} else {
|
||||
rebootFile.img11Lock = lock;
|
||||
}
|
||||
}
|
||||
rewriteRebootFile(rebootFile);
|
||||
}
|
||||
|
||||
void CoreController::setCurrentBootCopy(xsc::Chip chip, xsc::Copy copy) {
|
||||
CURRENT_CHIP = chip;
|
||||
CURRENT_COPY = copy;
|
||||
}
|
79
unittest/rebootLogic/src/CoreController.h
Normal file
79
unittest/rebootLogic/src/CoreController.h
Normal file
@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "SdCardManager.h"
|
||||
#include "definitions.h"
|
||||
|
||||
namespace xsc {
|
||||
|
||||
enum Chip : int { CHIP_0, CHIP_1, NO_CHIP, SELF_CHIP, ALL_CHIP };
|
||||
enum Copy : int { COPY_0, COPY_1, NO_COPY, SELF_COPY, ALL_COPY };
|
||||
|
||||
} // namespace xsc
|
||||
|
||||
struct RebootFile {
|
||||
static constexpr uint8_t DEFAULT_MAX_BOOT_CNT = 10;
|
||||
|
||||
bool enabled = true;
|
||||
size_t maxCount = DEFAULT_MAX_BOOT_CNT;
|
||||
uint32_t img00Cnt = 0;
|
||||
uint32_t img01Cnt = 0;
|
||||
uint32_t img10Cnt = 0;
|
||||
uint32_t img11Cnt = 0;
|
||||
bool img00Lock = false;
|
||||
bool img01Lock = false;
|
||||
bool img10Lock = false;
|
||||
bool img11Lock = false;
|
||||
uint32_t* relevantBootCnt = &img00Cnt;
|
||||
bool bootFlag = false;
|
||||
xsc::Chip lastChip = xsc::Chip::CHIP_0;
|
||||
xsc::Copy lastCopy = xsc::Copy::COPY_0;
|
||||
xsc::Chip mechanismNextChip = xsc::Chip::NO_CHIP;
|
||||
xsc::Copy mechanismNextCopy = xsc::Copy::NO_COPY;
|
||||
};
|
||||
|
||||
class SdCardManager;
|
||||
|
||||
class CoreController {
|
||||
public:
|
||||
static constexpr char REBOOT_FILE[] = "/conf/reboot.txt";
|
||||
//! [EXPORT] : [COMMENT] The reboot mechanism was triggered.
|
||||
//! P1: First 16 bits: Last Chip, Last 16 bits: Last Copy,
|
||||
//! P2: Each byte is the respective reboot count for the slots
|
||||
static constexpr Event REBOOT_MECHANISM_TRIGGERED = 1;
|
||||
static xsc::Chip CURRENT_CHIP;
|
||||
static xsc::Copy CURRENT_COPY;
|
||||
|
||||
static constexpr ActionId_t SWITCH_REBOOT_FILE_HANDLING = 5;
|
||||
static constexpr ActionId_t RESET_REBOOT_COUNTERS = 6;
|
||||
static constexpr ActionId_t SWITCH_IMG_LOCK = 7;
|
||||
static constexpr ActionId_t SET_MAX_REBOOT_CNT = 8;
|
||||
|
||||
CoreController();
|
||||
|
||||
static void setCurrentBootCopy(xsc::Chip chip, xsc::Copy copy);
|
||||
ReturnValue_t executeAction(ActionId_t actionId, MessageQueueId_t commandedBy,
|
||||
const uint8_t* data, size_t size);
|
||||
void performRebootFileHandling(bool recreateFile);
|
||||
void determineAndExecuteReboot(RebootFile& rf, bool& needsReboot, xsc::Chip& tgtChip,
|
||||
xsc::Copy& tgtCopy);
|
||||
void setRebootMechanismLock(bool lock, xsc::Chip tgtChip, xsc::Copy tgtCopy);
|
||||
void resetRebootCount(xsc::Chip tgtChip, xsc::Copy tgtCopy);
|
||||
bool parseRebootFile(std::string path, RebootFile& file);
|
||||
void rewriteRebootFile(RebootFile file);
|
||||
|
||||
private:
|
||||
struct SdInfo {
|
||||
sd::SdCard pref = sd::SdCard::NONE;
|
||||
sd::SdState prefState = sd::SdState::OFF;
|
||||
sd::SdCard other = sd::SdCard::NONE;
|
||||
sd::SdState otherState = sd::SdState::OFF;
|
||||
} sdInfo;
|
||||
|
||||
SdCardManager* sdcMan = nullptr;
|
||||
RebootFile rebootFile = {};
|
||||
bool doPerformRebootFileHandling = true;
|
||||
};
|
9
unittest/rebootLogic/src/HasActionsIF.h
Normal file
9
unittest/rebootLogic/src/HasActionsIF.h
Normal file
@ -0,0 +1,9 @@
|
||||
#include "definitions.h"
|
||||
|
||||
class HasActionsIF {
|
||||
public:
|
||||
static const ReturnValue_t IS_BUSY = 1;
|
||||
static const ReturnValue_t INVALID_PARAMETERS = 2;
|
||||
static const ReturnValue_t EXECUTION_FINISHED = 3;
|
||||
static const ReturnValue_t INVALID_ACTION_ID = 4;
|
||||
};
|
5
unittest/rebootLogic/src/SdCardManager.cpp
Normal file
5
unittest/rebootLogic/src/SdCardManager.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
#include "SdCardManager.h"
|
||||
|
||||
std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) { return "/tmp"; }
|
||||
|
||||
bool SdCardManager::isSdCardMounted(sd::SdCard sdCard) { return true; }
|
23
unittest/rebootLogic/src/SdCardManager.h
Normal file
23
unittest/rebootLogic/src/SdCardManager.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace sd {
|
||||
|
||||
enum SdState : uint8_t {
|
||||
OFF = 0,
|
||||
ON = 1,
|
||||
// A mounted SD card is on as well
|
||||
MOUNTED = 2
|
||||
};
|
||||
|
||||
enum SdCard : uint8_t { SLOT_0 = 0, SLOT_1 = 1, BOTH, NONE };
|
||||
|
||||
} // namespace sd
|
||||
|
||||
class SdCardManager {
|
||||
public:
|
||||
std::string getCurrentMountPrefix(sd::SdCard prefSdCard);
|
||||
bool isSdCardMounted(sd::SdCard sdCard);
|
||||
};
|
1
unittest/rebootLogic/src/conf.h
Normal file
1
unittest/rebootLogic/src/conf.h
Normal file
@ -0,0 +1 @@
|
||||
#define OBSW_VERBOSE_LEVEL 1
|
6
unittest/rebootLogic/src/definitions.h
Normal file
6
unittest/rebootLogic/src/definitions.h
Normal file
@ -0,0 +1,6 @@
|
||||
#include <cstdint>
|
||||
|
||||
using Event = uint32_t;
|
||||
using ActionId_t = uint32_t;
|
||||
using MessageQueueId_t = uint32_t;
|
||||
using ReturnValue_t = uint16_t;
|
21
unittest/rebootLogic/src/event.cpp
Normal file
21
unittest/rebootLogic/src/event.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include "event.h"
|
||||
|
||||
#include <queue>
|
||||
|
||||
std::queue<EventInfo> EVENT_QUEUE = {};
|
||||
|
||||
void triggerEvent(Event event, uint32_t p1, uint32_t p2) {
|
||||
EventInfo info = {};
|
||||
info.event = event;
|
||||
info.p1 = p1;
|
||||
info.p2 = p2;
|
||||
EVENT_QUEUE.push(info);
|
||||
}
|
||||
|
||||
void eventWasCalled(EventInfo& eventInfo, uint32_t& numEvents) {
|
||||
numEvents = EVENT_QUEUE.size();
|
||||
if (not EVENT_QUEUE.empty()) {
|
||||
eventInfo = std::move(EVENT_QUEUE.back());
|
||||
EVENT_QUEUE.pop();
|
||||
}
|
||||
}
|
41
unittest/rebootLogic/src/event.h
Normal file
41
unittest/rebootLogic/src/event.h
Normal file
@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
#include "definitions.h"
|
||||
|
||||
struct EventInfo {
|
||||
// That was just for testing, follow rule of 0
|
||||
/*
|
||||
EventInfo () {}
|
||||
|
||||
EventInfo (const EventInfo& other): event(other.event), p1(other.p1), p2(other.p2) {
|
||||
std::cout << "Event info copy ctor called" << std::endl;
|
||||
}
|
||||
|
||||
EventInfo &operator= (const EventInfo& other) {
|
||||
std::cout << "Event info assignment ctor called" << std::endl;
|
||||
this->event = other.event;
|
||||
this->p1 = other.p1;
|
||||
this->p2 = other.p2;
|
||||
return *this;
|
||||
}
|
||||
|
||||
EventInfo &operator= (EventInfo&& other) {
|
||||
std::cout << "Event info move ctor called" << std::endl;
|
||||
this->event = other.event;
|
||||
this->p1 = other.p1;
|
||||
this->p2 = other.p2;
|
||||
return *this;
|
||||
}
|
||||
*/
|
||||
|
||||
uint32_t event = 0;
|
||||
uint32_t p1 = 0;
|
||||
uint32_t p2 = 0;
|
||||
};
|
||||
|
||||
void triggerEvent(Event event, uint32_t p1, uint32_t p2);
|
||||
|
||||
void eventWasCalled(EventInfo& eventInfo, uint32_t& numEvents);
|
1
unittest/rebootLogic/src/fsfw/CMakeLists.txt
Normal file
1
unittest/rebootLogic/src/fsfw/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
add_subdirectory(serviceinterface)
|
9
unittest/rebootLogic/src/fsfw/FSFW.h
Normal file
9
unittest/rebootLogic/src/fsfw/FSFW.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#define FSFW_CPP_OSTREAM_ENABLED 1
|
||||
|
||||
namespace fsfwconfig {
|
||||
static constexpr size_t FSFW_PRINT_BUFFER_SIZE = 256;
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
target_sources(reboot-logic PRIVATE
|
||||
ServiceInterfaceStream.cpp
|
||||
ServiceInterfaceBuffer.cpp
|
||||
)
|
@ -0,0 +1,13 @@
|
||||
#ifndef FSFW_SERVICEINTERFACE_SERVICEINTERFACE_H_
|
||||
#define FSFW_SERVICEINTERFACE_SERVICEINTERFACE_H_
|
||||
|
||||
#include "fsfw/FSFW.h"
|
||||
#include "serviceInterfaceDefintions.h"
|
||||
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
#include "ServiceInterfaceStream.h"
|
||||
#else
|
||||
#include "ServiceInterfacePrinter.h"
|
||||
#endif
|
||||
|
||||
#endif /* FSFW_SERVICEINTERFACE_SERVICEINTERFACE_H_ */
|
@ -0,0 +1,252 @@
|
||||
#include "ServiceInterfaceBuffer.h"
|
||||
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "fsfw/serviceinterface/serviceInterfaceDefintions.h"
|
||||
|
||||
#if defined(WIN32) && FSFW_COLORED_OUTPUT == 1
|
||||
#include "Windows.h"
|
||||
#endif
|
||||
|
||||
// to be implemented by bsp
|
||||
extern "C" void printChar(const char*, bool errStream);
|
||||
|
||||
#ifndef UT699
|
||||
|
||||
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage, bool addCrToPreamble,
|
||||
bool buffered, bool errStream, uint16_t port)
|
||||
: isActive(true),
|
||||
logMessage(setMessage),
|
||||
addCrToPreamble(addCrToPreamble),
|
||||
buffered(buffered),
|
||||
errStream(errStream) {
|
||||
if (buffered) {
|
||||
// Set pointers if the stream is buffered.
|
||||
setp(buf, buf + BUF_SIZE);
|
||||
}
|
||||
|
||||
#if FSFW_COLORED_OUTPUT == 1
|
||||
if (setMessage.find("DEBUG") != std::string::npos) {
|
||||
colorPrefix = sif::ANSI_COLOR_CYAN;
|
||||
} else if (setMessage.find("INFO") != std::string::npos) {
|
||||
colorPrefix = sif::ANSI_COLOR_GREEN;
|
||||
} else if (setMessage.find("WARNING") != std::string::npos) {
|
||||
colorPrefix = sif::ANSI_COLOR_MAGENTA;
|
||||
} else if (setMessage.find("ERROR") != std::string::npos) {
|
||||
colorPrefix = sif::ANSI_COLOR_RED;
|
||||
} else {
|
||||
colorPrefix = sif::ANSI_COLOR_RESET;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
DWORD dwMode = 0;
|
||||
GetConsoleMode(hOut, &dwMode);
|
||||
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
||||
SetConsoleMode(hOut, dwMode);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
preamble.reserve(MAX_PREAMBLE_SIZE);
|
||||
preamble.resize(MAX_PREAMBLE_SIZE);
|
||||
}
|
||||
|
||||
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
|
||||
char array[BUF_SIZE];
|
||||
uint32_t length = end - begin;
|
||||
if (length > sizeof(array)) {
|
||||
length = sizeof(array);
|
||||
}
|
||||
memcpy(array, begin, length);
|
||||
|
||||
for (; begin != end; begin++) {
|
||||
if (errStream) {
|
||||
printChar(begin, true);
|
||||
} else {
|
||||
printChar(begin, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int ServiceInterfaceBuffer::overflow(int c) {
|
||||
if (not buffered and this->isActive) {
|
||||
if (c != Traits::eof()) {
|
||||
if (errStream) {
|
||||
printChar(reinterpret_cast<const char*>(&c), true);
|
||||
} else {
|
||||
printChar(reinterpret_cast<const char*>(&c), false);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// Handle output
|
||||
putChars(pbase(), pptr());
|
||||
if (c != Traits::eof()) {
|
||||
char c2 = c;
|
||||
// Handle the one character that didn't fit to buffer
|
||||
putChars(&c2, &c2 + 1);
|
||||
}
|
||||
// This tells that buffer is empty again
|
||||
setp(buf, buf + BUF_SIZE - 1);
|
||||
// I'm not sure about this return value!
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ServiceInterfaceBuffer::sync(void) {
|
||||
if (not this->isActive and not buffered) {
|
||||
if (not buffered) {
|
||||
setp(buf, buf + BUF_SIZE - 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (not buffered) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t preambleSize = 0;
|
||||
std::string* preamble = getPreamble(&preambleSize);
|
||||
// Write logMessage and time
|
||||
this->putChars(preamble->data(), preamble->data() + preambleSize);
|
||||
// Handle output
|
||||
this->putChars(pbase(), pptr());
|
||||
// This tells that buffer is empty again
|
||||
setp(buf, buf + BUF_SIZE - 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ServiceInterfaceBuffer::isBuffered() const { return buffered; }
|
||||
|
||||
std::string* ServiceInterfaceBuffer::getPreamble(size_t* preambleSize) {
|
||||
size_t currentSize = 0;
|
||||
char* parsePosition = &preamble[0];
|
||||
if (addCrToPreamble) {
|
||||
preamble[0] = '\r';
|
||||
currentSize += 1;
|
||||
parsePosition += 1;
|
||||
}
|
||||
|
||||
#if FSFW_COLORED_OUTPUT == 1
|
||||
currentSize += sprintf(parsePosition, "%s", colorPrefix.c_str());
|
||||
parsePosition += colorPrefix.size();
|
||||
#endif
|
||||
|
||||
int32_t charCount =
|
||||
sprintf(parsePosition, "%s%s | ", this->logMessage.c_str(), sif::ANSI_COLOR_RESET);
|
||||
if (charCount < 0) {
|
||||
printf("ServiceInterfaceBuffer: Failure parsing preamble\r\n");
|
||||
return &preamble;
|
||||
}
|
||||
if (charCount > MAX_PREAMBLE_SIZE) {
|
||||
printf(
|
||||
"ServiceInterfaceBuffer: Char count too large for maximum "
|
||||
"preamble size");
|
||||
return &preamble;
|
||||
}
|
||||
currentSize += charCount;
|
||||
if (preambleSize != nullptr) {
|
||||
*preambleSize = currentSize;
|
||||
}
|
||||
return &preamble;
|
||||
}
|
||||
|
||||
bool ServiceInterfaceBuffer::crAdditionEnabled() const { return addCrToPreamble; }
|
||||
|
||||
#if FSFW_COLORED_OUTPUT == 1
|
||||
void ServiceInterfaceBuffer::setAsciiColorPrefix(std::string colorPrefix) {
|
||||
this->colorPrefix = colorPrefix;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef UT699
|
||||
#include "../osal/rtems/Interrupt.h"
|
||||
|
||||
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message, uint16_t port) {
|
||||
this->log_message = set_message;
|
||||
this->isActive = true;
|
||||
setp(buf, buf + BUF_SIZE);
|
||||
}
|
||||
|
||||
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
|
||||
char array[BUF_SIZE];
|
||||
uint32_t length = end - begin;
|
||||
if (length > sizeof(array)) {
|
||||
length = sizeof(array);
|
||||
}
|
||||
memcpy(array, begin, length);
|
||||
|
||||
if (!Interrupt::isInterruptInProgress()) {
|
||||
std::cout << array;
|
||||
} else {
|
||||
// Uncomment the following line if you need ISR debug output.
|
||||
// printk(array);
|
||||
}
|
||||
}
|
||||
#endif // UT699
|
||||
|
||||
#ifdef ML505
|
||||
#include <bsp_flp/network/networkconfig.h>
|
||||
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message, uint16_t port)
|
||||
: isActive(true),
|
||||
log_message(set_message),
|
||||
udpSocket(0),
|
||||
remoteAddressLength(sizeof(remoteAddress)) {
|
||||
setp(buf, buf + BUF_SIZE);
|
||||
memset((uint8_t*)&remoteAddress, 0, sizeof(remoteAddress));
|
||||
remoteAddress.sin_family = AF_INET;
|
||||
remoteAddress.sin_port = htons(port);
|
||||
remoteAddress.sin_addr.s_addr = htonl(inet_addr("192.168.250.100"));
|
||||
}
|
||||
|
||||
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
|
||||
char array[BUF_SIZE];
|
||||
uint32_t length = end - begin;
|
||||
if (length > sizeof(array)) {
|
||||
length = sizeof(array);
|
||||
}
|
||||
memcpy(array, begin, length);
|
||||
|
||||
if (udpSocket <= 0) {
|
||||
initSocket();
|
||||
}
|
||||
|
||||
if (udpSocket > 0) {
|
||||
sendto(udpSocket, array, length, 0, (sockaddr*)&remoteAddress, sizeof(remoteAddress));
|
||||
}
|
||||
}
|
||||
|
||||
void ServiceInterfaceBuffer::initSocket() {
|
||||
sockaddr_in address;
|
||||
memset((uint8_t*)&address, 0, sizeof(address));
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = htons(0);
|
||||
address.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (socket < 0) {
|
||||
printf("Error opening socket!\n");
|
||||
return;
|
||||
}
|
||||
timeval timeout = {0, 20};
|
||||
if (setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
|
||||
printf("Error setting SO_RCVTIMEO socket options!\n");
|
||||
return;
|
||||
}
|
||||
if (setsockopt(udpSocket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0) {
|
||||
printf("Error setting SO_SNDTIMEO socket options!\n");
|
||||
return;
|
||||
}
|
||||
if (bind(udpSocket, (sockaddr*)&address, sizeof(address)) < 0) {
|
||||
printf("Error binding socket!\n");
|
||||
}
|
||||
}
|
||||
|
||||
#endif // ML505
|
||||
|
||||
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
@ -0,0 +1,159 @@
|
||||
#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
|
||||
#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
|
||||
|
||||
#include "fsfw/FSFW.h"
|
||||
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#ifndef UT699
|
||||
|
||||
/**
|
||||
* @brief This is the underlying stream buffer which implements the
|
||||
* streambuf class and overloads the overflow() and sync() methods
|
||||
* @details
|
||||
* This class is used to modify the output of the stream, for example by adding.
|
||||
* It also calls the char printing function which is implemented in the
|
||||
* board supply package (BSP).
|
||||
*/
|
||||
class ServiceInterfaceBuffer : public std::streambuf {
|
||||
friend class ServiceInterfaceStream;
|
||||
|
||||
public:
|
||||
static constexpr uint8_t MAX_PREAMBLE_SIZE = 40;
|
||||
|
||||
ServiceInterfaceBuffer(std::string setMessage, bool addCrToPreamble, bool buffered,
|
||||
bool errStream, uint16_t port);
|
||||
|
||||
protected:
|
||||
bool isActive;
|
||||
//! This is called when buffer becomes full. If
|
||||
//! buffer is not used, then this is called every
|
||||
//! time when characters are put to stream.
|
||||
int overflow(int c = Traits::eof()) override;
|
||||
|
||||
//! This function is called when stream is flushed,
|
||||
//! for example when std::endl is put to stream.
|
||||
int sync(void) override;
|
||||
|
||||
bool isBuffered() const;
|
||||
|
||||
private:
|
||||
//! For additional message information
|
||||
std::string logMessage;
|
||||
std::string preamble;
|
||||
|
||||
#if FSFW_COLORED_OUTPUT == 1
|
||||
std::string colorPrefix;
|
||||
void setAsciiColorPrefix(std::string colorPrefix);
|
||||
#endif
|
||||
|
||||
// For EOF detection
|
||||
typedef std::char_traits<char> Traits;
|
||||
|
||||
//! This is useful for some terminal programs which do not have
|
||||
//! implicit carriage return with newline characters.
|
||||
bool addCrToPreamble;
|
||||
|
||||
//! Specifies whether the stream operates in buffered or unbuffered mode.
|
||||
bool buffered;
|
||||
//! This specifies to print to stderr and work in unbuffered mode.
|
||||
bool errStream;
|
||||
|
||||
//! Needed for buffered mode.
|
||||
static size_t const BUF_SIZE = fsfwconfig::FSFW_PRINT_BUFFER_SIZE;
|
||||
char buf[BUF_SIZE];
|
||||
|
||||
//! In this function, the characters are parsed.
|
||||
void putChars(char const* begin, char const* end);
|
||||
|
||||
std::string* getPreamble(size_t* preambleSize = nullptr);
|
||||
|
||||
bool crAdditionEnabled() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef UT699
|
||||
class ServiceInterfaceBuffer : public std::basic_streambuf<char, std::char_traits<char> > {
|
||||
friend class ServiceInterfaceStream;
|
||||
|
||||
public:
|
||||
ServiceInterfaceBuffer(std::string set_message, uint16_t port);
|
||||
|
||||
protected:
|
||||
bool isActive;
|
||||
// This is called when buffer becomes full. If
|
||||
// buffer is not used, then this is called every
|
||||
// time when characters are put to stream.
|
||||
virtual int overflow(int c = Traits::eof());
|
||||
|
||||
// This function is called when stream is flushed,
|
||||
// for example when std::endl is put to stream.
|
||||
virtual int sync(void);
|
||||
|
||||
private:
|
||||
// For additional message information
|
||||
std::string log_message;
|
||||
// For EOF detection
|
||||
typedef std::char_traits<char> Traits;
|
||||
|
||||
// Work in buffer mode. It is also possible to work without buffer.
|
||||
static size_t const BUF_SIZE = 128;
|
||||
char buf[BUF_SIZE];
|
||||
|
||||
// In this function, the characters are parsed.
|
||||
void putChars(char const* begin, char const* end);
|
||||
};
|
||||
#endif // UT699
|
||||
|
||||
#ifdef ML505
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/udp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
class ServiceInterfaceBuffer : public std::basic_streambuf<char, std::char_traits<char> > {
|
||||
friend class ServiceInterfaceStream;
|
||||
|
||||
public:
|
||||
ServiceInterfaceBuffer(std::string set_message, uint16_t port);
|
||||
|
||||
protected:
|
||||
bool isActive;
|
||||
// This is called when buffer becomes full. If
|
||||
// buffer is not used, then this is called every
|
||||
// time when characters are put to stream.
|
||||
virtual int overflow(int c = Traits::eof());
|
||||
|
||||
// This function is called when stream is flushed,
|
||||
// for example when std::endl is put to stream.
|
||||
virtual int sync(void);
|
||||
|
||||
private:
|
||||
// For additional message information
|
||||
std::string log_message;
|
||||
// For EOF detection
|
||||
typedef std::char_traits<char> Traits;
|
||||
|
||||
// Work in buffer mode. It is also possible to work without buffer.
|
||||
static size_t const BUF_SIZE = 128;
|
||||
char buf[BUF_SIZE];
|
||||
|
||||
// In this function, the characters are parsed.
|
||||
void putChars(char const* begin, char const* end);
|
||||
|
||||
int udpSocket;
|
||||
sockaddr_in remoteAddress;
|
||||
socklen_t remoteAddressLength;
|
||||
void initSocket();
|
||||
};
|
||||
#endif // ML505
|
||||
|
||||
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||
|
||||
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_ */
|
@ -0,0 +1,21 @@
|
||||
#include "ServiceInterfaceStream.h"
|
||||
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
|
||||
ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage, bool addCrToPreamble,
|
||||
bool buffered, bool errStream, uint16_t port)
|
||||
: std::ostream(&streambuf), streambuf(setMessage, addCrToPreamble, buffered, errStream, port) {}
|
||||
|
||||
void ServiceInterfaceStream::setActive(bool myActive) { this->streambuf.isActive = myActive; }
|
||||
|
||||
std::string* ServiceInterfaceStream::getPreamble() { return streambuf.getPreamble(); }
|
||||
|
||||
bool ServiceInterfaceStream::crAdditionEnabled() const { return streambuf.crAdditionEnabled(); }
|
||||
|
||||
#if FSFW_COLORED_OUTPUT == 1
|
||||
void ServiceInterfaceStream::setAsciiColorPrefix(std::string asciiColorCode) {
|
||||
streambuf.setAsciiColorPrefix(asciiColorCode);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,67 @@
|
||||
#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_
|
||||
#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_
|
||||
|
||||
#include "ServiceInterfaceBuffer.h"
|
||||
#include "fsfw/FSFW.h"
|
||||
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* Generic service interface stream which can be used like std::cout or
|
||||
* std::cerr but has additional capability. Add preamble and timestamp
|
||||
* to output. Can be run in buffered or unbuffered mode.
|
||||
*/
|
||||
class ServiceInterfaceStream : public std::ostream {
|
||||
public:
|
||||
/**
|
||||
* This constructor is used by specifying the preamble message.
|
||||
* Optionally, the output can be directed to stderr and a CR character
|
||||
* can be prepended to the preamble.
|
||||
* @param setMessage message of preamble.
|
||||
* @param addCrToPreamble Useful for applications like Puttty.
|
||||
* @param buffered specify whether to use buffered mode.
|
||||
* @param errStream specify which output stream to use (stderr or stdout).
|
||||
*/
|
||||
ServiceInterfaceStream(std::string setMessage, bool addCrToPreamble = false, bool buffered = true,
|
||||
bool errStream = false, uint16_t port = 1234);
|
||||
|
||||
//! An inactive stream will not print anything.
|
||||
void setActive(bool);
|
||||
|
||||
/**
|
||||
* This can be used to retrieve the preamble in case it should be printed in
|
||||
* the unbuffered mode.
|
||||
* @return Preamle consisting of log message and timestamp.
|
||||
*/
|
||||
std::string* getPreamble();
|
||||
|
||||
/**
|
||||
* Can be used to determine if the stream was configured to add CR characters in addition
|
||||
* to newline characters.
|
||||
* @return
|
||||
*/
|
||||
bool crAdditionEnabled() const;
|
||||
|
||||
#if FSFW_COLORED_OUTPUT == 1
|
||||
void setAsciiColorPrefix(std::string asciiColorCode);
|
||||
#endif
|
||||
|
||||
protected:
|
||||
ServiceInterfaceBuffer streambuf;
|
||||
};
|
||||
|
||||
// Forward declaration of interface streams. These should be instantiated in
|
||||
// main. They can then be used like std::cout or std::cerr.
|
||||
namespace sif {
|
||||
extern ServiceInterfaceStream debug;
|
||||
extern ServiceInterfaceStream info;
|
||||
extern ServiceInterfaceStream warning;
|
||||
extern ServiceInterfaceStream error;
|
||||
} // namespace sif
|
||||
|
||||
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
||||
|
||||
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */
|
@ -0,0 +1,18 @@
|
||||
#ifndef FSFW_SERVICEINTERFACE_SERVICEINTERFACEDEFINTIONS_H_
|
||||
#define FSFW_SERVICEINTERFACE_SERVICEINTERFACEDEFINTIONS_H_
|
||||
|
||||
namespace sif {
|
||||
|
||||
enum class OutputTypes { OUT_INFO, OUT_DEBUG, OUT_WARNING, OUT_ERROR };
|
||||
|
||||
static const char* const ANSI_COLOR_RED = "\x1b[31m";
|
||||
static const char* const ANSI_COLOR_GREEN = "\x1b[32m";
|
||||
static const char* const ANSI_COLOR_YELLOW = "\x1b[33m";
|
||||
static const char* const ANSI_COLOR_BLUE = "\x1b[34m";
|
||||
static const char* const ANSI_COLOR_MAGENTA = "\x1b[35m";
|
||||
static const char* const ANSI_COLOR_CYAN = "\x1b[36m";
|
||||
static const char* const ANSI_COLOR_RESET = "\x1b[0m";
|
||||
|
||||
} // namespace sif
|
||||
|
||||
#endif /* FSFW_SERVICEINTERFACE_SERVICEINTERFACEDEFINTIONS_H_ */
|
25
unittest/rebootLogic/src/libxiphos.cpp
Normal file
25
unittest/rebootLogic/src/libxiphos.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "libxiphos.h"
|
||||
|
||||
#include "CoreController.h"
|
||||
|
||||
bool rebootWasCalled = false;
|
||||
xsc_libnor_chip_t lastChip;
|
||||
xsc_libnor_copy_t lastCopy;
|
||||
|
||||
bool getRebootWasCalled(xsc_libnor_chip_t& tgtChip, xsc_libnor_copy_t& tgtCopy) {
|
||||
tgtChip = lastChip;
|
||||
tgtCopy = lastCopy;
|
||||
bool tmp = rebootWasCalled;
|
||||
if (rebootWasCalled) {
|
||||
rebootWasCalled = false;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void xsc_boot_copy(xsc_libnor_chip_t boot_chip, xsc_libnor_copy_t boot_copy) {
|
||||
rebootWasCalled = true;
|
||||
lastChip = boot_chip;
|
||||
lastCopy = boot_copy;
|
||||
CoreController::setCurrentBootCopy(static_cast<xsc::Chip>(boot_chip),
|
||||
static_cast<xsc::Copy>(boot_copy));
|
||||
}
|
25
unittest/rebootLogic/src/libxiphos.h
Normal file
25
unittest/rebootLogic/src/libxiphos.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
/** Type for identifying the nominal or the gold (redundant) flash copy */
|
||||
typedef enum {
|
||||
XSC_LIBNOR_COPY_NOMINAL,
|
||||
XSC_LIBNOR_COPY_GOLD,
|
||||
XSC_LIBNOR_COPY_TOTAL_NUMBER
|
||||
} xsc_libnor_copy_t;
|
||||
|
||||
/** Type for identifying on of the two NOR flash chips */
|
||||
typedef enum {
|
||||
XSC_LIBNOR_CHIP_0, /* First NOR flash chip */
|
||||
XSC_LIBNOR_CHIP_1, /* Second NOR flash chip */
|
||||
XSC_LIBNOR_CHIP_TOTAL_NUMBER
|
||||
} xsc_libnor_chip_t;
|
||||
|
||||
/**
|
||||
* @brief Used to verify whether reboot function was called
|
||||
*
|
||||
* @param tgtChip
|
||||
* @param tgtCopy
|
||||
*/
|
||||
bool getRebootWasCalled(xsc_libnor_chip_t& tgtChip, xsc_libnor_copy_t& tgtCopy);
|
||||
|
||||
void xsc_boot_copy(xsc_libnor_chip_t boot_chip, xsc_libnor_copy_t boot_copy);
|
413
unittest/rebootLogic/src/main.cpp
Normal file
413
unittest/rebootLogic/src/main.cpp
Normal file
@ -0,0 +1,413 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "CoreController.h"
|
||||
#include "HasActionsIF.h"
|
||||
#include "event.h"
|
||||
#include "fsfw/serviceinterface/ServiceInterface.h"
|
||||
#include "libxiphos.h"
|
||||
|
||||
static constexpr bool CAT_FILE_TO_CONSOLE = false;
|
||||
const std::string CONF_PATH = "/tmp/conf";
|
||||
const std::string REBOOT_FILE = CONF_PATH + "/reboot.txt";
|
||||
|
||||
void catFileToConsole();
|
||||
|
||||
ServiceInterfaceStream sif::debug("DEBUG");
|
||||
ServiceInterfaceStream sif::info("INFO");
|
||||
ServiceInterfaceStream sif::warning("WARNING");
|
||||
ServiceInterfaceStream sif::error("ERROR", false, false, true);
|
||||
|
||||
TEST_CASE("Core Controller Reboot File Handling", "[reboot-file]") {
|
||||
if (not std::filesystem::exists(CONF_PATH)) {
|
||||
std::filesystem::create_directory(CONF_PATH);
|
||||
}
|
||||
CoreController ctrl;
|
||||
std::array<uint8_t, 12> cmdData = {};
|
||||
|
||||
SECTION("Primary") {
|
||||
xsc_libnor_chip_t chip;
|
||||
xsc_libnor_copy_t copy;
|
||||
RebootFile rf = {};
|
||||
ctrl.rewriteRebootFile(rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.maxCount == RebootFile::DEFAULT_MAX_BOOT_CNT);
|
||||
REQUIRE(rf.img00Cnt == 0);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
REQUIRE(rf.bootFlag == 0);
|
||||
REQUIRE(rf.lastChip == 0);
|
||||
REQUIRE(rf.lastCopy == 0);
|
||||
// This recreates the file but should also increment the boot counter of the current image
|
||||
REQUIRE(ctrl.CURRENT_CHIP == xsc::CHIP_0);
|
||||
REQUIRE(ctrl.CURRENT_COPY == xsc::COPY_0);
|
||||
ctrl.performRebootFileHandling(true);
|
||||
catFileToConsole();
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.maxCount == RebootFile::DEFAULT_MAX_BOOT_CNT);
|
||||
REQUIRE(rf.img00Cnt == 1);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
REQUIRE(rf.bootFlag == 0);
|
||||
REQUIRE(rf.lastChip == xsc::CHIP_0);
|
||||
REQUIRE(rf.lastCopy == xsc::COPY_0);
|
||||
uint8_t newRebootCnt = 3;
|
||||
CHECK(ctrl.executeAction(CoreController::SET_MAX_REBOOT_CNT, 0, &newRebootCnt, 1) ==
|
||||
HasActionsIF::EXECUTION_FINISHED);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.maxCount == 3);
|
||||
REQUIRE(not getRebootWasCalled(chip, copy));
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 3);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
REQUIRE(rf.bootFlag == true);
|
||||
REQUIRE(rf.lastChip == xsc::CHIP_0);
|
||||
REQUIRE(rf.lastCopy == xsc::COPY_0);
|
||||
REQUIRE(getRebootWasCalled(chip, copy));
|
||||
REQUIRE(chip == XSC_LIBNOR_CHIP_0);
|
||||
REQUIRE(copy == XSC_LIBNOR_COPY_GOLD);
|
||||
EventInfo info = {};
|
||||
uint32_t numEvents = 0;
|
||||
|
||||
// We are now on image 0 1 and an event will be triggered
|
||||
ctrl.performRebootFileHandling(false);
|
||||
eventWasCalled(info, numEvents);
|
||||
CHECK(numEvents == 1);
|
||||
CHECK(info.event == CoreController::REBOOT_MECHANISM_TRIGGERED);
|
||||
CHECK(static_cast<xsc::Chip>((info.p1 >> 16) & 0xFFFF) == xsc::CHIP_0);
|
||||
CHECK(static_cast<xsc::Copy>(info.p1 & 0xFFFF) == xsc::COPY_0);
|
||||
CHECK(((info.p2 >> 24) & 0xFF) == 3);
|
||||
CHECK(((info.p2 >> 16) & 0xFF) == 1);
|
||||
CHECK(((info.p2 >> 8) & 0xFF) == 0);
|
||||
CHECK((info.p2 & 0xFF) == 0);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 3);
|
||||
REQUIRE(rf.img01Cnt == 1);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
// Flag was cleared when event was thrown
|
||||
REQUIRE(rf.bootFlag == false);
|
||||
REQUIRE(rf.lastChip == xsc::CHIP_0);
|
||||
REQUIRE(rf.lastCopy == xsc::COPY_1);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 3);
|
||||
REQUIRE(rf.img01Cnt == 3);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
|
||||
// We are now on image 1 0 and an event will be triggered
|
||||
ctrl.performRebootFileHandling(false);
|
||||
eventWasCalled(info, numEvents);
|
||||
CHECK(numEvents == 1);
|
||||
CHECK(info.event == CoreController::REBOOT_MECHANISM_TRIGGERED);
|
||||
CHECK(static_cast<xsc::Chip>((info.p1 >> 16) & 0xFFFF) == xsc::CHIP_0);
|
||||
CHECK(static_cast<xsc::Copy>(info.p1 & 0xFFFF) == xsc::COPY_1);
|
||||
CHECK(((info.p2 >> 24) & 0xFF) == 3);
|
||||
CHECK(((info.p2 >> 16) & 0xFF) == 3);
|
||||
CHECK(((info.p2 >> 8) & 0xFF) == 1);
|
||||
CHECK((info.p2 & 0xFF) == 0);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 3);
|
||||
REQUIRE(rf.img01Cnt == 3);
|
||||
REQUIRE(rf.img10Cnt == 1);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 3);
|
||||
REQUIRE(rf.img01Cnt == 3);
|
||||
REQUIRE(rf.img10Cnt == 3);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
eventWasCalled(info, numEvents);
|
||||
|
||||
// On image 1 1 now
|
||||
ctrl.performRebootFileHandling(false);
|
||||
eventWasCalled(info, numEvents);
|
||||
CHECK(numEvents == 1);
|
||||
CHECK(info.event == CoreController::REBOOT_MECHANISM_TRIGGERED);
|
||||
CHECK(static_cast<xsc::Chip>((info.p1 >> 16) & 0xFFFF) == xsc::CHIP_1);
|
||||
CHECK(static_cast<xsc::Copy>(info.p1 & 0xFFFF) == xsc::COPY_0);
|
||||
CHECK(((info.p2 >> 24) & 0xFF) == 3);
|
||||
CHECK(((info.p2 >> 16) & 0xFF) == 3);
|
||||
CHECK(((info.p2 >> 8) & 0xFF) == 3);
|
||||
CHECK((info.p2 & 0xFF) == 1);
|
||||
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
// Now it should fall back to 0 0 because all are invalid
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 3);
|
||||
REQUIRE(rf.img01Cnt == 3);
|
||||
REQUIRE(rf.img10Cnt == 3);
|
||||
REQUIRE(rf.img11Cnt == 3);
|
||||
|
||||
// Should remain on image now
|
||||
ctrl.performRebootFileHandling(false);
|
||||
eventWasCalled(info, numEvents);
|
||||
CHECK(numEvents == 1);
|
||||
CHECK(info.event == CoreController::REBOOT_MECHANISM_TRIGGERED);
|
||||
CHECK(static_cast<xsc::Chip>((info.p1 >> 16) & 0xFFFF) == xsc::CHIP_1);
|
||||
CHECK(static_cast<xsc::Copy>(info.p1 & 0xFFFF) == xsc::COPY_1);
|
||||
CHECK(((info.p2 >> 24) & 0xFF) == 4);
|
||||
CHECK(((info.p2 >> 16) & 0xFF) == 3);
|
||||
CHECK(((info.p2 >> 8) & 0xFF) == 3);
|
||||
CHECK((info.p2 & 0xFF) == 3);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.img00Cnt == 4);
|
||||
REQUIRE(rf.img01Cnt == 3);
|
||||
REQUIRE(rf.img10Cnt == 3);
|
||||
REQUIRE(rf.img11Cnt == 3);
|
||||
|
||||
// Reset a specific reboot counter
|
||||
cmdData[0] = 0;
|
||||
cmdData[1] = 1;
|
||||
ctrl.executeAction(CoreController::RESET_REBOOT_COUNTERS, 0, cmdData.data(), 2);
|
||||
// Reboot to 0 0 again
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 5);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 3);
|
||||
REQUIRE(rf.img11Cnt == 3);
|
||||
CHECK(CoreController::CURRENT_CHIP == xsc::CHIP_0);
|
||||
CHECK(CoreController::CURRENT_COPY == xsc::COPY_1);
|
||||
ctrl.executeAction(CoreController::RESET_REBOOT_COUNTERS, 0, nullptr, 0);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 0);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
uint8_t enable = 0;
|
||||
ctrl.executeAction(CoreController::SWITCH_REBOOT_FILE_HANDLING, 0, &enable, 1);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 0);
|
||||
REQUIRE(rf.img00Cnt == 0);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
|
||||
// Reboot to 1 0 explicitely
|
||||
CoreController::setCurrentBootCopy(xsc::CHIP_1, xsc::COPY_0);
|
||||
// Reboot three times and verify that no reboot is performed
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 0);
|
||||
REQUIRE(rf.img00Cnt == 0);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 4);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
// Now enable the functionality again and verify it reboots to 1 1
|
||||
enable = 1;
|
||||
ctrl.executeAction(CoreController::SWITCH_REBOOT_FILE_HANDLING, 0, &enable, 1);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 0);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 5);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 0);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 5);
|
||||
REQUIRE(rf.img11Cnt == 1);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
// Should be on 0 0 now
|
||||
CHECK(CoreController::CURRENT_CHIP == xsc::CHIP_0);
|
||||
CHECK(CoreController::CURRENT_COPY == xsc::COPY_0);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 1);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 5);
|
||||
REQUIRE(rf.img11Cnt == 3);
|
||||
|
||||
// Now reset all reboot counters manually
|
||||
cmdData[0] = 0;
|
||||
cmdData[1] = 0;
|
||||
ctrl.executeAction(CoreController::RESET_REBOOT_COUNTERS, 0, cmdData.data(), 2);
|
||||
cmdData[0] = 1;
|
||||
cmdData[1] = 0;
|
||||
ctrl.executeAction(CoreController::RESET_REBOOT_COUNTERS, 0, cmdData.data(), 2);
|
||||
cmdData[0] = 1;
|
||||
cmdData[1] = 1;
|
||||
ctrl.executeAction(CoreController::RESET_REBOOT_COUNTERS, 0, cmdData.data(), 2);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 0);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
|
||||
// Reset lock on 0 1
|
||||
cmdData[0] = false;
|
||||
cmdData[1] = 0;
|
||||
cmdData[2] = 1;
|
||||
ctrl.executeAction(CoreController::SWITCH_IMG_LOCK, 0, cmdData.data(), 3);
|
||||
CHECK(CoreController::CURRENT_CHIP == xsc::CHIP_0);
|
||||
CHECK(CoreController::CURRENT_COPY == xsc::COPY_0);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
// Now should be on 0 1
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
CHECK(CoreController::CURRENT_CHIP == xsc::CHIP_0);
|
||||
CHECK(CoreController::CURRENT_COPY == xsc::COPY_1);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 3);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
REQUIRE(rf.img00Lock == false);
|
||||
REQUIRE(rf.img01Lock == false);
|
||||
REQUIRE(rf.img10Lock == false);
|
||||
REQUIRE(rf.img11Lock == false);
|
||||
// Now simulate failure in kernel boot or reboot from ProASIC3
|
||||
ctrl.setCurrentBootCopy(xsc::CHIP_0, xsc::COPY_0);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
// Image 0 1 should have been marked as invalid now. Reboot will be triggered
|
||||
// on 1 0 instead of 0 1 because 0 1 is marked as locked
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 4);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
REQUIRE(rf.img00Lock == false);
|
||||
REQUIRE(rf.img01Lock == true);
|
||||
REQUIRE(rf.img10Lock == false);
|
||||
REQUIRE(rf.img11Lock == false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 4);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 1);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
REQUIRE(rf.img00Lock == false);
|
||||
REQUIRE(rf.img01Lock == true);
|
||||
REQUIRE(rf.img10Lock == false);
|
||||
REQUIRE(rf.img11Lock == false);
|
||||
// Reset lock on 0 1 again
|
||||
cmdData[0] = false;
|
||||
cmdData[1] = 0;
|
||||
cmdData[2] = 1;
|
||||
ctrl.executeAction(CoreController::SWITCH_IMG_LOCK, 0, cmdData.data(), 3);
|
||||
// Lock 1 1 manually
|
||||
// Reset lock on 0 1 again
|
||||
cmdData[0] = true;
|
||||
cmdData[1] = 1;
|
||||
cmdData[2] = 1;
|
||||
ctrl.executeAction(CoreController::SWITCH_IMG_LOCK, 0, cmdData.data(), 3);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
// Should be on 0 1 now
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 4);
|
||||
REQUIRE(rf.img01Cnt == 1);
|
||||
REQUIRE(rf.img10Cnt == 3);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
REQUIRE(rf.img00Lock == false);
|
||||
REQUIRE(rf.img01Lock == false);
|
||||
REQUIRE(rf.img10Lock == false);
|
||||
REQUIRE(rf.img11Lock == true);
|
||||
// Lock everything except 0 0
|
||||
cmdData[0] = true;
|
||||
cmdData[1] = 0;
|
||||
cmdData[2] = 1;
|
||||
ctrl.executeAction(CoreController::SWITCH_IMG_LOCK, 0, cmdData.data(), 3);
|
||||
cmdData[0] = true;
|
||||
cmdData[1] = 1;
|
||||
cmdData[2] = 0;
|
||||
ctrl.executeAction(CoreController::SWITCH_IMG_LOCK, 0, cmdData.data(), 3);
|
||||
ctrl.executeAction(CoreController::RESET_REBOOT_COUNTERS, 0, nullptr, 0);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 0);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
REQUIRE(rf.img00Lock == false);
|
||||
REQUIRE(rf.img01Lock == true);
|
||||
REQUIRE(rf.img10Lock == true);
|
||||
REQUIRE(rf.img11Lock == true);
|
||||
// Switch to 0 0
|
||||
ctrl.setCurrentBootCopy(xsc::CHIP_0, xsc::COPY_0);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.performRebootFileHandling(false);
|
||||
// Should still be on 0 0
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 4);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
REQUIRE(rf.img00Lock == false);
|
||||
REQUIRE(rf.img01Lock == true);
|
||||
REQUIRE(rf.img10Lock == true);
|
||||
REQUIRE(rf.img11Lock == true);
|
||||
// Unlock 1 0
|
||||
cmdData[0] = false;
|
||||
cmdData[1] = 1;
|
||||
cmdData[2] = 0;
|
||||
ctrl.executeAction(CoreController::SWITCH_IMG_LOCK, 0, cmdData.data(), 3);
|
||||
// Reboots to 1 0 now
|
||||
ctrl.performRebootFileHandling(false);
|
||||
ctrl.parseRebootFile(REBOOT_FILE, rf);
|
||||
REQUIRE(rf.enabled == 1);
|
||||
REQUIRE(rf.img00Cnt == 5);
|
||||
REQUIRE(rf.img01Cnt == 0);
|
||||
REQUIRE(rf.img10Cnt == 0);
|
||||
REQUIRE(rf.img11Cnt == 0);
|
||||
REQUIRE(rf.img00Lock == false);
|
||||
REQUIRE(rf.img01Lock == true);
|
||||
REQUIRE(rf.img10Lock == false);
|
||||
REQUIRE(rf.img11Lock == true);
|
||||
REQUIRE(CoreController::CURRENT_CHIP == xsc::CHIP_1);
|
||||
REQUIRE(CoreController::CURRENT_COPY == xsc::COPY_0);
|
||||
}
|
||||
if (std::filesystem::exists(CONF_PATH)) {
|
||||
std::uintmax_t n = std::filesystem::remove_all(CONF_PATH);
|
||||
CHECK(n == 2);
|
||||
}
|
||||
}
|
||||
|
||||
void catFileToConsole() {
|
||||
if (CAT_FILE_TO_CONSOLE) {
|
||||
std::ifstream file(REBOOT_FILE);
|
||||
if (file.is_open()) {
|
||||
std::cout << file.rdbuf();
|
||||
}
|
||||
}
|
||||
}
|
10
unittest/rebootLogic/src/print.c
Normal file
10
unittest/rebootLogic/src/print.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void printChar(const char* character, bool errStream) {
|
||||
if (errStream) {
|
||||
putc(*character, stderr);
|
||||
return;
|
||||
}
|
||||
putc(*character, stdout);
|
||||
}
|
Reference in New Issue
Block a user