/****************************************Copyright (c)****************************************************
**                            Guangzhou ZHIYUAN electronics Co.,LTD.
**
**                                 http://www.embedtools.com
**
**--------------File Info---------------------------------------------------------------------------------
** File name:               rt_sys_c.c
** Latest modified Date:    2009-06-01
** Latest Version:          1.0
** Descriptions:            A part of the real-time library functions
**
**--------------------------------------------------------------------------------------------------------
** Created by:              Chenmingji
** Created date:            2009-06-01
** Version:                 1.0
** Descriptions:            The original version
**
**--------------------------------------------------------------------------------------------------------
** Modified by:             
** Modified date:           
** Version:                 
** Descriptions:            
**
*********************************************************************************************************/
#include "..\..\config.h"
#include <stdlib.h>

/*********************************************************************************************************
  The implementations for some library functions
  For more details, please refer to the ADS compiler handbook and The library
  function manual
  User could change it as needed
*********************************************************************************************************/

#include <rt_sys.h>
#include <rt_misc.h>
#include <time.h>
#include <stdio.h>

#pragma  import(__use_no_semihosting_swi)

/*********************************************************************************************************
  Adjust semihosting functions 
*********************************************************************************************************/

/*********************************************************************************************************
** Function name:           _sys_exit
** Descriptions:            return initial stack and heap location, usually implemented in Assembly codes
** input parameters:        No detail descriptions in manual
** output parameters:       none
** Returned value:          r0: Base address of heaps
**                          r1: Base address of stacks, which is the highest address of the stack
**                          r2: Limit of heaps
**                          r3: Limit of stack, which is the lowest address of the stack
*********************************************************************************************************/
#if 0
__value_in_regs struct __initial_stackheap __user_initial_stackheap (unsigned R0,
                                                                     unsigned SP,
                                                                     unsigned R2)
{
}
#endif                                                                  /*  0                           */

/*********************************************************************************************************
** Function name:           _sys_exit
** Descriptions:            Exit function of the library, all exit from library will eventualy call _sys_exit()¡£
** input parameters:        iReturnCode: Exit code
** output parameters:       none
** Returned value:          none
*********************************************************************************************************/
void _sys_exit (int iReturnCode)
{
    while (1) {
    }
}

/*********************************************************************************************************
** Function name:           _ttywrch
** Descriptions:            This function can write a character to the control console. Since the control 
**                          console may be redirect, so unless necessary, do not use this  function for 
**                          error handler routine
** input parameters:        iCh: Output string
** output parameters:       none
** Returned value:          none
*********************************************************************************************************/
void _ttywrch (int iCh)
{
}

/*********************************************************************************************************
** Function name:           _sys_open
** Descriptions:            This function can open a file
** input parameters:        pcFileName: File name
**                          iOpenMode:  Open mode, it this a bit mapping, which is directly corresponding 
**                                      to the ISO standard specification, for more detail, please refer 
**                                      to rt_sys.h
** output parameters:       none
** Returned value:          file handle
*********************************************************************************************************/
FILEHANDLE _sys_open (const char *pcFileName, int iOpenMode)
{
    return -1;
}

/*********************************************************************************************************
** Function name:           _sys_close
** Descriptions:            This function can close the file opened by _sys_open()
** input parameters:        fhHandle: File handle
** output parameters:       none
** Returned value:          File handle
*********************************************************************************************************/
int _sys_close (FILEHANDLE fhHandle)
{
    return -1;
}

/*********************************************************************************************************
** Function name:           _sys_read
** Descriptions:            This function can read file content to buffer
** input parameters:        fhHandle: File handle
**                          iLen:     Number of data to read
**                          iMode:    Not used
** output parameters:       puBcuf:   Data read out
** Returned value:          Number of data not read
*********************************************************************************************************/
int _sys_read (FILEHANDLE fhHandle, unsigned char *puBcuf, unsigned int iLen, int iMode)
{
    return (int)0x80000000;
}

/*********************************************************************************************************
** Function name:           _sys_write
** Descriptions:            Write buffer contents to a file opened by _sys_open()
** input parameters:        fhHandle: File handle
**                          puBcuf:   Data to write
**                          iLen:     Number of data to write
**                          iMode:    Not used
** output parameters:       none
** Returned value:          Number of data not write
*********************************************************************************************************/
int _sys_write (FILEHANDLE fhHandle, const unsigned char *puBcuf, unsigned int iLen, int iMode)
{
    return 0x80000000;
}

/*********************************************************************************************************
** Function name:           _sys_ensure
** Descriptions:            Call _sys_ensure() function will flush all buffer that related to file handle
**                          fh, and make sure the file on the storage media remains up-to-dated
** input parameters:        fhHandle: File handle
** output parameters:       none
** Returned value:          Negative value for any error.
*********************************************************************************************************/
int _sys_ensure (FILEHANDLE fhHandle)
{
    return 0;
}

/*********************************************************************************************************
** Function name:           _sys_flen
** Descriptions:            This file returns the current size of the file.
**                          if system provides search on the end of file, which means fseek() can be used
**                          instead of _sys_flen().
** input parameters:        fhHandle: File handle
** output parameters:       none
** Returned value:          Negative value for any error.
*********************************************************************************************************/
long _sys_flen (FILEHANDLE fhHandle)
{
    return -1;
}

