Closed chegewara closed 6 years ago
In examples that are available this is only example security callback:
class MySecurity : public BLESecurityCallbacks {
bool onConfirmPIN(unsigned int pin){
return true;
}
uint32_t onPassKeyRequest(){
ESP_LOGI(LOG_TAG, "PassKeyRequest");
return 123456;
}
void onPassKeyNotify(uint32_t pass_key){
ESP_LOGI(LOG_TAG, "On passkey Notify number:%d", pass_key);
}
bool onSecurityRequest(){
ESP_LOGI(LOG_TAG, "On Security Request");
return true;
}
void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){
ESP_LOGI(LOG_TAG, "Starting BLE work!");
if(cmpl.success){
uint16_t length;
esp_ble_gap_get_whitelist_size(&length);
ESP_LOGD(LOG_TAG, "size: %d", length);
}
}
};
For test purpose you can change this:
bool onSecurityRequest(){
ESP_LOGI(LOG_TAG, "On Security Request");
return false; // <--- reject all security requests
}
This is full example to show how to protect characteristic and CCC descriptor with access permission: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/security/SampleServer_authorization.cpp
I confirmed that I can't send any data from my PC to the dev board (write to characteristic). It is protected and it throws There was a GATT communication protocol error.
message on the PC, which is perfect. I have a little problem with my dev board at this point, and I can't upload a new sketch. Once I solve it, I'll update you on the progress. Thanks.
Let's forget about receiving any data on the Arduino server side, it works for me as it should and I'm good. Let's only focus on sending the data (Arduino -> Windows 10). The way I send the data now is through notifications, which my client on Windows PC is subscribed to. However, with the example that you provided, how is it that the data is send? I see that the notifications flag is set on BLE2902, but when does it trigger a send? Right now, in my code I have Characteristic->setValue(x, x);
and Characteristic->notify();
, but I don't see the notify()
anywhere in the example. Thanks.
Well, which one example you refer to? Sorry, but im a little bit lost in tracking all examples. In this example there is no notify, because it is no sending any notifications (its just not implemented here): https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/security/SampleServer_authorization.cpp
You can have in your code Characteristic->notify();
wherever you want/need it.
Got it. I've changed the code that you've pointed out:
ESP_LOGI(LOG_TAG, "On Security Request");
return false; // <--- reject all security requests
}
but it's still sending the characteristic information to the Windows 10 app. Like I mentioned before, when Windows 10 tries to send data to the esp32 dev board, it is blocked, which is exactly what I want for sending characteristic as well. I've also changed
Characteristic->addDescriptor(new BLE2902());
to
BLE2902* p2902Descriptor = new BLE2902();
p2902Descriptor->setNotifications(true);
pCharacteristic->addDescriptor(p2902Descriptor);
The example that you mentioned in the above comment, my code follows it.
Logs:
[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_CONNECT_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLEUtils.cpp:1707] dumpGattServerEvent(): [conn_id: 0, remote_bda: 54:27:1e:f4:95:2b]
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEServer.cpp:295] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_MTU_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_MTU_EVT
[D][BLEUtils.cpp:1766] dumpGattServerEvent(): [conn_id: 0, mtu: 512]
[I][BLEDevice.cpp:113] gattServerEventHandler(): ESP_GATTS_MTU_EVT, MTU 512
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEServer.cpp:295] handleGATTServerEvent(): << handleGATTServerEvent
**********************************
-288 -104 14708 2 -230 102
packet type: 0
packet_ID: 0
sizeof(packet): 21
[D][BLECharacteristic.cpp:664] setValue(): >> setValue: length=21, data=000000000000000000e0fe98ff743902001aff6600, characteristic UUID=6e400003-b5a3-f393-e0a9-e50e24dcca9e
[D][BLECharacteristic.cpp:671] setValue(): << setValue
[D][BLECharacteristic.cpp:524] notify(): >> notify: length: 21
[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_CONF_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_CONF_EVT
[D][BLEUtils.cpp:1692] dumpGattServerEvent(): [status: ESP_GATT_OK, conn_id: 0x00]
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONF_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONF_EVT
[D][BLECharacteristic.cpp:566] notify(): << notify
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONF_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEServer.cpp:295] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_WRITE_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_WRITE_EVT
[D][BLEUtils.cpp:1832] dumpGattServerEvent(): [conn_id: 0, trans_id: 1, bda: 54:27:1e:f4:95:2b, handle: 0x2b, offset: 0, need_rsp: 1, is_prep: 0, len: 2]
[D][BLEUtils.cpp:1834] dumpGattServerEvent(): [Data: 0100]
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_WRITE_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_WRITE_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_WRITE_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEServer.cpp:295] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_RESPONSE_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_RESPONSE_EVT
[D][BLEUtils.cpp:1784] dumpGattServerEvent(): [status: ESP_GATT_OK, handle: 0x2b]
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_RESPONSE_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_RESPONSE_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_RESPONSE_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEServer.cpp:295] handleGATTServerEvent(): << handleGATTServerEvent
Connected!
[D][BLEAdvertising.cpp:241] stop(): >> stop
[D][BLEAdvertising.cpp:247] stop(): << stop
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT
[D][BLEUtils.cpp:1135] dumpGapEvent(): [status: 0]
[D][BLEServer.cpp:135] handleGAPEvent(): BLEServer ... handling GAP event!
**********************************
-256 -148 14656 1 -217 121
packet type: 0
packet_ID: 1
sizeof(packet): 21
[D][BLECharacteristic.cpp:664] setValue(): >> setValue: length=21, data=00010000000000000000ff6cff4039010027ff7900, characteristic UUID=6e400003-b5a3-f393-e0a9-e50e24dcca9e
[D][BLECharacteristic.cpp:671] setValue(): << setValue
[D][BLECharacteristic.cpp:524] notify(): >> notify: length: 21
[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_CONF_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_CONF_EVT
[D][BLEUtils.cpp:1692] dumpGattServerEvent(): [status: ESP_GATT_OK, conn_id: 0x00]
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONF_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONF_EVT
[D][BLECharacteristic.cpp:566] notify(): << notify
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONF_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEServer.cpp:295] handleGATTServerEvent(): << handleGATTServerEvent
Thanks.
Edit: I can read the characteristics that are send from the dev board on my phone as well.
Just in case, if you could erase flash each time before you re-flash esp32, or at least if you are not doing this, to delete esp32 from paired device in windows settings. esp32 is saving paired peer devices and this can cause issue. Even if you re-flash esp32 with new code there is still old pairing data stored on device, and because i dont see key exchange logs this makes me concerned you are each time making test when your windows pc is bonded with esp32. Im working on my own project now, but i will try to think how to connect esp32 with my laptop and read/write characteristic. If you have some windows app this would be helpful.
I am always deleting the esp32 from paired devices in Windows when I test. I never actually re-flashed esp32 on my dev board, I have to find out how it's done. Thansk.
By re-flash i mean you upload new app to esp32 from arduino or vs code.
Oh yeah, I always make sure to do that too.
There is few levels of protection in BLE SM. All is very good described here: https://github.com/espressif/esp-idf/blob/master/examples/bluetooth/gatt_security_server/tutorial/Gatt_Security_Server_Example_Walkthrough.md
C++ class in cpp_utils is just layer on top of it to easy usage.