对结构体函数模式的使用和结构体数组函数的调用

来源:互联网 发布:苏州菜根谭网络面试 编辑:程序博客网 时间:2024/05/21 11:27

void chg_state(uint8 cur_state)

{

    uint8 i;

    static uint8 last_state=INVALID;

    

    plc_state.wait_t = 0;

    plc_state.init=1;

    for(i = 0; i < PLC_SLOT_SZ; i++)

    {

        if(plc_state_slot[i].cur_state == cur_state)

        {

            plc_state.pstate = (struct PLC_STATE *)&plc_state_slot[i];

            break;

        }

    }

    if(i >= PLC_SLOT_SZ)

    {

        plc_state.pstate = (struct PLC_STATE *)&plc_state_slot[0];

    }

 

    if(last_state != plc_state.pstate->cur_state)

    {

        plc_state.trycnt = 0;

    }

    last_state = plc_state.pstate->cur_state;

    notify(EV_STATE);

}

 

 

 

void local_frame_opt(struct SHS_frame *pframe)

{

    if(CMD_REGINFOR == pframe->infor[0])

    {

        get_reg(pframe);

    }

    else if(CMD_REQ_AID == pframe->infor[0])

    {

        chg_state(S_AID);//载波出现主动获取aid,说明载波芯片可能复位!!

    }

    else if(CMD_UNLINK == pframe->infor[0])

    {//网关断开连接,载波芯片上报断开连接,启动载波注册panid设置????

        chg_state(S_PWDREG);

    }

}

 

 

uint8 frame_handle(uint8 init, void *args)

{

    uint8 len,ret;

    struct SHS_frame *pframe = (struct SHS_frame *)args;

 

    if(pframe == NULL) return(0);

    if(is_all_xx(pframe->said, 0x00, ID_LEN))

    {//本地通信

        local_frame_opt(pframe);

        return(1);

    }

    else

    {//异地通信

        ret = remote_frame_opt(pframe);

        if (ret > 1) {

            len = set_ret_frame(pframe, ret);

            uart_write((uint8 *)pframe, len);

        }

    }

    

    return(0); 

}

 

 

const struct PLC_STATE plc_state_slot[]=

{

    //init plc

    {RST_PLC,   R_EID,   reset_plc},

    //{R_EID,     S_BPS,   rd_plc_eid},

    {R_EID,     S_AID,   rd_plc_eid},

    {S_BPS,     S_AID,   wr_plc_bps},

    {S_AID,     S_PWDREG,wr_plc_aid},

    {S_PWDREG,  S_PANID, set_register},

    {S_PANID,   _END,    wr_plc_panid},

#if KEY_REG

    //key reg

    {UNLINK1,   S_REG,   set_unlink},

#endif

    

    //密码注册

    {PWD_ERR,   UNLINK2, set_register},

    {UNLINK2,   S_PANID,    set_unlink},

    //注册成功

    {G_GWID,    S_REG,   rd_gw_aid},

    {S_REG,     G_SID,   set_register},//设置注册属性,等待载波芯片上报panid

{G_SID,     _END,    rd_gw_sid},

 

    {_END,      _END,    frame_handle},

};

0 0
原创粉丝点击