/*********************************************************************************************************
** Function name:           _sys_seek
** Descriptions:            This function can move the file pointer to a offset position lPos 
** input parameters:        fhHandle: File handle
**                          lPos:     Offset to the beginning of the file
** output parameters:       none
** Returned value:          Negative value for any error.
*********************************************************************************************************/
int _sys_seek (FILEHANDLE fhHandle, long lPos)
{
    return -1;
}

/*********************************************************************************************************
** Function name:           _sys_istty
** Descriptions:            This function can detect whether file handle is pointed to a terminal
** input parameters:        fhHandle: file handle
** output parameters:       none
** Returned value:          0:    No interacting device
**                          1:    there is a interacting device
**                          Other: Error
*********************************************************************************************************/
int _sys_istty (FILEHANDLE fhHandle)
{
    return -1;
}

/*********************************************************************************************************
** Function name:           _sys_tmpnam
** Descriptions:            This function can change the file number of the temporary file fileno to a 
**                          unique file name
** input parameters:        iFileNo:  File number
**                          uiMaxLen: Maximum length of the file name
** output parameters:       pcName:   File name 
** Returned value:          No specific description about this on the manual
*********************************************************************************************************/
int _sys_tmpnam (char * pcName, int iFileNo, unsigned int uiMaxLen)
{
    return 0;
}

/*********************************************************************************************************
** Function name:           _sys_command_string
** Descriptions:            This function can search the command line that calling this application from
**                          the environment that calling this applicatoin (no implemented yet).
** input parameters:        pcCmd: Command buffer
**                          iLen:  Lenth of the command buffer
** output parameters:       none   
** Returned value:          If succeed, returns a pointer to command line
**                          If failure, returns NULL¡£
*********************************************************************************************************/
char *_sys_command_string (char *pcCmd, int iLen)
{
    return NULL;
}

/*********************************************************************************************************
** Function name:           clock
** Descriptions:            Get current run time
** input parameters:        none
** output parameters:       none   
** Returned value:          Run time value, unit: (1/__CLK_TCK) second
*********************************************************************************************************/
clock_t clock(void)
{
    return 0;
}

/*********************************************************************************************************
** Function name:           _clock_init
** Descriptions:            Optional initialization function for clock()
** input parameters:        none
** output parameters:       none   
** Returned value:          none
*********************************************************************************************************/
#if 0
void _clock_init(void)
{
}
#endif                                                                  /*  0                           */

/*********************************************************************************************************
** Function name:           time
** Descriptions:            This function is ANSI C library function time() defined in time.h (not fully 
**                          implemented)
** input parameters:        none
** output parameters:       none   
** Returned value:          Return value is the closest value to the calendar time
**                          If the calendar time is not usable, returns (time_t)-1. If ptTimer is not a 
**                          NULL pointer, the return value will also be assigned to time_t*.
*********************************************************************************************************/
time_t time (time_t *ptTimer)
{
    time_t      tRt;                                                    /*  Return value                */
    struct tm   tmDataTime;                                             /*  Current calendar time       */
    
    /*
     * Add codes to obtain current calendar time:
     * struct tm definition:
     * struct tm {
     *   int tm_sec;   // seconds after the minute, 0 to 60
     *                    (0 - 60 allows for the occasional leap second)
     *   int tm_min;   // minutes after the hour, 0 to 59
     *   int tm_hour;  // hours since midnight, 0 to 23
     *   int tm_mday;  // day of the month, 1 to 31
     *   int tm_mon;   // months since January, 0 to 11
     *   int tm_year;  // years since 1900
     *   int tm_wday;  // days since Sunday, 0 to 6
     *   int tm_yday;  // days since January 1, 0 to 365
     *   int tm_isdst; // Daylight Savings Time flag
     * };
     */
    
    tRt = mktime(&tmDataTime);
    
    if (ptTimer != NULL) {
        *ptTimer = tRt;
    }
    return tRt;
}

/*********************************************************************************************************
** Function name:           remove
** Descriptions:            Delete a file
** input parameters:        pcFileName: File name
** output parameters:       none
** Returned value:          If succeed, returns 0, if failure, returns a non-zero value
*********************************************************************************************************/
int remove (const char * pcFileName)
{
    return -1;
}

/*********************************************************************************************************
** Function name:           rename
** Descriptions:            Rename a file
** input parameters:        pcOldName:    Old file name
**                          pcNewName:    New file name
** output parameters:       none
** Returned value:          If succeed, returns 0; if failure, returns a non-zero value. If a non-zero 
**                          value is returned and the file with the old file name exists, then it remains
**                          at the old file name
*********************************************************************************************************/
int rename (const char *pcOldName, const char *pcNewName)
{
    return -1;
}

/*********************************************************************************************************
** Function name:           system
** Descriptions:            Transfer the string pointed by pcString to the host, and let the command 
**                          handler execute it based on the definition of implementation. String can be a 
**                          NULL pointer, in this case, it will detect whether the command handler exists.
** input parameters:        pcString: String
** output parameters:       none
** Returned value:          For a NULL pointer, only when command handler is available, can it returns a 
**                          non-zero value
**                          For a not NULL pointer, system() will returns a value defined by implementation
*********************************************************************************************************/
int system (const char *pcString)
{
    return 0;
}

/*********************************************************************************************************
  End File
*********************************************************************************************************/
