/*******************************************************************************
  MPLAB Harmony Application Source File

  Company:
    Microchip Technology Inc.

  File Name:
    app.c

  Summary:
    This file contains the source code for the MPLAB Harmony application.

  Description:
    This file contains the source code for the MPLAB Harmony application.  It
    implements the logic of the application's state machine and it may call
    API routines of other MPLAB Harmony modules in the system, such as drivers,
    system services, and middleware.  However, it does not call any of the
    system interfaces (such as the "Initialize" and "Tasks" functions) of any of
    the modules in the system or make any assumptions about when those functions
    are called.  That is the responsibility of the configuration-specific system
    files.
 *******************************************************************************/

//DOM-IGNORE-BEGIN
/*******************************************************************************
* Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries.
*
* Subject to your compliance with these terms, you may use Microchip software
* and any derivatives exclusively with Microchip products. It is your
* responsibility to comply with third party license terms applicable to your
* use of third party software (including open source software) that may
* accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*******************************************************************************/
//DOM-IGNORE-END

// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include <string.h>
#include "app.h"
#include "user.h"
#include <stdio.h>
#include "time.h"
//#include "alltypes.h"
// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************

#define SDCARD_MOUNT_NAME    SYS_FS_MEDIA_IDX0_MOUNT_NAME_VOLUME_IDX0
#define SDCARD_DEV_NAME      SYS_FS_MEDIA_IDX0_DEVICE_NAME_VOLUME_IDX0
//#define SDCARD_FILE_NAME     "text.txt"
#define SDCARD_FILE_NAME     "test.txt"
#define SDCARD_DIR_NAME      "Dir1"
  char timeStr[32]; 
