#include "mySystem.h"
#include "myLog.h"
#include "myIcp67.h"
#include "method.h"
#include "parse_cfg.h"
#include "self_ptl.h"

typedef struct
{
    uint16_t rptr;
    uint16_t wptr;
    uint16_t size;
    uint16_t cnt;
    uint8_t buf[2048];
}stru_self_rx;

typedef struct
{
    uint32_t interface;
    stru_icp67 icp67;
    stru_genneral_method method;
    stru_self_rx icp67_rx;
    stru_self_rx iec_rx;
    uint8_t temp_buf[2048];
}stru_self_ptl;

stru_self_ptl g_self_ptl = 
{
    .method = 
    {
        .param_get_cb = NULL,
        .param_pop_out_cb = param_pop_out,
        .param_set_cb = NULL,
        .iec_point_tbl_get_cb = NULL,
        .iec_point_tbl_pop_out_cb = iec_point_tbl_pop_out,
        .time_set_cb = NULL,
        .self_check_get_cb = NULL,
        .self_check_pop_out_cb = self_check_pop_out,
        .upgrade_start_cb = NULL,
        
        .dir_read_cb = NULL,
        .dir_pop_out_cb = dir_pop_out,
        .file_read_act_cb = NULL,
        .file_read_act_confirm_cb = file_read_act_confirm,
        .file_read_cb = file_read,
        .file_read_confirm_cb = NULL,
        .file_write_act_cb = NULL,
        .file_write_act_confirm_cb = file_write_act_confirm,
        .file_write_cb = NULL,
        .file_write_confirm_cb = file_write_confirm,
        .file_write_end_cb = file_write_end,
        .file_write_end_confirm_cb = NULL,

        .zip_dir_pop_out_cb = zip_dir_pop_out,
        .zip_file_pop_out_cb = zip_file_pop_out,

        .mx_trans_cb = mx_trans,
        .st_trans_cb = st_trans,
        .dd_trans_cb = dd_trans,
        .soe_trans_cb = soe_trans,
        .fault_trans_cb = fault_trans,
        .mx_change_cb = mx_change,
    },
    .icp67_rx = 
    {
        .rptr = 0,
        .wptr = 0,
        .size = 2048,
        .cnt = 0,
    },
    .iec_rx = 
    {
        .rptr = 0,
        .wptr = 0,
        .size = 2048,
        .cnt = 0,
    },
};

stru_genneral_method *self_ptl_method_ptr_get()
{
    return &g_self_ptl.method;
}

stru_icp67 *self_ptl_icp67_ptr_get()
{
    return &g_self_ptl.icp67;
}

LOCAL void put_rx_data(stru_self_rx *p_slef_rx, uint8_t *p_rx, uint16_t len)
{
    if(NULL == p_slef_rx || NULL == p_rx || 0 == len)
    {
        LOG_E("put_rx_data p_slef_rx NULL or p_rx NULL or len 0");
        return;
    }

    for(uint16_t i = 0; i < len; i++)
    {
        p_slef_rx->buf[p_slef_rx->wptr] = p_rx[i];
        p_slef_rx->wptr = (p_slef_rx->wptr + 1) % p_slef_rx->size;
        p_slef_rx->cnt++;

        if(p_slef_rx->cnt > p_slef_rx->size)
        {
            p_slef_rx->cnt--;
            p_slef_rx->rptr = (p_slef_rx->rptr + 1) % p_slef_rx->size;

            // LOG_E("put_rx_data cnt %d, rptr %d, wptr %d", p_slef_rx->cnt, p_slef_rx->rptr, p_slef_rx->wptr);
        }
    }
}

LOCAL void get_rx_data(stru_self_rx *p_slef_rx, uint8_t *p_data, uint16_t *p_data_len)
{
    if(NULL == p_slef_rx || NULL == p_data || NULL == p_data_len)
    {
        LOG_E("get_rx_data p_slef_rx NULL or p_data NULL or p_data_len NULL");
        return;
    }

    uint16_t pos = p_slef_rx->rptr;

    for(uint16_t i = 0; i < p_slef_rx->cnt; i++)
    {
        p_data[i] = p_slef_rx->buf[pos];
        pos = (pos + 1) % p_slef_rx->size;
    }
    
    (*p_data_len) = p_slef_rx->cnt;
}

