h2zero / NimBLE-Arduino

A fork of the NimBLE library structured for compilation with Arduino, for use with ESP32, nRF5x.
https://h2zero.github.io/NimBLE-Arduino/
Apache License 2.0
687 stars 141 forks source link

How to set address type RANDOM #406

Closed paulhamsh closed 2 months ago

paulhamsh commented 2 years ago

Hi I have a device I am trying to emulate and in Wireshark scan response and advertising response it shows as having TxAdd: RANDOM with address 0xdb, 0xab, 0x7f, 0xe0, 0x2f, 0x5c. How can I change the settings on the ESP32 to achieve that? I don't need a random address, I just want it to show as RANDOM, not as PUBLIC.

I think it can only be done with esp calls, but the code below seems not to work - either the call to esp_ble_gap_set_rand_addr or esp_ble_gap_config_local_privacy The esp_base_mac_addr_set doesn't fail, the other two do. But in all cases it stops Lightblue from scanning the ESP32. I'm not sure where in the code to put the esp calls, either.

BLEDevice::init("XXXXXXXX"); // 
BLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db */

esp_bd_addr_t addr = {0xdb, 0xab, 0x7f, 0xe0, 0x2f,  0x5c}; 

// if (esp_ble_gap_set_rand_addr(addr) != ESP_OK) { Heltec.display->clear(); Heltec.display->drawString(0, 30, "FAIL RAND"); Heltec.display->display();}; // if (esp_ble_gap_config_local_privacy(true) != ESP_OK) { Heltec.display->clear(); Heltec.display->drawString(0, 30, "FAIL PRIV"); Heltec.display->display();}; if (esp_base_mac_addr_set(addr) != ESP_OK) { Heltec.display->clear(); Heltec.display->drawString(0, 30, "FAIL MAC"); Heltec.display->display();}; // esp_ble_gap_clear_rand_addr();

pSCServer = BLEDevice::createServer();
pSCServer->setCallbacks(new SCServerCallbacks());
h2zero commented 2 years ago

There is no API to do this but I believe if you modify the library slightly it may work.

change https://github.com/h2zero/NimBLE-Arduino/blob/8ff8f0fc35afed5db8276bd2f41afce22732e1f7/src/NimBLEDevice.cpp#L87 to BLE_OWN_ADDR_RANDOM

paulhamsh commented 2 years ago

When I do that it doesn't show in LightBlue scan - just vanishes. Should I change the address as well to meet the 'random' spec?

paulhamsh commented 2 years ago

What is really weird is if I make that change, load it, then undo the change, and re-load it - it works with the app that I needed it to work with, Without making the change it needs LightBlue (or other) to scan / connect first time then it is fine, with this change it doesn't need that. And it doesn't seem to be a ios caching thing because I just erased the iphone and it works fine. So it seems to make a difference to the ESP to have, at some point, been run with the RANDOM setting. Could that be true?

Update: tried on M5Stack and not the same effect, so perhaps just a fluke

h2zero commented 2 years ago

Interesting, unfortunately the way the esp32 handles the address and address type makes it somewhat difficult due to needing host based privacy. In addition to that the functions needed in NimBLE to set this aren't available without some code hacks to the core files.

If you're adventurous you can walk through the code call stack of NimBLEDevice::setOwnAddrType to find the required calls to set the address and type there.

paulhamsh commented 2 years ago

It looks like it works if you change the NimBLEDevice.cpp as you suggested, and also add the following, just before advertising:

    ble_addr_t blead;
    int rc;

    rc = ble_hs_id_gen_rnd(1, &blead);
    if (rc != 0) Serial.println("Rand failed");

    rc = ble_hs_id_set_rnd(blead.val);
    if (rc != 0) Serial.println("Addr failed");

    pAdvertising->setScanResponse(true);
    pAdvertising->start();

Thank you for your help!!!

h2zero commented 2 years ago

Ha, well done! I thought you were trying to set a specific address not just a random address of type random?

Awesome that this worked :smile:

paulhamsh commented 2 years ago

I thought I needed to (or, at least, that that would be easier) but this worked fine.

On 22 May 2022, at 02:38, h2zero @.***> wrote:

 Ha, well done! I thought you were trying to set a specific address not just a random address of type random?

Awesome that this worked 😄

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.

h2zero commented 2 months ago

This functionality has been added in the master branch.

paulhamsh commented 2 weeks ago

Hi - can you tell me where it was added? I'd love to see what the new code is.

h2zero commented 1 week ago

@paulhamsh, the function is here https://github.com/h2zero/NimBLE-Arduino/blob/b6a169f1f2e939ce0ca11d003cc38ef141698d6a/src/NimBLEDevice.cpp#L1106