////// *****************************************************************************
/////* Application Data
////
////  Summary:
////    Holds application data
////
////  Description:
////    This structure holds the application's data.
////
////  Remarks:
////    This structure should be initialized by the APP_Initialize function.
////
////    Application strings and buffers are be defined outside this structure.
////*/
////APP_DATA appData;
////
/////* Application data buffer */
////uint8_t BUFFER_ATTRIBUTES dataBuffer[APP_DATA_LEN] = " Elpis IT Solutions Pvt. Ltd.";
////
////// *****************************************************************************
////// *****************************************************************************
////// Section: Application Callback Functions
////// *****************************************************************************
////// *****************************************************************************
////
////static void APP_SysFSEventHandler(SYS_FS_EVENT event,void* eventData,uintptr_t context)
////{
////    switch(event)
////    {
////        /* If the event is mount then check if SDCARD media has been mounted */
////        case SYS_FS_EVENT_MOUNT:
////            if(strcmp((const char *)eventData, SDCARD_MOUNT_NAME) == 0)
////            {
////                appData.sdCardMountFlag = true;
////            }
////            break;
////
////        /* If the event is unmount then check if SDCARD media has been unmount */
////        case SYS_FS_EVENT_UNMOUNT:
////            if(strcmp((const char *)eventData, SDCARD_MOUNT_NAME) == 0)
////            {
////                appData.sdCardMountFlag = false;
////
////                appData.state = APP_MOUNT_WAIT;
////
////                LED_OFF();
////            }
////
////            break;
////
////        case SYS_FS_EVENT_ERROR:
////        default:
////            break;
////    }
////}
////
////// *****************************************************************************
////// *****************************************************************************
////// Section: Application Local Functions
////// *****************************************************************************
////// *****************************************************************************
////
////
/////* TODO:  Add any necessary local functions.
////*/
////
////
////// *****************************************************************************
////// *****************************************************************************
////// Section: Application Initialization and State Machine Functions
////// *****************************************************************************
////// *****************************************************************************
////
/////*******************************************************************************
////  Function:
////    void APP_Initialize ( void )
////
////  Remarks:
////    See prototype in app.h.
//// */
////
////void APP_Initialize ( void )
////{
////    /* Place the App state machine in its initial state. */
////    appData.state = APP_MOUNT_WAIT;
////
////    /* Register the File System Event handler */
////    SYS_FS_EventHandlerSet((void const*)APP_SysFSEventHandler,(uintptr_t)NULL);
////}
////
////
/////******************************************************************************
////  Function:
////    void APP_Tasks ( void )
////
////  Remarks:
////    See prototype in app.h.
//// */
////
//////void APP_Tasks ( void )
//////{
//////    /* Check the application's current state. */
//////    switch ( appData.state )
//////    {
//////        case APP_MOUNT_WAIT:
//////            /* Wait for SDCARD to be Auto Mounted */
//////            if(appData.sdCardMountFlag == true)
//////            {
//////                appData.state = APP_SET_CURRENT_DRIVE;
//////            }
//////            break;
//////
//////        case APP_SET_CURRENT_DRIVE:
//////            if(SYS_FS_CurrentDriveSet(SDCARD_MOUNT_NAME) == SYS_FS_RES_FAILURE)
//////            {
//////                /* Error while setting current drive */
//////                appData.state = APP_ERROR;
//////            }
//////            else
//////            {
//////                /* Open a file for reading. */
//////                printf("Mount successful\n");
//////                appData.state = APP_OPEN_FIRST_FILE;
//////            }
//////      
//////            break;
//////
//////        case APP_OPEN_FIRST_FILE:
//////            
//////            appData.fileHandle = SYS_FS_FileOpen(SDCARD_FILE_NAME,
//////                    (SYS_FS_FILE_OPEN_READ));
//////            if(appData.fileHandle == SYS_FS_HANDLE_INVALID)
//////            {
//////                /* Could not open the file. Error out*/
//////                printf("File opened Error\n");
//////                appData.state = APP_ERROR;
//////            }
//////            else
//////            {
//////                /* Create a directory. */
//////                printf("File opened for read successful\n");
//////                //appData.state = APP_CREATE_DIRECTORY;
//////                appData.state = APP_READ_WRITE_TO_FILE;
//////            }
//////            break;
//////
//////        case APP_CREATE_DIRECTORY:
//////            /* Delete the files under Dir1 directory (if any) */
//////            SYS_FS_FileDirectoryRemove(SDCARD_DIR_NAME"/"SDCARD_FILE_NAME);
//////
//////            /* Delete the Dir1 directory if it exists */
//////            SYS_FS_FileDirectoryRemove(SDCARD_DIR_NAME);
//////
//////            if(SYS_FS_DirectoryMake(SDCARD_DIR_NAME) == SYS_FS_RES_FAILURE)
//////            {
//////                /* Error while setting current drive */
//////                appData.state = APP_ERROR;
//////            }
//////            else
//////            {
//////                /* Open a second file for writing. */
//////                appData.state = APP_OPEN_SECOND_FILE;
//////            }
//////            break;
//////
//////        case APP_OPEN_SECOND_FILE:
//////            /* Open a second file inside "Dir1" */
//////            appData.fileHandle1 = SYS_FS_FileOpen(SDCARD_DIR_NAME"/"SDCARD_FILE_NAME,
//////                    (SYS_FS_FILE_OPEN_WRITE));
//////
//////            if(appData.fileHandle1 == SYS_FS_HANDLE_INVALID)
//////            {
//////                /* Could not open the file. Error out*/
//////                appData.state = APP_ERROR;
//////            }
//////            else
//////            {
//////                /* Read from one file and write to another file */
//////                appData.state = APP_READ_WRITE_TO_FILE;
//////            }
//////
////////        case APP_READ_WRITE_TO_FILE:
////////
////////            appData.nBytesRead = SYS_FS_FileRead(appData.fileHandle, (void *)dataBuffer, APP_DATA_LEN);
////////              
////////            if (appData.nBytesRead == -1)
////////            {
////////                /* There was an error while reading the file.
////////                 * Close the file and error out. */
////////                printf("ENTERED into the file \r\n");
////////                SYS_FS_FileClose(appData.fileHandle);
////////                appData.state = APP_ERROR;
////////            }
////////            else
////////            {
////////                printf("Size of File : %d\n", sizeof(dataBuffer));
////////                /* If read was success, try writing to the new file */
////////                if(SYS_FS_FileWrite(appData.fileHandle1, (const void *)dataBuffer, appData.nBytesRead) == -1)
////////                {
////////                    /* Write was not successful. Close the file
////////                     * and error out.*/
////////                    printf("SD Card Write Error\n");
////////                    SYS_FS_FileClose(appData.fileHandle1);
////////                    appData.state = APP_ERROR;
////////                }
////////                else if(SYS_FS_FileEOF(appData.fileHandle) == 1)    /* Test for end of file */
////////                {
////////                    /* Continue the read and write process, until the end of file is reached */
////////                    
////////                    appData.state = APP_CLOSE_FILE;
////////                }
////////            }
////////            break;
//////       case APP_READ_WRITE_TO_FILE:
//////
//////    appData.nBytesRead = SYS_FS_FileRead(appData.fileHandle,
//////                                         (void *)dataBuffer,
//////                                         APP_DATA_LEN - 1);
//////
//////    if (appData.nBytesRead < 0)
//////    {
//////        printf("Read Error\n");
//////        SYS_FS_FileClose(appData.fileHandle);
//////        appData.state = APP_ERROR;
//////    }
//////    else if (appData.nBytesRead > 0)
//////    {
//////        // Null terminate so we can print as string
//////        dataBuffer[appData.nBytesRead] = '\0';
//////
//////        printf("Bytes Read: %d\n", appData.nBytesRead);
//////        printf("File Content:\n%s\n", dataBuffer);
//////
//////        if(SYS_FS_FileEOF(appData.fileHandle))
//////        {
//////            appData.state = APP_CLOSE_FILE;
//////        }
//////    }
//////    else
//////    {
//////        // No more data
//////        appData.state = APP_CLOSE_FILE;
//////    }
//////
//////    break;
//////
//////        case APP_CLOSE_FILE:
//////            /* Close both files */
//////            SYS_FS_FileClose(appData.fileHandle);
//////            SYS_FS_FileClose(appData.fileHandle1);
//////
//////            /* The test was successful. Lets idle. */
//////            appData.state = APP_IDLE;
//////            break;
//////
//////        case APP_IDLE:
//////            /* The application comes here when the demo has completed successfully.*/
//////            LED_ON();
//////            break;
//////
//////        case APP_ERROR:
//////            /* The application comes here when the demo has failed. */
//////            break;
//////
//////        default:
//////            break;
//////    }
//////}
////
////void APP_Tasks ( void )
////{
////    /* Check the application's current state. */
////    switch ( appData.state )
////    {
////        case APP_MOUNT_WAIT:
////            /* Wait for SDCARD to be Auto Mounted */
////            if(appData.sdCardMountFlag == true)
////            {
////                appData.state = APP_SET_CURRENT_DRIVE;
////            }
////            break;
////
////case APP_SET_CURRENT_DRIVE:
////    if(SYS_FS_CurrentDriveSet(SDCARD_MOUNT_NAME) == SYS_FS_RES_FAILURE)
////    {
////        appData.state = APP_ERROR;
////    }
////    else
////    {
////        printf("Mount successful\n");
////        appData.state = APP_OPEN_FILE_FOR_WRITE;
////    }
////    break;
////
////case APP_OPEN_FILE_FOR_WRITE:
////
////    // OPEN IN APPEND MODE (creates file if not exists)
////    appData.fileHandle = SYS_FS_FileOpen(SDCARD_FILE_NAME,
////                SYS_FS_FILE_OPEN_APPEND);
////
////    if(appData.fileHandle == SYS_FS_HANDLE_INVALID)
////    {
////        printf("File open for write failed\n");
////        appData.state = APP_ERROR;
////    }
////    else
////    {
////        printf("File opened for write (or created)\n");
////        appData.state = APP_WRITE_TO_FILE;
////    }
////    break;
////
////case APP_WRITE_TO_FILE:
////
////    {
////        const char *data = "Internet down, storing data...\n";
////        uint32_t len = strlen(data);
////
////        if (SYS_FS_FileWrite(appData.fileHandle, (const void*)data, len) == -1)
////        {
////            printf("Write failed\n");
////            SYS_FS_FileClose(appData.fileHandle);
////            appData.state = APP_ERROR;
////        }
////        else
////        {
////            printf("Write successful\n");
////            SYS_FS_FileClose(appData.fileHandle);
////
////            // Now read the same file to verify
////            appData.state = APP_OPEN_FILE_FOR_READ;
////        }
////    }
////    break;
////
////case APP_OPEN_FILE_FOR_READ:
////
////    appData.fileHandle = SYS_FS_FileOpen(SDCARD_FILE_NAME,
////                SYS_FS_FILE_OPEN_READ);
////
////    if(appData.fileHandle == SYS_FS_HANDLE_INVALID)
////    {
////        printf("File open for read failed\n");
////        appData.state = APP_ERROR;
////    }
////    else
////    {
////        printf("File opened for read\n");
////        appData.state = APP_READ_FILE;
////    }
////    break;
////
////case APP_READ_FILE:
////
////    appData.nBytesRead = SYS_FS_FileRead(appData.fileHandle,
////                                         (void *)dataBuffer,
////                                         APP_DATA_LEN - 1);
////
////    if (appData.nBytesRead < 0)
////    {
////        printf("Read Error\n");
////        SYS_FS_FileClose(appData.fileHandle);
////        appData.state = APP_ERROR;
////    }
////    else
////    {
////        dataBuffer[appData.nBytesRead] = '\0';
////        printf("Bytes Read: %d\n", appData.nBytesRead);
////        printf("File Content:\n%s\n", dataBuffer);
////
////        SYS_FS_FileClose(appData.fileHandle);
////        appData.state = APP_IDLE;
////    }
////    break;
////
////case APP_IDLE:
////    LED_ON();
////    break;
////
////case APP_ERROR:
////    LED_OFF();
////    break;
////
/////*******************************************************************************
//// End of File
//// */
////        default:
////            break;
////    }
////}

