The Connection descriptor used by the client

本文介绍如何检查当前数据库连接数及最大连接数,并提供步骤指导如何调整最大连接数以解决因连接过多导致的问题。

可能是数据库上当前的连接数目已经超过了它能够处理的最大值.


查看当前的连接数

select count(*) from v$process;

 
数据库允许的最大连接数

select value from v$parameter where name='processes'; 

 

修改最大连接数:

alter system set processes=300 scope=spfile; 

 
重启数据库:

shutdown immediate;
startup; 

 shutdown   默认的是normal.可以省略;

 startup       默认的是open,也可以省略;

 

 

// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef __ESP_GATTC_API_H__ #define __ESP_GATTC_API_H__ #include "esp_bt_defs.h" #include "esp_gatt_defs.h" #include "esp_err.h" #ifdef __cplusplus extern "C" { #endif /// GATT Client callback function events typedef enum { ESP_GATTC_REG_EVT = 0, /*!< When GATT client is registered, the event comes */ ESP_GATTC_UNREG_EVT = 1, /*!< When GATT client is unregistered, the event comes */ ESP_GATTC_OPEN_EVT = 2, /*!< When GATT virtual connection is set up, the event comes */ ESP_GATTC_READ_CHAR_EVT = 3, /*!< When GATT characteristic is read, the event comes */ ESP_GATTC_WRITE_CHAR_EVT = 4, /*!< When GATT characteristic write operation completes, the event comes */ ESP_GATTC_CLOSE_EVT = 5, /*!< When GATT virtual connection is closed, the event comes */ ESP_GATTC_SEARCH_CMPL_EVT = 6, /*!< When GATT service discovery is completed, the event comes */ ESP_GATTC_SEARCH_RES_EVT = 7, /*!< When GATT service discovery result is got, the event comes */ ESP_GATTC_READ_DESCR_EVT = 8, /*!< When GATT characteristic descriptor read completes, the event comes */ ESP_GATTC_WRITE_DESCR_EVT = 9, /*!< When GATT characteristic descriptor write completes, the event comes */ ESP_GATTC_NOTIFY_EVT = 10, /*!< When GATT notification or indication arrives, the event comes */ ESP_GATTC_PREP_WRITE_EVT = 11, /*!< When GATT prepare-write operation completes, the event comes */ ESP_GATTC_EXEC_EVT = 12, /*!< When write execution completes, the event comes */ ESP_GATTC_ACL_EVT = 13, /*!< When ACL connection is up, the event comes */ ESP_GATTC_CANCEL_OPEN_EVT = 14, /*!< When GATT client ongoing connection is cancelled, the event comes */ ESP_GATTC_SRVC_CHG_EVT = 15, /*!< When "service changed" occurs, the event comes */ ESP_GATTC_ENC_CMPL_CB_EVT = 17, /*!< When encryption procedure completes, the event comes */ ESP_GATTC_CFG_MTU_EVT = 18, /*!< When configuration of MTU completes, the event comes */ ESP_GATTC_ADV_DATA_EVT = 19, /*!< When advertising of data, the event comes */ ESP_GATTC_MULT_ADV_ENB_EVT = 20, /*!< When multi-advertising is enabled, the event comes */ ESP_GATTC_MULT_ADV_UPD_EVT = 21, /*!< When multi-advertising parameters are updated, the event comes */ ESP_GATTC_MULT_ADV_DATA_EVT = 22, /*!< When multi-advertising data arrives, the event comes */ ESP_GATTC_MULT_ADV_DIS_EVT = 23, /*!< When multi-advertising is disabled, the event comes */ ESP_GATTC_CONGEST_EVT = 24, /*!< When GATT connection congestion comes, the event comes */ ESP_GATTC_BTH_SCAN_ENB_EVT = 25, /*!< When batch scan is enabled, the event comes */ ESP_GATTC_BTH_SCAN_CFG_EVT = 26, /*!< When batch scan storage is configured, the event comes */ ESP_GATTC_BTH_SCAN_RD_EVT = 27, /*!< When Batch scan read event is reported, the event comes */ ESP_GATTC_BTH_SCAN_THR_EVT = 28, /*!< When Batch scan threshold is set, the event comes */ ESP_GATTC_BTH_SCAN_PARAM_EVT = 29, /*!< When Batch scan parameters are set, the event comes */ ESP_GATTC_BTH_SCAN_DIS_EVT = 30, /*!< When Batch scan is disabled, the event comes */ ESP_GATTC_SCAN_FLT_CFG_EVT = 31, /*!< When Scan filter configuration completes, the event comes */ ESP_GATTC_SCAN_FLT_PARAM_EVT = 32, /*!< When Scan filter parameters are set, the event comes */ ESP_GATTC_SCAN_FLT_STATUS_EVT = 33, /*!< When Scan filter status is reported, the event comes */ ESP_GATTC_ADV_VSC_EVT = 34, /*!< When advertising vendor spec content event is reported, the event comes */ ESP_GATTC_REG_FOR_NOTIFY_EVT = 38, /*!< When register for notification of a service completes, the event comes */ ESP_GATTC_UNREG_FOR_NOTIFY_EVT = 39, /*!< When unregister for notification of a service completes, the event comes */ ESP_GATTC_CONNECT_EVT = 40, /*!< When the ble physical connection is set up, the event comes */ ESP_GATTC_DISCONNECT_EVT = 41, /*!< When the ble physical connection disconnected, the event comes */ ESP_GATTC_READ_MULTIPLE_EVT = 42, /*!< When the ble characteristic or descriptor multiple complete, the event comes */ ESP_GATTC_QUEUE_FULL_EVT = 43, /*!< When the gattc command queue full, the event comes */ ESP_GATTC_SET_ASSOC_EVT = 44, /*!< When the ble gattc set the associated address complete, the event comes */ ESP_GATTC_GET_ADDR_LIST_EVT = 45, /*!< When the ble get gattc address list in cache finish, the event comes */ ESP_GATTC_DIS_SRVC_CMPL_EVT = 46, /*!< When the ble discover service complete, the event comes */ ESP_GATTC_READ_MULTI_VAR_EVT = 47, /*!< When read multiple variable characteristic complete, the event comes */ } esp_gattc_cb_event_t; /** * @brief Gatt client callback parameters union */ typedef union { /** * @brief ESP_GATTC_REG_EVT */ struct gattc_reg_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t app_id; /*!< Application id which input in register API */ } reg; /*!< Gatt client callback param of ESP_GATTC_REG_EVT */ /** * @brief ESP_GATTC_OPEN_EVT */ struct gattc_open_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t conn_id; /*!< Connection id */ esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */ uint16_t mtu; /*!< MTU size */ } open; /*!< Gatt client callback param of ESP_GATTC_OPEN_EVT */ /** * @brief ESP_GATTC_CLOSE_EVT */ struct gattc_close_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t conn_id; /*!< Connection id */ esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */ esp_gatt_conn_reason_t reason; /*!< The reason of gatt connection close */ } close; /*!< Gatt client callback param of ESP_GATTC_CLOSE_EVT */ /** * @brief ESP_GATTC_CFG_MTU_EVT */ struct gattc_cfg_mtu_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t conn_id; /*!< Connection id */ uint16_t mtu; /*!< MTU size */ } cfg_mtu; /*!< Gatt client callback param of ESP_GATTC_CFG_MTU_EVT */ /** * @brief ESP_GATTC_SEARCH_CMPL_EVT */ struct gattc_search_cmpl_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t conn_id; /*!< Connection id */ esp_service_source_t searched_service_source; /*!< The source of the service information */ } search_cmpl; /*!< Gatt client callback param of ESP_GATTC_SEARCH_CMPL_EVT */ /** * @brief ESP_GATTC_SEARCH_RES_EVT */ struct gattc_search_res_evt_param { uint16_t conn_id; /*!< Connection id */ uint16_t start_handle; /*!< Service start handle */ uint16_t end_handle; /*!< Service end handle */ esp_gatt_id_t srvc_id; /*!< Service id, include service uuid and other information */ bool is_primary; /*!< True if this is the primary service */ } search_res; /*!< Gatt client callback param of ESP_GATTC_SEARCH_RES_EVT */ /** * @brief ESP_GATTC_READ_CHAR_EVT, ESP_GATTC_READ_DESCR_EVT, ESP_GATTC_READ_MULTIPLE_EVT, ESP_GATTC_READ_MULTI_VAR_EVT */ struct gattc_read_char_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t conn_id; /*!< Connection id */ uint16_t handle; /*!< Characteristic handle */ uint8_t *value; /*!< Characteristic value */ uint16_t value_len; /*!< Characteristic value length */ } read; /*!< Gatt client callback param of ESP_GATTC_READ_CHAR_EVT */ /** * @brief ESP_GATTC_WRITE_CHAR_EVT, ESP_GATTC_PREP_WRITE_EVT, ESP_GATTC_WRITE_DESCR_EVT */ struct gattc_write_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t conn_id; /*!< Connection id */ uint16_t handle; /*!< The Characteristic or descriptor handle */ uint16_t offset; /*!< The prepare write offset, this value is valid only when prepare write */ } write; /*!< Gatt client callback param of ESP_GATTC_WRITE_DESCR_EVT */ /** * @brief ESP_GATTC_EXEC_EVT */ struct gattc_exec_cmpl_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t conn_id; /*!< Connection id */ } exec_cmpl; /*!< Gatt client callback param of ESP_GATTC_EXEC_EVT */ /** * @brief ESP_GATTC_NOTIFY_EVT */ struct gattc_notify_evt_param { uint16_t conn_id; /*!< Connection id */ esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */ uint16_t handle; /*!< The Characteristic or descriptor handle */ uint16_t value_len; /*!< Notify attribute value */ uint8_t *value; /*!< Notify attribute value */ bool is_notify; /*!< True means notify, false means indicate */ } notify; /*!< Gatt client callback param of ESP_GATTC_NOTIFY_EVT */ /** * @brief ESP_GATTC_SRVC_CHG_EVT */ struct gattc_srvc_chg_evt_param { esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */ } srvc_chg; /*!< Gatt client callback param of ESP_GATTC_SRVC_CHG_EVT */ /** * @brief ESP_GATTC_CONGEST_EVT */ struct gattc_congest_evt_param { uint16_t conn_id; /*!< Connection id */ bool congested; /*!< Congested or not */ } congest; /*!< Gatt client callback param of ESP_GATTC_CONGEST_EVT */ /** * @brief ESP_GATTC_REG_FOR_NOTIFY_EVT */ struct gattc_reg_for_notify_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t handle; /*!< The characteristic or descriptor handle */ } reg_for_notify; /*!< Gatt client callback param of ESP_GATTC_REG_FOR_NOTIFY_EVT */ /** * @brief ESP_GATTC_UNREG_FOR_NOTIFY_EVT */ struct gattc_unreg_for_notify_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t handle; /*!< The characteristic or descriptor handle */ } unreg_for_notify; /*!< Gatt client callback param of ESP_GATTC_UNREG_FOR_NOTIFY_EVT */ /** * @brief ESP_GATTC_CONNECT_EVT */ struct gattc_connect_evt_param { uint16_t conn_id; /*!< Connection id */ uint8_t link_role; /*!< Link role : master role = 0 ; slave role = 1*/ esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */ esp_gatt_conn_params_t conn_params; /*!< current connection parameters */ esp_ble_addr_type_t ble_addr_type; /*!< Remote BLE device address type */ uint16_t conn_handle; /*!< HCI connection handle */ } connect; /*!< Gatt client callback param of ESP_GATTC_CONNECT_EVT */ /** * @brief ESP_GATTC_DISCONNECT_EVT */ struct gattc_disconnect_evt_param { esp_gatt_conn_reason_t reason; /*!< disconnection reason */ uint16_t conn_id; /*!< Connection id */ uint8_t link_role; /*!< Link role : master role = 0 ; slave role = 1*/ esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */ } disconnect; /*!< Gatt client callback param of ESP_GATTC_DISCONNECT_EVT */ /** * @brief ESP_GATTC_SET_ASSOC_EVT */ struct gattc_set_assoc_addr_cmp_evt_param { esp_gatt_status_t status; /*!< Operation status */ } set_assoc_cmp; /*!< Gatt client callback param of ESP_GATTC_SET_ASSOC_EVT */ /** * @brief ESP_GATTC_GET_ADDR_LIST_EVT */ struct gattc_get_addr_list_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint8_t num_addr; /*!< The number of address in the gattc cache address list */ esp_bd_addr_t *addr_list; /*!< The pointer to the address list which has been get from the gattc cache */ } get_addr_list; /*!< Gatt client callback param of ESP_GATTC_GET_ADDR_LIST_EVT */ /** * @brief ESP_GATTC_QUEUE_FULL_EVT */ struct gattc_queue_full_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t conn_id; /*!< Connection id */ bool is_full; /*!< The gattc command queue is full or not */ } queue_full; /*!< Gatt client callback param of ESP_GATTC_QUEUE_FULL_EVT */ /** * @brief ESP_GATTC_DIS_SRVC_CMPL_EVT */ struct gattc_dis_srvc_cmpl_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t conn_id; /*!< Connection id */ } dis_srvc_cmpl; /*!< Gatt client callback param of ESP_GATTC_DIS_SRVC_CMPL_EVT */ } esp_ble_gattc_cb_param_t; /*!< GATT client callback parameter union type */ /** * @brief GATT Client callback function type * @param event : Event type * @param gattc_if : GATT client access interface, normally * different gattc_if correspond to different profile * @param param : Point to callback parameter, currently is union type */ typedef void (* esp_gattc_cb_t)(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param); /** * @brief This function is called to register application callbacks * with GATTC module. * * @param[in] callback : pointer to the application callback function. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_register_callback(esp_gattc_cb_t callback); /** * @brief This function is called to get the current application callbacks * with BTA GATTC module. * * @return * - esp_gattC_cb_t : current callback * */ esp_gattc_cb_t esp_ble_gattc_get_callback(void); /** * @brief This function is called to register application callbacks * with GATTC module. * * @param[in] app_id : Application Identify (UUID), for different application * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_app_register(uint16_t app_id); /** * @brief This function is called to unregister an application * from GATTC module. * * @param[in] gattc_if: Gatt client access interface. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if); #if (BLE_42_FEATURE_SUPPORT == TRUE) /** * @brief Open a direct connection or add a background auto connection * * @param[in] gattc_if: Gatt client access interface. * @param[in] remote_bda: remote device bluetooth device address. * @param[in] remote_addr_type: remote device bluetooth device the address type. * @param[in] is_direct: direct connection or background auto connection(by now, background auto connection is not supported). * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct); #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE) #if (BLE_50_FEATURE_SUPPORT == TRUE) esp_err_t esp_ble_gattc_aux_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct); #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE) /** * @brief Close the virtual connection to the GATT server. gattc may have multiple virtual GATT server connections when multiple app_id registered, * this API only close one virtual GATT server connection. if there exist other virtual GATT server connections, * it does not disconnect the physical connection. * if you want to disconnect the physical connection directly, you can use esp_ble_gap_disconnect(esp_bd_addr_t remote_device). * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID to be closed. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_close (esp_gatt_if_t gattc_if, uint16_t conn_id); /** * @brief Configure the MTU size in the GATT channel. This can be done * only once per connection. Before using, use esp_ble_gatt_set_local_mtu() * to configure the local MTU size. * * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id); /** * @brief This function is called to get service from local cache. * This function report service search result by a callback * event, and followed by a service search complete event. * Note: 128-bit base UUID will automatically be converted to a 16-bit UUID in the search results. Other types of UUID remain unchanged. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID. * @param[in] filter_uuid: a UUID of the service application is interested in. * If Null, discover for all services. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_search_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *filter_uuid); /** * @brief Find all the service with the given service uuid in the gattc cache, if the svc_uuid is NULL, find all the service. * Note: It just get service from local cache, won't get from remote devices. If want to get it from remote device, need * to used the esp_ble_gattc_cache_refresh, then call esp_ble_gattc_get_service again. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID which identify the server. * @param[in] svc_uuid: the pointer to the service uuid. * @param[out] result: The pointer to the service which has been found in the gattc cache. * @param[inout] count: input the number of service want to find, * it will output the number of service has been found in the gattc cache with the given service uuid. * @param[in] offset: Offset of the service position to get. * * @return * - ESP_OK: success * - other: failed * */ esp_gatt_status_t esp_ble_gattc_get_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *svc_uuid, esp_gattc_service_elem_t *result, uint16_t *count, uint16_t offset); /** * @brief Find all the characteristic with the given service in the gattc cache * Note: It just get characteristic from local cache, won't get from remote devices. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID which identify the server. * @param[in] start_handle: the attribute start handle. * @param[in] end_handle: the attribute end handle * @param[out] result: The pointer to the characteristic in the service. * @param[inout] count: input the number of characteristic want to find, * it will output the number of characteristic has been found in the gattc cache with the given service. * @param[in] offset: Offset of the characteristic position to get. * * @return * - ESP_OK: success * - other: failed * */ esp_gatt_status_t esp_ble_gattc_get_all_char(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle, esp_gattc_char_elem_t *result, uint16_t *count, uint16_t offset); /** * @brief Find all the descriptor with the given characteristic in the gattc cache * Note: It just get descriptor from local cache, won't get from remote devices. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID which identify the server. * @param[in] char_handle: the given characteristic handle * @param[out] result: The pointer to the descriptor in the characteristic. * @param[inout] count: input the number of descriptor want to find, * it will output the number of descriptor has been found in the gattc cache with the given characteristic. * @param[in] offset: Offset of the descriptor position to get. * * @return * - ESP_OK: success * - other: failed * */ esp_gatt_status_t esp_ble_gattc_get_all_descr(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t char_handle, esp_gattc_descr_elem_t *result, uint16_t *count, uint16_t offset); /** * @brief Find the characteristic with the given characteristic uuid in the gattc cache * Note: It just get characteristic from local cache, won't get from remote devices. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID which identify the server. * @param[in] start_handle: the attribute start handle * @param[in] end_handle: the attribute end handle * @param[in] char_uuid: the characteristic uuid * @param[out] result: The pointer to the characteristic in the service. * @param[inout] count: input the number of characteristic want to find, * it will output the number of characteristic has been found in the gattc cache with the given service. * * @return * - ESP_OK: success * - other: failed * */ esp_gatt_status_t esp_ble_gattc_get_char_by_uuid(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle, esp_bt_uuid_t char_uuid, esp_gattc_char_elem_t *result, uint16_t *count); /** * @brief Find the descriptor with the given characteristic uuid in the gattc cache * Note: It just get descriptor from local cache, won't get from remote devices. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID which identify the server. * @param[in] start_handle: the attribute start handle * @param[in] end_handle: the attribute end handle * @param[in] char_uuid: the characteristic uuid. * @param[in] descr_uuid: the descriptor uuid. * @param[out] result: The pointer to the descriptor in the given characteristic. * @param[inout] count: input the number of descriptor want to find, * it will output the number of descriptor has been found in the gattc cache with the given characteristic. * * @return * - ESP_OK: success * - other: failed * */ esp_gatt_status_t esp_ble_gattc_get_descr_by_uuid(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle, esp_bt_uuid_t char_uuid, esp_bt_uuid_t descr_uuid, esp_gattc_descr_elem_t *result, uint16_t *count); /** * @brief Find the descriptor with the given characteristic handle in the gattc cache * Note: It just get descriptor from local cache, won't get from remote devices. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID which identify the server. * @param[in] char_handle: the characteristic handle. * @param[in] descr_uuid: the descriptor uuid. * @param[out] result: The pointer to the descriptor in the given characteristic. * @param[inout] count: input the number of descriptor want to find, * it will output the number of descriptor has been found in the gattc cache with the given characteristic. * * @return * - ESP_OK: success * - other: failed * */ esp_gatt_status_t esp_ble_gattc_get_descr_by_char_handle(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t char_handle, esp_bt_uuid_t descr_uuid, esp_gattc_descr_elem_t *result, uint16_t *count); /** * @brief Find the include service with the given service handle in the gattc cache * Note: It just get include service from local cache, won't get from remote devices. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID which identify the server. * @param[in] start_handle: the attribute start handle * @param[in] end_handle: the attribute end handle * @param[in] incl_uuid: the include service uuid * @param[out] result: The pointer to the include service in the given service. * @param[inout] count: input the number of include service want to find, * it will output the number of include service has been found in the gattc cache with the given service. * * @return * - ESP_OK: success * - other: failed * */ esp_gatt_status_t esp_ble_gattc_get_include_service(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle, esp_bt_uuid_t *incl_uuid, esp_gattc_incl_svc_elem_t *result, uint16_t *count); /** * @brief Find the attribute count with the given service or characteristic in the gattc cache * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID which identify the server. * @param[in] type: the attribute type. * @param[in] start_handle: the attribute start handle, if the type is ESP_GATT_DB_DESCRIPTOR, this parameter should be ignore * @param[in] end_handle: the attribute end handle, if the type is ESP_GATT_DB_DESCRIPTOR, this parameter should be ignore * @param[in] char_handle: the characteristic handle, this parameter valid when the type is ESP_GATT_DB_DESCRIPTOR. If the type * isn't ESP_GATT_DB_DESCRIPTOR, this parameter should be ignore. * @param[out] count: output the number of attribute has been found in the gattc cache with the given attribute type. * * @return * - ESP_OK: success * - other: failed * */ esp_gatt_status_t esp_ble_gattc_get_attr_count(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_gatt_db_attr_type_t type, uint16_t start_handle, uint16_t end_handle, uint16_t char_handle, uint16_t *count); /** * @brief This function is called to get the GATT database. * Note: It just get attribute data base from local cache, won't get from remote devices. * * @param[in] gattc_if: Gatt client access interface. * @param[in] start_handle: the attribute start handle * @param[in] end_handle: the attribute end handle * @param[in] conn_id: connection ID which identify the server. * @param[in] db: output parameter which will contain the GATT database copy. * Caller is responsible for freeing it. * @param[in] count: number of elements in database. * * @return * - ESP_OK: success * - other: failed * */ esp_gatt_status_t esp_ble_gattc_get_db(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle, esp_gattc_db_elem_t *db, uint16_t *count); /** * @brief This function is called to read a service's characteristics of * the given characteristic handle * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id : connection ID. * @param[in] handle : characteritic handle to read. * @param[in] auth_req : authenticate request type * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_read_char (esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t handle, esp_gatt_auth_req_t auth_req); /** * @brief This function is called to read a service's characteristics of * the given characteristic UUID * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id : connection ID. * @param[in] start_handle : the attribute start handle. * @param[in] end_handle : the attribute end handle * @param[in] uuid : The UUID of attribute which will be read. * @param[in] auth_req : authenticate request type * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_read_by_type (esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle, esp_bt_uuid_t *uuid, esp_gatt_auth_req_t auth_req); /** * @brief This function is called to read multiple characteristic or * characteristic descriptors. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id : connection ID. * @param[in] read_multi : pointer to the read multiple parameter. * @param[in] auth_req : authenticate request type * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_read_multiple(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_gattc_multi_t *read_multi, esp_gatt_auth_req_t auth_req); /** * @brief This function is called to read multiple variable length characteristic or * characteristic descriptors. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id : connection ID. * @param[in] read_multi : pointer to the read multiple parameter. * @param[in] auth_req : authenticate request type * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_read_multiple_variable(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_gattc_multi_t *read_multi, esp_gatt_auth_req_t auth_req); /** * @brief This function is called to read a characteristics descriptor. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id : connection ID. * @param[in] handle : descriptor handle to read. * @param[in] auth_req : authenticate request type * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_read_char_descr (esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t handle, esp_gatt_auth_req_t auth_req); /** * @brief This function is called to write characteristic value. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id : connection ID. * @param[in] handle : characteristic handle to write. * @param[in] value_len: length of the value to be written. * @param[in] value : the value to be written. * @param[in] write_type : the type of attribute write operation. * @param[in] auth_req : authentication request. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_write_char( esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t handle, uint16_t value_len, uint8_t *value, esp_gatt_write_type_t write_type, esp_gatt_auth_req_t auth_req); /** * @brief This function is called to write characteristic descriptor value. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id : connection ID * @param[in] handle : descriptor hadle to write. * @param[in] value_len: length of the value to be written. * @param[in] value : the value to be written. * @param[in] write_type : the type of attribute write operation. * @param[in] auth_req : authentication request. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t handle, uint16_t value_len, uint8_t *value, esp_gatt_write_type_t write_type, esp_gatt_auth_req_t auth_req); /** * @brief This function is called to prepare write a characteristic value. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id : connection ID. * @param[in] handle : characteristic handle to prepare write. * @param[in] offset : offset of the write value. * @param[in] value_len: length of the value to be written. * @param[in] value : the value to be written. * @param[in] auth_req : authentication request. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t handle, uint16_t offset, uint16_t value_len, uint8_t *value, esp_gatt_auth_req_t auth_req); /** * @brief This function is called to prepare write a characteristic descriptor value. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id : connection ID. * @param[in] handle : characteristic descriptor handle to prepare write. * @param[in] offset : offset of the write value. * @param[in] value_len: length of the value to be written. * @param[in] value : the value to be written. * @param[in] auth_req : authentication request. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_prepare_write_char_descr(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t handle, uint16_t offset, uint16_t value_len, uint8_t *value, esp_gatt_auth_req_t auth_req); /** * @brief This function is called to execute write a prepare write sequence. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id : connection ID. * @param[in] is_execute : execute or cancel. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_execute_write (esp_gatt_if_t gattc_if, uint16_t conn_id, bool is_execute); /** * @brief This function is called to register for notification of a service. * * @param[in] gattc_if: Gatt client access interface. * @param[in] server_bda : target GATT server. * @param[in] handle : GATT characteristic handle. * * @return * - ESP_OK: registration succeeds * - other: failed * */ esp_err_t esp_ble_gattc_register_for_notify (esp_gatt_if_t gattc_if, esp_bd_addr_t server_bda, uint16_t handle); /** * @brief This function is called to de-register for notification of a service. * * @param[in] gattc_if: Gatt client access interface. * @param[in] server_bda : target GATT server. * @param[in] handle : GATT characteristic handle. * * @return * - ESP_OK: unregister succeeds * - other: failed * */ esp_err_t esp_ble_gattc_unregister_for_notify (esp_gatt_if_t gattc_if, esp_bd_addr_t server_bda, uint16_t handle); /** * @brief Refresh the server cache store in the gattc stack of the remote device. If * the device is connected, this API will restart the discovery of service information of the remote device * * @param[in] remote_bda: remote device BD address. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_cache_refresh(esp_bd_addr_t remote_bda); /** * @brief Add or delete the associated address with the source address. * Note: The role of this API is mainly when the client side has stored a server-side database, * when it needs to connect another device, but the device's attribute database is the same * as the server database stored on the client-side, calling this API can use the database * that the device has stored used as the peer server database to reduce the attribute * database search and discovery process and speed up the connection time. * The associated address mains that device want to used the database has stored in the local cache. * The source address mains that device want to share the database to the associated address device. * * @param[in] gattc_if: Gatt client access interface. * @param[in] src_addr: the source address which provide the attribute table. * @param[in] assoc_addr: the associated device address which went to share the attribute table with the source address. * @param[in] is_assoc: true add the associated device address, false remove the associated device address. * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_cache_assoc(esp_gatt_if_t gattc_if, esp_bd_addr_t src_addr, esp_bd_addr_t assoc_addr, bool is_assoc); /** * @brief Get the address list which has store the attribute table in the gattc cache. There will * callback ESP_GATTC_GET_ADDR_LIST_EVT event when get address list complete. * * @param[in] gattc_if: Gatt client access interface. * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_cache_get_addr_list(esp_gatt_if_t gattc_if); /** * @brief Clean the service cache of this device in the gattc stack, * * @param[in] remote_bda: remote device BD address. * * @return * - ESP_OK: success * - other: failed * */ esp_err_t esp_ble_gattc_cache_clean(esp_bd_addr_t remote_bda); #ifdef __cplusplus } #endif #endif /* __ESP_GATTC_API_H__ */
最新发布
10-14
/// Client for a vhost-user device. The API is a thin abstraction over the vhost-user protocol. pub struct BackendClient { connection: Connection<FrontendReq>, // Cached virtio features from the backend. virtio_features: u64, // Cached acked virtio features from the driver. acked_virtio_features: u64, // Cached vhost-user protocol features. acked_protocol_features: u64, } impl BackendClient { /// Create a new instance. pub fn new(connection: Connection<FrontendReq>) -> Self { BackendClient { connection, virtio_features: 0, acked_virtio_features: 0, acked_protocol_features: 0, } } /// Get a bitmask of supported virtio/vhost features. pub fn get_features(&mut self) -> Result<u64> { let hdr = self.send_request_header(FrontendReq::GET_FEATURES, None)?; let val = self.recv_reply::<VhostUserU64>(&hdr)?; self.virtio_features = val.value; Ok(self.virtio_features) } /// Inform the vhost subsystem which features to enable. /// This should be a subset of supported features from get_features(). pub fn set_features(&mut self, features: u64) -> Result<()> { let val = VhostUserU64::new(features); let hdr = self.send_request_with_body(FrontendReq::SET_FEATURES, &val, None)?; self.acked_virtio_features = features & self.virtio_features; self.wait_for_ack(&hdr) } /// Set the current process as the owner of the vhost backend. /// This must be run before any other vhost commands. pub fn set_owner(&self) -> Result<()> { let hdr = self.send_request_header(FrontendReq::SET_OWNER, None)?; self.wait_for_ack(&hdr) } /// Used to be sent to request disabling all rings /// This is no longer used. pub fn reset_owner(&self) -> Result<()> { let hdr = self.send_request_header(FrontendReq::RESET_OWNER, None)?; self.wait_for_ack(&hdr) } /// Set the memory map regions on the backend so it can translate the vring /// addresses. In the ancillary data there is an array of file descriptors pub fn set_mem_table(&self, regions: &[VhostUserMemoryRegionInfo]) -> Result<()> { if regions.is_empty() || regions.len() > MAX_ATTACHED_FD_ENTRIES { return Err(VhostUserError::InvalidParam( "set_mem_table: regions empty or exceed max allowed regions per req.", )); } let mut ctx = VhostUserMemoryContext::new(); for region in regions.iter() { if region.memory_size == 0 || region.mmap_handle == INVALID_DESCRIPTOR { return Err(VhostUserError::InvalidParam( "set_mem_table: invalid memory region", )); } let reg = VhostUserMemoryRegion { guest_phys_addr: region.guest_phys_addr, memory_size: region.memory_size, user_addr: region.userspace_addr, mmap_offset: region.mmap_offset, }; ctx.append(&reg, region.mmap_handle); } let body = VhostUserMemory::new(ctx.regions.len() as u32); let hdr = self.send_request_with_payload( FrontendReq::SET_MEM_TABLE, &body, ctx.regions.as_bytes(), Some(ctx.fds.as_slice()), )?; self.wait_for_ack(&hdr) } /// Set base address for page modification logging. pub fn set_log_base(&self, base: u64, fd: Option<RawDescriptor>) -> Result<()> { let val = VhostUserU64::new(base); let should_have_fd = self.acked_protocol_features & VhostUserProtocolFeatures::LOG_SHMFD.bits() != 0; if should_have_fd != fd.is_some() { return Err(VhostUserError::InvalidParam("set_log_base: FD is missing")); } let _ = self.send_request_with_body( FrontendReq::SET_LOG_BASE, &val, fd.as_ref().map(std::slice::from_ref), )?; Ok(()) } /// Specify an event file descriptor to signal on log write. pub fn set_log_fd(&self, fd: RawDescriptor) -> Result<()> { let fds = [fd]; let hdr = self.send_request_header(FrontendReq::SET_LOG_FD, Some(&fds))?; self.wait_for_ack(&hdr) } /// Set the number of descriptors in the vring. pub fn set_vring_num(&self, queue_index: usize, num: u16) -> Result<()> { let val = VhostUserVringState::new(queue_index as u32, num.into()); let hdr = self.send_request_with_body(FrontendReq::SET_VRING_NUM, &val, None)?; self.wait_for_ack(&hdr) } /// Set the addresses for a given vring. pub fn set_vring_addr(&self, queue_index: usize, config_data: &VringConfigData) -> Result<()> { if config_data.flags & !(VhostUserVringAddrFlags::all().bits()) != 0 { return Err(VhostUserError::InvalidParam( "set_vring_addr: unsupported vring flags", )); } let val = VhostUserVringAddr::from_config_data(queue_index as u32, config_data); let hdr = self.send_request_with_body(FrontendReq::SET_VRING_ADDR, &val, None)?; self.wait_for_ack(&hdr) } /// Set the first index to look for available descriptors. // TODO: b/331466964 - Arguments and message format are wrong for packed queues. pub fn set_vring_base(&self, queue_index: usize, base: u16) -> Result<()> { let val = VhostUserVringState::new(queue_index as u32, base.into()); let hdr = self.send_request_with_body(FrontendReq::SET_VRING_BASE, &val, None)?; self.wait_for_ack(&hdr) } /// Get the available vring base offset. // TODO: b/331466964 - Return type is wrong for packed queues. pub fn get_vring_base(&self, queue_index: usize) -> Result<u32> { let req = VhostUserVringState::new(queue_index as u32, 0); let hdr = self.send_request_with_body(FrontendReq::GET_VRING_BASE, &req, None)?; let reply = self.recv_reply::<VhostUserVringState>(&hdr)?; Ok(reply.num) } /// Set the event to trigger when buffers have been used by the host. /// /// Bits (0-7) of the payload contain the vring index. Bit 8 is the invalid FD flag. This flag /// is set when there is no file descriptor in the ancillary data. This signals that polling /// will be used instead of waiting for the call. pub fn set_vring_call(&self, queue_index: usize, event: &Event) -> Result<()> { let hdr = self.send_fd_for_vring( FrontendReq::SET_VRING_CALL, queue_index, event.as_raw_descriptor(), )?; self.wait_for_ack(&hdr) } /// Set the event that will be signaled by the guest when buffers are available for the host to /// process. /// /// Bits (0-7) of the payload contain the vring index. Bit 8 is the invalid FD flag. This flag /// is set when there is no file descriptor in the ancillary data. This signals that polling /// should be used instead of waiting for a kick. pub fn set_vring_kick(&self, queue_index: usize, event: &Event) -> Result<()> { let hdr = self.send_fd_for_vring( FrontendReq::SET_VRING_KICK, queue_index, event.as_raw_descriptor(), )?; self.wait_for_ack(&hdr) } /// Set the event that will be signaled by the guest when error happens. /// /// Bits (0-7) of the payload contain the vring index. Bit 8 is the invalid FD flag. This flag /// is set when there is no file descriptor in the ancillary data. pub fn set_vring_err(&self, queue_index: usize, event: &Event) -> Result<()> { let hdr = self.send_fd_for_vring( FrontendReq::SET_VRING_ERR, queue_index, event.as_raw_descriptor(), )?; self.wait_for_ack(&hdr) } /// Front-end and back-end negotiate a channel over which to transfer the back-end’s internal /// state during migration. /// /// Requires VHOST_USER_PROTOCOL_F_DEVICE_STATE to be negotiated. pub fn set_device_state_fd( &self, transfer_direction: VhostUserTransferDirection, migration_phase: VhostUserMigrationPhase, fd: &impl AsRawDescriptor, ) -> Result<Option<File>> { if self.acked_protocol_features & VhostUserProtocolFeatures::DEVICE_STATE.bits() == 0 { return Err(VhostUserError::InvalidOperation); } // Send request. let req = DeviceStateTransferParameters { transfer_direction: match transfer_direction { VhostUserTransferDirection::Save => 0, VhostUserTransferDirection::Load => 1, }, migration_phase: match migration_phase { VhostUserMigrationPhase::Stopped => 0, }, }; let hdr = self.send_request_with_body( FrontendReq::SET_DEVICE_STATE_FD, &req, Some(&[fd.as_raw_descriptor()]), )?; // Receive reply. let (reply, files) = self.recv_reply_with_files::<VhostUserU64>(&hdr)?; let has_err = reply.value & 0xff != 0; let invalid_fd = reply.value & 0x100 != 0; if has_err { return Err(VhostUserError::BackendInternalError); } match (invalid_fd, files.len()) { (true, 0) => Ok(None), (false, 1) => Ok(files.into_iter().next()), _ => Err(VhostUserError::IncorrectFds), } } /// After transferring the back-end’s internal state during migration, check whether the /// back-end was able to successfully fully process the state. pub fn check_device_state(&self) -> Result<()> { if self.acked_protocol_features & VhostUserProtocolFeatures::DEVICE_STATE.bits() == 0 { return Err(VhostUserError::InvalidOperation); } let hdr = self.send_request_header(FrontendReq::CHECK_DEVICE_STATE, None)?; let reply = self.recv_reply::<VhostUserU64>(&hdr)?; if reply.value != 0 { return Err(VhostUserError::BackendInternalError); } Ok(()) } /// Get the protocol feature bitmask from the underlying vhost implementation. pub fn get_protocol_features(&self) -> Result<VhostUserProtocolFeatures> { if self.virtio_features & 1 << VHOST_USER_F_PROTOCOL_FEATURES == 0 { return Err(VhostUserError::InvalidOperation); } let hdr = self.send_request_header(FrontendReq::GET_PROTOCOL_FEATURES, None)?; let val = self.recv_reply::<VhostUserU64>(&hdr)?; Ok(VhostUserProtocolFeatures::from_bits_truncate(val.value)) } /// Enable protocol features in the underlying vhost implementation. pub fn set_protocol_features(&mut self, features: VhostUserProtocolFeatures) -> Result<()> { if self.virtio_features & 1 << VHOST_USER_F_PROTOCOL_FEATURES == 0 { return Err(VhostUserError::InvalidOperation); } if features.contains(VhostUserProtocolFeatures::SHARED_MEMORY_REGIONS) && !features.contains(VhostUserProtocolFeatures::BACKEND_REQ) { return Err(VhostUserError::FeatureMismatch); } let val = VhostUserU64::new(features.bits()); let hdr = self.send_request_with_body(FrontendReq::SET_PROTOCOL_FEATURES, &val, None)?; // Don't wait for ACK here because the protocol feature negotiation process hasn't been // completed yet. self.acked_protocol_features = features.bits(); self.wait_for_ack(&hdr) } /// Query how many queues the backend supports. pub fn get_queue_num(&self) -> Result<u64> { if !self.is_feature_mq_available() { return Err(VhostUserError::InvalidOperation); } let hdr = self.send_request_header(FrontendReq::GET_QUEUE_NUM, None)?; let val = self.recv_reply::<VhostUserU64>(&hdr)?; if val.value > VHOST_USER_MAX_VRINGS { return Err(VhostUserError::InvalidMessage); } Ok(val.value) } /// Signal backend to enable or disable corresponding vring. /// /// Backend must not pass data to/from the ring until ring is enabled by /// VHOST_USER_SET_VRING_ENABLE with parameter 1, or after it has been /// disabled by VHOST_USER_SET_VRING_ENABLE with parameter 0. pub fn set_vring_enable(&self, queue_index: usize, enable: bool) -> Result<()> { // set_vring_enable() is supported only when PROTOCOL_FEATURES has been enabled. if self.acked_virtio_features & 1 << VHOST_USER_F_PROTOCOL_FEATURES == 0 { return Err(VhostUserError::InvalidOperation); } let val = VhostUserVringState::new(queue_index as u32, enable.into()); let hdr = self.send_request_with_body(FrontendReq::SET_VRING_ENABLE, &val, None)?; self.wait_for_ack(&hdr) } /// Fetch the contents of the virtio device configuration space. pub fn get_config( &self, offset: u32, size: u32, flags: VhostUserConfigFlags, buf: &[u8], ) -> Result<(VhostUserConfig, VhostUserConfigPayload)> { let body = VhostUserConfig::new(offset, size, flags); if !body.is_valid() { return Err(VhostUserError::InvalidParam( "get_config: VhostUserConfig is invalid", )); } // depends on VhostUserProtocolFeatures::CONFIG if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIG.bits() == 0 { return Err(VhostUserError::InvalidOperation); } // vhost-user spec states that: // "Request payload: virtio device config space" // "Reply payload: virtio device config space" let hdr = self.send_request_with_payload(FrontendReq::GET_CONFIG, &body, buf, None)?; let (body_reply, buf_reply, rfds) = self.recv_reply_with_payload::<VhostUserConfig>(&hdr)?; if !rfds.is_empty() { return Err(VhostUserError::InvalidMessage); } else if body_reply.size == 0 { return Err(VhostUserError::BackendInternalError); } else if body_reply.size != body.size || body_reply.size as usize != buf.len() || body_reply.offset != body.offset { return Err(VhostUserError::InvalidMessage); } Ok((body_reply, buf_reply)) } /// Change the virtio device configuration space. It also can be used for live migration on the /// destination host to set readonly configuration space fields. pub fn set_config(&self, offset: u32, flags: VhostUserConfigFlags, buf: &[u8]) -> Result<()> { let body = VhostUserConfig::new( offset, buf.len() .try_into() .map_err(VhostUserError::InvalidCastToInt)?, flags, ); if !body.is_valid() { return Err(VhostUserError::InvalidParam( "set_config: VhostUserConfig is invalid", )); } // depends on VhostUserProtocolFeatures::CONFIG if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIG.bits() == 0 { return Err(VhostUserError::InvalidOperation); } let hdr = self.send_request_with_payload(FrontendReq::SET_CONFIG, &body, buf, None)?; self.wait_for_ack(&hdr) } /// Setup backend communication channel. pub fn set_backend_req_fd(&self, fd: &dyn AsRawDescriptor) -> Result<()> { if self.acked_protocol_features & VhostUserProtocolFeatures::BACKEND_REQ.bits() == 0 { return Err(VhostUserError::InvalidOperation); } let fds = [fd.as_raw_descriptor()]; let hdr = self.send_request_header(FrontendReq::SET_BACKEND_REQ_FD, Some(&fds))?; self.wait_for_ack(&hdr) } /// Retrieve shared buffer for inflight I/O tracking. pub fn get_inflight_fd( &self, inflight: &VhostUserInflight, ) -> Result<(VhostUserInflight, File)> { if self.acked_protocol_features & VhostUserProtocolFeatures::INFLIGHT_SHMFD.bits() == 0 { return Err(VhostUserError::InvalidOperation); } let hdr = self.send_request_with_body(FrontendReq::GET_INFLIGHT_FD, inflight, None)?; let (inflight, files) = self.recv_reply_with_files::<VhostUserInflight>(&hdr)?; match into_single_file(files) { Some(file) => Ok((inflight, file)), None => Err(VhostUserError::IncorrectFds), } } /// Set shared buffer for inflight I/O tracking. pub fn set_inflight_fd(&self, inflight: &VhostUserInflight, fd: RawDescriptor) -> Result<()> { if self.acked_protocol_features & VhostUserProtocolFeatures::INFLIGHT_SHMFD.bits() == 0 { return Err(VhostUserError::InvalidOperation); } if inflight.mmap_size == 0 || inflight.num_queues == 0 || inflight.queue_size == 0 || fd == INVALID_DESCRIPTOR { return Err(VhostUserError::InvalidParam( "set_inflight_fd: invalid fd or params", )); } let hdr = self.send_request_with_body(FrontendReq::SET_INFLIGHT_FD, inflight, Some(&[fd]))?; self.wait_for_ack(&hdr) } /// Query the maximum amount of memory slots supported by the backend. pub fn get_max_mem_slots(&self) -> Result<u64> { if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits() == 0 { return Err(VhostUserError::InvalidOperation); } let hdr = self.send_request_header(FrontendReq::GET_MAX_MEM_SLOTS, None)?; let val = self.recv_reply::<VhostUserU64>(&hdr)?; Ok(val.value) } /// Add a new guest memory mapping for vhost to use. pub fn add_mem_region(&self, region: &VhostUserMemoryRegionInfo) -> Result<()> { if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits() == 0 { return Err(VhostUserError::InvalidOperation); } if region.memory_size == 0 || region.mmap_handle == INVALID_DESCRIPTOR { return Err(VhostUserError::InvalidParam( "add_mem_region: region empty or mmap handle invalid", )); } let body = VhostUserSingleMemoryRegion::new( region.guest_phys_addr, region.memory_size, region.userspace_addr, region.mmap_offset, ); let fds = [region.mmap_handle]; let hdr = self.send_request_with_body(FrontendReq::ADD_MEM_REG, &body, Some(&fds))?; self.wait_for_ack(&hdr) } /// Remove a guest memory mapping from vhost. pub fn remove_mem_region(&self, region: &VhostUserMemoryRegionInfo) -> Result<()> { if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits() == 0 { return Err(VhostUserError::InvalidOperation); } if region.memory_size == 0 { return Err(VhostUserError::InvalidParam( "remove_mem_region: cannot remove zero sized region", )); } let body = VhostUserSingleMemoryRegion::new( region.guest_phys_addr, region.memory_size, region.userspace_addr, region.mmap_offset, ); let hdr = self.send_request_with_body(FrontendReq::REM_MEM_REG, &body, None)?; self.wait_for_ack(&hdr) } /// Gets the shared memory regions used by the device. pub fn get_shared_memory_regions(&self) -> Result<Vec<VhostSharedMemoryRegion>> { let hdr = self.send_request_header(FrontendReq::GET_SHARED_MEMORY_REGIONS, None)?; let (body_reply, buf_reply, rfds) = self.recv_reply_with_payload::<VhostUserU64>(&hdr)?; let struct_size = mem::size_of::<VhostSharedMemoryRegion>(); if !rfds.is_empty() || buf_reply.len() != body_reply.value as usize * struct_size { return Err(VhostUserError::InvalidMessage); } let mut regions = Vec::new(); let mut offset = 0; for _ in 0..body_reply.value { regions.push( // Can't fail because the input is the correct size. VhostSharedMemoryRegion::read_from(&buf_reply[offset..(offset + struct_size)]) .unwrap(), ); offset += struct_size; } Ok(regions) } fn send_request_header( &self, code: FrontendReq, fds: Option<&[RawDescriptor]>, ) -> VhostUserResult<VhostUserMsgHeader<FrontendReq>> { let hdr = self.new_request_header(code, 0); self.connection.send_header_only_message(&hdr, fds)?; Ok(hdr) } fn send_request_with_body<T: Sized + AsBytes>( &self, code: FrontendReq, msg: &T, fds: Option<&[RawDescriptor]>, ) -> VhostUserResult<VhostUserMsgHeader<FrontendReq>> { let hdr = self.new_request_header(code, mem::size_of::<T>() as u32); self.connection.send_message(&hdr, msg, fds)?; Ok(hdr) } fn send_request_with_payload<T: Sized + AsBytes>( &self, code: FrontendReq, msg: &T, payload: &[u8], fds: Option<&[RawDescriptor]>, ) -> VhostUserResult<VhostUserMsgHeader<FrontendReq>> { if let Some(fd_arr) = fds { if fd_arr.len() > MAX_ATTACHED_FD_ENTRIES { return Err(VhostUserError::InvalidParam( "send_request_with_payload: too many FDs supplied with message", )); } } let len = mem::size_of::<T>() .checked_add(payload.len()) .ok_or(VhostUserError::OversizedMsg)?; let hdr = self.new_request_header( code, len.try_into().map_err(VhostUserError::InvalidCastToInt)?, ); self.connection .send_message_with_payload(&hdr, msg, payload, fds)?; Ok(hdr) } fn send_fd_for_vring( &self, code: FrontendReq, queue_index: usize, fd: RawDescriptor, ) -> VhostUserResult<VhostUserMsgHeader<FrontendReq>> { // Bits (0-7) of the payload contain the vring index. Bit 8 is the invalid FD flag. // This flag is set when there is no file descriptor in the ancillary data. This signals // that polling will be used instead of waiting for the call. let msg = VhostUserU64::new(queue_index as u64); let hdr = self.new_request_header(code, mem::size_of::<VhostUserU64>() as u32); self.connection.send_message(&hdr, &msg, Some(&[fd]))?; Ok(hdr) } fn recv_reply<T: Sized + FromBytes + AsBytes + Default + VhostUserMsgValidator>( &self, hdr: &VhostUserMsgHeader<FrontendReq>, ) -> VhostUserResult<T> { if hdr.is_reply() { return Err(VhostUserError::InvalidParam( "recv_reply: header is not a reply", )); } let (reply, body, rfds) = self.connection.recv_message::<T>()?; if !reply.is_reply_for(hdr) || !rfds.is_empty() || !body.is_valid() { return Err(VhostUserError::InvalidMessage); } Ok(body) } fn recv_reply_with_files<T: Sized + AsBytes + FromBytes + Default + VhostUserMsgValidator>( &self, hdr: &VhostUserMsgHeader<FrontendReq>, ) -> VhostUserResult<(T, Vec<File>)> { if hdr.is_reply() { return Err(VhostUserError::InvalidParam( "with_files: expected a reply, but the header is not marked as a reply", )); } let (reply, body, files) = self.connection.recv_message::<T>()?; if !reply.is_reply_for(hdr) || !body.is_valid() { return Err(VhostUserError::InvalidMessage); } Ok((body, files)) } fn recv_reply_with_payload<T: Sized + AsBytes + FromBytes + Default + VhostUserMsgValidator>( &self, hdr: &VhostUserMsgHeader<FrontendReq>, ) -> VhostUserResult<(T, Vec<u8>, Vec<File>)> { if hdr.is_reply() { return Err(VhostUserError::InvalidParam( "with_payload: expected a reply, but the header is not marked as a reply", )); } let (reply, body, buf, files) = self.connection.recv_message_with_payload::<T>()?; if !reply.is_reply_for(hdr) || !files.is_empty() || !body.is_valid() { return Err(VhostUserError::InvalidMessage); } Ok((body, buf, files)) } fn wait_for_ack(&self, hdr: &VhostUserMsgHeader<FrontendReq>) -> VhostUserResult<()> { if self.acked_protocol_features & VhostUserProtocolFeatures::REPLY_ACK.bits() == 0 || !hdr.is_need_reply() { return Ok(()); } let (reply, body, rfds) = self.connection.recv_message::<VhostUserU64>()?; if !reply.is_reply_for(hdr) || !rfds.is_empty() || !body.is_valid() { return Err(VhostUserError::InvalidMessage); } if body.value != 0 { return Err(VhostUserError::BackendInternalError); } Ok(()) } fn is_feature_mq_available(&self) -> bool { self.acked_protocol_features & VhostUserProtocolFeatures::MQ.bits() != 0 } #[inline] fn new_request_header( &self, request: FrontendReq, size: u32, ) -> VhostUserMsgHeader<FrontendReq> { VhostUserMsgHeader::new(request, 0x1, size) } }
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值