//----------------------------------------------------------------------------------
// <copyright file="sd_ff.c" company="LiquidRobotics">
//	Copyright (c) Liquid Robotics Corporation.  All rights reserved.
// </copyright>
//
// <summary>
// 	Implementation of SD file system wrappers around the generic FAT File implementation
// </summary>
//
// <owner>Jim Kirklin</owner>
//---------------------------------------------------------------------------------
//#include "ff.h"
//#include "pib_apps.h"
#include "includes.h"
#include "SD_ff.h"

//typedef struct _SD_FILE_
//{
//	BOOL 	inUse;
//	FIL		file;
//}SD_FILE, *PSD_FILE;

//FATFS	fsObj;
//SD_FILE		files[MAX_FILES_OPEN + 1];
//DIR		directory;

uint8_t	SD_Init(void)
{
	uint8_t retval = 0;

//	memset(&directory, 0, sizeof(DIR));
//	memset(&fsObj, 0, sizeof(FATFS));
//	memset(files, 0, sizeof(SD_FILE) * (MAX_FILES_OPEN + 1));
//
//	retval = (uint8_t)SD_Mount();

	return retval;
}




uint8_t SD_Mount(void)
{
	uint8_t retval = 0;

//	retval = (uint8_t)f_mount(0, &fsObj);

	return retval;
}




uint8_t SD_Unmount (void)
{
	uint8_t retval = 0;

	//Make sure all files are closed
//	for(int i = 0; i<= MAX_FILES_OPEN; i++)
//	{
//		if(files[i].inUse == TRUE)
//		{
//			f_sync(&(files[i].file));
//			f_close(&(files[i].file));
//			memset(&(files[i].file), 0, sizeof(FIL));
//			files[i].inUse = FALSE;
//		}
//	}
//
//	memset(&fsObj, 0, sizeof(FATFS));

	return retval;
}



uint8_t SD_CreateFile(char* pFileName, int8_t* pHandle, uint8_t flags)
{
	uint8_t retval = SD_OK;
//
//	*pHandle = 0;
//
//	if((flags & SD_CREATE_AND_OPEN) == SD_CREATE_AND_OPEN)
//	{
//		for(int i = 1; i<= MAX_FILES_OPEN; i++)
//		{
//			retval = SD_TOO_MANY_FILES;
//			if(files[i].inUse == FALSE)
//			{
//				*pHandle = i;
//				retval = SD_OK;
//				break;
//			}
//		}
//	}
//
//	if(retval == SD_OK)
//	{
//		retval = (uint8_t)f_open(&(files[*pHandle].file), pFileName, SD_CREATE_ALWAYS | SD_READ | SD_WRITE);
//
//		if((*pHandle != 0) && (retval == SD_OK))
//		{
//			files[*pHandle].inUse = TRUE;
//		}
//		else if(retval == SD_OK)
//		{
//			f_sync(&(files[0].file));
//			retval = (uint8_t)f_close(&(files[0].file));
//			memset(&(files[0].file), 0, sizeof(FIL));
//			files[0].inUse = FALSE;
//		}
//	}
//
	return retval;
}



uint8_t SD_CreateFixedSizeFile(char* pFileName, uint32_t size, int8_t* pHandle, uint8_t flags)
{
	uint8_t retval = SD_OK;

//	*pHandle = 0;
//
//	if((flags & SD_CREATE_AND_OPEN) == SD_CREATE_AND_OPEN)
//	{
//		for(int i = 1; i<= MAX_FILES_OPEN; i++)
//		{
//			retval = SD_TOO_MANY_FILES;
//			if(files[i].inUse == FALSE)
//			{
//				*pHandle = i;
//				retval = SD_OK;
//				break;
//			}
//		}
//	}
//
//	if(retval == SD_OK)
//	{
//		retval = (uint8_t)f_open(&(files[*pHandle].file), pFileName, SD_CREATE_ALWAYS | SD_READ | SD_WRITE);
//		retval = f_lseek(&(files[*pHandle].file), size);
//
//		if((*pHandle != 0) && (retval == SD_OK))
//		{
//			files[*pHandle].inUse = TRUE;
//		}
//		else if(retval == SD_OK)
//		{
//			f_sync(&(files[0].file));
//			retval = (uint8_t)f_close(&(files[0].file));
//			memset(&(files[0].file), 0, sizeof(FIL));
//			files[0].inUse = FALSE;
//		}
//	}

	return retval;
}




