eive-obsw/linux/acs/StrComHandler.cpp

812 lines
25 KiB
C++
Raw Normal View History

2023-03-24 21:01:00 +01:00
#include "StrComHandler.h"
2023-03-21 20:35:28 +01:00
#include <fcntl.h>
2022-10-25 11:31:06 +02:00
#include <fsfw/filesystem/HasFileSystemIF.h>
2023-03-21 20:35:28 +01:00
#include <fsfw/tasks/TaskFactory.h>
2023-03-22 02:20:14 +01:00
#include <fsfw/timemanager/Stopwatch.h>
#include <mission/acs/str/strHelpers.h>
2023-03-21 20:35:28 +01:00
#include <unistd.h>
2022-10-25 11:31:06 +02:00
2021-11-30 16:01:02 +01:00
#include <filesystem>
2022-01-17 15:58:27 +01:00
#include <fstream>
2021-11-30 16:01:02 +01:00
2022-02-05 18:08:54 +01:00
#include "OBSWConfig.h"
2022-09-26 11:39:17 +02:00
#include "eive/definitions.h"
2022-02-23 18:15:34 +01:00
#include "fsfw/timemanager/Countdown.h"
2022-04-30 16:21:59 +02:00
#include "mission/utility/Filenaming.h"
2022-03-01 17:17:15 +01:00
#include "mission/utility/ProgressPrinter.h"
#include "mission/utility/Timestamp.h"
2021-11-30 16:01:02 +01:00
2023-03-21 20:35:28 +01:00
using namespace returnvalue;
2023-03-22 02:20:14 +01:00
StrComHandler::StrComHandler(object_id_t objectId) : SystemObject(objectId) {
lock = MutexFactory::instance()->createMutex();
semaphore.acquire();
}
2021-11-30 16:01:02 +01:00
2023-03-21 20:35:28 +01:00
StrComHandler::~StrComHandler() {}
2021-11-30 16:01:02 +01:00
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::initialize() {
2022-02-05 13:19:20 +01:00
#ifdef XIPHOS_Q7S
2022-01-17 15:58:27 +01:00
sdcMan = SdCardManager::instance();
if (sdcMan == nullptr) {
sif::warning << "StrHelper::initialize: Invalid SD Card Manager" << std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2022-02-05 13:19:20 +01:00
#endif
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-11-30 16:01:02 +01:00
}
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::performOperation(uint8_t operationCode) {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-01-17 15:58:27 +01:00
while (true) {
2023-03-22 02:20:14 +01:00
lock->lockMutex();
state = InternalState::SLEEPING;
lock->unlockMutex();
semaphore.acquire();
switch (state) {
case InternalState::POLL_ONE_REPLY: {
// Stopwatch watch;
replyTimeout.setTimeout(200);
replyResult = readOneReply(static_cast<uint32_t>(state));
{
MutexGuard mg(lock);
replyWasReceived = true;
}
2022-01-17 15:58:27 +01:00
break;
}
case InternalState::UPLOAD_IMAGE: {
2023-03-22 02:20:14 +01:00
replyTimeout.setTimeout(200);
resetReplyHandlingState();
2022-01-17 15:58:27 +01:00
result = performImageUpload();
2022-08-24 17:27:47 +02:00
if (result == returnvalue::OK) {
2022-01-17 15:58:27 +01:00
triggerEvent(IMAGE_UPLOAD_SUCCESSFUL);
} else {
triggerEvent(IMAGE_UPLOAD_FAILED);
2021-12-02 08:05:33 +01:00
}
2022-01-17 15:58:27 +01:00
break;
}
case InternalState::DOWNLOAD_IMAGE: {
2023-03-22 02:20:14 +01:00
replyTimeout.setTimeout(200);
resetReplyHandlingState();
2022-01-17 15:58:27 +01:00
result = performImageDownload();
2022-08-24 17:27:47 +02:00
if (result == returnvalue::OK) {
2022-01-17 15:58:27 +01:00
triggerEvent(IMAGE_DOWNLOAD_SUCCESSFUL);
} else {
triggerEvent(IMAGE_DOWNLOAD_FAILED);
2021-12-21 15:46:09 +01:00
}
2022-01-17 15:58:27 +01:00
break;
}
case InternalState::FLASH_READ: {
2023-03-22 02:20:14 +01:00
replyTimeout.setTimeout(200);
resetReplyHandlingState();
2022-01-17 15:58:27 +01:00
result = performFlashRead();
2022-08-24 17:27:47 +02:00
if (result == returnvalue::OK) {
2022-01-17 15:58:27 +01:00
triggerEvent(FLASH_READ_SUCCESSFUL);
} else {
triggerEvent(FLASH_READ_FAILED);
2021-12-29 20:33:20 +01:00
}
2022-01-17 15:58:27 +01:00
break;
}
2022-02-01 11:45:22 +01:00
case InternalState::FIRMWARE_UPDATE: {
replyTimeout.setTimeout(2000);
resetReplyHandlingState();
2022-02-01 11:45:22 +01:00
result = performFirmwareUpdate();
2022-08-24 17:27:47 +02:00
if (result == returnvalue::OK) {
2022-02-01 11:45:22 +01:00
triggerEvent(FIRMWARE_UPDATE_SUCCESSFUL);
} else {
triggerEvent(FIRMWARE_UPDATE_FAILED);
}
break;
}
2022-01-17 15:58:27 +01:00
default:
sif::debug << "StrHelper::performOperation: Invalid state" << std::endl;
break;
2021-11-30 16:01:02 +01:00
}
2022-01-17 15:58:27 +01:00
}
2021-11-30 16:01:02 +01:00
}
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::startImageUpload(std::string fullname) {
2023-03-22 02:20:14 +01:00
{
MutexGuard mg(lock);
if (state != InternalState::SLEEPING) {
return BUSY;
}
}
2022-02-05 13:19:20 +01:00
#ifdef XIPHOS_Q7S
2022-01-17 15:58:27 +01:00
ReturnValue_t result = checkPath(fullname);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
return result;
}
2022-02-05 13:19:20 +01:00
#endif
2022-01-17 15:58:27 +01:00
uploadImage.uploadFile = fullname;
if (not std::filesystem::exists(fullname)) {
return FILE_NOT_EXISTS;
}
2023-03-22 02:20:14 +01:00
{
MutexGuard mg(lock);
replyWasReceived = false;
2023-03-22 02:20:14 +01:00
state = InternalState::UPLOAD_IMAGE;
}
2022-01-17 15:58:27 +01:00
semaphore.release();
terminate = false;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-12-06 19:36:21 +01:00
}
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::startImageDownload(std::string path) {
2023-03-22 02:20:14 +01:00
{
MutexGuard mg(lock);
if (state != InternalState::SLEEPING) {
return BUSY;
}
}
2022-02-05 13:19:20 +01:00
#ifdef XIPHOS_Q7S
2022-01-17 15:58:27 +01:00
ReturnValue_t result = checkPath(path);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
return result;
}
2022-02-05 13:19:20 +01:00
#endif
2022-01-17 15:58:27 +01:00
if (not std::filesystem::exists(path)) {
return PATH_NOT_EXISTS;
}
downloadImage.path = path;
2023-03-22 02:20:14 +01:00
{
MutexGuard mg(lock);
replyWasReceived = false;
2023-03-22 02:20:14 +01:00
state = InternalState::DOWNLOAD_IMAGE;
}
2022-01-17 15:58:27 +01:00
terminate = false;
semaphore.release();
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-12-09 15:02:58 +01:00
}
2023-03-24 00:59:41 +01:00
void StrComHandler::stopProcess() { terminate = true; }
2021-12-10 10:07:23 +01:00
2023-03-21 20:35:28 +01:00
void StrComHandler::setDownloadImageName(std::string filename) {
downloadImage.filename = filename;
}
2021-12-11 11:56:47 +01:00
2023-03-21 20:35:28 +01:00
void StrComHandler::setFlashReadFilename(std::string filename) { flashRead.filename = filename; }
2021-12-22 16:06:30 +01:00
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::startFirmwareUpdate(std::string fullname) {
2023-03-22 02:20:14 +01:00
{
MutexGuard mg(lock);
if (state != InternalState::SLEEPING) {
return BUSY;
}
}
2022-02-05 13:19:20 +01:00
#ifdef XIPHOS_Q7S
2022-01-17 15:58:27 +01:00
ReturnValue_t result = checkPath(fullname);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
return result;
}
2022-02-05 13:19:20 +01:00
#endif
2022-01-17 15:58:27 +01:00
flashWrite.fullname = fullname;
if (not std::filesystem::exists(flashWrite.fullname)) {
return FILE_NOT_EXISTS;
}
2022-02-14 11:28:15 +01:00
flashWrite.firstRegion = static_cast<uint8_t>(startracker::FirmwareRegions::FIRST);
flashWrite.lastRegion = static_cast<uint8_t>(startracker::FirmwareRegions::LAST);
2023-03-22 02:20:14 +01:00
{
MutexGuard mg(lock);
replyWasReceived = false;
2023-03-22 02:20:14 +01:00
state = InternalState::FIRMWARE_UPDATE;
}
2022-01-17 15:58:27 +01:00
semaphore.release();
terminate = false;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-12-21 15:46:09 +01:00
}
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::startFlashRead(std::string path, uint8_t startRegion,
uint32_t length) {
2023-03-22 02:20:14 +01:00
{
MutexGuard mg(lock);
if (state != InternalState::SLEEPING) {
return BUSY;
}
}
2022-02-05 13:19:20 +01:00
#ifdef XIPHOS_Q7S
2022-01-17 15:58:27 +01:00
ReturnValue_t result = checkPath(path);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
return result;
}
2022-02-05 13:19:20 +01:00
#endif
2022-01-17 15:58:27 +01:00
flashRead.path = path;
if (not std::filesystem::exists(flashRead.path)) {
return FILE_NOT_EXISTS;
}
2022-02-25 14:24:51 +01:00
flashRead.startRegion = startRegion;
2022-01-17 15:58:27 +01:00
flashRead.size = length;
2023-03-22 02:20:14 +01:00
{
MutexGuard mg(lock);
replyWasReceived = false;
2023-03-22 02:20:14 +01:00
state = InternalState::FLASH_READ;
}
2022-01-17 15:58:27 +01:00
semaphore.release();
terminate = false;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-12-22 16:06:30 +01:00
}
2023-03-21 20:35:28 +01:00
void StrComHandler::disableTimestamping() { timestamping = false; }
2023-03-21 20:35:28 +01:00
void StrComHandler::enableTimestamping() { timestamping = true; }
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::performImageDownload() {
2022-10-25 11:31:06 +02:00
#ifdef XIPHOS_Q7S
if (not sdcMan->getActiveSdCard()) {
return HasFileSystemIF::FILESYSTEM_INACTIVE;
}
#endif
2022-01-17 15:58:27 +01:00
ReturnValue_t result;
2022-03-01 17:17:15 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
ProgressPrinter progressPrinter("Image download", ImageDownload::LAST_POSITION);
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-01-17 15:58:27 +01:00
struct DownloadActionRequest downloadReq;
uint32_t size = 0;
uint32_t retries = 0;
2023-03-21 20:47:06 +01:00
size_t replySize = 0;
2022-04-13 11:56:37 +02:00
std::string image = Filenaming::generateAbsoluteFilename(downloadImage.path,
downloadImage.filename, timestamping);
std::ofstream file(image, std::ios_base::out);
2022-01-17 15:58:27 +01:00
if (not std::filesystem::exists(image)) {
return FILE_CREATION_FAILED;
}
downloadReq.position = 0;
while (downloadReq.position < ImageDownload::LAST_POSITION) {
if (terminate) {
2022-02-23 18:15:34 +01:00
file.close();
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
2023-03-22 02:20:14 +01:00
arc_pack_download_action_req(&downloadReq, cmdBuf.data(), &size);
result = sendAndRead(size, downloadReq.position);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
if (retries < CONFIG_MAX_DOWNLOAD_RETRIES) {
2023-03-22 02:20:14 +01:00
serial::flushRxBuf(serialPort);
2022-01-17 15:58:27 +01:00
retries++;
continue;
}
file.close();
return result;
2021-12-09 15:02:58 +01:00
}
2023-03-22 02:20:14 +01:00
result = checkActionReply(replySize);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
if (retries < CONFIG_MAX_DOWNLOAD_RETRIES) {
2023-03-22 02:20:14 +01:00
serial::flushRxBuf(serialPort);
2022-01-17 15:58:27 +01:00
retries++;
continue;
}
file.close();
return result;
}
2023-03-22 02:20:14 +01:00
result = checkReplyPosition(downloadReq.position);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
if (retries < CONFIG_MAX_DOWNLOAD_RETRIES) {
2023-03-22 02:20:14 +01:00
serial::flushRxBuf(serialPort);
2022-01-17 15:58:27 +01:00
retries++;
continue;
}
file.close();
return result;
}
2023-03-22 02:20:14 +01:00
file.write(reinterpret_cast<const char*>(replyPtr + IMAGE_DATA_OFFSET), CHUNK_SIZE);
2022-02-01 10:45:07 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
2022-03-01 17:17:15 +01:00
progressPrinter.print(downloadReq.position);
2022-02-01 10:45:07 +01:00
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-03-01 17:17:15 +01:00
downloadReq.position++;
2022-01-17 15:58:27 +01:00
retries = 0;
}
file.close();
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-11-30 16:01:02 +01:00
}
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::performImageUpload() {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-01-17 15:58:27 +01:00
uint32_t size = 0;
uint32_t imageSize = 0;
struct UploadActionRequest uploadReq;
uploadReq.position = 0;
2022-10-25 11:31:06 +02:00
#ifdef XIPHOS_Q7S
if (not sdcMan->getActiveSdCard()) {
return HasFileSystemIF::FILESYSTEM_INACTIVE;
}
#endif
2022-01-17 15:58:27 +01:00
std::memset(&uploadReq.data, 0, sizeof(uploadReq.data));
if (not std::filesystem::exists(uploadImage.uploadFile)) {
2023-03-22 02:20:14 +01:00
triggerEvent(STR_HELPER_FILE_NOT_EXISTS, static_cast<uint32_t>(state));
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
std::ifstream file(uploadImage.uploadFile, std::ifstream::binary);
2023-03-21 17:37:39 +01:00
if (file.bad()) {
return HasFileSystemIF::GENERIC_FILE_ERROR;
}
2022-01-17 15:58:27 +01:00
// Set position of next character to end of file input stream
file.seekg(0, file.end);
// tellg returns position of character in input stream
imageSize = file.tellg();
2022-03-01 17:17:15 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
ProgressPrinter progressPrinter("Image upload", imageSize);
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-01-17 15:58:27 +01:00
while ((uploadReq.position + 1) * SIZE_IMAGE_PART < imageSize) {
if (terminate) {
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-12-02 08:05:33 +01:00
}
2021-12-09 15:02:58 +01:00
file.seekg(uploadReq.position * SIZE_IMAGE_PART, file.beg);
2022-01-17 15:58:27 +01:00
file.read(reinterpret_cast<char*>(uploadReq.data), SIZE_IMAGE_PART);
2023-03-22 02:20:14 +01:00
arc_pack_upload_action_req(&uploadReq, cmdBuf.data(), &size);
result = sendAndRead(size, uploadReq.position);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
return returnvalue::FAILED;
2021-12-06 19:36:21 +01:00
}
2023-03-22 02:20:14 +01:00
result = checkActionReply(replyLen);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
return result;
2021-12-06 19:36:21 +01:00
}
2022-02-01 10:45:07 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
2022-03-01 17:17:15 +01:00
progressPrinter.print((uploadReq.position + 1) * SIZE_IMAGE_PART);
2022-02-01 10:45:07 +01:00
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-01-17 15:58:27 +01:00
uploadReq.position++;
2023-03-22 02:20:14 +01:00
// This does a bit of delaying roughly every second
if (uploadReq.position % 50 == 0) {
// Some grace time for other tasks
TaskFactory::delayTask(2);
}
2022-01-17 15:58:27 +01:00
}
std::memset(uploadReq.data, 0, sizeof(uploadReq.data));
uint32_t remainder = imageSize - uploadReq.position * SIZE_IMAGE_PART;
file.seekg(uploadReq.position * SIZE_IMAGE_PART, file.beg);
file.read(reinterpret_cast<char*>(uploadReq.data), remainder);
file.close();
uploadReq.position++;
2023-03-22 02:20:14 +01:00
arc_pack_upload_action_req(&uploadReq, cmdBuf.data(), &size);
result = sendAndRead(size, uploadReq.position);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2023-03-22 02:20:14 +01:00
result = checkActionReply(replyLen);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
return result;
}
2022-02-01 10:45:07 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
2022-03-01 17:17:15 +01:00
progressPrinter.print((uploadReq.position + 1) * SIZE_IMAGE_PART);
2022-02-01 10:45:07 +01:00
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-12-06 19:36:21 +01:00
}
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::performFirmwareUpdate() {
2022-02-23 18:15:34 +01:00
using namespace startracker;
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-02-23 18:15:34 +01:00
result = unlockAndEraseRegions(static_cast<uint32_t>(startracker::FirmwareRegions::FIRST),
static_cast<uint32_t>(startracker::FirmwareRegions::LAST));
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-02-01 11:45:22 +01:00
return result;
2022-02-23 18:15:34 +01:00
}
result = performFlashWrite();
return result;
2022-02-01 11:45:22 +01:00
}
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::performFlashWrite() {
2022-10-25 11:31:06 +02:00
#ifdef XIPHOS_Q7S
if (not sdcMan->getActiveSdCard()) {
return HasFileSystemIF::FILESYSTEM_INACTIVE;
}
#endif
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-01-17 15:58:27 +01:00
uint32_t size = 0;
uint32_t bytesWrittenInRegion = 0;
size_t totalBytesWritten = 0;
2022-01-17 15:58:27 +01:00
uint32_t fileSize = 0;
2023-03-21 20:47:06 +01:00
2022-01-17 15:58:27 +01:00
struct WriteActionRequest req;
if (not std::filesystem::exists(flashWrite.fullname)) {
2023-03-22 02:20:14 +01:00
triggerEvent(STR_HELPER_FILE_NOT_EXISTS, static_cast<uint32_t>(state));
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
std::ifstream file(flashWrite.fullname, std::ifstream::binary);
2023-03-22 02:20:14 +01:00
if (file.bad()) {
return returnvalue::FAILED;
}
2022-01-17 15:58:27 +01:00
file.seekg(0, file.end);
fileSize = file.tellg();
2022-02-14 11:28:15 +01:00
if (fileSize > FLASH_REGION_SIZE * (flashWrite.lastRegion - flashWrite.firstRegion)) {
2022-02-23 18:15:34 +01:00
sif::warning << "StrHelper::performFlashWrite: Invalid file" << std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-02-14 11:28:15 +01:00
}
2022-03-01 17:17:15 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
ProgressPrinter progressPrinter("Flash write", fileSize);
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-02-14 11:28:15 +01:00
uint32_t fileChunks = fileSize / CHUNK_SIZE;
bytesWrittenInRegion = 0;
2022-02-14 11:28:15 +01:00
req.region = flashWrite.firstRegion;
req.length = CHUNK_SIZE;
auto writeNextSegment = [&](uint32_t chunkIdx) {
file.seekg(chunkIdx * CHUNK_SIZE, file.beg);
2022-02-14 11:28:15 +01:00
file.read(reinterpret_cast<char*>(req.data), CHUNK_SIZE);
if (bytesWrittenInRegion + CHUNK_SIZE > FLASH_REGION_SIZE) {
2022-02-23 18:15:34 +01:00
req.region++;
bytesWrittenInRegion = 0;
2022-02-14 11:28:15 +01:00
}
req.address = bytesWrittenInRegion;
2023-03-22 02:20:14 +01:00
arc_pack_write_action_req(&req, cmdBuf.data(), &size);
result = sendAndRead(size, req.address);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-02-04 13:06:56 +01:00
return result;
2021-12-21 15:46:09 +01:00
}
2023-03-22 02:20:14 +01:00
result = checkActionReply(replyLen);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
return result;
}
totalBytesWritten += CHUNK_SIZE;
bytesWrittenInRegion += CHUNK_SIZE;
2022-03-01 17:17:15 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
progressPrinter.print(chunkIdx * CHUNK_SIZE);
2022-03-01 17:17:15 +01:00
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
return result;
};
for (uint32_t idx = 0; idx < fileChunks; idx++) {
if (terminate) {
return returnvalue::OK;
}
result = writeNextSegment(idx);
if(result != returnvalue::OK) {
return result;
}
2023-03-22 02:20:14 +01:00
if (idx % 50 == 0) {
// Some grace time for other tasks
TaskFactory::delayTask(2);
}
2022-01-17 15:58:27 +01:00
}
2022-02-14 11:28:15 +01:00
uint32_t remainingBytes = fileSize - fileChunks * CHUNK_SIZE;
if (remainingBytes > 0) {
file.seekg(fileChunks * CHUNK_SIZE, file.beg);
file.read(reinterpret_cast<char*>(req.data), remainingBytes);
file.close();
if (bytesWrittenInRegion + CHUNK_SIZE > FLASH_REGION_SIZE) {
req.region++;
bytesWrittenInRegion = 0;
}
req.address = bytesWrittenInRegion;
req.length = remainingBytes;
totalBytesWritten += CHUNK_SIZE;
bytesWrittenInRegion += remainingBytes;
arc_pack_write_action_req(&req, cmdBuf.data(), &size);
result = sendAndRead(size, req.address);
if (result != returnvalue::OK) {
return result;
}
result = checkActionReply(replyLen);
if (result != returnvalue::OK) {
return result;
}
2022-01-17 15:58:27 +01:00
}
2022-03-01 17:17:15 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
progressPrinter.print(fileSize);
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-12-21 15:46:09 +01:00
}
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::performFlashRead() {
2022-10-25 11:31:06 +02:00
#ifdef XIPHOS_Q7S
if (not sdcMan->getActiveSdCard()) {
return HasFileSystemIF::FILESYSTEM_INACTIVE;
}
#endif
2022-01-17 15:58:27 +01:00
ReturnValue_t result;
2022-03-01 17:17:15 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
ProgressPrinter progressPrinter("Flash read", flashRead.size);
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-01-17 15:58:27 +01:00
struct ReadActionRequest req;
uint32_t bytesRead = 0;
uint32_t size = 0;
uint32_t retries = 0;
Timestamp timestamp;
2022-04-13 11:56:37 +02:00
std::string fullname =
Filenaming::generateAbsoluteFilename(flashRead.path, flashRead.filename, timestamping);
2022-01-17 15:58:27 +01:00
std::ofstream file(fullname, std::ios_base::app | std::ios_base::out);
if (not std::filesystem::exists(fullname)) {
return FILE_CREATION_FAILED;
}
2022-02-25 14:24:51 +01:00
req.region = flashRead.startRegion;
req.address = 0;
2022-01-17 15:58:27 +01:00
while (bytesRead < flashRead.size) {
if (terminate) {
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
2022-02-14 11:28:15 +01:00
if ((flashRead.size - bytesRead) < CHUNK_SIZE) {
2022-01-17 15:58:27 +01:00
req.length = flashRead.size - bytesRead;
} else {
2022-02-14 11:28:15 +01:00
req.length = CHUNK_SIZE;
2022-01-17 15:58:27 +01:00
}
2023-03-22 02:20:14 +01:00
arc_pack_read_action_req(&req, cmdBuf.data(), &size);
result = sendAndRead(size, req.address);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
if (retries < CONFIG_MAX_DOWNLOAD_RETRIES) {
2023-03-22 02:20:14 +01:00
serial::flushRxBuf(serialPort);
2022-01-17 15:58:27 +01:00
retries++;
continue;
}
file.close();
return result;
2021-12-22 11:14:27 +01:00
}
2023-03-22 02:20:14 +01:00
result = checkActionReply(replyLen);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
if (retries < CONFIG_MAX_DOWNLOAD_RETRIES) {
2023-03-22 02:20:14 +01:00
serial::flushRxBuf(serialPort);
2022-01-17 15:58:27 +01:00
retries++;
continue;
}
file.close();
return result;
2021-12-22 11:14:27 +01:00
}
2023-03-22 02:20:14 +01:00
file.write(reinterpret_cast<const char*>(replyPtr + FLASH_READ_DATA_OFFSET), req.length);
2022-01-17 15:58:27 +01:00
bytesRead += req.length;
2022-02-25 14:24:51 +01:00
req.address += req.length;
if (req.address >= FLASH_REGION_SIZE) {
2022-02-27 15:48:42 +01:00
req.address = 0;
req.region++;
2022-02-25 14:24:51 +01:00
}
2022-01-17 15:58:27 +01:00
retries = 0;
2022-02-27 15:46:06 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
2022-03-01 17:17:15 +01:00
progressPrinter.print(bytesRead);
2022-02-27 15:46:06 +01:00
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-01-17 15:58:27 +01:00
}
file.close();
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-12-22 11:14:27 +01:00
}
2023-03-22 02:20:14 +01:00
ReturnValue_t StrComHandler::sendAndRead(size_t size, uint32_t failParameter) {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2023-03-21 18:47:42 +01:00
2023-03-21 20:35:28 +01:00
const uint8_t* sendData;
2023-03-21 18:47:42 +01:00
size_t txFrameLen = 0;
2023-03-22 02:20:14 +01:00
datalinkLayer.encodeFrame(cmdBuf.data(), size, &sendData, txFrameLen);
2023-03-21 20:35:28 +01:00
int writeResult = write(serialPort, sendData, txFrameLen);
if (writeResult < 0) {
2022-01-17 15:58:27 +01:00
sif::warning << "StrHelper::sendAndRead: Failed to send packet" << std::endl;
2023-03-21 20:35:28 +01:00
triggerEvent(STR_HELPER_SENDING_PACKET_FAILED, result, failParameter);
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2023-03-22 02:20:14 +01:00
return readOneReply(failParameter);
2021-12-06 19:36:21 +01:00
}
2021-12-02 08:05:33 +01:00
2023-03-22 02:20:14 +01:00
ReturnValue_t StrComHandler::checkActionReply(size_t replySize) {
uint8_t type = startracker::getReplyFrameType(replyPtr);
2022-01-17 15:58:27 +01:00
if (type != TMTC_ACTIONREPLY) {
sif::warning << "StrHelper::checkActionReply: Received reply with invalid type ID" << std::endl;
return INVALID_TYPE_ID;
}
uint8_t status = startracker::getStatusField(replyPtr);
2022-01-17 15:58:27 +01:00
if (status != ArcsecDatalinkLayer::STATUS_OK) {
sif::warning << "StrHelper::checkActionReply: Status failure: "
<< static_cast<unsigned int>(status) << std::endl;
return STATUS_ERROR;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-11-30 16:01:02 +01:00
}
2021-12-09 15:02:58 +01:00
2023-03-22 02:20:14 +01:00
ReturnValue_t StrComHandler::checkReplyPosition(uint32_t expectedPosition) {
2022-01-17 15:58:27 +01:00
uint32_t receivedPosition = 0;
2023-03-22 02:20:14 +01:00
std::memcpy(&receivedPosition, replyPtr + POS_OFFSET, sizeof(receivedPosition));
2022-01-17 15:58:27 +01:00
if (receivedPosition != expectedPosition) {
triggerEvent(POSITION_MISMATCH, receivedPosition);
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-12-09 15:02:58 +01:00
}
2021-12-11 11:56:47 +01:00
2022-02-05 13:19:20 +01:00
#ifdef XIPHOS_Q7S
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::checkPath(std::string name) {
2022-09-26 11:39:17 +02:00
if (name.substr(0, sizeof(config::SD_0_MOUNT_POINT)) == std::string(config::SD_0_MOUNT_POINT)) {
2022-09-27 10:51:07 +02:00
if (!sdcMan->isSdCardUsable(sd::SLOT_0)) {
2022-01-17 15:58:27 +01:00
sif::warning << "StrHelper::checkPath: SD card 0 not mounted" << std::endl;
return SD_NOT_MOUNTED;
}
2022-09-26 11:39:17 +02:00
} else if (name.substr(0, sizeof(config::SD_1_MOUNT_POINT)) ==
std::string(config::SD_1_MOUNT_POINT)) {
2022-09-27 10:51:07 +02:00
if (!sdcMan->isSdCardUsable(sd::SLOT_0)) {
2022-01-17 15:58:27 +01:00
sif::warning << "StrHelper::checkPath: SD card 1 not mounted" << std::endl;
return SD_NOT_MOUNTED;
}
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-12-11 11:56:47 +01:00
}
2022-02-05 13:19:20 +01:00
#endif
2022-02-01 10:45:07 +01:00
2023-03-21 20:35:28 +01:00
ReturnValue_t StrComHandler::initializeInterface(CookieIF* cookie) {
if (cookie == nullptr) {
return returnvalue::FAILED;
}
SerialCookie* serCookie = dynamic_cast<SerialCookie*>(cookie);
if (serCookie == nullptr) {
return DeviceCommunicationIF::INVALID_COOKIE_TYPE;
}
// comCookie = serCookie;
std::string devname = serCookie->getDeviceFile();
/* Get file descriptor */
serialPort = open(devname.c_str(), O_RDWR);
if (serialPort < 0) {
sif::warning << "StrComHandler: open call failed with error [" << errno << ", "
<< strerror(errno) << std::endl;
return returnvalue::FAILED;
}
// Setting up UART parameters
tty.c_cflag &= ~PARENB; // Clear parity bit
2023-03-22 02:20:14 +01:00
serial::setStopbits(tty, serCookie->getStopBits());
serial::setBitsPerWord(tty, BitsPerWord::BITS_8);
2023-03-21 20:35:28 +01:00
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
2023-03-22 02:20:14 +01:00
serial::enableRead(tty);
serial::ignoreCtrlLines(tty);
2023-03-21 20:35:28 +01:00
// Use non-canonical mode and clear echo flag
tty.c_lflag &= ~(ICANON | ECHO);
// Non-blocking mode, use polling
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 0;
2023-03-22 02:20:14 +01:00
serial::setBaudrate(tty, serCookie->getBaudrate());
2023-03-21 20:35:28 +01:00
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
sif::warning << "ScexUartReader::initializeInterface: tcsetattr call failed with error ["
<< errno << ", " << strerror(errno) << std::endl;
}
// Flush received and unread data
tcflush(serialPort, TCIOFLUSH);
return returnvalue::OK;
}
ReturnValue_t StrComHandler::sendMessage(CookieIF* cookie, const uint8_t* sendData,
size_t sendLen) {
2023-03-22 02:20:14 +01:00
{
MutexGuard mg(lock);
if (state != InternalState::SLEEPING) {
return BUSY;
}
}
// Ensure consistent state.
resetReplyHandlingState();
2023-03-22 02:20:14 +01:00
2023-03-21 20:35:28 +01:00
const uint8_t* txFrame;
size_t frameLen;
datalinkLayer.encodeFrame(sendData, sendLen, &txFrame, frameLen);
2023-03-24 00:59:41 +01:00
ssize_t bytesWritten = write(serialPort, txFrame, frameLen);
if (bytesWritten != static_cast<ssize_t>(frameLen)) {
sif::warning << "StrComHandler: Sending packet failed" << std::endl;
2023-03-21 20:35:28 +01:00
return returnvalue::FAILED;
}
2023-03-22 02:20:14 +01:00
// Hacky, but the alternatives look bleak. The raw data contains the information we need
// and there are not too many special cases.
if (sendData[0] == TMTC_ACTIONREQ) {
// 1 is a firmware boot request and 7 is a reboot request. For both, no reply is expected.
if (sendData[1] == 7 or sendData[1] == 1) {
return returnvalue::OK;
}
}
{
MutexGuard mg(lock);
state = InternalState::POLL_ONE_REPLY;
}
// Unlock task to perform reply reading.
semaphore.release();
2023-03-21 20:35:28 +01:00
return returnvalue::OK;
}
ReturnValue_t StrComHandler::getSendSuccess(CookieIF* cookie) { return returnvalue::OK; }
ReturnValue_t StrComHandler::requestReceiveMessage(CookieIF* cookie, size_t requestLen) {
return returnvalue::OK;
}
ReturnValue_t StrComHandler::readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) {
2023-03-22 02:20:14 +01:00
// Consider it a configuration error if the task is not done with a command -> reply cycle
// in time.
bool replyWasReceived = false;
{
MutexGuard mg(lock);
if (state != InternalState::SLEEPING) {
return BUSY;
}
replyWasReceived = this->replyWasReceived;
}
if (not replyWasReceived) {
*size = 0;
return returnvalue::OK;
}
if (replyResult == returnvalue::OK) {
*buffer = const_cast<uint8_t*>(replyPtr);
*size = replyLen;
}
replyLen = 0;
2023-03-22 02:20:14 +01:00
return replyResult;
2023-03-21 20:35:28 +01:00
}
ReturnValue_t StrComHandler::unlockAndEraseRegions(uint32_t from, uint32_t to) {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-03-01 17:17:15 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
ProgressPrinter progressPrinter("Unlock and erase", to - from);
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-02-23 18:15:34 +01:00
struct UnlockActionRequest unlockReq;
struct EraseActionRequest eraseReq;
uint32_t size = 0;
2022-03-01 17:17:15 +01:00
for (uint32_t idx = from; idx <= to; idx++) {
2022-02-23 18:15:34 +01:00
unlockReq.region = idx;
unlockReq.code = startracker::region_secrets::secret[idx];
2023-03-22 02:20:14 +01:00
arc_pack_unlock_action_req(&unlockReq, cmdBuf.data(), &size);
result = sendAndRead(size, unlockReq.region);
if (result != returnvalue::OK) {
return result;
}
result = checkActionReply(replyLen);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-02-23 18:15:34 +01:00
sif::warning << "StrHelper::unlockAndEraseRegions: Failed to unlock region with id "
<< static_cast<unsigned int>(unlockReq.region) << std::endl;
return result;
2022-02-04 13:06:56 +01:00
}
2022-02-23 18:15:34 +01:00
eraseReq.region = idx;
2023-03-22 02:20:14 +01:00
arc_pack_erase_action_req(&eraseReq, cmdBuf.data(), &size);
result = sendAndRead(size, eraseReq.region);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-02-23 18:15:34 +01:00
sif::warning << "StrHelper::unlockAndEraseRegions: Failed to erase region with id "
<< static_cast<unsigned int>(eraseReq.region) << std::endl;
return result;
}
2022-03-01 17:17:15 +01:00
#if OBSW_DEBUG_STARTRACKER == 1
progressPrinter.print(idx - from);
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
2022-02-23 18:15:34 +01:00
}
return result;
2022-02-04 13:06:56 +01:00
}
2023-03-21 20:35:28 +01:00
2023-03-21 20:47:06 +01:00
ReturnValue_t StrComHandler::handleSerialReception() {
2023-03-21 20:35:28 +01:00
ssize_t bytesRead = read(serialPort, reinterpret_cast<void*>(recBuf.data()),
static_cast<unsigned int>(recBuf.size()));
if (bytesRead == 0) {
return NO_SERIAL_DATA_READ;
} else if (bytesRead < 0) {
2023-03-24 00:59:41 +01:00
sif::warning << "StrComHandler: read call failed with error [" << errno << ", "
<< strerror(errno) << "]" << std::endl;
2023-03-21 20:35:28 +01:00
return FAILED;
} else if (bytesRead >= static_cast<int>(recBuf.size())) {
2023-03-24 00:59:41 +01:00
sif::error << "StrComHandler: Receive buffer too small for " << bytesRead << " bytes"
<< std::endl;
2023-03-21 20:35:28 +01:00
return FAILED;
} else if (bytesRead > 0) {
2023-03-22 02:20:14 +01:00
// sif::info << "Received " << bytesRead << " bytes from the STR" << std::endl;
2023-03-21 20:35:28 +01:00
// arrayprinter::print(recBuf.data(), bytesRead);
datalinkLayer.feedData(recBuf.data(), bytesRead);
}
return OK;
}
2023-03-22 02:20:14 +01:00
ReturnValue_t StrComHandler::readOneReply(uint32_t failParameter) {
ReturnValue_t result;
uint32_t nextDelayMs = 1;
replyTimeout.resetTimer();
while (true) {
handleSerialReception();
result = datalinkLayer.checkRingBufForFrame(&replyPtr, replyLen);
if (result == returnvalue::OK) {
return returnvalue::OK;
} else if (result != ArcsecDatalinkLayer::DEC_IN_PROGRESS) {
triggerEvent(STR_HELPER_DEC_ERROR, result, failParameter);
return DECODING_ERROR;
}
if (replyTimeout.hasTimedOut()) {
triggerEvent(STR_COM_REPLY_TIMEOUT, failParameter, replyTimeout.getTimeoutMs());
return RECEPTION_TIMEOUT;
}
TaskFactory::delayTask(nextDelayMs);
if (nextDelayMs < 32) {
nextDelayMs *= 2;
}
}
}
void StrComHandler::resetReplyHandlingState() {
serial::flushRxBuf(serialPort);
datalinkLayer.reset();
}