FreakZ路由应答机制

FreakZ路由应答机制

路由应答机制是建立在路由发现和路由请求完成之后进行的,换句话说就是在通信链路建立完成之后,帧信息被传输到目的节点时,由目的节点进行的应答机制;路由应答机制在NWK.c下的mac_data_ind函数被调用,该函数的功能在另一篇文章《FreakZ学习笔记:接收过程详解》中有解释,这里不再赘述;相应代码如下:

switch (cmd.cmd_frm_id)

        {

        case NWK_CMD_RTE_REQ:

            if(nib.dev_type != NWK_END_DEVICE)//Added by LiuTianmin

            {   

                nwk_rte_mesh_rreq_handler(&hdr, &cmd);

            }

            break;

        case NWK_CMD_RTE_REP:

            if(nib.dev_type != NWK_END_DEVICE)//Added by LiuTianmin

            { 

                nwk_rte_mesh_rrep_handler(&hdr, &cmd);

            }

            break;

        case NWK_CMD_LEAVE:

            nwk_leave_handler(&hdr, &cmd);

            break;

        default:

            break;

        }

这里的switch语句会分别对路由请求和路由应答的ID进行判断,如果节点类型为终端节点的话,即执行相应的路由请求函数nwk_rte_mesh_rreq_handler或路由应答函数nwk_rte_mesh_rrep_handler。

路由应答函数nwk_rte_mesh_rrep_handler的主要功能是处理传入的路由线路并确定是否需要路由应答;首先,需要确保本次路由应答有一个对应的发现表和路由表,如果没有,那么丢弃回复路线,同样,如果路径的成本比目前发现路由表里保存的链路成本高的话,同样放弃回复路线直到有一个更高效率的链路建立。如果这条路由应答是给当前链路的,那么结束路由发现过程并发送路由应答,否则,在前进的路由回复中更新记录路由请求的发现表。代码原型如下:

void nwk_rte_mesh_rrep_handler(const nwk_hdr_t *hdr_in, const nwk_cmd_t *cmd_in)

{

    nwk_pcb_t *pcb = nwk_pcb_get();

    nwk_nib_t *nib = nwk_nib_get();

    mem_ptr_t *disc_mem_ptr;

    mem_ptr_t *rte_mem_ptr;

    U8 path_cost;

    disc_mem_ptr  = nwk_rte_disc_find(cmd_in->rrep.rreq_id, cmd_in->rrep.originator);

    rte_mem_ptr   = nwk_rte_tbl_find(cmd_in->rrep.responder);

    path_cost   = cmd_in->rrep.path_cost + NWK_STATIC_PATH_COST;

    // if the rte entry or discovery entry doesn't exist or the path cost is not less than the current resid cost, then drop

    // the rrep.

    if ((!rte_mem_ptr) || (!disc_mem_ptr) || (path_cost >= DISC_ENTRY(disc_mem_ptr)->resid_cost))

    {

        if (!rte_mem_ptr || !disc_mem_ptr)

        {///????????这里似乎有问题

            nwk_rte_tbl_free(rte_mem_ptr);

            nwk_rte_disc_free(disc_mem_ptr);

        }

        pcb->drop_rrep_frm++;

        return;

    }

    // we've met all the criteria to keep the rrep. that means we should update the rrep and disc entry.

    // any time we update the next hop in the rte entry, we need to reset the discovery entry's expiry

    DISC_ENTRY(disc_mem_ptr)->resid_cost  = path_cost;

    DISC_ENTRY(disc_mem_ptr)->expiry      = NWK_RTE_DISC_TIME;

    RTE_ENTRY(rte_mem_ptr)->next_hop      = hdr_in->mac_hdr->src_addr.short_addr;

    // update the entry to validation underway. once we send an actual frame through it, then we can change

    // it to NWK_VALIDATION_UNDERWAY.

    RTE_ENTRY(rte_mem_ptr)->status = (RTE_ENTRY(rte_mem_ptr)->status == NWK_DISCOVERY_UNDERWAY) ?

                                      NWK_VALIDATION_UNDERWAY :

                                      RTE_ENTRY(rte_mem_ptr)->status;

    // check if the rrep is meant for us.

    if (cmd_in->rrep.originator == nib->short_addr)

    {

        /*We should remove the roter request from req_list added by LiuTianmin*/

        mem_ptr_t *mem_ptr;

        mem_ptr = nwk_rte_mesh_rreq_find(nib->short_addr,cmd_in->rrep.responder);

        nwk_rte_mesh_rreq_free(mem_ptr);

        /*We should remove the roter request from req_list added by LiuTianmin*/

        // send out pending xfers

        nwk_pend_send_pending();

        DBG_PRINT("\nNWK_RTE_MESH: Route established.\n");

        return;

    }

    // forward the route reply

    nwk_rte_mesh_send_rrep(cmd_in->rrep.rreq_id, cmd_in->rrep.originator, cmd_in->rrep.responder,

                           path_cost, DISC_ENTRY(disc_mem_ptr)->sender_addr);

}

nwk_rte_mesh_send_rrep为发送路由应答函数,原型如下:

static void nwk_rte_mesh_send_rrep(U8 rreq_id, U16 originator, U16 responder, U8 path_cost, U16 sender_addr)

{

    nwk_nib_t *nib = nwk_nib_get();

    nwk_hdr_t hdr;

    nwk_cmd_t cmd;

    buffer_t *buf;

    cmd.cmd_frm_id              = NWK_CMD_RTE_REP;

    cmd.rrep.cmd_opts           = 0x00;

    cmd.rrep.rreq_id            = rreq_id;

    cmd.rrep.originator         = originator;

    cmd.rrep.responder          = responder;

    cmd.rrep.path_cost          = path_cost;

    hdr.nwk_frm_ctrl.frame_type = NWK_CMD_FRM;

    hdr.nwk_frm_ctrl.disc_route = false;

    hdr.src_addr                = nib->short_addr;

    hdr.dest_addr               = sender_addr;

    hdr.radius                  = (U8)(nib->max_depth << 1);

    hdr.seq_num                 = nib->seq_num;

    BUF_ALLOC(buf, TX);

    nwk_gen_cmd(buf, &cmd);

    debug_dump_nwk_cmd(&cmd);

    //nwk_fwd(buf, &hdr);   //Delete by LiuTianmin

    /*I think here should respone direct to the sender*/

    mac_hdr_t mac_hdr_out;

    buf->len = aMaxPHYPacketSize - (buf->dptr - buf->buf);

    hdr.mac_hdr         = &mac_hdr_out;

    hdr.mac_hdr->src_addr.mode          = SHORT_ADDR;

    hdr.mac_hdr->dest_addr.mode         = SHORT_ADDR;

    hdr.mac_hdr->src_addr.short_addr    = nib->short_addr;

    hdr.mac_hdr->dest_addr.short_addr   = hdr.dest_addr;

    nwk_tx(buf, &hdr, false);

}

当路由应答产生之后,会通过nwk_tx函数将该路由应答由NWK层传递到MAC层,通过MAC层到达Radio层,然后将应答信息发送。

你可能感兴趣的:(FreakZ路由应答机制)