uint8_t SD_fopen(char* pFileName, uint8_t flags, int8_t* pHandle)
{
	uint8_t retval = SD_TOO_MANY_FILES;
//	*pHandle = 0;
//	for(int i = 1; i<= MAX_FILES_OPEN; i++)
//	{
//		if(files[i].inUse == FALSE)
//		{
//			*pHandle = i;
//			break;
//		}
//	}
//
//	if(*pHandle != 0)
//	{
//		retval = (uint8_t)f_open(&(files[*pHandle].file), pFileName, flags);
//		files[*pHandle].inUse = TRUE;
//		if(retval != SD_OK)
//		{
//			*pHandle = 0; 
//			files[*pHandle].inUse = FALSE;
//		}
//	}
//
//	DEBUG_PRINT(DBG_ALL, "SD_fopen %s handle=%d retval=%u\r\n", pFileName, *pHandle, retval);
	return retval;
}




uint8_t SD_fclose(int8_t handle)
{
	uint8_t retval = SD_INVALID_HANDLE;

//	if((handle > 0) && (handle <= MAX_FILES_OPEN))
//	{
//		if(files[handle].inUse == TRUE)
//		{
//			f_sync(&(files[handle].file));
//			retval = (uint8_t)f_close(&(files[handle].file));
//			memset(&(files[handle].file), 0, sizeof(FIL));
//			files[handle].inUse = FALSE;
//		}
//	}
//
//	DEBUG_PRINT(DBG_ALL, "SD_fclose handle=%d retval=%u\r\n", handle, retval);
	return retval;
}




uint8_t SD_fread(int8_t handle, char* pBuffer, uint16_t bytesToRead, uint16_t* pBytesRead)
{
	uint8_t retval = SD_INVALID_HANDLE;

//	if((handle > 0) && (handle <= MAX_FILES_OPEN))
//	{
//		if(files[handle].inUse == TRUE)
//		{
//			retval = (uint8_t)f_read(&(files[handle].file), pBuffer, bytesToRead, pBytesRead);
//		}
//	}

	return retval;
}




uint8_t SD_freadat(int8_t handle, char* pBuffer,uint32_t start, uint16_t bytesToRead, uint16_t* pBytesRead)
{
	uint8_t retval = SD_INVALID_HANDLE;

//	if((handle > 0) && (handle <= MAX_FILES_OPEN))
//	{
//		if(files[handle].inUse == TRUE)
//		{
//			retval = SD_SEEK_PAST_EOF;
//			if(start <= files[handle].file.fsize)
//			{
//				retval = (uint8_t)f_lseek(&(files[handle].file), start);
//				if(start > files[handle].file.fptr)
//				{
//					retval = SD_SEEK_PAST_EOF;
//				}
//			}
//
//			if(retval == SD_OK)
//			{
//				retval = (uint8_t)f_read(&(files[handle].file), pBuffer, bytesToRead, pBytesRead);
//			}
//		}
//	}

	return retval;
}



uint8_t SD_freadat_atomic(char* pFileName, char* pBuffer, uint32_t start, uint16_t bytesToRead, uint16_t* pBytesRead)
{
	uint8_t retval = 0;
//	uint8_t temp = 0;
//
//	retval = (uint8_t) f_open(&(files[0].file), pFileName, SD_OPEN_EXISTING | SD_READ);
//	if(retval == SD_OK)
//	{
//		retval = SD_SEEK_PAST_EOF;
//		if(start <= files[0].file.fsize)
//		{
//			retval = f_lseek(&(files[0].file), start);
//			if(start > files[0].file.fptr)
//			{
//				retval = SD_SEEK_PAST_EOF;
//			}
//
//			if(retval == SD_OK)
//			{
//				retval = (uint8_t)f_read(&(files[0].file), pBuffer, bytesToRead, pBytesRead);
//			}
//		}
//		temp = f_close(&(files[0].file));
//		//This is to make sure the original error is returned if a problem occurs after the 
//		//successful open but before closing the file
//		if(retval == SD_OK) 
//		{
//			retval = temp;	
//		}
//	}
	return retval;
}




