12 namespace bluetooth_proxy {
14 static const char *
const TAG =
"bluetooth_proxy.connection";
17 esp_ble_gattc_cb_param_t *param) {
22 case ESP_GATTC_DISCONNECT_EVT: {
28 case ESP_GATTC_CLOSE_EVT: {
34 case ESP_GATTC_OPEN_EVT: {
35 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
46 case ESP_GATTC_CFG_MTU_EVT:
47 case ESP_GATTC_SEARCH_CMPL_EVT: {
59 case ESP_GATTC_READ_DESCR_EVT:
60 case ESP_GATTC_READ_CHAR_EVT: {
61 if (param->read.status != ESP_GATT_OK) {
62 ESP_LOGW(TAG,
"[%d] [%s] Error reading char/descriptor at handle 0x%2X, status=%d", this->
connection_index_,
63 this->
address_str_.c_str(), param->read.handle, param->read.status);
69 resp.
handle = param->read.handle;
70 resp.
data.reserve(param->read.value_len);
71 for (uint16_t i = 0; i < param->read.value_len; i++) {
72 resp.
data.push_back(param->read.value[i]);
77 case ESP_GATTC_WRITE_CHAR_EVT:
78 case ESP_GATTC_WRITE_DESCR_EVT: {
79 if (param->write.status != ESP_GATT_OK) {
80 ESP_LOGW(TAG,
"[%d] [%s] Error writing char/descriptor at handle 0x%2X, status=%d", this->
connection_index_,
81 this->
address_str_.c_str(), param->write.handle, param->write.status);
87 resp.
handle = param->write.handle;
91 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
92 if (param->unreg_for_notify.status != ESP_GATT_OK) {
93 ESP_LOGW(TAG,
"[%d] [%s] Error unregistering notifications for handle 0x%2X, status=%d",
95 param->unreg_for_notify.status);
101 resp.
handle = param->unreg_for_notify.handle;
105 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
106 if (param->reg_for_notify.status != ESP_GATT_OK) {
107 ESP_LOGW(TAG,
"[%d] [%s] Error registering notifications for handle 0x%2X, status=%d", this->
connection_index_,
108 this->
address_str_.c_str(), param->reg_for_notify.handle, param->reg_for_notify.status);
114 resp.
handle = param->reg_for_notify.handle;
118 case ESP_GATTC_NOTIFY_EVT: {
120 param->notify.handle);
123 resp.handle = param->notify.handle;
124 resp.data.reserve(param->notify.value_len);
125 for (uint16_t i = 0; i < param->notify.value_len; i++) {
126 resp.data.push_back(param->notify.value[i]);
141 case ESP_GAP_BLE_AUTH_CMPL_EVT:
142 if (memcmp(param->ble_security.auth_cmpl.bd_addr, this->remote_bda_, 6) != 0)
144 if (param->ble_security.auth_cmpl.success) {
157 ESP_LOGW(TAG,
"[%d] [%s] Cannot read GATT characteristic, not connected.", this->
connection_index_,
159 return ESP_GATT_NOT_CONNECTED;
165 esp_err_t err = esp_ble_gattc_read_char(this->
gattc_if_, this->
conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
167 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_read_char error, err=%d", this->
connection_index_,
176 ESP_LOGW(TAG,
"[%d] [%s] Cannot write GATT characteristic, not connected.", this->
connection_index_,
178 return ESP_GATT_NOT_CONNECTED;
184 esp_ble_gattc_write_char(this->
gattc_if_, this->
conn_id_, handle, data.size(), (uint8_t *) data.data(),
185 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
187 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_write_char error, err=%d", this->
connection_index_,
196 ESP_LOGW(TAG,
"[%d] [%s] Cannot read GATT descriptor, not connected.", this->
connection_index_,
198 return ESP_GATT_NOT_CONNECTED;
203 esp_err_t err = esp_ble_gattc_read_char_descr(this->
gattc_if_, this->
conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
205 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_read_char_descr error, err=%d", this->
connection_index_,
214 ESP_LOGW(TAG,
"[%d] [%s] Cannot write GATT descriptor, not connected.", this->
connection_index_,
216 return ESP_GATT_NOT_CONNECTED;
221 esp_err_t err = esp_ble_gattc_write_char_descr(
223 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
225 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_write_char_descr error, err=%d", this->
connection_index_,
234 ESP_LOGW(TAG,
"[%d] [%s] Cannot notify GATT characteristic, not connected.", this->
connection_index_,
236 return ESP_GATT_NOT_CONNECTED;
240 ESP_LOGV(TAG,
"[%d] [%s] Registering for GATT characteristic notifications handle %d", this->
connection_index_,
244 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_register_for_notify failed, err=%d", this->
connection_index_,
249 ESP_LOGV(TAG,
"[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->
connection_index_,
251 esp_err_t err = esp_ble_gattc_unregister_for_notify(this->
gattc_if_, this->
remote_bda_, handle);
253 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)
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)