2021-12-21 15:46:09 +01:00
|
|
|
#include "StrHelper.h"
|
|
|
|
|
2022-10-25 11:31:06 +02:00
|
|
|
#include <fsfw/filesystem/HasFileSystemIF.h>
|
|
|
|
|
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"
|
|
|
|
#include "linux/devices/devicedefinitions/StarTrackerDefinitions.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"
|
2022-03-04 15:14:02 +01:00
|
|
|
#include "mission/utility/Timestamp.h"
|
2021-11-30 16:01:02 +01:00
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
StrHelper::StrHelper(object_id_t objectId) : SystemObject(objectId) {}
|
2021-11-30 16:01:02 +01:00
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
StrHelper::~StrHelper() {}
|
2021-11-30 16:01:02 +01:00
|
|
|
|
2021-12-21 15:46:09 +01:00
|
|
|
ReturnValue_t StrHelper::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
|
|
|
}
|
|
|
|
|
2021-12-21 15:46:09 +01:00
|
|
|
ReturnValue_t StrHelper::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
|
|
|
semaphore.acquire();
|
|
|
|
while (true) {
|
|
|
|
switch (internalState) {
|
|
|
|
case InternalState::IDLE: {
|
|
|
|
semaphore.acquire();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case InternalState::UPLOAD_IMAGE: {
|
|
|
|
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
|
|
|
internalState = InternalState::IDLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case InternalState::DOWNLOAD_IMAGE: {
|
|
|
|
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
|
|
|
internalState = InternalState::IDLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case InternalState::FLASH_READ: {
|
|
|
|
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
|
|
|
internalState = InternalState::IDLE;
|
|
|
|
break;
|
|
|
|
}
|
2022-02-01 11:45:22 +01:00
|
|
|
case InternalState::FIRMWARE_UPDATE: {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
internalState = InternalState::IDLE;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-21 15:46:09 +01:00
|
|
|
ReturnValue_t StrHelper::setComIF(DeviceCommunicationIF* communicationInterface_) {
|
2022-11-10 17:32:11 +01:00
|
|
|
uartComIF = dynamic_cast<SerialComIF*>(communicationInterface_);
|
2022-01-17 15:58:27 +01:00
|
|
|
if (uartComIF == nullptr) {
|
|
|
|
sif::warning << "StrHelper::initialize: Invalid uart com if" << std::endl;
|
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-11-30 16:01:02 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
void StrHelper::setComCookie(CookieIF* comCookie_) { comCookie = comCookie_; }
|
2021-11-30 16:01:02 +01:00
|
|
|
|
2021-12-30 13:31:34 +01:00
|
|
|
ReturnValue_t StrHelper::startImageUpload(std::string fullname) {
|
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;
|
|
|
|
}
|
2022-01-19 11:52:06 +01:00
|
|
|
internalState = 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
|
|
|
}
|
|
|
|
|
2021-12-30 13:31:34 +01:00
|
|
|
ReturnValue_t StrHelper::startImageDownload(std::string path) {
|
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;
|
|
|
|
internalState = InternalState::DOWNLOAD_IMAGE;
|
|
|
|
terminate = false;
|
|
|
|
semaphore.release();
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-12-09 15:02:58 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
void StrHelper::stopProcess() { terminate = true; }
|
2021-12-10 10:07:23 +01:00
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
void StrHelper::setDownloadImageName(std::string filename) { downloadImage.filename = filename; }
|
2021-12-11 11:56:47 +01:00
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
void StrHelper::setFlashReadFilename(std::string filename) { flashRead.filename = filename; }
|
2021-12-22 16:06:30 +01:00
|
|
|
|
2022-02-01 11:45:22 +01:00
|
|
|
ReturnValue_t StrHelper::startFirmwareUpdate(std::string fullname) {
|
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);
|
2022-02-01 11:45:22 +01:00
|
|
|
internalState = 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
|
|
|
}
|
|
|
|
|
2022-02-25 14:24:51 +01:00
|
|
|
ReturnValue_t StrHelper::startFlashRead(std::string path, uint8_t startRegion, uint32_t length) {
|
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;
|
|
|
|
internalState = InternalState::FLASH_READ;
|
|
|
|
semaphore.release();
|
|
|
|
terminate = false;
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-12-22 16:06:30 +01:00
|
|
|
}
|
|
|
|
|
2022-02-27 15:48:42 +01:00
|
|
|
void StrHelper::disableTimestamping() { timestamping = false; }
|
2022-02-24 13:47:52 +01:00
|
|
|
|
2022-02-27 15:48:42 +01:00
|
|
|
void StrHelper::enableTimestamping() { timestamping = true; }
|
2022-02-24 13:47:52 +01:00
|
|
|
|
2021-12-21 15:46:09 +01:00
|
|
|
ReturnValue_t StrHelper::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;
|
2022-04-13 11:56:37 +02:00
|
|
|
std::string image = Filenaming::generateAbsoluteFilename(downloadImage.path,
|
|
|
|
downloadImage.filename, timestamping);
|
2022-02-24 13:47:52 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
arc_pack_download_action_req(&downloadReq, commandBuffer, &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) {
|
|
|
|
uartComIF->flushUartRxBuffer(comCookie);
|
|
|
|
retries++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
return result;
|
2021-12-09 15:02:58 +01:00
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
result = checkActionReply();
|
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) {
|
|
|
|
uartComIF->flushUartRxBuffer(comCookie);
|
|
|
|
retries++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
uartComIF->flushUartRxBuffer(comCookie);
|
|
|
|
retries++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
file.write(reinterpret_cast<const char*>(datalinkLayer.getReply() + IMAGE_DATA_OFFSET),
|
2022-02-14 11:28:15 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-21 15:46:09 +01:00
|
|
|
ReturnValue_t StrHelper::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)) {
|
|
|
|
triggerEvent(STR_HELPER_FILE_NOT_EXISTS, static_cast<uint32_t>(internalState));
|
|
|
|
internalState = InternalState::IDLE;
|
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);
|
|
|
|
// 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-02-23 18:15:34 +01:00
|
|
|
file.close();
|
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);
|
2021-12-09 15:02:58 +01:00
|
|
|
arc_pack_upload_action_req(&uploadReq, commandBuffer, &size);
|
|
|
|
result = sendAndRead(size, uploadReq.position);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-23 18:15:34 +01:00
|
|
|
file.close();
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-12-06 19:36:21 +01:00
|
|
|
}
|
2021-12-29 20:33:20 +01:00
|
|
|
result = checkActionReply();
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-23 18:15:34 +01:00
|
|
|
file.close();
|
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++;
|
|
|
|
}
|
|
|
|
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++;
|
|
|
|
arc_pack_upload_action_req(&uploadReq, commandBuffer, &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
|
|
|
}
|
|
|
|
result = checkActionReply();
|
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
|
|
|
}
|
|
|
|
|
2022-02-01 11:45:22 +01:00
|
|
|
ReturnValue_t StrHelper::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
|
|
|
}
|
|
|
|
|
2021-12-21 15:46:09 +01:00
|
|
|
ReturnValue_t StrHelper::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;
|
2022-02-14 11:28:15 +01:00
|
|
|
uint32_t bytesWritten = 0;
|
2022-01-17 15:58:27 +01:00
|
|
|
uint32_t fileSize = 0;
|
|
|
|
struct WriteActionRequest req;
|
|
|
|
if (not std::filesystem::exists(flashWrite.fullname)) {
|
|
|
|
triggerEvent(STR_HELPER_FILE_NOT_EXISTS, static_cast<uint32_t>(internalState));
|
|
|
|
internalState = InternalState::IDLE;
|
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);
|
|
|
|
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;
|
|
|
|
bytesWritten = 0;
|
|
|
|
req.region = flashWrite.firstRegion;
|
|
|
|
req.length = CHUNK_SIZE;
|
|
|
|
for (uint32_t idx = 0; idx < fileChunks; idx++) {
|
2022-01-17 15:58:27 +01:00
|
|
|
if (terminate) {
|
2022-02-23 18:15:34 +01:00
|
|
|
file.close();
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-12-21 15:46:09 +01:00
|
|
|
}
|
2022-02-14 11:28:15 +01:00
|
|
|
file.seekg(idx * CHUNK_SIZE, file.beg);
|
|
|
|
file.read(reinterpret_cast<char*>(req.data), CHUNK_SIZE);
|
|
|
|
if (bytesWritten + CHUNK_SIZE > FLASH_REGION_SIZE) {
|
2022-02-23 18:15:34 +01:00
|
|
|
req.region++;
|
|
|
|
bytesWritten = 0;
|
2022-02-14 11:28:15 +01:00
|
|
|
}
|
|
|
|
req.address = bytesWritten;
|
2021-12-21 15:46:09 +01:00
|
|
|
arc_pack_write_action_req(&req, commandBuffer, &size);
|
|
|
|
result = sendAndRead(size, req.address);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-23 18:15:34 +01:00
|
|
|
file.close();
|
2022-02-04 13:06:56 +01:00
|
|
|
return result;
|
2021-12-21 15:46:09 +01:00
|
|
|
}
|
2022-02-23 18:15:34 +01:00
|
|
|
result = checkActionReply();
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-23 18:15:34 +01:00
|
|
|
file.close();
|
2022-01-17 15:58:27 +01:00
|
|
|
return result;
|
|
|
|
}
|
2022-02-14 11:28:15 +01:00
|
|
|
bytesWritten += CHUNK_SIZE;
|
2022-03-01 17:17:15 +01:00
|
|
|
#if OBSW_DEBUG_STARTRACKER == 1
|
|
|
|
progressPrinter.print(idx * CHUNK_SIZE);
|
|
|
|
#endif /* OBSW_DEBUG_STARTRACKER == 1 */
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2022-02-14 11:28:15 +01:00
|
|
|
uint32_t remainingBytes = fileSize - fileChunks * CHUNK_SIZE;
|
2022-02-23 18:15:34 +01:00
|
|
|
file.seekg((fileChunks - 1) * CHUNK_SIZE, file.beg);
|
2022-01-17 15:58:27 +01:00
|
|
|
file.read(reinterpret_cast<char*>(req.data), remainingBytes);
|
|
|
|
file.close();
|
2022-02-14 11:28:15 +01:00
|
|
|
if (bytesWritten + CHUNK_SIZE > FLASH_REGION_SIZE) {
|
2022-02-23 18:15:34 +01:00
|
|
|
req.region++;
|
|
|
|
bytesWritten = 0;
|
2022-02-14 11:28:15 +01:00
|
|
|
}
|
|
|
|
req.address = bytesWritten;
|
|
|
|
req.length = remainingBytes;
|
2022-03-01 17:17:15 +01:00
|
|
|
bytesWritten += remainingBytes;
|
2022-01-17 15:58:27 +01:00
|
|
|
arc_pack_write_action_req(&req, commandBuffer, &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;
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2022-02-23 18:15:34 +01:00
|
|
|
result = checkActionReply();
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
return result;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-12-22 11:14:27 +01:00
|
|
|
ReturnValue_t StrHelper::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
|
|
|
}
|
|
|
|
arc_pack_read_action_req(&req, commandBuffer, &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) {
|
|
|
|
uartComIF->flushUartRxBuffer(comCookie);
|
|
|
|
retries++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
return result;
|
2021-12-22 11:14:27 +01:00
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
result = checkActionReply();
|
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) {
|
|
|
|
uartComIF->flushUartRxBuffer(comCookie);
|
|
|
|
retries++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
return result;
|
2021-12-22 11:14:27 +01:00
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
file.write(reinterpret_cast<const char*>(datalinkLayer.getReply() + FLASH_READ_DATA_OFFSET),
|
|
|
|
req.length);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-23 18:15:34 +01:00
|
|
|
ReturnValue_t StrHelper::sendAndRead(size_t size, uint32_t parameter, uint32_t delayMs) {
|
2022-08-24 17:27:47 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
|
|
|
ReturnValue_t decResult = returnvalue::OK;
|
2022-01-17 15:58:27 +01:00
|
|
|
size_t receivedDataLen = 0;
|
|
|
|
uint8_t* receivedData = nullptr;
|
|
|
|
size_t bytesLeft = 0;
|
|
|
|
uint32_t missedReplies = 0;
|
|
|
|
datalinkLayer.encodeFrame(commandBuffer, size);
|
|
|
|
result = uartComIF->sendMessage(comCookie, datalinkLayer.getEncodedFrame(),
|
|
|
|
datalinkLayer.getEncodedLength());
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
sif::warning << "StrHelper::sendAndRead: Failed to send packet" << std::endl;
|
|
|
|
triggerEvent(STR_HELPER_SENDING_PACKET_FAILED, result, parameter);
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
|
|
|
decResult = ArcsecDatalinkLayer::DEC_IN_PROGRESS;
|
|
|
|
while (decResult == ArcsecDatalinkLayer::DEC_IN_PROGRESS) {
|
2022-02-23 18:15:34 +01:00
|
|
|
Countdown delay(delayMs);
|
|
|
|
delay.resetTimer();
|
|
|
|
while (delay.isBusy()) {
|
|
|
|
}
|
2022-02-04 13:06:56 +01:00
|
|
|
result = uartComIF->requestReceiveMessage(comCookie, startracker::MAX_FRAME_SIZE * 2 + 2);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
sif::warning << "StrHelper::sendAndRead: Failed to request reply" << std::endl;
|
|
|
|
triggerEvent(STR_HELPER_REQUESTING_MSG_FAILED, result, parameter);
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-12-02 08:05:33 +01:00
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
result = uartComIF->readReceivedMessage(comCookie, &receivedData, &receivedDataLen);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
sif::warning << "StrHelper::sendAndRead: Failed to read received message" << std::endl;
|
|
|
|
triggerEvent(STR_HELPER_READING_REPLY_FAILED, result, parameter);
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
|
|
|
if (receivedDataLen == 0 && missedReplies < MAX_POLLS) {
|
|
|
|
missedReplies++;
|
|
|
|
continue;
|
|
|
|
} else if ((receivedDataLen == 0) && (missedReplies >= MAX_POLLS)) {
|
|
|
|
triggerEvent(STR_HELPER_NO_REPLY, parameter);
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-01-17 15:58:27 +01:00
|
|
|
} else {
|
|
|
|
missedReplies = 0;
|
|
|
|
}
|
|
|
|
decResult = datalinkLayer.decodeFrame(receivedData, receivedDataLen, &bytesLeft);
|
|
|
|
if (bytesLeft != 0) {
|
|
|
|
// This should never happen
|
|
|
|
sif::warning << "StrHelper::sendAndRead: Bytes left after decoding" << std::endl;
|
|
|
|
triggerEvent(STR_HELPER_COM_ERROR, result, parameter);
|
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
|
|
|
if (decResult != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
triggerEvent(STR_HELPER_DEC_ERROR, decResult, parameter);
|
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-06 19:36:21 +01:00
|
|
|
}
|
2021-12-02 08:05:33 +01:00
|
|
|
|
2021-12-29 20:33:20 +01:00
|
|
|
ReturnValue_t StrHelper::checkActionReply() {
|
2022-01-17 15:58:27 +01:00
|
|
|
uint8_t type = datalinkLayer.getReplyFrameType();
|
|
|
|
if (type != TMTC_ACTIONREPLY) {
|
|
|
|
sif::warning << "StrHelper::checkActionReply: Received reply with invalid type ID" << std::endl;
|
|
|
|
return INVALID_TYPE_ID;
|
|
|
|
}
|
|
|
|
uint8_t status = datalinkLayer.getStatusField();
|
|
|
|
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
|
|
|
|
2021-12-21 15:46:09 +01:00
|
|
|
ReturnValue_t StrHelper::checkReplyPosition(uint32_t expectedPosition) {
|
2022-01-17 15:58:27 +01:00
|
|
|
uint32_t receivedPosition = 0;
|
|
|
|
std::memcpy(&receivedPosition, datalinkLayer.getReply() + POS_OFFSET, sizeof(receivedPosition));
|
|
|
|
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
|
2021-12-21 15:46:09 +01:00
|
|
|
ReturnValue_t StrHelper::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
|
|
|
|
2022-02-04 13:06:56 +01:00
|
|
|
ReturnValue_t StrHelper::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];
|
|
|
|
arc_pack_unlock_action_req(&unlockReq, commandBuffer, &size);
|
|
|
|
sendAndRead(size, unlockReq.region);
|
|
|
|
result = checkActionReply();
|
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;
|
|
|
|
arc_pack_erase_action_req(&eraseReq, commandBuffer, &size);
|
|
|
|
result = sendAndRead(size, eraseReq.region, FLASH_ERASE_DELAY);
|
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
|
|
|
}
|