修改MTU方法

yangzh · 816次点击 · 2023-10-25

实际使用中,会用到MTU修改,修改后查看本次连接的MTU值是多少,现在举例(simple工程)查看MTU修改和查看MTU值


1)在下面函数中,添加update mtu

static int gapc_connection_req_ind_handler(ke_msg_id_t const msgid,
                                           struct gapc_connection_req_ind const *param,
                                           ke_task_id_t const dest_id,
                                           ke_task_id_t const src_id)
{
    app_env.conidx = KE_IDX_GET(src_id);
#if (BLE_APP_SEC)
    bool is_bond = app_sec_get_bond_status_by_addr(param->peer_addr);
#endif

    log_debug("Device type(%d) conidx(%d) connected, ", param->peer_addr_type, app_env.conidx);
    log_debug_array_ex("ADDR", &param->peer_addr, 6);
    // Check if the received Connection Handle was valid
    if (app_env.conidx != GAP_INVALID_CONIDX)
    {
        // Retrieve the connection info from the parameters
        app_env.conhdl = param->conhdl;

        // Send connection confirmation
        struct gapc_connection_cfm *cfm = KE_MSG_ALLOC(GAPC_CONNECTION_CFM,
                                                       KE_BUILD_ID(TASK_GAPC, app_env.conidx), TASK_APP,
                                                       gapc_connection_cfm);

#if (BLE_APP_SEC)
        cfm->auth = is_bond ? GAP_AUTH_REQ_NO_MITM_BOND : GAP_AUTH_REQ_NO_MITM_NO_BOND; // TODO [FBE] restore valid data
#else                                                                                   // !(BLE_APP_SEC)
        cfm->auth = GAP_AUTH_REQ_NO_MITM_NO_BOND;
#endif                                                                                  // (BLE_APP_SEC)
        // Send the message
        ke_msg_send(cfm);

        /*--------------------------------------------------------------
         * ENABLE REQUIRED PROFILES
         *--------------------------------------------------------------*/

#if (BLE_APP_BATT)
        // Enable Battery Service
        app_batt_enable_prf(app_env.conidx);
#endif //(BLE_APP_BATT)

#if (BLE_APP_SIMPLE_SERVER)
        // Enable SIMPLE_SERVER Service
        app_simple_server_enable_prf(app_env.conidx);
#endif //(BLE_APP_SIMPLE_SERVER)

        // We are now in connected State
        ke_state_set(dest_id, APPM_CONNECTED);

#if (BLE_APP_SEC)
        if (is_bond)
        {
            // Ask for the peer device to either start encryption
            app_sec_send_security_req(app_env.conidx);
        }
#endif // (BLE_APP_SEC)

        // Update mtu
        {
            uint8_t conidx = KE_IDX_GET(src_id);
            struct gattc_exc_mtu_cmd *mtu_cmd = KE_MSG_ALLOC_DYN(GATTC_EXC_MTU_CMD,
                                                                 KE_BUILD_ID(TASK_GATTC, conidx), dest_id,
                                                                 gattc_exc_mtu_cmd, 0);

            mtu_cmd->operation = GATTC_MTU_EXCH;

            // Send message
            ke_msg_send(mtu_cmd);
        }
    }
    else
    {
        // No connection has been established, restart advertising
        // appm_adv_update_state(true);
        appm_adv_start();
    }

    return (KE_MSG_CONSUMED);
}


2) 增加

{GATTC_MTU_CHANGED_IND, (ke_msg_func_t)gattc_mtu_changed_ind_handler}, // mtu change handle


/**< Default State handlers definition. */
KE_MSG_HANDLER_TAB(appm){
    // Note: first message is latest message checked by kernel so default is put on top.
    {KE_MSG_DEFAULT_HANDLER, (ke_msg_func_t)appm_msg_handler},

    // GAPM messages
    {GAPM_PROFILE_ADDED_IND, (ke_msg_func_t)gapm_profile_added_ind_handler},
    {GAPM_ACTIVITY_CREATED_IND, (ke_msg_func_t)gapm_activity_created_ind_handler},
    {GAPM_ACTIVITY_STOPPED_IND, (ke_msg_func_t)gapm_activity_stopped_ind_handler},
    {GAPM_CMP_EVT, (ke_msg_func_t)gapm_cmp_evt_handler},
    {GAPM_GEN_RAND_NB_IND, (ke_msg_func_t)gapm_gen_rand_nb_ind_handler},
    {GAPM_LIST_SIZE_IND, (ke_msg_func_t)gapm_list_size_ind_handler},
    {GAPM_DEV_BDADDR_IND, (ke_msg_func_t)gapm_dev_bdaddr_ind_handler},

    // GAPC messages
    {GAPC_GET_DEV_INFO_REQ_IND, (ke_msg_func_t)gapc_get_dev_info_req_ind_handler},
    {GAPC_SET_DEV_INFO_REQ_IND, (ke_msg_func_t)gapc_set_dev_info_req_ind_handler},
    {GAPC_CONNECTION_REQ_IND, (ke_msg_func_t)gapc_connection_req_ind_handler},
    {GAPC_CMP_EVT, (ke_msg_func_t)gapc_cmp_evt_handler},
    {GAPC_DISCONNECT_IND, (ke_msg_func_t)gapc_disconnect_ind_handler},
    {GAPC_PARAM_UPDATE_REQ_IND, (ke_msg_func_t)gapc_param_update_req_ind_handler},

    // gattc message
    {GATTC_MTU_CHANGED_IND, (ke_msg_func_t)gattc_mtu_changed_ind_handler}, // mtu change handle
};


3)增加对应回调函数

static int gattc_mtu_changed_ind_handler(ke_msg_id_t const msgid, struct gattc_mtu_changed_ind *param,
                                         ke_task_id_t const dest_id, ke_task_id_t const src_id)
{
    // log_debug("%s@%d\n", __func__, __LINE__);
    uint8_t conidx = KE_IDX_GET(src_id);
    log_debug("conidx=%x,mtu=%d\n", conidx, param->mtu);

    {

        // tspp_server_set_max_mtu(conidx, param->mtu);
    }
    return KE_MSG_CONSUMED;
}


4)连上手机后打印如下:

image.png

被收藏 1  ∙  0 赞  
加入收藏
点赞
1 回复  
善言善语 (您需要 登录 后才能回复 没有账号 ?)

请先登录网站