8000 Merge remote-tracking branch 'origin/master' · tsaitsai/esp32-snippets@de9f356 · GitHub
[go: up one dir, main page]

Skip to content

Commit de9f356

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents b22f825 + 412f9d2 commit de9f356

File tree

3 files changed

+85
-56
lines changed

3 files changed

+85
-56
lines changed

cpp_utils/BLEDevice.cpp

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static const char* LOG_TAG = "BLEDevice";
3939
BLEServer* BLEDevice::m_pServer = nullptr;
4040
BLEScan* BLEDevice::m_pScan = nullptr;
4141
BLEClient* BLEDevice::m_pClient = nullptr;
42-
42+
bool initialized = false;
4343
/**
4444
* @brief Create a new instance of a client.
4545
* @return A new instance of the client.
@@ -178,68 +178,77 @@ BLEScan* BLEDevice::getScan() {
178178
* @param deviceName The device name of the device.
179179
*/
180180
void BLEDevice::init(std::string deviceName) {
181-
esp_err_t errRc = ::nvs_flash_init();
182-
if (errRc != ESP_OK) {
183-
ESP_LOGE(LOG_TAG, "nvs_flash_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
184-
return;
185-
}
186-
187-
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
188-
errRc = esp_bt_controller_init(&bt_cfg);
189-
if (errRc != ESP_OK) {
190-
ESP_LOGE(LOG_TAG, "esp_bt_controller_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
191-
return;
192-
}
181+
if(!initialized){
182+
initialized = true;
183+
esp_err_t errRc = ::nvs_flash_init();
184+
if (errRc != ESP_OK) {
185+
ESP_LOGE(LOG_TAG, "nvs_flash_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
186+
return;
187+
}
193188

194-
errRc = esp_bt_controller_enable(ESP_BT_MODE_BLE);
195-
if (errRc != ESP_OK) {
196-
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
197-
return;
198-
}
189+
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
190+
errRc = esp_bt_controller_init(&bt_cfg);
191+
if (errRc != ESP_OK) {
192+
ESP_LOGE(LOG_TAG, "esp_bt_controller_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
193+
return;
194+
}
195+
#ifndef CLASSIC_BT_ENABLED
196+
// esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); //FIXME waiting for response from esp-idf issue
197+
errRc = esp_bt_controller_enable(ESP_BT_MODE_BLE);
198+
if (errRc != ESP_OK) {
199+
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
200+
return;
201+
}
202+
#else
203+
errRc = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
204+
if (errRc != ESP_OK) {
205+
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
206+
return;
207+
}
208+
#endif
209+
errRc = esp_bluedroid_init();
210+
if (errRc != ESP_OK) {
211+
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
212+
return;
213+
}
199214

200-
errRc = esp_bluedroid_init();
201-
if (errRc != ESP_OK) {
202-
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
203-
return;
204-
}
215+
errRc = esp_bluedroid_enable();
216+
if (errRc != ESP_OK) {
217+
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
218+
return;
219+
}
205220

206-
errRc = esp_bluedroid_enable();
207-
if (errRc != ESP_OK) {
208-
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
209-
return;
210-
}
221+
errRc = esp_ble_gap_register_callback(BLEDevice::gapEventHandler);
222+
if (errRc != ESP_OK) {
223+
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
224+
return;
225+
}
211226

212-
errRc = esp_ble_gap_register_callback(BLEDevice::gapEventHandler);
213-
if (errRc != ESP_OK) {
214-
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
215-
return;
216-
}
227+
errRc = esp_ble_gattc_register_callback(BLEDevice::gattClientEventHandler);
228+
if (errRc != ESP_OK) {
229+
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
230+
return;
231+
}
217232

218-
errRc = esp_ble_gattc_register_callback(BLEDevice::gattClientEventHandler);
219-
if (errRc != ESP_OK) {
220-
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
221-
return;
222-
}
233+
errRc = esp_ble_gatts_register_callback(BLEDevice::gattServerEventHandler);
234+
if (errRc != ESP_OK) {
235+
ESP_LOGE(LOG_TAG, "esp_ble_gatts_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
236+
return;
237+
}
223238

224-
errRc = esp_ble_gatts_register_callback(BLEDevice::gattServerEventHandler);
225-
if (errRc != ESP_OK) {
226-
ESP_LOGE(LOG_TAG, "esp_ble_gatts_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
227-
return;
239+
errRc = ::esp_ble_gap_set_device_name(deviceName.c_str());
240+
if (errRc != ESP_OK) {
241+
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_device_name: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
242+
return;
243+
};
244+
245+
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
246+
errRc = ::esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
247+
if (errRc != ESP_OK) {
248+
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_security_param: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
249+
return;
250+
};
228251
}
229-
230-
errRc = ::esp_ble_gap_set_device_name(deviceName.c_str());
231-
if (errRc != ESP_OK) {
232-
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_device_name: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
233-
return;
234-
};
235-
236-
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
237-
errRc = ::esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
238-
if (errRc != ESP_OK) {
239-
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_security_param: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
240-
return;
241-
};
242-
243252
vTaskDelay(200/portTICK_PERIOD_MS); // Delay for 200 msecs as a workaround to an apparent Arduino environment issue.
244253
} // init
245254

cpp_utils/BLEUUID.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ BLEUUID::BLEUUID(uint16_t uuid) {
144144
m_uuid.len = ESP_UUID_LEN_16;
145145
m_uuid.uuid.uuid16 = uuid;
146146
m_valueSet = true;
147+
147148
} // BLEUUID
148149

149150

@@ -345,4 +346,22 @@ std::string BLEUUID::toString() {
345346
std::setw(2) << (int)m_uuid.uuid.uuid128[0];
346347
return ss.str();
347348
} // toString
349+
350+
BLEUUID BLEUUID::fromString(std::string _uuid){
351+
uint8_t start = 0;
352+
if(strstr(_uuid.c_str(), "0x") != nullptr){
353+
start = 2;
354+
}
355+
uint8_t len = _uuid.length() - start;
356+
if(len == 4 ){
357+
uint16_t x = strtoul(_uuid.substr(start, len).c_str(), NULL, 16);
358+
return BLEUUID(x);
359+
}else if(len == 8){
360+
uint32_t x = strtoul(_uuid.substr(start, len).c_str(), NULL, 16);
361+
return BLEUUID(x);
362+
}else if (len == 36){
363+
return BLEUUID(_uuid);
364+
}
365+
return BLEUUID();
366+
}
348367
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLEUUID.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class BLEUUID {
2828
esp_bt_uuid_t* getNative();
2929
BLEUUID to128();
3030
std::string toString();
31+
static BLEUUID fromString(std::string uuid);
3132

3233
private:
3334
esp_bt_uuid_t m_uuid; // The underlying UUID structure that this class wraps.

0 commit comments

Comments
 (0)
0