LOCAL int self_ptl_iec104_check_ctrl(stru_iec104_apci *p)
{
    if(NULL == p)
    {
        return ERR_FRAME;
    }

    if(p->len > 4)
    {
        if((p->ctrl1 & 0x01) == 0 && (p->ctrl3 & 0x01) == 0)
        {
            return I_FRAME;
        }
    }
    else
    {   
        if((p->ctrl1 == 1) && (p->ctrl2 == 0) && (p->ctrl3 & 0x01) == 0)
        {
            return S_FRAME;
        }

        if((p->ctrl1 & 0x03) == 0x03 && (p->ctrl2 == 0) && (p->ctrl3 == 0) && (p->ctrl4 == 0))
        {
            return U_FRAME;
        }
    }
    return ERR_FRAME;
}

LOCAL int self_ptl_iec104_chech_asdu(stru_iec104_apci *p)
{
    if(NULL == p)
    {   
        return -1;
    }

    if(p->len < 4 + sizeof(stru_iec104_asdu))
    {
        return -1;
    }

    stru_iec104_asdu *p_asdu = (stru_iec104_asdu *)(p->data);

    if(p_asdu->ti == 0)
    {
        return -1;
    }

    if((p_asdu->vsq & 0x7f) == 0)
    {
        return -1;
    }

    if((p_asdu->cot & 0xFF00) != 0)
    {
        return -1;
    }

    uint8_t cot = p_asdu->cot & 0x00FF;
    if(cot == 0 || cot == 11 || cot == 12 || (cot >= 14 && cot <= 19) || (cot >= 37 && cot <= 43) || cot >= 51)
    {
        return -1;
    }

    return 0;
}

LOCAL int self_ptl_search_iec_frame(uint8_t *p_data, uint16_t len, uint16_t *p_pos, uint16_t *p_valid_len)
{
    if(NULL == p_data || 0 == len || NULL == p_pos || NULL == p_valid_len)
    {
        // LOG_E("self_ptl_search_iec_frame p_data NULL or len 0 or p_pos NULL or p_valid_len NULL");
        return -1;  
    }

    for(uint16_t i = 0; i < len; i++)
    {
        if(i + sizeof(stru_iec104_apci) > len)
        {
            break;
        }

        stru_iec104_apci *p_apci = (stru_iec104_apci *)&p_data[i];

        if(p_apci->start != IEC_START)
        {
            continue;
        }

        if(p_apci->len + 2 + i > len)
        {
            break;
        }

        int ret = self_ptl_iec104_check_ctrl(p_apci);
        if(ret == ERR_FRAME)
        {
            continue;
        }

        if(ret == I_FRAME && 0 != self_ptl_iec104_chech_asdu(p_apci))
        {
            continue;
        }

        *p_pos = i;
        *p_valid_len = p_apci->len + 2;
        return 0;
    }

    return -1;
}

LOCAL void self_ptl_search_first_head(uint8_t *p_data, uint16_t len, uint8_t head, uint16_t *p_pos)
{
    if(NULL == p_data || 0 == len || NULL == p_pos)
    {
        return;
    }

    uint16_t i = 0;

    (*p_pos) = 0;
    for(i = 0; i < len; i++)
    {
        if(p_data[i] == head)
        {
            *p_pos = i;
            return;
        }
    }

    if((*p_pos) == 0)
    {
        (*p_pos) = len;
    }
}

void self_ptl_data_tx(uint8_t *p_tx, uint16_t len, void *arg)
{
    if(NULL == p_tx || 0 == len || NULL == arg)
    {
        LOG_E("self_ptl_data_tx p_tx NULL or len 0 or arg NULL");
        return;
    }

    if(len + sizeof(stru_msg_head) > MSG_SIZE)
    {
        LOG_E("self_ptl_data_tx len %d too long", len);
        return;
    }
    
    uint32_t interface = *(uint32_t *)arg;
    stru_rtx_data *p_rtx = NULL;
    stru_app *p_app = NULL;
    stru_app_msg_queue *p_mq = NULL;

    if(NULL == (p_app = app_get_ptr(ENUM_APP_SELF_PTL)))
    {
        LOG_E("self_ptl_data_tx app_get_ptr failed");
        return;
    }

    p_rtx = &p_app->rtx;
    stru_msg_head *p_head = (stru_msg_head *)p_rtx->tx;

    p_head->interface = interface;
    p_head->len = len;
    memcpy(p_head->data, p_tx, len);

    if(NULL == (p_mq = app_get_msg_queue_ptr(ENUM_MQ_SELF_PTL_TO_COM)))
    {
        LOG_E("self_ptl_data_tx app_get_msg_queue_ptr failed");
        return;
    }

    if(0 != task_msg_queue_send(p_mq->p_msg_queue, p_rtx->tx, p_mq->msg_size))
    {
        LOG_E("self_ptl_data_tx task_msg_queue_send failed");
        return;
    }

    if(NULL == (p_app = app_get_ptr(ENUM_APP_COM_SCAN)) || 0 != task_event_send(p_app->p_event, EV_COM_RX_SELF_PTL))
    {
        LOG_E("self_ptl_data_tx task_event_send failed");
        task_msg_queue_try_recv(p_mq->p_msg_queue, p_rtx->rx, p_mq->msg_size);

        return;
    }
}

