/**
 * Generated by Eclipse Mita 0.1.0.
 * @date 2022-05-14 23:02:00
 */


#include <BCDS_Basics.h>
#include <BCDS_Retcode.h>
#include <FreeRTOS.h>
#include <inttypes.h>
#include <BCDS_SDCard_Driver.h>
#include <ff.h>
#include <XdkCommonInfo.h>
#include <string.h>
#include <task.h>
#include "SensorButton_two.h"
#include "SensorButton_one.h"
#include "MitaExceptions.h"

static FATFS StorageSDCardFatFSObject; /** File system specific objects */
/**< Macro to define default logical drive */
#define STORAGE_DEFAULT_LOGICAL_DRIVE           ""

/**< Macro to define force mount */
#define STORAGE_SDCARD_FORCE_MOUNT              UINT8_C(1)

/**< SD Card Drive 0 location */
#define STORAGE_SDCARD_DRIVE_NUMBER             UINT8_C(0)

static uint16_t ReadFromFileFilePosition = 0UL;


Retcode_T InputOutputSDCardSDc_Setup(void)
{
	
	return NO_EXCEPTION;
}

Retcode_T InputOutputSDCardSDc_Enable(void)
{
	Retcode_T retcode = RETCODE_OK;
	retcode = SDCardDriver_Initialize();
	if (RETCODE_OK == retcode)
	{
		if (SDCARD_INSERTED != SDCardDriver_GetDetectStatus())
		{
			printf("[ERROR, %s:%d] SD card was not detected for Storage\n", __FILE__, __LINE__);
			retcode = RETCODE(RETCODE_SEVERITY_WARNING, RETCODE_STORAGE_SDCARD_NOT_AVAILABLE);
		}
	}
	if (RETCODE_OK == retcode)
	{
		retcode = SDCardDriver_DiskInitialize(STORAGE_SDCARD_DRIVE_NUMBER); /* Initialize SD card */
	}
	if (RETCODE_OK == retcode)
	{
		/* Initialize file system */
		if (FR_OK != f_mount(&StorageSDCardFatFSObject, STORAGE_DEFAULT_LOGICAL_DRIVE, STORAGE_SDCARD_FORCE_MOUNT))
		{
			retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_STORAGE_SDCARD_MOUNT_FAILED);
		}
	}
	return retcode;
	
	return NO_EXCEPTION;
}

/**
 * Provides read access to the WriteToFile signal.
 */
Retcode_T InputOutputSDCardSDc_WriteToFile_Read(char** result)
{
	BCDS_UNUSED(result);
	return RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_FAILURE);
	
	return NO_EXCEPTION;
}

/**
 * Provides write access to the WriteToFile signal.
 */
Retcode_T InputOutputSDCardSDc_WriteToFile_Write(char** value)
{
	Retcode_T retcode = RETCODE_OK;
	FRESULT sdCardReturn = FR_OK, fileOpenReturn = FR_OK;
	FIL fileWriteHandle;
	UINT bytesWritten;
	FILINFO sdCardFileInfo;
	#if _USE_LFN
	sdCardFileInfo.lfname = NULL;
	#endif
	uint16_t WriteToFileFilePosition;
	fileOpenReturn = f_open(&fileWriteHandle, "Data.txt", FA_WRITE | FA_OPEN_ALWAYS);
	if ((FR_OK == sdCardReturn) && (FR_OK == fileOpenReturn))
	{
		sdCardReturn = f_stat("Data.txt", &sdCardFileInfo);
		WriteToFileFilePosition = sdCardFileInfo.fsize;
	}
	if ((FR_OK == sdCardReturn) && (FR_OK == fileOpenReturn))
	{
	    sdCardReturn = f_lseek(&fileWriteHandle, WriteToFileFilePosition);
	}
	if ((FR_OK == sdCardReturn) && (FR_OK == fileOpenReturn))
	{
	    sdCardReturn = f_write(&fileWriteHandle, *value, strlen(*value), &bytesWritten); /* Write it to the destination file */
	}
	if (FR_OK == fileOpenReturn)
	{
		sdCardReturn = f_close(&fileWriteHandle);
	}
	if ((FR_OK != sdCardReturn) || (FR_OK != fileOpenReturn))
	{
	    retcode = RETCODE(RETCODE_SEVERITY_WARNING, RETCODE_STORAGE_ERROR_IN_FILE_WRITE);
	}
	return retcode;
	
	return NO_EXCEPTION;
}


/**
 * Provides read access to the ReadFromFile signal.
 */
Retcode_T InputOutputSDCardSDc_ReadFromFile_Read(char** result)
{
	Retcode_T retcode = RETCODE_OK;
	FRESULT sdCardReturn = FR_OK, fileOpenReturn = FR_OK;
	FILINFO sdCardFileInfo;
	#if _USE_LFN
	sdCardFileInfo.lfname = NULL;
	#endif
	FIL fileReadHandle;
	UINT bytesRead;
	
	sdCardReturn = f_stat("Data.txt", &sdCardFileInfo);
	if (FR_OK == sdCardReturn)
	{
		if(ReadFromFileFilePosition >= sdCardFileInfo.fsize)
		{
			return EXCEPTION_ENDOFFILEEXCEPTION;
		}
		else
		{
			fileOpenReturn = f_open(&fileReadHandle, "Data.txt", FA_OPEN_EXISTING | FA_READ);
		}
	}
	if ((FR_OK == sdCardReturn) && (FR_OK == fileOpenReturn))
	{
	    sdCardReturn = f_lseek(&fileReadHandle, ReadFromFileFilePosition);
	}
	if ((FR_OK == sdCardReturn) && (FR_OK == fileOpenReturn))
	{
	    sdCardReturn = f_read(&fileReadHandle, *result, 6, &bytesRead); /* Read a chunk of source file */
	}
	if ((FR_OK == sdCardReturn) && (FR_OK == fileOpenReturn))
	{
		ReadFromFileFilePosition += bytesRead;
	}
	if (FR_OK == fileOpenReturn)
	{
		sdCardReturn = f_close(&fileReadHandle);
	}
	if ((FR_OK != sdCardReturn) || (FR_OK != fileOpenReturn))
	{
	    retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_STORAGE_SDCARD_READ_FAILED);
	}
	return retcode;
	
	return NO_EXCEPTION;
}

/**
 * Provides write access to the ReadFromFile signal.
 */
Retcode_T InputOutputSDCardSDc_ReadFromFile_Write(char** value)
{
	BCDS_UNUSED(value);
	return RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_FAILURE);
	
	return NO_EXCEPTION;
}



