1
- #include " myhomeiot_ble_client.h"
2
- #include " esphome/core/log.h"
3
-
4
1
#ifdef ARDUINO_ARCH_ESP32
5
2
6
3
#include < esp_gap_ble_api.h>
4
+ #include " esphome/core/log.h"
5
+ #include " myhomeiot_ble_client.h"
7
6
8
7
namespace esphome {
9
8
namespace myhomeiot_ble_client {
@@ -16,7 +15,7 @@ void MyHomeIOT_BLEClient::setup() {
16
15
17
16
void MyHomeIOT_BLEClient::dump_config () {
18
17
ESP_LOGCONFIG (TAG, " MyHomeIOT BLE Client" );
19
- ESP_LOGCONFIG (TAG, " MAC address: %s" , to_string (this ->address ).c_str ());
18
+ ESP_LOGCONFIG (TAG, " MAC address: %s" , to_string (this ->address_ ).c_str ());
20
19
ESP_LOGCONFIG (TAG, " Service UUID: %s" , this ->service_uuid_ .to_string ().c_str ());
21
20
ESP_LOGCONFIG (TAG, " Characteristic UUID: %s" , this ->char_uuid_ .to_string ().c_str ());
22
21
LOG_UPDATE_INTERVAL (this );
@@ -30,31 +29,31 @@ void MyHomeIOT_BLEClient::loop() {
30
29
}
31
30
32
31
void MyHomeIOT_BLEClient::connect () {
33
- ESP_LOGI (TAG, " [%s] Connecting" , to_string (this ->address ).c_str ());
32
+ ESP_LOGI (TAG, " [%s] Connecting" , to_string (this ->address_ ).c_str ());
34
33
this ->state_ = MYHOMEIOT_CONNECTING;
35
- if (auto status = esp_ble_gattc_open (ble_host_->gattc_if , this ->remote_bda , BLE_ADDR_TYPE_PUBLIC, true ))
34
+ if (auto status = esp_ble_gattc_open (ble_host_->gattc_if , this ->remote_bda_ , BLE_ADDR_TYPE_PUBLIC, true ))
36
35
{
37
- ESP_LOGW (TAG, " [%s] open error, status (%d)" , to_string (this ->address ).c_str (), status);
36
+ ESP_LOGW (TAG, " [%s] open error, status (%d)" , to_string (this ->address_ ).c_str (), status);
38
37
report_error (MYHOMEIOT_IDLE);
39
38
}
40
39
}
41
40
42
41
void MyHomeIOT_BLEClient::disconnect () {
43
- ESP_LOGI (TAG, " [%s] Disconnecting" , to_string (this ->address ).c_str ());
42
+ ESP_LOGI (TAG, " [%s] Disconnecting" , to_string (this ->address_ ).c_str ());
44
43
this ->state_ = MYHOMEIOT_IDLE;
45
- if (auto status = esp_ble_gattc_close (ble_host_->gattc_if , this ->conn_id ))
46
- ESP_LOGW (TAG, " [%s] close error, status (%d)" , to_string (this ->address ).c_str (), status);
44
+ if (auto status = esp_ble_gattc_close (ble_host_->gattc_if , this ->conn_id_ ))
45
+ ESP_LOGW (TAG, " [%s] close error, status (%d)" , to_string (this ->address_ ).c_str (), status);
47
46
}
48
47
49
48
void MyHomeIOT_BLEClient::update () {
50
- this ->is_update_requested = true ;
49
+ this ->is_update_requested_ = true ;
51
50
}
52
51
53
52
void MyHomeIOT_BLEClient::report_results (uint8_t *data, uint16_t len) {
54
53
this ->status_clear_warning ();
55
- std::vector<uint8_t > ret (data, data + len);
56
- this ->callback_ .call (ret );
57
- this ->is_update_requested = false ;
54
+ std::vector<uint8_t > value (data, data + len);
55
+ this ->callback_ .call (value, * this );
56
+ this ->is_update_requested_ = false ;
58
57
}
59
58
60
59
void MyHomeIOT_BLEClient::report_error (esp32_ble_tracker::ClientState state) {
@@ -63,12 +62,12 @@ void MyHomeIOT_BLEClient::report_error(esp32_ble_tracker::ClientState state) {
63
62
}
64
63
65
64
bool MyHomeIOT_BLEClient::parse_device (const esp32_ble_tracker::ESPBTDevice &device) {
66
- if (!this ->is_update_requested || this ->state_ != MYHOMEIOT_IDLE
67
- || device.address_uint64 () != this ->address )
65
+ if (!this ->is_update_requested_ || this ->state_ != MYHOMEIOT_IDLE
66
+ || device.address_uint64 () != this ->address_ )
68
67
return false ;
69
68
70
69
ESP_LOGD (TAG, " [%s] Found device" , device.address_str ().c_str ());
71
- memcpy (this ->remote_bda , device.address (), sizeof (this ->remote_bda ));
70
+ memcpy (this ->remote_bda_ , device.address (), sizeof (this ->remote_bda_ ));
72
71
this ->state_ = MYHOMEIOT_DISCOVERED;
73
72
return true ;
74
73
}
@@ -77,71 +76,71 @@ void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
77
76
esp_ble_gattc_cb_param_t *param) {
78
77
switch (event) {
79
78
case ESP_GATTC_OPEN_EVT: {
80
- if (memcmp (param->open .remote_bda , this ->remote_bda , sizeof (this ->remote_bda )) != 0 )
79
+ if (memcmp (param->open .remote_bda , this ->remote_bda_ , sizeof (this ->remote_bda_ )) != 0 )
81
80
break ;
82
81
if (param->open .status != ESP_GATT_OK) {
83
- ESP_LOGW (TAG, " [%s] OPEN_EVT failed, status (%d), app_id (%d)" , to_string (this ->address ).c_str (),
82
+ ESP_LOGW (TAG, " [%s] OPEN_EVT failed, status (%d), app_id (%d)" , to_string (this ->address_ ).c_str (),
84
83
param->open .status , ble_host_->app_id );
85
84
report_error (MYHOMEIOT_IDLE);
86
85
break ;
87
86
}
88
- ESP_LOGI (TAG, " [%s] Connected successfully, app_id (%d)" , to_string (this ->address ).c_str (), ble_host_->app_id );
89
- this ->conn_id = param->open .conn_id ;
87
+ ESP_LOGI (TAG, " [%s] Connected successfully, app_id (%d)" , to_string (this ->address_ ).c_str (), ble_host_->app_id );
88
+ this ->conn_id_ = param->open .conn_id ;
90
89
if (auto status = esp_ble_gattc_send_mtu_req (ble_host_->gattc_if , param->open .conn_id ))
91
90
{
92
- ESP_LOGW (TAG, " [%s] send_mtu_req failed, status (%d)" , to_string (this ->address ).c_str (), status);
91
+ ESP_LOGW (TAG, " [%s] send_mtu_req failed, status (%d)" , to_string (this ->address_ ).c_str (), status);
93
92
report_error ();
94
93
break ;
95
94
}
96
- this ->start_handle = this ->end_handle = this ->char_handle = ESP_GATT_ILLEGAL_HANDLE;
95
+ this ->start_handle_ = this ->end_handle_ = this ->char_handle_ = ESP_GATT_ILLEGAL_HANDLE;
97
96
this ->state_ = MYHOMEIOT_CONNECTED;
98
97
break ;
99
98
}
100
99
case ESP_GATTC_CFG_MTU_EVT: {
101
- if (param->cfg_mtu .conn_id != this ->conn_id )
100
+ if (param->cfg_mtu .conn_id != this ->conn_id_ )
102
101
break ;
103
102
if (param->cfg_mtu .status != ESP_GATT_OK) {
104
- ESP_LOGW (TAG, " [%s] CFG_MTU_EVT failed, status (%d)" , to_string (this ->address ).c_str (),
103
+ ESP_LOGW (TAG, " [%s] CFG_MTU_EVT failed, status (%d)" , to_string (this ->address_ ).c_str (),
105
104
param->cfg_mtu .status );
106
105
report_error ();
107
106
break ;
108
107
}
109
- ESP_LOGV (TAG, " [%s] CFG_MTU_EVT, MTU (%d)" , to_string (this ->address ).c_str (), param->cfg_mtu .mtu );
108
+ ESP_LOGV (TAG, " [%s] CFG_MTU_EVT, MTU (%d)" , to_string (this ->address_ ).c_str (), param->cfg_mtu .mtu );
110
109
if (auto status = esp_ble_gattc_search_service (esp_gattc_if, param->cfg_mtu .conn_id , nullptr )) {
111
- ESP_LOGW (TAG, " [%s] search_service failed, status (%d)" , to_string (this ->address ).c_str (), status);
110
+ ESP_LOGW (TAG, " [%s] search_service failed, status (%d)" , to_string (this ->address_ ).c_str (), status);
112
111
report_error ();
113
112
}
114
113
break ;
115
114
}
116
115
case ESP_GATTC_DISCONNECT_EVT: {
117
- if (memcmp (param->disconnect .remote_bda , this ->remote_bda , sizeof (this ->remote_bda )) != 0 )
116
+ if (memcmp (param->disconnect .remote_bda , this ->remote_bda_ , sizeof (this ->remote_bda_ )) != 0 )
118
117
break ;
119
- ESP_LOGD (TAG, " [%s] DISCONNECT_EVT" , to_string (this ->address ).c_str ());
118
+ ESP_LOGD (TAG, " [%s] DISCONNECT_EVT" , to_string (this ->address_ ).c_str ());
120
119
this ->state_ = MYHOMEIOT_IDLE;
121
120
break ;
122
121
}
123
122
case ESP_GATTC_SEARCH_RES_EVT: {
124
- if (param->search_res .conn_id != this ->conn_id )
123
+ if (param->search_res .conn_id != this ->conn_id_ )
125
124
break ;
126
125
esp32_ble_tracker::ESPBTUUID uuid = param->search_res .srvc_id .uuid .len == ESP_UUID_LEN_16 ? esp32_ble_tracker::ESPBTUUID::from_uint16 (param->search_res .srvc_id .uuid .uuid .uuid16 )
127
126
: param->search_res .srvc_id .uuid .len == ESP_UUID_LEN_32 ? esp32_ble_tracker::ESPBTUUID::from_uint32 (param->search_res .srvc_id .uuid .uuid .uuid32 )
128
127
: esp32_ble_tracker::ESPBTUUID::from_raw (param->search_res .srvc_id .uuid .uuid .uuid128 );
129
128
if (uuid == this ->service_uuid_ ) {
130
- ESP_LOGD (TAG, " [%s] SEARCH_RES_EVT service (%s) found" , to_string (this ->address ).c_str (),
129
+ ESP_LOGD (TAG, " [%s] SEARCH_RES_EVT service (%s) found" , to_string (this ->address_ ).c_str (),
131
130
this ->service_uuid_ .to_string ().c_str ());
132
- start_handle = param->search_res .start_handle ;
133
- end_handle = param->search_res .end_handle ;
131
+ this -> start_handle_ = param->search_res .start_handle ;
132
+ this -> end_handle_ = param->search_res .end_handle ;
134
133
}
135
134
break ;
136
135
}
137
136
case ESP_GATTC_SEARCH_CMPL_EVT: {
138
- if (param->search_cmpl .conn_id != this ->conn_id )
137
+ if (param->search_cmpl .conn_id != this ->conn_id_ )
139
138
break ;
140
- ESP_LOGV (TAG, " [%s] SEARCH_CMPL_EVT" , to_string (this ->address ).c_str ());
139
+ ESP_LOGV (TAG, " [%s] SEARCH_CMPL_EVT" , to_string (this ->address_ ).c_str ());
141
140
142
- if (this ->start_handle == ESP_GATT_ILLEGAL_HANDLE)
141
+ if (this ->start_handle_ == ESP_GATT_ILLEGAL_HANDLE)
143
142
{
144
- ESP_LOGE (TAG, " [%s] SEARCH_CMPL_EVT service (%s) not found" , to_string (this ->address ).c_str (),
143
+ ESP_LOGE (TAG, " [%s] SEARCH_CMPL_EVT service (%s) not found" , to_string (this ->address_ ).c_str (),
145
144
this ->service_uuid_ .to_string ().c_str ());
146
145
report_error ();
147
146
break ;
@@ -151,47 +150,47 @@ void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
151
150
esp_gattc_char_elem_t result;
152
151
while (true ) {
153
152
uint16_t count = 1 ;
154
- auto status = esp_ble_gattc_get_all_char (ble_host_->gattc_if , this ->conn_id ,
155
- this ->start_handle , this ->end_handle , &result, &count, offset);
153
+ auto status = esp_ble_gattc_get_all_char (ble_host_->gattc_if , this ->conn_id_ ,
154
+ this ->start_handle_ , this ->end_handle_ , &result, &count, offset);
156
155
if (status != ESP_GATT_OK) {
157
156
if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND)
158
157
break ;
159
- ESP_LOGW (TAG, " [%s] get_all_char error, status (%d)" , to_string (this ->address ).c_str (), status);
158
+ ESP_LOGW (TAG, " [%s] get_all_char error, status (%d)" , to_string (this ->address_ ).c_str (), status);
160
159
report_error ();
161
160
break ;
162
161
}
163
162
if (count == 0 )
164
163
break ;
165
164
166
165
if (this ->char_uuid_ == esp32_ble_tracker::ESPBTUUID::from_uuid (result.uuid )) {
167
- ESP_LOGD (TAG, " [%s] SEARCH_CMPL_EVT char (%s) found" , to_string (this ->address ).c_str (),
166
+ ESP_LOGD (TAG, " [%s] SEARCH_CMPL_EVT char (%s) found" , to_string (this ->address_ ).c_str (),
168
167
this ->char_uuid_ .to_string ().c_str ());
169
- this ->char_handle = result.char_handle ;
168
+ this ->char_handle_ = result.char_handle ;
170
169
171
- if (auto status = esp_ble_gattc_read_char (ble_host_->gattc_if , this ->conn_id ,
172
- this ->char_handle , ESP_GATT_AUTH_REQ_NONE) != ESP_GATT_OK) {
170
+ if (auto status = esp_ble_gattc_read_char (ble_host_->gattc_if , this ->conn_id_ ,
171
+ this ->char_handle_ , ESP_GATT_AUTH_REQ_NONE) != ESP_GATT_OK) {
173
172
ESP_LOGW (TAG, " [%s] read_char error sending read request, status (%d)" ,
174
- to_string (this ->address ).c_str (), status);
175
- this ->char_handle = ESP_GATT_ILLEGAL_HANDLE;
173
+ to_string (this ->address_ ).c_str (), status);
174
+ this ->char_handle_ = ESP_GATT_ILLEGAL_HANDLE;
176
175
}
177
176
break ;
178
177
}
179
178
offset++;
180
179
}
181
- if (this ->char_handle == ESP_GATT_ILLEGAL_HANDLE)
180
+ if (this ->char_handle_ == ESP_GATT_ILLEGAL_HANDLE)
182
181
{
183
- ESP_LOGE (TAG, " [%s] SEARCH_CMPL_EVT char (%s) not found" , to_string (this ->address ).c_str (),
182
+ ESP_LOGE (TAG, " [%s] SEARCH_CMPL_EVT char (%s) not found" , to_string (this ->address_ ).c_str (),
184
183
this ->char_uuid_ .to_string ().c_str ());
185
184
report_error ();
186
185
}
187
186
break ;
188
187
}
189
188
case ESP_GATTC_READ_CHAR_EVT: {
190
- if (param->read .conn_id != this ->conn_id || param->read .handle != this ->char_handle )
189
+ if (param->read .conn_id != this ->conn_id_ || param->read .handle != this ->char_handle_ )
191
190
break ;
192
191
if (param->read .status != ESP_GATT_OK)
193
192
{
194
- ESP_LOGW (TAG, " [%s] READ_CHAR_EVT error reading char at handle (%d), status (%d)" , to_string (this ->address ).c_str (),
193
+ ESP_LOGW (TAG, " [%s] READ_CHAR_EVT error reading char at handle (%d), status (%d)" , to_string (this ->address_ ).c_str (),
195
194
param->read .handle , param->read .status );
196
195
report_error ();
197
196
break ;
0 commit comments