LOCAL void self_ptl_search_frame(uint8_t *p_rx, uint16_t len, stru_icp67 *p_icp67)
{
    if(NULL == p_rx || 0 == len || NULL == p_icp67)
    {
        LOG_E("self_ptl_search_frame p_rx NULL or len 0 or p_icp67 NULL or p_data NULL or p_data_len NULL");
        return;
    }

    put_rx_data(&g_self_ptl.icp67_rx, p_rx, len);
    put_rx_data(&g_self_ptl.iec_rx, p_rx, len);

    uint16_t data_len = 0;
    uint16_t pos = 0;
    uint16_t valid_len = 0;
    int ret = -1;

    if(p_icp67->search_frame_cb)
    {
        do
        {
            pos = 0;
            valid_len = 0;
            get_rx_data(&g_self_ptl.icp67_rx, g_self_ptl.temp_buf, &data_len);
            ret = p_icp67->search_frame_cb(g_self_ptl.temp_buf, data_len, &pos, &valid_len);

            if(0 == ret)
            {
                g_self_ptl.icp67_rx.rptr += (pos + valid_len);
                g_self_ptl.icp67_rx.rptr %= g_self_ptl.icp67_rx.size;
                g_self_ptl.icp67_rx.cnt -= (pos + valid_len);
                
                stru_head *p_head = (stru_head *)&g_self_ptl.temp_buf[pos];

                if(p_head->dev_addr == DEV_MAINTENANCE_ADDR)
                {
                    uint32_t interface = com_channel_interface_get(ENUM_COMM_TCP_S_0);
                    self_ptl_data_tx(g_self_ptl.temp_buf + pos, valid_len, (void *)&interface);
                }
                else
                {
                    if(p_icp67->decode_cb)
                    {
                        p_icp67->decode_cb(p_icp67, g_self_ptl.temp_buf + pos, valid_len);
                    }
                }
            }
        }while(ret == 0);
    }

    get_rx_data(&g_self_ptl.icp67_rx, g_self_ptl.temp_buf, &data_len);
    self_ptl_search_first_head(g_self_ptl.temp_buf, data_len, 0x67, &pos);
    g_self_ptl.icp67_rx.rptr += pos;
    g_self_ptl.icp67_rx.rptr %= g_self_ptl.icp67_rx.size;
    g_self_ptl.icp67_rx.cnt -= pos;
    
    do
    {
        pos = 0;
        valid_len = 0;
        get_rx_data(&g_self_ptl.iec_rx, g_self_ptl.temp_buf, &data_len);
        ret = self_ptl_search_iec_frame(g_self_ptl.temp_buf, data_len, &pos, &valid_len);
        if(0 == ret)
        {
            g_self_ptl.iec_rx.rptr += (pos + valid_len);
            g_self_ptl.iec_rx.rptr %= g_self_ptl.iec_rx.size;
            g_self_ptl.iec_rx.cnt -= (pos + valid_len);

            uint32_t interface = com_channel_interface_get(ENUM_COMM_TCP_S_0);
            self_ptl_data_tx(g_self_ptl.temp_buf + pos, valid_len, (void *)&interface);
        }
    } while (ret == 0);
    
    get_rx_data(&g_self_ptl.iec_rx, g_self_ptl.temp_buf, &data_len);
    self_ptl_search_first_head(g_self_ptl.temp_buf, data_len, IEC_START, &pos);
    g_self_ptl.iec_rx.rptr += pos;
    g_self_ptl.iec_rx.rptr %= g_self_ptl.iec_rx.size;
    g_self_ptl.iec_rx.cnt -= pos;
}

