#include "tc_system.h"
#include "logger.h"
#include "config.h"
#include "com_ttySx.h"
#include "ptimer.h"
#include "acrel_DJSF1352RN.h"
#include "ast_XH.h"
#include "modbus_TCP_HW.h"
#include "goose_HW.h"
#include "stm32f103.h"
#include "tc_tcu.h"
#include "tcXHHW.h"
#include <ifaddrs.h>
// static int tcu_quit = 0;

// void sigint_handler(int sig)
// {
// 	tcu_quit = 1;
// }

// void parse_options(int argc, char** argv) 
// {
//     int c;

//     while ((c = getopt(argc, argv, "ct:")) != -1) {
//         switch (c) {
//         case 'c':
// 			config_filename = optarg;
//             break;
//         case 't':
//             // ttyS0_devname = optarg;
// 			break;
//         case '?':
//             break;
//         default:
//             exit(2);
//         }
//     }
// }


const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static char app_version[128] = {0};
char* formatAppVersion(void)
{
	int i;
	int month, mday, year, hour, min, sec;
    #define ASCII_CODE(x)  ((x)==32? 0:((x)-'0'))
    mday = ASCII_CODE(__DATE__[4])*10 + ASCII_CODE(__DATE__[5]);
    year = ASCII_CODE(__DATE__[7])*1000 + ASCII_CODE(__DATE__[8])*100 + ASCII_CODE(__DATE__[9])*10 + ASCII_CODE(__DATE__[10]);
    hour = ASCII_CODE(__TIME__[0])*10 + ASCII_CODE(__TIME__[1]);
    min  = ASCII_CODE(__TIME__[3])*10 + ASCII_CODE(__TIME__[4]);
    sec  = ASCII_CODE(__TIME__[6])*10 + ASCII_CODE(__TIME__[7]);
    #undef ASCII_CODE
  	for (i = 0; i < 12; i++)
  	{
    	if (strncmp(__DATE__, months[i], 3)==0)
    	{
      		month = i + 1;
     	 	break;
    	}
  	}

    snprintf(app_version, 128, "app_XHHW_V1R1-%04d%02d%02d %02d:%02d:%02d", year, month, mday, hour, min, sec);
    return app_version;
}

char version[10] ="1.1.7";
void  printVersion(char* arg)
{
    int len;
    static uint8_t comfire = 0;
    char src_path[32] = {0};
    if(comfire == 1)
    {
        printi("SW:%s\n",version);
        return ;
    }
    len = readlink(arg, src_path, sizeof(src_path) - 1);
    comfire = 1;
    if (len == -1) {
        printi("this is sr_path %s:%s\n", arg,version);
        return ;
    }
    src_path[len] = '\0';
    printi("SW:%s:%s\n", src_path,version);
}

int get_interface_ip(const char *ifname) {
    struct ifaddrs *ifap, *ifa;
    struct sockaddr_in *sa;
    int found = 0;
    char ip_buf[32];

    if (ifname == NULL) {
        return -1;
    }

    // 1. ???????????
    if (getifaddrs(&ifap) == -1) {
        return -1;
    }

    // 2. ???????????? IPv4 ??
    for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
        // ?????
        // - ?????
        // - ???? IPv4?AF_INET?
        // - ?????
        if (strcmp(ifa->ifa_name, ifname) == 0 &&
            ifa->ifa_addr != NULL &&
            ifa->ifa_addr->sa_family == AF_INET) {

            // ?? IPv4 ??
            sa = (struct sockaddr_in *)ifa->ifa_addr;
            inet_ntop(AF_INET, &(sa->sin_addr), ip_buf, 32);
            printi("ip:%s\n",ip_buf);
            found = 1;
            break;  
        }
    }

    freeifaddrs(ifap);

    if (!found) {
        printe("no ip\n");
        return -1;
    }

    return 0;
}



extern void rs232_test(int loop);

int main(int argc, char* argv[])
{
    signal(SIGPIPE, SIG_IGN);
    // signal(SIGINT, sigint_handler);
	// parse_options(argc, argv);

    clog_init("/home/root/log/tcu", CLOG_BUFF_SIZE*60*24, 30);
    tcu_BSP_init();
	tcu_config_init(config_filename);
   // printd("\n\r\tSW:%s\n", formatAppVersion());
   
    clog_bind_serverip(tcu_config.xhlog_ip, tcu_config.xhlog_port);

    extern void print_stationID();
    print_stationID();
    printVersion(argv[0]);
    
    get_interface_ip("eth0");

    set_GUNLED_on(0, 0);
    set_GUNLED_on(1, 0);
    set_GUNLED_on(2, 0);
    set_GUNLED_on(3, 0);

    set_KMD_out(0, 0);
    set_KMD_out(1, 0);
    set_KMD_out(2, 0);
    set_KMD_out(3, 0);

    // for (;;)
    // {
    //     // rs232_test(10);

    //     set_SYSLED_on(0, 1);
    //     printf("\nget_TCUID:%4d, get_USERKEY:%d\n\n", get_TCUID(), get_USEKEY());

    //     printf("set_GUNLED_on(...)\n");
    //     set_GUNLED_on(0, 1);
    //     printf("set_GUNLED_on(0)\n"); getchar();
    //     set_GUNLED_on(1, 1);
    //     printf("set_GUNLED_on(1)\n"); getchar();
    //     set_GUNLED_on(2, 1);
    //     printf("set_GUNLED_on(2)\n"); getchar();
    //     set_GUNLED_on(3, 1);
    //     printf("set_GUNLED_on(3)\n"); getchar();

    //     set_SYSLED_on(0, 0);
    //     printf("set_GUNLED_off(ALL)...\n");
    //     set_GUNLED_on(0, 0);
    //     set_GUNLED_on(1, 0);
    //     set_GUNLED_on(2, 0);
    //     set_GUNLED_on(3, 0);
    //     printf("set_GUNLED_off(all) done.\n"); getchar();

    //     set_SYSLED_on(1, 1);
    //     printf("set_KMD_out(...)\n");
    //     set_KMD_out(0, 1);
    //     printf("set_KMD_out(0)\n");getchar();
    //     set_KMD_out(1, 1);
    //     printf("set_KMD_out(1)\n");getchar();
    //     set_KMD_out(2, 1);
    //     printf("set_KMD_out(2)\n");getchar();
    //     set_KMD_out(3, 1);
    //     printf("set_KMD_out(3)\n");getchar();

    //     printf("get_KMDFB:%d, %d, %d, %d\n", get_KMDFB(0), get_KMDFB(1), get_KMDFB(2), get_KMDFB(3));

    //     set_SYSLED_on(1, 0);
    //     printf("set_KMD_off(ALL)...\n");
    //     set_KMD_out(0, 0);
    //     set_KMD_out(1, 0);
    //     set_KMD_out(2, 0);
    //     set_KMD_out(3, 0);
    //     printf("set_KMD_off(ALL) done.\n");
    //     msleep(500);
    //     printf("get_KMDFB:%d, %d, %d, %d\n", get_KMDFB(0), get_KMDFB(1), get_KMDFB(2), get_KMDFB(3));
    //     getchar();

    // }


    ccu_TCU_init();
    ccu_TCU_run(0);
    ccu_TCU_exit();
    
    tcu_BSP_exit();
    tcu_config_exit();
    clog_exit();
	return 0;
}