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

                        initiated by Vern, Joseph
                           (since 2019/07/19)
=============================================================================*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/termios.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <linux/sockios.h>
#include <linux/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <math.h>//for pow
#include <unistd.h>
#include "define.h"
#include "FactoryConfig.h"

unsigned char buf_log_factoryconfig[SIZE_OF_LOG_BUFFER];

/*===========================================================================
FUNCTION: StoreLogMsg
DESCRIPTION:
PRE-CONDITION:
INPUT:
OUTPUT:
GLOBAL VARIABLES:
=============================================================================*/
#if SAVE_SYS_LOG_MSG_FACTORY_CONFIG_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][FactoryConfig]%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_FACTORY_CONFIG_SYSTEM_LOG("[%02d:%02d:%02d.%06d][FactoryConfig]%s \n",
            tm->tm_hour,
            tm->tm_min,
            tm->tm_sec,
            tv.tv_usec,
            DataString);

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


/**************************************************************************************/
/************This task will create Factory default confgiuration file *****************/
/***********and store it into mtdblock 10,11,12                               ****************/
/**************************************************************************************/

int main(int argc, char *argv[])
{
    struct SysConfigData SysConfig;
    unsigned int i, Chk;
    unsigned char *ptr;
    int fd, wrd;

    ptr = malloc(sizeof(struct SysConfigData));

    if(ptr == NULL)
    {
        SAVE_SYS_LOG_MSG_FACTORY_CONFIG("[FactoryConfig]main: malloc for SysConfigData NG");
        return 0;
    }

    memset(ptr, 0, sizeof(struct SysConfigData));
    memset(&SysConfig, 0, sizeof(struct SysConfigData));

    //Set default configuration
    strcpy(SysConfig.Eth0Interface.EthIpAddress, "192.168.0.11");
    strcpy(SysConfig.Eth0Interface.EthSubmaskAddress, "255.255.255.0");
    strcpy(SysConfig.Eth0Interface.EthGatewayAddress, "192.168.0.1");
    strcpy(SysConfig.Eth1Interface.EthIpAddress, "192.168.0.20");
    strcpy(SysConfig.Eth1Interface.EthSubmaskAddress, "255.255.255.0");
    strcpy(SysConfig.Eth1Interface.EthGatewayAddress, "192.168.0.1");
    SysConfig.BackendConnTimeout = 300; //300 seconds

    //copy default configuration to pointer
    memcpy(ptr, &SysConfig, sizeof(struct SysConfigData));

    //calculate CRC
    Chk = 0;

    for(i = 0; i < (sizeof(struct SysConfigData) - 4); i++)
    {
        Chk += *(ptr + i);
    }

    SysConfig.Checksum = Chk;

    fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR | O_CREAT);

    if(fd < 0)
    {
        SAVE_SYS_LOG_MSG_FACTORY_CONFIG("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin NG");

        free(ptr);
        return 0;
    }

    wrd = write(fd, &SysConfig, sizeof(struct SysConfigData));
    close(fd);

    if(wrd != (sizeof(struct SysConfigData)))
    {
        SAVE_SYS_LOG_MSG_FACTORY_CONFIG("[FactoryConfig]main: write /mnt/FactoryDefaultConfig.bin NG");

        free(ptr);
        return 0;
    }

    fd = open("/dev/mtdblock12", O_RDWR);

    if(fd < 0)
    {
        SAVE_SYS_LOG_MSG_FACTORY_CONFIG("[FactoryConfig]main: open /dev/mtdblock12 NG");

        free(ptr);
        return 0;
    }

    wrd = write(fd, &SysConfig, sizeof(struct SysConfigData));
    close(fd);

    if(wrd != (sizeof(struct SysConfigData)))
    {
        SAVE_SYS_LOG_MSG_FACTORY_CONFIG("[FactoryConfig]main: write /dev/mtdblock12 NG");

        free(ptr);
        return 0;
    }

    fd = open("/dev/mtdblock11", O_RDWR);

    if(fd < 0)
    {
        SAVE_SYS_LOG_MSG_FACTORY_CONFIG("[FactoryConfig]main: open /dev/mtdblock11 NG");

        free(ptr);
        return 0;
    }

    wrd = write(fd, &SysConfig, sizeof(struct SysConfigData));
    close(fd);

    if(wrd != (sizeof(struct SysConfigData)))
    {
        SAVE_SYS_LOG_MSG_FACTORY_CONFIG("[FactoryConfig]main: write /dev/mtdblock11 NG");

        free(ptr);
        return 0;
    }

    fd = open("/dev/mtdblock10", O_RDWR);

    if(fd < 0)
    {
        SAVE_SYS_LOG_MSG_FACTORY_CONFIG("[FactoryConfig]main: open /dev/mtdblock10 NG");

        free(ptr);
        return 0;
    }

    wrd = write(fd, &SysConfig, sizeof(struct SysConfigData));
    close(fd);

    if(wrd != (sizeof(struct SysConfigData)))
    {
        SAVE_SYS_LOG_MSG_FACTORY_CONFIG("[FactoryConfig]main: write /dev/mtdblock10 NG");

        free(ptr);
        return 0;
    }

    free(ptr);

    SAVE_SYS_LOG_MSG_FACTORY_CONFIG("[FactoryConfig]main: FactoryConfig OK");
}