8000 Add fromString to BLEUUID · tsaitsai/esp32-snippets@c1011fd · GitHub
[go: up one dir, main page]

Skip to content

Commit c1011fd

Browse files
committed
Add fromString to BLEUUID
1 parent 90167ad commit c1011fd

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.
@@ -175,68 +175,77 @@ BLEScan* BLEDevice::getScan() {
175175
* @param deviceName The device name of the device.
176176
*/
177177
void BLEDevice::init(std::string deviceName) {
178-
esp_err_t errRc = ::nvs_flash_init();
179-
if (errRc != ESP_OK) {
180-
ESP_LOGE(LOG_TAG, "nvs_flash_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
181-
return;
182-
}
183-
184-
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
185-
errRc = esp_bt_controller_init(&bt_cfg);
186-
if (errRc != ESP_OK) {
187-
ESP_LOGE(LOG_TAG, "esp_bt_controller_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
188-
return;
189-
}
178+
if(!initialized){
179+
initialized = true;
180+
esp_err_t errRc = ::nvs_flash_init();
181+
if (errRc != ESP_OK) {
182+
ESP_LOGE(LOG_TAG, "nvs_flash_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
183+
return;
184+
}
190185

191-
errRc = esp_bt_controller_enable(ESP_BT_MODE_BLE);
192-
if (errRc != ESP_OK) {
193-
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
194-
return;
195-
}
186+
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
187+
errRc = esp_bt_controller_init(&bt_cfg);
188+
if (errRc != ESP_OK) {
189+
ESP_LOGE(LOG_TAG, "esp_bt_controller_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
190+
return;
191+
}
192+
#ifndef CLASSIC_BT_ENABLED
193+
// esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); //FIXME waiting for response from esp-idf issue
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+
}
199+
#else
200+
errRc = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
201+
if (errRc != ESP_OK) {
202+
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
203+
return;
204+
}
205+
#endif
206+
errRc = esp_bluedroid_init();
207+
if (errRc != ESP_OK) {
208+
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
209+
return;
210+
}
196211

197-
errRc = esp_bluedroid_init();
198-
if (errRc != ESP_OK) {
199-
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
200-
return;
201-
}
212+
errRc = esp_bluedroid_enable();
213+
if (errRc != ESP_OK) {
214+
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
215+
return;
216+
}
202217

203-
errRc = esp_bluedroid_enable();
204-
if (errRc != ESP_OK) {
205-
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
206-
return;
207-
}
218+
errRc = esp_ble_gap_register_callback(BLEDevice::gapEventHandler);
219+
if (errRc != ESP_OK) {
220+
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
221+
return;
222+
}
208223

209-
errRc = esp_ble_gap_register_callback(BLEDevice::gapEventHandler);
210-
if (errRc != ESP_OK) {
211-
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
212-
return;
213-
}
224+
errRc = esp_ble_gattc_register_callback(BLEDevice::gattClientEventHandler);
225+
if (errRc != ESP_OK) {
226+
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
227+
return;
228+
}
214229

215-
errRc = esp_ble_gattc_register_callback(BLEDevice::gattClientEventHandler);
216-
if (errRc != ESP_OK) {
217-
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
218-
return;
219-
}
230+
errRc = esp_ble_gatts_register_callback(BLEDevice::gattServerEventHandler);
231+
if (errRc != ESP_OK) {
232+
ESP_LOGE(LOG_TAG, "esp_ble_gatts_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
233+
return;
234+
}
220235

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

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