void self_ptl_data_rx(uint8_t *p_data)
{
    if(NULL == p_data)
    {
        LOG_E("self_ptl_data_rx p_data NULL");
        return;
    }
    
    stru_msg_head *p_head = (stru_msg_head *)p_data;
    uint32_t interface = p_head->interface;
    uint16_t len = p_head->len;
    uint8_t *p_rx = p_head->data;

    stru_icp67 *p_icp67 = &g_self_ptl.icp67;

    self_ptl_search_frame(p_rx, len, p_icp67);

    // if(p_rx[0] == 0x67)
    // {
    //     stru_head *p_67_head = (stru_head *)p_rx;

    //     if(p_67_head->dev_addr == DEV_MAINTENANCE_ADDR)
    //     {
    //         uint32_t interface = com_channel_interface_get(ENUM_COMM_TCP_S_0);
    //         self_ptl_data_tx(p_rx, len, (void *)&interface);
    //     }
    //     else
    //     {
    //         if(p_icp67->decode_cb)
    //         {
    //             p_icp67->decode_cb(p_icp67, p_rx, len);
    //         }
    //     }
    // }
    // else if(p_rx[0] == IEC_START)
    // {
    //     uint32_t interface = com_channel_interface_get(ENUM_COMM_TCP_S_0);
    //     self_ptl_data_tx(p_rx, len, (void *)&interface);
    // }

}

void self_ptl_task()
{
    if(g_self_ptl.icp67.timer_handler_cb)
    {
        g_self_ptl.icp67.timer_handler_cb(&g_self_ptl.icp67);
    }
}

void self_ptl_set_interface(uint32_t interface)
{
    g_self_ptl.interface = interface;

    LOG_I("self_ptl_init interface %d, %x", g_self_ptl.interface, g_self_ptl.interface);
}


void self_ptl_init()
{
    // g_self_ptl.interface = com_channel_interface_get(ENUM_COMM_TCP_C_0);
    

    stru_icp67 *p_icp67 = &g_self_ptl.icp67;
    icp67_init(p_icp67, self_ptl_data_tx, (void *)&g_self_ptl.interface);

    stru_app_cfg *p_app_cfg = get_app_cfg_ptr();
    if(NULL == p_app_cfg)
    {
        LOG_E("self_ptl_init p_app_cfg NULL");
        return;
    }

    icp67_set_param_cfg_md5(p_icp67, p_app_cfg->p_param_cfg->md5);

    stru_genneral_method *p_method = &g_self_ptl.method;

    icp67_set_param_get_cb(&p_method->param_get_cb);
    icp67_set_param_pop_out_cb(p_method->param_pop_out_cb);
    icp67_set_param_set_cb(&p_method->param_set_cb);
    icp67_set_iec_point_tbl_get_cb(&p_method->iec_point_tbl_get_cb);
    icp67_set_iec_point_tbl_pop_out_cb(p_method->iec_point_tbl_pop_out_cb);
    icp67_set_time_set_cb(&p_method->time_set_cb);
    icp67_set_self_check_get_cb(&p_method->self_check_get_cb);
    icp67_set_self_check_pop_out_cb(p_method->self_check_pop_out_cb);
    icp67_set_upgrade_start_cb(&p_method->upgrade_start_cb);

    icp67_set_dir_read_cb(&p_method->dir_read_cb);
    icp67_set_dir_pop_out_cb(p_method->dir_pop_out_cb);
    icp67_set_file_read_act_cb(&p_method->file_read_act_cb);
    icp67_set_file_read_act_confirm_cb(p_method->file_read_act_confirm_cb);
    icp67_set_file_read_cb(p_method->file_read_cb);
    icp67_set_file_read_confirm_cb(&p_method->file_read_confirm_cb);
    icp67_set_file_write_act_cb(&p_method->file_write_act_cb);
    icp67_set_file_write_act_confirm_cb(p_method->file_write_act_confirm_cb);
    icp67_set_file_write_cb(&p_method->file_write_cb);
    icp67_set_file_write_confirm_cb(p_method->file_write_confirm_cb);
    icp67_set_file_write_end_cb(p_method->file_write_end_cb);
    icp67_set_file_write_end_confirm_cb(&p_method->file_write_end_confirm_cb);

    icp67_set_zip_dir_pop_out_cb(p_method->zip_dir_pop_out_cb);
    icp67_set_zip_file_pop_out_cb(p_method->zip_file_pop_out_cb);

    icp67_set_mx_trans_cb(p_method->mx_trans_cb);
    icp67_set_st_trans_cb(p_method->st_trans_cb);
    icp67_set_dd_trans_cb(p_method->dd_trans_cb);
    icp67_set_soe_trans_cb(p_method->soe_trans_cb);
    icp67_set_fault_trans_cb(p_method->fault_trans_cb);
    icp67_set_mx_change_cb(p_method->mx_change_cb);
}

