#include "tc_system.h"
#include "tc_thread.h"
#include "com_ttySx.h"

static com_ttySx_t ttyS4;
static com_ttySx_t ttyS5;
static com_ttySx_t ttyS6;
static int loop_quit = 0;
static tc_thread_t ttyS4_thread;
static tc_thread_t ttyS5_thread;
static tc_thread_t ttyS6_thread;

void* com_ttyS4_loop(void* argv)
{
    com_ttySx_t* dev = (com_ttySx_t*)argv;
    uint8_t rxbuff[18];
    uint8_t txbuff[18];
    int txlen=0;
    int rxlen=0;
    while(loop_quit == 0)
    {
        txlen = sprintf(txbuff, "Hello ttyS4");txbuff[txlen++]='\0';
        com_ttySx_send(dev, txbuff, txlen);
        com_ttySx_recv_with_timeout(dev, rxbuff, 12, 1000);
        msleep(1000);
        txlen = sprintf(txbuff, "Hello ttyS6");txbuff[txlen++]='\0';
        com_ttySx_send(dev, txbuff, txlen);
        com_ttySx_recv_with_timeout(dev, rxbuff, 12, 1000);
        msleep(1000);
    }
    return NULL;
}
void* com_ttyS5_loop(void* argv)
{
    com_ttySx_t* dev = (com_ttySx_t*)argv;
    uint8_t rxbuff[18];
    uint8_t txbuff[18];
    int txlen=0;
    int rxlen=0;
    while(loop_quit == 0)
    {
        rxlen = com_ttySx_recv_with_timeout(dev, rxbuff, 12, 1000);
        if (rxlen){
            if (strcmp(rxbuff, "Hello ttyS5")==0)
            {
                txlen = sprintf(txbuff, "Hello ttyS4");txbuff[txlen++]='\0';
                com_ttySx_send(dev, txbuff, txlen);
            }
        }
    }
    return NULL;
}
void* com_ttyS6_loop(void* argv)
{
    com_ttySx_t* dev = (com_ttySx_t*)argv;
    uint8_t rxbuff[18];
    uint8_t txbuff[18];
    int txlen=0;
    int rxlen=0;
    while(loop_quit == 0)
    {
        rxlen = com_ttySx_recv_with_timeout(dev, rxbuff, 12, 1000);
        if (rxlen){
            if (strcmp(rxbuff, "Hello ttyS6")==0)
            {
                txlen = sprintf(txbuff, "Hello ttyS4");txbuff[txlen++]='\0';
                com_ttySx_send(dev, txbuff, txlen);
            }
        }
    }
    return NULL;
}

void rs485_test(int loop)
{
    printf("%s(%d):%s | loop=%d\n", __FILE__, __LINE__, __FUNCTION__, loop);
    com_ttySx_init(&ttyS4, "/dev/ttyS4", 19200, 8, 'N', 1);
    printf("%s(%d):%s | loop=%d\n", __FILE__, __LINE__, __FUNCTION__, loop);
    com_ttySx_init(&ttyS5, "/dev/ttyS5", 19200, 8, 'N', 1);
    printf("%s(%d):%s | loop=%d\n", __FILE__, __LINE__, __FUNCTION__, loop);
    com_ttySx_init(&ttyS6, "/dev/ttyS6", 19200, 8, 'N', 1);
    printf("%s(%d):%s | loop=%d\n", __FILE__, __LINE__, __FUNCTION__, loop);
    tc_thread_init(&ttyS4_thread, com_ttyS4_loop, &ttyS4);
    printf("%s(%d):%s | loop=%d\n", __FILE__, __LINE__, __FUNCTION__, loop);
    tc_thread_init(&ttyS5_thread, com_ttyS5_loop, &ttyS5);
    printf("%s(%d):%s | loop=%d\n", __FILE__, __LINE__, __FUNCTION__, loop);
    tc_thread_init(&ttyS6_thread, com_ttyS6_loop, &ttyS6);
    printf("%s(%d):%s | loop=%d\n", __FILE__, __LINE__, __FUNCTION__, loop);
    loop_quit = 0;
    while(loop>0)
    {
        msleep(1000);
        loop--;
        printf("%s(%d):%s | loop=%d\n", __FILE__, __LINE__, __FUNCTION__, loop);
    }
    loop_quit = 1;
    printf("%s(%d):%s | loop_quit=%d\n", __FILE__, __LINE__, __FUNCTION__, loop_quit);
    tc_thread_exit(&ttyS4_thread);
    printf("%s(%d):%s | loop_quit=%d\n", __FILE__, __LINE__, __FUNCTION__, loop_quit);
    tc_thread_exit(&ttyS5_thread);
    printf("%s(%d):%s | loop_quit=%d\n", __FILE__, __LINE__, __FUNCTION__, loop_quit);
    tc_thread_exit(&ttyS6_thread);
    printf("%s(%d):%s | loop_quit=%d\n", __FILE__, __LINE__, __FUNCTION__, loop_quit);
    com_ttySx_exit(&ttyS4);
    printf("%s(%d):%s | loop_quit=%d\n", __FILE__, __LINE__, __FUNCTION__, loop_quit);
    com_ttySx_exit(&ttyS5);
    printf("%s(%d):%s | loop_quit=%d\n", __FILE__, __LINE__, __FUNCTION__, loop_quit);
    com_ttySx_exit(&ttyS6);
    printf("%s(%d):%s | loop_quit=%d\n", __FILE__, __LINE__, __FUNCTION__, loop_quit);
}