/* ---------------------------------------------------------------------------
 * logging.c                                           (c) 2008 Micro-key bv
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AB Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl  
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: log driver
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Nov 28, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files
 * ---------------------------------------------------------------------------
 */
/* Compiler includes */
#include "string.h"

#include "LPC23xx.h"
#include "types.h"

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "task.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */ 
#include "logging.h"
#include "serial.h"
#include "SerOut.h"

#include "fat_public.h"
#include "fat_intern.h"
/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */
char returndate[12];
char returntime[9];
/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */

void enableLog (void)
{
    LogFlag = TRUE;
}


void disableLog (void)
{
    LogFlag = FALSE;
}


void startLog (UINT32 serialnumber)
{
    char fileName[13];
    char fileExt[4] = ".txt";
    
    sprintf (fileName, "%i", serialnumber); /* Convert INT-Value to DEZ-String  */
    strncat (fileName, fileExt, 4);
    debugPrint ("\n\r");
    debugPrint (fileName);

    FAT_StatusOut (f_open (&log_file, fileName, (FA_READ | FA_WRITE | FA_CREATE_ALWAYS)));
}


void stopLog (void)
{
    FAT_StatusOut (f_sync (&log_file));
    FAT_StatusOut (f_close (&log_file));
}


void writeLog (t_logSource source, t_serial_devices ComPort, Messagetype_t urgency, char * Message)
{
    UINT32 bytes_written;
    UINT32 string_length;
    char WriteBuffer[512];

    if (LogFlag == TRUE)
        {

        if (ComPort == COM1)
            {
            strcpy(WriteBuffer, "\nSerial Port: COM1\t");
            } 
        else if (ComPort == COM2)
            {
            strcpy(WriteBuffer, "\nSerial Port: COM2\t");
            }

        if (source == LogInput)
            {
            strcat(WriteBuffer, "Direction: Input\t");
            } 
        else if (source == LogOutput)
            {
            strcat(WriteBuffer, "Direction: Output\t");
            }

        switch (urgency)
            {
            case importantMessage:
                strcat(WriteBuffer, "Urgency: important Message\t");
                break;
            case headerMessage:
                strcat(WriteBuffer, "Urgency: header Message\t\t");
                break;
            case resultMessage:
                strcat(WriteBuffer, "Urgency: result Message\t\t");
                break;
            case noteMessage:
                strcat(WriteBuffer, "Urgency: note Message\t\t");
                break;
            case testMessage:
                strcat(WriteBuffer, "Urgency: test Message\t\t");
                break;
            case menuMessage:
                strcat(WriteBuffer, "Urgency: menu Message\t\t");
                break;
            default:
                ;
            }

//        strcat(WriteBuffer, "\n");
        strcat(WriteBuffer, Message);

        string_length = strlen(WriteBuffer);

        f_write(&log_file, &WriteBuffer, string_length, &bytes_written);
        }
}


void listLog (char* path)
{
    UINT32 loopcnt;
    DIR directory;
    FILINFO fileinfo;
    
    if (f_opendir(&directory, path) == FR_OK)
        {
        loopcnt = strlen(path);
        
        sendString (SerOutPort, TRUE, importantMessage,
                "Content of ", path, ":\n\r");
        
        while ((f_readdir(&directory, &fileinfo) == FR_OK) && fileinfo.fname[0])
            {
            sendString (SerOutPort, TRUE, importantMessage,
                    f_tab, &fileinfo.fname[0], Dummy);
            sendString (SerOutPort, FALSE, importantMessage,
                    f_tab, ItoDStr (fileinfo.fsize), Dummy);
            sendString (SerOutPort, FALSE, importantMessage,
                    f_tab, formatFiledate (fileinfo.fdate), Dummy);
            sendString (SerOutPort, FALSE, importantMessage,
                    f_tab, formatFiletime (fileinfo.ftime), Dummy);
            }
        }
}


char * formatFiledate (UINT16 date)
{
    char date_devider = '-';
    
    strcpy (returndate, ItoDStr (date & 0x0000001F));
    returndate[2] = date_devider;
    
    strcat (returndate, ItoDStr ((date & 0x000001E0) >> 5));
    returndate[5] = date_devider;
    
    strcat (returndate, ItoDStr (1980 + ((date & 0x0000FE00) >> 9)));
    returndate[10] = '\0';
    
    return (returndate);
}


char * formatFiletime (UINT16 time)
{
    char date_devider = ':';
    
    strcpy (returntime, ItoDStr ((time & 0x0000F800) >> 11));
    returntime[2] = date_devider;
    
    strcat (returntime, ItoDStr ((time & 0x000007E0) >> 5));
    returntime[5] = date_devider;
    
    strcat (returntime, ItoDStr (2 * (time & 0x0000001F)));
    returntime[8] = '\0';
    
    return (returntime);
}