// 你原有的分割函数，完全不动
std::vector<std::string> split(const std::string &s, char delimiter) 
{
    std::vector<std::string> tokens;
    std::string token;
    for (char c : s) 
    {
        if (c == delimiter) 
        {
            tokens.push_back(token);
            token.clear();
        } 
        else 
        {
            token += c;
        }
    }
    tokens.push_back(token);
    return tokens;
}

// 改造后的提取函数：提取 param 和 TERM_ID
void extract_saddr(const std::string &str, std::string &param, std::string &term_id) 
{
    // 按 . 分割：self_ptl | param | TERM_ID
    auto parts = split(str, '.');
    
    // 必须至少 3 段才有效
    if (parts.size() >= 3) 
    {
        param = parts[1];    // 第二段：param
        term_id = parts[2];  // 第三段：TERM_ID
    }
}

void self_ptl_signal_change_callback(std::string saddr, SIGNAL_CTRL_STEP step, uint8_t data_type, void *p_data)
{
    std::string value = "";
    if(p_data)
    {   
        value = dc_get_signal_val(p_data, data_type);
    }

    std::string step_str = "";
    switch(step)
    {
        case SIGNAL_CTRL_STEP::READY:
            step_str = "READY";
            break;
        case SIGNAL_CTRL_STEP::SELECT:
            step_str = "SELECT";
            break;
        case SIGNAL_CTRL_STEP::DIRECT:
            step_str = "DIRECT";
            break;
        case SIGNAL_CTRL_STEP::CANCEL:
            step_str = "CANCEL";
            break;
    }

    std::string data_type_str = "";
    data_type_str = dc_get_data_type_str_by_id(data_type);

    LOG_I("self_ptl_signal_change_callback saddr %s, step %s, data_type %s, value %s", saddr.c_str(), step_str.c_str(), data_type_str.c_str(), value.c_str());

    std::string signal_type = "", no = "";

    extract_saddr(saddr, signal_type, no);

    LOG_I("signal_type %s, no %s", signal_type.c_str(), no.c_str());

    stru_icp67 *p_icp67 = &g_self_ptl.icp67;
    stru_app_cfg *p_app_cfg = get_app_cfg_ptr();

    if(signal_type == "co")
    {

    }
    else if(signal_type == "param")
    {
        if(step == SIGNAL_CTRL_STEP::DIRECT)
        {
            if(g_self_ptl.method.param_set_cb)
            {
                for(uint32_t i = 0; i < p_app_cfg->p_param_cfg->vec_param.size(); i++)
                {
                    stru_param_cfg_data *p = &p_app_cfg->p_param_cfg->vec_param.at(i);
                    if(p->desc == no)
                    {
                        uint8_t temp[256] = {0};
                        stru_ti_1_2_data_info *p_data_info = (stru_ti_1_2_data_info *)temp;
                        p_data_info->data_addr = p->inf;
                        p_data_info->data_type = p->type;
                        p_data_info->data_len = self_ptl_get_type_len(p->type);
                        std::string val = dc_get_signal_val(p->p_data, p->type);
                        strcpy((char *)p_data_info->data, val.c_str());
                        g_self_ptl.method.param_set_cb(p_icp67, temp, 1);
                        break;
                    }
                }
            }
        }
    }
}

