/* HCI role defenitions */#defineHCI_ROLE_MASTER0x00#defineHCI_ROLE_SLAVE0x01#defineHCI_ROLE_UNKNOWN0xff/******************************************************************************* * * Function L2CA_GetBleConnRole * * Description This function returns the connection role. * * Returns link role. * ******************************************************************************/uint8_tL2CA_GetBleConnRole(constRawAddress& bd_addr) {uint8_t role = HCI_ROLE_UNKNOWN; tL2C_LCB* p_lcb; p_lcb =l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);if (p_lcb !=NULL) role =p_lcb->link_role;return role;}
回到smp_data_received(),会调用到smp_sm_event()
/******************************************************************************* * * Function smp_data_received * * Description This function is called when data is received from L2CAP on * SMP channel. * * * Returns void * ******************************************************************************/staticvoidsmp_data_received(uint16_t channel,constRawAddress& bd_addr,BT_HDR* p_buf) { ... /* reject the pairing request if there is an on-going SMP pairing */if (SMP_OPCODE_PAIRING_REQ == cmd || SMP_OPCODE_SEC_REQ == cmd) {if ((p_cb->state == SMP_STATE_IDLE) && (p_cb->br_state == SMP_BR_STATE_IDLE) &&!(p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD)) {p_cb->role =L2CA_GetBleConnRole(bd_addr); // <-- 1p_cb->pairing_bda = bd_addr; } ...if (bd_addr ==p_cb->pairing_bda) { ...p_cb->rcvd_cmd_code = cmd;p_cb->rcvd_cmd_len = (uint8_t)p_buf->len;smp_sm_event(p_cb, cmd, p); // <-- 2 }osi_free(p_buf);}
通过查看该函数,我们可以注意到,后续会直接使用该字段作为下标对数组进行取值操作
/******************************************************************************* * * Function smp_sm_event * * Description Handle events to the state machine. It looks up the entry * in the smp_entry_table array. * If it is a valid entry, it gets the state table. Set the next * state, if not NULL state. Execute the action function according * to the state table. If the state returned by action function is * not NULL state, adjust the new state to the returned state. If * (api_evt != MAX), call callback function. * * Returns void. * ******************************************************************************/voidsmp_sm_event(tSMP_CB* p_cb,tSMP_EVENT event,void* p_data) {uint8_t curr_state =p_cb->state; tSMP_SM_TBL state_table;uint8_t action, entry, i; tSMP_ENTRY_TBL entry_table =smp_entry_table[p_cb->role]; ...
/******************************************************************************* * * Function l2cu_find_lcb_by_bd_addr * * Description Look through all active LCBs for a match based on the * remote BD address. * * Returns pointer to matched LCB, or NULL if no match * ******************************************************************************/tL2C_LCB*l2cu_find_lcb_by_bd_addr(constRawAddress& p_bd_addr,tBT_TRANSPORT transport) {int xx; tL2C_LCB* p_lcb =&l2cb.lcb_pool[0];for (xx =0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++) {if ((p_lcb->in_use) &&p_lcb->transport == transport && (p_lcb->remote_bd_addr == p_bd_addr)) {return (p_lcb); } } /* If here, no match found */return (NULL);}