/*===========================================================================
                    Combined Charging System (CCS): SECC
                                 FWMaker.c

                        initiated by Vern, Joseph
                           (since 2019/07/19)
=============================================================================*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <net/if.h> /*struct ifreq*/
#include <linux/sockios.h> /*SIOCGIFINDEX*/
#include <linux/socket.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "FWMaker.h"

unsigned char buf_log_fwmaker[SIZE_OF_LOG_BUFFER];

/*===========================================================================
FUNCTION: StoreLogMsg
DESCRIPTION:
PRE-CONDITION:
INPUT:
OUTPUT:
GLOBAL VARIABLES:
=============================================================================*/
#if SAVE_SYS_LOG_MSG_FWMAKER_SWITCH == ENABLE
int StoreLogMsg(unsigned char *DataString)
{
    static unsigned char Buf[1024];
    static time_t CurrentTime;
    static struct tm *tm;
    static struct timeval tv;

    memset(Buf, 0, sizeof(Buf));
    CurrentTime = time(NULL);
    tm = localtime(&CurrentTime);
    gettimeofday(&tv, NULL); // get microseconds, 10^-6

    sprintf(Buf, "echo \"[%04d%02d%02d: %02d:%02d:%02d.%06d][FWMaker]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
            tm->tm_year + 1900,
            tm->tm_mon + 1,
            tm->tm_mday,
            tm->tm_hour,
            tm->tm_min,
            tm->tm_sec,
            tv.tv_usec,
            DataString,
            tm->tm_year + 1900,
            tm->tm_mon + 1);
    system(Buf);

    DEBUG_PRINTF_FWMAKER_SYSTEM_LOG("[%02d:%02d:%02d.%06d][FWMaker]%s \n",
            tm->tm_hour,
            tm->tm_min,
            tm->tm_sec,
            tv.tv_usec,
            DataString);

    //Reset the buf_log_fwmaker Buffer, i.e. DataString
    memset(buf_log_fwmaker, 0, SIZE_OF_LOG_BUFFER);
}
#endif

//./FWMaker 172.16.24.126
int main(int argc, char *argv[])
{
    unsigned char *MemBuf, *MemBuf2, buf[64];
    int fd, rd = 0, wrd = 0, tmp = 0, rd2 = 0;
    unsigned int CheckSum = 0;

    memset(buf, 0, 64);
    sprintf(buf, "tftp -gr uImage -l /mnt/uImage %s", argv[1]);
    system(buf);
    memset(buf, 0, 64);
    sprintf(buf, "tftp -gr rootfs_nor.img -l /mnt/rootfs_nor.img %s", argv[1]);
    system(buf);

    if((MemBuf = malloc(0x100000D)) == NULL)
    {
        DEBUG_PRINTF_FWMAKER_DETAIL("Allocate MemBuf memory error\n");
        return 0;
    }

    memset(MemBuf, 0xff, 0x100000D);
    fd = open("/mnt/uImage", O_RDWR);

    if(fd > 0)
    {
        if((rd = read(fd, MemBuf, 0x100000D)) <= 0)
        {
            DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/uImage read Error\n");
            free(MemBuf);
            close(fd);
            return 0;
        }

        close(fd);

        for(tmp = 0; tmp < rd; tmp++)
        {
            CheckSum += MemBuf[tmp];
        }

        strncpy(MemBuf + rd, "DELTADCOK", 9);
        *(MemBuf + rd + 9) = CheckSum >> 24;
        *(MemBuf + rd + 10) = CheckSum >> 16;
        *(MemBuf + rd + 11) = CheckSum >> 8;
        *(MemBuf + rd + 12) = CheckSum;
        // memcpy(MemBuf+rd+9,&CheckSum,4);
        fd = open("/mnt/DcoKImage", O_CREAT | O_RDWR);
        wrd = write(fd, MemBuf, rd + 13);

        if(wrd != (rd + 13))
        {
            DEBUG_PRINTF_FWMAKER_DETAIL("write error wrd=0x%x, rd=0x%x\n", wrd, rd + 13);
        }
        else
        {
            DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/DcoKImage OK\n");
        }

    }
    else
    {
        free(MemBuf);
        DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/uImage open Error\n");
        return 0;
    }


    memset(MemBuf, 0xff, 0x100000D);
    CheckSum = rd = 0;
    fd = open("/mnt/rootfs_nor.img", O_RDWR);

    if(fd > 0)
    {
        if((rd = read(fd, MemBuf, 0x100000D)) <= 0)
        {
            DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/rootfs_nor.img read Error\n");
            free(MemBuf);
            close(fd);
            return 0;
        }

        close(fd);

        for(tmp = 0; tmp < rd; tmp++)
        {
            CheckSum += MemBuf[tmp];
        }

        strncpy(MemBuf + rd, "DELTADCOF", 9);
        *(MemBuf + rd + 9) = CheckSum >> 24;
        *(MemBuf + rd + 10) = CheckSum >> 16;
        *(MemBuf + rd + 11) = CheckSum >> 8;
        *(MemBuf + rd + 12) = CheckSum;
        //memcpy(MemBuf+rd+9,&CheckSum,4);
        fd = open("/mnt/DcoFImage", O_CREAT | O_RDWR);
        wrd = write(fd, MemBuf, rd + 13);

        if(wrd != (rd + 13))
        {
            DEBUG_PRINTF_FWMAKER_DETAIL("write error wrd=0x%x, rd=0x%x\n", wrd, rd + 13);
        }
        else
        {
            DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/DcoFImage OK\n");
        }
    }
    else
    {
        free(MemBuf);
        DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/rootfs_nor.img open Error\n");
        return 0;
    }

}