int self_ptl_do_signal_out(stru_app *p_app)
{
    stru_app_cfg *p_app_cfg = get_app_cfg_ptr();
    if(NULL == p_app_cfg)
    {
        LOG_E("self_ptl_do_signal_out p_app_cfg NULL");
        return -1;
    }

    int ret = 0;
    ret |= dc_signal_out("self_ptl.run_cnt", "self_ptl.run_cnt", DATA_TYPE_U32, &p_app->run_cnt);
    if(ret != 0)
    {
        goto ERR;
    }

    // LOG_I("***********st_info_size %d", p_app_cfg->st_info_vec.size());
    
    for(uint32_t i = 0; i < p_app_cfg->st_info_vec.size(); i++)
    {
        stru_iec_st_info *p = &p_app_cfg->st_info_vec.at(i);
    
        p->saddr = "self_ptl.st." + std::to_string(i);
        ret |= dc_signal_out(p->saddr, p->desc, DATA_TYPE_U8, &p->data);
        // LOG_I("no %d, saddr %s, desc %s", i, p->saddr.c_str(), p->desc.c_str());
    }

    if(ret != 0)
    {
        goto ERR;
    }

    for(uint32_t i = 0; i < p_app_cfg->mx_info_vec.size(); i++)
    {
        stru_iec_mx_info *p = &p_app_cfg->mx_info_vec.at(i);
        p->saddr = "self_ptl.mx." + std::to_string(i);
        ret |= dc_signal_out(p->saddr, p->desc, DATA_TYPE_F32, &p->data);
    }
    
    if(ret != 0)
    {
        goto ERR;
    }

    for(uint32_t i = 0; i < p_app_cfg->co_info_vec.size(); i++)
    {
        stru_iec_co_info *p = &p_app_cfg->co_info_vec.at(i);
        p->saddr = "self_ptl.co." + std::to_string(i);
        // ret |= dc_signal_out(p->saddr, p->desc, DATA_TYPE_U8, &p->data);
        ret |= dc_signal_yk(p->saddr, p->desc, DATA_TYPE_U8, SIGNAL_CTRL_TYPE::SBO_NORMAL, &p->data, self_ptl_signal_change_callback);
    }

    if(ret != 0)
    {
        goto ERR;
    }

    for(uint32_t i = 0; i < p_app_cfg->dd_info_vec.size(); i++)
    {
        stru_iec_dd_info *p = &p_app_cfg->dd_info_vec.at(i);
        p->saddr = "self_ptl.dd." + std::to_string(i);
        ret |= dc_signal_out(p->saddr, p->desc, DATA_TYPE_F32, &p->data);
    }

    if(ret != 0)
    {
        goto ERR;
    }

    for(uint32_t i = 0; i < p_app_cfg->p_param_cfg->vec_param.size(); i++)
    {
        stru_param_cfg_data *p = &p_app_cfg->p_param_cfg->vec_param.at(i);
        std::string saddr = "self_ptl.param." + p->desc;
        stru_signal_param param;
        param.min = 0;
        param.max = 100;
        param.step = 1;
        param.p_default = NULL;
        param.unit = "";
        // ret |= dc_signal_out(saddr, p->name, p->type, p->p_data);
        ret |= dc_signal_param(saddr, p->name, p->type, param, SIGNAL_CTRL_TYPE::SBO_NORMAL, p->p_data, self_ptl_signal_change_callback);
    }

    if(ret != 0)
    {
        goto ERR;
    }

    return 0;

ERR:
    MY_LOG_E("self_ptl_do_signal_out ret %d", ret);
    return ret;
}

int app_self_ptl_init1(void *arg)
{
    stru_app *p_app = (stru_app *)arg;

    if(0 != parse_cfg_files())
    {
        MY_LOG_E("self ptl parse_cfg_files failed");
        return -1;
    }

    self_ptl_init();

    if(0 != self_ptl_do_signal_out(p_app))
    {
        MY_LOG_E("self ptl do_signal_out failed");
        return -1;
    }

    return 0;
}

LOCAL uint32_t *p_iec_run_cnt_in = NULL;

int app_self_ptl_init2(void *arg)
{
    dc_signal_in("self_ptl.iec_run_cnt_in", "self_ptl.iec_run_cnt_in", "iec.run_cnt", DATA_TYPE_U32, (void **)&p_iec_run_cnt_in);

    MY_LOG_I("p_iec_run_cnt_in %p", p_iec_run_cnt_in);
    return 0;
}

void *app_self_ptl(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_rtx_data *p_rtx = &p_app->rtx;


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

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

        if(event & EV_TIMER1)
        {
            self_ptl_task();
        }

        if(event & EV_TIMER2)
        {
            self_ptl_method_task();
        }

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

    return NULL;
}   