//#include "logger.h"
//#include "system/fs/sys_fs.h"
//#include "system/console/sys_console.h"
//#include <stdio.h>
//#include <string.h>

#define LOG_FILE_PATH "/mnt/myDrive1/log.txt"
//
//
////void APP_Initialize ( void )
////{
////    /* Place the App state machine in its initial state. */
////    appData.state = APP_MOUNT_WAIT;
////
////    /* Register the File System Event handler */
////    SYS_FS_EventHandlerSet((void const*)APP_SysFSEventHandler,(uintptr_t)NULL);
////}
//
//void APP_Tasks ( void )
//{
//
//
//Logger_Write("#2c0021000000getsignallist;Et3100_2411_001;3;4$");
//
//char readBuffer[2048];
//
//if(Logger_Read(readBuffer, sizeof(readBuffer)))
//{
//    printf("SD Content:\n%s\n", readBuffer);
//}
//
//}
///* ---------------- Timestamp (Replace with RTC later) ---------------- */
//static void GetTimestamp(char *ts)
//{
//    //sprintf(ts, "2026-02-13 12:00:00");
//     time_t now = time(NULL);
//     struct tm *tm_info = localtime(&now);
//     strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S | ", tm_info);
//
//}
//
///* ---------------- Init (Clear File at Power ON) ---------------- */
//void Logger_Init(void)
//{
//    SYS_FS_HANDLE fh =
//        SYS_FS_FileOpen(LOG_FILE_PATH, SYS_FS_FILE_OPEN_WRITE);
//
//    if(fh != SYS_FS_HANDLE_INVALID)
//    {
//        SYS_FS_FileClose(fh);
//        printf("Logger Initialized (File Cleared)\n");
//    }
//    else
//    {
//       printf("Logger Init Failed\n");
//    }
//}
//
///* ---------------- Write ---------------- */
//bool Logger_Write(const char *packet)
//{
//    char timestamp[32];
//    char finalBuffer[1024];
//
//    GetTimestamp(timestamp);
//
//    sprintf(finalBuffer, "%s | %s\n", timestamp, packet);
//
//    SYS_FS_HANDLE fh =
//        SYS_FS_FileOpen(LOG_FILE_PATH, SYS_FS_FILE_OPEN_APPEND);
//
//    if(fh == SYS_FS_HANDLE_INVALID)
//    {
//        printf("Write Open Failed\n");
//        return false;
//    }
//
//    SYS_FS_FileWrite(fh, finalBuffer, strlen(finalBuffer));
//    SYS_FS_FileClose(fh);
//
//    printf("Data Written to SD\n");
//    return true;
//}
//
///* ---------------- Read ---------------- */
//bool Logger_Read(char *buffer, uint32_t maxLen)
//{
//    SYS_FS_HANDLE fh =
//        SYS_FS_FileOpen(LOG_FILE_PATH, SYS_FS_FILE_OPEN_READ);
//
//    if(fh == SYS_FS_HANDLE_INVALID)
//    {
//        printf("Read Open Failed\n");
//        return false;
//    }
//
//    uint32_t bytesRead = SYS_FS_FileRead(fh, buffer, maxLen - 1);
//
//    buffer[bytesRead] = '\0';
//
//    SYS_FS_FileClose(fh);
//
//    printf("Read %lu bytes from SD\n", bytesRead);
//
//    return true;
//}

