/**
 * @file mfcc_sample_app.c
 *
 * @brief This file contains functions that provides demonstration of
 *        usage of TC TM Library APIs
 *
 * The functions provided in the file provides demonstration on
 * usage of TC TM library APIs
 */
/*********************************************************************/

#include <pthread.h>
#include "mfcc_tc_tm_sample.h"

/*
 *  added for FT
 */
#include "file_transfer.h"
#include "ft_sample_app_upload.h"

s_sock_info tctm_soc_inf;
FILE *inf;

/******** Automation Framework Start *********/

s_autom_user_input autom_user_input;

/******** Automation Framework Start *********/

/*
 * added for FT
 */
pthread_t ft_fsm_thread, ft_user_input, ft_tc_thread;

/*
 *
 *  This function read the time zone then set time zone as "UTC0"
 *
 */
void set_time_zone_utc0(void);


/*
 *
 *  This function read the time zone then set time zone as "UTC0"
 *
 */
void set_time_zone_utc0(void)
{

        char *tz = getenv("TZ");
        if (tz != 0)
        {
                printf("time zone read %s\n",tz);
        }
        else
        {
                printf("time zone not set yet\n");
        }

    // setting UTC0 as time zone
        setenv("TZ", "UTC0", 1);
        tzset();

        tz = getenv("TZ");
        if (tz != 0)
        {
                printf("time zone set %s\n",tz);
        }
        else
               {
                       printf("time zone set fail\n");
               }

       }


/**
 * @brief Main function that creates threads for sending TC and
 * receiving TM
 */
int main(int argc, char **argv)
{
    set_time_zone_utc0();   // setting UTC0 as time zone
    autom_user_input.autom_arg_cnt = argc;
    printf("Number of Arguments: %d \n",autom_user_input.autom_arg_cnt);
    //Enable Line buffering for stdout
    setvbuf(stdout, NULL, _IOLBF, 0);
    int i=1;
	for(; i<argc;i++)
    {
	long int autom_str_len = strlen(argv[i]);
	//printf("Argument no: %d Input String: %s Input String Length: %d \n",i,argv[i],autom_str_len);
	autom_user_input.autom_argv[i] = malloc(autom_str_len+2);
	autom_user_input.autom_argv_valid[i] = 1;
	memcpy(autom_user_input.autom_argv[i],argv[i],autom_str_len+1);
	printf("Argument no: %d String: %s \n",i,(char*)autom_user_input.autom_argv[i]);
    }

    pthread_t mfcc_tc_thread, mfcc_tm_thread;

    ////////////////////////////////////////
    //Read socket information from file
    /////////////////////////////////////////
    inf = fopen("gs_soc_cfg.txt", "r");

    if (inf == NULL) {
        printf("Error to open the file\n");
        exit(1);
    }

    fread(&tctm_soc_inf, sizeof(s_sock_info),1, inf);
    printf("GS TC TM socket address is %s %u \n",tctm_soc_inf.ip_addr,atoi(tctm_soc_inf.port_num));

    fclose(inf);

    CSP_Setup();

    /* Create thread that sends Telecommand binary data stream via socket interface */
    pthread_create(&mfcc_tc_thread, NULL, mfcc_usr_input_thread, NULL);
    /* Create thread that receives Telemetry binary data stream via socket interface */
    pthread_create(&mfcc_tm_thread, NULL, mfcc_tm_listener_thread, NULL);

    pthread_create(&ft_fsm_thread, NULL, ft_handler, NULL);
    pthread_create(&ft_tc_thread, NULL, mfcc_ft_tc_sender_thread, NULL);

    /* Wait until target thread exits */
    pthread_join(mfcc_tc_thread,NULL);

    pthread_join(ft_fsm_thread,NULL);
    pthread_join(ft_tc_thread,NULL);

    return 0;
}



void ft_init_ft_handler_thread(void)
{
    pthread_join(ft_fsm_thread,NULL);
    printf("FT handler thread created...\n");
}


void ft_init_ft_sus_res_thread(void)
{
    pthread_join(ft_user_input,NULL);
    printf("FT handler user input...\n");
}