uint8_t SD_fwrite(int8_t handle, char* pBuffer, uint16_t bytesToWrite, uint16_t* pBytesWritten)
{
	uint8_t retval = SD_INVALID_HANDLE;

//	if((handle > 0) && (handle <= MAX_FILES_OPEN))
//	{
//		if(files[handle].inUse == TRUE)
//		{
//			retval = (uint8_t)f_write(&(files[handle].file), pBuffer, bytesToWrite, pBytesWritten);
//			f_sync(&(files[0].file));
//		}
//	}

	return retval;
}




uint8_t SD_fwriteat(int8_t handle, char* pBuffer, uint32_t start,  uint16_t bytesToWrite, uint16_t* pBytesWritten)
{
	uint8_t retval = SD_INVALID_HANDLE;

//	if((handle > 0) && (handle <= MAX_FILES_OPEN))
//	{
//		if(files[handle].inUse == TRUE)
//		{
//			retval = (uint8_t)f_lseek(&(files[handle].file), start);
//			if(retval == SD_OK)
//			{
//				retval = (uint8_t)f_write(&(files[handle].file), pBuffer, bytesToWrite, pBytesWritten);
//				f_sync(&(files[0].file));
//			}
//		}
//	}

	return retval;
}



uint8_t SD_fwriteat_atomic(char* pFileName, char* pBuffer, uint32_t start, uint16_t bytesToWrite, uint16_t* pBytesWritten)
{
	uint8_t retval = 0;
//	uint8_t temp = 0;
//
//	retval = (uint8_t) f_open(&(files[0].file), pFileName, SD_OPEN_EXISTING | SD_WRITE);
//	if(retval == SD_OK)
//	{
//		if(start <= files[0].file.fsize)
//		{
//			retval = f_lseek(&(files[0].file), start);
//			if(start > files[0].file.fptr)
//			{
//				retval = SD_SEEK_PAST_EOF;
//			}
//
//			if(retval == SD_OK)
//			{
//				retval = (uint8_t)f_write(&(files[0].file), pBuffer, bytesToWrite, pBytesWritten);
//			}
//		}
//		f_sync(&(files[0].file));
//		temp = f_close(&(files[0].file));
//		//This is to make sure the original error is returned if a problem occurs after the 
//		//successful open but before closing the file
//		if(retval == SD_OK) 
//		{
//			retval = temp;	
//		}
//	}
	return retval;
}



uint8_t SD_delete(char* pFileName)
{
	uint8_t retval = 0;

//	retval = (uint8_t)f_unlink(pFileName);

	return retval;
}



uint8_t SD_dir(uint8_t getFirst, PFILEINFO pFileInfo)
{
	uint8_t retval = SD_OK;
////	FILINFO	temp;
//
//	if(getFirst != 0)
//	{
//		retval = (uint8_t)f_opendir(&directory, "");
//	}
//
//	if(retval == SD_OK)
//	{
//		retval = (uint8_t)f_readdir(&directory, (FILINFO*)pFileInfo);
////		retval = (uint8_t)f_readdir(&directory, &temp);
//	//	if(retval == SD_OK)
//	//	{
//	//		memcpy(pFileInfo, &temp, sizeof(FILEINFO));
//	//	}
//	}
//
	return retval;
}




uint8_t SD_getFileInfo(const char* lpszFileName, PFILEINFO pFileInfo)
{
	return 0;
//	return f_stat(lpszFileName, (FILINFO* ) pFileInfo);
}


uint8_t	SD_seek(int8_t handle, uint32_t position)
{
	uint8_t retval = SD_INVALID_HANDLE;

//	if((handle > 0) && (handle <= MAX_FILES_OPEN))
//	{
//		if(files[handle].inUse == TRUE)
//		{
//			retval = SD_SEEK_PAST_EOF;
//			if(position <= files[handle].file.fsize)
//			{
//				retval = (uint8_t)f_lseek(&(files[handle].file), position);
//			}
//		}
//	}
	return retval;
}

