/************************************************************************************
 * This file was auto-generated by CIDEA                           .                *
 * Please do not modify the contents of this file manually.                         *
 ***********************************************************************************/

#include "adcs_common.h"
#include "exo_osal_mem_management.h"

int16_t COMMON_GetTlmLen(COMMON_Telemetry_t tlm)
{
    switch(tlm)
    {
        case BlockChecksum:
            return 2;
        case BootIndexStatus:
            return 2;
        case Cache:
            return 1;
        case CommsStatus:
            return 6;
        case DownloadBlockReady:
            return 5;
        case EdacErrors:
            return 6;
        case ExtendedIdentification:
            return 6;
        case FileDownload:
            return 22;
        case FileInfo:
            return 12;
        case HoleMap1:
            return 16;
        case HoleMap2:
            return 16;
        case HoleMap3:
            return 16;
        case HoleMap4:
            return 16;
        case HoleMap5:
            return 16;
        case HoleMap6:
            return 16;
        case HoleMap7:
            return 16;
        case HoleMap8:
            return 16;
        case Identification:
            return 8;
        case InitializeUploadComplete:
            return 1;
        case LastLogEvent:
            return 6;
        case LatchupErrors:
            return 6;
        case SdProgress:
            return 1;
        case SramScrubSettings:
            return 2;
        case TelecommandAcknowledge:
            return 4;
        case UnixTime:
            return 6;
        case UnixTimeSave:
            return 2;
        case UploadBlockComplete:
            return 1;
        default:
            return -1;
    }
}
CUBEADCS_Result_t COMMON_ExtractBlockChecksumTlm(uint8_t* rxBuffer, U16* checksum)
{
    if (checksum == 0)
        return PointerIsNull;

    *checksum = *( (U16*) (rxBuffer + 0) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractBootIndexStatusTlm(uint8_t* rxBuffer, COMMON_BootIndexStatus_t* returnVal)
{
    uint8_t enumVal;

    if (returnVal == 0)
        return PointerIsNull;

    enumVal = rxBuffer[0];
    returnVal->programIndex = (COMMON_BootProgramsList_t) enumVal;

    enumVal = rxBuffer[1];
    returnVal->bootStatus = (COMMON_BootStatus_t) enumVal;

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractCacheTlm(uint8_t* rxBuffer, Boolean* enabled)
{
    if (enabled == 0)
        return PointerIsNull;

    *enabled = (rxBuffer[0] & 0x01) >> 0;

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractCommsStatusTlm(uint8_t* rxBuffer, COMMON_CommsStatus_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->tcCounter = *( (U16*) (rxBuffer + 0) );

    returnVal->tlmCounter = *( (U16*) (rxBuffer + 2) );

    returnVal->uartBufferOverrun = (rxBuffer[4] & 0x01) >> 0;

    returnVal->uartProtocolError = (rxBuffer[4] & 0x02) >> 1;

    returnVal->uartMsgIncomplete = (rxBuffer[4] & 0x04) >> 2;

    returnVal->i2CTelemetryError = (rxBuffer[4] & 0x08) >> 3;

    returnVal->i2CBufferError = (rxBuffer[4] & 0x10) >> 4;

    returnVal->cANBufferError = (rxBuffer[4] & 0x20) >> 5;


    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractDownloadBlockReadyTlm(uint8_t* rxBuffer, COMMON_DownloadBlockReady_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->ready = (rxBuffer[0] & 0x01) >> 0;

    returnVal->parameterError = (rxBuffer[0] & 0x02) >> 1;


    returnVal->checksum = *( (U16*) (rxBuffer + 1) );

    returnVal->length = *( (U16*) (rxBuffer + 3) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractEdacErrorsTlm(uint8_t* rxBuffer, COMMON_EdacErrors_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->singleSRAMSEUs = *( (U16*) (rxBuffer + 0) );

    returnVal->doubleSRAMSEUs = *( (U16*) (rxBuffer + 2) );

    returnVal->multiSRAMSEUs = *( (U16*) (rxBuffer + 4) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractExtendedIdentificationTlm(uint8_t* rxBuffer, COMMON_ExtendedIdentification_t* returnVal)
{
    uint8_t enumVal;

    if (returnVal == 0)
        return PointerIsNull;

    enumVal = (rxBuffer[0] >> 0) & 15;
    returnVal->mcuResetCause = (COMMON_ResetCause_t) enumVal;

    enumVal = (rxBuffer[0] >> 4) & 15;
    returnVal->bootCause = (COMMON_BootCause_t) enumVal;

    returnVal->bootCounter = *( (U16*) (rxBuffer + 1) );

    enumVal = rxBuffer[3];
    returnVal->runningProgramIndex = (COMMON_BootProgramsList_t) enumVal;

    returnVal->firmwareMajorVersion = *( (U8*) (rxBuffer + 4) );

    returnVal->firmwareMinorVersion = *( (U8*) (rxBuffer + 5) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractFileDownloadTlm(uint8_t* rxBuffer, COMMON_FileDownload_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->packetNo = *( (U16*) (rxBuffer + 0) );

    memcpy(returnVal->fileBytes, rxBuffer + 2, 20);

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractFileInfoTlm(uint8_t* rxBuffer, COMMON_FileInfo_t* returnVal)
{
    uint8_t enumVal;

    if (returnVal == 0)
        return PointerIsNull;

    enumVal = (rxBuffer[0] >> 0) & 15;
    returnVal->fileType = (COMMON_FileType_t) enumVal;

    returnVal->busyUpdating = (rxBuffer[0] & 0x10) >> 4;


    returnVal->fileCtr = *( (U8*) (rxBuffer + 1) );

    returnVal->size = *( (U32*) (rxBuffer + 2) );

    returnVal->unixTime = *( (U32*) (rxBuffer + 6) );

    returnVal->checksum = *( (U16*) (rxBuffer + 10) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractHoleMap1Tlm(uint8_t* rxBuffer, uint8_t* holeMap)
{
    if (holeMap == 0)
        return PointerIsNull;

    memcpy(holeMap, rxBuffer + 0, 16);

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractHoleMap2Tlm(uint8_t* rxBuffer, uint8_t* holeMap)
{
    if (holeMap == 0)
        return PointerIsNull;

    memcpy(holeMap, rxBuffer + 0, 16);

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractHoleMap3Tlm(uint8_t* rxBuffer, uint8_t* holeMap)
{
    if (holeMap == 0)
        return PointerIsNull;

    memcpy(holeMap, rxBuffer + 0, 16);

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractHoleMap4Tlm(uint8_t* rxBuffer, uint8_t* holeMap)
{
    if (holeMap == 0)
        return PointerIsNull;

    memcpy(holeMap, rxBuffer + 0, 16);

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractHoleMap5Tlm(uint8_t* rxBuffer, uint8_t* holeMap)
{
    if (holeMap == 0)
        return PointerIsNull;

    memcpy(holeMap, rxBuffer + 0, 16);

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractHoleMap6Tlm(uint8_t* rxBuffer, uint8_t* holeMap)
{
    if (holeMap == 0)
        return PointerIsNull;

    memcpy(holeMap, rxBuffer + 0, 16);

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractHoleMap7Tlm(uint8_t* rxBuffer, uint8_t* holeMap)
{
    if (holeMap == 0)
        return PointerIsNull;

    memcpy(holeMap, rxBuffer + 0, 16);

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractHoleMap8Tlm(uint8_t* rxBuffer, uint8_t* holeMap)
{
    if (holeMap == 0)
        return PointerIsNull;

    memcpy(holeMap, rxBuffer + 0, 16);

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractIdentificationTlm(uint8_t* rxBuffer, COMMON_Identification_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->nodeType = *( (U8*) (rxBuffer + 0) );

    returnVal->interfaceVersion = *( (U8*) (rxBuffer + 1) );

    returnVal->firmwareMajorVersion = *( (U8*) (rxBuffer + 2) );

    returnVal->firmwareMinorVersion = *( (U8*) (rxBuffer + 3) );

    returnVal->runtimeSeconds = *( (U16*) (rxBuffer + 4) );

    returnVal->runtimeMilliseconds = *( (U16*) (rxBuffer + 6) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractInitializeUploadCompleteTlm(uint8_t* rxBuffer, Boolean* busy)
{
    if (busy == 0)
        return PointerIsNull;

    *busy = (rxBuffer[0] & 0x01) >> 0;

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractLastLogEventTlm(uint8_t* rxBuffer, COMMON_LastLogEvent_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->unixTime = *( (U32*) (rxBuffer + 0) );

    returnVal->eventId = *( (U8*) (rxBuffer + 4) );

    returnVal->eventParam = *( (U8*) (rxBuffer + 5) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractLatchupErrorsTlm(uint8_t* rxBuffer, COMMON_LatchupErrors_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->sRAM1SELs = *( (U16*) (rxBuffer + 0) );

    returnVal->sRAM2SELs = *( (U16*) (rxBuffer + 2) );


    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractSdProgressTlm(uint8_t* rxBuffer, COMMON_SdProgress_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->formatBusy = (rxBuffer[0] & 0x01) >> 0;

    returnVal->eraseAllBusy = (rxBuffer[0] & 0x02) >> 1;

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractSramScrubSettingsTlm(uint8_t* rxBuffer, U16* scrubSize)
{
    if (scrubSize == 0)
        return PointerIsNull;

    *scrubSize = *( (U16*) (rxBuffer + 0) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractTelecommandAcknowledgeTlm(uint8_t* rxBuffer, COMMON_TelecommandAcknowledge_t* returnVal)
{
    uint8_t enumVal;

    if (returnVal == 0)
        return PointerIsNull;

    returnVal->lastTCID = *( (U8*) (rxBuffer + 0) );

    returnVal->processedFlag = (rxBuffer[1] & 0x01) >> 0;


    enumVal = rxBuffer[2];
    returnVal->tCerrorStatus  = (COMMON_TcErrorReason_t) enumVal;

    returnVal->tCParameterErrorIndex = *( (U8*) (rxBuffer + 3) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractUnixTimeTlm(uint8_t* rxBuffer, COMMON_UnixTime_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->time = *( (U32*) (rxBuffer + 0) );

    returnVal->milliSec = *( (U16*) (rxBuffer + 4) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractUnixTimeSaveTlm(uint8_t* rxBuffer, COMMON_UnixTimeSave_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->saveNow = (rxBuffer[0] & 0x01) >> 0;

    returnVal->saveOnUpdate = (rxBuffer[0] & 0x02) >> 1;

    returnVal->savePeriodic = (rxBuffer[0] & 0x04) >> 2;


    returnVal->period = *( (U8*) (rxBuffer + 1) );

    return CubeLibOk;
}

CUBEADCS_Result_t COMMON_ExtractUploadBlockCompleteTlm(uint8_t* rxBuffer, COMMON_UploadBlockComplete_t* returnVal)
{
    if (returnVal == 0)
        return PointerIsNull;

    returnVal->busy = (rxBuffer[0] & 0x01) >> 0;

    returnVal->error = (rxBuffer[0] & 0x02) >> 1;

    return CubeLibOk;
}


uint16_t COMMON_FormatAdvanceFileListPtrCmd(uint8_t* tcBuffer)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 113;


    return 1;
}

uint16_t COMMON_FormatAdvanceLogPtrCmd(uint8_t* tcBuffer)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 5;


    return 1;
}

uint16_t COMMON_FormatCacheCmd(uint8_t* tcBuffer, Boolean enabled)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 3;

    tcBuffer[1] = 
        (enabled << 0);

    return 2;
}

uint16_t COMMON_FormatDownloadBurstCmd(uint8_t* tcBuffer, U8 messageLength, Boolean ignoreHoleMap)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 119;

    tcBuffer[2] = 
        (ignoreHoleMap << 0);
    *( (U8*)(tcBuffer + 1) ) = messageLength;

    return 3;
}

uint16_t COMMON_FormatEraseFileCmd(uint8_t* tcBuffer, COMMON_FileType_t fileType, U8 fileCtr, Boolean eraseAll)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 108;

    tcBuffer[1] = 
        (fileType << 0);
    tcBuffer[3] = 
        (eraseAll << 0);
    *( (U8*)(tcBuffer + 2) ) = fileCtr;

    return 4;
}

uint16_t COMMON_FormatFileUploadCmd(uint8_t* tcBuffer, U16 packetNo, U8* fileBytes)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 115;

    *( (U16*)(tcBuffer + 1) ) = packetNo;
    memcpy(tcBuffer + 3,fileBytes,20);

    return 23;
}

uint16_t COMMON_FormatFinalizeUploadBlockCmd(uint8_t* tcBuffer, COMMON_FileUploadDestination_t destination, U32 offset, U16 blockLen)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 116;

    tcBuffer[1] = 
        (destination << 0);
    *( (U32*)(tcBuffer + 2) ) = offset;
    *( (U16*)(tcBuffer + 6) ) = blockLen;

    return 8;
}

uint16_t COMMON_FormatFormatSdCmd(uint8_t* tcBuffer, U8 formatMagic)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 33;

    *( (U8*)(tcBuffer + 1) ) = formatMagic;

    return 2;
}

uint16_t COMMON_FormatHoleMap1Cmd(uint8_t* tcBuffer, U8* holeMap)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 120;

    memcpy(tcBuffer + 1,holeMap,16);

    return 17;
}

uint16_t COMMON_FormatHoleMap2Cmd(uint8_t* tcBuffer, U8* holeMap)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 121;

    memcpy(tcBuffer + 1,holeMap,16);

    return 17;
}

uint16_t COMMON_FormatHoleMap3Cmd(uint8_t* tcBuffer, U8* holeMap)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 122;

    memcpy(tcBuffer + 1,holeMap,16);

    return 17;
}

uint16_t COMMON_FormatHoleMap4Cmd(uint8_t* tcBuffer, U8* holeMap)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 123;

    memcpy(tcBuffer + 1,holeMap,16);

    return 17;
}

uint16_t COMMON_FormatHoleMap5Cmd(uint8_t* tcBuffer, U8* holeMap)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 124;

    memcpy(tcBuffer + 1,holeMap,16);

    return 17;
}

uint16_t COMMON_FormatHoleMap6Cmd(uint8_t* tcBuffer, U8* holeMap)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 125;

    memcpy(tcBuffer + 1,holeMap,16);

    return 17;
}

uint16_t COMMON_FormatHoleMap7Cmd(uint8_t* tcBuffer, U8* holeMap)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 126;

    memcpy(tcBuffer + 1,holeMap,16);

    return 17;
}

uint16_t COMMON_FormatHoleMap8Cmd(uint8_t* tcBuffer, U8* holeMap)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 127;

    memcpy(tcBuffer + 1,holeMap,16);

    return 17;
}

uint16_t COMMON_FormatInitiateFileUploadCmd(uint8_t* tcBuffer, COMMON_FileUploadDestination_t destination, U8 blockSize)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 114;

    tcBuffer[1] = 
        (destination << 0);
    *( (U8*)(tcBuffer + 2) ) = blockSize;

    return 3;
}

uint16_t COMMON_FormatLoadDownloadBlockCmd(uint8_t* tcBuffer, COMMON_FileType_t fileType, U8 counter, U32 offset, U16 length)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 112;

    tcBuffer[1] = 
        (fileType << 0);
    *( (U8*)(tcBuffer + 2) ) = counter;
    *( (U32*)(tcBuffer + 3) ) = offset;
    *( (U16*)(tcBuffer + 7) ) = length;

    return 9;
}

uint16_t COMMON_FormatResetCmd(uint8_t* tcBuffer, U8 magic)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 1;

    *( (U8*)(tcBuffer + 1) ) = magic;

    return 2;
}

uint16_t COMMON_FormatResetBootRegistersCmd(uint8_t* tcBuffer)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 6;


    return 1;
}

uint16_t COMMON_FormatResetFileListPtrCmd(uint8_t* tcBuffer)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 118;


    return 1;
}

uint16_t COMMON_FormatResetLogPtrCmd(uint8_t* tcBuffer)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 4;


    return 1;
}

uint16_t COMMON_FormatResetUploadBlockCmd(uint8_t* tcBuffer)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 117;


    return 1;
}

uint16_t COMMON_FormatSramScrubSettingsCmd(uint8_t* tcBuffer, U16 scrubSize)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 8;

    *( (U16*)(tcBuffer + 1) ) = scrubSize;

    return 3;
}

uint16_t COMMON_FormatUnixTimeCmd(uint8_t* tcBuffer, U32 time, U16 milliSec)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 2;

    *( (U32*)(tcBuffer + 1) ) = time;
    *( (U16*)(tcBuffer + 5) ) = milliSec;

    return 7;
}

uint16_t COMMON_FormatUnixTimeSaveCmd(uint8_t* tcBuffer, Boolean saveNow, Boolean saveOnUpdate, Boolean savePeriodic, U8 period)
{
    // write TtcMessage ID to first element in buffer
    tcBuffer[0] = 9;

    tcBuffer[1] = 
        (saveNow << 0) + 
        (saveOnUpdate << 1) + 
        (savePeriodic << 2);
    *( (U8*)(tcBuffer + 2) ) = period;

    return 3;
}


