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_OPEN_EVT: {
29 if (param->open.conn_id != this->conn_id_)
31 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
42 case ESP_GATTC_CFG_MTU_EVT: {
43 if (param->cfg_mtu.conn_id != this->conn_id_)
56 case ESP_GATTC_SEARCH_CMPL_EVT: {
57 if (param->search_cmpl.conn_id != this->conn_id_)
70 case ESP_GATTC_READ_DESCR_EVT:
71 case ESP_GATTC_READ_CHAR_EVT: {
72 if (param->read.conn_id != this->conn_id_)
74 if (param->read.status != ESP_GATT_OK) {
75 ESP_LOGW(TAG,
"[%d] [%s] Error reading char/descriptor at handle 0x%2X, status=%d", this->
connection_index_,
76 this->
address_str_.c_str(), param->read.handle, param->read.status);
82 resp.
handle = param->read.handle;
83 resp.
data.reserve(param->read.value_len);
84 for (uint16_t i = 0; i < param->read.value_len; i++) {
85 resp.
data.push_back(param->read.value[i]);
90 case ESP_GATTC_WRITE_CHAR_EVT:
91 case ESP_GATTC_WRITE_DESCR_EVT: {
92 if (param->write.conn_id != this->conn_id_)
94 if (param->write.status != ESP_GATT_OK) {
95 ESP_LOGW(TAG,
"[%d] [%s] Error writing char/descriptor at handle 0x%2X, status=%d", this->
connection_index_,
96 this->
address_str_.c_str(), param->write.handle, param->write.status);
102 resp.
handle = param->write.handle;
106 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
107 if (param->unreg_for_notify.status != ESP_GATT_OK) {
108 ESP_LOGW(TAG,
"[%d] [%s] Error unregistering notifications for handle 0x%2X, status=%d",
110 param->unreg_for_notify.status);
116 resp.
handle = param->unreg_for_notify.handle;
120 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
121 if (param->reg_for_notify.status != ESP_GATT_OK) {
122 ESP_LOGW(TAG,
"[%d] [%s] Error registering notifications for handle 0x%2X, status=%d", this->
connection_index_,
123 this->
address_str_.c_str(), param->reg_for_notify.handle, param->reg_for_notify.status);
129 resp.
handle = param->reg_for_notify.handle;
133 case ESP_GATTC_NOTIFY_EVT: {
134 if (param->notify.conn_id != this->conn_id_)
137 param->notify.handle);
140 resp.handle = param->notify.handle;
141 resp.data.reserve(param->notify.value_len);
142 for (uint16_t i = 0; i < param->notify.value_len; i++) {
143 resp.data.push_back(param->notify.value[i]);
158 case ESP_GAP_BLE_AUTH_CMPL_EVT:
159 if (memcmp(param->ble_security.auth_cmpl.bd_addr, this->remote_bda_, 6) != 0)
161 if (param->ble_security.auth_cmpl.success) {
174 ESP_LOGW(TAG,
"[%d] [%s] Cannot read GATT characteristic, not connected.", this->
connection_index_,
176 return ESP_GATT_NOT_CONNECTED;
182 esp_err_t err = esp_ble_gattc_read_char(this->
gattc_if_, this->
conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
184 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_read_char error, err=%d", this->
connection_index_,
193 ESP_LOGW(TAG,
"[%d] [%s] Cannot write GATT characteristic, not connected.", this->
connection_index_,
195 return ESP_GATT_NOT_CONNECTED;
201 esp_ble_gattc_write_char(this->
gattc_if_, this->
conn_id_, handle, data.size(), (uint8_t *) data.data(),
202 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
204 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_write_char error, err=%d", this->
connection_index_,
213 ESP_LOGW(TAG,
"[%d] [%s] Cannot read GATT descriptor, not connected.", this->
connection_index_,
215 return ESP_GATT_NOT_CONNECTED;
220 esp_err_t err = esp_ble_gattc_read_char_descr(this->
gattc_if_, this->
conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
222 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_read_char_descr error, err=%d", this->
connection_index_,
231 ESP_LOGW(TAG,
"[%d] [%s] Cannot write GATT descriptor, not connected.", this->
connection_index_,
233 return ESP_GATT_NOT_CONNECTED;
238 esp_err_t err = esp_ble_gattc_write_char_descr(
240 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
242 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_write_char_descr error, err=%d", this->
connection_index_,
251 ESP_LOGW(TAG,
"[%d] [%s] Cannot notify GATT characteristic, not connected.", this->
connection_index_,
253 return ESP_GATT_NOT_CONNECTED;
257 ESP_LOGV(TAG,
"[%d] [%s] Registering for GATT characteristic notifications handle %d", this->
connection_index_,
261 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_register_for_notify failed, err=%d", this->
connection_index_,
266 ESP_LOGV(TAG,
"[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->
connection_index_,
268 esp_err_t err = esp_ble_gattc_unregister_for_notify(this->
gattc_if_, this->
remote_bda_, handle);
270 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)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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)