/*! Time-stamp: <@(#)SysFuncLinux.cpp   07/05/2007 - 15:46:04   P.Hoogervorst>
 *********************************************************************
 *  @file   : SysFuncLinux.cpp
 *
 *  Project : ATTestApp. Testappliction for Alitronika devices.
 *			  Supports Linux and windows operating System.
 *
 *  Package : 
 *
 *  Company : Engineering Spirit
 *
 *  Author  : P.Hoogervorst                              Date: 07/05/2007
 *
 *  Purpose : Implementation of methods for class 
 *
 *********************************************************************
 * Version History:
 *
 * V 0.10  07/05/2007  BN : First Revision
 *
 *********************************************************************
 */

/**********************************************************************************************************/
// includes
#include <sys/fcntl.h>			// for open
#include <sys/unistd.h>			// for close, read, write
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>

#include "SysFunc.h"


/**********************************************************************************************************/
// functions

// System control handler callback function
static void ConsoleHandlerCallBack(DWORD CEvent)
{
	SysConsoleHandlersCallBack();
}

// Initialize system console handler. Used for system break and system abort
void SysSetupConsoleHandlers()
{
	// TODO: also use ConsoleHandlerCallBack()
}

// sleep  //Already defined in the lib 
//void SysSleep(u32 nSleepMs)
//{
	//nSleepMs *= 1000;
	//usleep(nSleepMs );
//}


// open a file for read 
s32 SysFileOpenRead(char * pFileName)
{
	return (s32)open(pFileName, O_RDONLY| O_LARGEFILE);
}

// Rewind a file to continue from the beginning
s32 SysFileRewind(s32 iFileDescrValue)
{
	return (s32)lseek(iFileDescrValue, 0, SEEK_SET);
}

// read the data from the file
s32 SysFileRead(s32 iFileDescrValue, char * pBuffer, u32 nBufSize)
{
	return (s32)read(iFileDescrValue, pBuffer, nBufSize);
}

// open a file for write
s32 SysFileOpenWrite(char * pFileName)
{
	return (s32)open(pFileName, O_WRONLY | O_CREAT | O_LARGEFILE, S_IRWXU | S_IRWXG | S_IRWXO);
}

// write data to the file
s32 SysFileWrite(s32 iFileDescrValue, char * pBuffer, u32 nBufSize)
{
	return (s32)write (iFileDescrValue, pBuffer, nBufSize);
}

// close a file
s32 SysFileClose(s32 iFileDescrValue)
{
	return (s32)close(iFileDescrValue);
}

// Get current system time in seconds
u32 SysGetCurTime()
{
	return (u32)time(NULL);
}