void APP_Tasks ( void )
{


//
    {
        // simulate 1-second tick (replace with timer interrupt in real)
        //Delay_ms(1000);
        Logger_SysTime_Update();

        // write some test data
        Logger_Write("#2c0021000000getsignallist;Et3100_2411_001;3;4$");

        // optionally read log
        char buffer[2048];
        Logger_Read(buffer, sizeof(buffer));
        printf("SD Content:\n%s\n", buffer);
    }


}



// ---------------- System time counter ----------------
static uint32_t g_seconds = 0;  // seconds since power-on

void Logger_SysTime_Update(void)
{
    g_seconds++;
}

// ---------------- Timestamp generator ----------------
static void GetTimestamp(char *ts, size_t tsLen)
{
    uint32_t seconds = g_seconds;

    uint32_t hours   = seconds / 3600;
    uint32_t minutes = (seconds % 3600) / 60;
    uint32_t secs    = seconds % 60;

    snprintf(ts, tsLen, "%02lu:%02lu:%02lu", hours, minutes, secs);
}

// ---------------- Initialize ----------------
void Logger_Init(void)
{
    SYS_FS_HANDLE fh = SYS_FS_FileOpen(LOG_FILE_PATH, SYS_FS_FILE_OPEN_WRITE);
    if(fh != SYS_FS_HANDLE_INVALID)
    {
        SYS_FS_FileClose(fh);
        printf("Logger Initialized (File Cleared)\n");
    }
    else
    {
        printf("Logger Init Failed\n");
    }

    g_seconds = 0; // reset uptime counter
}

