eive-obsw/bsp_q7s/devices/startracker/StrImageLoader.cpp

214 lines
7.4 KiB
C++

#include "StrImageLoader.h"
#include <fstream>
#include <filesystem>
StrImageLoader::StrImageLoader(object_id_t objectId) : SystemObject(objectId){
}
StrImageLoader::~StrImageLoader() {
}
ReturnValue_t StrImageLoader::initialize() {
sdcMan = SdCardManager::instance();
if (sdcMan == nullptr) {
sif::warning << "StrImageLoader::initialize: Invalid SD Card Manager" << std::endl;
return RETURN_FAILED;
}
return RETURN_OK;
}
ReturnValue_t StrImageLoader::performOperation(uint8_t operationCode) {
ReturnValue_t result = RETURN_OK;
semaphore.acquire();
while(true) {
switch(internalState) {
case InternalState::IDLE: {
semaphore.acquire();
break;
}
case InternalState::UPLOAD_IMAGE: {
result = performImageUpload();
if (result == RETURN_OK){
triggerEvent(IMAGE_UPLOAD_SUCCESSFUL);
}
internalState = InternalState::IDLE;
break;
}
case InternalState::DOWNLOAD_IMAGE:
break;
}
}
}
void StrImageLoader::setComIF(DeviceCommunicationIF* communicationInterface_) {
communicationInterface = communicationInterface_;
}
void StrImageLoader::setComCookie(CookieIF* comCookie_) {
comCookie = comCookie_;
}
ReturnValue_t StrImageLoader::startImageUpload(std::string image) {
// Check if file is stored on SD card and if associated SD card is mounted
if (image.substr(0, sizeof(SdCardManager::SD_0_MOUNT_POINT))
== std::string(SdCardManager::SD_0_MOUNT_POINT)) {
if (!sdcMan->isSdCardMounted(sd::SLOT_0)) {
sif::warning << "StrImageLoader::getImageLocation: SD card 0 not mounted" << std::endl;
return SD_NOT_MOUNTED;
}
} else if (image.substr(0, sizeof(SdCardManager::SD_1_MOUNT_POINT))
== std::string(SdCardManager::SD_1_MOUNT_POINT)) {
if (!sdcMan->isSdCardMounted(sd::SLOT_0)) {
sif::warning << "StrImageLoader::getImageLocation: SD card 1 not mounted" << std::endl;
return SD_NOT_MOUNTED;
}
}
uploadImage = image;
if(not std::filesystem::exists(uploadImage)) {
return FILE_NOT_EXISTS;
}
internalState = InternalState::UPLOAD_IMAGE;
semaphore.release();
return RETURN_OK;
}
ReturnValue_t StrImageLoader::startImageDownload() {
return RETURN_OK;
}
ReturnValue_t StrImageLoader::performImageUpload() {
ReturnValue_t result = RETURN_OK;
uint32_t imageSize = 0;
struct UploadActionRequest uploadReq;
uploadReq.position = 0;
std::memset(&uploadReq.data, 0, sizeof(uploadReq.data));
if (not std::filesystem::exists(uploadImage)) {
triggerEvent(IMAGE_FILE_NOT_EXISTS, uploadReq.position);
internalState = InternalState::IDLE;
return RETURN_FAILED;
}
std::ifstream file(uploadImage, 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();
// Set position of next character to beginning of file input stream
file.seekg(0, file.beg);
if (imageSize >= SIZE_IMAGE_PART) {
file.read(reinterpret_cast<char*>(uploadReq.data), SIZE_IMAGE_PART);
}
else {
file.read(reinterpret_cast<char*>(uploadReq.data), imageSize);
}
while(uploadReq.position * SIZE_IMAGE_PART < imageSize) {
result = uploadSendAndRead(&uploadReq);
if (result != RETURN_OK) {
return RETURN_FAILED;
}
result = checkUploadReply();
if (result != RETURN_OK) {
return result;
}
uploadReq.position++;
file.seekg(uploadReq.position * SIZE_IMAGE_PART, file.beg);
}
std::memset(uploadReq.data, 0, sizeof(uploadReq.data));
uint32_t remainder = imageSize - uploadReq.position * SIZE_IMAGE_PART;
file.read(reinterpret_cast<char*>(uploadReq.data), remainder);
file.close();
uploadReq.position++;
result = uploadSendAndRead(&uploadReq);
if (result != RETURN_OK) {
return RETURN_FAILED;
}
result = checkUploadReply();
if (result != RETURN_OK) {
return result;
}
return RETURN_OK;
}
ReturnValue_t StrImageLoader::uploadSendAndRead(struct UploadActionRequest* uploadReq) {
ReturnValue_t result = RETURN_OK;
ReturnValue_t decResult = RETURN_OK;
size_t receivedDataLen = 0;
uint8_t *receivedData = nullptr;
size_t bytesLeft = 0;
uint32_t size = 0;
uint32_t missedReplies = 0;
arc_pack_upload_action_req(uploadReq, commandBuffer, &size);
datalinkLayer.encodeFrame(commandBuffer, size);
result = communicationInterface->sendMessage(comCookie, datalinkLayer.getEncodedFrame(),
datalinkLayer.getEncodedLength());
if (result != RETURN_OK) {
sif::warning << "StrImageLoader::uploadImage: Failed to send upload packet" << std::endl;
triggerEvent(SENDING_UPLOAD_PACKET_FAILED, result, uploadReq->position);
return RETURN_FAILED;
}
decResult = ArcsecDatalinkLayer::DEC_IN_PROGRESS;
while (decResult == ArcsecDatalinkLayer::DEC_IN_PROGRESS) {
result = communicationInterface->requestReceiveMessage(comCookie,
StarTracker::MAX_FRAME_SIZE * 2 + 2);
if (result != RETURN_OK) {
sif::warning << "StrImageLoader::uploadImage: Failed to request reply" << std::endl;
triggerEvent(UPLOAD_REQUESTING_MSG_FAILED, result, uploadReq->position);
return RETURN_FAILED;
}
result = communicationInterface->readReceivedMessage(comCookie, &receivedData, &receivedDataLen);
if (result != RETURN_OK) {
sif::warning << "StrImageLoader::uploadImage: Failed to read received message" << std::endl;
triggerEvent(UPLOAD_READING_REPLY_FAILED, result, uploadReq->position);
return RETURN_FAILED;
}
if (receivedDataLen == 0 && missedReplies < MAX_POLLS) {
missedReplies++;
continue;
}
else if ((receivedDataLen == 0) && (missedReplies >= MAX_POLLS)) {
triggerEvent(NO_REPLY, uploadReq->position);
return RETURN_FAILED;
}
else {
missedReplies = 0;
}
decResult = datalinkLayer.decodeFrame(receivedData, receivedDataLen, &bytesLeft);
if (bytesLeft != 0) {
// This should never happen
sif::warning << "StrImageLoader::uploadSendAndRead: Bytes left after decoding" << std::endl;
triggerEvent(UPLOAD_COM_ERROR, result, uploadReq->position);
return RETURN_FAILED;
}
}
if (decResult != RETURN_OK) {
triggerEvent(DEC_ERROR, decResult, uploadReq->position);
return RETURN_FAILED;
}
return RETURN_OK;
}
ReturnValue_t StrImageLoader::checkUploadReply() {
uint8_t type = datalinkLayer.getReplyFrameType();
if (type != TMTC_ACTIONREPLY) {
sif::warning << "StrImageLoader::checkUploadReply: Received invalid upload reply"
<< std::endl;
triggerEvent(INVALID_TYPE_ID);
return RETURN_FAILED;
}
uint8_t status = datalinkLayer.getStatusField();
if (status != ArcsecDatalinkLayer::STATUS_OK) {
triggerEvent(STATUS_ERROR);
sif::warning << "StrImageLoader::checkUploadReply: Status failure" << std::endl;
return RETURN_FAILED;
}
return RETURN_OK;
}