
/******************************************************************************/
/* Williams Advanced Engineering Limited                     Copyright(C) 2021*/
/* Grove                                                  All rights reserved */
/*                                                                            */
/******************************************************************************/

#include "Std_Types.h"

// Removing of compiler warnings and errors because of type re-declaration between hal_stdtypes.h and Std_Types.h
#define _BOOLEAN_DECLARED
#define _UINT8_DECLARED
#define _UINT16_DECLARED
#define _UINT32_DECLARED
#define _UINT64_DECLARED
#define _SINT8_DECLARED
#define _SINT16_DECLARED
#define _SINT32_DECLARED
#define _SINT64_DECLARED
#define _FLOAT32_DECLARED
#define _FLOAT64_DECLARED
#define _STD_RETURN_TYPE_DECLARED
#define _STDVERSION_DECLARED

#include "Ios_IntrTimerManagement.h"
#include "Ios_SecurityAccess.h"
#include "Ios_Std.h"


Std_ReturnType Fbl_Port_GetSeed(uint32 Seedlen,
                                uint32 AccDataRecsize,
                                uint8 * SecurityAccessDataRecord,
                                uint8 * Seed)
{
    Std_ReturnType ret = E_NOT_OK;
    uint8 index;
    uint8 SecurityAccessSeed[SEEDLENGTH]={0};

    // generate Seed
    fnIosGenerateSeed(SecurityAccessSeed, SEEDLENGTH);
    fnIosCalcClientKey(SecurityAccessSeed, SEEDLENGTH);

    for (index = 0; index < Seedlen; index++)
    {
        if(index < SEEDLENGTH)
        {
            // copy seed to recipient
            Seed[index] = SecurityAccessSeed[index];
        }
    }
    ret = E_OK;

    return ret;
}

Std_ReturnType Fbl_Port_CompareKey(uint32 KeyLen, const uint8 * KeyBuf)
{
    Std_ReturnType ret = E_OK;
    uint8 index;
    uint8 ClientKey[SEEDLENGTH];

    // todo: WAE to implement vs block that stores the key value
    fnIosGetCalcClientKey(ClientKey, SEEDLENGTH);

    //wcu_printf("Key Compare: \n");
    for (index = 1; index <= KeyLen; index++)
    {
        //wcu_printf("Byte %d - Calc key : 0x%x - Client key: 0x%x\n", index-1, ClientKey[index-1],KeyBuf[index-1]);
        if ( ClientKey[index-1] != KeyBuf[index-1] )
        {
            ret = E_NOT_OK;
        }
        else
        {
            /* Do nothing - if we go through all loop with no error, then key is OK */
        }
    }
    //wcu_printf("\n");

    return ret;
}


