12 namespace bluetooth_proxy {
14 static const char *
const TAG =
"bluetooth_proxy.connection";
17 ESP_LOGCONFIG(TAG,
"BLE Connection:");
22 esp_ble_gattc_cb_param_t *param) {
27 case ESP_GATTC_DISCONNECT_EVT: {
33 case ESP_GATTC_CLOSE_EVT: {
39 case ESP_GATTC_OPEN_EVT: {
40 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
51 case ESP_GATTC_CFG_MTU_EVT:
52 case ESP_GATTC_SEARCH_CMPL_EVT: {
64 case ESP_GATTC_READ_DESCR_EVT:
65 case ESP_GATTC_READ_CHAR_EVT: {
66 if (param->read.status != ESP_GATT_OK) {
67 ESP_LOGW(TAG,
"[%d] [%s] Error reading char/descriptor at handle 0x%2X, status=%d", this->
connection_index_,
68 this->
address_str_.c_str(), param->read.handle, param->read.status);
74 resp.
handle = param->read.handle;
75 resp.
data.reserve(param->read.value_len);
76 for (uint16_t i = 0; i < param->read.value_len; i++) {
77 resp.
data.push_back(param->read.value[i]);
82 case ESP_GATTC_WRITE_CHAR_EVT:
83 case ESP_GATTC_WRITE_DESCR_EVT: {
84 if (param->write.status != ESP_GATT_OK) {
85 ESP_LOGW(TAG,
"[%d] [%s] Error writing char/descriptor at handle 0x%2X, status=%d", this->
connection_index_,
86 this->
address_str_.c_str(), param->write.handle, param->write.status);
92 resp.
handle = param->write.handle;
96 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
97 if (param->unreg_for_notify.status != ESP_GATT_OK) {
98 ESP_LOGW(TAG,
"[%d] [%s] Error unregistering notifications for handle 0x%2X, status=%d",
100 param->unreg_for_notify.status);
106 resp.
handle = param->unreg_for_notify.handle;
110 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
111 if (param->reg_for_notify.status != ESP_GATT_OK) {
112 ESP_LOGW(TAG,
"[%d] [%s] Error registering notifications for handle 0x%2X, status=%d", this->
connection_index_,
113 this->
address_str_.c_str(), param->reg_for_notify.handle, param->reg_for_notify.status);
119 resp.
handle = param->reg_for_notify.handle;
123 case ESP_GATTC_NOTIFY_EVT: {
125 param->notify.handle);
128 resp.handle = param->notify.handle;
129 resp.data.reserve(param->notify.value_len);
130 for (uint16_t i = 0; i < param->notify.value_len; i++) {
131 resp.data.push_back(param->notify.value[i]);
146 case ESP_GAP_BLE_AUTH_CMPL_EVT:
147 if (memcmp(param->ble_security.auth_cmpl.bd_addr, this->remote_bda_, 6) != 0)
149 if (param->ble_security.auth_cmpl.success) {
162 ESP_LOGW(TAG,
"[%d] [%s] Cannot read GATT characteristic, not connected.", this->
connection_index_,
164 return ESP_GATT_NOT_CONNECTED;
170 esp_err_t err = esp_ble_gattc_read_char(this->
gattc_if_, this->
conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
172 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_read_char error, err=%d", this->
connection_index_,
181 ESP_LOGW(TAG,
"[%d] [%s] Cannot write GATT characteristic, not connected.", this->
connection_index_,
183 return ESP_GATT_NOT_CONNECTED;
189 esp_ble_gattc_write_char(this->
gattc_if_, this->
conn_id_, handle, data.size(), (uint8_t *) data.data(),
190 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
192 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_write_char error, err=%d", this->
connection_index_,
201 ESP_LOGW(TAG,
"[%d] [%s] Cannot read GATT descriptor, not connected.", this->
connection_index_,
203 return ESP_GATT_NOT_CONNECTED;
208 esp_err_t err = esp_ble_gattc_read_char_descr(this->
gattc_if_, this->
conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
210 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_read_char_descr error, err=%d", this->
connection_index_,
219 ESP_LOGW(TAG,
"[%d] [%s] Cannot write GATT descriptor, not connected.", this->
connection_index_,
221 return ESP_GATT_NOT_CONNECTED;
226 esp_err_t err = esp_ble_gattc_write_char_descr(
228 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
230 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_write_char_descr error, err=%d", this->
connection_index_,
239 ESP_LOGW(TAG,
"[%d] [%s] Cannot notify GATT characteristic, not connected.", this->
connection_index_,
241 return ESP_GATT_NOT_CONNECTED;
245 ESP_LOGV(TAG,
"[%d] [%s] Registering for GATT characteristic notifications handle %d", this->
connection_index_,
249 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_register_for_notify failed, err=%d", this->
connection_index_,
254 ESP_LOGV(TAG,
"[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->
connection_index_,
256 esp_err_t err = esp_ble_gattc_unregister_for_notify(this->
gattc_if_, this->
remote_bda_, handle);
258 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_unregister_for_notify failed, err=%d", this->
connection_index_,
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
void send_connections_free()
void send_device_pairing(uint64_t address, bool paired, esp_err_t error=ESP_OK)
bool send_bluetooth_gatt_read_response(const BluetoothGATTReadResponse &msg)
bool seen_mtu_or_services_
bool send_bluetooth_gatt_notify_data_response(const BluetoothGATTNotifyDataResponse &msg)
esp_err_t write_descriptor(uint16_t handle, const std::string &data, bool response)
void dump_config() override
esp_err_t read_descriptor(uint16_t handle)
esp_err_t write_characteristic(uint16_t handle, const std::string &data, bool response)
bool send_bluetooth_gatt_write_response(const BluetoothGATTWriteResponse &msg)
esp_bd_addr_t remote_bda_
api::APIConnection * get_api_connection()
esp32_ble_tracker::AdvertisementParserType get_advertisement_parser_type() override
uint8_t connection_index_
void send_device_connection(uint64_t address, bool connected, uint16_t mtu=0, esp_err_t error=ESP_OK)
espbt::ConnectionType connection_type_
bool send_bluetooth_gatt_notify_response(const BluetoothGATTNotifyResponse &msg)
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
esp32_ble_tracker::AdvertisementParserType get_advertisement_parser_type() override
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
void set_address(uint64_t address)
Implementation of SPI Controller mode.
void send_gatt_error(uint64_t address, uint16_t handle, esp_err_t error)
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
esp_err_t read_characteristic(uint16_t handle)
esp_err_t notify_characteristic(uint16_t handle, bool enable)
void dump_config() override