#include "StrImageLoader.h" #include StrImageLoader::StrImageLoader(object_id_t objectId) { } StrImageLoader::~StrImageLoader() { } ReturnValue_t StrImageLoader::initialize() { sdcMan = SdCardManager::instance(); if (sdcMan == nullptr) { sif::warning << "StrImageLoader::initialize: Invalid SD Card Manager" << std::endl; } } 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 = uploadImage(); if (result == RETURN_OK){ triggerEvent(IMAGE_UPLOAD_SUCCESSFUL); } 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(); } ReturnValue_t StrImageLoader::uploadImage() { ReturnValue_t result = RETURN_OK; size_t receivedDataLen = 0; uint8_t *receivedData = nullptr; size_t bytesLeft = 0; uint32_t readSize = 0; uint32_t imageSize = 0; struct UploadActionRequest uploadReq; uploadReq.position = 0; uploadReq.data = {0}; if (not std::filesystem::exists(uploadImage)) { triggerEvent(IMAGE_FILE_NOT_EXISTS, uploadReq.position); state = State::IDLE; return RETURN_FAILED; } std::ifstream file(uploadImage, std::ifstream::binary); file.seekg(0, file.end); imageSize = file.tellg(); file.seekg(uploadReq.position * SIZE_IMAGE_PART, file.beg); while(uploadReq.position * SIZE_IMAGE_PART < imageSize) { result = } uint32_t remainder = imageSize - uploadReq.position * SIZE_IMAGE_PART; file.read(reinterpret_cast(uploadReq.data), remainder); uploadReq.position++; datalinkLayer.encodeFrame(uploadReq.data, remainder); 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; } result = ArcsecDatalinkLayer::DEC_IN_PROGRESS; while(result == 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); } result = datalinkLayer.decodeFrame(receivedData, receivedDataLen, &bytesLeft); if (bytesLeft != 0) { // This should never happen triggerEvent(UPLOAD_COM_ERROR, result, uploadReq.position); return RETURN_FAILED; } } if (remainingCommands == 1) { dataLen = imageSize - file.tellg(); } else { dataLen = SIZE_IMAGE_PART; } size_t size = 0; size_t maxSize = sizeof(position); uint8_t* commandBufferPtr = tmpCommandBuffer; uint8_t** buffer = &commandBufferPtr; SerializeAdapter::serialize(&position, buffer, &size, maxSize, SerializeIF::Endianness::BIG); file.read(reinterpret_cast(uploadReq.data), dataLen); file.close(); arc_pack_upload_action_req() communicationInterface->requestReceiveMessage(comCookie, ) }