/*
 *  comtest.c
 *
 *  v 0.0 2021.6.3 
 *
 *  open port, read, write UART
 *
 */

#include "msw.h"
#include "i7k.h"
#include "i7000.h"
#include <string.h>

#define RCVBUF_SIZE 16

int main(int argc,char **argv)
{
	int fd,rs;						/* file descriptor */
	char cmd[16];					/* point to command string */
	char rbuf[RCVBUF_SIZE];		/* receive buffer */
	__tod_t texp;				/* to receive the expended time */
	int comport, s_s_comport, s_r_comport;
	char rx_str[32], tx_str[32];
	int s_s_idx = 0, s_r_idx, u_idx = 0;
	unsigned long baud = 115200;
	int addr = -1;	
	int timeout = 5;
	WORD wT;
	int num = 1, tmp_num = 1;
	unsigned long error1 = 0, error2 = 0;
	unsigned long test_num = 1, total = 0;

	int c;

        while((c=getopt(argc, argv, "a:b:c:n:r:s:u:")) != -1)
        {
                switch(c)
                {
/*
                        case 'a':
				addr = atoi(optarg);
                                break;

                        case 'c':
				comport = atoi(optarg);
                                break;
*/
                        case 'b':
				baud = atol(optarg);
                                break;

                        case 'n':
				num = atoi(optarg);
				tmp_num = num;
                                break;

                        case 'r':
				s_r_idx = atoi(optarg);
                                s_r_comport = s_r_idx + 2;
                                break;

                        case 's':
				s_s_idx = atoi(optarg);
                                s_s_comport = s_s_idx + 2;
                                break;
/*
                        case 'u':
				u_idx = atoi(optarg);
                                comport = 6 + u_idx;
                                break;
*/
                        case '?':
                                printf("wrong command\n");
                                break;
                }
        }

	//printf("comport = %d\n",comport);

	if(s_s_comport == s_r_comport)
	{
		comport = s_s_comport;
		rs = Open_Com(comport, baud, Data8Bit, NonParity, OneStopBit);

                if (rs > 0) {
                        printf("open port failed!\n");
                        return (-1);
                }
		
		//Disable_CTSRTS(comport);
	}
	else
	{
		rs = Open_Com(s_s_comport, baud, Data8Bit, NonParity, OneStopBit);

		if (rs > 0) {
			printf("open port failed!\n");
			return (-1);
		}

		rs = Open_Com(s_r_comport, baud, Data8Bit, NonParity, OneStopBit);

		if (rs > 0) {
			printf("open port failed!\n");
			return (-1);
		}
	
		//Disable_CTSRTS(s_s_comport);
		//Disable_CTSRTS(s_r_comport);
	}

	if(addr >= 0)
	{
		if(addr >= 10)
			sprintf(cmd, "$%dM\r", addr);
		else
			sprintf(cmd, "$0%dM\r", addr);
	}
	else
	{
		strcpy(cmd, "$01M\r");
	}

	//printf("cmd len : %ld\n",strlen(cmd));
	//while (getchar() != 27)
	while(num > 0)
	//while(1)
	{
			if(s_s_comport == s_r_comport)
			{
				sprintf(tx_str, "ttyS%d_test%d\r", s_s_idx, tmp_num);

	        		rs = Send_Receive_Cmd(comport, tx_str, rbuf, timeout, 0, &wT); 
				if(comport >= 10)
        	        	{
                	        	printf("COM%d(/dev/ttyS%d) Send    Command = %s\n", (s_s_idx + 2), s_s_idx, tx_str);
	                	        printf("COM%d(/dev/ttyS%d) Receive Command = %s\n", (s_r_idx + 2), s_r_idx, rbuf);
	        	        }
				else
				{
#if 1
					//1000 datas = 16 secs
					tx_str[strlen(tx_str) - 1] = '\0';
					printf("/dev/ttyS%d Send    : %s(len %ld)\n", s_s_idx, tx_str, strlen(tx_str));
					printf("/dev/ttyS%d Receive : %s(len %ld)\n", s_r_idx, rbuf, strlen(rbuf));
					if(strcmp(tx_str, rbuf))
					{	
						error1++;
						printf("ttyS%d --> ttyS%d Data %d : Send/Receive compare error\n", s_s_idx, s_r_idx, tmp_num); 	
						//if(error1 >= 10000)
							break;
					}
					printf("/dev/ttyS%d Send/Receive Test error = %ld\n", s_s_idx, error1);
#else
                	        	printf("COM%d(/dev/ttyO%d) Send    Command = %s\n", s_s_idx, s_s_idx, tx_str);
	                	        printf("COM%d(/dev/ttyO%d) Receive Command = %s\n", s_r_idx, s_r_idx, rbuf);
				
#endif
				}
				
				usleep(500);
			}
			else
			{
				//1000 datas = 6 secs
#if 1
				sprintf(tx_str, "ttyS%d_test%d\r", s_s_idx, tmp_num);
				Send_Cmd(s_s_comport, tx_str, 0);
#else
				sprintf(tx_str, "ttyS%d_test%d", s_s_idx, tmp_num);
				Send_String(s_s_comport, tx_str, 0);
#endif
				//usleep(100000);
				printf("/dev/ttyS%d Send: %s,cmd len: %ld\n", s_s_idx, tx_str, strlen(tx_str));

				memset(rx_str,0,sizeof(rx_str));
#if 1
				Receive_Cmd(s_r_comport, rx_str, 1, 0);
#else
				Receive_String(s_r_comport, rx_str, 100, 0);
#endif
				printf("/dev/ttyS%d Receive: %s,cmd len: %ld\n", s_r_idx, rx_str, strlen(rx_str));

				if(strcmp(tx_str, rx_str))
				{	
					error1++;
					printf("ttyS%d --> ttyS%d Data %d : Send/Receive compare error\n", s_s_idx, s_r_idx, tmp_num); 	
					//if(error1 >= 10000)
						break;
				}
				printf("ttyS%d --> ttyS%d : Send/Receive Test error1 = %ld\n", s_s_idx, s_r_idx, error1); 	
					
				
				usleep(10000);

#if 1
				sprintf(tx_str, "ttyS%d_test%d\r", s_r_idx, tmp_num);
				Send_Cmd(s_s_comport, tx_str, 0);
#else
				sprintf(tx_str, "ttyS%d_test%d", s_r_idx, tmp_num);
				Send_String(s_r_comport, tx_str, 0);
#endif
				//usleep(100000);
				printf("/dev/ttyS%d Send: %s,cmd len: %ld\n", s_r_idx, tx_str, strlen(tx_str));

				memset(rx_str,0,sizeof(rx_str));
#if 1
				Receive_Cmd(s_r_comport, rx_str, 1, 0);
#else
				Receive_String(s_s_comport, rx_str, 100, 0);
#endif
				printf("/dev/ttyS%d Receive: %s,cmd len: %ld\n", s_s_idx, rx_str, strlen(rx_str));
				if(strcmp(tx_str, rx_str))
				{	
					error2++;
					printf("ttyS%d --> ttyS%d Data %d : Send/Receive compare error\n", s_r_idx, s_s_idx, tmp_num); 	
					//if(error2 >= 10000)
						break;
				}
					
				printf("ttyS%d --> ttyS%d : Send/Receive Test error2 = %ld\n", s_r_idx, s_s_idx, error2); 	
				
				usleep(10000);
			}


		if(tmp_num <= 0)
		{
			tmp_num = num;
		}
		else
			tmp_num--;
		//printf("num = %d\n",num);
		
		num--;
	}

	printf("finish ...\n");
	if(s_s_comport == s_r_comport)
	{
		Close_Com(comport);
	}
	else
	{
		Close_Com(s_s_comport);
		Close_Com(s_r_comport);
	}

	return (0);
}
