#include "mySystem.h"
#include "myLog.h"
#include "myComm.h"


#include <string>



LOCAL int com_scan_send_data_to_app(stru_msg_head *p_head, uint16_t len, uint32_t mq_id)
{
    if(NULL == p_head || 0 == len)
    {
        LOG_E("com_scan_send_data_to_app p_head null or len 0");
        return -1;
    }

    stru_app_msg_queue *p_mq = NULL;
    stru_app *p_app = NULL;
    uint32_t event = 0;

    if(mq_id == ENUM_MQ_COM_TO_IEC && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_COM_TO_IEC)) && len < p_mq->msg_size)
    {
        p_app = app_get_ptr(ENUM_APP_IEC);
        event = EV_IEC_RX_COM;
    }
    else if(mq_id == ENUM_MQ_COM_TO_SELF_PTL && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_COM_TO_SELF_PTL)) && len < p_mq->msg_size)
    {
        p_app = app_get_ptr(ENUM_APP_SELF_PTL);
        event = EV_SELF_PTL_RX_COM;
    }
    else
    {
        LOG_E("com_scan_send_data_to_app mq_id %d or len %d error", mq_id, len);
        return -1;
    }

    if(0 != task_msg_queue_send(p_mq->p_msg_queue, p_head, p_mq->msg_size))
    {
        LOG_E("com_scan_send_data_to_app send to iec failed");
        return -1;
    }

    if(0 != task_event_send(p_app->p_event, event))
    {
        LOG_E("com_scan_send_data_to_app send to iec event failed");
        task_msg_queue_try_recv(p_mq->p_msg_queue, p_app->rtx.rx, p_mq->msg_size);
        return -1;
    }

    return 0;
}


void com_recv_data(uint32_t itfs, const uint8_t *p_rx, uint16_t rx_len)
{
    if(NULL == p_rx || 0 == rx_len)
    {
        LOG_E("com_recv_data p_rx null or len 0");
        return;
    }
    stru_app *p_app = NULL;
    if(NULL == (p_app = app_get_ptr(ENUM_APP_COM_SCAN)))
    {
        LOG_E("com_recv_data app_get_ptr failed");
        return;
    }

    stru_msg_head *p_head = (stru_msg_head *)p_app->rtx.tx;
    p_head->interface = itfs;
    p_head->len = rx_len;
    memcpy(p_head->data, p_rx, rx_len);

    uint32_t interface = com_channel_interface_get(ENUM_COMM_TCP_C_0);
    if(interface == itfs)
    {
        com_scan_send_data_to_app(p_head, rx_len + sizeof(stru_msg_head), ENUM_MQ_COM_TO_SELF_PTL);
    }
    else
    {
        stru_protocol_head *p= (stru_protocol_head *)p_head->data;

        if(p->icp67.head1 == 0x67 && p->icp67.head2 == 0x67)
        {
            com_scan_send_data_to_app(p_head, rx_len + sizeof(stru_msg_head), ENUM_MQ_COM_TO_SELF_PTL);
        }
        else if(p->iec.head == 0x10 || p->iec.head == 0x68)
        {
            com_scan_send_data_to_app(p_head, rx_len + sizeof(stru_msg_head), ENUM_MQ_COM_TO_IEC);
        }
    }
}

void com_send_data(uint8_t *data)
{
    if(NULL == data)
    {
        LOG_E("com_send_data data null");
        return;
    }

    stru_msg_head *p_head = (stru_msg_head *)data;
    
    int id = p_head->interface >> 16;
    int fd = p_head->interface & 0xFFFF;

    // LOG_I("com_send_data interface %x, id %d fd %d len %d", p_head->interface, id, fd, p_head->len);

    comm_send(id, fd, p_head->data, p_head->len);
}



int app_com_scan_init1(void *arg)
{
    return 0;
}

int app_com_scan_init2(void *arg)
{
    return 0;
}


void *app_com_scan(void *arg)
{
    if(NULL == arg)
    {
        LOG_E("app_com_scan arg null");
        return NULL;
    }

    stru_app *p_app = (stru_app *)arg;

    uint32_t event;
    stru_app_msg_queue *p_mq = NULL;
    stru_msg_head *p_head = NULL;
    stru_rtx_data *p_rtx = &p_app->rtx;

    while (1)
    {
        task_event_recv(p_app->p_event, 
            EV_TIMER1 | EV_TIMER2 | EV_TIMER3 | EV_COM_RX_IEC | EV_COM_RX_SELF_PTL,  
            TASK_EVENT_FLAG_OR | TASK_EVENT_FLAG_CLEAR,
            TASK_EVENT_WAIT_FOREVER,
            &event);

        if((event & EV_COM_RX_IEC) && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_IEC_TO_COM)))
        {
            while (1)
            {
                if(0 != task_msg_queue_try_recv(p_mq->p_msg_queue, p_rtx->rx, p_mq->msg_size))
                {
                    break;
                }
                  
                com_send_data(p_rtx->rx);
            }
        }

        if((event & EV_COM_RX_SELF_PTL) && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_SELF_PTL_TO_COM)))
        {
            while (1)
            {
                if(0 != task_msg_queue_try_recv(p_mq->p_msg_queue, p_rtx->rx, p_mq->msg_size))
                {
                    break;
                }

                com_send_data(p_rtx->rx);
            }
            
        }

        if(event & EV_TIMER1)
        {
            ;
        }

        if(event & EV_TIMER2)
        {
            ;
        }

        if(event & EV_TIMER3)
        {
            p_app->run_cnt++;
        }
    }
}