// ---------------- Write ----------------
bool Logger_Write(const char *packet)
{
    char timestamp[16];
    char finalBuffer[1024];

    GetTimestamp(timestamp, sizeof(timestamp));

    snprintf(finalBuffer, sizeof(finalBuffer), "%s | %s\n", timestamp, packet);

    SYS_FS_HANDLE fh = SYS_FS_FileOpen(LOG_FILE_PATH, SYS_FS_FILE_OPEN_APPEND);
    if(fh == SYS_FS_HANDLE_INVALID)
    {
        printf("Write Open Failed\n");
        return false;
    }

    SYS_FS_FileWrite(fh, finalBuffer, strlen(finalBuffer));
    SYS_FS_FileClose(fh);

    printf("Data Written to SD\n");
    return true;
}

// ---------------- Read ----------------
bool Logger_Read(char *buffer, uint32_t maxLen)
{
    SYS_FS_HANDLE fh = SYS_FS_FileOpen(LOG_FILE_PATH, SYS_FS_FILE_OPEN_READ);
    if(fh == SYS_FS_HANDLE_INVALID)
    {
        printf("Read Open Failed\n");
        return false;
    }

    uint32_t bytesRead = SYS_FS_FileRead(fh, buffer, maxLen - 1);
    buffer[bytesRead] = '\0';

    SYS_FS_FileClose(fh);

    printf("Read %lu bytes from SD\n", bytesRead);
    return true;
}
